Skip to content

PE loader: a broken delay-import table fails the load - #1208

Open
Longno242 wants to merge 1 commit into
xcl-dev:mainfrom
Longno242:pe-loader-delay-import-fail
Open

Longno242 wants to merge 1 commit into
xcl-dev:mainfrom
Longno242:pe-loader-delay-import-fail

Conversation

@Longno242

@Longno242 Longno242 commented Nov 24, 2026 •

Copy link
Copy Markdown

A malformed delay-import directory only logged a warning and left the module registered (and partially resolved). Normal imports already fail the load and unregister; delay-imports must do the same.

What is in it

  • crates/xcl/src/loader.rs: fail + unregister on delay_imports() error; unit test with a synthetic DLL whose delay Name RVA is outside the image

Tested

  • Hardware/OS (full suite by @jennydeth): RTX 3060 12 GB (32-lane waves, driver 32.0.16.2101), Ryzen 5 5600X, 32 GB RAM, Windows 11 build 26300, Rust 1.100.0, DXC 1.9.2609.5, debug build. Head 959e7a8
  • Commit tested: 959e7a829f2108ebf93c10e5f924f476df4c5549 (clean tree)
  • cargo test --workspace -- --test-threads=4: 742 passed, 0 failed, 41 ignored. SKIPPED: 2 GPU tests for no wave64 (expected on this card)
  • SKIPPED: 2 GPU tests for no wave64 (expected on this card)
  • Retail smoke: see Regression run (@Toons29, RX 9070)
  • @DavidM1911 re-checked cargo test -p xcl --lib on RTX 3060 Ti after merging this head with current main locally: all passed, 0 failed, 27 ignored. The PR head itself is unchanged.

Regression run

  • Retail scripts/smoke.ps1 from @Toons29 (RX 9070, 60 s, same title/time; blocks as printed):
xcl-smoke v1
commit: 4178e888c0b5be50a2887eda45cdb832763b68a4
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 14831 MB
xcl-smoke v1
commit: 959e7a829f2108ebf93c10e5f924f476df4c5549
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1633 (PresentX 1633)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14812 MB

Not tested

  • Full cargo test --workspace
  • Titles that ship delay-load imports (whether any retail path hits a parse error)

AI use

  • @jennydeth: Claude Code (Claude Opus 5.5) ran the full workspace suite and reported results in a PR comment
  • Cursor Auto (Composer): found the asymmetry vs the import-table failure path, implemented the fix and test, opened this draft PR. Exact underlying model name not disclosed to the agent.

A malformed delay-import directory only logged a warning and left the
module registered. Treat it like a broken import table: unregister and
fail the load.

Assisted-by: Cursor Auto (Composer)
Co-authored-by: Cursor <cursoragent@cursor.com>
@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib loader: 3 passed, 0 failed.

Note: the full d3d12x lib filter currently aborts with 0xc0000005 on unmodified main too (pre-existing, unrelated to this PR); the PR's own tests were run by name and pass.

@Toons29

Toons29 commented Nov 25, 2026

Copy link
Copy Markdown

Regression run of 959e7a82 on the retail title, for this PR's Regression run section. Both blocks are from scripts/smoke.ps1 as printed, not edited: main on 4178e888, this PR on its head commit with no uncommitted changes, same title and time.

xcl-smoke v1
commit: 4178e888c0b5be50a2887eda45cdb832763b68a4
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 14831 MB
xcl-smoke v1
commit: 959e7a829f2108ebf93c10e5f924f476df4c5549
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1633 (PresentX 1633)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14812 MB

Against the limits in CONTRIBUTING.md: 101 % of main's frames, 100 % of its peak commit, no crash, no device removal.

AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, cargo build --release -p xcl, 60 s, XCL_NETWORK=0. This is a regression check only: it does not show whether the run reaches the code this PR changes.

AI use: Claude Code (Claude Opus 5.5) built both trees and ran scripts/smoke.ps1; I checked the blocks before posting.

@Longno242

Copy link
Copy Markdown
Author

Thanks @Toons29 — pasted both of your xcl-smoke v1 blocks under Regression run so the description check can see them.

@Longno242
Longno242 marked this pull request as ready for review November 25, 2026 12:43
@Longno242

Copy link
Copy Markdown
Author

