xref: /llvm-project/compiler-rt/test/lsan/TestCases/leak_check_before_thread_started.cpp (revision a79098bc726e8de85d3ed0050de5395015bca031)
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: %env_lsan_opts="log_pointers=1:log_threads=1" %run %t
5 
6 // Fixme: remove once test passes with hwasan
7 // UNSUPPORTED: hwasan
8 
9 #include <assert.h>
10 #include <pthread.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 
14 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
15 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
16 bool flag = false;
17 
18 void *func(void *arg) {
19   // This mutex will never be grabbed.
20   fprintf(stderr, "entered func()\n");
21   pthread_mutex_lock(&mutex);
22   free(arg);
23   pthread_mutex_unlock(&mutex);
24   return 0;
25 }
26 
27 void create_detached_thread() {
28   pthread_t thread_id;
29   pthread_attr_t attr;
30 
31   pthread_attr_init(&attr);
32   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
33 
34   void *arg = malloc(1337);
35   assert(arg);
36   // This mutex is never unlocked by the main thread.
37   pthread_mutex_lock(&mutex);
38   int res = pthread_create(&thread_id, &attr, func, arg);
39   assert(res == 0);
40   pthread_attr_destroy(&attr);
41 }
42 
43 int main() {
44   create_detached_thread();
45 }
46