d3d12-loader: console_insn range checks don't overflow at the top of memory (D3D12-LOADER-CONSOLE-INSN-CHECKED-RANGES) #2858
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2858"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-01T10:04:09Z.
Claimed in the coordination issue as: D3D12-LOADER-CONSOLE-INSN-CHECKED-RANGES (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7278217071)
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
Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-12-01T10:09:50Z.
Verified on Windows: the crate is green with it, and it adds one test - measured against
main's own file.main3393ceec9, cherry-picked (one commit),cargo 1.98.1: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.
Imported from ubiquitarianism/leonida. Originally posted by Neutron-0 on 2026-12-01T10:33:24Z.
Verified on Windows 11, and the control bites: the new test panics on today's
mainand passes with this.main7fbcd456e, cargo 1.98.1: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:
readable(usize::MAX - 4, 16)overflows inva + bytesonmain, before the query is even made.Two notes for the record, both in the pull request's favour:
endwithchecked_addbefore theVirtualQuery, so a range past the top of the address space never asks the kernel about an address it cannot describe; and the region end is checked withchecked_addas well, which is the other overflow named here.No game files and no GPU were needed for this check: the tests are Windows unit tests and the title was not run.
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-01T14:53:04Z.
Verified by MoonEyes (another person than the author) on head
3cd5de60bmerged withmaincd0c52fcf: I read the whole diff.cargo test -p leonida-loader: 88 passed, 0 failed. Withchecked_addput back to a plainva + bytes,ranges_past_the_top_of_the_address_space_do_not_overflowfails.In-game run with the six others of a batch on
main21e6bbf4b: RTX 4060 8 GB, NVIDIA 610.88, Windows 11 26300,-SeriesS -FrameGen 2, about 860 s in the world, then a second one of about 440 s, 0 device lost, 0 panic; pictures of the same scene compared withmain, same. Black surfaces in early cutscenes while the PC's memory is full are there on plainmaintoo.AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by imShangLi on 2026-12-01T15:18:08Z.
VERIFY-2858 — Linux, Rust 1.98.1 (the pinned toolchain), no GPU device, no game files
Verdict: PASS. The fix is the right one, and the pull request's new test is a real red on
main: with the overflow checks thatcargo testturns on,main's code panics withattempt to add with overflowwhere the new code answersfalse.Head
3cd5de60, base the currentmain88a9f133, merge base3393ceec, 1 commit, 1 file, +32/−2:d3d12/crates/leonida-loader/src/console_insn.rs. It merges onto today'smainwithout a conflict.1. What it changes
Two range checks in the module that emulates the console-only instructions:
vais an address taken from the title's own register (ctx.Rip,ctx.Rax), so it can be any 64-bit value;va + bytescan therefore run off the top of the address space.2. The pull request's reasoning checks out
readableandwritableare called from exactly two places, and both are on the exception-dispatch path:and
emulateis:So a panic raised there unwinds out of the vectored exception handler, which is the "takes the process down" the comment describes.
ripisctx.Rip as usize— the title's own instruction pointer.The
clzeropath is already safe and the pull request leaves it alone:first.checked_add(passes * 64)atconsole_insn.rs:467, withpassesbounded byMOST = (2 << 30) / 64.3. How I checked it
leonida-loaderdoes not build on Linux —console_insnis#[cfg(windows)](lib.rs:24-25) and callsVirtualQuery,VirtualProtectandGetCurrentProcessorNumber. So I copiedreadable,writableandwritable_toout of the file verbatim, for bothmainand the pull request, into a standalone program (tools/rust/2858_harness.rs) with a mockVirtualQuery, and ran the pull request's two assertions against both.The mock has two behaviours, because the answer depends on it:
VirtualQueryfails. What real Windows does for a user-mode address that high: the address is not mapped, the query fails.VirtualQueryalways answers. A large committed, writable region, to see whether a wrapped end can let the check through.4. Red and green
Built with the overflow checks on, which is what
cargo test's test profile does (overflow-checksfollowsdebug-assertions):main's codecargo test)attempt to add with overflowfalse— PASScargo test)attempt to add with overflowfalse— PASSfalsefalsetrue— the wrapped end lets the check passfalseThe two assertions are the pull request's own:
On
mainthey do not merely fail, they panic:usize::MAX - 4 + 16andusize::MAX & !63 + 64both overflow, and inreadablethe overflow is only reached whenVirtualQueryanswers (the&&short-circuits before it otherwise), while inwritabletheva + bytesis an argument and is always evaluated. The newchecked_addturns both into a plainfalse.5. How much of this is observable in a shipped build
Worth being precise, because the release row above is the one a player would meet:
main's code panics inside the exception handler. That is the real defect, and the fix removes it.VirtualQueryfails anyway (row A), somainwould have answeredfalsetoo. The "a wrapped end let the range check pass" needsVirtualQueryto answer (row B), which on Windows needs a mapping at such an address — a user-mode address cannot have one, so that row is a hardening rather than an observable difference in a release build.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
TaynanGT/Vice-Tay@c2f2b5bis public: "Use checked addition in console_insn and write_sites bounds checks", 2026-11-29, touchingcrates/victory-loader/src/console_insn.rs(+28/−2) andcrates/victory-loader/src/write_sites.rs(+12/−1) — the same change, in the related project.I looked for the same class of unchecked addition in leonida's
write_sites.rs, the other file that fix touched: its arithmetic is on slice indices (code.get(at + 1)?, bounded byget) and on offsets parsed from our own list file (usize::from_str_radix(offset.trim_start_matches("0x"), 16)atwrite_sites.rs:238), not from the title's registers — a different threat model, so no counterpart to fix here.7. A small asymmetry, not a defect
readablenow guardsm.BaseAddress + m.RegionSizewithchecked_add, butwritable_tostill computes the same sum unguarded (console_insn.rs:346). It cannot overflow in practice —BaseAddressis a real user-mode region base andRegionSizeis that region's size — so this is symmetry rather than a bug, and I would not ask for a change.writable_to's other caller, theclzeroloop, is already guarded.8. What I did not check
The third assertion of the new test, which builds a
CONTEXT, pointsRaxatu64::MAXand callsemulate:It needs a real
CONTEXTand the emulator's instruction decoder, so it only runs on Windows. I did not run it. What I checked instead is that the code it exercises reachesreadable/writablewithvataken fromctx.Rax(console_insn.rs:369,:437), which is the case the fix covers.Windows. Everything above is Linux (Fedora 45, x86_64) with the pinned toolchain (
rustc 1.98.1). The mockVirtualQueryis a stand-in, not Windows' own; §3 and §5 say exactly what it does and does not settle.The rest of
leonida-loader's tests — the crate does not build here, so I ran neither the pull request's test in the crate nor the crate's other tests.The
CODE/WRITABLEcaches (thread_local!inconsole_insn.rs:35-41): my harness models them, but the remembered-region path is not what these two assertions exercise — both inputs are far outside any remembered region — so I did not measure it.The emulated instruction set (
console_insn_decode.rs) andnear()/jump(): untouched by this pull request and not examined beyond the two call sites above.AI-assisted: MiniMax M3.1 Flash.
Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-12-01T17:40:42Z.
Review: COMMENTED
The
writablehalf is right and pinned: reverting it towritable_to(va, va + bytes) == Some(va + bytes)panics in the new test atconsole_insn.rs:319:21, andcargo test -p leonida-loader --libis 85/85 here. WithRax = u64::MAX,line = MAX-63and+64overflows on the VEH path (crash.rs:469-477), so this is a real debug panic fixed.The
readablehalf is not pinned. Restoringreadableverbatim to main's one-liner still passes the new test, becauseVirtualQueryshort-circuits beforeva + bytes— I probed Windows directly:VirtualQuery(0xfffffffffffffffb) = 0,(0xffffffffffffffc0) = 0, a stack address = 48. Soassert!(!readable(usize::MAX - 4, 16))is already green on main, and the body's "it panics atconsole_insn.rs:313" is pointing at thewritableline. Either pinreadablethrough 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.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 needsVirtualQueryto succeed at0xffff…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.rsis byte-identical between the merge base and main, and no other open PR touches it.Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-12-01T22:35:28Z.
Merging (added to the merge queue). Head
3cd5de60bmerges cleanly with mainc4ca57615. Claim: D3D12-LOADER-CONSOLE-INSN-CHECKED-RANGES (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7278217071). 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 mainc4ca57615: 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.