Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 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 |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 6 | // RUN: env ASAN_OPTIONS=$ASAN_OPTIONS:strict_init_order=true %run %t 2>&1 |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 7 | |
| 8 | #if defined(SHARED_LIB) |
| 9 | #include <stdio.h> |
| 10 | |
| 11 | struct Bar { |
| 12 | Bar(int val) : val(val) { printf("Bar::Bar(%d)\n", val); } |
| 13 | int val; |
| 14 | }; |
| 15 | |
| 16 | int get_foo_val(); |
| 17 | Bar global_bar(get_foo_val()); |
| 18 | #else // SHARED LIB |
| 19 | #include <dlfcn.h> |
| 20 | #include <stdio.h> |
| 21 | #include <string> |
| 22 | struct Foo { |
| 23 | Foo() : val(42) { printf("Foo::Foo()\n"); } |
| 24 | int val; |
| 25 | }; |
| 26 | |
| 27 | Foo global_foo; |
| 28 | |
| 29 | int get_foo_val() { |
| 30 | return global_foo.val; |
| 31 | } |
| 32 | |
| 33 | int 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 |