[PATCH] D21900: [compiler-rt] Fix sanitizer memory allocator on win64.

Etienne Bergeron via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 30 11:24:44 PDT 2016


etienneb created this revision.
etienneb added a reviewer: rnk.
etienneb added subscribers: chrisha, wang0109, llvm-commits.
Herald added a subscriber: kubabrecka.

This patch is fixing unittests for sanitizer memory allocator.

There was two issues:
  1) The VirtualAlloc can't reserve twice a memory range.
     The memory space used by the SizeClass allocator is reserved
     with NoAccess and page are COMMIT on demand (using MmapFixedOrDie).

  2) The address space is allocated using two VirtualAlloc. The first one
     for the memory space, the second one for the AdditionnalSpace (after).

     On windows, they need to be freed separately.

http://reviews.llvm.org/D21900

Files:
  lib/sanitizer_common/sanitizer_allocator.h
  lib/sanitizer_common/sanitizer_win.cc
  lib/sanitizer_common/tests/sanitizer_allocator_test.cc

Index: lib/sanitizer_common/tests/sanitizer_allocator_test.cc
===================================================================
--- lib/sanitizer_common/tests/sanitizer_allocator_test.cc
+++ lib/sanitizer_common/tests/sanitizer_allocator_test.cc
@@ -29,9 +29,9 @@
 #if !SANITIZER_DEBUG
 
 #if SANITIZER_CAN_USE_ALLOCATOR64
-static const uptr kAllocatorSpace = 0x700000000000ULL;
-static const uptr kAllocatorSize  = 0x010000000000ULL;  // 1T.
-static const u64 kAddressSpaceSize = 1ULL << 47;
+static const uptr kAllocatorSpace = 0x10000000000ULL;
+static const uptr kAllocatorSize  =  0x10000000000ULL;  // 1T.
+static const u64 kAddressSpaceSize = 1ULL << 40;
 
 typedef SizeClassAllocator64<
   kAllocatorSpace, kAllocatorSize, 16, DefaultSizeClassMap> Allocator64;
@@ -235,10 +235,12 @@
 TEST(SanitizerCommon, SizeClassAllocator64CompactGetBlockBegin) {
   SizeClassAllocatorGetBlockBeginStress<Allocator64Compact>();
 }
+#endif  // SANITIZER_CAN_USE_ALLOCATOR64
+
 TEST(SanitizerCommon, SizeClassAllocator32CompactGetBlockBegin) {
   SizeClassAllocatorGetBlockBeginStress<Allocator32Compact>();
 }
-#endif  // SANITIZER_CAN_USE_ALLOCATOR64
+
 
 struct TestMapUnmapCallback {
   static int map_count, unmap_count;
@@ -266,7 +268,8 @@
   a->AllocateBatch(&stats, &cache, 32);
   EXPECT_EQ(TestMapUnmapCallback::map_count, 3);  // State + alloc + metadata.
   a->TestOnlyUnmap();
-  EXPECT_EQ(TestMapUnmapCallback::unmap_count, 1);  // The whole thing.
+  // Expect unmapping of 'memory space' and 'additional memory'.
+  EXPECT_EQ(TestMapUnmapCallback::unmap_count, 2);
   delete a;
 }
 #endif
Index: lib/sanitizer_common/sanitizer_win.cc
===================================================================
--- lib/sanitizer_common/sanitizer_win.cc
+++ lib/sanitizer_common/sanitizer_win.cc
@@ -183,7 +183,10 @@
 
 void *MmapFixedOrDie(uptr fixed_addr, uptr size) {
   void *p = VirtualAlloc((LPVOID)fixed_addr, size,
-      MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
+      MEM_COMMIT, PAGE_READWRITE);
+  if (p == 0)
+    p = VirtualAlloc((LPVOID)fixed_addr, size,
+        MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
   if (p == 0) {
     char mem_type[30];
     internal_snprintf(mem_type, sizeof(mem_type), "memory at address 0x%zx",
@@ -201,7 +204,7 @@
 void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name) {
   (void)name; // unsupported
   void *res = VirtualAlloc((LPVOID)fixed_addr, size,
-                           MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS);
+                           MEM_RESERVE, PAGE_NOACCESS);
   if (res == 0)
     Report("WARNING: %s failed to "
            "mprotect %p (%zd) bytes at %p (error code: %d)\n",
Index: lib/sanitizer_common/sanitizer_allocator.h
===================================================================
--- lib/sanitizer_common/sanitizer_allocator.h
+++ lib/sanitizer_common/sanitizer_allocator.h
@@ -421,7 +421,8 @@
 
   // Test-only.
   void TestOnlyUnmap() {
-    UnmapWithCallback(SpaceBeg(), kSpaceSize + AdditionalSize());
+    UnmapWithCallback(SpaceBeg(), kSpaceSize);
+    UnmapWithCallback(SpaceEnd(), AdditionalSize());
   }
 
   void PrintStats() {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D21900.62387.patch
Type: text/x-patch
Size: 3141 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160630/8320587d/attachment.bin>


More information about the llvm-commits mailing list