D3D12-LUA-STRING-DUMP: a mod cannot make a precompiled chunk #3047

Merged
imShangLi merged 1 commit from upstream-pr/3047 into main 2026-12-02 13:51:54 +00:00
imShangLi commented 2026-12-02 05:05:07 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by imShangLi on 2026-12-02T05:05:07Z.


Claimed in the coordination issue as: D3D12-LUA-STRING-DUMP

What and why

docs/lua-modding.md lists string.dump under "Not there", and says load takes text chunks only "because a
precompiled chunk can take the interpreter down". The mod's own copy of the string library does not have dump -
but the state has one string metatable, and it points at the real global string table. Faking getmetatable
for strings (sandbox.rs) hid the metatable from the mods, but string indexing still resolves through it, so:

("x").dump          --> the real string.dump

Measured in the crate's own sandbox: type(string.dump) is nil, type(("x").dump) is function, and
pcall(("x").dump, function() return 42 end) returns a 105-byte binary chunk that starts with the 0x1B signature.
A mod can therefore make a precompiled chunk, which the sandbox is built to keep away from it. Today it cannot run
one back (load refuses it: "attempt to load a binary chunk (mode is 't')", and require loads text only), so there
is no escape from it yet - but the guarantee the documentation states does not hold, and any later change that loads
a chunk in another way would turn it into the interpreter-killer the sandbox is meant to avoid.

The change: the state's string metatable points at a table without dump instead of at the real string table, so
the real one is out of reach as well. It is set once, in sandbox::new_lua, because all the mods share one Lua
state.

Tested

  • cargo test -p leonida-lua --locked - 40 tests, 0 failed. Fedora 45, Rust 1.98.1, no title, no game files.
  • python -I scripts/red_green.py fix/d3d12-lua-string-dump --crate leonida-lua - the repository's own red/green
    tool: red then green. The red side is origin/main (8422def7) with the changed test; on the base the mod finds
    ("x").dump and the "what is forbidden is gone" test fails.
  • cargo clippy -p leonida-lua --all-targets --locked - no new warning (the crate's two Default warnings in
    input.rs are on main too).
  • python -I scripts/check_no_game_data.py on the two 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 changed 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 test); an independent read-only reviewer agent audited the
