d3d12_device_calls: validate --out upfront and publish reports atomically #2262

Merged
leonida/mandy
kirigayakazima merged 1 commit from fix/d3d12-device-calls-safety into main 2026-11-28 15:05:33 +00:00

Pull request successfully merged and closed

kirigayakazima commented 2026-11-26 11:35:43 +00:00

Claimed in the coordination issue as: D3D12-DEVICE-CALLS-UPFRONT-OUT-AND-ATOMIC-REPORTS (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7190884297)

What and why

tools/d3d12_device_calls.py previously read and scanned the PE image before validating --out or same_report_path(...), and wrote <image>.device_calls.json and .txt directly in place with open(..., "w"). An unusable --out or self-overwrite path wasted a full PE decode (or created an empty --out directory on unreadable images), and an interrupted or failing write could truncate or partially update the report pair.

  • Check --out (non-empty and directory ancestors), input/report path collisions (same_report_path), and directory-occupied report paths before reading or scanning the input image, deferring os.makedirs until scan output is ready to write.
  • Stage both .json and .txt outputs into UTF-8 LF temp files inside --out, copy/probe permissions via file_mode.published_mode(...), replace both via replace_retry.replace(...), and clean up any leftover staged files on error.
  • Add CLI tests covering upfront --out / collision / directory-target errors, unopened invalid images, and preservation of prior reports on staging failure.

Tested

  • python -I tests/test_d3d12_device_calls.py -v (19 tests passed)
  • Detached upstream/main baseline negative control: failed with the new CLI tests before the fix
  • python -I tests/test_ci_registration.py, python -I tests/test_text_write_line_endings.py, python -I scripts/check_no_game_data.py, python -I -m compileall -q, and git diff --cached --check

GPUs tested: n/a

AI-assisted: yes

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.
Claimed in the coordination issue as: D3D12-DEVICE-CALLS-UPFRONT-OUT-AND-ATOMIC-REPORTS (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7190884297) ### What and why `tools/d3d12_device_calls.py` previously read and scanned the PE image before validating `--out` or `same_report_path(...)`, and wrote `<image>.device_calls.json` and `.txt` directly in place with `open(..., "w")`. An unusable `--out` or self-overwrite path wasted a full PE decode (or created an empty `--out` directory on unreadable images), and an interrupted or failing write could truncate or partially update the report pair. - Check `--out` (non-empty and directory ancestors), input/report path collisions (`same_report_path`), and directory-occupied report paths before reading or scanning the input image, deferring `os.makedirs` until scan output is ready to write. - Stage both `.json` and `.txt` outputs into UTF-8 LF temp files inside `--out`, copy/probe permissions via `file_mode.published_mode(...)`, replace both via `replace_retry.replace(...)`, and clean up any leftover staged files on error. - Add CLI tests covering upfront `--out` / collision / directory-target errors, unopened invalid images, and preservation of prior reports on staging failure. ### Tested - `python -I tests/test_d3d12_device_calls.py -v` (19 tests passed) - Detached `upstream/main` baseline negative control: failed with the new CLI tests before the fix - `python -I tests/test_ci_registration.py`, `python -I tests/test_text_write_line_endings.py`, `python -I scripts/check_no_game_data.py`, `python -I -m compileall -q`, and `git diff --cached --check` GPUs tested: n/a AI-assisted: yes ### 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.
real-rusher commented 2026-11-26 12:23:38 +00:00

Verified on main a73a57e1 (Ubuntu 24.04 x86-64, Python 3.13, no GPU). Merges cleanly. With this PR, python -I tests/test_d3d12_device_calls.py: 19 tests OK. The PR's test file against main's tool: 2 failures and 1 error, so the new tests fail on main and pass with the change. compileall clean.

AI-assisted: yes (Claude Code)

Verified on main a73a57e1 (Ubuntu 24.04 x86-64, Python 3.13, no GPU). Merges cleanly. With this PR, python -I tests/test_d3d12_device_calls.py: 19 tests OK. The PR's test file against main's tool: 2 failures and 1 error, so the new tests fail on main and pass with the change. compileall clean. AI-assisted: yes (Claude Code)
tiguan26 approved these changes 2026-11-27 04:36:47 +00:00
tiguan26 left a comment

Approving 6dd813ddb.

--out and its existing ancestors are checked before the scan, and both reports are staged as temp files and replaced only once all are written.

Merged onto main d1dff7949 in one batch with 22 other tool PRs of this series (no conflicts), on Linux with Python 3.13. tests/test_d3d12_device_calls.py: 19 passed (main: 15). AI-assisted (Claude Code, Claude Opus 5.5).

Approving 6dd813ddb. `--out` and its existing ancestors are checked before the scan, and both reports are staged as temp files and replaced only once all are written. Merged onto `main` `d1dff7949` in one batch with 22 other tool PRs of this series (no conflicts), on Linux with Python 3.13. `tests/test_d3d12_device_calls.py`: 19 passed (main: 15). AI-assisted (Claude Code, Claude Opus 5.5).
zmasi commented 2026-11-28 10:35:16 +00:00

#2262 | head 6dd813ddb1eedbac60c30af41403ae99f83f45f4 (merged cleanly with main 6c7f79343) | python -I tests/test_d3d12_device_calls.py | red on main: FAILED (failures=2, errors=1) (scan ran before validating --out / hardlink self-overwrite / directory report targets, and report writes were non-atomic) | green on PR+main: 19 tests OK | Windows 11, Python 3.12.10 | AI-assisted: yes

Audited _check_out_dir_ancestors, upfront same_report_path + directory-target checks before opening/scanning the PE image (avoiding empty --out creation on invalid images and catching Windows 11 NTFS os.link collisions), and staged UTF-8 LF atomic publishing via published_mode + replace_retry.replace in tools/d3d12_device_calls.py. Also passed tests/test_ci_registration.py (18 OK), tests/test_text_write_line_endings.py (3 OK), tests/test_runner_calls_all_tests.py (3 OK), and scripts/check_no_game_data.py.

#2262 | head 6dd813ddb1eedbac60c30af41403ae99f83f45f4 (merged cleanly with main 6c7f79343) | `python -I tests/test_d3d12_device_calls.py` | red on main: `FAILED (failures=2, errors=1)` (`scan` ran before validating `--out` / hardlink self-overwrite / directory report targets, and report writes were non-atomic) | green on PR+main: `19 tests OK` | Windows 11, Python 3.12.10 | AI-assisted: yes Audited `_check_out_dir_ancestors`, upfront `same_report_path` + directory-target checks before opening/scanning the PE image (avoiding empty `--out` creation on invalid images and catching Windows 11 NTFS `os.link` collisions), and staged UTF-8 LF atomic publishing via `published_mode` + `replace_retry.replace` in `tools/d3d12_device_calls.py`. Also passed `tests/test_ci_registration.py` (`18 OK`), `tests/test_text_write_line_endings.py` (`3 OK`), `tests/test_runner_calls_all_tests.py` (`3 OK`), and `scripts/check_no_game_data.py`.
MoonEyes commented 2026-11-28 15:05:30 +00:00

Red/green: the PR tests fail on main 09c153948 and pass on head 6dd813ddb1ee (cloud 2).
Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main d48a957bb in GitHub Actions: no new failure.
AI-assisted (Claude Code, Claude Opus 5.5)

Red/green: the PR tests fail on main 09c153948 and pass on head 6dd813ddb1ee (cloud 2). Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main d48a957bb in GitHub Actions: no new failure. AI-assisted (Claude Code, Claude Opus 5.5)
Sign in to join this conversation.
No description provided.