[llvm-commits] [compiler-rt] r147328 - in /compiler-rt/trunk/lib/asan: asan_allocator.cc asan_internal.h asan_linux.cc asan_mac.cc asan_rtl.cc asan_thread.cc

Kostya Serebryany kcc at google.com
Wed Dec 28 15:28:55 PST 2011


Author: kcc
Date: Wed Dec 28 17:28:54 2011
New Revision: 147328

URL: http://llvm.org/viewvc/llvm-project?rev=147328&view=rev
Log:
[asan] refactoring: don't #include <sys/mman.h> in non-os-specific files

Modified:
    compiler-rt/trunk/lib/asan/asan_allocator.cc
    compiler-rt/trunk/lib/asan/asan_internal.h
    compiler-rt/trunk/lib/asan/asan_linux.cc
    compiler-rt/trunk/lib/asan/asan_mac.cc
    compiler-rt/trunk/lib/asan/asan_rtl.cc
    compiler-rt/trunk/lib/asan/asan_thread.cc

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=147328&r1=147327&r2=147328&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_allocator.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_allocator.cc Wed Dec 28 17:28:54 2011
@@ -35,7 +35,6 @@
 #include "asan_thread.h"
 #include "asan_thread_registry.h"
 
-#include <sys/mman.h>
 #include <stdint.h>
 #include <string.h>
 #include <unistd.h>

Modified: compiler-rt/trunk/lib/asan/asan_internal.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_internal.h?rev=147328&r1=147327&r2=147328&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_internal.h (original)
+++ compiler-rt/trunk/lib/asan/asan_internal.h Wed Dec 28 17:28:54 2011
@@ -88,9 +88,10 @@
 // asan_linux.cc / asan_mac.cc
 void *AsanDoesNotSupportStaticLinkage();
 int AsanOpenReadonly(const char* filename);
-void *asan_mmap(void *addr, size_t length, int prot, int flags,
-                int fd, uint64_t offset);
 
+void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size);
+void *AsanMmapFixedReserve(uintptr_t fixed_addr, size_t size);
+void *AsanMprotect(uintptr_t fixed_addr, size_t size);
 void *AsanMmapSomewhereOrDie(size_t size, const char *where);
 void AsanUnmapOrDie(void *ptr, size_t size);
 

Modified: compiler-rt/trunk/lib/asan/asan_linux.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_linux.cc?rev=147328&r1=147327&r2=147328&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_linux.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_linux.cc Wed Dec 28 17:28:54 2011
@@ -30,7 +30,7 @@
   return &_DYNAMIC;
 }
 
-void *asan_mmap(void *addr, size_t length, int prot, int flags,
+static void *asan_mmap(void *addr, size_t length, int prot, int flags,
                 int fd, uint64_t offset) {
 # if __WORDSIZE == 64
   return (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset);
@@ -50,6 +50,27 @@
   return res;
 }
 
+void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size) {
+  return asan_mmap((void*)fixed_addr, size,
+                   PROT_READ | PROT_WRITE,
+                   MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
+                   0, 0);
+}
+
+void *AsanMmapFixedReserve(uintptr_t fixed_addr, size_t size) {
+  return asan_mmap((void*)fixed_addr, size,
+                   PROT_READ | PROT_WRITE,
+                   MAP_PRIVATE | MAP_ANON | MAP_FIXED,
+                   0, 0);
+}
+
+void *AsanMprotect(uintptr_t fixed_addr, size_t size) {
+  return asan_mmap((void*)fixed_addr, size,
+                   PROT_NONE,
+                   MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
+                   0, 0);
+}
+
 void AsanUnmapOrDie(void *addr, size_t size) {
   if (!addr || !size) return;
   int res = syscall(__NR_munmap, addr, size);

Modified: compiler-rt/trunk/lib/asan/asan_mac.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_mac.cc?rev=147328&r1=147327&r2=147328&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_mac.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_mac.cc Wed Dec 28 17:28:54 2011
@@ -22,6 +22,7 @@
 #include "asan_thread_registry.h"
 
 #include <sys/mman.h>
+#include <fcntl.h>
 #include <unistd.h>
 
 #include <new>
@@ -40,12 +41,12 @@
   return NULL;
 }
 
