Move ASan lit-tests under test/asan
[blocksruntime.git] / test / asan / TestCases / current_allocated_bytes.cc
blob669cf150bdfbb274e9a9a230da86c2e310fd2207
1 // RUN: %clangxx_asan -O0 %s -o %t && %t
2 // RUN: %clangxx_asan -O2 %s -o %t && %t
4 #include <assert.h>
5 #include <pthread.h>
6 #include <sanitizer/asan_interface.h>
7 #include <stdio.h>
8 #include <stdlib.h>
10 const size_t kLargeAlloc = 1UL << 20;
12 void* allocate(void *arg) {
13 volatile void *ptr = malloc(kLargeAlloc);
14 free((void*)ptr);
15 return 0;
18 void* check_stats(void *arg) {
19 assert(__asan_get_current_allocated_bytes() > 0);
20 return 0;
23 int main() {
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++) {
28 pthread_t thr[4];
29 for (int j = 0; j < 4; j++) {
30 assert(0 ==
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);
38 return 1;
41 printf("Success after %d iterations\n", kNumIterations);
42 return 0;