1 // RUN: %clangxx_asan -std=c++11 -O0 %s -o %t && %run %t
2 
3 #include <assert.h>
4 #include <unistd.h>
5 #include <sys/mman.h>
6 
main(void)7 int main(void) {
8   unsigned char vec[20];
9   int res;
10   size_t PS = sysconf(_SC_PAGESIZE);
11   void *addr = mmap(nullptr, 20 * PS, PROT_READ | PROT_WRITE,
12                     MAP_NORESERVE | MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
13 
14   res = mincore(addr, 10 * PS, vec);
15   assert(res == 0);
16   for (int i = 0; i < 10; ++i)
17     assert((vec[i] & 1) == 0);
18 
19   for (int i = 0; i < 5; ++i)
20     ((char *)addr)[i * PS] = 1;
21   res = mincore(addr, 10 * PS, vec);
22   assert(res == 0);
23   for (int i = 0; i < 10; ++i)
24     assert((vec[i] & 1) == (i < 5));
25 
26   for (int i = 5; i < 10; ++i)
27     ((char *)addr)[i * PS] = 1;
28   res = mincore(addr, 10 * PS, vec);
29   assert(res == 0);
30   for (int i = 0; i < 10; ++i)
31     assert((vec[i] & 1) == 1);
32 
33   return 0;
34 }
35