Loader: clamp VEH instruction reads to the faulting page in emulate_amd_only and sse4a #2443

Open
Texbirds wants to merge 1 commit from upstream-pr/2445 into main
Texbirds commented 2026-11-25 13:39:03 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Texbirds on 2026-11-25T13:39:03Z.


Problem

In crates/victory-loader/src/crash.rs, the vectored exception handler calls emulate_amd_only() on EXCEPTION_ILLEGAL_INSTRUCTION. On main:

  • emulate_amd_only() unconditionally creates a 3-byte slice std::slice::from_raw_parts(rip as *const u8, 3) before matching 3-byte AMD-only opcodes (0F 01 D9, 0F 01 F8, etc.).
  • sse4a() takes a raw *const u8 and reads up to 6 bytes (*code.add(at + 4)) without checking how many bytes remain in the readable page.

If a #UD occurs in the last 1–5 bytes of a mapped 4 KiB page followed by an unmapped or PAGE_NOACCESS page (for example ud2 at page offset 0xffe, a 1-byte #UD at 0xfff, or a truncated 0x66 0x0f 0x78 sequence at 0xffc), reading past 0x1000 causes a secondary Access Violation inside the crash handler itself.

Change

  • In emulate_amd_only(), clamp the instruction slice length to room = 0x1000 - (rip as usize & 0xfff) bytes (the remainder of the faulting 4 KiB page, which is known mapped because instruction fetch reached Rip).
  • Change sse4a() from unsafe fn(code: *const u8, ...) to safe fn(code: &[u8], ...) using checked .get(...) reads for every byte.
  • Scope note: Clamping to the current 4 KiB page avoids syscalls in the VEH path; a genuine multi-byte AMD-only or SSE4a instruction that itself straddles two readable 4 KiB pages would not be emulated by this check (falls through as unhandled #UD rather than reading the next page).

Test

ud_instructions_at_page_end_do_not_read_past_page (Windows, synthetic VirtualAlloc buffer, no game data):

  • Unit-checks sse4a on empty, 1-byte, and truncated 4-byte slices.
  • Allocates two 4 KiB pages with the second marked PAGE_NOACCESS, places a 1-byte #UD at 0xfff, ud2 (0f 0b) at 0xffe, and a truncated 4-byte extrq at 0xffc, and verifies emulate_amd_only returns false without faulting and leaves Rip unchanged.
  • Places a complete 6-byte extrq xmm0, 8, 4 ending at 0x1000 and verifies it still emulates cleanly (Rip advances by 6 and xmm0.Low extracts 0x23 from 0x1234).
<!-- greyfinch-sync:Marvisak/VIctory:issue:6605776104 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2445). Originally posted by [Texbirds](https://github.com/Texbirds) on 2026-11-25T13:39:03Z. --- ### Problem In `crates/victory-loader/src/crash.rs`, the vectored exception handler calls `emulate_amd_only()` on `EXCEPTION_ILLEGAL_INSTRUCTION`. On `main`: - `emulate_amd_only()` unconditionally creates a 3-byte slice `std::slice::from_raw_parts(rip as *const u8, 3)` before matching 3-byte AMD-only opcodes (`0F 01 D9`, `0F 01 F8`, etc.). - `sse4a()` takes a raw `*const u8` and reads up to 6 bytes (`*code.add(at + 4)`) without checking how many bytes remain in the readable page. If a `#UD` occurs in the last 1–5 bytes of a mapped 4 KiB page followed by an unmapped or `PAGE_NOACCESS` page (for example `ud2` at page offset `0xffe`, a 1-byte `#UD` at `0xfff`, or a truncated `0x66 0x0f 0x78` sequence at `0xffc`), reading past `0x1000` causes a secondary Access Violation inside the crash handler itself. ### Change - In `emulate_amd_only()`, clamp the instruction slice length to `room = 0x1000 - (rip as usize & 0xfff)` bytes (the remainder of the faulting 4 KiB page, which is known mapped because instruction fetch reached `Rip`). - Change `sse4a()` from `unsafe fn(code: *const u8, ...)` to safe `fn(code: &[u8], ...)` using checked `.get(...)` reads for every byte. - **Scope note:** Clamping to the current 4 KiB page avoids syscalls in the VEH path; a genuine multi-byte AMD-only or SSE4a instruction that itself straddles two readable 4 KiB pages would not be emulated by this check (falls through as unhandled `#UD` rather than reading the next page). ### Test `ud_instructions_at_page_end_do_not_read_past_page` (Windows, synthetic `VirtualAlloc` buffer, no game data): - Unit-checks `sse4a` on empty, 1-byte, and truncated 4-byte slices. - Allocates two 4 KiB pages with the second marked `PAGE_NOACCESS`, places a 1-byte `#UD` at `0xfff`, `ud2` (`0f 0b`) at `0xffe`, and a truncated 4-byte `extrq` at `0xffc`, and verifies `emulate_amd_only` returns `false` without faulting and leaves `Rip` unchanged. - Places a complete 6-byte `extrq xmm0, 8, 4` ending at `0x1000` and verifies it still emulates cleanly (`Rip` advances by 6 and `xmm0.Low` extracts `0x23` from `0x1234`).
DenRakEiw commented 2026-11-25 14:07:02 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T13:57:23Z.

Review: APPROVED


Verified against origin/main (940d32f):

  • Clamping VEH instruction reads to the faulting page in emulate_amd_only and sse4a cleanly prevents secondary page faults at page boundaries.
  • cargo test -p victory-loader crash::tests: 24/24 passed (including ud_instructions_at_page_end_do_not_read_past_page).
  • cargo clippy -p victory-loader --all-targets -- -D warnings: 0 warnings.
  • cargo xtask policy: 183 files ok.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6058934891 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2445#pullrequestreview-6058934891). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T13:57:23Z. **Review:** APPROVED --- Verified against `origin/main` (`940d32f`): - Clamping VEH instruction reads to the faulting page in `emulate_amd_only` and `sse4a` cleanly prevents secondary page faults at page boundaries. - `cargo test -p victory-loader crash::tests`: 24/24 passed (including `ud_instructions_at_page_end_do_not_read_past_page`). - `cargo clippy -p victory-loader --all-targets -- -D warnings`: 0 warnings. - `cargo xtask policy`: 183 files ok. AI-assisted (Gemini 4 Neon).
catminou4 commented 2026-11-25 15:03:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T14:11:08Z.

Review: APPROVED


Tested locally: merged into main 940d32f69b, cargo test -p victory-loader --locked on macOS arm64 — the 3 failures are exactly the known pre-existing macOS prepare::tests (host Mach-O parsed as PE, identical on main; #2334's cfg-gating covers this class). Approving — VEH instruction reads clamped to the faulting page.

<!-- greyfinch-sync:Marvisak/VIctory:review:6059174169 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2445#pullrequestreview-6059174169). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T14:11:08Z. **Review:** APPROVED --- Tested locally: merged into main 940d32f69b, `cargo test -p victory-loader --locked` on macOS arm64 — the 3 failures are exactly the known pre-existing macOS prepare::tests (host Mach-O parsed as PE, identical on main; #2334's cfg-gating covers this class). Approving — VEH instruction reads clamped to the faulting page.
cloph-dsp commented 2026-11-25 16:09:18 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-25T15:42:16Z.


Independent local validation (Windows, Rust 1.100.0/MSVC, head 609daf4, no game data): cargo test --locked -p victory-loader --lib -> 35 tests passed, 0 failed, exit 0; git diff --check clean. CPU-only regression run; synthetic VEH page-clamp scenario not replayed. AI-assisted (Hy4 flash). GPU/driver: N/A (CPU tests).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7167252289 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2445#issuecomment-7167252289). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-25T15:42:16Z. --- Independent local validation (Windows, Rust 1.100.0/MSVC, head 609daf4, no game data): `cargo test --locked -p victory-loader --lib` -> 35 tests passed, 0 failed, exit 0; `git diff --check` clean. CPU-only regression run; synthetic VEH page-clamp scenario not replayed. AI-assisted (Hy4 flash). GPU/driver: N/A (CPU tests).
Marvisak commented 2026-11-25 16:09:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T16:02:23Z.


Automated triage for the maintainers.

Merge candidate. In crates/victory-loader/src/crash.rs, emulate_amd_only and sse4a now read only the bytes left in the current page, the way main already does for clwb. Per the description, an opcode spanning two pages is no longer emulated. CI: passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7167538469 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2445#issuecomment-7167538469). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T16:02:23Z. --- <!-- victory-triage v1 head=609daf411f06a8cdb622fb6ac0f8deb7dc14e658 verdict=merge-candidate at=2026-11-25T15:57:43Z --> Automated triage for the maintainers. Merge candidate. In `crates/victory-loader/src/crash.rs`, `emulate_amd_only` and `sse4a` now read only the bytes left in the current page, the way main already does for `clwb`. Per the description, an opcode spanning two pages is no longer emulated. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
kotekompotek commented 2026-11-25 17:05:18 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T16:52:14Z.


Mechanics are right — copying a page-bounded window up front removes the read-past-page fault inside the VEH itself (the old [u8; 3] read could fault for a 3-byte instruction ending exactly at a page end when the next page is unmapped).

One trade-off worth a comment: a multi-byte instruction starting in the last 1–2 bytes of a page and spanning onto the next (mapped — it did execute) now gets a 2-byte slice, matches nothing, and returns false → unhandled crash where the old code emulated it. Rare (needs an AMD-only opcode at offset ≥ 0xffe), and strictly the safer direction, but it is a real regression edge. If ever hit, the fix is a second page-tolerant read attempt for the opcode only — not needed now.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7168656105 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2445#issuecomment-7168656105). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T16:52:14Z. --- Mechanics are right — copying a page-bounded window up front removes the read-past-page fault inside the VEH itself (the old `[u8; 3]` read could fault for a 3-byte instruction ending exactly at a page end when the next page is unmapped). One trade-off worth a comment: a multi-byte instruction *starting* in the last 1–2 bytes of a page and spanning onto the next (mapped — it did execute) now gets a 2-byte slice, matches nothing, and returns false → unhandled crash where the old code emulated it. Rare (needs an AMD-only opcode at offset ≥ 0xffe), and strictly the safer direction, but it is a real regression edge. If ever hit, the fix is a second page-tolerant read attempt for the opcode only — not needed now.
fuzeofficlal commented 2026-11-26 03:04:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T02:10:46Z.

Review: COMMENTED


Reviewed and verified against main (63bcca43) on Windows 11 — clean safety fix and ready to merge:

  • Clamping the VEH instruction copy to room = 0x1000 - (context.Rip as usize & 0xfff) (room.min(32)) before decoding and switching sse4a to safe slice indexing (&[u8]) eliminates the secondary ACCESS_VIOLATION when a #UD sits at the end of a mapped page next to an unmapped/no-access page.

Local verification:

  • cargo test --locked -p victory-loader: 40/40 passed (36 unit including ud_instructions_at_page_end_do_not_read_past_page + crash_log + load_probe + title_dlls)
  • cargo clippy --all-targets -- -D warnings + cargo xtask policy (191 files ok): 0 warnings
<!-- greyfinch-sync:Marvisak/VIctory:review:6067626774 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2445#pullrequestreview-6067626774). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T02:10:46Z. **Review:** COMMENTED --- Reviewed and verified against `main` (`63bcca43`) on Windows 11 — clean safety fix and ready to merge: - Clamping the VEH instruction copy to `room = 0x1000 - (context.Rip as usize & 0xfff)` (`room.min(32)`) before decoding and switching `sse4a` to safe slice indexing (`&[u8]`) eliminates the secondary `ACCESS_VIOLATION` when a `#UD` sits at the end of a mapped page next to an unmapped/no-access page. **Local verification**: - `cargo test --locked -p victory-loader`: **40/40 passed** (`36` unit including `ud_instructions_at_page_end_do_not_read_past_page` + `crash_log` + `load_probe` + `title_dlls`) - `cargo clippy --all-targets -- -D warnings` + `cargo xtask policy` (`191 files ok`): 0 warnings
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2445:upstream-pr/2445'
git switch 'upstream-pr/2445'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2445'
git switch 'upstream-pr/2445'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2445'
git switch 'upstream-pr/2445'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2445'
git switch 'main'
git merge --squash 'upstream-pr/2445'
git switch 'main'
git merge --ff-only 'upstream-pr/2445'
git switch 'main'
git merge 'upstream-pr/2445'
git push origin 'main'
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
Marvisak/VIctory!2443
No description provided.