Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions lib/interscript/interpreter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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.<doc>.stage.<name>"
# (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
Expand Down Expand Up @@ -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.<x>`
# 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
Expand Down
17 changes: 17 additions & 0 deletions spec/interscript/fixtures/fuzz/diamond-run.isc
Original file line number Diff line number Diff line change
@@ -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
}
}
13 changes: 13 additions & 0 deletions spec/interscript/fixtures/fuzz/mutual-run.isc
Original file line number Diff line number Diff line change
@@ -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
}
}
8 changes: 8 additions & 0 deletions spec/interscript/fixtures/fuzz/self-run.isc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
system "T:fz-selfrun:1" {
stage main {
parallel {
sub "a" "b"
}
run stage.main
}
}
37 changes: 37 additions & 0 deletions spec/interscript/isc/run_cycle_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require "interscript"

# WO20 differential-fuzz finding (maps#294): a self-recursive
# `run stage.<self>` (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.<self> (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
Loading