Skip to content

[upstream #1119] PE loader: thread callbacks wait for a DLL to be initialised - #40

Closed
ufoptg wants to merge 1 commit into
mainfrom
upstream-pr/1119
Closed

ufoptg wants to merge 1 commit into
mainfrom
upstream-pr/1119

Conversation

@ufoptg

@ufoptg ufoptg commented Nov 24, 2026

Copy link
Copy Markdown
Owner

loader::on_tls_event ran the TLS callbacks of every registered module on each DLL_THREAD_ATTACH / DETACH, and checked initialized only before calling DllMain. A game DLL is registered, and its TLS callbacks are read (map_image_ex step 5), before its pages get their protections (step 6) and before its PROCESS_ATTACH (init_dll).

So if the title LoadLibrarys a game DLL with TLS callbacks while another of its threads starts or ends, that DLL's callback can run:

  • on code that is not executable yet, which is a DEP fault (0xc0000005);
  • or before the DLL's runtime has set itself up for it.

Windows' loader sends thread notifications only to modules whose process attach has run.

What is in it

  • crates/xcl/src/loader.rs:
    • the module loop of on_tls_event is now thread_event;
    • it skips DLLs that are not initialised, for both the TLS callbacks and DllMain;
    • the executable, which never sets initialized, still gets its callbacks;
    • for initialised modules nothing changes (same order: callbacks then DllMain on attach, the reverse on detach).
  • Test thread_callbacks_wait_for_a_dll_to_be_initialised:
    • a DLL module that is registered but not initialised gets no callback on attach or detach;
    • once initialised it gets both, and an executable module gets them anyway;
    • without the change it fails with left: 5, right: 0 (both callbacks ran).

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4), driver 32.0.31044.16; Ryzen 5 5600, 32 GB; Windows 11 Pro 26300; rustc 1.100.0 x86_64-pc-windows-msvc; DXC 1.9.2602.17; debug for the suite, release for the runs.
  • cargo test --workspace -- --test-threads=4 at df36761: 742 passed, 0 failed, 41 ignored; no SKIPPED lines.

Regression run

The gpu: line names a virtual display adapter of this machine (what #281 / #282 / #285 fix); XCL's log names backend 'real' – AMD Radeon RX 9060 XT (16188 MiB VRAM).

main:

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: AMD Radeon RX 9060 XT
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1574 (PresentX 1574)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16862 MB

PR:

xcl-smoke v1
commit: df3676101e0a3f03f5ae35575bdbffc39c40d1ce
gpu: AMD Radeon RX 9060 XT
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1563 (PresentX 1563)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 19675 MB

Not tested

  • A title that loads a game DLL with TLS callbacks at run time while threads start: I have none that does. The race is reasoned from the code order above, and the unit test covers the check itself.
  • NVIDIA.

AI use

  • Claude Code with Claude Opus 5.5:
    • a sub-agent found this in an audit of the PE loader;
    • I checked the order in map_image_ex / init_dll / on_tls_event;
    • Claude Code wrote the change, the test and this description.
  • The runs above were made on my machine.

🤖 Generated with Claude Code


Mirror of xcl-dev#1119 (auto-synced, do not edit).

on_tls_event ran the TLS callbacks of every registered module on each
DLL_THREAD_ATTACH / DETACH, and checked `initialized` only before DllMain. A
game DLL is registered, and its TLS callbacks read, before its pages get
their protections and before its PROCESS_ATTACH (init_dll). So when the title
LoadLibrary's a game DLL while another of its threads starts or ends, that
DLL's callback can run on code that is not executable yet (a DEP fault) or
before the DLL's runtime has set itself up. Windows' loader sends thread
notifications only to modules whose process attach has run.

The module loop is now thread_event, which skips DLLs that are not
initialised for both callbacks and DllMain; the executable, which never
sets the flag, still gets its callbacks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ufoptg

ufoptg commented Nov 25, 2026

Copy link
Copy Markdown
Owner Author

Upstream PR xcl-dev#1119 was closed or merged.

@ufoptg ufoptg closed this Nov 25, 2026
@ufoptg
ufoptg deleted the upstream-pr/1119 branch November 25, 2026 00:14
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.

2 participants