diff options
| author | eileencodes <eileencodes@gmail.com> | 2023-02-07 15:46:50 -0500 |
|---|---|---|
| committer | nagachika <nagachika@ruby-lang.org> | 2023-07-01 14:17:30 +0900 |
| commit | 8a3d57971c99680d4baec84553247b9c6ee41080 (patch) | |
| tree | 46800a87d7f72c09087deaa37c504c005a187fd6 | |
| parent | 06dae46036316e6e9926c4613ac8058b78eb7f2e (diff) | |
Fix cvar caching when class is cloned
The class variable cache that was added in
https://github.com/ruby/ruby/pull/4544 changed the behavior of class
variables on cloned classes. As reported when a class is cloned AND a
class variable was set, and the class variable was read from the
original class, reading a class variable from the cloned class would
return the value from the original class.
This was happening because the IC (inline cache) is stored on the ISEQ
which is shared between the original and cloned class, therefore they
share the cache too.
To fix this we are now storing the `cref` in the cache so that we can
check if it's equal to the current `cref`. If it's different we don't
want to read from the cache. If it's the same we do. Cloned classes
don't share the same cref with their original class.
This will need to be backported to 3.1 in addition to 3.2 since the bug
exists in both versions.
We also added a marking function which was missing.
Fixes [Bug #19379]
Co-authored-by: Aaron Patterson <tenderlove@ruby-lang.org>
| -rw-r--r-- | class.c | 3 | ||||
| -rw-r--r-- | common.mk | 58 | ||||
| -rw-r--r-- | ext/objspace/depend | 14 | ||||
| -rw-r--r-- | gc.c | 30 | ||||
| -rw-r--r-- | internal/class.h | 4 | ||||
| -rw-r--r-- | test/ruby/test_variable.rb | 13 | ||||
| -rw-r--r-- | variable.c | 1 | ||||
| -rw-r--r-- | vm_insnhelper.c | 20 | ||||
| -rw-r--r-- | yjit/src/cruby_bindings.inc.rs | 1 |
9 files changed, 136 insertions, 8 deletions
@@ -419,9 +419,12 @@ cvc_table_copy(ID id, VALUE val, void *data) { ent = ALLOC(struct rb_cvar_class_tbl_entry); ent->class_value = ctx->clone; + ent->cref = orig_entry->cref; ent->global_cvar_state = orig_entry->global_cvar_state; rb_id_table_insert(ctx->new_table, id, (VALUE)ent); + RB_OBJ_WRITTEN(ctx->clone, Qundef, ent->cref); + return ID_TABLE_CONTINUE; } @@ -3220,8 +3220,13 @@ compile.$(OBJEXT): {$(VPATH)}vm_callinfo.h compile.$(OBJEXT): {$(VPATH)}vm_core.h compile.$(OBJEXT): {$(VPATH)}vm_debug.h compile.$(OBJEXT): {$(VPATH)}vm_opts.h +complex.$(OBJEXT): $(CCAN_DIR)/check_type/check_type.h +complex.$(OBJEXT): $(CCAN_DIR)/container_of/container_of.h +complex.$(OBJEXT): $(CCAN_DIR)/list/list.h +complex.$(OBJEXT): $(CCAN_DIR)/str/str.h complex.$(OBJEXT): $(hdrdir)/ruby/ruby.h complex.$(OBJEXT): $(top_srcdir)/internal/array.h +complex.$(OBJEXT): $(top_srcdir)/internal/basic_operators.h complex.$(OBJEXT): $(top_srcdir)/internal/bignum.h complex.$(OBJEXT): $(top_srcdir)/internal/bits.h complex.$(OBJEXT): $(top_srcdir)/internal/class.h @@ -3229,6 +3234,7 @@ complex.$(OBJEXT): $(top_srcdir)/internal/compilers.h complex.$(OBJEXT): $(top_srcdir)/internal/complex.h complex.$(OBJEXT): $(top_srcdir)/internal/fixnum.h complex.$(OBJEXT): $(top_srcdir)/internal/gc.h +complex.$(OBJEXT): $(top_srcdir)/internal/imemo.h complex.$(OBJEXT): $(top_srcdir)/internal/math.h complex.$(OBJEXT): $(top_srcdir)/internal/numeric.h complex.$(OBJEXT): $(top_srcdir)/internal/object.h @@ -3239,6 +3245,7 @@ complex.$(OBJEXT): $(top_srcdir)/internal/variable.h complex.$(OBJEXT): $(top_srcdir)/internal/vm.h complex.$(OBJEXT): $(top_srcdir)/internal/warnings.h complex.$(OBJEXT): {$(VPATH)}assert.h +complex.$(OBJEXT): {$(VPATH)}atomic.h complex.$(OBJEXT): {$(VPATH)}backward/2/assume.h complex.$(OBJEXT): {$(VPATH)}backward/2/attributes.h complex.$(OBJEXT): {$(VPATH)}backward/2/bool.h @@ -3396,11 +3403,18 @@ complex.$(OBJEXT): {$(VPATH)}internal/value_type.h complex.$(OBJEXT): {$(VPATH)}internal/variable.h complex.$(OBJEXT): {$(VPATH)}internal/warning_push.h complex.$(OBJEXT): {$(VPATH)}internal/xmalloc.h +complex.$(OBJEXT): {$(VPATH)}method.h complex.$(OBJEXT): {$(VPATH)}missing.h +complex.$(OBJEXT): {$(VPATH)}node.h complex.$(OBJEXT): {$(VPATH)}ruby_assert.h +complex.$(OBJEXT): {$(VPATH)}ruby_atomic.h complex.$(OBJEXT): {$(VPATH)}shape.h complex.$(OBJEXT): {$(VPATH)}st.h complex.$(OBJEXT): {$(VPATH)}subst.h +complex.$(OBJEXT): {$(VPATH)}thread_$(THREAD_MODEL).h +complex.$(OBJEXT): {$(VPATH)}thread_native.h +complex.$(OBJEXT): {$(VPATH)}vm_core.h +complex.$(OBJEXT): {$(VPATH)}vm_opts.h cont.$(OBJEXT): $(CCAN_DIR)/check_type/check_type.h cont.$(OBJEXT): $(CCAN_DIR)/container_of/container_of.h cont.$(OBJEXT): $(CCAN_DIR)/list/list.h @@ -5889,8 +5903,13 @@ enum.$(OBJEXT): {$(VPATH)}st.h enum.$(OBJEXT): {$(VPATH)}subst.h enum.$(OBJEXT): {$(VPATH)}symbol.h enum.$(OBJEXT): {$(VPATH)}util.h +enumerator.$(OBJEXT): $(CCAN_DIR)/check_type/check_type.h +enumerator.$(OBJEXT): $(CCAN_DIR)/container_of/container_of.h +enumerator.$(OBJEXT): $(CCAN_DIR)/list/list.h +enumerator.$(OBJEXT): $(CCAN_DIR)/str/str.h enumerator.$(OBJEXT): $(hdrdir)/ruby/ruby.h enumerator.$(OBJEXT): $(top_srcdir)/internal/array.h +enumerator.$(OBJEXT): $(top_srcdir)/internal/basic_operators.h enumerator.$(OBJEXT): $(top_srcdir)/internal/bignum.h enumerator.$(OBJEXT): $(top_srcdir)/internal/bits.h enumerator.$(OBJEXT): $(top_srcdir)/internal/class.h @@ -5911,6 +5930,7 @@ enumerator.$(OBJEXT): $(top_srcdir)/internal/struct.h enumerator.$(OBJEXT): $(top_srcdir)/internal/vm.h enumerator.$(OBJEXT): $(top_srcdir)/internal/warnings.h enumerator.$(OBJEXT): {$(VPATH)}assert.h +enumerator.$(OBJEXT): {$(VPATH)}atomic.h enumerator.$(OBJEXT): {$(VPATH)}backward/2/assume.h enumerator.$(OBJEXT): {$(VPATH)}backward/2/attributes.h enumerator.$(OBJEXT): {$(VPATH)}backward/2/bool.h @@ -6077,13 +6097,20 @@ enumerator.$(OBJEXT): {$(VPATH)}internal/value_type.h enumerator.$(OBJEXT): {$(VPATH)}internal/variable.h enumerator.$(OBJEXT): {$(VPATH)}internal/warning_push.h enumerator.$(OBJEXT): {$(VPATH)}internal/xmalloc.h +enumerator.$(OBJEXT): {$(VPATH)}method.h enumerator.$(OBJEXT): {$(VPATH)}missing.h +enumerator.$(OBJEXT): {$(VPATH)}node.h enumerator.$(OBJEXT): {$(VPATH)}onigmo.h enumerator.$(OBJEXT): {$(VPATH)}oniguruma.h enumerator.$(OBJEXT): {$(VPATH)}ruby_assert.h +enumerator.$(OBJEXT): {$(VPATH)}ruby_atomic.h enumerator.$(OBJEXT): {$(VPATH)}shape.h enumerator.$(OBJEXT): {$(VPATH)}st.h enumerator.$(OBJEXT): {$(VPATH)}subst.h +enumerator.$(OBJEXT): {$(VPATH)}thread_$(THREAD_MODEL).h +enumerator.$(OBJEXT): {$(VPATH)}thread_native.h +enumerator.$(OBJEXT): {$(VPATH)}vm_core.h +enumerator.$(OBJEXT): {$(VPATH)}vm_opts.h error.$(OBJEXT): $(CCAN_DIR)/check_type/check_type.h error.$(OBJEXT): $(CCAN_DIR)/container_of/container_of.h error.$(OBJEXT): $(CCAN_DIR)/list/list.h @@ -8889,8 +8916,13 @@ main.$(OBJEXT): {$(VPATH)}missing.h main.$(OBJEXT): {$(VPATH)}st.h main.$(OBJEXT): {$(VPATH)}subst.h main.$(OBJEXT): {$(VPATH)}vm_debug.h +marshal.$(OBJEXT): $(CCAN_DIR)/check_type/check_type.h +marshal.$(OBJEXT): $(CCAN_DIR)/container_of/container_of.h +marshal.$(OBJEXT): $(CCAN_DIR)/list/list.h +marshal.$(OBJEXT): $(CCAN_DIR)/str/str.h marshal.$(OBJEXT): $(hdrdir)/ruby/ruby.h marshal.$(OBJEXT): $(top_srcdir)/internal/array.h +marshal.$(OBJEXT): $(top_srcdir)/internal/basic_operators.h marshal.$(OBJEXT): $(top_srcdir)/internal/bignum.h marshal.$(OBJEXT): $(top_srcdir)/internal/bits.h marshal.$(OBJEXT): $(top_srcdir)/internal/class.h @@ -8900,6 +8932,7 @@ marshal.$(OBJEXT): $(top_srcdir)/internal/error.h marshal.$(OBJEXT): $(top_srcdir)/internal/fixnum.h marshal.$(OBJEXT): $(top_srcdir)/internal/gc.h marshal.$(OBJEXT): $(top_srcdir)/internal/hash.h +marshal.$(OBJEXT): $(top_srcdir)/internal/imemo.h marshal.$(OBJEXT): $(top_srcdir)/internal/numeric.h marshal.$(OBJEXT): $(top_srcdir)/internal/object.h marshal.$(OBJEXT): $(top_srcdir)/internal/serial.h @@ -8912,6 +8945,7 @@ marshal.$(OBJEXT): $(top_srcdir)/internal/variable.h marshal.$(OBJEXT): $(top_srcdir)/internal/vm.h marshal.$(OBJEXT): $(top_srcdir)/internal/warnings.h marshal.$(OBJEXT): {$(VPATH)}assert.h +marshal.$(OBJEXT): {$(VPATH)}atomic.h marshal.$(OBJEXT): {$(VPATH)}backward/2/assume.h marshal.$(OBJEXT): {$(VPATH)}backward/2/attributes.h marshal.$(OBJEXT): {$(VPATH)}backward/2/bool.h @@ -8927,6 +8961,7 @@ marshal.$(OBJEXT): {$(VPATH)}constant.h marshal.$(OBJEXT): {$(VPATH)}defines.h marshal.$(OBJEXT): {$(VPATH)}encindex.h marshal.$(OBJEXT): {$(VPATH)}encoding.h +marshal.$(OBJEXT): {$(VPATH)}id.h marshal.$(OBJEXT): {$(VPATH)}id_table.h marshal.$(OBJEXT): {$(VPATH)}intern.h marshal.$(OBJEXT): {$(VPATH)}internal.h @@ -9082,13 +9117,21 @@ marshal.$(OBJEXT): {$(VPATH)}internal/xmalloc.h marshal.$(OBJEXT): {$(VPATH)}io.h marshal.$(OBJEXT): {$(VPATH)}marshal.c marshal.$(OBJEXT): {$(VPATH)}marshal.rbinc +marshal.$(OBJEXT): {$(VPATH)}method.h marshal.$(OBJEXT): {$(VPATH)}missing.h +marshal.$(OBJEXT): {$(VPATH)}node.h marshal.$(OBJEXT): {$(VPATH)}onigmo.h marshal.$(OBJEXT): {$(VPATH)}oniguruma.h +marshal.$(OBJEXT): {$(VPATH)}ruby_assert.h +marshal.$(OBJEXT): {$(VPATH)}ruby_atomic.h marshal.$(OBJEXT): {$(VPATH)}shape.h marshal.$(OBJEXT): {$(VPATH)}st.h marshal.$(OBJEXT): {$(VPATH)}subst.h +marshal.$(OBJEXT): {$(VPATH)}thread_$(THREAD_MODEL).h +marshal.$(OBJEXT): {$(VPATH)}thread_native.h marshal.$(OBJEXT): {$(VPATH)}util.h +marshal.$(OBJEXT): {$(VPATH)}vm_core.h +marshal.$(OBJEXT): {$(VPATH)}vm_opts.h math.$(OBJEXT): $(hdrdir)/ruby/ruby.h math.$(OBJEXT): $(top_srcdir)/internal/bignum.h math.$(OBJEXT): $(top_srcdir)/internal/class.h @@ -10480,8 +10523,13 @@ numeric.$(OBJEXT): {$(VPATH)}shape.h numeric.$(OBJEXT): {$(VPATH)}st.h numeric.$(OBJEXT): {$(VPATH)}subst.h numeric.$(OBJEXT): {$(VPATH)}util.h +object.$(OBJEXT): $(CCAN_DIR)/check_type/check_type.h +object.$(OBJEXT): $(CCAN_DIR)/container_of/container_of.h +object.$(OBJEXT): $(CCAN_DIR)/list/list.h +object.$(OBJEXT): $(CCAN_DIR)/str/str.h object.$(OBJEXT): $(hdrdir)/ruby/ruby.h object.$(OBJEXT): $(top_srcdir)/internal/array.h +object.$(OBJEXT): $(top_srcdir)/internal/basic_operators.h object.$(OBJEXT): $(top_srcdir)/internal/bignum.h object.$(OBJEXT): $(top_srcdir)/internal/bits.h object.$(OBJEXT): $(top_srcdir)/internal/class.h @@ -10490,6 +10538,7 @@ object.$(OBJEXT): $(top_srcdir)/internal/error.h object.$(OBJEXT): $(top_srcdir)/internal/eval.h object.$(OBJEXT): $(top_srcdir)/internal/fixnum.h object.$(OBJEXT): $(top_srcdir)/internal/gc.h +object.$(OBJEXT): $(top_srcdir)/internal/imemo.h object.$(OBJEXT): $(top_srcdir)/internal/inits.h object.$(OBJEXT): $(top_srcdir)/internal/numeric.h object.$(OBJEXT): $(top_srcdir)/internal/object.h @@ -10502,6 +10551,7 @@ object.$(OBJEXT): $(top_srcdir)/internal/variable.h object.$(OBJEXT): $(top_srcdir)/internal/vm.h object.$(OBJEXT): $(top_srcdir)/internal/warnings.h object.$(OBJEXT): {$(VPATH)}assert.h +object.$(OBJEXT): {$(VPATH)}atomic.h object.$(OBJEXT): {$(VPATH)}backward/2/assume.h object.$(OBJEXT): {$(VPATH)}backward/2/attributes.h object.$(OBJEXT): {$(VPATH)}backward/2/bool.h @@ -10670,18 +10720,26 @@ object.$(OBJEXT): {$(VPATH)}internal/variable.h object.$(OBJEXT): {$(VPATH)}internal/warning_push.h object.$(OBJEXT): {$(VPATH)}internal/xmalloc.h object.$(OBJEXT): {$(VPATH)}kernel.rbinc +object.$(OBJEXT): {$(VPATH)}method.h object.$(OBJEXT): {$(VPATH)}missing.h object.$(OBJEXT): {$(VPATH)}nilclass.rbinc +object.$(OBJEXT): {$(VPATH)}node.h object.$(OBJEXT): {$(VPATH)}object.c object.$(OBJEXT): {$(VPATH)}onigmo.h object.$(OBJEXT): {$(VPATH)}oniguruma.h object.$(OBJEXT): {$(VPATH)}probes.dmyh object.$(OBJEXT): {$(VPATH)}probes.h +object.$(OBJEXT): {$(VPATH)}ruby_assert.h +object.$(OBJEXT): {$(VPATH)}ruby_atomic.h object.$(OBJEXT): {$(VPATH)}shape.h object.$(OBJEXT): {$(VPATH)}st.h object.$(OBJEXT): {$(VPATH)}subst.h +object.$(OBJEXT): {$(VPATH)}thread_$(THREAD_MODEL).h +object.$(OBJEXT): {$(VPATH)}thread_native.h object.$(OBJEXT): {$(VPATH)}util.h object.$(OBJEXT): {$(VPATH)}variable.h +object.$(OBJEXT): {$(VPATH)}vm_core.h +object.$(OBJEXT): {$(VPATH)}vm_opts.h pack.$(OBJEXT): $(hdrdir)/ruby/ruby.h pack.$(OBJEXT): $(top_srcdir)/internal/array.h pack.$(OBJEXT): $(top_srcdir)/internal/bits.h diff --git a/ext/objspace/depend b/ext/objspace/depend index de5fa6c6a3..e76cfa963f 100644 --- a/ext/objspace/depend +++ b/ext/objspace/depend @@ -164,6 +164,7 @@ object_tracing.o: objspace.h objspace.o: $(RUBY_EXTCONF_H) objspace.o: $(arch_hdrdir)/ruby/config.h objspace.o: $(hdrdir)/ruby/assert.h +objspace.o: $(hdrdir)/ruby/atomic.h objspace.o: $(hdrdir)/ruby/backward.h objspace.o: $(hdrdir)/ruby/backward/2/assume.h objspace.o: $(hdrdir)/ruby/backward/2/attributes.h @@ -336,10 +337,16 @@ objspace.o: $(hdrdir)/ruby/regex.h objspace.o: $(hdrdir)/ruby/ruby.h objspace.o: $(hdrdir)/ruby/st.h objspace.o: $(hdrdir)/ruby/subst.h +objspace.o: $(hdrdir)/ruby/thread_native.h +objspace.o: $(top_srcdir)/ccan/check_type/check_type.h +objspace.o: $(top_srcdir)/ccan/container_of/container_of.h +objspace.o: $(top_srcdir)/ccan/list/list.h +objspace.o: $(top_srcdir)/ccan/str/str.h objspace.o: $(top_srcdir)/gc.h objspace.o: $(top_srcdir)/id_table.h objspace.o: $(top_srcdir)/internal.h objspace.o: $(top_srcdir)/internal/array.h +objspace.o: $(top_srcdir)/internal/basic_operators.h objspace.o: $(top_srcdir)/internal/class.h objspace.o: $(top_srcdir)/internal/compilers.h objspace.o: $(top_srcdir)/internal/gc.h @@ -348,10 +355,17 @@ objspace.o: $(top_srcdir)/internal/imemo.h objspace.o: $(top_srcdir)/internal/sanitizers.h objspace.o: $(top_srcdir)/internal/serial.h objspace.o: $(top_srcdir)/internal/static_assert.h +objspace.o: $(top_srcdir)/internal/vm.h objspace.o: $(top_srcdir)/internal/warnings.h +objspace.o: $(top_srcdir)/method.h objspace.o: $(top_srcdir)/node.h +objspace.o: $(top_srcdir)/ruby_assert.h +objspace.o: $(top_srcdir)/ruby_atomic.h objspace.o: $(top_srcdir)/shape.h objspace.o: $(top_srcdir)/symbol.h +objspace.o: $(top_srcdir)/thread_pthread.h +objspace.o: $(top_srcdir)/vm_core.h +objspace.o: $(top_srcdir)/vm_opts.h objspace.o: objspace.c objspace.o: {$(VPATH)}id.h objspace_dump.o: $(RUBY_EXTCONF_H) @@ -7209,6 +7209,8 @@ gc_mark_imemo(rb_objspace_t *objspace, VALUE obj) } } +static void mark_cvc_tbl(rb_objspace_t *objspace, VALUE klass); + static void gc_mark_children(rb_objspace_t *objspace, VALUE obj) { @@ -7255,6 +7257,7 @@ gc_mark_children(rb_objspace_t *objspace, VALUE obj) if (!RCLASS_EXT(obj)) break; mark_m_tbl(objspace, RCLASS_M_TBL(obj)); + mark_cvc_tbl(objspace, obj); cc_table_mark(objspace, obj); for (attr_index_t i = 0; i < RCLASS_IV_COUNT(obj); i++) { gc_mark(objspace, RCLASS_IVPTR(obj)[i]); @@ -10468,9 +10471,14 @@ static enum rb_id_table_iterator_result update_cvc_tbl_i(VALUE cvc_entry, void *data) { struct rb_cvar_class_tbl_entry *entry; + rb_objspace_t * objspace = (rb_objspace_t *)data; entry = (struct rb_cvar_class_tbl_entry *)cvc_entry; + if (entry->cref) { + TYPED_UPDATE_IF_MOVED(objspace, rb_cref_t *, entry->cref); + } + entry->class_value = rb_gc_location(entry->class_value); return ID_TABLE_CONTINUE; @@ -10486,6 +10494,28 @@ update_cvc_tbl(rb_objspace_t *objspace, VALUE klass) } static enum rb_id_table_iterator_result +mark_cvc_tbl_i(VALUE cvc_entry, void *data) +{ + struct rb_cvar_class_tbl_entry *entry; + + entry = (struct rb_cvar_class_tbl_entry *)cvc_entry; + + RUBY_ASSERT(entry->cref == 0 || (BUILTIN_TYPE((VALUE)entry->cref) == T_IMEMO && IMEMO_TYPE_P(entry->cref, imemo_cref))); + rb_gc_mark((VALUE) entry->cref); + + return ID_TABLE_CONTINUE; +} + +static void +mark_cvc_tbl(rb_objspace_t *objspace, VALUE klass) +{ + struct rb_id_table *tbl = RCLASS_CVC_TBL(klass); + if (tbl) { + rb_id_table_foreach_values(tbl, mark_cvc_tbl_i, objspace); + } +} + +static enum rb_id_table_iterator_result update_const_table(VALUE value, void *data) { rb_const_entry_t *ce = (rb_const_entry_t *)value; diff --git a/internal/class.h b/internal/class.h index 5731a5bc33..af83cd8a33 100644 --- a/internal/class.h +++ b/internal/class.h @@ -15,6 +15,9 @@ #include "ruby/intern.h" /* for rb_alloc_func_t */ #include "ruby/ruby.h" /* for struct RBasic */ #include "shape.h" +#include "ruby_assert.h" +#include "vm_core.h" +#include "method.h" /* for rb_cref_t */ #ifdef RCLASS_SUPER # undef RCLASS_SUPER @@ -29,6 +32,7 @@ struct rb_subclass_entry { struct rb_cvar_class_tbl_entry { uint32_t index; rb_serial_t global_cvar_state; + const rb_cref_t * cref; VALUE class_value; }; diff --git a/test/ruby/test_variable.rb b/test/ruby/test_variable.rb index 4fe80a86c8..5e8abb0734 100644 --- a/test/ruby/test_variable.rb +++ b/test/ruby/test_variable.rb @@ -49,6 +49,19 @@ class TestVariable < Test::Unit::TestCase assert_equal(1, c.class_variable_get(:@@foo)) end + Zeus = Gods.clone + + def test_cloned_allows_setting_cvar + Zeus.class_variable_set(:@@rule, "Athena") + + god = Gods.new.ruler0 + zeus = Zeus.new.ruler0 + + assert_equal "Cronus", god + assert_equal "Athena", zeus + assert_not_equal god.object_id, zeus.object_id + end + def test_singleton_class_included_class_variable c = Class.new c.extend(Olympians) diff --git a/variable.c b/variable.c index c409209117..ed74e6495e 100644 --- a/variable.c +++ b/variable.c @@ -3707,6 +3707,7 @@ rb_cvar_set(VALUE klass, ID id, VALUE val) ent = ALLOC(struct rb_cvar_class_tbl_entry); ent->class_value = target; ent->global_cvar_state = GET_GLOBAL_CVAR_STATE(); + ent->cref = 0; rb_id_table_insert(rb_cvc_tbl, id, (VALUE)ent); RB_DEBUG_COUNTER_INC(cvar_inline_miss); } diff --git a/vm_insnhelper.c b/vm_insnhelper.c index fb77b1f1cf..b7a69232c9 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -1484,7 +1484,7 @@ vm_setivar(VALUE obj, ID id, VALUE val, shape_id_t dest_shape_id, attr_index_t i } static VALUE -update_classvariable_cache(const rb_iseq_t *iseq, VALUE klass, ID id, ICVARC ic) +update_classvariable_cache(const rb_iseq_t *iseq, VALUE klass, ID id, const rb_cref_t * cref, ICVARC ic) { VALUE defined_class = 0; VALUE cvar_value = rb_cvar_find(klass, id, &defined_class); @@ -1504,9 +1504,13 @@ update_classvariable_cache(const rb_iseq_t *iseq, VALUE klass, ID id, ICVARC ic) } struct rb_cvar_class_tbl_entry *ent = (void *)ent_data; - ent->global_cvar_state = GET_GLOBAL_CVAR_STATE(); + ent->global_cvar_state = GET_GLOBAL_CVAR_STATE(); + ent->cref = cref; ic->entry = ent; + + RUBY_ASSERT(BUILTIN_TYPE((VALUE)cref) == T_IMEMO && IMEMO_TYPE_P(cref, imemo_cref)); + RB_OBJ_WRITTEN(iseq, Qundef, ent->cref); RB_OBJ_WRITTEN(iseq, Qundef, ent->class_value); return cvar_value; @@ -1516,8 +1520,9 @@ static inline VALUE vm_getclassvariable(const rb_iseq_t *iseq, const rb_control_frame_t *reg_cfp, ID id, ICVARC ic) { const rb_cref_t *cref; + cref = vm_get_cref(GET_EP()); - if (ic->entry && ic->entry->global_cvar_state == GET_GLOBAL_CVAR_STATE() && LIKELY(rb_ractor_main_p())) { + if (ic->entry && ic->entry->global_cvar_state == GET_GLOBAL_CVAR_STATE() && ic->entry->cref == cref && LIKELY(rb_ractor_main_p())) { RB_DEBUG_COUNTER_INC(cvar_read_inline_hit); VALUE v = rb_ivar_lookup(ic->entry->class_value, id, Qundef); @@ -1526,10 +1531,9 @@ vm_getclassvariable(const rb_iseq_t *iseq, const rb_control_frame_t *reg_cfp, ID return v; } - cref = vm_get_cref(GET_EP()); VALUE klass = vm_get_cvar_base(cref, reg_cfp, 1); - return update_classvariable_cache(iseq, klass, id, ic); + return update_classvariable_cache(iseq, klass, id, cref, ic); } VALUE @@ -1542,20 +1546,20 @@ static inline void vm_setclassvariable(const rb_iseq_t *iseq, const rb_control_frame_t *reg_cfp, ID id, VALUE val, ICVARC ic) { const rb_cref_t *cref; + cref = vm_get_cref(GET_EP()); - if (ic->entry && ic->entry->global_cvar_state == GET_GLOBAL_CVAR_STATE()) { + if (ic->entry && ic->entry->global_cvar_state == GET_GLOBAL_CVAR_STATE() && ic->entry->cref == cref && LIKELY(rb_ractor_main_p())) { RB_DEBUG_COUNTER_INC(cvar_write_inline_hit); rb_class_ivar_set(ic->entry->class_value, id, val); return; } - cref = vm_get_cref(GET_EP()); VALUE klass = vm_get_cvar_base(cref, reg_cfp, 1); rb_cvar_set(klass, id, val); - update_classvariable_cache(iseq, klass, id, ic); + update_classvariable_cache(iseq, klass, id, cref, ic); } void diff --git a/yjit/src/cruby_bindings.inc.rs b/yjit/src/cruby_bindings.inc.rs index c9869d3f08..f7481bbfae 100644 --- a/yjit/src/cruby_bindings.inc.rs +++ b/yjit/src/cruby_bindings.inc.rs @@ -798,6 +798,7 @@ pub type vm_frame_env_flags = u32; pub struct rb_cvar_class_tbl_entry { pub index: u32, pub global_cvar_state: rb_serial_t, + pub cref: *const rb_cref_t, pub class_value: VALUE, } pub const VM_CALL_ARGS_SPLAT_bit: vm_call_flag_bits = 0; |
