Miscompiled `ctorElim` for inductives with `@[computed_field]` enables arbitrary type coercion in safe Lean

Aperta
#16,938 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

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

bug
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:

  1. A zero-import, 100%-pure-safe arbitrary type coercion α → β (no unsafe, sorry, @[extern], @[implemented_by], or native_decide).
  2. Miscompilation of stock Lean.Expr.ctorElim itself (since Lean.Expr has a @[computed_field]).
Context

Discovered while auditing pure-safe Lean 4 compiler soundness on nightly-2026-12-02 (5e756ba2).

Steps to Reproduce
  1. Zero-import pure-safe arbitrary type coercion — paste directly into https://live.lean-lang.org/#project=lean-nightly (or run lean --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
  1. Stock Lean.Expr.ctorElim miscompilation — paste into https://live.lean-lang.org/#project=lean-nightly (or run lean --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

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di leanprover/lean4

Tutte le issue di leanprover/lean4

Issue simili

Altre issue su Compilers

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.