blob: 6b1827c4ae3512022aff9cf3e77cdedfc8b5101e [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * Test Name: mremap03
22 *
23 * Test Description:
subrata_modak4bb656a2009-02-26 12:02:09 +000024 * Verify that,
25 * mremap() fails when used to expand the existing virtual memory mapped
plars865695b2001-08-27 22:15:12 +000026 * 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)
subrata_modak56207ce2009-03-23 13:35:39 +000042 * if errno set == expected errno
43 * Issue sys call fails with expected return value and errno.
44 * Otherwise,
plars865695b2001-08-27 22:15:12 +000045 * 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 *
iyermanoj874176f2001-11-09 07:07:02 +000064 * 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 *
plars865695b2001-08-27 22:15:12 +000073 * RESTRICTIONS:
74 * None.
75 */
76#include <errno.h>
77#include <unistd.h>
78#include <fcntl.h>
iyermanoj874176f2001-11-09 07:07:02 +000079#define __USE_GNU
plars865695b2001-08-27 22:15:12 +000080#include <sys/mman.h>
iyermanoj874176f2001-11-09 07:07:02 +000081#undef __USE_GNU
plars865695b2001-08-27 22:15:12 +000082
83#include "test.h"
plars865695b2001-08-27 22:15:12 +000084
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020085char *TCID = "mremap03";
86int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000087char *addr; /* addr of memory mapped region */
88int memsize; /* memory mapped size */
89int newsize; /* new size of virtual memory block */
plars865695b2001-08-27 22:15:12 +000090
91void setup(); /* Main setup function of test */
92void cleanup(); /* cleanup function for the test */
93
robbiewd34d5812005-07-11 22:28:09 +000094#if !defined(UCLINUX)
subrata_modak56207ce2009-03-23 13:35:39 +000095int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000096{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020097 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020098 const char *msg;
subrata_modakbdbaec52009-02-26 12:14:51 +000099
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800100 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800101 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000102
plars865695b2001-08-27 22:15:12 +0000103 setup();
104
plars865695b2001-08-27 22:15:12 +0000105 for (lc = 0; TEST_LOOPING(lc); lc++) {
106
Caspar Zhangd59a6592013-03-07 14:59:12 +0800107 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000108
subrata_modak4bb656a2009-02-26 12:02:09 +0000109 /*
110 * Attempt to expand the existing mapped
plars865695b2001-08-27 22:15:12 +0000111 * memory region (memsize) by newsize limits
112 * using mremap() should fail as specifed old
113 * virtual address was not mapped.
114 */
robbiewd5c21122002-03-22 16:22:40 +0000115 errno = 0;
116 addr = mremap(addr, memsize, newsize, MREMAP_MAYMOVE);
117 TEST_ERRNO = errno;
plars865695b2001-08-27 22:15:12 +0000118
119 /* Check for the return value of mremap() */
robbiewd5c21122002-03-22 16:22:40 +0000120 if (addr != MAP_FAILED) {
plars865695b2001-08-27 22:15:12 +0000121 tst_resm(TFAIL,
122 "mremap returned invalid value, expected: -1");
123
124 /* Unmap the mapped memory region */
robbiewd5c21122002-03-22 16:22:40 +0000125 if (munmap(addr, newsize) != 0) {
plars865695b2001-08-27 22:15:12 +0000126 tst_brkm(TFAIL, cleanup, "munmap fails to "
127 "unmap the expanded memory region, "
128 " error=%d", errno);
129 }
130 continue;
131 }
132
plars865695b2001-08-27 22:15:12 +0000133 /* Check for the expected errno */
134 if (errno == EFAULT) {
135 tst_resm(TPASS, "mremap() Fails, 'old region not "
136 "mapped', errno %d", TEST_ERRNO);
137 } else {
138 tst_resm(TFAIL, "mremap() Fails, "
139 "'Unexpected errno %d", TEST_ERRNO);
140 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800141 }
subrata_modak56207ce2009-03-23 13:35:39 +0000142
plars865695b2001-08-27 22:15:12 +0000143 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800144 tst_exit();
plars865695b2001-08-27 22:15:12 +0000145
Garrett Cooper2c282152010-12-16 00:55:50 -0800146}
plars865695b2001-08-27 22:15:12 +0000147
148/*
149 * setup() - performs all ONE TIME setup for this test.
150 *
151 * Get system page size.
152 * Set the old address point some high address which is not mapped.
153 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400154void setup(void)
plars865695b2001-08-27 22:15:12 +0000155{
subrata_modak56207ce2009-03-23 13:35:39 +0000156 int page_sz; /* system page size */
plars865695b2001-08-27 22:15:12 +0000157
plars865695b2001-08-27 22:15:12 +0000158 tst_sig(FORK, DEF_HANDLER, cleanup);
159
plars865695b2001-08-27 22:15:12 +0000160 TEST_PAUSE;
161
162 /* Get the system page size */
163 if ((page_sz = getpagesize()) < 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800164 tst_brkm(TFAIL, NULL,
plars865695b2001-08-27 22:15:12 +0000165 "getpagesize() fails to get system page size");
166 }
167
168 /* Get the size of virtual memory area to be mapped */
169 memsize = (1000 * page_sz);
170
171 /* Get the New size of virtual memory block after resize */
172 newsize = (memsize * 2);
173
174 /*
175 * Set the old virtual address point to some address
176 * which is not mapped.
177 */
178 addr = (char *)get_high_address();
179}
180
181/*
182 * cleanup() - performs all ONE TIME cleanup for this test at
183 * completion or premature exit.
184 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400185void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000186{
plars865695b2001-08-27 22:15:12 +0000187
188 /* Exit the program */
Garrett Cooper2c282152010-12-16 00:55:50 -0800189
plars865695b2001-08-27 22:15:12 +0000190}
robbiewd34d5812005-07-11 22:28:09 +0000191
192#else
193
Mike Frysingerc57fba52014-04-09 18:56:30 -0400194int main(void)
robbiewd34d5812005-07-11 22:28:09 +0000195{
vapier81a63072006-02-27 04:29:21 +0000196 tst_resm(TINFO, "test is not available on uClinux");
Garrett Cooper2c282152010-12-16 00:55:50 -0800197 tst_exit();
robbiewd34d5812005-07-11 22:28:09 +0000198}
199
Chris Dearmanec6edca2012-10-17 19:54:01 -0700200#endif /* if !defined(UCLINUX) */