Commit c07eac2c

c07eac2cfdd5Merged

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

Description

codegen: reset heap-promoted cells at class/instance method boundary

A Fiber.new closure capturing a method-local heap-promotes it and records _hcell_<name>_<fid> in @heap_promoted_names. That list is consulted by fiber_var_ref for every local read, so a sibling method whose parameter shares the captured name has its plain read rewritten to (*_hcell_<name>_<fid>), a cell declared only in the first method's C function. The top-level method emitter already reset the list at its boundary (#564); the class instance method and class method emitters did not, so the leak surfaced as "'hcell_' undeclared" in any program with two same-class methods sharing a captured parameter name. Save and restore the list length around both emitters, matching the top-level path.

Fixes #1007

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

Details

Commit
c07eac2cfdd5
Parent
b86e0e16f140

spinel_codegen.rbmodifiedView file

@@ -11467,6 +11467,14 @@
1146711467 @indent = 1
1146811468 @in_gc_scope = 0
1146911469
11470+ # Reset @heap_promoted_names at the method boundary; otherwise a
11471+ # sibling method whose param shares a name with a fiber-captured
11472+ # local here reads `(*_hcell_<name>_<n>)`, a cell declared only in
11473+ # this function's C scope. The top-level method path already does
11474+ # this; class instance / class methods did not.
11475+ saved_hp_names_len_mb = @heap_promoted_names.length
11476+ saved_hp_cells_len_mb = @heap_promoted_cells.length
11477+
1147011478 midx = cls_find_method_direct(ci, mname)
1147111479 if midx >= 0
1147211480 if cls_method_has_yield(ci, midx) == 1
@@ -11564,6 +11572,12 @@
1156411572 @in_yield_method = 0
1156511573 @current_method_yield_arity = 1
1156611574 @indent = 0
11575+ while @heap_promoted_names.length > saved_hp_names_len_mb
11576+ @heap_promoted_names.pop
11577+ end
11578+ while @heap_promoted_cells.length > saved_hp_cells_len_mb
11579+ @heap_promoted_cells.pop
11580+ end
1156711581 emit_raw(" return " + c_return_default(rt) + ";")
1156811582 emit_raw("}")
1156911583 emit_raw("")
@@ -11579,6 +11593,11 @@
1157911593 @indent = 1
1158011594 @in_gc_scope = 0
1158111595
11596+ # Reset @heap_promoted_names at the method boundary; see the note
11597+ # in emit_instance_method.
11598+ saved_hp_names_len_mb = @heap_promoted_names.length
11599+ saved_hp_cells_len_mb = @heap_promoted_cells.length
11600+
1158211601 emit_raw("static " + c_type(rt) + " sp_" + cname + "_cls_" + sanitize_name(mname) + "(" + build_params_decl(pnames, ptypes) + ") {")
1158311602
1158411603 push_scope
@@ -11625,6 +11644,12 @@
1162511644 @current_method_name = ""
1162611645 @current_method_block_param = ""
1162711646 @indent = 0
11647+ while @heap_promoted_names.length > saved_hp_names_len_mb
11648+ @heap_promoted_names.pop
11649+ end
11650+ while @heap_promoted_cells.length > saved_hp_cells_len_mb
11651+ @heap_promoted_cells.pop
11652+ end
1162811653 emit_raw(" return " + c_return_default(rt) + ";")
1162911654 emit_raw("}")
1163011655 emit_raw("")