d3d12_xs: route command-list slots 131/132 by the caller's GXDK edition #334

Closed
github-bot wants to merge 2 commits from gh-pr/334/head into gh-pr/334/base
Owner

@⁠GDAgent on GitHub · 2026-11-23 20:24 UTC · original
GDAgent:fix/d3d12-cmdlist-slots-131-132-edition → main · +441 −1 · 3 files · 2 commits · closed without merging

What

Two things in one small change, both needed before a newer-GDK title can record its first command list:

  1. cl_edition.rs: finds out which GXDK edition the calling module was built with. The module's .xbld section carries _xbld_edition_sdktype=GXDK,<edition> fields; the parser reads them from the module's own header and image (bounded by SizeOfImage, at most 1 MiB), keeps the highest edition and caches the answer per module. The caller is the first stack return address outside the emulator that follows a call instruction (unwinder first, then a raw scan bounded by GetCurrentThreadStackLimits, so a frame without unwind data cannot send the scan past the stack top).
  2. Command-list vtable slots 131 and 132 change meaning with the GXDK edition. For editions 2504 and newer they are a synchronisation command; this change records it as a global UAV barrier (same conservative mapping as the existing FlushTextureCacheX, and skipped on copy lists, where a UAV barrier is illegal). For older editions the slots are routed to whatever handler was registered before this layer. When there is none (the case on main), the command is ignored and the log says so: no success is faked and nothing is claimed.

The shim table grows from 128 to 256 entries, because slots 131/132 are read past the end of it today. That is the same one-line change as #296 (const N: usize = 128 to 256), so whichever lands first, the other merges without a conflict. This PR is based on main, not stacked on #296, and does not touch #296's tests or its IID changes.

Why / evidence

A very popular AAA title built with a 2504+ GDK calls list slots 131 and 132 right after list creation. With the old meaning of slot 131 (a batched acceleration-structure build, which only exists on the ray-tracing branch, not on main) the call is routed to the wrong operation; with the table at 128 it is a call through a null pointer. In native Windows 11 runs (not run under Proton) of that title, routing by edition got it past command-list creation and the first calls to these slots; the slot-131 arguments are logged for the first 16 calls so the exact semantics can be refined from evidence. What the two slots really do on the console is not known from documentation; "synchronisation command" is inferred from the call pattern, and the global UAV barrier is the conservative host equivalent (it can only add ordering).

Edition cut-off: titles with GXDK 250402 and 260405 (and 250405) use the new layout; the cut-off constant is 250400, so every edition from 2504 on takes the new meaning. No evidence for editions between 2021 and 2504; they keep the old layout.

Tests

cargo test -p d3d12_xs --lib cl_edition (all generic, no console files or title data):

  • edition field parser: exact GXDK,<6 digits> only, GRDK, wrong lengths and non-digits rejected, duplicates folded;
  • edition-to-layout switch at 2504;
  • .xbld section lookup in a synthetic PE header, bounded by the image size, bad magic and truncated headers rejected;
  • editions read through the real memory path from a synthetic in-memory image;
  • caller selection from synthetic return-address lists (first frame outside the emulator that follows a call);
  • return-address recognition from synthetic code bytes (call rel32, call rip-relative, call reg, call [reg+disp], with SIB; rejects mov, jmp and similar);
  • a module without .xbld keeps the old layout;
  • slot routing against a fake command list: 2504 callers get exactly one global UAV barrier per call, copy lists and null lists none, old-layout callers never get a barrier, and the previous handler is called with the original arguments only when it is not the unmapped-slot stub;
  • install keeps the previous slot occupants and is idempotent; short tables are left alone.

