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
5#include "../defines.h"
6#include <stdio.h>
7#include <windows.h>
8
9static volatile int sink;
10ATTRIBUTE_NOINLINE void Read(int *ptr) { sink = *ptr; }
11ATTRIBUTE_NOINLINE void Write(int *ptr) { *ptr = 0; }
12int main(int argc, char **argv) {
13 // Writes to shadow are detected as reads from shadow gap (because of how the
14 // shadow mapping works). This is kinda hard to fix. Test a random address in
15 // the application part of the address space.
16 void *volatile p = VirtualAlloc(0, 4096, MEM_COMMIT, PAGE_READONLY);
17 bool ok = VirtualFree(p, 0, MEM_RELEASE);
18 if (!ok) {
19 printf(format: "VirtualFree failed\n");
20 return 0;
21 }
22 if (argc == 1)
23 Read(ptr: (int *)p);
24 else
25 Write(ptr: (int *)p);
26}
27// READ: AddressSanitizer: access-violation on unknown address
28// READ: The signal is caused by a READ memory access.
29// WRITE: AddressSanitizer: access-violation on unknown address
30// WRITE: The signal is caused by a WRITE memory access.
31

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of compiler-rt/test/asan/TestCases/Windows/crash_read_write.cpp