Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 1 | /* |
Dmitry V. Levin | bde121b | 2016-01-05 23:07:59 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org> |
Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 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. |
| 15 | * |
| 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. |
| 26 | */ |
| 27 | |
Dmitry V. Levin | 0c8853c | 2016-01-02 13:28:43 +0000 | [diff] [blame] | 28 | #include "tests.h" |
Dmitry V. Levin | e7d671b | 2015-03-19 22:03:32 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | #include <stdint.h> |
| 31 | #include <unistd.h> |
| 32 | #include <limits.h> |
| 33 | #include <sys/mman.h> |
| 34 | |
| 35 | int |
Dmitry V. Levin | 0aae167 | 2016-02-12 01:56:10 +0000 | [diff] [blame] | 36 | main(int ac, char **av) |
Dmitry V. Levin | e7d671b | 2015-03-19 22:03:32 +0000 | [diff] [blame] | 37 | { |
Dmitry V. Levin | 0aae167 | 2016-02-12 01:56:10 +0000 | [diff] [blame] | 38 | const char *const name = ac > 1 ? av[1] : "mmap"; |
Dmitry V. Levin | bde121b | 2016-01-05 23:07:59 +0000 | [diff] [blame] | 39 | const intmax_t pagesize = get_page_size(); |
Dmitry V. Levin | 0aae167 | 2016-02-12 01:56:10 +0000 | [diff] [blame] | 40 | const unsigned long length1 = pagesize * 6; |
| 41 | const unsigned long length2 = pagesize * 3; |
| 42 | const unsigned long length3 = pagesize * 2; |
Dmitry V. Levin | e7d671b | 2015-03-19 22:03:32 +0000 | [diff] [blame] | 43 | const int fd = -1; |
| 44 | off_t offset; |
| 45 | void *addr, *p; |
| 46 | |
| 47 | #if ULONG_MAX > 4294967295UL |
| 48 | offset = 0xcafedeadbeef000 & -pagesize; |
| 49 | addr = (void *) (uintmax_t) (0xfacefeed000 & -pagesize); |
| 50 | #else |
| 51 | offset = 0xdeadbeef000 & -pagesize; |
| 52 | addr = (void *) (unsigned int) (0xfaced000 & -pagesize); |
| 53 | #endif |
Dmitry V. Levin | 0aae167 | 2016-02-12 01:56:10 +0000 | [diff] [blame] | 54 | const uintmax_t uoffset = |
| 55 | sizeof(offset) == sizeof(int) ? (uintmax_t) (unsigned int) offset |
| 56 | : (uintmax_t) offset; |
Dmitry V. Levin | e7d671b | 2015-03-19 22:03:32 +0000 | [diff] [blame] | 57 | |
Dmitry V. Levin | 0aae167 | 2016-02-12 01:56:10 +0000 | [diff] [blame] | 58 | (void) close(0); |
| 59 | (void) close(0); |
| 60 | printf("%s(NULL, 0, PROT_NONE, MAP_FILE, 0, 0) = -1 EBADF (%m)\n", |
| 61 | name); |
| 62 | mmap(NULL, 0, PROT_NONE, MAP_FILE, 0, 0); |
| 63 | |
| 64 | p = mmap(addr, length1, PROT_READ | PROT_WRITE, |
Dmitry V. Levin | e7d671b | 2015-03-19 22:03:32 +0000 | [diff] [blame] | 65 | MAP_PRIVATE | MAP_ANONYMOUS, fd, offset); |
Dmitry V. Levin | bde121b | 2016-01-05 23:07:59 +0000 | [diff] [blame] | 66 | if (MAP_FAILED == p) |
| 67 | perror_msg_and_fail("mmap"); |
Dmitry V. Levin | 0aae167 | 2016-02-12 01:56:10 +0000 | [diff] [blame] | 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); |
Dmitry V. Levin | e7d671b | 2015-03-19 22:03:32 +0000 | [diff] [blame] | 71 | |
Dmitry V. Levin | 0aae167 | 2016-02-12 01:56:10 +0000 | [diff] [blame] | 72 | if (msync(p, length1, MS_SYNC)) |
| 73 | perror_msg_and_fail("msync"); |
| 74 | printf("msync(%p, %lu, MS_SYNC) = 0\n", p, length1); |
| 75 | |
| 76 | if (mprotect(p, length1, PROT_NONE)) |
| 77 | perror_msg_and_fail("mprotect"); |
| 78 | printf("mprotect(%p, %lu, PROT_NONE) = 0\n", p, length1); |
| 79 | |
| 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); |
| 84 | |
| 85 | p = mremap(addr, length2, length3, MREMAP_MAYMOVE | MREMAP_FIXED, |
| 86 | addr + length2); |
| 87 | if (MAP_FAILED == p) |
| 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); |
| 91 | |
| 92 | if (madvise(p, length3, MADV_NORMAL)) |
| 93 | perror_msg_and_fail("madvise"); |
| 94 | printf("madvise(%p, %lu, MADV_NORMAL) = 0\n", p, length3); |
| 95 | |
| 96 | if (munmap(p, length3)) |
| 97 | perror_msg_and_fail("munmap"); |
| 98 | printf("munmap(%p, %lu) = 0\n", p, length3); |
| 99 | |
| 100 | if (mlockall(MCL_FUTURE)) |
| 101 | perror_msg_and_fail("mlockall"); |
| 102 | puts("mlockall(MCL_FUTURE) = 0"); |
| 103 | |
| 104 | puts("+++ exited with 0 +++"); |
Dmitry V. Levin | e7d671b | 2015-03-19 22:03:32 +0000 | [diff] [blame] | 105 | return 0; |
| 106 | } |