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