tooling: harden bounds in bink_heap_aot and add synthetic PE32+ unit and refusal suite #1901
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1908"
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-24T20:49:09Z.
Change
Harden bounds and CLI error handling in
tools/bink_heap_aot.pyand add a synthetic AMD64 PE32+ unit and refusal suite intests/bink_heap_aot_check.pyregistered asbink_heap_aot_checkinCMakeLists.txt:DIRECTORY_ENTRY_IMPORTintranslate()with a clearValueError("image has no import directory")instead of raisingAttributeError.import_name()viaimage_bytes(pe, target, 6, executable=True)before decoding the IAT thunk jump.main(argv=None)intools/bink_heap_aot.pyand handleValueError,OSError, andpefile.PEFormatErrorviaparser.exit(1, ...)instead of emitting raw tracebacks.tests/bink_heap_aot_check.pywith positive lowering controls (callback_setter(),translate(),group_caller(),generate(),--entries/--group-entryCLI mode) and negative refusal controls (non-writable, truncated, non-null, and duplicate callback slots; unexpected heap imports; missing import directory; unmapped, non-executable, and truncated IAT thunk targets; missing or duplicate heap transfers; non-AMD64/PE32 inputs; invalid CLI output/input paths).Verification
python -B tests/bink_heap_aot_check.py -v:Ran 12 tests in 2.244s—OK.cmake --preset dev+cmake --build --preset dev --target cpu_bink_heap_lifted cpu_bink_heap cpu_bink_heap_provider+ctest --preset dev --no-tests=error -R "^(bink_heap_aot_check|cpu_aot_bink_heap|cpu_aot_bink_group)$" --output-on-failure:3/3passed (cpu_aot_bink_heap,cpu_aot_bink_group,bink_heap_aot_check).cmake --preset release+cmake --build --preset release --target cpu_bink_heap_lifted cpu_bink_heap cpu_bink_heap_provider+ctest --preset release --no-tests=error -R "^(bink_heap_aot_check|cpu_aot_bink_heap|cpu_aot_bink_group)$" --output-on-failure:3/3passed (cpu_aot_bink_heap,cpu_aot_bink_group,bink_heap_aot_check).python -B tests/form_coverage_check.py: passed.python -B tests/docs_links_check.py: passed (Checked 18 Markdown files and 377 links.).python -B tests/bidi_controls_check.py: passed (Checked 1338 tracked source, script and build/CI files: no bidirectional controls.).Evidence and follow-up
tests/bink_heap_aot_check.pysynthesizes 100% self-contained in-memory AMD64 PE32+ images withstruct/pefile; no external binaries or proprietary files are used.DIRECTORY_ENTRY_IMPORTguard and pre-decodeimage_bytes()check confirmedtest_missing_import_directory_raises_value_errorandtest_import_name_thunk_bounds_rejectedfail without the hardening.Checklist
AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T21:02:06Z.
Review: COMMENTED
Review of exact head
fd8dfcf1400725be8cb3fb3accf37eadc77e1a6c(basemain). Scope:tools/bink_heap_aot.py(+21/-6), newtests/bink_heap_aot_check.py(+495),CMakeLists.txt(+2). Comment only, no approval.Verdict: no P1/P2 found. Looks good to me from a correctness and test-strength standpoint; one P3 behaviour note below.
Clean-room / title
XMemAlloc,XMemFree,BinkSetMemory) already exist in the tool.What I checked in the diff
import_name(tools/bink_heap_aot.py:19-22): the 6-byte thunk read now goes throughimage_bytes(..., executable=True)BEFORE decoding. Previouslype.get_dataran first and the bounds check only ran after a successfuljmp [rip+x]match, so an unmapped, non-executable or section-truncated branch target was decoded silently. The old post-hocimage_bytes(pe, target, thunk.len, ...)is correctly removed (it was redundant, since 6 bytes are now covered).translate:getattr(pe, "DIRECTORY_ENTRY_IMPORT", None)plus aValueErrorreplaces anAttributeErrortraceback on images without an import directory. Correct.main(argv=None): exits 1 with a message onValueError/OSError/PEFormatError;parser.errorpaths still exit 2. The output file is only written aftergeneratesucceeds (thewrite_textargument is evaluated first), so a failure leaves no partial.cpp. The test asserts this (assertFalse(out_cpp.exists())).Commands run locally (venv
x2w-rev-venv, worktree at the exact SHA,git rev-parse HEAD==headRefOid)git diff --check origin/main...HEAD-> exit 0, no outputpython -B tests/bink_heap_aot_check.py -v->Ran 12 tests ... OKpython -B tests/bidi_controls_check.py->Self-test passed. Checked 1339 tracked ... files: no bidirectional controls.python -B tests/docs_links_check.py->Self-test passed. Checked 18 Markdown files and 377 links.x2w_pipe.py win2025 1908 --targets cpu_bink_descriptors,...,xmem_shim --regex '^(bink_heap_aot_check|cpu_aot_bink_group|cpu_aot_bink_heap|cpu_bink_descriptors|cpu_bink_frame_planes|cpu_bink_frame_to_heap)$'(Windows Server 2025, MSVC 14.44.35207): Debug and Release both 6/6 passed, including the newbink_heap_aot_check(ctest #1161, ~1.8 s) and the real-imagecpu_aot_bink_heap/cpu_aot_bink_group, so the generated output for real inputs is unchanged in practice.Mutation testing (each applied to the PR head, suite re-run, then
git checkout -- .)import_nameback tope.get_data(target, 6)test_import_name_thunk_bounds_rejectedexecutable=Trueon the thunk readif not imports:->if False:test_missing_import_directory_raises_value_errormain()except clause narrowed toKeyErrortest_cli_rejects_invalid_output_and_missing_inputcheck_callback_slotboundoffset + 8->offset + 4test_truncated_callback_slot_rejectedtest_cli_rejects_invalid_output_and_missing_inputAll six mutants are killed, so the new tests do guard the hardened behaviour (including the three thunk-bound cases: unmapped, non-executable, truncated).
P3 (non-blocking)
tools/bink_heap_aot.py:19import_nameis called for everyCALL/JMPwith a near-branch operand inleaf_bodyandtranslate, not only for IAT thunks. Because the 6-byte read now raises instead of returningNone, a perfectly valid internal call/jmp whose target is a tiny function in the last 1 to 5 file-backed bytes of an executable section (e.g. a 3-bytexor eax,eax; retat the end of.text) now aborts generation withValueError: invalid file-backed range at RVA ...instead of being treated as "not an import". I reproduced it on a synthetic image (text_vsize=0x300, targetTEXT_RVA+0x2fd->ValueError ... 0x12fd). It fails closed (a clear error, never wrong output) and the real Bink images in CI are unaffected (tests above pass), so I would not block on it. If you want to keep the old tolerance, readmin(6, available)bytes for the probe and only enforce the full range once ajmp [rip+x]has decoded. Otherwise a one-line comment stating that stricter behaviour is intended would help.Nits
main()catchesValueError/OSError/PEFormatErroronly; other malformed-image failures (e.g.struct.error,IndexErrorfrompefile) would still print a traceback. A truncated-file probe I ran exits 1 cleanly, so this is only a robustness remark.add_test(NAME bink_heap_aot_check ...)has noTIMEOUT, unlike the neighbouring Python tests (e.g.pe_run_package_trap_missingsets 60). The test runs in ~1.9 s, so this is cosmetic.test_import_name_thunk_bounds_rejectedonly assertsValueErrorwithout a message; the three sub-cases could pass for the wrong reason if anotherValueErrorwere raised. Mutations M1/M2 show they currently do fail for the right reason.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T21:12:05Z.
Review: COMMENTED
Review of head
66f7013599e84be2a58b81d2ad3d624509fda597.Verdict: no P1. Two P2 test gaps, nothing blocking.
Diff read
tools/bink_heap_aot.py: the IAT-thunk read now goes throughimage_bytes(..., executable=True)before decoding (so an unmapped, non-executable or truncated thunk target raisesValueErrorinstead of reading garbage or returning short data); a missing import directory raisesValueError;main(argv)mapsValueError/OSError/PEFormatErrorto exit 1 with a message. Semantics and bounds look right; the old post-decodeimage_bytes(pe, target, thunk.len, ...)check is correctly subsumed by checking all 6 bytes first.CMakeLists.txt: oneadd_testin the Windows pe-env block.tests/bink_heap_aot_check.py: synthetic PE32+ builder with positive and refusal cases.P2 (test gaps)
tools/bink_heap_aot.py(the newexcept (ValueError, OSError, pefile.PEFormatError)inmain): no test makes the CLI hit aValueError. RemovingValueErrorfrom that tuple leaves all 12 tests green, so a bad image would again end in a traceback unnoticed. A CLI case with a refusal image (e.g. the no-import-directory one) asserting exit code 1 and no "Traceback" would pin it.import_name(thunk.mnemonic != M.JMP or not thunk.is_ip_rel_memory_operand): weakening it tothunk.mnemonic != M.JMPis not caught; no case has ajmpthunk with a non-RIP-relative operand. This line is not new in the PR, so it is only a suggestion.Commands run locally (Linux, venv python)
git diff --check origin/main...HEAD: exit 0.tests/bink_heap_aot_check.py: 12 tests, OK (1.1 s).tests/docs_links_check.py: 18 files, 380 links.tests/bidi_controls_check.py: 1343 files, clean.^bink_heap_aot_check$: Debug 1/1 and Release /O2 1/1 passed.Mutations (each reverted with
git checkout -- .)pe.get_data(target, 6)test_import_name_thunk_bounds_rejected)test_missing_import_directory_raises_value_error)ValueErrorfrom the CLIexceptmnemonic != JMPAI-assisted; commands listed were executed locally.