Repository navigation
d3d12/hle: read the title's own buffers unaligned in ffx and xgameruntime - #2605
Conversation
…TLE-POINTER-ALIGNMENT)
|
CI attribution: the five failing checks are red on This branch touches two Rust files under
None of the five is caused by this pull request. The three checks that do look at the change ( I reported the missing Rust CI separately at #2570 (comment): a job that runs AI-assisted. |
|
TAKEN by OreoFR (Windows, RTX 4070 8 GB): red/green on main + PR, ctest, gta6rt link |
|
Verified by OreoFR — READY
AI-assisted (Claude Code, Claude Opus 5.5), run for OreoFR. |
|
Merged. Tested by OreoFR (RTX 4070 Laptop 8 GB, NVIDIA 621.40, Windows 11) on this head: cargo test -p leonida-hle red on main with only this PR's tests, green with the PR (READY). AI-assisted: yes. |
Claimed in the coordination issue as: D3D12-HLE-TITLE-POINTER-ALIGNMENT (#2570 (comment))
What and why
The HLE runs inside the title's process, so several of its entry points read the title's own buffers through pointers the title chose. Two of them read those pointers with ALIGNED Rust references (
ptr::as_ref(),&*ptr.cast::<T>()), which require the address to be aligned for the Rust type. A title's buffer need not be, so those are undefined behaviour, and they fault ("misaligned pointer dereference") in a debug build.This is the same defect the maintainers just accepted in
d3d12/crates/leonida-hle/src/modules/rpcrt4.rs: "AGuidin a title's own buffer need not be aligned, and an aligned access to a misaligned address is undefined behaviour (it faults in a debug build)." Two more places have it:d3d12/crates/leonida-hle/src/modules/xgameruntime.rs,QueryApiImpl: the provider and the interface id are read withas_ref().Guidis{ u32, u16, u16, [u8; 8] }, so it wants 4-byte alignment. Both are now read withread_unaligned(); a null provider or id is still refused withNOT_SUPPORTED, as before.d3d12/crates/leonida-hle/src/modules/ffx.rs,ffxDispatchandffxCreateContext: the dispatch description is read withas_ref()and&*desc.cast::<Upscale>(), the creation description withas_ref()and&mut *desc.cast::<CreateUpscale>().Headeris{ u64, *const Header }, so it wants 8-byte alignment. The descriptions are now read withread_unaligned(), and the two sizesffxCreateContextwrites into the title's description (the stand-in sizes, put back afterwards) are written withwrite_unaligned()throughaddr_of_mut!, so the write needs no alignment either. The null handling, theUPSCALE/CREATE_UPSCALEkind checks, the stand-in-and-restore behaviour and the tracing output are unchanged.Two details worth naming:
&mutacross the call into the title's own upscaler. That reference aliased the title's buffer for the whole call, which was undefined behaviour on its own. The sizes are now written and restored by address, and the flags are read back from the title's buffer after the call, so a change the title's upscaler made to them is still reported.ffx.rswas not rustfmt-clean onmain(an extra blank line after the imports, and acreatesignature over the 100-column limit); both are fixed here, so the file is clean.d3d12/crates/leonida-hle/src/modules/dstorage.rshas the same shape (iid.as_ref(),desc.as_ref(),&*request.source.cast::<Instance<File>>()), and it is deliberately left alone: PR #2569 is in flight there, and touching it would duplicate another contributor's work.The new tests mirror the "odd address" test of the accepted
rpcrt4fix: a GUID / a description at an 8-byte aligned buffer plus 1 byte, which is an odd address for every type involved. The aligned access this replaces panics in a debug build, so the tests assert that the misaligned call reaches the same outcome as the aligned one and that nothing panics. They cover:QueryApiImplwith the provider and the id at an odd address (the same outcome as the aligned call,NOT_SUPPORTEDfor a pair that is not implemented, and nothing panics);ffxDispatchdescription at an odd address (the same outcome as the aligned call);ffxCreateContextdescription at an odd address (the title's own upscaler is asked for the stand-in sizes, the title's own sizes are back in its description afterwards, and the outcome is the aligned one).Tested
One machine (Windows 11 x64, Rust 1.98.1 as pinned by
d3d12/rust-toolchain.toml); every cargo invocation below used--locked --offline. Base: upstreammainatef329958483832297acb53e68be94b439e88a90e.Baseline, on the pristine tree, before this change:
cargo test --package leonida-hle --locked --offline->211 passed; 0 failed; 2 ignored(theleonida-hleunit tests, 213 tests run) and1 passed(theno_dll_plantingintegration test), that is 212 passed; 0 failed; 2 ignored in total.After this change, the same command: 215 passed; 0 failed; 2 ignored (
214 passed; 0 failed; 2 ignoredfor theleonida-hleunit tests, 216 tests run, and1 passedforno_dll_planting) - the three new tests, and nothing else changed.Red to green, for the two that fault:
a_creation_description_at_an_odd_address_is_read_and_writtenanda_dispatch_description_at_an_odd_address_is_readabort onmainwithmisaligned pointer dereference: address must be a multiple of 0x8inffx.rs. The third,a_guid_at_an_odd_address_is_read_as_the_aligned_one_is, does not fault onmain: the aligned read of theGuidis undefined behaviour there, but the debug check does not catch it on x64 and the load goes through, so that one is not a red-to-green pair and this pull request does not claim it is.Independently verified by @OreoFR (Windows 11, RTX 4070 Laptop 8 GB, NVIDIA 621.40): d3d12/hle: read the title's own buffers unaligned in ffx and xgameruntime #2605 (comment)
cargo clippy --package leonida-hle --all-targets --no-deps --locked --offline: the same 9 findings on the pristine tree and with this change - the two sets are identical, and none of them is inffx.rsorxgameruntime.rs. They are inleonida-hle/src/modules/dstorage.rs(4),xmem.rs(2),clock.rs(1),gameinput.rs(1) andgameinput/kbm.rs(1). With-- -D warningsthe command exits non-zero onmainas well, because those pre-existing findings become errors; this pull request neither adds a finding nor touches one of those files.rustfmt --edition 2024 --check crates/leonida-hle/src/modules/ffx.rs crates/leonida-hle/src/modules/xgameruntime.rs: clean.python -I scripts/check_no_game_data.py: exit 0.GPUs tested: n/a (a CPU-only HLE module; verified on Windows 11 x64)
AI-assisted: yes
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase). The change is ind3d12/crates/leonida-hle/src/modules/ffx.rsandd3d12/crates/leonida-hle/src/modules/xgameruntime.rs; the module in flight,d3d12/crates/leonida-hle/src/modules/dstorage.rs, is deliberately not touched.docs/design.md, with how it was confirmed. (n/a: this pull request adds no translator rule.)