]> git.proxmox.com Git - rustc.git/blob - src/libcompiler_builtins/compiler-rt/test/lsan/TestCases/Linux/use_tls_dynamic.cc
New upstream version 1.25.0+dfsg1
[rustc.git] / src / libcompiler_builtins / compiler-rt / test / lsan / TestCases / Linux / use_tls_dynamic.cc
1 // Test that dynamically allocated TLS space is included in the root set.
2 // RUN: LSAN_BASE="report_objects=1:use_stacks=0:use_registers=0:use_ld_allocations=0"
3 // RUN: %clangxx %s -DBUILD_DSO -fPIC -shared -o %t-so.so
4 // RUN: %clangxx_lsan %s -o %t
5 // RUN: %env_lsan_opts=$LSAN_BASE:"use_tls=0" not %run %t 2>&1 | FileCheck %s
6 // RUN: %env_lsan_opts=$LSAN_BASE:"use_tls=1" %run %t 2>&1
7 // RUN: %env_lsan_opts="" %run %t 2>&1
8 // UNSUPPORTED: i386-linux,arm
9
10 #ifndef BUILD_DSO
11 #include <assert.h>
12 #include <dlfcn.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string>
16 #include "sanitizer_common/print_address.h"
17
18 int main(int argc, char *argv[]) {
19 std::string path = std::string(argv[0]) + "-so.so";
20
21 void *handle = dlopen(path.c_str(), RTLD_LAZY);
22 assert(handle != 0);
23 typedef void **(* store_t)(void *p);
24 store_t StoreToTLS = (store_t)dlsym(handle, "StoreToTLS");
25 assert(dlerror() == 0);
26
27 void *p = malloc(1337);
28 // If we don't know about dynamic TLS, we will return a false leak above.
29 void **p_in_tls = StoreToTLS(p);
30 assert(*p_in_tls == p);
31 print_address("Test alloc: ", 1, p);
32 return 0;
33 }
34 // CHECK: Test alloc: [[ADDR:0x[0-9,a-f]+]]
35 // CHECK: LeakSanitizer: detected memory leaks
36 // CHECK: [[ADDR]] (1337 bytes)
37 // CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer:
38
39 #else // BUILD_DSO
40 // A loadable module with a large thread local section, which would require
41 // allocation of a new TLS storage chunk when loaded with dlopen(). We use it
42 // to test the reachability of such chunks in LSan tests.
43
44 // This must be large enough that it doesn't fit into preallocated static TLS
45 // space (see STATIC_TLS_SURPLUS in glibc).
46 __thread void *huge_thread_local_array[(1 << 20) / sizeof(void *)]; // NOLINT
47
48 extern "C" void **StoreToTLS(void *p) {
49 huge_thread_local_array[0] = p;
50 return &huge_thread_local_array[0];
51 }
52 #endif // BUILD_DSO