Kernel aborts process with `INTERNAL PANIC: universe level depth is too big` (`lean_level_mk_data`)
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 32/100
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
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
- Save the following self-contained file as
ReproLevelDepthPanic.lean(or paste intohttps://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
})
- Run
lean --trust=0 ReproLevelDepthPanic.lean(orLEAN_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:
- Saturate the cached
depthfield (analogous toapproxDepthinlean_expr_mk_data): Clampdepthat the sentinel0xFFFFFF(note also that bits34..39ofLevel.Dataare currently unused). Across the codebase,get_depth(level)is only used insrc/kernel/level.cppin four places:operator==(fast inequality filter, falling back to structural comparison on ties),is_lt(strict weak ordering key, falling back tokind/hash/children on ties), andmk_max/printon explicit levels (which can fall back to countingsuccs only whenget_depth(l) == 0xFFFFFF). - Pre-check in the C++ kernel and raise
kernel_exception(analogous toget_count_arginsrc/kernel/type_checker.cpp): Keeplean_internal_panicinlean_level_mk_dataas a last-resort safety net, and checkget_depthbefore constructing deeper levels in C++ kernel entry points (instantiateon universe parameters,mk_succ,mk_max,mk_imax), throwing a catchablekernel_exceptioninstead of terminating the process.
- Dominant language
- Lean
- Stars
- 10.5k
- Forks
- 1.2k
- Avg merge
- 19h 36m
- Merged PRs (30d)
- 323
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from leanprover/lean4
-
P-medium
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
leanprover/lean4#16958 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
leanprover/lean4#16897 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
leanprover/lean4#16892 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 1-3 hours Newbie friendliness 86/100
leanprover/lean4#16886 · 1 comment ·
Maintainers usually reply within 1 day
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 78/100
leanprover/lean4#16820 ·
Maintainers usually reply within 1 day
All issues in leanprover/lean4
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
superfly/contextwindow#18 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
Lincoln-cn/JAiRouter#1452 ·
Maintainers usually reply within 1 day
-
accepted
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
future-agi/future-agi#6351 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day