From 7ad02fca09a8451b6c2c37ce6d1094f212f7720c Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Wed, 7 Jan 2015 02:37:52 +0000 Subject: [PATCH] [asan] add flag quarantine_size_mb, deprecate quarantine_size llvm-svn: 225337 --- compiler-rt/lib/asan/asan_allocator.cc | 4 ++-- compiler-rt/lib/asan/asan_flags.cc | 13 ++++++++++++ compiler-rt/lib/asan/asan_flags.inc | 6 ++++-- compiler-rt/lib/asan/asan_rtl.cc | 2 +- .../asan/TestCases/Linux/quarantine_size_mb.cc | 24 ++++++++++++++++++++++ .../Posix/large_allocator_unpoisons_on_free.cc | 2 +- .../test/asan/TestCases/Posix/tsd_dtor_leak.cc | 21 ++++++++----------- 7 files changed, 53 insertions(+), 19 deletions(-) create mode 100644 compiler-rt/test/asan/TestCases/Linux/quarantine_size_mb.cc diff --git a/compiler-rt/lib/asan/asan_allocator.cc b/compiler-rt/lib/asan/asan_allocator.cc index 9525cf5..fd63ac68 100644 --- a/compiler-rt/lib/asan/asan_allocator.cc +++ b/compiler-rt/lib/asan/asan_allocator.cc @@ -206,7 +206,7 @@ QuarantineCache *GetQuarantineCache(AsanThreadLocalMallocStorage *ms) { } void AllocatorOptions::SetFrom(const Flags *f, const CommonFlags *cf) { - quarantine_size_mb = f->quarantine_size >> 20; + quarantine_size_mb = f->quarantine_size_mb; min_redzone = f->redzone; max_redzone = f->max_redzone; may_return_null = cf->allocator_may_return_null; @@ -214,7 +214,7 @@ void AllocatorOptions::SetFrom(const Flags *f, const CommonFlags *cf) { } void AllocatorOptions::CopyTo(Flags *f, CommonFlags *cf) { - f->quarantine_size = (int)quarantine_size_mb << 20; + f->quarantine_size_mb = quarantine_size_mb; f->redzone = min_redzone; f->max_redzone = max_redzone; cf->allocator_may_return_null = may_return_null; diff --git a/compiler-rt/lib/asan/asan_flags.cc b/compiler-rt/lib/asan/asan_flags.cc index d6144a7..2c2d5c8 100644 --- a/compiler-rt/lib/asan/asan_flags.cc +++ b/compiler-rt/lib/asan/asan_flags.cc @@ -64,6 +64,7 @@ void InitializeFlags(Flags *f) { OverrideCommonFlags(cf); } + const int kDefaultQuarantineSizeMb = (ASAN_LOW_MEMORY) ? 1UL << 6 : 1UL << 8; f->SetDefaults(); // Override from compile definition. @@ -118,6 +119,18 @@ void InitializeFlags(Flags *f) { CHECK_LE(f->max_redzone, 2048); CHECK(IsPowerOfTwo(f->redzone)); CHECK(IsPowerOfTwo(f->max_redzone)); + + // quarantine_size is deprecated but we still honor it. + // quarantine_size can not be used together with quarantine_size_mb. + if (f->quarantine_size >= 0 && f->quarantine_size_mb >= 0) { + Report("%s: please use either 'quarantine_size' (deprecated) or " + "quarantine_size_mb, but not both\n", SanitizerToolName); + Die(); + } + if (f->quarantine_size >= 0) + f->quarantine_size_mb = f->quarantine_size >> 20; + if (f->quarantine_size_mb < 0) + f->quarantine_size_mb = kDefaultQuarantineSizeMb; } } // namespace __asan diff --git a/compiler-rt/lib/asan/asan_flags.inc b/compiler-rt/lib/asan/asan_flags.inc index 066b47a..ec9d419 100644 --- a/compiler-rt/lib/asan/asan_flags.inc +++ b/compiler-rt/lib/asan/asan_flags.inc @@ -17,8 +17,10 @@ // ASAN_FLAG(Type, Name, DefaultValue, Description) // See COMMON_FLAG in sanitizer_flags.inc for more details. -ASAN_FLAG(int, quarantine_size, (ASAN_LOW_MEMORY) ? 1UL << 26 : 1UL << 28, - "Size (in bytes) of quarantine used to detect use-after-free " +ASAN_FLAG(int, quarantine_size, -1, + "Deprecated, please use quarantine_size_mb.") +ASAN_FLAG(int, quarantine_size_mb, -1, + "Size (in Mb) of quarantine used to detect use-after-free " "errors. Lower value may reduce memory usage but increase the " "chance of false negatives.") ASAN_FLAG(int, redzone, 16, diff --git a/compiler-rt/lib/asan/asan_rtl.cc b/compiler-rt/lib/asan/asan_rtl.cc index 9d99488..db258e73 100644 --- a/compiler-rt/lib/asan/asan_rtl.cc +++ b/compiler-rt/lib/asan/asan_rtl.cc @@ -281,7 +281,7 @@ static void PrintAddressSpaceLayout() { Printf("\n"); Printf("redzone=%zu\n", (uptr)flags()->redzone); Printf("max_redzone=%zu\n", (uptr)flags()->max_redzone); - Printf("quarantine_size=%zuM\n", (uptr)flags()->quarantine_size >> 20); + Printf("quarantine_size_mb=%zuM\n", (uptr)flags()->quarantine_size_mb); Printf("malloc_context_size=%zu\n", (uptr)common_flags()->malloc_context_size); diff --git a/compiler-rt/test/asan/TestCases/Linux/quarantine_size_mb.cc b/compiler-rt/test/asan/TestCases/Linux/quarantine_size_mb.cc new file mode 100644 index 0000000..4499992 --- /dev/null +++ b/compiler-rt/test/asan/TestCases/Linux/quarantine_size_mb.cc @@ -0,0 +1,24 @@ +// Test quarantine_size_mb (and the deprecated quarantine_size) +// RUN: %clangxx_asan %s -o %t +// RUN: ASAN_OPTIONS=quarantine_size=10485760:verbosity=1:hard_rss_limit_mb=50 %run %t 2>&1 | FileCheck %s --check-prefix=Q10 +// RUN: ASAN_OPTIONS=quarantine_size_mb=10:verbosity=1:hard_rss_limit_mb=50 %run %t 2>&1 | FileCheck %s --check-prefix=Q10 +// RUN: ASAN_OPTIONS=quarantine_size_mb=10:quarantine_size=20:verbosity=1 not %run %t 2>&1 | FileCheck %s --check-prefix=BOTH +// RUN: ASAN_OPTIONS=quarantine_size_mb=1000:hard_rss_limit_mb=50 not %run %t 2>&1 | FileCheck %s --check-prefix=RSS_LIMIT +// RUN: ASAN_OPTIONS=hard_rss_limit_mb=50 not %run %t 2>&1 | FileCheck %s --check-prefix=RSS_LIMIT +#include +char *g; + +static const int kNumAllocs = 1 << 11; +static const int kAllocSize = 1 << 20; + +int main() { + for (int i = 0; i < kNumAllocs; i++) { + g = new char[kAllocSize]; + memset(g, -1, kAllocSize); + delete [] (g); + } +} + +// Q10: quarantine_size_mb=10M +// BOTH: please use either 'quarantine_size' (deprecated) or quarantine_size_mb, but not both +// RSS_LIMIT: AddressSanitizer: hard rss limit exhausted diff --git a/compiler-rt/test/asan/TestCases/Posix/large_allocator_unpoisons_on_free.cc b/compiler-rt/test/asan/TestCases/Posix/large_allocator_unpoisons_on_free.cc index 0a49980..f852a62 100644 --- a/compiler-rt/test/asan/TestCases/Posix/large_allocator_unpoisons_on_free.cc +++ b/compiler-rt/test/asan/TestCases/Posix/large_allocator_unpoisons_on_free.cc @@ -2,7 +2,7 @@ // RUN: %clangxx_asan %s -o %t // The memory is released only when the deallocated chunk leaves the quarantine, // otherwise the mmap(p, ...) call overwrites the malloc header. -// RUN: ASAN_OPTIONS=quarantine_size=1 %run %t +// RUN: ASAN_OPTIONS=quarantine_size_mb=0 %run %t #include #include diff --git a/compiler-rt/test/asan/TestCases/Posix/tsd_dtor_leak.cc b/compiler-rt/test/asan/TestCases/Posix/tsd_dtor_leak.cc index cdccf1a..6952245 100644 --- a/compiler-rt/test/asan/TestCases/Posix/tsd_dtor_leak.cc +++ b/compiler-rt/test/asan/TestCases/Posix/tsd_dtor_leak.cc @@ -1,7 +1,7 @@ // Regression test for a leak in tsd: // https://code.google.com/p/address-sanitizer/issues/detail?id=233 // RUN: %clangxx_asan -O1 %s -pthread -o %t -// RUN: ASAN_OPTIONS=quarantine_size=1 %run %t +// RUN: ASAN_OPTIONS=quarantine_size_mb=0 %run %t #include #include #include @@ -25,22 +25,17 @@ void Dtor(void *tsd) { int main() { assert(0 == pthread_key_create(&tsd_key, Dtor)); - size_t old_heap_size = 0; + pthread_t t; + for (int i = 0; i < 3; i++) { + pthread_create(&t, 0, Thread, 0); + pthread_join(t, 0); + } + size_t old_heap_size = __sanitizer_get_heap_size(); for (int i = 0; i < 10; i++) { - pthread_t t; pthread_create(&t, 0, Thread, 0); pthread_join(t, 0); size_t new_heap_size = __sanitizer_get_heap_size(); fprintf(stderr, "heap size: new: %zd old: %zd\n", new_heap_size, old_heap_size); - if (old_heap_size) - assert(old_heap_size == new_heap_size); -#if defined(__FreeBSD__) - // On FreeBSD SYSCALL(sched_yielld) allocates some more space during the - // 2nd attempt. - if (i > 0) - old_heap_size = new_heap_size; -#else - old_heap_size = new_heap_size; -#endif + assert(old_heap_size == new_heap_size); } } -- 2.7.4