xref: /llvm-project/compiler-rt/test/lsan/TestCases/Linux/use_tls_pthread_specific_dynamic.cpp (revision b2aa0a465013aca2fc43ca729fdb714eb52150b3)
197ccf6b8SFangrui Song // Test that dynamically allocated thread-specific storage is included in the root set.
297ccf6b8SFangrui Song // RUN: %clangxx_lsan %s -o %t
3d08e5d4cSClemens Wasser // RUN: %env_lsan_opts="report_objects=1:use_stacks=0:use_registers=0:use_tls=0" not %run %t 2>&1 | FileCheck %s
4d08e5d4cSClemens Wasser // RUN: %env_lsan_opts="report_objects=1:use_stacks=0:use_registers=0:use_tls=1" %run %t 2>&1
597ccf6b8SFangrui Song // RUN: %env_lsan_opts="" %run %t 2>&1
697ccf6b8SFangrui Song 
7b9d3234cSAdhemerval Zanella // Investigate why it does not fail with use_tls=0
8b9d3234cSAdhemerval Zanella // UNSUPPORTED: arm-linux || armhf-linux
9b9d3234cSAdhemerval Zanella 
1097ccf6b8SFangrui Song #include <assert.h>
1197ccf6b8SFangrui Song #include <pthread.h>
1297ccf6b8SFangrui Song #include <stdio.h>
1397ccf6b8SFangrui Song #include <stdlib.h>
1497ccf6b8SFangrui Song #include "sanitizer_common/print_address.h"
1597ccf6b8SFangrui Song 
1697ccf6b8SFangrui Song // From glibc: this many keys are stored in the thread descriptor directly.
1797ccf6b8SFangrui Song const unsigned PTHREAD_KEY_2NDLEVEL_SIZE = 32;
1897ccf6b8SFangrui Song 
main()1997ccf6b8SFangrui Song int main() {
2097ccf6b8SFangrui Song   static const unsigned kDummyKeysCount = PTHREAD_KEY_2NDLEVEL_SIZE;
2197ccf6b8SFangrui Song   int res;
2297ccf6b8SFangrui Song   pthread_key_t dummy_keys[kDummyKeysCount];
2397ccf6b8SFangrui Song   for (unsigned i = 0; i < kDummyKeysCount; i++) {
2497ccf6b8SFangrui Song     res = pthread_key_create(&dummy_keys[i], NULL);
2597ccf6b8SFangrui Song     assert(res == 0);
2697ccf6b8SFangrui Song   }
2797ccf6b8SFangrui Song   pthread_key_t key;
2897ccf6b8SFangrui Song   res = pthread_key_create(&key, NULL);
2997ccf6b8SFangrui Song   assert(key >= PTHREAD_KEY_2NDLEVEL_SIZE);
3097ccf6b8SFangrui Song   assert(res == 0);
3197ccf6b8SFangrui Song   void *p  = malloc(1337);
3297ccf6b8SFangrui Song   res = pthread_setspecific(key, p);
3397ccf6b8SFangrui Song   assert(res == 0);
3497ccf6b8SFangrui Song   print_address("Test alloc: ", 1, p);
3597ccf6b8SFangrui Song   return 0;
3697ccf6b8SFangrui Song }
3797ccf6b8SFangrui Song // CHECK: Test alloc: [[ADDR:0x[0-9,a-f]+]]
3897ccf6b8SFangrui Song // CHECK: LeakSanitizer: detected memory leaks
3997ccf6b8SFangrui Song // CHECK: [[ADDR]] (1337 bytes)
40*49b081c8SKirill Stoimenov // CHECK: SUMMARY: {{.*}}Sanitizer:
41