fix(check): a damaged .nup header is refused in 176 bytes, and the check's memory is bounded #252

Open
dazemc wants to merge 2 commits from dazemc/LongshotLauncher:fix/nup-toc-memory into main
Contributor

The Linux launcher sat on "Checking…" for a .nup and took the PC's memory: Longshot#1525 saw 59.1 GiB of 60.4 GiB in
use. The check starts Longshot's tools, and nx2/nup.py (Nup.__init__, :143-159) trusts a damaged table of contents
before checking it — meta = f.read(0x80 + strings offset + strings size) reads up to the whole file, then
self.strings = meta[...] copies the names again. The launcher had no bound of its own either: it kept every line the
check wrote, and put no limit on the check's process tree. This is the launcher's half of #1525; nx2/nup.py itself is
@dyst's and is not touched here.

What changed

  • Core/NupIndex.cs: a .nup's header and the header of its table of contents are read first — 0xB0 = 176 bytes,
    whatever the file says it is — and a table that cannot fit the file, or is far larger than a game dump's (256 MiB TOC,
    64 MiB names, 2.5 M files), is refused before any tool starts, with the plain "damaged" or "incomplete" words. The
    wizard's game-file check asks this before it runs make_game.py --check.
  • MakeGameRun.Bounded: on Linux the check runs under an inherited RLIMIT_AS — half the PC's memory, 1 to 2 GiB —
    set with bash's ulimit -v before the tools exec, so make_game.py and the nx2/nup.py it starts inherit it and no
    .nup shape (a zstd frame's window included) can take more from the PC.
  • CheckAsync keeps the check's last 512 lines instead of every line.
  • A program that is not on the PC is not put behind the shell: started through it, a missing tool ran and exited 127, and
    the file was blamed instead of this PC. tests/launcher_core's "Tools that do not start: this PC's, not the file's"
    caught it; the wrap's own check now puts its program on the disk and a new check holds a missing program unwrapped.

Evidence (this PC, no game launch, no game data)

The launcher's own preflight on the real 25,622,267,860-byte stable dump and on three damaged headers, through a counting
stream:

file bytes the launcher read verdict
the real 25.6 GB dump 176 accepted, 1.8 ms
25.6 GB, names claiming 25 GB 176 refused: "its names are 25000000000 bytes, more than a game dump's", 0.02 ms
64 MiB, damaged TOC 176 refused: "its names start at 16, outside its table of contents"
64 MiB, 2^40 folders 176 refused: "it claims 1099511627776 folders, more than a game dump has"

The old path, on the same files:

  • the real dump: 258.5 MiB peak, parses (36.8 MB TOC, 2 titles, 1,199 dirs, 372,587 files) — constant in the 25.6 GB
    of payload, which is what the 1–2 GiB limit and the 256 MiB TOC cap are sized from;
  • a 64 MiB header of the report's shape: 148.3 MiB peak = 2.32× the file;
  • a 25.6 GB sparse file with the report's shape (names claiming 25 GB): the read plus the names copy is 50.0 GB = 46.6 GiB
    before the records are built, MemoryError at nx2/nup.py:143 under ulimit -v 2 GiB (the limit this branch now
    sets), and OOM-killed under a MemoryMax=2G scope. On the reporter's PC, with a header whose offsets land near the
    end of the file, the measured 2.3× is the 59.1 GiB they saw.

The sparse file is a header and a hole: no game data is in it, and nothing leaves this PC.

Tests

  • tests/launcher_core (built from Core/ at b372d00, run the way tests/test_launcher_core.py runs it):
    2704 checks passed, 0 warnings, 0 errors — including the new NupIndexChecks: a sound dump accepted from 176 bytes,
    a 1 TiB header with names past its TOC refused from 176 bytes, every count and offset bound refused, and the wizard
    reading NupIndex.Why before it starts the tools.
  • Tooling's tests/test_release.py: 42 tests, OK.
  • python -I tests/test_launcher_core.py still stops at its translation gate: 48 "left over" catalog texts, and the
    same 48 with the same texts on main (6887d57) — the state @dyst's open #246 fixes. This branch changes no catalog
    text and no launcher string the catalogs hold; it is red on main and here alike.

Ready for review

  • One topic, a branch named for it (fix/nup-toc-memory), Conventional Commits.
  • The tests read the source and pass (above), with the one pre-existing red named.
  • No game files, keys or anything made from them in the branch, the logs or this description.
  • Two platforms kept: NupIndex reads a stream, Windows and Linux alike; the limit is Linux's, where the report is.
  • nx2/nup.py left to its owner (#1525).
The Linux launcher sat on "Checking…" for a `.nup` and took the PC's memory: Longshot#1525 saw 59.1 GiB of 60.4 GiB in use. The check starts Longshot's tools, and `nx2/nup.py` (`Nup.__init__`, `:143-159`) trusts a damaged table of contents before checking it — `meta = f.read(0x80 + strings offset + strings size)` reads up to the whole file, then `self.strings = meta[...]` copies the names again. The launcher had no bound of its own either: it kept every line the check wrote, and put no limit on the check's process tree. This is the launcher's half of #1525; `nx2/nup.py` itself is @dyst's and is not touched here. ## What changed - **`Core/NupIndex.cs`**: a `.nup`'s header and the header of its table of contents are read first — **0xB0 = 176 bytes**, whatever the file says it is — and a table that cannot fit the file, or is far larger than a game dump's (256 MiB TOC, 64 MiB names, 2.5 M files), is refused before any tool starts, with the plain "damaged" or "incomplete" words. The wizard's game-file check asks this before it runs `make_game.py --check`. - **`MakeGameRun.Bounded`**: on Linux the check runs under an inherited `RLIMIT_AS` — half the PC's memory, 1 to 2 GiB — set with bash's `ulimit -v` before the tools `exec`, so `make_game.py` and the `nx2/nup.py` it starts inherit it and no `.nup` shape (a zstd frame's window included) can take more from the PC. - **`CheckAsync`** keeps the check's last 512 lines instead of every line. - A program that is not on the PC is not put behind the shell: started through it, a missing tool ran and exited 127, and the file was blamed instead of this PC. `tests/launcher_core`'s "Tools that do not start: this PC's, not the file's" caught it; the wrap's own check now puts its program on the disk and a new check holds a missing program unwrapped. ## Evidence (this PC, no game launch, no game data) The launcher's own preflight on the real 25,622,267,860-byte stable dump and on three damaged headers, through a counting stream: | file | bytes the launcher read | verdict | |---|---|---| | the real 25.6 GB dump | **176** | accepted, 1.8 ms | | 25.6 GB, names claiming 25 GB | **176** | refused: "its names are 25000000000 bytes, more than a game dump's", 0.02 ms | | 64 MiB, damaged TOC | **176** | refused: "its names start at 16, outside its table of contents" | | 64 MiB, 2^40 folders | **176** | refused: "it claims 1099511627776 folders, more than a game dump has" | The old path, on the same files: - the real dump: **258.5 MiB** peak, parses (36.8 MB TOC, 2 titles, 1,199 dirs, 372,587 files) — constant in the 25.6 GB of payload, which is what the 1–2 GiB limit and the 256 MiB TOC cap are sized from; - a 64 MiB header of the report's shape: **148.3 MiB peak = 2.32×** the file; - a 25.6 GB sparse file with the report's shape (names claiming 25 GB): the read plus the names copy is 50.0 GB = 46.6 GiB before the records are built, **`MemoryError` at `nx2/nup.py:143`** under `ulimit -v 2 GiB` (the limit this branch now sets), and **OOM-killed** under a `MemoryMax=2G` scope. On the reporter's PC, with a header whose offsets land near the end of the file, the measured 2.3× is the 59.1 GiB they saw. The sparse file is a header and a hole: no game data is in it, and nothing leaves this PC. ## Tests - `tests/launcher_core` (built from `Core/` at `b372d00`, run the way `tests/test_launcher_core.py` runs it): **2704 checks passed**, 0 warnings, 0 errors — including the new `NupIndexChecks`: a sound dump accepted from 176 bytes, a 1 TiB header with names past its TOC refused from 176 bytes, every count and offset bound refused, and the wizard reading `NupIndex.Why` before it starts the tools. - Tooling's `tests/test_release.py`: **42 tests, OK**. - `python -I tests/test_launcher_core.py` still stops at its **translation** gate: 48 "left over" catalog texts, and the same 48 with the same texts on `main` (`6887d57`) — the state @dyst's open #246 fixes. This branch changes no catalog text and no launcher string the catalogs hold; it is red on `main` and here alike. ## Ready for review - [x] One topic, a branch named for it (`fix/nup-toc-memory`), Conventional Commits. - [x] The tests read the source and pass (above), with the one pre-existing red named. - [x] No game files, keys or anything made from them in the branch, the logs or this description. - [x] Two platforms kept: `NupIndex` reads a stream, Windows and Linux alike; the limit is Linux's, where the report is. - [x] `nx2/nup.py` left to its owner (#1525).
A .nup's table of contents is u64 counts and offsets, and nx2/nup.py reads
0x80 + strings offset + strings size bytes into memory and slices them again
before it checks any of them. A damaged header therefore made the game-file
check read the whole file twice: a 25.6 GB dump took 59 GiB (Longshot#1525),
and the launcher sat on "Checking..." until the PC ran out of memory. The
launcher now reads 0xB0 bytes of the header first and refuses a table of
contents that does not fit the file or is far larger than a game dump's
(256 MiB, 2.5 million files), with the plain "damaged" or "incomplete" words
under the file.

On Linux the check also runs under RLIMIT_AS, half the PC's memory from 1 to
2 GiB, set by bash's ulimit before the tools exec: make_game.py starts nup.py as
its own process, and the limit is inherited, so no .nup shape (a zstd frame's
window included) can take more than that from the PC. The launcher also keeps
only the check's last 512 lines.

Assisted-by: DeepSeek V4.1 Flash 1017 (opencode-go)
fix(check): a check whose tools are not there still says so, not exit 127
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Compile and test (pull_request) Blocked by required conditions
b372d00612
The memory limit puts bash in front of the check (ulimit -v, then exec of make_game.py), so a program that is not on this PC
no longer made Process.Start fail: the shell started, exited 127, and the check answered "the build tools stopped without
saying why" instead of "The build tools could not start", and so blamed the player's file. Bounded now leaves a run alone
when its program is not there (a path on the disk, or a name in a folder of the PATH), and that answer is this PC's again.

tests/launcher_core's "Tools that do not start: this PC's, not the file's" is what caught it; the wrap's own check now puts
its program on the disk, and a new one holds a missing program unwrapped.

Assisted-by: DeepSeek V4.1 Flash 1017 (opencode-go)
Member

Possibly related, for context:

  • issue MagicBeanDev/Longshot#1525 Install: Linux launcher hangs checking .nup and exhausts system memory — Directly fixes reported Linux .nup check memory exhaustion
Possibly related, for context: - issue MagicBeanDev/Longshot#1525 Install: Linux launcher hangs checking .nup and exhausts system memory — Directly fixes reported Linux .nup check memory exhaustion
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Compile and test (pull_request) Blocked by required conditions
This pull request doesn't have enough approvals yet. 0 of 1 approvals granted.
Some workflows are waiting to be reviewed.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'fix/nup-toc-memory:dazemc-fix/nup-toc-memory'
git switch 'dazemc-fix/nup-toc-memory'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/LongshotLauncher!252
No description provided.