1 // Regression test for http://llvm.org/bugs/show_bug.cgi?id=21621
2 // This test relies on timing between threads, so any failures will be flaky.
3 // RUN: %clangxx_lsan %s -o %t
4 // RUN: LSAN_OPTIONS="log_pointers=1:log_threads=1" %run %t
5 #include <assert.h>
6 #include <pthread.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 
10 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
11 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
12 bool flag = false;
13 
func(void * arg)14 void *func(void *arg) {
15   // This mutex will never be grabbed.
16   fprintf(stderr, "entered func()\n");
17   pthread_mutex_lock(&mutex);
18   free(arg);
19   pthread_mutex_unlock(&mutex);
20   return 0;
21 }
22 
create_detached_thread()23 void create_detached_thread() {
24   pthread_t thread_id;
25   pthread_attr_t attr;
26 
27   pthread_attr_init(&attr);
28   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
29 
30   void *arg = malloc(1337);
31   assert(arg);
32   // This mutex is never unlocked by the main thread.
33   pthread_mutex_lock(&mutex);
34   int res = pthread_create(&thread_id, &attr, func, arg);
35   assert(res == 0);
36 }
37 
main()38 int main() {
39   create_detached_thread();
40 }
41