Skip to content

engine: unguarded panics on dangling module model_name in legacy from_salsa path (module_deps, topo_sort) #806

Closed

Description

@bpowers

Problem

Two latent unguarded-panic sites on the legacy Project::from_salsa dependency-resolution path crash with an "internal compiler error" / HashMap-index panic on user-controllable input (a module with an empty or dangling model_name, e.g. a freshly-drawn module or a reference to a deleted model).

1. model.rs:173 — unguarded HashMap index in module_deps

In the is_initial branch of module_deps (src/simlin-engine/src/model.rs):

if ctx.is_initial {
    let model = ctx.models[model_name];   // <- panics if model_name not in ctx.models

This is an unguarded Index on ctx.models. If a Variable::Module references a model_name that is not present in ctx.models, this panics with "no entry found for key" / index panic.

The sibling at model.rs:241-244 (module_output_deps) already guards this correctly:

if !ctx.models.contains_key(model_name) {
    return model_err!(BadModelName, model_name.to_string());
}
let model = ctx.models[model_name];

Line 173 should do the same — use .get() and return BadModelName (or skip gracefully) rather than indexing unconditionally. The fix is a near-mechanical mirror of the guarded twin 70 lines below it.

2. common.rs:2659 — topo_sort panics on an unknown ident (lower priority, same class)

panic!("internal compiler error: unknown ident {}", ident.as_str());

topo_sort panics when a runlist ident has no dependencies entry. This is reachable from the same legacy from_salsa path with a dangling module reference. Lower priority but the same class of latent crash on bad input — should surface as a proper error rather than a panic!.

Why it matters

  • Unguarded panic on user-controllable input: an empty/dangling model_name is something the UI can produce (a module drawn but not yet wired up, or a reference to a model that was deleted). A panic here is an "internal compiler error" crash rather than a clean diagnostic.
  • Inconsistency: model.rs:173 is the unguarded twin of an already-guarded sibling (model.rs:241-244) in the same file. The guard pattern is established; line 173 simply was not updated to match.
  • WASM: under panic = "abort" (WASM release builds) a panic aborts the entire runtime.

Currently latent (why it is tracked, not urgently fixed)

These sites are only reached via ModelStage1::set_dependencies -> Project::from_salsa, which the engine CLAUDE.md documents as a test-only / legacy path. Production uses db::compile_project_incremental, whose parallel logic at src/simlin-engine/src/db.rs:687 already uses .get(...)?:

let sub_model = project_models.get(sub_model_name.as_ref())?;

So the production incremental path is safe; only the legacy monolithic path has the gap.

Components affected

  • src/simlin-engine/src/model.rs (module_deps, line 173)
  • src/simlin-engine/src/common.rs (topo_sort, line 2659)

Possible approaches

  1. model.rs:173: mirror the guarded twin — contains_key/.get() check returning BadModelName, or .get() and skip the module's deps gracefully.
  2. common.rs:2659: return a Result::Err (e.g. a Generic/BadModelName compilation error) instead of panic!, so a dangling runlist ident degrades to a diagnostic.

Context / how discovered

Found while fixing a WASM panic with the same root cause in src/simlin-engine/src/units_infer.rs (an empty/dangling model_name caused self.models[model_name] to panic with "no entry found for key"). These two sibling sites share that root cause but were left unfixed because they are on the test-only/legacy from_salsa path. Filed for tracking; not fixed as part of the units_infer work.

Activity

  1. added
    engine
    on Jun 21, 2026
  2. bpowers commented on Sep 4, 2026

    @bpowers
    OwnerAuthor

    Re-scope, surfaced while writing the engine map for PR #1040 (compiler unification). Both code paths this issue names no longer exist, the panic sites it points at were removed before the code was deleted, and the crash class it describes is pinned by tests on main. Recommendation: close as resolved. Details follow so the next reader does not have to redo the archaeology.

    What the named paths became

    #806 site What happened
    model.rs:173, module_deps, unguarded ctx.models[model_name] Guarded in PR #807 (40c85bff): ctx.models.get(model_name) with an early return, citing this issue. The whole function was then deleted in PR #991 (a42654bc).
    common.rs:2659, topo_sort, panic!("internal compiler error: unknown ident ...") Replaced in PR #807 by a skip (if let Some(deps) = dependencies.get(ident)), with the trailing assert_eq!(runlist_len, result.len()) downgraded to debug_assert!(result.len() <= runlist_len). The function was then deleted in 6783dc01 ("engine: delete the test-only monolithic compile path", one of the Phase 7 commits of the compiler-unification plan; on main and an ancestor of PR #1040's base).
    Project::from_salsa / ModelStage1::set_dependencies, the "legacy path" that reached both Deleted in 6783dc01 together with project.rs, enumerate_modules, and the stage-level errors/model_deps/instantiations fields. db::compile_project_incremental is the only compiler.

    None of fn topo_sort, fn module_deps, fn from_salsa, or fn set_dependencies exists on origin/main (d04593e6) or at PR #1040's head (origin/compiler-unification-v2), so the reproduction steps in the description point at code a reader cannot find.

    What remains of the panic class: nothing reachable

    The only topological sort left is the topo_sort_str closure inside model_dependency_graph_impl (src/simlin-engine/src/db/dep_graph.rs:2342 on main, enclosing fn at :1837; :2188 at PR #1040's head). Its add looks dependencies up with deps.get(name) (:2410, and :2388 for resolved-SCC members) and pushes a name only if it is in allowed, the runlist's own variable set. An ident with no dependency entry is visited, contributes no edges, and is not emitted: no panic and no phantom runlist entry.

    Every production lookup of a module's model_name is guarded (main line numbers):

    • db/diagnostic.rs:1086: let Some(child_model) = project_models.get(..) else { .. }, emitting an Error BadModelName attributed to the module variable when the name is non-empty (added in 6783dc01; an empty name, the freshly-drawn state, stays quiet by design).
    • db/assemble.rs:1689-1693: contains_key, then return Err(..).
    • db/layout.rs:51 and :206: .get(..) / .get(..).copied(). db/layout.rs:399 (module_dep_shape) resolves a missing model to an empty ModelShape, so a reference into it fails as DoesNotExist.
    • db/query.rs:1021-1024: contains_key (module-cycle detection from PR Module editing: fix dangling/cyclic/mis-wired reference bugs found by adversarial audit #807).
    • db.rs:872, db/ltm/mod.rs:610-614, db/analysis.rs:1545: ? / if let Some.

    The remaining panic! sites in dep_graph.rs are a #[cfg(test)] helper (:1490-1525) and the SCC / CircularDependency consistency invariant (:644); neither is an identifier or model lookup.

    Current reproduction (already a test)

    module_targeting_a_missing_model_errors_without_panicking (src/simlin-engine/src/db/module_cycle_tests.rs:646 on main, :649 at PR #1040's head) builds a project with module_var("m", "nonexistent") and asserts that compile_project_incremental returns Err, that collect_all_diagnostics yields exactly one BadModelName Error attributed to main/m, and that analyze_model returns an analysis_error rather than panicking. module_with_an_empty_model_name_is_not_reported_as_dangling (:697) pins the empty-name arm (does not compile, no BadModelName). PR #807's self_referential_module_errors_without_panicking and mutually_recursive_modules_error_without_panicking cover the cyclic cousin. These run under the pre-commit hook on every commit; I did not re-run them for this comment.

    Recommendation

    Close. The two sites were fixed in #807 and then deleted (#991, 6783dc01); the production path has no unguarded lookup for a dangling module model_name, and the class is pinned end to end. There is no residual scope to retitle this issue onto, so the title is left unchanged.

  3. bpowers commented on Sep 4, 2026

    @bpowers
    OwnerAuthor

    Closing as resolved per the re-scope comment above: the panic sites were guarded in #807 and the code they lived in (module_deps, topo_sort, Project::from_salsa) was deleted on main by #991 and 6783dc01; the only topological sort left (db/dep_graph.rs model_dependency_graph_impl) cannot reach an unknown ident, and module_targeting_a_missing_model_errors_without_panicking pins the dangling-module case as a BadModelName error. Reopen if a current reproduction turns up.

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

    engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions