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
55 changes: 55 additions & 0 deletions lib/interscript/isc/node_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,20 @@ def from_key(rule)
end

def build_rule(rule_def)
# A capture reference whose capture group the from expression
# never opens is a named parse-time rejection (WO18 G8's
# fn-literal-in-from convention: a named MapLogicError, never an
# accidental exception — before the WO20 fix this exact surface
# crashed with a NameError, interscript-ruby#864). Paired refs
# stay legal: the geminate idiom `from capture(x) + ref(1)` is
# pinned by node_adapter_spec, and `to`-position refs (27 corpus
# maps) are unaffected. Whether unpaired refs should instead be
# a run-time error like ts/py/rs is the owner-held
# cross-runtime question from the fuzz triage.
if (unpaired = unpaired_capture_ref(rule_def[:from]))
raise Interscript::MapLogicError,
"ref(#{unpaired}) in from position has no matching capture group"
end
from = convert_item(rule_def[:from])
to = convert_item(rule_def[:to])
opts = {}
Expand Down Expand Up @@ -494,6 +508,47 @@ def convert_concat(concat)
parts.reduce { |acc, part| acc + part }
end

# The highest capture-reference index the from expression has no
# capture group to point at, or nil when every ref is paired.
# Onigmo accepts any existing group number from any position
# (forward and nested refs included), so pairing is a count
# comparison: a ref(N) is valid iff the expression opens >= N
# capture groups (CaptureGroup is the only capturing wrapper in
# build_item; plain groups and repeats render non-capturing).
def unpaired_capture_ref(item)
refs = max_capture_ref(item)
return nil if refs.nil? || refs <= count_capture_groups(item)
refs
end

def max_capture_ref(item)
case item
when Items::Capture
item.index
when Items::Concat
item.parts.filter_map { |p| max_capture_ref(p) }.max
when Items::CaptureGroup, Items::Maybe, Items::Some
max_capture_ref(item.inner)
when Items::Set
item.chars.filter_map { |c| max_capture_ref(c) }.max
end
end

def count_capture_groups(item)
case item
when Items::CaptureGroup
1 + count_capture_groups(item.inner)
when Items::Concat
item.parts.sum { |p| count_capture_groups(p) }
when Items::Maybe, Items::Some
count_capture_groups(item.inner)
when Items::Set
item.chars.sum { |c| count_capture_groups(c) }
else
0
end
end

