[compiler-rt] r306834 - [sanitizer] Small tweaks and fixes to allocator related functions

Kostya Kortchinsky via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 30 09:05:40 PDT 2017


Author: cryptoad
Date: Fri Jun 30 09:05:40 2017
New Revision: 306834

URL: http://llvm.org/viewvc/llvm-project?rev=306834&view=rev
Log:
[sanitizer] Small tweaks and fixes to allocator related functions

Summary:
In `sanitizer_allocator_primary32.h`:
- rounding up in `MapWithCallback` is not needed as `MmapOrDie` does it. Note
  that the 64-bit counterpart doesn't round up, this keeps the behavior
  consistent;
- since `IsAligned` exists, use it in `AllocateRegion`;
- in `PopulateFreeList`:
  - checking `b->Count` to be greater than 0 when `b->Count() == max_count` is
    redundant when done more than once. Just check that `max_count` is greater
    than 0 out of the loop; the compiler (at least on ARM) didn't optimize it;
  - mark the batch creation failure as `UNLIKELY`;

In `sanitizer_allocator_primary64.h`:
- in `MapWithCallback`, mark the failure condition as `UNLIKELY`;

In `sanitizer_posix.h`:
- mark a bunch of Mmap related failure conditions as `UNLIKELY`;
- in `MmapAlignedOrDieOnFatalError`, we have `IsAligned`, so use it; rearrange
  the conditions as one test was redudant;
- in `MmapFixedImpl`, 30 chars was not large enough to hold the message and a
  full 64-bit address (or at least a 48-bit usermode address), increase to 40.

Reviewers: alekseyshl

Reviewed By: alekseyshl

Subscribers: aemerson, kubamracek, kristof.beyls, llvm-commits

Differential Revision: https://reviews.llvm.org/D34840

Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary32.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary64.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary32.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary32.h?rev=306834&r1=306833&r2=306834&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary32.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary32.h Fri Jun 30 09:05:40 2017
@@ -118,7 +118,6 @@ class SizeClassAllocator32 {
   }
 
   void *MapWithCallback(uptr size) {
-    size = RoundUpTo(size, GetPageSizeCached());
     void *res = MmapOrDie(size, "SizeClassAllocator32");
     MapUnmapCallback().OnMap((uptr)res, size);
     return res;
@@ -285,7 +284,7 @@ class SizeClassAllocator32 {
       return 0;
     MapUnmapCallback().OnMap(res, kRegionSize);
     stat->Add(AllocatorStatMapped, kRegionSize);
-    CHECK_EQ(0U, (res & (kRegionSize - 1)));
+    CHECK(IsAligned(res, kRegionSize));
     possible_regions.set(ComputeRegionId(res), static_cast<u8>(class_id));
     return res;
   }
@@ -303,17 +302,17 @@ class SizeClassAllocator32 {
       return false;
     uptr n_chunks = kRegionSize / (size + kMetadataSize);
     uptr max_count = TransferBatch::MaxCached(class_id);
+    CHECK_GT(max_count, 0);
     TransferBatch *b = nullptr;
     for (uptr i = reg; i < reg + n_chunks * size; i += size) {
       if (!b) {
         b = c->CreateBatch(class_id, this, (TransferBatch*)i);
-        if (!b)
+        if (UNLIKELY(!b))
           return false;
         b->Clear();
       }
       b->Add((void*)i);
       if (b->Count() == max_count) {
-        CHECK_GT(b->Count(), 0);
         sci->free_list.push_back(b);
         b = nullptr;
       }

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary64.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary64.h?rev=306834&r1=306833&r2=306834&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary64.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary64.h Fri Jun 30 09:05:40 2017
@@ -389,7 +389,7 @@ class SizeClassAllocator64 {
 
   bool MapWithCallback(uptr beg, uptr size) {
     uptr mapped = reinterpret_cast<uptr>(MmapFixedOrDieOnFatalError(beg, size));
-    if (!mapped)
+    if (UNLIKELY(!mapped))
       return false;
     CHECK_EQ(beg, mapped);
     MapUnmapCallback().OnMap(beg, size);

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc?rev=306834&r1=306833&r2=306834&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc Fri Jun 30 09:05:40 2017
@@ -129,7 +129,7 @@ void *MmapOrDie(uptr size, const char *m
                            PROT_READ | PROT_WRITE,
                            MAP_PRIVATE | MAP_ANON, -1, 0);
   int reserrno;
-  if (internal_iserror(res, &reserrno))
+  if (UNLIKELY(internal_iserror(res, &reserrno)))
     ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno, raw_report);
   IncreaseTotalMmap(size);
   return (void *)res;
@@ -138,7 +138,7 @@ void *MmapOrDie(uptr size, const char *m
 void UnmapOrDie(void *addr, uptr size) {
   if (!addr || !size) return;
   uptr res = internal_munmap(addr, size);
-  if (internal_iserror(res)) {
+  if (UNLIKELY(internal_iserror(res))) {
     Report("ERROR: %s failed to deallocate 0x%zx (%zd) bytes at address %p\n",
            SanitizerToolName, size, size, addr);
     CHECK("unable to unmap" && 0);
@@ -152,7 +152,7 @@ void *MmapOrDieOnFatalError(uptr size, c
                            PROT_READ | PROT_WRITE,
                            MAP_PRIVATE | MAP_ANON, -1, 0);
   int reserrno;
-  if (internal_iserror(res, &reserrno)) {
+  if (UNLIKELY(internal_iserror(res, &reserrno))) {
     if (reserrno == ENOMEM)
       return nullptr;
     ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno);
@@ -170,15 +170,15 @@ void *MmapAlignedOrDieOnFatalError(uptr
   CHECK(IsPowerOfTwo(alignment));
   uptr map_size = size + alignment;
   uptr map_res = (uptr)MmapOrDieOnFatalError(map_size, mem_type);
-  if (!map_res)
+  if (UNLIKELY(!map_res))
     return nullptr;
   uptr map_end = map_res + map_size;
   uptr res = map_res;
-  if (res & (alignment - 1))  // Not aligned.
-    res = (map_res + alignment) & ~(alignment - 1);
-  uptr end = res + size;
-  if (res != map_res)
+  if (!IsAligned(res, alignment)) {
+    res = (map_res + alignment - 1) & ~(alignment - 1);
     UnmapOrDie((void*)map_res, res - map_res);
+  }
+  uptr end = res + size;
   if (end != map_end)
     UnmapOrDie((void*)end, map_end - end);
   return (void*)res;
@@ -192,7 +192,7 @@ void *MmapNoReserveOrDie(uptr size, cons
                          MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
                          -1, 0);
   int reserrno;
-  if (internal_iserror(p, &reserrno))
+  if (UNLIKELY(internal_iserror(p, &reserrno)))
     ReportMmapFailureAndDie(size, mem_type, "allocate noreserve", reserrno);
   IncreaseTotalMmap(size);
   return (void *)p;
@@ -206,10 +206,10 @@ void *MmapFixedImpl(uptr fixed_addr, upt
       MAP_PRIVATE | MAP_ANON | MAP_FIXED,
       -1, 0);
   int reserrno;
-  if (internal_iserror(p, &reserrno)) {
+  if (UNLIKELY(internal_iserror(p, &reserrno))) {
     if (tolerate_enomem && reserrno == ENOMEM)
       return nullptr;
-    char mem_type[30];
+    char mem_type[40];
     internal_snprintf(mem_type, sizeof(mem_type), "memory at address 0x%zx",
                       fixed_addr);
     ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno);




More information about the llvm-commits mailing list