1// RUN: %clangxx_asan -std=c++11 -O0 %s -o %t
2// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=READ
3// RUN: not %run %t write 2>&1 | FileCheck %s --check-prefix=WRITE
4// UNSUPPORTED: target={{(powerpc64|mips|s390).*}}
5
6#include <sys/mman.h>
7
8static volatile int sink;
9__attribute__((noinline)) void Read(int *ptr) { sink = *ptr; }
10__attribute__((noinline)) void Write(int *ptr) { *ptr = 0; }
11int main(int argc, char **argv) {
12 // Writes to shadow are detected as reads from shadow gap (because of how the
13 // shadow mapping works). This is kinda hard to fix. Test a random address in
14 // the application part of the address space.
15 void *volatile p =
16 mmap(addr: nullptr, len: 4096, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, fd: 0, offset: 0);
17 munmap(addr: p, len: 4096);
18 if (argc == 1)
19 Read(ptr: (int *)p);
20 else
21 Write(ptr: (int *)p);
22}
23// READ: AddressSanitizer: SEGV on unknown address
24// READ: The signal is caused by a READ memory access.
25// WRITE: AddressSanitizer: SEGV on unknown address
26// WRITE: The signal is caused by a WRITE memory access.
27

source code of compiler-rt/test/asan/TestCases/Linux/segv_read_write.c