• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 struct Bar {
BarBar16   Bar(int val) : val(val) { printf("Bar::Bar(%d)\n", val); }
17   int val;
18 };
19 
20 int get_foo_val();
21 Bar global_bar(get_foo_val());
22 #else  // SHARED LIB
23 #include <dlfcn.h>
24 #include <stdio.h>
25 #include <string>
26 struct Foo {
FooFoo27   Foo() : val(42) { printf("Foo::Foo()\n"); }
28   int val;
29 };
30 
31 Foo global_foo;
32 
get_foo_val()33 int get_foo_val() {
34   return global_foo.val;
35 }
36 
main(int argc,char * argv[])37 int main(int argc, char *argv[]) {
38   std::string path = std::string(argv[0]) + "-so.so";
39   void *handle = dlopen(path.c_str(), RTLD_NOW);
40   if (!handle) {
41     printf("error in dlopen(): %s\n", dlerror());
42     return 1;
43   }
44   printf("%d\n", get_foo_val());
45   return 0;
46 }
47 #endif  // SHARED_LIB
48