1// RUN: %clangxx_asan -O0 %s -pthread -o %t && %run %t 2// RUN: %clangxx_asan -O2 %s -pthread -o %t && %run %t 3// REQUIRES: stable-runtime 4 5#include <assert.h> 6#include <pthread.h> 7#include <sanitizer/allocator_interface.h> 8#include <stdio.h> 9#include <stdlib.h> 10 11const size_t kLargeAlloc = 1UL << 20; 12 13void* allocate(void *arg) { 14 volatile void *ptr = malloc(kLargeAlloc); 15 free((void*)ptr); 16 return 0; 17} 18 19void* check_stats(void *arg) { 20 assert(__sanitizer_get_current_allocated_bytes() > 0); 21 return 0; 22} 23 24int main() { 25 size_t used_mem = __sanitizer_get_current_allocated_bytes(); 26 printf("Before: %zu\n", used_mem); 27 const int kNumIterations = 1000; 28 for (int iter = 0; iter < kNumIterations; iter++) { 29 pthread_t thr[4]; 30 for (int j = 0; j < 4; j++) { 31 assert(0 == 32 pthread_create(&thr[j], 0, (j < 2) ? allocate : check_stats, 0)); 33 } 34 for (int j = 0; j < 4; j++) 35 assert(0 == pthread_join(thr[j], 0)); 36 used_mem = __sanitizer_get_current_allocated_bytes(); 37 if (used_mem > kLargeAlloc) { 38 printf("After iteration %d: %zu\n", iter, used_mem); 39 return 1; 40 } 41 } 42 printf("Success after %d iterations\n", kNumIterations); 43 return 0; 44} 45