def convert_set(set)
Interscript::Node::Item::Any.new(
set.chars.map do |c|
Expand Down
7 changes: 7 additions & 0 deletions lib/interscript/node/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ class Interscript::Node::Item
autoload :Any, "interscript/node/item/any"
autoload :Stage, "interscript/node/item/stage"
autoload :CaptureGroup, "interscript/node/item/capture"
# CaptureRef lives in the same file as CaptureGroup. It MUST have its
# own autoload entry: a map carrying only a to-position ref(1) (27
# corpus maps) references the constant without ever touching
# CaptureGroup, and without the entry it crashed with a NameError
# whenever no earlier parse had loaded the file by accident
# (interscript-ruby#864, WO20 fuzz campaign).
autoload :CaptureRef, "interscript/node/item/capture"
autoload :Repeat, "interscript/node/item/repeat"
# Maybe, MaybeSome, Some are subclasses of Repeat defined in
# the same file. Autoload points to repeat.rb which defines all of them.
Expand Down
160 changes: 160 additions & 0 deletions spec/isc/fail_loud_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# crashed with NoMethodError. ts/py/rs semantics are the reference.

require "tmpdir"
require "fileutils"

RSpec.describe "WO18 — interpreter fail-loud behavior" do
def with_map(name, body)
Expand Down Expand Up @@ -89,4 +90,163 @@ def with_map(name, body)
expect(stage { swapcase }.call("aBc")).to eq("AbC")
end
end

describe "WO20 fuzz — interscript-ruby#864: ref(N) constant-lookup crash" do
# The fuzz campaign's 14 crash cases: `ref(N)` anywhere in an ISC map
# crashed with NameError: uninitialized constant
# Node::Item::CaptureRef — the item class exists (item/capture.rb,
# next to CaptureGroup) but had no autoload entry, so the constant
# resolved only when an earlier dependency parse happened to touch
# CaptureGroup first. Corpus maps escaped by that accident.
#
# The named convention is the G8 one: an invalid from-position item
# gets a parse-time MapLogicError (fn-literal-in-from → "Wrong type
# Symbol" via try_convert). The capture-ref flavor is scoping-aware:
# a ref whose capture group the from expression never opens is the
# named rejection; PAIRED refs (the geminate idiom
# `capture(any_character) + ref(1)`, pinned by node_adapter_spec)
# and `to`-position refs (27 corpus maps) stay legal.

def fresh_process_parse(map_name, map_body)
lib = File.expand_path("../../lib", __dir__)
dir = Dir.mktmpdir
File.write(File.join(dir, "#{map_name}.isc"), map_body)
script = "require \"interscript\"; " \
"puts \"AUTOLOAD=\" + (Interscript::Node::Item.autoload?(:CaptureRef) || \"nil\").to_s; " \
"Interscript.load_path.unshift(ARGV[0]); " \
"begin; Interscript.parse(ARGV[1]); puts \"PARSE OK\"; " \
"rescue Exception => e; puts e.class.to_s + \": \" + e.message; end"
IO.popen([RbConfig.ruby, "-I", lib, "-e", script, dir, map_name], &:read)
ensure
FileUtils.remove_entry(dir) if dir
end

it "an unpaired capture ref in from position raises the named MapLogicError (was: NameError)" do
# The campaign's minimized repro (fuzz findings/ruby-ref-in-from-crash).
with_map("tst-wo20-ref-from.isc", <<~ISC) do
system "T:tst-wo20-ref-from:1" {
stage main {
sequence {
sub {
from some(any(["k", "q"])) + ref(1)
to "W" + ref(1)
}
}
}
}
ISC
expect { Interscript.parse("tst-wo20-ref-from") }.to raise_error(
Interscript::MapLogicError,
/ref\(1\) in from position has no matching capture group/
)
end
end

it "a capture ref nested in a from expression is rejected too" do
with_map("tst-wo20-ref-from-nested.isc", <<~ISC) do
system "T:tst-wo20-ref-from-nested:1" {
stage main {
sequence {
sub {
from some(ref(1))
to "W"
}
}
}
}
ISC
expect { Interscript.parse("tst-wo20-ref-from-nested") }.to raise_error(
Interscript::MapLogicError,
/ref\(1\) in from position has no matching capture group/
)
end
end

it "a paired ref in from stays legal (the geminate idiom)" do
# Scoping pin: the named rejection targets UNPAIRED refs only.
# `from capture(any_character) + ref(1)` compiles to X(X)→X and is
# pinned executable by node_adapter_spec; parsing it must never
# hit the gate.
with_map("tst-wo20-ref-paired.isc", <<~ISC) do
system "T:tst-wo20-ref-paired:1" {
stage main {
sequence {
sub {
from capture(any_character) + ref(1)
to ref(1)
}
}
}
}
ISC
expect { Interscript.parse("tst-wo20-ref-paired") }.not_to raise_error
expect(Interscript.transliterate("tst-wo20-ref-paired", "чч")).to eq("ч")
end
end

it "CaptureRef resolves by name — the autoload entry, not parse-order accident" do
# Root cause pin, in a fresh process: autoload? is only observable
# before the first resolution, so an in-process assertion here
# would be order-dependent on the rest of the suite.
out = fresh_process_parse("tst-wo20-autoload-probe", <<~ISC)
system "T:tst-wo20-autoload-probe:1" {
stage main { sub "a" "b" }
}
ISC
expect(out).to include("AUTOLOAD=interscript/node/item/capture")
end

it "the issue repro in a fresh process: MapLogicError, never NameError" do
out = fresh_process_parse("tst-wo20-fz-em8", <<~ISC)
system "T:fz-em8:1" {
stage main {
sequence {
sub {
from some(any(["k", "q"])) + ref(1)
to "W" + ref(1)
}
}
}
}
ISC
expect(out).to include("Interscript::MapLogicError")
expect(out).not_to include("NameError")
end

it "a to-position ref parses and runs as a backreference (was: the same NameError)" do
with_map("tst-wo20-ref-to.isc", <<~ISC) do
system "T:tst-wo20-ref-to:1" {
stage main {
sequence {
sub {
from capture("k")
to "W" + ref(1)
}
}
}
}
ISC
doc = Interscript.parse("tst-wo20-ref-to")
expect(doc.stages[:main]).not_to be_nil
expect(Interscript.transliterate("tst-wo20-ref-to", "kk")).to eq("WkWk")
end
end

it "a to-position ref parses in a fresh process (order-independent)" do
out = fresh_process_parse("tst-wo20-ref-to-fresh", <<~ISC)
system "T:tst-wo20-ref-to-fresh:1" {
stage main {
sequence {
sub {
from capture("k")
to "W" + ref(1)
}
}
}
}
ISC
expect(out).to include("PARSE OK")
expect(out).not_to include("NameError")
end
end
end
Loading