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