[llvm-commits] [compiler-rt] r160630 - in /compiler-rt/trunk/lib/asan: asan_internal.h asan_linux.cc asan_mac.cc asan_mac.h asan_malloc_mac.cc asan_thread.cc asan_win.cc tests/asan_mac_test.h tests/asan_test.cc

Alexander Potapenko glider at google.com
Mon Jul 23 07:07:59 PDT 2012


Author: glider
Date: Mon Jul 23 09:07:58 2012
New Revision: 160630

URL: http://llvm.org/viewvc/llvm-project?rev=160630&view=rev
Log:
Intercept CFAllocator for each thread in the program.
Test that child threads use the ASan allocator, that allocated memory can be passed to another thread and deallocated on it.
This should fix http://code.google.com/p/address-sanitizer/issues/detail?id=81

Modified:
    compiler-rt/trunk/lib/asan/asan_internal.h
    compiler-rt/trunk/lib/asan/asan_linux.cc
    compiler-rt/trunk/lib/asan/asan_mac.cc
    compiler-rt/trunk/lib/asan/asan_mac.h
    compiler-rt/trunk/lib/asan/asan_malloc_mac.cc
    compiler-rt/trunk/lib/asan/asan_thread.cc
    compiler-rt/trunk/lib/asan/asan_win.cc
    compiler-rt/trunk/lib/asan/tests/asan_mac_test.h
    compiler-rt/trunk/lib/asan/tests/asan_test.cc

Modified: compiler-rt/trunk/lib/asan/asan_internal.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_internal.h?rev=160630&r1=160629&r2=160630&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_internal.h (original)
+++ compiler-rt/trunk/lib/asan/asan_internal.h Mon Jul 23 09:07:58 2012
@@ -107,6 +107,7 @@
 void SetAlternateSignalStack();
 void UnsetAlternateSignalStack();
 void InstallSignalHandlers();
+void AsanPlatformThreadInit();
 
 // Wrapper for TLS/TSD.
 void AsanTSDInit(void (*destructor)(void *tsd));

Modified: compiler-rt/trunk/lib/asan/asan_linux.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_linux.cc?rev=160630&r1=160629&r2=160630&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_linux.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_linux.cc Mon Jul 23 09:07:58 2012
@@ -72,6 +72,10 @@
   return signum == SIGSEGV && flags()->handle_segv;
 }
 
+void AsanPlatformThreadInit() {
+  // Nothing here for now.
+}
+
 AsanLock::AsanLock(LinkerInitialized) {
   // We assume that pthread_mutex_t initialized to all zeroes is a valid
   // unlocked mutex. We can not use PTHREAD_MUTEX_INITIALIZER as it triggers

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=160630&r1=160629&r2=160630&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_mac.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_mac.cc Mon Jul 23 09:07:58 2012
@@ -92,6 +92,10 @@
   return (signum == SIGSEGV || signum == SIGBUS) && flags()->handle_segv;
 }
 
+void AsanPlatformThreadInit() {
+  ReplaceCFAllocator();
+}
+
 AsanLock::AsanLock(LinkerInitialized) {
   // We assume that OS_SPINLOCK_INIT is zero
 }

Modified: compiler-rt/trunk/lib/asan/asan_mac.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_mac.h?rev=160630&r1=160629&r2=160630&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_mac.h (original)
+++ compiler-rt/trunk/lib/asan/asan_mac.h Mon Jul 23 09:07:58 2012
@@ -46,6 +46,7 @@
 namespace __asan {
 
 int GetMacosVersion();
+void ReplaceCFAllocator();
 
 }  // 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=160630&r1=160629&r2=160630&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_malloc_mac.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_malloc_mac.cc Mon Jul 23 09:07:58 2012
@@ -336,8 +336,10 @@
         /*reallocate*/ &cf_realloc,
         /*deallocate*/ &cf_free,
         /*preferredSize*/ 0 };
