[compiler-rt] r333329 - [safestack] Lazy initialization of interceptors

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Fri May 25 18:18:32 PDT 2018


Author: vitalybuka
Date: Fri May 25 18:18:32 2018
New Revision: 333329

URL: http://llvm.org/viewvc/llvm-project?rev=333329&view=rev
Log:
[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.

Modified:
    compiler-rt/trunk/lib/safestack/safestack.cc

Modified: compiler-rt/trunk/lib/safestack/safestack.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/safestack/safestack.cc?rev=333329&r1=333328&r2=333329&view=diff
==============================================================================
--- compiler-rt/trunk/lib/safestack/safestack.cc (original)
+++ compiler-rt/trunk/lib/safestack/safestack.cc Fri May 25 18:18:32 2018
@@ -171,11 +171,13 @@ static void thread_cleanup_handler(void
   }
 }
 
+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
   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);
 }




More information about the llvm-commits mailing list