| 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, 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 | |
| 27 | void 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 | pthread_attr_destroy(attr: &attr); |
| 41 | } |
| 42 | |
| 43 | int main() { |
| 44 | create_detached_thread(); |
| 45 | } |
| 46 |
