• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define _GNU_SOURCE
77 #include <errno.h>
78 #include <unistd.h>
79 #include <fcntl.h>
80 #include <sys/mman.h>
81 
82 #include "test.h"
83 
84 char *TCID = "mremap03";
85 int TST_TOTAL = 1;
86 static char *bad_addr;
87 static char *addr;		/* addr of memory mapped region */
88 int memsize;			/* memory mapped size */
89 int newsize;			/* new size of virtual memory block */
90 
91 void setup();			/* Main setup function of test */
92 void cleanup();			/* cleanup function for the test */
93 
main(int ac,char ** av)94 int main(int ac, char **av)
95 {
96 	int lc;
97 
98 	tst_parse_opts(ac, av, NULL, NULL);
99 
100 	setup();
101 
102 	for (lc = 0; TEST_LOOPING(lc); lc++) {
103 
104 		tst_count = 0;
105 
106 		/*
107 		 * Attempt to expand the existing mapped
108 		 * memory region (memsize) by newsize limits
109 		 * using mremap() should fail as specified old
110 		 * virtual address was not mapped.
111 		 */
112 		errno = 0;
113 		addr = mremap(bad_addr, memsize, newsize, MREMAP_MAYMOVE);
114 		TEST_ERRNO = errno;
115 
116 		/* Check for the return value of mremap() */
117 		if (addr != MAP_FAILED) {
118 			tst_resm(TFAIL,
119 				 "mremap returned invalid value, expected: -1");
120 
121 			/* Unmap the mapped memory region */
122 			if (munmap(addr, newsize) != 0) {
123 				tst_brkm(TFAIL, cleanup, "munmap fails to "
124 					 "unmap the expanded memory region, "
125 					 " error=%d", errno);
126 			}
127 			continue;
128 		}
129 
130 		/* Check for the expected errno */
131 		if (errno == EFAULT) {
132 			tst_resm(TPASS, "mremap() Fails, 'old region not "
133 				 "mapped', errno %d", TEST_ERRNO);
134 		} else {
135 			tst_resm(TFAIL, "mremap() Fails, "
136 				 "'Unexpected errno %d", TEST_ERRNO);
137 		}
138 	}
139 
140 	cleanup();
141 	tst_exit();
142 
143 }
144 
145 /*
146  * setup() - performs all ONE TIME setup for this test.
147  *
148  * Get system page size.
149  * Set the old address point some high address which is not mapped.
150  */
setup(void)151 void setup(void)
152 {
153 	int page_sz;		/* system page size */
154 
155 	tst_sig(FORK, DEF_HANDLER, cleanup);
156 
157 	TEST_PAUSE;
158 
159 	/* Get the system page size */
160 	if ((page_sz = getpagesize()) < 0) {
161 		tst_brkm(TFAIL, NULL,
162 			 "getpagesize() fails to get system page size");
163 	}
164 
165 	/* Get the size of virtual memory area to be mapped */
166 	memsize = (1000 * page_sz);
167 
168 	/* Get the New size of virtual memory block after resize */
169 	newsize = (memsize * 2);
170 
171 	/*
172 	 * Set the old virtual address point to some address
173 	 * which is not mapped.
174 	 */
175 	bad_addr = tst_get_bad_addr(cleanup);
176 }
177 
178 /*
179  * cleanup() - performs all ONE TIME cleanup for this test at
180  *             completion or premature exit.
181  */
cleanup(void)182 void cleanup(void)
183 {
184 
185 	/* Exit the program */
186 
187 }
188