plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 3 | * |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 8 | * |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 12 | * the GNU General Public License for more details. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 13 | * |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 20 | * Test Description: |
| 21 | * Verify that, mmap() succeeds when used to map a file where size of the |
| 22 | * file is not a multiple of the page size, the memory area beyond the end |
| 23 | * of the file to the end of the page is accessible. Also, verify that |
| 24 | * this area is all zeroed and the modifications done to this area are |
| 25 | * not written to the file. |
| 26 | * |
| 27 | * Expected Result: |
| 28 | * mmap() should succeed returning the address of the mapped region. |
| 29 | * The memory area beyond the end of file to the end of page should be |
| 30 | * filled with zero. |
| 31 | * The changes beyond the end of file should not get written to the file. |
| 32 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 33 | * HISTORY |
| 34 | * 07/2001 Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 35 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <sys/types.h> |
| 39 | #include <errno.h> |
| 40 | #include <unistd.h> |
| 41 | #include <fcntl.h> |
| 42 | #include <string.h> |
| 43 | #include <signal.h> |
plars | 85cbba3 | 2002-09-25 16:54:54 +0000 | [diff] [blame] | 44 | #include <stdint.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | #include <sys/stat.h> |
| 46 | #include <sys/mman.h> |
subrata_modak | c327066 | 2008-05-02 06:28:13 +0000 | [diff] [blame] | 47 | #include <sys/shm.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | |
| 49 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | |
| 51 | #define TEMPFILE "mmapfile" |
| 52 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 53 | char *TCID = "mmap01"; |
| 54 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 56 | static char *addr; |
| 57 | static char *dummy; |
| 58 | static size_t page_sz; |
| 59 | static size_t file_sz; |
| 60 | static int fildes; |
| 61 | static char cmd_buffer[BUFSIZ]; |
| 62 | |
| 63 | static void setup(void); |
| 64 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 66 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 68 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 69 | const char *msg; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 70 | |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 71 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | setup(); |
| 75 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 77 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 78 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 80 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | * Call mmap to map the temporary file beyond EOF |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 82 | * with write access. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | */ |
robbiew | d5c2112 | 2002-03-22 16:22:40 +0000 | [diff] [blame] | 84 | errno = 0; |
| 85 | addr = mmap(addr, page_sz, PROT_READ | PROT_WRITE, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 86 | MAP_FILE | MAP_SHARED | MAP_FIXED, fildes, 0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | |
| 88 | /* Check for the return value of mmap() */ |
robbiew | d5c2112 | 2002-03-22 16:22:40 +0000 | [diff] [blame] | 89 | if (addr == MAP_FAILED) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 90 | tst_resm(TFAIL | TERRNO, "mmap of %s failed", TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | continue; |
| 92 | } |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 93 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 94 | /* |
| 95 | * Check if mapped memory area beyond EOF are |
| 96 | * zeros and changes beyond EOF are not written |
| 97 | * to file. |
| 98 | */ |
| 99 | if (memcmp(&addr[file_sz], dummy, page_sz - file_sz)) { |
| 100 | tst_brkm(TFAIL, cleanup, |
| 101 | "mapped memory area contains invalid " |
| 102 | "data"); |
| 103 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 105 | /* |
| 106 | * Initialize memory beyond file size |
| 107 | */ |
| 108 | addr[file_sz] = 'X'; |
| 109 | addr[file_sz + 1] = 'Y'; |
| 110 | addr[file_sz + 2] = 'Z'; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 112 | /* |
| 113 | * Synchronize the mapped memory region |
| 114 | * with the file. |
| 115 | */ |
| 116 | if (msync(addr, page_sz, MS_SYNC) != 0) { |
| 117 | tst_brkm(TFAIL | TERRNO, cleanup, |
| 118 | "failed to synchronize mapped file"); |
| 119 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 121 | /* |
| 122 | * Now, Search for the pattern 'XYZ' in the |
| 123 | * temporary file. The pattern should not be |
| 124 | * found and the return value should be 1. |
| 125 | */ |
| 126 | if (system(cmd_buffer) != 0) { |
| 127 | tst_resm(TPASS, |
| 128 | "Functionality of mmap() successful"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 130 | tst_resm(TFAIL, |
| 131 | "Specified pattern found in file"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* Clean up things in case we are looping */ |
| 135 | /* Unmap the mapped memory */ |
| 136 | if (munmap(addr, page_sz) != 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 137 | tst_brkm(TFAIL | TERRNO, NULL, "munmap failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 139 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 142 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 143 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 145 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | { |
| 147 | struct stat stat_buf; |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 148 | char Path_name[PATH_MAX]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | char write_buf[] = "hello world\n"; |
| 150 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 152 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | TEST_PAUSE; |
| 154 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | tst_tmpdir(); |
| 156 | |
| 157 | /* Get the path of temporary file to be created */ |
| 158 | if (getcwd(Path_name, sizeof(Path_name)) == NULL) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 159 | tst_brkm(TFAIL | TERRNO, cleanup, |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 160 | "getcwd failed to get current working directory"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /* Creat a temporary file used for mapping */ |
| 164 | if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0) { |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 165 | tst_brkm(TFAIL, cleanup, "opening %s failed", TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* Write some data into temporary file */ |
| 169 | if (write(fildes, write_buf, strlen(write_buf)) != strlen(write_buf)) { |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 170 | tst_brkm(TFAIL, cleanup, "writing to %s", TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 172 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | /* Get the size of temporary file */ |
| 174 | if (stat(TEMPFILE, &stat_buf) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 175 | tst_brkm(TFAIL | TERRNO, cleanup, "stat of %s failed", |
| 176 | TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | } |
| 178 | file_sz = stat_buf.st_size; |
| 179 | |
Cyril Hrubis | ec22fb2 | 2013-06-04 20:37:05 +0200 | [diff] [blame] | 180 | page_sz = getpagesize(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 181 | |
| 182 | /* Allocate and initialize dummy string of system page size bytes */ |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 183 | if ((dummy = calloc(page_sz, sizeof(char))) == NULL) { |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 184 | tst_brkm(TFAIL, cleanup, "calloc failed (dummy)"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /* |
subrata_modak | c327066 | 2008-05-02 06:28:13 +0000 | [diff] [blame] | 188 | * Initialize addr to align with the first segment boundary address |
| 189 | * above the break address of the process. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 190 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 191 | addr = (void *)(((intptr_t) sbrk(0) + (SHMLBA - 1)) & ~(SHMLBA - 1)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 192 | |
subrata_modak | c327066 | 2008-05-02 06:28:13 +0000 | [diff] [blame] | 193 | /* Set the break address of the process to the addr plus one |
| 194 | * page size. |
| 195 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 196 | if ((intptr_t) sbrk(SHMLBA + page_sz) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 197 | tst_brkm(TFAIL | TERRNO, cleanup, |
| 198 | "sbrk(SHMLBA + page_sz) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /* Initialize one page region from addr with 'A' */ |
| 202 | memset(addr, 'A', page_sz); |
| 203 | |
| 204 | /* Create the command which will be executed in the test */ |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 205 | sprintf(cmd_buffer, "grep XYZ %s/%s > /dev/null", Path_name, TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 208 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 209 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 210 | close(fildes); |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 211 | free(dummy); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 212 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 213 | } |