57 lines · cpp
1// RUN: %clangxx_asan -O0 -DSHARED_LIB %s -fPIC -shared -o %t-so.so2// RUN: %clangxx_asan -O0 %s %libdl -o %t && not %run %t 2>&1 | FileCheck %s3// RUN: %clangxx_asan -O1 -DSHARED_LIB %s -fPIC -shared -o %t-so.so4// RUN: %clangxx_asan -O1 %s %libdl -o %t && not %run %t 2>&1 | FileCheck %s5// RUN: %clangxx_asan -O2 -DSHARED_LIB %s -fPIC -shared -o %t-so.so6// RUN: %clangxx_asan -O2 %s %libdl -o %t && not %run %t 2>&1 | FileCheck %s7// RUN: %clangxx_asan -O3 -DSHARED_LIB %s -fPIC -shared -o %t-so.so8// RUN: %clangxx_asan -O3 %s %libdl -o %t && not %run %t 2>&1 | FileCheck %s9 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); // BOOM34 // 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_LIB41#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_LIB57