From 02674546eeb648f14e81480e01b3e0d3aec19615 Mon Sep 17 00:00:00 2001 From: Ronald Tse Date: Sat, 21 Nov 2026 15:24:26 +0800 Subject: [PATCH] fix(runtime): a recursive run is a named MapLogicError, not a SystemStackError A self-recursive `run stage.` (or a mutual run cycle across stages) recursed until SystemStackError - a crash-class death embedders cannot handle (maps#294, 15 fuzz cases; rs aborted the process outright pre-0.3.3, ts overflows its stack the same way). The run chain is threaded through nested stages and a revisited RESOLVED target (document + stage, not the alias spelling) is a named MapLogicError carrying the chain, in the directive's own syntax - mirroring rs's shipped 0.3.3 guard and the W1 `use cycle:` named errors. py's conversion-time guard fires earlier still. A non-cyclic diamond (two callers sharing one downstream run) still works - pinned alongside the self and mutual cycle repros. Part of interscript/maps#294 (the ts leg lands separately). --- lib/interscript/interpreter.rb | 38 ++++++++++++++++--- .../interscript/fixtures/fuzz/diamond-run.isc | 17 +++++++++ spec/interscript/fixtures/fuzz/mutual-run.isc | 13 +++++++ spec/interscript/fixtures/fuzz/self-run.isc | 8 ++++ spec/interscript/isc/run_cycle_spec.rb | 37 ++++++++++++++++++ 5 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 spec/interscript/fixtures/fuzz/diamond-run.isc create mode 100644 spec/interscript/fixtures/fuzz/mutual-run.isc create mode 100644 spec/interscript/fixtures/fuzz/self-run.isc create mode 100644 spec/interscript/isc/run_cycle_spec.rb diff --git a/lib/interscript/interpreter.rb b/lib/interscript/interpreter.rb index 3faa9027..8c4b1793 100644 --- a/lib/interscript/interpreter.rb +++ b/lib/interscript/interpreter.rb @@ -27,7 +27,8 @@ def call(str, stage = :main, each: false, language: nil, &block) # standard:disable Style/GlobalVars (deliberate $DEBUG / -d-flag debug idiom) $select_nth_string = true # standard:enable Style/GlobalVars - result = Stage.new(@map, str, language: language).execute_rule(stage) + result = Stage.new(@map, str, language: language, + run_chain: ["map.#{@map.name}.stage.#{stage_name}"]).execute_rule(stage) # standard:disable Style/GlobalVars (deliberate $DEBUG / -d-flag debug idiom) $select_nth_string = false # standard:enable Style/GlobalVars @@ -73,7 +74,8 @@ def call(str, stage = :main, each: false, language: nil, &block) e end else - Stage.new(@map, str, language: language).execute_rule(stage) + Stage.new(@map, str, language: language, + run_chain: ["map.#{@map.name}.stage.#{stage_name}"]).execute_rule(stage) end end @@ -122,10 +124,14 @@ def self.backref_without_group?(pattern) class Stage attr_reader :language - def initialize(map, str, language: nil) + def initialize(map, str, language: nil, run_chain: []) @str = str @map = map @language = Interscript::Interpreter.select_language(map, language) + # maps#294: stages currently in flight, keyed "map..stage." + # (the resolved target, not the alias spelling). Seeded by + # Interpreter#call with the invoked stage; every nested run extends it. + @run_chain = run_chain end # ISC-WF-1 W3 guard applicability. Unguarded rules always apply; a @@ -279,17 +285,39 @@ def execute_rule r doc = @map.dep_aliases[r.stage.map].document stage = doc.imported_stages[r.stage.name] raise Interscript::MapLogicError, %(stage "#{r.stage.name}" not found in #{doc.name}) unless stage - @str = Stage.new(doc, @str, language: @language).execute_rule(stage) + @str = Stage.new(doc, @str, language: @language, + run_chain: enter_run(doc, r.stage.name)).execute_rule(stage) else stage = @map.imported_stages[r.stage.name] raise Interscript::MapLogicError, %(stage "#{r.stage.name}" not found in #{@map.name}) unless stage - @str = Stage.new(@map, @str, language: @language).execute_rule(stage) + @str = Stage.new(@map, @str, language: @language, + run_chain: enter_run(@map, r.stage.name)).execute_rule(stage) end end @str end + # maps#294 (WO20 fuzz): a self- or mutual-recursive `run stage.` + # recursed until SystemStackError — a crash-class death embedders + # cannot handle (rs aborted the process outright pre-0.3.3; ts + # overflowed its stack the same way). The run chain is threaded + # through nested stages; a revisited RESOLVED target (document + + # stage, not the alias spelling — a dependency chain may reuse one + # alias for different targets) is a named MapLogicError carrying the + # chain, in the directive's own syntax. Mirrors rs's shipped 0.3.3 + # guard and the W1 `use cycle:` named errors; py's conversion-time + # guard fires earlier still. A NON-cyclic re-run (a diamond: two + # callers sharing one downstream run) stays legal — the chain holds + # only the stages in flight. + def enter_run(doc, stage_name) + entry = "map.#{doc.name}.stage.#{stage_name}" + if @run_chain.include?(entry) + raise Interscript::MapLogicError, "run cycle: #{(@run_chain + [entry]).join(" -> ")}" + end + @run_chain + [entry] + end + def build_regexp(r) from = build_item(r.from, :re) before = build_item(r.before, :re) if r.before diff --git a/spec/interscript/fixtures/fuzz/diamond-run.isc b/spec/interscript/fixtures/fuzz/diamond-run.isc new file mode 100644 index 00000000..cb45eb2f --- /dev/null +++ b/spec/interscript/fixtures/fuzz/diamond-run.isc @@ -0,0 +1,17 @@ +system "T:fz-diamondrun:1" { + stage common { + sub "1" "2" + } + stage a { + sub "a" "A" + run stage.common + } + stage b { + sub "b" "B" + run stage.common + } + stage main { + run stage.a + run stage.b + } +} diff --git a/spec/interscript/fixtures/fuzz/mutual-run.isc b/spec/interscript/fixtures/fuzz/mutual-run.isc new file mode 100644 index 00000000..a7ea3d80 --- /dev/null +++ b/spec/interscript/fixtures/fuzz/mutual-run.isc @@ -0,0 +1,13 @@ +system "T:fz-mutrun:1" { + stage main { + run stage.a + } + stage a { + sub "x" "y" + run stage.b + } + stage b { + sub "y" "x" + run stage.a + } +} diff --git a/spec/interscript/fixtures/fuzz/self-run.isc b/spec/interscript/fixtures/fuzz/self-run.isc new file mode 100644 index 00000000..a4456cca --- /dev/null +++ b/spec/interscript/fixtures/fuzz/self-run.isc @@ -0,0 +1,8 @@ +system "T:fz-selfrun:1" { + stage main { + parallel { + sub "a" "b" + } + run stage.main + } +} diff --git a/spec/interscript/isc/run_cycle_spec.rb b/spec/interscript/isc/run_cycle_spec.rb new file mode 100644 index 00000000..5a81d2b6 --- /dev/null +++ b/spec/interscript/isc/run_cycle_spec.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require "interscript" + +# WO20 differential-fuzz finding (maps#294): a self-recursive +# `run stage.` (or a mutual run cycle across stages) recursed until +# `SystemStackError` — a crash-class death embedders cannot meaningfully +# handle (rs aborted the process outright pre-0.3.3; ts overflowed its +# stack the same way). The cross-runtime contract: the run chain is +# threaded through stage execution and a revisited resolved target is a +# named `MapLogicError` carrying the chain, in the directive's own +# syntax — mirroring the W1 `use cycle:` named errors and rs's shipped +# 0.3.3 guard (py's conversion-time guard fires earlier still). +# A NON-cyclic re-run stays legal: the diamond pin below (two callers +# sharing one downstream run) must keep working. +RSpec.describe "fuzz: run cycles are named errors, never stack overflows" do + let(:fixture_dir) { File.expand_path("../fixtures/fuzz", __dir__) } + + before do + allow(Interscript).to receive(:load_path).and_return([fixture_dir]) + allow(Interscript).to receive(:map_aliases).and_return({}) + end + + it "names a self-recursive run stage. (maps#294 repro)" do + expect { Interscript.transliterate("self-run", "aa") } + .to raise_error(Interscript::MapLogicError, /run cycle/) + end + + it "names a mutual run cycle across stages" do + expect { Interscript.transliterate("mutual-run", "x") } + .to raise_error(Interscript::MapLogicError, /run cycle/) + end + + it "still allows a non-cyclic diamond of shared runs" do + expect(Interscript.transliterate("diamond-run", "ab1")).to eq("AB2") + end +end