Skip to content

Cygwin: aarch64: fix makecontext/setcontext and signal-handler asm#2

Open
mayankag-qti wants to merge 1 commit into
mcw/mainfrom
exceptions_arm64_pr
Open

Cygwin: aarch64: fix makecontext/setcontext and signal-handler asm#2
mayankag-qti wants to merge 1 commit into
mcw/mainfrom
exceptions_arm64_pr

Conversation

@mayankag-qti

Copy link
Copy Markdown
Owner

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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant