Skip to content

d3d12/hle: fix GUID parsing, OOM reporting and unaligned access in rpcrt4 - #2579

Merged
MoonEyes merged 1 commit into
ProjectLeonida:mainfrom
imShangLi:fix/d3d12-hle-rpcrt4-uuid-parse-and-oom-bounds
Nov 28, 2026
Merged

MoonEyes merged 1 commit into
ProjectLeonida:mainfrom
imShangLi:fix/d3d12-hle-rpcrt4-uuid-parse-and-oom-bounds

Conversation

@imShangLi

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D12-HLE-RPCRT4-UUID-PARSE-AND-OOM-BOUNDS (#2570 (comment))

What and why

Three defects in d3d12/crates/leonida-hle/src/modules/rpcrt4.rs, all in the GUID calls a title loads the RPC runtime for.

1. parse() took any text whose first 32 non-dash characters were hex digits. Win32 rpcrt4 refuses all of these with RPC_S_INVALID_STRING_UUID (1705):

  • a { with no matching },
  • a } with no opening {,
  • any text after the closing } (the old parser returned as soon as it had seen the 33rd character, so {...}trailing parsed as a GUID),
  • a - at any position, or none at all.

The parser now takes the canonical form only: 32 hex digits with the dashes at 8, 13, 18 and 23, and the braces both there or both not (36 or 38 characters, ASCII only). Everything else is RPC_S_INVALID_STRING_UUID, and a refused text leaves the caller's Guid untouched.

2. UuidToStringA / UuidToStringW reported success on a failed allocation. They returned RPC_S_OK (0) with *out == null when the allocation failed, so a title could not tell an empty string from an out-of-memory one. Both now report RPC_S_OUT_OF_MEMORY (14) and leave the caller's pointer as it was (publish()).

3. The caller's Guid was read and written with aligned accesses. UuidCreate, UuidFromStringA/W and UuidToStringA/W used write() / as_ref(), which require the caller's pointer to be aligned for Guid (4 bytes). A Guid in a title's own buffer need not be, and an aligned access to a misaligned address is undefined behaviour (it faults in a debug build). These are now write_unaligned / read_unaligned.

Tested

  • cargo test --package leonida-hle --locked: 186 passed; 0 failed; 2 ignored (10 in modules::rpcrt4, 5 of them new).
  • The new tests cover: the malformed forms above (unmatched braces, trailing text after }, a misplaced dash, a missing dash, empty text, text longer than any legal form), the canonical form with braces and in upper case, a wide string with a non-ASCII unit, a refused text leaving the Guid as it was, publish() reporting RPC_S_OUT_OF_MEMORY without touching the caller's pointer, and a Guid at an odd address (an 8-byte aligned buffer plus 1) written and read back - the aligned access this replaces panics in a debug build.
  • cargo clippy --package leonida-hle --all-targets --no-deps --locked -- -D warnings: no new findings. With the pre-change version of rpcrt4.rs put back in place the command's output is byte-identical to the branch's - 28 lint locations in both, in leonida-gpu, leonida-shader, and leonida-hle's xmem.rs / dstorage.rs / clock.rs / gameinput.rs - and none of them is in the file this pull request touches. The command exits non-zero on main as well, because -D warnings turns those pre-existing findings into errors.
  • rustfmt --edition 2024 --check crates/leonida-hle/src/modules/rpcrt4.rs: clean.
  • python -I scripts/check_no_game_data.py d3d12/crates/leonida-hle/src/modules/rpcrt4.rs (exit 0).

GPUs tested: n/a (a CPU-only HLE module; verified on Windows 11 x64)

AI-assisted: yes

Checklist

  • Based on main d48a957bb and merges cleanly into the current main: no commit since that base touches d3d12/crates/leonida-hle, so the one file this pull request changes is untouched upstream (a conflicting pull request is labeled needs rebase).
  • 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.)

…llocation says so (D3D12-HLE-RPCRT4-UUID-PARSE-AND-OOM-BOUNDS)
@imShangLi

Copy link
Copy Markdown
Contributor Author

Note on the CI: this pull request changes one Rust file under d3d12/, and the repository's jobs do not build or run the d3d12/ Rust crates at all (the nested d3d12/.github/workflows/ci.yml is not executed; the root workflow only builds tools/bcpack_calibration). The stdlib-tests-linux, macos-python and semantic-tests failures are the red tests that main carries on its own — the same set of jobs fails on the other open pull requests (#2575, #2566, #2549 and #2548 all show it). The change itself is verified in the description: cargo test --package leonida-hle --locked (186 passed, 0 failed), clippy with no new findings, rustfmt --check, and python -I scripts/check_no_game_data.py.

@lixonik lixonik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed d6e73e167 (d3d12/crates/leonida-hle/src/modules/rpcrt4.rs).

The read_unaligned/write_unaligned helpers handle arbitrary guest GUID alignment, the canonical ASCII parser (36 chars or {36} with - at 8/13/18/23) rejects malformed or non-ASCII inputs without clobbering *out, and publish reports RPC_S_OUT_OF_MEMORY cleanly when allocation fails.

Local verification:

  • cargo test --manifest-path d3d12/Cargo.toml -p leonida-hle --lib rpcrt4: 10/10 passed (a_created_guid_is_version_four, a_guid_formats_and_parses_back, a_malformed_guid_is_refused, a_text_outside_ascii_is_refused, a_refused_text_leaves_the_guid_alone, garbage_is_refused, a_failed_allocation_leaves_the_callers_pointer_alone, a_misaligned_guid_is_written_and_read, uuid_from_string_writes_what_it_reads, a_string_is_freed_once_through_either_call).
  • python -I scripts/check_no_game_data.py: passed.

AI-assisted (Antigravity, Gemini).

@MoonEyes

Copy link
Copy Markdown
Collaborator

Red/green: On main the rpcrt4 tests a_malformed_guid_is_refused and a_refused_text_leaves_the_guid_alone fail (a GUID without its closing brace or with misplaced dashes is accepted), and with the PR cargo test -p leonida-hle --lib -- rpcrt4 passes (12 OK) on Windows 11 at OreoFR's (RTX 4070).
Independent tester (OreoFR, RTX 4070, Windows 11): PR head merged with the other PRs of its batch on main c1ecf04, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.

@MoonEyes
MoonEyes merged commit c52eb6a into ProjectLeonida:main Nov 28, 2026
4 of 8 checks passed
@OreoFR

OreoFR commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Verified by OreoFR

  • Base: main 54d27d988 + PR head d6e73e167; green run in a merge package of 5 PRs, no new ctest failure vs main
  • Test: cargo test -p leonida-hle --lib --locked -- rpcrt4
  • Red on main: the full test diff fails to compile (publish missing on main); without the publish test: 2 failures (a_malformed_guid_is_refused, a_refused_text_leaves_the_guid_alone), 7 OK
  • Green with the PR: 12 OK in the package
  • Verdict: READY
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

AI-assisted (Claude Code, Claude Opus 5.5).

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.

4 participants