Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / asan / TestCases / Linux / pthread_create_from_constructor.cpp
blob8f9b0b4fed1a5d421c46f25df19e8e2e4f22147c
1 // Test that ASan doesn't deadlock in __interceptor_pthread_create called
2 // from dlopened shared library constructor. The deadlock happens only in shared
3 // ASan runtime with recent Glibc (2.23 fits) when __interceptor_pthread_create
4 // grabs global Glibc's GL(dl_load_lock) and waits for tls_get_addr_tail that
5 // also tries to acquire it.
6 //
7 // RUN: %clangxx_asan -DBUILD_SO=1 -fPIC -shared %s -o %t-so.so
8 // RUN: %clangxx_asan %s -o %t
9 // RUN: %run %t 2>&1
11 // dlopen() can not be intercepted on Android
12 // UNSUPPORTED: android
13 // REQUIRES: x86-target-arch
15 #ifdef BUILD_SO
17 #include <stdio.h>
18 #include <pthread.h>
19 #include <unistd.h>
21 void *threadFn(void *) {
22 fprintf(stderr, "thread started\n");
23 while (true) {
24 usleep(100000);
26 return 0;
29 void __attribute__((constructor)) startPolling() {
30 fprintf(stderr, "initializing library\n");
31 pthread_t t;
32 pthread_create(&t, 0, &threadFn, 0);
33 fprintf(stderr, "done\n");
36 #else
38 #include <dlfcn.h>
39 #include <stdlib.h>
40 #include <string>
42 int main(int argc, char **argv) {
43 std::string path = std::string(argv[0]) + "-so.so";
44 void *handle = dlopen(path.c_str(), RTLD_LAZY);
45 if (!handle)
46 abort();
47 return 0;
49 #endif