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 | |
7 | int main(void) { |
8 | unsigned char vec[20]; |
9 | int res; |
10 | size_t PS = sysconf(_SC_PAGESIZE); |
11 | void *addr = mmap(addr: nullptr, len: 20 * PS, PROT_READ | PROT_WRITE, |
12 | MAP_NORESERVE | MAP_PRIVATE | MAP_ANONYMOUS, fd: 0, offset: 0); |
13 | |
14 | res = mincore(start: addr, len: 10 * PS, vec: 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(start: addr, len: 10 * PS, vec: 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(start: addr, len: 10 * PS, vec: 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 | |