mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
| 3 | * Author: Rajeev Tiwari: rajeevti@in.ibm.com |
| 4 | * Copyright (c) 2004 Gernot Payer <gpayer@suse.de> |
| 5 | * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz> |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 6 | * |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 11 | * |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 15 | * the GNU General Public License for more details. |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 16 | * |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | /* |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 23 | * test1: |
| 24 | * Invoke mincore() when the start address is not multiple of page size. |
| 25 | * EINVAL |
| 26 | * test2: |
| 27 | * Invoke mincore() when the vector points to an invalid address. EFAULT |
| 28 | * test3: |
| 29 | * Invoke mincore() when the starting address + length contained unmapped |
| 30 | * memory. ENOMEM |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 31 | * test4: |
| 32 | * Invoke mincore() when length is greater than (TASK_SIZE - addr). ENOMEM |
| 33 | * In Linux 2.6.11 and earlier, the error EINVAL was returned for this |
| 34 | * condition. |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 35 | */ |
| 36 | |
| 37 | #include <fcntl.h> |
| 38 | #include <errno.h> |
| 39 | #include <sys/mman.h> |
| 40 | #include <stdlib.h> |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 41 | #include <unistd.h> |
subrata_modak | f798c21 | 2007-12-13 10:25:18 +0000 | [diff] [blame] | 42 | #include <sys/resource.h> |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 43 | #include <sys/types.h> |
| 44 | #include <sys/stat.h> |
| 45 | #include "test.h" |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 46 | #include "safe_macros.h" |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 47 | |
mridge | 051f423 | 2004-09-13 21:59:01 +0000 | [diff] [blame] | 48 | static int PAGESIZE; |
subrata_modak | f798c21 | 2007-12-13 10:25:18 +0000 | [diff] [blame] | 49 | static rlim_t STACK_LIMIT = 10485760; |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 50 | |
mridge | 051f423 | 2004-09-13 21:59:01 +0000 | [diff] [blame] | 51 | static void cleanup(void); |
| 52 | static void setup(void); |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 53 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 54 | char *TCID = "mincore01"; |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 55 | |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 56 | static char *global_pointer; |
| 57 | static unsigned char *global_vec; |
| 58 | static int global_len; |
| 59 | |
| 60 | struct test_case_t; |
| 61 | static void setup1(struct test_case_t *tc); |
| 62 | static void setup2(struct test_case_t *tc); |
| 63 | static void setup3(struct test_case_t *tc); |
| 64 | static void setup4(struct test_case_t *tc); |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 65 | |
mridge | 051f423 | 2004-09-13 21:59:01 +0000 | [diff] [blame] | 66 | static struct test_case_t { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 67 | char *addr; |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 68 | size_t len; |
vapier | 33acea5 | 2006-05-26 06:34:45 +0000 | [diff] [blame] | 69 | unsigned char *vector; |
mridge | 051f423 | 2004-09-13 21:59:01 +0000 | [diff] [blame] | 70 | int exp_errno; |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 71 | void (*setupfunc) (struct test_case_t *tc); |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 72 | } TC[] = { |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 73 | {NULL, 0, NULL, EINVAL, setup1}, |
| 74 | {NULL, 0, NULL, EFAULT, setup2}, |
| 75 | {NULL, 0, NULL, ENOMEM, setup3}, |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 76 | {NULL, 0, NULL, ENOMEM, setup4} |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 77 | }; |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 78 | |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 79 | static void mincore_verify(struct test_case_t *tc); |
| 80 | |
| 81 | int TST_TOTAL = ARRAY_SIZE(TC); |
| 82 | |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 83 | int main(int ac, char **av) |
| 84 | { |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 85 | int i, lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 86 | const char *msg; |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 87 | |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 88 | msg = parse_opts(ac, av, NULL, NULL); |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 89 | if (msg != NULL) |
mridge | 051f423 | 2004-09-13 21:59:01 +0000 | [diff] [blame] | 90 | tst_brkm(TBROK, cleanup, "error parsing options: %s", msg); |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 91 | |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 92 | setup(); |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 93 | |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 94 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 95 | tst_count = 0; |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 96 | |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 97 | for (i = 0; i < TST_TOTAL; i++) |
| 98 | mincore_verify(&TC[i]); |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 99 | } |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 100 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 102 | tst_exit(); |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 105 | static void setup1(struct test_case_t *tc) |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 106 | { |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 107 | tc->addr = global_pointer + 1; |
| 108 | tc->len = global_len; |
| 109 | tc->vector = global_vec; |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 112 | void setup2(struct test_case_t *tc) |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 113 | { |
vapier | 33acea5 | 2006-05-26 06:34:45 +0000 | [diff] [blame] | 114 | unsigned char *t; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 115 | struct rlimit limit; |
subrata_modak | f798c21 | 2007-12-13 10:25:18 +0000 | [diff] [blame] | 116 | |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 117 | t = SAFE_MMAP(cleanup, NULL, global_len, PROT_READ | PROT_WRITE, |
| 118 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 119 | |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 120 | SAFE_MUNMAP(cleanup, t, global_len); |
subrata_modak | f798c21 | 2007-12-13 10:25:18 +0000 | [diff] [blame] | 121 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 122 | /* set stack limit so that the unmaped pointer is invalid for architectures like s390 */ |
| 123 | limit.rlim_cur = STACK_LIMIT; |
| 124 | limit.rlim_max = STACK_LIMIT; |
subrata_modak | f798c21 | 2007-12-13 10:25:18 +0000 | [diff] [blame] | 125 | |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 126 | if (setrlimit(RLIMIT_STACK, &limit) != 0) |
| 127 | tst_brkm(TBROK | TERRNO, cleanup, "setrlimit failed"); |
subrata_modak | f798c21 | 2007-12-13 10:25:18 +0000 | [diff] [blame] | 128 | |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 129 | tc->addr = global_pointer; |
| 130 | tc->len = global_len; |
| 131 | tc->vector = t; |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 134 | static void setup3(struct test_case_t *tc) |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 135 | { |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 136 | tc->addr = global_pointer; |
| 137 | tc->len = global_len * 2; |
| 138 | SAFE_MUNMAP(cleanup, global_pointer + global_len, global_len); |
| 139 | tc->vector = global_vec; |
| 140 | } |
| 141 | |
| 142 | static void setup4(struct test_case_t *tc) |
| 143 | { |
| 144 | struct rlimit as_lim; |
| 145 | |
| 146 | if (getrlimit(RLIMIT_AS, &as_lim) == -1) { |
| 147 | tst_brkm(TBROK | TERRNO, cleanup, |
| 148 | "getrlimit(RLIMIT_AS) failed"); |
| 149 | } |
| 150 | |
| 151 | tc->addr = global_pointer; |
| 152 | tc->len = as_lim.rlim_cur - (rlim_t)global_pointer + PAGESIZE; |
| 153 | tc->vector = global_vec; |
| 154 | |
| 155 | /* |
| 156 | * In linux 2.6.11 and earlier, EINVAL was returned for this condition. |
| 157 | */ |
| 158 | if (tst_kvercmp(2, 6, 11) <= 0) |
| 159 | tc->exp_errno = EINVAL; |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 162 | static void setup(void) |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 163 | { |
mridge | 051f423 | 2004-09-13 21:59:01 +0000 | [diff] [blame] | 164 | char *buf; |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 165 | int fd; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 166 | |
mridge | 051f423 | 2004-09-13 21:59:01 +0000 | [diff] [blame] | 167 | PAGESIZE = getpagesize(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 168 | |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 169 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 170 | |
Cyril Hrubis | 8e9b997 | 2013-04-24 14:22:24 +0200 | [diff] [blame] | 171 | tst_tmpdir(); |
| 172 | |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 173 | TEST_PAUSE; |
| 174 | |
mridge | 051f423 | 2004-09-13 21:59:01 +0000 | [diff] [blame] | 175 | /* global_pointer will point to a mmapped area of global_len bytes */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 176 | global_len = PAGESIZE * 2; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 177 | |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 178 | buf = SAFE_MALLOC(cleanup, global_len); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 179 | memset(buf, 42, global_len); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 180 | |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 181 | fd = SAFE_OPEN(cleanup, "mincore01", O_CREAT | O_RDWR, |
| 182 | S_IRUSR | S_IWUSR); |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 183 | |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 184 | SAFE_WRITE(cleanup, 1, fd, buf, global_len); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 185 | |
mridge | 051f423 | 2004-09-13 21:59:01 +0000 | [diff] [blame] | 186 | free(buf); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 187 | |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 188 | global_pointer = SAFE_MMAP(cleanup, NULL, global_len * 2, |
| 189 | PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 190 | |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 191 | global_vec = SAFE_MALLOC(cleanup, |
| 192 | (global_len + PAGESIZE - 1) / PAGESIZE); |
| 193 | |
| 194 | SAFE_CLOSE(cleanup, fd); |
| 195 | } |
| 196 | |
| 197 | static void mincore_verify(struct test_case_t *tc) |
| 198 | { |
| 199 | if (tc->setupfunc) |
| 200 | tc->setupfunc(tc); |
| 201 | |
| 202 | TEST(mincore(tc->addr, tc->len, tc->vector)); |
| 203 | |
| 204 | if (TEST_RETURN != -1) { |
| 205 | tst_resm(TFAIL, "succeeded unexpectedly"); |
| 206 | return; |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 207 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 208 | |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 209 | if (TEST_ERRNO == tc->exp_errno) { |
| 210 | tst_resm(TPASS | TTERRNO, "failed as expected"); |
| 211 | } else { |
| 212 | tst_resm(TFAIL | TTERRNO, |
| 213 | "mincore failed unexpectedly; expected: " |
| 214 | "%d - %s", tc->exp_errno, |
| 215 | strerror(tc->exp_errno)); |
| 216 | } |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Cyril Hrubis | 123895c | 2013-04-24 13:34:46 +0200 | [diff] [blame] | 219 | static void cleanup(void) |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 220 | { |
mridge | 051f423 | 2004-09-13 21:59:01 +0000 | [diff] [blame] | 221 | free(global_vec); |
Xiaoguang Wang | e3280af | 2014-02-25 13:00:01 +0800 | [diff] [blame] | 222 | |
| 223 | if (munmap(global_pointer, global_len) == -1) |
| 224 | tst_resm(TWARN | TERRNO, "munmap failed"); |
| 225 | |
Cyril Hrubis | 8e9b997 | 2013-04-24 14:22:24 +0200 | [diff] [blame] | 226 | tst_rmdir(); |
mridge | 56130dc | 2004-08-24 20:30:46 +0000 | [diff] [blame] | 227 | } |