xref: /llvm-project/compiler-rt/test/asan/TestCases/Posix/asan-symbolize-sanity-test.cpp (revision 37445e96d867f4266993085e821fbd4c4d8fa401)
1 // FIXME: https://code.google.com/p/address-sanitizer/issues/detail?id=316
2 // XFAIL: android
3 //
4 // Check that asan_symbolize.py script works (for binaries, ASan RTL and
5 // shared object files.
6 
7 // RUN: %clangxx_asan -O0 -DSHARED_LIB %s -fPIC -shared -o %t-so.so
8 // RUN: %clangxx_asan -O0 %s %libdl -o %t
9 // RUN: %env_asan_opts=symbolize=0 not %run %t 2>&1 | %asan_symbolize | FileCheck %s
10 // REQUIRES: stable-runtime
11 
12 // UNSUPPORTED: ios
13 
14 #if !defined(SHARED_LIB)
15 #include <dlfcn.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 
19 #include <string>
20 
21 using std::string;
22 
23 typedef void (fun_t)(int*, int);
24 
main(int argc,char * argv[])25 int main(int argc, char *argv[]) {
26   string path = string(argv[0]) + "-so.so";
27   printf("opening %s ... \n", path.c_str());
28   void *lib = dlopen(path.c_str(), RTLD_NOW);
29   if (!lib) {
30     printf("error in dlopen(): %s\n", dlerror());
31     return 1;
32   }
33   fun_t *inc2 = (fun_t*)dlsym(lib, "inc2");
34   if (!inc2) return 1;
35   printf("ok\n");
36   int *array = (int*)malloc(40);
37   inc2(array, 1);
38   inc2(array, -1);  // BOOM
39   // CHECK: ERROR: AddressSanitizer: heap-buffer-overflow
40   // CHECK: READ of size 4 at 0x{{.*}}
41   // CHECK: #0 {{.*}} in inc2 {{.*}}asan-symbolize-sanity-test.cpp:[[@LINE+21]]
42   // CHECK: #1 {{.*}} in main {{.*}}asan-symbolize-sanity-test.cpp:[[@LINE-4]]
43   // CHECK: allocated by thread T{{.*}} here:
44   // CHECK: #{{.*}} in {{(wrap_|_?__interceptor_)?}}malloc
45   // CHECK: #{{.*}} in main {{.*}}asan-symbolize-sanity-test.cpp:[[@LINE-9]]
46   return 0;
47 }
48 #else  // SHARED_LIBS
49 #include <stdio.h>
50 #include <string.h>
51 
52 int pad[10];
53 int GLOB[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
54 
55 extern "C"
inc(int index)56 void inc(int index) {
57   GLOB[index]++;
58 }
59 
60 extern "C"
inc2(int * a,int index)61 void inc2(int *a, int index) {
62   a[index]++;
63 }
64 #endif  // SHARED_LIBS
65