Skip to content

Commit 7852eeb

Browse files
committed
Use initial-exec tls for libunwind's recursion flag
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.
1 parent a07f9fe commit 7852eeb

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

src/base/basictypes.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,12 @@ struct CompileAssert {
192192
# define ATTRIBUTE_UNUSED
193193
#endif
194194

195+
#if defined(HAVE___ATTRIBUTE__) && defined(HAVE_TLS)
196+
#define ATTR_INITIAL_EXEC __attribute__ ((tls_model ("initial-exec")))
197+
#else
198+
#define ATTR_INITIAL_EXEC
199+
#endif
200+
195201
#define COMPILE_ASSERT(expr, msg) \
196202
typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] ATTRIBUTE_UNUSED
197203

src/stacktrace_libunwind-inl.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ extern "C" {
4747
#include <libunwind.h>
4848
}
4949
#include "gperftools/stacktrace.h"
50+
51+
#include "base/basictypes.h"
5052
#include "base/logging.h"
5153

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

6163
#if defined(TCMALLOC_ENABLE_UNWIND_FROM_UCONTEXT) && (defined(__i386__) || defined(__x86_64__)) && defined(__GNU_LIBRARY__)
6264
#define BASE_STACKTRACE_UNW_CONTEXT_IS_UCONTEXT 1

src/thread_cache.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,6 @@ class ThreadCache {
259259
// Since we don't really use dlopen in google code -- and using dlopen
260260
// on a malloc replacement is asking for trouble in any case -- that's
261261
// a good tradeoff for us.
262-
#ifdef HAVE___ATTRIBUTE__
263-
#define ATTR_INITIAL_EXEC __attribute__ ((tls_model ("initial-exec")))
264-
#else
265-
#define ATTR_INITIAL_EXEC
266-
#endif
267-
268262
#ifdef HAVE_TLS
269263
struct ThreadLocalData {
270264
ThreadCache* heap;

0 commit comments

Comments
 (0)