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

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

@⁠ambroseautomation on GitHub · 2026-11-25 13:43 UTC · original
ambroseautomation:fix/rt-list-values-relay-lifecycle → main · +340 −27 · 4 files · 1 commits · open

Coordination item: RT-LIST-VALUES-RELAY-LIFECYCLE (claimed in #1608).

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

**@⁠ambroseautomation** on GitHub · 2026-11-25 13:43 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1868) `ambroseautomation:fix/rt-list-values-relay-lifecycle` → `main` · +340 −27 · 4 files · 1 commits · open Coordination item: `RT-LIST-VALUES-RELAY-LIFECYCLE` (claimed in https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608). ## 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
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:09:22 +00:00
Author
Owner

@⁠tiguan26 on GitHub · 2026-11-25 20:00 UTC · original · approved these changes

Approving. I merged it into current main (e48ff03) with no conflicts and cross-built the Windows runtime (clang-cl 19, xwin SDK). xs_proxy_mock_test passes under Wine 11.22, including the new relay case ("retains writer QueueValues across Wait and inspects the final overlapping write in a WriteGroup").

The relay change is the important one. On main, relay() takes the first write in a group with the waited address and a value that meets the wait, even when a later write in the same group stores something else there. The whole group lands at once, so memory ends up holding the later value, but the GPU wait on written already let the queue run as if the earlier one were there. Walking each group from its last write, and refusing on an overwrite, a partial overlap or a timestamp, matches what the group really leaves in memory. Returning the shared_ptr<QueueValues> keeps the written fence alive across wait(), so a queue destroyed between the lookup and the Wait can't free it.

The start/stop changes look right too: no thread is started after stop(), the add paths re-check exiting_ under mutex_, and the error code is captured before the cleanup can overwrite it.

AI-assisted (Claude).

**@⁠tiguan26** on GitHub · 2026-11-25 20:00 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1868#pullrequestreview-6064136769) · **approved these changes** Approving. I merged it into current main (e48ff03) with no conflicts and cross-built the Windows runtime (clang-cl 19, xwin SDK). `xs_proxy_mock_test` passes under Wine 11.22, including the new relay case ("retains writer QueueValues across Wait and inspects the final overlapping write in a WriteGroup"). The relay change is the important one. On main, `relay()` takes the first write in a group with the waited address and a value that meets the wait, even when a later write in the same group stores something else there. The whole group lands at once, so memory ends up holding the later value, but the GPU wait on `written` already let the queue run as if the earlier one were there. Walking each group from its last write, and refusing on an overwrite, a partial overlap or a timestamp, matches what the group really leaves in memory. Returning the `shared_ptr<QueueValues>` keeps the `written` fence alive across `wait()`, so a queue destroyed between the lookup and the Wait can't free it. The start/stop changes look right too: no thread is started after `stop()`, the add paths re-check `exiting_` under `mutex_`, and the error code is captured before the cleanup can overwrite it. AI-assisted (Claude).
This pull request has changes conflicting with the target branch.
  • runtime/shims/d3d12_xs_list_values.h
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

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