blob: c49e433b1e8bfc64d053089be47e63a5a0f20885 [file] [log] [blame]
Stephen Hines6a211c52014-07-21 00:49:56 -07001// 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
Stephen Hines2d1fdb22014-05-28 23:58:16 -07004
5#include <assert.h>
6#include <pthread.h>
Stephen Hines6a211c52014-07-21 00:49:56 -07007#include <sanitizer/allocator_interface.h>
Stephen Hines2d1fdb22014-05-28 23:58:16 -07008#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) {
Stephen Hines6a211c52014-07-21 00:49:56 -070020 assert(__sanitizer_get_current_allocated_bytes() > 0);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070021 return 0;
22}
23
24int main() {
Stephen Hines6a211c52014-07-21 00:49:56 -070025 size_t used_mem = __sanitizer_get_current_allocated_bytes();
Stephen Hines2d1fdb22014-05-28 23:58:16 -070026 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));
Stephen Hines6a211c52014-07-21 00:49:56 -070036 used_mem = __sanitizer_get_current_allocated_bytes();
Stephen Hines2d1fdb22014-05-28 23:58:16 -070037 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}