Commit 649ce571
649ce5718fccMerged
Yusuke Endoh <[email protected]> · 6 months ago · 6 files · +3 added · ~3 modified
Description
fix: mark sp_argv strings during GC
ARGV[i] returns a const char * into a string allocated by
sp_str_dup_external on main() entry, which lands on the str-heap
with mark byte 0xfe. sp_re_mark_globals marks the regex globals to
keep them alive across sp_str_sweep, but the same protection was
missing for sp_argv — so the first sp_gc_collect after main starts
reaps every argv string, leaving sp_argv.data[i] dangling.
The bug surfaces non-deterministically by string length: malloc reuses the freed slots in ways that depend on later allocations, so reads of certain ARGV[i] values segfault while others happen to find still-readable bytes. With optcarrot the failure mode is straight-line: any non-empty ARGV crashes optcarrot-single before the benchmark loop starts.
Move the sp_Argv typedef and sp_argv global from
spinel_codegen.rb's emit_forward_decls into sp_runtime.h, then
walk sp_argv.data inside sp_re_mark_globals so sweep keeps each
entry. The fix is symmetrical with the existing $1..$9 / $&
treatment a few lines above.
Add test/argv_gc.rb to lock the regression in: it allocates ~5 MB of throwaway objects to force several sp_gc_collect cycles, then reads each ARGV[i] back. Without the fix the read either segfaults or yields garbage; with the fix the lengths come back intact.
Tested: gen2 == gen3 bootstrap fixed point, 336/336 test pass,
make optcarrot reports checksum: 59662.
Details
- Commit
- 649ce5718fcc
- Parent
- ae1a0f329f3d
MakefilemodifiedView file
bin=$$tmpdir/test_bin$(EXE); \ exp=$$tmpdir/expected; \ act=$$tmpdir/actual; \ args=""; \ if [ -f "$<.args" ]; then args=$$(cat "$<.args"); fi; \ ./spinel_parse$(EXE) "$<" "$$ast" 2>/dev/null && \ ./spinel_codegen$(EXE) "$$ast" "$$cfile" 2>/dev/null && \ if [ -f "$<.expected" ]; then \ LC_ALL=C sed 's/\r$$//' "$<.expected" >"$$exp.n"; \ else \ $(TIMEOUT10) $(REF_RUBY) "$<" >"$$exp" 2>/dev/null; \ $(TIMEOUT10) $(REF_RUBY) "$<" $$args >"$$exp" 2>/dev/null; \ ruby_rc=$$?; \ if [ $$ruby_rc -ne 0 ] && [ "$(REF_RUBY)" != "ruby" ]; then \ $(TIMEOUT10) ruby "$<" >"$$exp" 2>/dev/null; \ $(TIMEOUT10) ruby "$<" $$args >"$$exp" 2>/dev/null; \ fi; \ LC_ALL=C sed 's/\r$$//' "$$exp" >"$$exp.n"; \ fi; \ $(TIMEOUT10) "$$bin" >"$$act" 2>/dev/null; \ $(TIMEOUT10) "$$bin" $$args >"$$act" 2>/dev/null; \ LC_ALL=C sed 's/\r$$//' "$$act" >"$$act.n"; \ if cmp -s "$$exp.n" "$$act.n"; then \ echo PASS > "$@"; \lib/sp_runtime.hmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/argv_gc.rbaddedView file
Loading diff…
test/argv_gc.rb.argsaddedView file
Loading diff…
test/argv_gc.rb.expectedaddedView file
Loading diff…