Loader: evict module from MAPPED on DllMain false and TLS error (#106) #1376

Open
github-bot wants to merge 1 commit from github-bot/gh-1376 into main AGit
Owner

@⁠slopgobblercobbler on GitHub · 2026-11-24 15:23 UTC · original
slopgobblercobbler:fix/loader-error-cleanup → main · +106 −5 · 2 files · 1 commits · open

Summary

Fixes #106 Item 3:

  1. When DllMain(PROCESS_ATTACH) returns FALSE during load_game_dll or direct init_dll, unregisters the module from MAPPED via modules::unregister_mapped(m.base). Previously, failing DLLs remained cached in MAPPED, causing subsequent load_game_dll calls to return Ok(m) for an uninitialized or broken module.
  2. In load_game_dll, if an existing mapped module is returned from cache, verify m.initialized.load(Ordering::Acquire). If initialization has not completed (e.g. concurrent race or pending attach), run init_dll(&m) and evict on attach failure instead of assuming success.
  3. In map_image_ex, handle pe.tls() parsing and static TLS installation errors cleanly instead of silently ignoring errors. Validate TLS index pointer and callback array offsets within the module bounds, and call modules::unregister_mapped(base) on TLS failure to prevent leaving partially initialized modules registered.
  4. In modules::unregister_mapped, clear MAIN atomic pointer if m.base == base.

Changes

  • crates/xcl/src/loader.rs:
    • init_dll: call modules::unregister_mapped(m.base) on DllMain failure (ret == 0).
    • load_game_dll: check m.initialized on cached module hits and run attach if needed; unregister on failure.
    • map_image_ex: properly handle TLS directory parsing and carrier installation errors with module eviction.
    • Unit tests: added 3 synthetic tests verifying eviction on DllMain(PROCESS_ATTACH) == 0, corrupted TLS directory registration rollback, and uninitialized mapped module eviction.
  • crates/xcl/src/modules.rs:
    • unregister_mapped: clear MAIN when unregistering the main module base.

Tested

  • Windows 11 x64, Rust 1.85+ stable (MSVC).
  • cargo test -p xcl -- loader: all 5 tests passed (100% pass):
    • loader::tests::a_dll_whose_dllmain_fails_is_not_loaded
    • loader::tests::a_dll_whose_direct_init_dll_fails_is_evicted_from_mapped
    • loader::tests::a_dll_with_a_broken_import_table_is_not_left_registered
    • loader::tests::a_dll_with_a_broken_tls_directory_is_not_left_registered
    • loader::tests::uninitialized_mapped_module_evicted_if_init_fails_on_subsequent_load
  • Clean diff: zero trailing whitespace, GPL-2.0-only compliant, zero game data/keys.

Not Tested

  • Retail title full boot traces (synthetic test DLLs only).

AI Use

Co-Authored-By: Claude Code (Claude Opus 5.5) / Gemini Antigravity

**@⁠slopgobblercobbler** on GitHub · 2026-11-24 15:23 UTC · [original](https://github.com/xcl-dev/xcl/pull/1376) `slopgobblercobbler:fix/loader-error-cleanup` → `main` · +106 −5 · 2 files · 1 commits · open ## Summary Fixes #106 Item 3: 1. When `DllMain(PROCESS_ATTACH)` returns `FALSE` during `load_game_dll` or direct `init_dll`, unregisters the module from `MAPPED` via `modules::unregister_mapped(m.base)`. Previously, failing DLLs remained cached in `MAPPED`, causing subsequent `load_game_dll` calls to return `Ok(m)` for an uninitialized or broken module. 2. In `load_game_dll`, if an existing mapped module is returned from cache, verify `m.initialized.load(Ordering::Acquire)`. If initialization has not completed (e.g. concurrent race or pending attach), run `init_dll(&m)` and evict on attach failure instead of assuming success. 3. In `map_image_ex`, handle `pe.tls()` parsing and static TLS installation errors cleanly instead of silently ignoring errors. Validate TLS index pointer and callback array offsets within the module bounds, and call `modules::unregister_mapped(base)` on TLS failure to prevent leaving partially initialized modules registered. 4. In `modules::unregister_mapped`, clear `MAIN` atomic pointer if `m.base == base`. ## Changes - `crates/xcl/src/loader.rs`: - `init_dll`: call `modules::unregister_mapped(m.base)` on `DllMain` failure (`ret == 0`). - `load_game_dll`: check `m.initialized` on cached module hits and run attach if needed; unregister on failure. - `map_image_ex`: properly handle TLS directory parsing and carrier installation errors with module eviction. - Unit tests: added 3 synthetic tests verifying eviction on `DllMain(PROCESS_ATTACH) == 0`, corrupted TLS directory registration rollback, and uninitialized mapped module eviction. - `crates/xcl/src/modules.rs`: - `unregister_mapped`: clear `MAIN` when unregistering the main module base. ## Tested - Windows 11 x64, Rust 1.85+ stable (MSVC). - `cargo test -p xcl -- loader`: all 5 tests passed (100% pass): - `loader::tests::a_dll_whose_dllmain_fails_is_not_loaded` - `loader::tests::a_dll_whose_direct_init_dll_fails_is_evicted_from_mapped` - `loader::tests::a_dll_with_a_broken_import_table_is_not_left_registered` - `loader::tests::a_dll_with_a_broken_tls_directory_is_not_left_registered` - `loader::tests::uninitialized_mapped_module_evicted_if_init_fails_on_subsequent_load` - Clean diff: zero trailing whitespace, GPL-2.0-only compliant, zero game data/keys. ## Not Tested - Retail title full boot traces (synthetic test DLLs only). ## AI Use Co-Authored-By: Claude Code (Claude Opus 5.5) / Gemini Antigravity
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:52:20 +00:00
Author
Owner

@⁠futurepro62 on GitHub · 2026-11-24 15:32 UTC · original · edited 2026-11-24 15:38 UTC

Full-suite run, since this PR has no test report yet. Tested exact head ed6f612 (on its base 930e8b4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4: 754 passed, 0 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The new tests ran and passed: a_dll_whose_direct_init_dll_fails_is_evicted_from_mapped, a_dll_with_a_broken_tls_directory_is_not_left_registered, uninitialized_mapped_module_evicted_if_init_fails_on_subsequent_load.
  • Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4: 754 passed, 0 failed, 41 ignored, no skips.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
**@⁠futurepro62** on GitHub · 2026-11-24 15:32 UTC · [original](https://github.com/xcl-dev/xcl/pull/1376#issuecomment-7136190369) · edited 2026-11-24 15:38 UTC Full-suite run, since this PR has no test report yet. Tested exact head `ed6f612` (on its base `930e8b4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed. - `cargo build --workspace`, then `XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4`: **754 passed, 0 failed, 41 ignored**. SKIPPED (no wave64): `gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware`. - The new tests ran and passed: `a_dll_whose_direct_init_dll_fails_is_evicted_from_mapped`, `a_dll_with_a_broken_tls_directory_is_not_left_registered`, `uninitialized_mapped_module_evicted_if_init_fails_on_subsequent_load`. - Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, `XCL_ADAPTER=radeon`), `cargo test --workspace -- --test-threads=4`: **754 passed, 0 failed, 41 ignored**, no skips. **Not tested** - a release build, a game (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-24 16:41 UTC · original

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

  • cargo test -p xcl --lib: all passed, 0 failed, ~21 ignored.

Thanks!

**@⁠DavidM1911** on GitHub · 2026-11-24 16:41 UTC · [original](https://github.com/xcl-dev/xcl/pull/1376#issuecomment-7138068998) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~21 ignored. Thanks!
Author
Owner

@⁠Toons29 on GitHub · 2026-11-25 03:27 UTC · original

Regression run of ed6f6122 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: ed6f61226e11d1ea71cb5385ea36a06b20f74985
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1632 (PresentX 1632)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14685 MB

Against the limits in CONTRIBUTING.md: 101 % of main's frames, 99 % 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.

**@⁠Toons29** on GitHub · 2026-11-25 03:27 UTC · [original](https://github.com/xcl-dev/xcl/pull/1376#issuecomment-7151933809) Regression run of `ed6f6122` 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: ed6f61226e11d1ea71cb5385ea36a06b20f74985 gpu: AMD Radeon RX 9070 title: game.exe seconds: 60 exit: 0x0 frames shown: 1632 (PresentX 1632) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14685 MB ``` Against the limits in CONTRIBUTING.md: 101 % of `main`'s frames, 99 % 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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1376/head:github-bot/gh-1376'
git switch 'github-bot/gh-1376'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1376'
git switch 'github-bot/gh-1376'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1376'
git switch 'github-bot/gh-1376'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1376'
git switch 'main'
git merge --squash 'github-bot/gh-1376'
git switch 'main'
git merge --ff-only 'github-bot/gh-1376'
git switch 'main'
git merge 'github-bot/gh-1376'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.