diff options
author | Vitaly Buka <vitalybuka@google.com> | 2018-05-26 01:18:32 +0000 |
---|---|---|
committer | Vitaly Buka <vitalybuka@google.com> | 2018-05-26 01:18:32 +0000 |
commit | da449d868c2a71baaf120c70ac764f138c8bcfbe (patch) | |
tree | 594d6ce7a8ae030e16765b264b9dcfb9ee1e3a67 /lib/safestack | |
parent | f59234b5f3396e43be4a8c9cc1684f1da057a457 (diff) |
[safestack] Lazy initialization of interceptors
Interceptors initialization may try to allocate memory and to call not
initialized allocator.
It's similar to r326025 for CFI.
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@333329 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/safestack')
-rw-r--r-- | lib/safestack/safestack.cc | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/lib/safestack/safestack.cc b/lib/safestack/safestack.cc index d783cd5a9..8b1fdb788 100644 --- a/lib/safestack/safestack.cc +++ b/lib/safestack/safestack.cc @@ -171,11 +171,13 @@ static void thread_cleanup_handler(void *_iter) { } } +static void EnsureInterceptorsInitialized(); + /// Intercept thread creation operation to allocate and setup the unsafe stack INTERCEPTOR(int, pthread_create, pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg) { - + EnsureInterceptorsInitialized(); size_t size = 0; size_t guard = 0; @@ -207,6 +209,19 @@ INTERCEPTOR(int, pthread_create, pthread_t *thread, return REAL(pthread_create)(thread, attr, thread_start, tinfo); } +static BlockingMutex interceptor_init_lock(LINKER_INITIALIZED); +static bool interceptors_inited = false; + +static void EnsureInterceptorsInitialized() { + BlockingMutexLock lock(&interceptor_init_lock); + if (interceptors_inited) return; + + // Initialize pthread interceptors for thread allocation + INTERCEPT_FUNCTION(pthread_create); + + interceptors_inited = true; +} + extern "C" __attribute__((visibility("default"))) #if !SANITIZER_CAN_USE_PREINIT_ARRAY // On ELF platforms, the constructor is invoked using .preinit_array (see below) @@ -227,9 +242,6 @@ void __safestack_init() { unsafe_stack_setup(addr, size, guard); pageSize = sysconf(_SC_PAGESIZE); - // Initialize pthread interceptors for thread allocation - INTERCEPT_FUNCTION(pthread_create); - // Setup the cleanup handler pthread_key_create(&thread_cleanup_key, thread_cleanup_handler); } |