1// Test for mmap/munmap interceptors.
2// RUN: %clang_asan %s -o %t
3// RUN: %run %t 2>&1
4
5#include <assert.h>
6#include <sanitizer/asan_interface.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <sys/mman.h>
10
11int main(int argc, char **argv) {
12 int size = 4096;
13 int val = 42;
14
15 // Get any mmaped pointer.
16 void *r =
17 mmap(addr: 0, len: size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, fd: -1, offset: 0);
18 assert(r != MAP_FAILED);
19
20 // Make sure the memory is unpoisoned.
21 if (__asan_region_is_poisoned(beg: r, size) != 0) {
22 fprintf(stderr, format: "Memory returned by mmap should be unpoisoned.\n");
23 abort();
24 }
25
26 // First munmmap and then mmap the same pointer using MAP_FIXED.
27 __asan_poison_memory_region(addr: r, size);
28 munmap(addr: r, len: size);
29 if (__asan_region_is_poisoned(beg: r, size) != 0) {
30 fprintf(stderr, format: "Shadow memory was not cleaned by munmap.\n");
31 abort();
32 }
33 __asan_poison_memory_region(addr: r, size);
34 void *p = mmap(addr: r, len: size, PROT_READ | PROT_WRITE,
35 MAP_FIXED | MAP_ANON | MAP_PRIVATE, fd: -1, offset: 0);
36 assert(r == p);
37
38 // Make sure the memory is unpoisoned.
39 if (__asan_region_is_poisoned(beg: r, size) != 0) {
40 fprintf(stderr, format: "Memory returned by mmap should be unpoisoned.\n");
41 abort();
42 }
43
44 return 0;
45}
46

source code of compiler-rt/test/asan/TestCases/Posix/mapped_mem_interceptors.c