197ccf6b8SFangrui Song // Test that dynamically allocated TLS space is included in the root set.
297ccf6b8SFangrui Song
33b3aef19SVy Nguyen // This is known to be broken with glibc-2.27+ but it should pass with Bionic
497ccf6b8SFangrui Song // https://bugs.llvm.org/show_bug.cgi?id=37804
597ccf6b8SFangrui Song // XFAIL: glibc-2.27
697ccf6b8SFangrui Song
797ccf6b8SFangrui Song // RUN: %clangxx %s -DBUILD_DSO -fPIC -shared -o %t-so.so
897ccf6b8SFangrui Song // RUN: %clangxx_lsan %s -o %t
9d08e5d4cSClemens Wasser // 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
10d08e5d4cSClemens Wasser // RUN: %env_lsan_opts="report_objects=1:use_stacks=0:use_registers=0:use_ld_allocations=0:use_tls=1" %run %t 2>&1
1197ccf6b8SFangrui Song // RUN: %env_lsan_opts="" %run %t 2>&1
120773a5cbSPaul Robinson // UNSUPPORTED: target={{(arm|powerpc).*}},i386-linux && !android
1397ccf6b8SFangrui Song
1497ccf6b8SFangrui Song #ifndef BUILD_DSO
1597ccf6b8SFangrui Song #include <assert.h>
1697ccf6b8SFangrui Song #include <dlfcn.h>
1797ccf6b8SFangrui Song #include <stdio.h>
1897ccf6b8SFangrui Song #include <stdlib.h>
1997ccf6b8SFangrui Song #include <string>
2097ccf6b8SFangrui Song #include "sanitizer_common/print_address.h"
2197ccf6b8SFangrui Song
main(int argc,char * argv[])2297ccf6b8SFangrui Song int main(int argc, char *argv[]) {
2397ccf6b8SFangrui Song std::string path = std::string(argv[0]) + "-so.so";
2497ccf6b8SFangrui Song
251f8031cdSMitch Phillips // Clear any previous errors. On Android, the dynamic loader can have some
261f8031cdSMitch Phillips // left over dlerror() messages due to a missing symbol resolution for a
271f8031cdSMitch Phillips // deprecated malloc function.
281f8031cdSMitch Phillips dlerror();
291f8031cdSMitch Phillips
3097ccf6b8SFangrui Song void *handle = dlopen(path.c_str(), RTLD_LAZY);
3197ccf6b8SFangrui Song assert(handle != 0);
3297ccf6b8SFangrui Song typedef void **(* store_t)(void *p);
3397ccf6b8SFangrui Song store_t StoreToTLS = (store_t)dlsym(handle, "StoreToTLS");
3497ccf6b8SFangrui Song
353b3aef19SVy Nguyen // Sometimes dlerror() occurs when we broke the interceptors.
363b3aef19SVy Nguyen // Add the message here to make the error more obvious.
373b3aef19SVy Nguyen const char *dlerror_msg = dlerror();
383b3aef19SVy Nguyen if (dlerror_msg != nullptr) {
393b3aef19SVy Nguyen fprintf(stderr, "DLERROR: %s\n", dlerror_msg);
403b3aef19SVy Nguyen fflush(stderr);
411f8031cdSMitch Phillips abort();
423b3aef19SVy Nguyen }
4397ccf6b8SFangrui Song void *p = malloc(1337);
4497ccf6b8SFangrui Song // If we don't know about dynamic TLS, we will return a false leak above.
4597ccf6b8SFangrui Song void **p_in_tls = StoreToTLS(p);
4697ccf6b8SFangrui Song assert(*p_in_tls == p);
4797ccf6b8SFangrui Song print_address("Test alloc: ", 1, p);
4897ccf6b8SFangrui Song return 0;
4997ccf6b8SFangrui Song }
5097ccf6b8SFangrui Song // CHECK: Test alloc: [[ADDR:0x[0-9,a-f]+]]
5197ccf6b8SFangrui Song // CHECK: LeakSanitizer: detected memory leaks
5297ccf6b8SFangrui Song // CHECK: [[ADDR]] (1337 bytes)
53*49b081c8SKirill Stoimenov // CHECK: SUMMARY: {{.*}}Sanitizer:
5497ccf6b8SFangrui Song
5597ccf6b8SFangrui Song #else // BUILD_DSO
5697ccf6b8SFangrui Song // A loadable module with a large thread local section, which would require
5797ccf6b8SFangrui Song // allocation of a new TLS storage chunk when loaded with dlopen(). We use it
5897ccf6b8SFangrui Song // to test the reachability of such chunks in LSan tests.
5997ccf6b8SFangrui Song
6097ccf6b8SFangrui Song // This must be large enough that it doesn't fit into preallocated static TLS
6197ccf6b8SFangrui Song // space (see STATIC_TLS_SURPLUS in glibc).
62c0fa6322SVitaly Buka __thread void *huge_thread_local_array[(1 << 20) / sizeof(void *)];
6397ccf6b8SFangrui Song
StoreToTLS(void * p)6497ccf6b8SFangrui Song extern "C" void **StoreToTLS(void *p) {
6597ccf6b8SFangrui Song huge_thread_local_array[0] = p;
6697ccf6b8SFangrui Song return &huge_thread_local_array[0];
6797ccf6b8SFangrui Song }
6897ccf6b8SFangrui Song #endif // BUILD_DSO
69