fix(relinker): reserve space after the Mach-O load commands for the code signature #11284
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11284"
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 boykopovar/AnyPS5. Originally posted by iamthehimansh on 2026-10-25T23:50:10Z.
What
A relinked macOS executable reserves no space after its Mach-O load commands: the entry stub starts right after them.
codesignadds anLC_CODE_SIGNATUREcommand to the end of that list, so signing the relinked game overwrites the first bytes of the entry stub, and the game exits withSIGILL(exit 132) before any guest code runs.elfpatcher/src/macos/MacOsImage.cppnow leaves0x400bytes after the load commands before placing the entry stub, like ld's-headerpad. The existing "load commands do not fit" check still applies, now with the reserve included. This file builds on every host, because the relinker can produce Mac executables anywhere, but the change only affects Mach-O output.This PR first also carried the guest entry, guest TLS and main-thread SDL code. @imellag's earlier #11047 and #11048 carry the same imports, and @krishalivaibhav's #10876 has the same
ApplicationProcessParameters.cppchange, so those parts were dropped here. Of the four other open PRs that changeMacOsImage.cpp(#11045, #10946, #10861, #10249), none touches the stub offset.Tested
relinker/tests/test_macos_output.py(themacos_outputCTest) now checks the reserve. On every host, each relinked output must keep its entry stub at least0x400bytes past the load commands. On macOS it also signs the import output withcodesign -s - -fand checks that the entry stub bytes are unchanged. Relinker-only Release build withBUILD_TESTING=ON, Apple M2 Pro:MacOsImage.cppmacos_outputmainimport: entry stub at 0x3e0 leaves under 0x400 bytes after the load commands for codesignEarlier runs on the same Apple M2 Pro, macOS 26.6.2, x86_64 Release under Rosetta 2, on
mainat4b1a9a3b2:relinker_conversion,relinker_game_patch,macos_tls_aluandrelinker_executablespass. A relinked game still can't start onmainalone: it needs #11047 for the guest TLS imports, and libkernel to build on macOS. #11048 and #10876 are now onmain. With the same reserve on #8658's branch, ccleste got past that exit on this machine, opened its window and created a MoltenVK device and swapchain.python3 tools/check_conventions.py --base origin/mainpasses.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T23:56:00Z.
Review: CHANGES_REQUESTED
I found a race in the new macOS guest TLS registration path (
GuestTls.cpp, especially thedescriptor->Keyreads inAps5GuestTlsAllocateBlock_nid_no_patchandAps5GuestTlsGetAddr_nid_no_patch). Multiple guest threads can make their first TLS access at the same time. The mutex serializespthread_key_createinsideAps5GuestTlsRegister_nid_no_patch, but the callers read and write the shared non-atomicKeyoutside that mutex, which is a C++ data race and can also expose a stale/partially published key. Please make every access toKeyuse one synchronization scheme (for example, route initialization and reads through a locked helper or an atomic representation that preserves the descriptor ABI), and add a test that races first access from multiple threads and verifies a single registered key and distinct per-thread blocks.Local
python3 tools/check_conventions.py --base upstream/main --head upstream/pr/11284andgit diff --check upstream/main...upstream/pr/11284pass. Hosted Linux relinker/ASan and policy checks pass; macOS relinker, macOS register-context, and other listed macOS jobs are still pending. I have not run macOS product tests locally.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T00:21:24Z.
Review: CHANGES_REQUESTED
Re-reviewed exact head
988eabf3dc3448b9cc1d51c267ec72f1ebe8d0d5. The newRegisteredKeyhelper uses an acquire/releaseatomic_reffor each access toGuestTlsDescriptor::Key, and the added concurrent first-access test exercises registration through both TLS entry paths. That addresses my earlier data-race finding; the author reports the focused test and ThreadSanitizer pass, which I have not independently reproduced.There is still a macOS main-thread gap in the video path.
presentLoopruns onpresentThreadand callswindowExtensions(window.Handle())both at startup and inprocessFlip;windowExtensionsinvokesSDL_Vulkan_GetInstanceExtensionswithoutRunOnMainThread. It also passeswindowDrawableSizeto the presentation driver; that callback callsSDL_GetWindowFlagsandSDL_Vulkan_GetDrawableSizedirectly, andpresentLoopcallsSDL_GetWindowSizedirectly after a flip. These are SDL video/window operations on worker or renderer threads. SDL2's own FAQ says SDL video functions should be called from the application's main thread: https://wiki.libsdl.org/SDL2/FAQDevelopment#can-i-call-sdl-video-functions-from-multiple-threads. Please marshal these paths through the main-thread helper (including the actual thread that invokes the drawable-size callback), or provide an exact-head macOS test demonstrating that these specific calls are safe on the supported SDL/Cocoa backend. The current PR says the full libc and libSceVideoOut product cannot link on macOS, so the older ccleste smoke run on #8658 does not verify these new call paths.Current hosted conventions, eligibility, labels and report checks pass; cross-platform relinker and macOS register-context/write-watch checks are still pending. Product builds are path-skipped.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T00:35:31Z.
Review: CHANGES_REQUESTED
Follow-up on exact head
ee2bfe5dbb9a1053d096f334e9b04381f8e75fba: the new wrappers now marshalwindowExtensions,windowDrawableSize, and the post-flip size query, which addresses those paths from my previous review. One main-thread violation remains in the event path:VideoOutDriver::presentLoopruns onpresentThreadand callspadInput.HandleEvent. That can callDisplayWindow::ToggleFullscreen(PadInput.cpp), whereSDL_GetWindowFlags(window)is still evaluated before theRunOnMainThreadwrapper aroundSDL_SetWindowFullscreen. Please move both the flags read and fullscreen update into the main-thread closure.presentLoopcallsSDL_GetWindowIDfor mouse, keyboard, and IME event routing.SDL_GetWindowIDis part of SDL's video API; SDL's FAQ says video functions should only be called on the application main thread (https://wiki.libsdl.org/SDL2/FAQDevelopment). Please marshal these queries or capture the window ID while on the main thread and use the captured value in the event loop.There is also a diagnostic issue in the new initialization wrapper:
SDL_InitSubSystemruns insideRunOnMainThread, but on failure the subsequentSDL_GetError()runs on the calling thread. SDL documents error strings as per-thread (https://wiki.libsdl.org/SDL2/SDL_GetError), so that message can lose the actual initialization error. Capture/copy the error inside the dispatched closure.The current head's macOS compile/test checks are still pending, and this environment has no macOS or Vulkan device to reproduce them. Please address the remaining video calls, retain the exact-head macOS compile/focused test evidence, and rerun the relevant checks before requesting another review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T00:37:01Z.
Review: CHANGES_REQUESTED
Reviewed exact head
d67dc5ba43d616b34a4929df230adbc732b9fdbb. The new mouse-mode operations now marshal to main, but the remaining SDL video calls and thread-local error handling still need attention:VideoOutDriver::presentLoopcallspadInput.HandleEventonpresentThread. That path reachesDisplayWindow::ToggleFullscreen, whoseSDL_GetWindowFlags(window)remains outsideRunOnMainThread; onlySDL_SetWindowFullscreenis marshaled. The event handlers also callSDL_GetWindowIDdirectly for mouse, keyboard and IME routing. These are SDL video API calls from the presentation thread. SDL's SDL2 FAQ says video functions should only be called from the application main thread: https://wiki.libsdl.org/SDL2/FAQDevelopment. Please marshal those queries or capture the needed values on the main thread.PadInput::setMouseModeandMouseInput::UpdateCapturechanges callSDL_SetRelativeMouseModeon main but callSDL_GetErroron the caller thread when it fails. The earlierSDL_InitSubSystemwrapper has the same problem. SDL2 error strings are per-thread (https://wiki.libsdl.org/SDL2/SDL_GetError), so copy the error inside theRunOnMainThreadclosure and report that copy outside it.The newest head's conventions/report checks are still running, and there is no macOS device in this environment for an independent compile or runtime check. Please address these paths and provide exact-head macOS compile/focused-test results before requesting another review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T05:47:29Z.
Review: DISMISSED
I rechecked the latest head
38a945da2b1c6a576a0399c482d732de318ebde3. The added cached window ID removes the remainingSDL_GetWindowIDcalls from presentation-thread routing, andToggleFullscreennow reads flags inside the main-thread closure; SDL error text is copied while still in that closure. Those changes address the main-thread issues from my prior reviews. I cannot approve this revision yet becausetools/check_main_sync.py --base upstream/main --head refs/review/pr11284-currentreports 5 commits ahead and 196 behind currentmain(14c99b52ac), and the reported macOS compile/TSan/test results are from the olderacb900c19base. Please rebase/refresh and rerun the focused macOS TLS/video tests on the resulting exact head. Hosted policy checks pass; product and relinker jobs are path-skipped.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-27T01:48:05Z.
Review: COMMENTED
Red to green on a Mac with real
codesign, at head27147acbbapplied ontomain27ad7244d2.Machine: Apple M2 Max, macOS 27.2, Xcode 27 clang, relinker-only Release, built both arm64-native and x86_64 (run under Rosetta 2,
/usr/bin/python33.9.6).Input:
macos/import_fixture.pywith the entry code replaced bymov eax, 42; ret, relinked with--macos --skip-sce-module. To run the output without the real runtime I put two x86_64 stub dylibs inlibs/(@rpath/libc.prxexporting the two NID symbols,@rpath/libkernel.prxwith anAps5StartGuest_nid_no_patchthat calls the entry and exits with its return value). Thencodesign -s - -fon a copy and ran both under Rosetta:entryoffcodesign -s -mainOn
main,codesignappends its 16-byteLC_CODE_SIGNATUREat 0x3d8 (1d000000 10000000 60300100 90030000), so the first 8 bytes of the entry stub at 0x3e0 becomedataoff/datasize. Worth noting:codesign --verify -von that broken binary still reportssatisfies its Designated Requirement, so the corruption is silent until the first instruction runs. With the PR the same signing leaves the stub untouched and the signed output runs. The arm64-built and Rosetta-built relinkers write byte-identical output.Full relinker-only suite with the PR: arm64 309/309, x86_64 under Rosetta 307/307 (same counts as
main).One gap: the PR adds no test, so nothing stops the reserve from regressing. A host-independent check in
test_macos_output.py(entryoff >= 32 + sizeofcmds + 16, or>= ... + 0x400to pin the reserve) would fail onmainand pass here; on macOS hosts it could alsocodesign -s -the output and check that the bytes atentryoffare unchanged.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-27T03:07:57Z.
Review: COMMENTED
Re-ran at head
1220ef855merged ontomainabed035468. Apple M2 Max, macOS 27.2, Xcode 27 clang, relinker-only Release,BUILD_TESTING=ON, built arm64-native and x86_64 (tests run under Rosetta 2 with/usr/bin/python33.9.6).macos_output,main'sMacOsImage.cppentry stub at 0x3e0 leaves under 0x400 bytes ...codesign overwrote the entry stubThe structural check fails first, so on
mainthecodesignhalf never gets to run in your table. With that line removed, thecodesign -s - -fcomparison catches the overwritten entry stub on its own, on both architectures. Both halves of the test work independently.guest_module_argsandguest_zero_initializerpass here in the relinker-only builds: arm64 with and without the PR, and x86_64 under Rosetta 2 with it. The failures in your build look local to that build.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.