9 passed. TOMBOX_REMOVED_EXIT_MS=off cargo test -p d3d12_xs --lib -- --test-threads=1 on the follow-up device-slot branch, which contains this commit: 677 passed, 4 failed, 71 ignored. The 4 failures also fail on main on this RDNA2 GPU and are not touched here (alias::tests::msaa_depth_with_a_larger_pc_layout_never_overlaps_the_next_title_placement, placed_offset::tests::uneven_views_get_exact_helpers_and_msaa_shares_the_alias_heap, title_shadow::tests::buffer_then_texture_migrates_to_one_local_heap_and_keeps_bytes: see #322; xs_exports::console_fixed_tests::fixed_result_dxgix_exports_match_every_console_d3d12_xs: needs a console DLL). Native Windows 11; not run under Proton.

Not verified

  • The title runs mentioned above used the same logic on a different base (a branch that also carries the ray-tracing work); this exact branch was built and unit-tested but not re-run in a title.
  • Not run on the console and not checked against a title other than the one described above.
  • The semantics of slots 131/132 on 2504+ editions are inferred (see above); a global UAV barrier is a safe over-approximation, not a proof of equivalence.
  • The old-layout slot 131 (batched acceleration-structure build) is not implemented on main; on main the command is ignored and logged. It is routed correctly once that implementation is registered in the table before this layer.
  • The unwinder fallback and the raw scan only run on a live stack; they are covered by the synthetic-frame tests of the selection logic, not by a test that fakes a title frame.
**@⁠GDAgent** on GitHub · 2026-11-23 20:24 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/334) `GDAgent:fix/d3d12-cmdlist-slots-131-132-edition` → `main` · +441 −1 · 3 files · 2 commits · closed without merging ## What Two things in one small change, both needed before a newer-GDK title can record its first command list: 1. `cl_edition.rs`: finds out which GXDK edition the calling module was built with. The module's `.xbld` section carries `_xbld_edition_sdktype=GXDK,<edition>` fields; the parser reads them from the module's own header and image (bounded by `SizeOfImage`, at most 1 MiB), keeps the highest edition and caches the answer per module. The caller is the first stack return address outside the emulator that follows a call instruction (unwinder first, then a raw scan bounded by `GetCurrentThreadStackLimits`, so a frame without unwind data cannot send the scan past the stack top). 2. Command-list vtable slots 131 and 132 change meaning with the GXDK edition. For editions 2504 and newer they are a synchronisation command; this change records it as a global UAV barrier (same conservative mapping as the existing `FlushTextureCacheX`, and skipped on copy lists, where a UAV barrier is illegal). For older editions the slots are routed to whatever handler was registered before this layer. When there is none (the case on `main`), the command is ignored and the log says so: no success is faked and nothing is claimed. The shim table grows from 128 to 256 entries, because slots 131/132 are read past the end of it today. That is the same one-line change as #296 (`const N: usize = 128` to `256`), so whichever lands first, the other merges without a conflict. This PR is based on `main`, not stacked on #296, and does not touch #296's tests or its IID changes. ## Why / evidence A very popular AAA title built with a 2504+ GDK calls list slots 131 and 132 right after list creation. With the old meaning of slot 131 (a batched acceleration-structure build, which only exists on the ray-tracing branch, not on `main`) the call is routed to the wrong operation; with the table at 128 it is a call through a null pointer. In native Windows 11 runs (not run under Proton) of that title, routing by edition got it past command-list creation and the first calls to these slots; the slot-131 arguments are logged for the first 16 calls so the exact semantics can be refined from evidence. What the two slots really do on the console is not known from documentation; "synchronisation command" is inferred from the call pattern, and the global UAV barrier is the conservative host equivalent (it can only add ordering). Edition cut-off: titles with GXDK 250402 and 260405 (and 250405) use the new layout; the cut-off constant is `250400`, so every edition from 2504 on takes the new meaning. No evidence for editions between 2021 and 2504; they keep the old layout. ## Tests `cargo test -p d3d12_xs --lib cl_edition` (all generic, no console files or title data): - edition field parser: exact `GXDK,<6 digits>` only, `GRDK`, wrong lengths and non-digits rejected, duplicates folded; - edition-to-layout switch at 2504; - `.xbld` section lookup in a synthetic PE header, bounded by the image size, bad magic and truncated headers rejected; - editions read through the real memory path from a synthetic in-memory image; - caller selection from synthetic return-address lists (first frame outside the emulator that follows a call); - return-address recognition from synthetic code bytes (call rel32, call rip-relative, call reg, call [reg+disp], with SIB; rejects mov, jmp and similar); - a module without `.xbld` keeps the old layout; - slot routing against a fake command list: 2504 callers get exactly one global UAV barrier per call, copy lists and null lists none, old-layout callers never get a barrier, and the previous handler is called with the original arguments only when it is not the unmapped-slot stub; - install keeps the previous slot occupants and is idempotent; short tables are left alone. 9 passed. `TOMBOX_REMOVED_EXIT_MS=off cargo test -p d3d12_xs --lib -- --test-threads=1` on the follow-up device-slot branch, which contains this commit: 677 passed, 4 failed, 71 ignored. The 4 failures also fail on `main` on this RDNA2 GPU and are not touched here (`alias::tests::msaa_depth_with_a_larger_pc_layout_never_overlaps_the_next_title_placement`, `placed_offset::tests::uneven_views_get_exact_helpers_and_msaa_shares_the_alias_heap`, `title_shadow::tests::buffer_then_texture_migrates_to_one_local_heap_and_keeps_bytes`: see #322; `xs_exports::console_fixed_tests::fixed_result_dxgix_exports_match_every_console_d3d12_xs`: needs a console DLL). Native Windows 11; not run under Proton. ## Not verified - The title runs mentioned above used the same logic on a different base (a branch that also carries the ray-tracing work); this exact branch was built and unit-tested but not re-run in a title. - Not run on the console and not checked against a title other than the one described above. - The semantics of slots 131/132 on 2504+ editions are inferred (see above); a global UAV barrier is a safe over-approximation, not a proof of equivalence. - The old-layout slot 131 (batched acceleration-structure build) is not implemented on `main`; on `main` the command is ignored and logged. It is routed correctly once that implementation is registered in the table before this layer. - The unwinder fallback and the raw scan only run on a live stack; they are covered by the synthetic-frame tests of the selection logic, not by a test that fakes a title frame.
Command-list vtable slots 131 and 132 change meaning with the GXDK edition
the calling module was built with. New cl_edition module reads the
_xbld_edition_sdktype=GXDK,<edition> fields from the caller's .xbld section
(bounded by SizeOfImage, highest edition wins, cached per module). The
caller is the first stack return address outside the emulator that follows a
call instruction: unwinder first, then a raw scan bounded by the thread's
stack limits.

