Commit | Line | Data |
---|---|---|
fddc8f81 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
362aecb2 | 2 | |
1da02f51 WT |
3 | #define _GNU_SOURCE |
4 | ||
362aecb2 WT |
5 | /* platform-specific include files coming from the compiler */ |
6 | #include <limits.h> | |
7 | ||
8 | /* libc-specific include files | |
1da02f51 | 9 | * The program may be built in 3 ways: |
362aecb2 | 10 | * $(CC) -nostdlib -include /path/to/nolibc.h => NOLIBC already defined |
1da02f51 WT |
11 | * $(CC) -nostdlib -I/path/to/nolibc/sysroot => _NOLIBC_* guards are present |
12 | * $(CC) with default libc => NOLIBC* never defined | |
362aecb2 WT |
13 | */ |
14 | #ifndef NOLIBC | |
15 | #include <stdio.h> | |
16 | #include <stdlib.h> | |
17 | #include <string.h> | |
1da02f51 WT |
18 | #ifndef _NOLIBC_STDIO_H |
19 | /* standard libcs need more includes */ | |
20 | #include <linux/reboot.h> | |
21 | #include <sys/io.h> | |
22 | #include <sys/ioctl.h> | |
69f2cd9f | 23 | #include <sys/mman.h> |
1da02f51 | 24 | #include <sys/mount.h> |
208aa9d9 | 25 | #include <sys/prctl.h> |
1da02f51 WT |
26 | #include <sys/reboot.h> |
27 | #include <sys/stat.h> | |
28 | #include <sys/syscall.h> | |
29 | #include <sys/sysmacros.h> | |
30 | #include <sys/time.h> | |
31 | #include <sys/wait.h> | |
32 | #include <dirent.h> | |
33 | #include <errno.h> | |
34 | #include <fcntl.h> | |
35 | #include <poll.h> | |
36 | #include <sched.h> | |
37 | #include <signal.h> | |
38 | #include <stdarg.h> | |
2df07fc5 WT |
39 | #include <stddef.h> |
40 | #include <stdint.h> | |
1da02f51 WT |
41 | #include <unistd.h> |
42 | #endif | |
362aecb2 WT |
43 | #endif |
44 | ||
45 | /* will be used by nolibc by getenv() */ | |
46 | char **environ; | |
47 | ||
23da7bc9 WT |
48 | /* definition of a series of tests */ |
49 | struct test { | |
fddc8f81 TW |
50 | const char *name; /* test name */ |
51 | int (*func)(int min, int max); /* handler */ | |
23da7bc9 WT |
52 | }; |
53 | ||
1da02f51 WT |
54 | #ifndef _NOLIBC_STDLIB_H |
55 | char *itoa(int i) | |
56 | { | |
57 | static char buf[12]; | |
58 | int ret; | |
59 | ||
60 | ret = snprintf(buf, sizeof(buf), "%d", i); | |
61 | return (ret >= 0 && ret < sizeof(buf)) ? buf : "#err"; | |
62 | } | |
63 | #endif | |
64 | ||
362aecb2 WT |
65 | #define CASE_ERR(err) \ |
66 | case err: return #err | |
67 | ||
68 | /* returns the error name (e.g. "ENOENT") for common errors, "SUCCESS" for 0, | |
69 | * or the decimal value for less common ones. | |
70 | */ | |
71 | const char *errorname(int err) | |
72 | { | |
73 | switch (err) { | |
74 | case 0: return "SUCCESS"; | |
75 | CASE_ERR(EPERM); | |
76 | CASE_ERR(ENOENT); | |
77 | CASE_ERR(ESRCH); | |
78 | CASE_ERR(EINTR); | |
79 | CASE_ERR(EIO); | |
80 | CASE_ERR(ENXIO); | |
81 | CASE_ERR(E2BIG); | |
82 | CASE_ERR(ENOEXEC); | |
83 | CASE_ERR(EBADF); | |
84 | CASE_ERR(ECHILD); | |
85 | CASE_ERR(EAGAIN); | |
86 | CASE_ERR(ENOMEM); | |
87 | CASE_ERR(EACCES); | |
88 | CASE_ERR(EFAULT); | |
89 | CASE_ERR(ENOTBLK); | |
90 | CASE_ERR(EBUSY); | |
91 | CASE_ERR(EEXIST); | |
92 | CASE_ERR(EXDEV); | |
93 | CASE_ERR(ENODEV); | |
94 | CASE_ERR(ENOTDIR); | |
95 | CASE_ERR(EISDIR); | |
96 | CASE_ERR(EINVAL); | |
97 | CASE_ERR(ENFILE); | |
98 | CASE_ERR(EMFILE); | |
99 | CASE_ERR(ENOTTY); | |
100 | CASE_ERR(ETXTBSY); | |
101 | CASE_ERR(EFBIG); | |
102 | CASE_ERR(ENOSPC); | |
103 | CASE_ERR(ESPIPE); | |
104 | CASE_ERR(EROFS); | |
105 | CASE_ERR(EMLINK); | |
106 | CASE_ERR(EPIPE); | |
107 | CASE_ERR(EDOM); | |
108 | CASE_ERR(ERANGE); | |
109 | CASE_ERR(ENOSYS); | |
758f970f | 110 | CASE_ERR(EOVERFLOW); |
362aecb2 WT |
111 | default: |
112 | return itoa(err); | |
113 | } | |
114 | } | |
115 | ||
443de903 TW |
116 | static void putcharn(char c, size_t n) |
117 | { | |
118 | char buf[64]; | |
119 | ||
120 | memset(buf, c, n); | |
121 | buf[n] = '\0'; | |
122 | fputs(buf, stdout); | |
123 | } | |
124 | ||
362aecb2 WT |
125 | static int pad_spc(int llen, int cnt, const char *fmt, ...) |
126 | { | |
127 | va_list args; | |
362aecb2 WT |
128 | int ret; |
129 | ||
443de903 | 130 | putcharn(' ', cnt - llen); |
362aecb2 WT |
131 | |
132 | va_start(args, fmt); | |
133 | ret = vfprintf(stdout, fmt, args); | |
134 | va_end(args); | |
443de903 | 135 | return ret < 0 ? ret : ret + cnt - llen; |
362aecb2 WT |
136 | } |
137 | ||
138 | /* The tests below are intended to be used by the macroes, which evaluate | |
139 | * expression <expr>, print the status to stdout, and update the "ret" | |
140 | * variable to count failures. The functions themselves return the number | |
141 | * of failures, thus either 0 or 1. | |
142 | */ | |
143 | ||
144 | #define EXPECT_ZR(cond, expr) \ | |
a0994fb9 | 145 | do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_zr(expr, llen); } while (0) |
362aecb2 WT |
146 | |
147 | static int expect_zr(int expr, int llen) | |
148 | { | |
149 | int ret = !(expr == 0); | |
150 | ||
151 | llen += printf(" = %d ", expr); | |
a0994fb9 | 152 | pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n"); |
362aecb2 WT |
153 | return ret; |
154 | } | |
155 | ||
156 | ||
157 | #define EXPECT_NZ(cond, expr, val) \ | |
a0994fb9 | 158 | do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_nz(expr, llen; } while (0) |
362aecb2 WT |
159 | |
160 | static int expect_nz(int expr, int llen) | |
161 | { | |
162 | int ret = !(expr != 0); | |
163 | ||
164 | llen += printf(" = %d ", expr); | |
a0994fb9 | 165 | pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n"); |
362aecb2 WT |
166 | return ret; |
167 | } | |
168 | ||
169 | ||
170 | #define EXPECT_EQ(cond, expr, val) \ | |
a0994fb9 | 171 | do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_eq(expr, llen, val); } while (0) |
362aecb2 | 172 | |
a0994fb9 | 173 | static int expect_eq(uint64_t expr, int llen, uint64_t val) |
362aecb2 WT |
174 | { |
175 | int ret = !(expr == val); | |
176 | ||
a0994fb9 VD |
177 | llen += printf(" = %lld ", expr); |
178 | pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n"); | |
362aecb2 WT |
179 | return ret; |
180 | } | |
181 | ||
182 | ||
183 | #define EXPECT_NE(cond, expr, val) \ | |
a0994fb9 | 184 | do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ne(expr, llen, val); } while (0) |
362aecb2 WT |
185 | |
186 | static int expect_ne(int expr, int llen, int val) | |
187 | { | |
188 | int ret = !(expr != val); | |
189 | ||
190 | llen += printf(" = %d ", expr); | |
a0994fb9 | 191 | pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n"); |
362aecb2 WT |
192 | return ret; |
193 | } | |
194 | ||
195 | ||
196 | #define EXPECT_GE(cond, expr, val) \ | |
a0994fb9 | 197 | do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ge(expr, llen, val); } while (0) |
362aecb2 WT |
198 | |
199 | static int expect_ge(int expr, int llen, int val) | |
200 | { | |
201 | int ret = !(expr >= val); | |
202 | ||
203 | llen += printf(" = %d ", expr); | |
a0994fb9 | 204 | pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n"); |
362aecb2 WT |
205 | return ret; |
206 | } | |
207 | ||
208 | ||
209 | #define EXPECT_GT(cond, expr, val) \ | |
a0994fb9 | 210 | do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_gt(expr, llen, val); } while (0) |
362aecb2 WT |
211 | |
212 | static int expect_gt(int expr, int llen, int val) | |
213 | { | |
214 | int ret = !(expr > val); | |
215 | ||
216 | llen += printf(" = %d ", expr); | |
a0994fb9 | 217 | pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n"); |
362aecb2 WT |
218 | return ret; |
219 | } | |
220 | ||
221 | ||
222 | #define EXPECT_LE(cond, expr, val) \ | |
a0994fb9 | 223 | do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_le(expr, llen, val); } while (0) |
362aecb2 WT |
224 | |
225 | static int expect_le(int expr, int llen, int val) | |
226 | { | |
227 | int ret = !(expr <= val); | |
228 | ||
229 | llen += printf(" = %d ", expr); | |
a0994fb9 | 230 | pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n"); |
362aecb2 WT |
231 | return ret; |
232 | } | |
233 | ||
234 | ||
235 | #define EXPECT_LT(cond, expr, val) \ | |
a0994fb9 | 236 | do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_lt(expr, llen, val); } while (0) |
362aecb2 WT |
237 | |
238 | static int expect_lt(int expr, int llen, int val) | |
239 | { | |
240 | int ret = !(expr < val); | |
241 | ||
242 | llen += printf(" = %d ", expr); | |
a0994fb9 | 243 | pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n"); |
362aecb2 WT |
244 | return ret; |
245 | } | |
246 | ||
247 | ||
248 | #define EXPECT_SYSZR(cond, expr) \ | |
a0994fb9 | 249 | do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_syszr(expr, llen); } while (0) |
362aecb2 WT |
250 | |
251 | static int expect_syszr(int expr, int llen) | |
252 | { | |
253 | int ret = 0; | |
254 | ||
255 | if (expr) { | |
256 | ret = 1; | |
257 | llen += printf(" = %d %s ", expr, errorname(errno)); | |
a0994fb9 | 258 | llen += pad_spc(llen, 64, "[FAIL]\n"); |
362aecb2 WT |
259 | } else { |
260 | llen += printf(" = %d ", expr); | |
a0994fb9 | 261 | llen += pad_spc(llen, 64, " [OK]\n"); |
362aecb2 WT |
262 | } |
263 | return ret; | |
264 | } | |
265 | ||
266 | ||
267 | #define EXPECT_SYSEQ(cond, expr, val) \ | |
a0994fb9 | 268 | do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_syseq(expr, llen, val); } while (0) |
362aecb2 WT |
269 | |
270 | static int expect_syseq(int expr, int llen, int val) | |
271 | { | |
272 | int ret = 0; | |
273 | ||
274 | if (expr != val) { | |
275 | ret = 1; | |
276 | llen += printf(" = %d %s ", expr, errorname(errno)); | |
a0994fb9 | 277 | llen += pad_spc(llen, 64, "[FAIL]\n"); |
362aecb2 WT |
278 | } else { |
279 | llen += printf(" = %d ", expr); | |
a0994fb9 | 280 | llen += pad_spc(llen, 64, " [OK]\n"); |
362aecb2 WT |
281 | } |
282 | return ret; | |
283 | } | |
284 | ||
285 | ||
286 | #define EXPECT_SYSNE(cond, expr, val) \ | |
a0994fb9 | 287 | do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_sysne(expr, llen, val); } while (0) |
362aecb2 WT |
288 | |
289 | static int expect_sysne(int expr, int llen, int val) | |
290 | { | |
291 | int ret = 0; | |
292 | ||
293 | if (expr == val) { | |
294 | ret = 1; | |
295 | llen += printf(" = %d %s ", expr, errorname(errno)); | |
a0994fb9 | 296 | llen += pad_spc(llen, 64, "[FAIL]\n"); |
362aecb2 WT |
297 | } else { |
298 | llen += printf(" = %d ", expr); | |
a0994fb9 | 299 | llen += pad_spc(llen, 64, " [OK]\n"); |
362aecb2 WT |
300 | } |
301 | return ret; | |
302 | } | |
303 | ||
304 | ||
305 | #define EXPECT_SYSER(cond, expr, expret, experr) \ | |
a0994fb9 | 306 | do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_syserr(expr, expret, experr, llen); } while (0) |
362aecb2 WT |
307 | |
308 | static int expect_syserr(int expr, int expret, int experr, int llen) | |
309 | { | |
310 | int ret = 0; | |
311 | int _errno = errno; | |
312 | ||
313 | llen += printf(" = %d %s ", expr, errorname(_errno)); | |
314 | if (expr != expret || _errno != experr) { | |
315 | ret = 1; | |
316 | llen += printf(" != (%d %s) ", expret, errorname(experr)); | |
a0994fb9 | 317 | llen += pad_spc(llen, 64, "[FAIL]\n"); |
362aecb2 | 318 | } else { |
a0994fb9 | 319 | llen += pad_spc(llen, 64, " [OK]\n"); |
362aecb2 WT |
320 | } |
321 | return ret; | |
322 | } | |
323 | ||
324 | ||
325 | #define EXPECT_PTRZR(cond, expr) \ | |
a0994fb9 | 326 | do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ptrzr(expr, llen); } while (0) |
362aecb2 WT |
327 | |
328 | static int expect_ptrzr(const void *expr, int llen) | |
329 | { | |
330 | int ret = 0; | |
331 | ||
332 | llen += printf(" = <%p> ", expr); | |
333 | if (expr) { | |
334 | ret = 1; | |
a0994fb9 | 335 | llen += pad_spc(llen, 64, "[FAIL]\n"); |
362aecb2 | 336 | } else { |
a0994fb9 | 337 | llen += pad_spc(llen, 64, " [OK]\n"); |
362aecb2 WT |
338 | } |
339 | return ret; | |
340 | } | |
341 | ||
342 | ||
343 | #define EXPECT_PTRNZ(cond, expr) \ | |
a0994fb9 | 344 | do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ptrnz(expr, llen); } while (0) |
362aecb2 WT |
345 | |
346 | static int expect_ptrnz(const void *expr, int llen) | |
347 | { | |
348 | int ret = 0; | |
349 | ||
350 | llen += printf(" = <%p> ", expr); | |
351 | if (!expr) { | |
352 | ret = 1; | |
a0994fb9 | 353 | llen += pad_spc(llen, 64, "[FAIL]\n"); |
362aecb2 | 354 | } else { |
a0994fb9 | 355 | llen += pad_spc(llen, 64, " [OK]\n"); |
362aecb2 WT |
356 | } |
357 | return ret; | |
358 | } | |
359 | ||
360 | ||
361 | #define EXPECT_STRZR(cond, expr) \ | |
a0994fb9 | 362 | do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_strzr(expr, llen); } while (0) |
362aecb2 WT |
363 | |
364 | static int expect_strzr(const char *expr, int llen) | |
365 | { | |
366 | int ret = 0; | |
367 | ||
368 | llen += printf(" = <%s> ", expr); | |
369 | if (expr) { | |
370 | ret = 1; | |
a0994fb9 | 371 | llen += pad_spc(llen, 64, "[FAIL]\n"); |
362aecb2 | 372 | } else { |
a0994fb9 | 373 | llen += pad_spc(llen, 64, " [OK]\n"); |
362aecb2 WT |
374 | } |
375 | return ret; | |
376 | } | |
377 | ||
378 | ||
379 | #define EXPECT_STRNZ(cond, expr) \ | |
a0994fb9 | 380 | do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_strnz(expr, llen); } while (0) |
362aecb2 WT |
381 | |
382 | static int expect_strnz(const char *expr, int llen) | |
383 | { | |
384 | int ret = 0; | |
385 | ||
386 | llen += printf(" = <%s> ", expr); | |
387 | if (!expr) { | |
388 | ret = 1; | |
a0994fb9 | 389 | llen += pad_spc(llen, 64, "[FAIL]\n"); |
362aecb2 | 390 | } else { |
a0994fb9 | 391 | llen += pad_spc(llen, 64, " [OK]\n"); |
362aecb2 WT |
392 | } |
393 | return ret; | |
394 | } | |
395 | ||
396 | ||
397 | #define EXPECT_STREQ(cond, expr, cmp) \ | |
a0994fb9 | 398 | do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_streq(expr, llen, cmp); } while (0) |
362aecb2 WT |
399 | |
400 | static int expect_streq(const char *expr, int llen, const char *cmp) | |
401 | { | |
402 | int ret = 0; | |
403 | ||
404 | llen += printf(" = <%s> ", expr); | |
405 | if (strcmp(expr, cmp) != 0) { | |
406 | ret = 1; | |
a0994fb9 | 407 | llen += pad_spc(llen, 64, "[FAIL]\n"); |
362aecb2 | 408 | } else { |
a0994fb9 | 409 | llen += pad_spc(llen, 64, " [OK]\n"); |
362aecb2 WT |
410 | } |
411 | return ret; | |
412 | } | |
413 | ||
414 | ||
415 | #define EXPECT_STRNE(cond, expr, cmp) \ | |
a0994fb9 | 416 | do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_strne(expr, llen, cmp); } while (0) |
362aecb2 WT |
417 | |
418 | static int expect_strne(const char *expr, int llen, const char *cmp) | |
419 | { | |
420 | int ret = 0; | |
421 | ||
422 | llen += printf(" = <%s> ", expr); | |
423 | if (strcmp(expr, cmp) == 0) { | |
424 | ret = 1; | |
a0994fb9 | 425 | llen += pad_spc(llen, 64, "[FAIL]\n"); |
362aecb2 | 426 | } else { |
a0994fb9 | 427 | llen += pad_spc(llen, 64, " [OK]\n"); |
362aecb2 WT |
428 | } |
429 | return ret; | |
430 | } | |
431 | ||
23da7bc9 | 432 | |
362aecb2 WT |
433 | /* declare tests based on line numbers. There must be exactly one test per line. */ |
434 | #define CASE_TEST(name) \ | |
435 | case __LINE__: llen += printf("%d %s", test, #name); | |
436 | ||
437 | ||
b4844fa0 WT |
438 | /* used by some syscall tests below */ |
439 | int test_getdents64(const char *dir) | |
440 | { | |
441 | char buffer[4096]; | |
442 | int fd, ret; | |
443 | int err; | |
444 | ||
445 | ret = fd = open(dir, O_RDONLY | O_DIRECTORY, 0); | |
446 | if (ret < 0) | |
447 | return ret; | |
448 | ||
449 | ret = getdents64(fd, (void *)buffer, sizeof(buffer)); | |
450 | err = errno; | |
451 | close(fd); | |
452 | ||
453 | errno = err; | |
454 | return ret; | |
455 | } | |
456 | ||
a290296a AF |
457 | static int test_getpagesize(void) |
458 | { | |
459 | long x = getpagesize(); | |
460 | int c; | |
461 | ||
462 | if (x < 0) | |
463 | return x; | |
464 | ||
465 | #if defined(__x86_64__) || defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) | |
466 | /* | |
467 | * x86 family is always 4K page. | |
468 | */ | |
469 | c = (x == 4096); | |
470 | #elif defined(__aarch64__) | |
471 | /* | |
472 | * Linux aarch64 supports three values of page size: 4K, 16K, and 64K | |
473 | * which are selected at kernel compilation time. | |
474 | */ | |
475 | c = (x == 4096 || x == (16 * 1024) || x == (64 * 1024)); | |
476 | #else | |
477 | /* | |
478 | * Assuming other architectures must have at least 4K page. | |
479 | */ | |
480 | c = (x >= 4096); | |
481 | #endif | |
482 | ||
483 | return !c; | |
484 | } | |
485 | ||
3ad09d72 TW |
486 | static int test_fork(void) |
487 | { | |
488 | int status; | |
ed495f09 ZW |
489 | pid_t pid; |
490 | ||
491 | /* flush the printf buffer to avoid child flush it */ | |
492 | fflush(stdout); | |
493 | fflush(stderr); | |
494 | ||
495 | pid = fork(); | |
3ad09d72 TW |
496 | |
497 | switch (pid) { | |
498 | case -1: | |
499 | return 1; | |
500 | ||
501 | case 0: | |
502 | exit(123); | |
503 | ||
504 | default: | |
505 | pid = waitpid(pid, &status, 0); | |
506 | ||
507 | return pid == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 123; | |
508 | } | |
509 | } | |
510 | ||
87b9fa66 TW |
511 | static int test_stat_timestamps(void) |
512 | { | |
513 | struct stat st; | |
514 | ||
515 | if (sizeof(st.st_atim.tv_sec) != sizeof(st.st_atime)) | |
516 | return 1; | |
517 | ||
518 | if (stat("/proc/self/", &st)) | |
519 | return 1; | |
520 | ||
521 | if (st.st_atim.tv_sec != st.st_atime || st.st_atim.tv_nsec > 1000000000) | |
522 | return 1; | |
523 | ||
524 | if (st.st_mtim.tv_sec != st.st_mtime || st.st_mtim.tv_nsec > 1000000000) | |
525 | return 1; | |
526 | ||
527 | if (st.st_ctim.tv_sec != st.st_ctime || st.st_ctim.tv_nsec > 1000000000) | |
528 | return 1; | |
529 | ||
530 | return 0; | |
531 | } | |
532 | ||
b4844fa0 WT |
533 | /* Run syscall tests between IDs <min> and <max>. |
534 | * Return 0 on success, non-zero on failure. | |
535 | */ | |
536 | int run_syscall(int min, int max) | |
537 | { | |
538 | struct stat stat_buf; | |
3e2d337b | 539 | int euid0; |
7172f1c6 | 540 | int proc; |
b4844fa0 WT |
541 | int test; |
542 | int tmp; | |
543 | int ret = 0; | |
544 | void *p1, *p2; | |
545 | ||
7172f1c6 WT |
546 | /* <proc> indicates whether or not /proc is mounted */ |
547 | proc = stat("/proc", &stat_buf) == 0; | |
548 | ||
3e2d337b WT |
549 | /* this will be used to skip certain tests that can't be run unprivileged */ |
550 | euid0 = geteuid() == 0; | |
551 | ||
b4844fa0 | 552 | for (test = min; test >= 0 && test <= max; test++) { |
fddc8f81 | 553 | int llen = 0; /* line length */ |
b4844fa0 WT |
554 | |
555 | /* avoid leaving empty lines below, this will insert holes into | |
556 | * test numbers. | |
557 | */ | |
558 | switch (test + __LINE__ + 1) { | |
559 | CASE_TEST(getpid); EXPECT_SYSNE(1, getpid(), -1); break; | |
560 | CASE_TEST(getppid); EXPECT_SYSNE(1, getppid(), -1); break; | |
1da02f51 | 561 | #ifdef NOLIBC |
b4844fa0 | 562 | CASE_TEST(gettid); EXPECT_SYSNE(1, gettid(), -1); break; |
1da02f51 | 563 | #endif |
b4844fa0 WT |
564 | CASE_TEST(getpgid_self); EXPECT_SYSNE(1, getpgid(0), -1); break; |
565 | CASE_TEST(getpgid_bad); EXPECT_SYSER(1, getpgid(-1), -1, ESRCH); break; | |
566 | CASE_TEST(kill_0); EXPECT_SYSZR(1, kill(getpid(), 0)); break; | |
567 | CASE_TEST(kill_CONT); EXPECT_SYSZR(1, kill(getpid(), 0)); break; | |
568 | CASE_TEST(kill_BADPID); EXPECT_SYSER(1, kill(INT_MAX, 0), -1, ESRCH); break; | |
569 | CASE_TEST(sbrk); if ((p1 = p2 = sbrk(4096)) != (void *)-1) p2 = sbrk(-4096); EXPECT_SYSZR(1, (p2 == (void *)-1) || p2 == p1); break; | |
570 | CASE_TEST(brk); EXPECT_SYSZR(1, brk(sbrk(0))); break; | |
571 | CASE_TEST(chdir_root); EXPECT_SYSZR(1, chdir("/")); break; | |
572 | CASE_TEST(chdir_dot); EXPECT_SYSZR(1, chdir(".")); break; | |
573 | CASE_TEST(chdir_blah); EXPECT_SYSER(1, chdir("/blah"), -1, ENOENT); break; | |
7172f1c6 WT |
574 | CASE_TEST(chmod_net); EXPECT_SYSZR(proc, chmod("/proc/self/net", 0555)); break; |
575 | CASE_TEST(chmod_self); EXPECT_SYSER(proc, chmod("/proc/self", 0555), -1, EPERM); break; | |
576 | CASE_TEST(chown_self); EXPECT_SYSER(proc, chown("/proc/self", 0, 0), -1, EPERM); break; | |
3e2d337b | 577 | CASE_TEST(chroot_root); EXPECT_SYSZR(euid0, chroot("/")); break; |
b4844fa0 | 578 | CASE_TEST(chroot_blah); EXPECT_SYSER(1, chroot("/proc/self/blah"), -1, ENOENT); break; |
7172f1c6 | 579 | CASE_TEST(chroot_exe); EXPECT_SYSER(proc, chroot("/proc/self/exe"), -1, ENOTDIR); break; |
b4844fa0 WT |
580 | CASE_TEST(close_m1); EXPECT_SYSER(1, close(-1), -1, EBADF); break; |
581 | CASE_TEST(close_dup); EXPECT_SYSZR(1, close(dup(0))); break; | |
582 | CASE_TEST(dup_0); tmp = dup(0); EXPECT_SYSNE(1, tmp, -1); close(tmp); break; | |
583 | CASE_TEST(dup_m1); tmp = dup(-1); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break; | |
584 | CASE_TEST(dup2_0); tmp = dup2(0, 100); EXPECT_SYSNE(1, tmp, -1); close(tmp); break; | |
585 | CASE_TEST(dup2_m1); tmp = dup2(-1, 100); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break; | |
586 | CASE_TEST(dup3_0); tmp = dup3(0, 100, 0); EXPECT_SYSNE(1, tmp, -1); close(tmp); break; | |
587 | CASE_TEST(dup3_m1); tmp = dup3(-1, 100, 0); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break; | |
588 | CASE_TEST(execve_root); EXPECT_SYSER(1, execve("/", (char*[]){ [0] = "/", [1] = NULL }, NULL), -1, EACCES); break; | |
3ad09d72 | 589 | CASE_TEST(fork); EXPECT_SYSZR(1, test_fork()); break; |
b4844fa0 WT |
590 | CASE_TEST(getdents64_root); EXPECT_SYSNE(1, test_getdents64("/"), -1); break; |
591 | CASE_TEST(getdents64_null); EXPECT_SYSER(1, test_getdents64("/dev/null"), -1, ENOTDIR); break; | |
1da02f51 | 592 | #ifdef NOLIBC |
b4844fa0 WT |
593 | CASE_TEST(gettimeofday_bad1); EXPECT_SYSER(1, gettimeofday((void *)1, NULL), -1, EFAULT); break; |
594 | CASE_TEST(gettimeofday_bad2); EXPECT_SYSER(1, gettimeofday(NULL, (void *)1), -1, EFAULT); break; | |
1da02f51 | 595 | #endif |
a290296a | 596 | CASE_TEST(getpagesize); EXPECT_SYSZR(1, test_getpagesize()); break; |
b4844fa0 WT |
597 | CASE_TEST(ioctl_tiocinq); EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); break; |
598 | CASE_TEST(ioctl_tiocinq); EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); break; | |
599 | CASE_TEST(link_root1); EXPECT_SYSER(1, link("/", "/"), -1, EEXIST); break; | |
600 | CASE_TEST(link_blah); EXPECT_SYSER(1, link("/proc/self/blah", "/blah"), -1, ENOENT); break; | |
3e2d337b | 601 | CASE_TEST(link_dir); EXPECT_SYSER(euid0, link("/", "/blah"), -1, EPERM); break; |
7172f1c6 | 602 | CASE_TEST(link_cross); EXPECT_SYSER(proc, link("/proc/self/net", "/blah"), -1, EXDEV); break; |
b4844fa0 WT |
603 | CASE_TEST(lseek_m1); EXPECT_SYSER(1, lseek(-1, 0, SEEK_SET), -1, EBADF); break; |
604 | CASE_TEST(lseek_0); EXPECT_SYSER(1, lseek(0, 0, SEEK_SET), -1, ESPIPE); break; | |
605 | CASE_TEST(mkdir_root); EXPECT_SYSER(1, mkdir("/", 0755), -1, EEXIST); break; | |
606 | CASE_TEST(open_tty); EXPECT_SYSNE(1, tmp = open("/dev/null", 0), -1); if (tmp != -1) close(tmp); break; | |
607 | CASE_TEST(open_blah); EXPECT_SYSER(1, tmp = open("/proc/self/blah", 0), -1, ENOENT); if (tmp != -1) close(tmp); break; | |
608 | CASE_TEST(poll_null); EXPECT_SYSZR(1, poll(NULL, 0, 0)); break; | |
609 | CASE_TEST(poll_stdout); EXPECT_SYSNE(1, ({ struct pollfd fds = { 1, POLLOUT, 0}; poll(&fds, 1, 0); }), -1); break; | |
610 | CASE_TEST(poll_fault); EXPECT_SYSER(1, poll((void *)1, 1, 0), -1, EFAULT); break; | |
208aa9d9 | 611 | CASE_TEST(prctl); EXPECT_SYSER(1, prctl(PR_SET_NAME, (unsigned long)NULL, 0, 0, 0), -1, EFAULT); break; |
b4844fa0 WT |
612 | CASE_TEST(read_badf); EXPECT_SYSER(1, read(-1, &tmp, 1), -1, EBADF); break; |
613 | CASE_TEST(sched_yield); EXPECT_SYSZR(1, sched_yield()); break; | |
614 | CASE_TEST(select_null); EXPECT_SYSZR(1, ({ struct timeval tv = { 0 }; select(0, NULL, NULL, NULL, &tv); })); break; | |
615 | CASE_TEST(select_stdout); EXPECT_SYSNE(1, ({ fd_set fds; FD_ZERO(&fds); FD_SET(1, &fds); select(2, NULL, &fds, NULL, NULL); }), -1); break; | |
616 | CASE_TEST(select_fault); EXPECT_SYSER(1, select(1, (void *)1, NULL, NULL, 0), -1, EFAULT); break; | |
617 | CASE_TEST(stat_blah); EXPECT_SYSER(1, stat("/proc/self/blah", &stat_buf), -1, ENOENT); break; | |
618 | CASE_TEST(stat_fault); EXPECT_SYSER(1, stat(NULL, &stat_buf), -1, EFAULT); break; | |
87b9fa66 | 619 | CASE_TEST(stat_timestamps); EXPECT_SYSZR(1, test_stat_timestamps()); break; |
b4844fa0 WT |
620 | CASE_TEST(symlink_root); EXPECT_SYSER(1, symlink("/", "/"), -1, EEXIST); break; |
621 | CASE_TEST(unlink_root); EXPECT_SYSER(1, unlink("/"), -1, EISDIR); break; | |
622 | CASE_TEST(unlink_blah); EXPECT_SYSER(1, unlink("/proc/self/blah"), -1, ENOENT); break; | |
623 | CASE_TEST(wait_child); EXPECT_SYSER(1, wait(&tmp), -1, ECHILD); break; | |
624 | CASE_TEST(waitpid_min); EXPECT_SYSER(1, waitpid(INT_MIN, &tmp, WNOHANG), -1, ESRCH); break; | |
625 | CASE_TEST(waitpid_child); EXPECT_SYSER(1, waitpid(getpid(), &tmp, WNOHANG), -1, ECHILD); break; | |
626 | CASE_TEST(write_badf); EXPECT_SYSER(1, write(-1, &tmp, 1), -1, EBADF); break; | |
627 | CASE_TEST(write_zero); EXPECT_SYSZR(1, write(1, &tmp, 0)); break; | |
53fcfafa | 628 | CASE_TEST(syscall_noargs); EXPECT_SYSEQ(1, syscall(__NR_getpid), getpid()); break; |
ec8e1b73 | 629 | CASE_TEST(syscall_args); EXPECT_SYSER(1, syscall(__NR_statx, 0, NULL, 0, 0, NULL), -1, EFAULT); break; |
b4844fa0 WT |
630 | case __LINE__: |
631 | return ret; /* must be last */ | |
632 | /* note: do not set any defaults so as to permit holes above */ | |
633 | } | |
634 | } | |
635 | return ret; | |
636 | } | |
637 | ||
95bc9894 WT |
638 | int run_stdlib(int min, int max) |
639 | { | |
640 | int test; | |
641 | int tmp; | |
642 | int ret = 0; | |
643 | void *p1, *p2; | |
644 | ||
645 | for (test = min; test >= 0 && test <= max; test++) { | |
fddc8f81 | 646 | int llen = 0; /* line length */ |
95bc9894 WT |
647 | |
648 | /* avoid leaving empty lines below, this will insert holes into | |
649 | * test numbers. | |
650 | */ | |
651 | switch (test + __LINE__ + 1) { | |
652 | CASE_TEST(getenv_TERM); EXPECT_STRNZ(1, getenv("TERM")); break; | |
653 | CASE_TEST(getenv_blah); EXPECT_STRZR(1, getenv("blah")); break; | |
654 | CASE_TEST(setcmp_blah_blah); EXPECT_EQ(1, strcmp("blah", "blah"), 0); break; | |
655 | CASE_TEST(setcmp_blah_blah2); EXPECT_NE(1, strcmp("blah", "blah2"), 0); break; | |
656 | CASE_TEST(setncmp_blah_blah); EXPECT_EQ(1, strncmp("blah", "blah", 10), 0); break; | |
657 | CASE_TEST(setncmp_blah_blah4); EXPECT_EQ(1, strncmp("blah", "blah4", 4), 0); break; | |
658 | CASE_TEST(setncmp_blah_blah5); EXPECT_NE(1, strncmp("blah", "blah5", 5), 0); break; | |
659 | CASE_TEST(setncmp_blah_blah6); EXPECT_NE(1, strncmp("blah", "blah6", 6), 0); break; | |
660 | CASE_TEST(strchr_foobar_o); EXPECT_STREQ(1, strchr("foobar", 'o'), "oobar"); break; | |
661 | CASE_TEST(strchr_foobar_z); EXPECT_STRZR(1, strchr("foobar", 'z')); break; | |
662 | CASE_TEST(strrchr_foobar_o); EXPECT_STREQ(1, strrchr("foobar", 'o'), "obar"); break; | |
663 | CASE_TEST(strrchr_foobar_z); EXPECT_STRZR(1, strrchr("foobar", 'z')); break; | |
c80b5a0a WT |
664 | CASE_TEST(memcmp_20_20); EXPECT_EQ(1, memcmp("aaa\x20", "aaa\x20", 4), 0); break; |
665 | CASE_TEST(memcmp_20_60); EXPECT_LT(1, memcmp("aaa\x20", "aaa\x60", 4), 0); break; | |
666 | CASE_TEST(memcmp_60_20); EXPECT_GT(1, memcmp("aaa\x60", "aaa\x20", 4), 0); break; | |
667 | CASE_TEST(memcmp_20_e0); EXPECT_LT(1, memcmp("aaa\x20", "aaa\xe0", 4), 0); break; | |
668 | CASE_TEST(memcmp_e0_20); EXPECT_GT(1, memcmp("aaa\xe0", "aaa\x20", 4), 0); break; | |
669 | CASE_TEST(memcmp_80_e0); EXPECT_LT(1, memcmp("aaa\x80", "aaa\xe0", 4), 0); break; | |
670 | CASE_TEST(memcmp_e0_80); EXPECT_GT(1, memcmp("aaa\xe0", "aaa\x80", 4), 0); break; | |
d1209597 VD |
671 | CASE_TEST(limit_int8_max); EXPECT_EQ(1, INT8_MAX, (int8_t) 0x7f); break; |
672 | CASE_TEST(limit_int8_min); EXPECT_EQ(1, INT8_MIN, (int8_t) 0x80); break; | |
673 | CASE_TEST(limit_uint8_max); EXPECT_EQ(1, UINT8_MAX, (uint8_t) 0xff); break; | |
674 | CASE_TEST(limit_int16_max); EXPECT_EQ(1, INT16_MAX, (int16_t) 0x7fff); break; | |
675 | CASE_TEST(limit_int16_min); EXPECT_EQ(1, INT16_MIN, (int16_t) 0x8000); break; | |
676 | CASE_TEST(limit_uint16_max); EXPECT_EQ(1, UINT16_MAX, (uint16_t) 0xffff); break; | |
677 | CASE_TEST(limit_int32_max); EXPECT_EQ(1, INT32_MAX, (int32_t) 0x7fffffff); break; | |
678 | CASE_TEST(limit_int32_min); EXPECT_EQ(1, INT32_MIN, (int32_t) 0x80000000); break; | |
679 | CASE_TEST(limit_uint32_max); EXPECT_EQ(1, UINT32_MAX, (uint32_t) 0xffffffff); break; | |
680 | CASE_TEST(limit_int64_max); EXPECT_EQ(1, INT64_MAX, (int64_t) 0x7fffffffffffffff); break; | |
681 | CASE_TEST(limit_int64_min); EXPECT_EQ(1, INT64_MIN, (int64_t) 0x8000000000000000); break; | |
682 | CASE_TEST(limit_uint64_max); EXPECT_EQ(1, UINT64_MAX, (uint64_t) 0xffffffffffffffff); break; | |
683 | CASE_TEST(limit_int_least8_max); EXPECT_EQ(1, INT_LEAST8_MAX, (int_least8_t) 0x7f); break; | |
684 | CASE_TEST(limit_int_least8_min); EXPECT_EQ(1, INT_LEAST8_MIN, (int_least8_t) 0x80); break; | |
685 | CASE_TEST(limit_uint_least8_max); EXPECT_EQ(1, UINT_LEAST8_MAX, (uint_least8_t) 0xff); break; | |
686 | CASE_TEST(limit_int_least16_max); EXPECT_EQ(1, INT_LEAST16_MAX, (int_least16_t) 0x7fff); break; | |
687 | CASE_TEST(limit_int_least16_min); EXPECT_EQ(1, INT_LEAST16_MIN, (int_least16_t) 0x8000); break; | |
688 | CASE_TEST(limit_uint_least16_max); EXPECT_EQ(1, UINT_LEAST16_MAX, (uint_least16_t) 0xffff); break; | |
689 | CASE_TEST(limit_int_least32_max); EXPECT_EQ(1, INT_LEAST32_MAX, (int_least32_t) 0x7fffffff); break; | |
690 | CASE_TEST(limit_int_least32_min); EXPECT_EQ(1, INT_LEAST32_MIN, (int_least32_t) 0x80000000); break; | |
691 | CASE_TEST(limit_uint_least32_max); EXPECT_EQ(1, UINT_LEAST32_MAX, (uint_least32_t) 0xffffffffU); break; | |
692 | CASE_TEST(limit_int_least64_min); EXPECT_EQ(1, INT_LEAST64_MIN, (int_least64_t) 0x8000000000000000LL); break; | |
693 | CASE_TEST(limit_int_least64_max); EXPECT_EQ(1, INT_LEAST64_MAX, (int_least64_t) 0x7fffffffffffffffLL); break; | |
694 | CASE_TEST(limit_uint_least64_max); EXPECT_EQ(1, UINT_LEAST64_MAX, (uint_least64_t) 0xffffffffffffffffULL); break; | |
695 | CASE_TEST(limit_int_fast8_max); EXPECT_EQ(1, INT_FAST8_MAX, (int_fast8_t) 0x7f); break; | |
696 | CASE_TEST(limit_int_fast8_min); EXPECT_EQ(1, INT_FAST8_MIN, (int_fast8_t) 0x80); break; | |
697 | CASE_TEST(limit_uint_fast8_max); EXPECT_EQ(1, UINT_FAST8_MAX, (uint_fast8_t) 0xff); break; | |
698 | CASE_TEST(limit_int_fast16_min); EXPECT_EQ(1, INT_FAST16_MIN, (int_fast16_t) INTPTR_MIN); break; | |
699 | CASE_TEST(limit_int_fast16_max); EXPECT_EQ(1, INT_FAST16_MAX, (int_fast16_t) INTPTR_MAX); break; | |
700 | CASE_TEST(limit_uint_fast16_max); EXPECT_EQ(1, UINT_FAST16_MAX, (uint_fast16_t) UINTPTR_MAX); break; | |
701 | CASE_TEST(limit_int_fast32_min); EXPECT_EQ(1, INT_FAST32_MIN, (int_fast32_t) INTPTR_MIN); break; | |
702 | CASE_TEST(limit_int_fast32_max); EXPECT_EQ(1, INT_FAST32_MAX, (int_fast32_t) INTPTR_MAX); break; | |
703 | CASE_TEST(limit_uint_fast32_max); EXPECT_EQ(1, UINT_FAST32_MAX, (uint_fast32_t) UINTPTR_MAX); break; | |
f9bf5944 TW |
704 | CASE_TEST(limit_int_fast64_min); EXPECT_EQ(1, INT_FAST64_MIN, (int_fast64_t) INT64_MIN); break; |
705 | CASE_TEST(limit_int_fast64_max); EXPECT_EQ(1, INT_FAST64_MAX, (int_fast64_t) INT64_MAX); break; | |
706 | CASE_TEST(limit_uint_fast64_max); EXPECT_EQ(1, UINT_FAST64_MAX, (uint_fast64_t) UINT64_MAX); break; | |
d1209597 VD |
707 | #if __SIZEOF_LONG__ == 8 |
708 | CASE_TEST(limit_intptr_min); EXPECT_EQ(1, INTPTR_MIN, (intptr_t) 0x8000000000000000LL); break; | |
709 | CASE_TEST(limit_intptr_max); EXPECT_EQ(1, INTPTR_MAX, (intptr_t) 0x7fffffffffffffffLL); break; | |
710 | CASE_TEST(limit_uintptr_max); EXPECT_EQ(1, UINTPTR_MAX, (uintptr_t) 0xffffffffffffffffULL); break; | |
711 | CASE_TEST(limit_ptrdiff_min); EXPECT_EQ(1, PTRDIFF_MIN, (ptrdiff_t) 0x8000000000000000LL); break; | |
712 | CASE_TEST(limit_ptrdiff_max); EXPECT_EQ(1, PTRDIFF_MAX, (ptrdiff_t) 0x7fffffffffffffffLL); break; | |
713 | CASE_TEST(limit_size_max); EXPECT_EQ(1, SIZE_MAX, (size_t) 0xffffffffffffffffULL); break; | |
714 | #elif __SIZEOF_LONG__ == 4 | |
715 | CASE_TEST(limit_intptr_min); EXPECT_EQ(1, INTPTR_MIN, (intptr_t) 0x80000000); break; | |
716 | CASE_TEST(limit_intptr_max); EXPECT_EQ(1, INTPTR_MAX, (intptr_t) 0x7fffffff); break; | |
717 | CASE_TEST(limit_uintptr_max); EXPECT_EQ(1, UINTPTR_MAX, (uintptr_t) 0xffffffffU); break; | |
718 | CASE_TEST(limit_ptrdiff_min); EXPECT_EQ(1, PTRDIFF_MIN, (ptrdiff_t) 0x80000000); break; | |
719 | CASE_TEST(limit_ptrdiff_max); EXPECT_EQ(1, PTRDIFF_MAX, (ptrdiff_t) 0x7fffffff); break; | |
720 | CASE_TEST(limit_size_max); EXPECT_EQ(1, SIZE_MAX, (size_t) 0xffffffffU); break; | |
721 | #else | |
722 | # warning "__SIZEOF_LONG__ is undefined" | |
723 | #endif /* __SIZEOF_LONG__ */ | |
95bc9894 WT |
724 | case __LINE__: |
725 | return ret; /* must be last */ | |
726 | /* note: do not set any defaults so as to permit holes above */ | |
727 | } | |
728 | } | |
729 | return ret; | |
730 | } | |
731 | ||
69f2cd9f TW |
732 | #define EXPECT_VFPRINTF(c, expected, fmt, ...) \ |
733 | ret += expect_vfprintf(llen, c, expected, fmt, ##__VA_ARGS__) | |
734 | ||
735 | static int expect_vfprintf(int llen, size_t c, const char *expected, const char *fmt, ...) | |
736 | { | |
737 | int ret, fd, w, r; | |
738 | char buf[100]; | |
739 | FILE *memfile; | |
740 | va_list args; | |
741 | ||
742 | fd = memfd_create("vfprintf", 0); | |
743 | if (fd == -1) { | |
744 | pad_spc(llen, 64, "[FAIL]\n"); | |
745 | return 1; | |
746 | } | |
747 | ||
748 | memfile = fdopen(fd, "w+"); | |
749 | if (!memfile) { | |
750 | pad_spc(llen, 64, "[FAIL]\n"); | |
751 | return 1; | |
752 | } | |
753 | ||
754 | va_start(args, fmt); | |
755 | w = vfprintf(memfile, fmt, args); | |
756 | va_end(args); | |
757 | ||
758 | if (w != c) { | |
759 | llen += printf(" written(%d) != %d", w, (int) c); | |
760 | pad_spc(llen, 64, "[FAIL]\n"); | |
761 | return 1; | |
762 | } | |
763 | ||
764 | fflush(memfile); | |
765 | lseek(fd, 0, SEEK_SET); | |
766 | ||
767 | r = read(fd, buf, sizeof(buf) - 1); | |
768 | buf[r] = '\0'; | |
769 | ||
770 | fclose(memfile); | |
771 | ||
772 | if (r != w) { | |
773 | llen += printf(" written(%d) != read(%d)", w, r); | |
774 | pad_spc(llen, 64, "[FAIL]\n"); | |
775 | return 1; | |
776 | } | |
777 | ||
778 | llen += printf(" \"%s\" = \"%s\"", expected, buf); | |
779 | ret = strncmp(expected, buf, c); | |
780 | ||
781 | pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n"); | |
782 | return ret; | |
783 | } | |
784 | ||
785 | static int run_vfprintf(int min, int max) | |
786 | { | |
787 | int test; | |
788 | int tmp; | |
789 | int ret = 0; | |
790 | void *p1, *p2; | |
791 | ||
792 | for (test = min; test >= 0 && test <= max; test++) { | |
fddc8f81 | 793 | int llen = 0; /* line length */ |
69f2cd9f TW |
794 | |
795 | /* avoid leaving empty lines below, this will insert holes into | |
796 | * test numbers. | |
797 | */ | |
798 | switch (test + __LINE__ + 1) { | |
799 | CASE_TEST(empty); EXPECT_VFPRINTF(0, "", ""); break; | |
800 | CASE_TEST(simple); EXPECT_VFPRINTF(3, "foo", "foo"); break; | |
801 | CASE_TEST(string); EXPECT_VFPRINTF(3, "foo", "%s", "foo"); break; | |
802 | CASE_TEST(number); EXPECT_VFPRINTF(4, "1234", "%d", 1234); break; | |
803 | CASE_TEST(negnumber); EXPECT_VFPRINTF(5, "-1234", "%d", -1234); break; | |
804 | CASE_TEST(unsigned); EXPECT_VFPRINTF(5, "12345", "%u", 12345); break; | |
805 | CASE_TEST(char); EXPECT_VFPRINTF(1, "c", "%c", 'c'); break; | |
806 | CASE_TEST(hex); EXPECT_VFPRINTF(1, "f", "%x", 0xf); break; | |
807 | CASE_TEST(pointer); EXPECT_VFPRINTF(3, "0x1", "%p", (void *) 0x1); break; | |
808 | case __LINE__: | |
809 | return ret; /* must be last */ | |
810 | /* note: do not set any defaults so as to permit holes above */ | |
811 | } | |
812 | } | |
813 | return ret; | |
814 | } | |
815 | ||
97357168 TW |
816 | static int smash_stack(void) |
817 | { | |
818 | char buf[100]; | |
e7654c3f | 819 | volatile char *ptr = buf; |
aa662d12 | 820 | size_t i; |
97357168 | 821 | |
aa662d12 | 822 | for (i = 0; i < 200; i++) |
e7654c3f | 823 | ptr[i] = 'P'; |
97357168 TW |
824 | |
825 | return 1; | |
826 | } | |
827 | ||
828 | static int run_protection(int min, int max) | |
829 | { | |
830 | pid_t pid; | |
831 | int llen = 0, status; | |
832 | ||
833 | llen += printf("0 -fstackprotector "); | |
834 | ||
818924d1 | 835 | #if !defined(_NOLIBC_STACKPROTECTOR) |
97357168 TW |
836 | llen += printf("not supported"); |
837 | pad_spc(llen, 64, "[SKIPPED]\n"); | |
838 | return 0; | |
839 | #endif | |
840 | ||
818924d1 | 841 | #if defined(_NOLIBC_STACKPROTECTOR) |
85250921 TW |
842 | if (!__stack_chk_guard) { |
843 | llen += printf("__stack_chk_guard not initialized"); | |
844 | pad_spc(llen, 64, "[FAIL]\n"); | |
845 | return 1; | |
846 | } | |
847 | #endif | |
848 | ||
97357168 TW |
849 | pid = -1; |
850 | pid = fork(); | |
851 | ||
852 | switch (pid) { | |
853 | case -1: | |
854 | llen += printf("fork()"); | |
855 | pad_spc(llen, 64, "[FAIL]\n"); | |
856 | return 1; | |
857 | ||
858 | case 0: | |
859 | close(STDOUT_FILENO); | |
860 | close(STDERR_FILENO); | |
861 | ||
9a75575b | 862 | prctl(PR_SET_DUMPABLE, 0, 0, 0, 0); |
97357168 TW |
863 | smash_stack(); |
864 | return 1; | |
865 | ||
866 | default: | |
867 | pid = waitpid(pid, &status, 0); | |
868 | ||
869 | if (pid == -1 || !WIFSIGNALED(status) || WTERMSIG(status) != SIGABRT) { | |
870 | llen += printf("waitpid()"); | |
871 | pad_spc(llen, 64, "[FAIL]\n"); | |
872 | return 1; | |
873 | } | |
874 | pad_spc(llen, 64, " [OK]\n"); | |
875 | return 0; | |
876 | } | |
877 | } | |
878 | ||
1a5454f6 WT |
879 | /* prepare what needs to be prepared for pid 1 (stdio, /dev, /proc, etc) */ |
880 | int prepare(void) | |
881 | { | |
882 | struct stat stat_buf; | |
883 | ||
884 | /* It's possible that /dev doesn't even exist or was not mounted, so | |
885 | * we'll try to create it, mount it, or create minimal entries into it. | |
886 | * We want at least /dev/null and /dev/console. | |
887 | */ | |
888 | if (stat("/dev/.", &stat_buf) == 0 || mkdir("/dev", 0755) == 0) { | |
889 | if (stat("/dev/console", &stat_buf) != 0 || | |
890 | stat("/dev/null", &stat_buf) != 0) { | |
891 | /* try devtmpfs first, otherwise fall back to manual creation */ | |
892 | if (mount("/dev", "/dev", "devtmpfs", 0, 0) != 0) { | |
893 | mknod("/dev/console", 0600 | S_IFCHR, makedev(5, 1)); | |
894 | mknod("/dev/null", 0666 | S_IFCHR, makedev(1, 3)); | |
895 | } | |
896 | } | |
897 | } | |
898 | ||
899 | /* If no /dev/console was found before calling init, stdio is closed so | |
900 | * we need to reopen it from /dev/console. If it failed above, it will | |
901 | * still fail here and we cannot emit a message anyway. | |
902 | */ | |
903 | if (close(dup(1)) == -1) { | |
904 | int fd = open("/dev/console", O_RDWR); | |
905 | ||
906 | if (fd >= 0) { | |
907 | if (fd != 0) | |
908 | dup2(fd, 0); | |
909 | if (fd != 1) | |
910 | dup2(fd, 1); | |
911 | if (fd != 2) | |
912 | dup2(fd, 2); | |
913 | if (fd > 2) | |
914 | close(fd); | |
915 | puts("\nSuccessfully reopened /dev/console."); | |
916 | } | |
917 | } | |
918 | ||
919 | /* try to mount /proc if not mounted. Silently fail otherwise */ | |
920 | if (stat("/proc/.", &stat_buf) == 0 || mkdir("/proc", 0755) == 0) { | |
921 | if (stat("/proc/self", &stat_buf) != 0) | |
922 | mount("/proc", "/proc", "proc", 0, 0); | |
923 | } | |
924 | ||
925 | return 0; | |
926 | } | |
b4844fa0 | 927 | |
23da7bc9 | 928 | /* This is the definition of known test names, with their functions */ |
c4560bd8 | 929 | static const struct test test_names[] = { |
23da7bc9 | 930 | /* add new tests here */ |
97357168 TW |
931 | { .name = "syscall", .func = run_syscall }, |
932 | { .name = "stdlib", .func = run_stdlib }, | |
69f2cd9f | 933 | { .name = "vfprintf", .func = run_vfprintf }, |
97357168 | 934 | { .name = "protection", .func = run_protection }, |
23da7bc9 WT |
935 | { 0 } |
936 | }; | |
937 | ||
362aecb2 WT |
938 | int main(int argc, char **argv, char **envp) |
939 | { | |
940 | int min = 0; | |
941 | int max = __INT_MAX__; | |
942 | int ret = 0; | |
23da7bc9 WT |
943 | int err; |
944 | int idx; | |
945 | char *test; | |
362aecb2 WT |
946 | |
947 | environ = envp; | |
948 | ||
1a5454f6 WT |
949 | /* when called as init, it's possible that no console was opened, for |
950 | * example if no /dev file system was provided. We'll check that fd#1 | |
951 | * was opened, and if not we'll attempt to create and open /dev/console | |
952 | * and /dev/null that we'll use for later tests. | |
953 | */ | |
954 | if (getpid() == 1) | |
955 | prepare(); | |
956 | ||
23da7bc9 WT |
957 | /* the definition of a series of tests comes from either argv[1] or the |
958 | * "NOLIBC_TEST" environment variable. It's made of a comma-delimited | |
959 | * series of test names and optional ranges: | |
960 | * syscall:5-15[:.*],stdlib:8-10 | |
961 | */ | |
962 | test = argv[1]; | |
963 | if (!test) | |
964 | test = getenv("NOLIBC_TEST"); | |
965 | ||
966 | if (test) { | |
967 | char *comma, *colon, *dash, *value; | |
968 | ||
969 | do { | |
970 | comma = strchr(test, ','); | |
971 | if (comma) | |
972 | *(comma++) = '\0'; | |
973 | ||
974 | colon = strchr(test, ':'); | |
975 | if (colon) | |
976 | *(colon++) = '\0'; | |
977 | ||
978 | for (idx = 0; test_names[idx].name; idx++) { | |
979 | if (strcmp(test, test_names[idx].name) == 0) | |
980 | break; | |
981 | } | |
982 | ||
983 | if (test_names[idx].name) { | |
984 | /* The test was named, it will be called at least | |
985 | * once. We may have an optional range at <colon> | |
986 | * here, which defaults to the full range. | |
987 | */ | |
988 | do { | |
989 | min = 0; max = __INT_MAX__; | |
990 | value = colon; | |
991 | if (value && *value) { | |
992 | colon = strchr(value, ':'); | |
993 | if (colon) | |
994 | *(colon++) = '\0'; | |
995 | ||
996 | dash = strchr(value, '-'); | |
997 | if (dash) | |
998 | *(dash++) = '\0'; | |
999 | ||
1000 | /* support :val: :min-max: :min-: :-max: */ | |
1001 | if (*value) | |
1002 | min = atoi(value); | |
1003 | if (!dash) | |
1004 | max = min; | |
1005 | else if (*dash) | |
1006 | max = atoi(dash); | |
1007 | ||
1008 | value = colon; | |
1009 | } | |
1010 | ||
1011 | /* now's time to call the test */ | |
1012 | printf("Running test '%s'\n", test_names[idx].name); | |
1013 | err = test_names[idx].func(min, max); | |
1014 | ret += err; | |
1015 | printf("Errors during this test: %d\n\n", err); | |
1016 | } while (colon && *colon); | |
1017 | } else | |
1018 | printf("Ignoring unknown test name '%s'\n", test); | |
1019 | ||
1020 | test = comma; | |
1021 | } while (test && *test); | |
1022 | } else { | |
1023 | /* no test mentioned, run everything */ | |
1024 | for (idx = 0; test_names[idx].name; idx++) { | |
1025 | printf("Running test '%s'\n", test_names[idx].name); | |
1026 | err = test_names[idx].func(min, max); | |
1027 | ret += err; | |
1028 | printf("Errors during this test: %d\n\n", err); | |
1029 | } | |
1030 | } | |
1031 | ||
362aecb2 | 1032 | printf("Total number of errors: %d\n", ret); |
f49896d7 WT |
1033 | |
1034 | if (getpid() == 1) { | |
1035 | /* we're running as init, there's no other process on the | |
1036 | * system, thus likely started from a VM for a quick check. | |
1037 | * Exiting will provoke a kernel panic that may be reported | |
1038 | * as an error by Qemu or the hypervisor, while stopping | |
1039 | * cleanly will often be reported as a success. This allows | |
1040 | * to use the output of this program for bisecting kernels. | |
1041 | */ | |
1042 | printf("Leaving init with final status: %d\n", !!ret); | |
1043 | if (ret == 0) | |
1044 | reboot(LINUX_REBOOT_CMD_POWER_OFF); | |
aa73a86c WT |
1045 | #if defined(__x86_64__) |
1046 | /* QEMU started with "-device isa-debug-exit -no-reboot" will | |
1047 | * exit with status code 2N+1 when N is written to 0x501. We | |
1048 | * hard-code the syscall here as it's arch-dependent. | |
1049 | */ | |
1da02f51 | 1050 | #if defined(_NOLIBC_SYS_H) |
aa73a86c | 1051 | else if (my_syscall3(__NR_ioperm, 0x501, 1, 1) == 0) |
1da02f51 WT |
1052 | #else |
1053 | else if (ioperm(0x501, 1, 1) == 0) | |
1054 | #endif | |
7f291cfa | 1055 | __asm__ volatile ("outb %%al, %%dx" :: "d"(0x501), "a"(0)); |
aa73a86c WT |
1056 | /* if it does nothing, fall back to the regular panic */ |
1057 | #endif | |
f49896d7 WT |
1058 | } |
1059 | ||
362aecb2 WT |
1060 | printf("Exiting with status %d\n", !!ret); |
1061 | return !!ret; | |
1062 | } |