xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mremap/mremap02.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: mremap02
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 the virtual memory area previously
27*49cdfc7eSAndroid Build Coastguard Worker  *   mapped was not page aligned or invalid argument specified.
28*49cdfc7eSAndroid Build Coastguard Worker  *
29*49cdfc7eSAndroid Build Coastguard Worker  * Expected Result:
30*49cdfc7eSAndroid Build Coastguard Worker  *  mremap() should return -1 and set errno to EINVAL.
31*49cdfc7eSAndroid Build Coastguard Worker  *
32*49cdfc7eSAndroid Build Coastguard Worker  * Algorithm:
33*49cdfc7eSAndroid Build Coastguard Worker  *  Setup:
34*49cdfc7eSAndroid Build Coastguard Worker  *   Setup signal handling.
35*49cdfc7eSAndroid Build Coastguard Worker  *   Pause for SIGUSR1 if option specified.
36*49cdfc7eSAndroid Build Coastguard Worker  *
37*49cdfc7eSAndroid Build Coastguard Worker  *  Test:
38*49cdfc7eSAndroid Build Coastguard Worker  *   Loop if the proper options are given.
39*49cdfc7eSAndroid Build Coastguard Worker  *   Execute system call
40*49cdfc7eSAndroid Build Coastguard Worker  *   Check return code, if system call failed (return=-1)
41*49cdfc7eSAndroid Build Coastguard Worker  *	if errno set == expected errno
42*49cdfc7eSAndroid Build Coastguard Worker  *		Issue sys call fails with expected return value and errno.
43*49cdfc7eSAndroid Build Coastguard Worker  *	Otherwise,
44*49cdfc7eSAndroid Build Coastguard Worker  *		Issue sys call fails with unexpected errno.
45*49cdfc7eSAndroid Build Coastguard Worker  *   Otherwise,
46*49cdfc7eSAndroid Build Coastguard Worker  *	Issue sys call returns unexpected value.
47*49cdfc7eSAndroid Build Coastguard Worker  *
48*49cdfc7eSAndroid Build Coastguard Worker  *  Cleanup:
49*49cdfc7eSAndroid Build Coastguard Worker  *   Print errno log and/or timing stats if options given
50*49cdfc7eSAndroid Build Coastguard Worker  *
51*49cdfc7eSAndroid Build Coastguard Worker  * Usage:  <for command-line>
52*49cdfc7eSAndroid Build Coastguard Worker  *  mremap02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
53*49cdfc7eSAndroid Build Coastguard Worker  *     where,  -c n : Run n copies concurrently.
54*49cdfc7eSAndroid Build Coastguard Worker  *	       -e   : Turn on errno logging.
55*49cdfc7eSAndroid Build Coastguard Worker  *	       -i n : Execute test n times.
56*49cdfc7eSAndroid Build Coastguard Worker  *	       -I x : Execute test for x seconds.
57*49cdfc7eSAndroid Build Coastguard Worker  *	       -p x : Pause for x seconds between iterations.
58*49cdfc7eSAndroid Build Coastguard Worker  *	       -t   : Turn on syscall timing.
59*49cdfc7eSAndroid Build Coastguard Worker  *
60*49cdfc7eSAndroid Build Coastguard Worker  * HISTORY
61*49cdfc7eSAndroid Build Coastguard Worker  *	07/2001 Ported by Wayne Boyer
62*49cdfc7eSAndroid Build Coastguard Worker  *
63*49cdfc7eSAndroid Build Coastguard Worker  *      11/09/2001 Manoj Iyer ([email protected])
64*49cdfc7eSAndroid Build Coastguard Worker  *      Modified.
65*49cdfc7eSAndroid Build Coastguard Worker  *      - #include <linux/mman.h> should not be included as per man page for
66*49cdfc7eSAndroid Build Coastguard Worker  *        mremap, #include <sys/mman.h> alone should do the job. But inorder
67*49cdfc7eSAndroid Build Coastguard Worker  *        to include definition of MREMAP_MAYMOVE defined in bits/mman.h
68*49cdfc7eSAndroid Build Coastguard Worker  *        (included by sys/mman.h) __USE_GNU needs to be defined.
69*49cdfc7eSAndroid Build Coastguard Worker  *        There may be a more elegant way of doing this...
70*49cdfc7eSAndroid Build Coastguard Worker  *
71*49cdfc7eSAndroid Build Coastguard Worker  *
72*49cdfc7eSAndroid Build Coastguard Worker  * RESTRICTIONS:
73*49cdfc7eSAndroid Build Coastguard Worker  *  None.
74*49cdfc7eSAndroid Build Coastguard Worker  */
75*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
76*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
77*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
78*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
79*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "mremap02";
84*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
85*49cdfc7eSAndroid Build Coastguard Worker char *addr;			/* addr of memory mapped region */
86*49cdfc7eSAndroid Build Coastguard Worker int memsize;			/* memory mapped size */
87*49cdfc7eSAndroid Build Coastguard Worker int newsize;			/* new size of virtual memory block */
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker void setup();			/* Main setup function of test */
90*49cdfc7eSAndroid Build Coastguard Worker void cleanup();			/* cleanup function for the test */
91*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)92*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
93*49cdfc7eSAndroid Build Coastguard Worker {
94*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker 	setup();
99*49cdfc7eSAndroid Build Coastguard Worker 
100*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
103*49cdfc7eSAndroid Build Coastguard Worker 
104*49cdfc7eSAndroid Build Coastguard Worker 		/*
105*49cdfc7eSAndroid Build Coastguard Worker 		 * Attempt to expand the existing mapped
106*49cdfc7eSAndroid Build Coastguard Worker 		 * memory region (memsize) by newsize limits using
107*49cdfc7eSAndroid Build Coastguard Worker 		 * mremap() should fail as old virtual address is not
108*49cdfc7eSAndroid Build Coastguard Worker 		 * page aligned.
109*49cdfc7eSAndroid Build Coastguard Worker 		 */
110*49cdfc7eSAndroid Build Coastguard Worker 		errno = 0;
111*49cdfc7eSAndroid Build Coastguard Worker 		addr = mremap(addr, memsize, newsize, MREMAP_MAYMOVE);
112*49cdfc7eSAndroid Build Coastguard Worker 		TEST_ERRNO = errno;
113*49cdfc7eSAndroid Build Coastguard Worker 
114*49cdfc7eSAndroid Build Coastguard Worker 		/* Check for the return value of mremap() */
115*49cdfc7eSAndroid Build Coastguard Worker 		if (addr != MAP_FAILED) {
116*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL,
117*49cdfc7eSAndroid Build Coastguard Worker 				 "mremap returned invalid value, expected: -1");
118*49cdfc7eSAndroid Build Coastguard Worker 
119*49cdfc7eSAndroid Build Coastguard Worker 			/* Unmap the mapped memory region */
120*49cdfc7eSAndroid Build Coastguard Worker 			if (munmap(addr, newsize) != 0) {
121*49cdfc7eSAndroid Build Coastguard Worker 				tst_brkm(TBROK, cleanup, "munmap fails to "
122*49cdfc7eSAndroid Build Coastguard Worker 					 "unmap the expanded memory region, "
123*49cdfc7eSAndroid Build Coastguard Worker 					 "error=%d", errno);
124*49cdfc7eSAndroid Build Coastguard Worker 			}
125*49cdfc7eSAndroid Build Coastguard Worker 			continue;
126*49cdfc7eSAndroid Build Coastguard Worker 		}
127*49cdfc7eSAndroid Build Coastguard Worker 
128*49cdfc7eSAndroid Build Coastguard Worker 		if (errno == EINVAL) {
129*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TPASS, "mremap() Failed, 'invalid argument "
130*49cdfc7eSAndroid Build Coastguard Worker 				 "specified' - errno %d", TEST_ERRNO);
131*49cdfc7eSAndroid Build Coastguard Worker 		} else {
132*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "mremap() Failed, "
133*49cdfc7eSAndroid Build Coastguard Worker 				 "'Unexpected errno %d", TEST_ERRNO);
134*49cdfc7eSAndroid Build Coastguard Worker 		}
135*49cdfc7eSAndroid Build Coastguard Worker 	}
136*49cdfc7eSAndroid Build Coastguard Worker 
137*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
138*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
139*49cdfc7eSAndroid Build Coastguard Worker 
140*49cdfc7eSAndroid Build Coastguard Worker }
141*49cdfc7eSAndroid Build Coastguard Worker 
142*49cdfc7eSAndroid Build Coastguard Worker /*
143*49cdfc7eSAndroid Build Coastguard Worker  * setup() - performs all ONE TIME setup for this test.
144*49cdfc7eSAndroid Build Coastguard Worker  *
145*49cdfc7eSAndroid Build Coastguard Worker  * Get system page size, Set the size of virtual memory area and the
146*49cdfc7eSAndroid Build Coastguard Worker  * new size after resize, Set the virtual memory address such that it
147*49cdfc7eSAndroid Build Coastguard Worker  * is not aligned.
148*49cdfc7eSAndroid Build Coastguard Worker  */
setup(void)149*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
150*49cdfc7eSAndroid Build Coastguard Worker {
151*49cdfc7eSAndroid Build Coastguard Worker 
152*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(FORK, DEF_HANDLER, cleanup);
153*49cdfc7eSAndroid Build Coastguard Worker 
154*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
155*49cdfc7eSAndroid Build Coastguard Worker 
156*49cdfc7eSAndroid Build Coastguard Worker 	/* Get the system page size */
157*49cdfc7eSAndroid Build Coastguard Worker 	if ((memsize = getpagesize()) < 0) {
158*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL, NULL,
159*49cdfc7eSAndroid Build Coastguard Worker 			 "getpagesize() fails to get system page size");
160*49cdfc7eSAndroid Build Coastguard Worker 	}
161*49cdfc7eSAndroid Build Coastguard Worker 
162*49cdfc7eSAndroid Build Coastguard Worker 	/* Get the New size of virtual memory block after resize */
163*49cdfc7eSAndroid Build Coastguard Worker 	newsize = (memsize * 2);
164*49cdfc7eSAndroid Build Coastguard Worker 
165*49cdfc7eSAndroid Build Coastguard Worker 	/* Set the old virtual memory address */
166*49cdfc7eSAndroid Build Coastguard Worker 	addr = (char *)(addr + (memsize - 1));
167*49cdfc7eSAndroid Build Coastguard Worker }
168*49cdfc7eSAndroid Build Coastguard Worker 
169*49cdfc7eSAndroid Build Coastguard Worker /*
170*49cdfc7eSAndroid Build Coastguard Worker  * cleanup() - performs all ONE TIME cleanup for this test at
171*49cdfc7eSAndroid Build Coastguard Worker  *             completion or premature exit.
172*49cdfc7eSAndroid Build Coastguard Worker  */
cleanup(void)173*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
174*49cdfc7eSAndroid Build Coastguard Worker {
175*49cdfc7eSAndroid Build Coastguard Worker 
176*49cdfc7eSAndroid Build Coastguard Worker 	/* Exit the program */
177*49cdfc7eSAndroid Build Coastguard Worker 
178*49cdfc7eSAndroid Build Coastguard Worker }
179