brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.5 KiB · 40a168b Raw
72 lines · cpp
1// UNSUPPORTED: ios2// REQUIRES: darwin_log_cmd3// RUN: %clangxx_asan -fsanitize-recover=address %s -o %t4// RUN: bash -c '{ %env_asan_opts=halt_on_error=0,log_to_syslog=1 %run %t > %t.process_output.txt 2>&1 & } \5// RUN: ; export TEST_PID=$! ; wait ${TEST_PID}; echo -n ${TEST_PID} > %t.test_pid'6 7// Check process output.8// RUN: FileCheck %s --check-prefixes CHECK,CHECK-PROC -input-file=%t.process_output.txt9 10// Check syslog output. We filter recent system logs based on PID to avoid11// getting the logs of previous test runs. Make some reattempts in case there12// is a delay.13// RUN: %S/Inputs/check-syslog.sh %{readfile:%t.test_pid} %t.process_syslog_output.txt14// RUN: FileCheck %s -input-file=%t.process_syslog_output.txt15#include <cassert>16#include <cstdio>17#include <cstdlib>18#include <cstring>19#include <sanitizer/asan_interface.h>20 21const int kBufferSize = 512;22char *buffer;23 24// `readZero` and `readOne` exist so that we can distinguish the two25// error reports based on the symbolized stacktrace.26void readZero() {27  assert(__asan_address_is_poisoned(buffer));28  char c = buffer[0];29  printf("Read %c\n", c);30}31 32void readOne() {33  assert(__asan_address_is_poisoned(buffer + 1));34  char c = buffer[1];35  printf("Read %c\n", c);36}37 38int main() {39  buffer = static_cast<char *>(malloc(kBufferSize));40  memset(static_cast<void *>(buffer), static_cast<int>('.'), kBufferSize);41  assert(buffer);42  // Deliberately poison `buffer` so that we have a deterministic way43  // triggering two ASan reports in a row in the no halt_on_error mode (e.g. Two44  // heap-use-after free in a row might not be deterministic).45  __asan_poison_memory_region(buffer, kBufferSize);46 47  // This sequence of ASan reports are designed to catch an old bug in the way48  // ASan's internal syslog buffer was handled after reporting an issue.49  // Previously in the no halt_on_error mode the internal buffer wasn't cleared50  // after reporting an issue. When another issue was encountered everything51  // that was already in the buffer would be written to the syslog again52  // leading to duplicate reports in the syslog.53 54  // First bad access.55  // CHECK: use-after-poison56  // CHECK-NEXT: READ of size 157  // CHECK-NEXT: #0 0x{{[0-9a-f]+}} in readZero58  // CHECK: SUMMARY: {{.*}} use-after-poison {{.*}} in readZero59  readZero();60 61  // Second bad access.62  // CHECK: use-after-poison63  // CHECK-NEXT: READ of size 164  // CHECK-NEXT: #0 0x{{[0-9a-f]+}} in readOne65  // CHECK: SUMMARY: {{.*}} use-after-poison {{.*}} in readOne66  readOne();67 68  // CHECK-PROC: DONE69  printf("DONE\n");70  return 0;71}72