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
# Build capture struct typedef if needed cap_typedef = "" cap_scan_name = "NULL" cap_scan_func = "" if free_vars.length > 0 cap_typedef = "typedef struct {" k = 0 k = k + 1 end cap_typedef = cap_typedef + " } " + cap_name + ";" + 10.chr # Capture-struct GC scan. sp_Fiber_scan marks user_data (this # struct) but without a scan hook the captured cells -- and the # objects they hold -- are never traced, so an object reachable # only from a parked fiber is collected (use-after-free under # concurrent fiber load). Mark each field's cell and its contents. cap_scan_name = cap_name + "_scan" cap_scan_func = "static void " + cap_scan_name + "(void *_p) { " + cap_name + " *_c = (" + cap_name + " *)_p;" k = 0 while k < free_vars.length fld = "_c->" + free_vars[k] cap_scan_func = cap_scan_func + " if (" + fld + ") { sp_gc_mark((void *)" + fld + ");" content_mark = gc_mark_stmt_for_expr("(*" + fld + ")", free_var_types[k]) if content_mark != "" cap_scan_func = cap_scan_func + " " + content_mark end cap_scan_func = cap_scan_func + " }" k = k + 1 end cap_scan_func = cap_scan_func + " }" + 10.chr end # Compile fiber body function fbody = fbody + @out_lines.join(10.chr) + 10.chr fbody = fbody + "}" + 10.chr @fiber_funcs << cap_typedef if cap_scan_func != "" @fiber_funcs << cap_scan_func end @fiber_funcs << fbody @out_lines = saved_out # Heap-allocate capture struct tmp_fb = "_tmpfb_" + fid.to_s cap_ptr = "_cap_ptr_" + fid.to_s emit(" " + cap_name + " *" + cap_ptr + " = (" + cap_name + "*)sp_gc_alloc(sizeof(" + cap_name + "), NULL, NULL);") emit(" " + cap_name + " *" + cap_ptr + " = (" + cap_name + "*)sp_gc_alloc(sizeof(" + cap_name + "), NULL, " + cap_scan_name + ");") k = 0 while k < free_vars.length vn = free_vars[k]test/i1031.rbaddedView file
Loading diff…
test/i1031.rb.expectedaddedView file
Loading diff…