xref: /llvm-project/compiler-rt/test/asan/TestCases/Posix/mapped_mem_interceptors.c (revision 3c0ae1f5ab134b7f83d5988752508a81d8cdf4e6)
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 
main(int argc,char ** argv)11 int main(int argc, char **argv) {
12   int size = 4096;
13   int val = 42;
14 
15   // Get any mmaped pointer.
16   void *r =
17       mmap(0, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
18   assert(r != MAP_FAILED);
19 
20   // Make sure the memory is unpoisoned.
21   if (__asan_region_is_poisoned(r, size) != 0) {
22     fprintf(stderr, "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(r, size);
28   munmap(r, size);
29   if (__asan_region_is_poisoned(r, size) != 0) {
30     fprintf(stderr, "Shadow memory was not cleaned by munmap.\n");
31     abort();
32   }
33   __asan_poison_memory_region(r, size);
34   void *p = mmap(r, size, PROT_READ | PROT_WRITE,
35                  MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
36   assert(r == p);
37 
38   // Make sure the memory is unpoisoned.
39   if (__asan_region_is_poisoned(r, size) != 0) {
40     fprintf(stderr, "Memory returned by mmap should be unpoisoned.\n");
41     abort();
42   }
43 
44   return 0;
45 }
46