[llvm-commits] [compiler-rt] r158082 - in /compiler-rt/trunk/lib: asan/asan_allocator.cc sanitizer_common/sanitizer_common.h

Kostya Serebryany kcc at google.com
Wed Jun 6 09:33:46 PDT 2012


Author: kcc
Date: Wed Jun  6 11:33:46 2012
New Revision: 158082

URL: http://llvm.org/viewvc/llvm-project?rev=158082&view=rev
Log:
[asan] more allocator compaction

Modified:
    compiler-rt/trunk/lib/asan/asan_allocator.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h

Modified: compiler-rt/trunk/lib/asan/asan_allocator.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_allocator.cc?rev=158082&r1=158081&r2=158082&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_allocator.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_allocator.cc Wed Jun  6 11:33:46 2012
@@ -157,15 +157,26 @@
 };
 
 struct ChunkBase {
+  // First 8 bytes.
   uptr  chunk_state : 8;
   uptr  size_class  : 8;
   uptr  alloc_tid   : 24;
   uptr  free_tid    : 24;
-  u32  offset;  // User-visible memory starts at this+offset (beg()).
-  uptr used_size;  // Size requested by the user.
+
+  // Second 8 bytes.
+  uptr alignment_log : 8;
+  uptr used_size : FIRST_32_SECOND_64(32, 56);  // Size requested by the user.
+
+  // The rest.
   AsanChunk *next;
 
-  uptr   beg() { return (uptr)this + offset; }
+  // Typically the beginning of the user-accessible memory is 'this'+REDZONE
+  // and is also aligned by REDZONE. However, if the memory is allocated
+  // by memalign, the alignment might be higher and the user-accessible memory
+  // starts at the first properly aligned address after the end of 'this'.
+  uptr   Beg() {
+    return RoundUpTo((uptr)this + sizeof(ChunkBase), 1 << alignment_log);
+  }
   uptr Size() { return SizeClassToSize(size_class); }
   u8 SizeClass() { return size_class; }
 };
@@ -189,27 +200,27 @@
   }
 
   bool AddrIsInside(uptr addr, uptr access_size, uptr *offset) {
-    if (addr >= beg() && (addr + access_size) <= (beg() + used_size)) {
-      *offset = addr - beg();
+    if (addr >= Beg() && (addr + access_size) <= (Beg() + used_size)) {
+      *offset = addr - Beg();
       return true;
     }
     return false;
   }
 
   bool AddrIsAtLeft(uptr addr, uptr access_size, uptr *offset) {
-    if (addr < beg()) {
-      *offset = beg() - addr;
+    if (addr < Beg()) {
+      *offset = Beg() - addr;
       return true;
     }
     return false;
   }
 
   bool AddrIsAtRight(uptr addr, uptr access_size, uptr *offset) {
-    if (addr + access_size >= beg() + used_size) {
-      if (addr <= beg() + used_size)
+    if (addr + access_size >= Beg() + used_size) {
+      if (addr <= Beg() + used_size)
         *offset = 0;
       else
-        *offset = addr - (beg() + used_size);
+        *offset = addr - (Beg() + used_size);
       return true;
     }
     return false;
@@ -228,7 +239,7 @@
       AsanPrintf(" somewhere around (this is AddressSanitizer bug!)");
     }
     AsanPrintf(" %zu-byte region [%p,%p)\n",
-               used_size, (void*)beg(), (void*)(beg() + used_size));
+               used_size, (void*)Beg(), (void*)(Beg() + used_size));
   }
 };
 
@@ -676,11 +687,14 @@
     AsanChunk *p = (AsanChunk*)(addr - REDZONE);
     p->chunk_state = CHUNK_MEMALIGN;
     p->next = m;
+    m->alignment_log = Log2(alignment);
+    CHECK(m->Beg() == addr);
+  } else {
+    m->alignment_log = Log2(REDZONE);
   }
   CHECK(m == PtrToChunk(addr));
   m->used_size = size;
-  m->offset = addr - (uptr)m;
-  CHECK(m->beg() == addr);
+  CHECK(m->Beg() == addr);
   m->alloc_tid = t ? t->tid() : 0;
   m->free_tid   = kInvalidTid;
   AsanStackTrace::CompressStack(stack, m->compressed_alloc_stack(),

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h?rev=158082&r1=158081&r2=158082&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h Wed Jun  6 11:33:46 2012
@@ -47,6 +47,12 @@
   return (size + boundary - 1) & ~(boundary - 1);
 }
 
+#if __WORDSIZE == 64
+# define FIRST_32_SECOND_64(a, b) (b)
+#else
+# define FIRST_32_SECOND_64(a, b) (a)
+#endif
+
 }  // namespace __sanitizer
 
 #endif  // SANITIZER_COMMON_H





More information about the llvm-commits mailing list