[llvm-commits] [compiler-rt] r159122 - in /compiler-rt/trunk/lib/sanitizer_common: sanitizer_allocator64.h tests/sanitizer_allocator64_test.cc

Kostya Serebryany kcc at google.com
Sun Jun 24 21:12:49 PDT 2012


Author: kcc
Date: Sun Jun 24 23:12:49 2012
New Revision: 159122

URL: http://llvm.org/viewvc/llvm-project?rev=159122&view=rev
Log:
[tsan] a better CHECK for OOM in the new allocator

Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator64.h
    compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator64_test.cc

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator64.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator64.h?rev=159122&r1=159121&r2=159122&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator64.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator64.h Sun Jun 24 23:12:49 2012
@@ -122,7 +122,7 @@
   uptr TotalMemoryUsedIncludingFreeLists() {
     uptr res = 0;
     for (uptr i = 0; i < kNumClasses; i++)
-      res += GetRegionInfo(i)->allocated;
+      res += GetRegionInfo(i)->allocated_user;
     return res;
   }
 
@@ -130,6 +130,7 @@
   void TestOnlyUnmap() {
     UnmapOrDie(reinterpret_cast<void*>(AllocBeg()), AllocSize());
   }
+
  private:
   static const uptr kNumClasses = 256;  // Power of two <= 256
   COMPILER_CHECK(kNumClasses <= SizeClassMap::kNumClasses);
@@ -146,9 +147,11 @@
   struct RegionInfo {
     uptr mutex;  // FIXME
     LifoListNode *free_list;
-    uptr allocated;
+    uptr allocated_user;  // Bytes allocated for user memory.
+    uptr allocated_meta;  // Bytes allocated for metadata.
     char padding[kCacheLineSize -
-                 sizeof(mutex) - sizeof(free_list) - sizeof(allocated)];
+                 sizeof(mutex) - sizeof(free_list) -
+                 sizeof(allocated_user) - sizeof(allocated_meta)];
   };
   COMPILER_CHECK(sizeof(RegionInfo) == kCacheLineSize);
 
@@ -183,18 +186,21 @@
 
   LifoListNode *PopulateFreeList(uptr class_id, RegionInfo *region) {
     uptr size = SizeClassMap::Size(class_id);
-    uptr beg_idx = region->allocated;
+    uptr beg_idx = region->allocated_user;
     uptr end_idx = beg_idx + kPopulateSize;
     LifoListNode *res = 0;
     uptr region_beg = kSpaceBeg + kRegionSize * class_id;
     uptr idx = beg_idx;
+    uptr i = 0;
     do {  // do-while loop because we need to put at least one item.
       uptr p = region_beg + idx;
       PushLifoList(&res, reinterpret_cast<LifoListNode*>(p));
       idx += size;
+      i++;
     } while (idx < end_idx);
-    CHECK_LT(idx, kRegionSize);
-    region->allocated += idx - beg_idx;
+    region->allocated_user += idx - beg_idx;
+    region->allocated_meta += i * kMetadataSize;
+    CHECK_LT(region->allocated_user + region->allocated_meta, kRegionSize);
     return res;
   }
 

Modified: compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator64_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator64_test.cc?rev=159122&r1=159121&r2=159122&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator64_test.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator64_test.cc Sun Jun 24 23:12:49 2012
@@ -125,3 +125,22 @@
 
   a.TestOnlyUnmap();
 }
+
+void FailInAssertionOnOOM() {
+  typedef DefaultSizeClassMap SCMap;
+  typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize,
+          16, SCMap> Allocator;
+  Allocator a;
+  a.Init();
+  const uptr size = 1 << 20;
+  for (int i = 0; i < 1000000; i++) {
+    a.Allocate(size);
+  }
+
+  a.TestOnlyUnmap();
+}
+
+TEST(SanitizerCommon, SizeClassAllocator64Overflow) {
+  EXPECT_DEATH(FailInAssertionOnOOM(),
+               "allocated_user.*allocated_meta.*kRegionSize");
+}





More information about the llvm-commits mailing list