Skip to content

Commit 8405395

Browse files
committed
ZJIT: Fix crash when Kernel is prepended before JIT init
Use RCLASS_ORIGIN when looking up methods to register for specialized codegen. Module#prepend moves original methods to an origin iclass, so rb_method_entry_at on the module itself returns NULL after prepend. This happens in practice when BUNDLER_SETUP triggers Kernel.prepend during ruby_init_prelude, before rb_zjit_init runs.
1 parent ed9b577 commit 8405395

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

zjit/src/cruby_methods.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ fn annotate_c_method(props_map: &mut HashMap<*mut c_void, FnProperties>, class:
7878
// TODO(alan): (side quest) make rust methods and clean up glue code for rb_method_cfunc_t and
7979
// rb_method_definition_t.
8080
let method_id = rb_intern2(method_name.as_ptr().cast(), method_name.len() as _);
81-
let method = rb_method_entry_at(class, method_id);
81+
// Use RCLASS_ORIGIN to find methods that were moved by Module#prepend
82+
let method = rb_method_entry_at(RCLASS_ORIGIN(class), method_id);
8283
assert!(!method.is_null());
8384
// ME-to-CME cast is fine due to identical layout
8485
debug_assert_eq!(VM_METHOD_TYPE_CFUNC, get_cme_def_type(method.cast()));
@@ -93,7 +94,8 @@ fn annotate_c_method(props_map: &mut HashMap<*mut c_void, FnProperties>, class:
9394
fn annotate_builtin_method(props_map: &mut HashMap<*mut c_void, FnProperties>, class: VALUE, method_name: &'static str, props: FnProperties) {
9495
unsafe {
9596
let method_id = rb_intern2(method_name.as_ptr().cast(), method_name.len().try_into().unwrap());
96-
let method = rb_method_entry_at(class, method_id);
97+
// Use RCLASS_ORIGIN to find methods that were moved by Module#prepend
98+
let method = rb_method_entry_at(RCLASS_ORIGIN(class), method_id);
9799
if method.is_null() {
98100
panic!("Method {}#{} not found", std::ffi::CStr::from_ptr(rb_class2name(class)).to_str().unwrap_or("?"), method_name);
99101
}

0 commit comments

Comments
 (0)