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// XFAIL: arm-linux-gnueabi
10
11#if !defined(SHARED_LIB)
12#include <dlfcn.h>
13#include <stdio.h>
14#include <string.h>
15
16#include <string>
17
18using std::string;
19
20typedef void (fun_t)(int x);
21
22int main(int argc, char *argv[]) {
23  string path = string(argv[0]) + "-so.so";
24  printf("opening %s ... \n", path.c_str());
25  void *lib = dlopen(path.c_str(), RTLD_NOW);
26  if (!lib) {
27    printf("error in dlopen(): %s\n", dlerror());
28    return 1;
29  }
30  fun_t *inc = (fun_t*)dlsym(lib, "inc");
31  if (!inc) return 1;
32  printf("ok\n");
33  inc(1);
34  inc(-1);  // BOOM
35  // CHECK: {{.*ERROR: AddressSanitizer: global-buffer-overflow}}
36  // CHECK: {{READ of size 4 at 0x.* thread T0}}
37  // CHECK: {{    #0 0x.*}}
38  // CHECK: {{    #1 0x.* in main .*shared-lib-test.cc:}}[[@LINE-4]]
39  return 0;
40}
41#else  // SHARED_LIBS
42#include <stdio.h>
43#include <string.h>
44
45int pad[10];
46int GLOB[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
47
48extern "C"
49void inc(int index) {
50  GLOB[index]++;
51}
52
53extern "C"
54void inc2(int *a, int index) {
55  a[index]++;
56}
57#endif  // SHARED_LIBS
58