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