|
| 1 | +// RUN: %clang_scudo %s -o %t |
| 2 | +// RUN: SCUDO_OPTIONS="QuarantineSizeMb=0:ThreadLocalQuarantineSizeKb=0" %run %t 5 1000000 2>&1 |
| 3 | +// RUN: SCUDO_OPTIONS="QuarantineSizeMb=1:ThreadLocalQuarantineSizeKb=64" %run %t 5 1000000 2>&1 |
| 4 | + |
| 5 | +// Tests parallel allocations and deallocations of memory chunks from a number |
| 6 | +// of concurrent threads, with and without quarantine. |
| 7 | +// This test passes if everything executes properly without crashing. |
| 8 | + |
| 9 | +#include <assert.h> |
| 10 | +#include <pthread.h> |
| 11 | +#include <stdlib.h> |
| 12 | +#include <stdio.h> |
| 13 | + |
| 14 | +#include <sanitizer/allocator_interface.h> |
| 15 | + |
| 16 | +int num_threads; |
| 17 | +int total_num_alloc; |
| 18 | +const int kMaxNumThreads = 500; |
| 19 | +pthread_t tid[kMaxNumThreads]; |
| 20 | + |
| 21 | +pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
| 22 | +pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
| 23 | +bool go = false; |
| 24 | + |
| 25 | +void *thread_fun(void *arg) { |
| 26 | + pthread_mutex_lock(&mutex); |
| 27 | + while (!go) pthread_cond_wait(&cond, &mutex); |
| 28 | + pthread_mutex_unlock(&mutex); |
| 29 | + for (int i = 0; i < total_num_alloc / num_threads; i++) { |
| 30 | + void *p = malloc(10); |
| 31 | + __asm__ __volatile__("" : : "r"(p) : "memory"); |
| 32 | + free(p); |
| 33 | + } |
| 34 | + return 0; |
| 35 | +} |
| 36 | + |
| 37 | +int main(int argc, char** argv) { |
| 38 | + assert(argc == 3); |
| 39 | + num_threads = atoi(argv[1]); |
| 40 | + assert(num_threads > 0); |
| 41 | + assert(num_threads <= kMaxNumThreads); |
| 42 | + total_num_alloc = atoi(argv[2]); |
| 43 | + assert(total_num_alloc > 0); |
| 44 | + |
| 45 | + printf("%d threads, %d allocations in each\n", num_threads, |
| 46 | + total_num_alloc / num_threads); |
| 47 | + fprintf(stderr, "Heap size before: %zd\n", __sanitizer_get_heap_size()); |
| 48 | + fprintf(stderr, "Allocated bytes before: %zd\n", |
| 49 | + __sanitizer_get_current_allocated_bytes()); |
| 50 | + |
| 51 | + for (int i = 0; i < num_threads; i++) |
| 52 | + pthread_create(&tid[i], 0, thread_fun, 0); |
| 53 | + pthread_mutex_lock(&mutex); |
| 54 | + go = true; |
| 55 | + pthread_cond_broadcast(&cond); |
| 56 | + pthread_mutex_unlock(&mutex); |
| 57 | + for (int i = 0; i < num_threads; i++) |
| 58 | + pthread_join(tid[i], 0); |
| 59 | + |
| 60 | + fprintf(stderr, "Heap size after: %zd\n", __sanitizer_get_heap_size()); |
| 61 | + fprintf(stderr, "Allocated bytes after: %zd\n", |
| 62 | + __sanitizer_get_current_allocated_bytes()); |
| 63 | + |
| 64 | + return 0; |
| 65 | +} |
0 commit comments