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