Skip to content

D3D12-LUA-MEMORY-SCAN-BOUNDS: a scan covers a bounded range, and its options are checked - #3043

Open
imShangLi wants to merge 4 commits into
ProjectLeonida:mainfrom
imShangLi:fix/d3d12-lua-memory-scan-bounds
Open

imShangLi wants to merge 4 commits into
ProjectLeonida:mainfrom
imShangLi:fix/d3d12-lua-memory-scan-bounds

Conversation

@imShangLi

@imShangLi imShangLi commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

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 the scan binding, 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 whole
64-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 is
not 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, the
F4 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 ReadProcessMemory call.

This change:

  • bounds the work: one scan covers at most 256 MiB (the whole image, when the image is bigger), so a scan cannot
    walk the address space;
  • checks the options: start, size and max are whole numbers. A negative one was silently turned into
    something else ({start = -1} scanned the whole image, {size = -1} scanned nothing) and is now refused with the
    reason, as is a value that is not a whole number;
  • fixes the default range: without a size, the range reaches the end of the image. It used to be
    size.saturating_sub(start.saturating_sub(base)), which ends 100 bytes short of the image's top when start is
    below the image, so a pattern there was silently missed.

docs/lua-modding.md says 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 = -1 used to mean "the image's base", and an
option 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 memory
    API, 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 own
    red/green tool: red then green, for both new tests. The red side is origin/main (8422def) with the new
    tests; they fail there and pass with the change.
  • cargo clippy -p leonida-lua --all-targets --locked - no new warning (the crate's two Default warnings in
    input.rs, and leonida-menu's three, are on main too).
  • python -I scripts/check_no_game_data.py on the four changed files - clean.
  • The d3d12 workflow (.github/workflows/d3d12.yml) runs cargo test --workspace --exclude xtask on
    windows-latest for a change under d3d12/, 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

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

…ptions are checked

The range of leonida.memory.scan was checked against the process, not against
the work it causes: a mod could ask for the whole user range (128 TiB) and the
scan's time was refunded to the call budget, so nothing stopped it. The one
thread that runs every mod then sat in the scan for hours, and the other mods,
the menu callbacks and the reload path waited for it.

- one scan covers at most 256 MiB (the whole image, when it is bigger)
- start, size and max are whole numbers: a negative one or another kind of
  value is refused with the reason, not taken for something else
- without a size, the range reaches the end of the image (it ended 100 bytes
  short when it started below the image)
- the test for the default range stands on its own, so it shows red without
  the fix (on the base the mod is disabled by the typed option before it
  gets there)
- the doc says what is really refused: a numeric string is a whole number
  and is taken
- the doc says that a string which names a whole number is taken as that
  number, so the sentence is true
- the test covers a negative size and a negative max too
@imShangLi imShangLi changed the title t D3D12-LUA-MEMORY-SCAN-BOUNDS: a scan covers a bounded range, and its options are checked Dec 2, 2026
@Kal0s-design

Copy link
Copy Markdown
Contributor

A native Windows run, since the description's run was on Fedora. Windows 11, MSVC toolchain 1.98.1. Head 915d4d5aa merged into current main 0ae5a2664: the merge is clean.

  • cargo test -p leonida-lua --locked: 42 passed, 0 failed.

  • Red/green, by hand. I put api.rs and memory.rs back to main and kept the new tests.rs. Both new tests fail, 40 passed and 2 failed:

    • a_scan_without_a_size_reaches_the_end_of_the_image gets ["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_checked gets ["far <table> nil", "neg <table>", "disabled (the other mods go on): error converting Lua string to i64 ..."] where it expects the five refusals. On main the 128 TiB range is accepted, start = -1 scans, and a string option raises and disables the mod.

    With the change both pass.

  • cargo clippy -p leonida-lua --all-targets --locked: no warning in api.rs or memory.rs.

Not checked: the running title, the same as in the description.

GPUs tested: n/a (CPU unit tests)

AI-assisted: yes (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants