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
14pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
15pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
16bool flag = false;
17
18void *func(void *arg) {
19 // This mutex will never be grabbed.
20 fprintf(stderr, format: "entered func()\n");
21 pthread_mutex_lock(mutex: &mutex);
22 free(ptr: arg);
23 pthread_mutex_unlock(mutex: &mutex);
24 return 0;
25}
26
27void create_detached_thread() {
28 pthread_t thread_id;
29 pthread_attr_t attr;
30
31 pthread_attr_init(attr: &attr);
32 pthread_attr_setdetachstate(attr: &attr, PTHREAD_CREATE_DETACHED);
33
34 void *arg = malloc(size: 1337);
35 assert(arg);
36 // This mutex is never unlocked by the main thread.
37 pthread_mutex_lock(mutex: &mutex);
38 int res = pthread_create(newthread: &thread_id, attr: &attr, start_routine: func, arg: arg);
39 assert(res == 0);
40}
41
42int main() {
43 create_detached_thread();
44}
45

source code of compiler-rt/test/lsan/TestCases/leak_check_before_thread_started.cpp