1// Test thread_local_quarantine_size_kb
2
3// RUN: %clangxx_asan %s -o %t
4// RUN: %env_asan_opts=thread_local_quarantine_size_kb=64:quarantine_size_mb=64:verbosity=1 %run %t 2>&1 | \
5// RUN: FileCheck %s --allow-empty --check-prefix=CHECK-SMALL-LOCAL-CACHE-SMALL-OVERHEAD
6// RUN: %env_asan_opts=thread_local_quarantine_size_kb=0:quarantine_size_mb=0 %run %t 2>&1 | \
7// RUN: FileCheck %s --check-prefix=CHECK-QUARANTINE-DISABLED-SMALL-OVERHEAD
8// RUN: %env_asan_opts=thread_local_quarantine_size_kb=0:quarantine_size_mb=64 not %run %t 2>&1 | \
9// RUN: FileCheck %s --check-prefix=CHECK-FOR-PARAMETER-ERROR
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <sanitizer/allocator_interface.h>
15
16// The idea is allocate a lot of small blocks, totaling 5Mb of user memory,
17// and verify that quarantine does not incur too much memory overhead.
18// There's always an overhead for red zones, shadow memory and such, but
19// quarantine accounting should not significantly contribute to that.
20// The zero sized thread local cache is specifically tested since it used to
21// generate a huge overhead of almost empty quarantine batches.
22static const size_t kHeapSizeIncrementLimit = 12 << 20;
23static const int kNumAllocs = 20000;
24static const int kAllocSize = 256;
25
26int main() {
27 size_t heap_size = __sanitizer_get_heap_size();
28 fprintf(stderr, format: "Heap size: %zd\n", heap_size);
29
30 for (int i = 0; i < kNumAllocs; i++) {
31 char *temp = new char[kAllocSize];
32 memset(s: temp, c: -1, n: kAllocSize);
33 delete [] (temp);
34 }
35
36 size_t new_heap_size = __sanitizer_get_heap_size();
37 fprintf(stderr, format: "New heap size: %zd\n", new_heap_size);
38 if (new_heap_size - heap_size < kHeapSizeIncrementLimit)
39 fprintf(stderr, format: "Heap growth is within limits\n");
40}
41
42// CHECK-SMALL-LOCAL-CACHE-SMALL-OVERHEAD: thread_local_quarantine_size_kb=64K
43// CHECK-SMALL-LOCAL-CACHE-SMALL-OVERHEAD: Heap growth is within limits
44// CHECK-QUARANTINE-DISABLED-SMALL-OVERHEAD: Heap growth is within limits
45// CHECK-FOR-PARAMETER-ERROR: thread_local_quarantine_size_kb can be set to 0 only when quarantine_size_mb is set to 0
46

source code of compiler-rt/test/asan/TestCases/Linux/thread_local_quarantine_size_kb.cpp