runtime: validate CLI arguments and numeric options in launch.py with dedicated unit suite #2054

Merged
Ammmae0x merged 2 commits from upstream-pr/2061 into main 2026-11-25 00:12:09 +00:00
Ammmae0x commented 2026-11-24 23:46:53 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T23:46:53Z.


Change

Validate CLI arguments and numeric options in tools/startup/launch.py, harden handle cleanup, and add a pure-Python unit and CLI test suite (tests/startup_launch_cli_check.py) registered in CMakeLists.txt (startup_launch_cli_check, TIMEOUT 30):

  • Extract parse_args(argv) and main(argv=None) -> int in tools/startup/launch.py.
  • Reject <3 or empty positional arguments (before --), unpaired trailing option flags (instead of silently dropping them via dict(zip(...))), unknown option flags, and invalid numeric options (--timeout non-numeric/<= 0/non-finite, --memory-gib non-numeric/< 0/non-finite, --max-processes non-integer/< 0) with a concise stderr diagnostic and exit code 2 before creating logdir or spawning processes.
  • Only set JOB_OBJECT_LIMIT_JOB_MEMORY (0x200) when memory > 0 so --memory-gib 0 cleanly disables the job memory cap (matching --max-processes 0).
  • Preserve existing CreateProcess failed: error <N> stdout output and exit code 2, and close all opened Win32 handles (out, err, thread_handle, process_handle, job) in finally.
  • Add tests/startup_launch_cli_check.py covering defaults, explicit options, -- pass-through, quote_argument, prepare_storage_path, check_shader_cache_warning, format_fast_fail_diagnostic, stubbed kernel32 exit/timeout/failure paths, subprocess CLI rejections, and negative controls pinning exit code 2 with zero raw tracebacks and no filesystem writes on validation failure.

Verification

Tested on commit e59c15d4 (rebased on upstream/main 489d5797, Windows x64, Python via out/pe-env):

  1. .\out\pe-env\Scripts\python.exe -B tests/startup_launch_cli_check.py
    • Result: Ran 7 tests in 0.332s — OK (exit 0).
  2. .\out\pe-env\Scripts\python.exe -B tests/startup_launch_check.py --unit
    • Result: launch-unit: storage fallback/preservation, stale shader cache warning, fast-fail diagnostics and argument quoting passed (exit 0).
  3. .\out\pe-env\Scripts\python.exe -B tests/startup_tools_lint_check.py
    • Result: PASS: 7 files under tools/startup parse; control snippet rejected and valid snippet accepted (exit 0).
  4. .\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py
    • Result: form coverage checks passed (exit 0).
  5. .\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py
    • Result: Self-test passed. Checked 18 Markdown files and 391 links. (exit 0).
  6. .\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py
    • Result: Self-test passed. Checked 1408 tracked source, script and build/CI files: no bidirectional controls. (exit 0).

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

