1 /* SPDX-License-Identifier: GPL-2.0 */
4 #define _LARGEFILE64_SOURCE
6 /* libc-specific include files
7 * The program may be built in 3 ways:
8 * $(CC) -nostdlib -include /path/to/nolibc.h => NOLIBC already defined
9 * $(CC) -nostdlib -I/path/to/nolibc/sysroot => _NOLIBC_* guards are present
10 * $(CC) with default libc => NOLIBC* never defined
16 #ifndef _NOLIBC_STDIO_H
17 /* standard libcs need more includes */
20 #include <sys/ioctl.h>
22 #include <sys/mount.h>
23 #include <sys/prctl.h>
24 #include <sys/reboot.h>
26 #include <sys/syscall.h>
27 #include <sys/sysmacros.h>
44 /* for the type of int_fast16_t and int_fast32_t, musl differs from glibc and nolibc */
45 #define SINT_MAX_OF_TYPE(type) (((type)1 << (sizeof(type) * 8 - 2)) - (type)1 + ((type)1 << (sizeof(type) * 8 - 2)))
46 #define SINT_MIN_OF_TYPE(type) (-SINT_MAX_OF_TYPE(type) - 1)
48 /* will be used to test initialization of environ */
49 static char **test_envp;
51 /* will be used to test initialization of argv */
52 static char **test_argv;
54 /* will be used to test initialization of argc */
57 /* will be used by some test cases as readable file, please don't write it */
58 static const char *argv0;
60 /* definition of a series of tests */
62 const char *name; /* test name */
63 int (*func)(int min, int max); /* handler */
66 #ifndef _NOLIBC_STDLIB_H
72 ret = snprintf(buf, sizeof(buf), "%d", i);
73 return (ret >= 0 && ret < sizeof(buf)) ? buf : "#err";
77 #define CASE_ERR(err) \
80 /* returns the error name (e.g. "ENOENT") for common errors, "SUCCESS" for 0,
81 * or the decimal value for less common ones.
83 static const char *errorname(int err)
86 case 0: return "SUCCESS";
128 static void putcharn(char c, size_t n)
143 static void result(int llen, enum RESULT r)
149 else if (r == SKIPPED)
155 putcharn(' ', 64 - llen);
159 /* The tests below are intended to be used by the macroes, which evaluate
160 * expression <expr>, print the status to stdout, and update the "ret"
161 * variable to count failures. The functions themselves return the number
162 * of failures, thus either 0 or 1.
165 #define EXPECT_ZR(cond, expr) \
166 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_zr(expr, llen); } while (0)
168 static __attribute__((unused))
169 int expect_zr(int expr, int llen)
171 int ret = !(expr == 0);
173 llen += printf(" = %d ", expr);
174 result(llen, ret ? FAIL : OK);
179 #define EXPECT_NZ(cond, expr, val) \
180 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_nz(expr, llen; } while (0)
182 static __attribute__((unused))
183 int expect_nz(int expr, int llen)
185 int ret = !(expr != 0);
187 llen += printf(" = %d ", expr);
188 result(llen, ret ? FAIL : OK);
193 #define EXPECT_EQ(cond, expr, val) \
194 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_eq(expr, llen, val); } while (0)
196 static __attribute__((unused))
197 int expect_eq(uint64_t expr, int llen, uint64_t val)
199 int ret = !(expr == val);
201 llen += printf(" = %lld ", (long long)expr);
202 result(llen, ret ? FAIL : OK);
207 #define EXPECT_NE(cond, expr, val) \
208 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ne(expr, llen, val); } while (0)
210 static __attribute__((unused))
211 int expect_ne(int expr, int llen, int val)
213 int ret = !(expr != val);
215 llen += printf(" = %d ", expr);
216 result(llen, ret ? FAIL : OK);
221 #define EXPECT_GE(cond, expr, val) \
222 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ge(expr, llen, val); } while (0)
224 static __attribute__((unused))
225 int expect_ge(int expr, int llen, int val)
227 int ret = !(expr >= val);
229 llen += printf(" = %d ", expr);
230 result(llen, ret ? FAIL : OK);
235 #define EXPECT_GT(cond, expr, val) \
236 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_gt(expr, llen, val); } while (0)
238 static __attribute__((unused))
239 int expect_gt(int expr, int llen, int val)
241 int ret = !(expr > val);
243 llen += printf(" = %d ", expr);
244 result(llen, ret ? FAIL : OK);
249 #define EXPECT_LE(cond, expr, val) \
250 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_le(expr, llen, val); } while (0)
252 static __attribute__((unused))
253 int expect_le(int expr, int llen, int val)
255 int ret = !(expr <= val);
257 llen += printf(" = %d ", expr);
258 result(llen, ret ? FAIL : OK);
263 #define EXPECT_LT(cond, expr, val) \
264 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_lt(expr, llen, val); } while (0)
266 static __attribute__((unused))
267 int expect_lt(int expr, int llen, int val)
269 int ret = !(expr < val);
271 llen += printf(" = %d ", expr);
272 result(llen, ret ? FAIL : OK);
277 #define EXPECT_SYSZR(cond, expr) \
278 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_syszr(expr, llen); } while (0)
280 static __attribute__((unused))
281 int expect_syszr(int expr, int llen)
287 llen += printf(" = %d %s ", expr, errorname(errno));
290 llen += printf(" = %d ", expr);
297 #define EXPECT_SYSEQ(cond, expr, val) \
298 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_syseq(expr, llen, val); } while (0)
300 static __attribute__((unused))
301 int expect_syseq(int expr, int llen, int val)
307 llen += printf(" = %d %s ", expr, errorname(errno));
310 llen += printf(" = %d ", expr);
317 #define EXPECT_SYSNE(cond, expr, val) \
318 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_sysne(expr, llen, val); } while (0)
320 static __attribute__((unused))
321 int expect_sysne(int expr, int llen, int val)
327 llen += printf(" = %d %s ", expr, errorname(errno));
330 llen += printf(" = %d ", expr);
337 #define EXPECT_SYSER2(cond, expr, expret, experr1, experr2) \
338 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_syserr2(expr, expret, experr1, experr2, llen); } while (0)
340 #define EXPECT_SYSER(cond, expr, expret, experr) \
341 EXPECT_SYSER2(cond, expr, expret, experr, 0)
343 static __attribute__((unused))
344 int expect_syserr2(int expr, int expret, int experr1, int experr2, int llen)
349 llen += printf(" = %d %s ", expr, errorname(_errno));
350 if (expr != expret || (_errno != experr1 && _errno != experr2)) {
353 llen += printf(" != (%d %s) ", expret, errorname(experr1));
355 llen += printf(" != (%d %s %s) ", expret, errorname(experr1), errorname(experr2));
364 #define EXPECT_PTRZR(cond, expr) \
365 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrzr(expr, llen); } while (0)
367 static __attribute__((unused))
368 int expect_ptrzr(const void *expr, int llen)
372 llen += printf(" = <%p> ", expr);
383 #define EXPECT_PTRNZ(cond, expr) \
384 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrnz(expr, llen); } while (0)
386 static __attribute__((unused))
387 int expect_ptrnz(const void *expr, int llen)
391 llen += printf(" = <%p> ", expr);
401 #define EXPECT_PTREQ(cond, expr, cmp) \
402 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptreq(expr, llen, cmp); } while (0)
404 static __attribute__((unused))
405 int expect_ptreq(const void *expr, int llen, const void *cmp)
409 llen += printf(" = <%p> ", expr);
419 #define EXPECT_PTRNE(cond, expr, cmp) \
420 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrne(expr, llen, cmp); } while (0)
422 static __attribute__((unused))
423 int expect_ptrne(const void *expr, int llen, const void *cmp)
427 llen += printf(" = <%p> ", expr);
437 #define EXPECT_PTRGE(cond, expr, cmp) \
438 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrge(expr, llen, cmp); } while (0)
440 static __attribute__((unused))
441 int expect_ptrge(const void *expr, int llen, const void *cmp)
443 int ret = !(expr >= cmp);
445 llen += printf(" = <%p> ", expr);
446 result(llen, ret ? FAIL : OK);
450 #define EXPECT_PTRGT(cond, expr, cmp) \
451 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrgt(expr, llen, cmp); } while (0)
453 static __attribute__((unused))
454 int expect_ptrgt(const void *expr, int llen, const void *cmp)
456 int ret = !(expr > cmp);
458 llen += printf(" = <%p> ", expr);
459 result(llen, ret ? FAIL : OK);
464 #define EXPECT_PTRLE(cond, expr, cmp) \
465 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrle(expr, llen, cmp); } while (0)
467 static __attribute__((unused))
468 int expect_ptrle(const void *expr, int llen, const void *cmp)
470 int ret = !(expr <= cmp);
472 llen += printf(" = <%p> ", expr);
473 result(llen, ret ? FAIL : OK);
478 #define EXPECT_PTRLT(cond, expr, cmp) \
479 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrlt(expr, llen, cmp); } while (0)
481 static __attribute__((unused))
482 int expect_ptrlt(const void *expr, int llen, const void *cmp)
484 int ret = !(expr < cmp);
486 llen += printf(" = <%p> ", expr);
487 result(llen, ret ? FAIL : OK);
491 #define EXPECT_PTRER2(cond, expr, expret, experr1, experr2) \
492 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrerr2(expr, expret, experr1, experr2, llen); } while (0)
494 #define EXPECT_PTRER(cond, expr, expret, experr) \
495 EXPECT_PTRER2(cond, expr, expret, experr, 0)
497 static __attribute__((unused))
498 int expect_ptrerr2(const void *expr, const void *expret, int experr1, int experr2, int llen)
503 llen += printf(" = <%p> %s ", expr, errorname(_errno));
504 if (expr != expret || (_errno != experr1 && _errno != experr2)) {
507 llen += printf(" != (<%p> %s) ", expret, errorname(experr1));
509 llen += printf(" != (<%p> %s %s) ", expret, errorname(experr1), errorname(experr2));
517 #define EXPECT_STRZR(cond, expr) \
518 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_strzr(expr, llen); } while (0)
520 static __attribute__((unused))
521 int expect_strzr(const char *expr, int llen)
525 llen += printf(" = <%s> ", expr);
536 #define EXPECT_STRNZ(cond, expr) \
537 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_strnz(expr, llen); } while (0)
539 static __attribute__((unused))
540 int expect_strnz(const char *expr, int llen)
544 llen += printf(" = <%s> ", expr);
555 #define EXPECT_STREQ(cond, expr, cmp) \
556 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_streq(expr, llen, cmp); } while (0)
558 static __attribute__((unused))
559 int expect_streq(const char *expr, int llen, const char *cmp)
563 llen += printf(" = <%s> ", expr);
564 if (strcmp(expr, cmp) != 0) {
574 #define EXPECT_STRNE(cond, expr, cmp) \
575 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_strne(expr, llen, cmp); } while (0)
577 static __attribute__((unused))
578 int expect_strne(const char *expr, int llen, const char *cmp)
582 llen += printf(" = <%s> ", expr);
583 if (strcmp(expr, cmp) == 0) {
593 /* declare tests based on line numbers. There must be exactly one test per line. */
594 #define CASE_TEST(name) \
595 case __LINE__: llen += printf("%d %s", test, #name);
597 int run_startup(int min, int max)
601 /* kernel at least passes HOME and TERM, shell passes more */
603 /* checking NULL for argv/argv0, environ and _auxv is not enough, let's compare with sbrk(0) or &end */
605 char *brk = sbrk(0) != (void *)-1 ? sbrk(0) : &end;
606 /* differ from nolibc, both glibc and musl have no global _auxv */
607 const unsigned long *test_auxv = (void *)-1;
612 for (test = min; test >= 0 && test <= max; test++) {
613 int llen = 0; /* line length */
615 /* avoid leaving empty lines below, this will insert holes into
618 switch (test + __LINE__ + 1) {
619 CASE_TEST(argc); EXPECT_GE(1, test_argc, 1); break;
620 CASE_TEST(argv_addr); EXPECT_PTRGT(1, test_argv, brk); break;
621 CASE_TEST(argv_environ); EXPECT_PTRLT(1, test_argv, environ); break;
622 CASE_TEST(argv_total); EXPECT_EQ(1, environ - test_argv - 1, test_argc ?: 1); break;
623 CASE_TEST(argv0_addr); EXPECT_PTRGT(1, argv0, brk); break;
624 CASE_TEST(argv0_str); EXPECT_STRNZ(1, argv0 > brk ? argv0 : NULL); break;
625 CASE_TEST(argv0_len); EXPECT_GE(1, argv0 > brk ? strlen(argv0) : 0, 1); break;
626 CASE_TEST(environ_addr); EXPECT_PTRGT(1, environ, brk); break;
627 CASE_TEST(environ_envp); EXPECT_PTREQ(1, environ, test_envp); break;
628 CASE_TEST(environ_auxv); EXPECT_PTRLT(test_auxv != (void *)-1, environ, test_auxv); break;
629 CASE_TEST(environ_total); EXPECT_GE(test_auxv != (void *)-1, (void *)test_auxv - (void *)environ - 1, env_total); break;
630 CASE_TEST(environ_HOME); EXPECT_PTRNZ(1, getenv("HOME")); break;
631 CASE_TEST(auxv_addr); EXPECT_PTRGT(test_auxv != (void *)-1, test_auxv, brk); break;
632 CASE_TEST(auxv_AT_UID); EXPECT_EQ(1, getauxval(AT_UID), getuid()); break;
633 CASE_TEST(auxv_AT_PAGESZ); EXPECT_GE(1, getauxval(AT_PAGESZ), 4096); break;
635 return ret; /* must be last */
636 /* note: do not set any defaults so as to permit holes above */
643 /* used by some syscall tests below */
644 int test_getdents64(const char *dir)
650 ret = fd = open(dir, O_RDONLY | O_DIRECTORY, 0);
654 ret = getdents64(fd, (void *)buffer, sizeof(buffer));
662 int test_getpagesize(void)
664 int x = getpagesize();
670 #if defined(__x86_64__) || defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__)
672 * x86 family is always 4K page.
675 #elif defined(__aarch64__)
677 * Linux aarch64 supports three values of page size: 4K, 16K, and 64K
678 * which are selected at kernel compilation time.
680 c = (x == 4096 || x == (16 * 1024) || x == (64 * 1024));
683 * Assuming other architectures must have at least 4K page.
696 /* flush the printf buffer to avoid child flush it */
710 pid = waitpid(pid, &status, 0);
712 return pid == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 123;
716 int test_stat_timestamps(void)
720 if (sizeof(st.st_atim.tv_sec) != sizeof(st.st_atime))
723 if (stat("/proc/self/", &st) && stat(argv0, &st) && stat("/", &st))
726 if (st.st_atim.tv_sec != st.st_atime || st.st_atim.tv_nsec > 1000000000)
729 if (st.st_mtim.tv_sec != st.st_mtime || st.st_mtim.tv_nsec > 1000000000)
732 if (st.st_ctim.tv_sec != st.st_ctime || st.st_ctim.tv_nsec > 1000000000)
738 int test_mmap_munmap(void)
740 int ret, fd, i, page_size;
742 size_t file_size, length;
743 off_t offset, pa_offset;
744 struct stat stat_buf;
745 const char * const files[] = {
747 "/proc/1/exe", "/proc/self/exe",
752 page_size = getpagesize();
756 /* find a right file to mmap, existed and accessible */
757 for (i = 0; files[i] != NULL; i++) {
758 ret = fd = open(files[i], O_RDONLY);
767 ret = stat(files[i], &stat_buf);
771 /* file size of the special /dev/zero is 0, let's assign one manually */
773 file_size = 3*page_size;
775 file_size = stat_buf.st_size;
777 offset = file_size - 1;
780 length = file_size - offset;
781 pa_offset = offset & ~(page_size - 1);
783 mem = mmap(NULL, length + offset - pa_offset, PROT_READ, MAP_SHARED, fd, pa_offset);
784 if (mem == MAP_FAILED) {
789 ret = munmap(mem, length + offset - pa_offset);
798 const char *const msg = "hello, nolibc";
803 if (pipe(pipefd) == -1)
806 write(pipefd[1], msg, strlen(msg));
808 len = read(pipefd[0], buf, sizeof(buf));
811 if (len != strlen(msg))
814 return !!memcmp(buf, msg, len);
818 /* Run syscall tests between IDs <min> and <max>.
819 * Return 0 on success, non-zero on failure.
821 int run_syscall(int min, int max)
825 struct stat stat_buf;
834 /* <proc> indicates whether or not /proc is mounted */
835 proc = stat("/proc", &stat_buf) == 0;
837 /* this will be used to skip certain tests that can't be run unprivileged */
838 euid0 = geteuid() == 0;
840 /* from 2.30, glibc provides gettid() */
841 #if defined(__GLIBC_MINOR__) && defined(__GLIBC__)
842 has_gettid = __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 30);
845 for (test = min; test >= 0 && test <= max; test++) {
846 int llen = 0; /* line length */
848 /* avoid leaving empty lines below, this will insert holes into
851 switch (test + __LINE__ + 1) {
852 CASE_TEST(getpid); EXPECT_SYSNE(1, getpid(), -1); break;
853 CASE_TEST(getppid); EXPECT_SYSNE(1, getppid(), -1); break;
854 CASE_TEST(gettid); EXPECT_SYSNE(has_gettid, gettid(), -1); break;
855 CASE_TEST(getpgid_self); EXPECT_SYSNE(1, getpgid(0), -1); break;
856 CASE_TEST(getpgid_bad); EXPECT_SYSER(1, getpgid(-1), -1, ESRCH); break;
857 CASE_TEST(kill_0); EXPECT_SYSZR(1, kill(getpid(), 0)); break;
858 CASE_TEST(kill_CONT); EXPECT_SYSZR(1, kill(getpid(), 0)); break;
859 CASE_TEST(kill_BADPID); EXPECT_SYSER(1, kill(INT_MAX, 0), -1, ESRCH); break;
860 CASE_TEST(sbrk_0); EXPECT_PTRNE(1, sbrk(0), (void *)-1); break;
861 CASE_TEST(sbrk); if ((p1 = p2 = sbrk(4096)) != (void *)-1) p2 = sbrk(-4096); EXPECT_SYSZR(1, (p2 == (void *)-1) || p2 == p1); break;
862 CASE_TEST(brk); EXPECT_SYSZR(1, brk(sbrk(0))); break;
863 CASE_TEST(chdir_root); EXPECT_SYSZR(1, chdir("/")); chdir(getenv("PWD")); break;
864 CASE_TEST(chdir_dot); EXPECT_SYSZR(1, chdir(".")); break;
865 CASE_TEST(chdir_blah); EXPECT_SYSER(1, chdir("/blah"), -1, ENOENT); break;
866 CASE_TEST(chmod_argv0); EXPECT_SYSZR(1, chmod(argv0, 0555)); break;
867 CASE_TEST(chmod_self); EXPECT_SYSER(proc, chmod("/proc/self", 0555), -1, EPERM); break;
868 CASE_TEST(chown_self); EXPECT_SYSER(proc, chown("/proc/self", 0, 0), -1, EPERM); break;
869 CASE_TEST(chroot_root); EXPECT_SYSZR(euid0, chroot("/")); break;
870 CASE_TEST(chroot_blah); EXPECT_SYSER(1, chroot("/proc/self/blah"), -1, ENOENT); break;
871 CASE_TEST(chroot_exe); EXPECT_SYSER(1, chroot(argv0), -1, ENOTDIR); break;
872 CASE_TEST(close_m1); EXPECT_SYSER(1, close(-1), -1, EBADF); break;
873 CASE_TEST(close_dup); EXPECT_SYSZR(1, close(dup(0))); break;
874 CASE_TEST(dup_0); tmp = dup(0); EXPECT_SYSNE(1, tmp, -1); close(tmp); break;
875 CASE_TEST(dup_m1); tmp = dup(-1); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break;
876 CASE_TEST(dup2_0); tmp = dup2(0, 100); EXPECT_SYSNE(1, tmp, -1); close(tmp); break;
877 CASE_TEST(dup2_m1); tmp = dup2(-1, 100); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break;
878 CASE_TEST(dup3_0); tmp = dup3(0, 100, 0); EXPECT_SYSNE(1, tmp, -1); close(tmp); break;
879 CASE_TEST(dup3_m1); tmp = dup3(-1, 100, 0); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break;
880 CASE_TEST(execve_root); EXPECT_SYSER(1, execve("/", (char*[]){ [0] = "/", [1] = NULL }, NULL), -1, EACCES); break;
881 CASE_TEST(fork); EXPECT_SYSZR(1, test_fork()); break;
882 CASE_TEST(getdents64_root); EXPECT_SYSNE(1, test_getdents64("/"), -1); break;
883 CASE_TEST(getdents64_null); EXPECT_SYSER(1, test_getdents64("/dev/null"), -1, ENOTDIR); break;
884 CASE_TEST(gettimeofday_tv); EXPECT_SYSZR(1, gettimeofday(&tv, NULL)); break;
885 CASE_TEST(gettimeofday_tv_tz);EXPECT_SYSZR(1, gettimeofday(&tv, &tz)); break;
886 CASE_TEST(getpagesize); EXPECT_SYSZR(1, test_getpagesize()); break;
887 CASE_TEST(ioctl_tiocinq); EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); break;
888 CASE_TEST(ioctl_tiocinq); EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); break;
889 CASE_TEST(link_root1); EXPECT_SYSER(1, link("/", "/"), -1, EEXIST); break;
890 CASE_TEST(link_blah); EXPECT_SYSER(1, link("/proc/self/blah", "/blah"), -1, ENOENT); break;
891 CASE_TEST(link_dir); EXPECT_SYSER(euid0, link("/", "/blah"), -1, EPERM); break;
892 CASE_TEST(link_cross); EXPECT_SYSER(proc, link("/proc/self/cmdline", "/blah"), -1, EXDEV); break;
893 CASE_TEST(lseek_m1); EXPECT_SYSER(1, lseek(-1, 0, SEEK_SET), -1, EBADF); break;
894 CASE_TEST(lseek_0); EXPECT_SYSER(1, lseek(0, 0, SEEK_SET), -1, ESPIPE); break;
895 CASE_TEST(mkdir_root); EXPECT_SYSER(1, mkdir("/", 0755), -1, EEXIST); break;
896 CASE_TEST(mmap_bad); EXPECT_PTRER(1, mmap(NULL, 0, PROT_READ, MAP_PRIVATE, 0, 0), MAP_FAILED, EINVAL); break;
897 CASE_TEST(munmap_bad); EXPECT_SYSER(1, munmap((void *)1, 0), -1, EINVAL); break;
898 CASE_TEST(mmap_munmap_good); EXPECT_SYSZR(1, test_mmap_munmap()); break;
899 CASE_TEST(open_tty); EXPECT_SYSNE(1, tmp = open("/dev/null", 0), -1); if (tmp != -1) close(tmp); break;
900 CASE_TEST(open_blah); EXPECT_SYSER(1, tmp = open("/proc/self/blah", 0), -1, ENOENT); if (tmp != -1) close(tmp); break;
901 CASE_TEST(pipe); EXPECT_SYSZR(1, test_pipe()); break;
902 CASE_TEST(poll_null); EXPECT_SYSZR(1, poll(NULL, 0, 0)); break;
903 CASE_TEST(poll_stdout); EXPECT_SYSNE(1, ({ struct pollfd fds = { 1, POLLOUT, 0}; poll(&fds, 1, 0); }), -1); break;
904 CASE_TEST(poll_fault); EXPECT_SYSER(1, poll((void *)1, 1, 0), -1, EFAULT); break;
905 CASE_TEST(prctl); EXPECT_SYSER(1, prctl(PR_SET_NAME, (unsigned long)NULL, 0, 0, 0), -1, EFAULT); break;
906 CASE_TEST(read_badf); EXPECT_SYSER(1, read(-1, &tmp, 1), -1, EBADF); break;
907 CASE_TEST(rmdir_blah); EXPECT_SYSER(1, rmdir("/blah"), -1, ENOENT); break;
908 CASE_TEST(sched_yield); EXPECT_SYSZR(1, sched_yield()); break;
909 CASE_TEST(select_null); EXPECT_SYSZR(1, ({ struct timeval tv = { 0 }; select(0, NULL, NULL, NULL, &tv); })); break;
910 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;
911 CASE_TEST(select_fault); EXPECT_SYSER(1, select(1, (void *)1, NULL, NULL, 0), -1, EFAULT); break;
912 CASE_TEST(stat_blah); EXPECT_SYSER(1, stat("/proc/self/blah", &stat_buf), -1, ENOENT); break;
913 CASE_TEST(stat_fault); EXPECT_SYSER(1, stat((void *)1, &stat_buf), -1, EFAULT); break;
914 CASE_TEST(stat_timestamps); EXPECT_SYSZR(1, test_stat_timestamps()); break;
915 CASE_TEST(symlink_root); EXPECT_SYSER(1, symlink("/", "/"), -1, EEXIST); break;
916 CASE_TEST(unlink_root); EXPECT_SYSER(1, unlink("/"), -1, EISDIR); break;
917 CASE_TEST(unlink_blah); EXPECT_SYSER(1, unlink("/proc/self/blah"), -1, ENOENT); break;
918 CASE_TEST(wait_child); EXPECT_SYSER(1, wait(&tmp), -1, ECHILD); break;
919 CASE_TEST(waitpid_min); EXPECT_SYSER(1, waitpid(INT_MIN, &tmp, WNOHANG), -1, ESRCH); break;
920 CASE_TEST(waitpid_child); EXPECT_SYSER(1, waitpid(getpid(), &tmp, WNOHANG), -1, ECHILD); break;
921 CASE_TEST(write_badf); EXPECT_SYSER(1, write(-1, &tmp, 1), -1, EBADF); break;
922 CASE_TEST(write_zero); EXPECT_SYSZR(1, write(1, &tmp, 0)); break;
923 CASE_TEST(syscall_noargs); EXPECT_SYSEQ(1, syscall(__NR_getpid), getpid()); break;
924 CASE_TEST(syscall_args); EXPECT_SYSER(1, syscall(__NR_statx, 0, NULL, 0, 0, NULL), -1, EFAULT); break;
926 return ret; /* must be last */
927 /* note: do not set any defaults so as to permit holes above */
933 int run_stdlib(int min, int max)
938 for (test = min; test >= 0 && test <= max; test++) {
939 int llen = 0; /* line length */
941 /* avoid leaving empty lines below, this will insert holes into
944 switch (test + __LINE__ + 1) {
945 CASE_TEST(getenv_TERM); EXPECT_STRNZ(1, getenv("TERM")); break;
946 CASE_TEST(getenv_blah); EXPECT_STRZR(1, getenv("blah")); break;
947 CASE_TEST(setcmp_blah_blah); EXPECT_EQ(1, strcmp("blah", "blah"), 0); break;
948 CASE_TEST(setcmp_blah_blah2); EXPECT_NE(1, strcmp("blah", "blah2"), 0); break;
949 CASE_TEST(setncmp_blah_blah); EXPECT_EQ(1, strncmp("blah", "blah", 10), 0); break;
950 CASE_TEST(setncmp_blah_blah4); EXPECT_EQ(1, strncmp("blah", "blah4", 4), 0); break;
951 CASE_TEST(setncmp_blah_blah5); EXPECT_NE(1, strncmp("blah", "blah5", 5), 0); break;
952 CASE_TEST(setncmp_blah_blah6); EXPECT_NE(1, strncmp("blah", "blah6", 6), 0); break;
953 CASE_TEST(strchr_foobar_o); EXPECT_STREQ(1, strchr("foobar", 'o'), "oobar"); break;
954 CASE_TEST(strchr_foobar_z); EXPECT_STRZR(1, strchr("foobar", 'z')); break;
955 CASE_TEST(strrchr_foobar_o); EXPECT_STREQ(1, strrchr("foobar", 'o'), "obar"); break;
956 CASE_TEST(strrchr_foobar_z); EXPECT_STRZR(1, strrchr("foobar", 'z')); break;
957 CASE_TEST(memcmp_20_20); EXPECT_EQ(1, memcmp("aaa\x20", "aaa\x20", 4), 0); break;
958 CASE_TEST(memcmp_20_60); EXPECT_LT(1, memcmp("aaa\x20", "aaa\x60", 4), 0); break;
959 CASE_TEST(memcmp_60_20); EXPECT_GT(1, memcmp("aaa\x60", "aaa\x20", 4), 0); break;
960 CASE_TEST(memcmp_20_e0); EXPECT_LT(1, memcmp("aaa\x20", "aaa\xe0", 4), 0); break;
961 CASE_TEST(memcmp_e0_20); EXPECT_GT(1, memcmp("aaa\xe0", "aaa\x20", 4), 0); break;
962 CASE_TEST(memcmp_80_e0); EXPECT_LT(1, memcmp("aaa\x80", "aaa\xe0", 4), 0); break;
963 CASE_TEST(memcmp_e0_80); EXPECT_GT(1, memcmp("aaa\xe0", "aaa\x80", 4), 0); break;
964 CASE_TEST(limit_int8_max); EXPECT_EQ(1, INT8_MAX, (int8_t) 0x7f); break;
965 CASE_TEST(limit_int8_min); EXPECT_EQ(1, INT8_MIN, (int8_t) 0x80); break;
966 CASE_TEST(limit_uint8_max); EXPECT_EQ(1, UINT8_MAX, (uint8_t) 0xff); break;
967 CASE_TEST(limit_int16_max); EXPECT_EQ(1, INT16_MAX, (int16_t) 0x7fff); break;
968 CASE_TEST(limit_int16_min); EXPECT_EQ(1, INT16_MIN, (int16_t) 0x8000); break;
969 CASE_TEST(limit_uint16_max); EXPECT_EQ(1, UINT16_MAX, (uint16_t) 0xffff); break;
970 CASE_TEST(limit_int32_max); EXPECT_EQ(1, INT32_MAX, (int32_t) 0x7fffffff); break;
971 CASE_TEST(limit_int32_min); EXPECT_EQ(1, INT32_MIN, (int32_t) 0x80000000); break;
972 CASE_TEST(limit_uint32_max); EXPECT_EQ(1, UINT32_MAX, (uint32_t) 0xffffffff); break;
973 CASE_TEST(limit_int64_max); EXPECT_EQ(1, INT64_MAX, (int64_t) 0x7fffffffffffffff); break;
974 CASE_TEST(limit_int64_min); EXPECT_EQ(1, INT64_MIN, (int64_t) 0x8000000000000000); break;
975 CASE_TEST(limit_uint64_max); EXPECT_EQ(1, UINT64_MAX, (uint64_t) 0xffffffffffffffff); break;
976 CASE_TEST(limit_int_least8_max); EXPECT_EQ(1, INT_LEAST8_MAX, (int_least8_t) 0x7f); break;
977 CASE_TEST(limit_int_least8_min); EXPECT_EQ(1, INT_LEAST8_MIN, (int_least8_t) 0x80); break;
978 CASE_TEST(limit_uint_least8_max); EXPECT_EQ(1, UINT_LEAST8_MAX, (uint_least8_t) 0xff); break;
979 CASE_TEST(limit_int_least16_max); EXPECT_EQ(1, INT_LEAST16_MAX, (int_least16_t) 0x7fff); break;
980 CASE_TEST(limit_int_least16_min); EXPECT_EQ(1, INT_LEAST16_MIN, (int_least16_t) 0x8000); break;
981 CASE_TEST(limit_uint_least16_max); EXPECT_EQ(1, UINT_LEAST16_MAX, (uint_least16_t) 0xffff); break;
982 CASE_TEST(limit_int_least32_max); EXPECT_EQ(1, INT_LEAST32_MAX, (int_least32_t) 0x7fffffff); break;
983 CASE_TEST(limit_int_least32_min); EXPECT_EQ(1, INT_LEAST32_MIN, (int_least32_t) 0x80000000); break;
984 CASE_TEST(limit_uint_least32_max); EXPECT_EQ(1, UINT_LEAST32_MAX, (uint_least32_t) 0xffffffffU); break;
985 CASE_TEST(limit_int_least64_min); EXPECT_EQ(1, INT_LEAST64_MIN, (int_least64_t) 0x8000000000000000LL); break;
986 CASE_TEST(limit_int_least64_max); EXPECT_EQ(1, INT_LEAST64_MAX, (int_least64_t) 0x7fffffffffffffffLL); break;
987 CASE_TEST(limit_uint_least64_max); EXPECT_EQ(1, UINT_LEAST64_MAX, (uint_least64_t) 0xffffffffffffffffULL); break;
988 CASE_TEST(limit_int_fast8_max); EXPECT_EQ(1, INT_FAST8_MAX, (int_fast8_t) 0x7f); break;
989 CASE_TEST(limit_int_fast8_min); EXPECT_EQ(1, INT_FAST8_MIN, (int_fast8_t) 0x80); break;
990 CASE_TEST(limit_uint_fast8_max); EXPECT_EQ(1, UINT_FAST8_MAX, (uint_fast8_t) 0xff); break;
991 CASE_TEST(limit_int_fast16_min); EXPECT_EQ(1, INT_FAST16_MIN, (int_fast16_t) SINT_MIN_OF_TYPE(int_fast16_t)); break;
992 CASE_TEST(limit_int_fast16_max); EXPECT_EQ(1, INT_FAST16_MAX, (int_fast16_t) SINT_MAX_OF_TYPE(int_fast16_t)); break;
993 CASE_TEST(limit_uint_fast16_max); EXPECT_EQ(1, UINT_FAST16_MAX, (uint_fast16_t) UINTPTR_MAX); break;
994 CASE_TEST(limit_int_fast32_min); EXPECT_EQ(1, INT_FAST32_MIN, (int_fast32_t) SINT_MIN_OF_TYPE(int_fast32_t)); break;
995 CASE_TEST(limit_int_fast32_max); EXPECT_EQ(1, INT_FAST32_MAX, (int_fast32_t) SINT_MAX_OF_TYPE(int_fast32_t)); break;
996 CASE_TEST(limit_uint_fast32_max); EXPECT_EQ(1, UINT_FAST32_MAX, (uint_fast32_t) UINTPTR_MAX); break;
997 CASE_TEST(limit_int_fast64_min); EXPECT_EQ(1, INT_FAST64_MIN, (int_fast64_t) INT64_MIN); break;
998 CASE_TEST(limit_int_fast64_max); EXPECT_EQ(1, INT_FAST64_MAX, (int_fast64_t) INT64_MAX); break;
999 CASE_TEST(limit_uint_fast64_max); EXPECT_EQ(1, UINT_FAST64_MAX, (uint_fast64_t) UINT64_MAX); break;
1000 CASE_TEST(sizeof_long_sane); EXPECT_EQ(1, sizeof(long) == 8 || sizeof(long) == 4, 1); break;
1001 CASE_TEST(limit_intptr_min); EXPECT_EQ(1, INTPTR_MIN, sizeof(long) == 8 ? (intptr_t) 0x8000000000000000LL : (intptr_t) 0x80000000); break;
1002 CASE_TEST(limit_intptr_max); EXPECT_EQ(1, INTPTR_MAX, sizeof(long) == 8 ? (intptr_t) 0x7fffffffffffffffLL : (intptr_t) 0x7fffffff); break;
1003 CASE_TEST(limit_uintptr_max); EXPECT_EQ(1, UINTPTR_MAX, sizeof(long) == 8 ? (uintptr_t) 0xffffffffffffffffULL : (uintptr_t) 0xffffffffU); break;
1004 CASE_TEST(limit_ptrdiff_min); EXPECT_EQ(1, PTRDIFF_MIN, sizeof(long) == 8 ? (ptrdiff_t) 0x8000000000000000LL : (ptrdiff_t) 0x80000000); break;
1005 CASE_TEST(limit_ptrdiff_max); EXPECT_EQ(1, PTRDIFF_MAX, sizeof(long) == 8 ? (ptrdiff_t) 0x7fffffffffffffffLL : (ptrdiff_t) 0x7fffffff); break;
1006 CASE_TEST(limit_size_max); EXPECT_EQ(1, SIZE_MAX, sizeof(long) == 8 ? (size_t) 0xffffffffffffffffULL : (size_t) 0xffffffffU); break;
1009 return ret; /* must be last */
1010 /* note: do not set any defaults so as to permit holes above */
1016 #define EXPECT_VFPRINTF(c, expected, fmt, ...) \
1017 ret += expect_vfprintf(llen, c, expected, fmt, ##__VA_ARGS__)
1019 static int expect_vfprintf(int llen, int c, const char *expected, const char *fmt, ...)
1027 fd = open("/tmp", O_TMPFILE | O_EXCL | O_RDWR, 0600);
1029 result(llen, SKIPPED);
1033 memfile = fdopen(fd, "w+");
1039 va_start(args, fmt);
1040 w = vfprintf(memfile, fmt, args);
1044 llen += printf(" written(%d) != %d", (int)w, c);
1050 lseek(fd, 0, SEEK_SET);
1052 r = read(fd, buf, sizeof(buf) - 1);
1057 llen += printf(" written(%d) != read(%d)", (int)w, (int)r);
1063 llen += printf(" \"%s\" = \"%s\"", expected, buf);
1064 ret = strncmp(expected, buf, c);
1066 result(llen, ret ? FAIL : OK);
1070 static int run_vfprintf(int min, int max)
1075 for (test = min; test >= 0 && test <= max; test++) {
1076 int llen = 0; /* line length */
1078 /* avoid leaving empty lines below, this will insert holes into
1081 switch (test + __LINE__ + 1) {
1082 CASE_TEST(empty); EXPECT_VFPRINTF(0, "", ""); break;
1083 CASE_TEST(simple); EXPECT_VFPRINTF(3, "foo", "foo"); break;
1084 CASE_TEST(string); EXPECT_VFPRINTF(3, "foo", "%s", "foo"); break;
1085 CASE_TEST(number); EXPECT_VFPRINTF(4, "1234", "%d", 1234); break;
1086 CASE_TEST(negnumber); EXPECT_VFPRINTF(5, "-1234", "%d", -1234); break;
1087 CASE_TEST(unsigned); EXPECT_VFPRINTF(5, "12345", "%u", 12345); break;
1088 CASE_TEST(char); EXPECT_VFPRINTF(1, "c", "%c", 'c'); break;
1089 CASE_TEST(hex); EXPECT_VFPRINTF(1, "f", "%x", 0xf); break;
1090 CASE_TEST(pointer); EXPECT_VFPRINTF(3, "0x1", "%p", (void *) 0x1); break;
1092 return ret; /* must be last */
1093 /* note: do not set any defaults so as to permit holes above */
1099 static int smash_stack(void)
1102 volatile char *ptr = buf;
1105 for (i = 0; i < 200; i++)
1111 static int run_protection(int min __attribute__((unused)),
1112 int max __attribute__((unused)))
1115 int llen = 0, status;
1117 llen += printf("0 -fstackprotector ");
1119 #if !defined(_NOLIBC_STACKPROTECTOR)
1120 llen += printf("not supported");
1121 result(llen, SKIPPED);
1125 #if defined(_NOLIBC_STACKPROTECTOR)
1126 if (!__stack_chk_guard) {
1127 llen += printf("__stack_chk_guard not initialized");
1138 llen += printf("fork()");
1143 close(STDOUT_FILENO);
1144 close(STDERR_FILENO);
1146 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
1151 pid = waitpid(pid, &status, 0);
1153 if (pid == -1 || !WIFSIGNALED(status) || WTERMSIG(status) != SIGABRT) {
1154 llen += printf("waitpid()");
1163 /* prepare what needs to be prepared for pid 1 (stdio, /dev, /proc, etc) */
1166 struct stat stat_buf;
1168 /* It's possible that /dev doesn't even exist or was not mounted, so
1169 * we'll try to create it, mount it, or create minimal entries into it.
1170 * We want at least /dev/null and /dev/console.
1172 if (stat("/dev/.", &stat_buf) == 0 || mkdir("/dev", 0755) == 0) {
1173 if (stat("/dev/console", &stat_buf) != 0 ||
1174 stat("/dev/null", &stat_buf) != 0 ||
1175 stat("/dev/zero", &stat_buf) != 0) {
1176 /* try devtmpfs first, otherwise fall back to manual creation */
1177 if (mount("/dev", "/dev", "devtmpfs", 0, 0) != 0) {
1178 mknod("/dev/console", 0600 | S_IFCHR, makedev(5, 1));
1179 mknod("/dev/null", 0666 | S_IFCHR, makedev(1, 3));
1180 mknod("/dev/zero", 0666 | S_IFCHR, makedev(1, 5));
1185 /* If no /dev/console was found before calling init, stdio is closed so
1186 * we need to reopen it from /dev/console. If it failed above, it will
1187 * still fail here and we cannot emit a message anyway.
1189 if (close(dup(1)) == -1) {
1190 int fd = open("/dev/console", O_RDWR);
1201 puts("\nSuccessfully reopened /dev/console.");
1205 /* try to mount /proc if not mounted. Silently fail otherwise */
1206 if (stat("/proc/.", &stat_buf) == 0 || mkdir("/proc", 0755) == 0) {
1207 if (stat("/proc/self", &stat_buf) != 0) {
1208 /* If not mountable, remove /proc completely to avoid misuse */
1209 if (mount("none", "/proc", "proc", 0, 0) != 0)
1214 /* some tests rely on a writable /tmp */
1215 mkdir("/tmp", 0755);
1220 /* This is the definition of known test names, with their functions */
1221 static const struct test test_names[] = {
1222 /* add new tests here */
1223 { .name = "startup", .func = run_startup },
1224 { .name = "syscall", .func = run_syscall },
1225 { .name = "stdlib", .func = run_stdlib },
1226 { .name = "vfprintf", .func = run_vfprintf },
1227 { .name = "protection", .func = run_protection },
1231 static int is_setting_valid(char *test)
1233 int idx, len, test_len, valid = 0;
1239 test_len = strlen(test);
1241 for (idx = 0; test_names[idx].name; idx++) {
1242 len = strlen(test_names[idx].name);
1246 if (strncmp(test, test_names[idx].name, len) != 0)
1249 delimiter = test[len];
1250 if (delimiter != ':' && delimiter != ',' && delimiter != '\0')
1260 int main(int argc, char **argv, char **envp)
1274 /* when called as init, it's possible that no console was opened, for
1275 * example if no /dev file system was provided. We'll check that fd#1
1276 * was opened, and if not we'll attempt to create and open /dev/console
1277 * and /dev/null that we'll use for later tests.
1282 /* the definition of a series of tests comes from either argv[1] or the
1283 * "NOLIBC_TEST" environment variable. It's made of a comma-delimited
1284 * series of test names and optional ranges:
1285 * syscall:5-15[:.*],stdlib:8-10
1288 if (!is_setting_valid(test))
1289 test = getenv("NOLIBC_TEST");
1291 if (is_setting_valid(test)) {
1292 char *comma, *colon, *dash, *value;
1295 comma = strchr(test, ',');
1299 colon = strchr(test, ':');
1303 for (idx = 0; test_names[idx].name; idx++) {
1304 if (strcmp(test, test_names[idx].name) == 0)
1308 if (test_names[idx].name) {
1309 /* The test was named, it will be called at least
1310 * once. We may have an optional range at <colon>
1311 * here, which defaults to the full range.
1314 min = 0; max = INT_MAX;
1316 if (value && *value) {
1317 colon = strchr(value, ':');
1321 dash = strchr(value, '-');
1325 /* support :val: :min-max: :min-: :-max: */
1336 /* now's time to call the test */
1337 printf("Running test '%s'\n", test_names[idx].name);
1338 err = test_names[idx].func(min, max);
1340 printf("Errors during this test: %d\n\n", err);
1341 } while (colon && *colon);
1343 printf("Ignoring unknown test name '%s'\n", test);
1346 } while (test && *test);
1348 /* no test mentioned, run everything */
1349 for (idx = 0; test_names[idx].name; idx++) {
1350 printf("Running test '%s'\n", test_names[idx].name);
1351 err = test_names[idx].func(min, max);
1353 printf("Errors during this test: %d\n\n", err);
1357 printf("Total number of errors: %d\n", ret);
1359 if (getpid() == 1) {
1360 /* we're running as init, there's no other process on the
1361 * system, thus likely started from a VM for a quick check.
1362 * Exiting will provoke a kernel panic that may be reported
1363 * as an error by Qemu or the hypervisor, while stopping
1364 * cleanly will often be reported as a success. This allows
1365 * to use the output of this program for bisecting kernels.
1367 printf("Leaving init with final status: %d\n", !!ret);
1369 reboot(RB_POWER_OFF);
1370 #if defined(__x86_64__)
1371 /* QEMU started with "-device isa-debug-exit -no-reboot" will
1372 * exit with status code 2N+1 when N is written to 0x501. We
1373 * hard-code the syscall here as it's arch-dependent.
1375 else if (syscall(__NR_ioperm, 0x501, 1, 1) == 0)
1376 __asm__ volatile ("outb %%al, %%dx" :: "d"(0x501), "a"(0));
1377 /* if it does nothing, fall back to the regular panic */
1381 printf("Exiting with status %d\n", !!ret);