Repository navigation
d3d12-loader: console_insn range checks don't overflow at the top of memory (D3D12-LOADER-CONSOLE-INSN-CHECKED-RANGES) - #2858
Conversation
…memory readable() and writable() computed va + bytes (and BaseAddress + RegionSize) unchecked. They run inside the vectored exception handler, where a clzero with RAX near the top of the address space made the debug build panic with an overflow (taking the process down) and let a wrapped end pass the range check in release. A range that runs past the top is now neither readable nor writable, and such a clzero is left to fault as it would. After ahmetbagci's fix in TaynanGT/Vice-Tay (c2f2b5b); its write_sites half is already in ProjectLeonida#2664. Co-Authored-By: ahmetbagci <6625856+ahmetbagci@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Verified on Windows: the crate is green with it, and it adds one test - measured against
So the overflow check is tied to its test and the other 84 did not move. One thing worth saying about the shape: a range check that does not overflow is exactly the kind of change that a test can pin down and a reviewer cannot eyeball - the arithmetic either wraps for a given input or it does not, and the input has to be constructed. The test that comes with it is the only evidence there is that the boundary is where the comment says. What I could not test: a real console instruction stream hitting that boundary - the unit tests are what I ran. AI-assisted: yes - DeepSeek v4.1 (flash), both arms on the machine above. Written with an AI assistant. |
|
Verified on Windows 11, and the control bites: the new test panics on today's
The control was this pull request's own test with the fix reverted, so the panic is the fix's doing and not the test's: Two notes for the record, both in the pull request's favour:
No game files and no GPU were needed for this check: the tests are Windows unit tests and the title was not run. |
|
Verified by MoonEyes (another person than the author) on head In-game run with the six others of a batch on AI-assisted (Claude Code). |
VERIFY-2858 — Linux, Rust 1.98.1 (the pinned toolchain), no GPU device, no game filesVerdict: PASS. The fix is the right one, and the pull request's new test is a real red on Head 1. What it changesTwo range checks in the module that emulates the console-only instructions: // before
&& m.BaseAddress as usize + m.RegionSize >= va + bytes
// after
&& (m.BaseAddress as usize).checked_add(m.RegionSize).is_some_and(|region_end| region_end >= end)
fn writable(va: usize, bytes: usize) -> bool {
let Some(end) = va.checked_add(bytes) else { return false };
writable_to(va, end) == Some(end)
}
2. The pull request's reasoning checks out
and
So a panic raised there unwinds out of the vectored exception handler, which is the "takes the process down" the comment describes. The 3. How I checked it
The mock has two behaviours, because the answer depends on it:
4. Red and greenBuilt with the overflow checks on, which is what
The two assertions are the pull request's own: assert!(!readable(usize::MAX - 4, 16));
assert!(!writable(usize::MAX & !63, 64));On 5. How much of this is observable in a shipped buildWorth being precise, because the release row above is the one a player would meet:
Either way the change is right: it removes the panic, and it closes the wrap. 6. The credit in the test's comment is checkable
I looked for the same class of unchecked addition in leonida's 7. A small asymmetry, not a defect
8. What I did not check
AI-assisted: MiniMax M3.1 Flash. |
KelboLive
left a comment
There was a problem hiding this comment.
The writable half is right and pinned: reverting it to writable_to(va, va + bytes) == Some(va + bytes) panics in the new test at console_insn.rs:319:21, and cargo test -p leonida-loader --lib is 85/85 here. With Rax = u64::MAX, line = MAX-63 and +64 overflows on the VEH path (crash.rs:469-477), so this is a real debug panic fixed.
The readable half is not pinned. Restoring readable verbatim to main's one-liner still passes the new test, because VirtualQuery short-circuits before va + bytes — I probed Windows directly: VirtualQuery(0xfffffffffffffffb) = 0, (0xffffffffffffffc0) = 0, a stack address = 48. So assert!(!readable(usize::MAX - 4, 16)) is already green on main, and the body's "it panics at console_insn.rs:313" is pointing at the writable line. Either pin readable through a pure range helper, or say in the body that it is hardening.
Two minor things:
writable_tostill computesm.BaseAddress as usize + m.RegionSizeunchecked (:350), which is the same expression this PR now guards inreadable— inconsistent with the "nothing in the handler may panic" rationale, though it cannot overflow for a real region.- "its
write_sites.rshalf is already inmainthrough #2664" is not true today: #2664 is open, and its head is not an ancestor of main; main'swrite_sites.rshas no suchchecked_add.
Also worth noting: the "in release the end wrapped and could let the check pass" line is true arithmetic but not observable — writable_to(va, 0) still needs VirtualQuery to succeed at 0xffff…ffc0, which Windows refuses. That is fine as hardening; it just is not a release bug.
Merge is clean; 368 commits behind, but console_insn.rs is byte-identical between the merge base and main, and no other open PR touches it.
|
Merging (added to the merge queue). Head 3cd5de6 merges cleanly with main c4ca576. Claim: D3D12-LOADER-CONSOLE-INSN-CHECKED-RANGES (#2570 (comment)). Red/green: imShangLi (Linux, PASS: main panics with an add overflow, the head passes), KelboLive review on this head (reverting the writable half panics in the new test). I ran: #2858, #2882, #2788, #2876 and #2884 merged together onto main c4ca576: cargo tests leonida-shader 420 passed / 0 failed, leonida-loader 85/0, leonida-gpu 644/0. In-game run: RTX 4070 Laptop 8 GB, NVIDIA 621.40, -SeriesS -NoDlss -NoFrameGen, a cold start to the world (135 s) then a 330 s warm walking run: median 35.4 ms (28.2 fps), private memory max 24.4 GB, 0 device lost. CI: green on head and main; the queue runs the full CI on the group before merging. AI-assisted: Claude Code (oreofr-agent), merge approved in writing by OreoFR. |
Claimed in the coordination issue as: D3D12-LOADER-CONSOLE-INSN-CHECKED-RANGES (#2570 (comment))
What and why
readable()andwritable()ind3d12/crates/leonida-loader/src/console_insn.rscomputedva + bytesandBaseAddress + RegionSizeunchecked. They run inside the vectored exception handler. Aclzerowith RAX near the top of the address space made the debug build panic with an overflow there, which takes the process down. In release the end wrapped and could let the range check pass.The change: both use
checked_addand answer false on overflow, so a range that runs past the top is neither readable nor writable, and such aclzerois left to fault as it would.This ports ahmetbagci's fix from the sibling VIctory tree (TaynanGT/Vice-Tay
c2f2b5b, GPL-3.0), credited as co-author. Itswrite_sites.rshalf is already inmainthrough #2664.Tested
console_insn::tests::ranges_past_the_top_of_the_address_space_do_not_overflow:readable(usize::MAX - 4, 16)andwritable(usize::MAX & !63, 64)are false, andemulateon aclzerowith RAX =u64::MAXreturns 0. Onmain(3393ceec9) it panics (attempt to add with overflowatconsole_insn.rs:313); with the change it passes.cargo test -p leonida-loader --lib: 85 passed, 0 failed (main: 84).cargo clippy -p leonida-loader --lib --tests: no warning in the changed lines.GPUs tested: n/a (CPU unit tests)
AI-assisted: yes
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a: no translator change)🤖 Generated with Claude Code