]> granicus.if.org Git - strace/blob - tests/mmap.c
Update copyright headers
[strace] / tests / mmap.c
1 /*
2  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
3  * Copyright (c) 2015-2018 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 #if XLAT_RAW
62         printf("%s(NULL, 0, %#x, %#x, 0, 0) = -1 EBADF (%m)\n",
63                name, PROT_NONE, MAP_FILE);
64 #elif XLAT_VERBOSE
65         printf("%s(NULL, 0, %#x /* PROT_NONE */, %#x /* MAP_FILE */, 0, 0) "
66                "= -1 EBADF (%m)\n",
67                name, PROT_NONE, MAP_FILE);
68 #else
69         printf("%s(NULL, 0, PROT_NONE, MAP_FILE, 0, 0) = -1 EBADF (%m)\n",
70                name);
71 #endif
72         mmap(NULL, 0, PROT_NONE, MAP_FILE, 0, 0);
73
74         p = mmap(addr, length1, PROT_READ | PROT_WRITE,
75                  MAP_PRIVATE | MAP_ANONYMOUS, fd, offset);
76         if (MAP_FAILED == p)
77                 perror_msg_and_fail("mmap");
78 #if XLAT_RAW
79         printf("%s(%p, %lu, %#x, "
80                "%#x|%#x, %d, %#jx) = %p\n",
81                name, addr, length1, PROT_READ | PROT_WRITE, MAP_PRIVATE,
82                MAP_ANONYMOUS, fd, uoffset, p);
83 #elif XLAT_VERBOSE
84         printf("%s(%p, %lu, %#x /* PROT_READ|PROT_WRITE */, "
85                "%#x /* MAP_PRIVATE */|%#x /* MAP_ANONYMOUS */, %d, %#jx) "
86                "= %p\n",
87                name, addr, length1, PROT_READ | PROT_WRITE, MAP_PRIVATE,
88                MAP_ANONYMOUS, fd, uoffset, p);
89 #else
90         printf("%s(%p, %lu, PROT_READ|PROT_WRITE, "
91                "MAP_PRIVATE|MAP_ANONYMOUS, %d, %#jx) = %p\n",
92                name, addr, length1, fd, uoffset, p);
93 #endif
94
95         if (msync(p, length1, MS_SYNC))
96                 perror_msg_and_fail("msync");
97 #if XLAT_RAW
98         printf("msync(%p, %lu, %#x) = 0\n", p, length1, MS_SYNC);
99 #elif XLAT_VERBOSE
100         printf("msync(%p, %lu, %#x /* MS_SYNC */) = 0\n", p, length1, MS_SYNC);
101 #else
102         printf("msync(%p, %lu, MS_SYNC) = 0\n", p, length1);
103 #endif
104
105         if (mprotect(p, length1, PROT_NONE))
106                 perror_msg_and_fail("mprotect");
107 #if XLAT_RAW
108         printf("mprotect(%p, %lu, %#x) = 0\n", p, length1, PROT_NONE);
109 #elif XLAT_VERBOSE
110         printf("mprotect(%p, %lu, %#x /* PROT_NONE */) = 0\n",
111                p, length1, PROT_NONE);
112 #else
113         printf("mprotect(%p, %lu, PROT_NONE) = 0\n", p, length1);
114 #endif
115
116         addr = mremap(p, length1, length2, 0);
117         if (MAP_FAILED == addr)
118                 perror_msg_and_fail("mremap");
119         printf("mremap(%p, %lu, %lu, 0) = %p\n", p, length1, length2, addr);
120
121         p =  mremap(addr, length2, length3, MREMAP_MAYMOVE | MREMAP_FIXED,
122                     addr + length2);
123         if (MAP_FAILED == p)
124                 perror_msg_and_fail("mremap");
125 #if XLAT_RAW
126         printf("mremap(%p, %lu, %lu, %#x, %p) = %p\n",
127                addr, length2, length3, MREMAP_MAYMOVE | MREMAP_FIXED,
128                addr + length2, p);
129 #elif XLAT_VERBOSE
130         printf("mremap(%p, %lu, %lu, %#x /* MREMAP_MAYMOVE|MREMAP_FIXED */"
131                ", %p) = %p\n",
132                addr, length2, length3, MREMAP_MAYMOVE | MREMAP_FIXED,
133                addr + length2, p);
134 #else
135         printf("mremap(%p, %lu, %lu, MREMAP_MAYMOVE|MREMAP_FIXED"
136                ", %p) = %p\n", addr, length2, length3, addr + length2, p);
137 #endif
138
139         if (munmap(p, length3))
140                 perror_msg_and_fail("munmap");
141         printf("munmap(%p, %lu) = 0\n", p, length3);
142
143         if (mlockall(MCL_FUTURE))
144                 perror_msg_and_fail("mlockall");
145 #if XLAT_RAW
146         printf("mlockall(%#x) = 0\n", MCL_FUTURE);
147 #elif XLAT_VERBOSE
148         printf("mlockall(%#x /* MCL_FUTURE */) = 0\n", MCL_FUTURE);
149 #else
150         puts("mlockall(MCL_FUTURE) = 0");
151 #endif
152
153         puts("+++ exited with 0 +++");
154         return 0;
155 }