Skip to content

fix: panic instead of wrapping counts in sharecommon_quick_fn - #15289

Merged
Kha merged 2 commits into
masterfrom
sharecommon-rc-overflow
Sep 24, 2026
Merged

Kha merged 2 commits into
masterfrom
sharecommon-rc-overflow

Conversation

@Kha

@Kha Kha commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

This PR makes maximal sharing, including the kernel's sharing of every theorem it checks, panic when a shared subterm gains more than INT_MAX references, instead of eventually freeing the subterm while it is still referenced. On inputs north of 100GB, the possibility of triggering undefined behavior in the official kernel this way, which could be extended into a proof of False, could not be excluded. Other kernels such as nanoda or con-leche not based on the Lean runtime or not making use of this specific function were not affected.

sharecommon_quick_fn takes references to the objects it creates with a raw increment, skipping the sign test of lean_inc_ref because these objects are single-threaded. Past INT_MAX that increment wrapped the count into the sticky range, and further cache hits walked it back out of that range. The increment now checks its overflow flag and panics, so the hit path keeps its single add plus a branch that is never taken.

This PR makes maximal sharing, including the kernel's sharing of every theorem it checks, panic when a shared subterm gains more than `INT_MAX` references, instead of eventually freeing the subterm while it is still referenced.

`sharecommon_quick_fn` takes references to the objects it creates with a raw increment, skipping the sign test of `lean_inc_ref` because these objects are single-threaded. Past `INT_MAX` that increment wrapped the count into the sticky range, and further cache hits walked it back out of that range. The increment now checks its overflow flag and panics, so the hit path keeps its single add plus a branch that is never taken.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@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 Sep 23, 2026
@Kha

Kha commented Sep 24, 2026

Copy link
Copy Markdown
Member Author

!bench

@leanprover-radar

leanprover-radar commented Sep 24, 2026 •

Copy link
Copy Markdown

Benchmark results for 6e3a708 against 3baf820 are in. No significant results found. @Kha

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

No significant changes detected.

@Kha
Kha marked this pull request as ready for review September 24, 2026 12:38
@Kha
Kha added this pull request to the merge queue Sep 24, 2026
@Kha
Kha removed this pull request from the merge queue due to a manual request Sep 24, 2026
@Kha Kha added runtime-soundness Safety bug in the Lean runtime that could be used to break soundness of the official kernel changelog-compiler Compiler, runtime, and FFI labels Sep 24, 2026
@Kha
Kha enabled auto-merge September 24, 2026 12:46
@Kha
Kha added this pull request to the merge queue Sep 24, 2026
@Kha
Kha removed this pull request from the merge queue due to a manual request Sep 24, 2026
Use `__builtin_add_overflow` only under GCC and clang, as `lean.h` does, with an explicit `INT_MAX` test otherwise.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@Kha
Kha enabled auto-merge September 24, 2026 12:58
@Kha
Kha added this pull request to the merge queue Sep 24, 2026
Merged via the queue into master with commit 1a63aff Sep 24, 2026
17 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

The backport to releases/v4.34.0 failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-releases/v4.34.0 releases/v4.34.0
# Navigate to the new working tree
cd .worktrees/backport-releases/v4.34.0
# Create a new branch
git switch --create backport-15289-to-releases/v4.34.0
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 1a63affc1b788fa3b9823ef3ee9805f03501e9b8
# Push it to GitHub
git push --set-upstream origin backport-15289-to-releases/v4.34.0
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-releases/v4.34.0

Then, create a pull request where the base branch is releases/v4.34.0 and the compare/head branch is backport-15289-to-releases/v4.34.0.

Kha added a commit that referenced this pull request Sep 24, 2026
…`sharecommon_quick_fn` (#15303)

Backport 1a63aff from #15289.

Co-authored-by: Sebastian Ullrich <[email protected]>
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

The backport to releases/v4.34.0 failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-releases/v4.34.0 releases/v4.34.0
# Navigate to the new working tree
cd .worktrees/backport-releases/v4.34.0
# Create a new branch
git switch --create backport-15289-to-releases/v4.34.0
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 1a63affc1b788fa3b9823ef3ee9805f03501e9b8
# Push it to GitHub
git push --set-upstream origin backport-15289-to-releases/v4.34.0
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-releases/v4.34.0

Then, create a pull request where the base branch is releases/v4.34.0 and the compare/head branch is backport-15289-to-releases/v4.34.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport releases/v4.34.0 backport releases/v4.35.0 changelog-compiler Compiler, runtime, and FFI runtime-soundness Safety bug in the Lean runtime that could be used to break soundness of the official kernel 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.

2 participants