-void *asan_mmap(void *addr, size_t length, int prot, int flags,
+static void *asan_mmap(void *addr, size_t length, int prot, int flags,
                 int fd, uint64_t offset) {
   return mmap(addr, length, prot, flags, fd, offset);
 }
 
-ssize_t asan_write(int fd, const void *buf, size_t count) {
+ssize_t AsanWrite(int fd, const void *buf, size_t count) {
   return write(fd, buf, count);
 }
 
@@ -60,6 +61,27 @@
   return res;
 }
 
+void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size) {
+  return asan_mmap((void*)fixed_addr, size,
+                   PROT_READ | PROT_WRITE,
+                   MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
+                   0, 0);
+}
+
+void *AsanMmapFixedReserve(uintptr_t fixed_addr, size_t size) {
+  return asan_mmap((void*)fixed_addr, size,
+                   PROT_READ | PROT_WRITE,
+                   MAP_PRIVATE | MAP_ANON | MAP_FIXED,
+                   0, 0);
+}
+
+void *AsanMprotect(uintptr_t fixed_addr, size_t size) {
+  return asan_mmap((void*)fixed_addr, size,
+                   PROT_NONE,
+                   MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
+                   0, 0);
+}
+
 void AsanUnmapOrDie(void *addr, size_t size) {
   if (!addr || !size) return;
   int res = munmap(addr, size);

Modified: compiler-rt/trunk/lib/asan/asan_rtl.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_rtl.cc?rev=147328&r1=147327&r2=147328&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_rtl.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_rtl.cc Wed Dec 28 17:28:54 2011
@@ -16,9 +16,7 @@
 #include "asan_interface.h"
 #include "asan_internal.h"
 #include "asan_lock.h"
-#ifdef __APPLE__
 #include "asan_mac.h"
-#endif
 #include "asan_mapping.h"
 #include "asan_stack.h"
 #include "asan_stats.h"
@@ -36,7 +34,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/mman.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #ifndef ANDROID
@@ -188,37 +185,13 @@
   ShowStatsAndAbort();
 }
 
-static char *mmap_pages(size_t start_page, size_t n_pages, const char *mem_type,
-                        bool abort_on_failure = true) {
-  void *res = asan_mmap((void*)start_page, kPageSize * n_pages,
-                   PROT_READ | PROT_WRITE,
-                   MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, 0, 0);
-  // Printf("%p => %p\n", (void*)start_page, res);
-  char *ch = (char*)res;
-  if (res == (void*)-1L && abort_on_failure) {
-    OutOfMemoryMessageAndDie(mem_type, n_pages * kPageSize);
-  }
-  CHECK(res == (void*)start_page || res == (void*)-1L);
-  return ch;
-}
-
-// mmap range [beg, end]
-static char *mmap_range(uintptr_t beg, uintptr_t end, const char *mem_type) {
+// Reserve memory range [beg, end].
+static void ReserveShadowMemoryRange(uintptr_t beg, uintptr_t end) {
   CHECK((beg % kPageSize) == 0);
   CHECK(((end + 1) % kPageSize) == 0);
-  // Printf("mmap_range %p %p %ld\n", beg, end, (end - beg) / kPageSize);
-  return mmap_pages(beg, (end - beg + 1) / kPageSize, mem_type);
-}
-
-// protect range [beg, end]
-static void protect_range(uintptr_t beg, uintptr_t end) {
-  CHECK((beg % kPageSize) == 0);
-  CHECK(((end+1) % kPageSize) == 0);
-  // Printf("protect_range %p %p %ld\n", beg, end, (end - beg) / kPageSize);
-  void *res = asan_mmap((void*)beg, end - beg + 1,
-                   PROT_NONE,
-                   MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, 0, 0);
-  CHECK(res == (void*)beg);
+  size_t size = end - beg + 1;
+  void *res = AsanMmapFixedNoReserve(beg, size);
+  CHECK(res == (void*)beg && "ReserveShadowMemoryRange failed");
 }
 
 // ---------------------- LowLevelAllocator ------------- {{{1
@@ -349,9 +322,7 @@
     // this address.
     const uintptr_t chunk_size = kPageSize << 10;  // 4M
     uintptr_t chunk = addr & ~(chunk_size - 1);
-    asan_mmap((void*)chunk, chunk_size,
-                   PROT_READ | PROT_WRITE,
-                   MAP_PRIVATE | MAP_ANON | MAP_FIXED, 0, 0);
+    AsanMmapFixedReserve(chunk, chunk_size);
     return;
   }
   // Write the first message using the bullet-proof write.
@@ -813,13 +784,14 @@
     if (!FLAG_lazy_shadow) {
       if (kLowShadowBeg != kLowShadowEnd) {
         // mmap the low shadow plus one page.
-        mmap_range(kLowShadowBeg - kPageSize, kLowShadowEnd, "LowShadow");
+        ReserveShadowMemoryRange(kLowShadowBeg - kPageSize, kLowShadowEnd);
       }
       // mmap the high shadow.
-      mmap_range(kHighShadowBeg, kHighShadowEnd, "HighShadow");
+      ReserveShadowMemoryRange(kHighShadowBeg, kHighShadowEnd);
     }
     // protect the gap
-    protect_range(kShadowGapBeg, kShadowGapEnd);
+    void *prot = AsanMprotect(kShadowGapBeg, kShadowGapEnd - kShadowGapBeg + 1);
+    CHECK(prot == (void*)kShadowGapBeg);
   }
 
   // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited

Modified: compiler-rt/trunk/lib/asan/asan_thread.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_thread.cc?rev=147328&r1=147327&r2=147328&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_thread.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_thread.cc Wed Dec 28 17:28:54 2011
@@ -21,7 +21,6 @@
 #include "sysinfo/sysinfo.h"
 #endif
 
-#include <sys/mman.h>
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <pthread.h>





More information about the llvm-commits mailing list