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
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 <stdio.h>
12#include <stdlib.h>
13
14#include <sanitizer/allocator_interface.h>
15
16int num_threads;
17int total_num_alloc;
18const int kMaxNumThreads = 500;
19pthread_t tid[kMaxNumThreads];
20
21pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
22pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
23char go = 0;
24
25void *thread_fun(void *arg) {
26 pthread_mutex_lock(mutex: &mutex);
27 while (!go)
28 pthread_cond_wait(cond: &cond, mutex: &mutex);
29 pthread_mutex_unlock(mutex: &mutex);
30 for (int i = 0; i < total_num_alloc / num_threads; i++) {
31 void *p = malloc(size: 10);
32 __asm__ __volatile__(""
33 :
34 : "r"(p)
35 : "memory");
36 free(ptr: p);
37 }
38 return 0;
39}
40
41int main(int argc, char **argv) {
42 assert(argc == 3);
43 num_threads = atoi(nptr: argv[1]);
44 assert(num_threads > 0);
45 assert(num_threads <= kMaxNumThreads);
46 total_num_alloc = atoi(nptr: argv[2]);
47 assert(total_num_alloc > 0);
48
49 printf(format: "%d threads, %d allocations in each\n", num_threads,
50 total_num_alloc / num_threads);
51 fprintf(stderr, format: "Heap size before: %zd\n", __sanitizer_get_heap_size());
52 fprintf(stderr, format: "Allocated bytes before: %zd\n",
53 __sanitizer_get_current_allocated_bytes());
54
55 for (int i = 0; i < num_threads; i++)
56 pthread_create(newthread: &tid[i], attr: 0, start_routine: thread_fun, arg: 0);
57 pthread_mutex_lock(mutex: &mutex);
58 go = 1;
59 pthread_cond_broadcast(cond: &cond);
60 pthread_mutex_unlock(mutex: &mutex);
61 for (int i = 0; i < num_threads; i++)
62 pthread_join(th: tid[i], thread_return: 0);
63
64 fprintf(stderr, format: "Heap size after: %zd\n", __sanitizer_get_heap_size());
65 fprintf(stderr, format: "Allocated bytes after: %zd\n",
66 __sanitizer_get_current_allocated_bytes());
67
68 return 0;
69}
70

source code of compiler-rt/test/scudo/threads.c