[asan/tsan] get rid of kPageSize completely in favor of GetPageSizeCached(). This...
authorKostya Serebryany <kcc@google.com>
Sat, 24 Nov 2012 05:03:11 +0000 (05:03 +0000)
committerKostya Serebryany <kcc@google.com>
Sat, 24 Nov 2012 05:03:11 +0000 (05:03 +0000)
llvm-svn: 168537

compiler-rt/lib/asan/asan_mac.cc
compiler-rt/lib/asan/asan_mapping.h
compiler-rt/lib/asan/asan_rtl.cc
compiler-rt/lib/asan/tests/asan_noinst_test.cc
compiler-rt/lib/sanitizer_common/sanitizer_allocator.cc
compiler-rt/lib/sanitizer_common/sanitizer_allocator64.h
compiler-rt/lib/sanitizer_common/sanitizer_common.h
compiler-rt/lib/sanitizer_common/sanitizer_stacktrace.cc
compiler-rt/lib/sanitizer_common/tests/sanitizer_allocator64_test.cc
compiler-rt/lib/sanitizer_common/tests/sanitizer_allocator64_testlib.cc
compiler-rt/lib/tsan/rtl/tsan_platform.h

index e4e897e..fe87843 100644 (file)
@@ -184,11 +184,11 @@ void ClearShadowMemoryForContext(void *context) {
 static void *island_allocator_pos = 0;
 
 #if SANITIZER_WORDSIZE == 32
-# define kIslandEnd (0xffdf0000 - kPageSize)
-# define kIslandBeg (kIslandEnd - 256 * kPageSize)
+# define kIslandEnd (0xffdf0000 - GetPageSizeCached())
+# define kIslandBeg (kIslandEnd - 256 * GetPageSizeCached())
 #else
-# define kIslandEnd (0x7fffffdf0000 - kPageSize)
-# define kIslandBeg (kIslandEnd - 256 * kPageSize)
+# define kIslandEnd (0x7fffffdf0000 - GetPageSizeCached())
+# define kIslandBeg (kIslandEnd - 256 * GetPageSizeCached())
 #endif
 
 extern "C"
@@ -212,7 +212,7 @@ mach_error_t __interception_allocate_island(void **ptr,
     internal_memset(island_allocator_pos, 0xCC, kIslandEnd - kIslandBeg);
   };
   *ptr = island_allocator_pos;
-  island_allocator_pos = (char*)island_allocator_pos + kPageSize;
+  island_allocator_pos = (char*)island_allocator_pos + GetPageSizeCached();
   if (flags()->verbosity) {
     Report("Branch island allocated at %p\n", *ptr);
   }
index 803fcd8..3a5f88b 100644 (file)
@@ -68,7 +68,12 @@ extern __attribute__((visibility("default"))) uptr __asan_mapping_offset;
 #define kHighShadowBeg  MEM_TO_SHADOW(kHighMemBeg)
 #define kHighShadowEnd  MEM_TO_SHADOW(kHighMemEnd)
 
-#define kShadowGapBeg   (kLowShadowEnd ? kLowShadowEnd + 1 : 16 * kPageSize)
+// With the zero shadow base we can not actually map pages starting from 0.
+// This constant is somewhat arbitrary.
+#define kZeroBaseShadowStart (1 << 18)
+
+#define kShadowGapBeg   (kLowShadowEnd ? kLowShadowEnd + 1 \
+                                       : kZeroBaseShadowStart)
 #define kShadowGapEnd   (kHighShadowBeg - 1)
 
 #define kGlobalAndStackRedzone \
index 28a8c03..bd5c0fe 100644 (file)
@@ -350,12 +350,13 @@ void __asan_init() {
   }
 
   uptr shadow_start = kLowShadowBeg;
-  if (kLowShadowBeg > 0) shadow_start -= kMmapGranularity;
+  if (kLowShadowBeg > 0) shadow_start -= GetMmapGranularity();
   uptr shadow_end = kHighShadowEnd;
   if (MemoryRangeIsAvailable(shadow_start, shadow_end)) {
     if (kLowShadowBeg != kLowShadowEnd) {
       // mmap the low shadow plus at least one page.
-      ReserveShadowMemoryRange(kLowShadowBeg - kMmapGranularity, kLowShadowEnd);
+      ReserveShadowMemoryRange(kLowShadowBeg - GetMmapGranularity(),
+                               kLowShadowEnd);
     }
     // mmap the high shadow.
     ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd);
