Skip to content

d3d12_xs: a device forward refuses a 'this' that is not the proxy - #7923

Open
SebSilent wants to merge 2 commits into
ItsNotRexed:mainfrom
SebSilent:fix/xs-forward-self-check
Open

SebSilent wants to merge 2 commits into
ItsNotRexed:mainfrom
SebSilent:fix/xs-forward-self-check

Conversation

@SebSilent

@SebSilent SebSilent commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: #7804 (comment) (first posted before the tracker moved: #7694 (comment))

What and why

A device thunk that forwards trusts the pointer it was handed. Slots past the 23 measured ones are refused and the tail read is guarded, but nothing checked that self is the device proxy at all: the generic thunk reads self->slot_count and self->real_vtable out of whatever it got and tail-jumps into real_vtable[slot]. Called on the PC device, on another of our proxies, on a pointer into the middle of a proxy, or on a foreign COM object, that reads a PC vtable out of memory that is not one and jumps to whatever the words there say.

Not theoretical on this machine: a --run boot stopped inside slot 26's thunk, at an address that is not code, with nothing named in the log.

The forward path now looks at the pointer before dereferencing anything:

  • device_forward_blocker(self, slot) says why a forward cannot be made, or null when it can: null pointer, unreadable pointer, first word is not this proxy's own table, no PC device or PC table behind it, slot past the table, unreadable PC vtable, null PC method.
  • refuse_bad_forward logs one line naming the slot, the reason and the two pointers (once per slot, its own flag array so it cannot hide the existing beyond line) and returns E_NOINTERFACE, the same code the tail guard gives for a wrong object.

Nothing is forwarded, no hit is counted and no PC method runs. The check sits before note_slot, so a refused call cannot even look like traffic. Honest calls on the proxy are untouched: QueryInterface, AddRef and Release still come from the proxy's own slots and a real forward still reaches the PC device.

Tested

Linux, clang-cl against the xwin SDK, run through my Proton prefix, CPU only, main dde82f27e plus this commit.

  • Red, main's d3d12_xs_device.cpp with the new case: xs_proxy_mock_test dies in slot 26 (exit 5, no report line). The case hands the forward an object whose words read as a proxy with 64 slots and a table full of values that are not addresses, then a mock of another kind, null, a queue proxy and a pointer 8 bytes into a real proxy. Each must return E_NOINTERFACE with the mock's slot 26 untouched.
  • Green: xs_proxy_mock_test: PASS (9 allocations refused on purpose), and the five setup-failure variants still pass (--fail-virtualalloc, --fail-virtualprotect, --fail-veh, --env-slots, --env-slots-invalid). The case also checks the proxy still forwards afterwards and that the refusals did not move the hit counters.
  • Both files compile warning-free at /W4 /permissive- in xs_proxy_mock_test.
  • device_proxy_test and root_sig_test do not start on this box: the Proton prefix has no vkd3d-proton, so wine exits 53 before the first line, and the same happens with the exes built from main. The mock test is the one covering this path here.

Overlap

