Repository navigation
Description
Summary
A compile-clean model panics the VM at simulation time with an out-of-bounds slice when a per-element (Equation::Arrayed) slot equation wraps an array-producing RANK over a sliced 1-D view of a 2-D array in a reducer:
panicked at src/simlin-engine/src/vm.rs:2946:32:
range end index 6 out of range for slice of length 4
This is a process-aborting panic, not an error result, so it also takes down library consumers (libsimlin, pysimlin, WASM hosts). No LTM involved (LTM disabled in the repro).
Repro
Verified 2026-06-12 at commit 00b6bc0a (branch ltm-phase1-residuals) and byte-identically at the earlier commit 2fc2f2ee -- pre-existing, NOT introduced by the gh792 commits.
Region = {nyc, boston}, D2 = {p, q}
pop[Region, D2] = stock[Region] * 0.1
share[Region] (Equation::Arrayed):
share[nyc] = SUM(RANK(pop[nyc, *], 1))
share[boston] = SUM(RANK(pop[boston, *], 1))
inflow[Region] = share[Region] * 0.05
stock[Region] integrates inflow, initial 100; sim 0..8 dt 1
Built via TestProject (array_with_ranges_direct), compiled via compile_project_incremental (compiles cleanly), then Vm::new(compiled) + vm.run_to_end() panics as above.
Analysis
vm.rs:2946 is the dense-linear fast path of the array-reduce fold:
for &value in &data[base + start..base + start + size]The SUM reads a temp view (the hoisted RANK result) whose computed start + size exceeds the backing temp storage length. Likely cause: the A2A/per-element hoisting of the array-producing RANK builtin (AssignTemp + TempArrayElement; see the hoisting of VectorSortOrder/Rank in compiler/mod.rs) produces a temp view sized/based inconsistently with what the wrapping reducer's dense-linear fold expects when RANK's argument is a sliced view (pop[nyc, *], a 1-D slice of a 2-D array) inside an Equation::Arrayed slot equation.
Why it matters
- Crash on valid-looking input that compiles cleanly -- severity medium-high.
- Panic (abort), not a recoverable error, crossing into FFI consumers.
RANKinside a reducer in per-element slot equations is reachable from Vensim imports viaVECTOR RANK.
Affected component
src/simlin-engine -- VM array-reduce fold (src/simlin-engine/src/vm.rs:2946) and/or the RANK temp-hoisting path in src/simlin-engine/src/compiler/mod.rs.
Possible approaches
- Audit the temp-view metadata (base/start/size) emitted for hoisted array-valued builtins when the argument is a sliced view in a per-element slot, and make it consistent with the reducer fold's expectations.
- Add a defensive bounds check in the dense-linear fast path that surfaces an internal error instead of panicking.
- Regression test:
SUM(RANK(arr[elem, *], 1))in anEquation::Arrayedslot over a 2-D array.
Dedup notes
Distinct from #776 (LTM diagonal-only element edges for de-hoisted RANK), #771/#742 (LTM RANK hoist/capture scoring), #781 (fragment-compile failure for mixed-arity reducers), and #789 (wrong values for bare ref sharing a reducer). This is a runtime VM crash, not an LTM-scoring or compile-time issue.
Identified while investigating the gh792 residuals (LTM phase 1) -- the repro fails identically with LTM disabled.
Activity