Denys Vlasenko | 8dedb0d | 2013-02-18 03:13:07 +0100 | [diff] [blame] | 1 | // Test program which explores whether mmap's ofs parameter |
| 2 | // is 64-bit, and whether it needs to be shifted << PAGE_SHIFT. |
| 3 | // Apparently it is 64-bit and isn't shifted. |
| 4 | // |
| 5 | // Build: x86_64-gcc -static -Wall -ox32_mmap x32_mmap.c |
| 6 | // Typical output: |
| 7 | // 7f9390696000-7f93906a6000 r--s 12345670000 08:06 2224545 /etc/passwd |
| 8 | // ^^^^^^^^^^^ |
| 9 | #define _GNU_SOURCE |
| 10 | #include <sys/types.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <unistd.h> |
| 13 | #include <errno.h> |
| 14 | #include <sys/mman.h> |
| 15 | #include <sys/stat.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <stdio.h> |
| 18 | #include <sys/syscall.h> |
| 19 | // Ensure we are compiling to 64 bits |
| 20 | struct bug { int t[sizeof(long) > 4 ? 1 : -1]; }; |
| 21 | int main(int argc, char **argv) |
| 22 | { |
| 23 | long ofs = 0x12345670000; // fails if not page-aligned |
| 24 | errno = 0; |
| 25 | close(0); |
| 26 | if (open("/etc/passwd", O_RDONLY)) |
| 27 | return 1; |
| 28 | long r = syscall( |
| 29 | (long) (__NR_mmap | 0x40000000), // make x32 call |
| 30 | (long) (0), // start |
| 31 | (long) (0x10000), // len |
| 32 | (long) (PROT_READ), // prot |
| 33 | (long) (MAP_SHARED), // flags |
| 34 | (long) (0), // fd |
| 35 | (long) (ofs) // ofs |
| 36 | ); |
| 37 | printf("ret:0x%lx errno:%m\n", r); |
| 38 | |
| 39 | char buf[16*1024]; |
| 40 | sprintf(buf, "/proc/%d/maps", getpid()); |
| 41 | int fd = open(buf, O_RDONLY); |
| 42 | if (fd > 0) { |
| 43 | int sz = read(fd, buf, sizeof(buf)); |
| 44 | if (sz > 0) |
| 45 | write(1, buf, sz); |
| 46 | } |
| 47 | |
| 48 | return 0; |
| 49 | } |