From 307091dc306c34ce9e4ee6cc3b467807b3a3bd12 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 27 Aug 2014 11:47:01 -0700 Subject: [PATCH] Actually ask the pthread implementation for the stack guard size. Bug: 17111575 Change-Id: I23919b1e8aeff627a65daf57f1109bec60b196cc --- runtime/thread.cc | 32 ++++++++++---------------------- runtime/utils.cc | 9 ++++++++- runtime/utils.h | 2 +- 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/runtime/thread.cc b/runtime/thread.cc index e6521006c..e58e8641e 100644 --- a/runtime/thread.cc +++ b/runtime/thread.cc @@ -487,11 +487,14 @@ void Thread::SetThreadName(const char* name) { void Thread::InitStackHwm() { void* read_stack_base; size_t read_stack_size; - GetThreadStack(tlsPtr_.pthread_self, &read_stack_base, &read_stack_size); + size_t read_guard_size; + GetThreadStack(tlsPtr_.pthread_self, &read_stack_base, &read_stack_size, &read_guard_size); - // TODO: include this in the thread dumps; potentially useful in SIGQUIT output? - VLOG(threads) << StringPrintf("Native stack is at %p (%s)", read_stack_base, - PrettySize(read_stack_size).c_str()); + // This is included in the SIGQUIT output, but it's useful here for thread debugging. + VLOG(threads) << StringPrintf("Native stack is at %p (%s with %s guard)", + read_stack_base, + PrettySize(read_stack_size).c_str(), + PrettySize(read_guard_size).c_str()); tlsPtr_.stack_begin = reinterpret_cast(read_stack_base); tlsPtr_.stack_size = read_stack_size; @@ -544,28 +547,13 @@ void Thread::InitStackHwm() { // Install the protected region if we are doing implicit overflow checks. if (implicit_stack_check) { - size_t guardsize; - pthread_attr_t attributes; - CHECK_PTHREAD_CALL(pthread_attr_init, (&attributes), "guard size query"); - CHECK_PTHREAD_CALL(pthread_attr_getguardsize, (&attributes, &guardsize), "guard size query"); - CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), "guard size query"); // The thread might have protected region at the bottom. We need // to install our own region so we need to move the limits // of the stack to make room for it. -#if defined(__i386__) || defined(__x86_64__) - // Work around issue trying to read last page of stack on Intel. - // The bug for this is b/17111575. The problem is that we are - // unable to read the page just above the guard page on the - // main stack on an intel target. When the bug is fixed - // this can be removed. - if (::art::GetTid() == getpid()) { - guardsize += 4 * KB; - } -#endif - tlsPtr_.stack_begin += guardsize + kStackOverflowProtectedSize; - tlsPtr_.stack_end += guardsize + kStackOverflowProtectedSize; - tlsPtr_.stack_size -= guardsize; + tlsPtr_.stack_begin += read_guard_size + kStackOverflowProtectedSize; + tlsPtr_.stack_end += read_guard_size + kStackOverflowProtectedSize; + tlsPtr_.stack_size -= read_guard_size; InstallImplicitProtection(); } diff --git a/runtime/utils.cc b/runtime/utils.cc index 4f73e6975..d704eec9a 100644 --- a/runtime/utils.cc +++ b/runtime/utils.cc @@ -82,7 +82,7 @@ std::string GetThreadName(pid_t tid) { return result; } -void GetThreadStack(pthread_t thread, void** stack_base, size_t* stack_size) { +void GetThreadStack(pthread_t thread, void** stack_base, size_t* stack_size, size_t* guard_size) { #if defined(__APPLE__) *stack_size = pthread_get_stacksize_np(thread); void* stack_addr = pthread_get_stackaddr_np(thread); @@ -95,10 +95,17 @@ void GetThreadStack(pthread_t thread, void** stack_base, size_t* stack_size) { } else { *stack_base = stack_addr; } + + // This is wrong, but there doesn't seem to be a way to get the actual value on the Mac. + pthread_attr_t attributes; + CHECK_PTHREAD_CALL(pthread_attr_init, (&attributes), __FUNCTION__); + CHECK_PTHREAD_CALL(pthread_attr_getguardsize, (&attributes, guard_size), __FUNCTION__); + CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__); #else pthread_attr_t attributes; CHECK_PTHREAD_CALL(pthread_getattr_np, (thread, &attributes), __FUNCTION__); CHECK_PTHREAD_CALL(pthread_attr_getstack, (&attributes, stack_base, stack_size), __FUNCTION__); + CHECK_PTHREAD_CALL(pthread_attr_getguardsize, (&attributes, guard_size), __FUNCTION__); CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__); #endif } diff --git a/runtime/utils.h b/runtime/utils.h index d821ae199..c4a58950c 100644 --- a/runtime/utils.h +++ b/runtime/utils.h @@ -416,7 +416,7 @@ pid_t GetTid(); std::string GetThreadName(pid_t tid); // Returns details of the given thread's stack. -void GetThreadStack(pthread_t thread, void** stack_base, size_t* stack_size); +void GetThreadStack(pthread_t thread, void** stack_base, size_t* stack_size, size_t* guard_size); // Reads data from "/proc/self/task/${tid}/stat". void GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu); -- 2.11.0