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