2 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 main(int ac, char **av)
38 const char *const name = ac > 1 ? av[1] : "mmap";
39 const intmax_t pagesize = get_page_size();
40 const unsigned long length1 = pagesize * 6;
41 const unsigned long length2 = pagesize * 3;
42 const unsigned long length3 = pagesize * 2;
47 #if ULONG_MAX > 4294967295UL
48 offset = 0xcafedeadbeef000ULL & -pagesize;
49 addr = (void *) (uintmax_t) (0xfacefeed000 & -pagesize);
51 offset = 0xdeadbeef000ULL & -pagesize;
52 addr = (void *) (unsigned int) (0xfaced000 & -pagesize);
54 const uintmax_t uoffset =
55 sizeof(offset) == sizeof(int) ? (uintmax_t) (unsigned int) offset
60 printf("%s(NULL, 0, PROT_NONE, MAP_FILE, 0, 0) = -1 EBADF (%m)\n",
62 mmap(NULL, 0, PROT_NONE, MAP_FILE, 0, 0);
64 p = mmap(addr, length1, PROT_READ | PROT_WRITE,
65 MAP_PRIVATE | MAP_ANONYMOUS, fd, offset);
67 perror_msg_and_fail("mmap");
68 printf("%s(%p, %lu, PROT_READ|PROT_WRITE, "
69 "MAP_PRIVATE|MAP_ANONYMOUS, %d, %#jx) = %p\n",
70 name, addr, length1, fd, uoffset, p);
72 if (msync(p, length1, MS_SYNC))
73 perror_msg_and_fail("msync");
74 printf("msync(%p, %lu, MS_SYNC) = 0\n", p, length1);
76 if (mprotect(p, length1, PROT_NONE))
77 perror_msg_and_fail("mprotect");
78 printf("mprotect(%p, %lu, PROT_NONE) = 0\n", p, length1);
80 addr = mremap(p, length1, length2, 0);
81 if (MAP_FAILED == addr)
82 perror_msg_and_fail("mremap");
83 printf("mremap(%p, %lu, %lu, 0) = %p\n", p, length1, length2, addr);
85 p = mremap(addr, length2, length3, MREMAP_MAYMOVE | MREMAP_FIXED,
88 perror_msg_and_fail("mremap");
89 printf("mremap(%p, %lu, %lu, MREMAP_MAYMOVE|MREMAP_FIXED"
90 ", %p) = %p\n", addr, length2, length3, addr + length2, p);
92 if (madvise(p, length3, MADV_NORMAL))
93 perror_msg_and_fail("madvise");
94 printf("madvise(%p, %lu, MADV_NORMAL) = 0\n", p, length3);
96 if (munmap(p, length3))
97 perror_msg_and_fail("munmap");
98 printf("munmap(%p, %lu) = 0\n", p, length3);
100 if (mlockall(MCL_FUTURE))
101 perror_msg_and_fail("mlockall");
102 puts("mlockall(MCL_FUTURE) = 0");
104 puts("+++ exited with 0 +++");