|
| 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 | +// FIXME: Crashes on CHECK. |
| 13 | +// XFAIL: asan && !i386-linux |
| 14 | +// XFAIL: msan && !i386-linux |
| 15 | + |
| 16 | +#ifndef BUILD_SO |
| 17 | +# include <assert.h> |
| 18 | +# include <dlfcn.h> |
| 19 | +# include <pthread.h> |
| 20 | +# include <stdio.h> |
| 21 | +# include <stdlib.h> |
| 22 | + |
| 23 | +typedef long *(*get_t)(); |
| 24 | +get_t GetTls; |
| 25 | +void *Thread(void *unused) { return GetTls(); } |
| 26 | + |
| 27 | +__thread long recursive_hook; |
| 28 | + |
| 29 | +// CHECK: __sanitizer_malloc_hook: |
| 30 | +void __sanitizer_malloc_hook(const volatile void *ptr, size_t sz) |
| 31 | + __attribute__((disable_sanitizer_instrumentation)) { |
| 32 | + ++recursive_hook; |
| 33 | + if (recursive_hook == 1 && GetTls) |
| 34 | + fprintf(stderr, "__sanitizer_malloc_hook: %p\n", GetTls()); |
| 35 | + --recursive_hook; |
| 36 | +} |
| 37 | + |
| 38 | +int main(int argc, char *argv[]) { |
| 39 | + char path[4096]; |
| 40 | + snprintf(path, sizeof(path), "%s-so.so", argv[0]); |
| 41 | + int i; |
| 42 | + |
| 43 | + void *handle = dlopen(path, RTLD_LAZY); |
| 44 | + if (!handle) |
| 45 | + fprintf(stderr, "%s\n", dlerror()); |
| 46 | + assert(handle != 0); |
| 47 | + GetTls = (get_t)dlsym(handle, "GetTls"); |
| 48 | + assert(dlerror() == 0); |
| 49 | + |
| 50 | + pthread_t t; |
| 51 | + pthread_create(&t, 0, Thread, 0); |
| 52 | + pthread_join(t, 0); |
| 53 | + pthread_create(&t, 0, Thread, 0); |
| 54 | + pthread_join(t, 0); |
| 55 | + return 0; |
| 56 | +} |
| 57 | +#else // BUILD_SO |
| 58 | +__thread long huge_thread_local_array[1 << 17]; |
| 59 | +long *GetTls() { return &huge_thread_local_array[0]; } |
| 60 | +#endif |
0 commit comments