d3d12_guid_sites, d3d12_iid_scan: report an unreadable header or import table (D3D12-TOOLS-ERROR-REPORTING) #943

Open
github-bot wants to merge 1 commit from github-bot/gh-943 into main AGit
Owner

@⁠TuroYT on GitHub · 2026-11-24 15:25 UTC · original
TuroYT:fix/d3d12-tools-error-reporting → main · +143 −10 · 4 files · 1 commits · open

Claimed in the coordination issue as: D3D12-TOOLS-ERROR-REPORTING (#14)

What was wrong

  1. tools/d3d12_guid_sites.py:391 skipped a --header that does not exist (if header and os.path.isfile(header)).
    Reproduction on main with a 2-byte image and a missing header: 0 known GUIDs from .../no_such_d3d12.h,
    skipped (unpack_from requires a buffer of at least 64 bytes ...), exit 0. tools/d3d12_iid_scan.py exits 2 on the
    same input with ERROR: d3d12.h not found; pass --header.
  2. Both tools, read_iids: the MIDL regex accepts any 36-character run of hex digits and dashes, and uuid.UUID
    then raises ValueError, which the OSError-only handler in main() did not catch. Reproduction with the header line
    MIDL_INTERFACE("000000000000000000000000000000000000"): traceback ending in
    ValueError: badly formed hexadecimal UUID string, in both tools.
  3. tools/d3d12_iid_scan.py:114: except (struct.error, ValueError): return None made a parse error look like "does not
    import d3d12_xs.dll", so the "imports may be IID data" warning was lost. Reproduction with two hand-built PE32+
    stubs (a few hundred bytes, written by the test): the good stub prints imports from d3d12_xs.dll: #50 and the
    warning, exit 0; the stub with an unterminated second import name prints neither. All three defects reproduced.

What changed

  • d3d12_guid_sites.py: an explicit --header that is not a file exits 2 with ERROR: d3d12.h not found: <path>.
    A malformed GUID exits 2 with ERROR: cannot read <header>: a MIDL_INTERFACE GUID is malformed (...). The same
    malformed-GUID error is in d3d12_iid_scan.py. The default SDK header lookup is unchanged.
  • d3d12_iid_scan.py: d3d12_xs_imports keeps its return value. A new read_d3d12_xs_imports also returns the reason the
    table could not be read to its end. Names read before the error stay in the list, a one-line WARNING goes to stderr
    naming the image, and the exit code stays 0 as on main. The JSON report gets a new key,
    d3d12_xs_import_error (null when the table reads to its end). cstr now raises a named ValueError instead of a
    bare index failure.
  • tests/test_d3d12_guid_sites.py: test_cli passed a missing --header and expected exit 0. It now writes a header
    file that lists no interfaces.
  • tests/test_d3d12_guid_tools_errors.py (new): four tests on hand-made files in a temporary folder: missing header
    (both tools), malformed GUID (both tools), unreadable import table (warning, names kept, no traceback), and a good
    header with a good stub (unchanged output).

The stub layout for defect 3 differs from the one in the report. A descriptor at file offset 0x214 overlaps the name
string at 0x220, so the name is at 0x230 (RVA 0x1030) here, and the unterminated name is at RVA 0x10F8 (file 0x2F8),
inside the section. With RVA 0x1300 the name is outside the section, and the parser skips that descriptor without an
error (see below).

Tested

  • Reproduction on origin/main before the change: the outputs listed above (guid_sites exit 0; both tools traceback on
    the malformed GUID; iid_scan on the bad stub prints no imports line and no warning).
  • python3 -I tests/test_d3d12_guid_tools_errors.py: Ran 4 tests, OK.
  • The same file against the origin/main copies of the two tools (fixed copies restored afterwards): Ran 4 tests,
    4 failures, one per new defect test (missing header in guid_sites, malformed GUID in both tools, the bad-stub warning).
  • python3 -I tests/test_d3d12_guid_sites.py: Ran 10 tests, OK. python3 -I tests/test_d3d12_iid_scan.py: Ran 15
    tests, OK. python3 -I tests/test_missing_input_exit_codes.py: Ran 2 tests, OK.
  • python3 -m compileall -q tools tests: exit 0.
  • python3 -I tests/test_text_write_line_endings.py: OK.
  • python3 -I scripts/check_no_game_data.py on the four changed files: exit 0.
  • git diff --check: clean.

Not tested:

  • tests/test_d3d12_guid_sites_float4.py: it has no __main__ block, so python3 -I exits 0 without running a test.
    pytest is not installed here, so the file was not run under pytest.
  • A real d3d12.h from the Windows SDK, and the default header lookup on Windows.
  • Interaction with #487 (open, kotekompotek): it adds lines in main() just before the known block and does not touch
    the lines this PR changes. A rebase may be needed if #487 lands first.

Not changed, on purpose:

  • A name RVA outside every section (the literal RVA 0x1300) is still skipped silently. Without the name the parser
    cannot tell whether that import is d3d12_xs.dll, and reporting it would change the result for every image.
  • guid_sites still runs with no header GUIDs when the default SDK header is not found. Only an explicit --header is
    refused. Making the default an error is a behaviour change for hosts without an SDK.
  • A truncated import directory that ends between descriptors is still not warned about.
  • A file that starts with "MZ" but is shorter than a PE header now gets the import-table warning (it was silent).

AI-assisted (Claude Code).

**@⁠TuroYT** on GitHub · 2026-11-24 15:25 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/943) `TuroYT:fix/d3d12-tools-error-reporting` → `main` · +143 −10 · 4 files · 1 commits · open Claimed in the coordination issue as: D3D12-TOOLS-ERROR-REPORTING (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) ## What was wrong 1. tools/d3d12_guid_sites.py:391 skipped a `--header` that does not exist (`if header and os.path.isfile(header)`). Reproduction on main with a 2-byte image and a missing header: `0 known GUIDs from .../no_such_d3d12.h`, `skipped (unpack_from requires a buffer of at least 64 bytes ...)`, exit 0. tools/d3d12_iid_scan.py exits 2 on the same input with `ERROR: d3d12.h not found; pass --header`. 2. Both tools, `read_iids`: the MIDL regex accepts any 36-character run of hex digits and dashes, and `uuid.UUID` then raises ValueError, which the OSError-only handler in main() did not catch. Reproduction with the header line `MIDL_INTERFACE("000000000000000000000000000000000000")`: traceback ending in `ValueError: badly formed hexadecimal UUID string`, in both tools. 3. tools/d3d12_iid_scan.py:114: `except (struct.error, ValueError): return None` made a parse error look like "does not import d3d12_xs.dll", so the "imports may be IID data" warning was lost. Reproduction with two hand-built PE32+ stubs (a few hundred bytes, written by the test): the good stub prints `imports from d3d12_xs.dll: #50` and the warning, exit 0; the stub with an unterminated second import name prints neither. All three defects reproduced. ## What changed - d3d12_guid_sites.py: an explicit `--header` that is not a file exits 2 with `ERROR: d3d12.h not found: <path>`. A malformed GUID exits 2 with `ERROR: cannot read <header>: a MIDL_INTERFACE GUID is malformed (...)`. The same malformed-GUID error is in d3d12_iid_scan.py. The default SDK header lookup is unchanged. - d3d12_iid_scan.py: `d3d12_xs_imports` keeps its return value. A new `read_d3d12_xs_imports` also returns the reason the table could not be read to its end. Names read before the error stay in the list, a one-line WARNING goes to stderr naming the image, and the exit code stays 0 as on main. The JSON report gets a new key, `d3d12_xs_import_error` (null when the table reads to its end). `cstr` now raises a named ValueError instead of a bare `index` failure. - tests/test_d3d12_guid_sites.py: `test_cli` passed a missing `--header` and expected exit 0. It now writes a header file that lists no interfaces. - tests/test_d3d12_guid_tools_errors.py (new): four tests on hand-made files in a temporary folder: missing header (both tools), malformed GUID (both tools), unreadable import table (warning, names kept, no traceback), and a good header with a good stub (unchanged output). The stub layout for defect 3 differs from the one in the report. A descriptor at file offset 0x214 overlaps the name string at 0x220, so the name is at 0x230 (RVA 0x1030) here, and the unterminated name is at RVA 0x10F8 (file 0x2F8), inside the section. With RVA 0x1300 the name is outside the section, and the parser skips that descriptor without an error (see below). ## Tested - Reproduction on origin/main before the change: the outputs listed above (guid_sites exit 0; both tools traceback on the malformed GUID; iid_scan on the bad stub prints no imports line and no warning). - `python3 -I tests/test_d3d12_guid_tools_errors.py`: Ran 4 tests, OK. - The same file against the origin/main copies of the two tools (fixed copies restored afterwards): Ran 4 tests, 4 failures, one per new defect test (missing header in guid_sites, malformed GUID in both tools, the bad-stub warning). - `python3 -I tests/test_d3d12_guid_sites.py`: Ran 10 tests, OK. `python3 -I tests/test_d3d12_iid_scan.py`: Ran 15 tests, OK. `python3 -I tests/test_missing_input_exit_codes.py`: Ran 2 tests, OK. - `python3 -m compileall -q tools tests`: exit 0. - `python3 -I tests/test_text_write_line_endings.py`: OK. - `python3 -I scripts/check_no_game_data.py` on the four changed files: exit 0. - `git diff --check`: clean. Not tested: - tests/test_d3d12_guid_sites_float4.py: it has no `__main__` block, so `python3 -I` exits 0 without running a test. pytest is not installed here, so the file was not run under pytest. - A real d3d12.h from the Windows SDK, and the default header lookup on Windows. - Interaction with #487 (open, kotekompotek): it adds lines in main() just before the `known` block and does not touch the lines this PR changes. A rebase may be needed if #487 lands first. Not changed, on purpose: - A name RVA outside every section (the literal RVA 0x1300) is still skipped silently. Without the name the parser cannot tell whether that import is d3d12_xs.dll, and reporting it would change the result for every image. - guid_sites still runs with no header GUIDs when the default SDK header is not found. Only an explicit `--header` is refused. Making the default an error is a behaviour change for hosts without an SDK. - A truncated import directory that ends between descriptors is still not warned about. - A file that starts with "MZ" but is shorter than a PE header now gets the import-table warning (it was silent). AI-assisted (Claude Code).
d3d12_guid_sites ignored an explicit --header that does not exist and exited 0 with no known GUIDs.
Both tools now exit 2 with an ERROR line for a missing header and for a header with a malformed GUID.
d3d12_iid_scan keeps the d3d12_xs.dll names read before a parse error and warns on stderr instead of
returning None, so the 'imports may be IID data' warning is not lost.
New tests use hand-made files: missing header, malformed GUID, unreadable import table, and a good header.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:29:37 +00:00
Author
Owner

