Loader: clwb_len() requires 15 remaining bytes in the page, causing unhandled #UD when clwb sits at page offset 0xFF2..0xFFB #924

Closed
opened 2026-11-26 04:14:12 +00:00 by github-bot · 2 comments
Owner

@⁠hyperland956-lgtm on GitHub · 2026-11-24 14:12 UTC · original

Summary

In crates/victory-loader/src/crash.rs:176, single-instruction clwb emulation is gated on n >= 15 (remaining bytes before the end of the 4 KiB page):

let room = 0x1000 - (context.Rip as usize & 0xfff);
let mut bytes = [0u8; 32];
let n = room.min(bytes.len());
std::ptr::copy_nonoverlapping(code, bytes.as_mut_ptr(), n);
...
if n >= 15
    && let Some(len) = clwb_len(bytes[..15].try_into().unwrap_or([0; 15]))
{
    context.Rip += len as u64;
    return true;
}

A clwb m8 instruction (66 [rex] 0f ae /6 [SIB] [disp8/disp32]) is only 4 to 10 bytes long. If a standalone clwb (or one not matched by clwb_loop) sits within the last 14 bytes of a page (Rip & 0xfff in 0xFF2..=0xFFB), n < 15, so clwb_len is skipped entirely and the exception falls through to sse4a() → false → unhandled STATUS_ILLEGAL_INSTRUCTION (0xc000001d) on pre-Ice Lake Intel CPUs (and any CPU without CLWB).

Suggested fix

Change clwb_len to accept a slice &[u8] (just like clwb_loop(&[u8]) right above it) and bounds-check each byte as it decodes 66, optional REX, 0f ae, ModRM, optional SIB, and displacement, returning Some(len) whenever len <= code.len(). Then remove the n >= 15 guard in emulate_amd_only.

Synthetic unit test

In crates/victory-loader/src/crash.rs (mod tests):

  • Call clwb_len(&[0x66, 0x0f, 0xae, 0x31]) (4-byte slice, simulating Rip & 0xfff == 0xffc) → expect Some(4).
  • Call clwb_len(&[0x66, 0x41, 0x0f, 0xae, 0x31]) (5-byte slice) → expect Some(5).
  • Call clwb_len(&[0x66, 0x0f, 0xae, 0x70]) (truncated disp8) → expect None.
**@⁠hyperland956-lgtm** on GitHub · 2026-11-24 14:12 UTC · [original](https://github.com/Marvisak/VIctory/issues/924) ## Summary In `crates/victory-loader/src/crash.rs:176`, single-instruction `clwb` emulation is gated on `n >= 15` (remaining bytes before the end of the 4 KiB page): ```rust let room = 0x1000 - (context.Rip as usize & 0xfff); let mut bytes = [0u8; 32]; let n = room.min(bytes.len()); std::ptr::copy_nonoverlapping(code, bytes.as_mut_ptr(), n); ... if n >= 15 && let Some(len) = clwb_len(bytes[..15].try_into().unwrap_or([0; 15])) { context.Rip += len as u64; return true; } ``` A `clwb m8` instruction (`66 [rex] 0f ae /6 [SIB] [disp8/disp32]`) is only 4 to 10 bytes long. If a standalone `clwb` (or one not matched by `clwb_loop`) sits within the last 14 bytes of a page (`Rip & 0xfff` in `0xFF2..=0xFFB`), `n < 15`, so `clwb_len` is skipped entirely and the exception falls through to `sse4a()` → `false` → unhandled `STATUS_ILLEGAL_INSTRUCTION` (`0xc000001d`) on pre-Ice Lake Intel CPUs (and any CPU without `CLWB`). ## Suggested fix Change `clwb_len` to accept a slice `&[u8]` (just like `clwb_loop(&[u8])` right above it) and bounds-check each byte as it decodes `66`, optional REX, `0f ae`, ModRM, optional SIB, and displacement, returning `Some(len)` whenever `len <= code.len()`. Then remove the `n >= 15` guard in `emulate_amd_only`. ## Synthetic unit test In `crates/victory-loader/src/crash.rs` (`mod tests`): * Call `clwb_len(&[0x66, 0x0f, 0xae, 0x31])` (4-byte slice, simulating `Rip & 0xfff == 0xffc`) → expect `Some(4)`. * Call `clwb_len(&[0x66, 0x41, 0x0f, 0xae, 0x31])` (5-byte slice) → expect `Some(5)`. * Call `clwb_len(&[0x66, 0x0f, 0xae, 0x70])` (truncated `disp8`) → expect `None`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:14:12 +00:00
Author
Owner

@⁠Super-cod on GitHub · 2026-11-24 15:35 UTC · original

I have implemented and verified a fix for this issue:

Summary of Changes

  1. In \crates/victory-loader/src/crash.rs, \clwb_len\ now accepts a byte slice &[u8]\ and validates bounds during prefix, opcode, ModRM, SIB, and displacement decoding.
  2. In \emulate_amd_only, removed the \

= 15\ threshold and called \clwb_len(&bytes[..n]), enabling single \clwb\ instructions to be emulated when located within the last 14 bytes of a 4 KiB page.

  1. Updated \clwb_lengths\ unit tests to pass slices without artificial padding, and added test cases covering short slices and page-boundary truncation.
  2. Validation: \cargo test -p victory-loader --lib\ (22 passed, 0 failed), \cargo clippy, \cargo run -p xtask -- policy\ (154 files ok), and \cargo fmt --check\ all pass.
**@⁠Super-cod** on GitHub · 2026-11-24 15:35 UTC · [original](https://github.com/Marvisak/VIctory/issues/924#issuecomment-7136270906) I have implemented and verified a fix for this issue: - **Commit**: 958f998 - **Pull Request**: https://github.com/Super-cod/VIctory/pull/3 - **Compare with main**: https://github.com/Marvisak/VIctory/compare/main...Super-cod:VIctory:fix/clwb-page-boundary?expand=1 ### Summary of Changes 1. In \crates/victory-loader/src/crash.rs\, \clwb_len\ now accepts a byte slice \&[u8]\ and validates bounds during prefix, opcode, ModRM, SIB, and displacement decoding. 2. In \emulate_amd_only\, removed the \ >= 15\ threshold and called \clwb_len(&bytes[..n])\, enabling single \clwb\ instructions to be emulated when located within the last 14 bytes of a 4 KiB page. 3. Updated \clwb_lengths\ unit tests to pass slices without artificial padding, and added test cases covering short slices and page-boundary truncation. 4. Validation: \cargo test -p victory-loader --lib\ (22 passed, 0 failed), \cargo clippy\, \cargo run -p xtask -- policy\ (154 files ok), and \cargo fmt --check\ all pass.
Author
Owner

@⁠winne42 on GitHub · 2026-11-24 19:53 UTC · original

I tested the three fixes side by side, as Windows builds (x86_64-pc-windows-msvc) run under Wine 10.0 on Linux: #941 (60d2cf9), Super-cod's branch (958f998), and the crash.rs part of #1330 (fb40a8f). All three fix the bug and decode the same way.

main #941 Super-cod #1330
Own crash:: tests 13 pass 14 pass 13 pass 13 pass
Truncated slices matching main's decoder (4,289,152) n/a all, 0 panics all, 0 panics all, 0 panics
clwb emulated near a page end, unreadable page after (136 placements) 67 136 136 136
clippy -D warnings clean clean clean clean
  • Differential check: every clwb-shaped encoding (no REX or REX 0x40–0x4f, every ModRM, and every SIB where one is read), cut at every length from 0 to 15, plus 2M random slices. The expected answer is main's decoder on the zero-padded bytes, kept only if that length fits in the slice.
  • Placements: 8 encodings of 4 to 10 bytes, each ending 0 to 16 bytes before a PAGE_NOACCESS page, run through the real emulate_amd_only. main fails exactly when fewer than 15 bytes remain.

Differences: #941 is the only one that tests the handler end to end, with a guard-page test. Super-cod's branch also reformats six unrelated spots in crash.rs (log_crash, append_to_log, title_output, sse4a, thrown); CI doesn't check formatting, but it's noise and conflict risk next to the other open PRs on this file. #1330 bundles the fix into a 76-file PR. #941 looks like the one to merge.

Related, outside this issue, and still in all three: emulate_amd_only first reads 3 bytes at Rip (code.cast::<[u8; 3]>()). So a 1- or 2-byte illegal instruction in a page's last bytes, before an unreadable page, faults inside the handler. With ud2 (0f 0b) in the last 2 bytes, the process dies with a page fault at the next page's first byte, on main and on every candidate. Closed #1038 had addressed that read.

AI-assisted (Claude Code). Tests were synthetic, with no game files.

**@⁠winne42** on GitHub · 2026-11-24 19:53 UTC · [original](https://github.com/Marvisak/VIctory/issues/924#issuecomment-7142732055) I tested the three fixes side by side, as Windows builds (`x86_64-pc-windows-msvc`) run under Wine 10.0 on Linux: #941 (`60d2cf9`), Super-cod's branch (`958f998`), and the `crash.rs` part of #1330 (`fb40a8f`). All three fix the bug and decode the same way. | | `main` | #941 | Super-cod | #1330 | |---|---|---|---|---| | Own `crash::` tests | 13 pass | 14 pass | 13 pass | 13 pass | | Truncated slices matching `main`'s decoder (4,289,152) | n/a | all, 0 panics | all, 0 panics | all, 0 panics | | `clwb` emulated near a page end, unreadable page after (136 placements) | 67 | **136** | **136** | **136** | | clippy `-D warnings` | clean | clean | clean | clean | - **Differential check:** every `clwb`-shaped encoding (no REX or REX `0x40`–`0x4f`, every ModRM, and every SIB where one is read), cut at every length from 0 to 15, plus 2M random slices. The expected answer is `main`'s decoder on the zero-padded bytes, kept only if that length fits in the slice. - **Placements:** 8 encodings of 4 to 10 bytes, each ending 0 to 16 bytes before a `PAGE_NOACCESS` page, run through the real `emulate_amd_only`. `main` fails exactly when fewer than 15 bytes remain. **Differences:** #941 is the only one that tests the handler end to end, with a guard-page test. Super-cod's branch also reformats six unrelated spots in `crash.rs` (`log_crash`, `append_to_log`, `title_output`, `sse4a`, `thrown`); CI doesn't check formatting, but it's noise and conflict risk next to the other open PRs on this file. #1330 bundles the fix into a 76-file PR. **#941 looks like the one to merge.** **Related, outside this issue, and still in all three:** `emulate_amd_only` first reads 3 bytes at `Rip` (`code.cast::<[u8; 3]>()`). So a 1- or 2-byte illegal instruction in a page's last bytes, before an unreadable page, faults inside the handler. With `ud2` (`0f 0b`) in the last 2 bytes, the process dies with a page fault at the next page's first byte, on `main` and on every candidate. Closed #1038 had addressed that read. _AI-assisted (Claude Code). Tests were synthetic, with no game files._
This discussion has been locked. Commenting is limited to contributors.
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory#924
No description provided.