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