Skip to content

PE loader: validate full-width named import RVAs before narrowing - #2839

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
s0las16:devin/1795374000-import-thunk-rva
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
s0las16:devin/1795374000-import-thunk-rva

Conversation

@s0las16

@s0las16 s0las16 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

claiming: Before this fix, runtime/src/pe_loader.cpp:1054-1055 narrowed a named import thunk's 64-bit AddressOfData to uint32_t before validating its range. A value (1ull << 32) | valid_name_rva therefore bound the low-RVA name rather than being rejected. The shared binder handles ordinary and delay imports. Read-only checks of coordination issues #2716/#2516 and open upstream PR searches found no matching claim or fix.

Claimed in the coordination issue as: new item (claim above; no comment posted to #2716). The item ID can be added by the coordinator.

What and why

Move the existing import-name range check before the uint32_t cast and pass it the full 64-bit AddressOfData value. SizeOfImage is 32-bit, so any non-ordinal value with high bits set now fails before narrowing, using the existing diagnostic: an import name runs past the image.

The shared binder applies the check to both ordinary and eagerly bound delay imports. Ordinal classification and binding remain unchanged.

Add byte-built loader_test cases for ordinary and delay imports, each with a valid control importing testshim.dll!F and a corrupted lookup thunk (1ull << 32) | 0x1120. The corrupted cases must fail with the import-name range diagnostic; the valid controls must load. All data is synthetic; no game files are used.

Overlap checks: searched open upstream PRs for AddressOfData, thunk/RVA, import/truncation, import/32, thunk, import/bounds, and named import. No matching fix found. #2075 concerns mapped module identity/export lookup and #2734 concerns entry-point bounds, not named-import thunk validation. No matching claim found in the bodies/comments of #2716 or #2516.

Tested

Not compiled locally: Linux without MSVC, with cmake/ctest unavailable. The attempted targeted loader CTest run reported ctest: command not found. Relies on the Windows runtime.yml workflow for compilation and the loader regression test; runtime execution remains unverified locally. No workflow dispatched as part of this change. CPU-only; no GPU or driver involved. AI-assisted (Devin).

git diff --check completed without output. Manually reviewed the final diff, byte-built descriptor/table offsets, zero terminators, test shim registration, and the full-width range-check signature.

Checklist

  • 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 (n/a: no translator change).

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@carlcdx

carlcdx commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Built, reviewed, and tested 9ccb1052 on Windows 11 (26300, MSVC, Windows SDK 10.0.26100.0, RelWithDebInfo, CPU only): pass.

Build & diff checks:

  • cmake --build runtime/build --config RelWithDebInfo --target loader_test loader_tls_test pe_loader_test test_pe_loader_bounds: built cleanly with 0 warnings.
  • git diff --check HEAD~1..HEAD: clean.
  • Verified in runtime/src/pe_loader.cpp:1054-1055 that require_range now validates the full 64-bit lookup->u1.AddressOfData before narrowing to uint32_t, covering both ordinary and delay imports through bind_thunks.

CTest (ctest --test-dir runtime/build -C RelWithDebInfo -R "loader" -V): 4/4 passed (7.86s):

  • loader (loader_test.exe): 71/71 checks passed, including all 4 new cases (a valid import name RVA loads, an import name RVA with high bits is rejected, a valid delay-import name RVA loads, a delay-import name RVA with high bits is rejected).
  • pe_loader (pe_loader_test.exe): 79/79 checks passed (2 optional package sections skipped, no LEONIDA_GAME_DIR).
  • pe_loader_bounds (test_pe_loader_bounds.exe): 50/50 checks passed.
  • loader_tls (loader_tls_test.exe): 58/58 checks passed.

Synthetic tests only; no game files. AI-assisted.

@tupacfan04

Copy link
Copy Markdown
Contributor

Code read of 9ccb1052. Looks right. The range check now runs on the full 64-bit AddressOfData before it's narrowed to 32 bits, so a thunk like (1ull << 32) | valid_rva is refused instead of aliasing a valid name. Ordinal thunks don't reach this path, and the test covers both the import and delay-import tables.

AI-assisted review (Claude Code).

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

Verified with an MSVC build against current main's files: loader_test -> 71 checks, 0 failed, including the 4 new checks; CTest loader 1/1; test_pe_loader_bounds 50/50. The old loader + new test fails the two (1ull<<32)|0x1120 cases while both valid controls pass. Synthetic PE fixtures only; no game data or absolute paths. Approve.

Non-blocking: the new checks live in loader_test.cpp, not the dedicated test_pe_loader_bounds.cpp.

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested 9ccb105 merged onto main 1628210 on an RTX 5090 (driver 616.92), Windows 11, MSVC 19.44, Vulkan SDK 1.4.363, RelWithDebInfo:

  • Full ctest: 130/130.
  • loader: passes 20 of 20 runs, and passes under LEONIDA_VK_VALIDATION=1 with 0 validation messages.
  • Warnings: none new in the files this PR changes.

AI-assisted (Claude Code).

seblessa added a commit to seblessa/Leonida that referenced this pull request Nov 22, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ItsNotRexed
ItsNotRexed merged commit 1c96d27 into ItsNotRexed:main Nov 22, 2026
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