Forward thread attach and detach to mapped images #138
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/139"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-21T16:53:42Z.
Change
This follows up on #78, which listed thread notifications as open.
pe_map.dllis itself loaded by Windows, so its ownDllMainreceivesDLL_THREAD_ATTACHandDLL_THREAD_DETACH. It now forwards both to every image thatpe_map_imagemapped and attached:Registration and forwarding:
pe_map_release, before its process detach.DllMain. Registration takes the lock exclusively, sope_map_releasewaits for in-flight notifications.Measured with
tls_routeloaded byLoadLibraryWon Windows 11 build 26300. For a new thread, the TLS callback and then the entry point are called withDLL_THREAD_ATTACHon that thread, then the same pair withDLL_THREAD_DETACH, all with a null reserved argument.Test changes:
cpu_tls_sink.dllnow records the calling thread. Unrelated threads in the process, such as loader workers, are then ignored.cpu_native_tlsstarts and joins one thread, whose start routine iskernel32!Sleep, between attach and release, under both loaders, and compares the notifications on that thread.Verification
At the head of this branch on
a2a62ee(Windows 11 build 26300, VS Build Tools 17.14, Python 3.14.10):The full suite gives 78 tests and 0 failures; only
bvh_wave64_hardwareandbvh_wave64_warpare skipped. As a one-off manual check (not in CI), I removed the forwarding loop;cpu_native_tlsthen fails because the mapped image gets no thread notifications.Evidence and follow-up
The fixtures are built from this repository; there is no game data. Two points are not measured:
CI runs Python 3.12; I only ran 3.14 locally.
Refs #78
AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T17:17:50Z.
The new shared registry lock is held while invoking guest callbacks under DllMain. The patch notes that a callback that maps or releases an image can deadlock. Please keep user callbacks outside the registry lock with a defined image-lifetime protocol, or enforce and test a fail-fast reentrancy contract. Please cover multiple mapped images and removal during notification.
Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-21T17:41:52Z.
Thanks. Guest callbacks now run outside the registry lock, under a defined image-lifetime protocol, and the branch is synced on
245b207.Protocol (
tools/pe_map.cpp, top of the registry section):DllMaintakes those references under the shared registry lock and calls the guest callbacks after releasing it. A callback may therefore map or release any image, its own included.pe_map_releaseunregisters the image, marks it released and returns 1 without waiting. It returns 0 for an unknown pointer or a second release. The image's process detach and unmapping run exactly once, when the last reference goes: insidepe_map_release, or on the thread that finishes the last notification.LoadLibraryWcopies on Windows 11 build 26300 does the same (attach A, B; detach B, A).Tests:
cpu_native_lifetimeruns one child process per case.two-images-native: twoLoadLibraryWcopies, asserting the attach/detach order above on each run.two-images: twope_mapcopies with the same order. Releasing B and then A gives one process detach each.release-other-during-notification: A's TLS callback callspe_map_release(B), through a one-shot hook incpu_tls_sinkarmed for that thread only. B gets no thread notification, its process detach is recorded once, right after A's attach, and a later release of B returns 0.release-self-during-notification: A's TLS callback releases A. A's thread attach completes, its process detach follows once, no thread detach reaches it, and a later release returns 0.Manual mutations (not in CI) each fail the test:
release-othertimes out after 60 s;pe_map_release;cpu_native_tlsstill passes; allcpu_native_*tests pass, and the full suite has 83 tests with 0 failures.Not covered: a release from a different thread while a callback is running. It is safe by construction under this protocol, since finalization is deferred to the notifying thread, but there is no test for it yet. I can add one with a small blocking helper if you want it in this PR.
AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T17:58:55Z.
The latest revision moves guest callbacks outside the registry lock and adds lifetime tests. GitHub still reports a merge conflict after the mapper changes landed. Please reconcile tools/pe_map.cpp and the test registrations with current main, preserving the delay-import changes from #127, and rerun the lifetime, TLS and delay-import checks together.
Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-21T18:01:13Z.
Synced on
ccfa703after the latest merges. The only conflict was theCMakeLists.txtblock next to the new frame and delay fixtures, and both were kept. Thecpu_tls_sinkchange, which adds the calling thread and the one-shot hook, keeps thesink_recordsignature thattls_route.asmand the frame guest use.All nine
cpu_native_*tests pass on Windows 11 build 26300, includingcpu_native_delay,cpu_native_frame_warp/_hardwareandcpu_native_lifetime. The full suite has 103 tests with 0 failures.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T18:09:36Z.
The rebase retains the delay-import cases and the author reports all nine native tests passed. The new
int pe_map_releaseresult and deferred teardown intersect the API decision in #164. This is now waiting on the lead developer there.Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T19:04:43Z.
The live-base registry in this PR is the approved design for #164. Keep the C symbol and pointer parameter, with an explicit
intreturn in declarations and ctypes bindings. Exactly one caller can claim a live mapping, including concurrent releases of the same base. Returning 1 means the image has been unregistered and the caller must stop using it and its exports. Null, interior, foreign and already-released addresses return 0 without reading the address or changing anything. Once an address has been reused, a stale pointer cannot identify the earlier instance.Without an outstanding notification, process detach, removal of registered unwind tables and unmapping must finish before return. An already-admitted notification may finish, with the last reference performing teardown exactly once. Admit no further notifications after unregistering. Run guest callbacks outside the registry lock so self-release never waits on itself.
d18809a5already covers self-release and repeat release. Please resolve the current merge conflict against main, preserve both sides' fixtures, and add the missing null, interior and foreign-base assertions. On the rebased SHA, please report the lifetime, TLS and SEH Windows results, including evidence for one winner on concurrent release, in-flight teardown and unwind-table removal. Reuse the existing controls wherever they already cover a case.Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-21T19:47:16Z.
The branch is rebased on
7233cec. The only conflict was the TLS paragraph indocs/experiments.md, where the thread-notification text and thepe_map_exportreadability sentence from main are both kept.b90d1a0adds three cases for the #164 contract tocpu_native_lifetime. Like the existing cases, each runs in its own child process.release-unowned-addresses. Each of these releases returns 0:base + 0x10andbase + 1;.rsrc, which no notification touches) after it is madePAGE_NOACCESS;PAGE_NOACCESSpage;Because the two unreadable pages would fault on any read, they show that the argument is not dereferenced. The buffer keeps its bytes and its
MEM_COMMIT/PAGE_READWRITEstate, and no process detach is recorded. The base is then released with result 1. Before that call returns, the process detach is recorded,RtlLookupFunctionEntrystops finding the entry point's function, and the base is no longer committed. A second release returns 0.concurrent-release.cpu_tls_sinkgainssink_race_release, which starts eight native threads that spin on one flag and then release the same image together. It returns only after every thread has exited, thread detach included. In 50 rounds, every round has seven 0 results, one 1, one process detach and no unwind entry left.Addresses are the only identity, as the registry comment and the docs now state. After the rounds, the test maps a new copy, asserts that it took the freed address, and releases it through the stale pointer.
release-during-notification-on-another-thread. A worker's TLS callback blocks inside its thread attach, and the main thread releases the image. The first release returns 1 at once and a second returns 0. At that point the unwind entry is still present and no process detach has run. Once the callback is unblocked:Admission is defined as taking the reference under the registry lock. An admitted notification skips an image already marked released, but a callback that has already started finishes.
Results on Windows 11 build 26300.9974, MSVC 14.44.35207, at
b90d1a0:The full suite passes 122 of 122 tests. Three hardware or WARP capability checks are skipped.
Mutations of
tools/pe_map.cppatb90d1a0, each run against the case that targets it, three times forconcurrent-release:pe_map_releasetakes the shared lock instead of the exclusive oneconcurrent-releasepe_map_releaselooks up and erases without the lockconcurrent-releaseconcurrent-releasepe_map_releasecallsfinalizeinstead of dropping its referencerelease-during-notification-on-another-threadpe_map_releasereads one byte of any non-null argument firstrelease-unowned-addressesfinalizeskipsRtlDeleteFunctionTableconcurrent-releaseAI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T19:56:12Z.
The registry, concurrent release, deferred teardown and unwind checks now cover the agreed contract. One fixture assertion still depends on Windows allocation placement:
expect("reused address", reused, previous_base)inconcurrent-release. The mapper callsVirtualAlloc(nullptr, ...), so the next allocation can legitimately land elsewhere. Please condition the stale-pointer case on actual reuse; when the address differs, require release of the old address to return 0 and release the new mapping by its own base. Keep the one-winner and unwind checks unconditional.Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-21T20:06:52Z.
Done in
34f9eea: the stale-pointer case now depends on actual reuse. If the new mapping lands at the released address, releasing the stale pointer returns 1 and records that mapping's process detach. Otherwise the stale release returns 0 with no detach, and the new mapping is released by its own base. The one-winner and unwind checks are unchanged and unconditional.On Windows 11 build 26300.9974,
--run concurrent-releasetook the reuse branch. In a second run, the old base was held with a temporaryVirtualAlloc(MEM_RESERVE)that is not part of the commit. The new mapping then landed elsewhere and the other branch passed. Both runs printed50 rounds of 8 native concurrent releases: one winner each. The full suite passes 122 of 122 tests. This supersedes the sentence in my previous comment saying that the test asserts the new copy took the freed address.AI-assisted.