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 */
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 /* for the type of int_fast16_t and int_fast32_t, musl differs from glibc and nolibc */
44 #define SINT_MAX_OF_TYPE(type) (((type)1 << (sizeof(type) * 8 - 2)) - (type)1 + ((type)1 << (sizeof(type) * 8 - 2)))
45 #define SINT_MIN_OF_TYPE(type) (-SINT_MAX_OF_TYPE(type) - 1)
47 /* will be used to test initialization of environ */
48 static char **test_envp;
50 /* will be used by some test cases as readable file, please don't write it */
51 static const char *argv0;
53 /* definition of a series of tests */
55 const char *name; /* test name */
56 int (*func)(int min, int max); /* handler */
59 #ifndef _NOLIBC_STDLIB_H
65 ret = snprintf(buf, sizeof(buf), "%d", i);
66 return (ret >= 0 && ret < sizeof(buf)) ? buf : "#err";
70 #define CASE_ERR(err) \
73 /* returns the error name (e.g. "ENOENT") for common errors, "SUCCESS" for 0,
74 * or the decimal value for less common ones.
76 const char *errorname(int err)
79 case 0: return "SUCCESS";
121 static void putcharn(char c, size_t n)
136 static void result(int llen, enum RESULT r)
142 else if (r == SKIPPED)
147 putcharn(' ', 64 - llen);
151 /* The tests below are intended to be used by the macroes, which evaluate
152 * expression <expr>, print the status to stdout, and update the "ret"
153 * variable to count failures. The functions themselves return the number
154 * of failures, thus either 0 or 1.
157 #define EXPECT_ZR(cond, expr) \
158 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_zr(expr, llen); } while (0)
160 static int expect_zr(int expr, int llen)
162 int ret = !(expr == 0);
164 llen += printf(" = %d ", expr);
165 result(llen, ret ? FAIL : OK);
170 #define EXPECT_NZ(cond, expr, val) \
171 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_nz(expr, llen; } while (0)
173 static int expect_nz(int expr, int llen)
175 int ret = !(expr != 0);
177 llen += printf(" = %d ", expr);
178 result(llen, ret ? FAIL : OK);
183 #define EXPECT_EQ(cond, expr, val) \
184 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_eq(expr, llen, val); } while (0)
186 static int expect_eq(uint64_t expr, int llen, uint64_t val)
188 int ret = !(expr == val);
190 llen += printf(" = %lld ", (long long)expr);
191 result(llen, ret ? FAIL : OK);
196 #define EXPECT_NE(cond, expr, val) \
197 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ne(expr, llen, val); } while (0)
199 static int expect_ne(int expr, int llen, int val)
201 int ret = !(expr != val);
203 llen += printf(" = %d ", expr);
204 result(llen, ret ? FAIL : OK);
209 #define EXPECT_GE(cond, expr, val) \
210 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ge(expr, llen, val); } while (0)
212 static int expect_ge(int expr, int llen, int val)
214 int ret = !(expr >= val);
216 llen += printf(" = %d ", expr);
217 result(llen, ret ? FAIL : OK);
222 #define EXPECT_GT(cond, expr, val) \
223 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_gt(expr, llen, val); } while (0)
225 static int expect_gt(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_LE(cond, expr, val) \
236 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_le(expr, llen, val); } while (0)
238 static int expect_le(int expr, int llen, int val)
240 int ret = !(expr <= val);
242 llen += printf(" = %d ", expr);
243 result(llen, ret ? FAIL : OK);
248 #define EXPECT_LT(cond, expr, val) \
249 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_lt(expr, llen, val); } while (0)
251 static int expect_lt(int expr, int llen, int val)
253 int ret = !(expr < val);
255 llen += printf(" = %d ", expr);
256 result(llen, ret ? FAIL : OK);
261 #define EXPECT_SYSZR(cond, expr) \
262 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_syszr(expr, llen); } while (0)
264 static int expect_syszr(int expr, int llen)
270 llen += printf(" = %d %s ", expr, errorname(errno));
273 llen += printf(" = %d ", expr);
280 #define EXPECT_SYSEQ(cond, expr, val) \
281 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_syseq(expr, llen, val); } while (0)
283 static int expect_syseq(int expr, int llen, int val)
289 llen += printf(" = %d %s ", expr, errorname(errno));
292 llen += printf(" = %d ", expr);
299 #define EXPECT_SYSNE(cond, expr, val) \
300 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_sysne(expr, llen, val); } while (0)
302 static int expect_sysne(int expr, int llen, int val)
308 llen += printf(" = %d %s ", expr, errorname(errno));
311 llen += printf(" = %d ", expr);
318 #define EXPECT_SYSER2(cond, expr, expret, experr1, experr2) \
319 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_syserr2(expr, expret, experr1, experr2, llen); } while (0)
321 #define EXPECT_SYSER(cond, expr, expret, experr) \
322 EXPECT_SYSER2(cond, expr, expret, experr, 0)
324 static int expect_syserr2(int expr, int expret, int experr1, int experr2, int llen)
329 llen += printf(" = %d %s ", expr, errorname(_errno));
330 if (expr != expret || (_errno != experr1 && _errno != experr2)) {
333 llen += printf(" != (%d %s) ", expret, errorname(experr1));
335 llen += printf(" != (%d %s %s) ", expret, errorname(experr1), errorname(experr2));
344 #define EXPECT_PTRZR(cond, expr) \
345 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrzr(expr, llen); } while (0)
347 static int expect_ptrzr(const void *expr, int llen)
351 llen += printf(" = <%p> ", expr);
362 #define EXPECT_PTRNZ(cond, expr) \
363 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrnz(expr, llen); } while (0)
365 static int expect_ptrnz(const void *expr, int llen)
369 llen += printf(" = <%p> ", expr);
379 #define EXPECT_PTREQ(cond, expr, cmp) \
380 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptreq(expr, llen, cmp); } while (0)
382 static int expect_ptreq(const void *expr, int llen, const void *cmp)
386 llen += printf(" = <%p> ", expr);
396 #define EXPECT_PTRNE(cond, expr, cmp) \
397 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrne(expr, llen, cmp); } while (0)
399 static int expect_ptrne(const void *expr, int llen, const void *cmp)
403 llen += printf(" = <%p> ", expr);
413 #define EXPECT_PTRER2(cond, expr, expret, experr1, experr2) \
414 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrerr2(expr, expret, experr1, experr2, llen); } while (0)
416 #define EXPECT_PTRER(cond, expr, expret, experr) \
417 EXPECT_PTRER2(cond, expr, expret, experr, 0)
419 static int expect_ptrerr2(const void *expr, const void *expret, int experr1, int experr2, int llen)
424 llen += printf(" = <%p> %s ", expr, errorname(_errno));
425 if (expr != expret || (_errno != experr1 && _errno != experr2)) {
428 llen += printf(" != (<%p> %s) ", expret, errorname(experr1));
430 llen += printf(" != (<%p> %s %s) ", expret, errorname(experr1), errorname(experr2));
438 #define EXPECT_STRZR(cond, expr) \
439 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_strzr(expr, llen); } while (0)
441 static int expect_strzr(const char *expr, int llen)
445 llen += printf(" = <%s> ", expr);
456 #define EXPECT_STRNZ(cond, expr) \
457 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_strnz(expr, llen); } while (0)
459 static int expect_strnz(const char *expr, int llen)
463 llen += printf(" = <%s> ", expr);
474 #define EXPECT_STREQ(cond, expr, cmp) \
475 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_streq(expr, llen, cmp); } while (0)
477 static int expect_streq(const char *expr, int llen, const char *cmp)
481 llen += printf(" = <%s> ", expr);
482 if (strcmp(expr, cmp) != 0) {
492 #define EXPECT_STRNE(cond, expr, cmp) \
493 do { if (!(cond)) result(llen, SKIPPED); else ret += expect_strne(expr, llen, cmp); } while (0)
495 static int expect_strne(const char *expr, int llen, const char *cmp)
499 llen += printf(" = <%s> ", expr);
500 if (strcmp(expr, cmp) == 0) {
510 /* declare tests based on line numbers. There must be exactly one test per line. */
511 #define CASE_TEST(name) \
512 case __LINE__: llen += printf("%d %s", test, #name);
515 /* used by some syscall tests below */
516 int test_getdents64(const char *dir)
522 ret = fd = open(dir, O_RDONLY | O_DIRECTORY, 0);
526 ret = getdents64(fd, (void *)buffer, sizeof(buffer));
534 static int test_getpagesize(void)
536 long x = getpagesize();
542 #if defined(__x86_64__) || defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__)
544 * x86 family is always 4K page.
547 #elif defined(__aarch64__)
549 * Linux aarch64 supports three values of page size: 4K, 16K, and 64K
550 * which are selected at kernel compilation time.
552 c = (x == 4096 || x == (16 * 1024) || x == (64 * 1024));
555 * Assuming other architectures must have at least 4K page.
563 static int test_fork(void)
568 /* flush the printf buffer to avoid child flush it */
582 pid = waitpid(pid, &status, 0);
584 return pid == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 123;
588 static int test_stat_timestamps(void)
592 if (sizeof(st.st_atim.tv_sec) != sizeof(st.st_atime))
595 if (stat("/proc/self/", &st) && stat(argv0, &st) && stat("/", &st))
598 if (st.st_atim.tv_sec != st.st_atime || st.st_atim.tv_nsec > 1000000000)
601 if (st.st_mtim.tv_sec != st.st_mtime || st.st_mtim.tv_nsec > 1000000000)
604 if (st.st_ctim.tv_sec != st.st_ctime || st.st_ctim.tv_nsec > 1000000000)
610 int test_mmap_munmap(void)
614 size_t page_size, file_size, length;
615 off_t offset, pa_offset;
616 struct stat stat_buf;
617 const char * const files[] = {
619 "/proc/1/exe", "/proc/self/exe",
624 page_size = getpagesize();
628 /* find a right file to mmap, existed and accessible */
629 for (i = 0; files[i] != NULL; i++) {
630 ret = fd = open(files[i], O_RDONLY);
639 ret = stat(files[i], &stat_buf);
643 /* file size of the special /dev/zero is 0, let's assign one manually */
645 file_size = 3*page_size;
647 file_size = stat_buf.st_size;
649 offset = file_size - 1;
652 length = file_size - offset;
653 pa_offset = offset & ~(page_size - 1);
655 mem = mmap(NULL, length + offset - pa_offset, PROT_READ, MAP_SHARED, fd, pa_offset);
656 if (mem == MAP_FAILED) {
661 ret = munmap(mem, length + offset - pa_offset);
669 /* Run syscall tests between IDs <min> and <max>.
670 * Return 0 on success, non-zero on failure.
672 int run_syscall(int min, int max)
676 struct stat stat_buf;
685 /* <proc> indicates whether or not /proc is mounted */
686 proc = stat("/proc", &stat_buf) == 0;
688 /* this will be used to skip certain tests that can't be run unprivileged */
689 euid0 = geteuid() == 0;
691 /* from 2.30, glibc provides gettid() */
692 #if defined(__GLIBC_MINOR__) && defined(__GLIBC__)
693 has_gettid = __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 30);
696 for (test = min; test >= 0 && test <= max; test++) {
697 int llen = 0; /* line length */
699 /* avoid leaving empty lines below, this will insert holes into
702 switch (test + __LINE__ + 1) {
703 CASE_TEST(getpid); EXPECT_SYSNE(1, getpid(), -1); break;
704 CASE_TEST(getppid); EXPECT_SYSNE(1, getppid(), -1); break;
705 CASE_TEST(gettid); EXPECT_SYSNE(has_gettid, gettid(), -1); break;
706 CASE_TEST(getpgid_self); EXPECT_SYSNE(1, getpgid(0), -1); break;
707 CASE_TEST(getpgid_bad); EXPECT_SYSER(1, getpgid(-1), -1, ESRCH); break;
708 CASE_TEST(kill_0); EXPECT_SYSZR(1, kill(getpid(), 0)); break;
709 CASE_TEST(kill_CONT); EXPECT_SYSZR(1, kill(getpid(), 0)); break;
710 CASE_TEST(kill_BADPID); EXPECT_SYSER(1, kill(INT_MAX, 0), -1, ESRCH); break;
711 CASE_TEST(sbrk_0); EXPECT_PTRNE(1, sbrk(0), (void *)-1); break;
712 CASE_TEST(sbrk); if ((p1 = p2 = sbrk(4096)) != (void *)-1) p2 = sbrk(-4096); EXPECT_SYSZR(1, (p2 == (void *)-1) || p2 == p1); break;
713 CASE_TEST(brk); EXPECT_SYSZR(1, brk(sbrk(0))); break;
714 CASE_TEST(chdir_root); EXPECT_SYSZR(1, chdir("/")); chdir(getenv("PWD")); break;
715 CASE_TEST(chdir_dot); EXPECT_SYSZR(1, chdir(".")); break;
716 CASE_TEST(chdir_blah); EXPECT_SYSER(1, chdir("/blah"), -1, ENOENT); break;
717 CASE_TEST(chmod_argv0); EXPECT_SYSZR(1, chmod(argv0, 0555)); break;
718 CASE_TEST(chmod_net); EXPECT_SYSZR(proc, chmod("/proc/self/net", 0555)); break;
719 CASE_TEST(chmod_self); EXPECT_SYSER(proc, chmod("/proc/self", 0555), -1, EPERM); break;
720 CASE_TEST(chown_self); EXPECT_SYSER(proc, chown("/proc/self", 0, 0), -1, EPERM); break;
721 CASE_TEST(chroot_root); EXPECT_SYSZR(euid0, chroot("/")); break;
722 CASE_TEST(chroot_blah); EXPECT_SYSER(1, chroot("/proc/self/blah"), -1, ENOENT); break;
723 CASE_TEST(chroot_exe); EXPECT_SYSER(1, chroot(argv0), -1, ENOTDIR); break;
724 CASE_TEST(close_m1); EXPECT_SYSER(1, close(-1), -1, EBADF); break;
725 CASE_TEST(close_dup); EXPECT_SYSZR(1, close(dup(0))); break;
726 CASE_TEST(dup_0); tmp = dup(0); EXPECT_SYSNE(1, tmp, -1); close(tmp); break;
727 CASE_TEST(dup_m1); tmp = dup(-1); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break;
728 CASE_TEST(dup2_0); tmp = dup2(0, 100); EXPECT_SYSNE(1, tmp, -1); close(tmp); break;
729 CASE_TEST(dup2_m1); tmp = dup2(-1, 100); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break;
730 CASE_TEST(dup3_0); tmp = dup3(0, 100, 0); EXPECT_SYSNE(1, tmp, -1); close(tmp); break;
731 CASE_TEST(dup3_m1); tmp = dup3(-1, 100, 0); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break;
732 CASE_TEST(execve_root); EXPECT_SYSER(1, execve("/", (char*[]){ [0] = "/", [1] = NULL }, NULL), -1, EACCES); break;
733 CASE_TEST(fork); EXPECT_SYSZR(1, test_fork()); break;
734 CASE_TEST(getdents64_root); EXPECT_SYSNE(1, test_getdents64("/"), -1); break;
735 CASE_TEST(getdents64_null); EXPECT_SYSER(1, test_getdents64("/dev/null"), -1, ENOTDIR); break;
736 CASE_TEST(gettimeofday_tv); EXPECT_SYSZR(1, gettimeofday(&tv, NULL)); break;
737 CASE_TEST(gettimeofday_tv_tz);EXPECT_SYSZR(1, gettimeofday(&tv, &tz)); break;
738 CASE_TEST(getpagesize); EXPECT_SYSZR(1, test_getpagesize()); break;
739 CASE_TEST(ioctl_tiocinq); EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); break;
740 CASE_TEST(ioctl_tiocinq); EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); break;
741 CASE_TEST(link_root1); EXPECT_SYSER(1, link("/", "/"), -1, EEXIST); break;
742 CASE_TEST(link_blah); EXPECT_SYSER(1, link("/proc/self/blah", "/blah"), -1, ENOENT); break;
743 CASE_TEST(link_dir); EXPECT_SYSER(euid0, link("/", "/blah"), -1, EPERM); break;
744 CASE_TEST(link_cross); EXPECT_SYSER(proc, link("/proc/self/cmdline", "/blah"), -1, EXDEV); break;
745 CASE_TEST(lseek_m1); EXPECT_SYSER(1, lseek(-1, 0, SEEK_SET), -1, EBADF); break;
746 CASE_TEST(lseek_0); EXPECT_SYSER(1, lseek(0, 0, SEEK_SET), -1, ESPIPE); break;
747 CASE_TEST(mkdir_root); EXPECT_SYSER(1, mkdir("/", 0755), -1, EEXIST); break;
748 CASE_TEST(mmap_bad); EXPECT_PTRER(1, mmap(NULL, 0, PROT_READ, MAP_PRIVATE, 0, 0), MAP_FAILED, EINVAL); break;
749 CASE_TEST(munmap_bad); EXPECT_SYSER(1, munmap((void *)1, 0), -1, EINVAL); break;
750 CASE_TEST(mmap_munmap_good); EXPECT_SYSZR(1, test_mmap_munmap()); break;
751 CASE_TEST(open_tty); EXPECT_SYSNE(1, tmp = open("/dev/null", 0), -1); if (tmp != -1) close(tmp); break;
752 CASE_TEST(open_blah); EXPECT_SYSER(1, tmp = open("/proc/self/blah", 0), -1, ENOENT); if (tmp != -1) close(tmp); break;
753 CASE_TEST(poll_null); EXPECT_SYSZR(1, poll(NULL, 0, 0)); break;
754 CASE_TEST(poll_stdout); EXPECT_SYSNE(1, ({ struct pollfd fds = { 1, POLLOUT, 0}; poll(&fds, 1, 0); }), -1); break;
755 CASE_TEST(poll_fault); EXPECT_SYSER(1, poll((void *)1, 1, 0), -1, EFAULT); break;
756 CASE_TEST(prctl); EXPECT_SYSER(1, prctl(PR_SET_NAME, (unsigned long)NULL, 0, 0, 0), -1, EFAULT); break;
757 CASE_TEST(read_badf); EXPECT_SYSER(1, read(-1, &tmp, 1), -1, EBADF); break;
758 CASE_TEST(rmdir_blah); EXPECT_SYSER(1, rmdir("/blah"), -1, ENOENT); break;
759 CASE_TEST(sched_yield); EXPECT_SYSZR(1, sched_yield()); break;
760 CASE_TEST(select_null); EXPECT_SYSZR(1, ({ struct timeval tv = { 0 }; select(0, NULL, NULL, NULL, &tv); })); break;
761 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;
762 CASE_TEST(select_fault); EXPECT_SYSER(1, select(1, (void *)1, NULL, NULL, 0), -1, EFAULT); break;
763 CASE_TEST(stat_blah); EXPECT_SYSER(1, stat("/proc/self/blah", &stat_buf), -1, ENOENT); break;
764 CASE_TEST(stat_fault); EXPECT_SYSER(1, stat((void *)1, &stat_buf), -1, EFAULT); break;
765 CASE_TEST(stat_timestamps); EXPECT_SYSZR(1, test_stat_timestamps()); break;
766 CASE_TEST(symlink_root); EXPECT_SYSER(1, symlink("/", "/"), -1, EEXIST); break;
767 CASE_TEST(unlink_root); EXPECT_SYSER(1, unlink("/"), -1, EISDIR); break;
768 CASE_TEST(unlink_blah); EXPECT_SYSER(1, unlink("/proc/self/blah"), -1, ENOENT); break;
769 CASE_TEST(wait_child); EXPECT_SYSER(1, wait(&tmp), -1, ECHILD); break;
770 CASE_TEST(waitpid_min); EXPECT_SYSER(1, waitpid(INT_MIN, &tmp, WNOHANG), -1, ESRCH); break;
771 CASE_TEST(waitpid_child); EXPECT_SYSER(1, waitpid(getpid(), &tmp, WNOHANG), -1, ECHILD); break;
772 CASE_TEST(write_badf); EXPECT_SYSER(1, write(-1, &tmp, 1), -1, EBADF); break;
773 CASE_TEST(write_zero); EXPECT_SYSZR(1, write(1, &tmp, 0)); break;
774 CASE_TEST(syscall_noargs); EXPECT_SYSEQ(1, syscall(__NR_getpid), getpid()); break;
775 CASE_TEST(syscall_args); EXPECT_SYSER(1, syscall(__NR_statx, 0, NULL, 0, 0, NULL), -1, EFAULT); break;
777 return ret; /* must be last */
778 /* note: do not set any defaults so as to permit holes above */
784 int run_stdlib(int min, int max)
791 for (test = min; test >= 0 && test <= max; test++) {
792 int llen = 0; /* line length */
794 /* avoid leaving empty lines below, this will insert holes into
797 switch (test + __LINE__ + 1) {
798 CASE_TEST(environ); EXPECT_PTREQ(1, environ, test_envp); break;
799 CASE_TEST(getenv_TERM); EXPECT_STRNZ(1, getenv("TERM")); break;
800 CASE_TEST(getenv_blah); EXPECT_STRZR(1, getenv("blah")); break;
801 CASE_TEST(setcmp_blah_blah); EXPECT_EQ(1, strcmp("blah", "blah"), 0); break;
802 CASE_TEST(setcmp_blah_blah2); EXPECT_NE(1, strcmp("blah", "blah2"), 0); break;
803 CASE_TEST(setncmp_blah_blah); EXPECT_EQ(1, strncmp("blah", "blah", 10), 0); break;
804 CASE_TEST(setncmp_blah_blah4); EXPECT_EQ(1, strncmp("blah", "blah4", 4), 0); break;
805 CASE_TEST(setncmp_blah_blah5); EXPECT_NE(1, strncmp("blah", "blah5", 5), 0); break;
806 CASE_TEST(setncmp_blah_blah6); EXPECT_NE(1, strncmp("blah", "blah6", 6), 0); break;
807 CASE_TEST(strchr_foobar_o); EXPECT_STREQ(1, strchr("foobar", 'o'), "oobar"); break;
808 CASE_TEST(strchr_foobar_z); EXPECT_STRZR(1, strchr("foobar", 'z')); break;
809 CASE_TEST(strrchr_foobar_o); EXPECT_STREQ(1, strrchr("foobar", 'o'), "obar"); break;
810 CASE_TEST(strrchr_foobar_z); EXPECT_STRZR(1, strrchr("foobar", 'z')); break;
811 CASE_TEST(memcmp_20_20); EXPECT_EQ(1, memcmp("aaa\x20", "aaa\x20", 4), 0); break;
812 CASE_TEST(memcmp_20_60); EXPECT_LT(1, memcmp("aaa\x20", "aaa\x60", 4), 0); break;
813 CASE_TEST(memcmp_60_20); EXPECT_GT(1, memcmp("aaa\x60", "aaa\x20", 4), 0); break;
814 CASE_TEST(memcmp_20_e0); EXPECT_LT(1, memcmp("aaa\x20", "aaa\xe0", 4), 0); break;
815 CASE_TEST(memcmp_e0_20); EXPECT_GT(1, memcmp("aaa\xe0", "aaa\x20", 4), 0); break;
816 CASE_TEST(memcmp_80_e0); EXPECT_LT(1, memcmp("aaa\x80", "aaa\xe0", 4), 0); break;
817 CASE_TEST(memcmp_e0_80); EXPECT_GT(1, memcmp("aaa\xe0", "aaa\x80", 4), 0); break;
818 CASE_TEST(limit_int8_max); EXPECT_EQ(1, INT8_MAX, (int8_t) 0x7f); break;
819 CASE_TEST(limit_int8_min); EXPECT_EQ(1, INT8_MIN, (int8_t) 0x80); break;
820 CASE_TEST(limit_uint8_max); EXPECT_EQ(1, UINT8_MAX, (uint8_t) 0xff); break;
821 CASE_TEST(limit_int16_max); EXPECT_EQ(1, INT16_MAX, (int16_t) 0x7fff); break;
822 CASE_TEST(limit_int16_min); EXPECT_EQ(1, INT16_MIN, (int16_t) 0x8000); break;
823 CASE_TEST(limit_uint16_max); EXPECT_EQ(1, UINT16_MAX, (uint16_t) 0xffff); break;
824 CASE_TEST(limit_int32_max); EXPECT_EQ(1, INT32_MAX, (int32_t) 0x7fffffff); break;
825 CASE_TEST(limit_int32_min); EXPECT_EQ(1, INT32_MIN, (int32_t) 0x80000000); break;
826 CASE_TEST(limit_uint32_max); EXPECT_EQ(1, UINT32_MAX, (uint32_t) 0xffffffff); break;
827 CASE_TEST(limit_int64_max); EXPECT_EQ(1, INT64_MAX, (int64_t) 0x7fffffffffffffff); break;
828 CASE_TEST(limit_int64_min); EXPECT_EQ(1, INT64_MIN, (int64_t) 0x8000000000000000); break;
829 CASE_TEST(limit_uint64_max); EXPECT_EQ(1, UINT64_MAX, (uint64_t) 0xffffffffffffffff); break;
830 CASE_TEST(limit_int_least8_max); EXPECT_EQ(1, INT_LEAST8_MAX, (int_least8_t) 0x7f); break;
831 CASE_TEST(limit_int_least8_min); EXPECT_EQ(1, INT_LEAST8_MIN, (int_least8_t) 0x80); break;
832 CASE_TEST(limit_uint_least8_max); EXPECT_EQ(1, UINT_LEAST8_MAX, (uint_least8_t) 0xff); break;
833 CASE_TEST(limit_int_least16_max); EXPECT_EQ(1, INT_LEAST16_MAX, (int_least16_t) 0x7fff); break;
834 CASE_TEST(limit_int_least16_min); EXPECT_EQ(1, INT_LEAST16_MIN, (int_least16_t) 0x8000); break;
835 CASE_TEST(limit_uint_least16_max); EXPECT_EQ(1, UINT_LEAST16_MAX, (uint_least16_t) 0xffff); break;
836 CASE_TEST(limit_int_least32_max); EXPECT_EQ(1, INT_LEAST32_MAX, (int_least32_t) 0x7fffffff); break;
837 CASE_TEST(limit_int_least32_min); EXPECT_EQ(1, INT_LEAST32_MIN, (int_least32_t) 0x80000000); break;
838 CASE_TEST(limit_uint_least32_max); EXPECT_EQ(1, UINT_LEAST32_MAX, (uint_least32_t) 0xffffffffU); break;
839 CASE_TEST(limit_int_least64_min); EXPECT_EQ(1, INT_LEAST64_MIN, (int_least64_t) 0x8000000000000000LL); break;
840 CASE_TEST(limit_int_least64_max); EXPECT_EQ(1, INT_LEAST64_MAX, (int_least64_t) 0x7fffffffffffffffLL); break;
841 CASE_TEST(limit_uint_least64_max); EXPECT_EQ(1, UINT_LEAST64_MAX, (uint_least64_t) 0xffffffffffffffffULL); break;
842 CASE_TEST(limit_int_fast8_max); EXPECT_EQ(1, INT_FAST8_MAX, (int_fast8_t) 0x7f); break;
843 CASE_TEST(limit_int_fast8_min); EXPECT_EQ(1, INT_FAST8_MIN, (int_fast8_t) 0x80); break;
844 CASE_TEST(limit_uint_fast8_max); EXPECT_EQ(1, UINT_FAST8_MAX, (uint_fast8_t) 0xff); break;
845 CASE_TEST(limit_int_fast16_min); EXPECT_EQ(1, INT_FAST16_MIN, (int_fast16_t) SINT_MIN_OF_TYPE(int_fast16_t)); break;
846 CASE_TEST(limit_int_fast16_max); EXPECT_EQ(1, INT_FAST16_MAX, (int_fast16_t) SINT_MAX_OF_TYPE(int_fast16_t)); break;
847 CASE_TEST(limit_uint_fast16_max); EXPECT_EQ(1, UINT_FAST16_MAX, (uint_fast16_t) UINTPTR_MAX); break;
848 CASE_TEST(limit_int_fast32_min); EXPECT_EQ(1, INT_FAST32_MIN, (int_fast32_t) SINT_MIN_OF_TYPE(int_fast32_t)); break;
849 CASE_TEST(limit_int_fast32_max); EXPECT_EQ(1, INT_FAST32_MAX, (int_fast32_t) SINT_MAX_OF_TYPE(int_fast32_t)); break;
850 CASE_TEST(limit_uint_fast32_max); EXPECT_EQ(1, UINT_FAST32_MAX, (uint_fast32_t) UINTPTR_MAX); break;
851 CASE_TEST(limit_int_fast64_min); EXPECT_EQ(1, INT_FAST64_MIN, (int_fast64_t) INT64_MIN); break;
852 CASE_TEST(limit_int_fast64_max); EXPECT_EQ(1, INT_FAST64_MAX, (int_fast64_t) INT64_MAX); break;
853 CASE_TEST(limit_uint_fast64_max); EXPECT_EQ(1, UINT_FAST64_MAX, (uint_fast64_t) UINT64_MAX); break;
854 #if __SIZEOF_LONG__ == 8
855 CASE_TEST(limit_intptr_min); EXPECT_EQ(1, INTPTR_MIN, (intptr_t) 0x8000000000000000LL); break;
856 CASE_TEST(limit_intptr_max); EXPECT_EQ(1, INTPTR_MAX, (intptr_t) 0x7fffffffffffffffLL); break;
857 CASE_TEST(limit_uintptr_max); EXPECT_EQ(1, UINTPTR_MAX, (uintptr_t) 0xffffffffffffffffULL); break;
858 CASE_TEST(limit_ptrdiff_min); EXPECT_EQ(1, PTRDIFF_MIN, (ptrdiff_t) 0x8000000000000000LL); break;
859 CASE_TEST(limit_ptrdiff_max); EXPECT_EQ(1, PTRDIFF_MAX, (ptrdiff_t) 0x7fffffffffffffffLL); break;
860 CASE_TEST(limit_size_max); EXPECT_EQ(1, SIZE_MAX, (size_t) 0xffffffffffffffffULL); break;
861 #elif __SIZEOF_LONG__ == 4
862 CASE_TEST(limit_intptr_min); EXPECT_EQ(1, INTPTR_MIN, (intptr_t) 0x80000000); break;
863 CASE_TEST(limit_intptr_max); EXPECT_EQ(1, INTPTR_MAX, (intptr_t) 0x7fffffff); break;
864 CASE_TEST(limit_uintptr_max); EXPECT_EQ(1, UINTPTR_MAX, (uintptr_t) 0xffffffffU); break;
865 CASE_TEST(limit_ptrdiff_min); EXPECT_EQ(1, PTRDIFF_MIN, (ptrdiff_t) 0x80000000); break;
866 CASE_TEST(limit_ptrdiff_max); EXPECT_EQ(1, PTRDIFF_MAX, (ptrdiff_t) 0x7fffffff); break;
867 CASE_TEST(limit_size_max); EXPECT_EQ(1, SIZE_MAX, (size_t) 0xffffffffU); break;
869 # warning "__SIZEOF_LONG__ is undefined"
870 #endif /* __SIZEOF_LONG__ */
872 return ret; /* must be last */
873 /* note: do not set any defaults so as to permit holes above */
879 #define EXPECT_VFPRINTF(c, expected, fmt, ...) \
880 ret += expect_vfprintf(llen, c, expected, fmt, ##__VA_ARGS__)
882 static int expect_vfprintf(int llen, size_t c, const char *expected, const char *fmt, ...)
889 fd = open("/tmp", O_TMPFILE | O_EXCL | O_RDWR, 0600);
891 result(llen, SKIPPED);
895 memfile = fdopen(fd, "w+");
902 w = vfprintf(memfile, fmt, args);
906 llen += printf(" written(%d) != %d", w, (int) c);
912 lseek(fd, 0, SEEK_SET);
914 r = read(fd, buf, sizeof(buf) - 1);
920 llen += printf(" written(%d) != read(%d)", w, r);
925 llen += printf(" \"%s\" = \"%s\"", expected, buf);
926 ret = strncmp(expected, buf, c);
928 result(llen, ret ? FAIL : OK);
932 static int run_vfprintf(int min, int max)
939 for (test = min; test >= 0 && test <= max; test++) {
940 int llen = 0; /* line length */
942 /* avoid leaving empty lines below, this will insert holes into
945 switch (test + __LINE__ + 1) {
946 CASE_TEST(empty); EXPECT_VFPRINTF(0, "", ""); break;
947 CASE_TEST(simple); EXPECT_VFPRINTF(3, "foo", "foo"); break;
948 CASE_TEST(string); EXPECT_VFPRINTF(3, "foo", "%s", "foo"); break;
949 CASE_TEST(number); EXPECT_VFPRINTF(4, "1234", "%d", 1234); break;
950 CASE_TEST(negnumber); EXPECT_VFPRINTF(5, "-1234", "%d", -1234); break;
951 CASE_TEST(unsigned); EXPECT_VFPRINTF(5, "12345", "%u", 12345); break;
952 CASE_TEST(char); EXPECT_VFPRINTF(1, "c", "%c", 'c'); break;
953 CASE_TEST(hex); EXPECT_VFPRINTF(1, "f", "%x", 0xf); break;
954 CASE_TEST(pointer); EXPECT_VFPRINTF(3, "0x1", "%p", (void *) 0x1); break;
956 return ret; /* must be last */
957 /* note: do not set any defaults so as to permit holes above */
963 static int smash_stack(void)
966 volatile char *ptr = buf;
969 for (i = 0; i < 200; i++)
975 static int run_protection(int min, int max)
978 int llen = 0, status;
980 llen += printf("0 -fstackprotector ");
982 #if !defined(_NOLIBC_STACKPROTECTOR)
983 llen += printf("not supported");
984 result(llen, SKIPPED);
988 #if defined(_NOLIBC_STACKPROTECTOR)
989 if (!__stack_chk_guard) {
990 llen += printf("__stack_chk_guard not initialized");
1001 llen += printf("fork()");
1006 close(STDOUT_FILENO);
1007 close(STDERR_FILENO);
1009 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
1014 pid = waitpid(pid, &status, 0);
1016 if (pid == -1 || !WIFSIGNALED(status) || WTERMSIG(status) != SIGABRT) {
1017 llen += printf("waitpid()");
1026 /* prepare what needs to be prepared for pid 1 (stdio, /dev, /proc, etc) */
1029 struct stat stat_buf;
1031 /* It's possible that /dev doesn't even exist or was not mounted, so
1032 * we'll try to create it, mount it, or create minimal entries into it.
1033 * We want at least /dev/null and /dev/console.
1035 if (stat("/dev/.", &stat_buf) == 0 || mkdir("/dev", 0755) == 0) {
1036 if (stat("/dev/console", &stat_buf) != 0 ||
1037 stat("/dev/null", &stat_buf) != 0 ||
1038 stat("/dev/zero", &stat_buf) != 0) {
1039 /* try devtmpfs first, otherwise fall back to manual creation */
1040 if (mount("/dev", "/dev", "devtmpfs", 0, 0) != 0) {
1041 mknod("/dev/console", 0600 | S_IFCHR, makedev(5, 1));
1042 mknod("/dev/null", 0666 | S_IFCHR, makedev(1, 3));
1043 mknod("/dev/zero", 0666 | S_IFCHR, makedev(1, 5));
1048 /* If no /dev/console was found before calling init, stdio is closed so
1049 * we need to reopen it from /dev/console. If it failed above, it will
1050 * still fail here and we cannot emit a message anyway.
1052 if (close(dup(1)) == -1) {
1053 int fd = open("/dev/console", O_RDWR);
1064 puts("\nSuccessfully reopened /dev/console.");
1068 /* try to mount /proc if not mounted. Silently fail otherwise */
1069 if (stat("/proc/.", &stat_buf) == 0 || mkdir("/proc", 0755) == 0) {
1070 if (stat("/proc/self", &stat_buf) != 0) {
1071 /* If not mountable, remove /proc completely to avoid misuse */
1072 if (mount("none", "/proc", "proc", 0, 0) != 0)
1077 /* some tests rely on a writable /tmp */
1078 mkdir("/tmp", 0755);
1083 /* This is the definition of known test names, with their functions */
1084 static const struct test test_names[] = {
1085 /* add new tests here */
1086 { .name = "syscall", .func = run_syscall },
1087 { .name = "stdlib", .func = run_stdlib },
1088 { .name = "vfprintf", .func = run_vfprintf },
1089 { .name = "protection", .func = run_protection },
1093 int is_setting_valid(char *test)
1095 int idx, len, test_len, valid = 0;
1101 test_len = strlen(test);
1103 for (idx = 0; test_names[idx].name; idx++) {
1104 len = strlen(test_names[idx].name);
1108 if (strncmp(test, test_names[idx].name, len) != 0)
1111 delimiter = test[len];
1112 if (delimiter != ':' && delimiter != ',' && delimiter != '\0')
1122 int main(int argc, char **argv, char **envp)
1134 /* when called as init, it's possible that no console was opened, for
1135 * example if no /dev file system was provided. We'll check that fd#1
1136 * was opened, and if not we'll attempt to create and open /dev/console
1137 * and /dev/null that we'll use for later tests.
1142 /* the definition of a series of tests comes from either argv[1] or the
1143 * "NOLIBC_TEST" environment variable. It's made of a comma-delimited
1144 * series of test names and optional ranges:
1145 * syscall:5-15[:.*],stdlib:8-10
1148 if (!is_setting_valid(test))
1149 test = getenv("NOLIBC_TEST");
1151 if (is_setting_valid(test)) {
1152 char *comma, *colon, *dash, *value;
1155 comma = strchr(test, ',');
1159 colon = strchr(test, ':');
1163 for (idx = 0; test_names[idx].name; idx++) {
1164 if (strcmp(test, test_names[idx].name) == 0)
1168 if (test_names[idx].name) {
1169 /* The test was named, it will be called at least
1170 * once. We may have an optional range at <colon>
1171 * here, which defaults to the full range.
1174 min = 0; max = INT_MAX;
1176 if (value && *value) {
1177 colon = strchr(value, ':');
1181 dash = strchr(value, '-');
1185 /* support :val: :min-max: :min-: :-max: */
1196 /* now's time to call the test */
1197 printf("Running test '%s'\n", test_names[idx].name);
1198 err = test_names[idx].func(min, max);
1200 printf("Errors during this test: %d\n\n", err);
1201 } while (colon && *colon);
1203 printf("Ignoring unknown test name '%s'\n", test);
1206 } while (test && *test);
1208 /* no test mentioned, run everything */
1209 for (idx = 0; test_names[idx].name; idx++) {
1210 printf("Running test '%s'\n", test_names[idx].name);
1211 err = test_names[idx].func(min, max);
1213 printf("Errors during this test: %d\n\n", err);
1217 printf("Total number of errors: %d\n", ret);
1219 if (getpid() == 1) {
1220 /* we're running as init, there's no other process on the
1221 * system, thus likely started from a VM for a quick check.
1222 * Exiting will provoke a kernel panic that may be reported
1223 * as an error by Qemu or the hypervisor, while stopping
1224 * cleanly will often be reported as a success. This allows
1225 * to use the output of this program for bisecting kernels.
1227 printf("Leaving init with final status: %d\n", !!ret);
1229 reboot(RB_POWER_OFF);
1230 #if defined(__x86_64__)
1231 /* QEMU started with "-device isa-debug-exit -no-reboot" will
1232 * exit with status code 2N+1 when N is written to 0x501. We
1233 * hard-code the syscall here as it's arch-dependent.
1235 else if (syscall(__NR_ioperm, 0x501, 1, 1) == 0)
1236 __asm__ volatile ("outb %%al, %%dx" :: "d"(0x501), "a"(0));
1237 /* if it does nothing, fall back to the regular panic */
1241 printf("Exiting with status %d\n", !!ret);