Repository navigation
Description
Disclaimer: this bug was found autonomously by Opus 4.6 using this setup and has not been reviewed by a Lean expert yet. Below is its (unedited) report.
Prerequisites
- Check that your issue is not already filed:
https://github.com/leanprover/lean4/issues - 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, for example on
https://live.lean-lang.org/#project=lean-nightly
(You can also use the settings there to switch to “Lean nightly”)
Description
In src/kernel/type_checker.cpp:
infer_proj, reduce_proj, and lazy_delta_proj_reduction all store projection indices in unsigned after an is_small() check — but is_small() only guarantees < 2^63, not < 2^32.
Example: Expr.proj(S, 2^32 + 1, e) → is_small() passes (fits in 64-bit) → get_small_value() returns 4294967297 as size_t → stored in unsigned → silently becomes 1.
Context
Steps to Reproduce
- Define a structure and construct an Expr.proj with index 2^32 (or any multiple of 2^32 plus k).
- Reduce it via Lean.Kernel.whnf — the kernel silently treats it as index k.
import Lean
structure Pair where
fst : Nat
snd : Bool
open Lean in
#eval show MetaM Unit from do
let val := mkApp2 (mkConst ``Pair.mk) (mkNatLit 42) (mkConst ``Bool.true)
let env ← getEnv
let lctx : LocalContext := {}
let proj0 := Expr.proj ``Pair 0 val
let projBig := Expr.proj ``Pair (2^32) val
match Lean.Kernel.whnf env lctx proj0, Lean.Kernel.whnf env lctx projBig with
| .ok r0, .ok rBig =>
IO.println s!"proj(Pair, 0, Pair.mk 42 true) = {r0}"
IO.println s!"proj(Pair, 2^32, Pair.mk 42 true) = {rBig}"
| _, _ => IO.println "error"
match Lean.Kernel.isDefEq env lctx proj0 projBig with
| .ok b => IO.println s!"Kernel.isDefEq(proj idx=0, proj idx=2^32) = {b}"
| .error _ => IO.println "isDefEq error"Expected behavior: The kernel should reject proj(Pair, 2^32, ...) since Pair only has 2 fields (indices 0 and 1). It should not be definitionally equal to proj(Pair, 0, ...).
Actual behavior:
proj(Pair, 0, Pair.mk 42 true) = 42
proj(Pair, 2^32, Pair.mk 42 true) = 42
Kernel.isDefEq(proj idx=0, proj idx=2^32) = true
Versions
Additional Information
N/A
Impact
Low. Normal Lean code never produces projection indices >= the field count. The bug requires constructing raw Expr.proj nodes via metaprogramming. The Meta layer rejects these, so exploitation would require bypassing Meta (e.g.,addDeclCore(doCheck := false) or crafted .olean files).
Activity
nomeata commentedon Mar 1, 2026
The analysis on why this is low impact is not right: we need the kernel to correctly reject code that was written directly, bypassing the elaborator.
The question is: does this threaten soundness: more concretely: does the kernel do this conversion uniformly wrong (i.e. type checking and reduction?). Then it's probably low impact. Or does is it possible to get into a bad state?
Hmm, probably using the structure eta rule we might have a problem. If we wait long enough for a 2^32 structure to be processed by the kernel.
Is this only a problem on 32bit architectures?
Maybe the kernel should enforce bounds on the number of fields and rule out such huge structures.
tcosmo commentedon Mar 1, 2026
I don't mean to be passing on slop but here are the answers provided by the agent who worked on this:
tcosmo commentedon Mar 3, 2026
Apparently, the issue is more general, see below.
The same pattern — get_small_value() returns size_t (64-bit) stored in unsigned (32-bit) — exists throughout src/kernel/declaration.h and src/kernel/inductive.cpp. The is_small() guard only rejects values ≥ 2^63 (on 64-bit systems), so values in the range [2^32, 2^63) are silently truncated.
Affected locations in src/kernel/declaration.h:
inductive_val (line 297–302):
constructor_val (line 325–327):
recursor_rule (line 345):
recursor_val (line 375–378):
Affected locations in src/kernel/inductive.cpp:
Affected consumers in the kernel:
rec_val.get_nminors() to index into rec_args. Truncation causes wrong arguments to be passed to the recursor rule RHS.
nomeata commentedon Mar 3, 2026
Copying what I wrote on zulip for those following here:
Thanks for pointing these out. For these code paths to be reachable, the user would first have to manage to get an inductive with 2^32 fields or parameters or indices through the declaration type checker, which is unlikely to be practically possible.
At some point we should probably still clean this up and use
size_teverywhere where appropriate. Then the “larger things couldn’t have fit in memory, if each element has its own address” pidgeon-hole argument used by the this implementation becomes robust.fix: stop truncating kernel counts and indices to 32 bits (leanprover…