pe_map: unwind mapped longjmp chains without the CFG long-jump target check #3467
No reviewers
Labels
No labels
accessibility
awaiting author
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
priority: P0
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/R0Y00--Xbox2Windows!3467
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3465"
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?
@Not-Saii on GitHub · 2026-11-29 15:20 UTC · original
Not-Saii:pe-map/mapped-longjmp-cfg→main· +456 −18 · 9 files · 12 commits · openRefs #1293
The approach was agreed with @MasterDomino and @R0Y00 outside the issue.
What changes
With Control Flow Guard enforced, the CRT
longjmpgoes throughRtlUnwindExand its long-jump target check. That check accepts only targets in images the loader registered, so alongjmpinside ape_mapmapping failed fast with0xC0000409. #1614 added a named skip for that case. This PR adds a replacement for mapped images and leaves the host's CFG policy unchanged.pe_map_longjmp(tools/pe_map.cpp) walks the frames from the caller to thejmp_bufframe withRtlLookupFunctionEntryandRtlVirtualUnwind. It calls each frame's language handler the wayRtlUnwindExdoes: aSTATUS_LONGJUMPrecord,EXCEPTION_UNWINDING, andEXCEPTION_TARGET_UNWINDon the target frame only, withTargetIpset to thejmp_buf'sRip. It then restores thejmp_bufregisters withRtlRestoreContextand no exception record. It never callsRtlUnwindEx, which is whereRtlGuardCheckLongJumpTargetis called from. That the check is not reached by any other route is inferred from the exit codes below.pe_mapmappings, a walk that leaves the stack or passes the target frame, or an unexpected disposition raises the noncontinuable0xE0000203.jmp_bufwith a zeroFrameis restored without unwinding.longjmpto the replacement.X2W_PE_MAP_CRT_LONGJMP=1binds the CRTlongjmpinstead, and the #1614 skip still applies in that case.launch_cfg(tests/cpu/load_config_callback_check.py) now takes an argument list, a stdin file, an environment and an output prefix. The existing caller is unchanged.cpu_mapped_longjmp_cfg(tests/cmake/cpu_mapped_longjmp.cmake) with a source-built fixture,tests/cpu/mapped_longjmp.c. Note:docs/experiments/pe-map-longjmp.md.Measurement first: restore under CFG
Windows 11 build 26300, system CFG off. Each case ran in a child with a per-process CFG policy (
ALWAYS_ON1 << 40,ALWAYS_OFF2 << 40). The target was in a private page, allocated read-write and then made read-execute.RtlRestoreContext(ctx, NULL), RSP on the thread stackRtlRestoreContext(ctx, NULL), RSP outside the thread stack0xC0000409RtlRestoreContextwith aSTATUS_LONGJUMPrecord0xC0000409longjmp0xC0000409Without an exception record, the restore checks the stack pointer, not the target's image. These causes are inferred from exit codes; the fast-fail subcode was not read.
Tests
Commit:
6c6d53e2. Windows 11 Pro build 26300, MSVC (VS 2022,devpreset), Windows SDK 10.0.26100, Python 3.12. No GPU.5/5 passed, 0 skipped, 6.1 s total (
cpu_mapped_longjmp_cfg2.4 s). Separately,ctest --preset dev --no-tests=error -R '^cpu_archive_texture_gpu_warp$'passed in 1.95 s, and its mapped truncated case ran (pe_map truncated: 1 frames, exit 2) rather than skipping. The host's system CFG setting is off;cpu_mapped_longjmp_cfgsets CFG per child, so its CFG-on cases do not depend on it. The mutants were standaloneclbuilds of the patchedpe_map.cpp. They were run with the check against the CMake-built fixture, reader, codec and blast at this commit.5c6cf83badds only the preset change. At the merged head (withmainat077bff3a),ctest --preset pr -Nlists the test, andctest --preset pr --no-tests=error -R '^cpu_mapped_longjmp_cfg$'passed in 3.3 s.cpu_mapped_longjmp_cfgruns each case with CFG on and with CFG off:pe_map, writes the same order:plain,jump,jump finally abnormal,nested finally abnormal,inner finally abnormal,resumed,outer finally.setjmpreturns 1 for value 0 and 7 for value 7. The nested__exceptfilter is not called.Negative controls:
longjmpbound), CFG on: the fixture fails fast with0xC0000409. The unmarked blast case fails fast afterinput 0. The marked one reports the #1614 skip (77).0xE0000203. Its__exceptfilter runs before any__finally, so nothing was unwound before the refusal.pe_map.cppand run against the check. All fail at the mapped fixture with CFG on:plain,jump,resumed,outer finallyEXCEPTION_TARGET_UNWINDouter finally abnormalbeforeresumed, thenouter finallyTargetIp0EXCEPTION_UNWINDINGplain,jump,filter,inner finally abnormal,resumed,outer finallyLimits
ntdllbuild (26300). Since5c6cf83btheprpreset includescpu_mapped_longjmp_cfg, so the PR check runs it on the hosted runner. The test sets CFG on and off per child, so that run covers both on the runner's build.6c6d53e2(run 46343854378,windows-2025, Debug and /O2) passedcpu_native_archive_reader,cpu_native_archive_blast,cpu_archive_texture_publication,cpu_archive_texture_gpu_warpandcpu_native_load_config_callbackin both jobs. Both jobs then hit the 30-minute job limit, which the recent full runs onmainalso hit, before reaching the new test.0xE0000203). Attach runs before the image joinsmapped_images()(tools/pe_map.cpp,notifybeforepush_back). This fails closed. Registering before attach would change whenpe_mapsends notifications, so it is not changed here.cpu_archive_texture_gpu_hardwarewas not run (no physical-GPU run for this change). It uses the same mapped runner ascpu_archive_texture_gpu_warp, which passed./guard:cfimages) is not read. The target must be the frame of a live caller, whichRtlUnwindExrequires too.__CxxFrameHandler4), collided unwinds, and user-mode shadow stacks.AI-assisted.
🤖 Generated with Claude Code
@lukaslw-lab on GitHub · 2026-11-30 12:38 UTC · original · approved these changes
Reviewed
6c6d53e2by reading the diff; not built or run.Verdict: approve.
pe_map_longjmpfollowsRtlUnwindEx's target unwind closely, and it checks the whole chain before any handler runs. No MUST.cpu_mapped_longjmp_cfgto theprpreset filter (CMakePresets.json). The Limits section says CI will be the first run of it on anotherntdllbuild, but the filter does not include it, so CI did not run it at this head. In CI, the replacement is reached only throughcpu_archive_texture_gpu_warp(passed, 4.3 s). Only the truncated blast case reaches it there, and the fixture's unwind order, the CRT control and the host-handler refusal are not run.0xE0000203(host handler). Attach runs attools/pe_map.cpp:1439, but the record joinsmapped_images()only at:1449, somapped_basedoes not know it yet. This is rare and fails closed, but a line in the Limits section would help.mapped_basetakesSharedLockper frame. Guest callbacks run outsidemapped_lock(:1036), so a longjmp from mapped code cannot self-deadlock.RtlUnwindEx's.TargetIpplusEXCEPTION_TARGET_UNWINDon the target frame only give__C_specific_handlerthe right scope cut. The fixture shows it:inner finally abnormalbeforeresumed,outer finallyonly on return._JUMP_BUFFER, including Xmm6–15, MxCsr and the x87 control word. Value 0 maps to 1, andFrame == 0skips the unwind, as the CRT does.archive_check.py). No runtime path changes.Tests:
cpu_mapped_longjmp_cfgruns each case under CFG on and off: the fixture order loaded and mapped, the__exceptfilter that must not run, the CRT fail-fast control, the host-relay refusal and truncated blast. It reaches every refusal reason exceptLONGJMP_DISPOSITION.@Not-Saii on GitHub · 2026-11-30 13:42 UTC · original
Thanks @lukaslw-lab.
5c6cf83b.cpu_mapped_longjmp_cfgis now in theprtest filter, and its targets (cpu_mapped_longjmp,archive_codec,blast_source,archive_reader_lifted) are in theprbuild list. Locally,ctest --preset pr -Nlists it, and it passes at the merged head in 3.3 s. The branch also merges currentmain(7f2b8d8a). I've also corrected the Limits section, which had said CI would run the test.notify(... DLL_PROCESS_ATTACH)runs before the record joinsmapped_images(). I added it to Limits. Registering before attach would change whenpe_mapsends notifications, so I left that out of this PR.AI-assisted.
@lukaslw-lab on GitHub · 2026-11-30 18:03 UTC · original · approved these changes
Reviewed
43fefff6by reading the diff; not built or run.Verdict: approve. Since
6c6d53e2, the PR's own changes are only theprpreset lines (5c6cf83b).tools/pe_map.cpp, the fixture and both checks are unchanged. I read them again at this head. No MUST, no SHOULD.tools/pe_map.cpp,pe_map_longjmpandunwind_to_jump):__finallyruns.Framerestores without unwinding, as the CRT does, and value 0 returns 1.Xmm6..Xmm15are contiguous in both_JUMP_BUFFERandCONTEXT, so the singlememcpyis exact.cpu_mapped_longjmp_cfguses.archive_reader_liftedbringsarchive_reader.exefrombuild_archive_reader, andpe_mapwas already in the list. The regex adds only the exact test name.Tests:
cpu_mapped_longjmp_cfgruns every case in children started with CFG on and with CFG off:pe_map_longjmpbound;longjmpcontrol, which fails fast with CFG on;__finallybefore it;CI ran it at this head: Windows job,
cpu_mapped_longjmp_cfg ... Passed 8.48 sec, 90/90 passed. The_hardwaretexture variant was not run, as the PR body says.View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.