mmtk / ruby

Fork of The Ruby Programming Language [mirror], with added support for MMTk
https://www.ruby-lang.org/
Other
0 stars 1 forks source link

String moved while enumerating lines #70

Closed wks closed 2 months ago

wks commented 2 months ago

Functions like String#each_line (rb_str_enumerate_lines) performs modification check after every yield (ENUM_ELEM). GC may be triggered in the block and the GC may move objects.

The function rb_str_enumerate_lines gets the pointer to the underlying buffer using RSTRING_PTR. This is not a problem with vanilla CRuby because the buffer is either embedded in the string and the string is pinned by the stack root, or the buffer is allocated by xmalloc which never moves. When using MMTk, however, the buffer may be a separate imemo:mmtk_strbuf. Currently, a pointer to the strbuf is not brought onto the stack, therefore it is not pinned, and GC may move it. Once that happens, str_mod_check will report "string modified" while the string is not actually modified (it is frozen), but just moved by the GC.