1 // RUN: %clangxx_asan -O0 %s -lpthread -o %t && %run %t
2 // RUN: %clangxx_asan -O2 %s -lpthread -o %t && %run %t
6 #include <sanitizer/asan_interface.h>
10 const size_t kLargeAlloc
= 1UL << 20;
12 void* allocate(void *arg
) {
13 volatile void *ptr
= malloc(kLargeAlloc
);
18 void* check_stats(void *arg
) {
19 assert(__asan_get_current_allocated_bytes() > 0);
24 size_t used_mem
= __asan_get_current_allocated_bytes();
25 printf("Before: %zu\n", used_mem
);
26 const int kNumIterations
= 1000;
27 for (int iter
= 0; iter
< kNumIterations
; iter
++) {
29 for (int j
= 0; j
< 4; j
++) {
31 pthread_create(&thr
[j
], 0, (j
< 2) ? allocate
: check_stats
, 0));
33 for (int j
= 0; j
< 4; j
++)
34 assert(0 == pthread_join(thr
[j
], 0));
35 used_mem
= __asan_get_current_allocated_bytes();
36 if (used_mem
> kLargeAlloc
) {
37 printf("After iteration %d: %zu\n", iter
, used_mem
);
41 printf("Success after %d iterations\n", kNumIterations
);