1 // Test for ASAN_OPTIONS=start_deactivated=1 mode.
2 // Main executable is uninstrumented, but linked to ASan runtime. The shared
3 // library is instrumented. Memory errors before dlopen are not detected.
4
5 // RUN: %clangxx_asan -O0 -DSHARED_LIB %s -fPIC -shared -o %t-so.so
6 // RUN: %clangxx -O0 %s -c -o %t.o
7 // RUN: %clangxx_asan -O0 %t.o %libdl -o %t
8 // RUN: %env_asan_opts=start_deactivated=1,allocator_may_return_null=0 \
9 // RUN: ASAN_ACTIVATION_OPTIONS=allocator_may_return_null=1 not %run %t 2>&1 | FileCheck %s
10 // RUN: %env_asan_opts=start_deactivated=1 \
11 // RUN: ASAN_ACTIVATION_OPTIONS=help=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-HELP
12 // RUN: %env_asan_opts=start_deactivated=1,verbosity=1 \
13 // RUN: ASAN_ACTIVATION_OPTIONS=help=1,handle_segv=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-UNSUPPORTED
14 // RUN: %env_asan_opts=start_deactivated=1 \
15 // RUN: ASAN_ACTIVATION_OPTIONS=help=1,handle_segv=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-UNSUPPORTED-V0
16
17 // Check that verbosity=1 in activation flags affects reporting of unrecognized activation flags.
18 // RUN: %env_asan_opts=start_deactivated=1 \
19 // RUN: ASAN_ACTIVATION_OPTIONS=help=1,handle_segv=0,verbosity=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-UNSUPPORTED
20
21 // XFAIL: arm-linux-gnueabi
22
23 #if !defined(SHARED_LIB)
24 #include <assert.h>
25 #include <dlfcn.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include <string>
32
33 #include "sanitizer/asan_interface.h"
34
test_malloc_shadow()35 void test_malloc_shadow() {
36 char *p = (char *)malloc(100);
37 char *q = (char *)__asan_region_is_poisoned(p + 95, 8);
38 fprintf(stderr, "=%zd=\n", q ? q - (p + 95) : -1);
39 free(p);
40 }
41
42 typedef void (*Fn)();
43
main(int argc,char * argv[])44 int main(int argc, char *argv[]) {
45 test_malloc_shadow();
46 // CHECK: =-1=
47
48 std::string path = std::string(argv[0]) + "-so.so";
49 void *dso = dlopen(path.c_str(), RTLD_NOW);
50 if (!dso) {
51 fprintf(stderr, "dlopen failed: %s\n", dlerror());
52 return 1;
53 }
54
55 test_malloc_shadow();
56 // CHECK: =5=
57
58 // After this line ASan is activated and starts detecting errors.
59 void *fn = dlsym(dso, "do_another_bad_thing");
60 if (!fn) {
61 fprintf(stderr, "dlsym failed: %s\n", dlerror());
62 return 1;
63 }
64
65 // Test that ASAN_ACTIVATION_OPTIONS=allocator_may_return_null=1 has effect.
66 void *p = malloc((unsigned long)-2);
67 assert(!p);
68 // CHECK: WARNING: AddressSanitizer failed to allocate 0xfff{{.*}} bytes
69
70 ((Fn)fn)();
71 // CHECK: AddressSanitizer: heap-buffer-overflow
72 // CHECK: READ of size 1
73 // CHECK: {{#0 .* in do_another_bad_thing}}
74 // CHECK: is located 5 bytes to the right of 100-byte region
75 // CHECK: in do_another_bad_thing
76
77 return 0;
78 }
79 #else // SHARED_LIB
80 #include <stdio.h>
81 #include <stdlib.h>
82
do_another_bad_thing()83 extern "C" void do_another_bad_thing() {
84 char *volatile p = (char *)malloc(100);
85 printf("%hhx\n", p[105]);
86 }
87 #endif // SHARED_LIB
88
89 // help=1 in activation flags lists only flags are are supported at activation
90 // CHECK-HELP: Available flags for {{.*}}Sanitizer:
91 // CHECK-HELP-NOT: handle_segv
92 // CHECK-HELP: max_redzone
93 // CHECK-HELP-NOT: handle_segv
94
95 // unsupported activation flags produce a warning ...
96 // CHECK-UNSUPPORTED: WARNING: found 1 unrecognized
97 // CHECK-UNSUPPORTED: handle_segv
98
99 // ... but not at verbosity=0
100 // CHECK-UNSUPPORTED-V0-NOT: WARNING: found {{.*}} unrecognized
101