1 /* SPDX-License-Identifier: GPL-2.0 */
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
15 #ifndef _NOLIBC_STDIO_H
16 /* standard libcs need more includes */
17 #include <linux/reboot.h>
19 #include <sys/ioctl.h>
21 #include <sys/mount.h>
22 #include <sys/prctl.h>
23 #include <sys/reboot.h>
25 #include <sys/syscall.h>
26 #include <sys/sysmacros.h>
43 /* will be used by nolibc by getenv() */
46 /* definition of a series of tests */
48 const char *name; /* test name */
49 int (*func)(int min, int max); /* handler */
52 #ifndef _NOLIBC_STDLIB_H
58 ret = snprintf(buf, sizeof(buf), "%d", i);
59 return (ret >= 0 && ret < sizeof(buf)) ? buf : "#err";
63 #define CASE_ERR(err) \
66 /* returns the error name (e.g. "ENOENT") for common errors, "SUCCESS" for 0,
67 * or the decimal value for less common ones.
69 const char *errorname(int err)
72 case 0: return "SUCCESS";
114 static void putcharn(char c, size_t n)
123 static int pad_spc(int llen, int cnt, const char *fmt, ...)
128 putcharn(' ', cnt - llen);
131 ret = vfprintf(stdout, fmt, args);
133 return ret < 0 ? ret : ret + cnt - llen;
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.
142 #define EXPECT_ZR(cond, expr) \
143 do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_zr(expr, llen); } while (0)
145 static int expect_zr(int expr, int llen)
147 int ret = !(expr == 0);
149 llen += printf(" = %d ", expr);
150 pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
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)
158 static int expect_nz(int expr, int llen)
160 int ret = !(expr != 0);
162 llen += printf(" = %d ", expr);
163 pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
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)
171 static int expect_eq(uint64_t expr, int llen, uint64_t val)
173 int ret = !(expr == val);
175 llen += printf(" = %lld ", (long long)expr);
176 pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
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)
184 static int expect_ne(int expr, int llen, int val)
186 int ret = !(expr != val);
188 llen += printf(" = %d ", expr);
189 pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
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)
197 static int expect_ge(int expr, int llen, int val)
199 int ret = !(expr >= val);
201 llen += printf(" = %d ", expr);
202 pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
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)
210 static int expect_gt(int expr, int llen, int val)
212 int ret = !(expr > val);
214 llen += printf(" = %d ", expr);
215 pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
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)
223 static int expect_le(int expr, int llen, int val)
225 int ret = !(expr <= val);
227 llen += printf(" = %d ", expr);
228 pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
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)
236 static int expect_lt(int expr, int llen, int val)
238 int ret = !(expr < val);
240 llen += printf(" = %d ", expr);
241 pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
246 #define EXPECT_SYSZR(cond, expr) \
247 do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_syszr(expr, llen); } while (0)
249 static int expect_syszr(int expr, int llen)
255 llen += printf(" = %d %s ", expr, errorname(errno));
256 llen += pad_spc(llen, 64, "[FAIL]\n");
258 llen += printf(" = %d ", expr);
259 llen += pad_spc(llen, 64, " [OK]\n");
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)
268 static int expect_syseq(int expr, int llen, int val)
274 llen += printf(" = %d %s ", expr, errorname(errno));
275 llen += pad_spc(llen, 64, "[FAIL]\n");
277 llen += printf(" = %d ", expr);
278 llen += pad_spc(llen, 64, " [OK]\n");
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)
287 static int expect_sysne(int expr, int llen, int val)
293 llen += printf(" = %d %s ", expr, errorname(errno));
294 llen += pad_spc(llen, 64, "[FAIL]\n");
296 llen += printf(" = %d ", expr);
297 llen += pad_spc(llen, 64, " [OK]\n");
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)
306 #define EXPECT_SYSER(cond, expr, expret, experr) \
307 EXPECT_SYSER2(cond, expr, expret, experr, 0)
309 static int expect_syserr2(int expr, int expret, int experr1, int experr2, int llen)
314 llen += printf(" = %d %s ", expr, errorname(_errno));
315 if (expr != expret || (_errno != experr1 && _errno != experr2)) {
318 llen += printf(" != (%d %s) ", expret, errorname(experr1));
320 llen += printf(" != (%d %s %s) ", expret, errorname(experr1), errorname(experr2));
321 llen += pad_spc(llen, 64, "[FAIL]\n");
323 llen += pad_spc(llen, 64, " [OK]\n");
329 #define EXPECT_PTRZR(cond, expr) \
330 do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ptrzr(expr, llen); } while (0)
332 static int expect_ptrzr(const void *expr, int llen)
336 llen += printf(" = <%p> ", expr);
339 llen += pad_spc(llen, 64, "[FAIL]\n");
341 llen += pad_spc(llen, 64, " [OK]\n");
347 #define EXPECT_PTRNZ(cond, expr) \
348 do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ptrnz(expr, llen); } while (0)
350 static int expect_ptrnz(const void *expr, int llen)
354 llen += printf(" = <%p> ", expr);
357 llen += pad_spc(llen, 64, "[FAIL]\n");
359 llen += pad_spc(llen, 64, " [OK]\n");
365 #define EXPECT_STRZR(cond, expr) \
366 do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_strzr(expr, llen); } while (0)
368 static int expect_strzr(const char *expr, int llen)
372 llen += printf(" = <%s> ", expr);
375 llen += pad_spc(llen, 64, "[FAIL]\n");
377 llen += pad_spc(llen, 64, " [OK]\n");
383 #define EXPECT_STRNZ(cond, expr) \
384 do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_strnz(expr, llen); } while (0)
386 static int expect_strnz(const char *expr, int llen)
390 llen += printf(" = <%s> ", expr);
393 llen += pad_spc(llen, 64, "[FAIL]\n");
395 llen += pad_spc(llen, 64, " [OK]\n");
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)
404 static int expect_streq(const char *expr, int llen, const char *cmp)
408 llen += printf(" = <%s> ", expr);
409 if (strcmp(expr, cmp) != 0) {
411 llen += pad_spc(llen, 64, "[FAIL]\n");
413 llen += pad_spc(llen, 64, " [OK]\n");
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)
422 static int expect_strne(const char *expr, int llen, const char *cmp)
426 llen += printf(" = <%s> ", expr);
427 if (strcmp(expr, cmp) == 0) {
429 llen += pad_spc(llen, 64, "[FAIL]\n");
431 llen += pad_spc(llen, 64, " [OK]\n");
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);
442 /* used by some syscall tests below */
443 int test_getdents64(const char *dir)
449 ret = fd = open(dir, O_RDONLY | O_DIRECTORY, 0);
453 ret = getdents64(fd, (void *)buffer, sizeof(buffer));
461 static int test_getpagesize(void)
463 long x = getpagesize();
469 #if defined(__x86_64__) || defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__)
471 * x86 family is always 4K page.
474 #elif defined(__aarch64__)
476 * Linux aarch64 supports three values of page size: 4K, 16K, and 64K
477 * which are selected at kernel compilation time.
479 c = (x == 4096 || x == (16 * 1024) || x == (64 * 1024));
482 * Assuming other architectures must have at least 4K page.
490 static int test_fork(void)
495 /* flush the printf buffer to avoid child flush it */
509 pid = waitpid(pid, &status, 0);
511 return pid == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 123;
515 static int test_stat_timestamps(void)
519 if (sizeof(st.st_atim.tv_sec) != sizeof(st.st_atime))
522 if (stat("/proc/self/", &st))
525 if (st.st_atim.tv_sec != st.st_atime || st.st_atim.tv_nsec > 1000000000)
528 if (st.st_mtim.tv_sec != st.st_mtime || st.st_mtim.tv_nsec > 1000000000)
531 if (st.st_ctim.tv_sec != st.st_ctime || st.st_ctim.tv_nsec > 1000000000)
537 /* Run syscall tests between IDs <min> and <max>.
538 * Return 0 on success, non-zero on failure.
540 int run_syscall(int min, int max)
544 struct stat stat_buf;
552 /* <proc> indicates whether or not /proc is mounted */
553 proc = stat("/proc", &stat_buf) == 0;
555 /* this will be used to skip certain tests that can't be run unprivileged */
556 euid0 = geteuid() == 0;
558 for (test = min; test >= 0 && test <= max; test++) {
559 int llen = 0; /* line length */
561 /* avoid leaving empty lines below, this will insert holes into
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;
568 CASE_TEST(gettid); EXPECT_SYSNE(1, gettid(), -1); break;
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;
635 return ret; /* must be last */
636 /* note: do not set any defaults so as to permit holes above */
642 int run_stdlib(int min, int max)
649 for (test = min; test >= 0 && test <= max; test++) {
650 int llen = 0; /* line length */
652 /* avoid leaving empty lines below, this will insert holes into
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;
726 # warning "__SIZEOF_LONG__ is undefined"
727 #endif /* __SIZEOF_LONG__ */
729 return ret; /* must be last */
730 /* note: do not set any defaults so as to permit holes above */
736 #define EXPECT_VFPRINTF(c, expected, fmt, ...) \
737 ret += expect_vfprintf(llen, c, expected, fmt, ##__VA_ARGS__)
739 static int expect_vfprintf(int llen, size_t c, const char *expected, const char *fmt, ...)
746 fd = memfd_create("vfprintf", 0);
748 pad_spc(llen, 64, "[FAIL]\n");
752 memfile = fdopen(fd, "w+");
754 pad_spc(llen, 64, "[FAIL]\n");
759 w = vfprintf(memfile, fmt, args);
763 llen += printf(" written(%d) != %d", w, (int) c);
764 pad_spc(llen, 64, "[FAIL]\n");
769 lseek(fd, 0, SEEK_SET);
771 r = read(fd, buf, sizeof(buf) - 1);
777 llen += printf(" written(%d) != read(%d)", w, r);
778 pad_spc(llen, 64, "[FAIL]\n");
782 llen += printf(" \"%s\" = \"%s\"", expected, buf);
783 ret = strncmp(expected, buf, c);
785 pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
789 static int run_vfprintf(int min, int max)
796 for (test = min; test >= 0 && test <= max; test++) {
797 int llen = 0; /* line length */
799 /* avoid leaving empty lines below, this will insert holes into
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;
813 return ret; /* must be last */
814 /* note: do not set any defaults so as to permit holes above */
820 static int smash_stack(void)
823 volatile char *ptr = buf;
826 for (i = 0; i < 200; i++)
832 static int run_protection(int min, int max)
835 int llen = 0, status;
837 llen += printf("0 -fstackprotector ");
839 #if !defined(_NOLIBC_STACKPROTECTOR)
840 llen += printf("not supported");
841 pad_spc(llen, 64, "[SKIPPED]\n");
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");
858 llen += printf("fork()");
859 pad_spc(llen, 64, "[FAIL]\n");
863 close(STDOUT_FILENO);
864 close(STDERR_FILENO);
866 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
871 pid = waitpid(pid, &status, 0);
873 if (pid == -1 || !WIFSIGNALED(status) || WTERMSIG(status) != SIGABRT) {
874 llen += printf("waitpid()");
875 pad_spc(llen, 64, "[FAIL]\n");
878 pad_spc(llen, 64, " [OK]\n");
883 /* prepare what needs to be prepared for pid 1 (stdio, /dev, /proc, etc) */
886 struct stat stat_buf;
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.
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));
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.
907 if (close(dup(1)) == -1) {
908 int fd = open("/dev/console", O_RDWR);
919 puts("\nSuccessfully reopened /dev/console.");
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);
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 },
942 int main(int argc, char **argv, char **envp)
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.
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
968 test = getenv("NOLIBC_TEST");
971 char *comma, *colon, *dash, *value;
974 comma = strchr(test, ',');
978 colon = strchr(test, ':');
982 for (idx = 0; test_names[idx].name; idx++) {
983 if (strcmp(test, test_names[idx].name) == 0)
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.
993 min = 0; max = INT_MAX;
995 if (value && *value) {
996 colon = strchr(value, ':');
1000 dash = strchr(value, '-');
1004 /* support :val: :min-max: :min-: :-max: */
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);
1019 printf("Errors during this test: %d\n\n", err);
1020 } while (colon && *colon);
1022 printf("Ignoring unknown test name '%s'\n", test);
1025 } while (test && *test);
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);
1032 printf("Errors during this test: %d\n\n", err);
1036 printf("Total number of errors: %d\n", ret);
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.
1046 printf("Leaving init with final status: %d\n", !!ret);
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.
1054 #if defined(_NOLIBC_SYS_H)
1055 else if (my_syscall3(__NR_ioperm, 0x501, 1, 1) == 0)
1057 else if (ioperm(0x501, 1, 1) == 0)
1059 __asm__ volatile ("outb %%al, %%dx" :: "d"(0x501), "a"(0));
1060 /* if it does nothing, fall back to the regular panic */
1064 printf("Exiting with status %d\n", !!ret);