Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / lsan / TestCases / many_tls_keys_thread.cpp
blobf0deb444c86d4bdb58fb572a44e1b224b5fc4373
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
7 // Patch r303906 did not fix all the problems.
8 // UNSUPPORTED: arm-linux,armhf-linux
10 #include <assert.h>
11 #include <limits.h>
12 #include <pthread.h>
13 #include <stdlib.h>
14 #include <unistd.h>
16 static const int NUM_THREADS = 10;
18 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
19 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
20 int finished = 0;
22 __thread void *ptr1;
23 __thread void *ptr2;
24 __thread void *ptr3;
25 __thread void *ptr4;
26 __thread void *ptr5;
28 void alloc() {
29 ptr1 = malloc(1111);
30 ptr2 = malloc(2222);
31 ptr3 = malloc(3333);
32 ptr4 = malloc(4444);
33 ptr5 = malloc(5555);
36 void *thread_start(void *arg) {
37 alloc();
39 pthread_mutex_lock(&mutex);
40 finished++;
41 pthread_mutex_unlock(&mutex);
43 // don't exit, to intentionally leak tls data
44 while (1)
45 sleep(100);
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));
53 // spin until all threads have finished
54 while (finished < NUM_THREADS)
55 sleep(1);
56 exit(0);
59 // CHECK: LeakSanitizer: detected memory leaks
60 // CHECK: SUMMARY: {{.*}}Sanitizer: