xref: /openbmc/linux/tools/testing/selftests/nolibc/nolibc-test.c (revision 447e56023fc281c588e4977add552f4d49d78b22)
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 
154*447e5602SThomas 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 {
639a290296aSAmmar Faizi 	long 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)
729fcdbf5ddSZhangjin 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)
740fcdbf5ddSZhangjin Wu 		return ret;
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) {
760fcdbf5ddSZhangjin 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);
768fcdbf5ddSZhangjin Wu 	return ret;
769fcdbf5ddSZhangjin Wu }
770fcdbf5ddSZhangjin Wu 
771fcdbf5ddSZhangjin Wu 
772b4844fa0SWilly Tarreau /* Run syscall tests between IDs <min> and <max>.
773b4844fa0SWilly Tarreau  * Return 0 on success, non-zero on failure.
774b4844fa0SWilly Tarreau  */
775b4844fa0SWilly Tarreau int run_syscall(int min, int max)
776b4844fa0SWilly Tarreau {
777957bfa31SZhangjin Wu 	struct timeval tv;
778957bfa31SZhangjin Wu 	struct timezone tz;
779b4844fa0SWilly Tarreau 	struct stat stat_buf;
7803e2d337bSWilly Tarreau 	int euid0;
7817172f1c6SWilly Tarreau 	int proc;
782b4844fa0SWilly Tarreau 	int test;
783b4844fa0SWilly Tarreau 	int tmp;
784b4844fa0SWilly Tarreau 	int ret = 0;
785b4844fa0SWilly Tarreau 	void *p1, *p2;
78679b4f68eSZhangjin Wu 	int has_gettid = 1;
787b4844fa0SWilly Tarreau 
7887172f1c6SWilly Tarreau 	/* <proc> indicates whether or not /proc is mounted */
7897172f1c6SWilly Tarreau 	proc = stat("/proc", &stat_buf) == 0;
7907172f1c6SWilly Tarreau 
7913e2d337bSWilly Tarreau 	/* this will be used to skip certain tests that can't be run unprivileged */
7923e2d337bSWilly Tarreau 	euid0 = geteuid() == 0;
7933e2d337bSWilly Tarreau 
79479b4f68eSZhangjin Wu 	/* from 2.30, glibc provides gettid() */
79579b4f68eSZhangjin Wu #if defined(__GLIBC_MINOR__) && defined(__GLIBC__)
79679b4f68eSZhangjin Wu 	has_gettid = __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 30);
79779b4f68eSZhangjin Wu #endif
79879b4f68eSZhangjin Wu 
799b4844fa0SWilly Tarreau 	for (test = min; test >= 0 && test <= max; test++) {
800fddc8f81SThomas Weißschuh 		int llen = 0; /* line length */
801b4844fa0SWilly Tarreau 
802b4844fa0SWilly Tarreau 		/* avoid leaving empty lines below, this will insert holes into
803b4844fa0SWilly Tarreau 		 * test numbers.
804b4844fa0SWilly Tarreau 		 */
805b4844fa0SWilly Tarreau 		switch (test + __LINE__ + 1) {
806b4844fa0SWilly Tarreau 		CASE_TEST(getpid);            EXPECT_SYSNE(1, getpid(), -1); break;
807b4844fa0SWilly Tarreau 		CASE_TEST(getppid);           EXPECT_SYSNE(1, getppid(), -1); break;
80879b4f68eSZhangjin Wu 		CASE_TEST(gettid);            EXPECT_SYSNE(has_gettid, gettid(), -1); break;
809b4844fa0SWilly Tarreau 		CASE_TEST(getpgid_self);      EXPECT_SYSNE(1, getpgid(0), -1); break;
810b4844fa0SWilly Tarreau 		CASE_TEST(getpgid_bad);       EXPECT_SYSER(1, getpgid(-1), -1, ESRCH); break;
811b4844fa0SWilly Tarreau 		CASE_TEST(kill_0);            EXPECT_SYSZR(1, kill(getpid(), 0)); break;
812b4844fa0SWilly Tarreau 		CASE_TEST(kill_CONT);         EXPECT_SYSZR(1, kill(getpid(), 0)); break;
813b4844fa0SWilly Tarreau 		CASE_TEST(kill_BADPID);       EXPECT_SYSER(1, kill(INT_MAX, 0), -1, ESRCH); break;
814f193ecbfSZhangjin Wu 		CASE_TEST(sbrk_0);            EXPECT_PTRNE(1, sbrk(0), (void *)-1); break;
815b4844fa0SWilly Tarreau 		CASE_TEST(sbrk);              if ((p1 = p2 = sbrk(4096)) != (void *)-1) p2 = sbrk(-4096); EXPECT_SYSZR(1, (p2 == (void *)-1) || p2 == p1); break;
816b4844fa0SWilly Tarreau 		CASE_TEST(brk);               EXPECT_SYSZR(1, brk(sbrk(0))); break;
81738fc0a35SZhangjin Wu 		CASE_TEST(chdir_root);        EXPECT_SYSZR(1, chdir("/")); chdir(getenv("PWD")); break;
818b4844fa0SWilly Tarreau 		CASE_TEST(chdir_dot);         EXPECT_SYSZR(1, chdir(".")); break;
819b4844fa0SWilly Tarreau 		CASE_TEST(chdir_blah);        EXPECT_SYSER(1, chdir("/blah"), -1, ENOENT); break;
820148e9718SZhangjin Wu 		CASE_TEST(chmod_argv0);       EXPECT_SYSZR(1, chmod(argv0, 0555)); break;
8217172f1c6SWilly Tarreau 		CASE_TEST(chmod_net);         EXPECT_SYSZR(proc, chmod("/proc/self/net", 0555)); break;
8227172f1c6SWilly Tarreau 		CASE_TEST(chmod_self);        EXPECT_SYSER(proc, chmod("/proc/self", 0555), -1, EPERM); break;
8237172f1c6SWilly Tarreau 		CASE_TEST(chown_self);        EXPECT_SYSER(proc, chown("/proc/self", 0, 0), -1, EPERM); break;
8243e2d337bSWilly Tarreau 		CASE_TEST(chroot_root);       EXPECT_SYSZR(euid0, chroot("/")); break;
825b4844fa0SWilly Tarreau 		CASE_TEST(chroot_blah);       EXPECT_SYSER(1, chroot("/proc/self/blah"), -1, ENOENT); break;
826135b622eSZhangjin Wu 		CASE_TEST(chroot_exe);        EXPECT_SYSER(1, chroot(argv0), -1, ENOTDIR); break;
827b4844fa0SWilly Tarreau 		CASE_TEST(close_m1);          EXPECT_SYSER(1, close(-1), -1, EBADF); break;
828b4844fa0SWilly Tarreau 		CASE_TEST(close_dup);         EXPECT_SYSZR(1, close(dup(0))); break;
829b4844fa0SWilly Tarreau 		CASE_TEST(dup_0);             tmp = dup(0);  EXPECT_SYSNE(1, tmp, -1); close(tmp); break;
830b4844fa0SWilly Tarreau 		CASE_TEST(dup_m1);            tmp = dup(-1); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break;
831b4844fa0SWilly Tarreau 		CASE_TEST(dup2_0);            tmp = dup2(0, 100);  EXPECT_SYSNE(1, tmp, -1); close(tmp); break;
832b4844fa0SWilly Tarreau 		CASE_TEST(dup2_m1);           tmp = dup2(-1, 100); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break;
833b4844fa0SWilly Tarreau 		CASE_TEST(dup3_0);            tmp = dup3(0, 100, 0);  EXPECT_SYSNE(1, tmp, -1); close(tmp); break;
834b4844fa0SWilly Tarreau 		CASE_TEST(dup3_m1);           tmp = dup3(-1, 100, 0); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break;
835b4844fa0SWilly Tarreau 		CASE_TEST(execve_root);       EXPECT_SYSER(1, execve("/", (char*[]){ [0] = "/", [1] = NULL }, NULL), -1, EACCES); break;
8363ad09d72SThomas Weißschuh 		CASE_TEST(fork);              EXPECT_SYSZR(1, test_fork()); break;
837b4844fa0SWilly Tarreau 		CASE_TEST(getdents64_root);   EXPECT_SYSNE(1, test_getdents64("/"), -1); break;
838b4844fa0SWilly Tarreau 		CASE_TEST(getdents64_null);   EXPECT_SYSER(1, test_getdents64("/dev/null"), -1, ENOTDIR); break;
839957bfa31SZhangjin Wu 		CASE_TEST(gettimeofday_tv);   EXPECT_SYSZR(1, gettimeofday(&tv, NULL)); break;
840957bfa31SZhangjin Wu 		CASE_TEST(gettimeofday_tv_tz);EXPECT_SYSZR(1, gettimeofday(&tv, &tz)); break;
841a290296aSAmmar Faizi 		CASE_TEST(getpagesize);       EXPECT_SYSZR(1, test_getpagesize()); break;
842b4844fa0SWilly Tarreau 		CASE_TEST(ioctl_tiocinq);     EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); break;
843b4844fa0SWilly Tarreau 		CASE_TEST(ioctl_tiocinq);     EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); break;
844b4844fa0SWilly Tarreau 		CASE_TEST(link_root1);        EXPECT_SYSER(1, link("/", "/"), -1, EEXIST); break;
845b4844fa0SWilly Tarreau 		CASE_TEST(link_blah);         EXPECT_SYSER(1, link("/proc/self/blah", "/blah"), -1, ENOENT); break;
8463e2d337bSWilly Tarreau 		CASE_TEST(link_dir);          EXPECT_SYSER(euid0, link("/", "/blah"), -1, EPERM); break;
847f7a419e3SZhangjin Wu 		CASE_TEST(link_cross);        EXPECT_SYSER(proc, link("/proc/self/cmdline", "/blah"), -1, EXDEV); break;
848b4844fa0SWilly Tarreau 		CASE_TEST(lseek_m1);          EXPECT_SYSER(1, lseek(-1, 0, SEEK_SET), -1, EBADF); break;
849b4844fa0SWilly Tarreau 		CASE_TEST(lseek_0);           EXPECT_SYSER(1, lseek(0, 0, SEEK_SET), -1, ESPIPE); break;
850b4844fa0SWilly Tarreau 		CASE_TEST(mkdir_root);        EXPECT_SYSER(1, mkdir("/", 0755), -1, EEXIST); break;
851d4a3b2b9SZhangjin Wu 		CASE_TEST(mmap_bad);          EXPECT_PTRER(1, mmap(NULL, 0, PROT_READ, MAP_PRIVATE, 0, 0), MAP_FAILED, EINVAL); break;
852ba3d0892SZhangjin Wu 		CASE_TEST(munmap_bad);        EXPECT_SYSER(1, munmap((void *)1, 0), -1, EINVAL); break;
853fcdbf5ddSZhangjin Wu 		CASE_TEST(mmap_munmap_good);  EXPECT_SYSZR(1, test_mmap_munmap()); break;
854b4844fa0SWilly Tarreau 		CASE_TEST(open_tty);          EXPECT_SYSNE(1, tmp = open("/dev/null", 0), -1); if (tmp != -1) close(tmp); break;
855b4844fa0SWilly Tarreau 		CASE_TEST(open_blah);         EXPECT_SYSER(1, tmp = open("/proc/self/blah", 0), -1, ENOENT); if (tmp != -1) close(tmp); break;
856b4844fa0SWilly Tarreau 		CASE_TEST(poll_null);         EXPECT_SYSZR(1, poll(NULL, 0, 0)); break;
857b4844fa0SWilly Tarreau 		CASE_TEST(poll_stdout);       EXPECT_SYSNE(1, ({ struct pollfd fds = { 1, POLLOUT, 0}; poll(&fds, 1, 0); }), -1); break;
858b4844fa0SWilly Tarreau 		CASE_TEST(poll_fault);        EXPECT_SYSER(1, poll((void *)1, 1, 0), -1, EFAULT); break;
859208aa9d9SThomas Weißschuh 		CASE_TEST(prctl);             EXPECT_SYSER(1, prctl(PR_SET_NAME, (unsigned long)NULL, 0, 0, 0), -1, EFAULT); break;
860b4844fa0SWilly Tarreau 		CASE_TEST(read_badf);         EXPECT_SYSER(1, read(-1, &tmp, 1), -1, EBADF); break;
8614e14e844SZhangjin Wu 		CASE_TEST(rmdir_blah);        EXPECT_SYSER(1, rmdir("/blah"), -1, ENOENT); break;
862b4844fa0SWilly Tarreau 		CASE_TEST(sched_yield);       EXPECT_SYSZR(1, sched_yield()); break;
863b4844fa0SWilly Tarreau 		CASE_TEST(select_null);       EXPECT_SYSZR(1, ({ struct timeval tv = { 0 }; select(0, NULL, NULL, NULL, &tv); })); break;
864b4844fa0SWilly 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;
865b4844fa0SWilly Tarreau 		CASE_TEST(select_fault);      EXPECT_SYSER(1, select(1, (void *)1, NULL, NULL, 0), -1, EFAULT); break;
866b4844fa0SWilly Tarreau 		CASE_TEST(stat_blah);         EXPECT_SYSER(1, stat("/proc/self/blah", &stat_buf), -1, ENOENT); break;
86746cf630cSZhangjin Wu 		CASE_TEST(stat_fault);        EXPECT_SYSER(1, stat((void *)1, &stat_buf), -1, EFAULT); break;
86887b9fa66SThomas Weißschuh 		CASE_TEST(stat_timestamps);   EXPECT_SYSZR(1, test_stat_timestamps()); break;
869b4844fa0SWilly Tarreau 		CASE_TEST(symlink_root);      EXPECT_SYSER(1, symlink("/", "/"), -1, EEXIST); break;
870b4844fa0SWilly Tarreau 		CASE_TEST(unlink_root);       EXPECT_SYSER(1, unlink("/"), -1, EISDIR); break;
871b4844fa0SWilly Tarreau 		CASE_TEST(unlink_blah);       EXPECT_SYSER(1, unlink("/proc/self/blah"), -1, ENOENT); break;
872b4844fa0SWilly Tarreau 		CASE_TEST(wait_child);        EXPECT_SYSER(1, wait(&tmp), -1, ECHILD); break;
873b4844fa0SWilly Tarreau 		CASE_TEST(waitpid_min);       EXPECT_SYSER(1, waitpid(INT_MIN, &tmp, WNOHANG), -1, ESRCH); break;
874b4844fa0SWilly Tarreau 		CASE_TEST(waitpid_child);     EXPECT_SYSER(1, waitpid(getpid(), &tmp, WNOHANG), -1, ECHILD); break;
875b4844fa0SWilly Tarreau 		CASE_TEST(write_badf);        EXPECT_SYSER(1, write(-1, &tmp, 1), -1, EBADF); break;
876b4844fa0SWilly Tarreau 		CASE_TEST(write_zero);        EXPECT_SYSZR(1, write(1, &tmp, 0)); break;
87753fcfafaSThomas Weißschuh 		CASE_TEST(syscall_noargs);    EXPECT_SYSEQ(1, syscall(__NR_getpid), getpid()); break;
878ec8e1b73SZhangjin Wu 		CASE_TEST(syscall_args);      EXPECT_SYSER(1, syscall(__NR_statx, 0, NULL, 0, 0, NULL), -1, EFAULT); break;
879b4844fa0SWilly Tarreau 		case __LINE__:
880b4844fa0SWilly Tarreau 			return ret; /* must be last */
881b4844fa0SWilly Tarreau 		/* note: do not set any defaults so as to permit holes above */
882b4844fa0SWilly Tarreau 		}
883b4844fa0SWilly Tarreau 	}
884b4844fa0SWilly Tarreau 	return ret;
885b4844fa0SWilly Tarreau }
886b4844fa0SWilly Tarreau 
88795bc9894SWilly Tarreau int run_stdlib(int min, int max)
88895bc9894SWilly Tarreau {
88995bc9894SWilly Tarreau 	int test;
89095bc9894SWilly Tarreau 	int tmp;
89195bc9894SWilly Tarreau 	int ret = 0;
89295bc9894SWilly Tarreau 	void *p1, *p2;
89395bc9894SWilly Tarreau 
89495bc9894SWilly Tarreau 	for (test = min; test >= 0 && test <= max; test++) {
895fddc8f81SThomas Weißschuh 		int llen = 0; /* line length */
89695bc9894SWilly Tarreau 
89795bc9894SWilly Tarreau 		/* avoid leaving empty lines below, this will insert holes into
89895bc9894SWilly Tarreau 		 * test numbers.
89995bc9894SWilly Tarreau 		 */
90095bc9894SWilly Tarreau 		switch (test + __LINE__ + 1) {
90195bc9894SWilly Tarreau 		CASE_TEST(getenv_TERM);        EXPECT_STRNZ(1, getenv("TERM")); break;
90295bc9894SWilly Tarreau 		CASE_TEST(getenv_blah);        EXPECT_STRZR(1, getenv("blah")); break;
90395bc9894SWilly Tarreau 		CASE_TEST(setcmp_blah_blah);   EXPECT_EQ(1, strcmp("blah", "blah"), 0); break;
90495bc9894SWilly Tarreau 		CASE_TEST(setcmp_blah_blah2);  EXPECT_NE(1, strcmp("blah", "blah2"), 0); break;
90595bc9894SWilly Tarreau 		CASE_TEST(setncmp_blah_blah);  EXPECT_EQ(1, strncmp("blah", "blah", 10), 0); break;
90695bc9894SWilly Tarreau 		CASE_TEST(setncmp_blah_blah4); EXPECT_EQ(1, strncmp("blah", "blah4", 4), 0); break;
90795bc9894SWilly Tarreau 		CASE_TEST(setncmp_blah_blah5); EXPECT_NE(1, strncmp("blah", "blah5", 5), 0); break;
90895bc9894SWilly Tarreau 		CASE_TEST(setncmp_blah_blah6); EXPECT_NE(1, strncmp("blah", "blah6", 6), 0); break;
90995bc9894SWilly Tarreau 		CASE_TEST(strchr_foobar_o);    EXPECT_STREQ(1, strchr("foobar", 'o'), "oobar"); break;
91095bc9894SWilly Tarreau 		CASE_TEST(strchr_foobar_z);    EXPECT_STRZR(1, strchr("foobar", 'z')); break;
91195bc9894SWilly Tarreau 		CASE_TEST(strrchr_foobar_o);   EXPECT_STREQ(1, strrchr("foobar", 'o'), "obar"); break;
91295bc9894SWilly Tarreau 		CASE_TEST(strrchr_foobar_z);   EXPECT_STRZR(1, strrchr("foobar", 'z')); break;
913c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_20_20);       EXPECT_EQ(1, memcmp("aaa\x20", "aaa\x20", 4), 0); break;
914c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_20_60);       EXPECT_LT(1, memcmp("aaa\x20", "aaa\x60", 4), 0); break;
915c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_60_20);       EXPECT_GT(1, memcmp("aaa\x60", "aaa\x20", 4), 0); break;
916c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_20_e0);       EXPECT_LT(1, memcmp("aaa\x20", "aaa\xe0", 4), 0); break;
917c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_e0_20);       EXPECT_GT(1, memcmp("aaa\xe0", "aaa\x20", 4), 0); break;
918c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_80_e0);       EXPECT_LT(1, memcmp("aaa\x80", "aaa\xe0", 4), 0); break;
919c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_e0_80);       EXPECT_GT(1, memcmp("aaa\xe0", "aaa\x80", 4), 0); break;
920d1209597SVincent Dagonneau 		CASE_TEST(limit_int8_max);          EXPECT_EQ(1, INT8_MAX,         (int8_t)          0x7f); break;
921d1209597SVincent Dagonneau 		CASE_TEST(limit_int8_min);          EXPECT_EQ(1, INT8_MIN,         (int8_t)          0x80); break;
922d1209597SVincent Dagonneau 		CASE_TEST(limit_uint8_max);         EXPECT_EQ(1, UINT8_MAX,        (uint8_t)         0xff); break;
923d1209597SVincent Dagonneau 		CASE_TEST(limit_int16_max);         EXPECT_EQ(1, INT16_MAX,        (int16_t)         0x7fff); break;
924d1209597SVincent Dagonneau 		CASE_TEST(limit_int16_min);         EXPECT_EQ(1, INT16_MIN,        (int16_t)         0x8000); break;
925d1209597SVincent Dagonneau 		CASE_TEST(limit_uint16_max);        EXPECT_EQ(1, UINT16_MAX,       (uint16_t)        0xffff); break;
926d1209597SVincent Dagonneau 		CASE_TEST(limit_int32_max);         EXPECT_EQ(1, INT32_MAX,        (int32_t)         0x7fffffff); break;
927d1209597SVincent Dagonneau 		CASE_TEST(limit_int32_min);         EXPECT_EQ(1, INT32_MIN,        (int32_t)         0x80000000); break;
928d1209597SVincent Dagonneau 		CASE_TEST(limit_uint32_max);        EXPECT_EQ(1, UINT32_MAX,       (uint32_t)        0xffffffff); break;
929d1209597SVincent Dagonneau 		CASE_TEST(limit_int64_max);         EXPECT_EQ(1, INT64_MAX,        (int64_t)         0x7fffffffffffffff); break;
930d1209597SVincent Dagonneau 		CASE_TEST(limit_int64_min);         EXPECT_EQ(1, INT64_MIN,        (int64_t)         0x8000000000000000); break;
931d1209597SVincent Dagonneau 		CASE_TEST(limit_uint64_max);        EXPECT_EQ(1, UINT64_MAX,       (uint64_t)        0xffffffffffffffff); break;
932d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least8_max);    EXPECT_EQ(1, INT_LEAST8_MAX,   (int_least8_t)    0x7f); break;
933d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least8_min);    EXPECT_EQ(1, INT_LEAST8_MIN,   (int_least8_t)    0x80); break;
934d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_least8_max);   EXPECT_EQ(1, UINT_LEAST8_MAX,  (uint_least8_t)   0xff); break;
935d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least16_max);   EXPECT_EQ(1, INT_LEAST16_MAX,  (int_least16_t)   0x7fff); break;
936d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least16_min);   EXPECT_EQ(1, INT_LEAST16_MIN,  (int_least16_t)   0x8000); break;
937d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_least16_max);  EXPECT_EQ(1, UINT_LEAST16_MAX, (uint_least16_t)  0xffff); break;
938d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least32_max);   EXPECT_EQ(1, INT_LEAST32_MAX,  (int_least32_t)   0x7fffffff); break;
939d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least32_min);   EXPECT_EQ(1, INT_LEAST32_MIN,  (int_least32_t)   0x80000000); break;
940d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_least32_max);  EXPECT_EQ(1, UINT_LEAST32_MAX, (uint_least32_t)  0xffffffffU); break;
941d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least64_min);   EXPECT_EQ(1, INT_LEAST64_MIN,  (int_least64_t)   0x8000000000000000LL); break;
942d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least64_max);   EXPECT_EQ(1, INT_LEAST64_MAX,  (int_least64_t)   0x7fffffffffffffffLL); break;
943d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_least64_max);  EXPECT_EQ(1, UINT_LEAST64_MAX, (uint_least64_t)  0xffffffffffffffffULL); break;
944d1209597SVincent Dagonneau 		CASE_TEST(limit_int_fast8_max);     EXPECT_EQ(1, INT_FAST8_MAX,    (int_fast8_t)     0x7f); break;
945d1209597SVincent Dagonneau 		CASE_TEST(limit_int_fast8_min);     EXPECT_EQ(1, INT_FAST8_MIN,    (int_fast8_t)     0x80); break;
946d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_fast8_max);    EXPECT_EQ(1, UINT_FAST8_MAX,   (uint_fast8_t)    0xff); break;
947989abf1cSZhangjin Wu 		CASE_TEST(limit_int_fast16_min);    EXPECT_EQ(1, INT_FAST16_MIN,   (int_fast16_t)    SINT_MIN_OF_TYPE(int_fast16_t)); break;
948989abf1cSZhangjin Wu 		CASE_TEST(limit_int_fast16_max);    EXPECT_EQ(1, INT_FAST16_MAX,   (int_fast16_t)    SINT_MAX_OF_TYPE(int_fast16_t)); break;
949d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_fast16_max);   EXPECT_EQ(1, UINT_FAST16_MAX,  (uint_fast16_t)   UINTPTR_MAX); break;
950989abf1cSZhangjin Wu 		CASE_TEST(limit_int_fast32_min);    EXPECT_EQ(1, INT_FAST32_MIN,   (int_fast32_t)    SINT_MIN_OF_TYPE(int_fast32_t)); break;
951989abf1cSZhangjin Wu 		CASE_TEST(limit_int_fast32_max);    EXPECT_EQ(1, INT_FAST32_MAX,   (int_fast32_t)    SINT_MAX_OF_TYPE(int_fast32_t)); break;
952d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_fast32_max);   EXPECT_EQ(1, UINT_FAST32_MAX,  (uint_fast32_t)   UINTPTR_MAX); break;
953f9bf5944SThomas Weißschuh 		CASE_TEST(limit_int_fast64_min);    EXPECT_EQ(1, INT_FAST64_MIN,   (int_fast64_t)    INT64_MIN); break;
954f9bf5944SThomas Weißschuh 		CASE_TEST(limit_int_fast64_max);    EXPECT_EQ(1, INT_FAST64_MAX,   (int_fast64_t)    INT64_MAX); break;
955f9bf5944SThomas Weißschuh 		CASE_TEST(limit_uint_fast64_max);   EXPECT_EQ(1, UINT_FAST64_MAX,  (uint_fast64_t)   UINT64_MAX); break;
956ceb528feSThomas Weißschuh 		CASE_TEST(sizeof_long_sane);        EXPECT_EQ(1, sizeof(long) == 8 || sizeof(long) == 4, 1); break;
957ceb528feSThomas Weißschuh 		CASE_TEST(limit_intptr_min_64);     EXPECT_EQ(sizeof(long) == 8, INTPTR_MIN,  (intptr_t)  0x8000000000000000LL); break;
958ceb528feSThomas Weißschuh 		CASE_TEST(limit_intptr_max_64);     EXPECT_EQ(sizeof(long) == 8, INTPTR_MAX,  (intptr_t)  0x7fffffffffffffffLL); break;
959ceb528feSThomas Weißschuh 		CASE_TEST(limit_uintptr_max_64);    EXPECT_EQ(sizeof(long) == 8, UINTPTR_MAX, (uintptr_t) 0xffffffffffffffffULL); break;
960ceb528feSThomas Weißschuh 		CASE_TEST(limit_ptrdiff_min_64);    EXPECT_EQ(sizeof(long) == 8, PTRDIFF_MIN, (ptrdiff_t) 0x8000000000000000LL); break;
961ceb528feSThomas Weißschuh 		CASE_TEST(limit_ptrdiff_max_64);    EXPECT_EQ(sizeof(long) == 8, PTRDIFF_MAX, (ptrdiff_t) 0x7fffffffffffffffLL); break;
962ceb528feSThomas Weißschuh 		CASE_TEST(limit_size_max_64);       EXPECT_EQ(sizeof(long) == 8, SIZE_MAX,    (size_t)    0xffffffffffffffffULL); break;
963ceb528feSThomas Weißschuh 		CASE_TEST(limit_intptr_min_32);     EXPECT_EQ(sizeof(long) == 4, INTPTR_MIN,  (intptr_t)  0x80000000); break;
964ceb528feSThomas Weißschuh 		CASE_TEST(limit_intptr_max_32);     EXPECT_EQ(sizeof(long) == 4, INTPTR_MAX,  (intptr_t)  0x7fffffff); break;
965ceb528feSThomas Weißschuh 		CASE_TEST(limit_uintptr_max_32);    EXPECT_EQ(sizeof(long) == 4, UINTPTR_MAX, (uintptr_t) 0xffffffffU); break;
966ceb528feSThomas Weißschuh 		CASE_TEST(limit_ptrdiff_min_32);    EXPECT_EQ(sizeof(long) == 4, PTRDIFF_MIN, (ptrdiff_t) 0x80000000); break;
967ceb528feSThomas Weißschuh 		CASE_TEST(limit_ptrdiff_max_32);    EXPECT_EQ(sizeof(long) == 4, PTRDIFF_MAX, (ptrdiff_t) 0x7fffffff); break;
968ceb528feSThomas Weißschuh 		CASE_TEST(limit_size_max_32);       EXPECT_EQ(sizeof(long) == 4, SIZE_MAX,    (size_t)    0xffffffffU); break;
96995bc9894SWilly Tarreau 		case __LINE__:
97095bc9894SWilly Tarreau 			return ret; /* must be last */
97195bc9894SWilly Tarreau 		/* note: do not set any defaults so as to permit holes above */
97295bc9894SWilly Tarreau 		}
97395bc9894SWilly Tarreau 	}
97495bc9894SWilly Tarreau 	return ret;
97595bc9894SWilly Tarreau }
97695bc9894SWilly Tarreau 
97769f2cd9fSThomas Weißschuh #define EXPECT_VFPRINTF(c, expected, fmt, ...)				\
97869f2cd9fSThomas Weißschuh 	ret += expect_vfprintf(llen, c, expected, fmt, ##__VA_ARGS__)
97969f2cd9fSThomas Weißschuh 
98069f2cd9fSThomas Weißschuh static int expect_vfprintf(int llen, size_t c, const char *expected, const char *fmt, ...)
98169f2cd9fSThomas Weißschuh {
98269f2cd9fSThomas Weißschuh 	int ret, fd, w, r;
98369f2cd9fSThomas Weißschuh 	char buf[100];
98469f2cd9fSThomas Weißschuh 	FILE *memfile;
98569f2cd9fSThomas Weißschuh 	va_list args;
98669f2cd9fSThomas Weißschuh 
9876861b1a3SZhangjin Wu 	fd = open("/tmp", O_TMPFILE | O_EXCL | O_RDWR, 0600);
98869f2cd9fSThomas Weißschuh 	if (fd == -1) {
989b184a261SThomas Weißschuh 		result(llen, SKIPPED);
9906861b1a3SZhangjin Wu 		return 0;
99169f2cd9fSThomas Weißschuh 	}
99269f2cd9fSThomas Weißschuh 
99369f2cd9fSThomas Weißschuh 	memfile = fdopen(fd, "w+");
99469f2cd9fSThomas Weißschuh 	if (!memfile) {
995b184a261SThomas Weißschuh 		result(llen, FAIL);
99669f2cd9fSThomas Weißschuh 		return 1;
99769f2cd9fSThomas Weißschuh 	}
99869f2cd9fSThomas Weißschuh 
99969f2cd9fSThomas Weißschuh 	va_start(args, fmt);
100069f2cd9fSThomas Weißschuh 	w = vfprintf(memfile, fmt, args);
100169f2cd9fSThomas Weißschuh 	va_end(args);
100269f2cd9fSThomas Weißschuh 
100369f2cd9fSThomas Weißschuh 	if (w != c) {
100469f2cd9fSThomas Weißschuh 		llen += printf(" written(%d) != %d", w, (int) c);
1005b184a261SThomas Weißschuh 		result(llen, FAIL);
100669f2cd9fSThomas Weißschuh 		return 1;
100769f2cd9fSThomas Weißschuh 	}
100869f2cd9fSThomas Weißschuh 
100969f2cd9fSThomas Weißschuh 	fflush(memfile);
101069f2cd9fSThomas Weißschuh 	lseek(fd, 0, SEEK_SET);
101169f2cd9fSThomas Weißschuh 
101269f2cd9fSThomas Weißschuh 	r = read(fd, buf, sizeof(buf) - 1);
101369f2cd9fSThomas Weißschuh 	buf[r] = '\0';
101469f2cd9fSThomas Weißschuh 
101569f2cd9fSThomas Weißschuh 	fclose(memfile);
101669f2cd9fSThomas Weißschuh 
101769f2cd9fSThomas Weißschuh 	if (r != w) {
101869f2cd9fSThomas Weißschuh 		llen += printf(" written(%d) != read(%d)", w, r);
1019b184a261SThomas Weißschuh 		result(llen, FAIL);
102069f2cd9fSThomas Weißschuh 		return 1;
102169f2cd9fSThomas Weißschuh 	}
102269f2cd9fSThomas Weißschuh 
102369f2cd9fSThomas Weißschuh 	llen += printf(" \"%s\" = \"%s\"", expected, buf);
102469f2cd9fSThomas Weißschuh 	ret = strncmp(expected, buf, c);
102569f2cd9fSThomas Weißschuh 
1026b184a261SThomas Weißschuh 	result(llen, ret ? FAIL : OK);
102769f2cd9fSThomas Weißschuh 	return ret;
102869f2cd9fSThomas Weißschuh }
102969f2cd9fSThomas Weißschuh 
103069f2cd9fSThomas Weißschuh static int run_vfprintf(int min, int max)
103169f2cd9fSThomas Weißschuh {
103269f2cd9fSThomas Weißschuh 	int test;
103369f2cd9fSThomas Weißschuh 	int tmp;
103469f2cd9fSThomas Weißschuh 	int ret = 0;
103569f2cd9fSThomas Weißschuh 	void *p1, *p2;
103669f2cd9fSThomas Weißschuh 
103769f2cd9fSThomas Weißschuh 	for (test = min; test >= 0 && test <= max; test++) {
1038fddc8f81SThomas Weißschuh 		int llen = 0; /* line length */
103969f2cd9fSThomas Weißschuh 
104069f2cd9fSThomas Weißschuh 		/* avoid leaving empty lines below, this will insert holes into
104169f2cd9fSThomas Weißschuh 		 * test numbers.
104269f2cd9fSThomas Weißschuh 		 */
104369f2cd9fSThomas Weißschuh 		switch (test + __LINE__ + 1) {
104469f2cd9fSThomas Weißschuh 		CASE_TEST(empty);        EXPECT_VFPRINTF(0, "", ""); break;
104569f2cd9fSThomas Weißschuh 		CASE_TEST(simple);       EXPECT_VFPRINTF(3, "foo", "foo"); break;
104669f2cd9fSThomas Weißschuh 		CASE_TEST(string);       EXPECT_VFPRINTF(3, "foo", "%s", "foo"); break;
104769f2cd9fSThomas Weißschuh 		CASE_TEST(number);       EXPECT_VFPRINTF(4, "1234", "%d", 1234); break;
104869f2cd9fSThomas Weißschuh 		CASE_TEST(negnumber);    EXPECT_VFPRINTF(5, "-1234", "%d", -1234); break;
104969f2cd9fSThomas Weißschuh 		CASE_TEST(unsigned);     EXPECT_VFPRINTF(5, "12345", "%u", 12345); break;
105069f2cd9fSThomas Weißschuh 		CASE_TEST(char);         EXPECT_VFPRINTF(1, "c", "%c", 'c'); break;
105169f2cd9fSThomas Weißschuh 		CASE_TEST(hex);          EXPECT_VFPRINTF(1, "f", "%x", 0xf); break;
105269f2cd9fSThomas Weißschuh 		CASE_TEST(pointer);      EXPECT_VFPRINTF(3, "0x1", "%p", (void *) 0x1); break;
105369f2cd9fSThomas Weißschuh 		case __LINE__:
105469f2cd9fSThomas Weißschuh 			return ret; /* must be last */
105569f2cd9fSThomas Weißschuh 		/* note: do not set any defaults so as to permit holes above */
105669f2cd9fSThomas Weißschuh 		}
105769f2cd9fSThomas Weißschuh 	}
105869f2cd9fSThomas Weißschuh 	return ret;
105969f2cd9fSThomas Weißschuh }
106069f2cd9fSThomas Weißschuh 
106197357168SThomas Weißschuh static int smash_stack(void)
106297357168SThomas Weißschuh {
106397357168SThomas Weißschuh 	char buf[100];
1064e7654c3fSThomas Weißschuh 	volatile char *ptr = buf;
1065aa662d12SWilly Tarreau 	size_t i;
106697357168SThomas Weißschuh 
1067aa662d12SWilly Tarreau 	for (i = 0; i < 200; i++)
1068e7654c3fSThomas Weißschuh 		ptr[i] = 'P';
106997357168SThomas Weißschuh 
107097357168SThomas Weißschuh 	return 1;
107197357168SThomas Weißschuh }
107297357168SThomas Weißschuh 
107397357168SThomas Weißschuh static int run_protection(int min, int max)
107497357168SThomas Weißschuh {
107597357168SThomas Weißschuh 	pid_t pid;
107697357168SThomas Weißschuh 	int llen = 0, status;
107797357168SThomas Weißschuh 
107897357168SThomas Weißschuh 	llen += printf("0 -fstackprotector ");
107997357168SThomas Weißschuh 
1080818924d1SThomas Weißschuh #if !defined(_NOLIBC_STACKPROTECTOR)
108197357168SThomas Weißschuh 	llen += printf("not supported");
1082b184a261SThomas Weißschuh 	result(llen, SKIPPED);
108397357168SThomas Weißschuh 	return 0;
108497357168SThomas Weißschuh #endif
108597357168SThomas Weißschuh 
1086818924d1SThomas Weißschuh #if defined(_NOLIBC_STACKPROTECTOR)
108785250921SThomas Weißschuh 	if (!__stack_chk_guard) {
108885250921SThomas Weißschuh 		llen += printf("__stack_chk_guard not initialized");
1089b184a261SThomas Weißschuh 		result(llen, FAIL);
109085250921SThomas Weißschuh 		return 1;
109185250921SThomas Weißschuh 	}
109285250921SThomas Weißschuh #endif
109385250921SThomas Weißschuh 
109497357168SThomas Weißschuh 	pid = -1;
109597357168SThomas Weißschuh 	pid = fork();
109697357168SThomas Weißschuh 
109797357168SThomas Weißschuh 	switch (pid) {
109897357168SThomas Weißschuh 	case -1:
109997357168SThomas Weißschuh 		llen += printf("fork()");
1100b184a261SThomas Weißschuh 		result(llen, FAIL);
110197357168SThomas Weißschuh 		return 1;
110297357168SThomas Weißschuh 
110397357168SThomas Weißschuh 	case 0:
110497357168SThomas Weißschuh 		close(STDOUT_FILENO);
110597357168SThomas Weißschuh 		close(STDERR_FILENO);
110697357168SThomas Weißschuh 
11079a75575bSThomas Weißschuh 		prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
110897357168SThomas Weißschuh 		smash_stack();
110997357168SThomas Weißschuh 		return 1;
111097357168SThomas Weißschuh 
111197357168SThomas Weißschuh 	default:
111297357168SThomas Weißschuh 		pid = waitpid(pid, &status, 0);
111397357168SThomas Weißschuh 
111497357168SThomas Weißschuh 		if (pid == -1 || !WIFSIGNALED(status) || WTERMSIG(status) != SIGABRT) {
111597357168SThomas Weißschuh 			llen += printf("waitpid()");
1116b184a261SThomas Weißschuh 			result(llen, FAIL);
111797357168SThomas Weißschuh 			return 1;
111897357168SThomas Weißschuh 		}
1119b184a261SThomas Weißschuh 		result(llen, OK);
112097357168SThomas Weißschuh 		return 0;
112197357168SThomas Weißschuh 	}
112297357168SThomas Weißschuh }
112397357168SThomas Weißschuh 
11241a5454f6SWilly Tarreau /* prepare what needs to be prepared for pid 1 (stdio, /dev, /proc, etc) */
11251a5454f6SWilly Tarreau int prepare(void)
11261a5454f6SWilly Tarreau {
11271a5454f6SWilly Tarreau 	struct stat stat_buf;
11281a5454f6SWilly Tarreau 
11291a5454f6SWilly Tarreau 	/* It's possible that /dev doesn't even exist or was not mounted, so
11301a5454f6SWilly Tarreau 	 * we'll try to create it, mount it, or create minimal entries into it.
11311a5454f6SWilly Tarreau 	 * We want at least /dev/null and /dev/console.
11321a5454f6SWilly Tarreau 	 */
11331a5454f6SWilly Tarreau 	if (stat("/dev/.", &stat_buf) == 0 || mkdir("/dev", 0755) == 0) {
11341a5454f6SWilly Tarreau 		if (stat("/dev/console", &stat_buf) != 0 ||
113582e339c2SZhangjin Wu 		    stat("/dev/null", &stat_buf) != 0 ||
113682e339c2SZhangjin Wu 		    stat("/dev/zero", &stat_buf) != 0) {
11371a5454f6SWilly Tarreau 			/* try devtmpfs first, otherwise fall back to manual creation */
11381a5454f6SWilly Tarreau 			if (mount("/dev", "/dev", "devtmpfs", 0, 0) != 0) {
11391a5454f6SWilly Tarreau 				mknod("/dev/console", 0600 | S_IFCHR, makedev(5, 1));
11401a5454f6SWilly Tarreau 				mknod("/dev/null",    0666 | S_IFCHR, makedev(1, 3));
114182e339c2SZhangjin Wu 				mknod("/dev/zero",    0666 | S_IFCHR, makedev(1, 5));
11421a5454f6SWilly Tarreau 			}
11431a5454f6SWilly Tarreau 		}
11441a5454f6SWilly Tarreau 	}
11451a5454f6SWilly Tarreau 
11461a5454f6SWilly Tarreau 	/* If no /dev/console was found before calling init, stdio is closed so
11471a5454f6SWilly Tarreau 	 * we need to reopen it from /dev/console. If it failed above, it will
11481a5454f6SWilly Tarreau 	 * still fail here and we cannot emit a message anyway.
11491a5454f6SWilly Tarreau 	 */
11501a5454f6SWilly Tarreau 	if (close(dup(1)) == -1) {
11511a5454f6SWilly Tarreau 		int fd = open("/dev/console", O_RDWR);
11521a5454f6SWilly Tarreau 
11531a5454f6SWilly Tarreau 		if (fd >= 0) {
11541a5454f6SWilly Tarreau 			if (fd != 0)
11551a5454f6SWilly Tarreau 				dup2(fd, 0);
11561a5454f6SWilly Tarreau 			if (fd != 1)
11571a5454f6SWilly Tarreau 				dup2(fd, 1);
11581a5454f6SWilly Tarreau 			if (fd != 2)
11591a5454f6SWilly Tarreau 				dup2(fd, 2);
11601a5454f6SWilly Tarreau 			if (fd > 2)
11611a5454f6SWilly Tarreau 				close(fd);
11621a5454f6SWilly Tarreau 			puts("\nSuccessfully reopened /dev/console.");
11631a5454f6SWilly Tarreau 		}
11641a5454f6SWilly Tarreau 	}
11651a5454f6SWilly Tarreau 
11661a5454f6SWilly Tarreau 	/* try to mount /proc if not mounted. Silently fail otherwise */
11671a5454f6SWilly Tarreau 	if (stat("/proc/.", &stat_buf) == 0 || mkdir("/proc", 0755) == 0) {
1168b8b26108SZhangjin Wu 		if (stat("/proc/self", &stat_buf) != 0) {
1169b8b26108SZhangjin Wu 			/* If not mountable, remove /proc completely to avoid misuse */
1170b8b26108SZhangjin Wu 			if (mount("none", "/proc", "proc", 0, 0) != 0)
1171b8b26108SZhangjin Wu 				rmdir("/proc");
1172b8b26108SZhangjin Wu 		}
11731a5454f6SWilly Tarreau 	}
11741a5454f6SWilly Tarreau 
1175bbb14546SZhangjin Wu 	/* some tests rely on a writable /tmp */
1176bbb14546SZhangjin Wu 	mkdir("/tmp", 0755);
1177bbb14546SZhangjin Wu 
11781a5454f6SWilly Tarreau 	return 0;
11791a5454f6SWilly Tarreau }
1180b4844fa0SWilly Tarreau 
118123da7bc9SWilly Tarreau /* This is the definition of known test names, with their functions */
1182c4560bd8SThomas Weißschuh static const struct test test_names[] = {
118323da7bc9SWilly Tarreau 	/* add new tests here */
118448967b73SZhangjin Wu 	{ .name = "startup",    .func = run_startup    },
1185b4844fa0SWilly Tarreau 	{ .name = "syscall",    .func = run_syscall    },
118695bc9894SWilly Tarreau 	{ .name = "stdlib",     .func = run_stdlib     },
118769f2cd9fSThomas Weißschuh 	{ .name = "vfprintf",   .func = run_vfprintf   },
118897357168SThomas Weißschuh 	{ .name = "protection", .func = run_protection },
118923da7bc9SWilly Tarreau 	{ 0 }
119023da7bc9SWilly Tarreau };
119123da7bc9SWilly Tarreau 
1192c388c992SZhangjin Wu int is_setting_valid(char *test)
1193c388c992SZhangjin Wu {
1194c388c992SZhangjin Wu 	int idx, len, test_len, valid = 0;
1195c388c992SZhangjin Wu 	char delimiter;
1196c388c992SZhangjin Wu 
1197c388c992SZhangjin Wu 	if (!test)
1198c388c992SZhangjin Wu 		return valid;
1199c388c992SZhangjin Wu 
1200c388c992SZhangjin Wu 	test_len = strlen(test);
1201c388c992SZhangjin Wu 
1202c388c992SZhangjin Wu 	for (idx = 0; test_names[idx].name; idx++) {
1203c388c992SZhangjin Wu 		len = strlen(test_names[idx].name);
1204c388c992SZhangjin Wu 		if (test_len < len)
1205c388c992SZhangjin Wu 			continue;
1206c388c992SZhangjin Wu 
1207c388c992SZhangjin Wu 		if (strncmp(test, test_names[idx].name, len) != 0)
1208c388c992SZhangjin Wu 			continue;
1209c388c992SZhangjin Wu 
1210c388c992SZhangjin Wu 		delimiter = test[len];
1211c388c992SZhangjin Wu 		if (delimiter != ':' && delimiter != ',' && delimiter != '\0')
1212c388c992SZhangjin Wu 			continue;
1213c388c992SZhangjin Wu 
1214c388c992SZhangjin Wu 		valid = 1;
1215c388c992SZhangjin Wu 		break;
1216c388c992SZhangjin Wu 	}
1217c388c992SZhangjin Wu 
1218c388c992SZhangjin Wu 	return valid;
1219c388c992SZhangjin Wu }
1220c388c992SZhangjin Wu 
1221362aecb2SWilly Tarreau int main(int argc, char **argv, char **envp)
1222362aecb2SWilly Tarreau {
1223362aecb2SWilly Tarreau 	int min = 0;
1224a36cfc5eSZhangjin Wu 	int max = INT_MAX;
1225362aecb2SWilly Tarreau 	int ret = 0;
122623da7bc9SWilly Tarreau 	int err;
122723da7bc9SWilly Tarreau 	int idx;
122823da7bc9SWilly Tarreau 	char *test;
1229362aecb2SWilly Tarreau 
1230938b5b98SZhangjin Wu 	argv0 = argv[0];
123148967b73SZhangjin Wu 	test_argc = argc;
123248967b73SZhangjin Wu 	test_argv = argv;
123367d108e2SThomas Weißschuh 	test_envp = envp;
1234362aecb2SWilly Tarreau 
12351a5454f6SWilly Tarreau 	/* when called as init, it's possible that no console was opened, for
12361a5454f6SWilly Tarreau 	 * example if no /dev file system was provided. We'll check that fd#1
12371a5454f6SWilly Tarreau 	 * was opened, and if not we'll attempt to create and open /dev/console
12381a5454f6SWilly Tarreau 	 * and /dev/null that we'll use for later tests.
12391a5454f6SWilly Tarreau 	 */
12401a5454f6SWilly Tarreau 	if (getpid() == 1)
12411a5454f6SWilly Tarreau 		prepare();
12421a5454f6SWilly Tarreau 
124323da7bc9SWilly Tarreau 	/* the definition of a series of tests comes from either argv[1] or the
124423da7bc9SWilly Tarreau 	 * "NOLIBC_TEST" environment variable. It's made of a comma-delimited
124523da7bc9SWilly Tarreau 	 * series of test names and optional ranges:
124623da7bc9SWilly Tarreau 	 *    syscall:5-15[:.*],stdlib:8-10
124723da7bc9SWilly Tarreau 	 */
124823da7bc9SWilly Tarreau 	test = argv[1];
1249c388c992SZhangjin Wu 	if (!is_setting_valid(test))
125023da7bc9SWilly Tarreau 		test = getenv("NOLIBC_TEST");
125123da7bc9SWilly Tarreau 
1252c388c992SZhangjin Wu 	if (is_setting_valid(test)) {
125323da7bc9SWilly Tarreau 		char *comma, *colon, *dash, *value;
125423da7bc9SWilly Tarreau 
125523da7bc9SWilly Tarreau 		do {
125623da7bc9SWilly Tarreau 			comma = strchr(test, ',');
125723da7bc9SWilly Tarreau 			if (comma)
125823da7bc9SWilly Tarreau 				*(comma++) = '\0';
125923da7bc9SWilly Tarreau 
126023da7bc9SWilly Tarreau 			colon = strchr(test, ':');
126123da7bc9SWilly Tarreau 			if (colon)
126223da7bc9SWilly Tarreau 				*(colon++) = '\0';
126323da7bc9SWilly Tarreau 
126423da7bc9SWilly Tarreau 			for (idx = 0; test_names[idx].name; idx++) {
126523da7bc9SWilly Tarreau 				if (strcmp(test, test_names[idx].name) == 0)
126623da7bc9SWilly Tarreau 					break;
126723da7bc9SWilly Tarreau 			}
126823da7bc9SWilly Tarreau 
126923da7bc9SWilly Tarreau 			if (test_names[idx].name) {
127023da7bc9SWilly Tarreau 				/* The test was named, it will be called at least
127123da7bc9SWilly Tarreau 				 * once. We may have an optional range at <colon>
127223da7bc9SWilly Tarreau 				 * here, which defaults to the full range.
127323da7bc9SWilly Tarreau 				 */
127423da7bc9SWilly Tarreau 				do {
1275a36cfc5eSZhangjin Wu 					min = 0; max = INT_MAX;
127623da7bc9SWilly Tarreau 					value = colon;
127723da7bc9SWilly Tarreau 					if (value && *value) {
127823da7bc9SWilly Tarreau 						colon = strchr(value, ':');
127923da7bc9SWilly Tarreau 						if (colon)
128023da7bc9SWilly Tarreau 							*(colon++) = '\0';
128123da7bc9SWilly Tarreau 
128223da7bc9SWilly Tarreau 						dash = strchr(value, '-');
128323da7bc9SWilly Tarreau 						if (dash)
128423da7bc9SWilly Tarreau 							*(dash++) = '\0';
128523da7bc9SWilly Tarreau 
128623da7bc9SWilly Tarreau 						/* support :val: :min-max: :min-: :-max: */
128723da7bc9SWilly Tarreau 						if (*value)
128823da7bc9SWilly Tarreau 							min = atoi(value);
128923da7bc9SWilly Tarreau 						if (!dash)
129023da7bc9SWilly Tarreau 							max = min;
129123da7bc9SWilly Tarreau 						else if (*dash)
129223da7bc9SWilly Tarreau 							max = atoi(dash);
129323da7bc9SWilly Tarreau 
129423da7bc9SWilly Tarreau 						value = colon;
129523da7bc9SWilly Tarreau 					}
129623da7bc9SWilly Tarreau 
129723da7bc9SWilly Tarreau 					/* now's time to call the test */
129823da7bc9SWilly Tarreau 					printf("Running test '%s'\n", test_names[idx].name);
129923da7bc9SWilly Tarreau 					err = test_names[idx].func(min, max);
130023da7bc9SWilly Tarreau 					ret += err;
130123da7bc9SWilly Tarreau 					printf("Errors during this test: %d\n\n", err);
130223da7bc9SWilly Tarreau 				} while (colon && *colon);
130323da7bc9SWilly Tarreau 			} else
130423da7bc9SWilly Tarreau 				printf("Ignoring unknown test name '%s'\n", test);
130523da7bc9SWilly Tarreau 
130623da7bc9SWilly Tarreau 			test = comma;
130723da7bc9SWilly Tarreau 		} while (test && *test);
130823da7bc9SWilly Tarreau 	} else {
130923da7bc9SWilly Tarreau 		/* no test mentioned, run everything */
131023da7bc9SWilly Tarreau 		for (idx = 0; test_names[idx].name; idx++) {
131123da7bc9SWilly Tarreau 			printf("Running test '%s'\n", test_names[idx].name);
131223da7bc9SWilly Tarreau 			err = test_names[idx].func(min, max);
131323da7bc9SWilly Tarreau 			ret += err;
131423da7bc9SWilly Tarreau 			printf("Errors during this test: %d\n\n", err);
131523da7bc9SWilly Tarreau 		}
131623da7bc9SWilly Tarreau 	}
131723da7bc9SWilly Tarreau 
1318362aecb2SWilly Tarreau 	printf("Total number of errors: %d\n", ret);
1319f49896d7SWilly Tarreau 
1320f49896d7SWilly Tarreau 	if (getpid() == 1) {
1321f49896d7SWilly Tarreau 		/* we're running as init, there's no other process on the
1322f49896d7SWilly Tarreau 		 * system, thus likely started from a VM for a quick check.
1323f49896d7SWilly Tarreau 		 * Exiting will provoke a kernel panic that may be reported
1324f49896d7SWilly Tarreau 		 * as an error by Qemu or the hypervisor, while stopping
1325f49896d7SWilly Tarreau 		 * cleanly will often be reported as a success. This allows
1326f49896d7SWilly Tarreau 		 * to use the output of this program for bisecting kernels.
1327f49896d7SWilly Tarreau 		 */
1328f49896d7SWilly Tarreau 		printf("Leaving init with final status: %d\n", !!ret);
1329f49896d7SWilly Tarreau 		if (ret == 0)
1330950add28SZhangjin Wu 			reboot(RB_POWER_OFF);
1331aa73a86cSWilly Tarreau #if defined(__x86_64__)
1332aa73a86cSWilly Tarreau 		/* QEMU started with "-device isa-debug-exit -no-reboot" will
1333aa73a86cSWilly Tarreau 		 * exit with status code 2N+1 when N is written to 0x501. We
1334aa73a86cSWilly Tarreau 		 * hard-code the syscall here as it's arch-dependent.
1335aa73a86cSWilly Tarreau 		 */
133667eb617aSThomas Weißschuh 		else if (syscall(__NR_ioperm, 0x501, 1, 1) == 0)
13377f291cfaSThomas Weißschuh 			__asm__ volatile ("outb %%al, %%dx" :: "d"(0x501), "a"(0));
1338aa73a86cSWilly Tarreau 		/* if it does nothing, fall back to the regular panic */
1339aa73a86cSWilly Tarreau #endif
1340f49896d7SWilly Tarreau 	}
1341f49896d7SWilly Tarreau 
1342362aecb2SWilly Tarreau 	printf("Exiting with status %d\n", !!ret);
1343362aecb2SWilly Tarreau 	return !!ret;
1344362aecb2SWilly Tarreau }
1345