Skip to content

engine: VM panic (out-of-bounds temp slice) on SUM(RANK(<sliced 1-D view>)) in a per-element Arrayed equation #794

Closed

Description

@bpowers

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.
  • RANK inside a reducer in per-element slot equations is reachable from Vensim imports via VECTOR 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 an Equation::Arrayed slot 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

  1. added
    bug
    engine
    on Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugengine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions