Skip to content

fix(tools): accept PE libraries without export tables - #10618

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
xTRACKGOD:fix/import-audit-empty-pe-exports
Oct 26, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
xTRACKGOD:fix/import-audit-empty-pe-exports

Conversation

@xTRACKGOD

Copy link
Copy Markdown
Contributor

What

import_audit.py aborts with PE file has no export table when a valid library has a zero export-directory address and size. Such a library can still import host dependencies and needs to participate in the audit. An independently compiled DLL with no exports loads successfully through Windows but fails the current reader.

After the existing PE header checks, return an empty export set when both directory fields are zero. Preserve errors for partial or malformed directories and keep dependency auditing active. Tests cover both PE32/PE32+ directory layouts, built-library and guest-module scans, absent requested NIDs and missing transitive host dependencies.

Tested

Windows x86-64, Python 3.12.14, CMake 4.3.1, WinLibs GCC 15.2.0; baseline main 8ec6397b3762fa23f90a6973d223b12553e420a5:

  • Four tests ported from the separate queued fix demonstrate the current-main failure: valid PE32/PE32+ layouts raise reader errors, and both real CLI scenarios abort with exit 2. The malformed-directory control already passes. All four tests pass after the two-line production change.
  • Compiled a real DLL with gcc -shared -Wl,--exclude-all-symbols; its export-directory pair is (0, 0). Windows LoadLibrary through ctypes.WinDLL succeeds. Main raises AuditError; this change returns an empty set while retaining all six imported host-library names. A separate root-level harness independently confirms the same result.
  • ctest --test-dir build-relinker -R '^(import_audit|test_script_registration)$' --output-on-failure -V: 2/2 passed. The audit suite ran 140 tests: 137 passed and 3 platform/privilege tests skipped. Audit CTest took 33.43 seconds on this host; all 175 Python test scripts are registered. Existing hardlink-protection tests ran successfully outside sandbox restrictions.
  • Negative controls retain errors for zero/nonzero partial directory pairs, undersized tables and unmapped addresses. CLI checks still report missing requested exports and missing host dependencies instead of treating an empty export set as successful resolution.
  • Conventions, PR title/body validation and git diff --check origin/main...HEAD pass. The older queued worktree and its uncommitted source/tests remain untouched.
  • tools/pr_overlap.py --branch fix/import-audit-empty-pe-exports checked 1,249 open PRs. It found no confirmed overlap. Two base-conflicted comparisons were manually checked at exact heads: fix(tools): recognize guest module identities in import audit #6171 (35bf15c510cb50ee8152d911c686e5eccdb95c15) and fix(tools): resolve $ORIGIN-prefixed converted module dependencies in import_audit #7454 (11840ad87c4065873cb2923b2367232d33bbe5ee). Paired three-way merges preserve every existing conflict block and add none; their behavior changes concern module identities and $ORIGIN dependency matching. Neither changes PE export parsing. No dependency. The unmodified repository checker used a local adapter for public GitHub pagination because gh is unavailable.

No title, console hardware, GPU or full system-library build was tested.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed exact head 5d6bc58bd028773973e25d8676a02461f6cc9e3c. pe_exports now returns an empty export set only when both export-directory fields are zero; partial, undersized, and unmapped directories still raise AuditError. The added tests cover both PE32 and PE32+, absence of requested NIDs, guest-module/library scans, and continued reporting of missing transitive host dependencies. On the exact head, python3 -m unittest tools.tests.test_import_audit passed (140 tests, 1 skipped), conventions passed, and git diff --check upstream/main...HEAD passed. Hosted eligibility/intake/conventions/labels pass; Linux relinker and ASan pass, while Windows/macOS relinker jobs remain in progress/queued. Those matrix jobs do not exercise the Python audit behavior; I did not run the Windows DLL integration test locally.

@github-actions

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 6171 tools/import_audit.py
tools/tests/test_import_audit.py
PR 7454 tools/import_audit.py
tools/tests/test_import_audit.py

@roboterspinne roboterspinne left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed head 5d6bc58 merged onto upstream/main dc9f1ef (2026-10-24); the merge is clean, also together with #10560 (same two files, no conflict), check_conventions.py passes and pr_overlap.py lists only the two base-conflicted PRs (#6171, #7454) the bot already names.

Environment: Windows 11 Home 10.0.26200, Python 3.13.9 (Anaconda), WinLibs GCC 15.2.0 for the DLL probe; Python only, no build needed.

Results

  • python -m unittest tools.tests.test_import_audit on the merged tree: 140 tests, 137 pass, 3 skipped, 7.7 s. Red/green: against main's import_audit.py the two reader tests error with PE file has no export table and both CLI tests fail (the audit exits 2 with that message instead of 1 or 0); the malformed-directory control passes on main as the body says; all four pass on the branch.
  • Real DLL: gcc -shared -Wl,--exclude-all-symbols on a source without __declspec(dllexport) gives an export data directory of rva 0, size 0; ctypes.WinDLL loads it; main's read_exports raises PE file has no export table, the branch returns an empty set. A DLL with one export (rva 0x9000, size 75) reads ['exported'] on both. The exportless DLL still imports KERNEL32.dll and the UCRT API sets, so the dependency audit has something to check on it.
  • The new return sits after pe_headers has validated the signature, the optional-header magic and size, the data-directory count and the section table, so a truncated or malformed header still raises before it; rva 0, size != 0 and rva != 0, size < 40 keep the old error.

