]> git.proxmox.com Git - rustc.git/blob - src/libcompiler_builtins/compiler-rt/test/asan/TestCases/Posix/start-deactivated.cc
New upstream version 1.20.0+dfsg1
[rustc.git] / src / libcompiler_builtins / compiler-rt / test / asan / TestCases / Posix / start-deactivated.cc
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 -std=c++11 -fPIC -shared -o %t-so.so
6 // RUN: %clangxx -O0 %s -std=c++11 -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
25 #include <assert.h>
26 #include <dlfcn.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #include <string>
33
34 #include "sanitizer/asan_interface.h"
35
36 void test_malloc_shadow(char *p, size_t sz, bool expect_redzones) {
37 // Last byte of the left redzone, if present.
38 assert((char *)__asan_region_is_poisoned(p - 1, sz + 1) ==
39 (expect_redzones ? p - 1 : nullptr));
40 // The user memory.
41 assert((char *)__asan_region_is_poisoned(p, sz) == nullptr);
42 // First byte of the right redzone, if present.
43 assert((char *)__asan_region_is_poisoned(p, sz + 1) ==
44 (expect_redzones ? p + sz : nullptr));
45 }
46
47 typedef void (*Fn)();
48
49 int main(int argc, char *argv[]) {
50 constexpr unsigned nPtrs = 200;
51 char *ptrs[nPtrs];
52
53 // Before activation: no redzones.
54 for (size_t sz = 1; sz < nPtrs; ++sz) {
55 ptrs[sz] = (char *)malloc(sz);
56 test_malloc_shadow(ptrs[sz], sz, false);
57 }
58
59 // Create a honey pot for the future, instrumented, allocations. Since the
60 // quarantine is disabled, chunks are going to be recycled right away and
61 // reused for the new allocations. New allocations must get the proper
62 // redzones anyway, whether it's a fresh or reused allocation.
63 constexpr size_t HoneyPotBlockSize = 4096;
64 constexpr int HoneyPotSize = 200;
65 char *honeyPot[HoneyPotSize];
66 for (int i = 1; i < HoneyPotSize; ++i) {
67 honeyPot[i] = (char *)malloc(HoneyPotBlockSize);
68 test_malloc_shadow(honeyPot[i], HoneyPotBlockSize, false);
69 }
70 for (int i = 1; i < HoneyPotSize; ++i)
71 free(honeyPot[i]);
72
73 std::string path = std::string(argv[0]) + "-so.so";
74 void *dso = dlopen(path.c_str(), RTLD_NOW);
75 if (!dso) {
76 fprintf(stderr, "dlopen failed: %s\n", dlerror());
77 return 1;
78 }
79
80 // After this line ASan is activated and starts detecting errors.
81 void *fn = dlsym(dso, "do_another_bad_thing");
82 if (!fn) {
83 fprintf(stderr, "dlsym failed: %s\n", dlerror());
84 return 1;
85 }
86
87 // After activation: redzones.
88 for (int i = 1; i < HoneyPotSize; ++i) {
89 honeyPot[i] = (char *)malloc(HoneyPotBlockSize);
90 test_malloc_shadow(honeyPot[i], HoneyPotBlockSize, true);
91 }
92 {
93 char *p = (char *)malloc(HoneyPotBlockSize);
94 test_malloc_shadow(p, HoneyPotBlockSize, true);
95 free(p);
96 }
97 for (int i = 1; i < HoneyPotSize; ++i)
98 free(honeyPot[i]);
99
100 // Pre-existing allocations got redzones, too.
101 for (size_t sz = 1; sz < nPtrs; ++sz) {
102 test_malloc_shadow(ptrs[sz], sz, true);
103 free(ptrs[sz]);
104 }
105
106 // Test that ASAN_ACTIVATION_OPTIONS=allocator_may_return_null=1 has effect.
107 void *p = malloc((unsigned long)-2);
108 assert(!p);
109 // CHECK: WARNING: AddressSanitizer failed to allocate 0xfff{{.*}} bytes
110
111 ((Fn)fn)();
112 // CHECK: AddressSanitizer: heap-buffer-overflow
113 // CHECK: READ of size 1
114 // CHECK: {{#0 .* in do_another_bad_thing}}
115 // CHECK: is located 5 bytes to the right of 100-byte region
116 // CHECK: in do_another_bad_thing
117
118 return 0;
119 }
120
121 #else // SHARED_LIB
122
123 #include <stdio.h>
124 #include <stdlib.h>
125
126 extern "C" void do_another_bad_thing() {
127 char *volatile p = (char *)malloc(100);
128 printf("%hhx\n", p[105]);
129 }
130
131 #endif // SHARED_LIB
132
133 // help=1 in activation flags lists only flags are are supported at activation
134 // CHECK-HELP: Available flags for {{.*}}Sanitizer:
135 // CHECK-HELP-NOT: handle_segv
136 // CHECK-HELP: max_redzone
137 // CHECK-HELP-NOT: handle_segv
138
139 // unsupported activation flags produce a warning ...
140 // CHECK-UNSUPPORTED: WARNING: found 1 unrecognized
141 // CHECK-UNSUPPORTED: handle_segv
142
143 // ... but not at verbosity=0
144 // CHECK-UNSUPPORTED-V0-NOT: WARNING: found {{.*}} unrecognized