Skip to content

feat: add a leanchecker-paranoid binary with mimalloc mitigations - #14884

Merged
Kha merged 3 commits into
masterfrom
leanchecker-paranoid
Aug 26, 2026
Merged

Kha merged 3 commits into
masterfrom
leanchecker-paranoid

Conversation

@Kha

@Kha Kha commented Aug 21, 2026

Copy link
Copy Markdown
Member

This PR adds bin/leanchecker-paranoid, a variant of leanchecker built with allocator hardening.

leanchecker-paranoid is built with mimalloc's memory-safety mitigations at MI_SECURE=3. Level 3 enables encoded free lists and the padding they imply. Encoding is the part that matters for Lean: lean_object.m_rc sits at offset 0, exactly where mimalloc keeps a freed block's free-list link, so a reference-count operation through a stale pointer writes into allocator metadata rather than into dead payload. Over 40 runs of a probe modelling that, level 3 eliminates uncontrolled crashes entirely and converts them into a checked abort raised before a bad block is handed out. Padding additionally catches out-of-bounds constructor and array writes. The cost is roughly +26% wall-clock and +17.5% heap size.

The binary is not exposed by elan; instead, it will be directly integrated into upcoming checker workflows.

This PR adds `leanchecker-paranoid`, a variant of `leanchecker` whose allocator is built with mimalloc's memory-safety mitigations at `MI_SECURE=3`. It is not built by default; release CI builds it explicitly so it ships alongside the ordinary binary, which is unaffected.

Level 3 enables encoded free lists and the padding they imply. Encoding is the part that matters for Lean: `lean_object.m_rc` sits at offset 0, exactly where mimalloc keeps a freed block's free-list link, so a reference-count operation through a stale pointer writes into allocator metadata rather than into dead payload. Over 40 runs of a probe modelling that, level 3 eliminates uncontrolled crashes entirely and converts them into a checked abort raised before a bad block is handed out. Padding additionally catches out-of-bounds constructor and array writes. The cost is roughly +26% wall and +17.5% of the live heap, paid by no other binary.

The binary has to be linked statically: `leanshared` owns the allocator of every dynamically linked binary and is linked `-Bsymbolic`, so its internal `mi_*` calls cannot be redirected from an executable. Only mimalloc's own translation unit depends on `MI_SECURE`, so `add_mimalloc_variant` compiles it once per (mitigation level, TLS model) the build needs and each runtime archive consumes the matching object. Those flags cannot live on the shared source instead, because source properties are directory-scoped and CMake emits them after target options, so a per-target override would silently lose.

`-rdynamic` is dropped here through the new `CMAKE_DYN_EXE_LINKER_FLAGS`, which collects the flags only dynamically linked binaries want; it exists for the interpreter, and on a static binary it defeats `--gc-sections` and inflates the symbol tables. With it gone and the binary stripped, the result is 78 MB on disk and about 19 MiB compressed.

Finally, the hardened mimalloc wins only because its archive precedes `-lleanrt`. A disturbed order would link successfully with no mitigations and no diagnostic, so the link rule asserts the property on the finished binary and fails the build if it is missing.
@Kha Kha added the release-ci Enable all CI checks for a PR, like is done for releases label Aug 21, 2026
@Kha
Kha requested a review from kim-em as a code owner August 21, 2026 15:55
@github-actions github-actions Bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Aug 21, 2026
@mathlib-lean-pr-testing

mathlib-lean-pr-testing Bot commented Aug 21, 2026 •

Copy link
Copy Markdown

Mathlib CI status (docs):

  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase f8facdb3303012c0d6806661867fc86e68234641 --onto 16e77c407779fde9a649adf3478204d1915371a3. You can force Mathlib CI using the force-mathlib-ci label. (2026-08-21 17:04:51)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase f8facdb3303012c0d6806661867fc86e68234641 --onto 4620e4d946957ae7c582764c79eb93cc779f77e2. You can force Mathlib CI using the force-mathlib-ci label. (2026-08-25 16:30:47)

@leanprover-bot

Copy link
Copy Markdown
Collaborator

Reference manual CI status:

  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase f8facdb3303012c0d6806661867fc86e68234641 --onto 16e77c407779fde9a649adf3478204d1915371a3. You can force reference manual CI using the force-manual-ci label. (2026-08-21 17:04:52)

@Kha

Kha commented Aug 25, 2026

Copy link
Copy Markdown
Member Author

!bench

@leanprover-radar

leanprover-radar commented Aug 25, 2026 •

Copy link
Copy Markdown

Benchmark results for 837df2e against f8facdb are in. No significant results found. @Kha

  • 🟥 build//instructions: +221.2M (+0.00%)

Small changes (1✅)

  • ✅ vcgen/ReaderState/1600/kernel//wall-clock: -20ms (-5.91%)

