D3D12-LUA-MEMORY-SCAN-BOUNDS: a scan covers a bounded range, and its options are checked #3043
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3043"
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 imShangLi on 2026-12-02T04:50:09Z.
Claimed in the coordination issue as: D3D12-LUA-MEMORY-SCAN-BOUNDS
(#2683 is open on the same crate, on
write_file; this one is on thescanbinding, they do not overlap.)What and why
leonida.memory.scan(the Lua modding API,d3d12/crates/leonida-lua) let a mod ask for a range covering the whole64-bit user address space:
{start = 0x10000, size = 0x7FFFFFFF0000}- 128 TiB less 64 KiB - is accepted today,because the range is checked against the process (
LOWEST/HIGHEST) and never against the work it causes.The time a scan takes is refunded to the mod's call budget (
guard.pause(began.elapsed()), so that "a long scan isnot the mod's own running time"), and the instruction hook cannot see inside a native call, so nothing stopped it: the
one thread that runs every mod (
crates/leonida-lua/src/lib.rs,rt.pass) sat in the scan, and the other mods, theF4 menu callbacks and the "Reload Lua mods" path waited for it. Reading 128 TiB of address space is 34 billion page
reads; on the running title every page read is a
ReadProcessMemorycall.This change:
walk the address space;
start,sizeandmaxare whole numbers. A negative one was silently turned intosomething else (
{start = -1}scanned the whole image,{size = -1}scanned nothing) and is now refused with thereason, as is a value that is not a whole number;
size, the range reaches the end of the image. It used to besize.saturating_sub(start.saturating_sub(base)), which ends 100 bytes short of the image's top whenstartisbelow the image, so a pattern there was silently missed.
docs/lua-modding.mdsays the same.Not in this pull request: the range may still be outside the title's image (only its size is bounded), and a scan's
time is still refunded to the mod's budget - the budget is about the mod's Lua, not about the runtime's work. So a mod
that scans back to back still runs for as long as it likes, in 256 MiB steps; stopping that would mean checking the
budget inside the scan, which is a bigger change than this one.
Two behaviours change for a mod that asks for something odd:
start = -1used to mean "the image's base", and anoption that was not a number used to raise (which disables that mod). Both are now a refusal with the reason, and the
mod stays alive.
Tested
cargo test -p leonida-lua --locked- 42 tests, 0 failed (the crate's own: the sandbox, the events, the memoryAPI, the files, the menu). Fedora 45, Rust 1.98.1, no title, no game files.
python -I scripts/red_green.py fix/d3d12-lua-memory-scan-bounds --crate leonida-lua- the repository's ownred/green tool: red then green, for both new tests. The red side is
origin/main(8422def7) with the newtests; they fail there and pass with the change.
cargo clippy -p leonida-lua --all-targets --locked- no new warning (the crate's twoDefaultwarnings ininput.rs, andleonida-menu's three, are onmaintoo).python -I scripts/check_no_game_data.pyon the four changed files - clean.d3d12workflow (.github/workflows/d3d12.yml) runscargo test --workspace --exclude xtaskonwindows-latestfor a change underd3d12/, so the new test runs there too.What I did not check: this has not run in the running title (the Lua API has never been run there -
docs/lua-modding.md,"Not tried yet"), and it touches no GPU code, so no GPU was involved.
GPUs tested: n/a (no GPU code; the crate's tests run on the CPU)
AI-assisted: yes - MiniMax M3.1 Flash (the change and the tests); two independent read-only reviewer agents
(SenseNova 6.8 Flash, SenseNova 6.8 Flash Lite) audited the diff, the edge cases and the red/green power of the test.
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed.Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-02T06:05:55Z.
A native Windows run, since the description's run was on Fedora. Windows 11, MSVC toolchain 1.98.1. Head
915d4d5aamerged into currentmain0ae5a2664: the merge is clean.cargo test -p leonida-lua --locked: 42 passed, 0 failed.Red/green, by hand. I put
api.rsandmemory.rsback tomainand kept the newtests.rs. Both new tests fail, 40 passed and 2 failed:a_scan_without_a_size_reaches_the_end_of_the_imagegets["tail 1 0"]where it expects["tail 1 1"]. That is the pattern near the image's top that the old default range missed.a_scan_covers_a_bounded_range_and_its_options_are_checkedgets["far <table> nil", "neg <table>", "disabled (the other mods go on): error converting Lua string to i64 ..."]where it expects the five refusals. Onmainthe 128 TiB range is accepted,start = -1scans, and a string option raises and disables the mod.With the change both pass.
cargo clippy -p leonida-lua --all-targets --locked: no warning inapi.rsormemory.rs.Not checked: the running title, the same as in the description.
GPUs tested: n/a (CPU unit tests)
AI-assisted: yes (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by imShangLi on 2026-12-02T08:28:29Z.
Thank you for the Windows run and for doing the red/green by hand — that is the part I could not do here (no Windows, and the
leonida-luacrate does not build for Linux).Your red side confirms the two new cases fail without the change, and the messages match what the description says: the scan without a size stops at the image's end, and the bounded range is refused for its options instead of scanning 128 TiB.
Nothing to change on my side from your run. If you want to re-check it after
mainmoves, the head is still915d4d5aand the branch isfix/d3d12-lua-memory-scan-bounds(I mergemaininto it rather than rebase, as the reviewer notes ask).GPUs tested: n/a (CPU unit tests)
AI-assisted: yes (MiniMax M3.1 Flash).
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T13:16:05Z.
RUN (in game, grouped): this head
915d4d5aawas merged withgit merge --no-ff, together with nine other rendering pull requests, ontomainf6dd2242e(no conflicts), and the d3d12 layer was rebuilt from that tree (diag profile, with DLSS). RTX 4060 8 GB, driver 610.88, Windows 11;scripts/play-d3d12.ps1 -SeriesS -Resolution 1080 -FrameGen 2, low-memory profile, keyboard-driven entry into the world and a walking route.Heads in this run: #2854
81a6e94a9, #293668215ad0b, #2939561bbc19c, #2997ef429f923, #3043915d4d5aa, #3045cb30a595a, #30460f19a0819, #3047c33e5b48e, #30482ab0b4b9d, #3051856615ce7.AI-assisted (Claude Code, Claude Opus 5.5)
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T13:20:49Z.
Merging. Head
915d4d5aamerges cleanly withmainbf7886746and does not force a rebase of #2566. Checked on head915d4d5aa: all 6 CI checks green; third-party red/green by Kal0s-design on this head; an in-game run of at least 381 s with this head merged onto mainf6dd2242e(grouped with nine other rendering pull requests, comment above) showed no device loss, no unhandled exception and no hang; item claimed in the coordination issue; author has earlier merged pull requests; conflict-free merge against main. Merged by MoonEyes.AI-assisted (Claude Code).