Skip to content

fix: mark the children of objects whose single-threaded count overflowed - #15288

Merged
Kha merged 1 commit into
masterfrom
rc-sticky-mark-mt
Sep 23, 2026
Merged

Kha merged 1 commit into
masterfrom
rc-sticky-mark-mt

Conversation

@Kha

@Kha Kha commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

This PR fixes potential undefined behavior when an object with a huge number of incoming references is shared between threads. The official kernel does not use multithreading in its default configuration (as used by comparator and lake check/compare), but other Lean-based checkers such as con-leche might be affected.

Specifically, it makes sharing an object with other threads mark everything it points to as thread-shared even when the object's reference count has overflowed. Previously such objects were shared without their children, so other threads could update the children's reference counts non-atomically and free them while still in use.

Overflowing single-threaded counts land at or below the new LEAN_RC_STUCK_ST: the inline increment wraps below it, and lean_inc_ref_huge_n freezes counts there instead of at LEAN_RC_STICKY. lean_mark_mt treats counts at or below it as unshared, visits their children, and freezes the object's count at LEAN_RC_STICKY. A frozen thread-shared count only reaches LEAN_RC_STUCK_ST with more than 4094 maximal increments in flight at once, two fewer than the sticky range already allows before a frozen count wraps.

lean_mark_mt also freezes single-threaded counts too large for the live thread-shared range at LEAN_RC_STICKY rather than negating them. Negating a count near INT_MAX would land at or below LEAN_RC_STUCK_ST, where a later lean_mark_mt would take the shared object for an unshared one and walk it while other threads use it.

The model gains isUnshared and markMtRc for is_unshared and the count lean_mark_mt leaves on each object it visits. incRefN_spec and incRefHugeMt_spec now state that an overflowing single-threaded count lands in the band isStuckSt reserves and that a thread-shared count never reaches it, and markMtRc_shared, decRef_stays_shared and frozen_shared_count_stays_shared show that a shared count never reads as unshared again. rc_sticky checks sharing after each overflow route and with a count near INT_MAX, as well as a frozen shared count drifted by the most increments in flight the bound admits.

This PR makes sharing an object with other threads mark everything it points to as thread-shared even when the object's reference count has overflowed. Previously such objects were shared without their children, so other threads could update the children's reference counts non-atomically and free them while still in use.

Overflowing single-threaded counts land at or below the new `LEAN_RC_STUCK_ST`: the inline increment wraps below it, and `lean_inc_ref_huge_n` freezes counts there instead of at `LEAN_RC_STICKY`. `lean_mark_mt` treats counts at or below it as unshared, visits their children, and freezes the object's count at `LEAN_RC_STICKY`. A frozen thread-shared count only reaches `LEAN_RC_STUCK_ST` with more than 4094 maximal increments in flight at once, two fewer than the sticky range already allows before a frozen count wraps.

`lean_mark_mt` also freezes single-threaded counts too large for the live thread-shared range at `LEAN_RC_STICKY` rather than negating them. Negating a count near `INT_MAX` would land at or below `LEAN_RC_STUCK_ST`, where a later `lean_mark_mt` would take the shared object for an unshared one and walk it while other threads use it.

The model gains `isUnshared` and `markMtRc` for `is_unshared` and the count `lean_mark_mt` leaves on each object it visits. `incRefN_spec` and `incRefHugeMt_spec` now state that an overflowing single-threaded count lands in the band `isStuckSt` reserves and that a thread-shared count never reaches it, and `markMtRc_shared`, `decRef_stays_shared` and `frozen_shared_count_stays_shared` show that a shared count never reads as unshared again. `rc_sticky` checks sharing after each overflow route and with a count near `INT_MAX`, as well as a frozen shared count drifted by the most increments in flight the bound admits.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@Kha Kha added changelog-compiler Compiler, runtime, and FFI release-ci Enable all CI checks for a PR, like is done for releases labels Sep 23, 2026
@Kha
Kha enabled auto-merge September 23, 2026 14:35
@Kha
Kha added this pull request to the merge queue Sep 23, 2026
@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
Merged via the queue into master with commit 2b21d8b Sep 23, 2026
35 of 40 checks passed
@Kha Kha added runtime-safety Safety bug in the Lean runtime, does not affect the official kernel backport releases/v4.34.0 backport releases/v4.35.0 labels Sep 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

The backport to releases/v4.35.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.35.0 releases/v4.35.0
# Navigate to the new working tree
cd .worktrees/backport-releases/v4.35.0
# Create a new branch
git switch --create backport-15288-to-releases/v4.35.0
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 2b21d8b2653388d34c0a4d1509b0982dbf499d14
# Push it to GitHub
git push --set-upstream origin backport-15288-to-releases/v4.35.0
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-releases/v4.35.0

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

