[PR #1868] d3d12_xs: fix list-values relay UAF, overwritten WriteGroup relay, and pump start/stop races (RT-LIST-VALUES-RELAY-LIFECYCLE) #1880

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

📋 Pull Request Information

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

Base: main ← Head: fix/rt-list-values-relay-lifecycle


📝 Commits (1)

  • 078dcc9 RT-LIST-VALUES-RELAY-LIFECYCLE: fix relay UAF, overwritten WriteGroup relay, and pump start/stop races

📊 Changes

4 files changed (+340 additions, -27 deletions)

View changed files

📝 runtime/CMakeLists.txt (+2 -0)
📝 runtime/shims/d3d12_xs_device.cpp (+2 -2)
📝 runtime/shims/d3d12_xs_list_values.h (+58 -25)
📝 runtime/test/xs_proxy_mock_test.cpp (+278 -0)

📄 Description

Coordination item: RT-LIST-VALUES-RELAY-LIFECYCLE (claimed in https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7163941870).

Problem

Four safety and concurrency bugs in the console list-values subsystem (runtime/shims/d3d12_xs_list_values.h, runtime/shims/d3d12_xs_device.cpp):

  1. Use-after-free on ID3D12Fence* in Pump::relay() (d3d12_xs_list_values.h:410, d3d12_xs_device.cpp:3548-3550): Pump::relay() looked up a matching in-flight WriteGroup under mutex_, handed out a raw pointer group.queue->written, and released mutex_ without retaining group.queue (std::shared_ptr<QueueValues>) or calling AddRef(). If the writer queue proxy was released while its write group was in flight, the pump thread could pop and land that group and destroy QueueValues (calling written->Release()) before ValueSubmission::stand_at() finished queue->Wait(fence, value).
  2. Stale / overwritten-value relay in Pump::relay() (d3d12_xs_list_values.h:410-422): All writes in a WriteGroup land together when its fence completes, but Pump::relay() scanned group.writes forward and returned on the first matching write. A later write in the same WriteGroup that overwrites the cell with a non-matching value, overwrites it with a GPU/system timestamp, or partially overlaps [wait.address, wait.address + need) (e.g. a 32-bit write to the high or low half of a 64-bit waited cell) was ignored, falsely relaying the waiter onto a fence that leaves a non-meeting value.
  3. Data race, post-stop thread leak, and add_action() TOCTOU in Pump::start() / Pump::stop() (d3d12_xs_list_values.h:389-489):
    • Pump::start() wrote thread_ and releaser_ inside std::call_once, while Pump::stop() read them without synchronization: a concurrent stop() could race on thread_/releaser_ and skip joining a started thread, and a start() after stop() still spawned both threads.
    • Pump::add_action() checked start() but did not check exiting_ under mutex_, leaving a TOCTOU window where CopyPageMappingsBatchX could enqueue an action gate after stop_list_values() had begun and return S_OK without running the remap.
  4. Unchecked gate.waits.front() in Pump::note_outcome() (d3d12_xs_list_values.h:968-972): Line 967 checked !gate.waits.empty(), but the logging block immediately below dereferenced gate.waits.front().address without checking !gate.waits.empty().

Changes

  • runtime/shims/d3d12_xs_list_values.h:
    • Changed Pump::relay() to take std::shared_ptr<QueueValues>& owner instead of a raw ID3D12Fence*&, keeping owner->written alive across queue->Wait().
    • Updated Pump::relay() to scan each WriteGroup's writes in reverse (rbegin() -> rend()) with overflow-safe range checks over [wait.address, wait.address + need), relaying only when the last write overlapping the waited range is a WriteKind::value covering the full waited range that satisfies console_value_met().
    • Replaced unsynchronized std::once_flag start_ with lifecycle_mutex_ + start_attempted_ + started_ (acquire/release), so start() never spawns threads after stop(), stop() snapshots and joins every started thread without data races, and add_writes() / add_gate() / add_action() check exiting_ (acquire) under mutex_.
    • Guarded gate.waits.front() in Pump::note_outcome() with !gate.waits.empty().
  • runtime/shims/d3d12_xs_device.cpp:
    • Updated ValueSubmission::stand_at() to hold std::shared_ptr<list_values::QueueValues> owner across wait(owner->written, value).
  • runtime/test/xs_proxy_mock_test.cpp & runtime/CMakeLists.txt:
    • Added check_list_values_relay_safety() covering:
      • deterministic relay fence-lifetime test (writer queue released while write is in flight, pump drains writes_ inside Wait, asserting fence->refs >= 1 during Wait and 0 after),
      • same-WriteGroup value overwrite, timestamp overwrite, high-half 32-bit overlap, low-half 32-bit overlap, disjoint 32-bit low-half relay, and final-match positive relay.
    • Added --list-values-stop-before-start CLI mode (registered as CTest xs_proxy_mock_list_values_stop_before_start) verifying idempotent stop_list_values() before start(), synchronous CopyPageMappingsBatchX fallback, post-stop drop of writes/gates, and clean refcount teardown.

Verification

  • Before fix (new check_list_values_relay_safety() on unpatched main 384abe5b0):
    • Failed deterministically at wait_fence_seen == temp_written && wait_fence_refs >= 1 (temp_written->refs dropped to 0 inside queue->Wait(fence, value)).
  • After fix (rebased onto main 5e95fa2f4, MSVC 14.51.36231 /std:c++20 /EHsc /W4 /permissive- /utf-8 /O2 /MD):
    • xs_proxy_mock_test.exe (default + --list-values-stop-before-start + --slot133-layout + --env-slots + --env-slots-invalid + --env-slots-70 + --env-slots-74 + --fail-virtualalloc + --fail-virtualprotect + --fail-veh) — all 10 modes PASS
    • root_sig_test.exe — PASS
    • device_proxy_test.exe — OK (0 failures)
    • LLVM clang-cl /W4 /permissive- /utf-8 /std:c++20 compile check on runtime/shims/d3d12_xs_device.cpp — exit 0, 0 new warnings
  • Repository checks:
    • python -I tests/test_ci_registration.py — 16/16 OK
    • python -I tests/test_build_runtime_fallback.py — 10/10 OK
    • python -I scripts/check_no_game_data.py runtime/shims/d3d12_xs_list_values.h runtime/shims/d3d12_xs_device.cpp runtime/test/xs_proxy_mock_test.cpp runtime/CMakeLists.txt — clean (exit 0)

Hardware / Environment

  • GPU / driver: n/a (mock-COM / WARP PC D3D12 runtime tests; no Vulkan SDK installed on this host)
  • Host: Windows 11 Pro 64-bit, MSVC 14.51.36231 / LLVM clang-cl

AI-assisted: yes


🔄 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/1868 **Author:** [@ambroseautomation](https://github.com/ambroseautomation) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/rt-list-values-relay-lifecycle` --- ### 📝 Commits (1) - [`078dcc9`](https://github.com/ubiquitarianism/leonida/commit/078dcc9d3e895d392515e0ac97ad1ffb4c133fb8) RT-LIST-VALUES-RELAY-LIFECYCLE: fix relay UAF, overwritten WriteGroup relay, and pump start/stop races ### 📊 Changes **4 files changed** (+340 additions, -27 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+2 -0) 📝 `runtime/shims/d3d12_xs_device.cpp` (+2 -2) 📝 `runtime/shims/d3d12_xs_list_values.h` (+58 -25) 📝 `runtime/test/xs_proxy_mock_test.cpp` (+278 -0) </details> ### 📄 Description Coordination item: `RT-LIST-VALUES-RELAY-LIFECYCLE` (claimed in https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7163941870). ## Problem Four safety and concurrency bugs in the console list-values subsystem (`runtime/shims/d3d12_xs_list_values.h`, `runtime/shims/d3d12_xs_device.cpp`): 1. **Use-after-free on `ID3D12Fence*` in `Pump::relay()` (`d3d12_xs_list_values.h:410`, `d3d12_xs_device.cpp:3548-3550`)**: `Pump::relay()` looked up a matching in-flight `WriteGroup` under `mutex_`, handed out a raw pointer `group.queue->written`, and released `mutex_` without retaining `group.queue` (`std::shared_ptr<QueueValues>`) or calling `AddRef()`. If the writer queue proxy was released while its write group was in flight, the pump thread could pop and land that group and destroy `QueueValues` (calling `written->Release()`) before `ValueSubmission::stand_at()` finished `queue->Wait(fence, value)`. 2. **Stale / overwritten-value relay in `Pump::relay()` (`d3d12_xs_list_values.h:410-422`)**: All writes in a `WriteGroup` land together when its fence completes, but `Pump::relay()` scanned `group.writes` forward and returned on the first matching write. A later write in the same `WriteGroup` that overwrites the cell with a non-matching value, overwrites it with a GPU/system timestamp, or partially overlaps `[wait.address, wait.address + need)` (e.g. a 32-bit write to the high or low half of a 64-bit waited cell) was ignored, falsely relaying the waiter onto a fence that leaves a non-meeting value. 3. **Data race, post-stop thread leak, and `add_action()` TOCTOU in `Pump::start()` / `Pump::stop()` (`d3d12_xs_list_values.h:389-489`)**: - `Pump::start()` wrote `thread_` and `releaser_` inside `std::call_once`, while `Pump::stop()` read them without synchronization: a concurrent `stop()` could race on `thread_`/`releaser_` and skip joining a started thread, and a `start()` after `stop()` still spawned both threads. - `Pump::add_action()` checked `start()` but did not check `exiting_` under `mutex_`, leaving a TOCTOU window where `CopyPageMappingsBatchX` could enqueue an action gate after `stop_list_values()` had begun and return `S_OK` without running the remap. 4. **Unchecked `gate.waits.front()` in `Pump::note_outcome()` (`d3d12_xs_list_values.h:968-972`)**: Line 967 checked `!gate.waits.empty()`, but the logging block immediately below dereferenced `gate.waits.front().address` without checking `!gate.waits.empty()`. ## Changes - **`runtime/shims/d3d12_xs_list_values.h`**: - Changed `Pump::relay()` to take `std::shared_ptr<QueueValues>& owner` instead of a raw `ID3D12Fence*&`, keeping `owner->written` alive across `queue->Wait()`. - Updated `Pump::relay()` to scan each `WriteGroup`'s writes in reverse (`rbegin()` -> `rend()`) with overflow-safe range checks over `[wait.address, wait.address + need)`, relaying only when the last write overlapping the waited range is a `WriteKind::value` covering the full waited range that satisfies `console_value_met()`. - Replaced unsynchronized `std::once_flag start_` with `lifecycle_mutex_` + `start_attempted_` + `started_` (`acquire`/`release`), so `start()` never spawns threads after `stop()`, `stop()` snapshots and joins every started thread without data races, and `add_writes()` / `add_gate()` / `add_action()` check `exiting_` (`acquire`) under `mutex_`. - Guarded `gate.waits.front()` in `Pump::note_outcome()` with `!gate.waits.empty()`. - **`runtime/shims/d3d12_xs_device.cpp`**: - Updated `ValueSubmission::stand_at()` to hold `std::shared_ptr<list_values::QueueValues> owner` across `wait(owner->written, value)`. - **`runtime/test/xs_proxy_mock_test.cpp`** & **`runtime/CMakeLists.txt`**: - Added `check_list_values_relay_safety()` covering: - deterministic relay fence-lifetime test (writer queue released while write is in flight, pump drains `writes_` inside `Wait`, asserting `fence->refs >= 1` during `Wait` and `0` after), - same-`WriteGroup` value overwrite, timestamp overwrite, high-half 32-bit overlap, low-half 32-bit overlap, disjoint 32-bit low-half relay, and final-match positive relay. - Added `--list-values-stop-before-start` CLI mode (registered as CTest `xs_proxy_mock_list_values_stop_before_start`) verifying idempotent `stop_list_values()` before `start()`, synchronous `CopyPageMappingsBatchX` fallback, post-stop drop of writes/gates, and clean refcount teardown. ## Verification - **Before fix** (new `check_list_values_relay_safety()` on unpatched `main` `384abe5b0`): - Failed deterministically at `wait_fence_seen == temp_written && wait_fence_refs >= 1` (`temp_written->refs` dropped to `0` inside `queue->Wait(fence, value)`). - **After fix** (rebased onto `main` `5e95fa2f4`, MSVC 14.51.36231 `/std:c++20 /EHsc /W4 /permissive- /utf-8 /O2 /MD`): - `xs_proxy_mock_test.exe` (default + `--list-values-stop-before-start` + `--slot133-layout` + `--env-slots` + `--env-slots-invalid` + `--env-slots-70` + `--env-slots-74` + `--fail-virtualalloc` + `--fail-virtualprotect` + `--fail-veh`) — all 10 modes PASS - `root_sig_test.exe` — PASS - `device_proxy_test.exe` — OK (0 failures) - LLVM `clang-cl` `/W4 /permissive- /utf-8 /std:c++20` compile check on `runtime/shims/d3d12_xs_device.cpp` — exit 0, 0 new warnings - **Repository checks**: - `python -I tests/test_ci_registration.py` — 16/16 OK - `python -I tests/test_build_runtime_fallback.py` — 10/10 OK - `python -I scripts/check_no_game_data.py runtime/shims/d3d12_xs_list_values.h runtime/shims/d3d12_xs_device.cpp runtime/test/xs_proxy_mock_test.cpp runtime/CMakeLists.txt` — clean (exit 0) ## Hardware / Environment - GPU / driver: n/a (mock-COM / WARP PC D3D12 runtime tests; no Vulkan SDK installed on this host) - Host: Windows 11 Pro 64-bit, MSVC 14.51.36231 / LLVM `clang-cl` AI-assisted: yes --- <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#1880
No description provided.