Skip to content

proj_idx truncation: size_t → unsigned #12746

Closed

Description

@tcosmo

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

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.

proj_truncation_test.lean

Context

Zulip thread

Steps to Reproduce

  1. Define a structure and construct an Expr.proj with index 2^32 (or any multiple of 2^32 plus k).
  2. 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

See proj_truncation_test.lean

Versions

  • Lean 4.28.0 (commit 7e01a1b)
  • Lean 4.29.0-rc2 (commit 83e54b6)
  • Linux x86-64

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

  1. added
    bug
    on Mar 1, 2026
  2. nomeata commented on Mar 1, 2026

    @nomeata
    Collaborator

    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.

  3. tcosmo commented on Mar 1, 2026

    @tcosmo
    Author

    I don't mean to be passing on slop but here are the answers provided by the agent who worked on this:

    Image
  4. tcosmo commented on Mar 3, 2026

    @tcosmo
    Author

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

    • get_nparams() — line 297
    • get_nindices() — line 298
    • get_nnested() — line 302
      constructor_val (line 325–327):
    • get_cidx() — line 325
    • get_nparams() — line 326
    • get_nfields() — line 327
      recursor_rule (line 345):
    • get_nfields() — line 345

    recursor_val (line 375–378):

    • get_nparams() — line 375
    • get_nindices() — line 376
    • get_nmotives() — line 377
    • get_nminors() — line 378
    • get_major_idx() — line 379 (sum of four truncated values)

    Affected locations in src/kernel/inductive.cpp:

    • m_nparams declared as unsigned (line 130), assigned from get_small_value() (line 167)
    • m_nindices is buffer (line 133)
    • m_nnested is unsigned (line 148)

    Affected consumers in the kernel:

    1. reduce_proj_core (type_checker.cpp:369): unsigned nparams = mk_info.to_constructor_val().get_nparams() — truncated nparams causes args[nparams + idx] to access the wrong constructor argument during projection reduction.
    2. inductive_reduce_rec (inductive.h:106,110): Uses rec_val.get_nparams() + rec_val.get_nmotives() +
      rec_val.get_nminors() to index into rec_args. Truncation causes wrong arguments to be passed to the recursor rule RHS.
    3. try_eta_struct_core (type_checker.cpp:801): get_app_num_args(s) != f_val.get_nparams() + f_val.get_nfields() — unsigned arithmetic with truncated values could cause false matches. The projection loop at line 806 would also project wrong fields.
    4. is_def_eq_unit_like (type_checker.cpp:1053): ctor_val.get_nfields() != 0 — if nfields is a multiple of 2^32, it truncates to 0, and the kernel treats a type with fields as unit-like (all values equal). This is a potential soundness concern.
    5. expand_eta_struct (inductive.cpp:107): Loop for (unsigned i = 0; i < ctor_val.get_nfields(); i++) with truncated nfields iterates wrong number of times.
  5. nomeata commented on Mar 3, 2026

    @nomeata
    Collaborator

    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_t everywhere 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.

  6. added
    P-medium
    on Mar 23, 2026
  7. added a commit that references this issue on Nov 5, 2026
    de275a3
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

    P-mediumbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions