pe: notify a started EXE's TLS callbacks on thread attach and detach #913
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/920"
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 rasptum on 2026-11-23T17:10:23Z.
Refs #165, Refs #870
What this changes
This is the source-built EXE comparison still open on #165: TLS notifications on worker threads and a startup log for an EXE's own image. Static intake on #870 reported two TLS callbacks in the main executable itself, not in a DLL.
tools/pe_map.cpp(minimal):pe_map_runcalled an EXE's TLS callbacks only for process attach.notify()returns early for an image withoutIMAGE_FILE_DLL, so threads a mapped EXE started never reached its callbacks. Under a Windows launch, an EXE's TLS callbacks receive thread attach and detach. Nowpe_map_runmarks the EXE's registration as started. From then until release,pe_map.dll's thread notifications call the EXE's TLS callbacks, never its entry point, withDLL_THREAD_ATTACHandDLL_THREAD_DETACH. A mapped EXE that was never started still receives nothing, as underLoadLibrary. The cookie path from #899 is untouched.Fixture
cpu_exe_tls(tests/cpu/exe_tls.cpp, CRT-free, MSVC): the EXE supplies its own TLS directory (_tls_used,_tls_index,.tlsmarkers,.CRT$XLA/XLZ). Its template holds the int and 8 KiB array from #883, so it spans a page boundary. Callbacks are in.CRT$XLBand.CRT$XLM. The entry point writes 20 to the main thread's copy and starts two workers that are alive at the same time: A writes 50, and B starts after A's write and writes 60. It then joins A, then B, so the order is fixed, and prints one ordered log.Test
cpu_native_exe_tls(tests/cpu/native_exe_tls_check.py) reuses the #883 helpers (layout,swap_callbacks,zero_second_page). It launches the EXE with Windows, then maps it withpe_map_imageand starts it withpe_map_runin a child process. The log and the exit code must be identical and must equal the expected log:Each callback event reads thread, reason, caller.
m11is the main thread on process attach, caller 1.a2x/a3xandb2x/b3xare the workers' thread attach and detach.Eis the entry point.f/w/lare a thread's own first, written and last observations.Negative controls: both are rejected under both loaders.
callback order: m1 ran callers ['3', '1'].template: callbacks on m1 observed ['4d40007', '4d40007'], expected ['7', '7'](dev).Without the
pe_map.cppchange, using the mapper from current main, the test fails withmapped: notifications for ['m1'], expected ['a2', 'a3', 'b2', 'b3', 'm1'].Test
Windows 10 Pro 10.0.19045.6466, MSVC 19.44.35207.1 x64, Ninja.
ef15a16(rebased on main with #899):ctest -R '^cpu_native_(exe_start|startup|tls|tls_value|tls_data|lifetime|exe_tls|load_cookie)$'passed 8/8 ondevand 8/8 onrelease.41b1de6, rebased again after main moved 12 commits. The merge was clean, and those commits did not touchpe_map.cppor the harness modules.cpu_native_exe_tls,cpu_native_startup,cpu_native_exe_startandcpu_native_tls_valuepassed 4/4 ondevand onrelease.Environment note: the C: system drive filled up during the first
releaserun onef15a16.cpu_native_tls_dataandcpu_native_tls_valuethen failed with Windows error 112 while writing the TLS carrier to%TEMP%. Rerunning withTMP/TEMPon another drive passed 8/8. This was a host disk condition, not a code result.Scope
This checks the EXE TLS stage ahead of the current #870 boundary (import binding, #880). It does not claim the executable reaches TLS or its entry point. Not covered: process detach at exit, C-runtime startup in a mapped EXE,
GetModuleHandle(NULL)and the PEB.🤖 Generated with Claude Code
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T17:15:39Z.
The two joined workers and per-thread log are a good match for the remaining #165 fixture. One ordering boundary needs a control: started is currently published before process-attach TLS callbacks finish, so a host thread notification can enter the EXE's TLS callbacks before process initialization has completed. Please define and test when notifications become eligible. For the entry-created workers in this fixture, publish after ordered process-attach callbacks and before calling the entry, with host-side thread creation during startup outside that scoped guarantee. Verify a mapped-only or refused run gets no notifications and keep EXE process detach separate. The child only needs a fixed kernel32 provider; please bind that explicitly instead of loading whatever DLL names arrive from the fixture PE.
Imported from R0Y00/Xbox2Windows. Originally posted by rasptum on 2026-11-23T17:24:03Z.
Thanks. Addressed in
c631998(a new commit on top, no force-push):pe_map_runnow publishesstartedonly after every process-attach TLS callback has returned, and before it calls the entry point. A thread that attaches during the process-attach callbacks is not notified, whether a callback or the host started it. That case is documented as out of scope inpe_map_run's comment and indocs/experiments.md, because Windows holds such a thread until process initialization ends. The control iscpu_exe_tls_early: its first process-attach callback starts and joins a probe thread. Underpe_mapthe probe runs (LOG:m11=7 pf=7 m13=7 E ...) and receives no EXE notification. With the previous ordering the test fails:notifications for [..., 'p2', 'p3'], and counts[0, 2, 6, 6]instead of[0, 2, 4, 4]. Because of that hold, the variant runs mapped only.exe_tls_mirror) and reads them after release. Two host threads attach and detach in each case. An EXE that is mapped but never started, and one whosepe_map_runis refused (null exit code, returns 0), both count[0, 0, 0, 0].notify_mappedforwards onlyDLL_THREAD_ATTACH/DLL_THREAD_DETACH. A started run counts 0 process detaches, 2 process attaches, 4 thread attaches and 4 thread detaches, including through release.kernel32.dllto the host's kernel32. Any other name stays unresolved, so the map fails. The test checks that kernel32 was the only DLL requested.Results: Windows 10 Pro 10.0.19045.6466, MSVC 19.44.35207.1.
ctest -R '^cpu_native_(exe_start|startup|tls|tls_value|tls_data|lifetime|exe_tls|load_cookie)$'passes 8/8 ondevand 8/8 onrelease. The native and mapped startup log and exit code are unchanged, and both negative controls are still rejected under both loaders.form_coverage_check,docs_links_checkandbidi_controls_checkpass.TMP/TEMPwere on a non-system drive for these runs.AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T17:31:35Z.
I reran the source-built comparison on
c631998with Windows x64 Release and MSVC 19.51. The original EXE reported exit 0x20014 and the same ordered log under Windows and pe_map. The early worker ran without a callback, and mapped-only and refused runs both recorded zero notifications. Both callback-order and cross-page template mutations failed under both loaders. This verifies the entry-created workers and the documented startup boundary. I will keep #165 open for its remaining coverage.