brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.2 KiB · 1418dc4 Raw
47 lines · cpp
1// Check handle_bus flag2// Defaults to true3// RUN: %clangxx_asan -std=c++11 %s -o %t4// RUN: not %run %t 2>&1 | FileCheck %s -check-prefix=CHECK-BUS5// RUN: %env_asan_opts=handle_sigbus=0 not --crash %run %t 2>&1 | FileCheck %s6 7// UNSUPPORTED: ios8 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_FILE18#define MAP_FILE 019#endif20 21char array[4096];22int main(int argc, char **argv) {23  int fd = open((std::string(argv[0]) + ".m").c_str(), O_RDWR | O_CREAT, 0700);24  if (fd < 0) {25    perror("open");26    exit(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 size31  char *addr = (char *)mmap(nullptr, sizeof(array), PROT_READ,32                            MAP_FILE | MAP_SHARED, fd, 0);33  if (addr == MAP_FAILED) {34    perror("mmap");35    exit(1);36  }37 38  // Truncate the file so our memory isn't valid any more39  assert(ftruncate(fd, 0) == 0);40 41  // Try to access the memory42  return addr[42];43  // CHECK-NOT: DEADLYSIGNAL44  // CHECK-BUS: DEADLYSIGNAL45  // CHECK-BUS: ERROR: AddressSanitizer: BUS46}47