summaryrefslogtreecommitdiff
path: root/vm.c
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2020-04-05 12:10:42 -0700
committerJeremy Evans <code@jeremyevans.net>2020-05-22 20:31:23 -0700
commitad729a1d11c6c57efd2e92803b4e937db0f75252 (patch)
tree572c79bde61b62de8728ca6d81e3c6dcfbe94d1a /vm.c
parentfdb31aa7ab1bb1718e57e2e052caff959af3111e (diff)
Fix origin iclass pointer for modules
If a module has an origin, and that module is included in another module or class, previously the iclass created for the module had an origin pointer to the module's origin instead of the iclass's origin. Setting the origin pointer correctly requires using a stack, since the origin iclass is not created until after the iclass itself. Use a hidden ruby array to implement that stack. Correctly assigning the origin pointers in the iclass caused a use-after-free in GC. If a module with an origin is included in a class, the iclass shares a method table with the module and the iclass origin shares a method table with module origin. Mark iclass origin with a flag that notes that even though the iclass is an origin, it shares a method table, so the method table should not be garbage collected. The shared method table will be garbage collected when the module origin is garbage collected. I've tested that this does not introduce a memory leak. This change caused a VM assertion failure, which was traced to callable method entries using the incorrect defined_class. Update rb_vm_check_redefinition_opt_method and find_defined_class_by_owner to treat iclass origins different than class origins to avoid this issue. This also includes a fix for Module#included_modules to skip iclasses with origins. Fixes [Bug #16736]
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/3136
Diffstat (limited to 'vm.c')
-rw-r--r--vm.c3
1 files changed, 2 insertions, 1 deletions
diff --git a/vm.c b/vm.c
index 90d4d68..fb065f5 100644
--- a/vm.c
+++ b/vm.c
@@ -1618,7 +1618,8 @@ static void
rb_vm_check_redefinition_opt_method(const rb_method_entry_t *me, VALUE klass)
{
st_data_t bop;
- if (RB_TYPE_P(klass, T_ICLASS) && FL_TEST(klass, RICLASS_IS_ORIGIN)) {
+ if (RB_TYPE_P(klass, T_ICLASS) && FL_TEST(klass, RICLASS_IS_ORIGIN) &&
+ RB_TYPE_P(RBASIC_CLASS(klass), T_CLASS)) {
klass = RBASIC_CLASS(klass);
}
if (vm_redefinition_check_method_type(me->def)) {