D3D12.X: read unaligned UTF-16 and qwords via byte slices in guard::describe_ptr #1594

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

@⁠b3IKK on GitHub · 2026-11-24 18:03 UTC · original
b3IKK:d3d12x/guard-describe-ptr-unaligned → main · +41 −9 · 1 files · 1 commits · open

guard::describe_ptr in crates/xcl/src/hle/d3d12x/guard.rs previously created &[u16] and &[u64] via std::slice::from_raw_parts directly from an arbitrary guest register p without checking 2-byte or 8-byte alignment, which is immediate UB (and a debug-mode panic in Rust) whenever a pointer argument is unaligned.

Moreover, calling std::slice::from_raw_parts directly over foreign guest memory after readable() only proves virtual pages are committed, not that p..p+span belongs to a single Rust allocation or won't race a concurrent decommit.

This copies the target bytes into a local stack buffer [0u8; 128] via ReadProcessMemory(GetCurrentProcess(), ...) and decodes UTF-16 code units and u64 words with from_le_bytes, eliminating both alignment UB and foreign-memory slice UB.

What is in it

  • crates/xcl/src/hle/d3d12x/guard.rs: copy up to 128 bytes via ReadProcessMemory into a local [0u8; 128] buffer and parse UTF-16 / u64 words via from_le_bytes; add unit test describe_ptr_handles_unaligned_utf16_and_binary_pointers (failed before the fix with unsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null).

Tested

  • hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2609.5, debug (scripts/test.ps1) and release (scripts/smoke.ps1)
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 777 passed, 0 failed, 41 ignored (SKIPPED GPU tests: 2 no wave64 — gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask, gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware; 1 no heap windows — heap_window_out_of_memory_falls_back_to_the_switch_form)
  • cargo test -p xcl --lib guard::tests: 1 passed, 0 failed

Regression run

main:

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1226 (PresentX 1226)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15341 MB

PR:

xcl-smoke v1
commit: 6ea563b6ca1155117fd01bde4d22a864dacc16e3
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1638 (PresentX 1639)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14598 MB

Not tested

  • AMD GPUs and native wave64 hardware
  • other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine

AI use

  • OpenCode, Gemini 4 Neon: audited hle/d3d12x/guard.rs, wrote the fix and unit test, switched to ReadProcessMemory into a local stack buffer per review feedback, ran the test suite and scripts/smoke.ps1 regression runs, and drafted this PR description.
**@⁠b3IKK** on GitHub · 2026-11-24 18:03 UTC · [original](https://github.com/xcl-dev/xcl/pull/1594) `b3IKK:d3d12x/guard-describe-ptr-unaligned` → `main` · +41 −9 · 1 files · 1 commits · open `guard::describe_ptr` in `crates/xcl/src/hle/d3d12x/guard.rs` previously created `&[u16]` and `&[u64]` via `std::slice::from_raw_parts` directly from an arbitrary guest register `p` without checking 2-byte or 8-byte alignment, which is immediate UB (and a debug-mode panic in Rust) whenever a pointer argument is unaligned. Moreover, calling `std::slice::from_raw_parts` directly over foreign guest memory after `readable()` only proves virtual pages are committed, not that `p..p+span` belongs to a single Rust allocation or won't race a concurrent decommit. This copies the target bytes into a local stack buffer `[0u8; 128]` via `ReadProcessMemory(GetCurrentProcess(), ...)` and decodes UTF-16 code units and `u64` words with `from_le_bytes`, eliminating both alignment UB and foreign-memory slice UB. **What is in it** - `crates/xcl/src/hle/d3d12x/guard.rs`: copy up to 128 bytes via `ReadProcessMemory` into a local `[0u8; 128]` buffer and parse UTF-16 / `u64` words via `from_le_bytes`; add unit test `describe_ptr_handles_unaligned_utf16_and_binary_pointers` (failed before the fix with `unsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null`). **Tested** - hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (`stable-x86_64-pc-windows-msvc`), DXC 1.9.2609.5, debug (`scripts/test.ps1`) and release (`scripts/smoke.ps1`) - `.\scripts\test.ps1` (`cargo build --workspace` + `cargo test --workspace -- --test-threads=4`): 777 passed, 0 failed, 41 ignored (SKIPPED GPU tests: 2 `no wave64` — `gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware`; 1 `no heap windows` — `heap_window_out_of_memory_falls_back_to_the_switch_form`) - `cargo test -p xcl --lib guard::tests`: 1 passed, 0 failed **Regression run** main: ``` xcl-smoke v1 commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3 gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1226 (PresentX 1226) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 15341 MB ``` PR: ``` xcl-smoke v1 commit: 6ea563b6ca1155117fd01bde4d22a864dacc16e3 gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1638 (PresentX 1639) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14598 MB ``` **Not tested** - AMD GPUs and native wave64 hardware - other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine **AI use** - OpenCode, Gemini 4 Neon: audited `hle/d3d12x/guard.rs`, wrote the fix and unit test, switched to `ReadProcessMemory` into a local stack buffer per review feedback, ran the test suite and `scripts/smoke.ps1` regression runs, and drafted this PR description.
guard::describe_ptr cast arbitrary traced register arguments directly to
`*const u16` / `std::slice::from_raw_parts::<u16>` and `*const u64`,
triggering immediate UB and a debug abort (`misaligned pointer
dereference`) whenever a traced argument pointed to an odd or non-8-byte-
aligned address.

Read UTF-16LE code units and 64-bit words via `chunks_exact(2)` and
`chunks_exact(8)` over the single `u8` byte slice instead.

Assisted-by: OpenCode (Gemini 4 Neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:58:46 +00:00
Author
Owner

@⁠b3IKK on GitHub · 2026-11-24 18:39 UTC · original

Good catch @⁠gut0to — updated in \6ea563b\ to copy into a local [0u8; 128]\ buffer via \ReadProcessMemory(GetCurrentProcess(), ...)\ instead of calling \slice::from_raw_parts\ over arbitrary foreign memory, and enlarged the test's backing array to 256 bytes. Re-ran \scripts/test.ps1\ and the 60 s \scripts/smoke.ps1\ run.

**@⁠b3IKK** on GitHub · 2026-11-24 18:39 UTC · [original](https://github.com/xcl-dev/xcl/pull/1594#issuecomment-7140952925) Good catch @⁠gut0to — updated in \6ea563b\ to copy into a local \[0u8; 128]\ buffer via \ReadProcessMemory(GetCurrentProcess(), ...)\ instead of calling \slice::from_raw_parts\ over arbitrary foreign memory, and enlarged the test's backing array to 256 bytes. Re-ran \scripts/test.ps1\ and the 60 s \scripts/smoke.ps1\ run.
Author
Owner

@⁠futurepro62 on GitHub · 2026-11-24 18:56 UTC · original

Full-suite run, since this PR has no test report yet. Tested exact head 6ea563b (on its base 9e3831e).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, DXC + dxil.dll 1.9.2602.17 (Windows SDK 10.0.26100), debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4 --skip heap_window_out_of_memory_falls_back_to_the_switch_form: 776 passed, 0 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The new tests ran and passed: describe_ptr_handles_unaligned_utf16_and_binary_pointers.
  • The previous head f940362 (the PR was updated before the NVIDIA run above) on the AMD Radeon integrated GPU (iGPU) of the Ryzen 7 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4: 777 passed, 0 failed, 41 ignored. SKIPPED: heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows).
  • heap_window_out_of_memory_falls_back_to_the_switch_form was skipped with --skip. It fails on main 9e3831e too, not because of this PR: under XCL_REQUIRE_GPU=mesh, runner::skip_test turns every skip except "no wave64" into a failure, including this test's opt-in "no heap windows" skip (XCL_HEAP_WINDOWS unset). Reported as #1651.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
**@⁠futurepro62** on GitHub · 2026-11-24 18:56 UTC · [original](https://github.com/xcl-dev/xcl/pull/1594#issuecomment-7141375910) Full-suite run, since this PR has no test report yet. Tested exact head `6ea563b` (on its base `9e3831e`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, DXC + dxil.dll 1.9.2602.17 (Windows SDK 10.0.26100), debug, D3D12 debug layer installed. - `cargo build --workspace`, then `XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4 --skip heap_window_out_of_memory_falls_back_to_the_switch_form`: **776 passed, 0 failed, 41 ignored**. SKIPPED (no wave64): `gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware`. - The new tests ran and passed: `describe_ptr_handles_unaligned_utf16_and_binary_pointers`. - The previous head `f940362` (the PR was updated before the NVIDIA run above) on the AMD Radeon integrated GPU (iGPU) of the Ryzen 7 9800X3D (RDNA2, `XCL_ADAPTER=radeon`), `cargo test --workspace -- --test-threads=4`: **777 passed, 0 failed, 41 ignored**. SKIPPED: `heap_window_out_of_memory_falls_back_to_the_switch_form` (no heap windows). - `heap_window_out_of_memory_falls_back_to_the_switch_form` was skipped with `--skip`. It fails on main `9e3831e` too, not because of this PR: under `XCL_REQUIRE_GPU=mesh`, `runner::skip_test` turns every skip except "no wave64" into a failure, including this test's opt-in "no heap windows" skip (`XCL_HEAP_WINDOWS` unset). Reported as #1651. **Not tested** - a release build, a game (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-24 20:12 UTC · original

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~22 ignored. (Intermittent single-test flake under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified main.)

Thanks!

**@⁠DavidM1911** on GitHub · 2026-11-24 20:12 UTC · [original](https://github.com/xcl-dev/xcl/pull/1594#issuecomment-7143185117) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~22 ignored. (Intermittent single-test flake under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified `main`.) Thanks!
Author
Owner

@⁠gut0to on GitHub · 2026-11-24 18:07 UTC · original
On crates/xcl/src/hle/d3d12x/guard.rs line 95 (outdated):

@@ -89,21 +89,28 @@ unsafe fn describe_ptr(p: u64) -> String {
     if p <= 0x10000 || !crate::gpu::shader::readable(p as usize, 32) {
         return String::new();
     }
-    let b = std::slice::from_raw_parts(p as *const u8, 32);
     let printable = |c: u8| (0x20..0x7f).contains(&c);
     // how far a string may be scanned without leaving readable memory
     let span = if crate::gpu::shader::readable(p as usize, 128) { 128 } else { 32 };
-    if b[..8].chunks(2).all(|c| printable(c[0]) && c[1] == 0) {
-        let n = (0..span / 2).take_while(|&k| *(p as *const u16).add(k) != 0).count();
-        let s = String::from_utf16_lossy(std::slice::from_raw_parts(p as *const u16, n));
+    let bytes = std::slice::from_raw_parts(p as *const u8, span);

readable only proves that the virtual pages are committed and readable; it cannot prove that p..p+span belongs to one Rust allocation. Forming a 128-byte slice here can therefore be undefined behavior for a valid game pointer near an allocation boundary. The new test passes a pointer into a 64-byte array, which makes this mismatch concrete if the surrounding stack page is readable. Please bound the read to caller-known storage or copy via a bounded raw-read mechanism before forming a slice. AI-assisted review: I verified this against readable and the new test.

**@⁠gut0to** on GitHub · 2026-11-24 18:07 UTC · [original](https://github.com/xcl-dev/xcl/pull/1594#discussion_r4684419644) On `crates/xcl/src/hle/d3d12x/guard.rs` line 95 (outdated): ```diff @@ -89,21 +89,28 @@ unsafe fn describe_ptr(p: u64) -> String { if p <= 0x10000 || !crate::gpu::shader::readable(p as usize, 32) { return String::new(); } - let b = std::slice::from_raw_parts(p as *const u8, 32); let printable = |c: u8| (0x20..0x7f).contains(&c); // how far a string may be scanned without leaving readable memory let span = if crate::gpu::shader::readable(p as usize, 128) { 128 } else { 32 }; - if b[..8].chunks(2).all(|c| printable(c[0]) && c[1] == 0) { - let n = (0..span / 2).take_while(|&k| *(p as *const u16).add(k) != 0).count(); - let s = String::from_utf16_lossy(std::slice::from_raw_parts(p as *const u16, n)); + let bytes = std::slice::from_raw_parts(p as *const u8, span); ``` `readable` only proves that the virtual pages are committed and readable; it cannot prove that `p..p+span` belongs to one Rust allocation. Forming a 128-byte slice here can therefore be undefined behavior for a valid game pointer near an allocation boundary. The new test passes a pointer into a 64-byte array, which makes this mismatch concrete if the surrounding stack page is readable. Please bound the read to caller-known storage or copy via a bounded raw-read mechanism before forming a slice. AI-assisted review: I verified this against `readable` and the new test.
Author
Owner

@⁠gut0to on GitHub · 2026-11-24 18:07 UTC · original · reviewed

AI-assisted review (GPT-6): I checked the readable implementation and traced the slice bounds in the new test.

**@⁠gut0to** on GitHub · 2026-11-24 18:07 UTC · [original](https://github.com/xcl-dev/xcl/pull/1594#pullrequestreview-6044743972) · **reviewed** AI-assisted review (GPT-6): I checked the `readable` implementation and traced the slice bounds in the new test.
Author
Owner

@⁠afpruivo on GitHub · 2026-11-26 12:18 UTC · original

Tested 6ea563b on an RTX 3090, as part of a batch merged onto main b49be18: no failures, no regression.

  • Batch: #1384 e15ddb2, #1594 6ea563b, #1621 39788e8, #1653 69f70c1. They touch no common file and merged cleanly onto b49be18 (local commit 1e3fb07). Check posted in #1607: B21, #1607.
  • Hardware: RTX 3090 24 GB, driver 621.40, Windows 11 26300, Rust stable MSVC.
  • Suite: cargo test --workspace -- --test-threads=4 (debug): 1,439 passed, 0 failed, 47 ignored. 26 SKIPPED GPU tests, mostly no wave64 on this card; they are not coverage.
  • Smoke: scripts/smoke.ps1, 60 s, retail 1.0.2.1 (title: 3c290c16), release. Peak commit was lower on the batch.
commit frames shown presents per 2 s (last 3) device removals crashes peak commit
main b49be18 1,649 60 60 60 0 0 17,023 MB
batch 1e3fb07 1,627 60 60 60 0 0 15,516 MB
  • I read the diff. The readable checks stay before the copy, and all three outputs (UTF-16, ASCII, the four qwords) now read only from the local 128-byte buffer.

Not tested: a run that reaches this change on purpose (the smoke title only shows that nothing regresses); AMD.

AI use: Claude Code (Claude Opus 5.5) ran the commands on my machine and drafted this. Checked by me before posting.

**@⁠afpruivo** on GitHub · 2026-11-26 12:18 UTC · [original](https://github.com/xcl-dev/xcl/pull/1594#issuecomment-7191831010) **Tested `6ea563b` on an RTX 3090, as part of a batch merged onto `main` `b49be18`: no failures, no regression.** - **Batch:** #1384 `e15ddb2`, #1594 `6ea563b`, #1621 `39788e8`, #1653 `69f70c1`. They touch no common file and merged cleanly onto `b49be18` (local commit `1e3fb07`). Check posted in #1607: B21, https://git.gtahub.st/github/xcl-dev--xcl/issues/1607. - **Hardware:** RTX 3090 24 GB, driver 621.40, Windows 11 26300, Rust stable MSVC. - **Suite:** `cargo test --workspace -- --test-threads=4` (debug): 1,439 passed, 0 failed, 47 ignored. 26 `SKIPPED` GPU tests, mostly no wave64 on this card; they are not coverage. - **Smoke:** `scripts/smoke.ps1`, 60 s, retail 1.0.2.1 (`title: 3c290c16`), release. Peak commit was lower on the batch. | | commit | frames shown | presents per 2 s (last 3) | device removals | crashes | peak commit | |---|---|---|---|---|---|---| | `main` | `b49be18` | 1,649 | 60 60 60 | 0 | 0 | 17,023 MB | | batch | `1e3fb07` | 1,627 | 60 60 60 | 0 | 0 | 15,516 MB | - I read the diff. The `readable` checks stay before the copy, and all three outputs (UTF-16, ASCII, the four qwords) now read only from the local 128-byte buffer. **Not tested:** a run that reaches this change on purpose (the smoke title only shows that nothing regresses); AMD. **AI use:** Claude Code (Claude Opus 5.5) ran the commands on my machine and drafted this. Checked by me before posting.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

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