1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[Bug #18627] Fix crash when including module

During lazy sweeping, the iclass could be a dead object that has not yet
been swept. However, the chain of superclasses of the iclass could
already have been swept (and become a new object), which would cause a
crash when trying to read the object.
This commit is contained in:
Peter Zhu 2022-03-16 15:46:40 -04:00
parent 634e0a97eb
commit 97426e15d7
Notes: git 2022-03-18 22:19:44 +09:00

12
class.c
View file

@ -1117,7 +1117,12 @@ rb_include_module(VALUE klass, VALUE module)
int do_include = 1;
while (iclass) {
VALUE check_class = iclass->klass;
/* During lazy sweeping, iclass->klass could be a dead object that
* has not yet been swept. */
if (!rb_objspace_garbage_object_p(check_class)) {
while (check_class) {
RUBY_ASSERT(!rb_objspace_garbage_object_p(check_class));
if (RB_TYPE_P(check_class, T_ICLASS) &&
(METACLASS_OF(check_class) == module)) {
do_include = 0;
@ -1128,6 +1133,8 @@ rb_include_module(VALUE klass, VALUE module)
if (do_include) {
include_modules_at(iclass->klass, RCLASS_ORIGIN(iclass->klass), module, TRUE);
}
}
iclass = iclass->next;
}
}
@ -1363,6 +1370,9 @@ rb_prepend_module(VALUE klass, VALUE module)
struct rb_id_table *klass_m_tbl = RCLASS_M_TBL(klass);
struct rb_id_table *klass_origin_m_tbl = RCLASS_M_TBL(klass_origin);
while (iclass) {
/* During lazy sweeping, iclass->klass could be a dead object that
* has not yet been swept. */
if (!rb_objspace_garbage_object_p(iclass->klass)) {
if (klass_had_no_origin && klass_origin_m_tbl == RCLASS_M_TBL(iclass->klass)) {
// backfill an origin iclass to handle refinements and future prepends
rb_id_table_foreach(RCLASS_M_TBL(iclass->klass), clear_module_cache_i, (void *)iclass->klass);
@ -1374,6 +1384,8 @@ rb_prepend_module(VALUE klass, VALUE module)
RICLASS_SET_ORIGIN_SHARED_MTBL(origin);
}
include_modules_at(iclass->klass, iclass->klass, module, FALSE);
}
iclass = iclass->next;
}
}