Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YJIT: Save PC and SP before calling leaf builtins #7090

Merged
merged 1 commit into from Jan 10, 2023

Conversation

XrXr
Copy link
Member

@XrXr XrXr commented Jan 9, 2023

Previously, we did not update cfp->sp before calling the C function of
ISEQs marked with Primitive.attr! "inline" (leaf builtins). This
caused the GC to miss temporary values on the stack in case the function
allocates and triggers a GC run. Right now, there is only a few leaf
builtins in numeric.rb on Integer methods such as Integer#~. Since
these methods only allocate when operating on big numbers, we missed
this issue.

Fix by saving PC and SP before calling the functions -- our usual
protocol for calling C functions that may allocate on the GC heap.

[Bug #19316]

Previously, we did not update `cfp->sp` before calling the C function of
ISEQs marked with `Primitive.attr! "inline"` (leaf builtins). This
caused the GC to miss temporary values on the stack in case the function
allocates and triggers a GC run. Right now, there is only a few leaf
builtins in numeric.rb on Integer methods such as `Integer#~`. Since
these methods only allocate when operating on big numbers, we missed
this issue.

Fix by saving PC and SP before calling the functions -- our usual
protocol for calling C functions that may allocate on the GC heap.

[Bug #19316]
@XrXr XrXr marked this pull request as ready for review January 9, 2023 23:13
@matzbot matzbot requested a review from a team January 9, 2023 23:13
Copy link
Member

@k0kubun k0kubun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like we originally had yjit_save_regs there Shopify/yjit#63, but then it was removed in Shopify/yjit#157 for switching to callee-saved registers. The fix makes sense. Thank you for fixing this.

@noahgibbs
Copy link
Contributor

Oh hey, good catch!

Copy link
Contributor

@maximecb maximecb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for catching this and I'm happpy you were able to include a regression test as well!

@maximecb maximecb merged commit aeddc19 into ruby:master Jan 10, 2023
@XrXr XrXr deleted the yjit-19316 branch January 10, 2023 17:57
nurse added a commit to nurse/ruby that referenced this pull request Jan 18, 2023
	YJIT: Save PC and SP before calling leaf builtins (ruby#7090)

	Previously, we did not update `cfp->sp` before calling the C function of
	ISEQs marked with `Primitive.attr! "inline"` (leaf builtins). This
	caused the GC to miss temporary values on the stack in case the function
	allocates and triggers a GC run. Right now, there is only a few leaf
	builtins in numeric.rb on Integer methods such as `Integer#~`. Since
	these methods only allocate when operating on big numbers, we missed
	this issue.

	Fix by saving PC and SP before calling the functions -- our usual
	protocol for calling C functions that may allocate on the GC heap.

	[Bug #19316]
	---
	 test/ruby/test_yjit.rb | 16 ++++++++++++++++
	 yjit/src/codegen.rs    |  4 ++++
	 2 files changed, 20 insertions(+)
matzbot pushed a commit that referenced this pull request Mar 21, 2023
	YJIT: Save PC and SP before calling leaf builtins (#7090)

	Previously, we did not update `cfp->sp` before calling the C function of
	ISEQs marked with `Primitive.attr! "inline"` (leaf builtins). This
	caused the GC to miss temporary values on the stack in case the function
	allocates and triggers a GC run. Right now, there is only a few leaf
	builtins in numeric.rb on Integer methods such as `Integer#~`. Since
	these methods only allocate when operating on big numbers, we missed
	this issue.

	Fix by saving PC and SP before calling the functions -- our usual
	protocol for calling C functions that may allocate on the GC heap.

	[Bug #19316]
	---
	 test/ruby/test_yjit.rb | 16 ++++++++++++++++
	 yjit/src/codegen.rs    |  4 ++++
	 2 files changed, 20 insertions(+)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants