d3d12/hle: read the title's own buffers unaligned in ffx and xgameruntime #2605
No reviewers
No assignees
Labels
No labels
bug
dependencies
documentation
enhancement
missing claim
needs rebase
needs-triage
question
ready-for-agent
submodules
No project
No milestone
Dependencies
No dependencies set
Notifications
1 participant
Due date
No due date set.
Reference
leonida/mandy!2605
Loading…
Reference in a new issue
No description provided.
Delete branch "fix/d3d12-hle-title-pointer-alignment"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Pull request successfully merged and closed
Claimed in the coordination issue as: D3D12-HLE-TITLE-POINTER-ALIGNMENT (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7242004561)
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): https://github.com/ProjectLeonida/mandy/pull/2605#issuecomment-7252248670
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.)CI attribution: the five failing checks are red on
mainitself.This branch touches two Rust files under
d3d12/, and the repository's workflow does not build or test that tree, so every failing job here fails the same way on the tip ofmain(ef32995848):272ca397)main(ef32995848)checkmacos-pythontranslator-testssemantic-tests (ubuntu-latest)stdlib-tests-linuxviewer-testsno-game-datachange-scopemain: https://github.com/ProjectLeonida/mandy/actions/runs/46223969584None of the five is caused by this pull request. The three checks that do look at the change (
no-game-data,change-scope,viewer-tests) pass, and the local verification (212 -> 215 tests, clippy unchanged, rustfmt clean, no game data) is in the description.I reported the missing Rust CI separately at https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7242123868: a job that runs
cargo testfor thed3d12/workspace would need the hardware-free reds onmainfixed first, otherwise it is red on arrival.AI-assisted.
TAKEN by OreoFR (Windows, RTX 4070 8 GB): red/green on main + PR, ctest, gta6rt link
Verified by OreoFR — READY
cargo test -p leonida-hle --locked --offline(debug, Rust fromd3d12/rust-toolchain.toml), each new test run alone because the first panic aborts the test process.a_creation_description_at_an_odd_address_is_read_and_writtenanda_dispatch_description_at_an_odd_address_is_readabort on main withmisaligned pointer dereference: address must be a multiple of 0x8inffx.rs.a_guid_at_an_odd_address_is_read_as_the_aligned_one_ispasses on main: theas_ref()read is undefined behaviour, but the debug check does not catch it there and x86 loads the GUID anyway.cargo test -p leonida-hle214 passed, 0 failed, 2 ignored, plusno_dll_planting1 passed (main alone: 211 passed, 2 ignored). No other test changed.d3d12/changes, nothing gta6rt links)read_unaligned/write_unaligned. The&mutthat aliased the title's buffer across the call into its own upscaler is gone, and the flags are read back after that call. Null handling and the kind checks are unchanged. Nit: theunsafeflags read-back increatehas no// SAFETY:line like the others.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.