]> git.proxmox.com Git - rustc.git/blob - src/compiler-rt/test/asan/TestCases/init-order-dlopen.cc
Imported Upstream version 1.5.0+dfsg1
[rustc.git] / src / compiler-rt / test / asan / TestCases / init-order-dlopen.cc
1 // Regression test for
2 // https://code.google.com/p/address-sanitizer/issues/detail?id=178
3
4 // Assume we're on Darwin and try to pass -U to the linker. If this flag is
5 // unsupported, don't use it.
6 // RUN: %clangxx_asan -O0 %p/SharedLibs/init-order-dlopen-so.cc \
7 // RUN: -fPIC -shared -o %t-so.so -Wl,-U,_inc_global || \
8 // RUN: %clangxx_asan -O0 %p/SharedLibs/init-order-dlopen-so.cc \
9 // RUN: -fPIC -shared -o %t-so.so
10 // If the linker doesn't support --export-dynamic (which is ELF-specific),
11 // try to link without that option.
12 // FIXME: find a better solution.
13 // RUN: %clangxx_asan -O0 %s -lpthread -ldl -o %t -Wl,--export-dynamic || \
14 // RUN: %clangxx_asan -O0 %s -lpthread -ldl -o %t
15 // RUN: ASAN_OPTIONS=strict_init_order=true %run %t 2>&1 | FileCheck %s
16 #include <dlfcn.h>
17 #include <pthread.h>
18 #include <stdio.h>
19 #include <unistd.h>
20
21 #include <string>
22
23 using std::string;
24
25 int foo() {
26 return 42;
27 }
28 int global = foo();
29
30 __attribute__((visibility("default")))
31 extern "C"
32 void inc_global() {
33 global++;
34 }
35
36 void *global_poller(void *arg) {
37 while (true) {
38 if (global != 42)
39 break;
40 usleep(100);
41 }
42 return 0;
43 }
44
45 int main(int argc, char *argv[]) {
46 pthread_t p;
47 pthread_create(&p, 0, global_poller, 0);
48 string path = string(argv[0]) + "-so.so";
49 if (0 == dlopen(path.c_str(), RTLD_NOW)) {
50 fprintf(stderr, "dlerror: %s\n", dlerror());
51 return 1;
52 }
53 pthread_join(p, 0);
54 printf("PASSED\n");
55 // CHECK: PASSED
56 return 0;
57 }