For editions 2504 and newer the slots are a list synchronisation command,
recorded as a global UAV barrier (skipped on copy lists). For older editions
they go to the handler registered before this layer; with none, the command
is ignored and logged, without claiming success.

The shim table grows from 128 to 256 entries so slots 131/132 exist at all.
after_call read code[c - k + 2] for a two-byte candidate whose ModRM
needs a SIB byte (FF 14 at the end of the window), one past the eight
bytes it was given, and panicked. Such a candidate cannot be a two-byte
call, so it is skipped and the longer candidates are still examined.

Tests: return_addresses_must_follow_a_call rejects a trailing FF 14 and
still accepts 90 90 FF 90 00 00 FF 14 (call [rax+disp32]); without the
fix the first assertion panics with "index out of bounds: the len is 8
but the index is 8".

Reported by RomainROCH in review.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:19:18 +00:00
github-bot closed this pull request 2026-11-26 06:19:18 +00:00
Author
Owner

@⁠GDAgent on GitHub · 2026-11-23 23:06 UTC · original

Thanks, confirmed and fixed in bb26712.

FF 14 at the end of the window gives sib == 1 for k == 2, and the guard read code[8]. I added your skip right after sib is computed (continue, so the longer candidates are still examined) and both regressions to return_addresses_must_follow_a_call. Without the fix the new negative case panics with index out of bounds: the len is 8 but the index is 8; with it all nine cl_edition tests pass (Windows 11, RDNA2 host, cargo test -p d3d12_xs --lib cl_edition).

