pe: report the pe_map main EXE as the process image #1144
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1151"
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 mumba17 on 2026-11-24T01:47:26Z.
Change
Under a Windows launch the EXE is the process image. The PEB's
ImageBaseAddressholds its base,GetModuleHandle(NULL)returns it, andFindResource(NULL, ...)searches its resources. Ape_mapEXE found the host there instead, so it read the host's headers and resources.docs/experiments.mdlistedGetModuleHandle(NULL)and the PEB as not covered.pe_map_run_mainwrites the mapped EXE's base intoImageBaseAddressbefore the process-attach callbacks, keeping the host's value. This applies with or without a TLS template.pe_map_releasewrites the host's value back. A refused release (participants still running, #988) changes nothing, as before.pe_map_run_mainreturns 0 and runs nothing while another mapped image is reported.pe_map_run.GetModuleFileName(NULL)andGetModuleHandleExon an image address still describe the host. This is documented as not covered.cpu_exe_io!finish_exe: it treatedGetModuleHandleW(NULL) == image_baseas "launched by Windows". Under a reported main image that is now true, so it ended the host (cpu_package_shim_lifetimeruns two packages in one process). It now also requires the loader to know the image (GetModuleHandleExW(FROM_ADDRESS)), so a mapped main image gets its exit code back and a Windows launch still exits.Test
cpu_exe_process_imageis a CRT-free EXE with one RCDATA resource (tests/cpu/exe_process_image.{cpp,rc}). It logs:self:GetModuleHandleW(NULL)is its own__ImageBasehdr: the headers reached through that handle name its entry pointres:FindResourceW(NULL, 101, RT_RCDATA)returns its exact bytes, inside the imagemiss: the absent resource 102 is not foundk32:GetModuleHandleW(L"kernel32.dll")still resolvestests/cpu/process_image_check.py, registered ascpu_native_process_image:self=y hdr=y res=y miss=y k32=y X=0, exit 0pe_run_package.py --system kernel32.dllexit_code0pe_map_run_main, thenpe_map_releasereleased=1; afterwards the host'sGetModuleHandleW(NULL)is its own base againpe_map_run(negative control)self=n hdr=n res=n miss=y k32=y X=3: the EXE finds the hostpe_map_run_mainreturns 0 and logs nothing while the first is reported; after the first release it starts and logs the Windows result; each release restores the host's valueMutation controls (local, not committed):
runner log: expected ['self=y ...'], got ['self=n hdr=n res=n miss=y k32=y X=3']0xC0000005(the PEB still names the released image)Verification
Environment: Windows 11 Pro build 26300, MSVC 19.44.35207.1 x64, Python 3.13.1 (
out/pe-env). Rebased ona8692e5.cpu_native_process_imagepasses indevandrelease.cpu_*,pe_run_package*,pe_package*,static_exe*andxgameruntime_pre*test passes indev, as do the 8cpu_*_hardwaretests run one at a time.-j 6across all 209,cpu_native_startuphit its time limit once at 32 s. It passed 5 of 5 serial runs and in a 52-test-j 6subset, so I read it as load. It is the same pattern as the_hardwaretimeouts reported on #448.dev, and 10/10 inrelease: the three #988 slot-0 tests,cpu_package_main_image,cpu_package_shim_lifetime,cpu_native_startup,pe_run_package_trap_missing,cpu_native_dstorage,cpu_native_acphal_hal, thecpu_native_xapu*tests and this one.docs_links_check.py,bidi_controls_check.pyandgit diff --checkare clean.Local observation on the main executable (no image or log attached): under
pe_mapit logs a runtime error about failing to get its module identity, which it does not log under the Windows loader. With this change it still logs it, and the boundary is unchanged (the first missing function import, with #1098'sxmem.dll). So that message is not explained byGetModuleHandle(NULL). The loader list, which this PR leaves alone, is the next candidate, but that is a hypothesis. This PR rests on the fixture above, not on that observation.docs/experiments.mdadds a "Process image" section after "Main-image TLS slot 0".Refs #870, #988.
🤖 Generated with Claude Code
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T01:59:00Z.
Review: COMMENTED
Reviewed commit
1173ad619555842c4c34ec8dd2639c0f1f327cc2on Linux x86_64 (Python 3.14.8; Windows MSVC/PE execution relies on CI).Checks run:
python3 -B tests/gpu_report_format_check.py-> 10/10 OKpython3 -B -m py_compile tests/cpu/process_image_check.py-> exit 0Diff notes:
tools/pe_map.cpp:report_process_image/restore_process_imageunderSlotLockcleanly swap and restore x64PEB.ImageBaseAddress(gs:[0x60]+0x10) forpe_map_run_main(including images without a TLS carrier) and refuse a second concurrent main image before touchingmain_image().tests/cpu/exe_io.cpp: updatingfinish_exeto also checkGetModuleHandleExW(FROM_ADDRESS)avoids falseExitProcesscalls when ape_mapmain image setsPEB.ImageBaseAddress.tests/cpu/process_image_check.py+exe_process_image.cpp: covers Windows launch,pe_run_package.py,pe_map_run_main,pe_map_runnegative control, and concurrent-main refusal;docs/experiments.mdis updated.Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T02:04:31Z.
Review: CHANGES_REQUESTED
On
1173ad61, Windows Release passed the source-built process-image, package main-image and shim-lifetime checks (3/3). The resource lookup, plain pe_map_run control and rejected second main image behaved as described.Please also pin the new process-image state at the two TLS lifecycle boundaries. The new resource fixture has no TLS callbacks, and the current slot-zero check does not read process identity. An existing TLS fixture can assert that GetModuleHandleW(nullptr) sees the mapped base in its process-attach callback, stays there across a paused-worker refusal of pe_map_release, then returns to the original host base after the accepted release. This covers the process-global state before callbacks and through the failed teardown path.
Imported from R0Y00/Xbox2Windows. Originally posted by mumba17 on 2026-11-24T09:04:05Z.
@R0Y00, thanks. On
c9d15f70(rebased ontod6cbd5d7) I pinned the process-image state at both TLS lifecycle boundaries in the existing slot-0 fixture.pe_map.cppis unchanged.cpu_exe_tls_slot0now also records whetherGetModuleHandleW(nullptr)is its own__ImageBase(the thirdm1result). The Windows launch,pe_map_run_mainandpe_run_package.pyall logm1=yyy. Underpe_map_run(plain) it ism1=n-n, withX=9.cpu_exe_tls_slot0_hostreadsGetModuleHandleW(nullptr)right after the start, right after the refused release and right after the accepted release. It reportsprocess_image=<start><refusal><release>, whereiis the mapped base andhis the host's. Inpaused, worker c is still inside the image's code when the first release is refused. The result isiih, and c then resumes and finds its block.aliveanduntrackedare alsoiih. The scenarios without a refusal and the enroll cases arei-h. Plain ish-h.package_main_image_check.pyderives from the same log, so its explicit-provider string changed with it.Local results, Windows 11 build 26300, MSVC 19.44,
releasepreset, RTX 3090:Temporary mutations to
pe_map.cpp, one at a time, each caught:main: process_image i-i, enrollprobe: i-i; the package runner crashes at exitend_main's refusalalive: process_image ihh;pausedrun alone:ihhm1=yyn,X=1in the slot-0, enroll and package checksEvery non-hardware
cpu_test also passes (ctest --preset release -R '^cpu_' -E '_hardware$': 223/223).AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T10:40:20Z.
Review: COMMENTED
Dual-platform peer retest of #1151 (
c9d15f70):Checks
git diff --check origin/main...HEAD: clean.tests/docs_links_check.py: passed (Checked 16 Markdown files and 299 links.).tests/bidi_controls_check.py: passed (Checked 1121 tracked source, script and build/CI files: no bidirectional controls.).py_compileontests/cpu/native_exe_tls_slot0_check.py,tests/cpu/package_main_image_check.py,tests/cpu/process_image_check.py: passed.x2w_pipe.py win2025 1151 --targets pe_map,cpu_exe_process_image,cpu_exe_tls_slot0,cpu_exe_tls_slot0_pause,cpu_exe_tls_slot0_enroll,cpu_exe_tls_slot0_host --regex '^(cpu_native_process_image|cpu_package_main_image|cpu_native_exe_tls_slot0(_windows|_enroll)?)$'):--preset dev): 5/5 passed (0.69s:cpu_native_exe_tls_slot0_windows,cpu_native_exe_tls_slot0_enroll,cpu_native_exe_tls_slot0,cpu_native_process_image,cpu_package_main_image)./O2,--preset release): 5/5 passed (0.78s: same 5/5 passed).Audit notes
tools/pe_map.cpp:report_process_image/restore_process_imageupdatePEB->ImageBaseAddressunderSlotLockviaInterlockedExchangePointer, cover both TLS-template and non-TLS images inpe_map_run_main, refuse a second main image whileg_process_image.baseis set, and restore the host's value only on an acceptedpe_map_release.tests/cpu/exe_io.cpp:finish_exenow checksGetModuleHandleExWloader membership alongsideGetModuleHandleW(nullptr)sope_map_run_mainreturns to the host instead of callingExitProcess.pe_map_runplaincontrol leavingImageBaseAddressuntouched,twicerefusal while active + recovery after release) are covered and passing.Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-25T14:19:07Z.
Review: APPROVED
Rechecked
c9d15f70. Windows Release passed the six process-image, package and TLS checks. The mapped image is visible from the process-attach callback, a paused worker's refused release preserves that identity, and the accepted release restores the host. The Windows launch and the plain pe_map_run control both retained the expected results. This clears my previous lifecycle finding.