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: 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,
47 * Verify the Functionality of system call
48 * 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:
69 * None.$
70 */
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"
78
79 #define TEMPFILE "mmapfile"
80
81 char *TCID = "munmap01";
82 int TST_TOTAL = 1;
83
84 char *addr; /* addr of memory mapped region */
85 int fildes; /* file descriptor for tempfile */
86 unsigned int map_len; /* length of the region to be mapped */
87
88 void setup(); /* Main setup function of test */
89 void cleanup(); /* cleanup function for the test */
90 void sig_handler(); /* signal catching function */
91
main(int ac,char ** av)92 int main(int ac, char **av)
93 {
94 int lc;
95
96 tst_parse_opts(ac, av, NULL, NULL);
97
98 for (lc = 0; TEST_LOOPING(lc); lc++) {
99
100 tst_count = 0;
101
102 setup();
103
104 /*
105 * Call munmap to unmap the mapped region of the
106 * temporary file from the calling process's address space.
107 */
108 TEST(munmap(addr, map_len));
109
110 /* Check for the return value of munmap() */
111 if (TEST_RETURN == -1) {
112 tst_resm(TFAIL, "munmap() fails, errno=%d : %s",
113 TEST_ERRNO, strerror(TEST_ERRNO));
114 continue;
115 }
116 #ifdef UCLINUX
117 /*
118 * No SIGSEGV on uClinux since
119 * MMU not implemented on uClinux
120 */
121 tst_resm(TPASS, "call succedded");
122 #else
123 /*
124 * Check whether further reference is possible
125 * to the unmapped memory region by writing
126 * to the first byte of region with
127 * some arbitrary number.
128 */
129 *addr = 50;
130
131 /* This message is printed if no SIGSEGV */
132 tst_resm(TFAIL, "process succeeds to refer unmapped "
133 "memory region");
134 #endif
135
136 cleanup();
137
138 }
139 tst_exit();
140 }
141
142 /*
143 * setup() - performs all ONE TIME setup for this test.
144 *
145 * Set up signal handler to catch SIGSEGV.
146 * Get system page size, create a temporary file for reading/writing,
147 * write one byte data into it, map the open file for the specified
148 * map length.
149 */
setup(void)150 void setup(void)
151 {
152 size_t page_sz;
153
154 tst_sig(NOFORK, DEF_HANDLER, cleanup);
155
156 /* call signal function to trap the signal generated */
157 if (signal(SIGSEGV, sig_handler) == SIG_ERR) {
158 tst_brkm(TBROK, cleanup, "signal fails to catch signal");
159 }
160
161 TEST_PAUSE;
162
163 /* Get the system page size */
164 page_sz = getpagesize();
165
166 /*
167 * Get the length of the open file to be mapped into process
168 * address space.
169 */
170 map_len = 3 * page_sz;
171
172 tst_tmpdir();
173
174 /* Creat a temporary file used for mapping */
175 if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0) {
176 tst_brkm(TBROK, cleanup, "open() on %s Failed, errno=%d : %s",
177 TEMPFILE, errno, strerror(errno));
178 }
179
180 /*
181 * move the file pointer to maplength position from the beginning
182 * of the file.
183 */
184 if (lseek(fildes, map_len, SEEK_SET) == -1) {
185 tst_brkm(TBROK, cleanup, "lseek() fails on %s, errno=%d : %s",
186 TEMPFILE, errno, strerror(errno));
187 }
188
189 /* Write one byte into temporary file */
190 if (write(fildes, "a", 1) != 1) {
191 tst_brkm(TBROK, cleanup, "write() on %s Failed, errno=%d : %s",
192 TEMPFILE, errno, strerror(errno));
193 }
194
195 /*
196 * map the open file 'TEMPFILE' from its beginning up to the maplength
197 * into the calling process's address space at the system choosen
198 * with read/write permissions to the the mapped region.
199 */
200 #ifdef UCLINUX
201 /* MAP_SHARED is not implemented on uClinux */
202 addr = mmap(0, map_len, PROT_READ | PROT_WRITE,
203 MAP_FILE | MAP_PRIVATE, fildes, 0);
204 #else
205 addr = mmap(0, map_len, PROT_READ | PROT_WRITE,
206 MAP_FILE | MAP_SHARED, fildes, 0);
207 #endif
208
209 /* check for the return value of mmap system call */
210 if (addr == (char *)MAP_FAILED) {
211 tst_brkm(TBROK, cleanup, "mmap() Failed on %s, errno=%d : %s",
212 TEMPFILE, errno, strerror(errno));
213 }
214
215 }
216
217 /*
218 * sig_handler() - signal catching function.
219 * This function is used to trap the signal generated when tried to read or
220 * write to the memory mapped region which is already detached from the
221 * calling process address space.
222 * this function is invoked when SIGSEGV generated and it calls test
223 * cleanup function and exit the program.
224 */
sig_handler(void)225 void sig_handler(void)
226 {
227 tst_resm(TPASS, "Functionality of munmap() successful");
228
229 /* Invoke test cleanup function and exit */
230 cleanup();
231
232 tst_exit();
233 }
234
235 /*
236 * cleanup() - performs all ONE TIME cleanup for this test at
237 * completion or premature exit.
238 * Close the temporary file.
239 * Remove the temporary directory.
240 */
cleanup(void)241 void cleanup(void)
242 {
243
244 /* Close the temporary file */
245 if (close(fildes) < 0) {
246 tst_brkm(TFAIL, NULL, "close() on %s Failed, errno=%d : %s",
247 TEMPFILE, errno, strerror(errno));
248 }
249
250 tst_rmdir();
251 }
252