]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/lsan/TestCases/Linux/cleanup_in_tsd_destructor.c
Vendor import of compiler-rt trunk r351319 (just before the release_80
[FreeBSD/FreeBSD.git] / test / lsan / TestCases / Linux / cleanup_in_tsd_destructor.c
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: %env_lsan_opts=$LSAN_BASE:use_tls=1 %run %t
9 // RUN: %env_lsan_opts=$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 #include "sanitizer_common/print_address.h"
18
19 pthread_key_t key;
20 __thread void *p;
21
22 void key_destructor(void *arg) {
23   // Generally this may happen on a different thread.
24   __lsan_do_leak_check();
25 }
26
27 void *thread_func(void *arg) {
28   p = malloc(1337);
29   print_address("Test alloc: ", 1, p);
30   int res = pthread_setspecific(key, (void*)1);
31   assert(res == 0);
32   return 0;
33 }
34
35 int main() {
36   int res = pthread_key_create(&key, &key_destructor);
37   assert(res == 0);
38   pthread_t thread_id;
39   res = pthread_create(&thread_id, 0, thread_func, 0);
40   assert(res == 0);
41   res = pthread_join(thread_id, 0);
42   assert(res == 0);
43   return 0;
44 }
45 // CHECK: Test alloc: [[ADDR:0x[0-9,a-f]+]]
46 // CHECK: [[ADDR]] (1337 bytes)