blob: 65220c754bcfe1e1eeb0faad09443406dc92dc6e [file] [log] [blame]
mridge56130dc2004-08-24 20:30:46 +00001/*
Cyril Hrubis123895c2013-04-24 13:34:46 +02002 * 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>
mridge56130dc2004-08-24 20:30:46 +00006 *
Cyril Hrubis123895c2013-04-24 13:34:46 +02007 * 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.
mridge56130dc2004-08-24 20:30:46 +000011 *
Cyril Hrubis123895c2013-04-24 13:34:46 +020012 * 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.
mridge56130dc2004-08-24 20:30:46 +000016 *
Cyril Hrubis123895c2013-04-24 13:34:46 +020017 * 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
mridge56130dc2004-08-24 20:30:46 +000020 */
21
22/*
Cyril Hrubis123895c2013-04-24 13:34:46 +020023 * 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 Wange3280af2014-02-25 13:00:01 +080031 * 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.
mridge56130dc2004-08-24 20:30:46 +000035 */
36
37#include <fcntl.h>
38#include <errno.h>
39#include <sys/mman.h>
40#include <stdlib.h>
mridge56130dc2004-08-24 20:30:46 +000041#include <unistd.h>
subrata_modakf798c212007-12-13 10:25:18 +000042#include <sys/resource.h>
mridge56130dc2004-08-24 20:30:46 +000043#include <sys/types.h>
44#include <sys/stat.h>
45#include "test.h"
Xiaoguang Wange3280af2014-02-25 13:00:01 +080046#include "safe_macros.h"
mridge56130dc2004-08-24 20:30:46 +000047
mridge051f4232004-09-13 21:59:01 +000048static int PAGESIZE;
subrata_modakf798c212007-12-13 10:25:18 +000049static rlim_t STACK_LIMIT = 10485760;
mridge56130dc2004-08-24 20:30:46 +000050
mridge051f4232004-09-13 21:59:01 +000051static void cleanup(void);
52static void setup(void);
mridge56130dc2004-08-24 20:30:46 +000053
subrata_modak56207ce2009-03-23 13:35:39 +000054char *TCID = "mincore01";
mridge56130dc2004-08-24 20:30:46 +000055
Xiaoguang Wange3280af2014-02-25 13:00:01 +080056static char *global_pointer;
57static unsigned char *global_vec;
58static int global_len;
59
60struct test_case_t;
61static void setup1(struct test_case_t *tc);
62static void setup2(struct test_case_t *tc);
63static void setup3(struct test_case_t *tc);
64static void setup4(struct test_case_t *tc);
mridge56130dc2004-08-24 20:30:46 +000065
mridge051f4232004-09-13 21:59:01 +000066static struct test_case_t {
subrata_modak56207ce2009-03-23 13:35:39 +000067 char *addr;
Xiaoguang Wange3280af2014-02-25 13:00:01 +080068 size_t len;
vapier33acea52006-05-26 06:34:45 +000069 unsigned char *vector;
mridge051f4232004-09-13 21:59:01 +000070 int exp_errno;
Xiaoguang Wange3280af2014-02-25 13:00:01 +080071 void (*setupfunc) (struct test_case_t *tc);
mridge56130dc2004-08-24 20:30:46 +000072} TC[] = {
Cyril Hrubis123895c2013-04-24 13:34:46 +020073 {NULL, 0, NULL, EINVAL, setup1},
74 {NULL, 0, NULL, EFAULT, setup2},
75 {NULL, 0, NULL, ENOMEM, setup3},
Xiaoguang Wange3280af2014-02-25 13:00:01 +080076 {NULL, 0, NULL, ENOMEM, setup4}
Cyril Hrubis123895c2013-04-24 13:34:46 +020077};
mridge56130dc2004-08-24 20:30:46 +000078
Xiaoguang Wange3280af2014-02-25 13:00:01 +080079static void mincore_verify(struct test_case_t *tc);
80
81int TST_TOTAL = ARRAY_SIZE(TC);
82
mridge56130dc2004-08-24 20:30:46 +000083int main(int ac, char **av)
84{
Xiaoguang Wange3280af2014-02-25 13:00:01 +080085 int i, lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020086 const char *msg;
mridge56130dc2004-08-24 20:30:46 +000087
Cyril Hrubis123895c2013-04-24 13:34:46 +020088 msg = parse_opts(ac, av, NULL, NULL);
Cyril Hrubis123895c2013-04-24 13:34:46 +020089 if (msg != NULL)
mridge051f4232004-09-13 21:59:01 +000090 tst_brkm(TBROK, cleanup, "error parsing options: %s", msg);
mridge56130dc2004-08-24 20:30:46 +000091
Cyril Hrubis123895c2013-04-24 13:34:46 +020092 setup();
mridge56130dc2004-08-24 20:30:46 +000093
mridge56130dc2004-08-24 20:30:46 +000094 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080095 tst_count = 0;
mridge56130dc2004-08-24 20:30:46 +000096
Xiaoguang Wange3280af2014-02-25 13:00:01 +080097 for (i = 0; i < TST_TOTAL; i++)
98 mincore_verify(&TC[i]);
mridge56130dc2004-08-24 20:30:46 +000099 }
Xiaoguang Wange3280af2014-02-25 13:00:01 +0800100
subrata_modak56207ce2009-03-23 13:35:39 +0000101 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800102 tst_exit();
mridge56130dc2004-08-24 20:30:46 +0000103}
104
Xiaoguang Wange3280af2014-02-25 13:00:01 +0800105static void setup1(struct test_case_t *tc)
mridge56130dc2004-08-24 20:30:46 +0000106{
Xiaoguang Wange3280af2014-02-25 13:00:01 +0800107 tc->addr = global_pointer + 1;
108 tc->len = global_len;
109 tc->vector = global_vec;
mridge56130dc2004-08-24 20:30:46 +0000110}
111
Xiaoguang Wange3280af2014-02-25 13:00:01 +0800112void setup2(struct test_case_t *tc)
mridge56130dc2004-08-24 20:30:46 +0000113{
vapier33acea52006-05-26 06:34:45 +0000114 unsigned char *t;
subrata_modak56207ce2009-03-23 13:35:39 +0000115 struct rlimit limit;
subrata_modakf798c212007-12-13 10:25:18 +0000116
Xiaoguang Wange3280af2014-02-25 13:00:01 +0800117 t = SAFE_MMAP(cleanup, NULL, global_len, PROT_READ | PROT_WRITE,
118 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Cyril Hrubis123895c2013-04-24 13:34:46 +0200119
Xiaoguang Wange3280af2014-02-25 13:00:01 +0800120 SAFE_MUNMAP(cleanup, t, global_len);
subrata_modakf798c212007-12-13 10:25:18 +0000121
subrata_modak56207ce2009-03-23 13:35:39 +0000122 /* 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_modakf798c212007-12-13 10:25:18 +0000125
Cyril Hrubis123895c2013-04-24 13:34:46 +0200126 if (setrlimit(RLIMIT_STACK, &limit) != 0)
127 tst_brkm(TBROK | TERRNO, cleanup, "setrlimit failed");
subrata_modakf798c212007-12-13 10:25:18 +0000128
Xiaoguang Wange3280af2014-02-25 13:00:01 +0800129 tc->addr = global_pointer;
130 tc->len = global_len;
131 tc->vector = t;
mridge56130dc2004-08-24 20:30:46 +0000132}
133
Xiaoguang Wange3280af2014-02-25 13:00:01 +0800134static void setup3(struct test_case_t *tc)
mridge56130dc2004-08-24 20:30:46 +0000135{
Xiaoguang Wange3280af2014-02-25 13:00:01 +0800136 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
142static 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;
mridge56130dc2004-08-24 20:30:46 +0000160}
161
Cyril Hrubis123895c2013-04-24 13:34:46 +0200162static void setup(void)
mridge56130dc2004-08-24 20:30:46 +0000163{
mridge051f4232004-09-13 21:59:01 +0000164 char *buf;
Xiaoguang Wange3280af2014-02-25 13:00:01 +0800165 int fd;
subrata_modakbdbaec52009-02-26 12:14:51 +0000166
mridge051f4232004-09-13 21:59:01 +0000167 PAGESIZE = getpagesize();
subrata_modakbdbaec52009-02-26 12:14:51 +0000168
Xiaoguang Wange3280af2014-02-25 13:00:01 +0800169 tst_sig(NOFORK, DEF_HANDLER, cleanup);
170
Cyril Hrubis8e9b9972013-04-24 14:22:24 +0200171 tst_tmpdir();
172
Xiaoguang Wange3280af2014-02-25 13:00:01 +0800173 TEST_PAUSE;
174
mridge051f4232004-09-13 21:59:01 +0000175 /* global_pointer will point to a mmapped area of global_len bytes */
subrata_modak56207ce2009-03-23 13:35:39 +0000176 global_len = PAGESIZE * 2;
subrata_modakbdbaec52009-02-26 12:14:51 +0000177
Xiaoguang Wange3280af2014-02-25 13:00:01 +0800178 buf = SAFE_MALLOC(cleanup, global_len);
subrata_modak56207ce2009-03-23 13:35:39 +0000179 memset(buf, 42, global_len);
subrata_modakbdbaec52009-02-26 12:14:51 +0000180
Xiaoguang Wange3280af2014-02-25 13:00:01 +0800181 fd = SAFE_OPEN(cleanup, "mincore01", O_CREAT | O_RDWR,
182 S_IRUSR | S_IWUSR);
mridge56130dc2004-08-24 20:30:46 +0000183
Xiaoguang Wange3280af2014-02-25 13:00:01 +0800184 SAFE_WRITE(cleanup, 1, fd, buf, global_len);
subrata_modakbdbaec52009-02-26 12:14:51 +0000185
mridge051f4232004-09-13 21:59:01 +0000186 free(buf);
subrata_modakbdbaec52009-02-26 12:14:51 +0000187
Xiaoguang Wange3280af2014-02-25 13:00:01 +0800188 global_pointer = SAFE_MMAP(cleanup, NULL, global_len * 2,
189 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Cyril Hrubis123895c2013-04-24 13:34:46 +0200190
Xiaoguang Wange3280af2014-02-25 13:00:01 +0800191 global_vec = SAFE_MALLOC(cleanup,
192 (global_len + PAGESIZE - 1) / PAGESIZE);
193
194 SAFE_CLOSE(cleanup, fd);
195}
196
197static 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;
mridge56130dc2004-08-24 20:30:46 +0000207 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000208
Xiaoguang Wange3280af2014-02-25 13:00:01 +0800209 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 }
mridge56130dc2004-08-24 20:30:46 +0000217}
218
Cyril Hrubis123895c2013-04-24 13:34:46 +0200219static void cleanup(void)
mridge56130dc2004-08-24 20:30:46 +0000220{
mridge051f4232004-09-13 21:59:01 +0000221 free(global_vec);
Xiaoguang Wange3280af2014-02-25 13:00:01 +0800222
223 if (munmap(global_pointer, global_len) == -1)
224 tst_resm(TWARN | TERRNO, "munmap failed");
225
Cyril Hrubis8e9b9972013-04-24 14:22:24 +0200226 tst_rmdir();
mridge56130dc2004-08-24 20:30:46 +0000227}