[C++20] [Modules] Fix may-be incorrect ADL for module local entities (#123931)
[llvm-project.git] / compiler-rt / test / sanitizer_common / TestCases / Linux / tls_malloc_hook.c
blob587f3b1401f1007d399ed935c00fabf137b5216a
1 // Test that we don't crash accessing DTLS from malloc hook.
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
7 // REQUIRES: glibc
9 // No allocator and hooks.
10 // XFAIL: ubsan
12 #ifndef BUILD_SO
13 # include <assert.h>
14 # include <dlfcn.h>
15 # include <pthread.h>
16 # include <stdio.h>
17 # include <stdlib.h>
19 typedef long *(*get_t)();
20 get_t GetTls;
21 void *Thread(void *unused) { return GetTls(); }
23 __thread long recursive_hook;
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;
34 int main(int argc, char *argv[]) {
35 char path[4096];
36 snprintf(path, sizeof(path), "%s-so.so", argv[0]);
37 int i;
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);
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;
53 #else // BUILD_SO
54 __thread long huge_thread_local_array[1 << 17];
55 long *GetTls() { return &huge_thread_local_array[0]; }
56 #endif