1 // RUN: %clang_scudo %s -o %t
2 // RUN: %env_scudo_opts="QuarantineSizeKb=0:ThreadLocalQuarantineSizeKb=0" %run %t 5 1000000 2>&1
3 // RUN: %env_scudo_opts="QuarantineSizeKb=1024:ThreadLocalQuarantineSizeKb=64" %run %t 5 1000000 2>&1
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.
14 #include <sanitizer/allocator_interface.h>
18 const int kMaxNumThreads
= 500;
19 pthread_t tid
[kMaxNumThreads
];
21 pthread_cond_t cond
= PTHREAD_COND_INITIALIZER
;
22 pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
25 void *thread_fun(void *arg
) {
26 pthread_mutex_lock(&mutex
);
28 pthread_cond_wait(&cond
, &mutex
);
29 pthread_mutex_unlock(&mutex
);
30 for (int i
= 0; i
< total_num_alloc
/ num_threads
; i
++) {
32 __asm__
__volatile__(""
41 int main(int argc
, char **argv
) {
43 num_threads
= atoi(argv
[1]);
44 assert(num_threads
> 0);
45 assert(num_threads
<= kMaxNumThreads
);
46 total_num_alloc
= atoi(argv
[2]);
47 assert(total_num_alloc
> 0);
49 printf("%d threads, %d allocations in each\n", num_threads
,
50 total_num_alloc
/ num_threads
);
51 fprintf(stderr
, "Heap size before: %zd\n", __sanitizer_get_heap_size());
52 fprintf(stderr
, "Allocated bytes before: %zd\n",
53 __sanitizer_get_current_allocated_bytes());
55 for (int i
= 0; i
< num_threads
; i
++)
56 pthread_create(&tid
[i
], 0, thread_fun
, 0);
57 pthread_mutex_lock(&mutex
);
59 pthread_cond_broadcast(&cond
);
60 pthread_mutex_unlock(&mutex
);
61 for (int i
= 0; i
< num_threads
; i
++)
62 pthread_join(tid
[i
], 0);
64 fprintf(stderr
, "Heap size after: %zd\n", __sanitizer_get_heap_size());
65 fprintf(stderr
, "Allocated bytes after: %zd\n",
66 __sanitizer_get_current_allocated_bytes());