57 lines · c
1// Test that we don't crash accessing DTLS from malloc hook.2 3// RUN: %clang %s -o %t4// RUN: %clang %s -DBUILD_SO -fPIC -o %t-so.so -shared5// RUN: %run %t 2>&1 | FileCheck %s6 7// REQUIRES: glibc8 9// No allocator and hooks.10// XFAIL: ubsan11 12#ifndef BUILD_SO13# 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, "__sanitizer_malloc_hook: %p\n", GetTls());31 --recursive_hook;32}33 34int 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_SO54__thread long huge_thread_local_array[1 << 17];55long *GetTls() { return &huge_thread_local_array[0]; }56#endif57