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_OPTIONS=strict_init_order=true %run %t 2>&1
7
8#if defined(SHARED_LIB)
9#include <stdio.h>
10
11struct Bar {
12  Bar(int val) : val(val) { printf("Bar::Bar(%d)\n", val); }
13  int val;
14};
15
16int get_foo_val();
17Bar global_bar(get_foo_val());
18#else  // SHARED LIB
19#include <dlfcn.h>
20#include <stdio.h>
21#include <string>
22struct Foo {
23  Foo() : val(42) { printf("Foo::Foo()\n"); }
24  int val;
25};
26
27Foo global_foo;
28
29int get_foo_val() {
30  return global_foo.val;
31}
32
33int main(int argc, char *argv[]) {
34  std::string path = std::string(argv[0]) + "-so.so";
35  void *handle = dlopen(path.c_str(), RTLD_NOW);
36  if (!handle) {
37    printf("error in dlopen(): %s\n", dlerror());
38    return 1;
39  }
40  printf("%d\n", get_foo_val());
41  return 0;
42}
43#endif  // SHARED_LIB
44