From 6417f82ee3097bdbd8c78d16bd1ae610115fb98f Mon Sep 17 00:00:00 2001
From: Austin Wise <AustinWise@gmail.com>
Date: Sun, 7 Jul 2024 14:28:04 -0700
Subject: [PATCH] WIP: more accurately detect running on alternate stack

Probably the correct thing to do is either:
* don't try to use an alternate stack at all
* change the way we switch back to the regular stack from alternate stack handler to go through the signal return path. This would clear the SS_ONSTACK bit for the lwp, allowing it to use the alternate stack again in the future.
---
 src/coreclr/pal/src/exception/signal.cpp | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/coreclr/pal/src/exception/signal.cpp b/src/coreclr/pal/src/exception/signal.cpp
index 5dd071b5c61dca..916487e72cd259 100644
--- a/src/coreclr/pal/src/exception/signal.cpp
+++ b/src/coreclr/pal/src/exception/signal.cpp
@@ -342,9 +342,19 @@ bool IsRunningOnAlternateStack(void *context)
     bool isRunningOnAlternateStack;
     if (g_enable_alternate_stack_check)
     {
+#if defined(__sun)
+        ucontext_t currentThreadContext;
+        memset(&currentThreadContext, 0, sizeof(currentThreadContext));
+        if (getcontext(&currentThreadContext))
+        {
+            abort();
+        }
+        stack_t *signalStack = &currentThreadContext.uc_stack;
+#else
         // Note: WSL doesn't return the alternate signal ranges in the uc_stack (the whole structure is zeroed no
         // matter whether the code is running on an alternate stack or not). So the check would always fail on WSL.
         stack_t *signalStack = &((native_context_t *)context)->uc_stack;
+#endif
         // Check if the signalStack local variable address is within the alternate stack range. If it is not,
         // then either the alternate stack was not installed at all or the current method is not running on it.
         void* alternateStackEnd = (char *)signalStack->ss_sp + signalStack->ss_size;