Skip to content

Kernel.Environment.replay skips forged inductives and stored entries named in their all (inductives and definitions), accepted by default-mode leanchecker #16688

Done

Description

@seconds-0

Prerequisites

Description

Kernel.Environment.replay does not validate stored .inductInfo entries against the InductiveVal values generated by the kernel, nor does it check that an inductive entry's all list contains its own name.

In src/Lean/Replay.lean, the .inductInfo info branch of replayConstant constructs Declaration.inductDecl only from the entries named in info.all (their types and constructors), erases those names from remaining/pending, and after the branch erases the entry's own name from pending. The stored InductiveVal under name (its all, ctors, numIndices, numNested, isRec, isReflexive, and type) is never compared against the kernel-generated InductiveVal. If info.all does not contain name, name is never added to the replayed environment, yet replay succeeds.

Constructors and recursors are compared after replay via checkPostponedConstructors and checkPostponedRecursors, but there is no counterpart check for inductives.

Because default-mode leanchecker replays each module against its imports as loaded from their .olean files, a forged .inductInfo entry stored in Forge.olean (named bad, of type False, with no constructors and all listing only the honest inductive Z : Prop) is accepted by leanchecker Forge, and a downstream module Use importing Forge sees bad : False from the loaded import environment, proves boom : False without axioms, and passes leanchecker Use.

Default-mode leanchecker trusts a module's imports by design, so checking Use relies on leanchecker Forge having checked Forge; here both exit 0.

Context

This affects anyone who uses default-mode leanchecker or calls Kernel.Environment.replay on constants they did not produce in order to validate .olean files built by someone else. Such an .olean file can be produced by stock lean from source using a metaprogram (as Forge.lean below demonstrates). leanchecker --fresh is not affected (it rejects the downstream use of the forged constant), and the export-based checkers bundled for lake check --paranoid reject the proof of False (see Additional Information). Kernel soundness itself is not in question.

Steps to Reproduce

In an empty directory, create the following three files:

Forge.lean:

import Lean
open Lean

inductive Z : Prop

-- `Lean.Kernel.Environment.add` (private, inserts a constant without checking), referenced by name
elab "kernelAdd" : term =>
  return mkConst (Name.mkStr (Name.mkNum (Name.mkStr (Name.mkStr (Name.mkStr .anonymous "_private") "Lean")
    "Environment") 0 |>.append `Lean.Kernel.Environment) "add")
def kernelAdd' : Kernel.Environment → ConstantInfo → Kernel.Environment := kernelAdd

-- an "inductive" `bad` whose type is `False`, with no constructors, whose `all` lists only `Z`
run_cmd Elab.Command.liftCoreM do
  let v : InductiveVal := {
    name := `bad, levelParams := [], type := mkConst ``False, numParams := 0, numIndices := 0
    all := [`Z], ctors := [], numNested := 0, isRec := false, isUnsafe := false, isReflexive := false }
  setEnv (Environment.ofKernelEnv (kernelAdd' (← getEnv).toKernelEnv (.inductInfo v)))

Use.lean:

import Forge

theorem boom : False := bad
#print axioms boom

ReplayTest.lean:

import Lean
open Lean

inductive Z : Prop

-- `replay` accepts a map holding an inductive entry that the kernel never checks
run_meta show MetaM Unit from do
  let env ← getEnv
  let mut consts : Std.HashMap Name ConstantInfo :=
    env.constants.map₂.foldl (init := ∅) fun consts n info => consts.insert n info
  let bad : InductiveVal := {
    name := `bad, levelParams := [], type := mkConst ``False, numParams := 0, numIndices := 0
    all := [`Z], ctors := [], numNested := 0, isRec := false, isUnsafe := false, isReflexive := false }
  consts := consts.insert `bad (.inductInfo bad)
  let some importEnv := env.importEnv? | unreachable!
  let env' ← importEnv.toKernelEnv.replay consts
  logInfo m!"replay accepted the map; bad in the replayed environment: {(env'.find? `bad).isSome}"

Run:

export LEAN_PATH="$PWD"
lean Forge.lean -o Forge.olean
lean Use.lean -o Use.olean
leanchecker Forge
leanchecker Use
leanchecker --fresh Use
lean ReplayTest.lean

Expected behavior:

leanchecker Forge rejects Forge.olean because bad is not a valid kernel-checked inductive declaration, and ReplayTest.lean fails in Kernel.Environment.replay with an error.

Actual behavior:

  • lean Forge.lean -o Forge.olean exits 0.
  • lean Use.lean -o Use.olean prints 'boom' does not depend on any axioms and exits 0.
  • leanchecker Forge exits 0 with no output.
  • leanchecker Use exits 0 with no output.
  • leanchecker --fresh Use exits 1 with:
    uncaught exception: while replaying declaration 'boom':
    (kernel) unknown constant 'bad'
    
  • lean ReplayTest.lean exits 0 with:
    replay accepted the map; bad in the replayed environment: false
    

Versions

Identical behavior observed on all five toolchains (Linux x86_64):

Lean (version 4.34.0, x86_64-unknown-linux-gnu, commit 293d5d0c0c3f3dded4688b3ccd6a33939ac5102b, Release)
Lean (version 4.35.0-rc3, x86_64-unknown-linux-gnu, commit 470d5ce1400764999581fd26d5d72b00d990b0f4, Release)
Lean (version 4.36.0, x86_64-unknown-linux-gnu, commit 611c117bfaf7b7ae1cfcf246ce6fba31e8f509e9, Release)
Lean (version 4.37.0-rc1, x86_64-unknown-linux-gnu, commit 0b77e12952ad39077d80d8bd6b66d601f225c321, Release)
Lean (version 4.38.0-nightly-2026-11-22, x86_64-unknown-linux-gnu, commit 7e2fc6a7267dbad33478f18096b4c1c7f1a8746e, Release)

src/Lean/Replay.lean was also checked at master commit 231ef9a (2026-11-23), after #16677; the relevant code is unchanged (not run on a master build).

Additional Information

  • A stored definition is skipped the same way: On all five toolchains, lean ReplayDef.lean exits 0 and prints replay accepted the map; D in the replayed environment is a definition: false; it also prints a panic to stderr: PANIC at Lean.ConstantInfo.inductiveVal! Lean.Declaration:<line>:9: Expected a `ConstantInfo.inductInfo`. (from inductiveVal! on D in the .inductInfo branch), but replay returns normally. D : Type := Type is ill-typed (Type : Type 1); its stored definition is never checked: the .inductInfo branch for bad erases D from remaining/pending and adds bad and D as empty inductives. As with the main reproducer, this depends on bad being visited before D (with names I/D, replay instead refuses D with a type mismatch).

    ReplayDef.lean:

    import Lean
    open Lean
    
    -- `replay` accepts a map holding an ill-typed definition `D : Type := Type`, because a forged inductive `bad`
    -- lists `D` in its `all`
    run_meta show MetaM Unit from do
      let env ← getEnv
      let mut consts : Std.HashMap Name ConstantInfo :=
        env.constants.map₂.foldl (init := ∅) fun consts n info => consts.insert n info
      let bad : InductiveVal := {
        name := `bad, levelParams := [], type := mkSort 1, numParams := 0, numIndices := 0
        all := [`bad, `D], ctors := [], numNested := 0, isRec := false, isUnsafe := false, isReflexive := false }
      let d : DefinitionVal := {
        name := `D, levelParams := [], type := mkSort 1, value := mkSort 1, hints := .abbrev, safety := .safe, all := [`D] }
      consts := consts.insert `bad (.inductInfo bad) |>.insert `D (.defnInfo d)
      let some importEnv := env.importEnv? | unreachable!
      let env' ← importEnv.toKernelEnv.replay consts
      logInfo m!"replay accepted the map; D in the replayed environment is a definition: {(env'.find? `D).any (·.isDefinition)}"
  • Order dependence: Whether leanchecker Forge itself errors depends on the order in which replay visits names. With other names for the forged constant, leanchecker Forge fails with (kernel) constant has already been declared 'Z' (because replay adds Z first, and then the forged entry's branch adds Z again). In those runs, leanchecker Use still exited 0. The name bad is one where both leanchecker Forge and leanchecker Use exit 0.

  • Edited InductiveVal metadata (numIndices): An .olean in which the stored InductiveVal of an honest inductive with one index (inductive EqZ : Nat → Prop | mk : EqZ 0) was edited to numIndices := 0 (by a small program rewriting the module data; not included) was accepted by leanchecker in both default mode and --fresh on 4.34.0 and 4.35.0-rc3. No proof of False was derived from it.

  • Export path (lake check --paranoid checkers, tested on 4.35.0-rc3): The checkers bundled for lake check --paranoid were run directly with the arguments in Lake's Check.lean (lake check itself was not run):

    • For the module proving boom from the forged inductive, leanexport includes the forged constant, and leanchecker-paranoid --silent --from-export, lean4lean --import, nanoda_bin, con-leche, and con-ron all reject it at boom (unknown constant). An honest control export passes all five.
    • For the numIndices-edited variant's export, leanchecker-paranoid --from-export, lean4lean, con-leche, and con-ron accept it and nanoda_bin rejects it (assertion old.aux_data_ck(new)).

Suggested fix

As a possible direction:

  1. In the .inductInfo branch of replayConstant in src/Lean/Replay.lean, reject the entry unless info.all contains name and every name in info.all is a stored, safe .inductInfo (instead of newConstants[n]! / inductiveVal!, which panic and continue).
  2. After replay, for every non-unsafe, non-partial entry of newConstants, require env.find? n to be present and equal to the stored ConstantInfo (for theorems skipped as duplicates by the existing rule, compare name, type, level params, and all only; InductiveVal, QuotVal, and QuotKind may lack BEq instances, so compare their fields).

A general "every stored constant equals the replayed one" post-check was used in an independent replay tool over a library of about 41,000 constants with no false positives.

Recommended prompt to fix it

Fix `Kernel.Environment.replay` / `replayConstant` in `src/Lean/Replay.lean` so stored `.inductInfo` entries are validated against what the kernel generates.
Currently, the `.inductInfo` branch only replays inductives listed in `info.all` and never checks that `name` is in `info.all` or that the stored `InductiveVal` matches the kernel-generated one.
1. In `replayConstant`'s `.inductInfo` branch, reject the entry unless `info.all` contains `name` and every name in `info.all` is a stored, safe `.inductInfo` (instead of `newConstants[n]!` / `inductiveVal!`, which panic and continue).
2. After replay, for every non-unsafe, non-partial entry of `newConstants`, verify that `env.find? n` is present and equal to the stored `ConstantInfo` (for theorems skipped as duplicates by the existing rule, compare name, type, level params, and `all` only; `InductiveVal`, `QuotVal`, and `QuotKind` may lack `BEq` instances, so compare their fields).
3. Add a regression test in `tests/elab/` in the style of `tests/elab/issue16675.lean`:
   - A `run_meta` test building a replay map from the environment plus a forged `.inductInfo` `bad` (`type := False`, `all := [`Z]` for an honest `inductive Z : Prop`), checking with `#guard_msgs` that `replay` errors.
   - A second test mutating an indexed inductive's stored `InductiveVal` (`numIndices := 0` instead of `1`) in the replay map, checking with `#guard_msgs` that `replay` errors.
   - A third test building the `ReplayDef.lean` map (forged inductive `bad` with `all := [`bad, `D]` and a stored definition `D : Type := Type`), checking with `#guard_msgs` that `replay` throws.
4. Keep replay of honest environments (including mutual inductives, nested inductives, and structures) passing.
5. Keep the existing #16677 key-name check intact, and do not change `leanchecker --fresh` or export path behaviour.
6. Run the existing replay and `leanchecker` tests and confirm everything passes.

Happy to help with a regression test or a patch if that is useful.

Activity

  1. added a commit that references this issue on Nov 24, 2026
    e6e2521
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions