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

Merged
IlyassRS3 merged 1 commit from upstream-pr/2015 into main 2026-11-28 17:54:43 +00:00
IlyassRS3 commented 2026-11-25 18:11:14 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T18:11:14Z.


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.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5496655530 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2015). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T18:11:14Z. --- 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.
copilot-pull-request-reviewer[bot] commented 2026-11-25 18:11:30 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T18:11:30Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6062665979 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2015#pullrequestreview-6062665979). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T18:11:30Z. **Review:** COMMENTED --- 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.
zmasi commented 2026-11-28 12:24:15 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T12:24:15Z.


Verified on Windows 11 (10.0.26200.9974) with MSVC 19.44.35228 (x64, /std:c++20 /EHsc /W4 /permissive- /utf-8 /O2) against main d48a957bb + 435d7541fe (claim #2570#issuecomment-7239117859):

  • Line-by-line diff audit: Clean (runtime/shims/dstorage_staging.h, runtime/test/dstorage_test.cpp); saturates 64-bit staging_bytes page-table and multi-stage sums, tracks Lease::held_ so duplicate try_pair calls are refused and acquire(...) / acquire(0) / release() return any prior reservation cleanly, and moves changed_.notify_all() outside pool_.mutex_. check_no_game_data.py clean.
  • Red (d48a957bb + PR test): Fails to build (rc=2, Lease::release() absent on main); with reclaimed.release() omitted, dstorage_test.exe deadlocks (TIMEOUT after 15s) when re-acquiring a held Lease leaks pool_.used_.
  • Green (d48a957bb + 435d7541fe merge): Auto-merges cleanly (rc=0), compiles dstorage_staging.h / dstorage_test.cpp with 0 warnings, and passes dstorage_test.exe (rc=0).

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7239203492 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2015#issuecomment-7239203492). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T12:24:15Z. --- Verified on Windows 11 (`10.0.26200.9974`) with MSVC `19.44.35228` (`x64`, `/std:c++20 /EHsc /W4 /permissive- /utf-8 /O2`) against `main` `d48a957bb` + `435d7541fe` (claim `#2570#issuecomment-7239117859`): - **Line-by-line diff audit**: Clean (`runtime/shims/dstorage_staging.h`, `runtime/test/dstorage_test.cpp`); saturates 64-bit `staging_bytes` page-table and multi-stage sums, tracks `Lease::held_` so duplicate `try_pair` calls are refused and `acquire(...)` / `acquire(0)` / `release()` return any prior reservation cleanly, and moves `changed_.notify_all()` outside `pool_.mutex_`. `check_no_game_data.py` clean. - **Red (`d48a957bb` + PR test)**: Fails to build (`rc=2`, `Lease::release()` absent on `main`); with `reclaimed.release()` omitted, `dstorage_test.exe` deadlocks (`TIMEOUT after 15s`) when re-acquiring a held `Lease` leaks `pool_.used_`. - **Green (`d48a957bb` + `435d7541fe` merge)**: Auto-merges cleanly (`rc=0`), compiles `dstorage_staging.h` / `dstorage_test.cpp` with `0` warnings, and passes `dstorage_test.exe` (`rc=0`). AI-assisted.
MoonEyes commented 2026-11-28 17:54:40 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T17:54:40Z.


Red/green: On main dstorage_test does not compile (StagingPool::Lease::release is missing) and without that call it deadlocks re-acquiring a held Lease, and with the PR ctest -R "^dstorage$" passes.
Independent tester (OreoFR, RTX 4070, Windows 11): PR head merged with the other PRs of its batch on main c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7243382580 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2015#issuecomment-7243382580). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T17:54:40Z. --- Red/green: On main dstorage_test does not compile (StagingPool::Lease::release is missing) and without that call it deadlocks re-acquiring a held Lease, and with the PR ctest -R "^dstorage$" passes. Independent tester (OreoFR, RTX 4070, Windows 11): PR head merged with the other PRs of its batch on main c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.
OreoFR commented 2026-11-28 21:58:01 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T21:58:01Z.


Verified by OreoFR

  • Base: main c1ecf04ed + PR head 435d7541f; green run in a merge package of 16 PRs, no new ctest failure vs main
  • Test: ctest -R ^dstorage$
  • Red on main: does not compile (StagingPool::Lease::release missing on main)
  • Green with the PR: OK
  • Verdict: READY
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7246400781 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2015#issuecomment-7246400781). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-28T21:58:01Z. --- **Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `435d7541f`; green run in a merge package of 16 PRs, no new ctest failure vs main - Test: `ctest -R ^dstorage$` - Red on main: does not compile (StagingPool::Lease::release missing on main) - Green with the PR: OK - Verdict: READY - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!2015
No description provided.