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

@@ -277,6 +277,8 @@
277277 bin=$$tmpdir/test_bin$(EXE); \
278278 exp=$$tmpdir/expected; \
279279 act=$$tmpdir/actual; \
280+ args=""; \
281+ if [ -f "$<.args" ]; then args=$$(cat "$<.args"); fi; \
280282 rm -f "[email protected]"; \
281283 ./spinel_parse$(EXE) "$<" "$$ast" 2>/dev/null && \
282284 ./spinel_codegen$(EXE) "$$ast" "$$cfile" 2>/dev/null && \
@@ -286,14 +288,14 @@
286288 if [ -f "$<.expected" ]; then \
287289 LC_ALL=C sed 's/\r$$//' "$<.expected" >"$$exp.n"; \
288290 else \
289- $(TIMEOUT10) $(REF_RUBY) "$<" >"$$exp" 2>/dev/null; \
291+ $(TIMEOUT10) $(REF_RUBY) "$<" $$args >"$$exp" 2>/dev/null; \
290292 ruby_rc=$$?; \
291293 if [ $$ruby_rc -ne 0 ] && [ "$(REF_RUBY)" != "ruby" ]; then \
292- $(TIMEOUT10) ruby "$<" >"$$exp" 2>/dev/null; \
294+ $(TIMEOUT10) ruby "$<" $$args >"$$exp" 2>/dev/null; \
293295 fi; \
294296 LC_ALL=C sed 's/\r$$//' "$$exp" >"$$exp.n"; \
295297 fi; \
296- $(TIMEOUT10) "$$bin" >"$$act" 2>/dev/null; \
298+ $(TIMEOUT10) "$$bin" $$args >"$$act" 2>/dev/null; \
297299 LC_ALL=C sed 's/\r$$//' "$$act" >"$$act.n"; \
298300 if cmp -s "$$exp.n" "$$act.n"; then \
299301 echo PASS > "$@"; \