Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 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 |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 4 | |
| 5 | #include <assert.h> |
| 6 | #include <pthread.h> |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 7 | #include <sanitizer/allocator_interface.h> |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | |
| 11 | const size_t kLargeAlloc = 1UL << 20; |
| 12 | |
| 13 | void* allocate(void *arg) { |
| 14 | volatile void *ptr = malloc(kLargeAlloc); |
| 15 | free((void*)ptr); |
| 16 | return 0; |
| 17 | } |
| 18 | |
| 19 | void* check_stats(void *arg) { |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 20 | assert(__sanitizer_get_current_allocated_bytes() > 0); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 21 | return 0; |
| 22 | } |
| 23 | |
| 24 | int main() { |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 25 | size_t used_mem = __sanitizer_get_current_allocated_bytes(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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)); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 36 | used_mem = __sanitizer_get_current_allocated_bytes(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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 | } |