1673dc3d4SNico Weber // Test for ASAN_OPTIONS=start_deactivated=1 mode.
2673dc3d4SNico Weber // Main executable is uninstrumented, but linked to ASan runtime. The shared
3673dc3d4SNico Weber // library is instrumented. Memory errors before dlopen are not detected.
4895878f4SAlex Richardson // Fails with debug checks: https://bugs.llvm.org/show_bug.cgi?id=46862
5895878f4SAlex Richardson // XFAIL: !compiler-rt-optimized
6673dc3d4SNico Weber
7673dc3d4SNico Weber // RUN: %clangxx_asan -O0 -DSHARED_LIB %s -std=c++11 -fPIC -shared -o %t-so.so
8673dc3d4SNico Weber // RUN: %clangxx -O0 %s -std=c++11 -c -o %t.o
9673dc3d4SNico Weber // RUN: %clangxx_asan -O0 %t.o %libdl -o %t
10673dc3d4SNico Weber // RUN: %env_asan_opts=start_deactivated=1,allocator_may_return_null=0 \
11673dc3d4SNico Weber // RUN: ASAN_ACTIVATION_OPTIONS=allocator_may_return_null=1 not %run %t 2>&1 | FileCheck %s
12673dc3d4SNico Weber // RUN: %env_asan_opts=start_deactivated=1 \
13673dc3d4SNico Weber // RUN: ASAN_ACTIVATION_OPTIONS=help=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-HELP
14673dc3d4SNico Weber // RUN: %env_asan_opts=start_deactivated=1,verbosity=1 \
15673dc3d4SNico Weber // RUN: ASAN_ACTIVATION_OPTIONS=help=1,handle_segv=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-UNSUPPORTED
16673dc3d4SNico Weber // RUN: %env_asan_opts=start_deactivated=1 \
17673dc3d4SNico Weber // RUN: ASAN_ACTIVATION_OPTIONS=help=1,handle_segv=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-UNSUPPORTED-V0
18673dc3d4SNico Weber
19673dc3d4SNico Weber // Check that verbosity=1 in activation flags affects reporting of unrecognized activation flags.
20673dc3d4SNico Weber // RUN: %env_asan_opts=start_deactivated=1 \
21673dc3d4SNico Weber // RUN: ASAN_ACTIVATION_OPTIONS=help=1,handle_segv=0,verbosity=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-UNSUPPORTED
22673dc3d4SNico Weber
23673dc3d4SNico Weber
24673dc3d4SNico Weber // END.
25673dc3d4SNico Weber
26673dc3d4SNico Weber #if !defined(SHARED_LIB)
27673dc3d4SNico Weber
28673dc3d4SNico Weber #include <assert.h>
29673dc3d4SNico Weber #include <dlfcn.h>
30673dc3d4SNico Weber #include <stdio.h>
31673dc3d4SNico Weber #include <stdlib.h>
32673dc3d4SNico Weber #include <string.h>
33673dc3d4SNico Weber #include <unistd.h>
34673dc3d4SNico Weber
35673dc3d4SNico Weber #include <string>
36673dc3d4SNico Weber
37673dc3d4SNico Weber #include "sanitizer/asan_interface.h"
38673dc3d4SNico Weber
test_malloc_shadow(char * p,size_t sz,bool expect_redzones)39673dc3d4SNico Weber void test_malloc_shadow(char *p, size_t sz, bool expect_redzones) {
40673dc3d4SNico Weber // Last byte of the left redzone, if present.
41673dc3d4SNico Weber assert((char *)__asan_region_is_poisoned(p - 1, sz + 1) ==
42673dc3d4SNico Weber (expect_redzones ? p - 1 : nullptr));
43673dc3d4SNico Weber // The user memory.
44673dc3d4SNico Weber assert((char *)__asan_region_is_poisoned(p, sz) == nullptr);
45673dc3d4SNico Weber // First byte of the right redzone, if present.
46673dc3d4SNico Weber assert((char *)__asan_region_is_poisoned(p, sz + 1) ==
47673dc3d4SNico Weber (expect_redzones ? p + sz : nullptr));
48673dc3d4SNico Weber }
49673dc3d4SNico Weber
50673dc3d4SNico Weber typedef void (*Fn)();
51673dc3d4SNico Weber
main(int argc,char * argv[])52673dc3d4SNico Weber int main(int argc, char *argv[]) {
53673dc3d4SNico Weber constexpr unsigned nPtrs = 200;
54673dc3d4SNico Weber char *ptrs[nPtrs];
55673dc3d4SNico Weber
56673dc3d4SNico Weber // Before activation: no redzones.
57673dc3d4SNico Weber for (size_t sz = 1; sz < nPtrs; ++sz) {
58673dc3d4SNico Weber ptrs[sz] = (char *)malloc(sz);
59673dc3d4SNico Weber test_malloc_shadow(ptrs[sz], sz, false);
60673dc3d4SNico Weber }
61673dc3d4SNico Weber
62673dc3d4SNico Weber // Create a honey pot for the future, instrumented, allocations. Since the
63673dc3d4SNico Weber // quarantine is disabled, chunks are going to be recycled right away and
64673dc3d4SNico Weber // reused for the new allocations. New allocations must get the proper
65673dc3d4SNico Weber // redzones anyway, whether it's a fresh or reused allocation.
66673dc3d4SNico Weber constexpr size_t HoneyPotBlockSize = 4096;
67673dc3d4SNico Weber constexpr int HoneyPotSize = 200;
68673dc3d4SNico Weber char *honeyPot[HoneyPotSize];
69673dc3d4SNico Weber for (int i = 1; i < HoneyPotSize; ++i) {
70673dc3d4SNico Weber honeyPot[i] = (char *)malloc(HoneyPotBlockSize);
71673dc3d4SNico Weber test_malloc_shadow(honeyPot[i], HoneyPotBlockSize, false);
72673dc3d4SNico Weber }
73673dc3d4SNico Weber for (int i = 1; i < HoneyPotSize; ++i)
74673dc3d4SNico Weber free(honeyPot[i]);
75673dc3d4SNico Weber
76673dc3d4SNico Weber std::string path = std::string(argv[0]) + "-so.so";
77673dc3d4SNico Weber void *dso = dlopen(path.c_str(), RTLD_NOW);
78673dc3d4SNico Weber if (!dso) {
79673dc3d4SNico Weber fprintf(stderr, "dlopen failed: %s\n", dlerror());
80673dc3d4SNico Weber return 1;
81673dc3d4SNico Weber }
82673dc3d4SNico Weber
83673dc3d4SNico Weber // After this line ASan is activated and starts detecting errors.
84673dc3d4SNico Weber void *fn = dlsym(dso, "do_another_bad_thing");
85673dc3d4SNico Weber if (!fn) {
86673dc3d4SNico Weber fprintf(stderr, "dlsym failed: %s\n", dlerror());
87673dc3d4SNico Weber return 1;
88673dc3d4SNico Weber }
89673dc3d4SNico Weber
90673dc3d4SNico Weber // After activation: redzones.
91673dc3d4SNico Weber for (int i = 1; i < HoneyPotSize; ++i) {
92673dc3d4SNico Weber honeyPot[i] = (char *)malloc(HoneyPotBlockSize);
93673dc3d4SNico Weber test_malloc_shadow(honeyPot[i], HoneyPotBlockSize, true);
94673dc3d4SNico Weber }
95673dc3d4SNico Weber {
96673dc3d4SNico Weber char *p = (char *)malloc(HoneyPotBlockSize);
97673dc3d4SNico Weber test_malloc_shadow(p, HoneyPotBlockSize, true);
98673dc3d4SNico Weber free(p);
99673dc3d4SNico Weber }
100673dc3d4SNico Weber for (int i = 1; i < HoneyPotSize; ++i)
101673dc3d4SNico Weber free(honeyPot[i]);
102673dc3d4SNico Weber
103673dc3d4SNico Weber // Pre-existing allocations got redzones, too.
104673dc3d4SNico Weber for (size_t sz = 1; sz < nPtrs; ++sz) {
105673dc3d4SNico Weber test_malloc_shadow(ptrs[sz], sz, true);
106673dc3d4SNico Weber free(ptrs[sz]);
107673dc3d4SNico Weber }
108673dc3d4SNico Weber
109673dc3d4SNico Weber // Test that ASAN_ACTIVATION_OPTIONS=allocator_may_return_null=1 has effect.
110673dc3d4SNico Weber void *p = malloc((unsigned long)-2);
111673dc3d4SNico Weber assert(!p);
112673dc3d4SNico Weber // CHECK: WARNING: AddressSanitizer failed to allocate 0xfff{{.*}} bytes
113673dc3d4SNico Weber
114673dc3d4SNico Weber ((Fn)fn)();
115673dc3d4SNico Weber // CHECK: AddressSanitizer: heap-buffer-overflow
116673dc3d4SNico Weber // CHECK: READ of size 1
117673dc3d4SNico Weber // CHECK: {{#0 .* in do_another_bad_thing}}
118*be366041SFlorian Mayer // CHECK: is located 5 bytes after 100-byte region
119673dc3d4SNico Weber // CHECK: in do_another_bad_thing
120673dc3d4SNico Weber
121673dc3d4SNico Weber return 0;
122673dc3d4SNico Weber }
123673dc3d4SNico Weber
124673dc3d4SNico Weber #else // SHARED_LIB
125673dc3d4SNico Weber
126673dc3d4SNico Weber #include <stdio.h>
127673dc3d4SNico Weber #include <stdlib.h>
128673dc3d4SNico Weber
do_another_bad_thing()129673dc3d4SNico Weber extern "C" void do_another_bad_thing() {
130673dc3d4SNico Weber char *volatile p = (char *)malloc(100);
131673dc3d4SNico Weber printf("%hhx\n", p[105]);
132673dc3d4SNico Weber }
133673dc3d4SNico Weber
134673dc3d4SNico Weber #endif // SHARED_LIB
135673dc3d4SNico Weber
136673dc3d4SNico Weber // help=1 in activation flags lists only flags are are supported at activation
137673dc3d4SNico Weber // CHECK-HELP: Available flags for {{.*}}Sanitizer:
138673dc3d4SNico Weber // CHECK-HELP-NOT: handle_segv
139673dc3d4SNico Weber // CHECK-HELP: max_redzone
140673dc3d4SNico Weber // CHECK-HELP-NOT: handle_segv
141673dc3d4SNico Weber
142673dc3d4SNico Weber // unsupported activation flags produce a warning ...
143673dc3d4SNico Weber // CHECK-UNSUPPORTED: WARNING: found 1 unrecognized
144673dc3d4SNico Weber // CHECK-UNSUPPORTED: handle_segv
145673dc3d4SNico Weber
146673dc3d4SNico Weber // ... but not at verbosity=0
147673dc3d4SNico Weber // CHECK-UNSUPPORTED-V0-NOT: WARNING: found {{.*}} unrecognized
148