Commit afb9dac6

afb9dac64cceMerged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): emit SP_GC_SAVE for value-type ctors with GC-managed locals

A value-type constructor body that introduces any pointer-typed local needs SP_GC_SAVE() paired with the SP_GC_ROOT that declare_method_locals emits for those locals. Otherwise the ROOT is pushed onto sp_gc_nroots without a matching restore — the root persists past the ctor return, and GC eventually scans freed slots.

emit_constructor only emits SP_GC_SAVE in its non-value-type branch (where the heap allocation needs it). For value types it left @in_gc_scope at whatever the previously-emitted method had set it to. If the previous method was a non-value class's body (@in_gc_scope = 1), the leftover scope made declare_method_locals skip its own SP_GC_SAVE, and the value- type ctor ended up with unbalanced ROOTs.

Reset @in_gc_scope = 0 at the value-type branch entry so declare_method_locals always emits a fresh SP_GC_SAVE when the body needs one. The saved_gc_scope save/restore wrapping emit_constructor already restores the caller's view on exit.

Encountered while compiling optcarrot's Config::Parser#initialize (value-type Config alongside heap-allocated Parser, ordering- dependent).

Details

Commit
afb9dac64cce
Parent
eaad8a4edb93

spinel_codegen.rbmodifiedView file

@@ -12919,6 +12919,7 @@
1291912919 if @cls_is_value_type[ci] == 1
1292012920 emit_raw("static sp_" + cname + " sp_" + cname + "_new(" + constructor_params_decl(ci) + ") {")
1292112921 emit_raw(" sp_" + cname + " self = {0};")
12922+ @in_gc_scope = 0
1292212923 else
1292312924 emit_raw("static inline sp_" + cname + " *sp_" + cname + "_new(" + constructor_params_decl(ci) + ") {")
1292412925 emit_raw(" SP_GC_SAVE();")