[llvm-commits] [compiler-rt] r171144 - in /compiler-rt/trunk/lib: asan/asan_allocator2.cc sanitizer_common/sanitizer_common.h sanitizer_common/sanitizer_posix.cc sanitizer_common/tests/sanitizer_allocator64_testlib.cc tsan/rtl/tsan_platform_linux.cc tsan/rtl/tsan_rtl.h

Kostya Serebryany kcc at google.com
Wed Dec 26 23:37:25 PST 2012


Author: kcc
Date: Thu Dec 27 01:37:24 2012
New Revision: 171144

URL: http://llvm.org/viewvc/llvm-project?rev=171144&view=rev
Log:
[asan/tsan] when unmapping a chunk of user memory, apply madvise(MADV_DONTNEED) to the corresponding chunk of shadow memory. Also update sanitizer_allocator64_testlib.cc

Modified:
    compiler-rt/trunk/lib/asan/asan_allocator2.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc
    compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator64_testlib.cc
    compiler-rt/trunk/lib/tsan/rtl/tsan_platform_linux.cc
    compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.h

Modified: compiler-rt/trunk/lib/asan/asan_allocator2.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_allocator2.cc?rev=171144&r1=171143&r2=171144&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_allocator2.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_allocator2.cc Thu Dec 27 01:37:24 2012
@@ -37,10 +37,17 @@
     AsanStats &thread_stats = asanThreadRegistry().GetCurrentThreadStats();
     thread_stats.mmaps++;
     thread_stats.mmaped += size;
-    // thread_stats.mmaped_by_size[size_class] += n_chunks;
   }
   void OnUnmap(uptr p, uptr size) const {
     PoisonShadow(p, size, 0);
+    // We are about to unmap a chunk of user memory.
+    // Mark the corresponding shadow memory as not needed.
+    // Since asan's mapping is compacting, the shadow chunk may be
+    // not page-aligned, so we only flush the page-aligned portion.
+    uptr page_size = GetPageSizeCached();
+    uptr shadow_beg = RoundUpTo(MemToShadow(p), page_size);
+    uptr shadow_end = RoundDownTo(MemToShadow(p + size), page_size);
+    FlushUnneededShadowMemory(shadow_beg, shadow_end - shadow_beg);
     // Statistics.
     AsanStats &thread_stats = asanThreadRegistry().GetCurrentThreadStats();
     thread_stats.munmaps++;

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=171144&r1=171143&r2=171144&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h Thu Dec 27 01:37:24 2012
@@ -50,6 +50,7 @@
 void *MmapAlignedOrDie(uptr size, uptr alignment, const char *mem_type);
 // Used to check if we can map shadow memory to a fixed location.
 bool MemoryRangeIsAvailable(uptr range_start, uptr range_end);
+void FlushUnneededShadowMemory(uptr addr, uptr size);
 
 // Internal allocator
 void *InternalAlloc(uptr 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=171144&r1=171143&r2=171144&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc Thu Dec 27 01:37:24 2012
@@ -115,6 +115,10 @@
                        -1, 0);
 }
 
