49 lines · c
1// Regression test for thread lifetime tracking. Thread data should be2// considered live during the thread's termination, at least until the3// user-installed TSD destructors have finished running (since they may contain4// additional cleanup tasks). LSan doesn't actually meet that goal 100%, but it5// makes its best effort.6// RUN: %clang_lsan %s -o %t7// RUN: %env_lsan_opts="report_objects=1:use_registers=0:use_stacks=0:use_tls=1" %run %t8// RUN: %env_lsan_opts="report_objects=1:use_registers=0:use_stacks=0:use_tls=0" not %run %t 2>&1 | FileCheck %s9 10// Investigate why it does not fail with use_stack=011// UNSUPPORTED: arm-linux || armhf-linux12 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 21pthread_key_t key;22__thread void *p;23 24void key_destructor(void *arg) {25 // Generally this may happen on a different thread.26 __lsan_do_leak_check();27}28 29void *thread_func(void *arg) {30 p = malloc(1337);31 print_address("Test alloc: ", 1, p);32 int res = pthread_setspecific(key, (void*)1);33 assert(res == 0);34 return 0;35}36 37int main() {38 int res = pthread_key_create(&key, &key_destructor);39 assert(res == 0);40 pthread_t thread_id;41 res = pthread_create(&thread_id, 0, thread_func, 0);42 assert(res == 0);43 res = pthread_join(thread_id, 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