blob: e9510b780f7314a72e05e72d26cfce6e6d3d6058 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * Test Name: munmap01
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 * and after successful completion of munmap, the unmapped region is no
27 * longer accessible.
28 *
29 * Expected Result:
30 * munmap call should succeed to unmap a mapped file or anonymous shared
31 * memory region from the process's address space and it returns with a
32 * value 0, further reference to the unmapped region should result in a
33 * segmentation fault (SIGSEGV).
34 *
35 * Algorithm:
36 * Setup:
37 * Setup signal handling.
38 * Create temporary directory.
39 * Pause for SIGUSR1 if option specified.
40 *
41 * Test:
42 * Loop if the proper options are given.
43 * Execute system call
44 * Check return code, if system call failed (return=-1)
45 * Log the errno and Issue a FAIL message.
46 * Otherwise,
subrata_modakbdbaec52009-02-26 12:14:51 +000047 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000048 * if successful,
49 * Issue Functionality-Pass message.
50 * Otherwise,
51 * Issue Functionality-Fail message.
52 * Cleanup:
53 * Print errno log and/or timing stats if options given
54 * Delete the temporary directory created.
55 *
56 * Usage: <for command-line>
57 * munmap01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
58 * where, -c n : Run n copies concurrently.
59 * -f : Turn off functionality Testing.
60 * -i n : Execute test n times.
61 * -I x : Execute test for x seconds.
62 * -P x : Pause for x seconds between iterations.
63 * -t : Turn on syscall timing.
64 *
65 * HISTORY
66 * 07/2001 Ported by Wayne Boyer
67 *
68 * RESTRICTIONS:
subrata_modak56207ce2009-03-23 13:35:39 +000069 * None.$
plars865695b2001-08-27 22:15:12 +000070 */
71#include <errno.h>
72#include <unistd.h>
73#include <fcntl.h>
74#include <sys/stat.h>
75#include <sys/mman.h>
76
77#include "test.h"
plars865695b2001-08-27 22:15:12 +000078
79#define TEMPFILE "mmapfile"
80
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020081char *TCID = "munmap01";
82int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000083
84char *addr; /* addr of memory mapped region */
85int fildes; /* file descriptor for tempfile */
86unsigned int map_len; /* length of the region to be mapped */
87
88void setup(); /* Main setup function of test */
89void cleanup(); /* cleanup function for the test */
90void sig_handler(); /* signal catching function */
91
subrata_modak56207ce2009-03-23 13:35:39 +000092int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000093{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020094 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020095 const char *msg;
subrata_modakbdbaec52009-02-26 12:14:51 +000096
Garrett Coopera9e49f12010-12-16 10:54:03 -080097 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080098 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000099
plars865695b2001-08-27 22:15:12 +0000100 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800101
Caspar Zhangd59a6592013-03-07 14:59:12 +0800102 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000103
plars865695b2001-08-27 22:15:12 +0000104 setup();
105
subrata_modak4bb656a2009-02-26 12:02:09 +0000106 /*
107 * Call munmap to unmap the mapped region of the
subrata_modak56207ce2009-03-23 13:35:39 +0000108 * temporary file from the calling process's address space.
plars865695b2001-08-27 22:15:12 +0000109 */
110 TEST(munmap(addr, map_len));
111
112 /* Check for the return value of munmap() */
113 if (TEST_RETURN == -1) {
114 tst_resm(TFAIL, "munmap() fails, errno=%d : %s",
115 TEST_ERRNO, strerror(TEST_ERRNO));
116 continue;
117 }
robbiew034306d2005-12-23 18:07:41 +0000118#ifdef UCLINUX
119 /*
120 * No SIGSEGV on uClinux since
121 * MMU not implemented on uClinux
122 */
123 tst_resm(TPASS, "call succedded");
124#else
plars865695b2001-08-27 22:15:12 +0000125 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200126 * Check whether further reference is possible
127 * to the unmapped memory region by writing
128 * to the first byte of region with
129 * some arbitrary number.
plars865695b2001-08-27 22:15:12 +0000130 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200131 *addr = 50;
plars865695b2001-08-27 22:15:12 +0000132
Cyril Hrubise38b9612014-06-02 17:20:57 +0200133 /* This message is printed if no SIGSEGV */
134 tst_resm(TFAIL, "process succeeds to refer unmapped "
135 "memory region");
robbiew034306d2005-12-23 18:07:41 +0000136#endif
plars865695b2001-08-27 22:15:12 +0000137
plars865695b2001-08-27 22:15:12 +0000138 cleanup();
139
Garrett Cooper2c282152010-12-16 00:55:50 -0800140 }
Garrett Cooperad8b91f2010-12-19 10:25:10 -0800141 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800142}
plars865695b2001-08-27 22:15:12 +0000143
144/*
145 * setup() - performs all ONE TIME setup for this test.
146 *
147 * Set up signal handler to catch SIGSEGV.
148 * Get system page size, create a temporary file for reading/writing,
149 * write one byte data into it, map the open file for the specified
150 * map length.
151 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400152void setup(void)
plars865695b2001-08-27 22:15:12 +0000153{
Zeng Linggang9d048f82015-01-29 18:20:45 +0800154 size_t page_sz;
plars865695b2001-08-27 22:15:12 +0000155
plars865695b2001-08-27 22:15:12 +0000156 tst_sig(NOFORK, DEF_HANDLER, cleanup);
157
158 /* call signal function to trap the signal generated */
159 if (signal(SIGSEGV, sig_handler) == SIG_ERR) {
160 tst_brkm(TBROK, cleanup, "signal fails to catch signal");
plars865695b2001-08-27 22:15:12 +0000161 }
162
plars865695b2001-08-27 22:15:12 +0000163 TEST_PAUSE;
164
165 /* Get the system page size */
Zeng Linggang9d048f82015-01-29 18:20:45 +0800166 page_sz = getpagesize();
plars865695b2001-08-27 22:15:12 +0000167
168 /*
169 * Get the length of the open file to be mapped into process
170 * address space.
171 */
172 map_len = 3 * page_sz;
173
plars865695b2001-08-27 22:15:12 +0000174 tst_tmpdir();
175
176 /* Creat a temporary file used for mapping */
177 if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0) {
178 tst_brkm(TBROK, cleanup, "open() on %s Failed, errno=%d : %s",
179 TEMPFILE, errno, strerror(errno));
plars865695b2001-08-27 22:15:12 +0000180 }
181
182 /*
183 * move the file pointer to maplength position from the beginning
184 * of the file.
185 */
186 if (lseek(fildes, map_len, SEEK_SET) == -1) {
187 tst_brkm(TBROK, cleanup, "lseek() fails on %s, errno=%d : %s",
188 TEMPFILE, errno, strerror(errno));
plars865695b2001-08-27 22:15:12 +0000189 }
190
191 /* Write one byte into temporary file */
192 if (write(fildes, "a", 1) != 1) {
193 tst_brkm(TBROK, cleanup, "write() on %s Failed, errno=%d : %s",
194 TEMPFILE, errno, strerror(errno));
plars865695b2001-08-27 22:15:12 +0000195 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000196
plars865695b2001-08-27 22:15:12 +0000197 /*
198 * map the open file 'TEMPFILE' from its beginning up to the maplength
199 * into the calling process's address space at the system choosen
200 * with read/write permissions to the the mapped region.
201 */
robbiew034306d2005-12-23 18:07:41 +0000202#ifdef UCLINUX
203 /* MAP_SHARED is not implemented on uClinux */
subrata_modak56207ce2009-03-23 13:35:39 +0000204 addr = mmap(0, map_len, PROT_READ | PROT_WRITE,
robbiew034306d2005-12-23 18:07:41 +0000205 MAP_FILE | MAP_PRIVATE, fildes, 0);
206#else
subrata_modak56207ce2009-03-23 13:35:39 +0000207 addr = mmap(0, map_len, PROT_READ | PROT_WRITE,
plars865695b2001-08-27 22:15:12 +0000208 MAP_FILE | MAP_SHARED, fildes, 0);
robbiew034306d2005-12-23 18:07:41 +0000209#endif
plars865695b2001-08-27 22:15:12 +0000210
211 /* check for the return value of mmap system call */
212 if (addr == (char *)MAP_FAILED) {
213 tst_brkm(TBROK, cleanup, "mmap() Failed on %s, errno=%d : %s",
214 TEMPFILE, errno, strerror(errno));
plars865695b2001-08-27 22:15:12 +0000215 }
robbiew09830082003-03-26 23:18:32 +0000216
plars865695b2001-08-27 22:15:12 +0000217}
218
219/*
220 * sig_handler() - signal catching function.
221 * This function is used to trap the signal generated when tried to read or
222 * write to the memory mapped region which is already detached from the
223 * calling process address space.
224 * this function is invoked when SIGSEGV generated and it calls test
225 * cleanup function and exit the program.
226 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400227void sig_handler(void)
plars865695b2001-08-27 22:15:12 +0000228{
229 tst_resm(TPASS, "Functionality of munmap() successful");
230
231 /* Invoke test cleanup function and exit */
232 cleanup();
233
plars865695b2001-08-27 22:15:12 +0000234 tst_exit();
235}
236
237/*
238 * cleanup() - performs all ONE TIME cleanup for this test at
239 * completion or premature exit.
240 * Close the temporary file.
241 * Remove the temporary directory.
242 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400243void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000244{
plars865695b2001-08-27 22:15:12 +0000245
246 /* Close the temporary file */
247 if (close(fildes) < 0) {
248 tst_brkm(TFAIL, NULL, "close() on %s Failed, errno=%d : %s",
249 TEMPFILE, errno, strerror(errno));
250 }
251
plars865695b2001-08-27 22:15:12 +0000252 tst_rmdir();
Garrett Cooperad8b91f2010-12-19 10:25:10 -0800253}