Debian Codegraph @

compiler-rt/test/sanitizer_common/TestCases/Linux/tls_malloc_hook.c

raw
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
19 typedef long *(*get_t)();
20 get_t GetTls;
21 void *Thread(void *unused) { return GetTls(); }
22
23 __thread long recursive_hook;
24
25 // CHECK: __sanitizer_malloc_hook:
26 void __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, "__sanitizer_malloc_hook: %p\n", GetTls());
31 --recursive_hook;
32 }
33
34 int main(int argc, char *argv[]) {
35 char path[4096];
36 snprintf(path, sizeof(path), "%s-so.so", argv[0]);
37 int i;
38
39 void *handle = dlopen(path, RTLD_LAZY);
40 if (!handle)
41 fprintf(stderr, "%s\n", dlerror());
42 assert(handle != 0);
43 GetTls = (get_t)dlsym(handle, "GetTls");
44 assert(dlerror() == 0);
45
46 pthread_t t;
47 pthread_create(&t, 0, Thread, 0);
48 pthread_join(t, 0);
49 pthread_create(&t, 0, Thread, 0);
50 pthread_join(t, 0);
51 return 0;
52 }
53 #else // BUILD_SO
54 __thread long huge_thread_local_array[1 << 17];
55 long *GetTls() { return &huge_thread_local_array[0]; }
56 #endif