1fddc8f81SThomas Weißschuh /* SPDX-License-Identifier: GPL-2.0 */ 2362aecb2SWilly Tarreau 31da02f51SWilly Tarreau #define _GNU_SOURCE 45f2de00eSZhangjin Wu #define _LARGEFILE64_SOURCE 51da02f51SWilly Tarreau 6362aecb2SWilly Tarreau /* libc-specific include files 71da02f51SWilly Tarreau * The program may be built in 3 ways: 8362aecb2SWilly Tarreau * $(CC) -nostdlib -include /path/to/nolibc.h => NOLIBC already defined 91da02f51SWilly Tarreau * $(CC) -nostdlib -I/path/to/nolibc/sysroot => _NOLIBC_* guards are present 101da02f51SWilly Tarreau * $(CC) with default libc => NOLIBC* never defined 11362aecb2SWilly Tarreau */ 12362aecb2SWilly Tarreau #ifndef NOLIBC 13362aecb2SWilly Tarreau #include <stdio.h> 14362aecb2SWilly Tarreau #include <stdlib.h> 15362aecb2SWilly Tarreau #include <string.h> 161da02f51SWilly Tarreau #ifndef _NOLIBC_STDIO_H 171da02f51SWilly Tarreau /* standard libcs need more includes */ 1848967b73SZhangjin Wu #include <sys/auxv.h> 191da02f51SWilly Tarreau #include <sys/io.h> 201da02f51SWilly Tarreau #include <sys/ioctl.h> 2169f2cd9fSThomas Weißschuh #include <sys/mman.h> 221da02f51SWilly Tarreau #include <sys/mount.h> 23208aa9d9SThomas Weißschuh #include <sys/prctl.h> 241da02f51SWilly Tarreau #include <sys/reboot.h> 251da02f51SWilly Tarreau #include <sys/stat.h> 261da02f51SWilly Tarreau #include <sys/syscall.h> 271da02f51SWilly Tarreau #include <sys/sysmacros.h> 281da02f51SWilly Tarreau #include <sys/time.h> 291da02f51SWilly Tarreau #include <sys/wait.h> 301da02f51SWilly Tarreau #include <dirent.h> 311da02f51SWilly Tarreau #include <errno.h> 321da02f51SWilly Tarreau #include <fcntl.h> 331da02f51SWilly Tarreau #include <poll.h> 341da02f51SWilly Tarreau #include <sched.h> 351da02f51SWilly Tarreau #include <signal.h> 361da02f51SWilly Tarreau #include <stdarg.h> 372df07fc5SWilly Tarreau #include <stddef.h> 382df07fc5SWilly Tarreau #include <stdint.h> 391da02f51SWilly Tarreau #include <unistd.h> 40bd27fef3SZhangjin Wu #include <limits.h> 411da02f51SWilly Tarreau #endif 42362aecb2SWilly Tarreau #endif 43362aecb2SWilly Tarreau 44989abf1cSZhangjin Wu /* for the type of int_fast16_t and int_fast32_t, musl differs from glibc and nolibc */ 45989abf1cSZhangjin Wu #define SINT_MAX_OF_TYPE(type) (((type)1 << (sizeof(type) * 8 - 2)) - (type)1 + ((type)1 << (sizeof(type) * 8 - 2))) 46989abf1cSZhangjin Wu #define SINT_MIN_OF_TYPE(type) (-SINT_MAX_OF_TYPE(type) - 1) 47989abf1cSZhangjin Wu 4867d108e2SThomas Weißschuh /* will be used to test initialization of environ */ 4967d108e2SThomas Weißschuh static char **test_envp; 50362aecb2SWilly Tarreau 5148967b73SZhangjin Wu /* will be used to test initialization of argv */ 5248967b73SZhangjin Wu static char **test_argv; 5348967b73SZhangjin Wu 5448967b73SZhangjin Wu /* will be used to test initialization of argc */ 5548967b73SZhangjin Wu static int test_argc; 5648967b73SZhangjin Wu 57938b5b98SZhangjin Wu /* will be used by some test cases as readable file, please don't write it */ 58938b5b98SZhangjin Wu static const char *argv0; 59938b5b98SZhangjin Wu 6023da7bc9SWilly Tarreau /* definition of a series of tests */ 6123da7bc9SWilly Tarreau struct test { 62fddc8f81SThomas Weißschuh const char *name; /* test name */ 63fddc8f81SThomas Weißschuh int (*func)(int min, int max); /* handler */ 6423da7bc9SWilly Tarreau }; 6523da7bc9SWilly Tarreau 661da02f51SWilly Tarreau #ifndef _NOLIBC_STDLIB_H 671da02f51SWilly Tarreau char *itoa(int i) 681da02f51SWilly Tarreau { 691da02f51SWilly Tarreau static char buf[12]; 701da02f51SWilly Tarreau int ret; 711da02f51SWilly Tarreau 721da02f51SWilly Tarreau ret = snprintf(buf, sizeof(buf), "%d", i); 731da02f51SWilly Tarreau return (ret >= 0 && ret < sizeof(buf)) ? buf : "#err"; 741da02f51SWilly Tarreau } 751da02f51SWilly Tarreau #endif 761da02f51SWilly Tarreau 77362aecb2SWilly Tarreau #define CASE_ERR(err) \ 78362aecb2SWilly Tarreau case err: return #err 79362aecb2SWilly Tarreau 80362aecb2SWilly Tarreau /* returns the error name (e.g. "ENOENT") for common errors, "SUCCESS" for 0, 81362aecb2SWilly Tarreau * or the decimal value for less common ones. 82362aecb2SWilly Tarreau */ 83362aecb2SWilly Tarreau const char *errorname(int err) 84362aecb2SWilly Tarreau { 85362aecb2SWilly Tarreau switch (err) { 86362aecb2SWilly Tarreau case 0: return "SUCCESS"; 87362aecb2SWilly Tarreau CASE_ERR(EPERM); 88362aecb2SWilly Tarreau CASE_ERR(ENOENT); 89362aecb2SWilly Tarreau CASE_ERR(ESRCH); 90362aecb2SWilly Tarreau CASE_ERR(EINTR); 91362aecb2SWilly Tarreau CASE_ERR(EIO); 92362aecb2SWilly Tarreau CASE_ERR(ENXIO); 93362aecb2SWilly Tarreau CASE_ERR(E2BIG); 94362aecb2SWilly Tarreau CASE_ERR(ENOEXEC); 95362aecb2SWilly Tarreau CASE_ERR(EBADF); 96362aecb2SWilly Tarreau CASE_ERR(ECHILD); 97362aecb2SWilly Tarreau CASE_ERR(EAGAIN); 98362aecb2SWilly Tarreau CASE_ERR(ENOMEM); 99362aecb2SWilly Tarreau CASE_ERR(EACCES); 100362aecb2SWilly Tarreau CASE_ERR(EFAULT); 101362aecb2SWilly Tarreau CASE_ERR(ENOTBLK); 102362aecb2SWilly Tarreau CASE_ERR(EBUSY); 103362aecb2SWilly Tarreau CASE_ERR(EEXIST); 104362aecb2SWilly Tarreau CASE_ERR(EXDEV); 105362aecb2SWilly Tarreau CASE_ERR(ENODEV); 106362aecb2SWilly Tarreau CASE_ERR(ENOTDIR); 107362aecb2SWilly Tarreau CASE_ERR(EISDIR); 108362aecb2SWilly Tarreau CASE_ERR(EINVAL); 109362aecb2SWilly Tarreau CASE_ERR(ENFILE); 110362aecb2SWilly Tarreau CASE_ERR(EMFILE); 111362aecb2SWilly Tarreau CASE_ERR(ENOTTY); 112362aecb2SWilly Tarreau CASE_ERR(ETXTBSY); 113362aecb2SWilly Tarreau CASE_ERR(EFBIG); 114362aecb2SWilly Tarreau CASE_ERR(ENOSPC); 115362aecb2SWilly Tarreau CASE_ERR(ESPIPE); 116362aecb2SWilly Tarreau CASE_ERR(EROFS); 117362aecb2SWilly Tarreau CASE_ERR(EMLINK); 118362aecb2SWilly Tarreau CASE_ERR(EPIPE); 119362aecb2SWilly Tarreau CASE_ERR(EDOM); 120362aecb2SWilly Tarreau CASE_ERR(ERANGE); 121362aecb2SWilly Tarreau CASE_ERR(ENOSYS); 122758f970fSZhangjin Wu CASE_ERR(EOVERFLOW); 123362aecb2SWilly Tarreau default: 124362aecb2SWilly Tarreau return itoa(err); 125362aecb2SWilly Tarreau } 126362aecb2SWilly Tarreau } 127362aecb2SWilly Tarreau 128443de903SThomas Weißschuh static void putcharn(char c, size_t n) 129443de903SThomas Weißschuh { 130443de903SThomas Weißschuh char buf[64]; 131443de903SThomas Weißschuh 132443de903SThomas Weißschuh memset(buf, c, n); 133443de903SThomas Weißschuh buf[n] = '\0'; 134443de903SThomas Weißschuh fputs(buf, stdout); 135443de903SThomas Weißschuh } 136443de903SThomas Weißschuh 137b184a261SThomas Weißschuh enum RESULT { 138b184a261SThomas Weißschuh OK, 139b184a261SThomas Weißschuh FAIL, 140b184a261SThomas Weißschuh SKIPPED, 141b184a261SThomas Weißschuh }; 142b184a261SThomas Weißschuh 143b184a261SThomas Weißschuh static void result(int llen, enum RESULT r) 144362aecb2SWilly Tarreau { 145b184a261SThomas Weißschuh const char *msg; 146362aecb2SWilly Tarreau 147b184a261SThomas Weißschuh if (r == OK) 148b184a261SThomas Weißschuh msg = " [OK]"; 149b184a261SThomas Weißschuh else if (r == SKIPPED) 150b184a261SThomas Weißschuh msg = "[SKIPPED]"; 151b184a261SThomas Weißschuh else 152b184a261SThomas Weißschuh msg = "[FAIL]"; 153362aecb2SWilly Tarreau 154447e5602SThomas Weißschuh if (llen < 64) 155b184a261SThomas Weißschuh putcharn(' ', 64 - llen); 156b184a261SThomas Weißschuh puts(msg); 157362aecb2SWilly Tarreau } 158362aecb2SWilly Tarreau 159362aecb2SWilly Tarreau /* The tests below are intended to be used by the macroes, which evaluate 160362aecb2SWilly Tarreau * expression <expr>, print the status to stdout, and update the "ret" 161362aecb2SWilly Tarreau * variable to count failures. The functions themselves return the number 162362aecb2SWilly Tarreau * of failures, thus either 0 or 1. 163362aecb2SWilly Tarreau */ 164362aecb2SWilly Tarreau 165362aecb2SWilly Tarreau #define EXPECT_ZR(cond, expr) \ 166b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_zr(expr, llen); } while (0) 167362aecb2SWilly Tarreau 168362aecb2SWilly Tarreau static int expect_zr(int expr, int llen) 169362aecb2SWilly Tarreau { 170362aecb2SWilly Tarreau int ret = !(expr == 0); 171362aecb2SWilly Tarreau 172362aecb2SWilly Tarreau llen += printf(" = %d ", expr); 173b184a261SThomas Weißschuh result(llen, ret ? FAIL : OK); 174362aecb2SWilly Tarreau return ret; 175362aecb2SWilly Tarreau } 176362aecb2SWilly Tarreau 177362aecb2SWilly Tarreau 178362aecb2SWilly Tarreau #define EXPECT_NZ(cond, expr, val) \ 179b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_nz(expr, llen; } while (0) 180362aecb2SWilly Tarreau 181362aecb2SWilly Tarreau static int expect_nz(int expr, int llen) 182362aecb2SWilly Tarreau { 183362aecb2SWilly Tarreau int ret = !(expr != 0); 184362aecb2SWilly Tarreau 185362aecb2SWilly Tarreau llen += printf(" = %d ", expr); 186b184a261SThomas Weißschuh result(llen, ret ? FAIL : OK); 187362aecb2SWilly Tarreau return ret; 188362aecb2SWilly Tarreau } 189362aecb2SWilly Tarreau 190362aecb2SWilly Tarreau 191362aecb2SWilly Tarreau #define EXPECT_EQ(cond, expr, val) \ 192b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_eq(expr, llen, val); } while (0) 193362aecb2SWilly Tarreau 194a0994fb9SVincent Dagonneau static int expect_eq(uint64_t expr, int llen, uint64_t val) 195362aecb2SWilly Tarreau { 196362aecb2SWilly Tarreau int ret = !(expr == val); 197362aecb2SWilly Tarreau 1980858aec4SZhangjin Wu llen += printf(" = %lld ", (long long)expr); 199b184a261SThomas Weißschuh result(llen, ret ? FAIL : OK); 200362aecb2SWilly Tarreau return ret; 201362aecb2SWilly Tarreau } 202362aecb2SWilly Tarreau 203362aecb2SWilly Tarreau 204362aecb2SWilly Tarreau #define EXPECT_NE(cond, expr, val) \ 205b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ne(expr, llen, val); } while (0) 206362aecb2SWilly Tarreau 207362aecb2SWilly Tarreau static int expect_ne(int expr, int llen, int val) 208362aecb2SWilly Tarreau { 209362aecb2SWilly Tarreau int ret = !(expr != val); 210362aecb2SWilly Tarreau 211362aecb2SWilly Tarreau llen += printf(" = %d ", expr); 212b184a261SThomas Weißschuh result(llen, ret ? FAIL : OK); 213362aecb2SWilly Tarreau return ret; 214362aecb2SWilly Tarreau } 215362aecb2SWilly Tarreau 216362aecb2SWilly Tarreau 217362aecb2SWilly Tarreau #define EXPECT_GE(cond, expr, val) \ 218b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ge(expr, llen, val); } while (0) 219362aecb2SWilly Tarreau 220362aecb2SWilly Tarreau static int expect_ge(int expr, int llen, int val) 221362aecb2SWilly Tarreau { 222362aecb2SWilly Tarreau int ret = !(expr >= val); 223362aecb2SWilly Tarreau 224362aecb2SWilly Tarreau llen += printf(" = %d ", expr); 225b184a261SThomas Weißschuh result(llen, ret ? FAIL : OK); 226362aecb2SWilly Tarreau return ret; 227362aecb2SWilly Tarreau } 228362aecb2SWilly Tarreau 229362aecb2SWilly Tarreau 230362aecb2SWilly Tarreau #define EXPECT_GT(cond, expr, val) \ 231b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_gt(expr, llen, val); } while (0) 232362aecb2SWilly Tarreau 233362aecb2SWilly Tarreau static int expect_gt(int expr, int llen, int val) 234362aecb2SWilly Tarreau { 235362aecb2SWilly Tarreau int ret = !(expr > val); 236362aecb2SWilly Tarreau 237362aecb2SWilly Tarreau llen += printf(" = %d ", expr); 238b184a261SThomas Weißschuh result(llen, ret ? FAIL : OK); 239362aecb2SWilly Tarreau return ret; 240362aecb2SWilly Tarreau } 241362aecb2SWilly Tarreau 242362aecb2SWilly Tarreau 243362aecb2SWilly Tarreau #define EXPECT_LE(cond, expr, val) \ 244b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_le(expr, llen, val); } while (0) 245362aecb2SWilly Tarreau 246362aecb2SWilly Tarreau static int expect_le(int expr, int llen, int val) 247362aecb2SWilly Tarreau { 248362aecb2SWilly Tarreau int ret = !(expr <= val); 249362aecb2SWilly Tarreau 250362aecb2SWilly Tarreau llen += printf(" = %d ", expr); 251b184a261SThomas Weißschuh result(llen, ret ? FAIL : OK); 252362aecb2SWilly Tarreau return ret; 253362aecb2SWilly Tarreau } 254362aecb2SWilly Tarreau 255362aecb2SWilly Tarreau 256362aecb2SWilly Tarreau #define EXPECT_LT(cond, expr, val) \ 257b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_lt(expr, llen, val); } while (0) 258362aecb2SWilly Tarreau 259362aecb2SWilly Tarreau static int expect_lt(int expr, int llen, int val) 260362aecb2SWilly Tarreau { 261362aecb2SWilly Tarreau int ret = !(expr < val); 262362aecb2SWilly Tarreau 263362aecb2SWilly Tarreau llen += printf(" = %d ", expr); 264b184a261SThomas Weißschuh result(llen, ret ? FAIL : OK); 265362aecb2SWilly Tarreau return ret; 266362aecb2SWilly Tarreau } 267362aecb2SWilly Tarreau 268362aecb2SWilly Tarreau 269362aecb2SWilly Tarreau #define EXPECT_SYSZR(cond, expr) \ 270b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_syszr(expr, llen); } while (0) 271362aecb2SWilly Tarreau 272362aecb2SWilly Tarreau static int expect_syszr(int expr, int llen) 273362aecb2SWilly Tarreau { 274362aecb2SWilly Tarreau int ret = 0; 275362aecb2SWilly Tarreau 276362aecb2SWilly Tarreau if (expr) { 277362aecb2SWilly Tarreau ret = 1; 278362aecb2SWilly Tarreau llen += printf(" = %d %s ", expr, errorname(errno)); 279b184a261SThomas Weißschuh result(llen, FAIL); 280362aecb2SWilly Tarreau } else { 281362aecb2SWilly Tarreau llen += printf(" = %d ", expr); 282b184a261SThomas Weißschuh result(llen, OK); 283362aecb2SWilly Tarreau } 284362aecb2SWilly Tarreau return ret; 285362aecb2SWilly Tarreau } 286362aecb2SWilly Tarreau 287362aecb2SWilly Tarreau 288362aecb2SWilly Tarreau #define EXPECT_SYSEQ(cond, expr, val) \ 289b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_syseq(expr, llen, val); } while (0) 290362aecb2SWilly Tarreau 291362aecb2SWilly Tarreau static int expect_syseq(int expr, int llen, int val) 292362aecb2SWilly Tarreau { 293362aecb2SWilly Tarreau int ret = 0; 294362aecb2SWilly Tarreau 295362aecb2SWilly Tarreau if (expr != val) { 296362aecb2SWilly Tarreau ret = 1; 297362aecb2SWilly Tarreau llen += printf(" = %d %s ", expr, errorname(errno)); 298b184a261SThomas Weißschuh result(llen, FAIL); 299362aecb2SWilly Tarreau } else { 300362aecb2SWilly Tarreau llen += printf(" = %d ", expr); 301b184a261SThomas Weißschuh result(llen, OK); 302362aecb2SWilly Tarreau } 303362aecb2SWilly Tarreau return ret; 304362aecb2SWilly Tarreau } 305362aecb2SWilly Tarreau 306362aecb2SWilly Tarreau 307362aecb2SWilly Tarreau #define EXPECT_SYSNE(cond, expr, val) \ 308b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_sysne(expr, llen, val); } while (0) 309362aecb2SWilly Tarreau 310362aecb2SWilly Tarreau static int expect_sysne(int expr, int llen, int val) 311362aecb2SWilly Tarreau { 312362aecb2SWilly Tarreau int ret = 0; 313362aecb2SWilly Tarreau 314362aecb2SWilly Tarreau if (expr == val) { 315362aecb2SWilly Tarreau ret = 1; 316362aecb2SWilly Tarreau llen += printf(" = %d %s ", expr, errorname(errno)); 317b184a261SThomas Weißschuh result(llen, FAIL); 318362aecb2SWilly Tarreau } else { 319362aecb2SWilly Tarreau llen += printf(" = %d ", expr); 320b184a261SThomas Weißschuh result(llen, OK); 321362aecb2SWilly Tarreau } 322362aecb2SWilly Tarreau return ret; 323362aecb2SWilly Tarreau } 324362aecb2SWilly Tarreau 325362aecb2SWilly Tarreau 32675d75a7bSZhangjin Wu #define EXPECT_SYSER2(cond, expr, expret, experr1, experr2) \ 327b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_syserr2(expr, expret, experr1, experr2, llen); } while (0) 328362aecb2SWilly Tarreau 32975d75a7bSZhangjin Wu #define EXPECT_SYSER(cond, expr, expret, experr) \ 33075d75a7bSZhangjin Wu EXPECT_SYSER2(cond, expr, expret, experr, 0) 33175d75a7bSZhangjin Wu 33275d75a7bSZhangjin Wu static int expect_syserr2(int expr, int expret, int experr1, int experr2, int llen) 333362aecb2SWilly Tarreau { 334362aecb2SWilly Tarreau int ret = 0; 335362aecb2SWilly Tarreau int _errno = errno; 336362aecb2SWilly Tarreau 337362aecb2SWilly Tarreau llen += printf(" = %d %s ", expr, errorname(_errno)); 33875d75a7bSZhangjin Wu if (expr != expret || (_errno != experr1 && _errno != experr2)) { 339362aecb2SWilly Tarreau ret = 1; 34075d75a7bSZhangjin Wu if (experr2 == 0) 34175d75a7bSZhangjin Wu llen += printf(" != (%d %s) ", expret, errorname(experr1)); 34275d75a7bSZhangjin Wu else 34375d75a7bSZhangjin Wu llen += printf(" != (%d %s %s) ", expret, errorname(experr1), errorname(experr2)); 344b184a261SThomas Weißschuh result(llen, FAIL); 345362aecb2SWilly Tarreau } else { 346b184a261SThomas Weißschuh result(llen, OK); 347362aecb2SWilly Tarreau } 348362aecb2SWilly Tarreau return ret; 349362aecb2SWilly Tarreau } 350362aecb2SWilly Tarreau 351362aecb2SWilly Tarreau 352362aecb2SWilly Tarreau #define EXPECT_PTRZR(cond, expr) \ 353b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrzr(expr, llen); } while (0) 354362aecb2SWilly Tarreau 355362aecb2SWilly Tarreau static int expect_ptrzr(const void *expr, int llen) 356362aecb2SWilly Tarreau { 357362aecb2SWilly Tarreau int ret = 0; 358362aecb2SWilly Tarreau 359362aecb2SWilly Tarreau llen += printf(" = <%p> ", expr); 360362aecb2SWilly Tarreau if (expr) { 361362aecb2SWilly Tarreau ret = 1; 362b184a261SThomas Weißschuh result(llen, FAIL); 363362aecb2SWilly Tarreau } else { 364b184a261SThomas Weißschuh result(llen, OK); 365362aecb2SWilly Tarreau } 366362aecb2SWilly Tarreau return ret; 367362aecb2SWilly Tarreau } 368362aecb2SWilly Tarreau 369362aecb2SWilly Tarreau 370362aecb2SWilly Tarreau #define EXPECT_PTRNZ(cond, expr) \ 371b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrnz(expr, llen); } while (0) 372362aecb2SWilly Tarreau 373362aecb2SWilly Tarreau static int expect_ptrnz(const void *expr, int llen) 374362aecb2SWilly Tarreau { 375362aecb2SWilly Tarreau int ret = 0; 376362aecb2SWilly Tarreau 377362aecb2SWilly Tarreau llen += printf(" = <%p> ", expr); 378362aecb2SWilly Tarreau if (!expr) { 379362aecb2SWilly Tarreau ret = 1; 380b184a261SThomas Weißschuh result(llen, FAIL); 381362aecb2SWilly Tarreau } else { 382b184a261SThomas Weißschuh result(llen, OK); 383362aecb2SWilly Tarreau } 384362aecb2SWilly Tarreau return ret; 385362aecb2SWilly Tarreau } 386362aecb2SWilly Tarreau 38729f5540bSZhangjin Wu #define EXPECT_PTREQ(cond, expr, cmp) \ 388b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptreq(expr, llen, cmp); } while (0) 38929f5540bSZhangjin Wu 39029f5540bSZhangjin Wu static int expect_ptreq(const void *expr, int llen, const void *cmp) 39129f5540bSZhangjin Wu { 39229f5540bSZhangjin Wu int ret = 0; 39329f5540bSZhangjin Wu 39429f5540bSZhangjin Wu llen += printf(" = <%p> ", expr); 39529f5540bSZhangjin Wu if (expr != cmp) { 39629f5540bSZhangjin Wu ret = 1; 397b184a261SThomas Weißschuh result(llen, FAIL); 39829f5540bSZhangjin Wu } else { 399b184a261SThomas Weißschuh result(llen, OK); 40029f5540bSZhangjin Wu } 40129f5540bSZhangjin Wu return ret; 40229f5540bSZhangjin Wu } 40329f5540bSZhangjin Wu 40429f5540bSZhangjin Wu #define EXPECT_PTRNE(cond, expr, cmp) \ 405b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrne(expr, llen, cmp); } while (0) 40629f5540bSZhangjin Wu 40729f5540bSZhangjin Wu static int expect_ptrne(const void *expr, int llen, const void *cmp) 40829f5540bSZhangjin Wu { 40929f5540bSZhangjin Wu int ret = 0; 41029f5540bSZhangjin Wu 41129f5540bSZhangjin Wu llen += printf(" = <%p> ", expr); 41229f5540bSZhangjin Wu if (expr == cmp) { 41329f5540bSZhangjin Wu ret = 1; 414b184a261SThomas Weißschuh result(llen, FAIL); 41529f5540bSZhangjin Wu } else { 416b184a261SThomas Weißschuh result(llen, OK); 41729f5540bSZhangjin Wu } 41829f5540bSZhangjin Wu return ret; 41929f5540bSZhangjin Wu } 42029f5540bSZhangjin Wu 421fd3a9efdSZhangjin Wu #define EXPECT_PTRGE(cond, expr, cmp) \ 422fd3a9efdSZhangjin Wu do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrge(expr, llen, cmp); } while (0) 423fd3a9efdSZhangjin Wu 424fd3a9efdSZhangjin Wu static int expect_ptrge(const void *expr, int llen, const void *cmp) 425fd3a9efdSZhangjin Wu { 426fd3a9efdSZhangjin Wu int ret = !(expr >= cmp); 427fd3a9efdSZhangjin Wu 428fd3a9efdSZhangjin Wu llen += printf(" = <%p> ", expr); 429fd3a9efdSZhangjin Wu result(llen, ret ? FAIL : OK); 430fd3a9efdSZhangjin Wu return ret; 431fd3a9efdSZhangjin Wu } 432fd3a9efdSZhangjin Wu 433fd3a9efdSZhangjin Wu #define EXPECT_PTRGT(cond, expr, cmp) \ 434fd3a9efdSZhangjin Wu do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrgt(expr, llen, cmp); } while (0) 435fd3a9efdSZhangjin Wu 436fd3a9efdSZhangjin Wu static int expect_ptrgt(const void *expr, int llen, const void *cmp) 437fd3a9efdSZhangjin Wu { 438fd3a9efdSZhangjin Wu int ret = !(expr > cmp); 439fd3a9efdSZhangjin Wu 440fd3a9efdSZhangjin Wu llen += printf(" = <%p> ", expr); 441fd3a9efdSZhangjin Wu result(llen, ret ? FAIL : OK); 442fd3a9efdSZhangjin Wu return ret; 443fd3a9efdSZhangjin Wu } 444fd3a9efdSZhangjin Wu 445fd3a9efdSZhangjin Wu 446fd3a9efdSZhangjin Wu #define EXPECT_PTRLE(cond, expr, cmp) \ 447fd3a9efdSZhangjin Wu do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrle(expr, llen, cmp); } while (0) 448fd3a9efdSZhangjin Wu 449fd3a9efdSZhangjin Wu static int expect_ptrle(const void *expr, int llen, const void *cmp) 450fd3a9efdSZhangjin Wu { 451fd3a9efdSZhangjin Wu int ret = !(expr <= cmp); 452fd3a9efdSZhangjin Wu 453fd3a9efdSZhangjin Wu llen += printf(" = <%p> ", expr); 454fd3a9efdSZhangjin Wu result(llen, ret ? FAIL : OK); 455fd3a9efdSZhangjin Wu return ret; 456fd3a9efdSZhangjin Wu } 457fd3a9efdSZhangjin Wu 458fd3a9efdSZhangjin Wu 459fd3a9efdSZhangjin Wu #define EXPECT_PTRLT(cond, expr, cmp) \ 460fd3a9efdSZhangjin Wu do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrlt(expr, llen, cmp); } while (0) 461fd3a9efdSZhangjin Wu 462fd3a9efdSZhangjin Wu static int expect_ptrlt(const void *expr, int llen, const void *cmp) 463fd3a9efdSZhangjin Wu { 464fd3a9efdSZhangjin Wu int ret = !(expr < cmp); 465fd3a9efdSZhangjin Wu 466fd3a9efdSZhangjin Wu llen += printf(" = <%p> ", expr); 467fd3a9efdSZhangjin Wu result(llen, ret ? FAIL : OK); 468fd3a9efdSZhangjin Wu return ret; 469fd3a9efdSZhangjin Wu } 470fd3a9efdSZhangjin Wu 47129f5540bSZhangjin Wu #define EXPECT_PTRER2(cond, expr, expret, experr1, experr2) \ 472b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrerr2(expr, expret, experr1, experr2, llen); } while (0) 47329f5540bSZhangjin Wu 47429f5540bSZhangjin Wu #define EXPECT_PTRER(cond, expr, expret, experr) \ 47529f5540bSZhangjin Wu EXPECT_PTRER2(cond, expr, expret, experr, 0) 47629f5540bSZhangjin Wu 47729f5540bSZhangjin Wu static int expect_ptrerr2(const void *expr, const void *expret, int experr1, int experr2, int llen) 47829f5540bSZhangjin Wu { 47929f5540bSZhangjin Wu int ret = 0; 48029f5540bSZhangjin Wu int _errno = errno; 48129f5540bSZhangjin Wu 48229f5540bSZhangjin Wu llen += printf(" = <%p> %s ", expr, errorname(_errno)); 48329f5540bSZhangjin Wu if (expr != expret || (_errno != experr1 && _errno != experr2)) { 48429f5540bSZhangjin Wu ret = 1; 48529f5540bSZhangjin Wu if (experr2 == 0) 48629f5540bSZhangjin Wu llen += printf(" != (<%p> %s) ", expret, errorname(experr1)); 48729f5540bSZhangjin Wu else 48829f5540bSZhangjin Wu llen += printf(" != (<%p> %s %s) ", expret, errorname(experr1), errorname(experr2)); 489b184a261SThomas Weißschuh result(llen, FAIL); 49029f5540bSZhangjin Wu } else { 491b184a261SThomas Weißschuh result(llen, OK); 49229f5540bSZhangjin Wu } 49329f5540bSZhangjin Wu return ret; 49429f5540bSZhangjin Wu } 495362aecb2SWilly Tarreau 496362aecb2SWilly Tarreau #define EXPECT_STRZR(cond, expr) \ 497b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_strzr(expr, llen); } while (0) 498362aecb2SWilly Tarreau 499362aecb2SWilly Tarreau static int expect_strzr(const char *expr, int llen) 500362aecb2SWilly Tarreau { 501362aecb2SWilly Tarreau int ret = 0; 502362aecb2SWilly Tarreau 503362aecb2SWilly Tarreau llen += printf(" = <%s> ", expr); 504362aecb2SWilly Tarreau if (expr) { 505362aecb2SWilly Tarreau ret = 1; 506b184a261SThomas Weißschuh result(llen, FAIL); 507362aecb2SWilly Tarreau } else { 508b184a261SThomas Weißschuh result(llen, OK); 509362aecb2SWilly Tarreau } 510362aecb2SWilly Tarreau return ret; 511362aecb2SWilly Tarreau } 512362aecb2SWilly Tarreau 513362aecb2SWilly Tarreau 514362aecb2SWilly Tarreau #define EXPECT_STRNZ(cond, expr) \ 515b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_strnz(expr, llen); } while (0) 516362aecb2SWilly Tarreau 517362aecb2SWilly Tarreau static int expect_strnz(const char *expr, int llen) 518362aecb2SWilly Tarreau { 519362aecb2SWilly Tarreau int ret = 0; 520362aecb2SWilly Tarreau 521362aecb2SWilly Tarreau llen += printf(" = <%s> ", expr); 522362aecb2SWilly Tarreau if (!expr) { 523362aecb2SWilly Tarreau ret = 1; 524b184a261SThomas Weißschuh result(llen, FAIL); 525362aecb2SWilly Tarreau } else { 526b184a261SThomas Weißschuh result(llen, OK); 527362aecb2SWilly Tarreau } 528362aecb2SWilly Tarreau return ret; 529362aecb2SWilly Tarreau } 530362aecb2SWilly Tarreau 531362aecb2SWilly Tarreau 532362aecb2SWilly Tarreau #define EXPECT_STREQ(cond, expr, cmp) \ 533b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_streq(expr, llen, cmp); } while (0) 534362aecb2SWilly Tarreau 535362aecb2SWilly Tarreau static int expect_streq(const char *expr, int llen, const char *cmp) 536362aecb2SWilly Tarreau { 537362aecb2SWilly Tarreau int ret = 0; 538362aecb2SWilly Tarreau 539362aecb2SWilly Tarreau llen += printf(" = <%s> ", expr); 540362aecb2SWilly Tarreau if (strcmp(expr, cmp) != 0) { 541362aecb2SWilly Tarreau ret = 1; 542b184a261SThomas Weißschuh result(llen, FAIL); 543362aecb2SWilly Tarreau } else { 544b184a261SThomas Weißschuh result(llen, OK); 545362aecb2SWilly Tarreau } 546362aecb2SWilly Tarreau return ret; 547362aecb2SWilly Tarreau } 548362aecb2SWilly Tarreau 549362aecb2SWilly Tarreau 550362aecb2SWilly Tarreau #define EXPECT_STRNE(cond, expr, cmp) \ 551b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_strne(expr, llen, cmp); } while (0) 552362aecb2SWilly Tarreau 553362aecb2SWilly Tarreau static int expect_strne(const char *expr, int llen, const char *cmp) 554362aecb2SWilly Tarreau { 555362aecb2SWilly Tarreau int ret = 0; 556362aecb2SWilly Tarreau 557362aecb2SWilly Tarreau llen += printf(" = <%s> ", expr); 558362aecb2SWilly Tarreau if (strcmp(expr, cmp) == 0) { 559362aecb2SWilly Tarreau ret = 1; 560b184a261SThomas Weißschuh result(llen, FAIL); 561362aecb2SWilly Tarreau } else { 562b184a261SThomas Weißschuh result(llen, OK); 563362aecb2SWilly Tarreau } 564362aecb2SWilly Tarreau return ret; 565362aecb2SWilly Tarreau } 566362aecb2SWilly Tarreau 56723da7bc9SWilly Tarreau 568362aecb2SWilly Tarreau /* declare tests based on line numbers. There must be exactly one test per line. */ 569362aecb2SWilly Tarreau #define CASE_TEST(name) \ 570362aecb2SWilly Tarreau case __LINE__: llen += printf("%d %s", test, #name); 571362aecb2SWilly Tarreau 57248967b73SZhangjin Wu int run_startup(int min, int max) 57348967b73SZhangjin Wu { 57448967b73SZhangjin Wu int test; 57548967b73SZhangjin Wu int ret = 0; 57648967b73SZhangjin Wu /* kernel at least passes HOME and TERM, shell passes more */ 57748967b73SZhangjin Wu int env_total = 2; 57848967b73SZhangjin Wu /* checking NULL for argv/argv0, environ and _auxv is not enough, let's compare with sbrk(0) or &end */ 57948967b73SZhangjin Wu extern char end; 58048967b73SZhangjin Wu char *brk = sbrk(0) != (void *)-1 ? sbrk(0) : &end; 58148967b73SZhangjin Wu /* differ from nolibc, both glibc and musl have no global _auxv */ 58248967b73SZhangjin Wu const unsigned long *test_auxv = (void *)-1; 58348967b73SZhangjin Wu #ifdef NOLIBC 58448967b73SZhangjin Wu test_auxv = _auxv; 58548967b73SZhangjin Wu #endif 58648967b73SZhangjin Wu 58748967b73SZhangjin Wu for (test = min; test >= 0 && test <= max; test++) { 58848967b73SZhangjin Wu int llen = 0; /* line length */ 58948967b73SZhangjin Wu 59048967b73SZhangjin Wu /* avoid leaving empty lines below, this will insert holes into 59148967b73SZhangjin Wu * test numbers. 59248967b73SZhangjin Wu */ 59348967b73SZhangjin Wu switch (test + __LINE__ + 1) { 59448967b73SZhangjin Wu CASE_TEST(argc); EXPECT_GE(1, test_argc, 1); break; 59548967b73SZhangjin Wu CASE_TEST(argv_addr); EXPECT_PTRGT(1, test_argv, brk); break; 59648967b73SZhangjin Wu CASE_TEST(argv_environ); EXPECT_PTRLT(1, test_argv, environ); break; 59748967b73SZhangjin Wu CASE_TEST(argv_total); EXPECT_EQ(1, environ - test_argv - 1, test_argc ?: 1); break; 59848967b73SZhangjin Wu CASE_TEST(argv0_addr); EXPECT_PTRGT(1, argv0, brk); break; 59948967b73SZhangjin Wu CASE_TEST(argv0_str); EXPECT_STRNZ(1, argv0 > brk ? argv0 : NULL); break; 60048967b73SZhangjin Wu CASE_TEST(argv0_len); EXPECT_GE(1, argv0 > brk ? strlen(argv0) : 0, 1); break; 60148967b73SZhangjin Wu CASE_TEST(environ_addr); EXPECT_PTRGT(1, environ, brk); break; 60248967b73SZhangjin Wu CASE_TEST(environ_envp); EXPECT_PTREQ(1, environ, test_envp); break; 60348967b73SZhangjin Wu CASE_TEST(environ_auxv); EXPECT_PTRLT(test_auxv != (void *)-1, environ, test_auxv); break; 60448967b73SZhangjin Wu CASE_TEST(environ_total); EXPECT_GE(test_auxv != (void *)-1, (void *)test_auxv - (void *)environ - 1, env_total); break; 60548967b73SZhangjin Wu CASE_TEST(environ_HOME); EXPECT_PTRNZ(1, getenv("HOME")); break; 60648967b73SZhangjin Wu CASE_TEST(auxv_addr); EXPECT_PTRGT(test_auxv != (void *)-1, test_auxv, brk); break; 60748967b73SZhangjin Wu CASE_TEST(auxv_AT_UID); EXPECT_EQ(1, getauxval(AT_UID), getuid()); break; 60848967b73SZhangjin Wu CASE_TEST(auxv_AT_PAGESZ); EXPECT_GE(1, getauxval(AT_PAGESZ), 4096); break; 60948967b73SZhangjin Wu case __LINE__: 61048967b73SZhangjin Wu return ret; /* must be last */ 61148967b73SZhangjin Wu /* note: do not set any defaults so as to permit holes above */ 61248967b73SZhangjin Wu } 61348967b73SZhangjin Wu } 61448967b73SZhangjin Wu return ret; 61548967b73SZhangjin Wu } 61648967b73SZhangjin Wu 617362aecb2SWilly Tarreau 618b4844fa0SWilly Tarreau /* used by some syscall tests below */ 619b4844fa0SWilly Tarreau int test_getdents64(const char *dir) 620b4844fa0SWilly Tarreau { 621b4844fa0SWilly Tarreau char buffer[4096]; 622b4844fa0SWilly Tarreau int fd, ret; 623b4844fa0SWilly Tarreau int err; 624b4844fa0SWilly Tarreau 625b4844fa0SWilly Tarreau ret = fd = open(dir, O_RDONLY | O_DIRECTORY, 0); 626b4844fa0SWilly Tarreau if (ret < 0) 627b4844fa0SWilly Tarreau return ret; 628b4844fa0SWilly Tarreau 629b4844fa0SWilly Tarreau ret = getdents64(fd, (void *)buffer, sizeof(buffer)); 630b4844fa0SWilly Tarreau err = errno; 631b4844fa0SWilly Tarreau close(fd); 632b4844fa0SWilly Tarreau 633b4844fa0SWilly Tarreau errno = err; 634b4844fa0SWilly Tarreau return ret; 635b4844fa0SWilly Tarreau } 636b4844fa0SWilly Tarreau 637a290296aSAmmar Faizi static int test_getpagesize(void) 638a290296aSAmmar Faizi { 639*64077502SThomas Weißschuh int x = getpagesize(); 640a290296aSAmmar Faizi int c; 641a290296aSAmmar Faizi 642a290296aSAmmar Faizi if (x < 0) 643a290296aSAmmar Faizi return x; 644a290296aSAmmar Faizi 645a290296aSAmmar Faizi #if defined(__x86_64__) || defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) 646a290296aSAmmar Faizi /* 647a290296aSAmmar Faizi * x86 family is always 4K page. 648a290296aSAmmar Faizi */ 649a290296aSAmmar Faizi c = (x == 4096); 650a290296aSAmmar Faizi #elif defined(__aarch64__) 651a290296aSAmmar Faizi /* 652a290296aSAmmar Faizi * Linux aarch64 supports three values of page size: 4K, 16K, and 64K 653a290296aSAmmar Faizi * which are selected at kernel compilation time. 654a290296aSAmmar Faizi */ 655a290296aSAmmar Faizi c = (x == 4096 || x == (16 * 1024) || x == (64 * 1024)); 656a290296aSAmmar Faizi #else 657a290296aSAmmar Faizi /* 658a290296aSAmmar Faizi * Assuming other architectures must have at least 4K page. 659a290296aSAmmar Faizi */ 660a290296aSAmmar Faizi c = (x >= 4096); 661a290296aSAmmar Faizi #endif 662a290296aSAmmar Faizi 663a290296aSAmmar Faizi return !c; 664a290296aSAmmar Faizi } 665a290296aSAmmar Faizi 6663ad09d72SThomas Weißschuh static int test_fork(void) 6673ad09d72SThomas Weißschuh { 6683ad09d72SThomas Weißschuh int status; 669ed495f09SZhangjin Wu pid_t pid; 670ed495f09SZhangjin Wu 671ed495f09SZhangjin Wu /* flush the printf buffer to avoid child flush it */ 672ed495f09SZhangjin Wu fflush(stdout); 673ed495f09SZhangjin Wu fflush(stderr); 674ed495f09SZhangjin Wu 675ed495f09SZhangjin Wu pid = fork(); 6763ad09d72SThomas Weißschuh 6773ad09d72SThomas Weißschuh switch (pid) { 6783ad09d72SThomas Weißschuh case -1: 6793ad09d72SThomas Weißschuh return 1; 6803ad09d72SThomas Weißschuh 6813ad09d72SThomas Weißschuh case 0: 6823ad09d72SThomas Weißschuh exit(123); 6833ad09d72SThomas Weißschuh 6843ad09d72SThomas Weißschuh default: 6853ad09d72SThomas Weißschuh pid = waitpid(pid, &status, 0); 6863ad09d72SThomas Weißschuh 6873ad09d72SThomas Weißschuh return pid == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 123; 6883ad09d72SThomas Weißschuh } 6893ad09d72SThomas Weißschuh } 6903ad09d72SThomas Weißschuh 69187b9fa66SThomas Weißschuh static int test_stat_timestamps(void) 69287b9fa66SThomas Weißschuh { 69387b9fa66SThomas Weißschuh struct stat st; 69487b9fa66SThomas Weißschuh 69587b9fa66SThomas Weißschuh if (sizeof(st.st_atim.tv_sec) != sizeof(st.st_atime)) 69687b9fa66SThomas Weißschuh return 1; 69787b9fa66SThomas Weißschuh 698f576d3c0SZhangjin Wu if (stat("/proc/self/", &st) && stat(argv0, &st) && stat("/", &st)) 69987b9fa66SThomas Weißschuh return 1; 70087b9fa66SThomas Weißschuh 70187b9fa66SThomas Weißschuh if (st.st_atim.tv_sec != st.st_atime || st.st_atim.tv_nsec > 1000000000) 70287b9fa66SThomas Weißschuh return 1; 70387b9fa66SThomas Weißschuh 70487b9fa66SThomas Weißschuh if (st.st_mtim.tv_sec != st.st_mtime || st.st_mtim.tv_nsec > 1000000000) 70587b9fa66SThomas Weißschuh return 1; 70687b9fa66SThomas Weißschuh 70787b9fa66SThomas Weißschuh if (st.st_ctim.tv_sec != st.st_ctime || st.st_ctim.tv_nsec > 1000000000) 70887b9fa66SThomas Weißschuh return 1; 70987b9fa66SThomas Weißschuh 71087b9fa66SThomas Weißschuh return 0; 71187b9fa66SThomas Weißschuh } 71287b9fa66SThomas Weißschuh 713fcdbf5ddSZhangjin Wu int test_mmap_munmap(void) 714fcdbf5ddSZhangjin Wu { 715fcdbf5ddSZhangjin Wu int ret, fd, i; 716fcdbf5ddSZhangjin Wu void *mem; 717fcdbf5ddSZhangjin Wu size_t page_size, file_size, length; 718fcdbf5ddSZhangjin Wu off_t offset, pa_offset; 719fcdbf5ddSZhangjin Wu struct stat stat_buf; 720fcdbf5ddSZhangjin Wu const char * const files[] = { 721fcdbf5ddSZhangjin Wu "/dev/zero", 722fcdbf5ddSZhangjin Wu "/proc/1/exe", "/proc/self/exe", 723fcdbf5ddSZhangjin Wu argv0, 724fcdbf5ddSZhangjin Wu NULL 725fcdbf5ddSZhangjin Wu }; 726fcdbf5ddSZhangjin Wu 727fcdbf5ddSZhangjin Wu page_size = getpagesize(); 728fcdbf5ddSZhangjin Wu if (page_size < 0) 729e7d0129dSZhangjin Wu return 1; 730fcdbf5ddSZhangjin Wu 731fcdbf5ddSZhangjin Wu /* find a right file to mmap, existed and accessible */ 732fcdbf5ddSZhangjin Wu for (i = 0; files[i] != NULL; i++) { 733fcdbf5ddSZhangjin Wu ret = fd = open(files[i], O_RDONLY); 734fcdbf5ddSZhangjin Wu if (ret == -1) 735fcdbf5ddSZhangjin Wu continue; 736fcdbf5ddSZhangjin Wu else 737fcdbf5ddSZhangjin Wu break; 738fcdbf5ddSZhangjin Wu } 739fcdbf5ddSZhangjin Wu if (ret == -1) 740e7d0129dSZhangjin Wu return 1; 741fcdbf5ddSZhangjin Wu 742fcdbf5ddSZhangjin Wu ret = stat(files[i], &stat_buf); 743fcdbf5ddSZhangjin Wu if (ret == -1) 744fcdbf5ddSZhangjin Wu goto end; 745fcdbf5ddSZhangjin Wu 746fcdbf5ddSZhangjin Wu /* file size of the special /dev/zero is 0, let's assign one manually */ 747fcdbf5ddSZhangjin Wu if (i == 0) 748fcdbf5ddSZhangjin Wu file_size = 3*page_size; 749fcdbf5ddSZhangjin Wu else 750fcdbf5ddSZhangjin Wu file_size = stat_buf.st_size; 751fcdbf5ddSZhangjin Wu 752fcdbf5ddSZhangjin Wu offset = file_size - 1; 753fcdbf5ddSZhangjin Wu if (offset < 0) 754fcdbf5ddSZhangjin Wu offset = 0; 755fcdbf5ddSZhangjin Wu length = file_size - offset; 756fcdbf5ddSZhangjin Wu pa_offset = offset & ~(page_size - 1); 757fcdbf5ddSZhangjin Wu 758fcdbf5ddSZhangjin Wu mem = mmap(NULL, length + offset - pa_offset, PROT_READ, MAP_SHARED, fd, pa_offset); 759fcdbf5ddSZhangjin Wu if (mem == MAP_FAILED) { 760e7d0129dSZhangjin Wu ret = 1; 761fcdbf5ddSZhangjin Wu goto end; 762fcdbf5ddSZhangjin Wu } 763fcdbf5ddSZhangjin Wu 764fcdbf5ddSZhangjin Wu ret = munmap(mem, length + offset - pa_offset); 765fcdbf5ddSZhangjin Wu 766fcdbf5ddSZhangjin Wu end: 767fcdbf5ddSZhangjin Wu close(fd); 768e7d0129dSZhangjin Wu return !!ret; 769fcdbf5ddSZhangjin Wu } 770fcdbf5ddSZhangjin Wu 7715c01259bSYuan Tan static int test_pipe(void) 7725c01259bSYuan Tan { 7735c01259bSYuan Tan const char *const msg = "hello, nolibc"; 7745c01259bSYuan Tan int pipefd[2]; 7755c01259bSYuan Tan char buf[32]; 7765c01259bSYuan Tan size_t len; 7775c01259bSYuan Tan 7785c01259bSYuan Tan if (pipe(pipefd) == -1) 7795c01259bSYuan Tan return 1; 7805c01259bSYuan Tan 7815c01259bSYuan Tan write(pipefd[1], msg, strlen(msg)); 7825c01259bSYuan Tan close(pipefd[1]); 7835c01259bSYuan Tan len = read(pipefd[0], buf, sizeof(buf)); 7845c01259bSYuan Tan close(pipefd[0]); 7855c01259bSYuan Tan 7865c01259bSYuan Tan if (len != strlen(msg)) 7875c01259bSYuan Tan return 1; 7885c01259bSYuan Tan 7895c01259bSYuan Tan return !!memcmp(buf, msg, len); 7905c01259bSYuan Tan } 7915c01259bSYuan Tan 792fcdbf5ddSZhangjin Wu 793b4844fa0SWilly Tarreau /* Run syscall tests between IDs <min> and <max>. 794b4844fa0SWilly Tarreau * Return 0 on success, non-zero on failure. 795b4844fa0SWilly Tarreau */ 796b4844fa0SWilly Tarreau int run_syscall(int min, int max) 797b4844fa0SWilly Tarreau { 798957bfa31SZhangjin Wu struct timeval tv; 799957bfa31SZhangjin Wu struct timezone tz; 800b4844fa0SWilly Tarreau struct stat stat_buf; 8013e2d337bSWilly Tarreau int euid0; 8027172f1c6SWilly Tarreau int proc; 803b4844fa0SWilly Tarreau int test; 804b4844fa0SWilly Tarreau int tmp; 805b4844fa0SWilly Tarreau int ret = 0; 806b4844fa0SWilly Tarreau void *p1, *p2; 80779b4f68eSZhangjin Wu int has_gettid = 1; 808b4844fa0SWilly Tarreau 8097172f1c6SWilly Tarreau /* <proc> indicates whether or not /proc is mounted */ 8107172f1c6SWilly Tarreau proc = stat("/proc", &stat_buf) == 0; 8117172f1c6SWilly Tarreau 8123e2d337bSWilly Tarreau /* this will be used to skip certain tests that can't be run unprivileged */ 8133e2d337bSWilly Tarreau euid0 = geteuid() == 0; 8143e2d337bSWilly Tarreau 81579b4f68eSZhangjin Wu /* from 2.30, glibc provides gettid() */ 81679b4f68eSZhangjin Wu #if defined(__GLIBC_MINOR__) && defined(__GLIBC__) 81779b4f68eSZhangjin Wu has_gettid = __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 30); 81879b4f68eSZhangjin Wu #endif 81979b4f68eSZhangjin Wu 820b4844fa0SWilly Tarreau for (test = min; test >= 0 && test <= max; test++) { 821fddc8f81SThomas Weißschuh int llen = 0; /* line length */ 822b4844fa0SWilly Tarreau 823b4844fa0SWilly Tarreau /* avoid leaving empty lines below, this will insert holes into 824b4844fa0SWilly Tarreau * test numbers. 825b4844fa0SWilly Tarreau */ 826b4844fa0SWilly Tarreau switch (test + __LINE__ + 1) { 827b4844fa0SWilly Tarreau CASE_TEST(getpid); EXPECT_SYSNE(1, getpid(), -1); break; 828b4844fa0SWilly Tarreau CASE_TEST(getppid); EXPECT_SYSNE(1, getppid(), -1); break; 82979b4f68eSZhangjin Wu CASE_TEST(gettid); EXPECT_SYSNE(has_gettid, gettid(), -1); break; 830b4844fa0SWilly Tarreau CASE_TEST(getpgid_self); EXPECT_SYSNE(1, getpgid(0), -1); break; 831b4844fa0SWilly Tarreau CASE_TEST(getpgid_bad); EXPECT_SYSER(1, getpgid(-1), -1, ESRCH); break; 832b4844fa0SWilly Tarreau CASE_TEST(kill_0); EXPECT_SYSZR(1, kill(getpid(), 0)); break; 833b4844fa0SWilly Tarreau CASE_TEST(kill_CONT); EXPECT_SYSZR(1, kill(getpid(), 0)); break; 834b4844fa0SWilly Tarreau CASE_TEST(kill_BADPID); EXPECT_SYSER(1, kill(INT_MAX, 0), -1, ESRCH); break; 835f193ecbfSZhangjin Wu CASE_TEST(sbrk_0); EXPECT_PTRNE(1, sbrk(0), (void *)-1); break; 836b4844fa0SWilly Tarreau CASE_TEST(sbrk); if ((p1 = p2 = sbrk(4096)) != (void *)-1) p2 = sbrk(-4096); EXPECT_SYSZR(1, (p2 == (void *)-1) || p2 == p1); break; 837b4844fa0SWilly Tarreau CASE_TEST(brk); EXPECT_SYSZR(1, brk(sbrk(0))); break; 83838fc0a35SZhangjin Wu CASE_TEST(chdir_root); EXPECT_SYSZR(1, chdir("/")); chdir(getenv("PWD")); break; 839b4844fa0SWilly Tarreau CASE_TEST(chdir_dot); EXPECT_SYSZR(1, chdir(".")); break; 840b4844fa0SWilly Tarreau CASE_TEST(chdir_blah); EXPECT_SYSER(1, chdir("/blah"), -1, ENOENT); break; 841148e9718SZhangjin Wu CASE_TEST(chmod_argv0); EXPECT_SYSZR(1, chmod(argv0, 0555)); break; 8427172f1c6SWilly Tarreau CASE_TEST(chmod_net); EXPECT_SYSZR(proc, chmod("/proc/self/net", 0555)); break; 8437172f1c6SWilly Tarreau CASE_TEST(chmod_self); EXPECT_SYSER(proc, chmod("/proc/self", 0555), -1, EPERM); break; 8447172f1c6SWilly Tarreau CASE_TEST(chown_self); EXPECT_SYSER(proc, chown("/proc/self", 0, 0), -1, EPERM); break; 8453e2d337bSWilly Tarreau CASE_TEST(chroot_root); EXPECT_SYSZR(euid0, chroot("/")); break; 846b4844fa0SWilly Tarreau CASE_TEST(chroot_blah); EXPECT_SYSER(1, chroot("/proc/self/blah"), -1, ENOENT); break; 847135b622eSZhangjin Wu CASE_TEST(chroot_exe); EXPECT_SYSER(1, chroot(argv0), -1, ENOTDIR); break; 848b4844fa0SWilly Tarreau CASE_TEST(close_m1); EXPECT_SYSER(1, close(-1), -1, EBADF); break; 849b4844fa0SWilly Tarreau CASE_TEST(close_dup); EXPECT_SYSZR(1, close(dup(0))); break; 850b4844fa0SWilly Tarreau CASE_TEST(dup_0); tmp = dup(0); EXPECT_SYSNE(1, tmp, -1); close(tmp); break; 851b4844fa0SWilly Tarreau CASE_TEST(dup_m1); tmp = dup(-1); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break; 852b4844fa0SWilly Tarreau CASE_TEST(dup2_0); tmp = dup2(0, 100); EXPECT_SYSNE(1, tmp, -1); close(tmp); break; 853b4844fa0SWilly Tarreau CASE_TEST(dup2_m1); tmp = dup2(-1, 100); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break; 854b4844fa0SWilly Tarreau CASE_TEST(dup3_0); tmp = dup3(0, 100, 0); EXPECT_SYSNE(1, tmp, -1); close(tmp); break; 855b4844fa0SWilly Tarreau CASE_TEST(dup3_m1); tmp = dup3(-1, 100, 0); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break; 856b4844fa0SWilly Tarreau CASE_TEST(execve_root); EXPECT_SYSER(1, execve("/", (char*[]){ [0] = "/", [1] = NULL }, NULL), -1, EACCES); break; 8573ad09d72SThomas Weißschuh CASE_TEST(fork); EXPECT_SYSZR(1, test_fork()); break; 858b4844fa0SWilly Tarreau CASE_TEST(getdents64_root); EXPECT_SYSNE(1, test_getdents64("/"), -1); break; 859b4844fa0SWilly Tarreau CASE_TEST(getdents64_null); EXPECT_SYSER(1, test_getdents64("/dev/null"), -1, ENOTDIR); break; 860957bfa31SZhangjin Wu CASE_TEST(gettimeofday_tv); EXPECT_SYSZR(1, gettimeofday(&tv, NULL)); break; 861957bfa31SZhangjin Wu CASE_TEST(gettimeofday_tv_tz);EXPECT_SYSZR(1, gettimeofday(&tv, &tz)); break; 862a290296aSAmmar Faizi CASE_TEST(getpagesize); EXPECT_SYSZR(1, test_getpagesize()); break; 863b4844fa0SWilly Tarreau CASE_TEST(ioctl_tiocinq); EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); break; 864b4844fa0SWilly Tarreau CASE_TEST(ioctl_tiocinq); EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); break; 865b4844fa0SWilly Tarreau CASE_TEST(link_root1); EXPECT_SYSER(1, link("/", "/"), -1, EEXIST); break; 866b4844fa0SWilly Tarreau CASE_TEST(link_blah); EXPECT_SYSER(1, link("/proc/self/blah", "/blah"), -1, ENOENT); break; 8673e2d337bSWilly Tarreau CASE_TEST(link_dir); EXPECT_SYSER(euid0, link("/", "/blah"), -1, EPERM); break; 868f7a419e3SZhangjin Wu CASE_TEST(link_cross); EXPECT_SYSER(proc, link("/proc/self/cmdline", "/blah"), -1, EXDEV); break; 869b4844fa0SWilly Tarreau CASE_TEST(lseek_m1); EXPECT_SYSER(1, lseek(-1, 0, SEEK_SET), -1, EBADF); break; 870b4844fa0SWilly Tarreau CASE_TEST(lseek_0); EXPECT_SYSER(1, lseek(0, 0, SEEK_SET), -1, ESPIPE); break; 871b4844fa0SWilly Tarreau CASE_TEST(mkdir_root); EXPECT_SYSER(1, mkdir("/", 0755), -1, EEXIST); break; 872d4a3b2b9SZhangjin Wu CASE_TEST(mmap_bad); EXPECT_PTRER(1, mmap(NULL, 0, PROT_READ, MAP_PRIVATE, 0, 0), MAP_FAILED, EINVAL); break; 873ba3d0892SZhangjin Wu CASE_TEST(munmap_bad); EXPECT_SYSER(1, munmap((void *)1, 0), -1, EINVAL); break; 874fcdbf5ddSZhangjin Wu CASE_TEST(mmap_munmap_good); EXPECT_SYSZR(1, test_mmap_munmap()); break; 875b4844fa0SWilly Tarreau CASE_TEST(open_tty); EXPECT_SYSNE(1, tmp = open("/dev/null", 0), -1); if (tmp != -1) close(tmp); break; 876b4844fa0SWilly Tarreau CASE_TEST(open_blah); EXPECT_SYSER(1, tmp = open("/proc/self/blah", 0), -1, ENOENT); if (tmp != -1) close(tmp); break; 8775c01259bSYuan Tan CASE_TEST(pipe); EXPECT_SYSZR(1, test_pipe()); break; 878b4844fa0SWilly Tarreau CASE_TEST(poll_null); EXPECT_SYSZR(1, poll(NULL, 0, 0)); break; 879b4844fa0SWilly Tarreau CASE_TEST(poll_stdout); EXPECT_SYSNE(1, ({ struct pollfd fds = { 1, POLLOUT, 0}; poll(&fds, 1, 0); }), -1); break; 880b4844fa0SWilly Tarreau CASE_TEST(poll_fault); EXPECT_SYSER(1, poll((void *)1, 1, 0), -1, EFAULT); break; 881208aa9d9SThomas Weißschuh CASE_TEST(prctl); EXPECT_SYSER(1, prctl(PR_SET_NAME, (unsigned long)NULL, 0, 0, 0), -1, EFAULT); break; 882b4844fa0SWilly Tarreau CASE_TEST(read_badf); EXPECT_SYSER(1, read(-1, &tmp, 1), -1, EBADF); break; 8834e14e844SZhangjin Wu CASE_TEST(rmdir_blah); EXPECT_SYSER(1, rmdir("/blah"), -1, ENOENT); break; 884b4844fa0SWilly Tarreau CASE_TEST(sched_yield); EXPECT_SYSZR(1, sched_yield()); break; 885b4844fa0SWilly Tarreau CASE_TEST(select_null); EXPECT_SYSZR(1, ({ struct timeval tv = { 0 }; select(0, NULL, NULL, NULL, &tv); })); break; 886b4844fa0SWilly Tarreau 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; 887b4844fa0SWilly Tarreau CASE_TEST(select_fault); EXPECT_SYSER(1, select(1, (void *)1, NULL, NULL, 0), -1, EFAULT); break; 888b4844fa0SWilly Tarreau CASE_TEST(stat_blah); EXPECT_SYSER(1, stat("/proc/self/blah", &stat_buf), -1, ENOENT); break; 88946cf630cSZhangjin Wu CASE_TEST(stat_fault); EXPECT_SYSER(1, stat((void *)1, &stat_buf), -1, EFAULT); break; 89087b9fa66SThomas Weißschuh CASE_TEST(stat_timestamps); EXPECT_SYSZR(1, test_stat_timestamps()); break; 891b4844fa0SWilly Tarreau CASE_TEST(symlink_root); EXPECT_SYSER(1, symlink("/", "/"), -1, EEXIST); break; 892b4844fa0SWilly Tarreau CASE_TEST(unlink_root); EXPECT_SYSER(1, unlink("/"), -1, EISDIR); break; 893b4844fa0SWilly Tarreau CASE_TEST(unlink_blah); EXPECT_SYSER(1, unlink("/proc/self/blah"), -1, ENOENT); break; 894b4844fa0SWilly Tarreau CASE_TEST(wait_child); EXPECT_SYSER(1, wait(&tmp), -1, ECHILD); break; 895b4844fa0SWilly Tarreau CASE_TEST(waitpid_min); EXPECT_SYSER(1, waitpid(INT_MIN, &tmp, WNOHANG), -1, ESRCH); break; 896b4844fa0SWilly Tarreau CASE_TEST(waitpid_child); EXPECT_SYSER(1, waitpid(getpid(), &tmp, WNOHANG), -1, ECHILD); break; 897b4844fa0SWilly Tarreau CASE_TEST(write_badf); EXPECT_SYSER(1, write(-1, &tmp, 1), -1, EBADF); break; 898b4844fa0SWilly Tarreau CASE_TEST(write_zero); EXPECT_SYSZR(1, write(1, &tmp, 0)); break; 89953fcfafaSThomas Weißschuh CASE_TEST(syscall_noargs); EXPECT_SYSEQ(1, syscall(__NR_getpid), getpid()); break; 900ec8e1b73SZhangjin Wu CASE_TEST(syscall_args); EXPECT_SYSER(1, syscall(__NR_statx, 0, NULL, 0, 0, NULL), -1, EFAULT); break; 901b4844fa0SWilly Tarreau case __LINE__: 902b4844fa0SWilly Tarreau return ret; /* must be last */ 903b4844fa0SWilly Tarreau /* note: do not set any defaults so as to permit holes above */ 904b4844fa0SWilly Tarreau } 905b4844fa0SWilly Tarreau } 906b4844fa0SWilly Tarreau return ret; 907b4844fa0SWilly Tarreau } 908b4844fa0SWilly Tarreau 90995bc9894SWilly Tarreau int run_stdlib(int min, int max) 91095bc9894SWilly Tarreau { 91195bc9894SWilly Tarreau int test; 91295bc9894SWilly Tarreau int tmp; 91395bc9894SWilly Tarreau int ret = 0; 91495bc9894SWilly Tarreau void *p1, *p2; 91595bc9894SWilly Tarreau 91695bc9894SWilly Tarreau for (test = min; test >= 0 && test <= max; test++) { 917fddc8f81SThomas Weißschuh int llen = 0; /* line length */ 91895bc9894SWilly Tarreau 91995bc9894SWilly Tarreau /* avoid leaving empty lines below, this will insert holes into 92095bc9894SWilly Tarreau * test numbers. 92195bc9894SWilly Tarreau */ 92295bc9894SWilly Tarreau switch (test + __LINE__ + 1) { 92395bc9894SWilly Tarreau CASE_TEST(getenv_TERM); EXPECT_STRNZ(1, getenv("TERM")); break; 92495bc9894SWilly Tarreau CASE_TEST(getenv_blah); EXPECT_STRZR(1, getenv("blah")); break; 92595bc9894SWilly Tarreau CASE_TEST(setcmp_blah_blah); EXPECT_EQ(1, strcmp("blah", "blah"), 0); break; 92695bc9894SWilly Tarreau CASE_TEST(setcmp_blah_blah2); EXPECT_NE(1, strcmp("blah", "blah2"), 0); break; 92795bc9894SWilly Tarreau CASE_TEST(setncmp_blah_blah); EXPECT_EQ(1, strncmp("blah", "blah", 10), 0); break; 92895bc9894SWilly Tarreau CASE_TEST(setncmp_blah_blah4); EXPECT_EQ(1, strncmp("blah", "blah4", 4), 0); break; 92995bc9894SWilly Tarreau CASE_TEST(setncmp_blah_blah5); EXPECT_NE(1, strncmp("blah", "blah5", 5), 0); break; 93095bc9894SWilly Tarreau CASE_TEST(setncmp_blah_blah6); EXPECT_NE(1, strncmp("blah", "blah6", 6), 0); break; 93195bc9894SWilly Tarreau CASE_TEST(strchr_foobar_o); EXPECT_STREQ(1, strchr("foobar", 'o'), "oobar"); break; 93295bc9894SWilly Tarreau CASE_TEST(strchr_foobar_z); EXPECT_STRZR(1, strchr("foobar", 'z')); break; 93395bc9894SWilly Tarreau CASE_TEST(strrchr_foobar_o); EXPECT_STREQ(1, strrchr("foobar", 'o'), "obar"); break; 93495bc9894SWilly Tarreau CASE_TEST(strrchr_foobar_z); EXPECT_STRZR(1, strrchr("foobar", 'z')); break; 935c80b5a0aSWilly Tarreau CASE_TEST(memcmp_20_20); EXPECT_EQ(1, memcmp("aaa\x20", "aaa\x20", 4), 0); break; 936c80b5a0aSWilly Tarreau CASE_TEST(memcmp_20_60); EXPECT_LT(1, memcmp("aaa\x20", "aaa\x60", 4), 0); break; 937c80b5a0aSWilly Tarreau CASE_TEST(memcmp_60_20); EXPECT_GT(1, memcmp("aaa\x60", "aaa\x20", 4), 0); break; 938c80b5a0aSWilly Tarreau CASE_TEST(memcmp_20_e0); EXPECT_LT(1, memcmp("aaa\x20", "aaa\xe0", 4), 0); break; 939c80b5a0aSWilly Tarreau CASE_TEST(memcmp_e0_20); EXPECT_GT(1, memcmp("aaa\xe0", "aaa\x20", 4), 0); break; 940c80b5a0aSWilly Tarreau CASE_TEST(memcmp_80_e0); EXPECT_LT(1, memcmp("aaa\x80", "aaa\xe0", 4), 0); break; 941c80b5a0aSWilly Tarreau CASE_TEST(memcmp_e0_80); EXPECT_GT(1, memcmp("aaa\xe0", "aaa\x80", 4), 0); break; 942d1209597SVincent Dagonneau CASE_TEST(limit_int8_max); EXPECT_EQ(1, INT8_MAX, (int8_t) 0x7f); break; 943d1209597SVincent Dagonneau CASE_TEST(limit_int8_min); EXPECT_EQ(1, INT8_MIN, (int8_t) 0x80); break; 944d1209597SVincent Dagonneau CASE_TEST(limit_uint8_max); EXPECT_EQ(1, UINT8_MAX, (uint8_t) 0xff); break; 945d1209597SVincent Dagonneau CASE_TEST(limit_int16_max); EXPECT_EQ(1, INT16_MAX, (int16_t) 0x7fff); break; 946d1209597SVincent Dagonneau CASE_TEST(limit_int16_min); EXPECT_EQ(1, INT16_MIN, (int16_t) 0x8000); break; 947d1209597SVincent Dagonneau CASE_TEST(limit_uint16_max); EXPECT_EQ(1, UINT16_MAX, (uint16_t) 0xffff); break; 948d1209597SVincent Dagonneau CASE_TEST(limit_int32_max); EXPECT_EQ(1, INT32_MAX, (int32_t) 0x7fffffff); break; 949d1209597SVincent Dagonneau CASE_TEST(limit_int32_min); EXPECT_EQ(1, INT32_MIN, (int32_t) 0x80000000); break; 950d1209597SVincent Dagonneau CASE_TEST(limit_uint32_max); EXPECT_EQ(1, UINT32_MAX, (uint32_t) 0xffffffff); break; 951d1209597SVincent Dagonneau CASE_TEST(limit_int64_max); EXPECT_EQ(1, INT64_MAX, (int64_t) 0x7fffffffffffffff); break; 952d1209597SVincent Dagonneau CASE_TEST(limit_int64_min); EXPECT_EQ(1, INT64_MIN, (int64_t) 0x8000000000000000); break; 953d1209597SVincent Dagonneau CASE_TEST(limit_uint64_max); EXPECT_EQ(1, UINT64_MAX, (uint64_t) 0xffffffffffffffff); break; 954d1209597SVincent Dagonneau CASE_TEST(limit_int_least8_max); EXPECT_EQ(1, INT_LEAST8_MAX, (int_least8_t) 0x7f); break; 955d1209597SVincent Dagonneau CASE_TEST(limit_int_least8_min); EXPECT_EQ(1, INT_LEAST8_MIN, (int_least8_t) 0x80); break; 956d1209597SVincent Dagonneau CASE_TEST(limit_uint_least8_max); EXPECT_EQ(1, UINT_LEAST8_MAX, (uint_least8_t) 0xff); break; 957d1209597SVincent Dagonneau CASE_TEST(limit_int_least16_max); EXPECT_EQ(1, INT_LEAST16_MAX, (int_least16_t) 0x7fff); break; 958d1209597SVincent Dagonneau CASE_TEST(limit_int_least16_min); EXPECT_EQ(1, INT_LEAST16_MIN, (int_least16_t) 0x8000); break; 959d1209597SVincent Dagonneau CASE_TEST(limit_uint_least16_max); EXPECT_EQ(1, UINT_LEAST16_MAX, (uint_least16_t) 0xffff); break; 960d1209597SVincent Dagonneau CASE_TEST(limit_int_least32_max); EXPECT_EQ(1, INT_LEAST32_MAX, (int_least32_t) 0x7fffffff); break; 961d1209597SVincent Dagonneau CASE_TEST(limit_int_least32_min); EXPECT_EQ(1, INT_LEAST32_MIN, (int_least32_t) 0x80000000); break; 962d1209597SVincent Dagonneau CASE_TEST(limit_uint_least32_max); EXPECT_EQ(1, UINT_LEAST32_MAX, (uint_least32_t) 0xffffffffU); break; 963d1209597SVincent Dagonneau CASE_TEST(limit_int_least64_min); EXPECT_EQ(1, INT_LEAST64_MIN, (int_least64_t) 0x8000000000000000LL); break; 964d1209597SVincent Dagonneau CASE_TEST(limit_int_least64_max); EXPECT_EQ(1, INT_LEAST64_MAX, (int_least64_t) 0x7fffffffffffffffLL); break; 965d1209597SVincent Dagonneau CASE_TEST(limit_uint_least64_max); EXPECT_EQ(1, UINT_LEAST64_MAX, (uint_least64_t) 0xffffffffffffffffULL); break; 966d1209597SVincent Dagonneau CASE_TEST(limit_int_fast8_max); EXPECT_EQ(1, INT_FAST8_MAX, (int_fast8_t) 0x7f); break; 967d1209597SVincent Dagonneau CASE_TEST(limit_int_fast8_min); EXPECT_EQ(1, INT_FAST8_MIN, (int_fast8_t) 0x80); break; 968d1209597SVincent Dagonneau CASE_TEST(limit_uint_fast8_max); EXPECT_EQ(1, UINT_FAST8_MAX, (uint_fast8_t) 0xff); break; 969989abf1cSZhangjin Wu CASE_TEST(limit_int_fast16_min); EXPECT_EQ(1, INT_FAST16_MIN, (int_fast16_t) SINT_MIN_OF_TYPE(int_fast16_t)); break; 970989abf1cSZhangjin Wu CASE_TEST(limit_int_fast16_max); EXPECT_EQ(1, INT_FAST16_MAX, (int_fast16_t) SINT_MAX_OF_TYPE(int_fast16_t)); break; 971d1209597SVincent Dagonneau CASE_TEST(limit_uint_fast16_max); EXPECT_EQ(1, UINT_FAST16_MAX, (uint_fast16_t) UINTPTR_MAX); break; 972989abf1cSZhangjin Wu CASE_TEST(limit_int_fast32_min); EXPECT_EQ(1, INT_FAST32_MIN, (int_fast32_t) SINT_MIN_OF_TYPE(int_fast32_t)); break; 973989abf1cSZhangjin Wu CASE_TEST(limit_int_fast32_max); EXPECT_EQ(1, INT_FAST32_MAX, (int_fast32_t) SINT_MAX_OF_TYPE(int_fast32_t)); break; 974d1209597SVincent Dagonneau CASE_TEST(limit_uint_fast32_max); EXPECT_EQ(1, UINT_FAST32_MAX, (uint_fast32_t) UINTPTR_MAX); break; 975f9bf5944SThomas Weißschuh CASE_TEST(limit_int_fast64_min); EXPECT_EQ(1, INT_FAST64_MIN, (int_fast64_t) INT64_MIN); break; 976f9bf5944SThomas Weißschuh CASE_TEST(limit_int_fast64_max); EXPECT_EQ(1, INT_FAST64_MAX, (int_fast64_t) INT64_MAX); break; 977f9bf5944SThomas Weißschuh CASE_TEST(limit_uint_fast64_max); EXPECT_EQ(1, UINT_FAST64_MAX, (uint_fast64_t) UINT64_MAX); break; 978ceb528feSThomas Weißschuh CASE_TEST(sizeof_long_sane); EXPECT_EQ(1, sizeof(long) == 8 || sizeof(long) == 4, 1); break; 979ceb528feSThomas Weißschuh CASE_TEST(limit_intptr_min_64); EXPECT_EQ(sizeof(long) == 8, INTPTR_MIN, (intptr_t) 0x8000000000000000LL); break; 980ceb528feSThomas Weißschuh CASE_TEST(limit_intptr_max_64); EXPECT_EQ(sizeof(long) == 8, INTPTR_MAX, (intptr_t) 0x7fffffffffffffffLL); break; 981ceb528feSThomas Weißschuh CASE_TEST(limit_uintptr_max_64); EXPECT_EQ(sizeof(long) == 8, UINTPTR_MAX, (uintptr_t) 0xffffffffffffffffULL); break; 982ceb528feSThomas Weißschuh CASE_TEST(limit_ptrdiff_min_64); EXPECT_EQ(sizeof(long) == 8, PTRDIFF_MIN, (ptrdiff_t) 0x8000000000000000LL); break; 983ceb528feSThomas Weißschuh CASE_TEST(limit_ptrdiff_max_64); EXPECT_EQ(sizeof(long) == 8, PTRDIFF_MAX, (ptrdiff_t) 0x7fffffffffffffffLL); break; 984ceb528feSThomas Weißschuh CASE_TEST(limit_size_max_64); EXPECT_EQ(sizeof(long) == 8, SIZE_MAX, (size_t) 0xffffffffffffffffULL); break; 985ceb528feSThomas Weißschuh CASE_TEST(limit_intptr_min_32); EXPECT_EQ(sizeof(long) == 4, INTPTR_MIN, (intptr_t) 0x80000000); break; 986ceb528feSThomas Weißschuh CASE_TEST(limit_intptr_max_32); EXPECT_EQ(sizeof(long) == 4, INTPTR_MAX, (intptr_t) 0x7fffffff); break; 987ceb528feSThomas Weißschuh CASE_TEST(limit_uintptr_max_32); EXPECT_EQ(sizeof(long) == 4, UINTPTR_MAX, (uintptr_t) 0xffffffffU); break; 988ceb528feSThomas Weißschuh CASE_TEST(limit_ptrdiff_min_32); EXPECT_EQ(sizeof(long) == 4, PTRDIFF_MIN, (ptrdiff_t) 0x80000000); break; 989ceb528feSThomas Weißschuh CASE_TEST(limit_ptrdiff_max_32); EXPECT_EQ(sizeof(long) == 4, PTRDIFF_MAX, (ptrdiff_t) 0x7fffffff); break; 990ceb528feSThomas Weißschuh CASE_TEST(limit_size_max_32); EXPECT_EQ(sizeof(long) == 4, SIZE_MAX, (size_t) 0xffffffffU); break; 99195bc9894SWilly Tarreau case __LINE__: 99295bc9894SWilly Tarreau return ret; /* must be last */ 99395bc9894SWilly Tarreau /* note: do not set any defaults so as to permit holes above */ 99495bc9894SWilly Tarreau } 99595bc9894SWilly Tarreau } 99695bc9894SWilly Tarreau return ret; 99795bc9894SWilly Tarreau } 99895bc9894SWilly Tarreau 99969f2cd9fSThomas Weißschuh #define EXPECT_VFPRINTF(c, expected, fmt, ...) \ 100069f2cd9fSThomas Weißschuh ret += expect_vfprintf(llen, c, expected, fmt, ##__VA_ARGS__) 100169f2cd9fSThomas Weißschuh 100269f2cd9fSThomas Weißschuh static int expect_vfprintf(int llen, size_t c, const char *expected, const char *fmt, ...) 100369f2cd9fSThomas Weißschuh { 100469f2cd9fSThomas Weißschuh int ret, fd, w, r; 100569f2cd9fSThomas Weißschuh char buf[100]; 100669f2cd9fSThomas Weißschuh FILE *memfile; 100769f2cd9fSThomas Weißschuh va_list args; 100869f2cd9fSThomas Weißschuh 10096861b1a3SZhangjin Wu fd = open("/tmp", O_TMPFILE | O_EXCL | O_RDWR, 0600); 101069f2cd9fSThomas Weißschuh if (fd == -1) { 1011b184a261SThomas Weißschuh result(llen, SKIPPED); 10126861b1a3SZhangjin Wu return 0; 101369f2cd9fSThomas Weißschuh } 101469f2cd9fSThomas Weißschuh 101569f2cd9fSThomas Weißschuh memfile = fdopen(fd, "w+"); 101669f2cd9fSThomas Weißschuh if (!memfile) { 1017b184a261SThomas Weißschuh result(llen, FAIL); 101869f2cd9fSThomas Weißschuh return 1; 101969f2cd9fSThomas Weißschuh } 102069f2cd9fSThomas Weißschuh 102169f2cd9fSThomas Weißschuh va_start(args, fmt); 102269f2cd9fSThomas Weißschuh w = vfprintf(memfile, fmt, args); 102369f2cd9fSThomas Weißschuh va_end(args); 102469f2cd9fSThomas Weißschuh 102569f2cd9fSThomas Weißschuh if (w != c) { 102669f2cd9fSThomas Weißschuh llen += printf(" written(%d) != %d", w, (int) c); 1027b184a261SThomas Weißschuh result(llen, FAIL); 102869f2cd9fSThomas Weißschuh return 1; 102969f2cd9fSThomas Weißschuh } 103069f2cd9fSThomas Weißschuh 103169f2cd9fSThomas Weißschuh fflush(memfile); 103269f2cd9fSThomas Weißschuh lseek(fd, 0, SEEK_SET); 103369f2cd9fSThomas Weißschuh 103469f2cd9fSThomas Weißschuh r = read(fd, buf, sizeof(buf) - 1); 103569f2cd9fSThomas Weißschuh buf[r] = '\0'; 103669f2cd9fSThomas Weißschuh 103769f2cd9fSThomas Weißschuh fclose(memfile); 103869f2cd9fSThomas Weißschuh 103969f2cd9fSThomas Weißschuh if (r != w) { 104069f2cd9fSThomas Weißschuh llen += printf(" written(%d) != read(%d)", w, r); 1041b184a261SThomas Weißschuh result(llen, FAIL); 104269f2cd9fSThomas Weißschuh return 1; 104369f2cd9fSThomas Weißschuh } 104469f2cd9fSThomas Weißschuh 104569f2cd9fSThomas Weißschuh llen += printf(" \"%s\" = \"%s\"", expected, buf); 104669f2cd9fSThomas Weißschuh ret = strncmp(expected, buf, c); 104769f2cd9fSThomas Weißschuh 1048b184a261SThomas Weißschuh result(llen, ret ? FAIL : OK); 104969f2cd9fSThomas Weißschuh return ret; 105069f2cd9fSThomas Weißschuh } 105169f2cd9fSThomas Weißschuh 105269f2cd9fSThomas Weißschuh static int run_vfprintf(int min, int max) 105369f2cd9fSThomas Weißschuh { 105469f2cd9fSThomas Weißschuh int test; 105569f2cd9fSThomas Weißschuh int tmp; 105669f2cd9fSThomas Weißschuh int ret = 0; 105769f2cd9fSThomas Weißschuh void *p1, *p2; 105869f2cd9fSThomas Weißschuh 105969f2cd9fSThomas Weißschuh for (test = min; test >= 0 && test <= max; test++) { 1060fddc8f81SThomas Weißschuh int llen = 0; /* line length */ 106169f2cd9fSThomas Weißschuh 106269f2cd9fSThomas Weißschuh /* avoid leaving empty lines below, this will insert holes into 106369f2cd9fSThomas Weißschuh * test numbers. 106469f2cd9fSThomas Weißschuh */ 106569f2cd9fSThomas Weißschuh switch (test + __LINE__ + 1) { 106669f2cd9fSThomas Weißschuh CASE_TEST(empty); EXPECT_VFPRINTF(0, "", ""); break; 106769f2cd9fSThomas Weißschuh CASE_TEST(simple); EXPECT_VFPRINTF(3, "foo", "foo"); break; 106869f2cd9fSThomas Weißschuh CASE_TEST(string); EXPECT_VFPRINTF(3, "foo", "%s", "foo"); break; 106969f2cd9fSThomas Weißschuh CASE_TEST(number); EXPECT_VFPRINTF(4, "1234", "%d", 1234); break; 107069f2cd9fSThomas Weißschuh CASE_TEST(negnumber); EXPECT_VFPRINTF(5, "-1234", "%d", -1234); break; 107169f2cd9fSThomas Weißschuh CASE_TEST(unsigned); EXPECT_VFPRINTF(5, "12345", "%u", 12345); break; 107269f2cd9fSThomas Weißschuh CASE_TEST(char); EXPECT_VFPRINTF(1, "c", "%c", 'c'); break; 107369f2cd9fSThomas Weißschuh CASE_TEST(hex); EXPECT_VFPRINTF(1, "f", "%x", 0xf); break; 107469f2cd9fSThomas Weißschuh CASE_TEST(pointer); EXPECT_VFPRINTF(3, "0x1", "%p", (void *) 0x1); break; 107569f2cd9fSThomas Weißschuh case __LINE__: 107669f2cd9fSThomas Weißschuh return ret; /* must be last */ 107769f2cd9fSThomas Weißschuh /* note: do not set any defaults so as to permit holes above */ 107869f2cd9fSThomas Weißschuh } 107969f2cd9fSThomas Weißschuh } 108069f2cd9fSThomas Weißschuh return ret; 108169f2cd9fSThomas Weißschuh } 108269f2cd9fSThomas Weißschuh 108397357168SThomas Weißschuh static int smash_stack(void) 108497357168SThomas Weißschuh { 108597357168SThomas Weißschuh char buf[100]; 1086e7654c3fSThomas Weißschuh volatile char *ptr = buf; 1087aa662d12SWilly Tarreau size_t i; 108897357168SThomas Weißschuh 1089aa662d12SWilly Tarreau for (i = 0; i < 200; i++) 1090e7654c3fSThomas Weißschuh ptr[i] = 'P'; 109197357168SThomas Weißschuh 109297357168SThomas Weißschuh return 1; 109397357168SThomas Weißschuh } 109497357168SThomas Weißschuh 109597357168SThomas Weißschuh static int run_protection(int min, int max) 109697357168SThomas Weißschuh { 109797357168SThomas Weißschuh pid_t pid; 109897357168SThomas Weißschuh int llen = 0, status; 109997357168SThomas Weißschuh 110097357168SThomas Weißschuh llen += printf("0 -fstackprotector "); 110197357168SThomas Weißschuh 1102818924d1SThomas Weißschuh #if !defined(_NOLIBC_STACKPROTECTOR) 110397357168SThomas Weißschuh llen += printf("not supported"); 1104b184a261SThomas Weißschuh result(llen, SKIPPED); 110597357168SThomas Weißschuh return 0; 110697357168SThomas Weißschuh #endif 110797357168SThomas Weißschuh 1108818924d1SThomas Weißschuh #if defined(_NOLIBC_STACKPROTECTOR) 110985250921SThomas Weißschuh if (!__stack_chk_guard) { 111085250921SThomas Weißschuh llen += printf("__stack_chk_guard not initialized"); 1111b184a261SThomas Weißschuh result(llen, FAIL); 111285250921SThomas Weißschuh return 1; 111385250921SThomas Weißschuh } 111485250921SThomas Weißschuh #endif 111585250921SThomas Weißschuh 111697357168SThomas Weißschuh pid = -1; 111797357168SThomas Weißschuh pid = fork(); 111897357168SThomas Weißschuh 111997357168SThomas Weißschuh switch (pid) { 112097357168SThomas Weißschuh case -1: 112197357168SThomas Weißschuh llen += printf("fork()"); 1122b184a261SThomas Weißschuh result(llen, FAIL); 112397357168SThomas Weißschuh return 1; 112497357168SThomas Weißschuh 112597357168SThomas Weißschuh case 0: 112697357168SThomas Weißschuh close(STDOUT_FILENO); 112797357168SThomas Weißschuh close(STDERR_FILENO); 112897357168SThomas Weißschuh 11299a75575bSThomas Weißschuh prctl(PR_SET_DUMPABLE, 0, 0, 0, 0); 113097357168SThomas Weißschuh smash_stack(); 113197357168SThomas Weißschuh return 1; 113297357168SThomas Weißschuh 113397357168SThomas Weißschuh default: 113497357168SThomas Weißschuh pid = waitpid(pid, &status, 0); 113597357168SThomas Weißschuh 113697357168SThomas Weißschuh if (pid == -1 || !WIFSIGNALED(status) || WTERMSIG(status) != SIGABRT) { 113797357168SThomas Weißschuh llen += printf("waitpid()"); 1138b184a261SThomas Weißschuh result(llen, FAIL); 113997357168SThomas Weißschuh return 1; 114097357168SThomas Weißschuh } 1141b184a261SThomas Weißschuh result(llen, OK); 114297357168SThomas Weißschuh return 0; 114397357168SThomas Weißschuh } 114497357168SThomas Weißschuh } 114597357168SThomas Weißschuh 11461a5454f6SWilly Tarreau /* prepare what needs to be prepared for pid 1 (stdio, /dev, /proc, etc) */ 11471a5454f6SWilly Tarreau int prepare(void) 11481a5454f6SWilly Tarreau { 11491a5454f6SWilly Tarreau struct stat stat_buf; 11501a5454f6SWilly Tarreau 11511a5454f6SWilly Tarreau /* It's possible that /dev doesn't even exist or was not mounted, so 11521a5454f6SWilly Tarreau * we'll try to create it, mount it, or create minimal entries into it. 11531a5454f6SWilly Tarreau * We want at least /dev/null and /dev/console. 11541a5454f6SWilly Tarreau */ 11551a5454f6SWilly Tarreau if (stat("/dev/.", &stat_buf) == 0 || mkdir("/dev", 0755) == 0) { 11561a5454f6SWilly Tarreau if (stat("/dev/console", &stat_buf) != 0 || 115782e339c2SZhangjin Wu stat("/dev/null", &stat_buf) != 0 || 115882e339c2SZhangjin Wu stat("/dev/zero", &stat_buf) != 0) { 11591a5454f6SWilly Tarreau /* try devtmpfs first, otherwise fall back to manual creation */ 11601a5454f6SWilly Tarreau if (mount("/dev", "/dev", "devtmpfs", 0, 0) != 0) { 11611a5454f6SWilly Tarreau mknod("/dev/console", 0600 | S_IFCHR, makedev(5, 1)); 11621a5454f6SWilly Tarreau mknod("/dev/null", 0666 | S_IFCHR, makedev(1, 3)); 116382e339c2SZhangjin Wu mknod("/dev/zero", 0666 | S_IFCHR, makedev(1, 5)); 11641a5454f6SWilly Tarreau } 11651a5454f6SWilly Tarreau } 11661a5454f6SWilly Tarreau } 11671a5454f6SWilly Tarreau 11681a5454f6SWilly Tarreau /* If no /dev/console was found before calling init, stdio is closed so 11691a5454f6SWilly Tarreau * we need to reopen it from /dev/console. If it failed above, it will 11701a5454f6SWilly Tarreau * still fail here and we cannot emit a message anyway. 11711a5454f6SWilly Tarreau */ 11721a5454f6SWilly Tarreau if (close(dup(1)) == -1) { 11731a5454f6SWilly Tarreau int fd = open("/dev/console", O_RDWR); 11741a5454f6SWilly Tarreau 11751a5454f6SWilly Tarreau if (fd >= 0) { 11761a5454f6SWilly Tarreau if (fd != 0) 11771a5454f6SWilly Tarreau dup2(fd, 0); 11781a5454f6SWilly Tarreau if (fd != 1) 11791a5454f6SWilly Tarreau dup2(fd, 1); 11801a5454f6SWilly Tarreau if (fd != 2) 11811a5454f6SWilly Tarreau dup2(fd, 2); 11821a5454f6SWilly Tarreau if (fd > 2) 11831a5454f6SWilly Tarreau close(fd); 11841a5454f6SWilly Tarreau puts("\nSuccessfully reopened /dev/console."); 11851a5454f6SWilly Tarreau } 11861a5454f6SWilly Tarreau } 11871a5454f6SWilly Tarreau 11881a5454f6SWilly Tarreau /* try to mount /proc if not mounted. Silently fail otherwise */ 11891a5454f6SWilly Tarreau if (stat("/proc/.", &stat_buf) == 0 || mkdir("/proc", 0755) == 0) { 1190b8b26108SZhangjin Wu if (stat("/proc/self", &stat_buf) != 0) { 1191b8b26108SZhangjin Wu /* If not mountable, remove /proc completely to avoid misuse */ 1192b8b26108SZhangjin Wu if (mount("none", "/proc", "proc", 0, 0) != 0) 1193b8b26108SZhangjin Wu rmdir("/proc"); 1194b8b26108SZhangjin Wu } 11951a5454f6SWilly Tarreau } 11961a5454f6SWilly Tarreau 1197bbb14546SZhangjin Wu /* some tests rely on a writable /tmp */ 1198bbb14546SZhangjin Wu mkdir("/tmp", 0755); 1199bbb14546SZhangjin Wu 12001a5454f6SWilly Tarreau return 0; 12011a5454f6SWilly Tarreau } 1202b4844fa0SWilly Tarreau 120323da7bc9SWilly Tarreau /* This is the definition of known test names, with their functions */ 1204c4560bd8SThomas Weißschuh static const struct test test_names[] = { 120523da7bc9SWilly Tarreau /* add new tests here */ 120648967b73SZhangjin Wu { .name = "startup", .func = run_startup }, 1207b4844fa0SWilly Tarreau { .name = "syscall", .func = run_syscall }, 120895bc9894SWilly Tarreau { .name = "stdlib", .func = run_stdlib }, 120969f2cd9fSThomas Weißschuh { .name = "vfprintf", .func = run_vfprintf }, 121097357168SThomas Weißschuh { .name = "protection", .func = run_protection }, 121123da7bc9SWilly Tarreau { 0 } 121223da7bc9SWilly Tarreau }; 121323da7bc9SWilly Tarreau 1214c388c992SZhangjin Wu int is_setting_valid(char *test) 1215c388c992SZhangjin Wu { 1216c388c992SZhangjin Wu int idx, len, test_len, valid = 0; 1217c388c992SZhangjin Wu char delimiter; 1218c388c992SZhangjin Wu 1219c388c992SZhangjin Wu if (!test) 1220c388c992SZhangjin Wu return valid; 1221c388c992SZhangjin Wu 1222c388c992SZhangjin Wu test_len = strlen(test); 1223c388c992SZhangjin Wu 1224c388c992SZhangjin Wu for (idx = 0; test_names[idx].name; idx++) { 1225c388c992SZhangjin Wu len = strlen(test_names[idx].name); 1226c388c992SZhangjin Wu if (test_len < len) 1227c388c992SZhangjin Wu continue; 1228c388c992SZhangjin Wu 1229c388c992SZhangjin Wu if (strncmp(test, test_names[idx].name, len) != 0) 1230c388c992SZhangjin Wu continue; 1231c388c992SZhangjin Wu 1232c388c992SZhangjin Wu delimiter = test[len]; 1233c388c992SZhangjin Wu if (delimiter != ':' && delimiter != ',' && delimiter != '\0') 1234c388c992SZhangjin Wu continue; 1235c388c992SZhangjin Wu 1236c388c992SZhangjin Wu valid = 1; 1237c388c992SZhangjin Wu break; 1238c388c992SZhangjin Wu } 1239c388c992SZhangjin Wu 1240c388c992SZhangjin Wu return valid; 1241c388c992SZhangjin Wu } 1242c388c992SZhangjin Wu 1243362aecb2SWilly Tarreau int main(int argc, char **argv, char **envp) 1244362aecb2SWilly Tarreau { 1245362aecb2SWilly Tarreau int min = 0; 1246a36cfc5eSZhangjin Wu int max = INT_MAX; 1247362aecb2SWilly Tarreau int ret = 0; 124823da7bc9SWilly Tarreau int err; 124923da7bc9SWilly Tarreau int idx; 125023da7bc9SWilly Tarreau char *test; 1251362aecb2SWilly Tarreau 1252938b5b98SZhangjin Wu argv0 = argv[0]; 125348967b73SZhangjin Wu test_argc = argc; 125448967b73SZhangjin Wu test_argv = argv; 125567d108e2SThomas Weißschuh test_envp = envp; 1256362aecb2SWilly Tarreau 12571a5454f6SWilly Tarreau /* when called as init, it's possible that no console was opened, for 12581a5454f6SWilly Tarreau * example if no /dev file system was provided. We'll check that fd#1 12591a5454f6SWilly Tarreau * was opened, and if not we'll attempt to create and open /dev/console 12601a5454f6SWilly Tarreau * and /dev/null that we'll use for later tests. 12611a5454f6SWilly Tarreau */ 12621a5454f6SWilly Tarreau if (getpid() == 1) 12631a5454f6SWilly Tarreau prepare(); 12641a5454f6SWilly Tarreau 126523da7bc9SWilly Tarreau /* the definition of a series of tests comes from either argv[1] or the 126623da7bc9SWilly Tarreau * "NOLIBC_TEST" environment variable. It's made of a comma-delimited 126723da7bc9SWilly Tarreau * series of test names and optional ranges: 126823da7bc9SWilly Tarreau * syscall:5-15[:.*],stdlib:8-10 126923da7bc9SWilly Tarreau */ 127023da7bc9SWilly Tarreau test = argv[1]; 1271c388c992SZhangjin Wu if (!is_setting_valid(test)) 127223da7bc9SWilly Tarreau test = getenv("NOLIBC_TEST"); 127323da7bc9SWilly Tarreau 1274c388c992SZhangjin Wu if (is_setting_valid(test)) { 127523da7bc9SWilly Tarreau char *comma, *colon, *dash, *value; 127623da7bc9SWilly Tarreau 127723da7bc9SWilly Tarreau do { 127823da7bc9SWilly Tarreau comma = strchr(test, ','); 127923da7bc9SWilly Tarreau if (comma) 128023da7bc9SWilly Tarreau *(comma++) = '\0'; 128123da7bc9SWilly Tarreau 128223da7bc9SWilly Tarreau colon = strchr(test, ':'); 128323da7bc9SWilly Tarreau if (colon) 128423da7bc9SWilly Tarreau *(colon++) = '\0'; 128523da7bc9SWilly Tarreau 128623da7bc9SWilly Tarreau for (idx = 0; test_names[idx].name; idx++) { 128723da7bc9SWilly Tarreau if (strcmp(test, test_names[idx].name) == 0) 128823da7bc9SWilly Tarreau break; 128923da7bc9SWilly Tarreau } 129023da7bc9SWilly Tarreau 129123da7bc9SWilly Tarreau if (test_names[idx].name) { 129223da7bc9SWilly Tarreau /* The test was named, it will be called at least 129323da7bc9SWilly Tarreau * once. We may have an optional range at <colon> 129423da7bc9SWilly Tarreau * here, which defaults to the full range. 129523da7bc9SWilly Tarreau */ 129623da7bc9SWilly Tarreau do { 1297a36cfc5eSZhangjin Wu min = 0; max = INT_MAX; 129823da7bc9SWilly Tarreau value = colon; 129923da7bc9SWilly Tarreau if (value && *value) { 130023da7bc9SWilly Tarreau colon = strchr(value, ':'); 130123da7bc9SWilly Tarreau if (colon) 130223da7bc9SWilly Tarreau *(colon++) = '\0'; 130323da7bc9SWilly Tarreau 130423da7bc9SWilly Tarreau dash = strchr(value, '-'); 130523da7bc9SWilly Tarreau if (dash) 130623da7bc9SWilly Tarreau *(dash++) = '\0'; 130723da7bc9SWilly Tarreau 130823da7bc9SWilly Tarreau /* support :val: :min-max: :min-: :-max: */ 130923da7bc9SWilly Tarreau if (*value) 131023da7bc9SWilly Tarreau min = atoi(value); 131123da7bc9SWilly Tarreau if (!dash) 131223da7bc9SWilly Tarreau max = min; 131323da7bc9SWilly Tarreau else if (*dash) 131423da7bc9SWilly Tarreau max = atoi(dash); 131523da7bc9SWilly Tarreau 131623da7bc9SWilly Tarreau value = colon; 131723da7bc9SWilly Tarreau } 131823da7bc9SWilly Tarreau 131923da7bc9SWilly Tarreau /* now's time to call the test */ 132023da7bc9SWilly Tarreau printf("Running test '%s'\n", test_names[idx].name); 132123da7bc9SWilly Tarreau err = test_names[idx].func(min, max); 132223da7bc9SWilly Tarreau ret += err; 132323da7bc9SWilly Tarreau printf("Errors during this test: %d\n\n", err); 132423da7bc9SWilly Tarreau } while (colon && *colon); 132523da7bc9SWilly Tarreau } else 132623da7bc9SWilly Tarreau printf("Ignoring unknown test name '%s'\n", test); 132723da7bc9SWilly Tarreau 132823da7bc9SWilly Tarreau test = comma; 132923da7bc9SWilly Tarreau } while (test && *test); 133023da7bc9SWilly Tarreau } else { 133123da7bc9SWilly Tarreau /* no test mentioned, run everything */ 133223da7bc9SWilly Tarreau for (idx = 0; test_names[idx].name; idx++) { 133323da7bc9SWilly Tarreau printf("Running test '%s'\n", test_names[idx].name); 133423da7bc9SWilly Tarreau err = test_names[idx].func(min, max); 133523da7bc9SWilly Tarreau ret += err; 133623da7bc9SWilly Tarreau printf("Errors during this test: %d\n\n", err); 133723da7bc9SWilly Tarreau } 133823da7bc9SWilly Tarreau } 133923da7bc9SWilly Tarreau 1340362aecb2SWilly Tarreau printf("Total number of errors: %d\n", ret); 1341f49896d7SWilly Tarreau 1342f49896d7SWilly Tarreau if (getpid() == 1) { 1343f49896d7SWilly Tarreau /* we're running as init, there's no other process on the 1344f49896d7SWilly Tarreau * system, thus likely started from a VM for a quick check. 1345f49896d7SWilly Tarreau * Exiting will provoke a kernel panic that may be reported 1346f49896d7SWilly Tarreau * as an error by Qemu or the hypervisor, while stopping 1347f49896d7SWilly Tarreau * cleanly will often be reported as a success. This allows 1348f49896d7SWilly Tarreau * to use the output of this program for bisecting kernels. 1349f49896d7SWilly Tarreau */ 1350f49896d7SWilly Tarreau printf("Leaving init with final status: %d\n", !!ret); 1351f49896d7SWilly Tarreau if (ret == 0) 1352950add28SZhangjin Wu reboot(RB_POWER_OFF); 1353aa73a86cSWilly Tarreau #if defined(__x86_64__) 1354aa73a86cSWilly Tarreau /* QEMU started with "-device isa-debug-exit -no-reboot" will 1355aa73a86cSWilly Tarreau * exit with status code 2N+1 when N is written to 0x501. We 1356aa73a86cSWilly Tarreau * hard-code the syscall here as it's arch-dependent. 1357aa73a86cSWilly Tarreau */ 135867eb617aSThomas Weißschuh else if (syscall(__NR_ioperm, 0x501, 1, 1) == 0) 13597f291cfaSThomas Weißschuh __asm__ volatile ("outb %%al, %%dx" :: "d"(0x501), "a"(0)); 1360aa73a86cSWilly Tarreau /* if it does nothing, fall back to the regular panic */ 1361aa73a86cSWilly Tarreau #endif 1362f49896d7SWilly Tarreau } 1363f49896d7SWilly Tarreau 1364362aecb2SWilly Tarreau printf("Exiting with status %d\n", !!ret); 1365362aecb2SWilly Tarreau return !!ret; 1366362aecb2SWilly Tarreau } 1367