1// Test that we don't crash accessing DTLS from malloc hook.
2
3// RUN: %clang %s -o %t
4// RUN: %clang %s -DBUILD_SO -fPIC -o %t-so.so -shared
5// RUN: %run %t 2>&1 | FileCheck %s
6
7// REQUIRES: glibc
8
9// No allocator and hooks.
10// XFAIL: ubsan
11
12#ifndef BUILD_SO
13# include <assert.h>
14# include <dlfcn.h>
15# include <pthread.h>
16# include <stdio.h>
17# include <stdlib.h>
18
19typedef long *(*get_t)();
20get_t GetTls;
21void *Thread(void *unused) { return GetTls(); }
22
23__thread long recursive_hook;
24
25// CHECK: __sanitizer_malloc_hook:
26void __sanitizer_malloc_hook(const volatile void *ptr, size_t sz)
27 __attribute__((disable_sanitizer_instrumentation)) {
28 ++recursive_hook;
29 if (recursive_hook == 1 && GetTls)
30 fprintf(stderr, format: "__sanitizer_malloc_hook: %p\n", GetTls());
31 --recursive_hook;
32}
33
34int main(int argc, char *argv[]) {
35 char path[4096];
36 snprintf(s: path, maxlen: sizeof(path), format: "%s-so.so", argv[0]);
37 int i;
38
39 void *handle = dlopen(file: path, RTLD_LAZY);
40 if (!handle)
41 fprintf(stderr, format: "%s\n", dlerror());
42 assert(handle != 0);
43 GetTls = (get_t)dlsym(handle: handle, name: "GetTls");
44 assert(dlerror() == 0);
45
46 pthread_t t;
47 pthread_create(newthread: &t, attr: 0, start_routine: Thread, arg: 0);
48 pthread_join(th: t, thread_return: 0);
49 pthread_create(newthread: &t, attr: 0, start_routine: Thread, arg: 0);
50 pthread_join(th: t, thread_return: 0);
51 return 0;
52}
53#else // BUILD_SO
54__thread long huge_thread_local_array[1 << 17];
55long *GetTls() { return &huge_thread_local_array[0]; }
56#endif
57

source code of compiler-rt/test/sanitizer_common/TestCases/Linux/tls_malloc_hook.c