blob: e2ff71631d665c8b03a20f9e06f000c0a2f0bd1c [file] [log] [blame]
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00001/*
Dmitry V. Levinbde121b2016-01-05 23:07:59 +00002 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00003 * 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. Levin0c8853c2016-01-02 13:28:43 +000028#include "tests.h"
Dmitry V. Levine7d671b2015-03-19 22:03:32 +000029#include <stdio.h>
30#include <stdint.h>
31#include <unistd.h>
32#include <limits.h>
33#include <sys/mman.h>
34
35int
Dmitry V. Levin0aae1672016-02-12 01:56:10 +000036main(int ac, char **av)
Dmitry V. Levine7d671b2015-03-19 22:03:32 +000037{
Dmitry V. Levin0aae1672016-02-12 01:56:10 +000038 const char *const name = ac > 1 ? av[1] : "mmap";
Dmitry V. Levinbde121b2016-01-05 23:07:59 +000039 const intmax_t pagesize = get_page_size();
Dmitry V. Levin0aae1672016-02-12 01:56:10 +000040 const unsigned long length1 = pagesize * 6;
41 const unsigned long length2 = pagesize * 3;
42 const unsigned long length3 = pagesize * 2;
Dmitry V. Levine7d671b2015-03-19 22:03:32 +000043 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. Levin0aae1672016-02-12 01:56:10 +000054 const uintmax_t uoffset =
55 sizeof(offset) == sizeof(int) ? (uintmax_t) (unsigned int) offset
56 : (uintmax_t) offset;
Dmitry V. Levine7d671b2015-03-19 22:03:32 +000057
Dmitry V. Levin0aae1672016-02-12 01:56:10 +000058 (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. Levine7d671b2015-03-19 22:03:32 +000065 MAP_PRIVATE | MAP_ANONYMOUS, fd, offset);
Dmitry V. Levinbde121b2016-01-05 23:07:59 +000066 if (MAP_FAILED == p)
67 perror_msg_and_fail("mmap");
Dmitry V. Levin0aae1672016-02-12 01:56:10 +000068 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. Levine7d671b2015-03-19 22:03:32 +000071
Dmitry V. Levin0aae1672016-02-12 01:56:10 +000072 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. Levine7d671b2015-03-19 22:03:32 +0000105 return 0;
106}