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