1 /*
2 * Copyright (c) International Business Machines Corp., 2004
3 * Copyright (c) Linux Test Project, 2013-2016
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
16 /*
17 * This is a test for the madvise(2) system call. It is intended
18 * to provide a complete exposure of the system call. It tests
19 * madvise(2) for all error conditions to occur correctly.
20 *
21 * (A) Test Case for EINVAL
22 * 1. start is not page-aligned
23 * 2. advice is not a valid value
24 * 3. application is attempting to release
25 * locked or shared pages (with MADV_DONTNEED)
26 * 4. MADV_MERGEABLE or MADV_UNMERGEABLE was specified in advice,
27 * but the kernel was not configured with CONFIG_KSM.
28 * 8|9. The MADV_FREE & MADV_WIPEONFORK operation can be applied
29 * only to private anonymous pages.
30 *
31 * (B) Test Case for ENOMEM
32 * 5|6. addresses in the specified range are not currently mapped
33 * or are outside the address space of the process
34 * b. Not enough memory - paging in failed
35 *
36 * (C) Test Case for EBADF
37 * 7. the map exists,
38 * but the area maps something that isn't a file.
39 */
40
41 #include <sys/types.h>
42 #include <sys/mman.h>
43 #include <sys/resource.h>
44 #include <sys/stat.h>
45 #include <sys/time.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52
53 #include "tst_test.h"
54 #include "lapi/mmap.h"
55
56 #define MAP_SIZE (4 * 1024)
57 #define TEST_FILE "testfile"
58 #define STR "abcdefghijklmnopqrstuvwxyz12345\n"
59 #define KSM_SYS_DIR "/sys/kernel/mm/ksm"
60
61 static struct stat st;
62 static long pagesize;
63 static char *file1;
64 static char *file2;
65 static char *file3;
66 static char *shared_anon;
67 static char *ptr_addr;
68 static char *tmp_addr;
69 static char *nonalign;
70
71 static struct tcase {
72 int advice;
73 char *name;
74 char **addr;
75 int exp_errno;
76 int skip;
77 } tcases[] = {
78 {MADV_NORMAL, "MADV_NORMAL", &nonalign, EINVAL, 0},
79 {1212, "MADV_NORMAL", &file1, EINVAL, 0},
80 {MADV_REMOVE, "MADV_REMOVE", &file1, EINVAL, 0},
81 {MADV_DONTNEED, "MADV_DONTNEED", &file1, EINVAL, 1},
82 {MADV_MERGEABLE, "MADV_MERGEABLE", &file1, EINVAL, 0},
83 {MADV_UNMERGEABLE, "MADV_UNMERGEABLE", &file1, EINVAL, 0},
84 {MADV_NORMAL, "MADV_NORMAL", &file2, ENOMEM, 0},
85 {MADV_WILLNEED, "MADV_WILLNEED", &file2, ENOMEM, 0},
86 {MADV_WILLNEED, "MADV_WILLNEED", &tmp_addr, EBADF, 0},
87 {MADV_FREE, "MADV_FREE", &file1, EINVAL, 0},
88 {MADV_WIPEONFORK, "MADV_WIPEONFORK", &file1, EINVAL, 0},
89 {MADV_WIPEONFORK, "MADV_WIPEONFORK shared_anon", &shared_anon, EINVAL, 0},
90 {MADV_WIPEONFORK, "MADV_WIPEONFORK private file backed", &file3, EINVAL, 0},
91 };
92
tcases_filter(void)93 static void tcases_filter(void)
94 {
95 unsigned int i;
96
97 for (i = 0; i < ARRAY_SIZE(tcases); i++) {
98 struct tcase *tc = &tcases[i];
99
100 switch (tc->advice) {
101 case MADV_DONTNEED:
102 #if !defined(UCLINUX)
103 if (mlock(file1, st.st_size) < 0)
104 tst_brk(TBROK | TERRNO, "mlock failed");
105 tc->skip = 0;
106 #endif /* if !defined(UCLINUX) */
107 break;
108 case MADV_REMOVE:
109 if ((tst_kvercmp(2, 6, 16)) < 0)
110 tc->skip = 1;
111 break;
112 case MADV_MERGEABLE:
113 case MADV_UNMERGEABLE:
114 if ((tst_kvercmp(2, 6, 32)) < 0)
115 tc->skip = 1;
116
117 /* kernel configured with CONFIG_KSM,
118 * skip EINVAL test for MADV_MERGEABLE. */
119 if (access(KSM_SYS_DIR, F_OK) == 0)
120 tc->skip = 1;
121 break;
122 case MADV_WILLNEED:
123 /* In kernel commit 1998cc0, madvise(MADV_WILLNEED) to
124 * anon mem doesn't return -EBADF now, as now we support
125 * swap prefretch. */
126 if ((tst_kvercmp(3, 9, 0)) > 0 &&
127 tc->exp_errno == EBADF)
128 tc->skip = 1;
129 break;
130 case MADV_FREE:
131 if ((tst_kvercmp(4, 5, 0)) < 0)
132 tc->skip = 1;
133 break;
134 case MADV_WIPEONFORK:
135 if ((tst_kvercmp(4, 14, 0)) < 0)
136 tc->skip = 1;
137 break;
138 default:
139 break;
140 }
141 }
142 }
143
setup(void)144 static void setup(void)
145 {
146 int i, fd;
147
148 fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0664);
149
150 pagesize = getpagesize();
151
152 /* Writing 16 pages of random data into this file */
153 for (i = 0; i < (pagesize / 2); i++)
154 SAFE_WRITE(1, fd, STR, sizeof(STR) - 1);
155
156 SAFE_FSTAT(fd, &st);
157
158 file1 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
159 file2 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
160 file3 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
161 shared_anon = SAFE_MMAP(0, MAP_SIZE, PROT_READ, MAP_SHARED |
162 MAP_ANONYMOUS, -1, 0);
163
164 nonalign = file1 + 100;
165
166 ptr_addr = SAFE_MALLOC(st.st_size);
167 tmp_addr = (void*)LTP_ALIGN((long)ptr_addr, pagesize);
168
169 /* unmap as last step to avoid subsequent mmap(s) pick same address */
170 SAFE_MUNMAP(file2 + st.st_size - pagesize, pagesize);
171 SAFE_CLOSE(fd);
172
173 tcases_filter();
174 }
175
176
advice_test(unsigned int i)177 static void advice_test(unsigned int i)
178 {
179 struct tcase *tc = &tcases[i];
180
181 if (tc->skip == 1) {
182 tst_res(TCONF, "%s is not supported", tc->name);
183 return;
184 }
185
186 TEST(madvise(*(tc->addr), st.st_size, tc->advice));
187 if (TST_RET == -1) {
188 if (TST_ERR == tc->exp_errno) {
189 tst_res(TPASS | TTERRNO, "%s failed as expected", tc->name);
190 } else {
191 tst_res(TFAIL | TTERRNO,
192 "%s failed unexpectedly; expected - %d : %s",
193 tc->name, tc->exp_errno,
194 tst_strerrno(TFAIL | TTERRNO));
195 }
196 } else {
197 tst_res(TFAIL, "madvise succeeded unexpectedly");
198 }
199 }
200
cleanup(void)201 static void cleanup(void)
202 {
203 free(ptr_addr);
204 SAFE_MUNMAP(file1, st.st_size);
205 SAFE_MUNMAP(file2, st.st_size - pagesize);
206 SAFE_MUNMAP(file3, st.st_size);
207 SAFE_MUNMAP(shared_anon, MAP_SIZE);
208 }
209
210 static struct tst_test test = {
211 .tcnt = ARRAY_SIZE(tcases),
212 .test = advice_test,
213 .needs_tmpdir = 1,
214 .needs_root = 1,
215 .setup = setup,
216 .cleanup = cleanup,
217 };
218