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