+void FlushUnneededShadowMemory(uptr addr, uptr size) {
+  madvise((void*)addr, size, MADV_DONTNEED);
+}
+
 void *MapFileToMemory(const char *file_name, uptr *buff_size) {
   fd_t fd = internal_open(file_name, false);
   CHECK_NE(fd, kInvalidFd);

Modified: compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator64_testlib.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator64_testlib.cc?rev=171144&r1=171143&r2=171144&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator64_testlib.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator64_testlib.cc Thu Dec 27 01:37:24 2012
@@ -10,7 +10,14 @@
 // The primary purpose of this file is an end-to-end integration test
 // for CombinedAllocator.
 //===----------------------------------------------------------------------===//
+/* Usage:
+clang++ -fno-exceptions  -g -fPIC -I. -I../include -Isanitizer \
+ sanitizer_common/tests/sanitizer_allocator64_testlib.cc \
+ sanitizer_common/sanitizer_*.cc -shared -o testmalloc.so
+LD_PRELOAD=`pwd`/testmalloc.so /your/app
+*/
 #include "sanitizer_common/sanitizer_allocator.h"
+#include "sanitizer_common/sanitizer_common.h"
 #include <stddef.h>
 #include <stdio.h>
 #include <unistd.h>
@@ -20,10 +27,9 @@
 static const uptr kAllocatorSpace = 0x600000000000ULL;
 static const uptr kAllocatorSize = 0x10000000000;  // 1T.
 
-typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, 16,
-  DefaultSizeClassMap> PrimaryAllocator;
-typedef SizeClassAllocatorLocalCache<PrimaryAllocator::kNumClasses,
-  PrimaryAllocator> AllocatorCache;
+typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, 0,
+  CompactSizeClassMap> PrimaryAllocator;
+typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
 typedef LargeMmapAllocator<> SecondaryAllocator;
 typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
           SecondaryAllocator> Allocator;
@@ -34,7 +40,7 @@
 static int inited = 0;
 
 __attribute__((constructor))
-void Init() {
+static void Init() {
   if (inited) return;
   inited = true;  // this must happen before any threads are created.
   allocator.Init();
@@ -51,37 +57,54 @@
 }
 
 void free(void *p) {
-  assert(inited);
+  if (!inited) return;
+  // assert(inited);
   allocator.Deallocate(&cache, p);
 }
 
 void *calloc(size_t nmemb, size_t size) {
+  Init();
   assert(inited);
   return allocator.Allocate(&cache, nmemb * size, 8, /*cleared=*/true);
 }
 
 void *realloc(void *p, size_t new_size) {
+  Init();
   assert(inited);
   return allocator.Reallocate(&cache, p, new_size, 8);
 }
 
-void *memalign() { assert(0); }
+void *memalign(size_t boundary, size_t size) {
+  Init();
+  return allocator.Allocate(&cache, size, boundary);
+}
+void *__libc_memalign(size_t boundary, size_t size) {
+  Init();
+  return allocator.Allocate(&cache, size, boundary);
+}
 
 int posix_memalign(void **memptr, size_t alignment, size_t size) {
+  Init();
   *memptr = allocator.Allocate(&cache, size, alignment);
   CHECK_EQ(((uptr)*memptr & (alignment - 1)), 0);
   return 0;
 }
 
 void *valloc(size_t size) {
+  Init();
   assert(inited);
   return allocator.Allocate(&cache, size, GetPageSizeCached());
 }
 
 void *pvalloc(size_t size) {
+  Init();
   assert(inited);
   if (size == 0) size = GetPageSizeCached();
   return allocator.Allocate(&cache, size, GetPageSizeCached());
 }
+
+void malloc_usable_size() { }
+void mallinfo() { }
+void mallopt() { }
 }
 #endif

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_platform_linux.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_platform_linux.cc?rev=171144&r1=171143&r2=171144&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_platform_linux.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_platform_linux.cc Thu Dec 27 01:37:24 2012
@@ -71,9 +71,7 @@
 }
 
 void FlushShadowMemory() {
-  madvise((void*)kLinuxShadowBeg,
-          kLinuxShadowEnd - kLinuxShadowBeg,
-          MADV_DONTNEED);
+  FlushUnneededShadowMemory(kLinuxShadowBeg, kLinuxShadowEnd - kLinuxShadowBeg);
 }
 
 #ifndef TSAN_GO

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.h?rev=171144&r1=171143&r2=171144&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.h (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.h Thu Dec 27 01:37:24 2012
@@ -65,10 +65,22 @@
 #endif
 const uptr kAllocatorSize  =  0x10000000000ULL;  // 1T.
 
+struct TsanMapUnmapCallback {
+  void OnMap(uptr p, uptr size) const { }
+  void OnUnmap(uptr p, uptr size) const {
+    // We are about to unmap a chunk of user memory.
+    // Mark the corresponding shadow memory as not needed.
+    uptr shadow_beg = MemToShadow(p);
+    uptr shadow_end = MemToShadow(p + size);
+    CHECK(IsAligned(shadow_end|shadow_beg, GetPageSizeCached()));
+    FlushUnneededShadowMemory(shadow_beg, shadow_end - shadow_beg);
+  }
+};
+
 typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, sizeof(MBlock),
     DefaultSizeClassMap> PrimaryAllocator;
 typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
-typedef LargeMmapAllocator<> SecondaryAllocator;
+typedef LargeMmapAllocator<TsanMapUnmapCallback> SecondaryAllocator;
 typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
     SecondaryAllocator> Allocator;
 Allocator *allocator();





More information about the llvm-commits mailing list