1 | // Test that lsan handles tls correctly for many threads |
---|---|
2 | // RUN: %clangxx_lsan %s -o %t |
3 | // RUN: %env_lsan_opts="report_objects=1:use_stacks=0:use_registers=0:use_tls=0" not %run %t 2>&1 | FileCheck %s |
4 | // RUN: %env_lsan_opts="report_objects=1:use_stacks=0:use_registers=0:use_tls=1" %run %t 2>&1 |
5 | // RUN: %env_lsan_opts="" %run %t 2>&1 |
6 | |
7 | // Patch r303906 did not fix all the problems. |
8 | // UNSUPPORTED: arm-linux,armhf-linux |
9 | |
10 | #include <assert.h> |
11 | #include <limits.h> |
12 | #include <pthread.h> |
13 | #include <stdlib.h> |
14 | #include <unistd.h> |
15 | |
16 | static const int NUM_THREADS = 10; |
17 | |
18 | pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
19 | pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
20 | int finished = 0; |
21 | |
22 | __thread void *ptr1; |
23 | __thread void *ptr2; |
24 | __thread void *ptr3; |
25 | __thread void *ptr4; |
26 | __thread void *ptr5; |
27 | |
28 | void alloc() { |
29 | ptr1 = malloc(size: 1111); |
30 | ptr2 = malloc(size: 2222); |
31 | ptr3 = malloc(size: 3333); |
32 | ptr4 = malloc(size: 4444); |
33 | ptr5 = malloc(size: 5555); |
34 | } |
35 | |
36 | void *thread_start(void *arg) { |
37 | alloc(); |
38 | |
39 | pthread_mutex_lock(mutex: &mutex); |
40 | finished++; |
41 | pthread_mutex_unlock(mutex: &mutex); |
42 | |
43 | // don't exit, to intentionally leak tls data |
44 | while (1) |
45 | sleep(seconds: 100); |
46 | } |
47 | |
48 | int main() { |
49 | pthread_t thread[NUM_THREADS]; |
50 | for (int i = 0; i < NUM_THREADS; ++i) { |
51 | assert(0 == pthread_create(&thread[i], 0, thread_start, 0)); |
52 | } |
53 | // spin until all threads have finished |
54 | while (finished < NUM_THREADS) |
55 | sleep(seconds: 1); |
56 | exit(status: 0); |
57 | } |
58 | |
59 | // CHECK: LeakSanitizer: detected memory leaks |
60 | // CHECK: SUMMARY: {{.*}}Sanitizer: |
61 |