forward_device and the end of check_tail are busy right now, so I checked instead of guessing. Ten open PRs edit runtime/shims/d3d12_xs_device.cpp (#7639, #7642, #7653, #7679, #7685, #7759, #7821, #7880, #7949, #7969) and none of them looks at self itself, they all stop at Slot >= self->slot_count bounds and per-slot refusals, so this check is new. I ran git merge-tree of this branch against each of those ten plus #6964, #7125, #7187, #7201, #7490 and #7503: all sixteen merge clean, and the branch is on current main (dde82f27e). Earlier in the day some of them conflicted, that cleared as their heads moved.

This is a refusal, not a slot mapping, so it should not change anyone's answers. If anything still lands on top of it I will rebase.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. Only aggregate counts and slot numbers from our own shim.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (no translator rule changed; this is a refusal inside the existing forward)
  • Docs updated for any new flag, tool or workflow. (no new flag; the refusal line follows the format of the existing [rt] d3d12_xs: lines)

AI-assisted (Tacit).

@RAINighty

Copy link
Copy Markdown

Tested 0c459ce04 merged with main dde82f27e (clean merge) on native Windows 11 (10.0.26300), hybrid laptop (NVIDIA GeForce RTX 5060 Laptop GPU, driver 32.0.16.2124, plus Intel UHD Graphics), MSVC 19.44.35228, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0, Ninja, RelWithDebInfo: pass. The description reports a Linux clang-cl + Proton run, so this is the native MSVC build.

  • ctest -R xs_proxy_mock: all 6 pass (xs_proxy_mock, _no_reserve, _no_protect, _no_handler, _env_slots, _env_slots_invalid).
  • Negative control: this PR's xs_proxy_mock_test.cpp with main's runtime/shims/d3d12_xs_device.cpp ends in a SEGFAULT in xs_proxy_mock, so the new case does catch the missing refusal. On main with the old test all 6 pass.
  • Code not reviewed beyond running it. CPU only, no game files.

AI-assisted (Claude Code).

@MartinIndra02

Copy link
Copy Markdown

Independent build + test: tests PASS, but there's a regression for slots at or past the PC table end (BUG)

AI-assisted (Claude Code). Windows 11 x64, MSVC 19.44.35220, Ninja RelWithDebInfo, NVIDIA GeForce RTX 3080 driver 621.24 / 32.0.16.2124.

Head 0c459ce04, based on current main (dde82f27e).

Test Result
xs_proxy_mock_test.exe exit 0, PASS (9 allocations refused on purpose), new check_bad_self included
allocator_proxy_test.exe exit 0, 3 backend(s) passed (host WARP, host RTX 3080, Vulkan RTX 3080)

Bug: device_forward_blocker reads real_vtable[Slot] before the Xbox routes and before the slot_count check.

d3d12_xs_device.cpp, in proxy_slot: the new check runs before note_slot, before the 48/73/78/79/80 routes, and before if (Slot >= self->slot_count) refuse_beyond_device(...). The blocker itself does IsBadReadPtr(self->real_vtable, (slot + 1) * sizeof(void*)) and if (!self->real_vtable[slot]) return "null PC method" for any slot < kMaxSlots (128). For any slot at or past slot_count, that read goes past the end of the PC vtable. Two cases:

  1. Slot 80 (WaitFrameEventX) on an ID3D12Device11 proxy (slot_count == 80). The code comment says this slot "is outside a PC ID3D12Device11 table". If the word after the PC table is 0, or that memory isn't readable, the call returns E_NOINTERFACE and never reaches the HLE route.
  2. Any slot at or past slot_count that used to return E_NOTIMPL from refuse_beyond_device now returns E_NOINTERFACE (or E_NOTIMPL, depending on what memory happens to follow the PC table).

The mock doesn't catch this because its real table has 128 non-null entries. To show it, I temporarily set mock_vtable()[80] and [100] to nullptr in check_xbox_slots (level-11 device, 80 slots) and called both slots:

PR shim:   [rt] d3d12_xs: device slot 80 (Xbox WaitFrameEventX) refused: null PC method (... 80 slots)
           [rt] d3d12_xs: device slot 100 (forwarded) refused: null PC method (... 80 slots)
           REVIEW PROBE: slot 80 (WaitFrameEventX) -> 0x80004002 token=0; slot 100 (beyond) -> 0x80004002
main shim: REVIEW PROBE: slot 80 (WaitFrameEventX) -> 0x00000000 token=1; slot 100 (beyond) -> 0x80004001

Suggested fix: keep only the self checks (null, readable, vtable == get_vtable(), real/real_vtable non-null) before the routes. Then do the real_vtable[Slot] readability and null check after if (Slot >= self->slot_count), right before the forward.

Smaller notes:

  • xs_proxy_mock_test.cpp:888: the comment says "The refusals are counted as calls of slot 26", but the refusal returns before note_slot, and the next line checks device_slot_hits(26) == 1 (only the honest call). The code is right and the comment is wrong.
  • IsBadReadPtr is now called twice on every forwarded device call. It's documented as unsafe around guard pages, and this file uses guard regions for its tables. That isn't a problem for the device table today (no guard on real_vtable), but it's worth keeping in mind.

… keep their route

Reported in ItsNotRexed#7923 review: the forward guard read the PC vtable entry for every slot, so a slot the PC device does not have (Xbox WaitFrameEventX at 80, or anything past slot_count) got E_NOINTERFACE where the routes and the beyond-table refusal used to answer. The pointer-shape check now runs before the routes and the entry check only once the slot is inside slot_count.
@SebSilent

Copy link
Copy Markdown
Contributor Author

Good catch, that was real. Pushed a fix in 55c042f.

The guard was reading the PC vtable entry for every slot, so a slot the PC device does not have answered E_NOINTERFACE where the Xbox routes and the beyond-table refusal used to answer. Now the pointer-shape check runs before the routes and the entry check only once the slot is inside slot_count, so past the end of the PC table nothing changes: 80 keeps its route, 100 keeps E_NOTIMPL, and only a hole inside the range (77 here) gets the missing-method refusal.

Red before, green after, with the new case in section 11 of the test:

# old shim + new case
xs_proxy_mock line 920: hr_of(raw(device, 80, 0, 0, 0, 0, word(&token))) == S_OK && token != 0
exit=1
[rt] d3d12_xs: device slot 80 (Xbox WaitFrameEventX) refused: null PC method
# fixed shim
xs_proxy_mock_test: PASS (9 allocations refused on purpose)
exit=0

The this-shape case from the original report still passes, and the slot 26 counter only counts the honest call, so a refusal no longer looks like a forward.

One thing I could not check locally: device_proxy_test will not start in my Proton prefix (wined3d wants libvkd3d-1.dll, exit 53 before main), so that one is CI-only for me.

@AirKyzzZ

Copy link
Copy Markdown
Contributor

Tested 55c042f80 on RTX 2060 (NVIDIA 566.14), Windows 10 22H2, clang-cl 23.1.6 with the MSVC 14.44 STL, RelWithDebInfo: pass.

Built xs_proxy_mock_test and ran ctest -R ^xs_proxy_mock: 6/6 pass. With main's runtime/shims/d3d12_xs_device.cpp and this test, xs_proxy_mock ends in a SegFault (the other 5 pass), so the new case catches the missing refusal.

No game files. AI-assisted (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants