Dmitry V. Levin | e7d671b | 2015-03-19 22:03:32 +0000 | [diff] [blame] | 1 | #ifdef HAVE_CONFIG_H |
| 2 | # include "config.h" |
| 3 | #endif |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <stdint.h> |
| 7 | #include <unistd.h> |
| 8 | #include <limits.h> |
| 9 | #include <sys/mman.h> |
| 10 | |
| 11 | int |
| 12 | main(void) |
| 13 | { |
| 14 | const intmax_t pagesize = sysconf(_SC_PAGESIZE); |
| 15 | const unsigned long length = pagesize * 3; |
| 16 | const int fd = -1; |
| 17 | off_t offset; |
| 18 | void *addr, *p; |
| 19 | |
| 20 | #if ULONG_MAX > 4294967295UL |
| 21 | offset = 0xcafedeadbeef000 & -pagesize; |
| 22 | addr = (void *) (uintmax_t) (0xfacefeed000 & -pagesize); |
| 23 | #else |
| 24 | offset = 0xdeadbeef000 & -pagesize; |
| 25 | addr = (void *) (unsigned int) (0xfaced000 & -pagesize); |
| 26 | #endif |
| 27 | |
| 28 | p = mmap(addr, length, PROT_READ | PROT_WRITE, |
| 29 | MAP_PRIVATE | MAP_ANONYMOUS, fd, offset); |
| 30 | if (p == MAP_FAILED || |
| 31 | mprotect(p, length, PROT_NONE) || |
| 32 | munmap(p, length)) |
| 33 | return 77; |
| 34 | |
| 35 | if (sizeof(offset) == sizeof(int)) |
| 36 | printf("(mmap2?|old_mmap)\\(%p, %lu, PROT_READ\\|PROT_WRITE, " |
| 37 | "MAP_PRIVATE\\|MAP_ANONYMOUS, %d, %#x\\) = %p\n", |
| 38 | addr, length, fd, (unsigned int) offset, p); |
| 39 | else |
| 40 | printf("(mmap2?|old_mmap)\\(%p, %lu, PROT_READ\\|PROT_WRITE, " |
| 41 | "MAP_PRIVATE\\|MAP_ANONYMOUS, %d, %#jx\\) = %p\n", |
| 42 | addr, length, fd, (uintmax_t) offset, p); |
| 43 | printf("mprotect\\(%p, %lu, PROT_NONE\\) += 0\n", p, length); |
| 44 | printf("munmap\\(%p, %lu\\) += 0\n", p, length); |
| 45 | return 0; |
| 46 | } |