CI is stuck on action_required (first-time contributor workflow approval). Smoke + PR description are green; please approve the pending CI workflow when you get a chance.

@jennydeth

Copy link
Copy Markdown

Full workspace run (listed under Not tested).

Setup: RTX 3060 12 GB (32-lane waves, driver 32.0.16.2101), Ryzen 5 5600X, 32 GB RAM, Windows 11 build 26300, Rust 1.100.0, DXC 1.9.2609.5, debug build. Head 959e7a8.

Result: cargo test --workspace -- --test-threads=4: 742 passed, 0 failed, 41 ignored. SKIPPED: 2 GPU tests for no wave64 (expected on this card).

Not tested: titles that ship delay-load imports.

AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this report.

@Longno242

Copy link
Copy Markdown
Author

Thanks @jennydeth — copied your workspace counts into Tested (742 passed, 0 failed, 41 ignored. SKIPPED: 2 GPU tests for no wave64 (expected on this card)).

@DavidM1911

Copy link
Copy Markdown
Contributor

Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.

Thanks!

— Devin

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
… load (port upstream xcl-dev#1212 + xcl-dev#1208)

Windows loader.rs (cfg(windows)-only, reference-only on Linux): delay_imports
Err arm goes from warn-only to unregister+fail like a broken import table;
TLS AddressOfCallbacks walk is capped at 256 entries and bounded to the
image (va_to_rva outside-image guard + checked_add past-end guard), with
unregister+fail on all three failure legs. Both upstream tests verbatim;
0 tests run on Linux.

Linux loader_linux.rs twin: TLS walk already image-bounded
(checked_rva + at+8>size + per-target va_to_rva, strictly stricter than
the 256-entry cap) so pin test only; delay_imports warn-only mirrored to
unregister+fail with twin retry test. Graft red pre-fix, green post-fix.
@Longno242

Copy link
Copy Markdown
Author

Thanks @DavidM1911 — noted the cargo test -p xcl --lib re-check (passed, 0 failed, 27 ignored) under Tested. Head commit is unchanged.

@saintwolf

Copy link
Copy Markdown
Contributor

merge-check claim
head: 959e7a8

@saintwolf

Copy link
Copy Markdown
Contributor

merge-check
base: abec7ce
head: 959e7a8
merge: clean (trial merge 5e1c7f9b4d3603d8cbe2f3935a126f2e91a3c4e3; git diff --check clean; +33/-1 in crates/xcl/src/loader.rs)
build: cargo build --workspace -> ok (39 lib + 19 test warnings, all pre-existing dead code / deprecations, none from the PR's lines)
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2887 passed, 0 failed, 51 ignored (base + 1 new test; the base abec7ce includes #3513's resinfo-D16 test fix, so the previously failing gpu::dxil::msaa_tests::gpu_direct_image_get_resinfo_d16 passes now — this was the only failure on this machine's earlier baseline). SKIPPED: 27, the standard set for this card (22 "no wave64" — wave32-only GPU, 2 "no heap windows", 1 "cannot disable the driver cache (Developer Mode)", 2 "no xgameruntime.dll"). The new a_dll_with_a_broken_delay_import_table_is_not_left_registered passes in the targeted loader run (5/0) and inside the full suite.
review: read the whole diff and the surrounding code. In map_image_ex a parse failure of pe.delay_imports() only logged a warning and continued the load, leaving the mapped module registered; the module's own comment (loader.rs 160-162) promises "a failure from here on takes the module out of the registry again", and the import-table path honours it (unregister + fail) but the delay path did not. The consequence: load_game_dll's idempotency check (find_mapped_by_name) would later return the half-initialised module — delay imports unresolved, module handles not written — as "already loaded", and the address-range lookup would hit it too. The fix makes the delay path identical to the import-table path: unregister_mapped(base) then fail(...). I checked the two helpers: fail frees the mapped memory (VirtualFree MEM_RELEASE) and returns Err, and unregister_mapped removes the module from both the name registry and the lock-free range table (modules.rs:140, documented as Windows' behaviour for a failed DLL), so nothing leaks and no stale entry survives. At the point delay_imports() fails the normal imports have been parsed but not resolved (resolution starts after both parses), so unregistering there is exactly as safe as on the import-table failure. The parser side is already bounded (pe.rs:455-488: at most 4096 descriptors, checked arithmetic, a name RVA outside the image answers BadRva via cstr_at). The test builds a DLL with the existing tiny_dll helper whose delay directory (#13 of 16 data directories, at the helper's computed offset 0x58+0x70+13*8) points at a descriptor with attrs=1 (RVA-based) and Name=0x7FFF0000 outside the image, so the parse errors; it then asserts load_game_dll fails with a "delay-import table" message and find_mapped_by_name is None, over two attempts — the second attempt is what fails under the old behaviour, which would return the still-registered module. The zeroed second descriptor at 0x1030 terminates the parse loop (name_rva == 0 && iat_rva == 0). Thread: DavidM1911 on an RTX 3060 Ti — cargo test -p xcl --lib loader 3/0 at the head merged with current main, plus a re-verification (cargo test -p xcl --lib all passed, 0 failed, 27 ignored); jennydeth on an RTX 3060 — cargo test --workspace 742 passed, 0 failed, 41 ignored, 2 wave64 SKIPPED; Toons29's paired retail xcl-smoke on an RX 9070 (main 4178e88 vs this head: 101% of main's frames, 100% of its peak commit, 0 crashes, 0 device removals). Overlap note: #1367 (open, not in the queue — red CI) "PE: validate directory and table bounds in parser and loader" is complementary, not a duplicate: it hardens the parser (more malformed shapes answer BadRva) and bounds-checks the IAT / module-handle writes in the resolution loop, a different hunk; if it lands first, this PR's error path simply fires for more shapes. No #1607 duplicate group covers #1208. One file, no new dependencies, no stop-criteria files.
performance: none; the success path is unchanged, only the already-failing path now fails and frees its mapping instead of continuing with a half-initialised module registered.
verdict: safe to merge
hardware / tool and model: RTX 4070 SUPER (driver 32.0.16.2140), Ryzen 7 7800X3D, Windows 11 build 26300, Rust 1.100.0 / OpenCode, Qwen 3.8 27B

@github-actions github-actions Bot added area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) labels Nov 30, 2026
@coolpersonnn

Copy link
Copy Markdown
Contributor

merge-check claim
head: 959e7a8

@coolpersonnn

Copy link
Copy Markdown
Contributor

merge-check
base: abec7ce
head: 959e7a8
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2887 passed, 0 failed, 51 ignored (25 SKIPPED: 22 no wave64, 2 no heap windows, 1 cannot disable the driver cache); failing: none; new test loader::tests::a_dll_with_a_broken_delay_import_table_is_not_left_registered passed
review: none. Read the full diff (+33/-1 in crates/xcl/src/loader.rs). In map_image_ex, pe.delay_imports() returning Err now unregisters the mapped base and calls fail(...) (freeing the mapping and returning Err), matching pe.imports() right above it before any imports are resolved against the module. The new unit test builds a synthetic DLL with data directory #13 pointing to a delay-import descriptor whose Name RVA is outside the image, runs load_game_dll twice, and verifies both attempts fail with "delay-import table" and leave nothing registered (would fail before the fix, since the old code only logged a warning and kept loading). Minimal scope; no duplicates found in open/merged PRs or #1607.
performance: none (error path during DLL loading only; no hot-path or memory overhead)
verdict: safe to merge
hardware / tool and model: NVIDIA GeForce RTX 5090 (driver 32.0.16.2101), AMD Ryzen 9 9950X3D, Windows 11 Pro build 26300, Rust 1.100.0 / T3 Code (Antigravity), Gemini 4 Neon (Medium)

@github-actions github-actions Bot added ready-to-merge Has 2 independent safe merge-checks on current head (#3041) and removed needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) labels Nov 30, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head 959e7a8

No open or merged PR makes the same change (#1367 bounds the delay-load module-handle write but still only warns when the delay-import table does not parse). One point to consider in loader.rs:map_image_ex: until now a delay-import table that XCL's parser rejects only skipped eager resolution, and the image's own __delayLoadHelper2 thunks still loaded lazily. With this change the whole module fails to load, which is stricter than Windows (it reports a broken delay descriptor only on the first call). The PR description should say why a hard failure is preferred.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH ready-to-merge Has 2 independent safe merge-checks on current head (#3041)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants