Skip to content

pe_loader: roll back failed image maps - #5805

Open
editrice wants to merge 5 commits into
ItsNotRexed:mainfrom
editrice:fix/map-image-rollback
Open

editrice wants to merge 5 commits into
ItsNotRexed:mainfrom
editrice:fix/map-image-rollback

Conversation

@editrice

@editrice editrice commented Nov 23, 2026 •

Copy link
Copy Markdown

Claimed in the coordination tracker.

Why

Loader::map_image published an allocation, module and address range before section, relocation and export validation finished. A failed map remained in modules() and find_address() and held its preferred address until Loader destruction, so a corrected non-relocatable image at the same path could not be retried.

Change

Prepare and validate the image before publishing it. On failure, release a standalone allocation or decommit an inherited base reservation, and remove any TLS state installed on the current or existing threads. Import binding and later initialization are separate paths.

Regression coverage

Three synthetic Windows CPU cases cover distinct cleanup rules: ordinary failed map and same-path retry; failed map inside an inherited reservation and recommit; injected existing-thread TLS record failure, including verification that the worker and loading thread TLS slots are restored and the global owner is retired before retry. The injection hook is compiled only into loader_test.

Validation

  • Red baseline on upstream main: the original retry check failed (86/87 checks passed).
  • Current head f73564a7: loader_test 89 checks, 0 failed; focused loader, pe_loader, pe_loader_bounds CTest 3/3 passed. The preceding test-only head also passed 20 repeated loader runs.
  • scripts/check_no_game_data.py on changed source/test files and git diff --check passed. Windows 11 CPU with clang-cl 21.1.8; no game files or GPU used.

Overlap

Merged #2954 and #3667 intentionally retain failed mappings/ranges until Loader destruction. #3981 handles later initialization failures. This PR covers map-time rollback.

AI-assisted: yes

Checklist

  • No game data, keys or decryption, including in quoted logs or reports.

@Bj0rnis

Bj0rnis commented Nov 23, 2026

Copy link
Copy Markdown

Verified on Windows 11 (MSVC 19.44 / Ninja) at 85ee375f:

  • loader PASS (10.73 s) — 87 checks, 0 failed.

@editrice
editrice marked this pull request as draft November 23, 2026 12:20

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

Reviewed head 85ee375f54d33cab677a25683e9210f4451772be. Windows 11 26200, MSVC 19.44.35207, Ninja, RelWithDebInfo, LEONIDA_VK_DEVICE='RTX 5070 Ti'.

Counts:

  • loader: 1/1 passed, 87 checks / 0 failed (includes the new a failed map releases its range and preferred base for a corrected retry)
  • pe_loader: 1/1 passed, 79 checks
  • run_tiny_gdk_load: 1/1 passed
  • run_tiny_atexit: 1/1 passed, 8 termination paths match the Windows sequence
  • run_tiny_frame: 1/1 passed on the Vulkan D3D12 device
  • ctest -R "^loader$|^pe_loader$|^run_tiny_frame$|^run_tiny_gdk_load$|^run_tiny_atexit$": 5/5 passed

Red baseline: reverting only runtime/src/pe_loader.cpp to its parent gives 87 checks / 1 failed, a failed map ...: map_retry.dll: preferred base is taken and the image has no relocations, so the new test does catch the bug.

Rollback path beyond the PR test: I temporarily added (not part of the review) a VirtualQuery assertion to the new test and a second case that fails inside initialize_tls (inverted TLS template) after by_name_ publication. Both passed: the failed section map's preferred range is MEM_FREE, and the TLS failure leaves modules() empty, find_address() false, the range MEM_FREE, and a corrected retry reclaims the preferred base. 88 checks / 0 failed; the worktree was restored afterwards.

Static check: m->base is only assigned by base_reservation::commit_reserved or VirtualAlloc, and both are released with VirtualFree(..., MEM_RELEASE), so the catch cannot unmap memory it did not map. Reserving order_ and ranges_ before publication makes the post-TLS pushes non-throwing.

Verdict: LGTM, ready to merge. Non-blocking nits: lines 1195-1236 of pe_loader.cpp keep the pre-try indentation (the relocation block is now inside it), and the PR's own test does not cover the initialize_tls failure path that the new inner try/catch handles (it works when exercised).

@editrice
editrice marked this pull request as ready for review November 23, 2026 12:44
@milas6669

milas6669 commented Nov 23, 2026 •

Copy link
Copy Markdown

Checked head e30f90462 (the two earlier checks above are on 85ee375f, before the TLS tests existed). Windows 11, MSVC 19.44, RelWithDebInfo, merged onto main 561b65a8 with no conflicts. loader_test is 89 checks, 0 failed, and ctest -R "^(loader|pe_loader|pe_loader_bounds)$" is 3/3.

New data: I mutated each rollback line in runtime/src/pe_loader.cpp one at a time and rebuilt loader_test:

Removed line Result
VirtualFree(m->base, m->size, MEM_DECOMMIT) (inherited-reservation decommit) caught: 1 failed
VirtualFree(m->base, 0, MEM_RELEASE) (standalone release) caught: 10 failed
restore_thread_slot(...) in the install_on_existing_threads record catch caught: the test process crashes (exit 0xC0000005)
g_tls_slot_owners[slot].compare_exchange_strong(owner, nullptr) in initialize_tls's catch survives: 89 checks, 0 failed
remove_from_existing_threads(m) in that catch survives
uninstall_thread_block(m) in that catch survives
by_name_.erase(key) in map_image survives, but Loader::load erases the same key itself, so this one is redundant rather than untested

The injected record failure does reach initialize_tls's catch, since the retry works. But nothing asserts what that catch cleans up. After the failed map, delete m has run while g_tls_slot_owners[slot] could still hold the freed Module*, and the loading thread's block could stay installed. One extra assertion would pin that down. For example, after the failed map, check that the slot owner is null and that the loading thread's slot is back to its original value, the way the test already does for the worker thread.

No defect reproduced. This is a coverage gap in the new test, not a failing case. Of the other open PRs touching pe_loader.cpp, #5677, #5302 and #5395 merge cleanly with this one (merge-tree). #3981 and #4313 conflict with it, but both already conflict with main on their own. No game data, GPU not needed. AI-assisted (Claude Code).

@kapitche

Copy link
Copy Markdown
Contributor

Tested 5b2fdcd merged with main 561b65a on Windows (native, MSVC 14.51 / VS 18, Ninja, RelWithDebInfo, built from a git archive copy of the merge; loader_test target only): pass

loader_test.exe: 89 checks, 0 failed, including the new rollback cases (failed map releases range and preferred base for a corrected retry, failed reserved map keeps the reserved range, failed existing-thread TLS record rolls the map back for retry, failed load leaves neither module nor failed dependency loaded). Not run: the rest of the runtime tests, Wine, Linux, a GPU.

Negative control (weak): with pe_loader.cpp / pe_loader.h taken from upstream/main and the PR's test kept, loader_test does not compile (fail_next_existing_thread_tls_record_for_test is not a member of rt), so I could not show the individual new checks failing against the old behaviour; I did not mutate the fix. Cosmetic: the relocation and export block inside the new try in Loader::map_image is not re-indented.

@editrice

Copy link
Copy Markdown
Author

Thanks for the mutation review. I added the two requested checks at f73564a: after the injected record failure, the test now verifies that the global TLS owner is retired and the loading thread's helper slot is restored, alongside the existing worker-thread check. The same 89 loader checks and three focused CTests pass locally; the PR description now names this head. AI-assisted.

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