• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: LSAN_BASE="report_objects=1:use_registers=0:use_stacks=0:use_globals=0"
7 // RUN: %clang_lsan %s -o %t
8 // RUN: LSAN_OPTIONS=$LSAN_BASE:use_tls=1 %run %t
9 // RUN: LSAN_OPTIONS=$LSAN_BASE:use_tls=0 not %run %t 2>&1 | FileCheck %s
10 
11 #include <assert.h>
12 #include <pthread.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 
16 #include "sanitizer/lsan_interface.h"
17 
18 pthread_key_t key;
19 __thread void *p;
20 
key_destructor(void * arg)21 void key_destructor(void *arg) {
22   // Generally this may happen on a different thread.
23   __lsan_do_leak_check();
24 }
25 
thread_func(void * arg)26 void *thread_func(void *arg) {
27   p = malloc(1337);
28   fprintf(stderr, "Test alloc: %p.\n", p);
29   int res = pthread_setspecific(key, (void*)1);
30   assert(res == 0);
31   return 0;
32 }
33 
main()34 int main() {
35   int res = pthread_key_create(&key, &key_destructor);
36   assert(res == 0);
37   pthread_t thread_id;
38   res = pthread_create(&thread_id, 0, thread_func, 0);
39   assert(res == 0);
40   res = pthread_join(thread_id, 0);
41   assert(res == 0);
42   return 0;
43 }
44 // CHECK: Test alloc: [[ADDR:.*]].
45 // CHECK: [[ADDR]] (1337 bytes)
46