Kernel aborts process with `INTERNAL PANIC: universe level depth is too big` (`lean_level_mk_data`)

Open
#16,891 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
32/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Domain
backend, compilers

Research direction

Start with src/kernel/level.cpp:42–50 and the uses of get_depth named in the issue; compare the suggested approaches with approxDepth in src/kernel/expr.cpp and get_count_arg in src/kernel/type_checker.cpp. Run the supplied ReproLevelDepthPanic.lean with lean --trust=0 to confirm the panic. Done means deep universe levels no longer terminate Lean, the LSP worker, or leanchecker unexpectedly, with the behavior covered by a regression check.

Written by the indexing model from the issue text.

Description

bug P-medium
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

lean_level_mk_data in src/kernel/level.cpp:42–50 packs a universe level's depth into a 24-bit field (d <= 16777215) and calls lean_internal_panic("universe level depth is too big") whenever depth exceeds 16777215:

extern "C" LEAN_EXPORT uint64_t lean_level_mk_data (uint64_t h, object * depth, uint8_t hasMVar, uint8_t hasParam) {
    if (!is_scalar(depth))
        lean_internal_panic("universe level depth is too big");
    size_t d = unbox(depth);
    if (d > 16777215)
        lean_internal_panic("universe level depth is too big");
    ...
}

Because lean_internal_panic immediately terminates the process (std::exit(1) by default, or abort() / SIGABRT with LEAN_ABORT_ON_PANIC=1) rather than raising a catchable kernel exception, a 38-line pure safe, well-typed Lean file (no unsafe, no sorry, no @[extern]) defining a chain of universe-polymorphic definitions d_{i+1}.{u} : Prop := d_i.{succ^4096 u} accumulates level depth $4097 \times 4096 + 1 = 16{,}781{,}313 > 2^{24} - 1$ during whnf and crashes lean --trust=0, the Lean LSP server worker, leanchecker --from-export, and leanchecker-paranoid --from-export.

Context

Discovered while auditing bit-field invariants in src/kernel/level.cpp and src/kernel/expr.cpp against independent kernel implementations. When the same valid environment is exported to lean4export NDJSON, nanoda_bin checks and accepts all declarations cleanly, whereas leanchecker and leanchecker-paranoid terminate with INTERNAL PANIC: universe level depth is too big.

Steps to Reproduce
  1. Save the following self-contained file as ReproLevelDepthPanic.lean (or paste into https://live.lean-lang.org/#project=lean-nightly):
import Lean
open Lean

run_meta do
  let u := Level.param `u
  let mut L := u
  for _ in [:4096] do
    L := Level.succ L
  let Lsucc := Level.succ L
  let prop := Expr.sort .zero
  let polyIdTy := Expr.forallE `P prop (Expr.forallE `p (.bvar 0) (.bvar 1) .default) .default
  let polyIdVal := Expr.lam `P prop (Expr.lam `p (.bvar 0) (.bvar 0) .default) .default
  let d0Val := Expr.app (Expr.lam `x (.sort Lsucc) polyIdTy .default) (.sort L)
  addDecl (.defnDecl {
    name := `d_0, levelParams := [`u], type := prop, value := d0Val,
    hints := .regular 1, safety := .safe
  })
  for i in [1:4097] do
    let prev := Name.mkSimple s!"d_{i - 1}"
    let cur := Name.mkSimple s!"d_{i}"
    addDecl (.defnDecl {
      name := cur, levelParams := [`u], type := prop, value := Expr.const prev [L],
      hints := .regular (i + 1).toUInt32, safety := .safe
    })
  addDecl (.thmDecl {
    name := `levelDepthPanicTrigger, levelParams := [],
    type := Expr.const `d_4096 [.zero], value := polyIdVal
  })
  1. Run lean --trust=0 ReproLevelDepthPanic.lean (or LEAN_ABORT_ON_PANIC=1 lean --trust=0 ReproLevelDepthPanic.lean).

Expected behavior: Either universe levels of arbitrary depth are supported (e.g. saturating the cached depth field or falling back when d >= 2^24 - 1), or exceeding the maximum supported universe level depth raises a structured, catchable kernel exception instead of terminating the lean / LSP / leanchecker process via lean_internal_panic.

Actual behavior: The kernel prints INTERNAL PANIC: universe level depth is too big to stderr and immediately terminates the process (exit 1, or SIGABRT / exit 134 under LEAN_ABORT_ON_PANIC=1).

Versions
Lean (version 4.38.0-nightly-2026-11-26, x86_64-unknown-linux-gnu, commit e1228b8b45918ece1a00d68e5fc5e6a685ca59ee, Release)
Lean (version 4.36.0, x86_64-unknown-linux-gnu, commit 611c117bfaf7b7ae1cfcf246ce6fba31e8f509e9, Release)
Ubuntu 24.04.4 LTS (Linux 6.17.0-1022-azure x86_64)
Additional Information

Because lean_level_mk_data is an extern "C" primitive invoked directly by pure Lean.Level constructors (src/Lean/Level.lean), throwing a C++ exception directly inside lean_level_mk_data is unsafe across Lean FFI frames. We suggest two possible fix directions:

  1. Saturate the cached depth field (analogous to approxDepth in lean_expr_mk_data): Clamp depth at the sentinel 0xFFFFFF (note also that bits 34..39 of Level.Data are currently unused). Across the codebase, get_depth(level) is only used in src/kernel/level.cpp in four places: operator== (fast inequality filter, falling back to structural comparison on ties), is_lt (strict weak ordering key, falling back to kind/hash/children on ties), and mk_max / print on explicit levels (which can fall back to counting succs only when get_depth(l) == 0xFFFFFF).
  2. Pre-check in the C++ kernel and raise kernel_exception (analogous to get_count_arg in src/kernel/type_checker.cpp): Keep lean_internal_panic in lean_level_mk_data as a last-resort safety net, and check get_depth before constructing deeper levels in C++ kernel entry points (instantiate on universe parameters, mk_succ, mk_max, mk_imax), throwing a catchable kernel_exception instead of terminating the process.
Dominant language
Lean
Stars
10.5k
Forks
1.2k
Avg merge
19h 36m
Merged PRs (30d)
323

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from leanprover/lean4

All issues in leanprover/lean4

Similar issues

More Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.