Skip to content

d3d12/hle: read the title's own buffers unaligned in ffx and xgameruntime - #2605

Merged
MoonEyes merged 1 commit into
ProjectLeonida:mainfrom
Mby159:fix/d3d12-hle-title-pointer-alignment
Nov 29, 2026
Merged

MoonEyes merged 1 commit into
ProjectLeonida:mainfrom
Mby159:fix/d3d12-hle-title-pointer-alignment

Conversation

@Mby159

@Mby159 Mby159 commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

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: "A Guid in 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 with as_ref(). Guid is { u32, u16, u16, [u8; 8] }, so it wants 4-byte alignment. Both are now read with read_unaligned(); a null provider or id is still refused with NOT_SUPPORTED, as before.
  • d3d12/crates/leonida-hle/src/modules/ffx.rs, ffxDispatch and ffxCreateContext: the dispatch description is read with as_ref() and &*desc.cast::<Upscale>(), the creation description with as_ref() and &mut *desc.cast::<CreateUpscale>(). Header is { u64, *const Header }, so it wants 8-byte alignment. The descriptions are now read with read_unaligned(), and the two sizes ffxCreateContext writes into the title's description (the stand-in sizes, put back afterwards) are written with write_unaligned() through addr_of_mut!, so the write needs no alignment either. The null handling, the UPSCALE / CREATE_UPSCALE kind checks, the stand-in-and-restore behaviour and the tracing output are unchanged.

Two details worth naming:

  • The creation description is no longer held as a &mut across 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.rs was not rustfmt-clean on main (an extra blank line after the imports, and a create signature over the 100-column limit); both are fixed here, so the file is clean.

d3d12/crates/leonida-hle/src/modules/dstorage.rs has 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 rpcrt4 fix: 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:

  • QueryApiImpl with the provider and the id at an odd address (the same outcome as the aligned call, NOT_SUPPORTED for a pair that is not implemented, and nothing panics);
  • an ffxDispatch description at an odd address (the same outcome as the aligned call);
  • an ffxCreateContext description 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: upstream main at ef329958483832297acb53e68be94b439e88a90e.

  • Baseline, on the pristine tree, before this change:
    cargo test --package leonida-hle --locked --offline -> 211 passed; 0 failed; 2 ignored (the leonida-hle unit tests, 213 tests run) and 1 passed (the no_dll_planting integration 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 ignored for the leonida-hle unit tests, 216 tests run, and 1 passed for no_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_written and a_dispatch_description_at_an_odd_address_is_read abort on main with misaligned pointer dereference: address must be a multiple of 0x8 in ffx.rs. The third, a_guid_at_an_odd_address_is_read_as_the_aligned_one_is, does not fault on main: the aligned read of the Guid is 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 in ffx.rs or xgameruntime.rs. They are in leonida-hle/src/modules/dstorage.rs (4), xmem.rs (2), clock.rs (1), gameinput.rs (1) and gameinput/kbm.rs (1). With -- -D warnings the command exits non-zero on main as 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

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase). The change is in d3d12/crates/leonida-hle/src/modules/ffx.rs and d3d12/crates/leonida-hle/src/modules/xgameruntime.rs; the module in flight, d3d12/crates/leonida-hle/src/modules/dstorage.rs, is deliberately not touched.
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: this pull request adds no translator rule.)

@Mby159

Mby159 commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

CI attribution: the five failing checks are red on main itself.

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 of main (ef32995848):

check this PR (272ca397) main (ef32995848)
check failure failure
macos-python failure failure
translator-tests failure failure
semantic-tests (ubuntu-latest) failure failure
stdlib-tests-linux failure failure
viewer-tests success success
no-game-data success success
change-scope success skipped

None 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 #2570 (comment): a job that runs cargo test for the d3d12/ workspace would need the hardware-free reds on main fixed first, otherwise it is red on arrival.

AI-assisted.

@OreoFR

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

TAKEN by OreoFR (Windows, RTX 4070 8 GB): red/green on main + PR, ctest, gta6rt link

@OreoFR

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Verified by OreoFR — READY

  • Base: main ef32995 + this PR (head 272ca39) merged with --no-ff
  • Red (main + only this PR's tests): cargo test -p leonida-hle --locked --offline (debug, Rust from d3d12/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_written and a_dispatch_description_at_an_odd_address_is_read abort on main with misaligned pointer dereference: address must be a multiple of 0x8 in ffx.rs. a_guid_at_an_odd_address_is_read_as_the_aligned_one_is passes on main: the as_ref() read is undefined behaviour, but the debug check does not catch it there and x86 loads the GUID anyway.
  • Green (merge): cargo test -p leonida-hle 214 passed, 0 failed, 2 ignored, plus no_dll_planting 1 passed (main alone: 211 passed, 2 ignored). No other test changed.
  • gta6rt link: n/a (only the Rust HLE crate under d3d12/ changes, nothing gta6rt links)
  • Diff read: no issue. Every read and write of the title's description now goes through read_unaligned/write_unaligned. The &mut that 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: the unsafe flags read-back in create has no // SAFETY: line like the others.
  • Other runs: none before this one.
  • Machine: Windows 11, Intel Core Ultra 7 155H, RTX 4070 Laptop 8 GB, NVIDIA 621.40, Python 3.12.10, MSVC 19.44

AI-assisted (Claude Code, Claude Opus 5.5), run for OreoFR.

@MoonEyes
MoonEyes merged commit aec1623 into ProjectLeonida:main Nov 29, 2026
3 of 8 checks passed
@MoonEyes

Copy link
Copy Markdown
Collaborator

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.

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.

3 participants