[PR #1345] [MERGED] pe_inspect: resolve legacy VA delay-import thunks and skip ImageBase DLL name (PE-INSPECT-DELAY-VA-THUNKS) #1375

Closed
opened 2026-11-26 20:21:02 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1345
Author: @IlyassRS3
Created: 11/24/2026
Status: ✅ Merged
Merged: 11/25/2026
Merged by: @QemmHD

Base: main ← Head: fix/pe-inspect-delay-va-thunks


📝 Commits (1)

  • 3a7b8b2 pe_inspect: resolve legacy VA delay-import thunks and skip ImageBase DLL name

📊 Changes

2 files changed (+25 additions, -4 deletions)

View changed files

📝 tests/test_pe_inspect.py (+21 -1)
📝 tools/pe_inspect.py (+4 -3)

📄 Description

Item ID: PE-INSPECT-DELAY-VA-THUNKS
AI-assisted: yes

Summary

Fixes two bugs in tools/pe_inspect.py:_imports for legacy VA-based delay-import descriptors (dlattrRva == 0):

  1. Unused _va_entries broke named function resolution in dlattrRva == 0 delay-import tables:
    • PeImage.import_descriptors computes va_entries = True when attributes & 1 == 0 (dlattrRva clear, per the PE/COFF v1 ImgDelayDescr format where szName, pIAT, pINT, and the named lookup entries in pINT all store virtual addresses), and PeImage.thunk_names(lookup_rva, *, va_entries=False) already supports converting those VAs via value -= self.image_base (used by tools/import_call_sites.py:68).
    • However, tools/pe_inspect.py:_imports unpacked _va_entries and dropped it, calling img.thunk_names(lookup) with va_entries=False, so every named delay-import entry in a dlattrRva == 0 image (including tests/test_import_call_sites.py's own build_delay_image(attributes=0) fixture) resolved as "?".
    • Fixed by passing va_entries=va_entries to img.thunk_names(lookup, va_entries=va_entries).
  2. Legacy VA descriptor with szName == ImageBase (dll_rva == 0) read "MZ" from the DOS header:
    • Guarded dll = img.string_at(dll_rva) if dll_rva > 0 else None so a descriptor pointing at ImageBase is skipped (matching tools/import_call_sites.py).
  3. Tests:
    • Updated the legacy dlattrRva == 0 delay-import lookup slot in tests/test_pe_inspect.py:build_image(is64=False) to store a spec-compliant virtual address (base + 0x1440, matching lines 119–120 and tools/pe_inspect.py:149), and added test_delay_import_va_descriptor_skips_image_base_dll_and_handles_ordinals.

Merge compatibility

  • Leaves lines 242–245 untouched (3-line buffer before #693's _exports edit) and keeps test additions at lines 124 and 232 (away from #384, #649, #653, #693, #698).

Verification

Tested on Apple M5 arm64 (macOS 27.2.1, Python 3.14; CPU-only, no game files):

  • python3 -I tests/test_pe_inspect.py -v — 61 tests (60 ok, 1 Windows-only skipped)
  • python3 -I tests/test_import_call_sites.py -v — 21/21 OK
  • Mutation proof: reverting tools/pe_inspect.py to upstream/main fails both VA delay-import tests in tests/test_pe_inspect.py (['?'] != ['D3D12SerializeVersionedRootSignature']).
  • python3 -m compileall -q tools/pe_inspect.py tests/test_pe_inspect.py — OK
  • python3 -I scripts/check_no_game_data.py tools/pe_inspect.py tests/test_pe_inspect.py — OK
  • python3 -I tests/test_text_write_line_endings.py — 3/3 OK

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1345 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/24/2026 **Status:** ✅ Merged **Merged:** 11/25/2026 **Merged by:** [@QemmHD](https://github.com/QemmHD) **Base:** `main` ← **Head:** `fix/pe-inspect-delay-va-thunks` --- ### 📝 Commits (1) - [`3a7b8b2`](https://github.com/ubiquitarianism/leonida/commit/3a7b8b219056c4d8251d36fab388b61e9f061c22) pe_inspect: resolve legacy VA delay-import thunks and skip ImageBase DLL name ### 📊 Changes **2 files changed** (+25 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_pe_inspect.py` (+21 -1) 📝 `tools/pe_inspect.py` (+4 -3) </details> ### 📄 Description Item ID: `PE-INSPECT-DELAY-VA-THUNKS` AI-assisted: yes ## Summary Fixes two bugs in `tools/pe_inspect.py:_imports` for legacy VA-based delay-import descriptors (`dlattrRva == 0`): 1. **Unused `_va_entries` broke named function resolution in `dlattrRva == 0` delay-import tables**: - `PeImage.import_descriptors` computes `va_entries = True` when `attributes & 1 == 0` (`dlattrRva` clear, per the PE/COFF v1 `ImgDelayDescr` format where `szName`, `pIAT`, `pINT`, and the named lookup entries in `pINT` all store virtual addresses), and `PeImage.thunk_names(lookup_rva, *, va_entries=False)` already supports converting those VAs via `value -= self.image_base` (used by `tools/import_call_sites.py:68`). - However, `tools/pe_inspect.py:_imports` unpacked `_va_entries` and dropped it, calling `img.thunk_names(lookup)` with `va_entries=False`, so every named delay-import entry in a `dlattrRva == 0` image (including `tests/test_import_call_sites.py`'s own `build_delay_image(attributes=0)` fixture) resolved as `"?"`. - Fixed by passing `va_entries=va_entries` to `img.thunk_names(lookup, va_entries=va_entries)`. 2. **Legacy VA descriptor with `szName == ImageBase` (`dll_rva == 0`) read `"MZ"` from the DOS header**: - Guarded `dll = img.string_at(dll_rva) if dll_rva > 0 else None` so a descriptor pointing at `ImageBase` is skipped (matching `tools/import_call_sites.py`). 3. **Tests**: - Updated the legacy `dlattrRva == 0` delay-import lookup slot in `tests/test_pe_inspect.py:build_image(is64=False)` to store a spec-compliant virtual address (`base + 0x1440`, matching lines 119–120 and `tools/pe_inspect.py:149`), and added `test_delay_import_va_descriptor_skips_image_base_dll_and_handles_ordinals`. ## Merge compatibility - Leaves lines 242–245 untouched (3-line buffer before `#693`'s `_exports` edit) and keeps test additions at lines 124 and 232 (away from `#384`, `#649`, `#653`, `#693`, `#698`). ## Verification Tested on Apple M5 arm64 (macOS 27.2.1, Python 3.14; CPU-only, no game files): - `python3 -I tests/test_pe_inspect.py -v` — 61 tests (60 ok, 1 Windows-only skipped) - `python3 -I tests/test_import_call_sites.py -v` — 21/21 OK - Mutation proof: reverting `tools/pe_inspect.py` to `upstream/main` fails both VA delay-import tests in `tests/test_pe_inspect.py` (`['?'] != ['D3D12SerializeVersionedRootSignature']`). - `python3 -m compileall -q tools/pe_inspect.py tests/test_pe_inspect.py` — OK - `python3 -I scripts/check_no_game_data.py tools/pe_inspect.py tests/test_pe_inspect.py` — OK - `python3 -I tests/test_text_write_line_endings.py` — 3/3 OK --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:21:02 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#1375
No description provided.