shader: harden DXIL/PSV0 validation and add unit suite for shader_profile_audit #1843
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1850"
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-24T19:33:47Z.
Change
Hardens DXIL, PSV0, disassembly, and archive/CLI input validation in
tools/shader_profile_audit.pyand adds a unit suite intests/shader_profile_audit_check.py(registered inCMakeLists.txtasshader_profile_audit_check):stage(rejecting booleans, non-integers, and unknown stage IDs),sectionsmapping,DXILpresence/length (>= 24),PSV0presence/length (>= 52, plus mesh stage byte13, PSV size bounds, and container payload length>= 32) inclassify(), raising explicitValueErrordiagnostics.group_sizetuple bounds, and disassembly return shape inpixel_profiles(),compute_profiles(), andcb_diagnostic(), and guardsemit=Noneand missingDXILduring--cb-readsdiagnostics inscan().OSError/ValueErrorCLI exit handling inscan()andmain().tests/shader_profile_audit_check.pycovering PS (general,attribute,depth,coverage), CS (store), and MS (point mesh,indexed mesh) classification,--source-dirHLSL output,cb_diagnostic()+--cb-readsaggregation, CLI JSON execution, and negative controls for missing/truncated DXIL/PSV0, unsupported instructions, unknown stages, and empty/missing archives.Verification
Commit:
759e9063(rebased ontoupstream/mainfb8947f0)cmake --preset dev -DINTAKE_PYTHON=<pe-env python>: configured cleanly (Total Tests: 1133).cmake --preset release -DINTAKE_PYTHON=<pe-env python>: configured cleanly (Total Tests: 1133).ctest --preset dev --no-tests=error -R "^(shader_profile_audit_check|shader_metadata_census|shader_metadata_controls)$" -E "cpu_native_archive_" -V:3/3passed (shader_metadata_controls,shader_metadata_census,shader_profile_audit_check).ctest --preset release --no-tests=error -R "^(shader_profile_audit_check|shader_metadata_census|shader_metadata_controls)$" -E "cpu_native_archive_" -V:3/3passed (shader_metadata_controls,shader_metadata_census,shader_profile_audit_check).<pe-env python> -B tests/shader_profile_audit_check.py -v:Ran 3 tests in 1.566s - OK.<pe-env python> -B tests/form_coverage_check.py:form coverage checks passed.<pe-env python> -B tests/docs_links_check.py:Self-test passed. Checked 18 Markdown files and 370 links.<pe-env python> -B tests/bidi_controls_check.py:Self-test passed. Checked 1325 tracked source, script and build/CI files: no bidirectional controls.Note:
shader_profile_audit_checkis a pure-Python CTest target (no compiled binary target).tests/shader_profile_audit_check.pystubspixel_aot.disassembleandmesh_entry_aot.disassemblewith synthetic DXBC/DXIL/PSV0 containers so tests run without LLVM 15.Evidence and follow-up
All test containers and shader instruction sequences in
tests/shader_profile_audit_check.pyare synthetic and self-contained; no external archives, dumps, or game files were used.Checklist
AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T19:38:03Z.
Review: COMMENTED
Review of
759e9063(basefb8947f0;origin/mainis now8e2923ae, and none of the three touched files changed on main in between). Comment only, not an approval.Verdict
Looks sound, with one behaviour change I would fix or state before merge (P2) and a few guards that no test pins (P3). Clean-room and title-neutral: the diff and the fixtures are synthetic, with no game files, dumps or titles.
P2: unknown stage ids now change the audit report
classify()now raisesValueError("Unknown shader stage")for any stage not inSTAGES({0,1,2,5,13}). Before, every stage outside 0/5/13 returned([], "no stage profile"). Inscan()that ValueError is caught per record and counted infirst_stops, so the report changes for real archives with other stage ids (hull and domain are 3 and 4).Reproduced on this SHA with a synthetic record of stage 3:
origin/main(with the PR's test helpers copied in):first_stops = {'3': [('no stage profile', 1)]}first_stops = {'3': [('Unknown shader stage', 1)]}The new test only checks -1, 99 and True, so it does not show this. Either keep
no stage profilefor any non-negative int stage and reject only non-ints/bools, or say in the description that the report label changes and pin it with a stage-3 case.P3: guards the suite does not pin
I removed each guard one at a time and ran
tests/shader_profile_audit_check.py. These mutants survived (3 tests, OK):len(psv) < 52inclassify(): the latergroup_sizeand mesh size checks already refuse the same inputs.len(dxil) < 24inclassify(): no case reaches it thatcontainer_codewould not refuse anyway.len(blob) < 32check.psv_size <= len(psv) - 4.Either add a case that only that guard rejects, or drop the guard. The mesh PSV0 size check also duplicates
compute_store_aot.group_size; sharing one helper would keep them from drifting.Bounds and pointer arithmetic
I found no out-of-bounds read, integer overflow or pointer arithmetic in the diff. All offsets (
unpack_from("<I", psv),psv[28]) are behind the length checks.scan()closes the mmap only after the result is built, as before. The CLI now mapsOSError/ValueErrorto exit 1 with a message on stderr and empty stdout.Commands run (Linux, Python 3.14.8, pefile 2024.8.26, iced-x86 1.21.0)
python -B tests/shader_profile_audit_check.py -v: Ran 3 tests, OKpython -B tests/docs_links_check.py: Self-test passed. Checked 18 Markdown files and 370 links.python -B tests/bidi_controls_check.py: Self-test passed. Checked 1325 tracked source, script and build/CI files: no bidirectional controls.python -B tests/form_coverage_check.py: form coverage checks passed;tests/shader_census_check.py: OKgit diff --check origin/main...HEAD: cleangit checkout -- .), the 4 survivors are listed under P3. Killed by the suite: dropping the mesh PSV0 stage-byte check, accepting a zero group dimension, accepting a bool stage, dropping the pixel 4-byte alignment check.Not run
The Windows ctest registration (
shader_profile_audit_checkinCMakeLists.txt) and the dev/release presets were not run; I had no Windows/MSVC environment. The one-line add_test follows the neighbouring pure-Python entries. The tests stubdisassemble, so no LLVM 15 path was exercised.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T19:51:03Z.
Review: COMMENTED
Re-review of
dffd7f56acec6c49af5691bba635b45dba6bb229(previous review at759e9063; one new commit on top of it).Verdict: the P2 is closed, the P3 guards are pinned or removed, no new finding above P3.
Previous findings
classify()now rejects only a bool, a non-int or a negative stage (stage < 0); any other stage not in 0/5/13 returns([], "no stage profile")again, as onorigin/main. The test now runs stages 1, 3, 4 and 99 throughclassifyand expectsno stage profile, adds a stage-3 record to the archive scan (first_stops["3"] == [("no stage profile", 1)],records["3"] == 1in the report and in the CLI JSON), and keeps -1, True and "PS" asValueError.len(psv) < 52for CS and MS,len(dxil) < 24) are removed. That is safe:compute_store_aot.group_sizeand the mesh branch both treat a PSV0 shorter than 4 bytes as size 0 and raiseValueError, the mesh branch requires48 <= size <= len(psv) - 4(sopsv[28]is in range), andcontainer_codealready refuses a DXIL part under 24 bytes. The mesh payload guard and the PSV0 upper bound now have cases: an oversized PSV0 (struct.pack("<I", 52)plus a short body), a 1-byte PSV0, andNone/b"short"as the mesh blob.compute_store_aot.group_size: still open, unchanged (the two checks agree today).Commands run locally on Linux (Python 3.14.8, iced-x86 installed in a scratch folder outside the repository)
python3 -B tests/shader_profile_audit_check.py -v: Ran 3 tests, OK.tests/form_coverage_check.py: form coverage checks passed.tests/shader_census_check.py: OK.python3 -B tests/docs_links_check.py(18 files, 370 links),python3 -B tests/bidi_controls_check.py(1325 files): pass.git diff --check origin/main...HEAD: clean.Local mutations of
tools/shader_profile_audit.py(each restored withgit checkout -- ., tree clean afterwards)stage not in STAGESlen(blob) < 32removed<= len(psv) - 4removedfirst_diagnostic:tokens[0] if tokens else reasonreduced totokens[0]Finding
first_diagnostic(an "Unsupported instruction" message with nothing after the colon) is not pinned; the mutation above survives. One case with that message would pin it.Not run: the Windows ctest registration (
shader_profile_audit_checkinCMakeLists.txt) and the dev/release presets; no Windows/MSVC here. The tests stubdisassemble, so no LLVM 15 path was exercised.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T19:42:21Z.
Updated
tools/shader_profile_audit.pyandtests/shader_profile_audit_check.py(commitdffd7f56) to preserve the"no stage profile"classification for non-negative integer shader stages (while rejecting negative, boolean, or non-integer stages), remove redundant length guards already covered bycontainer_code/group_size/PSV0bounds checks, and pin thePSV0upper-bound, short-buffer, stage-3scan(), and truncated mesh container controls. Verified withtests/shader_profile_audit_check.py,tests/shader_census_check.py,tests/form_coverage_check.py,tests/docs_links_check.py,tests/bidi_controls_check.py, andshader_profile_audit_checkindevandrelease. AI-assisted.