[llvm-commits] [compiler-rt] r170680 - in /compiler-rt/trunk/lib/asan: asan_allocator2.cc asan_thread_registry.cc tests/asan_noinst_test.cc

Kostya Serebryany kcc at google.com
Thu Dec 20 00:53:41 PST 2012


Author: kcc
Date: Thu Dec 20 02:53:41 2012
New Revision: 170680

URL: http://llvm.org/viewvc/llvm-project?rev=170680&view=rev
Log:
[asan] asan_allocator2: make all remaining tests pass.

Modified:
    compiler-rt/trunk/lib/asan/asan_allocator2.cc
    compiler-rt/trunk/lib/asan/asan_thread_registry.cc
    compiler-rt/trunk/lib/asan/tests/asan_noinst_test.cc

Modified: compiler-rt/trunk/lib/asan/asan_allocator2.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_allocator2.cc?rev=170680&r1=170679&r2=170680&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_allocator2.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_allocator2.cc Thu Dec 20 02:53:41 2012
@@ -196,9 +196,7 @@
     PushList(q);
     PopAndDeallocateLoop(ms);
   }
-  void SwallowThreadLocalCache(AllocatorCache *cache) {
-    // FIXME.
-  }
+
   void BypassThreadLocalQuarantine(AsanChunk *m) {
     SpinMutexLock l(&mutex_);
     Push(m);
@@ -225,6 +223,12 @@
     void *p = reinterpret_cast<void *>(alloc_beg);
     if (m->from_memalign)
       p = allocator.GetBlockBegin(p);
+
+    // Statistics.
+    AsanStats &thread_stats = asanThreadRegistry().GetCurrentThreadStats();
+    thread_stats.real_frees++;
+    thread_stats.really_freed += m->UsedSize();
+
     allocator.Deallocate(GetAllocatorCache(ms), p);
   }
   SpinMutex mutex_;
@@ -308,6 +312,10 @@
     *shadow = size & (SHADOW_GRANULARITY - 1);
   }
 
+  AsanStats &thread_stats = asanThreadRegistry().GetCurrentThreadStats();
+  thread_stats.mallocs++;
+  thread_stats.malloced += size;
+
   void *res = reinterpret_cast<void *>(user_beg);
   ASAN_MALLOC_HOOK(res, size);
   return res;
@@ -341,6 +349,10 @@
                RoundUpTo(m->user_requested_size, SHADOW_GRANULARITY),
                kAsanHeapFreeMagic);
 
