xref: /llvm-project/compiler-rt/test/lsan/TestCases/Linux/use_tls_pthread_specific_static.cpp (revision eb3be66028eac88098e0186cff80e0421949dd69)
197ccf6b8SFangrui Song // Test that statically 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 
797ccf6b8SFangrui Song #include <assert.h>
897ccf6b8SFangrui Song #include <pthread.h>
997ccf6b8SFangrui Song #include <stdio.h>
1097ccf6b8SFangrui Song #include <stdlib.h>
1197ccf6b8SFangrui Song #include "sanitizer_common/print_address.h"
1297ccf6b8SFangrui Song 
1397ccf6b8SFangrui Song // From glibc: this many keys are stored in the thread descriptor directly.
1497ccf6b8SFangrui Song const unsigned PTHREAD_KEY_2NDLEVEL_SIZE = 32;
1597ccf6b8SFangrui Song 
main()1697ccf6b8SFangrui Song int main() {
1797ccf6b8SFangrui Song   pthread_key_t key;
1897ccf6b8SFangrui Song   int res;
1997ccf6b8SFangrui Song   res = pthread_key_create(&key, NULL);
2097ccf6b8SFangrui Song   assert(res == 0);
213b3aef19SVy Nguyen #if !defined(__ANDROID__) && !defined(__BIONIC__)
223b3aef19SVy Nguyen   // Bionic doesn't have specific limit.
2397ccf6b8SFangrui Song   assert(key < PTHREAD_KEY_2NDLEVEL_SIZE);
243b3aef19SVy Nguyen #endif
2597ccf6b8SFangrui Song   void *p = malloc(1337);
2697ccf6b8SFangrui Song   res = pthread_setspecific(key, p);
2797ccf6b8SFangrui Song   assert(res == 0);
2897ccf6b8SFangrui Song   print_address("Test alloc: ", 1, p);
2997ccf6b8SFangrui Song   return 0;
3097ccf6b8SFangrui Song }
3197ccf6b8SFangrui Song // CHECK: Test alloc: [[ADDR:0x[0-9,a-f]+]]
3297ccf6b8SFangrui Song // CHECK: LeakSanitizer: detected memory leaks
3397ccf6b8SFangrui Song // CHECK: [[ADDR]] (1337 bytes)
34*eb3be660SKirill Stoimenov // CHECK: SUMMARY: {{.*}}Sanitizer:
35