diff --git a/src/coreclr/pal/src/include/pal/thread.hpp b/src/coreclr/pal/src/include/pal/thread.hpp index 4b99a362381e59..414e57ec01c246 100644 --- a/src/coreclr/pal/src/include/pal/thread.hpp +++ b/src/coreclr/pal/src/include/pal/thread.hpp @@ -249,6 +249,7 @@ namespace CorUnix void* m_stackLimit; // Signal handler's alternate stack to help with stack overflow void* m_alternateStack; + size_t m_alternateStackSize; // // The thread entry routine (called from InternalCreateThread) @@ -301,7 +302,8 @@ namespace CorUnix m_fStartStatusSet(FALSE), m_stackBase(NULL), m_stackLimit(NULL), - m_alternateStack(NULL) + m_alternateStack(NULL), + m_alternateStackSize(0) { }; diff --git a/src/coreclr/pal/src/thread/thread.cpp b/src/coreclr/pal/src/thread/thread.cpp index b280a76e12c662..cb21db893f0f49 100644 --- a/src/coreclr/pal/src/thread/thread.cpp +++ b/src/coreclr/pal/src/thread/thread.cpp @@ -2078,6 +2078,7 @@ CPalThread::EnsureSignalAlternateStack() if (st == 0) { m_alternateStack = altStack; + m_alternateStackSize = altStackSize; } else { @@ -2109,27 +2110,25 @@ CPalThread::FreeSignalAlternateStack() { #ifndef TARGET_WASM // WebAssembly does not support alternate signal stacks. void *altstack = m_alternateStack; + size_t altStackSize = m_alternateStackSize; m_alternateStack = nullptr; + m_alternateStackSize = 0; if (altstack != nullptr) { - stack_t ss, oss; - // The man page for sigaltstack says that when the ss.ss_flags is set to SS_DISABLE, - // all other ss fields are ignored. However, MUSL implementation checks that the - // ss_size is >= MINSIGSTKSZ even in this case. - ss.ss_size = MINSIGSTKSZ; - ss.ss_flags = SS_DISABLE; - ss.ss_sp = NULL; - int st = sigaltstack(&ss, &oss); - if ((st == 0) && (oss.ss_flags != SS_DISABLE)) + stack_t ss; + if (sigaltstack(nullptr, &ss) == 0 && (ss.ss_flags & SS_DISABLE) == 0 && ss.ss_sp == altstack) { - // Make sure this altstack is this PAL's before freeing. - if (oss.ss_sp == altstack) - { - int st = munmap(oss.ss_sp, oss.ss_size); - _ASSERTE(st == 0); - } + // The man page for sigaltstack says that when the ss.ss_flags is set to SS_DISABLE, + // all other ss fields are ignored. However, MUSL implementation checks that the + // ss_size is >= MINSIGSTKSZ even in this case. + ss.ss_size = MINSIGSTKSZ; + ss.ss_flags = SS_DISABLE; + ss.ss_sp = nullptr; + sigaltstack(&ss, nullptr); } + int st = munmap(altstack, altStackSize); + _ASSERTE(st == 0); } #endif // !TARGET_WASM }