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

@@ -78,9 +78,12 @@
7878 * String tag bytes: 0xfe heap-unmarked -> 0xfc marked; others skipped. */
7979 static inline void sp_mark_string(const char *s) {
8080 if (!s) return;
81- if ((unsigned char)s[-1] == 0xfe) {
82- ((char *)s)[-1] = (char)0xfc;
83- }
81+ unsigned char m = (unsigned char)s[-1];
82+ if (m == 0xfe) ((char *)s)[-1] = (char)0xfc;
83+ /* A heap string frozen by .freeze (0xf1) lives on the sweepable string heap
84+ too; give it a reachable-this-cycle state (0xf0) so sp_str_sweep keeps it
85+ while rooted instead of freeing a live frozen global (#1449). */
86+ else if (m == 0xf1) ((char *)s)[-1] = (char)0xf0;
8487 }
8588 static inline void sp_mark_rbval(sp_RbVal v) {
8689 if (v.tag == SP_TAG_STR) sp_mark_string(v.v.s);