shared-lib-test.cc revision 2d1fdb26e458c4ddc04155c1d421bced3ba90cd0
1// RUN: %clangxx_asan -O0 -DSHARED_LIB %s -fPIC -shared -o %t-so.so 2// RUN: %clangxx_asan -O0 %s -ldl -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 -ldl -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 -ldl -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 -ldl -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 17using std::string; 18 19typedef void (fun_t)(int x); 20 21int main(int argc, char *argv[]) { 22 string path = string(argv[0]) + "-so.so"; 23 printf("opening %s ... \n", path.c_str()); 24 void *lib = dlopen(path.c_str(), RTLD_NOW); 25 if (!lib) { 26 printf("error in dlopen(): %s\n", dlerror()); 27 return 1; 28 } 29 fun_t *inc = (fun_t*)dlsym(lib, "inc"); 30 if (!inc) return 1; 31 printf("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.cc:}}[[@LINE-4]] 38 return 0; 39} 40#else // SHARED_LIBS 41#include <stdio.h> 42#include <string.h> 43 44int pad[10]; 45int GLOB[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 46 47extern "C" 48void inc(int index) { 49 GLOB[index]++; 50} 51 52extern "C" 53void inc2(int *a, int index) { 54 a[index]++; 55} 56#endif // SHARED_LIBS 57