Agreed on the limit: this only removes the out-of-bounds read. The suffix check stays a filter for plausible call sites, not a proof that a stack word is a return address; the caller still has to land inside a module with an .xbld section before its edition is used.

**@⁠GDAgent** on GitHub · 2026-11-23 23:06 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/334#issuecomment-7116564865) Thanks, confirmed and fixed in bb26712. `FF 14` at the end of the window gives `sib == 1` for `k == 2`, and the guard read `code[8]`. I added your skip right after `sib` is computed (`continue`, so the longer candidates are still examined) and both regressions to `return_addresses_must_follow_a_call`. Without the fix the new negative case panics with `index out of bounds: the len is 8 but the index is 8`; with it all nine `cl_edition` tests pass (Windows 11, RDNA2 host, `cargo test -p d3d12_xs --lib cl_edition`). Agreed on the limit: this only removes the out-of-bounds read. The suffix check stays a filter for plausible call sites, not a proof that a stack word is a return address; the caller still has to land inside a module with an `.xbld` section before its edition is used.
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-25 13:50 UTC · original

Applied on main in d68b34a (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

**@⁠omonkeyz** on GitHub · 2026-11-25 13:50 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/334#issuecomment-7164357885) Applied on main in d68b34a (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.
Author
Owner

@⁠RomainROCH on GitHub · 2026-11-23 20:54 UTC · original · reviewed

Found a concrete bounds bug in cl_edition::after_call at this head. For k == 2, a suffix FF 14 (mod=0, /2, rm=4) gives sib == 1; the match guard reads code[c-k+2], i.e. code[8] on an eight-byte array. That panics, potentially aborting when reached through the extern-system slot handlers. It can even occur at a genuine return address: 90 90 FF 90 00 00 FF 14 is a valid call [rax+disp32], but the shorter candidate is examined first.

Minimal fix: immediately after calculating sib, add if sib == 1 && k < 3 { continue; }. Do not return false there, because a longer candidate can still be valid. Suggested regressions in return_addresses_must_follow_a_call: assert!(!t(&[0xff, 0x14])); and assert!(t(&[0xff, 0x90, 0, 0, 0xff, 0x14]));.

I verified the original failure and the guard using an isolated Python transcription of this function, including all 256 ModRM values following FF at the end of an 8-byte buffer and the existing positive/negative controls. No Rust build or game test here. This fixes the out-of-bounds case only; it does not establish that arbitrary stack words or suffixes are reliable call sites.

**@⁠RomainROCH** on GitHub · 2026-11-23 20:54 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/334#pullrequestreview-6029353410) · **reviewed** Found a concrete bounds bug in `cl_edition::after_call` at this head. For `k == 2`, a suffix `FF 14` (mod=0, /2, rm=4) gives `sib == 1`; the match guard reads `code[c-k+2]`, i.e. `code[8]` on an eight-byte array. That panics, potentially aborting when reached through the extern-system slot handlers. It can even occur at a genuine return address: `90 90 FF 90 00 00 FF 14` is a valid `call [rax+disp32]`, but the shorter candidate is examined first. Minimal fix: immediately after calculating `sib`, add `if sib == 1 && k < 3 { continue; }`. Do not return false there, because a longer candidate can still be valid. Suggested regressions in `return_addresses_must_follow_a_call`: `assert!(!t(&[0xff, 0x14]));` and `assert!(t(&[0xff, 0x90, 0, 0, 0xff, 0x14]));`. I verified the original failure and the guard using an isolated Python transcription of this function, including all 256 ModRM values following FF at the end of an 8-byte buffer and the existing positive/negative controls. No Rust build or game test here. This fixes the out-of-bounds case only; it does not establish that arbitrary stack words or suffixes are reliable call sites.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.