Evidence and follow-up

  • All tests in tests/startup_launch_cli_check.py are synthetic and self-contained (Win32 handle lifecycle is exercised with a fake kernel32 stub plus real subprocess CLI checks); existing startup_launch end-to-end probe containment tests remain intact.
  • Negative controls verify exit code 2, zero Python tracebacks, no logdir creation on invalid arguments, exact CreateProcess failed: error ... stdout preservation, and zero leaked handles on CreateProcessW/SetInformationJobObject/AssignProcessToJobObject failures.

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:6593765355 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2061). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-24T23:46:53Z. --- ## Change Validate CLI arguments and numeric options in `tools/startup/launch.py`, harden handle cleanup, and add a pure-Python unit and CLI test suite (`tests/startup_launch_cli_check.py`) registered in `CMakeLists.txt` (`startup_launch_cli_check`, `TIMEOUT 30`): - Extract `parse_args(argv)` and `main(argv=None) -> int` in `tools/startup/launch.py`. - Reject `<3` or empty positional arguments (before `--`), unpaired trailing option flags (instead of silently dropping them via `dict(zip(...))`), unknown option flags, and invalid numeric options (`--timeout` non-numeric/`<= 0`/non-finite, `--memory-gib` non-numeric/`< 0`/non-finite, `--max-processes` non-integer/`< 0`) with a concise stderr diagnostic and exit code `2` before creating `logdir` or spawning processes. - Only set `JOB_OBJECT_LIMIT_JOB_MEMORY` (`0x200`) when `memory > 0` so `--memory-gib 0` cleanly disables the job memory cap (matching `--max-processes 0`). - Preserve existing `CreateProcess failed: error <N>` stdout output and exit code `2`, and close all opened Win32 handles (`out`, `err`, `thread_handle`, `process_handle`, `job`) in `finally`. - Add `tests/startup_launch_cli_check.py` covering defaults, explicit options, `--` pass-through, `quote_argument`, `prepare_storage_path`, `check_shader_cache_warning`, `format_fast_fail_diagnostic`, stubbed kernel32 exit/timeout/failure paths, subprocess CLI rejections, and negative controls pinning exit code `2` with zero raw tracebacks and no filesystem writes on validation failure. ## Verification Tested on commit `e59c15d4` (rebased on `upstream/main` `489d5797`, Windows x64, Python via `out/pe-env`): 1. `.\out\pe-env\Scripts\python.exe -B tests/startup_launch_cli_check.py` - Result: `Ran 7 tests in 0.332s` — `OK` (exit 0). 2. `.\out\pe-env\Scripts\python.exe -B tests/startup_launch_check.py --unit` - Result: `launch-unit: storage fallback/preservation, stale shader cache warning, fast-fail diagnostics and argument quoting passed` (exit 0). 3. `.\out\pe-env\Scripts\python.exe -B tests/startup_tools_lint_check.py` - Result: `PASS: 7 files under tools/startup parse; control snippet rejected and valid snippet accepted` (exit 0). 4. `.\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py` - Result: `form coverage checks passed` (exit 0). 5. `.\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py` - Result: `Self-test passed. Checked 18 Markdown files and 391 links.` (exit 0). 6. `.\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py` - Result: `Self-test passed. Checked 1408 tracked source, script and build/CI files: no bidirectional controls.` (exit 0). Note: No C++ targets were modified; `startup_launch_cli_check` is pure Python and registered next to `startup_launch` in `CMakeLists.txt`. ## Evidence and follow-up - All tests in `tests/startup_launch_cli_check.py` are synthetic and self-contained (Win32 handle lifecycle is exercised with a fake kernel32 stub plus real subprocess CLI checks); existing `startup_launch` end-to-end probe containment tests remain intact. - Negative controls verify exit code `2`, zero Python tracebacks, no `logdir` creation on invalid arguments, exact `CreateProcess failed: error ...` stdout preservation, and zero leaked handles on `CreateProcessW`/`SetInformationJobObject`/`AssignProcessToJobObject` failures. ## 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:24 +00:00
R0Y00 commented 2026-11-25 01:02:01 +00:00 (Migrated from github.com)

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


I added Win32 conversion boundaries before any filesystem or process side effect. The launcher rejects the infinite wait sentinel, DWORD process-count overflow, and positive memory caps that would round to zero or exceed size_t. Explicit zero caps still disable their limits. Windows Release startup_launch and startup_launch_cli_check passed 2/2, including the real source-built child and the malformed input cases.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7148164148 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2061#issuecomment-7148164148). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-25T00:07:14Z. --- I added Win32 conversion boundaries before any filesystem or process side effect. The launcher rejects the infinite wait sentinel, DWORD process-count overflow, and positive memory caps that would round to zero or exceed size_t. Explicit zero caps still disable their limits. Windows Release startup_launch and startup_launch_cli_check passed 2/2, including the real source-built child and the malformed input cases.
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!2054
No description provided.