tools/import_call_sites: bounds-check ordinary IAT slots and parse PE once in main #1855

Closed
kirigayakazima wants to merge 1 commit from upstream-pr/1855 into main
kirigayakazima commented 2026-11-25 13:13:32 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kirigayakazima on 2026-11-25T13:13:32Z.


Claimed in the coordination issue as: IMPORT-CALL-SITES-ORDINARY-IAT-BOUNDS-AND-SINGLE-PARSE (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7163426538)

What and why

  • Guard ordinary PE32+ import descriptors in tools/import_call_sites.py's import_entries() against zero or unmapped iat_rva / lookup RVAs (img.offset(..., 8)) and stop slot enumeration if img.offset(slot, 8) is None, matching the existing delay-import slot validation.
  • Extract _call_sites_from_image() and _unmatched_from_entries() so main() parses pe_inspect.PeImage and import_entries(img) once per CLI run instead of twice (while keeping public call_sites() and unmatched_selectors() signatures unchanged).
  • Add regression tests in tests/test_import_call_sites.py.

Tested

  • python -I tests/test_import_call_sites.py -v -> Ran 24 tests in 0.106s, OK.
  • Detached upstream/main (98c2f777) baseline worktree with updated tests/test_import_call_sites.py -> Ran 24 tests in 0.090s, FAILED (failures=4).
  • python -I tests/test_ci_registration.py -> Ran 16 tests in 6.127s, OK.
  • python -I tests/test_text_write_line_endings.py -> Ran 3 tests in 14.536s, OK.
  • python -I scripts/check_no_game_data.py tools/import_call_sites.py tests/test_import_call_sites.py, python -I -m compileall -q tools/import_call_sites.py tests/test_import_call_sites.py, and git diff --check -> exit 0.

