From a072f123f332ff9afccf36089227ec2bf404d59c Mon Sep 17 00:00:00 2001 From: Jeff Muizelaar Date: Thu, 8 Dec 2011 10:46:02 -0500 Subject: [PATCH] Bug 708629. Avoid using uninitialized pkey_stack. r=bgirard This was causing crashes with make check/xpcshell on OS X. --- tools/profiler/sps/TableTicker.cpp | 6 ++++++ tools/profiler/sps/sps_sampler.h | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/tools/profiler/sps/TableTicker.cpp b/tools/profiler/sps/TableTicker.cpp index 67f6bd26745..f1509eb8063 100644 --- a/tools/profiler/sps/TableTicker.cpp +++ b/tools/profiler/sps/TableTicker.cpp @@ -52,6 +52,11 @@ using std::string; pthread_key_t pkey_stack; pthread_key_t pkey_ticker; +// We need to track whether we've been initialized otherwise +// we end up using pkey_stack without initializing it. +// Because pkey_stack is totally opaque to us we can't reuse +// it as the flag itself. +bool stack_key_initialized; TimeStamp sLastTracerEvent; @@ -378,6 +383,7 @@ void mozilla_sampler_init() LOG("Failed to init."); return; } + stack_key_initialized = true; Stack *stack = new Stack(); pthread_setspecific(pkey_stack, stack); diff --git a/tools/profiler/sps/sps_sampler.h b/tools/profiler/sps/sps_sampler.h index 963fcce8885..25f05134d97 100644 --- a/tools/profiler/sps/sps_sampler.h +++ b/tools/profiler/sps/sps_sampler.h @@ -47,6 +47,7 @@ using mozilla::TimeDuration; // TODO Merge into Sampler.h extern pthread_key_t pkey_stack; +extern bool stack_key_initialized; #define SAMPLER_INIT() mozilla_sampler_init(); #define SAMPLER_DEINIT() mozilla_sampler_deinit(); @@ -194,7 +195,16 @@ public: inline void* mozilla_sampler_call_enter(const char *aInfo) { + // check if we've been initialized to avoid calling pthread_getspecific + // with a null pkey_stack which will return undefined results. + if (!stack_key_initialized) + return NULL; + Stack *stack = (Stack*)pthread_getspecific(pkey_stack); + // we can't infer whether 'stack' has been initialized + // based on the value of stack_key_intiailized because + // 'stack' is only intialized when a thread is being + // profiled. if (!stack) { return stack; }