+  AsanStats &thread_stats = asanThreadRegistry().GetCurrentThreadStats();
+  thread_stats.frees++;
+  thread_stats.freed += m->UsedSize();
+
   // Push into quarantine.
   if (t) {
     AsanChunkFifoList &q = t->malloc_storage().quarantine_;
@@ -432,7 +444,7 @@
 
 void AsanThreadLocalMallocStorage::CommitBack() {
   quarantine.SwallowThreadLocalQuarantine(this);
-  quarantine.SwallowThreadLocalCache(GetAllocatorCache(this));
+  allocator.SwallowCache(GetAllocatorCache(this));
 }
 
 SANITIZER_INTERFACE_ATTRIBUTE
@@ -517,20 +529,24 @@
 using namespace __asan;  // NOLINT
 
 // ASan allocator doesn't reserve extra bytes, so normally we would
-// just return "size".
+// just return "size". We don't want to expose our redzone sizes, etc here.
 uptr __asan_get_estimated_allocated_size(uptr size) {
-  UNIMPLEMENTED();
-  return 0;
+  return size;
 }
 
 bool __asan_get_ownership(const void *p) {
-  UNIMPLEMENTED();
-  return false;
+  return AllocationSize(reinterpret_cast<uptr>(p)) > 0;
 }
 
 uptr __asan_get_allocated_size(const void *p) {
-  UNIMPLEMENTED();
-  return 0;
+  if (p == 0) return 0;
+  uptr allocated_size = AllocationSize(reinterpret_cast<uptr>(p));
+  // Die if p is not malloced or if it is already freed.
+  if (allocated_size == 0) {
+    GET_STACK_TRACE_FATAL_HERE;
+    ReportAsanGetAllocatedSizeNotOwned(reinterpret_cast<uptr>(p), &stack);
+  }
+  return allocated_size;
 }
 
 #if !SANITIZER_SUPPORTS_WEAK_HOOKS

Modified: compiler-rt/trunk/lib/asan/asan_thread_registry.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_thread_registry.cc?rev=170680&r1=170679&r2=170680&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_thread_registry.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_thread_registry.cc Thu Dec 20 02:53:41 2012
@@ -130,6 +130,7 @@
   ScopedLock lock(&mu_);
   UpdateAccumulatedStatsUnlocked();
   uptr total_free = accumulated_stats_.mmaped
+                  - accumulated_stats_.munmaped
                   + accumulated_stats_.really_freed
                   + accumulated_stats_.really_freed_redzones;
   uptr total_used = accumulated_stats_.malloced

Modified: compiler-rt/trunk/lib/asan/tests/asan_noinst_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/tests/asan_noinst_test.cc?rev=170680&r1=170679&r2=170680&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/tests/asan_noinst_test.cc (original)
+++ compiler-rt/trunk/lib/asan/tests/asan_noinst_test.cc Thu Dec 20 02:53:41 2012
@@ -345,7 +345,11 @@
 }
 
 TEST(AddressSanitizerInterface, GetEstimatedAllocatedSize) {
+#if ASAN_ALLOCATOR_VERSION == 1
   EXPECT_EQ(1U, __asan_get_estimated_allocated_size(0));
+#elif ASAN_ALLOCATOR_VERSION == 2
+  EXPECT_EQ(0U, __asan_get_estimated_allocated_size(0));
+#endif
   const size_t sizes[] = { 1, 30, 1<<30 };
   for (size_t i = 0; i < 3; i++) {
     EXPECT_EQ(sizes[i], __asan_get_estimated_allocated_size(sizes[i]));
@@ -410,6 +414,7 @@
   delete Ident(x);
 }
 
+#if ASAN_ALLOCATOR_VERSION == 1
 // This test is run in a separate process, so that large malloced
 // chunk won't remain in the free lists after the test.
 // Note: use ASSERT_* instead of EXPECT_* here.
@@ -441,9 +446,26 @@
 TEST(AddressSanitizerInterface, GetHeapSizeTest) {
   EXPECT_DEATH(RunGetHeapSizeTestAndDie(), "double-free");
 }
+#elif ASAN_ALLOCATOR_VERSION == 2
+TEST(AddressSanitizerInterface, GetHeapSizeTest) {
+  // asan_allocator2 does not keep huge chunks in free list, but unmaps them.
+  // The chunk should be greater than the quarantine size,
+  // otherwise it will be stuck in quarantine instead of being unmaped.
+  static const size_t kLargeMallocSize = 1 << 28;  // 256M
+  uptr old_heap_size = __asan_get_heap_size();
+  for (int i = 0; i < 3; i++) {
+    // fprintf(stderr, "allocating %zu bytes:\n", kLargeMallocSize);
+    free(Ident(malloc(kLargeMallocSize)));
+    EXPECT_EQ(old_heap_size, __asan_get_heap_size());
+  }
+}
+#endif
 
 // Note: use ASSERT_* instead of EXPECT_* here.
 static void DoLargeMallocForGetFreeBytesTestAndDie() {
+#if ASAN_ALLOCATOR_VERSION == 1
+  // asan_allocator2 does not keep large chunks in free_lists, so this test
+  // will not work.
   size_t old_free_bytes, new_free_bytes;
   static const size_t kLargeMallocSize = 1 << 29;  // 512M
   // If we malloc and free a large memory chunk, it will not fall
@@ -455,6 +477,7 @@
   new_free_bytes = __asan_get_free_bytes();
   fprintf(stderr, "free bytes after malloc and free: %zu\n", new_free_bytes);
   ASSERT_GE(new_free_bytes, old_free_bytes + kLargeMallocSize);
+#endif  // ASAN_ALLOCATOR_VERSION
   // Test passed.
   DoDoubleFree();
 }





More information about the llvm-commits mailing list