@⁠KernelSmoker on GitHub · 2026-11-24 15:30 UTC · original · requested changes

The new warning covers an unterminated import-name string, but the parser still silently treats other truncated tables as complete. The existing synthetic regression test_truncated_file_rva_past_eof_keeps_prior_imports in tests/test_d3d12_iid_scan.py places the second descriptor’s thunk RVA exactly at EOF after a valid #80 import. offset() returns None, the thunk loop exits normally, and read_d3d12_xs_imports() returns (["#80"], None). Consequently scan() and the CLI emit no d3d12_xs_import_error warning even though the table was not read to its end. The descriptor loop similarly exits silently if fewer than 20 bytes remain before a null descriptor.

Could you distinguish a valid terminator / absent table from an unmappable RVA or truncated descriptor/thunk sequence, and report those as incomplete while retaining already-read names? Please extend the synthetic tests to assert the CLI warning and JSON error for the existing truncated-thunk fixture (and, ideally, a truncated descriptor table). The current test suite passes, but it does not assert the new warning on these already-covered truncation cases.

**@⁠KernelSmoker** on GitHub · 2026-11-24 15:30 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/943#pullrequestreview-6042247667) · **requested changes** The new warning covers an unterminated import-name string, but the parser still silently treats other truncated tables as complete. The existing synthetic regression `test_truncated_file_rva_past_eof_keeps_prior_imports` in `tests/test_d3d12_iid_scan.py` places the second descriptor’s thunk RVA exactly at EOF after a valid `#80` import. `offset()` returns `None`, the thunk loop exits normally, and `read_d3d12_xs_imports()` returns `(["#80"], None)`. Consequently `scan()` and the CLI emit no `d3d12_xs_import_error` warning even though the table was not read to its end. The descriptor loop similarly exits silently if fewer than 20 bytes remain before a null descriptor. Could you distinguish a valid terminator / absent table from an unmappable RVA or truncated descriptor/thunk sequence, and report those as incomplete while retaining already-read names? Please extend the synthetic tests to assert the CLI warning and JSON error for the existing truncated-thunk fixture (and, ideally, a truncated descriptor table). The current test suite passes, but it does not assert the new warning on these already-covered truncation cases.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/943/head:github-bot/gh-943'
git switch 'github-bot/gh-943'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-943'
git switch 'github-bot/gh-943'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-943'
git switch 'github-bot/gh-943'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-943'
git switch 'main'
git merge --squash 'github-bot/gh-943'
git switch 'main'
git merge --ff-only 'github-bot/gh-943'
git switch 'main'
git merge 'github-bot/gh-943'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.