Cache System32 PE export tables across static intake scans #137

Merged
PrivacyOnion merged 1 commit from upstream-pr/138 into main 2026-11-21 17:56:04 +00:00
PrivacyOnion commented 2026-11-21 16:53:15 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by PrivacyOnion on 2026-11-21T16:53:15Z.


## Change

  1. tools/pe_inventory.py:
    • Decorate api_sets(system) with @cache and move System32 DLL discovery (_system_images(system)) and per-DLL export-table parsing (_parsed_system_exports(path)) to module-level @cache functions instead of defining @cache def system_exports(name) inside scan().
    • Avoids re-enumerating System32/*.dll and re-parsing the export directories of kernel32.dll, ntdll.dll, and ucrtbased.dll (~13,500 exports combined) on every scan() call within the same Python process.
  2. tests/intake/check.py:
    • Keep the first intake_exe.py invocation as a subprocess.run CLI check and run the remaining verification passes in-process via intake_exe.main(cli_args) so the cached System32 export tables are reused across all 9 intake passes (static_exe_intake drops from 6.41s to 1.36s).

Verification

.\tools\dev.ps1
ctest --preset dev -R "^(conditional_import_paths|static_exe_intake)$" --no-tests=error -V
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6531714443 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/138). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-21T16:53:15Z. --- ## Change 1. **`tools/pe_inventory.py`**: - Decorate `api_sets(system)` with `@cache` and move `System32` DLL discovery (`_system_images(system)`) and per-DLL export-table parsing (`_parsed_system_exports(path)`) to module-level `@cache` functions instead of defining `@cache def system_exports(name)` inside `scan()`. - Avoids re-enumerating `System32/*.dll` and re-parsing the export directories of `kernel32.dll`, `ntdll.dll`, and `ucrtbased.dll` (~13,500 exports combined) on every `scan()` call within the same Python process. 2. **`tests/intake/check.py`**: - Keep the first `intake_exe.py` invocation as a `subprocess.run` CLI check and run the remaining verification passes in-process via `intake_exe.main(cli_args)` so the cached `System32` export tables are reused across all 9 intake passes (`static_exe_intake` drops from `6.41s` to `1.36s`). ## Verification ```powershell .\tools\dev.ps1 ctest --preset dev -R "^(conditional_import_paths|static_exe_intake)$" --no-tests=error -V ```
upstream-sync closed this pull request 2026-11-24 06:41:30 +00:00
R0Y00 commented 2026-11-24 06:47:41 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T17:17:46Z.


Caching export tables by path across scan() calls makes a second inventory stale when a DLL is replaced at the same path or the directory gains a DLL. Please scope the cache to an explicit inventory session or invalidate it on changes, and cover two scans of a modified synthetic directory.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7066367082 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/138#issuecomment-7066367082). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T17:17:46Z. --- Caching export tables by path across scan() calls makes a second inventory stale when a DLL is replaced at the same path or the directory gains a DLL. Please scope the cache to an explicit inventory session or invalidate it on changes, and cover two scans of a modified synthetic directory.
PrivacyOnion commented 2026-11-24 06:47:44 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by PrivacyOnion on 2026-11-21T17:54:10Z.


Rebased onto main and updated tools/pe_inventory.py and tests/intake/check.py so the cache invalidates automatically on directory or file changes:

  1. Fresh directory enumeration per scan():
    • system_images = {p.name.lower(): p for p in system.glob("*.dll")} is enumerated freshly on each scan() call, so any DLL added to or removed from the directory is visible immediately on the next scan().
  2. Content-digest cache key ((str(path), _file_digest(path))):
    • _parsed_api_sets and _parsed_system_exports key on (str(path), sha256_digest), so replacing or modifying a DLL in place at the exact same path invalidates its cached export table even within the same filesystem timestamp tick.
    • Within a single scan() session, @cache def system_exports(name) hashes and looks up each queried DLL at most once.
  3. Two-scan modified synthetic directory check (check_modified_directory):
    • Scans a synthetic directory (synth_root and synth_system) where intake_provider.dll initially has named_value patched out (export_not_found) and intake_delay.dll is absent (provider_not_found), then overwrites intake_provider.dll in place at the same path and adds intake_delay.dll before a second scan(), verifying that both changes are reflected (export_present and bundle_exports_in).
1/3 Test #80: conditional_import_paths .........   Passed    0.15 sec
2/3 Test #81: static_exe_intake ................   Passed    1.31 sec
3/3 Test #82: bundled_export_forwarders ........   Passed    0.33 sec
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7066844326 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/138#issuecomment-7066844326). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-21T17:54:10Z. --- Rebased onto `main` and updated `tools/pe_inventory.py` and `tests/intake/check.py` so the cache invalidates automatically on directory or file changes: 1. **Fresh directory enumeration per `scan()`**: - `system_images = {p.name.lower(): p for p in system.glob("*.dll")}` is enumerated freshly on each `scan()` call, so any DLL added to or removed from the directory is visible immediately on the next `scan()`. 2. **Content-digest cache key (`(str(path), _file_digest(path))`)**: - `_parsed_api_sets` and `_parsed_system_exports` key on `(str(path), sha256_digest)`, so replacing or modifying a DLL in place at the exact same path invalidates its cached export table even within the same filesystem timestamp tick. - Within a single `scan()` session, `@cache def system_exports(name)` hashes and looks up each queried DLL at most once. 3. **Two-scan modified synthetic directory check (`check_modified_directory`)**: - Scans a synthetic directory (`synth_root` and `synth_system`) where `intake_provider.dll` initially has `named_value` patched out (`export_not_found`) and `intake_delay.dll` is absent (`provider_not_found`), then overwrites `intake_provider.dll` in place at the same path and adds `intake_delay.dll` before a second `scan()`, verifying that both changes are reflected (`export_present` and `bundle_exports_in`). ```text 1/3 Test #80: conditional_import_paths ......... Passed 0.15 sec 2/3 Test #81: static_exe_intake ................ Passed 1.31 sec 3/3 Test #82: bundled_export_forwarders ........ Passed 0.33 sec ```
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!137
No description provided.