https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=0d0e76b99025704d8ee44a44b19a23af9aafe297

commit 0d0e76b99025704d8ee44a44b19a23af9aafe297
Author: Takashi Yano <takashi.y...@nifty.ne.jp>
Date:   Tue Mar 25 18:59:53 2025 +0900

    Cygwin: signal: Copy context to alternate stack in the SA_ONSTACK case
    
    After the commit 0210c77311ae, the context passed to signal handler
    cannot be accessed from the signal handler that uses alternate stack.
    This is because the context locally copied is on the stack that is
    different area from the signal handler uses. With this patch, copy
    the context to alternate signal stack area to avoid this situation.
    
    Addresses: https://cygwin.com/pipermail/cygwin/2025-March/257714.html
    Fixes: 0210c77311ae ("Cygwin: signal: Use context locally copied in 
call_signal_handler()")
    Reported-by: Bruno Haible <br...@clisp.org>
    Reviewed-by: Corinna Vischen <cori...@vinschen.de>
    Signed-off-by: Takashi Yano <takashi.y...@nifty.ne.jp>

Diff:
---
 winsup/cygwin/exceptions.cc | 8 ++++++++
 winsup/cygwin/release/3.6.1 | 5 +++++
 2 files changed, 13 insertions(+)

diff --git a/winsup/cygwin/exceptions.cc b/winsup/cygwin/exceptions.cc
index 2e25aa214..a3aae2ce5 100644
--- a/winsup/cygwin/exceptions.cc
+++ b/winsup/cygwin/exceptions.cc
@@ -1791,6 +1791,13 @@ _cygtls::call_signal_handler ()
             to 16 byte. */
          uintptr_t new_sp = ((uintptr_t) _my_tls.altstack.ss_sp
                              + _my_tls.altstack.ss_size) & ~0xf;
+         /* Copy context1 to the alternate signal stack area, because the
+            context1 allocated in the normal stack area is not accessible
+            from the signal handler that uses alternate signal stack. */
+         thiscontext = (ucontext_t *) ((new_sp - sizeof (ucontext_t)) & ~0xf);
+         memcpy (thiscontext, &context1, sizeof (ucontext_t));
+         new_sp = (uintptr_t) thiscontext;
+
          /* In assembler: Save regs on new stack, move to alternate stack,
             call thisfunc, revert stack regs. */
 #ifdef __x86_64__
@@ -1834,6 +1841,7 @@ _cygtls::call_signal_handler ()
 #else
 #error unimplemented for this target
 #endif
+         memcpy (&context1, thiscontext, sizeof (ucontext_t));
        }
       else
        /* No alternate signal stack requested or available, just call
diff --git a/winsup/cygwin/release/3.6.1 b/winsup/cygwin/release/3.6.1
index 40ef2973f..95c2c054e 100644
--- a/winsup/cygwin/release/3.6.1
+++ b/winsup/cygwin/release/3.6.1
@@ -10,3 +10,8 @@ Fixes:
 - getlocalename_l: Fix a crash and handle LC_ALL according to final
   POSIX-1.2024 docs.
   Addresses: https://cygwin.com/pipermail/cygwin/2025-March/257715.html
+
+- Copy context to alternate signal stack area in call_signal_handler()
+  in the SA_ONSTACK case, because locally-copied context on the normal
+  stack area is not accessible from the signal handler.
+  Addresses: https://cygwin.com/pipermail/cygwin/2025-March/257714.html

Reply via email to