Commit 668c4dec

668c4decf8faMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

Scan fiber capture structs so parked-fiber objects survive GC

A fiber body's captured locals live in a heap-allocated capture struct referenced from the Fiber's user_data. sp_Fiber_scan marks that struct, but it was allocated with a NULL scan hook, so the GC never traced through it to the captured cells or the objects they hold. An object reachable only from a suspended fiber was therefore collected; a later resume read freed memory. The corruption only surfaced under concurrent fiber load because a single collection left the freed slot intact, while repeated collections with allocation churn reused it (intermittent SIGSEGV / response corruption).

Emit a per-capture-struct scan function that marks each field's cell and its contents (reusing gc_mark_stmt_for_expr for the per-type dispatch), and pass it to sp_gc_alloc.

Fixes #1031

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
668c4decf8fa
Parent
cdcb9d6d4c80

spinel_codegen.rbmodifiedView file

@@ -15924,6 +15924,8 @@
1592415924
1592515925 # Build capture struct typedef if needed
1592615926 cap_typedef = ""
15927+ cap_scan_name = "NULL"
15928+ cap_scan_func = ""
1592715929 if free_vars.length > 0
1592815930 cap_typedef = "typedef struct {"
1592915931 k = 0
@@ -15932,6 +15934,25 @@
1593215934 k = k + 1
1593315935 end
1593415936 cap_typedef = cap_typedef + " } " + cap_name + ";" + 10.chr
15937+ # Capture-struct GC scan. sp_Fiber_scan marks user_data (this
15938+ # struct) but without a scan hook the captured cells -- and the
15939+ # objects they hold -- are never traced, so an object reachable
15940+ # only from a parked fiber is collected (use-after-free under
15941+ # concurrent fiber load). Mark each field's cell and its contents.
15942+ cap_scan_name = cap_name + "_scan"
15943+ cap_scan_func = "static void " + cap_scan_name + "(void *_p) { " + cap_name + " *_c = (" + cap_name + " *)_p;"
15944+ k = 0
15945+ while k < free_vars.length
15946+ fld = "_c->" + free_vars[k]
15947+ cap_scan_func = cap_scan_func + " if (" + fld + ") { sp_gc_mark((void *)" + fld + ");"
15948+ content_mark = gc_mark_stmt_for_expr("(*" + fld + ")", free_var_types[k])
15949+ if content_mark != ""
15950+ cap_scan_func = cap_scan_func + " " + content_mark
15951+ end
15952+ cap_scan_func = cap_scan_func + " }"
15953+ k = k + 1
15954+ end
15955+ cap_scan_func = cap_scan_func + " }" + 10.chr
1593515956 end
1593615957
1593715958 # Compile fiber body function
@@ -16007,6 +16028,9 @@
1600716028 fbody = fbody + @out_lines.join(10.chr) + 10.chr
1600816029 fbody = fbody + "}" + 10.chr
1600916030 @fiber_funcs << cap_typedef
16031+ if cap_scan_func != ""
16032+ @fiber_funcs << cap_scan_func
16033+ end
1601016034 @fiber_funcs << fbody
1601116035
1601216036 @out_lines = saved_out
@@ -16082,7 +16106,7 @@
1608216106 # Heap-allocate capture struct
1608316107 tmp_fb = "_tmpfb_" + fid.to_s
1608416108 cap_ptr = "_cap_ptr_" + fid.to_s
16085- emit(" " + cap_name + " *" + cap_ptr + " = (" + cap_name + "*)sp_gc_alloc(sizeof(" + cap_name + "), NULL, NULL);")
16109+ emit(" " + cap_name + " *" + cap_ptr + " = (" + cap_name + "*)sp_gc_alloc(sizeof(" + cap_name + "), NULL, " + cap_scan_name + ");")
1608616110 k = 0
1608716111 while k < free_vars.length
1608816112 vn = free_vars[k]