Move ASan lit-tests under test/asan
[blocksruntime.git] / test / asan / TestCases / init-order-dlopen.cc
blobd30d11999246fd4451553462c4c3c6422d0570d4
1 // Regression test for
2 // https://code.google.com/p/address-sanitizer/issues/detail?id=178
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 -o %t -Wl,--export-dynamic || \
14 // RUN: %clangxx_asan -O0 %s -o %t
15 // RUN: ASAN_OPTIONS=strict_init_order=true %t 2>&1 | FileCheck %s
16 #include <dlfcn.h>
17 #include <pthread.h>
18 #include <stdio.h>
19 #include <unistd.h>
21 #include <string>
23 using std::string;
25 int foo() {
26 return 42;
28 int global = foo();
30 __attribute__((visibility("default")))
31 extern "C"
32 void inc_global() {
33 global++;
36 void *global_poller(void *arg) {
37 while (true) {
38 if (global != 42)
39 break;
40 usleep(100);
42 return 0;
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;
53 pthread_join(p, 0);
54 printf("PASSED\n");
55 // CHECK: PASSED
56 return 0;