tooling: validate positive interval bounds in range_manifest and add unit and CLI suite #2086

Merged
Ammmae0x merged 1 commit from upstream-pr/2093 into main 2026-11-25 00:33:27 +00:00
Ammmae0x commented 2026-11-25 00:11:43 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T00:11:43Z.


Change

Validate function names and positive interval bounds in tools/range_manifest.py, extract a testable main(argv=None) -> int with atomic temp-file writes, and add a pure-Python unit and CLI test suite (tests/range_manifest_check.py) registered in CMakeLists.txt (range_manifest_check):

  • manifest() rejects empty names, empty/blank/non-string function names, duplicate function names, and empty/inverted intervals (end <= start) with ValueError while preserving map_symbols() and the existing JSON schema (image_sha256, functions).
  • Extract CLI into main(argv=None) -> int with _write_atomic_text() (temp file in output parent + os.replace, unlinking the temp file on any error so failures never corrupt an existing manifest or leave leftover temp files) and parser.exit(1, ...) on (ValueError, OSError) with zero raw tracebacks.
  • Add tests/range_manifest_check.py covering synthetic MSVC /MAP parsing (preferred load address, RVA subtraction, setdefault first-symbol precedence), binary image SHA-256, CLI JSON output (direct and subprocess), and negative controls pinning ValueError/SystemExit(1) and zero temp-file leaks on missing load address, missing symbol/_end, end == start, end < start, empty/blank/duplicate names, missing files, and failed os.replace.

Verification

Tested on commit 02c31a29 (rebased onto upstream/main 75b25ad0, Windows x64, Python via out/pe-env):

  1. .\out\pe-env\Scripts\python.exe -B tests/range_manifest_check.py
    • Result: Ran 6 tests in 0.204s — OK (exit 0).
  2. .\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py
    • Result: form coverage checks passed (exit 0).
  3. .\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py
    • Result: Self-test passed. Checked 18 Markdown files and 393 links. (exit 0).
  4. .\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py
    • Result: Self-test passed. Checked 1414 tracked source, script and build/CI files: no bidirectional controls. (exit 0).

Note: No C++ targets were modified; range_manifest_check is pure Python and registered next to bink_descriptors_aot_check in CMakeLists.txt.

Evidence and follow-up

  • All test fixtures in tests/range_manifest_check.py are synthetic in-memory MSVC /MAP text and small byte buffers; no external binaries or game files are used.
  • Negative controls verify exact ValueError messages and SystemExit(1) exit codes with zero raw tracebacks, preserved target files on os.replace failure, and zero leftover temp files.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6594093145 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2093). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-25T00:11:43Z. --- ## Change Validate function names and positive interval bounds in `tools/range_manifest.py`, extract a testable `main(argv=None) -> int` with atomic temp-file writes, and add a pure-Python unit and CLI test suite (`tests/range_manifest_check.py`) registered in `CMakeLists.txt` (`range_manifest_check`): - `manifest()` rejects empty `names`, empty/blank/non-string function names, duplicate function names, and empty/inverted intervals (`end <= start`) with `ValueError` while preserving `map_symbols()` and the existing JSON schema (`image_sha256`, `functions`). - Extract CLI into `main(argv=None) -> int` with `_write_atomic_text()` (temp file in output parent + `os.replace`, unlinking the temp file on any error so failures never corrupt an existing manifest or leave leftover temp files) and `parser.exit(1, ...)` on `(ValueError, OSError)` with zero raw tracebacks. - Add `tests/range_manifest_check.py` covering synthetic MSVC `/MAP` parsing (preferred load address, RVA subtraction, `setdefault` first-symbol precedence), binary image SHA-256, CLI JSON output (direct and subprocess), and negative controls pinning `ValueError`/`SystemExit(1)` and zero temp-file leaks on missing load address, missing symbol/`_end`, `end == start`, `end < start`, empty/blank/duplicate names, missing files, and failed `os.replace`. ## Verification Tested on commit `02c31a29` (rebased onto `upstream/main` `75b25ad0`, Windows x64, Python via `out/pe-env`): 1. `.\out\pe-env\Scripts\python.exe -B tests/range_manifest_check.py` - Result: `Ran 6 tests in 0.204s` — `OK` (exit 0). 2. `.\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py` - Result: `form coverage checks passed` (exit 0). 3. `.\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py` - Result: `Self-test passed. Checked 18 Markdown files and 393 links.` (exit 0). 4. `.\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py` - Result: `Self-test passed. Checked 1414 tracked source, script and build/CI files: no bidirectional controls.` (exit 0). Note: No C++ targets were modified; `range_manifest_check` is pure Python and registered next to `bink_descriptors_aot_check` in `CMakeLists.txt`. ## Evidence and follow-up - All test fixtures in `tests/range_manifest_check.py` are synthetic in-memory MSVC `/MAP` text and small byte buffers; no external binaries or game files are used. - Negative controls verify exact `ValueError` messages and `SystemExit(1)` exit codes with zero raw tracebacks, preserved target files on `os.replace` failure, and zero leftover temp files. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted.
upstream-sync closed this pull request 2026-11-25 01:01:37 +00:00
ChazY11 commented 2026-11-25 01:02:04 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T00:23:16Z.


Ran it, broke the validator seven ways — all caught — and one side effect I would rather you hear from me than from CI

