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 | 4add8a1f4e6762e480380bfa3403fa69a1483bd9 (patch) | |
tree | 6935ed910ce4ca61b1440a2f66ed49336cc5051f | |
parent | 796bb7d4ceb520bf611ead899ba56324761edaa8 (diff) | |
download | bcm5719-llvm-4add8a1f4e6762e480380bfa3403fa69a1483bd9.tar.gz bcm5719-llvm-4add8a1f4e6762e480380bfa3403fa69a1483bd9.zip |
[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.
llvm-svn: 333329
-rw-r--r-- | compiler-rt/lib/safestack/safestack.cc | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/compiler-rt/lib/safestack/safestack.cc b/compiler-rt/lib/safestack/safestack.cc index d783cd5a9b2..8b1fdb788fe 100644 --- a/compiler-rt/lib/safestack/safestack.cc +++ b/compiler-rt/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); } |