xref: /openbmc/linux/tools/testing/selftests/nolibc/nolibc-test.c (revision bbb14546bd22b1c41af7182d085abe89ae21eecd)
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 */
181da02f51SWilly Tarreau #include <sys/io.h>
191da02f51SWilly Tarreau #include <sys/ioctl.h>
2069f2cd9fSThomas Weißschuh #include <sys/mman.h>
211da02f51SWilly Tarreau #include <sys/mount.h>
22208aa9d9SThomas Weißschuh #include <sys/prctl.h>
231da02f51SWilly Tarreau #include <sys/reboot.h>
241da02f51SWilly Tarreau #include <sys/stat.h>
251da02f51SWilly Tarreau #include <sys/syscall.h>
261da02f51SWilly Tarreau #include <sys/sysmacros.h>
271da02f51SWilly Tarreau #include <sys/time.h>
281da02f51SWilly Tarreau #include <sys/wait.h>
291da02f51SWilly Tarreau #include <dirent.h>
301da02f51SWilly Tarreau #include <errno.h>
311da02f51SWilly Tarreau #include <fcntl.h>
321da02f51SWilly Tarreau #include <poll.h>
331da02f51SWilly Tarreau #include <sched.h>
341da02f51SWilly Tarreau #include <signal.h>
351da02f51SWilly Tarreau #include <stdarg.h>
362df07fc5SWilly Tarreau #include <stddef.h>
372df07fc5SWilly Tarreau #include <stdint.h>
381da02f51SWilly Tarreau #include <unistd.h>
39bd27fef3SZhangjin Wu #include <limits.h>
401da02f51SWilly Tarreau #endif
41362aecb2SWilly Tarreau #endif
42362aecb2SWilly Tarreau 
43989abf1cSZhangjin Wu /* for the type of int_fast16_t and int_fast32_t, musl differs from glibc and nolibc */
44989abf1cSZhangjin Wu #define SINT_MAX_OF_TYPE(type) (((type)1 << (sizeof(type) * 8 - 2)) - (type)1 + ((type)1 << (sizeof(type) * 8 - 2)))
45989abf1cSZhangjin Wu #define SINT_MIN_OF_TYPE(type) (-SINT_MAX_OF_TYPE(type) - 1)
46989abf1cSZhangjin Wu 
47362aecb2SWilly Tarreau /* will be used by nolibc by getenv() */
48362aecb2SWilly Tarreau char **environ;
49362aecb2SWilly Tarreau 
50938b5b98SZhangjin Wu /* will be used by some test cases as readable file, please don't write it */
51938b5b98SZhangjin Wu static const char *argv0;
52938b5b98SZhangjin Wu 
5323da7bc9SWilly Tarreau /* definition of a series of tests */
5423da7bc9SWilly Tarreau struct test {
55fddc8f81SThomas Weißschuh 	const char *name;              /* test name */
56fddc8f81SThomas Weißschuh 	int (*func)(int min, int max); /* handler */
5723da7bc9SWilly Tarreau };
5823da7bc9SWilly Tarreau 
591da02f51SWilly Tarreau #ifndef _NOLIBC_STDLIB_H
601da02f51SWilly Tarreau char *itoa(int i)
611da02f51SWilly Tarreau {
621da02f51SWilly Tarreau 	static char buf[12];
631da02f51SWilly Tarreau 	int ret;
641da02f51SWilly Tarreau 
651da02f51SWilly Tarreau 	ret = snprintf(buf, sizeof(buf), "%d", i);
661da02f51SWilly Tarreau 	return (ret >= 0 && ret < sizeof(buf)) ? buf : "#err";
671da02f51SWilly Tarreau }
681da02f51SWilly Tarreau #endif
691da02f51SWilly Tarreau 
70362aecb2SWilly Tarreau #define CASE_ERR(err) \
71362aecb2SWilly Tarreau 	case err: return #err
72362aecb2SWilly Tarreau 
73362aecb2SWilly Tarreau /* returns the error name (e.g. "ENOENT") for common errors, "SUCCESS" for 0,
74362aecb2SWilly Tarreau  * or the decimal value for less common ones.
75362aecb2SWilly Tarreau  */
76362aecb2SWilly Tarreau const char *errorname(int err)
77362aecb2SWilly Tarreau {
78362aecb2SWilly Tarreau 	switch (err) {
79362aecb2SWilly Tarreau 	case 0: return "SUCCESS";
80362aecb2SWilly Tarreau 	CASE_ERR(EPERM);
81362aecb2SWilly Tarreau 	CASE_ERR(ENOENT);
82362aecb2SWilly Tarreau 	CASE_ERR(ESRCH);
83362aecb2SWilly Tarreau 	CASE_ERR(EINTR);
84362aecb2SWilly Tarreau 	CASE_ERR(EIO);
85362aecb2SWilly Tarreau 	CASE_ERR(ENXIO);
86362aecb2SWilly Tarreau 	CASE_ERR(E2BIG);
87362aecb2SWilly Tarreau 	CASE_ERR(ENOEXEC);
88362aecb2SWilly Tarreau 	CASE_ERR(EBADF);
89362aecb2SWilly Tarreau 	CASE_ERR(ECHILD);
90362aecb2SWilly Tarreau 	CASE_ERR(EAGAIN);
91362aecb2SWilly Tarreau 	CASE_ERR(ENOMEM);
92362aecb2SWilly Tarreau 	CASE_ERR(EACCES);
93362aecb2SWilly Tarreau 	CASE_ERR(EFAULT);
94362aecb2SWilly Tarreau 	CASE_ERR(ENOTBLK);
95362aecb2SWilly Tarreau 	CASE_ERR(EBUSY);
96362aecb2SWilly Tarreau 	CASE_ERR(EEXIST);
97362aecb2SWilly Tarreau 	CASE_ERR(EXDEV);
98362aecb2SWilly Tarreau 	CASE_ERR(ENODEV);
99362aecb2SWilly Tarreau 	CASE_ERR(ENOTDIR);
100362aecb2SWilly Tarreau 	CASE_ERR(EISDIR);
101362aecb2SWilly Tarreau 	CASE_ERR(EINVAL);
102362aecb2SWilly Tarreau 	CASE_ERR(ENFILE);
103362aecb2SWilly Tarreau 	CASE_ERR(EMFILE);
104362aecb2SWilly Tarreau 	CASE_ERR(ENOTTY);
105362aecb2SWilly Tarreau 	CASE_ERR(ETXTBSY);
106362aecb2SWilly Tarreau 	CASE_ERR(EFBIG);
107362aecb2SWilly Tarreau 	CASE_ERR(ENOSPC);
108362aecb2SWilly Tarreau 	CASE_ERR(ESPIPE);
109362aecb2SWilly Tarreau 	CASE_ERR(EROFS);
110362aecb2SWilly Tarreau 	CASE_ERR(EMLINK);
111362aecb2SWilly Tarreau 	CASE_ERR(EPIPE);
112362aecb2SWilly Tarreau 	CASE_ERR(EDOM);
113362aecb2SWilly Tarreau 	CASE_ERR(ERANGE);
114362aecb2SWilly Tarreau 	CASE_ERR(ENOSYS);
115758f970fSZhangjin Wu 	CASE_ERR(EOVERFLOW);
116362aecb2SWilly Tarreau 	default:
117362aecb2SWilly Tarreau 		return itoa(err);
118362aecb2SWilly Tarreau 	}
119362aecb2SWilly Tarreau }
120362aecb2SWilly Tarreau 
121443de903SThomas Weißschuh static void putcharn(char c, size_t n)
122443de903SThomas Weißschuh {
123443de903SThomas Weißschuh 	char buf[64];
124443de903SThomas Weißschuh 
125443de903SThomas Weißschuh 	memset(buf, c, n);
126443de903SThomas Weißschuh 	buf[n] = '\0';
127443de903SThomas Weißschuh 	fputs(buf, stdout);
128443de903SThomas Weißschuh }
129443de903SThomas Weißschuh 
130362aecb2SWilly Tarreau static int pad_spc(int llen, int cnt, const char *fmt, ...)
131362aecb2SWilly Tarreau {
132362aecb2SWilly Tarreau 	va_list args;
133362aecb2SWilly Tarreau 	int ret;
134362aecb2SWilly Tarreau 
135443de903SThomas Weißschuh 	putcharn(' ', cnt - llen);
136362aecb2SWilly Tarreau 
137362aecb2SWilly Tarreau 	va_start(args, fmt);
138362aecb2SWilly Tarreau 	ret = vfprintf(stdout, fmt, args);
139362aecb2SWilly Tarreau 	va_end(args);
140443de903SThomas Weißschuh 	return ret < 0 ? ret : ret + cnt - llen;
141362aecb2SWilly Tarreau }
142362aecb2SWilly Tarreau 
143362aecb2SWilly Tarreau /* The tests below are intended to be used by the macroes, which evaluate
144362aecb2SWilly Tarreau  * expression <expr>, print the status to stdout, and update the "ret"
145362aecb2SWilly Tarreau  * variable to count failures. The functions themselves return the number
146362aecb2SWilly Tarreau  * of failures, thus either 0 or 1.
147362aecb2SWilly Tarreau  */
148362aecb2SWilly Tarreau 
149362aecb2SWilly Tarreau #define EXPECT_ZR(cond, expr)				\
150a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_zr(expr, llen); } while (0)
151362aecb2SWilly Tarreau 
152362aecb2SWilly Tarreau static int expect_zr(int expr, int llen)
153362aecb2SWilly Tarreau {
154362aecb2SWilly Tarreau 	int ret = !(expr == 0);
155362aecb2SWilly Tarreau 
156362aecb2SWilly Tarreau 	llen += printf(" = %d ", expr);
157a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
158362aecb2SWilly Tarreau 	return ret;
159362aecb2SWilly Tarreau }
160362aecb2SWilly Tarreau 
161362aecb2SWilly Tarreau 
162362aecb2SWilly Tarreau #define EXPECT_NZ(cond, expr, val)			\
163a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_nz(expr, llen; } while (0)
164362aecb2SWilly Tarreau 
165362aecb2SWilly Tarreau static int expect_nz(int expr, int llen)
166362aecb2SWilly Tarreau {
167362aecb2SWilly Tarreau 	int ret = !(expr != 0);
168362aecb2SWilly Tarreau 
169362aecb2SWilly Tarreau 	llen += printf(" = %d ", expr);
170a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
171362aecb2SWilly Tarreau 	return ret;
172362aecb2SWilly Tarreau }
173362aecb2SWilly Tarreau 
174362aecb2SWilly Tarreau 
175362aecb2SWilly Tarreau #define EXPECT_EQ(cond, expr, val)				\
176a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_eq(expr, llen, val); } while (0)
177362aecb2SWilly Tarreau 
178a0994fb9SVincent Dagonneau static int expect_eq(uint64_t expr, int llen, uint64_t val)
179362aecb2SWilly Tarreau {
180362aecb2SWilly Tarreau 	int ret = !(expr == val);
181362aecb2SWilly Tarreau 
1820858aec4SZhangjin Wu 	llen += printf(" = %lld ", (long long)expr);
183a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
184362aecb2SWilly Tarreau 	return ret;
185362aecb2SWilly Tarreau }
186362aecb2SWilly Tarreau 
187362aecb2SWilly Tarreau 
188362aecb2SWilly Tarreau #define EXPECT_NE(cond, expr, val)				\
189a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ne(expr, llen, val); } while (0)
190362aecb2SWilly Tarreau 
191362aecb2SWilly Tarreau static int expect_ne(int expr, int llen, int val)
192362aecb2SWilly Tarreau {
193362aecb2SWilly Tarreau 	int ret = !(expr != val);
194362aecb2SWilly Tarreau 
195362aecb2SWilly Tarreau 	llen += printf(" = %d ", expr);
196a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
197362aecb2SWilly Tarreau 	return ret;
198362aecb2SWilly Tarreau }
199362aecb2SWilly Tarreau 
200362aecb2SWilly Tarreau 
201362aecb2SWilly Tarreau #define EXPECT_GE(cond, expr, val)				\
202a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ge(expr, llen, val); } while (0)
203362aecb2SWilly Tarreau 
204362aecb2SWilly Tarreau static int expect_ge(int expr, int llen, int val)
205362aecb2SWilly Tarreau {
206362aecb2SWilly Tarreau 	int ret = !(expr >= val);
207362aecb2SWilly Tarreau 
208362aecb2SWilly Tarreau 	llen += printf(" = %d ", expr);
209a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
210362aecb2SWilly Tarreau 	return ret;
211362aecb2SWilly Tarreau }
212362aecb2SWilly Tarreau 
213362aecb2SWilly Tarreau 
214362aecb2SWilly Tarreau #define EXPECT_GT(cond, expr, val)				\
215a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_gt(expr, llen, val); } while (0)
216362aecb2SWilly Tarreau 
217362aecb2SWilly Tarreau static int expect_gt(int expr, int llen, int val)
218362aecb2SWilly Tarreau {
219362aecb2SWilly Tarreau 	int ret = !(expr > val);
220362aecb2SWilly Tarreau 
221362aecb2SWilly Tarreau 	llen += printf(" = %d ", expr);
222a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
223362aecb2SWilly Tarreau 	return ret;
224362aecb2SWilly Tarreau }
225362aecb2SWilly Tarreau 
226362aecb2SWilly Tarreau 
227362aecb2SWilly Tarreau #define EXPECT_LE(cond, expr, val)				\
228a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_le(expr, llen, val); } while (0)
229362aecb2SWilly Tarreau 
230362aecb2SWilly Tarreau static int expect_le(int expr, int llen, int val)
231362aecb2SWilly Tarreau {
232362aecb2SWilly Tarreau 	int ret = !(expr <= val);
233362aecb2SWilly Tarreau 
234362aecb2SWilly Tarreau 	llen += printf(" = %d ", expr);
235a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
236362aecb2SWilly Tarreau 	return ret;
237362aecb2SWilly Tarreau }
238362aecb2SWilly Tarreau 
239362aecb2SWilly Tarreau 
240362aecb2SWilly Tarreau #define EXPECT_LT(cond, expr, val)				\
241a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_lt(expr, llen, val); } while (0)
242362aecb2SWilly Tarreau 
243362aecb2SWilly Tarreau static int expect_lt(int expr, int llen, int val)
244362aecb2SWilly Tarreau {
245362aecb2SWilly Tarreau 	int ret = !(expr < val);
246362aecb2SWilly Tarreau 
247362aecb2SWilly Tarreau 	llen += printf(" = %d ", expr);
248a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
249362aecb2SWilly Tarreau 	return ret;
250362aecb2SWilly Tarreau }
251362aecb2SWilly Tarreau 
252362aecb2SWilly Tarreau 
253362aecb2SWilly Tarreau #define EXPECT_SYSZR(cond, expr)				\
254a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_syszr(expr, llen); } while (0)
255362aecb2SWilly Tarreau 
256362aecb2SWilly Tarreau static int expect_syszr(int expr, int llen)
257362aecb2SWilly Tarreau {
258362aecb2SWilly Tarreau 	int ret = 0;
259362aecb2SWilly Tarreau 
260362aecb2SWilly Tarreau 	if (expr) {
261362aecb2SWilly Tarreau 		ret = 1;
262362aecb2SWilly Tarreau 		llen += printf(" = %d %s ", expr, errorname(errno));
263a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
264362aecb2SWilly Tarreau 	} else {
265362aecb2SWilly Tarreau 		llen += printf(" = %d ", expr);
266a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
267362aecb2SWilly Tarreau 	}
268362aecb2SWilly Tarreau 	return ret;
269362aecb2SWilly Tarreau }
270362aecb2SWilly Tarreau 
271362aecb2SWilly Tarreau 
272362aecb2SWilly Tarreau #define EXPECT_SYSEQ(cond, expr, val)				\
273a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_syseq(expr, llen, val); } while (0)
274362aecb2SWilly Tarreau 
275362aecb2SWilly Tarreau static int expect_syseq(int expr, int llen, int val)
276362aecb2SWilly Tarreau {
277362aecb2SWilly Tarreau 	int ret = 0;
278362aecb2SWilly Tarreau 
279362aecb2SWilly Tarreau 	if (expr != val) {
280362aecb2SWilly Tarreau 		ret = 1;
281362aecb2SWilly Tarreau 		llen += printf(" = %d %s ", expr, errorname(errno));
282a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
283362aecb2SWilly Tarreau 	} else {
284362aecb2SWilly Tarreau 		llen += printf(" = %d ", expr);
285a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
286362aecb2SWilly Tarreau 	}
287362aecb2SWilly Tarreau 	return ret;
288362aecb2SWilly Tarreau }
289362aecb2SWilly Tarreau 
290362aecb2SWilly Tarreau 
291362aecb2SWilly Tarreau #define EXPECT_SYSNE(cond, expr, val)				\
292a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_sysne(expr, llen, val); } while (0)
293362aecb2SWilly Tarreau 
294362aecb2SWilly Tarreau static int expect_sysne(int expr, int llen, int val)
295362aecb2SWilly Tarreau {
296362aecb2SWilly Tarreau 	int ret = 0;
297362aecb2SWilly Tarreau 
298362aecb2SWilly Tarreau 	if (expr == val) {
299362aecb2SWilly Tarreau 		ret = 1;
300362aecb2SWilly Tarreau 		llen += printf(" = %d %s ", expr, errorname(errno));
301a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
302362aecb2SWilly Tarreau 	} else {
303362aecb2SWilly Tarreau 		llen += printf(" = %d ", expr);
304a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
305362aecb2SWilly Tarreau 	}
306362aecb2SWilly Tarreau 	return ret;
307362aecb2SWilly Tarreau }
308362aecb2SWilly Tarreau 
309362aecb2SWilly Tarreau 
31075d75a7bSZhangjin Wu #define EXPECT_SYSER2(cond, expr, expret, experr1, experr2)		\
31175d75a7bSZhangjin Wu 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_syserr2(expr, expret, experr1, experr2, llen); } while (0)
312362aecb2SWilly Tarreau 
31375d75a7bSZhangjin Wu #define EXPECT_SYSER(cond, expr, expret, experr)			\
31475d75a7bSZhangjin Wu 	EXPECT_SYSER2(cond, expr, expret, experr, 0)
31575d75a7bSZhangjin Wu 
31675d75a7bSZhangjin Wu static int expect_syserr2(int expr, int expret, int experr1, int experr2, int llen)
317362aecb2SWilly Tarreau {
318362aecb2SWilly Tarreau 	int ret = 0;
319362aecb2SWilly Tarreau 	int _errno = errno;
320362aecb2SWilly Tarreau 
321362aecb2SWilly Tarreau 	llen += printf(" = %d %s ", expr, errorname(_errno));
32275d75a7bSZhangjin Wu 	if (expr != expret || (_errno != experr1 && _errno != experr2)) {
323362aecb2SWilly Tarreau 		ret = 1;
32475d75a7bSZhangjin Wu 		if (experr2 == 0)
32575d75a7bSZhangjin Wu 			llen += printf(" != (%d %s) ", expret, errorname(experr1));
32675d75a7bSZhangjin Wu 		else
32775d75a7bSZhangjin Wu 			llen += printf(" != (%d %s %s) ", expret, errorname(experr1), errorname(experr2));
328a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
329362aecb2SWilly Tarreau 	} else {
330a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
331362aecb2SWilly Tarreau 	}
332362aecb2SWilly Tarreau 	return ret;
333362aecb2SWilly Tarreau }
334362aecb2SWilly Tarreau 
335362aecb2SWilly Tarreau 
336362aecb2SWilly Tarreau #define EXPECT_PTRZR(cond, expr)				\
337a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ptrzr(expr, llen); } while (0)
338362aecb2SWilly Tarreau 
339362aecb2SWilly Tarreau static int expect_ptrzr(const void *expr, int llen)
340362aecb2SWilly Tarreau {
341362aecb2SWilly Tarreau 	int ret = 0;
342362aecb2SWilly Tarreau 
343362aecb2SWilly Tarreau 	llen += printf(" = <%p> ", expr);
344362aecb2SWilly Tarreau 	if (expr) {
345362aecb2SWilly Tarreau 		ret = 1;
346a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
347362aecb2SWilly Tarreau 	} else {
348a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
349362aecb2SWilly Tarreau 	}
350362aecb2SWilly Tarreau 	return ret;
351362aecb2SWilly Tarreau }
352362aecb2SWilly Tarreau 
353362aecb2SWilly Tarreau 
354362aecb2SWilly Tarreau #define EXPECT_PTRNZ(cond, expr)				\
355a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ptrnz(expr, llen); } while (0)
356362aecb2SWilly Tarreau 
357362aecb2SWilly Tarreau static int expect_ptrnz(const void *expr, int llen)
358362aecb2SWilly Tarreau {
359362aecb2SWilly Tarreau 	int ret = 0;
360362aecb2SWilly Tarreau 
361362aecb2SWilly Tarreau 	llen += printf(" = <%p> ", expr);
362362aecb2SWilly Tarreau 	if (!expr) {
363362aecb2SWilly Tarreau 		ret = 1;
364a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
365362aecb2SWilly Tarreau 	} else {
366a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
367362aecb2SWilly Tarreau 	}
368362aecb2SWilly Tarreau 	return ret;
369362aecb2SWilly Tarreau }
370362aecb2SWilly Tarreau 
37129f5540bSZhangjin Wu #define EXPECT_PTREQ(cond, expr, cmp)				\
37229f5540bSZhangjin Wu 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ptreq(expr, llen, cmp); } while (0)
37329f5540bSZhangjin Wu 
37429f5540bSZhangjin Wu static int expect_ptreq(const void *expr, int llen, const void *cmp)
37529f5540bSZhangjin Wu {
37629f5540bSZhangjin Wu 	int ret = 0;
37729f5540bSZhangjin Wu 
37829f5540bSZhangjin Wu 	llen += printf(" = <%p> ", expr);
37929f5540bSZhangjin Wu 	if (expr != cmp) {
38029f5540bSZhangjin Wu 		ret = 1;
38129f5540bSZhangjin Wu 		llen += pad_spc(llen, 64, "[FAIL]\n");
38229f5540bSZhangjin Wu 	} else {
38329f5540bSZhangjin Wu 		llen += pad_spc(llen, 64, " [OK]\n");
38429f5540bSZhangjin Wu 	}
38529f5540bSZhangjin Wu 	return ret;
38629f5540bSZhangjin Wu }
38729f5540bSZhangjin Wu 
38829f5540bSZhangjin Wu #define EXPECT_PTRNE(cond, expr, cmp)				\
38929f5540bSZhangjin Wu 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ptrne(expr, llen, cmp); } while (0)
39029f5540bSZhangjin Wu 
39129f5540bSZhangjin Wu static int expect_ptrne(const void *expr, int llen, const void *cmp)
39229f5540bSZhangjin Wu {
39329f5540bSZhangjin Wu 	int ret = 0;
39429f5540bSZhangjin Wu 
39529f5540bSZhangjin Wu 	llen += printf(" = <%p> ", expr);
39629f5540bSZhangjin Wu 	if (expr == cmp) {
39729f5540bSZhangjin Wu 		ret = 1;
39829f5540bSZhangjin Wu 		llen += pad_spc(llen, 64, "[FAIL]\n");
39929f5540bSZhangjin Wu 	} else {
40029f5540bSZhangjin Wu 		llen += pad_spc(llen, 64, " [OK]\n");
40129f5540bSZhangjin Wu 	}
40229f5540bSZhangjin Wu 	return ret;
40329f5540bSZhangjin Wu }
40429f5540bSZhangjin Wu 
40529f5540bSZhangjin Wu #define EXPECT_PTRER2(cond, expr, expret, experr1, experr2)		\
40629f5540bSZhangjin Wu 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ptrerr2(expr, expret, experr1, experr2, llen); } while (0)
40729f5540bSZhangjin Wu 
40829f5540bSZhangjin Wu #define EXPECT_PTRER(cond, expr, expret, experr)			\
40929f5540bSZhangjin Wu 	EXPECT_PTRER2(cond, expr, expret, experr, 0)
41029f5540bSZhangjin Wu 
41129f5540bSZhangjin Wu static int expect_ptrerr2(const void *expr, const void *expret, int experr1, int experr2, int llen)
41229f5540bSZhangjin Wu {
41329f5540bSZhangjin Wu 	int ret = 0;
41429f5540bSZhangjin Wu 	int _errno = errno;
41529f5540bSZhangjin Wu 
41629f5540bSZhangjin Wu 	llen += printf(" = <%p> %s ", expr, errorname(_errno));
41729f5540bSZhangjin Wu 	if (expr != expret || (_errno != experr1 && _errno != experr2)) {
41829f5540bSZhangjin Wu 		ret = 1;
41929f5540bSZhangjin Wu 		if (experr2 == 0)
42029f5540bSZhangjin Wu 			llen += printf(" != (<%p> %s) ", expret, errorname(experr1));
42129f5540bSZhangjin Wu 		else
42229f5540bSZhangjin Wu 			llen += printf(" != (<%p> %s %s) ", expret, errorname(experr1), errorname(experr2));
42329f5540bSZhangjin Wu 		llen += pad_spc(llen, 64, "[FAIL]\n");
42429f5540bSZhangjin Wu 	} else {
42529f5540bSZhangjin Wu 		llen += pad_spc(llen, 64, " [OK]\n");
42629f5540bSZhangjin Wu 	}
42729f5540bSZhangjin Wu 	return ret;
42829f5540bSZhangjin Wu }
429362aecb2SWilly Tarreau 
430362aecb2SWilly Tarreau #define EXPECT_STRZR(cond, expr)				\
431a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_strzr(expr, llen); } while (0)
432362aecb2SWilly Tarreau 
433362aecb2SWilly Tarreau static int expect_strzr(const char *expr, int llen)
434362aecb2SWilly Tarreau {
435362aecb2SWilly Tarreau 	int ret = 0;
436362aecb2SWilly Tarreau 
437362aecb2SWilly Tarreau 	llen += printf(" = <%s> ", expr);
438362aecb2SWilly Tarreau 	if (expr) {
439362aecb2SWilly Tarreau 		ret = 1;
440a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
441362aecb2SWilly Tarreau 	} else {
442a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
443362aecb2SWilly Tarreau 	}
444362aecb2SWilly Tarreau 	return ret;
445362aecb2SWilly Tarreau }
446362aecb2SWilly Tarreau 
447362aecb2SWilly Tarreau 
448362aecb2SWilly Tarreau #define EXPECT_STRNZ(cond, expr)				\
449a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_strnz(expr, llen); } while (0)
450362aecb2SWilly Tarreau 
451362aecb2SWilly Tarreau static int expect_strnz(const char *expr, int llen)
452362aecb2SWilly Tarreau {
453362aecb2SWilly Tarreau 	int ret = 0;
454362aecb2SWilly Tarreau 
455362aecb2SWilly Tarreau 	llen += printf(" = <%s> ", expr);
456362aecb2SWilly Tarreau 	if (!expr) {
457362aecb2SWilly Tarreau 		ret = 1;
458a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
459362aecb2SWilly Tarreau 	} else {
460a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
461362aecb2SWilly Tarreau 	}
462362aecb2SWilly Tarreau 	return ret;
463362aecb2SWilly Tarreau }
464362aecb2SWilly Tarreau 
465362aecb2SWilly Tarreau 
466362aecb2SWilly Tarreau #define EXPECT_STREQ(cond, expr, cmp)				\
467a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_streq(expr, llen, cmp); } while (0)
468362aecb2SWilly Tarreau 
469362aecb2SWilly Tarreau static int expect_streq(const char *expr, int llen, const char *cmp)
470362aecb2SWilly Tarreau {
471362aecb2SWilly Tarreau 	int ret = 0;
472362aecb2SWilly Tarreau 
473362aecb2SWilly Tarreau 	llen += printf(" = <%s> ", expr);
474362aecb2SWilly Tarreau 	if (strcmp(expr, cmp) != 0) {
475362aecb2SWilly Tarreau 		ret = 1;
476a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
477362aecb2SWilly Tarreau 	} else {
478a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
479362aecb2SWilly Tarreau 	}
480362aecb2SWilly Tarreau 	return ret;
481362aecb2SWilly Tarreau }
482362aecb2SWilly Tarreau 
483362aecb2SWilly Tarreau 
484362aecb2SWilly Tarreau #define EXPECT_STRNE(cond, expr, cmp)				\
485a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_strne(expr, llen, cmp); } while (0)
486362aecb2SWilly Tarreau 
487362aecb2SWilly Tarreau static int expect_strne(const char *expr, int llen, const char *cmp)
488362aecb2SWilly Tarreau {
489362aecb2SWilly Tarreau 	int ret = 0;
490362aecb2SWilly Tarreau 
491362aecb2SWilly Tarreau 	llen += printf(" = <%s> ", expr);
492362aecb2SWilly Tarreau 	if (strcmp(expr, cmp) == 0) {
493362aecb2SWilly Tarreau 		ret = 1;
494a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
495362aecb2SWilly Tarreau 	} else {
496a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
497362aecb2SWilly Tarreau 	}
498362aecb2SWilly Tarreau 	return ret;
499362aecb2SWilly Tarreau }
500362aecb2SWilly Tarreau 
50123da7bc9SWilly Tarreau 
502362aecb2SWilly Tarreau /* declare tests based on line numbers. There must be exactly one test per line. */
503362aecb2SWilly Tarreau #define CASE_TEST(name) \
504362aecb2SWilly Tarreau 	case __LINE__: llen += printf("%d %s", test, #name);
505362aecb2SWilly Tarreau 
506362aecb2SWilly Tarreau 
507b4844fa0SWilly Tarreau /* used by some syscall tests below */
508b4844fa0SWilly Tarreau int test_getdents64(const char *dir)
509b4844fa0SWilly Tarreau {
510b4844fa0SWilly Tarreau 	char buffer[4096];
511b4844fa0SWilly Tarreau 	int fd, ret;
512b4844fa0SWilly Tarreau 	int err;
513b4844fa0SWilly Tarreau 
514b4844fa0SWilly Tarreau 	ret = fd = open(dir, O_RDONLY | O_DIRECTORY, 0);
515b4844fa0SWilly Tarreau 	if (ret < 0)
516b4844fa0SWilly Tarreau 		return ret;
517b4844fa0SWilly Tarreau 
518b4844fa0SWilly Tarreau 	ret = getdents64(fd, (void *)buffer, sizeof(buffer));
519b4844fa0SWilly Tarreau 	err = errno;
520b4844fa0SWilly Tarreau 	close(fd);
521b4844fa0SWilly Tarreau 
522b4844fa0SWilly Tarreau 	errno = err;
523b4844fa0SWilly Tarreau 	return ret;
524b4844fa0SWilly Tarreau }
525b4844fa0SWilly Tarreau 
526a290296aSAmmar Faizi static int test_getpagesize(void)
527a290296aSAmmar Faizi {
528a290296aSAmmar Faizi 	long x = getpagesize();
529a290296aSAmmar Faizi 	int c;
530a290296aSAmmar Faizi 
531a290296aSAmmar Faizi 	if (x < 0)
532a290296aSAmmar Faizi 		return x;
533a290296aSAmmar Faizi 
534a290296aSAmmar Faizi #if defined(__x86_64__) || defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__)
535a290296aSAmmar Faizi 	/*
536a290296aSAmmar Faizi 	 * x86 family is always 4K page.
537a290296aSAmmar Faizi 	 */
538a290296aSAmmar Faizi 	c = (x == 4096);
539a290296aSAmmar Faizi #elif defined(__aarch64__)
540a290296aSAmmar Faizi 	/*
541a290296aSAmmar Faizi 	 * Linux aarch64 supports three values of page size: 4K, 16K, and 64K
542a290296aSAmmar Faizi 	 * which are selected at kernel compilation time.
543a290296aSAmmar Faizi 	 */
544a290296aSAmmar Faizi 	c = (x == 4096 || x == (16 * 1024) || x == (64 * 1024));
545a290296aSAmmar Faizi #else
546a290296aSAmmar Faizi 	/*
547a290296aSAmmar Faizi 	 * Assuming other architectures must have at least 4K page.
548a290296aSAmmar Faizi 	 */
549a290296aSAmmar Faizi 	c = (x >= 4096);
550a290296aSAmmar Faizi #endif
551a290296aSAmmar Faizi 
552a290296aSAmmar Faizi 	return !c;
553a290296aSAmmar Faizi }
554a290296aSAmmar Faizi 
5553ad09d72SThomas Weißschuh static int test_fork(void)
5563ad09d72SThomas Weißschuh {
5573ad09d72SThomas Weißschuh 	int status;
558ed495f09SZhangjin Wu 	pid_t pid;
559ed495f09SZhangjin Wu 
560ed495f09SZhangjin Wu 	/* flush the printf buffer to avoid child flush it */
561ed495f09SZhangjin Wu 	fflush(stdout);
562ed495f09SZhangjin Wu 	fflush(stderr);
563ed495f09SZhangjin Wu 
564ed495f09SZhangjin Wu 	pid = fork();
5653ad09d72SThomas Weißschuh 
5663ad09d72SThomas Weißschuh 	switch (pid) {
5673ad09d72SThomas Weißschuh 	case -1:
5683ad09d72SThomas Weißschuh 		return 1;
5693ad09d72SThomas Weißschuh 
5703ad09d72SThomas Weißschuh 	case 0:
5713ad09d72SThomas Weißschuh 		exit(123);
5723ad09d72SThomas Weißschuh 
5733ad09d72SThomas Weißschuh 	default:
5743ad09d72SThomas Weißschuh 		pid = waitpid(pid, &status, 0);
5753ad09d72SThomas Weißschuh 
5763ad09d72SThomas Weißschuh 		return pid == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 123;
5773ad09d72SThomas Weißschuh 	}
5783ad09d72SThomas Weißschuh }
5793ad09d72SThomas Weißschuh 
58087b9fa66SThomas Weißschuh static int test_stat_timestamps(void)
58187b9fa66SThomas Weißschuh {
58287b9fa66SThomas Weißschuh 	struct stat st;
58387b9fa66SThomas Weißschuh 
58487b9fa66SThomas Weißschuh 	if (sizeof(st.st_atim.tv_sec) != sizeof(st.st_atime))
58587b9fa66SThomas Weißschuh 		return 1;
58687b9fa66SThomas Weißschuh 
58787b9fa66SThomas Weißschuh 	if (stat("/proc/self/", &st))
58887b9fa66SThomas Weißschuh 		return 1;
58987b9fa66SThomas Weißschuh 
59087b9fa66SThomas Weißschuh 	if (st.st_atim.tv_sec != st.st_atime || st.st_atim.tv_nsec > 1000000000)
59187b9fa66SThomas Weißschuh 		return 1;
59287b9fa66SThomas Weißschuh 
59387b9fa66SThomas Weißschuh 	if (st.st_mtim.tv_sec != st.st_mtime || st.st_mtim.tv_nsec > 1000000000)
59487b9fa66SThomas Weißschuh 		return 1;
59587b9fa66SThomas Weißschuh 
59687b9fa66SThomas Weißschuh 	if (st.st_ctim.tv_sec != st.st_ctime || st.st_ctim.tv_nsec > 1000000000)
59787b9fa66SThomas Weißschuh 		return 1;
59887b9fa66SThomas Weißschuh 
59987b9fa66SThomas Weißschuh 	return 0;
60087b9fa66SThomas Weißschuh }
60187b9fa66SThomas Weißschuh 
602fcdbf5ddSZhangjin Wu int test_mmap_munmap(void)
603fcdbf5ddSZhangjin Wu {
604fcdbf5ddSZhangjin Wu 	int ret, fd, i;
605fcdbf5ddSZhangjin Wu 	void *mem;
606fcdbf5ddSZhangjin Wu 	size_t page_size, file_size, length;
607fcdbf5ddSZhangjin Wu 	off_t offset, pa_offset;
608fcdbf5ddSZhangjin Wu 	struct stat stat_buf;
609fcdbf5ddSZhangjin Wu 	const char * const files[] = {
610fcdbf5ddSZhangjin Wu 		"/dev/zero",
611fcdbf5ddSZhangjin Wu 		"/proc/1/exe", "/proc/self/exe",
612fcdbf5ddSZhangjin Wu 		argv0,
613fcdbf5ddSZhangjin Wu 		NULL
614fcdbf5ddSZhangjin Wu 	};
615fcdbf5ddSZhangjin Wu 
616fcdbf5ddSZhangjin Wu 	page_size = getpagesize();
617fcdbf5ddSZhangjin Wu 	if (page_size < 0)
618fcdbf5ddSZhangjin Wu 		return -1;
619fcdbf5ddSZhangjin Wu 
620fcdbf5ddSZhangjin Wu 	/* find a right file to mmap, existed and accessible */
621fcdbf5ddSZhangjin Wu 	for (i = 0; files[i] != NULL; i++) {
622fcdbf5ddSZhangjin Wu 		ret = fd = open(files[i], O_RDONLY);
623fcdbf5ddSZhangjin Wu 		if (ret == -1)
624fcdbf5ddSZhangjin Wu 			continue;
625fcdbf5ddSZhangjin Wu 		else
626fcdbf5ddSZhangjin Wu 			break;
627fcdbf5ddSZhangjin Wu 	}
628fcdbf5ddSZhangjin Wu 	if (ret == -1)
629fcdbf5ddSZhangjin Wu 		return ret;
630fcdbf5ddSZhangjin Wu 
631fcdbf5ddSZhangjin Wu 	ret = stat(files[i], &stat_buf);
632fcdbf5ddSZhangjin Wu 	if (ret == -1)
633fcdbf5ddSZhangjin Wu 		goto end;
634fcdbf5ddSZhangjin Wu 
635fcdbf5ddSZhangjin Wu 	/* file size of the special /dev/zero is 0, let's assign one manually */
636fcdbf5ddSZhangjin Wu 	if (i == 0)
637fcdbf5ddSZhangjin Wu 		file_size = 3*page_size;
638fcdbf5ddSZhangjin Wu 	else
639fcdbf5ddSZhangjin Wu 		file_size = stat_buf.st_size;
640fcdbf5ddSZhangjin Wu 
641fcdbf5ddSZhangjin Wu 	offset = file_size - 1;
642fcdbf5ddSZhangjin Wu 	if (offset < 0)
643fcdbf5ddSZhangjin Wu 		offset = 0;
644fcdbf5ddSZhangjin Wu 	length = file_size - offset;
645fcdbf5ddSZhangjin Wu 	pa_offset = offset & ~(page_size - 1);
646fcdbf5ddSZhangjin Wu 
647fcdbf5ddSZhangjin Wu 	mem = mmap(NULL, length + offset - pa_offset, PROT_READ, MAP_SHARED, fd, pa_offset);
648fcdbf5ddSZhangjin Wu 	if (mem == MAP_FAILED) {
649fcdbf5ddSZhangjin Wu 		ret = -1;
650fcdbf5ddSZhangjin Wu 		goto end;
651fcdbf5ddSZhangjin Wu 	}
652fcdbf5ddSZhangjin Wu 
653fcdbf5ddSZhangjin Wu 	ret = munmap(mem, length + offset - pa_offset);
654fcdbf5ddSZhangjin Wu 
655fcdbf5ddSZhangjin Wu end:
656fcdbf5ddSZhangjin Wu 	close(fd);
657fcdbf5ddSZhangjin Wu 	return ret;
658fcdbf5ddSZhangjin Wu }
659fcdbf5ddSZhangjin Wu 
660fcdbf5ddSZhangjin Wu 
661b4844fa0SWilly Tarreau /* Run syscall tests between IDs <min> and <max>.
662b4844fa0SWilly Tarreau  * Return 0 on success, non-zero on failure.
663b4844fa0SWilly Tarreau  */
664b4844fa0SWilly Tarreau int run_syscall(int min, int max)
665b4844fa0SWilly Tarreau {
666957bfa31SZhangjin Wu 	struct timeval tv;
667957bfa31SZhangjin Wu 	struct timezone tz;
668b4844fa0SWilly Tarreau 	struct stat stat_buf;
6693e2d337bSWilly Tarreau 	int euid0;
6707172f1c6SWilly Tarreau 	int proc;
671b4844fa0SWilly Tarreau 	int test;
672b4844fa0SWilly Tarreau 	int tmp;
673b4844fa0SWilly Tarreau 	int ret = 0;
674b4844fa0SWilly Tarreau 	void *p1, *p2;
67579b4f68eSZhangjin Wu 	int has_gettid = 1;
676b4844fa0SWilly Tarreau 
6777172f1c6SWilly Tarreau 	/* <proc> indicates whether or not /proc is mounted */
6787172f1c6SWilly Tarreau 	proc = stat("/proc", &stat_buf) == 0;
6797172f1c6SWilly Tarreau 
6803e2d337bSWilly Tarreau 	/* this will be used to skip certain tests that can't be run unprivileged */
6813e2d337bSWilly Tarreau 	euid0 = geteuid() == 0;
6823e2d337bSWilly Tarreau 
68379b4f68eSZhangjin Wu 	/* from 2.30, glibc provides gettid() */
68479b4f68eSZhangjin Wu #if defined(__GLIBC_MINOR__) && defined(__GLIBC__)
68579b4f68eSZhangjin Wu 	has_gettid = __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 30);
68679b4f68eSZhangjin Wu #endif
68779b4f68eSZhangjin Wu 
688b4844fa0SWilly Tarreau 	for (test = min; test >= 0 && test <= max; test++) {
689fddc8f81SThomas Weißschuh 		int llen = 0; /* line length */
690b4844fa0SWilly Tarreau 
691b4844fa0SWilly Tarreau 		/* avoid leaving empty lines below, this will insert holes into
692b4844fa0SWilly Tarreau 		 * test numbers.
693b4844fa0SWilly Tarreau 		 */
694b4844fa0SWilly Tarreau 		switch (test + __LINE__ + 1) {
695b4844fa0SWilly Tarreau 		CASE_TEST(getpid);            EXPECT_SYSNE(1, getpid(), -1); break;
696b4844fa0SWilly Tarreau 		CASE_TEST(getppid);           EXPECT_SYSNE(1, getppid(), -1); break;
69779b4f68eSZhangjin Wu 		CASE_TEST(gettid);            EXPECT_SYSNE(has_gettid, gettid(), -1); break;
698b4844fa0SWilly Tarreau 		CASE_TEST(getpgid_self);      EXPECT_SYSNE(1, getpgid(0), -1); break;
699b4844fa0SWilly Tarreau 		CASE_TEST(getpgid_bad);       EXPECT_SYSER(1, getpgid(-1), -1, ESRCH); break;
700b4844fa0SWilly Tarreau 		CASE_TEST(kill_0);            EXPECT_SYSZR(1, kill(getpid(), 0)); break;
701b4844fa0SWilly Tarreau 		CASE_TEST(kill_CONT);         EXPECT_SYSZR(1, kill(getpid(), 0)); break;
702b4844fa0SWilly Tarreau 		CASE_TEST(kill_BADPID);       EXPECT_SYSER(1, kill(INT_MAX, 0), -1, ESRCH); break;
703f193ecbfSZhangjin Wu 		CASE_TEST(sbrk_0);            EXPECT_PTRNE(1, sbrk(0), (void *)-1); break;
704b4844fa0SWilly Tarreau 		CASE_TEST(sbrk);              if ((p1 = p2 = sbrk(4096)) != (void *)-1) p2 = sbrk(-4096); EXPECT_SYSZR(1, (p2 == (void *)-1) || p2 == p1); break;
705b4844fa0SWilly Tarreau 		CASE_TEST(brk);               EXPECT_SYSZR(1, brk(sbrk(0))); break;
706b4844fa0SWilly Tarreau 		CASE_TEST(chdir_root);        EXPECT_SYSZR(1, chdir("/")); break;
707b4844fa0SWilly Tarreau 		CASE_TEST(chdir_dot);         EXPECT_SYSZR(1, chdir(".")); break;
708b4844fa0SWilly Tarreau 		CASE_TEST(chdir_blah);        EXPECT_SYSER(1, chdir("/blah"), -1, ENOENT); break;
7097172f1c6SWilly Tarreau 		CASE_TEST(chmod_net);         EXPECT_SYSZR(proc, chmod("/proc/self/net", 0555)); break;
7107172f1c6SWilly Tarreau 		CASE_TEST(chmod_self);        EXPECT_SYSER(proc, chmod("/proc/self", 0555), -1, EPERM); break;
7117172f1c6SWilly Tarreau 		CASE_TEST(chown_self);        EXPECT_SYSER(proc, chown("/proc/self", 0, 0), -1, EPERM); break;
7123e2d337bSWilly Tarreau 		CASE_TEST(chroot_root);       EXPECT_SYSZR(euid0, chroot("/")); break;
713b4844fa0SWilly Tarreau 		CASE_TEST(chroot_blah);       EXPECT_SYSER(1, chroot("/proc/self/blah"), -1, ENOENT); break;
7147172f1c6SWilly Tarreau 		CASE_TEST(chroot_exe);        EXPECT_SYSER(proc, chroot("/proc/self/exe"), -1, ENOTDIR); break;
715b4844fa0SWilly Tarreau 		CASE_TEST(close_m1);          EXPECT_SYSER(1, close(-1), -1, EBADF); break;
716b4844fa0SWilly Tarreau 		CASE_TEST(close_dup);         EXPECT_SYSZR(1, close(dup(0))); break;
717b4844fa0SWilly Tarreau 		CASE_TEST(dup_0);             tmp = dup(0);  EXPECT_SYSNE(1, tmp, -1); close(tmp); break;
718b4844fa0SWilly Tarreau 		CASE_TEST(dup_m1);            tmp = dup(-1); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break;
719b4844fa0SWilly Tarreau 		CASE_TEST(dup2_0);            tmp = dup2(0, 100);  EXPECT_SYSNE(1, tmp, -1); close(tmp); break;
720b4844fa0SWilly Tarreau 		CASE_TEST(dup2_m1);           tmp = dup2(-1, 100); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break;
721b4844fa0SWilly Tarreau 		CASE_TEST(dup3_0);            tmp = dup3(0, 100, 0);  EXPECT_SYSNE(1, tmp, -1); close(tmp); break;
722b4844fa0SWilly Tarreau 		CASE_TEST(dup3_m1);           tmp = dup3(-1, 100, 0); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break;
723b4844fa0SWilly Tarreau 		CASE_TEST(execve_root);       EXPECT_SYSER(1, execve("/", (char*[]){ [0] = "/", [1] = NULL }, NULL), -1, EACCES); break;
7243ad09d72SThomas Weißschuh 		CASE_TEST(fork);              EXPECT_SYSZR(1, test_fork()); break;
725b4844fa0SWilly Tarreau 		CASE_TEST(getdents64_root);   EXPECT_SYSNE(1, test_getdents64("/"), -1); break;
726b4844fa0SWilly Tarreau 		CASE_TEST(getdents64_null);   EXPECT_SYSER(1, test_getdents64("/dev/null"), -1, ENOTDIR); break;
727957bfa31SZhangjin Wu 		CASE_TEST(gettimeofday_tv);   EXPECT_SYSZR(1, gettimeofday(&tv, NULL)); break;
728957bfa31SZhangjin Wu 		CASE_TEST(gettimeofday_tv_tz);EXPECT_SYSZR(1, gettimeofday(&tv, &tz)); break;
729a290296aSAmmar Faizi 		CASE_TEST(getpagesize);       EXPECT_SYSZR(1, test_getpagesize()); break;
730b4844fa0SWilly Tarreau 		CASE_TEST(ioctl_tiocinq);     EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); break;
731b4844fa0SWilly Tarreau 		CASE_TEST(ioctl_tiocinq);     EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); break;
732b4844fa0SWilly Tarreau 		CASE_TEST(link_root1);        EXPECT_SYSER(1, link("/", "/"), -1, EEXIST); break;
733b4844fa0SWilly Tarreau 		CASE_TEST(link_blah);         EXPECT_SYSER(1, link("/proc/self/blah", "/blah"), -1, ENOENT); break;
7343e2d337bSWilly Tarreau 		CASE_TEST(link_dir);          EXPECT_SYSER(euid0, link("/", "/blah"), -1, EPERM); break;
735f7a419e3SZhangjin Wu 		CASE_TEST(link_cross);        EXPECT_SYSER(proc, link("/proc/self/cmdline", "/blah"), -1, EXDEV); break;
736b4844fa0SWilly Tarreau 		CASE_TEST(lseek_m1);          EXPECT_SYSER(1, lseek(-1, 0, SEEK_SET), -1, EBADF); break;
737b4844fa0SWilly Tarreau 		CASE_TEST(lseek_0);           EXPECT_SYSER(1, lseek(0, 0, SEEK_SET), -1, ESPIPE); break;
738b4844fa0SWilly Tarreau 		CASE_TEST(mkdir_root);        EXPECT_SYSER(1, mkdir("/", 0755), -1, EEXIST); break;
739d4a3b2b9SZhangjin Wu 		CASE_TEST(mmap_bad);          EXPECT_PTRER(1, mmap(NULL, 0, PROT_READ, MAP_PRIVATE, 0, 0), MAP_FAILED, EINVAL); break;
740ba3d0892SZhangjin Wu 		CASE_TEST(munmap_bad);        EXPECT_SYSER(1, munmap((void *)1, 0), -1, EINVAL); break;
741fcdbf5ddSZhangjin Wu 		CASE_TEST(mmap_munmap_good);  EXPECT_SYSZR(1, test_mmap_munmap()); break;
742b4844fa0SWilly Tarreau 		CASE_TEST(open_tty);          EXPECT_SYSNE(1, tmp = open("/dev/null", 0), -1); if (tmp != -1) close(tmp); break;
743b4844fa0SWilly Tarreau 		CASE_TEST(open_blah);         EXPECT_SYSER(1, tmp = open("/proc/self/blah", 0), -1, ENOENT); if (tmp != -1) close(tmp); break;
744b4844fa0SWilly Tarreau 		CASE_TEST(poll_null);         EXPECT_SYSZR(1, poll(NULL, 0, 0)); break;
745b4844fa0SWilly Tarreau 		CASE_TEST(poll_stdout);       EXPECT_SYSNE(1, ({ struct pollfd fds = { 1, POLLOUT, 0}; poll(&fds, 1, 0); }), -1); break;
746b4844fa0SWilly Tarreau 		CASE_TEST(poll_fault);        EXPECT_SYSER(1, poll((void *)1, 1, 0), -1, EFAULT); break;
747208aa9d9SThomas Weißschuh 		CASE_TEST(prctl);             EXPECT_SYSER(1, prctl(PR_SET_NAME, (unsigned long)NULL, 0, 0, 0), -1, EFAULT); break;
748b4844fa0SWilly Tarreau 		CASE_TEST(read_badf);         EXPECT_SYSER(1, read(-1, &tmp, 1), -1, EBADF); break;
7494e14e844SZhangjin Wu 		CASE_TEST(rmdir_blah);        EXPECT_SYSER(1, rmdir("/blah"), -1, ENOENT); break;
750b4844fa0SWilly Tarreau 		CASE_TEST(sched_yield);       EXPECT_SYSZR(1, sched_yield()); break;
751b4844fa0SWilly Tarreau 		CASE_TEST(select_null);       EXPECT_SYSZR(1, ({ struct timeval tv = { 0 }; select(0, NULL, NULL, NULL, &tv); })); break;
752b4844fa0SWilly 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;
753b4844fa0SWilly Tarreau 		CASE_TEST(select_fault);      EXPECT_SYSER(1, select(1, (void *)1, NULL, NULL, 0), -1, EFAULT); break;
754b4844fa0SWilly Tarreau 		CASE_TEST(stat_blah);         EXPECT_SYSER(1, stat("/proc/self/blah", &stat_buf), -1, ENOENT); break;
75546cf630cSZhangjin Wu 		CASE_TEST(stat_fault);        EXPECT_SYSER(1, stat((void *)1, &stat_buf), -1, EFAULT); break;
75687b9fa66SThomas Weißschuh 		CASE_TEST(stat_timestamps);   EXPECT_SYSZR(1, test_stat_timestamps()); break;
757b4844fa0SWilly Tarreau 		CASE_TEST(symlink_root);      EXPECT_SYSER(1, symlink("/", "/"), -1, EEXIST); break;
758b4844fa0SWilly Tarreau 		CASE_TEST(unlink_root);       EXPECT_SYSER(1, unlink("/"), -1, EISDIR); break;
759b4844fa0SWilly Tarreau 		CASE_TEST(unlink_blah);       EXPECT_SYSER(1, unlink("/proc/self/blah"), -1, ENOENT); break;
760b4844fa0SWilly Tarreau 		CASE_TEST(wait_child);        EXPECT_SYSER(1, wait(&tmp), -1, ECHILD); break;
761b4844fa0SWilly Tarreau 		CASE_TEST(waitpid_min);       EXPECT_SYSER(1, waitpid(INT_MIN, &tmp, WNOHANG), -1, ESRCH); break;
762b4844fa0SWilly Tarreau 		CASE_TEST(waitpid_child);     EXPECT_SYSER(1, waitpid(getpid(), &tmp, WNOHANG), -1, ECHILD); break;
763b4844fa0SWilly Tarreau 		CASE_TEST(write_badf);        EXPECT_SYSER(1, write(-1, &tmp, 1), -1, EBADF); break;
764b4844fa0SWilly Tarreau 		CASE_TEST(write_zero);        EXPECT_SYSZR(1, write(1, &tmp, 0)); break;
76553fcfafaSThomas Weißschuh 		CASE_TEST(syscall_noargs);    EXPECT_SYSEQ(1, syscall(__NR_getpid), getpid()); break;
766ec8e1b73SZhangjin Wu 		CASE_TEST(syscall_args);      EXPECT_SYSER(1, syscall(__NR_statx, 0, NULL, 0, 0, NULL), -1, EFAULT); break;
767b4844fa0SWilly Tarreau 		case __LINE__:
768b4844fa0SWilly Tarreau 			return ret; /* must be last */
769b4844fa0SWilly Tarreau 		/* note: do not set any defaults so as to permit holes above */
770b4844fa0SWilly Tarreau 		}
771b4844fa0SWilly Tarreau 	}
772b4844fa0SWilly Tarreau 	return ret;
773b4844fa0SWilly Tarreau }
774b4844fa0SWilly Tarreau 
77595bc9894SWilly Tarreau int run_stdlib(int min, int max)
77695bc9894SWilly Tarreau {
77795bc9894SWilly Tarreau 	int test;
77895bc9894SWilly Tarreau 	int tmp;
77995bc9894SWilly Tarreau 	int ret = 0;
78095bc9894SWilly Tarreau 	void *p1, *p2;
78195bc9894SWilly Tarreau 
78295bc9894SWilly Tarreau 	for (test = min; test >= 0 && test <= max; test++) {
783fddc8f81SThomas Weißschuh 		int llen = 0; /* line length */
78495bc9894SWilly Tarreau 
78595bc9894SWilly Tarreau 		/* avoid leaving empty lines below, this will insert holes into
78695bc9894SWilly Tarreau 		 * test numbers.
78795bc9894SWilly Tarreau 		 */
78895bc9894SWilly Tarreau 		switch (test + __LINE__ + 1) {
78995bc9894SWilly Tarreau 		CASE_TEST(getenv_TERM);        EXPECT_STRNZ(1, getenv("TERM")); break;
79095bc9894SWilly Tarreau 		CASE_TEST(getenv_blah);        EXPECT_STRZR(1, getenv("blah")); break;
79195bc9894SWilly Tarreau 		CASE_TEST(setcmp_blah_blah);   EXPECT_EQ(1, strcmp("blah", "blah"), 0); break;
79295bc9894SWilly Tarreau 		CASE_TEST(setcmp_blah_blah2);  EXPECT_NE(1, strcmp("blah", "blah2"), 0); break;
79395bc9894SWilly Tarreau 		CASE_TEST(setncmp_blah_blah);  EXPECT_EQ(1, strncmp("blah", "blah", 10), 0); break;
79495bc9894SWilly Tarreau 		CASE_TEST(setncmp_blah_blah4); EXPECT_EQ(1, strncmp("blah", "blah4", 4), 0); break;
79595bc9894SWilly Tarreau 		CASE_TEST(setncmp_blah_blah5); EXPECT_NE(1, strncmp("blah", "blah5", 5), 0); break;
79695bc9894SWilly Tarreau 		CASE_TEST(setncmp_blah_blah6); EXPECT_NE(1, strncmp("blah", "blah6", 6), 0); break;
79795bc9894SWilly Tarreau 		CASE_TEST(strchr_foobar_o);    EXPECT_STREQ(1, strchr("foobar", 'o'), "oobar"); break;
79895bc9894SWilly Tarreau 		CASE_TEST(strchr_foobar_z);    EXPECT_STRZR(1, strchr("foobar", 'z')); break;
79995bc9894SWilly Tarreau 		CASE_TEST(strrchr_foobar_o);   EXPECT_STREQ(1, strrchr("foobar", 'o'), "obar"); break;
80095bc9894SWilly Tarreau 		CASE_TEST(strrchr_foobar_z);   EXPECT_STRZR(1, strrchr("foobar", 'z')); break;
801c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_20_20);       EXPECT_EQ(1, memcmp("aaa\x20", "aaa\x20", 4), 0); break;
802c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_20_60);       EXPECT_LT(1, memcmp("aaa\x20", "aaa\x60", 4), 0); break;
803c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_60_20);       EXPECT_GT(1, memcmp("aaa\x60", "aaa\x20", 4), 0); break;
804c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_20_e0);       EXPECT_LT(1, memcmp("aaa\x20", "aaa\xe0", 4), 0); break;
805c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_e0_20);       EXPECT_GT(1, memcmp("aaa\xe0", "aaa\x20", 4), 0); break;
806c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_80_e0);       EXPECT_LT(1, memcmp("aaa\x80", "aaa\xe0", 4), 0); break;
807c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_e0_80);       EXPECT_GT(1, memcmp("aaa\xe0", "aaa\x80", 4), 0); break;
808d1209597SVincent Dagonneau 		CASE_TEST(limit_int8_max);          EXPECT_EQ(1, INT8_MAX,         (int8_t)          0x7f); break;
809d1209597SVincent Dagonneau 		CASE_TEST(limit_int8_min);          EXPECT_EQ(1, INT8_MIN,         (int8_t)          0x80); break;
810d1209597SVincent Dagonneau 		CASE_TEST(limit_uint8_max);         EXPECT_EQ(1, UINT8_MAX,        (uint8_t)         0xff); break;
811d1209597SVincent Dagonneau 		CASE_TEST(limit_int16_max);         EXPECT_EQ(1, INT16_MAX,        (int16_t)         0x7fff); break;
812d1209597SVincent Dagonneau 		CASE_TEST(limit_int16_min);         EXPECT_EQ(1, INT16_MIN,        (int16_t)         0x8000); break;
813d1209597SVincent Dagonneau 		CASE_TEST(limit_uint16_max);        EXPECT_EQ(1, UINT16_MAX,       (uint16_t)        0xffff); break;
814d1209597SVincent Dagonneau 		CASE_TEST(limit_int32_max);         EXPECT_EQ(1, INT32_MAX,        (int32_t)         0x7fffffff); break;
815d1209597SVincent Dagonneau 		CASE_TEST(limit_int32_min);         EXPECT_EQ(1, INT32_MIN,        (int32_t)         0x80000000); break;
816d1209597SVincent Dagonneau 		CASE_TEST(limit_uint32_max);        EXPECT_EQ(1, UINT32_MAX,       (uint32_t)        0xffffffff); break;
817d1209597SVincent Dagonneau 		CASE_TEST(limit_int64_max);         EXPECT_EQ(1, INT64_MAX,        (int64_t)         0x7fffffffffffffff); break;
818d1209597SVincent Dagonneau 		CASE_TEST(limit_int64_min);         EXPECT_EQ(1, INT64_MIN,        (int64_t)         0x8000000000000000); break;
819d1209597SVincent Dagonneau 		CASE_TEST(limit_uint64_max);        EXPECT_EQ(1, UINT64_MAX,       (uint64_t)        0xffffffffffffffff); break;
820d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least8_max);    EXPECT_EQ(1, INT_LEAST8_MAX,   (int_least8_t)    0x7f); break;
821d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least8_min);    EXPECT_EQ(1, INT_LEAST8_MIN,   (int_least8_t)    0x80); break;
822d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_least8_max);   EXPECT_EQ(1, UINT_LEAST8_MAX,  (uint_least8_t)   0xff); break;
823d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least16_max);   EXPECT_EQ(1, INT_LEAST16_MAX,  (int_least16_t)   0x7fff); break;
824d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least16_min);   EXPECT_EQ(1, INT_LEAST16_MIN,  (int_least16_t)   0x8000); break;
825d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_least16_max);  EXPECT_EQ(1, UINT_LEAST16_MAX, (uint_least16_t)  0xffff); break;
826d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least32_max);   EXPECT_EQ(1, INT_LEAST32_MAX,  (int_least32_t)   0x7fffffff); break;
827d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least32_min);   EXPECT_EQ(1, INT_LEAST32_MIN,  (int_least32_t)   0x80000000); break;
828d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_least32_max);  EXPECT_EQ(1, UINT_LEAST32_MAX, (uint_least32_t)  0xffffffffU); break;
829d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least64_min);   EXPECT_EQ(1, INT_LEAST64_MIN,  (int_least64_t)   0x8000000000000000LL); break;
830d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least64_max);   EXPECT_EQ(1, INT_LEAST64_MAX,  (int_least64_t)   0x7fffffffffffffffLL); break;
831d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_least64_max);  EXPECT_EQ(1, UINT_LEAST64_MAX, (uint_least64_t)  0xffffffffffffffffULL); break;
832d1209597SVincent Dagonneau 		CASE_TEST(limit_int_fast8_max);     EXPECT_EQ(1, INT_FAST8_MAX,    (int_fast8_t)     0x7f); break;
833d1209597SVincent Dagonneau 		CASE_TEST(limit_int_fast8_min);     EXPECT_EQ(1, INT_FAST8_MIN,    (int_fast8_t)     0x80); break;
834d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_fast8_max);    EXPECT_EQ(1, UINT_FAST8_MAX,   (uint_fast8_t)    0xff); break;
835989abf1cSZhangjin Wu 		CASE_TEST(limit_int_fast16_min);    EXPECT_EQ(1, INT_FAST16_MIN,   (int_fast16_t)    SINT_MIN_OF_TYPE(int_fast16_t)); break;
836989abf1cSZhangjin Wu 		CASE_TEST(limit_int_fast16_max);    EXPECT_EQ(1, INT_FAST16_MAX,   (int_fast16_t)    SINT_MAX_OF_TYPE(int_fast16_t)); break;
837d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_fast16_max);   EXPECT_EQ(1, UINT_FAST16_MAX,  (uint_fast16_t)   UINTPTR_MAX); break;
838989abf1cSZhangjin Wu 		CASE_TEST(limit_int_fast32_min);    EXPECT_EQ(1, INT_FAST32_MIN,   (int_fast32_t)    SINT_MIN_OF_TYPE(int_fast32_t)); break;
839989abf1cSZhangjin Wu 		CASE_TEST(limit_int_fast32_max);    EXPECT_EQ(1, INT_FAST32_MAX,   (int_fast32_t)    SINT_MAX_OF_TYPE(int_fast32_t)); break;
840d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_fast32_max);   EXPECT_EQ(1, UINT_FAST32_MAX,  (uint_fast32_t)   UINTPTR_MAX); break;
841f9bf5944SThomas Weißschuh 		CASE_TEST(limit_int_fast64_min);    EXPECT_EQ(1, INT_FAST64_MIN,   (int_fast64_t)    INT64_MIN); break;
842f9bf5944SThomas Weißschuh 		CASE_TEST(limit_int_fast64_max);    EXPECT_EQ(1, INT_FAST64_MAX,   (int_fast64_t)    INT64_MAX); break;
843f9bf5944SThomas Weißschuh 		CASE_TEST(limit_uint_fast64_max);   EXPECT_EQ(1, UINT_FAST64_MAX,  (uint_fast64_t)   UINT64_MAX); break;
844d1209597SVincent Dagonneau #if __SIZEOF_LONG__ == 8
845d1209597SVincent Dagonneau 		CASE_TEST(limit_intptr_min);        EXPECT_EQ(1, INTPTR_MIN,       (intptr_t)        0x8000000000000000LL); break;
846d1209597SVincent Dagonneau 		CASE_TEST(limit_intptr_max);        EXPECT_EQ(1, INTPTR_MAX,       (intptr_t)        0x7fffffffffffffffLL); break;
847d1209597SVincent Dagonneau 		CASE_TEST(limit_uintptr_max);       EXPECT_EQ(1, UINTPTR_MAX,      (uintptr_t)       0xffffffffffffffffULL); break;
848d1209597SVincent Dagonneau 		CASE_TEST(limit_ptrdiff_min);       EXPECT_EQ(1, PTRDIFF_MIN,      (ptrdiff_t)       0x8000000000000000LL); break;
849d1209597SVincent Dagonneau 		CASE_TEST(limit_ptrdiff_max);       EXPECT_EQ(1, PTRDIFF_MAX,      (ptrdiff_t)       0x7fffffffffffffffLL); break;
850d1209597SVincent Dagonneau 		CASE_TEST(limit_size_max);          EXPECT_EQ(1, SIZE_MAX,         (size_t)          0xffffffffffffffffULL); break;
851d1209597SVincent Dagonneau #elif __SIZEOF_LONG__ == 4
852d1209597SVincent Dagonneau 		CASE_TEST(limit_intptr_min);        EXPECT_EQ(1, INTPTR_MIN,       (intptr_t)        0x80000000); break;
853d1209597SVincent Dagonneau 		CASE_TEST(limit_intptr_max);        EXPECT_EQ(1, INTPTR_MAX,       (intptr_t)        0x7fffffff); break;
854d1209597SVincent Dagonneau 		CASE_TEST(limit_uintptr_max);       EXPECT_EQ(1, UINTPTR_MAX,      (uintptr_t)       0xffffffffU); break;
855d1209597SVincent Dagonneau 		CASE_TEST(limit_ptrdiff_min);       EXPECT_EQ(1, PTRDIFF_MIN,      (ptrdiff_t)       0x80000000); break;
856d1209597SVincent Dagonneau 		CASE_TEST(limit_ptrdiff_max);       EXPECT_EQ(1, PTRDIFF_MAX,      (ptrdiff_t)       0x7fffffff); break;
857d1209597SVincent Dagonneau 		CASE_TEST(limit_size_max);          EXPECT_EQ(1, SIZE_MAX,         (size_t)          0xffffffffU); break;
858d1209597SVincent Dagonneau #else
859d1209597SVincent Dagonneau # warning "__SIZEOF_LONG__ is undefined"
860d1209597SVincent Dagonneau #endif /* __SIZEOF_LONG__ */
86195bc9894SWilly Tarreau 		case __LINE__:
86295bc9894SWilly Tarreau 			return ret; /* must be last */
86395bc9894SWilly Tarreau 		/* note: do not set any defaults so as to permit holes above */
86495bc9894SWilly Tarreau 		}
86595bc9894SWilly Tarreau 	}
86695bc9894SWilly Tarreau 	return ret;
86795bc9894SWilly Tarreau }
86895bc9894SWilly Tarreau 
86969f2cd9fSThomas Weißschuh #define EXPECT_VFPRINTF(c, expected, fmt, ...)				\
87069f2cd9fSThomas Weißschuh 	ret += expect_vfprintf(llen, c, expected, fmt, ##__VA_ARGS__)
87169f2cd9fSThomas Weißschuh 
87269f2cd9fSThomas Weißschuh static int expect_vfprintf(int llen, size_t c, const char *expected, const char *fmt, ...)
87369f2cd9fSThomas Weißschuh {
87469f2cd9fSThomas Weißschuh 	int ret, fd, w, r;
87569f2cd9fSThomas Weißschuh 	char buf[100];
87669f2cd9fSThomas Weißschuh 	FILE *memfile;
87769f2cd9fSThomas Weißschuh 	va_list args;
87869f2cd9fSThomas Weißschuh 
87969f2cd9fSThomas Weißschuh 	fd = memfd_create("vfprintf", 0);
88069f2cd9fSThomas Weißschuh 	if (fd == -1) {
88169f2cd9fSThomas Weißschuh 		pad_spc(llen, 64, "[FAIL]\n");
88269f2cd9fSThomas Weißschuh 		return 1;
88369f2cd9fSThomas Weißschuh 	}
88469f2cd9fSThomas Weißschuh 
88569f2cd9fSThomas Weißschuh 	memfile = fdopen(fd, "w+");
88669f2cd9fSThomas Weißschuh 	if (!memfile) {
88769f2cd9fSThomas Weißschuh 		pad_spc(llen, 64, "[FAIL]\n");
88869f2cd9fSThomas Weißschuh 		return 1;
88969f2cd9fSThomas Weißschuh 	}
89069f2cd9fSThomas Weißschuh 
89169f2cd9fSThomas Weißschuh 	va_start(args, fmt);
89269f2cd9fSThomas Weißschuh 	w = vfprintf(memfile, fmt, args);
89369f2cd9fSThomas Weißschuh 	va_end(args);
89469f2cd9fSThomas Weißschuh 
89569f2cd9fSThomas Weißschuh 	if (w != c) {
89669f2cd9fSThomas Weißschuh 		llen += printf(" written(%d) != %d", w, (int) c);
89769f2cd9fSThomas Weißschuh 		pad_spc(llen, 64, "[FAIL]\n");
89869f2cd9fSThomas Weißschuh 		return 1;
89969f2cd9fSThomas Weißschuh 	}
90069f2cd9fSThomas Weißschuh 
90169f2cd9fSThomas Weißschuh 	fflush(memfile);
90269f2cd9fSThomas Weißschuh 	lseek(fd, 0, SEEK_SET);
90369f2cd9fSThomas Weißschuh 
90469f2cd9fSThomas Weißschuh 	r = read(fd, buf, sizeof(buf) - 1);
90569f2cd9fSThomas Weißschuh 	buf[r] = '\0';
90669f2cd9fSThomas Weißschuh 
90769f2cd9fSThomas Weißschuh 	fclose(memfile);
90869f2cd9fSThomas Weißschuh 
90969f2cd9fSThomas Weißschuh 	if (r != w) {
91069f2cd9fSThomas Weißschuh 		llen += printf(" written(%d) != read(%d)", w, r);
91169f2cd9fSThomas Weißschuh 		pad_spc(llen, 64, "[FAIL]\n");
91269f2cd9fSThomas Weißschuh 		return 1;
91369f2cd9fSThomas Weißschuh 	}
91469f2cd9fSThomas Weißschuh 
91569f2cd9fSThomas Weißschuh 	llen += printf(" \"%s\" = \"%s\"", expected, buf);
91669f2cd9fSThomas Weißschuh 	ret = strncmp(expected, buf, c);
91769f2cd9fSThomas Weißschuh 
91869f2cd9fSThomas Weißschuh 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
91969f2cd9fSThomas Weißschuh 	return ret;
92069f2cd9fSThomas Weißschuh }
92169f2cd9fSThomas Weißschuh 
92269f2cd9fSThomas Weißschuh static int run_vfprintf(int min, int max)
92369f2cd9fSThomas Weißschuh {
92469f2cd9fSThomas Weißschuh 	int test;
92569f2cd9fSThomas Weißschuh 	int tmp;
92669f2cd9fSThomas Weißschuh 	int ret = 0;
92769f2cd9fSThomas Weißschuh 	void *p1, *p2;
92869f2cd9fSThomas Weißschuh 
92969f2cd9fSThomas Weißschuh 	for (test = min; test >= 0 && test <= max; test++) {
930fddc8f81SThomas Weißschuh 		int llen = 0; /* line length */
93169f2cd9fSThomas Weißschuh 
93269f2cd9fSThomas Weißschuh 		/* avoid leaving empty lines below, this will insert holes into
93369f2cd9fSThomas Weißschuh 		 * test numbers.
93469f2cd9fSThomas Weißschuh 		 */
93569f2cd9fSThomas Weißschuh 		switch (test + __LINE__ + 1) {
93669f2cd9fSThomas Weißschuh 		CASE_TEST(empty);        EXPECT_VFPRINTF(0, "", ""); break;
93769f2cd9fSThomas Weißschuh 		CASE_TEST(simple);       EXPECT_VFPRINTF(3, "foo", "foo"); break;
93869f2cd9fSThomas Weißschuh 		CASE_TEST(string);       EXPECT_VFPRINTF(3, "foo", "%s", "foo"); break;
93969f2cd9fSThomas Weißschuh 		CASE_TEST(number);       EXPECT_VFPRINTF(4, "1234", "%d", 1234); break;
94069f2cd9fSThomas Weißschuh 		CASE_TEST(negnumber);    EXPECT_VFPRINTF(5, "-1234", "%d", -1234); break;
94169f2cd9fSThomas Weißschuh 		CASE_TEST(unsigned);     EXPECT_VFPRINTF(5, "12345", "%u", 12345); break;
94269f2cd9fSThomas Weißschuh 		CASE_TEST(char);         EXPECT_VFPRINTF(1, "c", "%c", 'c'); break;
94369f2cd9fSThomas Weißschuh 		CASE_TEST(hex);          EXPECT_VFPRINTF(1, "f", "%x", 0xf); break;
94469f2cd9fSThomas Weißschuh 		CASE_TEST(pointer);      EXPECT_VFPRINTF(3, "0x1", "%p", (void *) 0x1); break;
94569f2cd9fSThomas Weißschuh 		case __LINE__:
94669f2cd9fSThomas Weißschuh 			return ret; /* must be last */
94769f2cd9fSThomas Weißschuh 		/* note: do not set any defaults so as to permit holes above */
94869f2cd9fSThomas Weißschuh 		}
94969f2cd9fSThomas Weißschuh 	}
95069f2cd9fSThomas Weißschuh 	return ret;
95169f2cd9fSThomas Weißschuh }
95269f2cd9fSThomas Weißschuh 
95397357168SThomas Weißschuh static int smash_stack(void)
95497357168SThomas Weißschuh {
95597357168SThomas Weißschuh 	char buf[100];
956e7654c3fSThomas Weißschuh 	volatile char *ptr = buf;
957aa662d12SWilly Tarreau 	size_t i;
95897357168SThomas Weißschuh 
959aa662d12SWilly Tarreau 	for (i = 0; i < 200; i++)
960e7654c3fSThomas Weißschuh 		ptr[i] = 'P';
96197357168SThomas Weißschuh 
96297357168SThomas Weißschuh 	return 1;
96397357168SThomas Weißschuh }
96497357168SThomas Weißschuh 
96597357168SThomas Weißschuh static int run_protection(int min, int max)
96697357168SThomas Weißschuh {
96797357168SThomas Weißschuh 	pid_t pid;
96897357168SThomas Weißschuh 	int llen = 0, status;
96997357168SThomas Weißschuh 
97097357168SThomas Weißschuh 	llen += printf("0 -fstackprotector ");
97197357168SThomas Weißschuh 
972818924d1SThomas Weißschuh #if !defined(_NOLIBC_STACKPROTECTOR)
97397357168SThomas Weißschuh 	llen += printf("not supported");
97497357168SThomas Weißschuh 	pad_spc(llen, 64, "[SKIPPED]\n");
97597357168SThomas Weißschuh 	return 0;
97697357168SThomas Weißschuh #endif
97797357168SThomas Weißschuh 
978818924d1SThomas Weißschuh #if defined(_NOLIBC_STACKPROTECTOR)
97985250921SThomas Weißschuh 	if (!__stack_chk_guard) {
98085250921SThomas Weißschuh 		llen += printf("__stack_chk_guard not initialized");
98185250921SThomas Weißschuh 		pad_spc(llen, 64, "[FAIL]\n");
98285250921SThomas Weißschuh 		return 1;
98385250921SThomas Weißschuh 	}
98485250921SThomas Weißschuh #endif
98585250921SThomas Weißschuh 
98697357168SThomas Weißschuh 	pid = -1;
98797357168SThomas Weißschuh 	pid = fork();
98897357168SThomas Weißschuh 
98997357168SThomas Weißschuh 	switch (pid) {
99097357168SThomas Weißschuh 	case -1:
99197357168SThomas Weißschuh 		llen += printf("fork()");
99297357168SThomas Weißschuh 		pad_spc(llen, 64, "[FAIL]\n");
99397357168SThomas Weißschuh 		return 1;
99497357168SThomas Weißschuh 
99597357168SThomas Weißschuh 	case 0:
99697357168SThomas Weißschuh 		close(STDOUT_FILENO);
99797357168SThomas Weißschuh 		close(STDERR_FILENO);
99897357168SThomas Weißschuh 
9999a75575bSThomas Weißschuh 		prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
100097357168SThomas Weißschuh 		smash_stack();
100197357168SThomas Weißschuh 		return 1;
100297357168SThomas Weißschuh 
100397357168SThomas Weißschuh 	default:
100497357168SThomas Weißschuh 		pid = waitpid(pid, &status, 0);
100597357168SThomas Weißschuh 
100697357168SThomas Weißschuh 		if (pid == -1 || !WIFSIGNALED(status) || WTERMSIG(status) != SIGABRT) {
100797357168SThomas Weißschuh 			llen += printf("waitpid()");
100897357168SThomas Weißschuh 			pad_spc(llen, 64, "[FAIL]\n");
100997357168SThomas Weißschuh 			return 1;
101097357168SThomas Weißschuh 		}
101197357168SThomas Weißschuh 		pad_spc(llen, 64, " [OK]\n");
101297357168SThomas Weißschuh 		return 0;
101397357168SThomas Weißschuh 	}
101497357168SThomas Weißschuh }
101597357168SThomas Weißschuh 
10161a5454f6SWilly Tarreau /* prepare what needs to be prepared for pid 1 (stdio, /dev, /proc, etc) */
10171a5454f6SWilly Tarreau int prepare(void)
10181a5454f6SWilly Tarreau {
10191a5454f6SWilly Tarreau 	struct stat stat_buf;
10201a5454f6SWilly Tarreau 
10211a5454f6SWilly Tarreau 	/* It's possible that /dev doesn't even exist or was not mounted, so
10221a5454f6SWilly Tarreau 	 * we'll try to create it, mount it, or create minimal entries into it.
10231a5454f6SWilly Tarreau 	 * We want at least /dev/null and /dev/console.
10241a5454f6SWilly Tarreau 	 */
10251a5454f6SWilly Tarreau 	if (stat("/dev/.", &stat_buf) == 0 || mkdir("/dev", 0755) == 0) {
10261a5454f6SWilly Tarreau 		if (stat("/dev/console", &stat_buf) != 0 ||
102782e339c2SZhangjin Wu 		    stat("/dev/null", &stat_buf) != 0 ||
102882e339c2SZhangjin Wu 		    stat("/dev/zero", &stat_buf) != 0) {
10291a5454f6SWilly Tarreau 			/* try devtmpfs first, otherwise fall back to manual creation */
10301a5454f6SWilly Tarreau 			if (mount("/dev", "/dev", "devtmpfs", 0, 0) != 0) {
10311a5454f6SWilly Tarreau 				mknod("/dev/console", 0600 | S_IFCHR, makedev(5, 1));
10321a5454f6SWilly Tarreau 				mknod("/dev/null",    0666 | S_IFCHR, makedev(1, 3));
103382e339c2SZhangjin Wu 				mknod("/dev/zero",    0666 | S_IFCHR, makedev(1, 5));
10341a5454f6SWilly Tarreau 			}
10351a5454f6SWilly Tarreau 		}
10361a5454f6SWilly Tarreau 	}
10371a5454f6SWilly Tarreau 
10381a5454f6SWilly Tarreau 	/* If no /dev/console was found before calling init, stdio is closed so
10391a5454f6SWilly Tarreau 	 * we need to reopen it from /dev/console. If it failed above, it will
10401a5454f6SWilly Tarreau 	 * still fail here and we cannot emit a message anyway.
10411a5454f6SWilly Tarreau 	 */
10421a5454f6SWilly Tarreau 	if (close(dup(1)) == -1) {
10431a5454f6SWilly Tarreau 		int fd = open("/dev/console", O_RDWR);
10441a5454f6SWilly Tarreau 
10451a5454f6SWilly Tarreau 		if (fd >= 0) {
10461a5454f6SWilly Tarreau 			if (fd != 0)
10471a5454f6SWilly Tarreau 				dup2(fd, 0);
10481a5454f6SWilly Tarreau 			if (fd != 1)
10491a5454f6SWilly Tarreau 				dup2(fd, 1);
10501a5454f6SWilly Tarreau 			if (fd != 2)
10511a5454f6SWilly Tarreau 				dup2(fd, 2);
10521a5454f6SWilly Tarreau 			if (fd > 2)
10531a5454f6SWilly Tarreau 				close(fd);
10541a5454f6SWilly Tarreau 			puts("\nSuccessfully reopened /dev/console.");
10551a5454f6SWilly Tarreau 		}
10561a5454f6SWilly Tarreau 	}
10571a5454f6SWilly Tarreau 
10581a5454f6SWilly Tarreau 	/* try to mount /proc if not mounted. Silently fail otherwise */
10591a5454f6SWilly Tarreau 	if (stat("/proc/.", &stat_buf) == 0 || mkdir("/proc", 0755) == 0) {
1060b8b26108SZhangjin Wu 		if (stat("/proc/self", &stat_buf) != 0) {
1061b8b26108SZhangjin Wu 			/* If not mountable, remove /proc completely to avoid misuse */
1062b8b26108SZhangjin Wu 			if (mount("none", "/proc", "proc", 0, 0) != 0)
1063b8b26108SZhangjin Wu 				rmdir("/proc");
1064b8b26108SZhangjin Wu 		}
10651a5454f6SWilly Tarreau 	}
10661a5454f6SWilly Tarreau 
1067*bbb14546SZhangjin Wu 	/* some tests rely on a writable /tmp */
1068*bbb14546SZhangjin Wu 	mkdir("/tmp", 0755);
1069*bbb14546SZhangjin Wu 
10701a5454f6SWilly Tarreau 	return 0;
10711a5454f6SWilly Tarreau }
1072b4844fa0SWilly Tarreau 
107323da7bc9SWilly Tarreau /* This is the definition of known test names, with their functions */
1074c4560bd8SThomas Weißschuh static const struct test test_names[] = {
107523da7bc9SWilly Tarreau 	/* add new tests here */
1076b4844fa0SWilly Tarreau 	{ .name = "syscall",    .func = run_syscall    },
107795bc9894SWilly Tarreau 	{ .name = "stdlib",     .func = run_stdlib     },
107869f2cd9fSThomas Weißschuh 	{ .name = "vfprintf",   .func = run_vfprintf   },
107997357168SThomas Weißschuh 	{ .name = "protection", .func = run_protection },
108023da7bc9SWilly Tarreau 	{ 0 }
108123da7bc9SWilly Tarreau };
108223da7bc9SWilly Tarreau 
1083c388c992SZhangjin Wu int is_setting_valid(char *test)
1084c388c992SZhangjin Wu {
1085c388c992SZhangjin Wu 	int idx, len, test_len, valid = 0;
1086c388c992SZhangjin Wu 	char delimiter;
1087c388c992SZhangjin Wu 
1088c388c992SZhangjin Wu 	if (!test)
1089c388c992SZhangjin Wu 		return valid;
1090c388c992SZhangjin Wu 
1091c388c992SZhangjin Wu 	test_len = strlen(test);
1092c388c992SZhangjin Wu 
1093c388c992SZhangjin Wu 	for (idx = 0; test_names[idx].name; idx++) {
1094c388c992SZhangjin Wu 		len = strlen(test_names[idx].name);
1095c388c992SZhangjin Wu 		if (test_len < len)
1096c388c992SZhangjin Wu 			continue;
1097c388c992SZhangjin Wu 
1098c388c992SZhangjin Wu 		if (strncmp(test, test_names[idx].name, len) != 0)
1099c388c992SZhangjin Wu 			continue;
1100c388c992SZhangjin Wu 
1101c388c992SZhangjin Wu 		delimiter = test[len];
1102c388c992SZhangjin Wu 		if (delimiter != ':' && delimiter != ',' && delimiter != '\0')
1103c388c992SZhangjin Wu 			continue;
1104c388c992SZhangjin Wu 
1105c388c992SZhangjin Wu 		valid = 1;
1106c388c992SZhangjin Wu 		break;
1107c388c992SZhangjin Wu 	}
1108c388c992SZhangjin Wu 
1109c388c992SZhangjin Wu 	return valid;
1110c388c992SZhangjin Wu }
1111c388c992SZhangjin Wu 
1112362aecb2SWilly Tarreau int main(int argc, char **argv, char **envp)
1113362aecb2SWilly Tarreau {
1114362aecb2SWilly Tarreau 	int min = 0;
1115a36cfc5eSZhangjin Wu 	int max = INT_MAX;
1116362aecb2SWilly Tarreau 	int ret = 0;
111723da7bc9SWilly Tarreau 	int err;
111823da7bc9SWilly Tarreau 	int idx;
111923da7bc9SWilly Tarreau 	char *test;
1120362aecb2SWilly Tarreau 
1121938b5b98SZhangjin Wu 	argv0 = argv[0];
1122362aecb2SWilly Tarreau 	environ = envp;
1123362aecb2SWilly Tarreau 
11241a5454f6SWilly Tarreau 	/* when called as init, it's possible that no console was opened, for
11251a5454f6SWilly Tarreau 	 * example if no /dev file system was provided. We'll check that fd#1
11261a5454f6SWilly Tarreau 	 * was opened, and if not we'll attempt to create and open /dev/console
11271a5454f6SWilly Tarreau 	 * and /dev/null that we'll use for later tests.
11281a5454f6SWilly Tarreau 	 */
11291a5454f6SWilly Tarreau 	if (getpid() == 1)
11301a5454f6SWilly Tarreau 		prepare();
11311a5454f6SWilly Tarreau 
113223da7bc9SWilly Tarreau 	/* the definition of a series of tests comes from either argv[1] or the
113323da7bc9SWilly Tarreau 	 * "NOLIBC_TEST" environment variable. It's made of a comma-delimited
113423da7bc9SWilly Tarreau 	 * series of test names and optional ranges:
113523da7bc9SWilly Tarreau 	 *    syscall:5-15[:.*],stdlib:8-10
113623da7bc9SWilly Tarreau 	 */
113723da7bc9SWilly Tarreau 	test = argv[1];
1138c388c992SZhangjin Wu 	if (!is_setting_valid(test))
113923da7bc9SWilly Tarreau 		test = getenv("NOLIBC_TEST");
114023da7bc9SWilly Tarreau 
1141c388c992SZhangjin Wu 	if (is_setting_valid(test)) {
114223da7bc9SWilly Tarreau 		char *comma, *colon, *dash, *value;
114323da7bc9SWilly Tarreau 
114423da7bc9SWilly Tarreau 		do {
114523da7bc9SWilly Tarreau 			comma = strchr(test, ',');
114623da7bc9SWilly Tarreau 			if (comma)
114723da7bc9SWilly Tarreau 				*(comma++) = '\0';
114823da7bc9SWilly Tarreau 
114923da7bc9SWilly Tarreau 			colon = strchr(test, ':');
115023da7bc9SWilly Tarreau 			if (colon)
115123da7bc9SWilly Tarreau 				*(colon++) = '\0';
115223da7bc9SWilly Tarreau 
115323da7bc9SWilly Tarreau 			for (idx = 0; test_names[idx].name; idx++) {
115423da7bc9SWilly Tarreau 				if (strcmp(test, test_names[idx].name) == 0)
115523da7bc9SWilly Tarreau 					break;
115623da7bc9SWilly Tarreau 			}
115723da7bc9SWilly Tarreau 
115823da7bc9SWilly Tarreau 			if (test_names[idx].name) {
115923da7bc9SWilly Tarreau 				/* The test was named, it will be called at least
116023da7bc9SWilly Tarreau 				 * once. We may have an optional range at <colon>
116123da7bc9SWilly Tarreau 				 * here, which defaults to the full range.
116223da7bc9SWilly Tarreau 				 */
116323da7bc9SWilly Tarreau 				do {
1164a36cfc5eSZhangjin Wu 					min = 0; max = INT_MAX;
116523da7bc9SWilly Tarreau 					value = colon;
116623da7bc9SWilly Tarreau 					if (value && *value) {
116723da7bc9SWilly Tarreau 						colon = strchr(value, ':');
116823da7bc9SWilly Tarreau 						if (colon)
116923da7bc9SWilly Tarreau 							*(colon++) = '\0';
117023da7bc9SWilly Tarreau 
117123da7bc9SWilly Tarreau 						dash = strchr(value, '-');
117223da7bc9SWilly Tarreau 						if (dash)
117323da7bc9SWilly Tarreau 							*(dash++) = '\0';
117423da7bc9SWilly Tarreau 
117523da7bc9SWilly Tarreau 						/* support :val: :min-max: :min-: :-max: */
117623da7bc9SWilly Tarreau 						if (*value)
117723da7bc9SWilly Tarreau 							min = atoi(value);
117823da7bc9SWilly Tarreau 						if (!dash)
117923da7bc9SWilly Tarreau 							max = min;
118023da7bc9SWilly Tarreau 						else if (*dash)
118123da7bc9SWilly Tarreau 							max = atoi(dash);
118223da7bc9SWilly Tarreau 
118323da7bc9SWilly Tarreau 						value = colon;
118423da7bc9SWilly Tarreau 					}
118523da7bc9SWilly Tarreau 
118623da7bc9SWilly Tarreau 					/* now's time to call the test */
118723da7bc9SWilly Tarreau 					printf("Running test '%s'\n", test_names[idx].name);
118823da7bc9SWilly Tarreau 					err = test_names[idx].func(min, max);
118923da7bc9SWilly Tarreau 					ret += err;
119023da7bc9SWilly Tarreau 					printf("Errors during this test: %d\n\n", err);
119123da7bc9SWilly Tarreau 				} while (colon && *colon);
119223da7bc9SWilly Tarreau 			} else
119323da7bc9SWilly Tarreau 				printf("Ignoring unknown test name '%s'\n", test);
119423da7bc9SWilly Tarreau 
119523da7bc9SWilly Tarreau 			test = comma;
119623da7bc9SWilly Tarreau 		} while (test && *test);
119723da7bc9SWilly Tarreau 	} else {
119823da7bc9SWilly Tarreau 		/* no test mentioned, run everything */
119923da7bc9SWilly Tarreau 		for (idx = 0; test_names[idx].name; idx++) {
120023da7bc9SWilly Tarreau 			printf("Running test '%s'\n", test_names[idx].name);
120123da7bc9SWilly Tarreau 			err = test_names[idx].func(min, max);
120223da7bc9SWilly Tarreau 			ret += err;
120323da7bc9SWilly Tarreau 			printf("Errors during this test: %d\n\n", err);
120423da7bc9SWilly Tarreau 		}
120523da7bc9SWilly Tarreau 	}
120623da7bc9SWilly Tarreau 
1207362aecb2SWilly Tarreau 	printf("Total number of errors: %d\n", ret);
1208f49896d7SWilly Tarreau 
1209f49896d7SWilly Tarreau 	if (getpid() == 1) {
1210f49896d7SWilly Tarreau 		/* we're running as init, there's no other process on the
1211f49896d7SWilly Tarreau 		 * system, thus likely started from a VM for a quick check.
1212f49896d7SWilly Tarreau 		 * Exiting will provoke a kernel panic that may be reported
1213f49896d7SWilly Tarreau 		 * as an error by Qemu or the hypervisor, while stopping
1214f49896d7SWilly Tarreau 		 * cleanly will often be reported as a success. This allows
1215f49896d7SWilly Tarreau 		 * to use the output of this program for bisecting kernels.
1216f49896d7SWilly Tarreau 		 */
1217f49896d7SWilly Tarreau 		printf("Leaving init with final status: %d\n", !!ret);
1218f49896d7SWilly Tarreau 		if (ret == 0)
1219950add28SZhangjin Wu 			reboot(RB_POWER_OFF);
1220aa73a86cSWilly Tarreau #if defined(__x86_64__)
1221aa73a86cSWilly Tarreau 		/* QEMU started with "-device isa-debug-exit -no-reboot" will
1222aa73a86cSWilly Tarreau 		 * exit with status code 2N+1 when N is written to 0x501. We
1223aa73a86cSWilly Tarreau 		 * hard-code the syscall here as it's arch-dependent.
1224aa73a86cSWilly Tarreau 		 */
122567eb617aSThomas Weißschuh 		else if (syscall(__NR_ioperm, 0x501, 1, 1) == 0)
12267f291cfaSThomas Weißschuh 			__asm__ volatile ("outb %%al, %%dx" :: "d"(0x501), "a"(0));
1227aa73a86cSWilly Tarreau 		/* if it does nothing, fall back to the regular panic */
1228aa73a86cSWilly Tarreau #endif
1229f49896d7SWilly Tarreau 	}
1230f49896d7SWilly Tarreau 
1231362aecb2SWilly Tarreau 	printf("Exiting with status %d\n", !!ret);
1232362aecb2SWilly Tarreau 	return !!ret;
1233362aecb2SWilly Tarreau }
1234