Call TLS callbacks and the entry point from pe_map in the Windows order #77
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/78"
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-21T15:38:12Z.
Change
Follow-up to #46, now closed. Before this change
pe_maprejected every image with a TLS directory or an entry point, which covers almost every real DLL. It now notifies a mapped image the wayLoadLibraryandFreeLibrarydo on Windows.DLL_PROCESS_ATTACH, the module base and a null reserved argument.pe_map_release): TLS callbacks, then the entry point, withDLL_PROCESS_DETACH.MAP_INIT_FAILED.LoadLibraryWfails with error 1114 in the same case.LoadLibraryWdoes the same, and for an EXE the entry point is the program itself.ThreadLocalStoragePointerarray, soAddressOfIndexis left as linked. In the test process, Windows wrote index 8 for this image.I did not assume the order. I measured it on Windows 11 build 26300 with
tls_routeloaded throughLoadLibraryW, and the test compares both loaders. I know of no Microsoft documentation for the detach order, so the docs describe it as measured.New files:
tests/cpu/tls_route.asm: a handwritten_tls_usedwith one callback and an empty template, plus/ENTRY:dll_entry. Therefusevariant returns FALSE from the entry point.tests/cpu/tls_sink.cpp: records every call and outlives the image, so detach calls stay observable.tests/cpu/native_tls_check.py(cpu_native_tls): one child process per loader and image.Verification
At the head of this branch, on
b7f7e3f(Windows 11 build 26300, VS Build Tools 17.14, Python 3.14.10):The full suite gives 75 tests and 0 failures; only
bvh_wave64_hardwareandbvh_wave64_warpare skipped.Controls in
cpu_native_tls:SizeOfZeroFill, an entry point in.dataand a TLS callback pointing into.dataare rejected before any notification;IMAGE_FILE_DLLcleared, with and without an entry point, both loaders load and release the copy without any call;cpu_native_mapnow expects "entry point outside executable section" for an out-of-image entry point.As one-off manual checks (not in CI), I broke
pe_mapeight ways, and each madecpu_native_tlsfail:Evidence and follow-up
The fixtures are original and built from this repository; there is no game data. Still open: implicit TLS data, thread attach and detach for threads created after mapping, and delay imports. CI runs Python 3.12; I only ran 3.14 locally.
AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T15:46:21Z.
Thanks for the native Windows comparison. Please address one review finding before merge: tls_callbacks reads the directory and each slot after section protections are applied, including during release. Image bounds alone do not guarantee these bytes are readable. Please validate readability over each full read and cover the rejection with a source-owned fixture. The branch also currently conflicts with main. Please sync it and rerun the native TLS comparisons.
pe_map_image: reject an empty relocation directory when rebasing #86Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-21T16:02:48Z.
Thanks for the review. Both points are addressed, and the branch is rebased on
88fd8c5.Readability.
tls_callbacksnow callsreadable(address, size)before reading the TLS directory and before reading each 8-byte callback slot. That helper queries the live protection of every page in the range withVirtualQuery: the pages must be committed and notPAGE_NOACCESS, execute-only orPAGE_GUARD. It checks the live protection rather than the section headers, so it also covers pages the image reprotects after mapping. It is used on attach and inpe_map_release. Each value is copied once after its check, so the validated value is the one used. The query is not atomic with the read; a comment says that concurrent reprotection by another thread is not handled.Source-owned fixtures.
tls_route.asmnow places the callback list in its own read-only.tlscbsection, so the directory and the list can be made unreadable separately. New controls incpu_native_tls:.datawith READ and WRITE cleared, which makes the directory inaccessible, is rejected as "TLS directory not readable" before any notification;.tlscbwith READ cleared is rejected as "TLS callback list not readable" before any notification;PAGE_NOACCESSwithVirtualProtectbeforepe_map_release: the list is skipped and only the entry point is detached.Without either
readable()check, the test fails with an access violation (two manual mutants, not in CI). The native TLS comparisons were rerun after the sync. Locallycpu_native_map,cpu_native_sehandcpu_native_tlspass, and the full suite has 76 tests with 0 failures (wave64 skipped on the RTX 5080 and WARP).pe_map_exportreads the export tables without this check; the docs note that as a remaining limit.AI-assisted.