Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / tsan / dl_iterate_phdr.cpp
blob4a1fcc21c9cc8868b0eab0dc5fa20ed398e9a74b
1 // RUN: %clangxx_tsan -O1 %s -DBUILD_SO -fPIC -shared -o %t-so.so
2 // RUN: %clangxx_tsan -O1 %s %link_libcxx_tsan -o %t && %run %t 2>&1 | FileCheck %s
4 // dl_iterate_phdr doesn't exist on OS X.
5 // UNSUPPORTED: darwin
7 #ifdef BUILD_SO
9 #include "test.h"
11 int exported_var = 0;
13 #else // BUILD_SO
15 #include "test.h"
16 #include <dlfcn.h>
17 #include <link.h>
18 #include <string.h>
19 #include <string>
21 static int callback(struct dl_phdr_info *info, size_t size, void *data) {
22 if (info->dlpi_name[0] == '\0')
23 info->dlpi_name = "/proc/self/exe";
24 return !strcmp(info->dlpi_name, "non existent module");
27 void *thread(void *unused) {
28 for (int i = 0; i < 1000; i++) {
29 barrier_wait(&barrier);
30 dl_iterate_phdr(callback, 0);
32 return 0;
35 int main(int argc, char *argv[]) {
36 barrier_init(&barrier, 2);
37 std::string path = std::string(argv[0]) + std::string("-so.so");
38 pthread_t th;
39 pthread_create(&th, 0, thread, 0);
40 for (int i = 0; i < 1000; i++) {
41 barrier_wait(&barrier);
42 void *lib = dlopen(path.c_str(), RTLD_NOW);
43 if (!lib) {
44 printf("error in dlopen: %s\n", dlerror());
45 return 1;
47 dlclose(lib);
49 pthread_join(th, 0);
50 fprintf(stderr, "DONE\n");
51 return 0;
54 #endif // BUILD_SO
56 // CHECK-NOT: WARNING: ThreadSanitizer: data race
57 // CHECK: DONE