Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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 -fPIC -shared -o %t-so.so |
| 6 | // RUN: %clangxx -O0 %s -c -o %t.o |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 7 | // RUN: %clangxx_asan -O0 %t.o %libdl -o %t |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 8 | // RUN: %env_asan_opts=start_deactivated=1,allocator_may_return_null=0 \ |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 9 | // RUN: ASAN_ACTIVATION_OPTIONS=allocator_may_return_null=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 10 | // RUN: %env_asan_opts=start_deactivated=1 \ |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 11 | // RUN: ASAN_ACTIVATION_OPTIONS=help=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-HELP |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 12 | // RUN: %env_asan_opts=start_deactivated=1,verbosity=1 \ |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 13 | // RUN: ASAN_ACTIVATION_OPTIONS=help=1,handle_segv=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-UNSUPPORTED |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 14 | // RUN: %env_asan_opts=start_deactivated=1 \ |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 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. |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 18 | // RUN: %env_asan_opts=start_deactivated=1 \ |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 19 | // RUN: ASAN_ACTIVATION_OPTIONS=help=1,handle_segv=0,verbosity=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-UNSUPPORTED |
| 20 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 21 | // XFAIL: arm-linux-gnueabi |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 22 | |
| 23 | #if !defined(SHARED_LIB) |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 24 | #include <assert.h> |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 25 | #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 | |
| 35 | void 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 | |
| 42 | typedef void (*Fn)(); |
| 43 | |
| 44 | int 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 Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 58 | // After this line ASan is activated and starts detecting errors. |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 59 | 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 Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 65 | // 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 Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 70 | ((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 | |
| 83 | extern "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 Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 88 | |
| 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 |