aboutsummaryrefslogtreecommitdiffstats
path: root/vm_insnhelper.c
diff options
context:
space:
mode:
authoreileencodes <eileencodes@gmail.com>2023-06-02 14:25:19 -0400
committerAaron Patterson <aaron.patterson@gmail.com>2023-06-05 11:11:12 -0700
commit40f090f4339820d19da8ecdf81a981489c22eb57 (patch)
treeee8f90b5d49c2bb6cc44f135e3638fcd9599722e /vm_insnhelper.c
parent29ea3c5aaa9513fcd497781e7051c0f436725eb3 (diff)
downloadruby-40f090f4339820d19da8ecdf81a981489c22eb57.tar.gz
Revert "Revert "Fix cvar caching when class is cloned""
This reverts commit 10621f7cb9a0c70e568f89cce47a02e878af6778. This was reverted because the gc integrity build started failing. We have figured out a fix so I'm reopening the PR. Original commit message: Fix cvar caching when class is cloned The class variable cache that was added in ruby#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>
Diffstat (limited to 'vm_insnhelper.c')
-rw-r--r--vm_insnhelper.c20
1 files changed, 12 insertions, 8 deletions
diff --git a/vm_insnhelper.c b/vm_insnhelper.c
index 9407a75981..5ef1ad10de 100644
--- a/vm_insnhelper.c
+++ b/vm_insnhelper.c
@@ -1491,7 +1491,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);
@@ -1511,9 +1511,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;
@@ -1523,8 +1527,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);
@@ -1533,10 +1538,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
@@ -1549,20 +1553,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