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

Merged
imShangLi merged 4 commits from upstream-pr/3043 into main 2026-12-02 13:42:49 +00:00
imShangLi commented 2026-12-02 04:50:09 +00:00 (Migrated from github.com)

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 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 (8422def7) 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.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5600063387 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3043). Originally posted by [imShangLi](https://github.com/imShangLi) on 2026-12-02T04:50:09Z. --- Claimed in the coordination issue as: [D3D12-LUA-MEMORY-SCAN-BOUNDS](https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7334712501) (#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` (8422def7) 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 - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] 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.
Kal0s-design commented 2026-12-02 06:05:55 +00:00 (Migrated from github.com)

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 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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7336632202 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3043#issuecomment-7336632202). Originally posted by [Kal0s-design](https://github.com/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 `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).
imShangLi commented 2026-12-02 08:28:29 +00:00 (Migrated from github.com)

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-lua crate 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 main moves, the head is still 915d4d5a and the branch is fix/d3d12-lua-memory-scan-bounds (I merge main into it rather than rebase, as the reviewer notes ask).

GPUs tested: n/a (CPU unit tests)

AI-assisted: yes (MiniMax M3.1 Flash).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7339500395 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3043#issuecomment-7339500395). Originally posted by [imShangLi](https://github.com/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-lua` crate 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 `main` moves, the head is still `915d4d5a` and the branch is `fix/d3d12-lua-memory-scan-bounds` (I merge `main` into it rather than rebase, as the reviewer notes ask). GPUs tested: n/a (CPU unit tests) AI-assisted: yes (MiniMax M3.1 Flash).
MoonEyes commented 2026-12-02 13:16:05 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T13:16:05Z.


RUN (in game, grouped): this head 915d4d5aa was merged with git merge --no-ff, together with nine other rendering pull requests, onto main f6dd2242e (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.

  • At least 381 s in the world (time limit 330 s, then the run script stopped the game): no device loss, no unhandled exception, no hang or GPU stall in the log.
  • Rendering 14.7 frames/s median over the last 240 s (a single earlier run with the same launcher options on a different tree: 15.2).
  • Limits: one run, grouped, so a failure would not have named one pull request; the picture was not checked; free RAM was about 1.2 GB during the run.

Heads in this run: #2854 81a6e94a9, #2936 68215ad0b, #2939 561bbc19c, #2997 ef429f923, #3043 915d4d5aa, #3045 cb30a595a, #3046 0f19a0819, #3047 c33e5b48e, #3048 2ab0b4b9d, #3051 856615ce7.

AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7346607572 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3043#issuecomment-7346607572). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T13:16:05Z. --- RUN (in game, grouped): this head `915d4d5aa` was merged with `git merge --no-ff`, together with nine other rendering pull requests, onto `main` `f6dd2242e` (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. - At least 381 s in the world (time limit 330 s, then the run script stopped the game): no device loss, no unhandled exception, no hang or GPU stall in the log. - Rendering 14.7 frames/s median over the last 240 s (a single earlier run with the same launcher options on a different tree: 15.2). - Limits: one run, grouped, so a failure would not have named one pull request; the picture was not checked; free RAM was about 1.2 GB during the run. Heads in this run: #2854 81a6e94a9, #2936 68215ad0b, #2939 561bbc19c, #2997 ef429f923, #3043 915d4d5aa, #3045 cb30a595a, #3046 0f19a0819, #3047 c33e5b48e, #3048 2ab0b4b9d, #3051 856615ce7. AI-assisted (Claude Code, Claude Opus 5.5)
MoonEyes commented 2026-12-02 13:20:49 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T13:20:49Z.


Merging. Head 915d4d5aa merges cleanly with main bf7886746 and does not force a rebase of #2566. Checked on head 915d4d5aa: 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 main f6dd2242e (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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7346730442 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3043#issuecomment-7346730442). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T13:20:49Z. --- Merging. Head `915d4d5aa` merges cleanly with `main` `bf7886746` and does not force a rebase of #2566. Checked on head 915d4d5aa: 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 main f6dd2242e (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).
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
ProjectLeonida/mandy!3043
No description provided.