-  cf_asan = CFAllocatorCreate(kCFAllocatorUseContext, &asan_context);
-  CFAllocatorSetDefault(cf_asan);
+  if (!cf_asan)
+    cf_asan = CFAllocatorCreate(kCFAllocatorUseContext, &asan_context);
+  if (CFAllocatorGetDefault() != cf_asan)
+    CFAllocatorSetDefault(cf_asan);
 }
 
 void ReplaceSystemMalloc() {

Modified: compiler-rt/trunk/lib/asan/asan_thread.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_thread.cc?rev=160630&r1=160629&r2=160630&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_thread.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_thread.cc Mon Jul 23 09:07:58 2012
@@ -89,6 +89,7 @@
            stack_top_ - stack_bottom_, &local);
   }
   fake_stack_.Init(stack_size());
+  AsanPlatformThreadInit();
 }
 
 thread_return_t AsanThread::ThreadStart() {

Modified: compiler-rt/trunk/lib/asan/asan_win.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_win.cc?rev=160630&r1=160629&r2=160630&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_win.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_win.cc Mon Jul 23 09:07:58 2012
@@ -172,6 +172,10 @@
   // FIXME: Decide what to do on Windows.
 }
 
+void AsanPlatformThreadInit() {
+  // Nothing here for now.
+}
+
 }  // namespace __asan
 
 #endif  // _WIN32

Modified: compiler-rt/trunk/lib/asan/tests/asan_mac_test.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/tests/asan_mac_test.h?rev=160630&r1=160629&r2=160630&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/tests/asan_mac_test.h (original)
+++ compiler-rt/trunk/lib/asan/tests/asan_mac_test.h Mon Jul 23 09:07:58 2012
@@ -15,4 +15,5 @@
   void TestGCDGroupAsync();
   void TestOOBNSObjects();
   void TestNSURLDeallocation();
+  void TestPassCFMemoryToAnotherThread();
 }

Modified: compiler-rt/trunk/lib/asan/tests/asan_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/tests/asan_test.cc?rev=160630&r1=160629&r2=160630&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/tests/asan_test.cc (original)
+++ compiler-rt/trunk/lib/asan/tests/asan_test.cc Mon Jul 23 09:07:58 2012
@@ -1906,10 +1906,41 @@
   pthread_join(child, NULL);  // Shouldn't be reached.
 }
 
-TEST(AddressSanitizerMac, DISABLED_CFAllocatorDefaultDoubleFree_ChildPhread) {
+TEST(AddressSanitizerMac, CFAllocatorDefaultDoubleFree_ChildPhread) {
   EXPECT_DEATH(CFAllocator_DoubleFreeOnPthread(), "attempting double-free");
 }
 
+namespace {
+
+void *GLOB;
+
+void *CFAllocatorAllocateToGlob(void *unused) {
+  GLOB = CFAllocatorAllocate(NULL, 100, /*hint*/0);
+  return NULL;
+}
+
+void *CFAllocatorDeallocateFromGlob(void *unused) {
+  char *p = (char*)GLOB;
+  p[100] = 'A';  // ASan should report an error here.
+  CFAllocatorDeallocate(NULL, GLOB);
+  return NULL;
+}
+
+void CFAllocator_PassMemoryToAnotherThread() {
+  pthread_t th1, th2;
+  pthread_create(&th1, NULL, CFAllocatorAllocateToGlob, NULL);
+  pthread_join(th1, NULL);
+  pthread_create(&th2, NULL, CFAllocatorDeallocateFromGlob, NULL);
+  pthread_join(th2, NULL);
+}
+
+TEST(AddressSanitizerMac, CFAllocator_PassMemoryToAnotherThread) {
+  EXPECT_DEATH(CFAllocator_PassMemoryToAnotherThread(),
+               "heap-buffer-overflow");
+}
+
+}  // namespace
+
 // TODO(glider): figure out whether we still need these tests. Is it correct
 // to intercept the non-default CFAllocators?
 TEST(AddressSanitizerMac, DISABLED_CFAllocatorSystemDefaultDoubleFree) {





More information about the llvm-commits mailing list