Skip to content

[Backport releases/v4.35.0] fix: mark the children of objects whose single-threaded count overflowed - #15306

Merged
Kha merged 1 commit into
releases/v4.35.0from
backport-rc-sticky-to-releases/v4.35.0
Sep 24, 2026
Merged

Kha merged 1 commit into
releases/v4.35.0from
backport-rc-sticky-to-releases/v4.35.0

Conversation

@Kha

@Kha Kha commented Sep 24, 2026

Copy link
Copy Markdown
Member

Backport 2b21d8b from #15288.

…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)
@Kha
Kha merged commit b5e7a35 into releases/v4.35.0 Sep 24, 2026
9 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant