xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/munmap/munmap02.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: munmap02
22*49cdfc7eSAndroid Build Coastguard Worker  *
23*49cdfc7eSAndroid Build Coastguard Worker  * Test Description:
24*49cdfc7eSAndroid Build Coastguard Worker  *  Verify that, munmap call will succeed to unmap a mapped file or
25*49cdfc7eSAndroid Build Coastguard Worker  *  anonymous shared memory region from the calling process's address space
26*49cdfc7eSAndroid Build Coastguard Worker  *  if the region specified by the address and the length is part or all of
27*49cdfc7eSAndroid Build Coastguard Worker  *  the mapped region.
28*49cdfc7eSAndroid Build Coastguard Worker  *
29*49cdfc7eSAndroid Build Coastguard Worker  * Expected Result:
30*49cdfc7eSAndroid Build Coastguard Worker  *  munmap call should succeed to unmap a part or all of mapped region of a
31*49cdfc7eSAndroid Build Coastguard Worker  *  file or anonymous shared memory from the process's address space and it
32*49cdfc7eSAndroid Build Coastguard Worker  *  returns with a value 0,
33*49cdfc7eSAndroid Build Coastguard Worker  *  further reference to the unmapped region should result in a segmentation
34*49cdfc7eSAndroid Build Coastguard Worker  *  fault (SIGSEGV).
35*49cdfc7eSAndroid Build Coastguard Worker  *
36*49cdfc7eSAndroid Build Coastguard Worker  * Algorithm:
37*49cdfc7eSAndroid Build Coastguard Worker  *  Setup:
38*49cdfc7eSAndroid Build Coastguard Worker  *   Setup signal handling.
39*49cdfc7eSAndroid Build Coastguard Worker  *   Create temporary directory.
40*49cdfc7eSAndroid Build Coastguard Worker  *   Pause for SIGUSR1 if option specified.
41*49cdfc7eSAndroid Build Coastguard Worker  *
42*49cdfc7eSAndroid Build Coastguard Worker  *  Test:
43*49cdfc7eSAndroid Build Coastguard Worker  *   Loop if the proper options are given.
44*49cdfc7eSAndroid Build Coastguard Worker  *   Execute system call
45*49cdfc7eSAndroid Build Coastguard Worker  *   Check return code, if system call failed (return=-1)
46*49cdfc7eSAndroid Build Coastguard Worker  *   	Log the errno and Issue a FAIL message.
47*49cdfc7eSAndroid Build Coastguard Worker  *   Otherwise,
48*49cdfc7eSAndroid Build Coastguard Worker  *   	Verify the Functionality of system call
49*49cdfc7eSAndroid Build Coastguard Worker  *      if successful,
50*49cdfc7eSAndroid Build Coastguard Worker  *      	Issue Functionality-Pass message.
51*49cdfc7eSAndroid Build Coastguard Worker  *      Otherwise,
52*49cdfc7eSAndroid Build Coastguard Worker  *		Issue Functionality-Fail message.
53*49cdfc7eSAndroid Build Coastguard Worker  *  Cleanup:
54*49cdfc7eSAndroid Build Coastguard Worker  *   Print errno log and/or timing stats if options given
55*49cdfc7eSAndroid Build Coastguard Worker  *   Delete the temporary directory created.
56*49cdfc7eSAndroid Build Coastguard Worker  *
57*49cdfc7eSAndroid Build Coastguard Worker  * Usage:  <for command-line>
58*49cdfc7eSAndroid Build Coastguard Worker  *  munmap01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
59*49cdfc7eSAndroid Build Coastguard Worker  *     where,  -c n : Run n copies concurrently.
60*49cdfc7eSAndroid Build Coastguard Worker  *             -f   : Turn off functionality Testing.
61*49cdfc7eSAndroid Build Coastguard Worker  *	       -i n : Execute test n times.
62*49cdfc7eSAndroid Build Coastguard Worker  *	       -I x : Execute test for x seconds.
63*49cdfc7eSAndroid Build Coastguard Worker  *	       -P x : Pause for x seconds between iterations.
64*49cdfc7eSAndroid Build Coastguard Worker  *	       -t   : Turn on syscall timing.
65*49cdfc7eSAndroid Build Coastguard Worker  *
66*49cdfc7eSAndroid Build Coastguard Worker  * HISTORY
67*49cdfc7eSAndroid Build Coastguard Worker  *	07/2001 Ported by Wayne Boyer
68*49cdfc7eSAndroid Build Coastguard Worker  *
69*49cdfc7eSAndroid Build Coastguard Worker  * RESTRICTIONS:
70*49cdfc7eSAndroid Build Coastguard Worker  *  None.
71*49cdfc7eSAndroid Build Coastguard Worker  */
72*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
73*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
74*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
75*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
76*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
79*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker #define TEMPFILE	"mmapfile"
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "munmap02";
84*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker static size_t page_sz;
87*49cdfc7eSAndroid Build Coastguard Worker char *addr;			/* addr of memory mapped region */
88*49cdfc7eSAndroid Build Coastguard Worker int fildes;			/* file descriptor for tempfile */
89*49cdfc7eSAndroid Build Coastguard Worker unsigned int map_len;		/* length of the region to be mapped */
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 void sig_handler();		/* signal catching function */
94*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)95*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
96*49cdfc7eSAndroid Build Coastguard Worker {
97*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
100*49cdfc7eSAndroid Build Coastguard Worker 
101*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
104*49cdfc7eSAndroid Build Coastguard Worker 
105*49cdfc7eSAndroid Build Coastguard Worker 		setup();
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker 		/*
108*49cdfc7eSAndroid Build Coastguard Worker 		 * Call munmap to unmap the part of the mapped region of the
109*49cdfc7eSAndroid Build Coastguard Worker 		 * temporary file from the address and length that is part of
110*49cdfc7eSAndroid Build Coastguard Worker 		 * the mapped region.
111*49cdfc7eSAndroid Build Coastguard Worker 		 */
112*49cdfc7eSAndroid Build Coastguard Worker 		TEST(munmap(addr, map_len));
113*49cdfc7eSAndroid Build Coastguard Worker 
114*49cdfc7eSAndroid Build Coastguard Worker 		/* Check for the return value of munmap() */
115*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_RETURN == -1) {
116*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "munmap() fails, errno=%d : %s",
117*49cdfc7eSAndroid Build Coastguard Worker 				 TEST_ERRNO, strerror(TEST_ERRNO));
118*49cdfc7eSAndroid Build Coastguard Worker 			continue;
119*49cdfc7eSAndroid Build Coastguard Worker 		}
120*49cdfc7eSAndroid Build Coastguard Worker 		/*
121*49cdfc7eSAndroid Build Coastguard Worker 		 * Check whether further reference is possible
122*49cdfc7eSAndroid Build Coastguard Worker 		 * to the unmapped memory region by writing
123*49cdfc7eSAndroid Build Coastguard Worker 		 * to the first byte of region with
124*49cdfc7eSAndroid Build Coastguard Worker 		 * some arbitrary number.
125*49cdfc7eSAndroid Build Coastguard Worker 		 */
126*49cdfc7eSAndroid Build Coastguard Worker 		*addr = 50;
127*49cdfc7eSAndroid Build Coastguard Worker 
128*49cdfc7eSAndroid Build Coastguard Worker 		/* This message is printed if no SIGSEGV */
129*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "process succeeds to refer unmapped "
130*49cdfc7eSAndroid Build Coastguard Worker 			 "memory region");
131*49cdfc7eSAndroid Build Coastguard Worker 		cleanup();
132*49cdfc7eSAndroid Build Coastguard Worker 
133*49cdfc7eSAndroid Build Coastguard Worker 	}
134*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
135*49cdfc7eSAndroid Build Coastguard Worker }
136*49cdfc7eSAndroid Build Coastguard Worker 
137*49cdfc7eSAndroid Build Coastguard Worker /*
138*49cdfc7eSAndroid Build Coastguard Worker  * setup() - performs all ONE TIME setup for this test.
139*49cdfc7eSAndroid Build Coastguard Worker  * Setup signal handler to catch SIGSEGV.
140*49cdfc7eSAndroid Build Coastguard Worker  * Get system page size, create a temporary file for reading/writing,
141*49cdfc7eSAndroid Build Coastguard Worker  * write one byte data into it, map the open file for the specified
142*49cdfc7eSAndroid Build Coastguard Worker  * map length.
143*49cdfc7eSAndroid Build Coastguard Worker  */
setup(void)144*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
145*49cdfc7eSAndroid Build Coastguard Worker {
146*49cdfc7eSAndroid Build Coastguard Worker 
147*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
148*49cdfc7eSAndroid Build Coastguard Worker 
149*49cdfc7eSAndroid Build Coastguard Worker 	/* call signal function to trap the signal generated */
150*49cdfc7eSAndroid Build Coastguard Worker 	if (signal(SIGSEGV, sig_handler) == SIG_ERR) {
151*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK, cleanup, "signal fails to catch signal");
152*49cdfc7eSAndroid Build Coastguard Worker 	}
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 	page_sz = getpagesize();
158*49cdfc7eSAndroid Build Coastguard Worker 
159*49cdfc7eSAndroid Build Coastguard Worker 	/*
160*49cdfc7eSAndroid Build Coastguard Worker 	 * Get the length of the open file to be mapped into process
161*49cdfc7eSAndroid Build Coastguard Worker 	 * address space.
162*49cdfc7eSAndroid Build Coastguard Worker 	 */
163*49cdfc7eSAndroid Build Coastguard Worker 	map_len = 3 * page_sz;
164*49cdfc7eSAndroid Build Coastguard Worker 
165*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
166*49cdfc7eSAndroid Build Coastguard Worker 
167*49cdfc7eSAndroid Build Coastguard Worker 	/* Creat a temporary file used for mapping */
168*49cdfc7eSAndroid Build Coastguard Worker 	if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0) {
169*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK, cleanup, "open() on %s Failed, errno=%d : %s",
170*49cdfc7eSAndroid Build Coastguard Worker 			 TEMPFILE, errno, strerror(errno));
171*49cdfc7eSAndroid Build Coastguard Worker 	}
172*49cdfc7eSAndroid Build Coastguard Worker 
173*49cdfc7eSAndroid Build Coastguard Worker 	/*
174*49cdfc7eSAndroid Build Coastguard Worker 	 * move the file pointer to maplength position from the beginning
175*49cdfc7eSAndroid Build Coastguard Worker 	 * of the file.
176*49cdfc7eSAndroid Build Coastguard Worker 	 */
177*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_LSEEK(cleanup, fildes, map_len, SEEK_SET);
178*49cdfc7eSAndroid Build Coastguard Worker 
179*49cdfc7eSAndroid Build Coastguard Worker 	/* Write one byte into temporary file */
180*49cdfc7eSAndroid Build Coastguard Worker 	if (write(fildes, "a", 1) != 1) {
181*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK, cleanup, "write() on %s Failed, errno=%d : %s",
182*49cdfc7eSAndroid Build Coastguard Worker 			 TEMPFILE, errno, strerror(errno));
183*49cdfc7eSAndroid Build Coastguard Worker 	}
184*49cdfc7eSAndroid Build Coastguard Worker 
185*49cdfc7eSAndroid Build Coastguard Worker 	/*
186*49cdfc7eSAndroid Build Coastguard Worker 	 * map the open file 'TEMPFILE' from its beginning up to the maplength
187*49cdfc7eSAndroid Build Coastguard Worker 	 * into the calling process's address space at the system choosen
188*49cdfc7eSAndroid Build Coastguard Worker 	 * with read/write permissions to the mapped region.
189*49cdfc7eSAndroid Build Coastguard Worker 	 */
190*49cdfc7eSAndroid Build Coastguard Worker 	addr = mmap(0, map_len, PROT_READ | PROT_WRITE,
191*49cdfc7eSAndroid Build Coastguard Worker 		    MAP_FILE | MAP_SHARED, fildes, 0);
192*49cdfc7eSAndroid Build Coastguard Worker 
193*49cdfc7eSAndroid Build Coastguard Worker 	/* check for the return value of mmap system call */
194*49cdfc7eSAndroid Build Coastguard Worker 	if (addr == (char *)MAP_FAILED) {
195*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK, cleanup, "mmap() Failed on %s, errno=%d : %s",
196*49cdfc7eSAndroid Build Coastguard Worker 			 TEMPFILE, errno, strerror(errno));
197*49cdfc7eSAndroid Build Coastguard Worker 	}
198*49cdfc7eSAndroid Build Coastguard Worker 
199*49cdfc7eSAndroid Build Coastguard Worker 	/*
200*49cdfc7eSAndroid Build Coastguard Worker 	 * increment the start address of the region at which the file is
201*49cdfc7eSAndroid Build Coastguard Worker 	 * mapped to a maplength of 3 times the system page size by the value
202*49cdfc7eSAndroid Build Coastguard Worker 	 * of system  page size and decrement the maplength value by the value
203*49cdfc7eSAndroid Build Coastguard Worker 	 * of system page size.
204*49cdfc7eSAndroid Build Coastguard Worker 	 */
205*49cdfc7eSAndroid Build Coastguard Worker 	addr = (char *)((long)addr + page_sz);
206*49cdfc7eSAndroid Build Coastguard Worker 	map_len = map_len - page_sz;
207*49cdfc7eSAndroid Build Coastguard Worker }
208*49cdfc7eSAndroid Build Coastguard Worker 
209*49cdfc7eSAndroid Build Coastguard Worker /*
210*49cdfc7eSAndroid Build Coastguard Worker  * void
211*49cdfc7eSAndroid Build Coastguard Worker  * sig_handler() - signal catching function.
212*49cdfc7eSAndroid Build Coastguard Worker  *   This function is used to trap the signal generated when tried to read or
213*49cdfc7eSAndroid Build Coastguard Worker  *   write to the memory mapped region which is already detached from the
214*49cdfc7eSAndroid Build Coastguard Worker  *   calling process address space.
215*49cdfc7eSAndroid Build Coastguard Worker  *   this function is invoked when SIGSEGV generated and it calls test
216*49cdfc7eSAndroid Build Coastguard Worker  *   cleanup function and exit the program.
217*49cdfc7eSAndroid Build Coastguard Worker  */
sig_handler(void)218*49cdfc7eSAndroid Build Coastguard Worker void sig_handler(void)
219*49cdfc7eSAndroid Build Coastguard Worker {
220*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TPASS, "Functionality of munmap() successful");
221*49cdfc7eSAndroid Build Coastguard Worker 
222*49cdfc7eSAndroid Build Coastguard Worker 	/* Invoke test cleanup function and exit */
223*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
224*49cdfc7eSAndroid Build Coastguard Worker 
225*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
226*49cdfc7eSAndroid Build Coastguard Worker }
227*49cdfc7eSAndroid Build Coastguard Worker 
228*49cdfc7eSAndroid Build Coastguard Worker /*
229*49cdfc7eSAndroid Build Coastguard Worker  * cleanup() - performs all ONE TIME cleanup for this test at
230*49cdfc7eSAndroid Build Coastguard Worker  *             completion or premature exit.
231*49cdfc7eSAndroid Build Coastguard Worker  *  Unmap the portion of the region of the file left unmapped.
232*49cdfc7eSAndroid Build Coastguard Worker  *  Close the temporary file.
233*49cdfc7eSAndroid Build Coastguard Worker  *  Remove the temporary directory.
234*49cdfc7eSAndroid Build Coastguard Worker  */
cleanup(void)235*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
236*49cdfc7eSAndroid Build Coastguard Worker {
237*49cdfc7eSAndroid Build Coastguard Worker 
238*49cdfc7eSAndroid Build Coastguard Worker 	/*
239*49cdfc7eSAndroid Build Coastguard Worker 	 * get the start address and length of the portion of
240*49cdfc7eSAndroid Build Coastguard Worker 	 * the mapped region of the file.
241*49cdfc7eSAndroid Build Coastguard Worker 	 */
242*49cdfc7eSAndroid Build Coastguard Worker 	addr = (char *)((long)addr - page_sz);
243*49cdfc7eSAndroid Build Coastguard Worker 	map_len = map_len - page_sz;
244*49cdfc7eSAndroid Build Coastguard Worker 
245*49cdfc7eSAndroid Build Coastguard Worker 	/* unmap the portion of the region of the file left unmapped */
246*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MUNMAP(NULL, addr, map_len);
247*49cdfc7eSAndroid Build Coastguard Worker 
248*49cdfc7eSAndroid Build Coastguard Worker 	/* Close the temporary file */
249*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(NULL, fildes);
250*49cdfc7eSAndroid Build Coastguard Worker 
251*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
252*49cdfc7eSAndroid Build Coastguard Worker }
253