From 3beb0c52b728de7703924bb017c384e933c111e2 Mon Sep 17 00:00:00 2001 From: Stephan Zuercher Date: Mon, 31 Jul 2017 13:21:12 -0700 Subject: [PATCH 1/4] osx: thread id and signal stack support --- source/common/common/thread.cc | 14 +++++++++++++- source/exe/signal_action.cc | 13 +++++++++++++ source/exe/signal_action.h | 6 +++++- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/source/common/common/thread.cc b/source/common/common/thread.cc index 719381c981a5a..0215e2a905ed9 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,15 @@ 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__) + int ret = mach_thread_self(); + mach_port_deallocate(mach_task_self(), ret); + return ret; +#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(); } From 1dd74ddf30b7aff10bc4829fe5f9cf6ee9d7baab Mon Sep 17 00:00:00 2001 From: Stephan Zuercher Date: Wed, 2 Aug 2017 09:31:50 -0700 Subject: [PATCH 2/4] switch to pthread_mach_thread_np(pthread_self()) for thread id --- source/common/common/thread.cc | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/source/common/common/thread.cc b/source/common/common/thread.cc index 0215e2a905ed9..9e4529376c68a 100644 --- a/source/common/common/thread.cc +++ b/source/common/common/thread.cc @@ -4,6 +4,7 @@ #include #elif defined(__APPLE__) #include +#include #endif #include @@ -29,9 +30,7 @@ int32_t Thread::currentThreadId() { #ifdef __linux__ return syscall(SYS_gettid); #elif defined(__APPLE__) - int ret = mach_thread_self(); - mach_port_deallocate(mach_task_self(), ret); - return ret; + return pthread_mach_thread_np(pthread_self()); #endif } From 50efa9fde45ca9079c2941c1e65f8d38e419f8fd Mon Sep 17 00:00:00 2001 From: Stephan Zuercher Date: Wed, 2 Aug 2017 12:47:14 -0700 Subject: [PATCH 3/4] switch to pthread_threadid_np --- source/common/common/thread.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/source/common/common/thread.cc b/source/common/common/thread.cc index 9e4529376c68a..eacc3011a5dc9 100644 --- a/source/common/common/thread.cc +++ b/source/common/common/thread.cc @@ -3,7 +3,6 @@ #ifdef __linux__ #include #elif defined(__APPLE__) -#include #include #endif @@ -30,7 +29,9 @@ int32_t Thread::currentThreadId() { #ifdef __linux__ return syscall(SYS_gettid); #elif defined(__APPLE__) - return pthread_mach_thread_np(pthread_self()); + uint64_t tid; + pthread_threadid_np(NULL, &tid); + return static_cast(tid); #endif } From 6b2e1a315cf0cdc235851423c641a9e13ccac0e0 Mon Sep 17 00:00:00 2001 From: Stephan Zuercher Date: Wed, 2 Aug 2017 13:40:28 -0700 Subject: [PATCH 4/4] fail with an error when currentThreadId is unimplemented --- source/common/common/thread.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/common/common/thread.cc b/source/common/common/thread.cc index eacc3011a5dc9..153322c44aa03 100644 --- a/source/common/common/thread.cc +++ b/source/common/common/thread.cc @@ -32,6 +32,8 @@ int32_t Thread::currentThreadId() { 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 }