D3D12-HLE-XNETWORKING-WIDE-ALIGNMENT: read title UTF-16 pointers unaligned in xnetworking::wide #2673

Merged
leonida/mandy
bobcatchris merged 1 commit from fix/d3d12-hle-xnetworking-wide-alignment into main 2026-11-30 16:19:20 +00:00

Pull request successfully merged and closed

bobcatchris commented 2026-11-30 01:42:51 +00:00

Claimed in #2570 as D3D12-HLE-XNETWORKING-WIDE-ALIGNMENT (#issuecomment-7260961050).

What and why

In d3d12/crates/leonida-hle/src/modules/xnetworking.rs, wide(p: *const u16) dereferences the title's UTF-16 string pointer via *p.add(len) and std::slice::from_raw_parts(p, len). Both require 2-byte alignment and trigger undefined behavior (aborting with 0xc0000409 / misaligned pointer dereference in debug builds) when a title passes an odd-aligned u16 pointer.

This change reads UTF-16 code units with p.add(len).read_unaligned() into a Vec<u16> before calling String::from_utf16_lossy, matching the unaligned-read convention used elsewhere in leonida-hle and leonida-gpu.

Verification (Red -> Green)

Hardware / OS: native Windows 11 Pro (10.0.26200), AMD Ryzen 7 7800X3D, Rust 1.98.

  • Red (before fix): cargo test -p leonida-hle --lib xnetworking aborted with 0xc0000409 (misaligned pointer dereference in wide_reads_unaligned_utf16_strings on an odd-aligned buffer).
  • Green (after fix, rebased onto main 40e7fe69d):
    • cargo test -p leonida-hle --lib xnetworking — 2 passed; 0 failed.
    • python -I scripts/check_no_game_data.py d3d12/crates/leonida-hle/src/modules/xnetworking.rs — PASS.

AI-assisted: Antigravity (Gemini 4 Neon)

Claimed in #2570 as `D3D12-HLE-XNETWORKING-WIDE-ALIGNMENT` (`#issuecomment-7260961050`). ### What and why In `d3d12/crates/leonida-hle/src/modules/xnetworking.rs`, `wide(p: *const u16)` dereferences the title's UTF-16 string pointer via `*p.add(len)` and `std::slice::from_raw_parts(p, len)`. Both require 2-byte alignment and trigger undefined behavior (aborting with `0xc0000409` / misaligned pointer dereference in debug builds) when a title passes an odd-aligned `u16` pointer. This change reads UTF-16 code units with `p.add(len).read_unaligned()` into a `Vec<u16>` before calling `String::from_utf16_lossy`, matching the unaligned-read convention used elsewhere in `leonida-hle` and `leonida-gpu`. ### Verification (Red -> Green) Hardware / OS: native Windows 11 Pro (`10.0.26200`), AMD Ryzen 7 7800X3D, Rust `1.98`. - **Red (before fix):** `cargo test -p leonida-hle --lib xnetworking` aborted with `0xc0000409` (misaligned pointer dereference in `wide_reads_unaligned_utf16_strings` on an odd-aligned buffer). - **Green (after fix, rebased onto `main` `40e7fe69d`):** - `cargo test -p leonida-hle --lib xnetworking` — **`2 passed; 0 failed`**. - `python -I scripts/check_no_game_data.py d3d12/crates/leonida-hle/src/modules/xnetworking.rs` — **PASS**. AI-assisted: Antigravity (Gemini 4 Neon)
Kal0s-design commented 2026-11-30 11:25:22 +00:00

Independent Windows verification of c23a0f51b on main d367675c7 (clean merge, 6896ee602). Native Windows 11, MSVC x64, Rust 1.98.1, debug cargo test -p leonida-hle --lib.

  • Red: main + only this PR's test hunk. modules::xnetworking::tests::wide_reads_unaligned_utf16_strings aborts with misaligned pointer dereference: address must be a multiple of 0x2 at xnetworking.rs:85 (the *text.add(i) in wide), exit 0xc0000409.
  • Green: main + this PR. That test passes; whole crate 219 passed, 2 ignored, 0 failed (main: 218 + this one).

Read the change too: the loop stops at the first NUL exactly like the old take_while, and null still gives None, so aligned callers see the same string.

GPUs tested: n/a (CPU unit tests). AI-assisted: yes.

**Independent Windows verification** of `c23a0f51b` on `main` `d367675c7` (clean merge, `6896ee602`). Native Windows 11, MSVC x64, Rust 1.98.1, debug `cargo test -p leonida-hle --lib`. - Red: `main` + only this PR's test hunk. `modules::xnetworking::tests::wide_reads_unaligned_utf16_strings` aborts with `misaligned pointer dereference: address must be a multiple of 0x2` at `xnetworking.rs:85` (the `*text.add(i)` in `wide`), exit `0xc0000409`. - Green: `main` + this PR. That test passes; whole crate 219 passed, 2 ignored, 0 failed (`main`: 218 + this one). Read the change too: the loop stops at the first NUL exactly like the old `take_while`, and null still gives `None`, so aligned callers see the same string. GPUs tested: n/a (CPU unit tests). AI-assisted: yes.
KelboLive commented 2026-11-30 16:05:45 +00:00

Independent check on Windows (not the author), at c23a0f51b.

  • cargo test --locked -p leonida-hle --lib: 219 passed, 0 failed, 2 ignored in 0.75 s.
  • leonida-hle only; no game run, so this covers the unaligned wide-pointer unit cases plus the crate's existing tests.

Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, Rust 1.98.1 pinned.

AI-assisted: yes (opencode).

Independent check on Windows (not the author), at `c23a0f51b`. - `cargo test --locked -p leonida-hle --lib`: **219 passed, 0 failed, 2 ignored** in 0.75 s. - `leonida-hle` only; no game run, so this covers the unaligned wide-pointer unit cases plus the crate's existing tests. Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, Rust 1.98.1 pinned. AI-assisted: yes (opencode).
MoonEyes commented 2026-11-30 16:19:50 +00:00

Merged on head c23a0f51b. @Kal0s-design ran the red/green on this head (Windows 11, Rust 1.98.1: the new test aborts on main and passes with this pull request); @KelboLive ran the crate green on this head (219 passed). CPU unit tests, no GPU. CI was green on this head and no test was run by the merger.

Merged by MoonEyes. AI-assisted: yes (Claude Code).

Merged on head `c23a0f51b`. @Kal0s-design ran the red/green on this head (Windows 11, Rust 1.98.1: the new test aborts on `main` and passes with this pull request); @KelboLive ran the crate green on this head (219 passed). CPU unit tests, no GPU. CI was green on this head and no test was run by the merger. Merged by MoonEyes. AI-assisted: yes (Claude Code).
Sign in to join this conversation.
No description provided.