Skip to content

Commit

Permalink
Use initial-exec tls for libunwind's recursion flag
Browse files Browse the repository at this point in the history
If we don't do it, then reading variable calls to __tls_get_addr, which
uses malloc on first call. initial-exec makes dynamic linker reserve tls
offset for recusion flag early and thus avoid unsafe calls to malloc.

This fixes issue #786.
  • Loading branch information
alk committed Apr 9, 2016
1 parent a07f9fe commit 7852eeb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/base/basictypes.h
Expand Up @@ -192,6 +192,12 @@ struct CompileAssert {
# define ATTRIBUTE_UNUSED
#endif

#if defined(HAVE___ATTRIBUTE__) && defined(HAVE_TLS)
#define ATTR_INITIAL_EXEC __attribute__ ((tls_model ("initial-exec")))
#else
#define ATTR_INITIAL_EXEC
#endif

#define COMPILE_ASSERT(expr, msg) \
typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] ATTRIBUTE_UNUSED

Expand Down
4 changes: 3 additions & 1 deletion src/stacktrace_libunwind-inl.h
Expand Up @@ -47,6 +47,8 @@ extern "C" {
#include <libunwind.h>
}
#include "gperftools/stacktrace.h"

#include "base/basictypes.h"
#include "base/logging.h"

// Sometimes, we can try to get a stack trace from within a stack
Expand All @@ -56,7 +58,7 @@ extern "C" {
// recursive request, we'd end up with infinite recursion or deadlock.
// Luckily, it's safe to ignore those subsequent traces. In such
// cases, we return 0 to indicate the situation.
static __thread int recursive;
static __thread int recursive ATTR_INITIAL_EXEC;

#if defined(TCMALLOC_ENABLE_UNWIND_FROM_UCONTEXT) && (defined(__i386__) || defined(__x86_64__)) && defined(__GNU_LIBRARY__)
#define BASE_STACKTRACE_UNW_CONTEXT_IS_UCONTEXT 1
Expand Down
6 changes: 0 additions & 6 deletions src/thread_cache.h
Expand Up @@ -259,12 +259,6 @@ class ThreadCache {
// Since we don't really use dlopen in google code -- and using dlopen
// on a malloc replacement is asking for trouble in any case -- that's
// a good tradeoff for us.
#ifdef HAVE___ATTRIBUTE__
#define ATTR_INITIAL_EXEC __attribute__ ((tls_model ("initial-exec")))
#else
#define ATTR_INITIAL_EXEC
#endif

#ifdef HAVE_TLS
struct ThreadLocalData {
ThreadCache* heap;
Expand Down

0 comments on commit 7852eeb

Please sign in to comment.