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