ffx: harden device-init dispatch lookup and CLI error handling with unit suite #1926
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1933"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T21:40:08Z.
Change
Harden
tools/ffx_device_init.pyand add a synthetic AMD64 PE32+ unit and refusal suite intests/ffx_device_init_check.py, registered in CTest asffx_device_init_check:ins.memory_displacementingenerate()with amethods.get(...)check that raises a contextualValueError(including the displacement and instruction IP) instead of aKeyErroron unexpected indirect calls.main(argv=None) -> intintools/ffx_device_init.pyand catch(OSError, ValueError, pefile.PEFormatError, struct.error)withparser.exit(1, ...)so missing files, malformed PE/struct inputs, non-.cppoutput paths, and matching image/output paths exit cleanly with code1and no traceback.tests/ffx_device_init_check.py(9 tests: synthetic PE32+generate()/main()output checks, mockedchecked_image,.pdatainterval check, non-RAXbase call refusal, unexpectedRAXdisplacement refusal, mismatched heap UUID refusal, missing/non-PE refusal,struct.errorhandling, and.cpp/same-file CLI checks) and registerffx_device_init_checkinCMakeLists.txt.AI-assisted.
Verification
python.exe -B tests/ffx_device_init_check.py -v:Ran 9 tests in 2.607s—OK(withtools/ffx_device_init.pyreverted as a mutation sanity check, 5/9 tests fail as expected).cmake --preset dev&ctest --preset dev -N:Total Tests: 1217.ctest --preset dev --no-tests=error -R "^(ffx_device_init_check|ffx_create_context_check|ffx_dispatch_profile_check)$" -V:3/3passed (ffx_dispatch_profile_check10/10,ffx_create_context_check6/6,ffx_device_init_check9/9).cmake --preset release&ctest --preset release -N:Total Tests: 1217.ctest --preset release --no-tests=error -R "^(ffx_device_init_check|ffx_create_context_check|ffx_dispatch_profile_check)$" -V:3/3passed (ffx_dispatch_profile_check10/10,ffx_create_context_check6/6,ffx_device_init_check9/9).python.exe -B tests/form_coverage_check.py,python.exe -B tests/docs_links_check.py,python.exe -B tests/bidi_controls_check.py: all exited0.Evidence and follow-up
All fixtures in
tests/ffx_device_init_check.pyare synthetic AMD64 PE32+ images constructed in-memory; no packaged binary files are required or included.Checklist
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T21:54:31Z.
Review: COMMENTED
Review of head
6bd3e3d2c9835a862ca7e847b44a430c7c15befc.Result: no P1, no P2, one P3.
Commands run locally, with results:
git rev-parse HEADmatches the head above.git merge-tree --write-tree --name-only origin/main HEADgave a clean tree, no conflicts.gh pr checks 1933: Linux checks, Windows build and gfx1013 fixtures pass. The /O2 job and chart publish are skipped.git diff --check origin/main...HEAD: clean.python3 tests/bidi_controls_check.py: pass.python3 tests/docs_links_check.py: pass.python3 -B tests/ffx_device_init_check.pyin a venv fromrequirements-dev.txt: 9 tests OK. The system python has nopefile, so the test needs the dev requirements, as CI has.python3 $P win2025 1933 --wait 900: Debug and Release /O2 both pass. Build andctest -R ^ffx_device_init_check$passed 1/1 in each.Mutation: I removed
pefile.PEFormatErrorfrom theexcepttuple inmain(). The suite failed (1 error), so the change is covered. I then restored the file withgit checkout -- ..Findings:
tools/ffx_device_init.py:66-71: the bad suffix and same-file checks usedparser.error, which exits 2. They now go throughValueErrorand exit 1. The only consumer, the CMake custom command atCMakeLists.txt:1926, only checks for nonzero, so nothing breaks. The test pins exit 1. Worth a line in the PR description.Checked and fine:
ValueErrorwith the displacement and address, instead of a bareKeyError. A test covers it.main(argv)is testable, and the output is not created when generation fails. Tests assert this.add_testsits inside the existingINTAKE_PYTHONguard.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T21:59:14Z.
Ran the suite, broke four paths, one guard turns out not to be the one being tested
Windows 11 build 26300.9974:
Mutations to
tools/ffx_device_init.py, each reverted:{8: …, 0x70: …}[ins.memory_displacement](rawKeyError)except (OSError, ValueError, pefile.PEFormatError, struct.error)→except (KeyboardInterrupt,)parser.exit(1, …)→parser.exit(0, …)raise ValueError("choose a distinct .cpp output")→passThe same-file guard is not what makes that test pass
test_non_cpp_suffix_and_same_file_exit_1_cleanlyusessame.cppfor both image and output and assertsreturncode == 1plussame.read_bytes() == raw. With the guard removed, it still passes — becausegenerate()fails first, on a different error:Same exit code, same untouched file, different reason. So the test asserts the outcome but not the cause, and
--outputpointing atthe input image would be a real overwrite if
generate()ever succeeded on that input (their in-process tests mockchecked_image, which is why the subprocess case never reaches a successfulgenerate).Minimal way to pin it, in the style the file already uses:
gen.assert_not_called()is the part that distinguishes this from what the current test observes.What is good here
methods.getchange adds the displacement and the instruction IP to the message; losing theKeyErroris the differencebetween "a dict lookup failed somewhere" and "a call at
0x…used displacement0x…".main(argv=None) -> intwithSystemExitfromparser.exitis the right shape: the exit code is1for every refusal, andthe traceback is gone, which is what the subprocess assertions check.
pefile.PEFormatErrorandstruct.errorat the boundary rather than deep inside is the right place — the syntheticPE builder is what made those reachable.
Verdict
Reviewed. Three of the four behaviours are pinned; the fourth is asserted by outcome only and would regress silently. One small
test closes it.
Notes