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

Merged
Mby159 merged 1 commit from upstream-pr/2579 into main 2026-11-28 20:19:28 +00:00
Mby159 commented 2026-11-28 16:03:05 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Mby159 on 2026-11-28T16:03:05Z.


Claimed in the coordination issue as: D3D12-HLE-RPCRT4-UUID-PARSE-AND-OOM-BOUNDS (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7239926176)

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.)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5542046393 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2579). Originally posted by [Mby159](https://github.com/Mby159) on 2026-11-28T16:03:05Z. --- Claimed in the coordination issue as: `D3D12-HLE-RPCRT4-UUID-PARSE-AND-OOM-BOUNDS` (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7239926176) ### 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 - [x] 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`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] 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 commented 2026-11-28 16:08:22 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Mby159 on 2026-11-28T16:08:22Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7242003130 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2579#issuecomment-7242003130). Originally posted by [Mby159](https://github.com/Mby159) on 2026-11-28T16:08:22Z. --- 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 commented 2026-11-28 20:07:21 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-28T20:07:21Z.

Review: APPROVED


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).

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6099890717 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2579#pullrequestreview-6099890717). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-28T20:07:21Z. **Review:** APPROVED --- 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 commented 2026-11-28 20:19:26 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T20:19:26Z.


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 c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7245201722 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2579#issuecomment-7245201722). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T20:19:26Z. --- 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 c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.
OreoFR commented 2026-11-28 22:05:12 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T22:05:12Z.


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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7246485605 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2579#issuecomment-7246485605). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-28T22:05:12Z. --- **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 in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!2579
No description provided.