blob: cbef36d3f3a9da09178cd39e5e8625b332860e72 [file] [log] [blame]
Elliott Hughes5dec78d2014-02-26 15:56:23 -08001// 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
20struct bug { int t[sizeof(long) > 4 ? 1 : -1]; };
21int 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}