Repository navigation
loader: the address lookup reads its own table, not the module vector - #3667
Conversation
lookup_in_loader walked Loader::modules() with no lock while map_image appends to it under Loader::mutex_. The lookup runs on any thread: behind the RtlPcToFileHeader redirect on every C++ throw from a mapped image, and for trace and stop lines. A throw during another thread's run-time load read a vector that was being reallocated. find_address reads a table of (base, size, name) under a leaf lock; map_image adds an entry once the image's memory exists. loader_test: lookups from one thread while another loads 300 images. With the old code it crashes under AddressSanitizer at the vector read. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Review note about the cost of the lookup. An AI tool helped me to read the diff. I did not build or run the change. Result of the check The correction of the race condition is correct. The table has its own lock. If Two small items The description says that this lookup runs on each C++ throw from a mapped image.
|
Concurrent throws on different threads no longer queue on it; map_image takes it exclusively for the one push. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Thanks for the read. Taken and not taken:
Tested for the new commit: |
|
Tested |
|
Tested on Windows 11 with two GPUs on one machine (runtime built from this PR's head merged with main 4c52ce2, MSVC 14.51):
Only the |
|
Tested
PR head as-is, no game files. AI-assisted. |
|
Tested Windows 11 (26200), MSVC 14.50 (VS 2026), Ninja RelWithDebInfo. CPU only.
Not checked: the ASan build from the description, the whole AI-assisted; measured on my PC. |
|
Tested b1fdafc on Windows 11 (MSVC RelWithDebInfo): full runtime build, then ctest for the shim/loader tests (loader, pe_loader, loader_tls, dll_thread_calls, unimplemented_policy, startup_trace, xmem, process_cmdline, file_system, winhttp_offline, xgameruntime, run_tiny_*, allocator_proxy) — 25 of 25 passed. AI-assisted. |
- One lookup path: the identity queries are a find_module entry in BundledModuleOps, and GetProcAddress on a mapped module, NULL included, goes only through main's resolve_module_export. The separate hooks, mapped_proc_address and Loader::export_address are gone, and so is the unused Loader::main_module(). - Locks: names are looked up in by_name_ under Loader::mutex_, as main's find_loaded_module does; bases, addresses and the main module come from ItsNotRexed#3667's range table under its leaf lock, which now keeps each image's path. loader_test gains test_identity_during_loads. - GetProcAddress(NULL, name) resolves against the mapped main EXE, as Windows 11 resolves it against the process's EXE (measured natively); without --run it stays the host's answer. - A null name gives ERROR_INVALID_PARAMETER when the image has an export directory and ERROR_INVALID_ORDINAL when it has none, as Windows does. - GetModuleHandle with a relative path (..\x.dll, .\x.dll) searches the DLL search path (ntdll's LdrGetDllPath) as Windows does, instead of the current folder, and trailing dots, however many, mean no extension. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claim: #3381 (comment) (new RT item)
What and why
lookup_in_loaderwalkedLoader::modules()with no lock, whilemap_imageappends to that vector underLoader::mutex_. The lookup is whatrt::shims::mapped_image_basecalls, behind theRtlPcToFileHeaderredirect on every C++ throw from a mapped image, and whatmodule_offsetanddescribe_addressuse for trace and stop lines. A worker that throws while another threadLoadLibrarys a bundled DLL read a vector that was being reallocated: a rare crash with no pattern during boot.Taking
mutex_in the lookup is not an option: it is held while image code runs, and the registry holds its own lock around the callback.Loader::find_addressreads a small table of (base, size, name), one entry per mapped image, underranges_mutex_, a leaf lock nothing else is taken under.map_imageadds the entry once the image's memory exists.lookup_in_loadercalls it and no longer touchesmodules().~Loaderalready unregisters the lookup before it frees the images.Test
test_lookup_during_loadsinruntime/test/loader_test.cpp: one thread asksmapped_image_baseandmodule_offsetfor an address in the first module (and for one in no module) in a loop while the main threadload()s 300 small images; every answer must be right, and each new module must be found by address.The race shows up for certain only under AddressSanitizer, so the proof that the test fails on
mainis an ASan build made by hand (cl /fsanitize=addressoverloader_test.cpp,pe_loader.cpp,registry.cpp), not the regular ctest:pe_loader.cppf847748cAddressSanitizer: access-violationinlookup_in_loader,pe_loader.cpp:458, called frommapped_image_baseon the reader threadI did not measure how often the plain build of the old code fails.
The ASan build, from a Visual Studio x64 developer prompt in
runtime/(the sanitizer runtime DLL has to be onPATH, andgta6rt_tls_host.dllbeside the executable):For the first row,
src\pe_loader.cppandsrc\pe_loader.hweref847748c's and the test was this branch's.One more thing a reviewer should know: open #2075 adds two further readers of the same vector,
find_in_loaderandproc_in_loader, with a comment that reading it without a lock is fine becauseload()fills the list before image code runs. A run-timeLoadLibraryof a bundled DLL (load_runtime_module) is the case where that does not hold: the list grows while image code is running on other threads. This PR fixes the reader that is onmain; it does not depend on #2075 and does not cover its two readers, which are not onmain. Whichever lands second can move them onto the same table (it would need the path next to the name); I will do that as a follow-up or on #2075's branch if its author prefers.Tested
Windows 11 26200, MSVC 19.44, RelWithDebInfo, built and run with Visual Studio 2022's CMake 3.31.6 and its Ninja. NVIDIA GeForce RTX 4090, driver 621.14; the change itself needs no GPU.
ctest -j 4: 141 tests, 133 passed, 8 skipped (the five package tests,vk_sync_validation,d3d12_rgb9e5_rtv,d3d12_first_light), 0 failed.loader_test10 more times: 80 checks, 0 failed each. The new case adds about 3 s to it (8.8 s to 11.8 s here; most of that is writing 300 image files).AI-assisted (Claude Code).
🤖 Generated with Claude Code