out/pe-env/Scripts/python.exe -B tests/range_manifest_check.py
Ran 6 tests in 0.573s
OK      exit 0
mutation result
if not names: removed (empty name list accepted) caught
name.strip() dropped (blank name accepted) caught
the duplicate check removed caught
the interval check end <= start → end < start (allows a zero-length range) caught
the interval check removed entirely caught
os.replace(tmp_path, output) → output.write_text(...) (non-atomic) caught
main returns 1 instead of 0 caught

The fourth and the sixth are the two I would have expected to slip: the boundary end == start is the one an interval validator usually gets
wrong, and "atomic write" is the kind of property that is easy to state and hard to check. Both are pinned.

Two things the implementation gets right that the mutations confirm indirectly:

  • the temp file is created in the target directory (dir=str(output.parent)), which is what makes os.replace atomic across the rename;
  • the except BaseException path unlinks the temp file, and no .tmp file is left behind after a successful write — I checked the directory
    contents after the call.

The side effect: the output file's mode changes on POSIX

On Windows this machine reports 0666 for both the atomic writer and the plain write_text path, so nothing to see here:

atomic.txt   mode=0666
plain.txt    mode=0666

On POSIX it is not the same. tempfile.mkstemp opens the file with mode 0600 regardless of umask, and os.replace carries that mode
to the output. Path.write_text follows the umask instead (typically 0644). So this PR silently changes the permissions of a
range-manifest.json produced on Linux from 0644 to 0600 — and CI runs on Linux, so the first place that shows up is a later step reading the
file as a different user, or a packaging step that copies it into an image.

I could not measure this here (Windows only), so treat it as a claim about the standard library's contract rather than a measurement. If you want to
keep the old behaviour, one line after mkstemp preserves it:

os.chmod(tmp_path, 0o666 & ~_current_umask())   # or: copy the mode of output if it already exists

Alternatively, if the manifest is only ever read by the invoking user, say so in the docstring and this becomes a non-issue — but that is the sort of
thing worth writing down while the change is small.

One smaller question

manifest() now raises for an empty names. The CLI passes args.names; if that argument is optional, a caller who used to get an empty
functions: [] document now gets a ValueError. That is probably the intent (an empty manifest is a bug), but it is a behaviour change for existing
callers — worth a line in the description, since the title frames this as validation of bounds.

What I did not check

Anything that needs a real PE image and map file: I exercised the check and the helpers it covers, not manifest() against a fixture pair. The
6 tests you wrote presumably do; my mutations were aimed at the guards they should be protecting.

AI-assisted; the seven mutations and the mode probe were run locally on Windows 11 build 26300.9974.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7148456514 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2093#issuecomment-7148456514). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-25T00:23:16Z. --- ## Ran it, broke the validator seven ways — all caught — and one side effect I would rather you hear from me than from CI ```powershell out/pe-env/Scripts/python.exe -B tests/range_manifest_check.py Ran 6 tests in 0.573s OK exit 0 ``` | mutation | result | |---|---| | `if not names:` removed (empty name list accepted) | **caught** | | `name.strip()` dropped (blank name accepted) | **caught** | | the duplicate check removed | **caught** | | the interval check `end <= start` → `end < start` (allows a zero-length range) | **caught** | | the interval check removed entirely | **caught** | | `os.replace(tmp_path, output)` → `output.write_text(...)` (non-atomic) | **caught** | | `main` returns 1 instead of 0 | **caught** | The fourth and the sixth are the two I would have expected to slip: the boundary `end == start` is the one an interval validator usually gets wrong, and "atomic write" is the kind of property that is easy to *state* and hard to *check*. Both are pinned. Two things the implementation gets right that the mutations confirm indirectly: - the temp file is created **in the target directory** (`dir=str(output.parent)`), which is what makes `os.replace` atomic across the rename; - the `except BaseException` path unlinks the temp file, and no `.tmp` file is left behind after a successful write — I checked the directory contents after the call. ## The side effect: the output file's mode changes on POSIX On Windows this machine reports `0666` for both the atomic writer and the plain `write_text` path, so nothing to see here: ```text atomic.txt mode=0666 plain.txt mode=0666 ``` On **POSIX** it is not the same. `tempfile.mkstemp` opens the file with mode **0600** regardless of umask, and `os.replace` carries that mode to the output. `Path.write_text` follows the umask instead (typically `0644`). So this PR silently changes the permissions of a `range-manifest.json` produced on Linux from `0644` to `0600` — and CI runs on Linux, so the first place that shows up is a later step reading the file as a different user, or a packaging step that copies it into an image. I could not measure this here (Windows only), so treat it as a claim about the standard library's contract rather than a measurement. If you want to keep the old behaviour, one line after `mkstemp` preserves it: ```python os.chmod(tmp_path, 0o666 & ~_current_umask()) # or: copy the mode of output if it already exists ``` Alternatively, if the manifest is only ever read by the invoking user, say so in the docstring and this becomes a non-issue — but that is the sort of thing worth writing down while the change is small. ## One smaller question `manifest()` now raises for an empty `names`. The CLI passes `args.names`; if that argument is optional, a caller who used to get an empty `functions: []` document now gets a `ValueError`. That is probably the intent (an empty manifest is a bug), but it is a behaviour change for existing callers — worth a line in the description, since the title frames this as validation of bounds. ## What I did not check Anything that needs a real PE image and map file: I exercised the check and the helpers it covers, not `manifest()` against a fixture pair. The `6 tests` you wrote presumably do; my mutations were aimed at the guards they should be protecting. AI-assisted; the seven mutations and the mode probe were run locally on Windows 11 build 26300.9974.
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
R0Y00/Xbox2Windows!2086
No description provided.