]>
git.proxmox.com Git - rustc.git/blob - 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.
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
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
21 // XFAIL: arm-linux-gnueabi
23 #if !defined(SHARED_LIB)
34 #include "sanitizer/asan_interface.h"
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));
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));
49 int main(int argc
, char *argv
[]) {
50 constexpr unsigned nPtrs
= 200;
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);
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);
70 for (int i
= 1; i
< HoneyPotSize
; ++i
)
73 std::string path
= std::string(argv
[0]) + "-so.so";
74 void *dso
= dlopen(path
.c_str(), RTLD_NOW
);
76 fprintf(stderr
, "dlopen failed: %s\n", dlerror());
80 // After this line ASan is activated and starts detecting errors.
81 void *fn
= dlsym(dso
, "do_another_bad_thing");
83 fprintf(stderr
, "dlsym failed: %s\n", dlerror());
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);
93 char *p
= (char *)malloc(HoneyPotBlockSize
);
94 test_malloc_shadow(p
, HoneyPotBlockSize
, true);
97 for (int i
= 1; i
< HoneyPotSize
; ++i
)
100 // Pre-existing allocations got redzones, too.
101 for (size_t sz
= 1; sz
< nPtrs
; ++sz
) {
102 test_malloc_shadow(ptrs
[sz
], sz
, true);
106 // Test that ASAN_ACTIVATION_OPTIONS=allocator_may_return_null=1 has effect.
107 void *p
= malloc((unsigned long)-2);
109 // CHECK: WARNING: AddressSanitizer failed to allocate 0xfff{{.*}} bytes
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
126 extern "C" void do_another_bad_thing() {
127 char *volatile p
= (char *)malloc(100);
128 printf("%hhx\n", p
[105]);
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
139 // unsupported activation flags produce a warning ...
140 // CHECK-UNSUPPORTED: WARNING: found 1 unrecognized
141 // CHECK-UNSUPPORTED: handle_segv
143 // ... but not at verbosity=0
144 // CHECK-UNSUPPORTED-V0-NOT: WARNING: found {{.*}} unrecognized