| 1 | // Regression test for thread lifetime tracking. Thread data should be |
| 2 | // considered live during the thread's termination, at least until the |
| 3 | // user-installed TSD destructors have finished running (since they may contain |
| 4 | // additional cleanup tasks). LSan doesn't actually meet that goal 100%, but it |
| 5 | // makes its best effort. |
| 6 | // RUN: %clang_lsan %s -o %t |
| 7 | // RUN: %env_lsan_opts="report_objects=1:use_registers=0:use_stacks=0:use_tls=1" %run %t |
| 8 | // RUN: %env_lsan_opts="report_objects=1:use_registers=0:use_stacks=0:use_tls=0" not %run %t 2>&1 | FileCheck %s |
| 9 | |
| 10 | // Investigate why it does not fail with use_stack=0 |
| 11 | // UNSUPPORTED: arm-linux || armhf-linux |
| 12 | |
| 13 | #include <assert.h> |
| 14 | #include <pthread.h> |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | |
| 18 | #include "sanitizer/lsan_interface.h" |
| 19 | #include "sanitizer_common/print_address.h" |
| 20 | |
| 21 | pthread_key_t key; |
| 22 | __thread void *p; |
| 23 | |
| 24 | void key_destructor(void *arg) { |
| 25 | // Generally this may happen on a different thread. |
| 26 | __lsan_do_leak_check(); |
| 27 | } |
| 28 | |
| 29 | void *thread_func(void *arg) { |
| 30 | p = malloc(size: 1337); |
| 31 | print_address("Test alloc: " , 1, p); |
| 32 | int res = pthread_setspecific(key: key, pointer: (void*)1); |
| 33 | assert(res == 0); |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | int main() { |
| 38 | int res = pthread_key_create(key: &key, destr_function: &key_destructor); |
| 39 | assert(res == 0); |
| 40 | pthread_t thread_id; |
| 41 | res = pthread_create(newthread: &thread_id, attr: 0, start_routine: thread_func, arg: 0); |
| 42 | assert(res == 0); |
| 43 | res = pthread_join(th: thread_id, thread_return: 0); |
| 44 | assert(res == 0); |
| 45 | return 0; |
| 46 | } |
| 47 | // CHECK: Test alloc: [[ADDR:0x[0-9,a-f]+]] |
| 48 | // CHECK: [[ADDR]] (1337 bytes) |
| 49 | |