Skip to content

import_call_sites: exclude raw padding from executable scans - #8272

Open
F1auct wants to merge 1 commit into
ItsNotRexed:mainfrom
F1auct:fix/import-call-sites-virtual-span
Open

F1auct wants to merge 1 commit into
ItsNotRexed:mainfrom
F1auct:fix/import-call-sites-virtual-span

Conversation

@F1auct

@F1auct F1auct commented Nov 23, 2026

Copy link
Copy Markdown

Claimed in the coordination issue: #8186 (comment)

What and why

When SizeOfRawData exceeds VirtualSize, the scanner counts import thunks, calls and tail jumps in raw alignment padding that Leonida’s loader does not map. Instructions crossing the virtual boundary are counted too.

Restrict each executable section’s scan buffer to its file-backed virtual span, matching PeImage._range() and pe_loader.cpp. Preserve the SizeOfRawData fallback when VirtualSize is zero. Synthetic tests cover padding, instructions crossing or exactly ending at the boundary, and zero/larger VirtualSize controls.

Based directly on main dde82f2. Independent of #7822 (machine type), #7116 (direct IAT calls) and #7915 (delay imports); none of their changes are included. Those PRs touch the same tool and later merges may need a context rebase.

Tested

Windows, CPython 3.13; CPU only, GPU/driver not applicable.

  • Import-call-site suite: 11 tests pass. Negative control with the unchanged main implementation: 6 new padding/boundary subcases fail.
  • PE-inspection suite: 60 tests pass.
  • compileall for make_game.py, tools and tests; make_game.py --help; changed-file check_no_game_data.py; git diff --check: all pass.
  • Full semantic suite: 447 files / 5,956 tests, with 2 failing files (test_image_atomics_guard and test_image_store_d16). Independently reproduced both on a clean worktree at the exact main base (7 tests / 48 failing subcases). No translator files changed.
  • Local x86-64 reference: complete before/after scanner result unchanged. No reference bytes or derived reports included.

AI-assisted: OpenAI Codex.

Checklist

  • No game data, keys or decryption, including in logs or reports quoted here.
  • No translator rule changed; the design contract table is unaffected.

@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 via Tier 2 Free Cloud (Groq gpt-oss-120b) & local test execution: Restricting the section scan to [:s['vsize'] or s['rawsize']] limits analysis to the file-backed virtual span mapped by the loader, preventing compiler/disk alignment padding from triggering false-positive call/thunk matches. All 11 tests in test_import_call_sites.py pass cleanly.

@leoleahy leoleahy 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.

AI-assisted.

Independently verified 5597ac54 on Windows 11 (Python 3.14.8, CPU-only, no game data):

  • Clean merge check against upstream/main (dde82f27): git merge-tree --write-tree exit 0, git diff --check exit 0, python -I -m compileall -q tools/import_call_sites.py tests/test_import_call_sites.py exit 0.
  • python -I tests/test_import_call_sites.py: 11/11 OK on HEAD 5597ac54.
  • Negative control (reverted tools/import_call_sites.py to base dde82f27 with the PR tests): 6 failures (4 calls/jumps and 2 thunks inside raw alignment padding falsely counted).
  • Diff review: slicing img.section_data(s)[:s["vsize"] or s["rawsize"]] cleanly bounds scans to the mapped virtual span while preserving file-backed data when vsize == 0 or vsize > rawsize.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

The new slice doubles peak scan memory for a section with one byte of padding. At 5597ac5, section_data copies the raw section. The next slice copies the mapped part while the first copy is still live.

I tested a synthetic 32 MiB section on Windows with CPython 3.14. These are peak Python allocations during call_sites. The measurement excludes the input image.

Implementation Peak bytes
Parent dde82f2 33,561,863
Head 5597ac5 67,115,235
Head with a bounded memoryview 3,773

Please take a view of the original image. For example, use length = min(s["rawsize"], s["vsize"] or s["rawsize"]) and memoryview(data)[s["rawptr"]:s["rawptr"] + length]. Both regex scans and struct.unpack_from accept this view. I tested this change locally. All 11 scanner tests pass. The three implementations give the same import report on the input below.

Run from the repository root with py -3.14 -I probe.py.

import pathlib, struct, sys, tracemalloc
sys.path.insert(0, str(pathlib.Path("tests").resolve()))
from test_import_call_sites import build_image, import_call_sites

size = 32 * 1024**2
data = bytearray(build_image([]))
table = 0x58 + 0xf0
struct.pack_into("<H", data, 0x46, 3)
struct.pack_into("<I", data, 0x58 + 56, 0x3000 + size)
struct.pack_into("<8sIIII", data, table + 80, b".code", size - 1, 0x3000, size, 0x3000)
struct.pack_into("<I", data, table + 80 + 36, 0x60000020)
data.extend(bytes(size))
data = bytes(data)
tracemalloc.start()
out = import_call_sites.call_sites(data)
print("peak bytes:", tracemalloc.get_traced_memory()[1], "imports:", out["imports"])

AI-assisted.

@molodayamama

Copy link
Copy Markdown

Verified commit 5597ac5493042773cc049ff6bf1b9eb9b404e7b9. All 11 import-call-site tests passed on Windows using synthetic PE fixtures. A thunk-shaped byte sequence in raw padding is excluded. The controls still count the bytes backed by the file when the declared virtual size is zero or larger than that raw area. This was an offline scan of synthetic inputs.

AI-assisted verification.

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