1445b810fSDan Liew // UNSUPPORTED: ios
2e47e2264SDan Liew // Don't re-enable until rdar://problem/62141527 is fixed.
3e47e2264SDan Liew // REQUIRES: rdar_62141527
4445b810fSDan Liew // REQUIRES: shell
5445b810fSDan Liew // REQUIRES: darwin_log_cmd
6445b810fSDan Liew // RUN: %clangxx_asan -fsanitize-recover=address %s -o %t
7445b810fSDan Liew // RUN: { %env_asan_opts=halt_on_error=0,log_to_syslog=1 %run %t > %t.process_output.txt 2>&1 & } \
8445b810fSDan Liew // RUN: ; export TEST_PID=$! ; wait ${TEST_PID}
9445b810fSDan Liew 
10445b810fSDan Liew // Check process output.
11445b810fSDan Liew // RUN: FileCheck %s --check-prefixes CHECK,CHECK-PROC -input-file=%t.process_output.txt
12445b810fSDan Liew 
13445b810fSDan Liew // Check syslog output. We filter recent system logs based on PID to avoid
14445b810fSDan Liew // getting the logs of previous test runs.
154155784cSDan Liew // RUN: log show --debug --last 5m  --predicate "processID == ${TEST_PID}" --style syslog > %t.process_syslog_output.txt
16445b810fSDan Liew // RUN: FileCheck %s -input-file=%t.process_syslog_output.txt
17445b810fSDan Liew #include <cassert>
18445b810fSDan Liew #include <cstdio>
19*fc532c1aSDan Liew #include <cstring>
20445b810fSDan Liew #include <sanitizer/asan_interface.h>
21445b810fSDan Liew 
22445b810fSDan Liew const int kBufferSize = 512;
23445b810fSDan Liew char *buffer;
24445b810fSDan Liew 
25445b810fSDan Liew // `readZero` and `readOne` exist so that we can distinguish the two
26445b810fSDan Liew // error reports based on the symbolized stacktrace.
readZero()27445b810fSDan Liew void readZero() {
28445b810fSDan Liew   assert(__asan_address_is_poisoned(buffer));
29445b810fSDan Liew   char c = buffer[0];
30445b810fSDan Liew   printf("Read %c\n", c);
31445b810fSDan Liew }
32445b810fSDan Liew 
readOne()33445b810fSDan Liew void readOne() {
34445b810fSDan Liew   assert(__asan_address_is_poisoned(buffer + 1));
35445b810fSDan Liew   char c = buffer[1];
36445b810fSDan Liew   printf("Read %c\n", c);
37445b810fSDan Liew }
38445b810fSDan Liew 
main()39445b810fSDan Liew int main() {
40445b810fSDan Liew   buffer = static_cast<char *>(malloc(kBufferSize));
41*fc532c1aSDan Liew   memset(static_cast<void *>(buffer), static_cast<int>('.'), kBufferSize);
42445b810fSDan Liew   assert(buffer);
43445b810fSDan Liew   // Deliberately poison `buffer` so that we have a deterministic way
44445b810fSDan Liew   // triggering two ASan reports in a row in the no halt_on_error mode (e.g. Two
45445b810fSDan Liew   // heap-use-after free in a row might not be deterministic).
46445b810fSDan Liew   __asan_poison_memory_region(buffer, kBufferSize);
47445b810fSDan Liew 
48445b810fSDan Liew   // This sequence of ASan reports are designed to catch an old bug in the way
49445b810fSDan Liew   // ASan's internal syslog buffer was handled after reporting an issue.
50445b810fSDan Liew   // Previously in the no halt_on_error mode the internal buffer wasn't cleared
51445b810fSDan Liew   // after reporting an issue. When another issue was encountered everything
52445b810fSDan Liew   // that was already in the buffer would be written to the syslog again
53445b810fSDan Liew   // leading to duplicate reports in the syslog.
54445b810fSDan Liew 
55445b810fSDan Liew   // First bad access.
56445b810fSDan Liew   // CHECK: use-after-poison
57445b810fSDan Liew   // CHECK-NEXT: READ of size 1
58445b810fSDan Liew   // CHECK-NEXT: #0 0x{{[0-9a-f]+}} in readZero
59445b810fSDan Liew   // CHECK: SUMMARY: {{.*}} use-after-poison {{.*}} in readZero
60445b810fSDan Liew   readZero();
61445b810fSDan Liew 
62445b810fSDan Liew   // Second bad access.
63445b810fSDan Liew   // CHECK: use-after-poison
64445b810fSDan Liew   // CHECK-NEXT: READ of size 1
65445b810fSDan Liew   // CHECK-NEXT: #0 0x{{[0-9a-f]+}} in readOne
66445b810fSDan Liew   // CHECK: SUMMARY: {{.*}} use-after-poison {{.*}} in readOne
67445b810fSDan Liew   readOne();
68445b810fSDan Liew 
69445b810fSDan Liew   // CHECK-PROC: DONE
70445b810fSDan Liew   printf("DONE\n");
71445b810fSDan Liew   return 0;
72445b810fSDan Liew }
73