Miscompiled `ctorElim` for inductives with `@[computed_field]` enables arbitrary type coercion in safe Lean
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 20/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Ambito
- compilers
Direzione di ricerca
Start with the ordering of mkAuxConstructions, applyComputedFields, and elabInductiveViewsFinalize in src/Lean/Elab/MutualInductive.lean, then compare ctorElim handling with constructor and casesOn overrides in src/Lean/Elab/ComputedFields.lean. Run both reproductions with lean --trust=0; done means evaluation agrees with the kernel proofs and neither example permits the reported coercion.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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”) - When reporting a perceived soundness bug: Does it affect
lake check --paranoid?
If in doubt, please reach out on [email protected] for responsible disclosure.
Description
In src/Lean/Elab/MutualInductive.lean, elabInductiveViews runs mkAuxConstructions (lines 1554–1560, which constructs and compiles <Ind>.ctorElim) before elabInductiveViewsFinalize runs applyComputedFields views (line 1669, which carries the comment -- NOTE: any generated code before this line is invalid).
ComputedFields.setComputedFields (src/Lean/Elab/ComputedFields.lean) installs @[implemented_by] overrides for constructors, casesOn, and computed fields to account for the prepended computed-field slots in the runtime layout, but never overrides or recompiles <Ind>.ctorElim. As a result, compiled <Ind>.ctorElim projects oproj[0] (the prepended computed field) as user field 0, yielding:
- A zero-import, 100%-pure-safe arbitrary type coercion
α → β(nounsafe,sorry,@[extern],@[implemented_by], ornative_decide). - Miscompilation of stock
Lean.Expr.ctorElimitself (sinceLean.Exprhas a@[computed_field]).
Context
Discovered while auditing pure-safe Lean 4 compiler soundness on nightly-2026-12-02 (5e756ba2).
Steps to Reproduce
- Zero-import pure-safe arbitrary type coercion — paste directly into
https://live.lean-lang.org/#project=lean-nightly(or runlean --trust=0 Repro.lean):
inductive CastBox (α β : Type) where
| empty
| mk (x : Option β) (a : α)
with
@[computed_field] cf : (α β : Type) → CastBox α β → Option α
| _, _, .empty => none
| _, _, .mk _ a => some a
@[noinline]
def callElim {α β : Type} (i : Nat) (b : CastBox α β) (h : i = b.ctorIdx)
(k : @CastBox.ctorElimType α β (fun _ => Option β) i) : Option β :=
CastBox.ctorElim (motive := fun _ => Option β) i b h k
def coerce! {α β : Type} [Inhabited β] (a : α) : β :=
(callElim 1 (CastBox.mk (α := α) (β := β) none a) rfl (PULift.up (fun x _ => x))).getD default
def forgedBool : Bool := coerce! (β := Bool) (1 : Nat)
theorem kernel_eq : forgedBool = false := rfl
#eval forgedBool
- Stock
Lean.Expr.ctorElimmiscompilation — paste intohttps://live.lean-lang.org/#project=lean-nightly(or runlean --trust=0 ReproExpr.lean):
import Lean.Expr
open Lean
@[noinline]
def callExprElim (i : Nat) (e : Expr) (h : i = e.ctorIdx)
(k : @Expr.ctorElimType (fun _ => Bool) i) : Bool :=
Expr.ctorElim (motive := fun _ => Bool) i e h k
def runtimeVal : Bool :=
callExprElim 8 (.letE `x (.sort .zero) (.sort .zero) (.bvar 49) false) rfl
(PULift.up (fun _ _ _ _ nondep => nondep))
theorem kernel_eq : runtimeVal = false := rfl
#eval runtimeVal
Expected behavior: #eval forgedBool prints false (matching kernel_eq : forgedBool = false := rfl), and #eval runtimeVal prints false (matching kernel_eq : runtimeVal = false := rfl).
Actual behavior: #eval forgedBool prints true (coercing 1 : Nat into Bool), and #eval runtimeVal prints true.
Versions
"4.38.0-nightly-2026-12-02"
Lean (version 4.38.0-nightly-2026-12-02, x86_64-unknown-linux-gnu, commit 5e756ba2526bd88b18fa8b01588964fe848e0f91, Release)
Ubuntu 24.04.4 LTS (Linux 6.17.0-1022-azure x86_64)
Suggested fix
Either defer mkCtorElim (and any other compiled auxiliary constructions whose projections depend on constructor field offsets) until after applyComputedFields in elabInductiveViewsFinalize, or override/recompile <Ind>.ctorElim inside ComputedFields.setComputedFields (as already done for casesOn and constructors).
- Lingua principale
- Lean
- Stelle
- 10.5k
- Fork
- 1.2k
- Merge medio
- 19h 36m
- PR unite (30g)
- 323
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di leanprover/lean4
-
P-medium
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
leanprover/lean4#16958 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
leanprover/lean4#16897 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
leanprover/lean4#16892 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 1-3 ore Idoneità per principianti 86/100
leanprover/lean4#16886 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 78/100
leanprover/lean4#16820 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di leanprover/lean4
Issue simili
-
chat compiler P1
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
HerringtonDarkholme/hd-lang#2793 ·
I maintainer di solito rispondono entro 1 giorno
-
area-dart-model gardening
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 87/100
I maintainer di solito rispondono entro 1 giorno
-
`shift(x, y)` with a uint256 shift amount >= 2**255 shifts in the wrong directionForse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 3 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 71/100
facebookexperimental/semcode#195 ·
I maintainer di solito rispondono entro 2 giorni