diff and the edge cases.

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:5600220275 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3047). Originally posted by [imShangLi](https://github.com/imShangLi) on 2026-12-02T05:05:07Z. --- Claimed in the coordination issue as: [D3D12-LUA-STRING-DUMP](https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7334838643) ### What and why `docs/lua-modding.md` lists `string.dump` under "Not there", and says `load` takes text chunks only "because a precompiled chunk can take the interpreter down". The mod's own copy of the `string` library does not have `dump` - but the state has **one** string metatable, and it points at the **real** global `string` table. Faking `getmetatable` for strings (`sandbox.rs`) hid the metatable from the mods, but string *indexing* still resolves through it, so: ```lua ("x").dump --> the real string.dump ``` Measured in the crate's own sandbox: `type(string.dump)` is `nil`, `type(("x").dump)` is `function`, and `pcall(("x").dump, function() return 42 end)` returns a 105-byte binary chunk that starts with the `0x1B` signature. A mod can therefore make a precompiled chunk, which the sandbox is built to keep away from it. Today it cannot run one back (`load` refuses it: "attempt to load a binary chunk (mode is 't')", and `require` loads text only), so there is no escape from it yet - but the guarantee the documentation states does not hold, and any later change that loads a chunk in another way would turn it into the interpreter-killer the sandbox is meant to avoid. The change: the state's string metatable points at a table without `dump` instead of at the real `string` table, so the real one is out of reach as well. It is set once, in `sandbox::new_lua`, because all the mods share one Lua state. ### Tested - `cargo test -p leonida-lua --locked` - 40 tests, 0 failed. Fedora 45, Rust 1.98.1, no title, no game files. - `python -I scripts/red_green.py fix/d3d12-lua-string-dump --crate leonida-lua` - the repository's own red/green tool: **red then green**. The red side is `origin/main` (8422def7) with the changed test; on the base the mod finds `("x").dump` and the "what is forbidden is gone" test fails. - `cargo clippy -p leonida-lua --all-targets --locked` - no new warning (the crate's two `Default` warnings in `input.rs` are on `main` too). - `python -I scripts/check_no_game_data.py` on the two 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 changed 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 test); an independent read-only reviewer agent audited the diff and the edge cases. ### 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:17:39 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-02T06:17:39Z.


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

  • cargo test -p leonida-lua --locked: 40 passed, 0 failed.
  • Red/green. With only sandbox.rs put back to main, what_is_forbidden_is_gone_and_what_is_safe_is_there fails with left: "gone:string.dump through a string". With the change it passes.
  • A scratch test, removed afterwards, with two mods in the shared state:
    • ("x").dump and string.dump are both nil. Calling ("x").dump(function() end) fails with "attempt to call a nil value (field 'dump')".
    • The usual string methods still answer through the new metatable: upper, format, rep, gsub, sub, byte give X 3-z abab heLLo bc 120.
    • Mod a assigns string.foo in its own copy, and mod b doesn't see it as ("x").foo, which stays nil. The public table the metatable now points at isn't reachable from a mod's string.
  • cargo clippy -p leonida-lua --all-targets --locked: no warning.

Not checked: the running title.

GPUs tested: n/a (CPU unit tests)

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7336851403 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3047#issuecomment-7336851403). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-12-02T06:17:39Z. --- A native Windows run, since the description's run was on Fedora. Windows 11, MSVC toolchain 1.98.1. Head `c33e5b48e` merged into current `main` `0ae5a2664`: the merge is clean. - `cargo test -p leonida-lua --locked`: 40 passed, 0 failed. - **Red/green.** With only `sandbox.rs` put back to `main`, `what_is_forbidden_is_gone_and_what_is_safe_is_there` fails with `left: "gone:string.dump through a string"`. With the change it passes. - A scratch test, removed afterwards, with two mods in the shared state: - `("x").dump` and `string.dump` are both `nil`. Calling `("x").dump(function() end)` fails with "attempt to call a nil value (field 'dump')". - The usual string methods still answer through the new metatable: `upper`, `format`, `rep`, `gsub`, `sub`, `byte` give `X 3-z abab heLLo bc 120`. - Mod `a` assigns `string.foo` in its own copy, and mod `b` doesn't see it as `("x").foo`, which stays `nil`. The public table the metatable now points at isn't reachable from a mod's `string`. - `cargo clippy -p leonida-lua --all-targets --locked`: no warning. Not checked: the running title. GPUs tested: n/a (CPU unit tests) AI-assisted: yes (Claude Code).
imShangLi commented 2026-12-02 08:28:30 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by imShangLi on 2026-12-02T08:28:30Z.


Thank you for the Windows run and for the scratch test with two mods in the shared state — the per-mod copy of the string table (mod a assigning string.foo and mod b not seeing it) is the part I wanted checked, since the fix replaces the environment's metatable with a table of its own.

Your red side confirms the new case fails without the change (gone:string.dump through a string) and that the string methods still answer through the new metatable.

Nothing to change on my side from your run. The head is still c33e5b4 and the branch is fix/d3d12-lua-string-dump.

GPUs tested: n/a (CPU unit tests)

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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7339501094 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3047#issuecomment-7339501094). Originally posted by [imShangLi](https://github.com/imShangLi) on 2026-12-02T08:28:30Z. --- Thank you for the Windows run and for the scratch test with two mods in the shared state — the per-mod copy of the `string` table (mod `a` assigning `string.foo` and mod `b` not seeing it) is the part I wanted checked, since the fix replaces the environment's metatable with a table of its own. Your red side confirms the new case fails without the change (`gone:string.dump through a string`) and that the string methods still answer through the new metatable. Nothing to change on my side from your run. The head is still `c33e5b4` and the branch is `fix/d3d12-lua-string-dump`. GPUs tested: n/a (CPU unit tests) AI-assisted: yes (MiniMax M3.1 Flash).
MoonEyes commented 2026-12-02 13:16:11 +00:00 (Migrated from github.com)

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


RUN (in game, grouped): this head c33e5b48e 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:7346610033 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3047#issuecomment-7346610033). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T13:16:11Z. --- RUN (in game, grouped): this head `c33e5b48e` 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:21:45 +00:00 (Migrated from github.com)

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


Merging. Head c33e5b48e merges cleanly with main bf7886746 and does not force a rebase of #2566. Checked on head c33e5b48e: 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:7346754955 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3047#issuecomment-7346754955). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T13:21:45Z. --- Merging. Head `c33e5b48e` merges cleanly with `main` `bf7886746` and does not force a rebase of #2566. Checked on head c33e5b48e: 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!3047
No description provided.