xref: /openbmc/linux/tools/testing/selftests/nolibc/nolibc-test.c (revision 938b5b983330a55af8e31eabf329db7dbc7a5038)
1fddc8f81SThomas Weißschuh /* SPDX-License-Identifier: GPL-2.0 */
2362aecb2SWilly Tarreau 
31da02f51SWilly Tarreau #define _GNU_SOURCE
41da02f51SWilly Tarreau 
5362aecb2SWilly Tarreau /* libc-specific include files
61da02f51SWilly Tarreau  * The program may be built in 3 ways:
7362aecb2SWilly Tarreau  *   $(CC) -nostdlib -include /path/to/nolibc.h => NOLIBC already defined
81da02f51SWilly Tarreau  *   $(CC) -nostdlib -I/path/to/nolibc/sysroot  => _NOLIBC_* guards are present
91da02f51SWilly Tarreau  *   $(CC) with default libc                    => NOLIBC* never defined
10362aecb2SWilly Tarreau  */
11362aecb2SWilly Tarreau #ifndef NOLIBC
12362aecb2SWilly Tarreau #include <stdio.h>
13362aecb2SWilly Tarreau #include <stdlib.h>
14362aecb2SWilly Tarreau #include <string.h>
151da02f51SWilly Tarreau #ifndef _NOLIBC_STDIO_H
161da02f51SWilly Tarreau /* standard libcs need more includes */
171da02f51SWilly Tarreau #include <linux/reboot.h>
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 
43362aecb2SWilly Tarreau /* will be used by nolibc by getenv() */
44362aecb2SWilly Tarreau char **environ;
45362aecb2SWilly Tarreau 
46*938b5b98SZhangjin Wu /* will be used by some test cases as readable file, please don't write it */
47*938b5b98SZhangjin Wu static const char *argv0;
48*938b5b98SZhangjin Wu 
4923da7bc9SWilly Tarreau /* definition of a series of tests */
5023da7bc9SWilly Tarreau struct test {
51fddc8f81SThomas Weißschuh 	const char *name;              /* test name */
52fddc8f81SThomas Weißschuh 	int (*func)(int min, int max); /* handler */
5323da7bc9SWilly Tarreau };
5423da7bc9SWilly Tarreau 
551da02f51SWilly Tarreau #ifndef _NOLIBC_STDLIB_H
561da02f51SWilly Tarreau char *itoa(int i)
571da02f51SWilly Tarreau {
581da02f51SWilly Tarreau 	static char buf[12];
591da02f51SWilly Tarreau 	int ret;
601da02f51SWilly Tarreau 
611da02f51SWilly Tarreau 	ret = snprintf(buf, sizeof(buf), "%d", i);
621da02f51SWilly Tarreau 	return (ret >= 0 && ret < sizeof(buf)) ? buf : "#err";
631da02f51SWilly Tarreau }
641da02f51SWilly Tarreau #endif
651da02f51SWilly Tarreau 
66362aecb2SWilly Tarreau #define CASE_ERR(err) \
67362aecb2SWilly Tarreau 	case err: return #err
68362aecb2SWilly Tarreau 
69362aecb2SWilly Tarreau /* returns the error name (e.g. "ENOENT") for common errors, "SUCCESS" for 0,
70362aecb2SWilly Tarreau  * or the decimal value for less common ones.
71362aecb2SWilly Tarreau  */
72362aecb2SWilly Tarreau const char *errorname(int err)
73362aecb2SWilly Tarreau {
74362aecb2SWilly Tarreau 	switch (err) {
75362aecb2SWilly Tarreau 	case 0: return "SUCCESS";
76362aecb2SWilly Tarreau 	CASE_ERR(EPERM);
77362aecb2SWilly Tarreau 	CASE_ERR(ENOENT);
78362aecb2SWilly Tarreau 	CASE_ERR(ESRCH);
79362aecb2SWilly Tarreau 	CASE_ERR(EINTR);
80362aecb2SWilly Tarreau 	CASE_ERR(EIO);
81362aecb2SWilly Tarreau 	CASE_ERR(ENXIO);
82362aecb2SWilly Tarreau 	CASE_ERR(E2BIG);
83362aecb2SWilly Tarreau 	CASE_ERR(ENOEXEC);
84362aecb2SWilly Tarreau 	CASE_ERR(EBADF);
85362aecb2SWilly Tarreau 	CASE_ERR(ECHILD);
86362aecb2SWilly Tarreau 	CASE_ERR(EAGAIN);
87362aecb2SWilly Tarreau 	CASE_ERR(ENOMEM);
88362aecb2SWilly Tarreau 	CASE_ERR(EACCES);
89362aecb2SWilly Tarreau 	CASE_ERR(EFAULT);
90362aecb2SWilly Tarreau 	CASE_ERR(ENOTBLK);
91362aecb2SWilly Tarreau 	CASE_ERR(EBUSY);
92362aecb2SWilly Tarreau 	CASE_ERR(EEXIST);
93362aecb2SWilly Tarreau 	CASE_ERR(EXDEV);
94362aecb2SWilly Tarreau 	CASE_ERR(ENODEV);
95362aecb2SWilly Tarreau 	CASE_ERR(ENOTDIR);
96362aecb2SWilly Tarreau 	CASE_ERR(EISDIR);
97362aecb2SWilly Tarreau 	CASE_ERR(EINVAL);
98362aecb2SWilly Tarreau 	CASE_ERR(ENFILE);
99362aecb2SWilly Tarreau 	CASE_ERR(EMFILE);
100362aecb2SWilly Tarreau 	CASE_ERR(ENOTTY);
101362aecb2SWilly Tarreau 	CASE_ERR(ETXTBSY);
102362aecb2SWilly Tarreau 	CASE_ERR(EFBIG);
103362aecb2SWilly Tarreau 	CASE_ERR(ENOSPC);
104362aecb2SWilly Tarreau 	CASE_ERR(ESPIPE);
105362aecb2SWilly Tarreau 	CASE_ERR(EROFS);
106362aecb2SWilly Tarreau 	CASE_ERR(EMLINK);
107362aecb2SWilly Tarreau 	CASE_ERR(EPIPE);
108362aecb2SWilly Tarreau 	CASE_ERR(EDOM);
109362aecb2SWilly Tarreau 	CASE_ERR(ERANGE);
110362aecb2SWilly Tarreau 	CASE_ERR(ENOSYS);
111758f970fSZhangjin Wu 	CASE_ERR(EOVERFLOW);
112362aecb2SWilly Tarreau 	default:
113362aecb2SWilly Tarreau 		return itoa(err);
114362aecb2SWilly Tarreau 	}
115362aecb2SWilly Tarreau }
116362aecb2SWilly Tarreau 
117443de903SThomas Weißschuh static void putcharn(char c, size_t n)
118443de903SThomas Weißschuh {
119443de903SThomas Weißschuh 	char buf[64];
120443de903SThomas Weißschuh 
121443de903SThomas Weißschuh 	memset(buf, c, n);
122443de903SThomas Weißschuh 	buf[n] = '\0';
123443de903SThomas Weißschuh 	fputs(buf, stdout);
124443de903SThomas Weißschuh }
125443de903SThomas Weißschuh 
126362aecb2SWilly Tarreau static int pad_spc(int llen, int cnt, const char *fmt, ...)
127362aecb2SWilly Tarreau {
128362aecb2SWilly Tarreau 	va_list args;
129362aecb2SWilly Tarreau 	int ret;
130362aecb2SWilly Tarreau 
131443de903SThomas Weißschuh 	putcharn(' ', cnt - llen);
132362aecb2SWilly Tarreau 
133362aecb2SWilly Tarreau 	va_start(args, fmt);
134362aecb2SWilly Tarreau 	ret = vfprintf(stdout, fmt, args);
135362aecb2SWilly Tarreau 	va_end(args);
136443de903SThomas Weißschuh 	return ret < 0 ? ret : ret + cnt - llen;
137362aecb2SWilly Tarreau }
138362aecb2SWilly Tarreau 
139362aecb2SWilly Tarreau /* The tests below are intended to be used by the macroes, which evaluate
140362aecb2SWilly Tarreau  * expression <expr>, print the status to stdout, and update the "ret"
141362aecb2SWilly Tarreau  * variable to count failures. The functions themselves return the number
142362aecb2SWilly Tarreau  * of failures, thus either 0 or 1.
143362aecb2SWilly Tarreau  */
144362aecb2SWilly Tarreau 
145362aecb2SWilly Tarreau #define EXPECT_ZR(cond, expr)				\
146a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_zr(expr, llen); } while (0)
147362aecb2SWilly Tarreau 
148362aecb2SWilly Tarreau static int expect_zr(int expr, int llen)
149362aecb2SWilly Tarreau {
150362aecb2SWilly Tarreau 	int ret = !(expr == 0);
151362aecb2SWilly Tarreau 
152362aecb2SWilly Tarreau 	llen += printf(" = %d ", expr);
153a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
154362aecb2SWilly Tarreau 	return ret;
155362aecb2SWilly Tarreau }
156362aecb2SWilly Tarreau 
157362aecb2SWilly Tarreau 
158362aecb2SWilly Tarreau #define EXPECT_NZ(cond, expr, val)			\
159a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_nz(expr, llen; } while (0)
160362aecb2SWilly Tarreau 
161362aecb2SWilly Tarreau static int expect_nz(int expr, int llen)
162362aecb2SWilly Tarreau {
163362aecb2SWilly Tarreau 	int ret = !(expr != 0);
164362aecb2SWilly Tarreau 
165362aecb2SWilly Tarreau 	llen += printf(" = %d ", expr);
166a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
167362aecb2SWilly Tarreau 	return ret;
168362aecb2SWilly Tarreau }
169362aecb2SWilly Tarreau 
170362aecb2SWilly Tarreau 
171362aecb2SWilly Tarreau #define EXPECT_EQ(cond, expr, val)				\
172a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_eq(expr, llen, val); } while (0)
173362aecb2SWilly Tarreau 
174a0994fb9SVincent Dagonneau static int expect_eq(uint64_t expr, int llen, uint64_t val)
175362aecb2SWilly Tarreau {
176362aecb2SWilly Tarreau 	int ret = !(expr == val);
177362aecb2SWilly Tarreau 
1780858aec4SZhangjin Wu 	llen += printf(" = %lld ", (long long)expr);
179a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
180362aecb2SWilly Tarreau 	return ret;
181362aecb2SWilly Tarreau }
182362aecb2SWilly Tarreau 
183362aecb2SWilly Tarreau 
184362aecb2SWilly Tarreau #define EXPECT_NE(cond, expr, val)				\
185a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ne(expr, llen, val); } while (0)
186362aecb2SWilly Tarreau 
187362aecb2SWilly Tarreau static int expect_ne(int expr, int llen, int val)
188362aecb2SWilly Tarreau {
189362aecb2SWilly Tarreau 	int ret = !(expr != val);
190362aecb2SWilly Tarreau 
191362aecb2SWilly Tarreau 	llen += printf(" = %d ", expr);
192a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
193362aecb2SWilly Tarreau 	return ret;
194362aecb2SWilly Tarreau }
195362aecb2SWilly Tarreau 
196362aecb2SWilly Tarreau 
197362aecb2SWilly Tarreau #define EXPECT_GE(cond, expr, val)				\
198a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ge(expr, llen, val); } while (0)
199362aecb2SWilly Tarreau 
200362aecb2SWilly Tarreau static int expect_ge(int expr, int llen, int val)
201362aecb2SWilly Tarreau {
202362aecb2SWilly Tarreau 	int ret = !(expr >= val);
203362aecb2SWilly Tarreau 
204362aecb2SWilly Tarreau 	llen += printf(" = %d ", expr);
205a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
206362aecb2SWilly Tarreau 	return ret;
207362aecb2SWilly Tarreau }
208362aecb2SWilly Tarreau 
209362aecb2SWilly Tarreau 
210362aecb2SWilly Tarreau #define EXPECT_GT(cond, expr, val)				\
211a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_gt(expr, llen, val); } while (0)
212362aecb2SWilly Tarreau 
213362aecb2SWilly Tarreau static int expect_gt(int expr, int llen, int val)
214362aecb2SWilly Tarreau {
215362aecb2SWilly Tarreau 	int ret = !(expr > val);
216362aecb2SWilly Tarreau 
217362aecb2SWilly Tarreau 	llen += printf(" = %d ", expr);
218a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
219362aecb2SWilly Tarreau 	return ret;
220362aecb2SWilly Tarreau }
221362aecb2SWilly Tarreau 
222362aecb2SWilly Tarreau 
223362aecb2SWilly Tarreau #define EXPECT_LE(cond, expr, val)				\
224a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_le(expr, llen, val); } while (0)
225362aecb2SWilly Tarreau 
226362aecb2SWilly Tarreau static int expect_le(int expr, int llen, int val)
227362aecb2SWilly Tarreau {
228362aecb2SWilly Tarreau 	int ret = !(expr <= val);
229362aecb2SWilly Tarreau 
230362aecb2SWilly Tarreau 	llen += printf(" = %d ", expr);
231a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
232362aecb2SWilly Tarreau 	return ret;
233362aecb2SWilly Tarreau }
234362aecb2SWilly Tarreau 
235362aecb2SWilly Tarreau 
236362aecb2SWilly Tarreau #define EXPECT_LT(cond, expr, val)				\
237a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_lt(expr, llen, val); } while (0)
238362aecb2SWilly Tarreau 
239362aecb2SWilly Tarreau static int expect_lt(int expr, int llen, int val)
240362aecb2SWilly Tarreau {
241362aecb2SWilly Tarreau 	int ret = !(expr < val);
242362aecb2SWilly Tarreau 
243362aecb2SWilly Tarreau 	llen += printf(" = %d ", expr);
244a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
245362aecb2SWilly Tarreau 	return ret;
246362aecb2SWilly Tarreau }
247362aecb2SWilly Tarreau 
248362aecb2SWilly Tarreau 
249362aecb2SWilly Tarreau #define EXPECT_SYSZR(cond, expr)				\
250a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_syszr(expr, llen); } while (0)
251362aecb2SWilly Tarreau 
252362aecb2SWilly Tarreau static int expect_syszr(int expr, int llen)
253362aecb2SWilly Tarreau {
254362aecb2SWilly Tarreau 	int ret = 0;
255362aecb2SWilly Tarreau 
256362aecb2SWilly Tarreau 	if (expr) {
257362aecb2SWilly Tarreau 		ret = 1;
258362aecb2SWilly Tarreau 		llen += printf(" = %d %s ", expr, errorname(errno));
259a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
260362aecb2SWilly Tarreau 	} else {
261362aecb2SWilly Tarreau 		llen += printf(" = %d ", expr);
262a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
263362aecb2SWilly Tarreau 	}
264362aecb2SWilly Tarreau 	return ret;
265362aecb2SWilly Tarreau }
266362aecb2SWilly Tarreau 
267362aecb2SWilly Tarreau 
268362aecb2SWilly Tarreau #define EXPECT_SYSEQ(cond, expr, val)				\
269a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_syseq(expr, llen, val); } while (0)
270362aecb2SWilly Tarreau 
271362aecb2SWilly Tarreau static int expect_syseq(int expr, int llen, int val)
272362aecb2SWilly Tarreau {
273362aecb2SWilly Tarreau 	int ret = 0;
274362aecb2SWilly Tarreau 
275362aecb2SWilly Tarreau 	if (expr != val) {
276362aecb2SWilly Tarreau 		ret = 1;
277362aecb2SWilly Tarreau 		llen += printf(" = %d %s ", expr, errorname(errno));
278a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
279362aecb2SWilly Tarreau 	} else {
280362aecb2SWilly Tarreau 		llen += printf(" = %d ", expr);
281a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
282362aecb2SWilly Tarreau 	}
283362aecb2SWilly Tarreau 	return ret;
284362aecb2SWilly Tarreau }
285362aecb2SWilly Tarreau 
286362aecb2SWilly Tarreau 
287362aecb2SWilly Tarreau #define EXPECT_SYSNE(cond, expr, val)				\
288a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_sysne(expr, llen, val); } while (0)
289362aecb2SWilly Tarreau 
290362aecb2SWilly Tarreau static int expect_sysne(int expr, int llen, int val)
291362aecb2SWilly Tarreau {
292362aecb2SWilly Tarreau 	int ret = 0;
293362aecb2SWilly Tarreau 
294362aecb2SWilly Tarreau 	if (expr == val) {
295362aecb2SWilly Tarreau 		ret = 1;
296362aecb2SWilly Tarreau 		llen += printf(" = %d %s ", expr, errorname(errno));
297a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
298362aecb2SWilly Tarreau 	} else {
299362aecb2SWilly Tarreau 		llen += printf(" = %d ", expr);
300a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
301362aecb2SWilly Tarreau 	}
302362aecb2SWilly Tarreau 	return ret;
303362aecb2SWilly Tarreau }
304362aecb2SWilly Tarreau 
305362aecb2SWilly Tarreau 
30675d75a7bSZhangjin Wu #define EXPECT_SYSER2(cond, expr, expret, experr1, experr2)		\
30775d75a7bSZhangjin Wu 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_syserr2(expr, expret, experr1, experr2, llen); } while (0)
308362aecb2SWilly Tarreau 
30975d75a7bSZhangjin Wu #define EXPECT_SYSER(cond, expr, expret, experr)			\
31075d75a7bSZhangjin Wu 	EXPECT_SYSER2(cond, expr, expret, experr, 0)
31175d75a7bSZhangjin Wu 
31275d75a7bSZhangjin Wu static int expect_syserr2(int expr, int expret, int experr1, int experr2, int llen)
313362aecb2SWilly Tarreau {
314362aecb2SWilly Tarreau 	int ret = 0;
315362aecb2SWilly Tarreau 	int _errno = errno;
316362aecb2SWilly Tarreau 
317362aecb2SWilly Tarreau 	llen += printf(" = %d %s ", expr, errorname(_errno));
31875d75a7bSZhangjin Wu 	if (expr != expret || (_errno != experr1 && _errno != experr2)) {
319362aecb2SWilly Tarreau 		ret = 1;
32075d75a7bSZhangjin Wu 		if (experr2 == 0)
32175d75a7bSZhangjin Wu 			llen += printf(" != (%d %s) ", expret, errorname(experr1));
32275d75a7bSZhangjin Wu 		else
32375d75a7bSZhangjin Wu 			llen += printf(" != (%d %s %s) ", expret, errorname(experr1), errorname(experr2));
324a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
325362aecb2SWilly Tarreau 	} else {
326a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
327362aecb2SWilly Tarreau 	}
328362aecb2SWilly Tarreau 	return ret;
329362aecb2SWilly Tarreau }
330362aecb2SWilly Tarreau 
331362aecb2SWilly Tarreau 
332362aecb2SWilly Tarreau #define EXPECT_PTRZR(cond, expr)				\
333a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ptrzr(expr, llen); } while (0)
334362aecb2SWilly Tarreau 
335362aecb2SWilly Tarreau static int expect_ptrzr(const void *expr, int llen)
336362aecb2SWilly Tarreau {
337362aecb2SWilly Tarreau 	int ret = 0;
338362aecb2SWilly Tarreau 
339362aecb2SWilly Tarreau 	llen += printf(" = <%p> ", expr);
340362aecb2SWilly Tarreau 	if (expr) {
341362aecb2SWilly Tarreau 		ret = 1;
342a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
343362aecb2SWilly Tarreau 	} else {
344a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
345362aecb2SWilly Tarreau 	}
346362aecb2SWilly Tarreau 	return ret;
347362aecb2SWilly Tarreau }
348362aecb2SWilly Tarreau 
349362aecb2SWilly Tarreau 
350362aecb2SWilly Tarreau #define EXPECT_PTRNZ(cond, expr)				\
351a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ptrnz(expr, llen); } while (0)
352362aecb2SWilly Tarreau 
353362aecb2SWilly Tarreau static int expect_ptrnz(const void *expr, int llen)
354362aecb2SWilly Tarreau {
355362aecb2SWilly Tarreau 	int ret = 0;
356362aecb2SWilly Tarreau 
357362aecb2SWilly Tarreau 	llen += printf(" = <%p> ", expr);
358362aecb2SWilly Tarreau 	if (!expr) {
359362aecb2SWilly Tarreau 		ret = 1;
360a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
361362aecb2SWilly Tarreau 	} else {
362a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
363362aecb2SWilly Tarreau 	}
364362aecb2SWilly Tarreau 	return ret;
365362aecb2SWilly Tarreau }
366362aecb2SWilly Tarreau 
367362aecb2SWilly Tarreau 
368362aecb2SWilly Tarreau #define EXPECT_STRZR(cond, expr)				\
369a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_strzr(expr, llen); } while (0)
370362aecb2SWilly Tarreau 
371362aecb2SWilly Tarreau static int expect_strzr(const char *expr, int llen)
372362aecb2SWilly Tarreau {
373362aecb2SWilly Tarreau 	int ret = 0;
374362aecb2SWilly Tarreau 
375362aecb2SWilly Tarreau 	llen += printf(" = <%s> ", expr);
376362aecb2SWilly Tarreau 	if (expr) {
377362aecb2SWilly Tarreau 		ret = 1;
378a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
379362aecb2SWilly Tarreau 	} else {
380a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
381362aecb2SWilly Tarreau 	}
382362aecb2SWilly Tarreau 	return ret;
383362aecb2SWilly Tarreau }
384362aecb2SWilly Tarreau 
385362aecb2SWilly Tarreau 
386362aecb2SWilly Tarreau #define EXPECT_STRNZ(cond, expr)				\
387a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_strnz(expr, llen); } while (0)
388362aecb2SWilly Tarreau 
389362aecb2SWilly Tarreau static int expect_strnz(const char *expr, int llen)
390362aecb2SWilly Tarreau {
391362aecb2SWilly Tarreau 	int ret = 0;
392362aecb2SWilly Tarreau 
393362aecb2SWilly Tarreau 	llen += printf(" = <%s> ", expr);
394362aecb2SWilly Tarreau 	if (!expr) {
395362aecb2SWilly Tarreau 		ret = 1;
396a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
397362aecb2SWilly Tarreau 	} else {
398a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
399362aecb2SWilly Tarreau 	}
400362aecb2SWilly Tarreau 	return ret;
401362aecb2SWilly Tarreau }
402362aecb2SWilly Tarreau 
403362aecb2SWilly Tarreau 
404362aecb2SWilly Tarreau #define EXPECT_STREQ(cond, expr, cmp)				\
405a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_streq(expr, llen, cmp); } while (0)
406362aecb2SWilly Tarreau 
407362aecb2SWilly Tarreau static int expect_streq(const char *expr, int llen, const char *cmp)
408362aecb2SWilly Tarreau {
409362aecb2SWilly Tarreau 	int ret = 0;
410362aecb2SWilly Tarreau 
411362aecb2SWilly Tarreau 	llen += printf(" = <%s> ", expr);
412362aecb2SWilly Tarreau 	if (strcmp(expr, cmp) != 0) {
413362aecb2SWilly Tarreau 		ret = 1;
414a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
415362aecb2SWilly Tarreau 	} else {
416a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
417362aecb2SWilly Tarreau 	}
418362aecb2SWilly Tarreau 	return ret;
419362aecb2SWilly Tarreau }
420362aecb2SWilly Tarreau 
421362aecb2SWilly Tarreau 
422362aecb2SWilly Tarreau #define EXPECT_STRNE(cond, expr, cmp)				\
423a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_strne(expr, llen, cmp); } while (0)
424362aecb2SWilly Tarreau 
425362aecb2SWilly Tarreau static int expect_strne(const char *expr, int llen, const char *cmp)
426362aecb2SWilly Tarreau {
427362aecb2SWilly Tarreau 	int ret = 0;
428362aecb2SWilly Tarreau 
429362aecb2SWilly Tarreau 	llen += printf(" = <%s> ", expr);
430362aecb2SWilly Tarreau 	if (strcmp(expr, cmp) == 0) {
431362aecb2SWilly Tarreau 		ret = 1;
432a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
433362aecb2SWilly Tarreau 	} else {
434a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
435362aecb2SWilly Tarreau 	}
436362aecb2SWilly Tarreau 	return ret;
437362aecb2SWilly Tarreau }
438362aecb2SWilly Tarreau 
43923da7bc9SWilly Tarreau 
440362aecb2SWilly Tarreau /* declare tests based on line numbers. There must be exactly one test per line. */
441362aecb2SWilly Tarreau #define CASE_TEST(name) \
442362aecb2SWilly Tarreau 	case __LINE__: llen += printf("%d %s", test, #name);
443362aecb2SWilly Tarreau 
444362aecb2SWilly Tarreau 
445b4844fa0SWilly Tarreau /* used by some syscall tests below */
446b4844fa0SWilly Tarreau int test_getdents64(const char *dir)
447b4844fa0SWilly Tarreau {
448b4844fa0SWilly Tarreau 	char buffer[4096];
449b4844fa0SWilly Tarreau 	int fd, ret;
450b4844fa0SWilly Tarreau 	int err;
451b4844fa0SWilly Tarreau 
452b4844fa0SWilly Tarreau 	ret = fd = open(dir, O_RDONLY | O_DIRECTORY, 0);
453b4844fa0SWilly Tarreau 	if (ret < 0)
454b4844fa0SWilly Tarreau 		return ret;
455b4844fa0SWilly Tarreau 
456b4844fa0SWilly Tarreau 	ret = getdents64(fd, (void *)buffer, sizeof(buffer));
457b4844fa0SWilly Tarreau 	err = errno;
458b4844fa0SWilly Tarreau 	close(fd);
459b4844fa0SWilly Tarreau 
460b4844fa0SWilly Tarreau 	errno = err;
461b4844fa0SWilly Tarreau 	return ret;
462b4844fa0SWilly Tarreau }
463b4844fa0SWilly Tarreau 
464a290296aSAmmar Faizi static int test_getpagesize(void)
465a290296aSAmmar Faizi {
466a290296aSAmmar Faizi 	long x = getpagesize();
467a290296aSAmmar Faizi 	int c;
468a290296aSAmmar Faizi 
469a290296aSAmmar Faizi 	if (x < 0)
470a290296aSAmmar Faizi 		return x;
471a290296aSAmmar Faizi 
472a290296aSAmmar Faizi #if defined(__x86_64__) || defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__)
473a290296aSAmmar Faizi 	/*
474a290296aSAmmar Faizi 	 * x86 family is always 4K page.
475a290296aSAmmar Faizi 	 */
476a290296aSAmmar Faizi 	c = (x == 4096);
477a290296aSAmmar Faizi #elif defined(__aarch64__)
478a290296aSAmmar Faizi 	/*
479a290296aSAmmar Faizi 	 * Linux aarch64 supports three values of page size: 4K, 16K, and 64K
480a290296aSAmmar Faizi 	 * which are selected at kernel compilation time.
481a290296aSAmmar Faizi 	 */
482a290296aSAmmar Faizi 	c = (x == 4096 || x == (16 * 1024) || x == (64 * 1024));
483a290296aSAmmar Faizi #else
484a290296aSAmmar Faizi 	/*
485a290296aSAmmar Faizi 	 * Assuming other architectures must have at least 4K page.
486a290296aSAmmar Faizi 	 */
487a290296aSAmmar Faizi 	c = (x >= 4096);
488a290296aSAmmar Faizi #endif
489a290296aSAmmar Faizi 
490a290296aSAmmar Faizi 	return !c;
491a290296aSAmmar Faizi }
492a290296aSAmmar Faizi 
4933ad09d72SThomas Weißschuh static int test_fork(void)
4943ad09d72SThomas Weißschuh {
4953ad09d72SThomas Weißschuh 	int status;
496ed495f09SZhangjin Wu 	pid_t pid;
497ed495f09SZhangjin Wu 
498ed495f09SZhangjin Wu 	/* flush the printf buffer to avoid child flush it */
499ed495f09SZhangjin Wu 	fflush(stdout);
500ed495f09SZhangjin Wu 	fflush(stderr);
501ed495f09SZhangjin Wu 
502ed495f09SZhangjin Wu 	pid = fork();
5033ad09d72SThomas Weißschuh 
5043ad09d72SThomas Weißschuh 	switch (pid) {
5053ad09d72SThomas Weißschuh 	case -1:
5063ad09d72SThomas Weißschuh 		return 1;
5073ad09d72SThomas Weißschuh 
5083ad09d72SThomas Weißschuh 	case 0:
5093ad09d72SThomas Weißschuh 		exit(123);
5103ad09d72SThomas Weißschuh 
5113ad09d72SThomas Weißschuh 	default:
5123ad09d72SThomas Weißschuh 		pid = waitpid(pid, &status, 0);
5133ad09d72SThomas Weißschuh 
5143ad09d72SThomas Weißschuh 		return pid == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 123;
5153ad09d72SThomas Weißschuh 	}
5163ad09d72SThomas Weißschuh }
5173ad09d72SThomas Weißschuh 
51887b9fa66SThomas Weißschuh static int test_stat_timestamps(void)
51987b9fa66SThomas Weißschuh {
52087b9fa66SThomas Weißschuh 	struct stat st;
52187b9fa66SThomas Weißschuh 
52287b9fa66SThomas Weißschuh 	if (sizeof(st.st_atim.tv_sec) != sizeof(st.st_atime))
52387b9fa66SThomas Weißschuh 		return 1;
52487b9fa66SThomas Weißschuh 
52587b9fa66SThomas Weißschuh 	if (stat("/proc/self/", &st))
52687b9fa66SThomas Weißschuh 		return 1;
52787b9fa66SThomas Weißschuh 
52887b9fa66SThomas Weißschuh 	if (st.st_atim.tv_sec != st.st_atime || st.st_atim.tv_nsec > 1000000000)
52987b9fa66SThomas Weißschuh 		return 1;
53087b9fa66SThomas Weißschuh 
53187b9fa66SThomas Weißschuh 	if (st.st_mtim.tv_sec != st.st_mtime || st.st_mtim.tv_nsec > 1000000000)
53287b9fa66SThomas Weißschuh 		return 1;
53387b9fa66SThomas Weißschuh 
53487b9fa66SThomas Weißschuh 	if (st.st_ctim.tv_sec != st.st_ctime || st.st_ctim.tv_nsec > 1000000000)
53587b9fa66SThomas Weißschuh 		return 1;
53687b9fa66SThomas Weißschuh 
53787b9fa66SThomas Weißschuh 	return 0;
53887b9fa66SThomas Weißschuh }
53987b9fa66SThomas Weißschuh 
540b4844fa0SWilly Tarreau /* Run syscall tests between IDs <min> and <max>.
541b4844fa0SWilly Tarreau  * Return 0 on success, non-zero on failure.
542b4844fa0SWilly Tarreau  */
543b4844fa0SWilly Tarreau int run_syscall(int min, int max)
544b4844fa0SWilly Tarreau {
545957bfa31SZhangjin Wu 	struct timeval tv;
546957bfa31SZhangjin Wu 	struct timezone tz;
547b4844fa0SWilly Tarreau 	struct stat stat_buf;
5483e2d337bSWilly Tarreau 	int euid0;
5497172f1c6SWilly Tarreau 	int proc;
550b4844fa0SWilly Tarreau 	int test;
551b4844fa0SWilly Tarreau 	int tmp;
552b4844fa0SWilly Tarreau 	int ret = 0;
553b4844fa0SWilly Tarreau 	void *p1, *p2;
554b4844fa0SWilly Tarreau 
5557172f1c6SWilly Tarreau 	/* <proc> indicates whether or not /proc is mounted */
5567172f1c6SWilly Tarreau 	proc = stat("/proc", &stat_buf) == 0;
5577172f1c6SWilly Tarreau 
5583e2d337bSWilly Tarreau 	/* this will be used to skip certain tests that can't be run unprivileged */
5593e2d337bSWilly Tarreau 	euid0 = geteuid() == 0;
5603e2d337bSWilly Tarreau 
561b4844fa0SWilly Tarreau 	for (test = min; test >= 0 && test <= max; test++) {
562fddc8f81SThomas Weißschuh 		int llen = 0; /* line length */
563b4844fa0SWilly Tarreau 
564b4844fa0SWilly Tarreau 		/* avoid leaving empty lines below, this will insert holes into
565b4844fa0SWilly Tarreau 		 * test numbers.
566b4844fa0SWilly Tarreau 		 */
567b4844fa0SWilly Tarreau 		switch (test + __LINE__ + 1) {
568b4844fa0SWilly Tarreau 		CASE_TEST(getpid);            EXPECT_SYSNE(1, getpid(), -1); break;
569b4844fa0SWilly Tarreau 		CASE_TEST(getppid);           EXPECT_SYSNE(1, getppid(), -1); break;
5701da02f51SWilly Tarreau #ifdef NOLIBC
571b4844fa0SWilly Tarreau 		CASE_TEST(gettid);            EXPECT_SYSNE(1, gettid(), -1); break;
5721da02f51SWilly Tarreau #endif
573b4844fa0SWilly Tarreau 		CASE_TEST(getpgid_self);      EXPECT_SYSNE(1, getpgid(0), -1); break;
574b4844fa0SWilly Tarreau 		CASE_TEST(getpgid_bad);       EXPECT_SYSER(1, getpgid(-1), -1, ESRCH); break;
575b4844fa0SWilly Tarreau 		CASE_TEST(kill_0);            EXPECT_SYSZR(1, kill(getpid(), 0)); break;
576b4844fa0SWilly Tarreau 		CASE_TEST(kill_CONT);         EXPECT_SYSZR(1, kill(getpid(), 0)); break;
577b4844fa0SWilly Tarreau 		CASE_TEST(kill_BADPID);       EXPECT_SYSER(1, kill(INT_MAX, 0), -1, ESRCH); break;
578b4844fa0SWilly Tarreau 		CASE_TEST(sbrk);              if ((p1 = p2 = sbrk(4096)) != (void *)-1) p2 = sbrk(-4096); EXPECT_SYSZR(1, (p2 == (void *)-1) || p2 == p1); break;
579b4844fa0SWilly Tarreau 		CASE_TEST(brk);               EXPECT_SYSZR(1, brk(sbrk(0))); break;
580b4844fa0SWilly Tarreau 		CASE_TEST(chdir_root);        EXPECT_SYSZR(1, chdir("/")); break;
581b4844fa0SWilly Tarreau 		CASE_TEST(chdir_dot);         EXPECT_SYSZR(1, chdir(".")); break;
582b4844fa0SWilly Tarreau 		CASE_TEST(chdir_blah);        EXPECT_SYSER(1, chdir("/blah"), -1, ENOENT); break;
5837172f1c6SWilly Tarreau 		CASE_TEST(chmod_net);         EXPECT_SYSZR(proc, chmod("/proc/self/net", 0555)); break;
5847172f1c6SWilly Tarreau 		CASE_TEST(chmod_self);        EXPECT_SYSER(proc, chmod("/proc/self", 0555), -1, EPERM); break;
5857172f1c6SWilly Tarreau 		CASE_TEST(chown_self);        EXPECT_SYSER(proc, chown("/proc/self", 0, 0), -1, EPERM); break;
5863e2d337bSWilly Tarreau 		CASE_TEST(chroot_root);       EXPECT_SYSZR(euid0, chroot("/")); break;
587b4844fa0SWilly Tarreau 		CASE_TEST(chroot_blah);       EXPECT_SYSER(1, chroot("/proc/self/blah"), -1, ENOENT); break;
5887172f1c6SWilly Tarreau 		CASE_TEST(chroot_exe);        EXPECT_SYSER(proc, chroot("/proc/self/exe"), -1, ENOTDIR); break;
589b4844fa0SWilly Tarreau 		CASE_TEST(close_m1);          EXPECT_SYSER(1, close(-1), -1, EBADF); break;
590b4844fa0SWilly Tarreau 		CASE_TEST(close_dup);         EXPECT_SYSZR(1, close(dup(0))); break;
591b4844fa0SWilly Tarreau 		CASE_TEST(dup_0);             tmp = dup(0);  EXPECT_SYSNE(1, tmp, -1); close(tmp); break;
592b4844fa0SWilly Tarreau 		CASE_TEST(dup_m1);            tmp = dup(-1); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break;
593b4844fa0SWilly Tarreau 		CASE_TEST(dup2_0);            tmp = dup2(0, 100);  EXPECT_SYSNE(1, tmp, -1); close(tmp); break;
594b4844fa0SWilly Tarreau 		CASE_TEST(dup2_m1);           tmp = dup2(-1, 100); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break;
595b4844fa0SWilly Tarreau 		CASE_TEST(dup3_0);            tmp = dup3(0, 100, 0);  EXPECT_SYSNE(1, tmp, -1); close(tmp); break;
596b4844fa0SWilly Tarreau 		CASE_TEST(dup3_m1);           tmp = dup3(-1, 100, 0); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break;
597b4844fa0SWilly Tarreau 		CASE_TEST(execve_root);       EXPECT_SYSER(1, execve("/", (char*[]){ [0] = "/", [1] = NULL }, NULL), -1, EACCES); break;
5983ad09d72SThomas Weißschuh 		CASE_TEST(fork);              EXPECT_SYSZR(1, test_fork()); break;
599b4844fa0SWilly Tarreau 		CASE_TEST(getdents64_root);   EXPECT_SYSNE(1, test_getdents64("/"), -1); break;
600b4844fa0SWilly Tarreau 		CASE_TEST(getdents64_null);   EXPECT_SYSER(1, test_getdents64("/dev/null"), -1, ENOTDIR); break;
601957bfa31SZhangjin Wu 		CASE_TEST(gettimeofday_tv);   EXPECT_SYSZR(1, gettimeofday(&tv, NULL)); break;
602957bfa31SZhangjin Wu 		CASE_TEST(gettimeofday_tv_tz);EXPECT_SYSZR(1, gettimeofday(&tv, &tz)); break;
603a290296aSAmmar Faizi 		CASE_TEST(getpagesize);       EXPECT_SYSZR(1, test_getpagesize()); break;
604b4844fa0SWilly Tarreau 		CASE_TEST(ioctl_tiocinq);     EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); break;
605b4844fa0SWilly Tarreau 		CASE_TEST(ioctl_tiocinq);     EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); break;
606b4844fa0SWilly Tarreau 		CASE_TEST(link_root1);        EXPECT_SYSER(1, link("/", "/"), -1, EEXIST); break;
607b4844fa0SWilly Tarreau 		CASE_TEST(link_blah);         EXPECT_SYSER(1, link("/proc/self/blah", "/blah"), -1, ENOENT); break;
6083e2d337bSWilly Tarreau 		CASE_TEST(link_dir);          EXPECT_SYSER(euid0, link("/", "/blah"), -1, EPERM); break;
6097172f1c6SWilly Tarreau 		CASE_TEST(link_cross);        EXPECT_SYSER(proc, link("/proc/self/net", "/blah"), -1, EXDEV); break;
610b4844fa0SWilly Tarreau 		CASE_TEST(lseek_m1);          EXPECT_SYSER(1, lseek(-1, 0, SEEK_SET), -1, EBADF); break;
611b4844fa0SWilly Tarreau 		CASE_TEST(lseek_0);           EXPECT_SYSER(1, lseek(0, 0, SEEK_SET), -1, ESPIPE); break;
612b4844fa0SWilly Tarreau 		CASE_TEST(mkdir_root);        EXPECT_SYSER(1, mkdir("/", 0755), -1, EEXIST); break;
613b4844fa0SWilly Tarreau 		CASE_TEST(open_tty);          EXPECT_SYSNE(1, tmp = open("/dev/null", 0), -1); if (tmp != -1) close(tmp); break;
614b4844fa0SWilly Tarreau 		CASE_TEST(open_blah);         EXPECT_SYSER(1, tmp = open("/proc/self/blah", 0), -1, ENOENT); if (tmp != -1) close(tmp); break;
615b4844fa0SWilly Tarreau 		CASE_TEST(poll_null);         EXPECT_SYSZR(1, poll(NULL, 0, 0)); break;
616b4844fa0SWilly Tarreau 		CASE_TEST(poll_stdout);       EXPECT_SYSNE(1, ({ struct pollfd fds = { 1, POLLOUT, 0}; poll(&fds, 1, 0); }), -1); break;
617b4844fa0SWilly Tarreau 		CASE_TEST(poll_fault);        EXPECT_SYSER(1, poll((void *)1, 1, 0), -1, EFAULT); break;
618208aa9d9SThomas Weißschuh 		CASE_TEST(prctl);             EXPECT_SYSER(1, prctl(PR_SET_NAME, (unsigned long)NULL, 0, 0, 0), -1, EFAULT); break;
619b4844fa0SWilly Tarreau 		CASE_TEST(read_badf);         EXPECT_SYSER(1, read(-1, &tmp, 1), -1, EBADF); break;
620b4844fa0SWilly Tarreau 		CASE_TEST(sched_yield);       EXPECT_SYSZR(1, sched_yield()); break;
621b4844fa0SWilly Tarreau 		CASE_TEST(select_null);       EXPECT_SYSZR(1, ({ struct timeval tv = { 0 }; select(0, NULL, NULL, NULL, &tv); })); break;
622b4844fa0SWilly 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;
623b4844fa0SWilly Tarreau 		CASE_TEST(select_fault);      EXPECT_SYSER(1, select(1, (void *)1, NULL, NULL, 0), -1, EFAULT); break;
624b4844fa0SWilly Tarreau 		CASE_TEST(stat_blah);         EXPECT_SYSER(1, stat("/proc/self/blah", &stat_buf), -1, ENOENT); break;
625b4844fa0SWilly Tarreau 		CASE_TEST(stat_fault);        EXPECT_SYSER(1, stat(NULL, &stat_buf), -1, EFAULT); break;
62687b9fa66SThomas Weißschuh 		CASE_TEST(stat_timestamps);   EXPECT_SYSZR(1, test_stat_timestamps()); break;
627b4844fa0SWilly Tarreau 		CASE_TEST(symlink_root);      EXPECT_SYSER(1, symlink("/", "/"), -1, EEXIST); break;
628b4844fa0SWilly Tarreau 		CASE_TEST(unlink_root);       EXPECT_SYSER(1, unlink("/"), -1, EISDIR); break;
629b4844fa0SWilly Tarreau 		CASE_TEST(unlink_blah);       EXPECT_SYSER(1, unlink("/proc/self/blah"), -1, ENOENT); break;
630b4844fa0SWilly Tarreau 		CASE_TEST(wait_child);        EXPECT_SYSER(1, wait(&tmp), -1, ECHILD); break;
631b4844fa0SWilly Tarreau 		CASE_TEST(waitpid_min);       EXPECT_SYSER(1, waitpid(INT_MIN, &tmp, WNOHANG), -1, ESRCH); break;
632b4844fa0SWilly Tarreau 		CASE_TEST(waitpid_child);     EXPECT_SYSER(1, waitpid(getpid(), &tmp, WNOHANG), -1, ECHILD); break;
633b4844fa0SWilly Tarreau 		CASE_TEST(write_badf);        EXPECT_SYSER(1, write(-1, &tmp, 1), -1, EBADF); break;
634b4844fa0SWilly Tarreau 		CASE_TEST(write_zero);        EXPECT_SYSZR(1, write(1, &tmp, 0)); break;
63553fcfafaSThomas Weißschuh 		CASE_TEST(syscall_noargs);    EXPECT_SYSEQ(1, syscall(__NR_getpid), getpid()); break;
636ec8e1b73SZhangjin Wu 		CASE_TEST(syscall_args);      EXPECT_SYSER(1, syscall(__NR_statx, 0, NULL, 0, 0, NULL), -1, EFAULT); break;
637b4844fa0SWilly Tarreau 		case __LINE__:
638b4844fa0SWilly Tarreau 			return ret; /* must be last */
639b4844fa0SWilly Tarreau 		/* note: do not set any defaults so as to permit holes above */
640b4844fa0SWilly Tarreau 		}
641b4844fa0SWilly Tarreau 	}
642b4844fa0SWilly Tarreau 	return ret;
643b4844fa0SWilly Tarreau }
644b4844fa0SWilly Tarreau 
64595bc9894SWilly Tarreau int run_stdlib(int min, int max)
64695bc9894SWilly Tarreau {
64795bc9894SWilly Tarreau 	int test;
64895bc9894SWilly Tarreau 	int tmp;
64995bc9894SWilly Tarreau 	int ret = 0;
65095bc9894SWilly Tarreau 	void *p1, *p2;
65195bc9894SWilly Tarreau 
65295bc9894SWilly Tarreau 	for (test = min; test >= 0 && test <= max; test++) {
653fddc8f81SThomas Weißschuh 		int llen = 0; /* line length */
65495bc9894SWilly Tarreau 
65595bc9894SWilly Tarreau 		/* avoid leaving empty lines below, this will insert holes into
65695bc9894SWilly Tarreau 		 * test numbers.
65795bc9894SWilly Tarreau 		 */
65895bc9894SWilly Tarreau 		switch (test + __LINE__ + 1) {
65995bc9894SWilly Tarreau 		CASE_TEST(getenv_TERM);        EXPECT_STRNZ(1, getenv("TERM")); break;
66095bc9894SWilly Tarreau 		CASE_TEST(getenv_blah);        EXPECT_STRZR(1, getenv("blah")); break;
66195bc9894SWilly Tarreau 		CASE_TEST(setcmp_blah_blah);   EXPECT_EQ(1, strcmp("blah", "blah"), 0); break;
66295bc9894SWilly Tarreau 		CASE_TEST(setcmp_blah_blah2);  EXPECT_NE(1, strcmp("blah", "blah2"), 0); break;
66395bc9894SWilly Tarreau 		CASE_TEST(setncmp_blah_blah);  EXPECT_EQ(1, strncmp("blah", "blah", 10), 0); break;
66495bc9894SWilly Tarreau 		CASE_TEST(setncmp_blah_blah4); EXPECT_EQ(1, strncmp("blah", "blah4", 4), 0); break;
66595bc9894SWilly Tarreau 		CASE_TEST(setncmp_blah_blah5); EXPECT_NE(1, strncmp("blah", "blah5", 5), 0); break;
66695bc9894SWilly Tarreau 		CASE_TEST(setncmp_blah_blah6); EXPECT_NE(1, strncmp("blah", "blah6", 6), 0); break;
66795bc9894SWilly Tarreau 		CASE_TEST(strchr_foobar_o);    EXPECT_STREQ(1, strchr("foobar", 'o'), "oobar"); break;
66895bc9894SWilly Tarreau 		CASE_TEST(strchr_foobar_z);    EXPECT_STRZR(1, strchr("foobar", 'z')); break;
66995bc9894SWilly Tarreau 		CASE_TEST(strrchr_foobar_o);   EXPECT_STREQ(1, strrchr("foobar", 'o'), "obar"); break;
67095bc9894SWilly Tarreau 		CASE_TEST(strrchr_foobar_z);   EXPECT_STRZR(1, strrchr("foobar", 'z')); break;
671c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_20_20);       EXPECT_EQ(1, memcmp("aaa\x20", "aaa\x20", 4), 0); break;
672c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_20_60);       EXPECT_LT(1, memcmp("aaa\x20", "aaa\x60", 4), 0); break;
673c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_60_20);       EXPECT_GT(1, memcmp("aaa\x60", "aaa\x20", 4), 0); break;
674c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_20_e0);       EXPECT_LT(1, memcmp("aaa\x20", "aaa\xe0", 4), 0); break;
675c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_e0_20);       EXPECT_GT(1, memcmp("aaa\xe0", "aaa\x20", 4), 0); break;
676c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_80_e0);       EXPECT_LT(1, memcmp("aaa\x80", "aaa\xe0", 4), 0); break;
677c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_e0_80);       EXPECT_GT(1, memcmp("aaa\xe0", "aaa\x80", 4), 0); break;
678d1209597SVincent Dagonneau 		CASE_TEST(limit_int8_max);          EXPECT_EQ(1, INT8_MAX,         (int8_t)          0x7f); break;
679d1209597SVincent Dagonneau 		CASE_TEST(limit_int8_min);          EXPECT_EQ(1, INT8_MIN,         (int8_t)          0x80); break;
680d1209597SVincent Dagonneau 		CASE_TEST(limit_uint8_max);         EXPECT_EQ(1, UINT8_MAX,        (uint8_t)         0xff); break;
681d1209597SVincent Dagonneau 		CASE_TEST(limit_int16_max);         EXPECT_EQ(1, INT16_MAX,        (int16_t)         0x7fff); break;
682d1209597SVincent Dagonneau 		CASE_TEST(limit_int16_min);         EXPECT_EQ(1, INT16_MIN,        (int16_t)         0x8000); break;
683d1209597SVincent Dagonneau 		CASE_TEST(limit_uint16_max);        EXPECT_EQ(1, UINT16_MAX,       (uint16_t)        0xffff); break;
684d1209597SVincent Dagonneau 		CASE_TEST(limit_int32_max);         EXPECT_EQ(1, INT32_MAX,        (int32_t)         0x7fffffff); break;
685d1209597SVincent Dagonneau 		CASE_TEST(limit_int32_min);         EXPECT_EQ(1, INT32_MIN,        (int32_t)         0x80000000); break;
686d1209597SVincent Dagonneau 		CASE_TEST(limit_uint32_max);        EXPECT_EQ(1, UINT32_MAX,       (uint32_t)        0xffffffff); break;
687d1209597SVincent Dagonneau 		CASE_TEST(limit_int64_max);         EXPECT_EQ(1, INT64_MAX,        (int64_t)         0x7fffffffffffffff); break;
688d1209597SVincent Dagonneau 		CASE_TEST(limit_int64_min);         EXPECT_EQ(1, INT64_MIN,        (int64_t)         0x8000000000000000); break;
689d1209597SVincent Dagonneau 		CASE_TEST(limit_uint64_max);        EXPECT_EQ(1, UINT64_MAX,       (uint64_t)        0xffffffffffffffff); break;
690d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least8_max);    EXPECT_EQ(1, INT_LEAST8_MAX,   (int_least8_t)    0x7f); break;
691d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least8_min);    EXPECT_EQ(1, INT_LEAST8_MIN,   (int_least8_t)    0x80); break;
692d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_least8_max);   EXPECT_EQ(1, UINT_LEAST8_MAX,  (uint_least8_t)   0xff); break;
693d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least16_max);   EXPECT_EQ(1, INT_LEAST16_MAX,  (int_least16_t)   0x7fff); break;
694d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least16_min);   EXPECT_EQ(1, INT_LEAST16_MIN,  (int_least16_t)   0x8000); break;
695d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_least16_max);  EXPECT_EQ(1, UINT_LEAST16_MAX, (uint_least16_t)  0xffff); break;
696d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least32_max);   EXPECT_EQ(1, INT_LEAST32_MAX,  (int_least32_t)   0x7fffffff); break;
697d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least32_min);   EXPECT_EQ(1, INT_LEAST32_MIN,  (int_least32_t)   0x80000000); break;
698d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_least32_max);  EXPECT_EQ(1, UINT_LEAST32_MAX, (uint_least32_t)  0xffffffffU); break;
699d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least64_min);   EXPECT_EQ(1, INT_LEAST64_MIN,  (int_least64_t)   0x8000000000000000LL); break;
700d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least64_max);   EXPECT_EQ(1, INT_LEAST64_MAX,  (int_least64_t)   0x7fffffffffffffffLL); break;
701d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_least64_max);  EXPECT_EQ(1, UINT_LEAST64_MAX, (uint_least64_t)  0xffffffffffffffffULL); break;
702d1209597SVincent Dagonneau 		CASE_TEST(limit_int_fast8_max);     EXPECT_EQ(1, INT_FAST8_MAX,    (int_fast8_t)     0x7f); break;
703d1209597SVincent Dagonneau 		CASE_TEST(limit_int_fast8_min);     EXPECT_EQ(1, INT_FAST8_MIN,    (int_fast8_t)     0x80); break;
704d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_fast8_max);    EXPECT_EQ(1, UINT_FAST8_MAX,   (uint_fast8_t)    0xff); break;
705d1209597SVincent Dagonneau 		CASE_TEST(limit_int_fast16_min);    EXPECT_EQ(1, INT_FAST16_MIN,   (int_fast16_t)    INTPTR_MIN); break;
706d1209597SVincent Dagonneau 		CASE_TEST(limit_int_fast16_max);    EXPECT_EQ(1, INT_FAST16_MAX,   (int_fast16_t)    INTPTR_MAX); break;
707d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_fast16_max);   EXPECT_EQ(1, UINT_FAST16_MAX,  (uint_fast16_t)   UINTPTR_MAX); break;
708d1209597SVincent Dagonneau 		CASE_TEST(limit_int_fast32_min);    EXPECT_EQ(1, INT_FAST32_MIN,   (int_fast32_t)    INTPTR_MIN); break;
709d1209597SVincent Dagonneau 		CASE_TEST(limit_int_fast32_max);    EXPECT_EQ(1, INT_FAST32_MAX,   (int_fast32_t)    INTPTR_MAX); break;
710d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_fast32_max);   EXPECT_EQ(1, UINT_FAST32_MAX,  (uint_fast32_t)   UINTPTR_MAX); break;
711f9bf5944SThomas Weißschuh 		CASE_TEST(limit_int_fast64_min);    EXPECT_EQ(1, INT_FAST64_MIN,   (int_fast64_t)    INT64_MIN); break;
712f9bf5944SThomas Weißschuh 		CASE_TEST(limit_int_fast64_max);    EXPECT_EQ(1, INT_FAST64_MAX,   (int_fast64_t)    INT64_MAX); break;
713f9bf5944SThomas Weißschuh 		CASE_TEST(limit_uint_fast64_max);   EXPECT_EQ(1, UINT_FAST64_MAX,  (uint_fast64_t)   UINT64_MAX); break;
714d1209597SVincent Dagonneau #if __SIZEOF_LONG__ == 8
715d1209597SVincent Dagonneau 		CASE_TEST(limit_intptr_min);        EXPECT_EQ(1, INTPTR_MIN,       (intptr_t)        0x8000000000000000LL); break;
716d1209597SVincent Dagonneau 		CASE_TEST(limit_intptr_max);        EXPECT_EQ(1, INTPTR_MAX,       (intptr_t)        0x7fffffffffffffffLL); break;
717d1209597SVincent Dagonneau 		CASE_TEST(limit_uintptr_max);       EXPECT_EQ(1, UINTPTR_MAX,      (uintptr_t)       0xffffffffffffffffULL); break;
718d1209597SVincent Dagonneau 		CASE_TEST(limit_ptrdiff_min);       EXPECT_EQ(1, PTRDIFF_MIN,      (ptrdiff_t)       0x8000000000000000LL); break;
719d1209597SVincent Dagonneau 		CASE_TEST(limit_ptrdiff_max);       EXPECT_EQ(1, PTRDIFF_MAX,      (ptrdiff_t)       0x7fffffffffffffffLL); break;
720d1209597SVincent Dagonneau 		CASE_TEST(limit_size_max);          EXPECT_EQ(1, SIZE_MAX,         (size_t)          0xffffffffffffffffULL); break;
721d1209597SVincent Dagonneau #elif __SIZEOF_LONG__ == 4
722d1209597SVincent Dagonneau 		CASE_TEST(limit_intptr_min);        EXPECT_EQ(1, INTPTR_MIN,       (intptr_t)        0x80000000); break;
723d1209597SVincent Dagonneau 		CASE_TEST(limit_intptr_max);        EXPECT_EQ(1, INTPTR_MAX,       (intptr_t)        0x7fffffff); break;
724d1209597SVincent Dagonneau 		CASE_TEST(limit_uintptr_max);       EXPECT_EQ(1, UINTPTR_MAX,      (uintptr_t)       0xffffffffU); break;
725d1209597SVincent Dagonneau 		CASE_TEST(limit_ptrdiff_min);       EXPECT_EQ(1, PTRDIFF_MIN,      (ptrdiff_t)       0x80000000); break;
726d1209597SVincent Dagonneau 		CASE_TEST(limit_ptrdiff_max);       EXPECT_EQ(1, PTRDIFF_MAX,      (ptrdiff_t)       0x7fffffff); break;
727d1209597SVincent Dagonneau 		CASE_TEST(limit_size_max);          EXPECT_EQ(1, SIZE_MAX,         (size_t)          0xffffffffU); break;
728d1209597SVincent Dagonneau #else
729d1209597SVincent Dagonneau # warning "__SIZEOF_LONG__ is undefined"
730d1209597SVincent Dagonneau #endif /* __SIZEOF_LONG__ */
73195bc9894SWilly Tarreau 		case __LINE__:
73295bc9894SWilly Tarreau 			return ret; /* must be last */
73395bc9894SWilly Tarreau 		/* note: do not set any defaults so as to permit holes above */
73495bc9894SWilly Tarreau 		}
73595bc9894SWilly Tarreau 	}
73695bc9894SWilly Tarreau 	return ret;
73795bc9894SWilly Tarreau }
73895bc9894SWilly Tarreau 
73969f2cd9fSThomas Weißschuh #define EXPECT_VFPRINTF(c, expected, fmt, ...)				\
74069f2cd9fSThomas Weißschuh 	ret += expect_vfprintf(llen, c, expected, fmt, ##__VA_ARGS__)
74169f2cd9fSThomas Weißschuh 
74269f2cd9fSThomas Weißschuh static int expect_vfprintf(int llen, size_t c, const char *expected, const char *fmt, ...)
74369f2cd9fSThomas Weißschuh {
74469f2cd9fSThomas Weißschuh 	int ret, fd, w, r;
74569f2cd9fSThomas Weißschuh 	char buf[100];
74669f2cd9fSThomas Weißschuh 	FILE *memfile;
74769f2cd9fSThomas Weißschuh 	va_list args;
74869f2cd9fSThomas Weißschuh 
74969f2cd9fSThomas Weißschuh 	fd = memfd_create("vfprintf", 0);
75069f2cd9fSThomas Weißschuh 	if (fd == -1) {
75169f2cd9fSThomas Weißschuh 		pad_spc(llen, 64, "[FAIL]\n");
75269f2cd9fSThomas Weißschuh 		return 1;
75369f2cd9fSThomas Weißschuh 	}
75469f2cd9fSThomas Weißschuh 
75569f2cd9fSThomas Weißschuh 	memfile = fdopen(fd, "w+");
75669f2cd9fSThomas Weißschuh 	if (!memfile) {
75769f2cd9fSThomas Weißschuh 		pad_spc(llen, 64, "[FAIL]\n");
75869f2cd9fSThomas Weißschuh 		return 1;
75969f2cd9fSThomas Weißschuh 	}
76069f2cd9fSThomas Weißschuh 
76169f2cd9fSThomas Weißschuh 	va_start(args, fmt);
76269f2cd9fSThomas Weißschuh 	w = vfprintf(memfile, fmt, args);
76369f2cd9fSThomas Weißschuh 	va_end(args);
76469f2cd9fSThomas Weißschuh 
76569f2cd9fSThomas Weißschuh 	if (w != c) {
76669f2cd9fSThomas Weißschuh 		llen += printf(" written(%d) != %d", w, (int) c);
76769f2cd9fSThomas Weißschuh 		pad_spc(llen, 64, "[FAIL]\n");
76869f2cd9fSThomas Weißschuh 		return 1;
76969f2cd9fSThomas Weißschuh 	}
77069f2cd9fSThomas Weißschuh 
77169f2cd9fSThomas Weißschuh 	fflush(memfile);
77269f2cd9fSThomas Weißschuh 	lseek(fd, 0, SEEK_SET);
77369f2cd9fSThomas Weißschuh 
77469f2cd9fSThomas Weißschuh 	r = read(fd, buf, sizeof(buf) - 1);
77569f2cd9fSThomas Weißschuh 	buf[r] = '\0';
77669f2cd9fSThomas Weißschuh 
77769f2cd9fSThomas Weißschuh 	fclose(memfile);
77869f2cd9fSThomas Weißschuh 
77969f2cd9fSThomas Weißschuh 	if (r != w) {
78069f2cd9fSThomas Weißschuh 		llen += printf(" written(%d) != read(%d)", w, r);
78169f2cd9fSThomas Weißschuh 		pad_spc(llen, 64, "[FAIL]\n");
78269f2cd9fSThomas Weißschuh 		return 1;
78369f2cd9fSThomas Weißschuh 	}
78469f2cd9fSThomas Weißschuh 
78569f2cd9fSThomas Weißschuh 	llen += printf(" \"%s\" = \"%s\"", expected, buf);
78669f2cd9fSThomas Weißschuh 	ret = strncmp(expected, buf, c);
78769f2cd9fSThomas Weißschuh 
78869f2cd9fSThomas Weißschuh 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
78969f2cd9fSThomas Weißschuh 	return ret;
79069f2cd9fSThomas Weißschuh }
79169f2cd9fSThomas Weißschuh 
79269f2cd9fSThomas Weißschuh static int run_vfprintf(int min, int max)
79369f2cd9fSThomas Weißschuh {
79469f2cd9fSThomas Weißschuh 	int test;
79569f2cd9fSThomas Weißschuh 	int tmp;
79669f2cd9fSThomas Weißschuh 	int ret = 0;
79769f2cd9fSThomas Weißschuh 	void *p1, *p2;
79869f2cd9fSThomas Weißschuh 
79969f2cd9fSThomas Weißschuh 	for (test = min; test >= 0 && test <= max; test++) {
800fddc8f81SThomas Weißschuh 		int llen = 0; /* line length */
80169f2cd9fSThomas Weißschuh 
80269f2cd9fSThomas Weißschuh 		/* avoid leaving empty lines below, this will insert holes into
80369f2cd9fSThomas Weißschuh 		 * test numbers.
80469f2cd9fSThomas Weißschuh 		 */
80569f2cd9fSThomas Weißschuh 		switch (test + __LINE__ + 1) {
80669f2cd9fSThomas Weißschuh 		CASE_TEST(empty);        EXPECT_VFPRINTF(0, "", ""); break;
80769f2cd9fSThomas Weißschuh 		CASE_TEST(simple);       EXPECT_VFPRINTF(3, "foo", "foo"); break;
80869f2cd9fSThomas Weißschuh 		CASE_TEST(string);       EXPECT_VFPRINTF(3, "foo", "%s", "foo"); break;
80969f2cd9fSThomas Weißschuh 		CASE_TEST(number);       EXPECT_VFPRINTF(4, "1234", "%d", 1234); break;
81069f2cd9fSThomas Weißschuh 		CASE_TEST(negnumber);    EXPECT_VFPRINTF(5, "-1234", "%d", -1234); break;
81169f2cd9fSThomas Weißschuh 		CASE_TEST(unsigned);     EXPECT_VFPRINTF(5, "12345", "%u", 12345); break;
81269f2cd9fSThomas Weißschuh 		CASE_TEST(char);         EXPECT_VFPRINTF(1, "c", "%c", 'c'); break;
81369f2cd9fSThomas Weißschuh 		CASE_TEST(hex);          EXPECT_VFPRINTF(1, "f", "%x", 0xf); break;
81469f2cd9fSThomas Weißschuh 		CASE_TEST(pointer);      EXPECT_VFPRINTF(3, "0x1", "%p", (void *) 0x1); break;
81569f2cd9fSThomas Weißschuh 		case __LINE__:
81669f2cd9fSThomas Weißschuh 			return ret; /* must be last */
81769f2cd9fSThomas Weißschuh 		/* note: do not set any defaults so as to permit holes above */
81869f2cd9fSThomas Weißschuh 		}
81969f2cd9fSThomas Weißschuh 	}
82069f2cd9fSThomas Weißschuh 	return ret;
82169f2cd9fSThomas Weißschuh }
82269f2cd9fSThomas Weißschuh 
82397357168SThomas Weißschuh static int smash_stack(void)
82497357168SThomas Weißschuh {
82597357168SThomas Weißschuh 	char buf[100];
826e7654c3fSThomas Weißschuh 	volatile char *ptr = buf;
827aa662d12SWilly Tarreau 	size_t i;
82897357168SThomas Weißschuh 
829aa662d12SWilly Tarreau 	for (i = 0; i < 200; i++)
830e7654c3fSThomas Weißschuh 		ptr[i] = 'P';
83197357168SThomas Weißschuh 
83297357168SThomas Weißschuh 	return 1;
83397357168SThomas Weißschuh }
83497357168SThomas Weißschuh 
83597357168SThomas Weißschuh static int run_protection(int min, int max)
83697357168SThomas Weißschuh {
83797357168SThomas Weißschuh 	pid_t pid;
83897357168SThomas Weißschuh 	int llen = 0, status;
83997357168SThomas Weißschuh 
84097357168SThomas Weißschuh 	llen += printf("0 -fstackprotector ");
84197357168SThomas Weißschuh 
842818924d1SThomas Weißschuh #if !defined(_NOLIBC_STACKPROTECTOR)
84397357168SThomas Weißschuh 	llen += printf("not supported");
84497357168SThomas Weißschuh 	pad_spc(llen, 64, "[SKIPPED]\n");
84597357168SThomas Weißschuh 	return 0;
84697357168SThomas Weißschuh #endif
84797357168SThomas Weißschuh 
848818924d1SThomas Weißschuh #if defined(_NOLIBC_STACKPROTECTOR)
84985250921SThomas Weißschuh 	if (!__stack_chk_guard) {
85085250921SThomas Weißschuh 		llen += printf("__stack_chk_guard not initialized");
85185250921SThomas Weißschuh 		pad_spc(llen, 64, "[FAIL]\n");
85285250921SThomas Weißschuh 		return 1;
85385250921SThomas Weißschuh 	}
85485250921SThomas Weißschuh #endif
85585250921SThomas Weißschuh 
85697357168SThomas Weißschuh 	pid = -1;
85797357168SThomas Weißschuh 	pid = fork();
85897357168SThomas Weißschuh 
85997357168SThomas Weißschuh 	switch (pid) {
86097357168SThomas Weißschuh 	case -1:
86197357168SThomas Weißschuh 		llen += printf("fork()");
86297357168SThomas Weißschuh 		pad_spc(llen, 64, "[FAIL]\n");
86397357168SThomas Weißschuh 		return 1;
86497357168SThomas Weißschuh 
86597357168SThomas Weißschuh 	case 0:
86697357168SThomas Weißschuh 		close(STDOUT_FILENO);
86797357168SThomas Weißschuh 		close(STDERR_FILENO);
86897357168SThomas Weißschuh 
8699a75575bSThomas Weißschuh 		prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
87097357168SThomas Weißschuh 		smash_stack();
87197357168SThomas Weißschuh 		return 1;
87297357168SThomas Weißschuh 
87397357168SThomas Weißschuh 	default:
87497357168SThomas Weißschuh 		pid = waitpid(pid, &status, 0);
87597357168SThomas Weißschuh 
87697357168SThomas Weißschuh 		if (pid == -1 || !WIFSIGNALED(status) || WTERMSIG(status) != SIGABRT) {
87797357168SThomas Weißschuh 			llen += printf("waitpid()");
87897357168SThomas Weißschuh 			pad_spc(llen, 64, "[FAIL]\n");
87997357168SThomas Weißschuh 			return 1;
88097357168SThomas Weißschuh 		}
88197357168SThomas Weißschuh 		pad_spc(llen, 64, " [OK]\n");
88297357168SThomas Weißschuh 		return 0;
88397357168SThomas Weißschuh 	}
88497357168SThomas Weißschuh }
88597357168SThomas Weißschuh 
8861a5454f6SWilly Tarreau /* prepare what needs to be prepared for pid 1 (stdio, /dev, /proc, etc) */
8871a5454f6SWilly Tarreau int prepare(void)
8881a5454f6SWilly Tarreau {
8891a5454f6SWilly Tarreau 	struct stat stat_buf;
8901a5454f6SWilly Tarreau 
8911a5454f6SWilly Tarreau 	/* It's possible that /dev doesn't even exist or was not mounted, so
8921a5454f6SWilly Tarreau 	 * we'll try to create it, mount it, or create minimal entries into it.
8931a5454f6SWilly Tarreau 	 * We want at least /dev/null and /dev/console.
8941a5454f6SWilly Tarreau 	 */
8951a5454f6SWilly Tarreau 	if (stat("/dev/.", &stat_buf) == 0 || mkdir("/dev", 0755) == 0) {
8961a5454f6SWilly Tarreau 		if (stat("/dev/console", &stat_buf) != 0 ||
8971a5454f6SWilly Tarreau 		    stat("/dev/null", &stat_buf) != 0) {
8981a5454f6SWilly Tarreau 			/* try devtmpfs first, otherwise fall back to manual creation */
8991a5454f6SWilly Tarreau 			if (mount("/dev", "/dev", "devtmpfs", 0, 0) != 0) {
9001a5454f6SWilly Tarreau 				mknod("/dev/console", 0600 | S_IFCHR, makedev(5, 1));
9011a5454f6SWilly Tarreau 				mknod("/dev/null",    0666 | S_IFCHR, makedev(1, 3));
9021a5454f6SWilly Tarreau 			}
9031a5454f6SWilly Tarreau 		}
9041a5454f6SWilly Tarreau 	}
9051a5454f6SWilly Tarreau 
9061a5454f6SWilly Tarreau 	/* If no /dev/console was found before calling init, stdio is closed so
9071a5454f6SWilly Tarreau 	 * we need to reopen it from /dev/console. If it failed above, it will
9081a5454f6SWilly Tarreau 	 * still fail here and we cannot emit a message anyway.
9091a5454f6SWilly Tarreau 	 */
9101a5454f6SWilly Tarreau 	if (close(dup(1)) == -1) {
9111a5454f6SWilly Tarreau 		int fd = open("/dev/console", O_RDWR);
9121a5454f6SWilly Tarreau 
9131a5454f6SWilly Tarreau 		if (fd >= 0) {
9141a5454f6SWilly Tarreau 			if (fd != 0)
9151a5454f6SWilly Tarreau 				dup2(fd, 0);
9161a5454f6SWilly Tarreau 			if (fd != 1)
9171a5454f6SWilly Tarreau 				dup2(fd, 1);
9181a5454f6SWilly Tarreau 			if (fd != 2)
9191a5454f6SWilly Tarreau 				dup2(fd, 2);
9201a5454f6SWilly Tarreau 			if (fd > 2)
9211a5454f6SWilly Tarreau 				close(fd);
9221a5454f6SWilly Tarreau 			puts("\nSuccessfully reopened /dev/console.");
9231a5454f6SWilly Tarreau 		}
9241a5454f6SWilly Tarreau 	}
9251a5454f6SWilly Tarreau 
9261a5454f6SWilly Tarreau 	/* try to mount /proc if not mounted. Silently fail otherwise */
9271a5454f6SWilly Tarreau 	if (stat("/proc/.", &stat_buf) == 0 || mkdir("/proc", 0755) == 0) {
9281a5454f6SWilly Tarreau 		if (stat("/proc/self", &stat_buf) != 0)
9291a5454f6SWilly Tarreau 			mount("/proc", "/proc", "proc", 0, 0);
9301a5454f6SWilly Tarreau 	}
9311a5454f6SWilly Tarreau 
9321a5454f6SWilly Tarreau 	return 0;
9331a5454f6SWilly Tarreau }
934b4844fa0SWilly Tarreau 
93523da7bc9SWilly Tarreau /* This is the definition of known test names, with their functions */
936c4560bd8SThomas Weißschuh static const struct test test_names[] = {
93723da7bc9SWilly Tarreau 	/* add new tests here */
938b4844fa0SWilly Tarreau 	{ .name = "syscall",    .func = run_syscall    },
93995bc9894SWilly Tarreau 	{ .name = "stdlib",     .func = run_stdlib     },
94069f2cd9fSThomas Weißschuh 	{ .name = "vfprintf",   .func = run_vfprintf   },
94197357168SThomas Weißschuh 	{ .name = "protection", .func = run_protection },
94223da7bc9SWilly Tarreau 	{ 0 }
94323da7bc9SWilly Tarreau };
94423da7bc9SWilly Tarreau 
945362aecb2SWilly Tarreau int main(int argc, char **argv, char **envp)
946362aecb2SWilly Tarreau {
947362aecb2SWilly Tarreau 	int min = 0;
948a36cfc5eSZhangjin Wu 	int max = INT_MAX;
949362aecb2SWilly Tarreau 	int ret = 0;
95023da7bc9SWilly Tarreau 	int err;
95123da7bc9SWilly Tarreau 	int idx;
95223da7bc9SWilly Tarreau 	char *test;
953362aecb2SWilly Tarreau 
954*938b5b98SZhangjin Wu 	argv0 = argv[0];
955362aecb2SWilly Tarreau 	environ = envp;
956362aecb2SWilly Tarreau 
9571a5454f6SWilly Tarreau 	/* when called as init, it's possible that no console was opened, for
9581a5454f6SWilly Tarreau 	 * example if no /dev file system was provided. We'll check that fd#1
9591a5454f6SWilly Tarreau 	 * was opened, and if not we'll attempt to create and open /dev/console
9601a5454f6SWilly Tarreau 	 * and /dev/null that we'll use for later tests.
9611a5454f6SWilly Tarreau 	 */
9621a5454f6SWilly Tarreau 	if (getpid() == 1)
9631a5454f6SWilly Tarreau 		prepare();
9641a5454f6SWilly Tarreau 
96523da7bc9SWilly Tarreau 	/* the definition of a series of tests comes from either argv[1] or the
96623da7bc9SWilly Tarreau 	 * "NOLIBC_TEST" environment variable. It's made of a comma-delimited
96723da7bc9SWilly Tarreau 	 * series of test names and optional ranges:
96823da7bc9SWilly Tarreau 	 *    syscall:5-15[:.*],stdlib:8-10
96923da7bc9SWilly Tarreau 	 */
97023da7bc9SWilly Tarreau 	test = argv[1];
97123da7bc9SWilly Tarreau 	if (!test)
97223da7bc9SWilly Tarreau 		test = getenv("NOLIBC_TEST");
97323da7bc9SWilly Tarreau 
97423da7bc9SWilly Tarreau 	if (test) {
97523da7bc9SWilly Tarreau 		char *comma, *colon, *dash, *value;
97623da7bc9SWilly Tarreau 
97723da7bc9SWilly Tarreau 		do {
97823da7bc9SWilly Tarreau 			comma = strchr(test, ',');
97923da7bc9SWilly Tarreau 			if (comma)
98023da7bc9SWilly Tarreau 				*(comma++) = '\0';
98123da7bc9SWilly Tarreau 
98223da7bc9SWilly Tarreau 			colon = strchr(test, ':');
98323da7bc9SWilly Tarreau 			if (colon)
98423da7bc9SWilly Tarreau 				*(colon++) = '\0';
98523da7bc9SWilly Tarreau 
98623da7bc9SWilly Tarreau 			for (idx = 0; test_names[idx].name; idx++) {
98723da7bc9SWilly Tarreau 				if (strcmp(test, test_names[idx].name) == 0)
98823da7bc9SWilly Tarreau 					break;
98923da7bc9SWilly Tarreau 			}
99023da7bc9SWilly Tarreau 
99123da7bc9SWilly Tarreau 			if (test_names[idx].name) {
99223da7bc9SWilly Tarreau 				/* The test was named, it will be called at least
99323da7bc9SWilly Tarreau 				 * once. We may have an optional range at <colon>
99423da7bc9SWilly Tarreau 				 * here, which defaults to the full range.
99523da7bc9SWilly Tarreau 				 */
99623da7bc9SWilly Tarreau 				do {
997a36cfc5eSZhangjin Wu 					min = 0; max = INT_MAX;
99823da7bc9SWilly Tarreau 					value = colon;
99923da7bc9SWilly Tarreau 					if (value && *value) {
100023da7bc9SWilly Tarreau 						colon = strchr(value, ':');
100123da7bc9SWilly Tarreau 						if (colon)
100223da7bc9SWilly Tarreau 							*(colon++) = '\0';
100323da7bc9SWilly Tarreau 
100423da7bc9SWilly Tarreau 						dash = strchr(value, '-');
100523da7bc9SWilly Tarreau 						if (dash)
100623da7bc9SWilly Tarreau 							*(dash++) = '\0';
100723da7bc9SWilly Tarreau 
100823da7bc9SWilly Tarreau 						/* support :val: :min-max: :min-: :-max: */
100923da7bc9SWilly Tarreau 						if (*value)
101023da7bc9SWilly Tarreau 							min = atoi(value);
101123da7bc9SWilly Tarreau 						if (!dash)
101223da7bc9SWilly Tarreau 							max = min;
101323da7bc9SWilly Tarreau 						else if (*dash)
101423da7bc9SWilly Tarreau 							max = atoi(dash);
101523da7bc9SWilly Tarreau 
101623da7bc9SWilly Tarreau 						value = colon;
101723da7bc9SWilly Tarreau 					}
101823da7bc9SWilly Tarreau 
101923da7bc9SWilly Tarreau 					/* now's time to call the test */
102023da7bc9SWilly Tarreau 					printf("Running test '%s'\n", test_names[idx].name);
102123da7bc9SWilly Tarreau 					err = test_names[idx].func(min, max);
102223da7bc9SWilly Tarreau 					ret += err;
102323da7bc9SWilly Tarreau 					printf("Errors during this test: %d\n\n", err);
102423da7bc9SWilly Tarreau 				} while (colon && *colon);
102523da7bc9SWilly Tarreau 			} else
102623da7bc9SWilly Tarreau 				printf("Ignoring unknown test name '%s'\n", test);
102723da7bc9SWilly Tarreau 
102823da7bc9SWilly Tarreau 			test = comma;
102923da7bc9SWilly Tarreau 		} while (test && *test);
103023da7bc9SWilly Tarreau 	} else {
103123da7bc9SWilly Tarreau 		/* no test mentioned, run everything */
103223da7bc9SWilly Tarreau 		for (idx = 0; test_names[idx].name; idx++) {
103323da7bc9SWilly Tarreau 			printf("Running test '%s'\n", test_names[idx].name);
103423da7bc9SWilly Tarreau 			err = test_names[idx].func(min, max);
103523da7bc9SWilly Tarreau 			ret += err;
103623da7bc9SWilly Tarreau 			printf("Errors during this test: %d\n\n", err);
103723da7bc9SWilly Tarreau 		}
103823da7bc9SWilly Tarreau 	}
103923da7bc9SWilly Tarreau 
1040362aecb2SWilly Tarreau 	printf("Total number of errors: %d\n", ret);
1041f49896d7SWilly Tarreau 
1042f49896d7SWilly Tarreau 	if (getpid() == 1) {
1043f49896d7SWilly Tarreau 		/* we're running as init, there's no other process on the
1044f49896d7SWilly Tarreau 		 * system, thus likely started from a VM for a quick check.
1045f49896d7SWilly Tarreau 		 * Exiting will provoke a kernel panic that may be reported
1046f49896d7SWilly Tarreau 		 * as an error by Qemu or the hypervisor, while stopping
1047f49896d7SWilly Tarreau 		 * cleanly will often be reported as a success. This allows
1048f49896d7SWilly Tarreau 		 * to use the output of this program for bisecting kernels.
1049f49896d7SWilly Tarreau 		 */
1050f49896d7SWilly Tarreau 		printf("Leaving init with final status: %d\n", !!ret);
1051f49896d7SWilly Tarreau 		if (ret == 0)
1052f49896d7SWilly Tarreau 			reboot(LINUX_REBOOT_CMD_POWER_OFF);
1053aa73a86cSWilly Tarreau #if defined(__x86_64__)
1054aa73a86cSWilly Tarreau 		/* QEMU started with "-device isa-debug-exit -no-reboot" will
1055aa73a86cSWilly Tarreau 		 * exit with status code 2N+1 when N is written to 0x501. We
1056aa73a86cSWilly Tarreau 		 * hard-code the syscall here as it's arch-dependent.
1057aa73a86cSWilly Tarreau 		 */
105867eb617aSThomas Weißschuh 		else if (syscall(__NR_ioperm, 0x501, 1, 1) == 0)
10597f291cfaSThomas Weißschuh 			__asm__ volatile ("outb %%al, %%dx" :: "d"(0x501), "a"(0));
1060aa73a86cSWilly Tarreau 		/* if it does nothing, fall back to the regular panic */
1061aa73a86cSWilly Tarreau #endif
1062f49896d7SWilly Tarreau 	}
1063f49896d7SWilly Tarreau 
1064362aecb2SWilly Tarreau 	printf("Exiting with status %d\n", !!ret);
1065362aecb2SWilly Tarreau 	return !!ret;
1066362aecb2SWilly Tarreau }
1067