1 // Test that dynamically allocated TLS space is included in the root set. 2 3 // This is known to be broken with glibc-2.27+ but it should pass with Bionic 4 // https://bugs.llvm.org/show_bug.cgi?id=37804 5 // XFAIL: glibc-2.27 6 7 // RUN: %clangxx %s -DBUILD_DSO -fPIC -shared -o %t-so.so 8 // RUN: %clangxx_lsan %s -o %t 9 // RUN: %env_lsan_opts="report_objects=1:use_stacks=0:use_registers=0:use_ld_allocations=0:use_tls=0" not %run %t 2>&1 | FileCheck %s 10 // RUN: %env_lsan_opts="report_objects=1:use_stacks=0:use_registers=0:use_ld_allocations=0:use_tls=1" %run %t 2>&1 11 // RUN: %env_lsan_opts="" %run %t 2>&1 12 // UNSUPPORTED: arm,powerpc,i386-linux && !android 13 14 #ifndef BUILD_DSO 15 #include <assert.h> 16 #include <dlfcn.h> 17 #include <stdio.h> 18 #include <stdlib.h> 19 #include <string> 20 #include "sanitizer_common/print_address.h" 21 22 int main(int argc, char *argv[]) { 23 std::string path = std::string(argv[0]) + "-so.so"; 24 25 // Clear any previous errors. On Android, the dynamic loader can have some 26 // left over dlerror() messages due to a missing symbol resolution for a 27 // deprecated malloc function. 28 dlerror(); 29 30 void *handle = dlopen(path.c_str(), RTLD_LAZY); 31 assert(handle != 0); 32 typedef void **(* store_t)(void *p); 33 store_t StoreToTLS = (store_t)dlsym(handle, "StoreToTLS"); 34 35 // Sometimes dlerror() occurs when we broke the interceptors. 36 // Add the message here to make the error more obvious. 37 const char *dlerror_msg = dlerror(); 38 if (dlerror_msg != nullptr) { 39 fprintf(stderr, "DLERROR: %s\n", dlerror_msg); 40 fflush(stderr); 41 abort(); 42 } 43 void *p = malloc(1337); 44 // If we don't know about dynamic TLS, we will return a false leak above. 45 void **p_in_tls = StoreToTLS(p); 46 assert(*p_in_tls == p); 47 print_address("Test alloc: ", 1, p); 48 return 0; 49 } 50 // CHECK: Test alloc: [[ADDR:0x[0-9,a-f]+]] 51 // CHECK: LeakSanitizer: detected memory leaks 52 // CHECK: [[ADDR]] (1337 bytes) 53 // CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer: 54 55 #else // BUILD_DSO 56 // A loadable module with a large thread local section, which would require 57 // allocation of a new TLS storage chunk when loaded with dlopen(). We use it 58 // to test the reachability of such chunks in LSan tests. 59 60 // This must be large enough that it doesn't fit into preallocated static TLS 61 // space (see STATIC_TLS_SURPLUS in glibc). 62 __thread void *huge_thread_local_array[(1 << 20) / sizeof(void *)]; 63 64 extern "C" void **StoreToTLS(void *p) { 65 huge_thread_local_array[0] = p; 66 return &huge_thread_local_array[0]; 67 } 68 #endif // BUILD_DSO 69