[llvm-commits] [compiler-rt] r159749 - in /compiler-rt/trunk/lib/asan: asan_mac.cc asan_malloc_mac.cc

Alexander Potapenko glider at google.com
Thu Jul 5 07:46:56 PDT 2012


Author: glider
Date: Thu Jul  5 09:46:56 2012
New Revision: 159749

URL: http://llvm.org/viewvc/llvm-project?rev=159749&view=rev
Log:
Fix http://code.google.com/p/address-sanitizer/issues/detail?id=87 by making sure we replace the default CFAllocator only after __CFInitialize has been called.

Modified:
    compiler-rt/trunk/lib/asan/asan_mac.cc
    compiler-rt/trunk/lib/asan/asan_malloc_mac.cc

Modified: compiler-rt/trunk/lib/asan/asan_mac.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_mac.cc?rev=159749&r1=159748&r2=159749&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_mac.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_mac.cc Thu Jul  5 09:46:56 2012
@@ -295,8 +295,6 @@
                                 asan_dispatch_call_block_and_release);
 }
 
-DECLARE_REAL_AND_INTERCEPTOR(void, free, void *ptr)
-
 INTERCEPTOR(void, dispatch_sync_f, dispatch_queue_t dq, void *ctxt,
                                    dispatch_function_t func) {
   GET_STACK_TRACE_HERE(kStackTraceMax);
@@ -428,6 +426,12 @@
   }
 }
 
+DECLARE_REAL_AND_INTERCEPTOR(void, free, void *ptr)
+
+extern "C"
+void __CFInitialize();
+DECLARE_REAL_AND_INTERCEPTOR(void, __CFInitialize)
+
 namespace __asan {
 
 void InitializeMacInterceptors() {
@@ -453,6 +457,9 @@
   // Some of the library functions call free() directly, so we have to
   // intercept it.
   CHECK(INTERCEPT_FUNCTION(free));
+  if (FLAG_replace_cfallocator) {
+    CHECK(INTERCEPT_FUNCTION(__CFInitialize));
+  }
 }
 
 }  // namespace __asan

Modified: compiler-rt/trunk/lib/asan/asan_malloc_mac.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_malloc_mac.cc?rev=159749&r1=159748&r2=159749&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_malloc_mac.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_malloc_mac.cc Thu Jul  5 09:46:56 2012
@@ -78,6 +78,21 @@
   }
 }
 
+// We can't always replace the default CFAllocator with cf_asan right in
+// ReplaceSystemMalloc(), because it is sometimes called before
+// __CFInitialize(), when the default allocator is invalid and replacing it may
+// crash the program. Instead we wait for the allocator to initialize and jump
+// in just after __CFInitialize(). Nobody is going to allocate memory using
+// CFAllocators before that, so we won't miss anything.
+//
+// See http://code.google.com/p/address-sanitizer/issues/detail?id=87
+// and http://opensource.apple.com/source/CF/CF-550.43/CFRuntime.c
+INTERCEPTOR(void, __CFInitialize) {
+  CHECK(FLAG_replace_cfallocator);
+  REAL(__CFInitialize)();
+  if (cf_asan) CFAllocatorSetDefault(cf_asan);
+}
+
 namespace {
 // TODO(glider): the mz_* functions should be united with the Linux wrappers,
 // as they are basically copied from there.
@@ -300,7 +315,7 @@
 
 }  // unnamed namespace
 
-extern bool kCFUseCollectableAllocator;  // is GC on?
+extern int __CFRuntimeClassTableSize;
 
 namespace __asan {
 void ReplaceSystemMalloc() {
@@ -377,7 +392,11 @@
           /*deallocate*/ &cf_free,
           /*preferredSize*/ 0 };
     cf_asan = CFAllocatorCreate(kCFAllocatorUseContext, &asan_context);
-    CFAllocatorSetDefault(cf_asan);
+    // If __CFInitialize() hasn't been called yet, cf_asan will be installed
+    // as the default allocator after __CFInitialize() finishes (see the
+    // interceptor for __CFInitialize() above). Otherwise (if
+    // __CFRuntimeClassTableSize is initialized) install cf_asan right now.
+    if (__CFRuntimeClassTableSize) CFAllocatorSetDefault(cf_asan);
   }
 }
 }  // namespace __asan





More information about the llvm-commits mailing list