Commit 123dc3bc
123dc3bccff7Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
gc: keep a rooted frozen heap string alive across a string-pool sweep
A heap string frozen by .freeze carries the 0xf1 marker and lives on the
sweepable string heap (sp_str_alloc), but sp_mark_string only promoted
0xfe -> 0xfc, never marking a frozen string. sp_str_sweep then freed every
non-0xfc string -- including a frozen one still rooted via
sp_mark_user_globals (a VERSION = "x".freeze-style constant). The next
collect's mark phase read the freed pointer: a deterministic
heap-use-after-free (ASAN: sp_mark_string -> sp_mark_user_globals ->
sp_gc_collect -> sp_gc_alloc).
Give a frozen string a reachable-this-cycle state: sp_mark_string now marks 0xf1 -> 0xf0, and sp_str_sweep keeps a 0xf0 string, restoring it to 0xf1 (an unreachable frozen string stays 0xf1 and is still reclaimed, so frozen strings remain collectable -- no leak/immortality).
Minimal repro reproduces the exact ASAN trace before this change and runs clean after.
Refs #1449.
make test 998 pass / make bench 57 pass / optcarrot checksum 59662.
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 123dc3bccff7
- Parent
- e5d831ac948d
lib/sp_gc.hmodifiedView file
* String tag bytes: 0xfe heap-unmarked -> 0xfc marked; others skipped. */static inline void sp_mark_string(const char *s) { if (!s) return; if ((unsigned char)s[-1] == 0xfe) { ((char *)s)[-1] = (char)0xfc; } unsigned char m = (unsigned char)s[-1]; if (m == 0xfe) ((char *)s)[-1] = (char)0xfc; /* A heap string frozen by .freeze (0xf1) lives on the sweepable string heap too; give it a reachable-this-cycle state (0xf0) so sp_str_sweep keeps it while rooted instead of freeing a live frozen global (#1449). */ else if (m == 0xf1) ((char *)s)[-1] = (char)0xf0;}static inline void sp_mark_rbval(sp_RbVal v) { if (v.tag == SP_TAG_STR) sp_mark_string(v.v.s);lib/sp_runtime.hmodifiedView file
Loading diff…
test/frozen_string_global_survives_gc.rbaddedView file
Loading diff…
test/frozen_string_global_survives_gc.rb.expectedaddedView file
Loading diff…