| 1 | // RUN: %clangxx_asan -O0 -DSHARED_LIB %s -fPIC -shared -o %t-so.so |
| 2 | // RUN: %clangxx_asan -O0 %s %libdl -o %t && not %run %t 2>&1 | FileCheck %s |
| 3 | // RUN: %clangxx_asan -O1 -DSHARED_LIB %s -fPIC -shared -o %t-so.so |
| 4 | // RUN: %clangxx_asan -O1 %s %libdl -o %t && not %run %t 2>&1 | FileCheck %s |
| 5 | // RUN: %clangxx_asan -O2 -DSHARED_LIB %s -fPIC -shared -o %t-so.so |
| 6 | // RUN: %clangxx_asan -O2 %s %libdl -o %t && not %run %t 2>&1 | FileCheck %s |
| 7 | // RUN: %clangxx_asan -O3 -DSHARED_LIB %s -fPIC -shared -o %t-so.so |
| 8 | // RUN: %clangxx_asan -O3 %s %libdl -o %t && not %run %t 2>&1 | FileCheck %s |
| 9 | |
| 10 | #if !defined(SHARED_LIB) |
| 11 | #include <dlfcn.h> |
| 12 | #include <stdio.h> |
| 13 | #include <string.h> |
| 14 | |
| 15 | #include <string> |
| 16 | |
| 17 | using std::string; |
| 18 | |
| 19 | typedef void (fun_t)(int x); |
| 20 | |
| 21 | int main(int argc, char *argv[]) { |
| 22 | string path = string(argv[0]) + "-so.so" ; |
| 23 | printf(format: "opening %s ... \n" , path.c_str()); |
| 24 | void *lib = dlopen(file: path.c_str(), RTLD_NOW); |
| 25 | if (!lib) { |
| 26 | printf(format: "error in dlopen(): %s\n" , dlerror()); |
| 27 | return 1; |
| 28 | } |
| 29 | fun_t *inc = (fun_t*)dlsym(handle: lib, name: "inc" ); |
| 30 | if (!inc) return 1; |
| 31 | printf(format: "ok\n" ); |
| 32 | inc(1); |
| 33 | inc(-1); // BOOM |
| 34 | // CHECK: {{.*ERROR: AddressSanitizer: global-buffer-overflow}} |
| 35 | // CHECK: {{READ of size 4 at 0x.* thread T0}} |
| 36 | // CHECK: {{ #0 0x.*}} |
| 37 | // CHECK: {{ #1 0x.* in main .*shared-lib-test.cpp:}}[[@LINE-4]] |
| 38 | return 0; |
| 39 | } |
| 40 | #else // SHARED_LIB |
| 41 | #include <stdio.h> |
| 42 | #include <string.h> |
| 43 | |
| 44 | int pad[10]; |
| 45 | int GLOB[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 46 | |
| 47 | extern "C" |
| 48 | void inc(int index) { |
| 49 | GLOB[index]++; |
| 50 | } |
| 51 | |
| 52 | extern "C" |
| 53 | void inc2(int *a, int index) { |
| 54 | a[index]++; |
| 55 | } |
| 56 | #endif // SHARED_LIB |
| 57 | |