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