@Kha

Kha commented Aug 25, 2026

Copy link
Copy Markdown
Member Author

!bench

@Kha Kha removed the release-ci Enable all CI checks for a PR, like is done for releases label Aug 25, 2026
@leanprover-radar

leanprover-radar commented Aug 25, 2026 •

Copy link
Copy Markdown

Benchmark results for 33b4a76 against f8facdb are in. There are significant results. @Kha

  • 🟥 build exited with code 1

No significant changes detected.

Stages after the first skip `src/runtime/CMakeLists.txt` entirely and import the previous stage's C++ archives instead, so `leanchecker-paranoid` ended up with an unresolvable dependency there: CMake fell back to treating the target name as a file name, and `make -C build/stage2 leanchecker-paranoid` failed with `No rule to make target 'mimalloc_paranoid'`. This hit the size benchmark, which runs in stage 2, and would have hit release CI on any change to `stdlib_flags.h`.

Move the stage condition into `runtime/CMakeLists.txt`: the mimalloc setup and the `mimalloc_paranoid` archive are now defined in every stage, and the file returns early before the `leanrt` libraries that later stages do import. The archive is not among those imports because the previous stage does not build it by default.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@Kha

Kha commented Aug 26, 2026

Copy link
Copy Markdown
Member Author

!bench

@leanprover-radar

leanprover-radar commented Aug 26, 2026 •

Copy link
Copy Markdown

Benchmark results for ee9c0f7 against f8facdb are in. There are significant results. @Kha

  • 🟥 build//instructions: +961.5M (+0.01%)

Large changes (1🟥)

  • 🟥 size/install//bytes: +79MiB (+2.53%)

Small changes (1✅)

  • ✅ vcgen/ReaderState/1600/kernel//wall-clock: -17ms (-5.07%)

@Kha
Kha added this pull request to the merge queue Aug 26, 2026
Merged via the queue into master with commit ec5816f Aug 26, 2026
20 checks passed
@nomeata nomeata mentioned this pull request Aug 29, 2026
FawadHa1der added a commit to FawadHa1der/lean4 that referenced this pull request Oct 23, 2026
298 upstream commits. The line's merge base with v4.35.0 is master (leanprover#14729); the line also carries the
v4.34.0 release branch's backports (leanprover#14788, leanprover#14791, leanprover#14833, leanprover#14838, leanprover#14849, leanprover#14477's intrinsic-
verification change, leanprover#14938, leanprover#15104), which v4.35.0 holds as the master originals, several reworked
since. Eighteen files conflicted; how each was resolved:

- Upstream-only files (no fork commit touches them): v4.35.0's version, byte for byte —
  CMakeLists.txt (version 4.35), src/Init/{Core,Prelude}.lean, src/Lean/Compiler/LCNF/ToDecl.lean,
  src/Lean/Elab/{BuiltinDo/For,Do/Basic,Tactic/Do/Contract}.lean, src/Lean/Server/InfoUtils.lean
  (drops the v4.34.0-only backport leanprover#15104, which v4.35.0 does not carry), tests/elab/
  {csimp_macroinline,experimentalIntrinsic,intrinsicVerification}.lean; tests/elab/14383.lean (a
  v4.34.0-backport test v4.35.0 does not have) removed. Afterwards every upstream-only file equals
  v4.35.0; the 57 files that differ are the fork's own.
- src/include/lean/lean.h, src/runtime/object.cpp: the conflicts were upstream's reference-count
  ("sticky"/rc_model) code against its v4.34.0 backport: v4.35.0's side; the line's older
  lean_dec_ref_cold (backport, with LEAN_LAZY_RC, which v4.35.0 removed) deleted so v4.35.0's is the
  only one. Kept: the fork's pointer-width LEAN_SCALAR_PTR_LITERAL (lean.h) and task manager
  (0035/0038, object.cpp) — each file now differs from v4.35.0 by exactly those.
- src/stdlib.make.in: the `lean` link keeps the fork's ${LEAN_WASM_SYMTAB} (0039) and takes
  upstream's new ${CMAKE_DYN_EXE_LINKER_FLAGS_MAKE} (leanprover#14884), which is empty under Emscripten
  (-rdynamic and the shared libraries are added only outside it) and needed by native64.
- src/Lean/Shell.lean: both imports (the fork's Lean.Compiler.InitAttr, upstream's Std.Async.Process).
- src/runtime/uv/event_loop.cpp: v4.35.0's line (a comment differed).
- .github/workflows/{build-template,ci}.yml: the fork's CI.
- stage0/: v4.35.0's, with the line's two pointer-width gates re-applied by hand (the patch's context
  moved); wasm64-build/stage0-line-edits.patch refreshed from `git diff --text v4.35.0 -- stage0`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog-other toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants