Cygwin: aarch64: fix makecontext/setcontext and signal-handler asm#2
Open
mayankag-qti wants to merge 1 commit into
Open
Cygwin: aarch64: fix makecontext/setcontext and signal-handler asm#2mayankag-qti wants to merge 1 commit into
mayankag-qti wants to merge 1 commit into
Conversation
Three ARM64-specific problems in exceptions.cc prevented makecontext, setcontext and swapcontext from working and could corrupt state after a signal delivered on an alternate stack: * setcontext: RtlRestoreContext raises STATUS_ILLEGAL_INSTRUCTION when handed a synthetic context built by makecontext (as opposed to one captured by RtlCaptureContext/GetThreadContext) - it rejects an arbitrary PC and a stack outside the thread's registered range. Restore the registers by hand and branch to the saved PC instead, as glibc/musl do for aarch64. * __cont_link_context: the old trampoline copied the uc_link slot address into SP before reading it. That address is only 8-byte aligned when makecontext is called with an odd number of stack arguments, so the access through SP faulted. Read via x19 and mask into SP in one step. Tail-call setcontext/cygwin_exit with 'b' instead of 'bl' so the noreturn frame stays leaf for SEH unwinding, and load the exit code before the NULL-uc_link branch so cygwin_exit(0xff) is actually used. * call_signal_handler: the alternate-stack asm calls altstack_wrapper() but only declared x0-x7,x9,x10 clobbered. Declare every caller-saved register the call may touch (x8, x11-x17, v0-v7, v16-v31, cc); x18 (the Windows TEB pointer) and v8-v15 (callee-saved on Windows) are preserved and stay out of the list.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Three ARM64-specific problems in exceptions.cc prevented makecontext, setcontext and swapcontext from working and could corrupt state after a signal delivered on an alternate stack:
setcontext: RtlRestoreContext raises STATUS_ILLEGAL_INSTRUCTION when handed a synthetic context built by makecontext (as opposed to one captured by RtlCaptureContext/GetThreadContext) - it rejects an arbitrary PC and a stack outside the thread's registered range. Restore the registers by hand and branch to the saved PC instead, as glibc/musl do for aarch64.
__cont_link_context: the old trampoline copied the uc_link slot address into SP before reading it. That address is only 8-byte aligned when makecontext is called with an odd number of stack arguments, so the access through SP faulted. Read via x19 and mask into SP in one step. Tail-call setcontext/cygwin_exit with 'b' instead of 'bl' so the noreturn frame stays leaf for SEH unwinding, and load the exit code before the NULL-uc_link branch so cygwin_exit(0xff) is actually used.
call_signal_handler: the alternate-stack asm calls altstack_wrapper() but only declared x0-x7,x9,x10 clobbered. Declare every caller-saved register the call may touch (x8, x11-x17, v0-v7, v16-v31, cc); x18 (the Windows TEB pointer) and v8-v15 (callee-saved on Windows) are preserved and stay out of the list.