@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-15288-to-releases/v4.34.0
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 2b21d8b2653388d34c0a4d1509b0982dbf499d14
# Push it to GitHub
git push --set-upstream origin backport-15288-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-15288-to-releases/v4.34.0.

Kha added a commit that referenced this pull request Sep 24, 2026
…ingle-threaded count overflowed (#15306)

Backport 2b21d8b from #15288.

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
Kha added a commit that referenced this pull request Sep 24, 2026
…wed (#15288)

This PR fixes potential undefined behavior when an object with a huge
number of incoming references is shared between threads.

Specifically, it makes sharing an object with other threads mark
everything it points to as thread-shared even when the object's
reference count has overflowed. Previously such objects were shared
without their children, so other threads could update the children's
reference counts non-atomically and free them while still in use.

Overflowing single-threaded counts land at or below the new
`LEAN_RC_STUCK_ST`: the inline increment wraps below it, and
`lean_inc_ref_huge_n` freezes counts there instead of at
`LEAN_RC_STICKY`. `lean_mark_mt` treats counts at or below it as
unshared, visits their children, and freezes the object's count at
`LEAN_RC_STICKY`. A frozen thread-shared count only reaches
`LEAN_RC_STUCK_ST` with more than 4094 maximal increments in flight at
once, two fewer than the sticky range already allows before a frozen
count wraps.

`lean_mark_mt` also freezes single-threaded counts too large for the
live thread-shared range at `LEAN_RC_STICKY` rather than negating them.
Negating a count near `INT_MAX` would land at or below
`LEAN_RC_STUCK_ST`, where a later `lean_mark_mt` would take the shared
object for an unshared one and walk it while other threads use it.

The model gains `isUnshared` and `markMtRc` for `is_unshared` and the
count `lean_mark_mt` leaves on each object it visits. `incRefN_spec` and
`incRefHugeMt_spec` now state that an overflowing single-threaded count
lands in the band `isStuckSt` reserves and that a thread-shared count
never reaches it, and `markMtRc_shared`, `decRef_stays_shared` and
`frozen_shared_count_stays_shared` show that a shared count never reads
as unshared again. `rc_sticky` checks sharing after each overflow route
and with a count near `INT_MAX`, as well as a frozen shared count
drifted by the most increments in flight the bound admits.

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
(cherry picked from commit 2b21d8b)
hyperpolymath added a commit to hyperpolymath/marches that referenced this pull request Oct 14, 2026
Follow-up to the CodeRabbit review on #36 (thread
on `lean-toolchain`, flagged Major / security): pin v4.34.1, the patched
release of the 4.34 line.

## Verified against the primary source, not the bot's summary

`git log v4.34.0..v4.34.1` in leanprover/lean4 shows:

- three runtime reference-count fixes:
  - leanprover/lean4#15241: preserve sticky counts in deletion cascades
- leanprover/lean4#15288: mark the children of objects whose
single-threaded count overflowed
- leanprover/lean4#15307: panic instead of wrapping counts in
`sharecommon_quick_fn`
- a `BitVec.ofNatClamp` backport
- `chore: prepare release` (`5045d00`, 2026-09-24)

marches checks only its own proofs, not untrusted input, so the
practical risk was low. Still, this is a same-line patch bump, it costs
nothing, and it keeps the estate's 4.34 soundness floor on the patched
build.

## Change

- `lean-toolchain`: `v4.34.0` → `v4.34.1`
- `README.adoc`: the two version mentions
- `STATE.adoc`: a dated entry. The v4.34.0 results are now marked
historical.

No source change.

## Gates (v4.34.1)

```
$ rm -rf .lake && lake build
Build completed successfully (15 jobs).
$ grep -rnw sorry Marches/ Marches.lean ; echo $?
1
$ lake env lean Marches/BadGadget.lean
[(false, false), (true, true), (false, false), (true, true)]
```

## Note on the stack

#38 (rung-3 docs) is independent of this PR. Both
add a `STATE.adoc` entry at the top, so whichever merges second will
need a trivial merge, which I'll handle.

CI here shows the same two pre-existing reds as `main` (`CI`
`startup_failure` from `actions.lock` drift, and `estate-audit` from the
cicd-suite pipefail bug). Both are diagnosed in
#37.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01VTeVWDEJgdYKsUNr4uhnXX

---
_Generated by [Claude
Code](https://claude.ai/code/session_01VTeVWDEJgdYKsUNr4uhnXX)_

Co-authored-by: Claude <[email protected]>
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 release-ci Enable all CI checks for a PR, like is done for releases runtime-safety Safety bug in the Lean runtime, does not affect 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.

1 participant