Nits

  • test_import_audit.py:1559: the malformed-directory control uses a bare assertRaises(AuditError), so it would still pass if the fixture started failing earlier in pe_headers. Today (0, 40), (0x1000, 0) and (0x1000, 39) raise PE file has no export table and (0xffffff, 40) raises RVA 0xffffff is not inside any section, for both magics, on main and on the branch; assertRaisesRegex(import_audit.AuditError, "no export table|not inside any section") would pin the intended failure and passes on both.
  • test_import_audit.py:1542-1549: the PE32 subtest of the positive test does not pin the PE32 directory offset: build_pe([]) leaves a valid empty export entry at 0x58 + 112, so the subtest would also pass with PE_DIRECTORY_OFFSET[0x10b] wrongly set to 112. Only the malformed control's magic=0x10b cases catch that; writing a malformed entry such as (0x1000, 39) at 0x58 + 112 in the positive fixture would make the PE32 half self-sufficient. Optional.

Notes, nothing to change here: the PE/COFF data-directory text never defines a "no table" encoding; the Windows loader treats VirtualAddress 0 as absent whatever Size says (a patched (0, 40) DLL loads and GetProcAddress fails with ERROR_PROC_NOT_FOUND), while the branch keeps rejecting it and pe_imports in the same file treats rva 0 as absent regardless of size; no linker I could check emits (0, n), so accepting only (0, 0) is a defensible choice, just stricter than the loader. (0x1000, 0) is rightly still an error, the loader follows that RVA into garbage. The guest-module half of test_audit_accepts_pe_libraries_and_modules_without_export_directories shows that read_modules no longer raises, not that module dependencies are audited: read_modules records ([], []) for every PE module's imports, as on main. The body's 33.43 s for the audit ctest is the author's host; here the suite takes 7.7 s and CI 0.8 to 8.4 s.

Not checked: an MSVC- or lld-linked exportless DLL, macOS and Linux runs, the body's pr_overlap adapter and its manual three-way merges with #6171 and #7454.

I found no blocking issue: (0, 0) is what GNU ld writes for an exportless DLL and what Windows loads, the return sits after every header check, an exportless library stays present for the dependency audit and cannot satisfy a NID, elf_exports already behaves the same way for an ELF without SHT_DYNSYM, and the counter-check holds for all four tests. For disclosure: I have open PRs (#10667 on tools/tests/test_suggest_patches.py, and relinker PRs #10658, #10646, #10607, #10596, #10564, #10403, #10402, #10401, #10391, #10390); none touches these files.

Written with AI assistance (Claude Code). I ran the tests and the DLL probe above on my Windows 11 machine.

struct.pack_into("<I", image, 0x58 + directory - 4, 16)
struct.pack_into("<II", image, 0x58 + directory, rva, size)
with self.subTest(magic=magic, rva=rva, size=size):
with self.assertRaises(import_audit.AuditError):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: a bare assertRaises also passes if the fixture starts failing earlier in pe_headers. Today the three partial pairs raise PE file has no export table and (0xffffff, 40) raises RVA 0xffffff is not inside any section, on both magics, on main and here; assertRaisesRegex(import_audit.AuditError, "no export table|not inside any section") pins the intended failure and passes on both.

struct.pack_into("<I", image, 0x58 + directory - 4, 16)
struct.pack_into("<II", image, 0x58 + directory, 0, 0)
with self.subTest(magic=magic):
self.assertEqual(import_audit.read_exports(Path("empty.prx"), image), set())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, optional: the magic=0x10b subtest would also pass with PE_DIRECTORY_OFFSET[0x10b] wrongly set to 112, because build_pe([]) leaves a valid empty export entry at 0x58 + 112; only the malformed control pins the PE32 offset. A malformed entry such as (0x1000, 39) at 0x58 + 112 in this fixture would make the PE32 half catch that on its own.

@THEMANnt THEMANnt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 5d6bc58bd028 against pinned base 6cd36e651724. No actionable findings in this focused review.

Source review

pe_exports now returns an empty set only when both export-directory fields are zero, after the existing header checks. Partial entries, undersized tables, and unmapped addresses still fail. Empty exports preserve library presence, while dependency auditing runs separately. The tests exercise both PE directory layouts, converted guest modules, absent requested NIDs, and missing host dependencies through the real audit entry point with isolated fixtures.

Validation

Controller-recorded tools/tests/test_import_audit.py execution on Windows, Python 3.11.9:

  • Baseline: 133 passed, 3 skipped; 136 total.
  • Pristine PR and candidate: each 137 passed, 3 skipped; 140 total. No local amendments were applied.
  • Old-code control: two CLI assertion failures showed the previous exit-2 rejection; two reader subtest errors raised the same AuditError. The control stops before the later missing-NID assertions, which pass on the candidate.
  • Recorded whitespace and conventions checks passed.

GitHub reports success for relinker-asan, the listed Windows/Ubuntu/macOS relinker jobs, eligibility, conventions, labels, and intake. write-watch, linux, windows, and register-context were skipped.

Scope

Validation covers the Python tool; full application behavior was not tested. The author's compiled-DLL/LoadLibrary and Python 3.12.14/CTest checks were not independently reproduced.

@oneandonlydean oneandonlydean self-assigned this Oct 25, 2026
@oneandonlydean
oneandonlydean merged commit a49a39d into boykopovar:main Oct 26, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants