dstorage: harden staging_bytes overflow and StagingPool::Lease reuse (RT-DSTORAGE-STAGING-OVERFLOW-AND-REUSE) #2015

Open
github-bot wants to merge 1 commit from github-bot/gh-2015 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 18:11 UTC · original
IlyassRS3:fix/rt-dstorage-staging-overflow-and-reuse → main · +74 −22 · 2 files · 1 commits · open

Claim: RT-DSTORAGE-STAGING-OVERFLOW-AND-REUSE (claim comment #7170510712 on #1608).

Problem

In runtime/shims/dstorage_staging.h:

  1. Unchecked 64-bit arithmetic in staging_bytes(): pages() computed size + sizeof(uint64_t) * (1 + (size - 1 + offset) / 65536) and stage() computed input + output without saturation, allowing extreme values to wrap uint64_t below StagingPool::limit.
  2. StagingPool::Lease accounting leak on reuse: calling try_pair() or acquire() on a Lease that already held a reservation overwrote bytes_ without returning the previous reservation to pool_.used_ / pool_.helpers_, permanently leaking pool capacity and eventually deadlocking future waiters.
  3. Lock-held notify_all() and %llu format casts: acquire() and ~Lease() called pool_.changed_.notify_all() while still holding pool_.mutex_ (causing immediate lock re-contention in woken DirectStorage threads), and printed uint64_t with %llu without casting to unsigned long long.

Fix

  • Make staging_bytes() pages() and stage() calculations saturating against UINT64_MAX.
  • Track held_ in StagingPool::Lease: refuse duplicate try_pair() while held, add explicit release() (called from ~Lease() and before re-acquire()), and call notify_all() after unlocking pool_.mutex_.
  • Cast pool_.peak_ and limit to unsigned long long in trace std::fprintf calls.
  • Add regression tests in runtime/test/dstorage_test.cpp (test_staging_budget(), 0 CMakeLists.txt edits) covering duplicate try_pair, acquire(0) release, re-acquire, explicit release(), and large staging_bytes() inputs.

Verification

  • AI-assisted: yes (Antigravity agent by Google DeepMind).
  • GPUs tested: none (host is macOS arm64; no retail files).
  • Compiled and ran runtime/shims/dstorage_staging.h (including 4-thread concurrent StagingPool stress tests and all test_staging_budget assertions) under minimal Win32 ABI stubs with:
    • clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror
    • clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror
    • clang++ -std=c++20 -O2 -Wall -Wextra -Werror
  • Ran python3 -I scripts/check_no_game_data.py runtime/shims/dstorage_staging.h runtime/test/dstorage_test.cpp and python3 -I tests/test_text_write_line_endings.py: all passed.
**@⁠IlyassRS3** on GitHub · 2026-11-25 18:11 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2015) `IlyassRS3:fix/rt-dstorage-staging-overflow-and-reuse` → `main` · +74 −22 · 2 files · 1 commits · open Claim: `RT-DSTORAGE-STAGING-OVERFLOW-AND-REUSE` (claim comment `#7170510712` on #1608). ### Problem In `runtime/shims/dstorage_staging.h`: 1. **Unchecked 64-bit arithmetic in `staging_bytes()`**: `pages()` computed `size + sizeof(uint64_t) * (1 + (size - 1 + offset) / 65536)` and `stage()` computed `input + output` without saturation, allowing extreme values to wrap `uint64_t` below `StagingPool::limit`. 2. **`StagingPool::Lease` accounting leak on reuse**: calling `try_pair()` or `acquire()` on a `Lease` that already held a reservation overwrote `bytes_` without returning the previous reservation to `pool_.used_` / `pool_.helpers_`, permanently leaking pool capacity and eventually deadlocking future waiters. 3. **Lock-held `notify_all()` and `%llu` format casts**: `acquire()` and `~Lease()` called `pool_.changed_.notify_all()` while still holding `pool_.mutex_` (causing immediate lock re-contention in woken DirectStorage threads), and printed `uint64_t` with `%llu` without casting to `unsigned long long`. ### Fix - Make `staging_bytes()` `pages()` and `stage()` calculations saturating against `UINT64_MAX`. - Track `held_` in `StagingPool::Lease`: refuse duplicate `try_pair()` while held, add explicit `release()` (called from `~Lease()` and before re-`acquire()`), and call `notify_all()` after unlocking `pool_.mutex_`. - Cast `pool_.peak_` and `limit` to `unsigned long long` in trace `std::fprintf` calls. - Add regression tests in `runtime/test/dstorage_test.cpp` (`test_staging_budget()`, 0 `CMakeLists.txt` edits) covering duplicate `try_pair`, `acquire(0)` release, re-`acquire`, explicit `release()`, and large `staging_bytes()` inputs. ### Verification - **AI-assisted**: yes (Antigravity agent by Google DeepMind). - **GPUs tested**: none (host is macOS arm64; no retail files). - Compiled and ran `runtime/shims/dstorage_staging.h` (including 4-thread concurrent `StagingPool` stress tests and all `test_staging_budget` assertions) under minimal Win32 ABI stubs with: - `clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror` - `clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror` - `clang++ -std=c++20 -O2 -Wall -Wextra -Werror` - Ran `python3 -I scripts/check_no_game_data.py runtime/shims/dstorage_staging.h runtime/test/dstorage_test.cpp` and `python3 -I tests/test_text_write_line_endings.py`: all passed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:14:49 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 18:11 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 18:11 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2015#pullrequestreview-6062665979) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2015/head:github-bot/gh-2015'
git switch 'github-bot/gh-2015'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2015'
git switch 'github-bot/gh-2015'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2015'
git switch 'github-bot/gh-2015'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2015'
git switch 'main'
git merge --squash 'github-bot/gh-2015'
git switch 'main'
git merge --ff-only 'github-bot/gh-2015'
git switch 'main'
git merge 'github-bot/gh-2015'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.