Repository navigation
Description
Prerequisites
- Check that your issue is not already filed. (Related, all closed by fix: reject replay map entries stored under a different name #16677, which rejects a replay-map key that differs from the stored constant's name: leanchecker accepts constructor entries whose map key differs from the embedded name #15100 constructors, leanchecker treats arbitrary quotInfo entries as canonical quotient declarations #15101 quotients, leanchecker: replay map does not check constNames[i] against constants[i].name (recursor entry stored under a different key), recursor analogue of #15100 #16675 recursors. This case is different: the forged entry is stored under its own name, so fix: reject replay map entries stored under a different name #16677 does not apply.)
- Reduce the issue to a minimal, self-contained, reproducible test case. Avoid dependencies to Mathlib or Batteries.
- Test your test case against the latest nightly release. (nightly-2026-11-22; also 4.34.0, 4.35.0-rc3, 4.36.0, 4.37.0-rc1.)
- When reporting a perceived soundness bug: Does it affect
lake check --paranoid? -> No for the proof of False (details in Additional Information). Kernel soundness is not in question; this is about what default-modeleancheckerandKernel.Environment.replayverify.
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 boomReplayTest.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.leanExpected 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.oleanexits 0.lean Use.lean -o Use.oleanprints'boom' does not depend on any axiomsand exits 0.leanchecker Forgeexits 0 with no output.leanchecker Useexits 0 with no output.leanchecker --fresh Useexits 1 with:uncaught exception: while replaying declaration 'boom': (kernel) unknown constant 'bad'lean ReplayTest.leanexits 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.leanexits 0 and printsreplay 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`.(frominductiveVal!onDin the.inductInfobranch), butreplayreturns normally.D : Type := Typeis ill-typed (Type : Type 1); its stored definition is never checked: the.inductInfobranch forbaderasesDfromremaining/pendingand addsbadandDas empty inductives. As with the main reproducer, this depends onbadbeing visited beforeD(with namesI/D,replayinstead refusesDwith 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 Forgeitself errors depends on the order in whichreplayvisits names. With other names for the forged constant,leanchecker Forgefails with(kernel) constant has already been declared 'Z'(becausereplayaddsZfirst, and then the forged entry's branch addsZagain). In those runs,leanchecker Usestill exited 0. The namebadis one where bothleanchecker Forgeandleanchecker Useexit 0. -
Edited
InductiveValmetadata (numIndices): An.oleanin which the storedInductiveValof an honest inductive with one index (inductive EqZ : Nat → Prop | mk : EqZ 0) was edited tonumIndices := 0(by a small program rewriting the module data; not included) was accepted byleancheckerin both default mode and--freshon 4.34.0 and 4.35.0-rc3. No proof ofFalsewas derived from it. -
Export path (
lake check --paranoidcheckers, tested on 4.35.0-rc3): The checkers bundled forlake check --paranoidwere run directly with the arguments in Lake'sCheck.lean(lake checkitself was not run):- For the module proving
boomfrom the forged inductive,leanexportincludes the forged constant, andleanchecker-paranoid --silent --from-export,lean4lean --import,nanoda_bin,con-leche, andcon-ronall reject it atboom(unknown constant). An honest control export passes all five. - For the
numIndices-edited variant's export,leanchecker-paranoid --from-export,lean4lean,con-leche, andcon-ronaccept it andnanoda_binrejects it (assertionold.aux_data_ck(new)).
- For the module proving
Suggested fix
As a possible direction:
- In the
.inductInfobranch ofreplayConstantinsrc/Lean/Replay.lean, reject the entry unlessinfo.allcontainsnameand every name ininfo.allis a stored, safe.inductInfo(instead ofnewConstants[n]!/inductiveVal!, which panic and continue). - After replay, for every non-unsafe, non-partial entry of
newConstants, requireenv.find? nto be present and equal to the storedConstantInfo(for theorems skipped as duplicates by the existing rule, compare name, type, level params, andallonly;InductiveVal,QuotVal, andQuotKindmay lackBEqinstances, 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
fix: reject malformed inductive entries in `Kernel.Environment.replay` (