diff options
author | Jeremy Evans <code@jeremyevans.net> | 2020-04-05 12:10:42 -0700 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2020-05-22 07:36:52 -0700 |
commit | c745a60634260ba2080d35af6fdeaaae86fe5193 (patch) | |
tree | b0b3a34af7c2847940dc294408a763535d1861c5 /internal | |
parent | ef13558fcd99dc2057cbfc0973f9592f0f8b3071 (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 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/2978
Diffstat (limited to 'internal')
-rw-r--r-- | internal/class.h | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/internal/class.h b/internal/class.h index 4bebb22905..7724fe43a6 100644 --- a/internal/class.h +++ b/internal/class.h @@ -95,9 +95,10 @@ typedef struct rb_classext_struct rb_classext_t; #endif #define RCLASS_INCLUDER(c) (RCLASS_EXT(c)->includer) -#define RCLASS_CLONED FL_USER6 #define RICLASS_IS_ORIGIN FL_USER5 +#define RCLASS_CLONED FL_USER6 #define RCLASS_REFINED_BY_ANY FL_USER7 +#define RICLASS_ORIGIN_SHARED_MTBL FL_USER8 /* class.c */ void rb_class_subclass_add(VALUE super, VALUE klass); @@ -120,6 +121,7 @@ VALUE rb_singleton_class_get(VALUE obj); int rb_class_has_methods(VALUE c); void rb_undef_methods_from(VALUE klass, VALUE super); static inline void RCLASS_SET_ORIGIN(VALUE klass, VALUE origin); +static inline void RICLASS_SET_ORIGIN_SHARED_MTBL(VALUE iclass); static inline VALUE RCLASS_SUPER(VALUE klass); static inline VALUE RCLASS_SET_SUPER(VALUE klass, VALUE super); static inline void RCLASS_SET_INCLUDER(VALUE iclass, VALUE klass); @@ -137,6 +139,12 @@ RCLASS_SET_ORIGIN(VALUE klass, VALUE origin) } static inline void +RICLASS_SET_ORIGIN_SHARED_MTBL(VALUE iclass) +{ + FL_SET(iclass, RICLASS_ORIGIN_SHARED_MTBL); +} + +static inline void RCLASS_SET_INCLUDER(VALUE iclass, VALUE klass) { RB_OBJ_WRITE(iclass, &RCLASS_INCLUDER(iclass), klass); |