Skip to content

import_call_sites: detect direct IAT call [rip+disp32] (FF 15) sites - #7116

Open
sanchaton wants to merge 1 commit into
ItsNotRexed:mainfrom
sanchaton:feat/import-call-sites-direct-iat
Open

sanchaton wants to merge 1 commit into
ItsNotRexed:mainfrom
sanchaton:feat/import-call-sites-direct-iat

Conversation

@sanchaton

Copy link
Copy Markdown

Item: RT-IMPORT-CALL-SITES-IAT (claimed on #7049)

Summary

tools/import_call_sites.py previously only matched thunk-routed calls (call/jmp rel32 to jmp qword [rip+disp32] / FF 25 thunks). MSVC __declspec(dllimport) callers without a thunk emit direct IAT calls (call qword [rip+disp32], opcode FF 15 disp32) targeting the IAT slot directly.

This PR adds direct IAT call detection alongside the existing thunk-routed tracking:

  • Extracts _rip_slot_sites(code, pattern, slots) in tools/import_call_sites.py so both JMP_INDIRECT (FF 25) thunks and CALL_INDIRECT (FF 15) direct IAT calls share one decoding path.
  • Records direct IAT call RVAs in a sorted iat_call list per import entry in call_sites() (automatically exported as hex RVAs in --json mode).
  • Adds an iat column to the summary table, includes iat_call in row sorting and the call site(s) total, while leaving thunks, call, and jmp unchanged for existing consumers.
  • Leaves if __name__ == __main__: untouched to stay clean alongside import_call_sites, world_drawable_lookup, world_drawable_verify: guard stdout like every other tool #7032.
  • Adds unit tests in tests/test_import_call_sites.py covering direct IAT calls without thunks, out-of-IAT slot filtering, CLI table output, and JSON output.

Verification (AI-assisted)

Environment: Windows 11, Python 3.14 (python -I), NVIDIA GeForce RTX 5060 Laptop GPU (Blackwell, driver 617.14).

  • python -I tests/test_import_call_sites.py -v: 10/10 passed (0 failed).
  • python -m compileall -q make_game.py tools tests: exit 0.
  • python -I make_game.py --help: exit 0.
  • python -I scripts/check_no_game_data.py: exit 0.
  • git diff --check: clean.
  • Counts-only check on local retail game_scarlett_master.exe (no game data committed or quoted): out of 587 imports, total detected call sites increased from 35,572 to 39,016 (+3,444 direct FF 15 IAT sites across 336 imports, including 334 imports that previously reported 0 call sites — e.g. 33 across Bink2Scarlett.dll and 25 across XboxBvhBuilder.dll).

@kapitche

Copy link
Copy Markdown
Contributor

Tested d06f36a merged with main 8575d36 on Linux/Python 3.12.16: pass

Merge was clean (tools/import_call_sites.py, tests/test_import_call_sites.py).

  • python -I tests/test_import_call_sites.py: Ran 10 tests, OK.
  • Negative control (tools/import_call_sites.py restored from upstream/main, tests from the PR): Ran 10 tests, FAILED (failures=1, errors=1): test_counts_direct_iat_calls_without_a_thunk errors and test_cli_table_and_json_include_direct_iat_calls fails. PR version restored afterwards.

Diff read: the thunk scan is factored into _rip_slot_sites and reused for FF 15 disp32 calls through an IAT slot; the new iat_call list is included in the per-import dict, the table column and the total. The test covers a direct slot call for two imports, a call through a slot outside the IAT (ignored), the ordinal import with none, and the table and JSON output. The only other users of the tool in the tree are docs (docs/xmem_exe_calls.md, docs/audio_hal_exe_calls.md, docs/README.md); no code consumes the JSON.

@gut0to

gut0to commented Nov 23, 2026

Copy link
Copy Markdown

Independent Windows validation of d06f36a:

  • python -I tests/test_import_call_sites.py -v: 10/10 passed.
  • python -I scripts/check_no_game_data.py: passed.
  • compileall and git diff --check: passed.

Reviewed the shared RIP-relative slot helper: both FF 25 thunk discovery and FF 15 direct-call discovery use the instruction end (site + 6) plus signed disp32, and direct calls outside selected IAT slots stay filtered. Existing call/jmp lists and totals remain separate; iat_call is added to the display, JSON and total as intended. Synthetic PE inputs only. AI-assisted.

@devnull34 devnull34 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verified on Windows 11:

  • python -m unittest tests/test_import_call_sites.py passed all 10 tests (0.051s).
  • Validated direct IAT call opcode FF 15 [rip+disp32] scanning alongside existing thunk call/jmp forms.
  • Whitespace clean (git diff HEAD~1..HEAD --check).

@leozinhoszg

Copy link
Copy Markdown
Contributor

Tested d06f36af on Windows 11 Pro (26200), CPython 3.14.7, CPU only, no game files: pass with notes.

  • python -I tests/test_import_call_sites.py: 10/10 pass. compileall and git diff --check are clean. I also ran synthetic PE32+ probes with .idata placed before .text and raw offsets that differ from RVAs.
  • Negative control: with main's tools/import_call_sites.py, both new tests fail (KeyError: 'iat_call', and the table has no iat column).
  1. A tail call through the slot is still counted as a thunk. I put 48 FF 25 disp32 (REX.W jmp qword [rip+disp32]) at .text+0x50. It lands in thunks (0x3051), in no site column, and is missing from the call site(s) total (tools/import_call_sites.py:91). MSVC x64 uses this REX.W form for a tail call to a dllimport function, because the unwinder only accepts a REX.W indirect jmp as an epilogue exit. Linker import thunks are plain FF 25. So after this PR, thunks mixes real thunks with direct tail jumps. I could not count these on the real executable (no game files).
  2. With a REX prefix, the reported site is off by one byte. 48 FF 15 at 0x3020 and 41 FF 15 at 0x3030 hit the right slot but are reported at 0x3021 and 0x3031, the FF byte (tools/import_call_sites.py:43-44). The target is computed correctly. Only the start address is off, which matters when someone reads the argument setup just before the call.
  3. Nit: the call_sites docstring (tools/import_call_sites.py:70) still lists only thunks, call and jmp.

Suggestion for 1: report a 48 FF 25 that hits a slot as a direct tail jump (an iat_jmp list, counted in the total), and keep thunks for the unprefixed FF 25. If that is out of scope, add a sentence to the module docstring saying that REX.W tail jumps through a slot show up as thunks. For 2, a sentence in the module docstring is enough. Moving the site back over a 0x40-0x4F byte without decoding could pick up the last byte of the previous instruction.

AI-assisted.

@airp1ane

Copy link
Copy Markdown
Contributor

Tested d06f36a on Windows 11, Python 3.15.2, NVIDIA RTX 4060 Ti (driver 621.24; CPU-only checks, no game files, no GPU/llvm-mc/MSVC used): pass.

Ran the test files this PR adds or changes, from a clean checkout of the PR head: test_import_call_sites.py - all exit 0.

AI-assisted (Claude Code).

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.

6 participants