blob: 2ca8015fc742759ba6fea7855bf2ed982700cfe0 [file] [log] [blame]
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001// 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
Stephen Hines86277eb2015-03-23 12:06:32 -07007// RUN: %clangxx_asan -O0 %t.o %libdl -o %t
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -07008// RUN: env ASAN_OPTIONS=$ASAN_OPTIONS:start_deactivated=1,allocator_may_return_null=0 \
Stephen Hines86277eb2015-03-23 12:06:32 -07009// RUN: ASAN_ACTIVATION_OPTIONS=allocator_may_return_null=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070010// RUN: env ASAN_OPTIONS=$ASAN_OPTIONS:start_deactivated=1 \
Stephen Hines86277eb2015-03-23 12:06:32 -070011// RUN: ASAN_ACTIVATION_OPTIONS=help=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-HELP
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070012// RUN: env ASAN_OPTIONS=$ASAN_OPTIONS:start_deactivated=1,verbosity=1 \
Stephen Hines86277eb2015-03-23 12:06:32 -070013// RUN: ASAN_ACTIVATION_OPTIONS=help=1,handle_segv=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-UNSUPPORTED
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070014// RUN: env ASAN_OPTIONS=$ASAN_OPTIONS:start_deactivated=1 \
Stephen Hines86277eb2015-03-23 12:06:32 -070015// 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.
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070018// RUN: env ASAN_OPTIONS=$ASAN_OPTIONS:start_deactivated=1 \
Stephen Hines86277eb2015-03-23 12:06:32 -070019// RUN: ASAN_ACTIVATION_OPTIONS=help=1,handle_segv=0,verbosity=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-UNSUPPORTED
20
Stephen Hines6a211c52014-07-21 00:49:56 -070021// XFAIL: arm-linux-gnueabi
Stephen Hines2d1fdb22014-05-28 23:58:16 -070022
23#if !defined(SHARED_LIB)
Stephen Hines86277eb2015-03-23 12:06:32 -070024#include <assert.h>
Stephen Hines2d1fdb22014-05-28 23:58:16 -070025#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
35void 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
42typedef void (*Fn)();
43
44int 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
Stephen Hines86277eb2015-03-23 12:06:32 -070058 // After this line ASan is activated and starts detecting errors.
Stephen Hines2d1fdb22014-05-28 23:58:16 -070059 void *fn = dlsym(dso, "do_another_bad_thing");
60 if (!fn) {
61 fprintf(stderr, "dlsym failed: %s\n", dlerror());
62 return 1;
63 }
64
Stephen Hines86277eb2015-03-23 12:06:32 -070065 // 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
Stephen Hines2d1fdb22014-05-28 23:58:16 -070070 ((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
83extern "C" void do_another_bad_thing() {
84 char *volatile p = (char *)malloc(100);
85 printf("%hhx\n", p[105]);
86}
87#endif // SHARED_LIB
Stephen Hines86277eb2015-03-23 12:06:32 -070088
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