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

Open
opened 2026-11-26 21:19:46 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2015
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/rt-dstorage-staging-overflow-and-reuse


📝 Commits (1)

  • 435d754 dstorage: harden staging_bytes overflow and StagingPool::Lease reuse

📊 Changes

2 files changed (+74 additions, -22 deletions)

View changed files

📝 runtime/shims/dstorage_staging.h (+51 -22)
📝 runtime/test/dstorage_test.cpp (+23 -0)

📄 Description

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.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2015 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/rt-dstorage-staging-overflow-and-reuse` --- ### 📝 Commits (1) - [`435d754`](https://github.com/ubiquitarianism/leonida/commit/435d7541fed13805ad3d487a454cce4e23b6ffbc) dstorage: harden staging_bytes overflow and StagingPool::Lease reuse ### 📊 Changes **2 files changed** (+74 additions, -22 deletions) <details> <summary>View changed files</summary> 📝 `runtime/shims/dstorage_staging.h` (+51 -22) 📝 `runtime/test/dstorage_test.cpp` (+23 -0) </details> ### 📄 Description 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#2022
No description provided.