index a75f154..fdb7820 100644 (file)
@@ -336,11 +336,12 @@ TEST(AddressSanitizer, MemsetWildAddressTest) {
   typedef void*(*memset_p)(void*, int, size_t);
   // Prevent inlining of memset().
   volatile memset_p libc_memset = (memset_p)memset;
-  EXPECT_DEATH(libc_memset((void*)(kLowShadowBeg + kPageSize), 0, 100),
+  uptr PageSize = GetPageSizeCached();
+  EXPECT_DEATH(libc_memset((void*)(kLowShadowBeg + PageSize), 0, 100),
                "unknown-crash.*low shadow");
-  EXPECT_DEATH(libc_memset((void*)(kShadowGapBeg + kPageSize), 0, 100),
+  EXPECT_DEATH(libc_memset((void*)(kShadowGapBeg + PageSize), 0, 100),
                "unknown-crash.*shadow gap");
-  EXPECT_DEATH(libc_memset((void*)(kHighShadowBeg + kPageSize), 0, 100),
+  EXPECT_DEATH(libc_memset((void*)(kHighShadowBeg + PageSize), 0, 100),
                "unknown-crash.*high shadow");
 }
 
index 985f91a..c439330 100644 (file)
@@ -63,7 +63,7 @@ void *LowLevelAllocator::Allocate(uptr size) {
   // Align allocation size.
   size = RoundUpTo(size, 8);
   if (allocated_end_ - allocated_current_ < (sptr)size) {
-    uptr size_to_allocate = Max(size, kPageSize);
+    uptr size_to_allocate = Max(size, GetPageSizeCached());
     allocated_current_ =
         (char*)MmapOrDie(size_to_allocate, __FUNCTION__);
     allocated_end_ = allocated_current_ + size_to_allocate;
index a537ff0..2f4e697 100644 (file)
@@ -217,7 +217,6 @@ class SizeClassAllocator64 {
   }
 
   static uptr AllocBeg()  { return kSpaceBeg; }
-  static uptr AllocEnd()  { return kSpaceBeg  + kSpaceSize + AdditionalSize(); }
   static uptr AllocSize() { return kSpaceSize + AdditionalSize(); }
 
   static const uptr kNumClasses = 256;  // Power of two <= 256
@@ -243,7 +242,7 @@ class SizeClassAllocator64 {
 
   static uptr AdditionalSize() {
     uptr res = sizeof(RegionInfo) * kNumClasses;
-    CHECK_EQ(res % kPageSize, 0);
+    CHECK_EQ(res % GetPageSizeCached(), 0);
     return res;
   }
 
@@ -366,17 +365,18 @@ class LargeMmapAllocator {
  public:
   void Init() {
     internal_memset(this, 0, sizeof(*this));
+    page_size_ = GetPageSizeCached();
   }
   void *Allocate(uptr size, uptr alignment) {
     CHECK(IsPowerOfTwo(alignment));
     uptr map_size = RoundUpMapSize(size);
-    if (alignment > kPageSize)
+    if (alignment > page_size_)
       map_size += alignment;
     if (map_size < size) return 0;  // Overflow.
     uptr map_beg = reinterpret_cast<uptr>(
         MmapOrDie(map_size, "LargeMmapAllocator"));
     uptr map_end = map_beg + map_size;
-    uptr res = map_beg + kPageSize;
+    uptr res = map_beg + page_size_;
     if (res & (alignment - 1))  // Align.
       res += alignment - (res & (alignment - 1));
     CHECK_EQ(0, res & (alignment - 1));
@@ -423,7 +423,7 @@ class LargeMmapAllocator {
 
   bool PointerIsMine(void *p) {
     // Fast check.
-    if ((reinterpret_cast<uptr>(p) % kPageSize) != 0) return false;
+    if ((reinterpret_cast<uptr>(p) & (page_size_ - 1))) return false;
     SpinMutexLock l(&mutex_);
     for (Header *l = list_; l; l = l->next) {
       if (GetUser(l) == p) return true;
@@ -432,10 +432,10 @@ class LargeMmapAllocator {
   }
 
   uptr GetActuallyAllocatedSize(void *p) {
-    return RoundUpMapSize(GetHeader(p)->size) - kPageSize;
+    return RoundUpMapSize(GetHeader(p)->size) - page_size_;
   }
 
-  // At least kPageSize/2 metadata bytes is available.
+  // At least page_size_/2 metadata bytes is available.
   void *GetMetaData(void *p) {
     return GetHeader(p) + 1;
   }
@@ -459,17 +459,20 @@ class LargeMmapAllocator {
     Header *prev;
   };
 
-  Header *GetHeader(uptr p) { return reinterpret_cast<Header*>(p - kPageSize); }
+  Header *GetHeader(uptr p) {
+    return reinterpret_cast<Header*>(p - page_size_);
+  }
   Header *GetHeader(void *p) { return GetHeader(reinterpret_cast<uptr>(p)); }
 
   void *GetUser(Header *h) {
-    return reinterpret_cast<void*>(reinterpret_cast<uptr>(h) + kPageSize);
+    return reinterpret_cast<void*>(reinterpret_cast<uptr>(h) + page_size_);
   }
 
   uptr RoundUpMapSize(uptr size) {
-    return RoundUpTo(size, kPageSize) + kPageSize;
+    return RoundUpTo(size, page_size_) + page_size_;
   }
 
+  uptr page_size_;
   Header *list_;
   SpinMutex mutex_;
 };
index 1842446..dc53d88 100644 (file)
@@ -23,23 +23,11 @@ namespace __sanitizer {
 // Constants.
 const uptr kWordSize = SANITIZER_WORDSIZE / 8;
 const uptr kWordSizeInBits = 8 * kWordSize;
+
 #if defined(__powerpc__) || defined(__powerpc64__)
-// Current PPC64 kernels use 64K pages sizes, but they can be
-// configured with 4K or even other sizes.
-// We may want to use getpagesize() or sysconf(_SC_PAGESIZE) here rather than
-// hardcoding the values, but today these values need to be compile-time
-// constants.
-const uptr kPageSize = 1UL << 16;
 const uptr kCacheLineSize = 128;
-const uptr kMmapGranularity = kPageSize;
-#elif !defined(_WIN32)
-const uptr kPageSize = 1UL << 12;
-const uptr kCacheLineSize = 64;
-const uptr kMmapGranularity = kPageSize;
 #else
-const uptr kPageSize = 1UL << 12;
 const uptr kCacheLineSize = 64;
-const uptr kMmapGranularity = 1UL << 16;
 #endif
 
 uptr GetPageSize();
index 4da41ee..aeb2914 100644 (file)
@@ -65,7 +65,7 @@ void StackTrace::PrintStack(const uptr *addr, uptr size,
                             bool symbolize, const char *strip_file_prefix,
                             SymbolizeCallback symbolize_callback ) {
   MemoryMappingLayout proc_maps;
-  InternalScopedBuffer<char> buff(kPageSize * 2);
+  InternalScopedBuffer<char> buff(GetPageSizeCached() * 2);
   InternalScopedBuffer<AddressInfo> addr_frames(64);
   uptr frame_num = 0;
   for (uptr i = 0; i < size && addr[i]; i++) {
index 2d97d3a..e915399 100644 (file)
@@ -182,7 +182,7 @@ TEST(SanitizerCommon, LargeMmapAllocator) {
 
   for (uptr alignment = 8; alignment <= (1<<28); alignment *= 2) {
     for (int i = 0; i < kNumAllocs; i++) {
-      uptr size = ((i % 10) + 1) * kPageSize;
+      uptr size = ((i % 10) + 1) * 4096;
       allocated[i] = a.Allocate(size, alignment);
       CHECK_EQ(0, (uptr)allocated[i] % alignment);
       char *p = (char*)allocated[i];
index 3552f0d..d95c217 100644 (file)
@@ -75,13 +75,13 @@ int posix_memalign(void **memptr, size_t alignment, size_t size) {
 
 void *valloc(size_t size) {
   assert(inited);
-  return allocator.Allocate(&cache, size, kPageSize);
+  return allocator.Allocate(&cache, size, GetPageSizeCached());
 }
 
 void *pvalloc(size_t size) {
   assert(inited);
-  if (size == 0) size = kPageSize;
-  return allocator.Allocate(&cache, size, kPageSize);
+  if (size == 0) size = GetPageSizeCached();
+  return allocator.Allocate(&cache, size, GetPageSizeCached());
 }
 }
 #endif
index 26e1712..a3c962d 100644 (file)
@@ -52,7 +52,7 @@ static const uptr kLinuxAppMemMsk = 0x7c0000000000ULL;
 
 static const uptr kLinuxShadowBeg = MemToShadow(kLinuxAppMemBeg);
 static const uptr kLinuxShadowEnd =
-  MemToShadow(kLinuxAppMemEnd) | (kPageSize - 1);
+    MemToShadow(kLinuxAppMemEnd) | 0xff;
 
 static inline bool IsAppMem(uptr mem) {
   return mem >= kLinuxAppMemBeg && mem <= kLinuxAppMemEnd;