xref: /llvm-project/compiler-rt/test/lsan/TestCases/Linux/disabler_in_tsd_destructor.c (revision c9258ab7f25636be24934d8727b74d9487365efe)
1 // Regression test. Disabler should not depend on TSD validity.
2 // RUN: %clang_lsan %s -o %t
3 // RUN: %env_lsan_opts="report_objects=1:use_registers=0:use_stacks=0:use_tls=1:use_ld_allocations=0" %run %t
4 
5 #include <assert.h>
6 #include <pthread.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 
10 #include "sanitizer/lsan_interface.h"
11 
12 pthread_key_t key;
13 
key_destructor(void * arg)14 void key_destructor(void *arg) {
15   __lsan_disable();
16   void *p = malloc(1337);
17   // Break optimization.
18   fprintf(stderr, "Test alloc: %p.\n", p);
19   pthread_setspecific(key, 0);
20   __lsan_enable();
21 }
22 
thread_func(void * arg)23 void *thread_func(void *arg) {
24   int res = pthread_setspecific(key, (void*)1);
25   assert(res == 0);
26   return 0;
27 }
28 
main()29 int main() {
30   int res = pthread_key_create(&key, &key_destructor);
31   assert(res == 0);
32   pthread_t thread_id;
33   res = pthread_create(&thread_id, 0, thread_func, 0);
34   assert(res == 0);
35   res = pthread_join(thread_id, 0);
36   assert(res == 0);
37   return 0;
38 }
39