| 1 | // Check handle_bus flag |
| 2 | // Defaults to true |
| 3 | // RUN: %clangxx_asan -std=c++11 %s -o %t |
| 4 | // RUN: not %run %t 2>&1 | FileCheck %s -check-prefix=CHECK-BUS |
| 5 | // RUN: %env_asan_opts=handle_sigbus=0 not --crash %run %t 2>&1 | FileCheck %s |
| 6 | |
| 7 | // UNSUPPORTED: ios |
| 8 | |
| 9 | #include <assert.h> |
| 10 | #include <fcntl.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <sys/mman.h> |
| 14 | #include <unistd.h> |
| 15 | #include <string> |
| 16 | |
| 17 | #ifndef MAP_FILE |
| 18 | #define MAP_FILE 0 |
| 19 | #endif |
| 20 | |
| 21 | char array[4096]; |
| 22 | int main(int argc, char **argv) { |
| 23 | int fd = open(file: (std::string(argv[0]) + ".m" ).c_str(), O_RDWR | O_CREAT, 0700); |
| 24 | if (fd < 0) { |
| 25 | perror(s: "open" ); |
| 26 | exit(status: 1); |
| 27 | } |
| 28 | assert(write(fd, array, sizeof(array)) == sizeof(array)); |
| 29 | |
| 30 | // Write some zeroes to the file, then mmap it while it has a 4KiB size |
| 31 | char *addr = (char *)mmap(addr: nullptr, len: sizeof(array), PROT_READ, |
| 32 | MAP_FILE | MAP_SHARED, fd: fd, offset: 0); |
| 33 | if (addr == MAP_FAILED) { |
| 34 | perror(s: "mmap" ); |
| 35 | exit(status: 1); |
| 36 | } |
| 37 | |
| 38 | // Truncate the file so our memory isn't valid any more |
| 39 | assert(ftruncate(fd, 0) == 0); |
| 40 | |
| 41 | // Try to access the memory |
| 42 | return addr[42]; |
| 43 | // CHECK-NOT: DEADLYSIGNAL |
| 44 | // CHECK-BUS: DEADLYSIGNAL |
| 45 | // CHECK-BUS: ERROR: AddressSanitizer: BUS |
| 46 | } |
| 47 | |