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