GPUs tested: n/a (CPU/stdlib Python tests on Windows 11 x64)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5492020201 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1855). Originally posted by [kirigayakazima](https://github.com/kirigayakazima) on 2026-11-25T13:13:32Z. --- Claimed in the coordination issue as: IMPORT-CALL-SITES-ORDINARY-IAT-BOUNDS-AND-SINGLE-PARSE (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7163426538) ### What and why - Guard ordinary PE32+ import descriptors in `tools/import_call_sites.py`'s `import_entries()` against zero or unmapped `iat_rva` / `lookup` RVAs (`img.offset(..., 8)`) and stop slot enumeration if `img.offset(slot, 8) is None`, matching the existing delay-import slot validation. - Extract `_call_sites_from_image()` and `_unmatched_from_entries()` so `main()` parses `pe_inspect.PeImage` and `import_entries(img)` once per CLI run instead of twice (while keeping public `call_sites()` and `unmatched_selectors()` signatures unchanged). - Add regression tests in `tests/test_import_call_sites.py`. ### Tested - `python -I tests/test_import_call_sites.py -v` -> `Ran 24 tests in 0.106s`, `OK`. - Detached `upstream/main` (`98c2f777`) baseline worktree with updated `tests/test_import_call_sites.py` -> `Ran 24 tests in 0.090s`, `FAILED (failures=4)`. - `python -I tests/test_ci_registration.py` -> `Ran 16 tests in 6.127s`, `OK`. - `python -I tests/test_text_write_line_endings.py` -> `Ran 3 tests in 14.536s`, `OK`. - `python -I scripts/check_no_game_data.py tools/import_call_sites.py tests/test_import_call_sites.py`, `python -I -m compileall -q tools/import_call_sites.py tests/test_import_call_sites.py`, and `git diff --check` -> exit `0`. GPUs tested: n/a (CPU/stdlib Python tests on Windows 11 x64) AI-assisted: yes ### Checklist - [x] Based on current main and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a)
madvillain91 commented 2026-11-25 13:30:11 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by madvillain91 on 2026-11-25T13:30:11Z.

Review: COMMENTED


Independent Windows 11 26H2 (26300.9711) check at 9a936d9e8 against main 384abe5b0 (PR base 98c2f7777), using CPython 3.12.3 (python -I) and Git for Windows 2.56.0.windows.2. Passed test_import_call_sites.py (24/24 in 0.215s) and test_text_write_line_endings.py (3/3 in 19.667s), with check_no_game_data.py and compileall clean. Verified ordinary IAT descriptors with zero/unmapped RVAs are skipped, truncated IAT tables keep only whole mapped 8-byte slots, and main() parses the PE image and import entries once via _call_sites_from_image/_unmatched_from_entries while keeping public call_sites() and unmatched_selectors() intact. LGTM.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6058506026 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1855#pullrequestreview-6058506026). Originally posted by [madvillain91](https://github.com/madvillain91) on 2026-11-25T13:30:11Z. **Review:** COMMENTED --- Independent Windows 11 26H2 (26300.9711) check at 9a936d9e8 against main 384abe5b0 (PR base 98c2f7777), using CPython 3.12.3 (python -I) and Git for Windows 2.56.0.windows.2. Passed test_import_call_sites.py (24/24 in 0.215s) and test_text_write_line_endings.py (3/3 in 19.667s), with check_no_game_data.py and compileall clean. Verified ordinary IAT descriptors with zero/unmapped RVAs are skipped, truncated IAT tables keep only whole mapped 8-byte slots, and main() parses the PE image and import entries once via _call_sites_from_image/_unmatched_from_entries while keeping public call_sites() and unmatched_selectors() intact. LGTM.
DeadlyChirp commented 2026-11-25 15:37:38 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by DeadlyChirp on 2026-11-25T15:37:38Z.


Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main.

python -m pytest -q tests/test_import_call_sites.py:

  • PR: 24 passed. main: 21 passed. The new tests pass on Windows.

(AI-assisted test run; output checked by hand.)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7167133257 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1855#issuecomment-7167133257). Originally posted by [DeadlyChirp](https://github.com/DeadlyChirp) on 2026-11-25T15:37:38Z. --- Tested on Windows 11, Python 3.12.10, with this PR merged onto current upstream main. `python -m pytest -q tests/test_import_call_sites.py`: - PR: 24 passed. main: 21 passed. The new tests pass on Windows. (AI-assisted test run; output checked by hand.)
aslan2142 commented 2026-11-25 17:27:32 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-11-25T17:27:32Z.


Thanks. The new bounds check copies the existing delay-import check, but it only fires on a malformed executable: an image that loads has mapped IATs. The single-parse change saves one header parse.

Can you name a real input that reaches the new guard? Otherwise we'd rather not take the extra code.

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7169532795 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1855#issuecomment-7169532795). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-11-25T17:27:32Z. --- Thanks. The new bounds check copies the existing delay-import check, but it only fires on a malformed executable: an image that loads has mapped IATs. The single-parse change saves one header parse. Can you name a real input that reaches the new guard? Otherwise we'd rather not take the extra code. AI-assisted (Claude Code, Claude Opus 5.5).
MSedgeMC commented 2026-11-25 17:47:26 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MSedgeMC on 2026-11-25T17:47:26Z.

Review: APPROVED


Verified on the PR branch: python3 -m unittest tests.test_import_call_sites -v -> 24 tests OK, including the new cases for zero/unmapped ordinary IAT, truncated IAT keeping only whole mapped slots, and parsing the PE image once in the CLI. These guard against reading out-of-range IAT slots from malformed PE data. Python-only, no game data.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6062329087 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1855#pullrequestreview-6062329087). Originally posted by [MSedgeMC](https://github.com/MSedgeMC) on 2026-11-25T17:47:26Z. **Review:** APPROVED --- Verified on the PR branch: python3 -m unittest tests.test_import_call_sites -v -> 24 tests OK, including the new cases for zero/unmapped ordinary IAT, truncated IAT keeping only whole mapped slots, and parsing the PE image once in the CLI. These guard against reading out-of-range IAT slots from malformed PE data. Python-only, no game data.
kirigayakazima commented 2026-12-01 11:12:53 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kirigayakazima on 2026-12-01T11:12:53Z.


Thank you @aslan2142, @madvillain91, @MSedgeMC, and @DeadlyChirp for the reviews and verification! Since there is no real loadable executable reproducing malformed ordinary IATs, closing this PR cleanly per @aslan2142's preference not to take the extra guard code. (AI-assisted.)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7309328232 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1855#issuecomment-7309328232). Originally posted by [kirigayakazima](https://github.com/kirigayakazima) on 2026-12-01T11:12:53Z. --- Thank you @aslan2142, @madvillain91, @MSedgeMC, and @DeadlyChirp for the reviews and verification! Since there is no real loadable executable reproducing malformed ordinary IATs, closing this PR cleanly per @aslan2142's preference not to take the extra guard code. (AI-assisted.)

Pull request closed

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
ProjectLeonida/mandy!1855
No description provided.