1 /*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /*
21 * Test Name: mremap03
22 *
23 * Test Description:
24 * Verify that,
25 * mremap() fails when used to expand the existing virtual memory mapped
26 * region to the requested size, if there already exists mappings that
27 * cover the whole address space requsted or the old address specified was
28 * not mapped.
29 *
30 * Expected Result:
31 * mremap() should return -1 and set errno to EFAULT.
32 *
33 * Algorithm:
34 * Setup:
35 * Setup signal handling.
36 * Pause for SIGUSR1 if option specified.
37 *
38 * Test:
39 * Loop if the proper options are given.
40 * Execute system call
41 * Check return code, if system call failed (return=-1)
42 * if errno set == expected errno
43 * Issue sys call fails with expected return value and errno.
44 * Otherwise,
45 * Issue sys call fails with unexpected errno.
46 * Otherwise,
47 * Issue sys call returns unexpected value.
48 *
49 * Cleanup:
50 * Print errno log and/or timing stats if options given
51 *
52 * Usage: <for command-line>
53 * mremap03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
54 * where, -c n : Run n copies concurrently.
55 * -e : Turn on errno logging.
56 * -i n : Execute test n times.
57 * -I x : Execute test for x seconds.
58 * -p x : Pause for x seconds between iterations.
59 * -t : Turn on syscall timing.
60 *
61 * HISTORY
62 * 07/2001 Ported by Wayne Boyer
63 *
64 * 11/09/2001 Manoj Iyer (manjo@austin.ibm.com)
65 * Modified.
66 * - #include <linux/mman.h> should not be included as per man page for
67 * mremap, #include <sys/mman.h> alone should do the job. But inorder
68 * to include definition of MREMAP_MAYMOVE defined in bits/mman.h
69 * (included by sys/mman.h) __USE_GNU needs to be defined.
70 * There may be a more elegant way of doing this...
71 *
72 *
73 * RESTRICTIONS:
74 * None.
75 */
76 #include <errno.h>
77 #include <unistd.h>
78 #include <fcntl.h>
79 #define __USE_GNU
80 #include <sys/mman.h>
81 #undef __USE_GNU
82
83 #include "test.h"
84
85 char *TCID = "mremap03";
86 int TST_TOTAL = 1;
87 static char *bad_addr;
88 static char *addr; /* addr of memory mapped region */
89 int memsize; /* memory mapped size */
90 int newsize; /* new size of virtual memory block */
91
92 void setup(); /* Main setup function of test */
93 void cleanup(); /* cleanup function for the test */
94
main(int ac,char ** av)95 int main(int ac, char **av)
96 {
97 int lc;
98
99 tst_parse_opts(ac, av, NULL, NULL);
100
101 setup();
102
103 for (lc = 0; TEST_LOOPING(lc); lc++) {
104
105 tst_count = 0;
106
107 /*
108 * Attempt to expand the existing mapped
109 * memory region (memsize) by newsize limits
110 * using mremap() should fail as specified old
111 * virtual address was not mapped.
112 */
113 errno = 0;
114 addr = mremap(bad_addr, memsize, newsize, MREMAP_MAYMOVE);
115 TEST_ERRNO = errno;
116
117 /* Check for the return value of mremap() */
118 if (addr != MAP_FAILED) {
119 tst_resm(TFAIL,
120 "mremap returned invalid value, expected: -1");
121
122 /* Unmap the mapped memory region */
123 if (munmap(addr, newsize) != 0) {
124 tst_brkm(TFAIL, cleanup, "munmap fails to "
125 "unmap the expanded memory region, "
126 " error=%d", errno);
127 }
128 continue;
129 }
130
131 /* Check for the expected errno */
132 if (errno == EFAULT) {
133 tst_resm(TPASS, "mremap() Fails, 'old region not "
134 "mapped', errno %d", TEST_ERRNO);
135 } else {
136 tst_resm(TFAIL, "mremap() Fails, "
137 "'Unexpected errno %d", TEST_ERRNO);
138 }
139 }
140
141 cleanup();
142 tst_exit();
143
144 }
145
146 /*
147 * setup() - performs all ONE TIME setup for this test.
148 *
149 * Get system page size.
150 * Set the old address point some high address which is not mapped.
151 */
setup(void)152 void setup(void)
153 {
154 int page_sz; /* system page size */
155
156 tst_sig(FORK, DEF_HANDLER, cleanup);
157
158 TEST_PAUSE;
159
160 /* Get the system page size */
161 if ((page_sz = getpagesize()) < 0) {
162 tst_brkm(TFAIL, NULL,
163 "getpagesize() fails to get system page size");
164 }
165
166 /* Get the size of virtual memory area to be mapped */
167 memsize = (1000 * page_sz);
168
169 /* Get the New size of virtual memory block after resize */
170 newsize = (memsize * 2);
171
172 /*
173 * Set the old virtual address point to some address
174 * which is not mapped.
175 */
176 bad_addr = tst_get_bad_addr(cleanup);
177 }
178
179 /*
180 * cleanup() - performs all ONE TIME cleanup for this test at
181 * completion or premature exit.
182 */
cleanup(void)183 void cleanup(void)
184 {
185
186 /* Exit the program */
187
188 }
189