1// Regression test for
2// https://code.google.com/p/address-sanitizer/issues/detail?id=178
3
4// RUN: %clangxx_asan -O0 -DSHARED_LIB %s -fPIC -shared -o %t-so.so
5// RUN: %clangxx_asan -O0 %s %libdl -Wl,--export-dynamic -o %t
6// RUN: %env_asan_opts=strict_init_order=true %run %t 2>&1
7
8// dlopen() can not be intercepted on Android, making strict_init_order nearly
9// useless there.
10// UNSUPPORTED: android
11
12#if defined(SHARED_LIB)
13#include <stdio.h>
14
15struct Bar {
16 Bar(int val) : val(val) { printf("Bar::Bar(%d)\n", val); }
17 int val;
18};
19
20int get_foo_val();
21Bar global_bar(get_foo_val());
22#else // SHARED LIB
23#include <dlfcn.h>
24#include <stdio.h>
25#include <string>
26struct Foo {
27 Foo() : val(42) { printf(format: "Foo::Foo()\n"); }
28 int val;
29};
30
31Foo global_foo;
32
33int get_foo_val() {
34 return global_foo.val;
35}
36
37int main(int argc, char *argv[]) {
38 std::string path = std::string(argv[0]) + "-so.so";
39 void *handle = dlopen(file: path.c_str(), RTLD_NOW);
40 if (!handle) {
41 printf(format: "error in dlopen(): %s\n", dlerror());
42 return 1;
43 }
44 printf(format: "%d\n", get_foo_val());
45 return 0;
46}
47#endif // SHARED_LIB
48

source code of compiler-rt/test/asan/TestCases/Linux/init-order-dlopen.cpp