diff --git a/source/common/common/thread.cc b/source/common/common/thread.cc index 719381c981a5a..153322c44aa03 100644 --- a/source/common/common/thread.cc +++ b/source/common/common/thread.cc @@ -1,6 +1,10 @@ #include "common/common/thread.h" +#ifdef __linux__ #include +#elif defined(__APPLE__) +#include +#endif #include @@ -21,7 +25,17 @@ Thread::Thread(std::function thread_routine) : thread_routine_(thread_ro UNREFERENCED_PARAMETER(rc); } -int32_t Thread::currentThreadId() { return syscall(SYS_gettid); } +int32_t Thread::currentThreadId() { +#ifdef __linux__ + return syscall(SYS_gettid); +#elif defined(__APPLE__) + uint64_t tid; + pthread_threadid_np(NULL, &tid); + return static_cast(tid); +#else +#error "Enable and test pthread id retrieval code for you arch in thread.cc" +#endif +} void Thread::join() { int rc = pthread_join(thread_id_, nullptr); diff --git a/source/exe/signal_action.cc b/source/exe/signal_action.cc index 0494c595356ae..74857a6060ff9 100644 --- a/source/exe/signal_action.cc +++ b/source/exe/signal_action.cc @@ -16,6 +16,8 @@ void SignalAction::sigHandler(int sig, siginfo_t* info, void* context) { #ifdef REG_RIP // x86_64 error_pc = reinterpret_cast(ucontext->uc_mcontext.gregs[REG_RIP]); +#elif defined(__APPLE__) && defined(__x86_64__) + error_pc = reinterpret_cast(ucontext->uc_mcontext->__ss.__rip); #else #warning "Please enable and test PC retrieval code for your arch in signal_action.cc" // x86 Classic: reinterpret_cast(ucontext->uc_mcontext.gregs[REG_EIP]); @@ -57,13 +59,24 @@ void SignalAction::installSigHandlers() { } void SignalAction::removeSigHandlers() { +#if defined(__APPLE__) + // ss_flags contains SS_DISABLE, but Darwin still checks the size, contrary to the man page + if (previous_altstack_.ss_size < MINSIGSTKSZ) { + previous_altstack_.ss_size = MINSIGSTKSZ; + } +#endif RELEASE_ASSERT(sigaltstack(&previous_altstack_, nullptr) == 0); + int hidx = 0; for (const auto& sig : FATAL_SIGS) { RELEASE_ASSERT(sigaction(sig, &previous_handlers_[hidx++], nullptr) == 0); } } +#if defined(__APPLE__) && !defined(MAP_STACK) +#define MAP_STACK (0) +#endif + void SignalAction::mapAndProtectStackMemory() { // Per docs MAP_STACK doesn't actually do anything today but provides a // library hint that might be used in the future. diff --git a/source/exe/signal_action.h b/source/exe/signal_action.h index fa54b58bed6ff..b7131345ab205 100644 --- a/source/exe/signal_action.h +++ b/source/exe/signal_action.h @@ -3,6 +3,8 @@ #include #include +#include + #include "common/common/non_copyable.h" #include "server/backtrace.h" @@ -47,7 +49,9 @@ namespace Envoy { class SignalAction : NonCopyable { public: SignalAction() - : guard_size_(sysconf(_SC_PAGE_SIZE)), altstack_size_(guard_size_ * 4), altstack_(nullptr) { + : guard_size_(sysconf(_SC_PAGE_SIZE)), + altstack_size_(std::max(guard_size_ * 4, static_cast(MINSIGSTKSZ))), + altstack_(nullptr) { mapAndProtectStackMemory(); installSigHandlers(); }