Thanks to visit codestin.com
Credit goes to github.com

Skip to content

YJIT: call alloc func directly in new #13219

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion yjit/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4912,7 +4912,16 @@ fn gen_opt_new(
// Fast path
// call rb_class_alloc to actually allocate
jit_prepare_non_leaf_call(jit, asm);
let obj = asm.ccall(rb_obj_alloc as _, vec![comptime_recv.into()]);

let allocator =
if unsafe { FL_TEST(comptime_recv, VALUE(RUBY_FL_SINGLETON as usize)) } != VALUE(0) {
None
} else {
unsafe { rb_get_alloc_func(comptime_recv) }
};

let allocator = allocator.unwrap_or(rb_obj_alloc);
let obj = asm.ccall(allocator as _, vec![comptime_recv.into()]);
Copy link
Member

@k0kubun k0kubun Apr 30, 2025

Choose a reason for hiding this comment

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

When an allocator function is found, it seems to skip the if (rb_obj_class(obj) != rb_class_real(klass)) { rb_raise(rb_eTypeError, "wrong instance allocation"); } check in class_call_alloc_func. Is the error unreachable?

Copy link
Member

@tenderlove tenderlove Apr 30, 2025

Choose a reason for hiding this comment

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

Wouldn't this only be a problem for T_DATA objects? I don't think they use rb_class_allocate_instance as their allocator.

Copy link
Member Author

@jhawthorn jhawthorn Apr 30, 2025

Choose a reason for hiding this comment

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

I think it's only reachable from a custom allocator which is implemented incorrectly (there's no reason to ever return an object with the wrong class), so I'm not sure if it's necessary to keep.

If it is I suppose we could allowlist just the most common builtin allocators (or even just the T_OBJECT allocator).

Copy link
Member

Choose a reason for hiding this comment

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

I honestly didn't know when it happens (it'd be nice to have a comment there) and thus asked a question. If the check is useless (on opt_new's rb_obj_alloc call) on the interpreter too, it's fine.


// Get a reference to the stack location where we need to save the
// return instance.
Expand Down
Loading