xref: /openbmc/linux/tools/testing/selftests/nolibc/nolibc-test.c (revision 8525092104ca97d5131fe594081dc176bcce34b5)
1fddc8f81SThomas Weißschuh /* SPDX-License-Identifier: GPL-2.0 */
2362aecb2SWilly Tarreau 
31da02f51SWilly Tarreau #define _GNU_SOURCE
41da02f51SWilly Tarreau 
5362aecb2SWilly Tarreau /* platform-specific include files coming from the compiler */
6362aecb2SWilly Tarreau #include <limits.h>
7362aecb2SWilly Tarreau 
8362aecb2SWilly Tarreau /* libc-specific include files
91da02f51SWilly Tarreau  * The program may be built in 3 ways:
10362aecb2SWilly Tarreau  *   $(CC) -nostdlib -include /path/to/nolibc.h => NOLIBC already defined
111da02f51SWilly Tarreau  *   $(CC) -nostdlib -I/path/to/nolibc/sysroot  => _NOLIBC_* guards are present
121da02f51SWilly Tarreau  *   $(CC) with default libc                    => NOLIBC* never defined
13362aecb2SWilly Tarreau  */
14362aecb2SWilly Tarreau #ifndef NOLIBC
15362aecb2SWilly Tarreau #include <stdio.h>
16362aecb2SWilly Tarreau #include <stdlib.h>
17362aecb2SWilly Tarreau #include <string.h>
181da02f51SWilly Tarreau #ifndef _NOLIBC_STDIO_H
191da02f51SWilly Tarreau /* standard libcs need more includes */
201da02f51SWilly Tarreau #include <linux/reboot.h>
211da02f51SWilly Tarreau #include <sys/io.h>
221da02f51SWilly Tarreau #include <sys/ioctl.h>
2369f2cd9fSThomas Weißschuh #include <sys/mman.h>
241da02f51SWilly Tarreau #include <sys/mount.h>
251da02f51SWilly Tarreau #include <sys/reboot.h>
261da02f51SWilly Tarreau #include <sys/stat.h>
271da02f51SWilly Tarreau #include <sys/syscall.h>
281da02f51SWilly Tarreau #include <sys/sysmacros.h>
291da02f51SWilly Tarreau #include <sys/time.h>
301da02f51SWilly Tarreau #include <sys/wait.h>
311da02f51SWilly Tarreau #include <dirent.h>
321da02f51SWilly Tarreau #include <errno.h>
331da02f51SWilly Tarreau #include <fcntl.h>
341da02f51SWilly Tarreau #include <poll.h>
351da02f51SWilly Tarreau #include <sched.h>
361da02f51SWilly Tarreau #include <signal.h>
371da02f51SWilly Tarreau #include <stdarg.h>
382df07fc5SWilly Tarreau #include <stddef.h>
392df07fc5SWilly Tarreau #include <stdint.h>
401da02f51SWilly Tarreau #include <unistd.h>
411da02f51SWilly Tarreau #endif
42362aecb2SWilly Tarreau #endif
43362aecb2SWilly Tarreau 
44362aecb2SWilly Tarreau /* will be used by nolibc by getenv() */
45362aecb2SWilly Tarreau char **environ;
46362aecb2SWilly Tarreau 
4723da7bc9SWilly Tarreau /* definition of a series of tests */
4823da7bc9SWilly Tarreau struct test {
49fddc8f81SThomas Weißschuh 	const char *name;              /* test name */
50fddc8f81SThomas Weißschuh 	int (*func)(int min, int max); /* handler */
5123da7bc9SWilly Tarreau };
5223da7bc9SWilly Tarreau 
531da02f51SWilly Tarreau #ifndef _NOLIBC_STDLIB_H
541da02f51SWilly Tarreau char *itoa(int i)
551da02f51SWilly Tarreau {
561da02f51SWilly Tarreau 	static char buf[12];
571da02f51SWilly Tarreau 	int ret;
581da02f51SWilly Tarreau 
591da02f51SWilly Tarreau 	ret = snprintf(buf, sizeof(buf), "%d", i);
601da02f51SWilly Tarreau 	return (ret >= 0 && ret < sizeof(buf)) ? buf : "#err";
611da02f51SWilly Tarreau }
621da02f51SWilly Tarreau #endif
631da02f51SWilly Tarreau 
64362aecb2SWilly Tarreau #define CASE_ERR(err) \
65362aecb2SWilly Tarreau 	case err: return #err
66362aecb2SWilly Tarreau 
67362aecb2SWilly Tarreau /* returns the error name (e.g. "ENOENT") for common errors, "SUCCESS" for 0,
68362aecb2SWilly Tarreau  * or the decimal value for less common ones.
69362aecb2SWilly Tarreau  */
70362aecb2SWilly Tarreau const char *errorname(int err)
71362aecb2SWilly Tarreau {
72362aecb2SWilly Tarreau 	switch (err) {
73362aecb2SWilly Tarreau 	case 0: return "SUCCESS";
74362aecb2SWilly Tarreau 	CASE_ERR(EPERM);
75362aecb2SWilly Tarreau 	CASE_ERR(ENOENT);
76362aecb2SWilly Tarreau 	CASE_ERR(ESRCH);
77362aecb2SWilly Tarreau 	CASE_ERR(EINTR);
78362aecb2SWilly Tarreau 	CASE_ERR(EIO);
79362aecb2SWilly Tarreau 	CASE_ERR(ENXIO);
80362aecb2SWilly Tarreau 	CASE_ERR(E2BIG);
81362aecb2SWilly Tarreau 	CASE_ERR(ENOEXEC);
82362aecb2SWilly Tarreau 	CASE_ERR(EBADF);
83362aecb2SWilly Tarreau 	CASE_ERR(ECHILD);
84362aecb2SWilly Tarreau 	CASE_ERR(EAGAIN);
85362aecb2SWilly Tarreau 	CASE_ERR(ENOMEM);
86362aecb2SWilly Tarreau 	CASE_ERR(EACCES);
87362aecb2SWilly Tarreau 	CASE_ERR(EFAULT);
88362aecb2SWilly Tarreau 	CASE_ERR(ENOTBLK);
89362aecb2SWilly Tarreau 	CASE_ERR(EBUSY);
90362aecb2SWilly Tarreau 	CASE_ERR(EEXIST);
91362aecb2SWilly Tarreau 	CASE_ERR(EXDEV);
92362aecb2SWilly Tarreau 	CASE_ERR(ENODEV);
93362aecb2SWilly Tarreau 	CASE_ERR(ENOTDIR);
94362aecb2SWilly Tarreau 	CASE_ERR(EISDIR);
95362aecb2SWilly Tarreau 	CASE_ERR(EINVAL);
96362aecb2SWilly Tarreau 	CASE_ERR(ENFILE);
97362aecb2SWilly Tarreau 	CASE_ERR(EMFILE);
98362aecb2SWilly Tarreau 	CASE_ERR(ENOTTY);
99362aecb2SWilly Tarreau 	CASE_ERR(ETXTBSY);
100362aecb2SWilly Tarreau 	CASE_ERR(EFBIG);
101362aecb2SWilly Tarreau 	CASE_ERR(ENOSPC);
102362aecb2SWilly Tarreau 	CASE_ERR(ESPIPE);
103362aecb2SWilly Tarreau 	CASE_ERR(EROFS);
104362aecb2SWilly Tarreau 	CASE_ERR(EMLINK);
105362aecb2SWilly Tarreau 	CASE_ERR(EPIPE);
106362aecb2SWilly Tarreau 	CASE_ERR(EDOM);
107362aecb2SWilly Tarreau 	CASE_ERR(ERANGE);
108362aecb2SWilly Tarreau 	CASE_ERR(ENOSYS);
109362aecb2SWilly Tarreau 	default:
110362aecb2SWilly Tarreau 		return itoa(err);
111362aecb2SWilly Tarreau 	}
112362aecb2SWilly Tarreau }
113362aecb2SWilly Tarreau 
114443de903SThomas Weißschuh static void putcharn(char c, size_t n)
115443de903SThomas Weißschuh {
116443de903SThomas Weißschuh 	char buf[64];
117443de903SThomas Weißschuh 
118443de903SThomas Weißschuh 	memset(buf, c, n);
119443de903SThomas Weißschuh 	buf[n] = '\0';
120443de903SThomas Weißschuh 	fputs(buf, stdout);
121443de903SThomas Weißschuh }
122443de903SThomas Weißschuh 
123362aecb2SWilly Tarreau static int pad_spc(int llen, int cnt, const char *fmt, ...)
124362aecb2SWilly Tarreau {
125362aecb2SWilly Tarreau 	va_list args;
126362aecb2SWilly Tarreau 	int ret;
127362aecb2SWilly Tarreau 
128443de903SThomas Weißschuh 	putcharn(' ', cnt - llen);
129362aecb2SWilly Tarreau 
130362aecb2SWilly Tarreau 	va_start(args, fmt);
131362aecb2SWilly Tarreau 	ret = vfprintf(stdout, fmt, args);
132362aecb2SWilly Tarreau 	va_end(args);
133443de903SThomas Weißschuh 	return ret < 0 ? ret : ret + cnt - llen;
134362aecb2SWilly Tarreau }
135362aecb2SWilly Tarreau 
136362aecb2SWilly Tarreau /* The tests below are intended to be used by the macroes, which evaluate
137362aecb2SWilly Tarreau  * expression <expr>, print the status to stdout, and update the "ret"
138362aecb2SWilly Tarreau  * variable to count failures. The functions themselves return the number
139362aecb2SWilly Tarreau  * of failures, thus either 0 or 1.
140362aecb2SWilly Tarreau  */
141362aecb2SWilly Tarreau 
142362aecb2SWilly Tarreau #define EXPECT_ZR(cond, expr)				\
143a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_zr(expr, llen); } while (0)
144362aecb2SWilly Tarreau 
145362aecb2SWilly Tarreau static int expect_zr(int expr, int llen)
146362aecb2SWilly Tarreau {
147362aecb2SWilly Tarreau 	int ret = !(expr == 0);
148362aecb2SWilly Tarreau 
149362aecb2SWilly Tarreau 	llen += printf(" = %d ", expr);
150a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
151362aecb2SWilly Tarreau 	return ret;
152362aecb2SWilly Tarreau }
153362aecb2SWilly Tarreau 
154362aecb2SWilly Tarreau 
155362aecb2SWilly Tarreau #define EXPECT_NZ(cond, expr, val)			\
156a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_nz(expr, llen; } while (0)
157362aecb2SWilly Tarreau 
158362aecb2SWilly Tarreau static int expect_nz(int expr, int llen)
159362aecb2SWilly Tarreau {
160362aecb2SWilly Tarreau 	int ret = !(expr != 0);
161362aecb2SWilly Tarreau 
162362aecb2SWilly Tarreau 	llen += printf(" = %d ", expr);
163a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
164362aecb2SWilly Tarreau 	return ret;
165362aecb2SWilly Tarreau }
166362aecb2SWilly Tarreau 
167362aecb2SWilly Tarreau 
168362aecb2SWilly Tarreau #define EXPECT_EQ(cond, expr, val)				\
169a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_eq(expr, llen, val); } while (0)
170362aecb2SWilly Tarreau 
171a0994fb9SVincent Dagonneau static int expect_eq(uint64_t expr, int llen, uint64_t val)
172362aecb2SWilly Tarreau {
173362aecb2SWilly Tarreau 	int ret = !(expr == val);
174362aecb2SWilly Tarreau 
175a0994fb9SVincent Dagonneau 	llen += printf(" = %lld ", expr);
176a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
177362aecb2SWilly Tarreau 	return ret;
178362aecb2SWilly Tarreau }
179362aecb2SWilly Tarreau 
180362aecb2SWilly Tarreau 
181362aecb2SWilly Tarreau #define EXPECT_NE(cond, expr, val)				\
182a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ne(expr, llen, val); } while (0)
183362aecb2SWilly Tarreau 
184362aecb2SWilly Tarreau static int expect_ne(int expr, int llen, int val)
185362aecb2SWilly Tarreau {
186362aecb2SWilly Tarreau 	int ret = !(expr != val);
187362aecb2SWilly Tarreau 
188362aecb2SWilly Tarreau 	llen += printf(" = %d ", expr);
189a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
190362aecb2SWilly Tarreau 	return ret;
191362aecb2SWilly Tarreau }
192362aecb2SWilly Tarreau 
193362aecb2SWilly Tarreau 
194362aecb2SWilly Tarreau #define EXPECT_GE(cond, expr, val)				\
195a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ge(expr, llen, val); } while (0)
196362aecb2SWilly Tarreau 
197362aecb2SWilly Tarreau static int expect_ge(int expr, int llen, int val)
198362aecb2SWilly Tarreau {
199362aecb2SWilly Tarreau 	int ret = !(expr >= val);
200362aecb2SWilly Tarreau 
201362aecb2SWilly Tarreau 	llen += printf(" = %d ", expr);
202a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
203362aecb2SWilly Tarreau 	return ret;
204362aecb2SWilly Tarreau }
205362aecb2SWilly Tarreau 
206362aecb2SWilly Tarreau 
207362aecb2SWilly Tarreau #define EXPECT_GT(cond, expr, val)				\
208a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_gt(expr, llen, val); } while (0)
209362aecb2SWilly Tarreau 
210362aecb2SWilly Tarreau static int expect_gt(int expr, int llen, int val)
211362aecb2SWilly Tarreau {
212362aecb2SWilly Tarreau 	int ret = !(expr > val);
213362aecb2SWilly Tarreau 
214362aecb2SWilly Tarreau 	llen += printf(" = %d ", expr);
215a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
216362aecb2SWilly Tarreau 	return ret;
217362aecb2SWilly Tarreau }
218362aecb2SWilly Tarreau 
219362aecb2SWilly Tarreau 
220362aecb2SWilly Tarreau #define EXPECT_LE(cond, expr, val)				\
221a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_le(expr, llen, val); } while (0)
222362aecb2SWilly Tarreau 
223362aecb2SWilly Tarreau static int expect_le(int expr, int llen, int val)
224362aecb2SWilly Tarreau {
225362aecb2SWilly Tarreau 	int ret = !(expr <= val);
226362aecb2SWilly Tarreau 
227362aecb2SWilly Tarreau 	llen += printf(" = %d ", expr);
228a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
229362aecb2SWilly Tarreau 	return ret;
230362aecb2SWilly Tarreau }
231362aecb2SWilly Tarreau 
232362aecb2SWilly Tarreau 
233362aecb2SWilly Tarreau #define EXPECT_LT(cond, expr, val)				\
234a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_lt(expr, llen, val); } while (0)
235362aecb2SWilly Tarreau 
236362aecb2SWilly Tarreau static int expect_lt(int expr, int llen, int val)
237362aecb2SWilly Tarreau {
238362aecb2SWilly Tarreau 	int ret = !(expr < val);
239362aecb2SWilly Tarreau 
240362aecb2SWilly Tarreau 	llen += printf(" = %d ", expr);
241a0994fb9SVincent Dagonneau 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
242362aecb2SWilly Tarreau 	return ret;
243362aecb2SWilly Tarreau }
244362aecb2SWilly Tarreau 
245362aecb2SWilly Tarreau 
246362aecb2SWilly Tarreau #define EXPECT_SYSZR(cond, expr)				\
247a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_syszr(expr, llen); } while (0)
248362aecb2SWilly Tarreau 
249362aecb2SWilly Tarreau static int expect_syszr(int expr, int llen)
250362aecb2SWilly Tarreau {
251362aecb2SWilly Tarreau 	int ret = 0;
252362aecb2SWilly Tarreau 
253362aecb2SWilly Tarreau 	if (expr) {
254362aecb2SWilly Tarreau 		ret = 1;
255362aecb2SWilly Tarreau 		llen += printf(" = %d %s ", expr, errorname(errno));
256a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
257362aecb2SWilly Tarreau 	} else {
258362aecb2SWilly Tarreau 		llen += printf(" = %d ", expr);
259a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
260362aecb2SWilly Tarreau 	}
261362aecb2SWilly Tarreau 	return ret;
262362aecb2SWilly Tarreau }
263362aecb2SWilly Tarreau 
264362aecb2SWilly Tarreau 
265362aecb2SWilly Tarreau #define EXPECT_SYSEQ(cond, expr, val)				\
266a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_syseq(expr, llen, val); } while (0)
267362aecb2SWilly Tarreau 
268362aecb2SWilly Tarreau static int expect_syseq(int expr, int llen, int val)
269362aecb2SWilly Tarreau {
270362aecb2SWilly Tarreau 	int ret = 0;
271362aecb2SWilly Tarreau 
272362aecb2SWilly Tarreau 	if (expr != val) {
273362aecb2SWilly Tarreau 		ret = 1;
274362aecb2SWilly Tarreau 		llen += printf(" = %d %s ", expr, errorname(errno));
275a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
276362aecb2SWilly Tarreau 	} else {
277362aecb2SWilly Tarreau 		llen += printf(" = %d ", expr);
278a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
279362aecb2SWilly Tarreau 	}
280362aecb2SWilly Tarreau 	return ret;
281362aecb2SWilly Tarreau }
282362aecb2SWilly Tarreau 
283362aecb2SWilly Tarreau 
284362aecb2SWilly Tarreau #define EXPECT_SYSNE(cond, expr, val)				\
285a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_sysne(expr, llen, val); } while (0)
286362aecb2SWilly Tarreau 
287362aecb2SWilly Tarreau static int expect_sysne(int expr, int llen, int val)
288362aecb2SWilly Tarreau {
289362aecb2SWilly Tarreau 	int ret = 0;
290362aecb2SWilly Tarreau 
291362aecb2SWilly Tarreau 	if (expr == val) {
292362aecb2SWilly Tarreau 		ret = 1;
293362aecb2SWilly Tarreau 		llen += printf(" = %d %s ", expr, errorname(errno));
294a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
295362aecb2SWilly Tarreau 	} else {
296362aecb2SWilly Tarreau 		llen += printf(" = %d ", expr);
297a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
298362aecb2SWilly Tarreau 	}
299362aecb2SWilly Tarreau 	return ret;
300362aecb2SWilly Tarreau }
301362aecb2SWilly Tarreau 
302362aecb2SWilly Tarreau 
303362aecb2SWilly Tarreau #define EXPECT_SYSER(cond, expr, expret, experr)			\
304a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_syserr(expr, expret, experr, llen); } while (0)
305362aecb2SWilly Tarreau 
306362aecb2SWilly Tarreau static int expect_syserr(int expr, int expret, int experr, int llen)
307362aecb2SWilly Tarreau {
308362aecb2SWilly Tarreau 	int ret = 0;
309362aecb2SWilly Tarreau 	int _errno = errno;
310362aecb2SWilly Tarreau 
311362aecb2SWilly Tarreau 	llen += printf(" = %d %s ", expr, errorname(_errno));
312362aecb2SWilly Tarreau 	if (expr != expret || _errno != experr) {
313362aecb2SWilly Tarreau 		ret = 1;
314362aecb2SWilly Tarreau 		llen += printf(" != (%d %s) ", expret, errorname(experr));
315a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
316362aecb2SWilly Tarreau 	} else {
317a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
318362aecb2SWilly Tarreau 	}
319362aecb2SWilly Tarreau 	return ret;
320362aecb2SWilly Tarreau }
321362aecb2SWilly Tarreau 
322362aecb2SWilly Tarreau 
323362aecb2SWilly Tarreau #define EXPECT_PTRZR(cond, expr)				\
324a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ptrzr(expr, llen); } while (0)
325362aecb2SWilly Tarreau 
326362aecb2SWilly Tarreau static int expect_ptrzr(const void *expr, int llen)
327362aecb2SWilly Tarreau {
328362aecb2SWilly Tarreau 	int ret = 0;
329362aecb2SWilly Tarreau 
330362aecb2SWilly Tarreau 	llen += printf(" = <%p> ", expr);
331362aecb2SWilly Tarreau 	if (expr) {
332362aecb2SWilly Tarreau 		ret = 1;
333a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
334362aecb2SWilly Tarreau 	} else {
335a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
336362aecb2SWilly Tarreau 	}
337362aecb2SWilly Tarreau 	return ret;
338362aecb2SWilly Tarreau }
339362aecb2SWilly Tarreau 
340362aecb2SWilly Tarreau 
341362aecb2SWilly Tarreau #define EXPECT_PTRNZ(cond, expr)				\
342a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_ptrnz(expr, llen); } while (0)
343362aecb2SWilly Tarreau 
344362aecb2SWilly Tarreau static int expect_ptrnz(const void *expr, int llen)
345362aecb2SWilly Tarreau {
346362aecb2SWilly Tarreau 	int ret = 0;
347362aecb2SWilly Tarreau 
348362aecb2SWilly Tarreau 	llen += printf(" = <%p> ", expr);
349362aecb2SWilly Tarreau 	if (!expr) {
350362aecb2SWilly Tarreau 		ret = 1;
351a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
352362aecb2SWilly Tarreau 	} else {
353a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
354362aecb2SWilly Tarreau 	}
355362aecb2SWilly Tarreau 	return ret;
356362aecb2SWilly Tarreau }
357362aecb2SWilly Tarreau 
358362aecb2SWilly Tarreau 
359362aecb2SWilly Tarreau #define EXPECT_STRZR(cond, expr)				\
360a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_strzr(expr, llen); } while (0)
361362aecb2SWilly Tarreau 
362362aecb2SWilly Tarreau static int expect_strzr(const char *expr, int llen)
363362aecb2SWilly Tarreau {
364362aecb2SWilly Tarreau 	int ret = 0;
365362aecb2SWilly Tarreau 
366362aecb2SWilly Tarreau 	llen += printf(" = <%s> ", expr);
367362aecb2SWilly Tarreau 	if (expr) {
368362aecb2SWilly Tarreau 		ret = 1;
369a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
370362aecb2SWilly Tarreau 	} else {
371a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
372362aecb2SWilly Tarreau 	}
373362aecb2SWilly Tarreau 	return ret;
374362aecb2SWilly Tarreau }
375362aecb2SWilly Tarreau 
376362aecb2SWilly Tarreau 
377362aecb2SWilly Tarreau #define EXPECT_STRNZ(cond, expr)				\
378a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_strnz(expr, llen); } while (0)
379362aecb2SWilly Tarreau 
380362aecb2SWilly Tarreau static int expect_strnz(const char *expr, int llen)
381362aecb2SWilly Tarreau {
382362aecb2SWilly Tarreau 	int ret = 0;
383362aecb2SWilly Tarreau 
384362aecb2SWilly Tarreau 	llen += printf(" = <%s> ", expr);
385362aecb2SWilly Tarreau 	if (!expr) {
386362aecb2SWilly Tarreau 		ret = 1;
387a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
388362aecb2SWilly Tarreau 	} else {
389a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
390362aecb2SWilly Tarreau 	}
391362aecb2SWilly Tarreau 	return ret;
392362aecb2SWilly Tarreau }
393362aecb2SWilly Tarreau 
394362aecb2SWilly Tarreau 
395362aecb2SWilly Tarreau #define EXPECT_STREQ(cond, expr, cmp)				\
396a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_streq(expr, llen, cmp); } while (0)
397362aecb2SWilly Tarreau 
398362aecb2SWilly Tarreau static int expect_streq(const char *expr, int llen, const char *cmp)
399362aecb2SWilly Tarreau {
400362aecb2SWilly Tarreau 	int ret = 0;
401362aecb2SWilly Tarreau 
402362aecb2SWilly Tarreau 	llen += printf(" = <%s> ", expr);
403362aecb2SWilly Tarreau 	if (strcmp(expr, cmp) != 0) {
404362aecb2SWilly Tarreau 		ret = 1;
405a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
406362aecb2SWilly Tarreau 	} else {
407a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
408362aecb2SWilly Tarreau 	}
409362aecb2SWilly Tarreau 	return ret;
410362aecb2SWilly Tarreau }
411362aecb2SWilly Tarreau 
412362aecb2SWilly Tarreau 
413362aecb2SWilly Tarreau #define EXPECT_STRNE(cond, expr, cmp)				\
414a0994fb9SVincent Dagonneau 	do { if (!cond) pad_spc(llen, 64, "[SKIPPED]\n"); else ret += expect_strne(expr, llen, cmp); } while (0)
415362aecb2SWilly Tarreau 
416362aecb2SWilly Tarreau static int expect_strne(const char *expr, int llen, const char *cmp)
417362aecb2SWilly Tarreau {
418362aecb2SWilly Tarreau 	int ret = 0;
419362aecb2SWilly Tarreau 
420362aecb2SWilly Tarreau 	llen += printf(" = <%s> ", expr);
421362aecb2SWilly Tarreau 	if (strcmp(expr, cmp) == 0) {
422362aecb2SWilly Tarreau 		ret = 1;
423a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, "[FAIL]\n");
424362aecb2SWilly Tarreau 	} else {
425a0994fb9SVincent Dagonneau 		llen += pad_spc(llen, 64, " [OK]\n");
426362aecb2SWilly Tarreau 	}
427362aecb2SWilly Tarreau 	return ret;
428362aecb2SWilly Tarreau }
429362aecb2SWilly Tarreau 
43023da7bc9SWilly Tarreau 
431362aecb2SWilly Tarreau /* declare tests based on line numbers. There must be exactly one test per line. */
432362aecb2SWilly Tarreau #define CASE_TEST(name) \
433362aecb2SWilly Tarreau 	case __LINE__: llen += printf("%d %s", test, #name);
434362aecb2SWilly Tarreau 
435362aecb2SWilly Tarreau 
436b4844fa0SWilly Tarreau /* used by some syscall tests below */
437b4844fa0SWilly Tarreau int test_getdents64(const char *dir)
438b4844fa0SWilly Tarreau {
439b4844fa0SWilly Tarreau 	char buffer[4096];
440b4844fa0SWilly Tarreau 	int fd, ret;
441b4844fa0SWilly Tarreau 	int err;
442b4844fa0SWilly Tarreau 
443b4844fa0SWilly Tarreau 	ret = fd = open(dir, O_RDONLY | O_DIRECTORY, 0);
444b4844fa0SWilly Tarreau 	if (ret < 0)
445b4844fa0SWilly Tarreau 		return ret;
446b4844fa0SWilly Tarreau 
447b4844fa0SWilly Tarreau 	ret = getdents64(fd, (void *)buffer, sizeof(buffer));
448b4844fa0SWilly Tarreau 	err = errno;
449b4844fa0SWilly Tarreau 	close(fd);
450b4844fa0SWilly Tarreau 
451b4844fa0SWilly Tarreau 	errno = err;
452b4844fa0SWilly Tarreau 	return ret;
453b4844fa0SWilly Tarreau }
454b4844fa0SWilly Tarreau 
455a290296aSAmmar Faizi static int test_getpagesize(void)
456a290296aSAmmar Faizi {
457a290296aSAmmar Faizi 	long x = getpagesize();
458a290296aSAmmar Faizi 	int c;
459a290296aSAmmar Faizi 
460a290296aSAmmar Faizi 	if (x < 0)
461a290296aSAmmar Faizi 		return x;
462a290296aSAmmar Faizi 
463a290296aSAmmar Faizi #if defined(__x86_64__) || defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__)
464a290296aSAmmar Faizi 	/*
465a290296aSAmmar Faizi 	 * x86 family is always 4K page.
466a290296aSAmmar Faizi 	 */
467a290296aSAmmar Faizi 	c = (x == 4096);
468a290296aSAmmar Faizi #elif defined(__aarch64__)
469a290296aSAmmar Faizi 	/*
470a290296aSAmmar Faizi 	 * Linux aarch64 supports three values of page size: 4K, 16K, and 64K
471a290296aSAmmar Faizi 	 * which are selected at kernel compilation time.
472a290296aSAmmar Faizi 	 */
473a290296aSAmmar Faizi 	c = (x == 4096 || x == (16 * 1024) || x == (64 * 1024));
474a290296aSAmmar Faizi #else
475a290296aSAmmar Faizi 	/*
476a290296aSAmmar Faizi 	 * Assuming other architectures must have at least 4K page.
477a290296aSAmmar Faizi 	 */
478a290296aSAmmar Faizi 	c = (x >= 4096);
479a290296aSAmmar Faizi #endif
480a290296aSAmmar Faizi 
481a290296aSAmmar Faizi 	return !c;
482a290296aSAmmar Faizi }
483a290296aSAmmar Faizi 
4843ad09d72SThomas Weißschuh static int test_fork(void)
4853ad09d72SThomas Weißschuh {
4863ad09d72SThomas Weißschuh 	int status;
4873ad09d72SThomas Weißschuh 	pid_t pid = fork();
4883ad09d72SThomas Weißschuh 
4893ad09d72SThomas Weißschuh 	switch (pid) {
4903ad09d72SThomas Weißschuh 	case -1:
4913ad09d72SThomas Weißschuh 		return 1;
4923ad09d72SThomas Weißschuh 
4933ad09d72SThomas Weißschuh 	case 0:
4943ad09d72SThomas Weißschuh 		exit(123);
4953ad09d72SThomas Weißschuh 
4963ad09d72SThomas Weißschuh 	default:
4973ad09d72SThomas Weißschuh 		pid = waitpid(pid, &status, 0);
4983ad09d72SThomas Weißschuh 
4993ad09d72SThomas Weißschuh 		return pid == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 123;
5003ad09d72SThomas Weißschuh 	}
5013ad09d72SThomas Weißschuh }
5023ad09d72SThomas Weißschuh 
503b4844fa0SWilly Tarreau /* Run syscall tests between IDs <min> and <max>.
504b4844fa0SWilly Tarreau  * Return 0 on success, non-zero on failure.
505b4844fa0SWilly Tarreau  */
506b4844fa0SWilly Tarreau int run_syscall(int min, int max)
507b4844fa0SWilly Tarreau {
508b4844fa0SWilly Tarreau 	struct stat stat_buf;
5093e2d337bSWilly Tarreau 	int euid0;
5107172f1c6SWilly Tarreau 	int proc;
511b4844fa0SWilly Tarreau 	int test;
512b4844fa0SWilly Tarreau 	int tmp;
513b4844fa0SWilly Tarreau 	int ret = 0;
514b4844fa0SWilly Tarreau 	void *p1, *p2;
515b4844fa0SWilly Tarreau 
5167172f1c6SWilly Tarreau 	/* <proc> indicates whether or not /proc is mounted */
5177172f1c6SWilly Tarreau 	proc = stat("/proc", &stat_buf) == 0;
5187172f1c6SWilly Tarreau 
5193e2d337bSWilly Tarreau 	/* this will be used to skip certain tests that can't be run unprivileged */
5203e2d337bSWilly Tarreau 	euid0 = geteuid() == 0;
5213e2d337bSWilly Tarreau 
522b4844fa0SWilly Tarreau 	for (test = min; test >= 0 && test <= max; test++) {
523fddc8f81SThomas Weißschuh 		int llen = 0; /* line length */
524b4844fa0SWilly Tarreau 
525b4844fa0SWilly Tarreau 		/* avoid leaving empty lines below, this will insert holes into
526b4844fa0SWilly Tarreau 		 * test numbers.
527b4844fa0SWilly Tarreau 		 */
528b4844fa0SWilly Tarreau 		switch (test + __LINE__ + 1) {
529b4844fa0SWilly Tarreau 		CASE_TEST(getpid);            EXPECT_SYSNE(1, getpid(), -1); break;
530b4844fa0SWilly Tarreau 		CASE_TEST(getppid);           EXPECT_SYSNE(1, getppid(), -1); break;
5311da02f51SWilly Tarreau #ifdef NOLIBC
532b4844fa0SWilly Tarreau 		CASE_TEST(gettid);            EXPECT_SYSNE(1, gettid(), -1); break;
5331da02f51SWilly Tarreau #endif
534b4844fa0SWilly Tarreau 		CASE_TEST(getpgid_self);      EXPECT_SYSNE(1, getpgid(0), -1); break;
535b4844fa0SWilly Tarreau 		CASE_TEST(getpgid_bad);       EXPECT_SYSER(1, getpgid(-1), -1, ESRCH); break;
536b4844fa0SWilly Tarreau 		CASE_TEST(kill_0);            EXPECT_SYSZR(1, kill(getpid(), 0)); break;
537b4844fa0SWilly Tarreau 		CASE_TEST(kill_CONT);         EXPECT_SYSZR(1, kill(getpid(), 0)); break;
538b4844fa0SWilly Tarreau 		CASE_TEST(kill_BADPID);       EXPECT_SYSER(1, kill(INT_MAX, 0), -1, ESRCH); break;
539b4844fa0SWilly Tarreau 		CASE_TEST(sbrk);              if ((p1 = p2 = sbrk(4096)) != (void *)-1) p2 = sbrk(-4096); EXPECT_SYSZR(1, (p2 == (void *)-1) || p2 == p1); break;
540b4844fa0SWilly Tarreau 		CASE_TEST(brk);               EXPECT_SYSZR(1, brk(sbrk(0))); break;
541b4844fa0SWilly Tarreau 		CASE_TEST(chdir_root);        EXPECT_SYSZR(1, chdir("/")); break;
542b4844fa0SWilly Tarreau 		CASE_TEST(chdir_dot);         EXPECT_SYSZR(1, chdir(".")); break;
543b4844fa0SWilly Tarreau 		CASE_TEST(chdir_blah);        EXPECT_SYSER(1, chdir("/blah"), -1, ENOENT); break;
5447172f1c6SWilly Tarreau 		CASE_TEST(chmod_net);         EXPECT_SYSZR(proc, chmod("/proc/self/net", 0555)); break;
5457172f1c6SWilly Tarreau 		CASE_TEST(chmod_self);        EXPECT_SYSER(proc, chmod("/proc/self", 0555), -1, EPERM); break;
5467172f1c6SWilly Tarreau 		CASE_TEST(chown_self);        EXPECT_SYSER(proc, chown("/proc/self", 0, 0), -1, EPERM); break;
5473e2d337bSWilly Tarreau 		CASE_TEST(chroot_root);       EXPECT_SYSZR(euid0, chroot("/")); break;
548b4844fa0SWilly Tarreau 		CASE_TEST(chroot_blah);       EXPECT_SYSER(1, chroot("/proc/self/blah"), -1, ENOENT); break;
5497172f1c6SWilly Tarreau 		CASE_TEST(chroot_exe);        EXPECT_SYSER(proc, chroot("/proc/self/exe"), -1, ENOTDIR); break;
550b4844fa0SWilly Tarreau 		CASE_TEST(close_m1);          EXPECT_SYSER(1, close(-1), -1, EBADF); break;
551b4844fa0SWilly Tarreau 		CASE_TEST(close_dup);         EXPECT_SYSZR(1, close(dup(0))); break;
552b4844fa0SWilly Tarreau 		CASE_TEST(dup_0);             tmp = dup(0);  EXPECT_SYSNE(1, tmp, -1); close(tmp); break;
553b4844fa0SWilly Tarreau 		CASE_TEST(dup_m1);            tmp = dup(-1); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break;
554b4844fa0SWilly Tarreau 		CASE_TEST(dup2_0);            tmp = dup2(0, 100);  EXPECT_SYSNE(1, tmp, -1); close(tmp); break;
555b4844fa0SWilly Tarreau 		CASE_TEST(dup2_m1);           tmp = dup2(-1, 100); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break;
556b4844fa0SWilly Tarreau 		CASE_TEST(dup3_0);            tmp = dup3(0, 100, 0);  EXPECT_SYSNE(1, tmp, -1); close(tmp); break;
557b4844fa0SWilly Tarreau 		CASE_TEST(dup3_m1);           tmp = dup3(-1, 100, 0); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break;
558b4844fa0SWilly Tarreau 		CASE_TEST(execve_root);       EXPECT_SYSER(1, execve("/", (char*[]){ [0] = "/", [1] = NULL }, NULL), -1, EACCES); break;
5593ad09d72SThomas Weißschuh 		CASE_TEST(fork);              EXPECT_SYSZR(1, test_fork()); break;
560b4844fa0SWilly Tarreau 		CASE_TEST(getdents64_root);   EXPECT_SYSNE(1, test_getdents64("/"), -1); break;
561b4844fa0SWilly Tarreau 		CASE_TEST(getdents64_null);   EXPECT_SYSER(1, test_getdents64("/dev/null"), -1, ENOTDIR); break;
562b4844fa0SWilly Tarreau 		CASE_TEST(gettimeofday_null); EXPECT_SYSZR(1, gettimeofday(NULL, NULL)); break;
5631da02f51SWilly Tarreau #ifdef NOLIBC
564b4844fa0SWilly Tarreau 		CASE_TEST(gettimeofday_bad1); EXPECT_SYSER(1, gettimeofday((void *)1, NULL), -1, EFAULT); break;
565b4844fa0SWilly Tarreau 		CASE_TEST(gettimeofday_bad2); EXPECT_SYSER(1, gettimeofday(NULL, (void *)1), -1, EFAULT); break;
566b4844fa0SWilly Tarreau 		CASE_TEST(gettimeofday_bad2); EXPECT_SYSER(1, gettimeofday(NULL, (void *)1), -1, EFAULT); break;
5671da02f51SWilly Tarreau #endif
568a290296aSAmmar Faizi 		CASE_TEST(getpagesize);       EXPECT_SYSZR(1, test_getpagesize()); break;
569b4844fa0SWilly Tarreau 		CASE_TEST(ioctl_tiocinq);     EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); break;
570b4844fa0SWilly Tarreau 		CASE_TEST(ioctl_tiocinq);     EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); break;
571b4844fa0SWilly Tarreau 		CASE_TEST(link_root1);        EXPECT_SYSER(1, link("/", "/"), -1, EEXIST); break;
572b4844fa0SWilly Tarreau 		CASE_TEST(link_blah);         EXPECT_SYSER(1, link("/proc/self/blah", "/blah"), -1, ENOENT); break;
5733e2d337bSWilly Tarreau 		CASE_TEST(link_dir);          EXPECT_SYSER(euid0, link("/", "/blah"), -1, EPERM); break;
5747172f1c6SWilly Tarreau 		CASE_TEST(link_cross);        EXPECT_SYSER(proc, link("/proc/self/net", "/blah"), -1, EXDEV); break;
575b4844fa0SWilly Tarreau 		CASE_TEST(lseek_m1);          EXPECT_SYSER(1, lseek(-1, 0, SEEK_SET), -1, EBADF); break;
576b4844fa0SWilly Tarreau 		CASE_TEST(lseek_0);           EXPECT_SYSER(1, lseek(0, 0, SEEK_SET), -1, ESPIPE); break;
577b4844fa0SWilly Tarreau 		CASE_TEST(mkdir_root);        EXPECT_SYSER(1, mkdir("/", 0755), -1, EEXIST); break;
578b4844fa0SWilly Tarreau 		CASE_TEST(open_tty);          EXPECT_SYSNE(1, tmp = open("/dev/null", 0), -1); if (tmp != -1) close(tmp); break;
579b4844fa0SWilly Tarreau 		CASE_TEST(open_blah);         EXPECT_SYSER(1, tmp = open("/proc/self/blah", 0), -1, ENOENT); if (tmp != -1) close(tmp); break;
580b4844fa0SWilly Tarreau 		CASE_TEST(poll_null);         EXPECT_SYSZR(1, poll(NULL, 0, 0)); break;
581b4844fa0SWilly Tarreau 		CASE_TEST(poll_stdout);       EXPECT_SYSNE(1, ({ struct pollfd fds = { 1, POLLOUT, 0}; poll(&fds, 1, 0); }), -1); break;
582b4844fa0SWilly Tarreau 		CASE_TEST(poll_fault);        EXPECT_SYSER(1, poll((void *)1, 1, 0), -1, EFAULT); break;
583b4844fa0SWilly Tarreau 		CASE_TEST(read_badf);         EXPECT_SYSER(1, read(-1, &tmp, 1), -1, EBADF); break;
584b4844fa0SWilly Tarreau 		CASE_TEST(sched_yield);       EXPECT_SYSZR(1, sched_yield()); break;
585b4844fa0SWilly Tarreau 		CASE_TEST(select_null);       EXPECT_SYSZR(1, ({ struct timeval tv = { 0 }; select(0, NULL, NULL, NULL, &tv); })); break;
586b4844fa0SWilly 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;
587b4844fa0SWilly Tarreau 		CASE_TEST(select_fault);      EXPECT_SYSER(1, select(1, (void *)1, NULL, NULL, 0), -1, EFAULT); break;
588b4844fa0SWilly Tarreau 		CASE_TEST(stat_blah);         EXPECT_SYSER(1, stat("/proc/self/blah", &stat_buf), -1, ENOENT); break;
589b4844fa0SWilly Tarreau 		CASE_TEST(stat_fault);        EXPECT_SYSER(1, stat(NULL, &stat_buf), -1, EFAULT); break;
590b4844fa0SWilly Tarreau 		CASE_TEST(symlink_root);      EXPECT_SYSER(1, symlink("/", "/"), -1, EEXIST); break;
591b4844fa0SWilly Tarreau 		CASE_TEST(unlink_root);       EXPECT_SYSER(1, unlink("/"), -1, EISDIR); break;
592b4844fa0SWilly Tarreau 		CASE_TEST(unlink_blah);       EXPECT_SYSER(1, unlink("/proc/self/blah"), -1, ENOENT); break;
593b4844fa0SWilly Tarreau 		CASE_TEST(wait_child);        EXPECT_SYSER(1, wait(&tmp), -1, ECHILD); break;
594b4844fa0SWilly Tarreau 		CASE_TEST(waitpid_min);       EXPECT_SYSER(1, waitpid(INT_MIN, &tmp, WNOHANG), -1, ESRCH); break;
595b4844fa0SWilly Tarreau 		CASE_TEST(waitpid_child);     EXPECT_SYSER(1, waitpid(getpid(), &tmp, WNOHANG), -1, ECHILD); break;
596b4844fa0SWilly Tarreau 		CASE_TEST(write_badf);        EXPECT_SYSER(1, write(-1, &tmp, 1), -1, EBADF); break;
597b4844fa0SWilly Tarreau 		CASE_TEST(write_zero);        EXPECT_SYSZR(1, write(1, &tmp, 0)); break;
59853fcfafaSThomas Weißschuh 		CASE_TEST(syscall_noargs);    EXPECT_SYSEQ(1, syscall(__NR_getpid), getpid()); break;
599ec8e1b73SZhangjin Wu 		CASE_TEST(syscall_args);      EXPECT_SYSER(1, syscall(__NR_statx, 0, NULL, 0, 0, NULL), -1, EFAULT); break;
600b4844fa0SWilly Tarreau 		case __LINE__:
601b4844fa0SWilly Tarreau 			return ret; /* must be last */
602b4844fa0SWilly Tarreau 		/* note: do not set any defaults so as to permit holes above */
603b4844fa0SWilly Tarreau 		}
604b4844fa0SWilly Tarreau 	}
605b4844fa0SWilly Tarreau 	return ret;
606b4844fa0SWilly Tarreau }
607b4844fa0SWilly Tarreau 
60895bc9894SWilly Tarreau int run_stdlib(int min, int max)
60995bc9894SWilly Tarreau {
61095bc9894SWilly Tarreau 	int test;
61195bc9894SWilly Tarreau 	int tmp;
61295bc9894SWilly Tarreau 	int ret = 0;
61395bc9894SWilly Tarreau 	void *p1, *p2;
61495bc9894SWilly Tarreau 
61595bc9894SWilly Tarreau 	for (test = min; test >= 0 && test <= max; test++) {
616fddc8f81SThomas Weißschuh 		int llen = 0; /* line length */
61795bc9894SWilly Tarreau 
61895bc9894SWilly Tarreau 		/* avoid leaving empty lines below, this will insert holes into
61995bc9894SWilly Tarreau 		 * test numbers.
62095bc9894SWilly Tarreau 		 */
62195bc9894SWilly Tarreau 		switch (test + __LINE__ + 1) {
62295bc9894SWilly Tarreau 		CASE_TEST(getenv_TERM);        EXPECT_STRNZ(1, getenv("TERM")); break;
62395bc9894SWilly Tarreau 		CASE_TEST(getenv_blah);        EXPECT_STRZR(1, getenv("blah")); break;
62495bc9894SWilly Tarreau 		CASE_TEST(setcmp_blah_blah);   EXPECT_EQ(1, strcmp("blah", "blah"), 0); break;
62595bc9894SWilly Tarreau 		CASE_TEST(setcmp_blah_blah2);  EXPECT_NE(1, strcmp("blah", "blah2"), 0); break;
62695bc9894SWilly Tarreau 		CASE_TEST(setncmp_blah_blah);  EXPECT_EQ(1, strncmp("blah", "blah", 10), 0); break;
62795bc9894SWilly Tarreau 		CASE_TEST(setncmp_blah_blah4); EXPECT_EQ(1, strncmp("blah", "blah4", 4), 0); break;
62895bc9894SWilly Tarreau 		CASE_TEST(setncmp_blah_blah5); EXPECT_NE(1, strncmp("blah", "blah5", 5), 0); break;
62995bc9894SWilly Tarreau 		CASE_TEST(setncmp_blah_blah6); EXPECT_NE(1, strncmp("blah", "blah6", 6), 0); break;
63095bc9894SWilly Tarreau 		CASE_TEST(strchr_foobar_o);    EXPECT_STREQ(1, strchr("foobar", 'o'), "oobar"); break;
63195bc9894SWilly Tarreau 		CASE_TEST(strchr_foobar_z);    EXPECT_STRZR(1, strchr("foobar", 'z')); break;
63295bc9894SWilly Tarreau 		CASE_TEST(strrchr_foobar_o);   EXPECT_STREQ(1, strrchr("foobar", 'o'), "obar"); break;
63395bc9894SWilly Tarreau 		CASE_TEST(strrchr_foobar_z);   EXPECT_STRZR(1, strrchr("foobar", 'z')); break;
634c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_20_20);       EXPECT_EQ(1, memcmp("aaa\x20", "aaa\x20", 4), 0); break;
635c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_20_60);       EXPECT_LT(1, memcmp("aaa\x20", "aaa\x60", 4), 0); break;
636c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_60_20);       EXPECT_GT(1, memcmp("aaa\x60", "aaa\x20", 4), 0); break;
637c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_20_e0);       EXPECT_LT(1, memcmp("aaa\x20", "aaa\xe0", 4), 0); break;
638c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_e0_20);       EXPECT_GT(1, memcmp("aaa\xe0", "aaa\x20", 4), 0); break;
639c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_80_e0);       EXPECT_LT(1, memcmp("aaa\x80", "aaa\xe0", 4), 0); break;
640c80b5a0aSWilly Tarreau 		CASE_TEST(memcmp_e0_80);       EXPECT_GT(1, memcmp("aaa\xe0", "aaa\x80", 4), 0); break;
641d1209597SVincent Dagonneau 		CASE_TEST(limit_int8_max);          EXPECT_EQ(1, INT8_MAX,         (int8_t)          0x7f); break;
642d1209597SVincent Dagonneau 		CASE_TEST(limit_int8_min);          EXPECT_EQ(1, INT8_MIN,         (int8_t)          0x80); break;
643d1209597SVincent Dagonneau 		CASE_TEST(limit_uint8_max);         EXPECT_EQ(1, UINT8_MAX,        (uint8_t)         0xff); break;
644d1209597SVincent Dagonneau 		CASE_TEST(limit_int16_max);         EXPECT_EQ(1, INT16_MAX,        (int16_t)         0x7fff); break;
645d1209597SVincent Dagonneau 		CASE_TEST(limit_int16_min);         EXPECT_EQ(1, INT16_MIN,        (int16_t)         0x8000); break;
646d1209597SVincent Dagonneau 		CASE_TEST(limit_uint16_max);        EXPECT_EQ(1, UINT16_MAX,       (uint16_t)        0xffff); break;
647d1209597SVincent Dagonneau 		CASE_TEST(limit_int32_max);         EXPECT_EQ(1, INT32_MAX,        (int32_t)         0x7fffffff); break;
648d1209597SVincent Dagonneau 		CASE_TEST(limit_int32_min);         EXPECT_EQ(1, INT32_MIN,        (int32_t)         0x80000000); break;
649d1209597SVincent Dagonneau 		CASE_TEST(limit_uint32_max);        EXPECT_EQ(1, UINT32_MAX,       (uint32_t)        0xffffffff); break;
650d1209597SVincent Dagonneau 		CASE_TEST(limit_int64_max);         EXPECT_EQ(1, INT64_MAX,        (int64_t)         0x7fffffffffffffff); break;
651d1209597SVincent Dagonneau 		CASE_TEST(limit_int64_min);         EXPECT_EQ(1, INT64_MIN,        (int64_t)         0x8000000000000000); break;
652d1209597SVincent Dagonneau 		CASE_TEST(limit_uint64_max);        EXPECT_EQ(1, UINT64_MAX,       (uint64_t)        0xffffffffffffffff); break;
653d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least8_max);    EXPECT_EQ(1, INT_LEAST8_MAX,   (int_least8_t)    0x7f); break;
654d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least8_min);    EXPECT_EQ(1, INT_LEAST8_MIN,   (int_least8_t)    0x80); break;
655d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_least8_max);   EXPECT_EQ(1, UINT_LEAST8_MAX,  (uint_least8_t)   0xff); break;
656d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least16_max);   EXPECT_EQ(1, INT_LEAST16_MAX,  (int_least16_t)   0x7fff); break;
657d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least16_min);   EXPECT_EQ(1, INT_LEAST16_MIN,  (int_least16_t)   0x8000); break;
658d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_least16_max);  EXPECT_EQ(1, UINT_LEAST16_MAX, (uint_least16_t)  0xffff); break;
659d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least32_max);   EXPECT_EQ(1, INT_LEAST32_MAX,  (int_least32_t)   0x7fffffff); break;
660d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least32_min);   EXPECT_EQ(1, INT_LEAST32_MIN,  (int_least32_t)   0x80000000); break;
661d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_least32_max);  EXPECT_EQ(1, UINT_LEAST32_MAX, (uint_least32_t)  0xffffffffU); break;
662d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least64_min);   EXPECT_EQ(1, INT_LEAST64_MIN,  (int_least64_t)   0x8000000000000000LL); break;
663d1209597SVincent Dagonneau 		CASE_TEST(limit_int_least64_max);   EXPECT_EQ(1, INT_LEAST64_MAX,  (int_least64_t)   0x7fffffffffffffffLL); break;
664d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_least64_max);  EXPECT_EQ(1, UINT_LEAST64_MAX, (uint_least64_t)  0xffffffffffffffffULL); break;
665d1209597SVincent Dagonneau 		CASE_TEST(limit_int_fast8_max);     EXPECT_EQ(1, INT_FAST8_MAX,    (int_fast8_t)     0x7f); break;
666d1209597SVincent Dagonneau 		CASE_TEST(limit_int_fast8_min);     EXPECT_EQ(1, INT_FAST8_MIN,    (int_fast8_t)     0x80); break;
667d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_fast8_max);    EXPECT_EQ(1, UINT_FAST8_MAX,   (uint_fast8_t)    0xff); break;
668d1209597SVincent Dagonneau 		CASE_TEST(limit_int_fast16_min);    EXPECT_EQ(1, INT_FAST16_MIN,   (int_fast16_t)    INTPTR_MIN); break;
669d1209597SVincent Dagonneau 		CASE_TEST(limit_int_fast16_max);    EXPECT_EQ(1, INT_FAST16_MAX,   (int_fast16_t)    INTPTR_MAX); break;
670d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_fast16_max);   EXPECT_EQ(1, UINT_FAST16_MAX,  (uint_fast16_t)   UINTPTR_MAX); break;
671d1209597SVincent Dagonneau 		CASE_TEST(limit_int_fast32_min);    EXPECT_EQ(1, INT_FAST32_MIN,   (int_fast32_t)    INTPTR_MIN); break;
672d1209597SVincent Dagonneau 		CASE_TEST(limit_int_fast32_max);    EXPECT_EQ(1, INT_FAST32_MAX,   (int_fast32_t)    INTPTR_MAX); break;
673d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_fast32_max);   EXPECT_EQ(1, UINT_FAST32_MAX,  (uint_fast32_t)   UINTPTR_MAX); break;
674d1209597SVincent Dagonneau 		CASE_TEST(limit_int_fast64_min);    EXPECT_EQ(1, INT_FAST64_MIN,   (int_fast64_t)    INTPTR_MIN); break;
675d1209597SVincent Dagonneau 		CASE_TEST(limit_int_fast64_max);    EXPECT_EQ(1, INT_FAST64_MAX,   (int_fast64_t)    INTPTR_MAX); break;
676d1209597SVincent Dagonneau 		CASE_TEST(limit_uint_fast64_max);   EXPECT_EQ(1, UINT_FAST64_MAX,  (uint_fast64_t)   UINTPTR_MAX); break;
677d1209597SVincent Dagonneau #if __SIZEOF_LONG__ == 8
678d1209597SVincent Dagonneau 		CASE_TEST(limit_intptr_min);        EXPECT_EQ(1, INTPTR_MIN,       (intptr_t)        0x8000000000000000LL); break;
679d1209597SVincent Dagonneau 		CASE_TEST(limit_intptr_max);        EXPECT_EQ(1, INTPTR_MAX,       (intptr_t)        0x7fffffffffffffffLL); break;
680d1209597SVincent Dagonneau 		CASE_TEST(limit_uintptr_max);       EXPECT_EQ(1, UINTPTR_MAX,      (uintptr_t)       0xffffffffffffffffULL); break;
681d1209597SVincent Dagonneau 		CASE_TEST(limit_ptrdiff_min);       EXPECT_EQ(1, PTRDIFF_MIN,      (ptrdiff_t)       0x8000000000000000LL); break;
682d1209597SVincent Dagonneau 		CASE_TEST(limit_ptrdiff_max);       EXPECT_EQ(1, PTRDIFF_MAX,      (ptrdiff_t)       0x7fffffffffffffffLL); break;
683d1209597SVincent Dagonneau 		CASE_TEST(limit_size_max);          EXPECT_EQ(1, SIZE_MAX,         (size_t)          0xffffffffffffffffULL); break;
684d1209597SVincent Dagonneau #elif __SIZEOF_LONG__ == 4
685d1209597SVincent Dagonneau 		CASE_TEST(limit_intptr_min);        EXPECT_EQ(1, INTPTR_MIN,       (intptr_t)        0x80000000); break;
686d1209597SVincent Dagonneau 		CASE_TEST(limit_intptr_max);        EXPECT_EQ(1, INTPTR_MAX,       (intptr_t)        0x7fffffff); break;
687d1209597SVincent Dagonneau 		CASE_TEST(limit_uintptr_max);       EXPECT_EQ(1, UINTPTR_MAX,      (uintptr_t)       0xffffffffU); break;
688d1209597SVincent Dagonneau 		CASE_TEST(limit_ptrdiff_min);       EXPECT_EQ(1, PTRDIFF_MIN,      (ptrdiff_t)       0x80000000); break;
689d1209597SVincent Dagonneau 		CASE_TEST(limit_ptrdiff_max);       EXPECT_EQ(1, PTRDIFF_MAX,      (ptrdiff_t)       0x7fffffff); break;
690d1209597SVincent Dagonneau 		CASE_TEST(limit_size_max);          EXPECT_EQ(1, SIZE_MAX,         (size_t)          0xffffffffU); break;
691d1209597SVincent Dagonneau #else
692d1209597SVincent Dagonneau # warning "__SIZEOF_LONG__ is undefined"
693d1209597SVincent Dagonneau #endif /* __SIZEOF_LONG__ */
69495bc9894SWilly Tarreau 		case __LINE__:
69595bc9894SWilly Tarreau 			return ret; /* must be last */
69695bc9894SWilly Tarreau 		/* note: do not set any defaults so as to permit holes above */
69795bc9894SWilly Tarreau 		}
69895bc9894SWilly Tarreau 	}
69995bc9894SWilly Tarreau 	return ret;
70095bc9894SWilly Tarreau }
70195bc9894SWilly Tarreau 
70269f2cd9fSThomas Weißschuh #define EXPECT_VFPRINTF(c, expected, fmt, ...)				\
70369f2cd9fSThomas Weißschuh 	ret += expect_vfprintf(llen, c, expected, fmt, ##__VA_ARGS__)
70469f2cd9fSThomas Weißschuh 
70569f2cd9fSThomas Weißschuh static int expect_vfprintf(int llen, size_t c, const char *expected, const char *fmt, ...)
70669f2cd9fSThomas Weißschuh {
70769f2cd9fSThomas Weißschuh 	int ret, fd, w, r;
70869f2cd9fSThomas Weißschuh 	char buf[100];
70969f2cd9fSThomas Weißschuh 	FILE *memfile;
71069f2cd9fSThomas Weißschuh 	va_list args;
71169f2cd9fSThomas Weißschuh 
71269f2cd9fSThomas Weißschuh 	fd = memfd_create("vfprintf", 0);
71369f2cd9fSThomas Weißschuh 	if (fd == -1) {
71469f2cd9fSThomas Weißschuh 		pad_spc(llen, 64, "[FAIL]\n");
71569f2cd9fSThomas Weißschuh 		return 1;
71669f2cd9fSThomas Weißschuh 	}
71769f2cd9fSThomas Weißschuh 
71869f2cd9fSThomas Weißschuh 	memfile = fdopen(fd, "w+");
71969f2cd9fSThomas Weißschuh 	if (!memfile) {
72069f2cd9fSThomas Weißschuh 		pad_spc(llen, 64, "[FAIL]\n");
72169f2cd9fSThomas Weißschuh 		return 1;
72269f2cd9fSThomas Weißschuh 	}
72369f2cd9fSThomas Weißschuh 
72469f2cd9fSThomas Weißschuh 	va_start(args, fmt);
72569f2cd9fSThomas Weißschuh 	w = vfprintf(memfile, fmt, args);
72669f2cd9fSThomas Weißschuh 	va_end(args);
72769f2cd9fSThomas Weißschuh 
72869f2cd9fSThomas Weißschuh 	if (w != c) {
72969f2cd9fSThomas Weißschuh 		llen += printf(" written(%d) != %d", w, (int) c);
73069f2cd9fSThomas Weißschuh 		pad_spc(llen, 64, "[FAIL]\n");
73169f2cd9fSThomas Weißschuh 		return 1;
73269f2cd9fSThomas Weißschuh 	}
73369f2cd9fSThomas Weißschuh 
73469f2cd9fSThomas Weißschuh 	fflush(memfile);
73569f2cd9fSThomas Weißschuh 	lseek(fd, 0, SEEK_SET);
73669f2cd9fSThomas Weißschuh 
73769f2cd9fSThomas Weißschuh 	r = read(fd, buf, sizeof(buf) - 1);
73869f2cd9fSThomas Weißschuh 	buf[r] = '\0';
73969f2cd9fSThomas Weißschuh 
74069f2cd9fSThomas Weißschuh 	fclose(memfile);
74169f2cd9fSThomas Weißschuh 
74269f2cd9fSThomas Weißschuh 	if (r != w) {
74369f2cd9fSThomas Weißschuh 		llen += printf(" written(%d) != read(%d)", w, r);
74469f2cd9fSThomas Weißschuh 		pad_spc(llen, 64, "[FAIL]\n");
74569f2cd9fSThomas Weißschuh 		return 1;
74669f2cd9fSThomas Weißschuh 	}
74769f2cd9fSThomas Weißschuh 
74869f2cd9fSThomas Weißschuh 	llen += printf(" \"%s\" = \"%s\"", expected, buf);
74969f2cd9fSThomas Weißschuh 	ret = strncmp(expected, buf, c);
75069f2cd9fSThomas Weißschuh 
75169f2cd9fSThomas Weißschuh 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
75269f2cd9fSThomas Weißschuh 	return ret;
75369f2cd9fSThomas Weißschuh }
75469f2cd9fSThomas Weißschuh 
75569f2cd9fSThomas Weißschuh static int run_vfprintf(int min, int max)
75669f2cd9fSThomas Weißschuh {
75769f2cd9fSThomas Weißschuh 	int test;
75869f2cd9fSThomas Weißschuh 	int tmp;
75969f2cd9fSThomas Weißschuh 	int ret = 0;
76069f2cd9fSThomas Weißschuh 	void *p1, *p2;
76169f2cd9fSThomas Weißschuh 
76269f2cd9fSThomas Weißschuh 	for (test = min; test >= 0 && test <= max; test++) {
763fddc8f81SThomas Weißschuh 		int llen = 0; /* line length */
76469f2cd9fSThomas Weißschuh 
76569f2cd9fSThomas Weißschuh 		/* avoid leaving empty lines below, this will insert holes into
76669f2cd9fSThomas Weißschuh 		 * test numbers.
76769f2cd9fSThomas Weißschuh 		 */
76869f2cd9fSThomas Weißschuh 		switch (test + __LINE__ + 1) {
76969f2cd9fSThomas Weißschuh 		CASE_TEST(empty);        EXPECT_VFPRINTF(0, "", ""); break;
77069f2cd9fSThomas Weißschuh 		CASE_TEST(simple);       EXPECT_VFPRINTF(3, "foo", "foo"); break;
77169f2cd9fSThomas Weißschuh 		CASE_TEST(string);       EXPECT_VFPRINTF(3, "foo", "%s", "foo"); break;
77269f2cd9fSThomas Weißschuh 		CASE_TEST(number);       EXPECT_VFPRINTF(4, "1234", "%d", 1234); break;
77369f2cd9fSThomas Weißschuh 		CASE_TEST(negnumber);    EXPECT_VFPRINTF(5, "-1234", "%d", -1234); break;
77469f2cd9fSThomas Weißschuh 		CASE_TEST(unsigned);     EXPECT_VFPRINTF(5, "12345", "%u", 12345); break;
77569f2cd9fSThomas Weißschuh 		CASE_TEST(char);         EXPECT_VFPRINTF(1, "c", "%c", 'c'); break;
77669f2cd9fSThomas Weißschuh 		CASE_TEST(hex);          EXPECT_VFPRINTF(1, "f", "%x", 0xf); break;
77769f2cd9fSThomas Weißschuh 		CASE_TEST(pointer);      EXPECT_VFPRINTF(3, "0x1", "%p", (void *) 0x1); break;
77869f2cd9fSThomas Weißschuh 		case __LINE__:
77969f2cd9fSThomas Weißschuh 			return ret; /* must be last */
78069f2cd9fSThomas Weißschuh 		/* note: do not set any defaults so as to permit holes above */
78169f2cd9fSThomas Weißschuh 		}
78269f2cd9fSThomas Weißschuh 	}
78369f2cd9fSThomas Weißschuh 	return ret;
78469f2cd9fSThomas Weißschuh }
78569f2cd9fSThomas Weißschuh 
78697357168SThomas Weißschuh static int smash_stack(void)
78797357168SThomas Weißschuh {
78897357168SThomas Weißschuh 	char buf[100];
789e7654c3fSThomas Weißschuh 	volatile char *ptr = buf;
790aa662d12SWilly Tarreau 	size_t i;
79197357168SThomas Weißschuh 
792aa662d12SWilly Tarreau 	for (i = 0; i < 200; i++)
793e7654c3fSThomas Weißschuh 		ptr[i] = 'P';
79497357168SThomas Weißschuh 
79597357168SThomas Weißschuh 	return 1;
79697357168SThomas Weißschuh }
79797357168SThomas Weißschuh 
79897357168SThomas Weißschuh static int run_protection(int min, int max)
79997357168SThomas Weißschuh {
80097357168SThomas Weißschuh 	pid_t pid;
80197357168SThomas Weißschuh 	int llen = 0, status;
80297357168SThomas Weißschuh 
80397357168SThomas Weißschuh 	llen += printf("0 -fstackprotector ");
80497357168SThomas Weißschuh 
80597357168SThomas Weißschuh #if !defined(NOLIBC_STACKPROTECTOR)
80697357168SThomas Weißschuh 	llen += printf("not supported");
80797357168SThomas Weißschuh 	pad_spc(llen, 64, "[SKIPPED]\n");
80897357168SThomas Weißschuh 	return 0;
80997357168SThomas Weißschuh #endif
81097357168SThomas Weißschuh 
811*85250921SThomas Weißschuh #if defined(NOLIBC_STACKPROTECTOR)
812*85250921SThomas Weißschuh 	if (!__stack_chk_guard) {
813*85250921SThomas Weißschuh 		llen += printf("__stack_chk_guard not initialized");
814*85250921SThomas Weißschuh 		pad_spc(llen, 64, "[FAIL]\n");
815*85250921SThomas Weißschuh 		return 1;
816*85250921SThomas Weißschuh 	}
817*85250921SThomas Weißschuh #endif
818*85250921SThomas Weißschuh 
81997357168SThomas Weißschuh 	pid = -1;
82097357168SThomas Weißschuh 	pid = fork();
82197357168SThomas Weißschuh 
82297357168SThomas Weißschuh 	switch (pid) {
82397357168SThomas Weißschuh 	case -1:
82497357168SThomas Weißschuh 		llen += printf("fork()");
82597357168SThomas Weißschuh 		pad_spc(llen, 64, "[FAIL]\n");
82697357168SThomas Weißschuh 		return 1;
82797357168SThomas Weißschuh 
82897357168SThomas Weißschuh 	case 0:
82997357168SThomas Weißschuh 		close(STDOUT_FILENO);
83097357168SThomas Weißschuh 		close(STDERR_FILENO);
83197357168SThomas Weißschuh 
83297357168SThomas Weißschuh 		smash_stack();
83397357168SThomas Weißschuh 		return 1;
83497357168SThomas Weißschuh 
83597357168SThomas Weißschuh 	default:
83697357168SThomas Weißschuh 		pid = waitpid(pid, &status, 0);
83797357168SThomas Weißschuh 
83897357168SThomas Weißschuh 		if (pid == -1 || !WIFSIGNALED(status) || WTERMSIG(status) != SIGABRT) {
83997357168SThomas Weißschuh 			llen += printf("waitpid()");
84097357168SThomas Weißschuh 			pad_spc(llen, 64, "[FAIL]\n");
84197357168SThomas Weißschuh 			return 1;
84297357168SThomas Weißschuh 		}
84397357168SThomas Weißschuh 		pad_spc(llen, 64, " [OK]\n");
84497357168SThomas Weißschuh 		return 0;
84597357168SThomas Weißschuh 	}
84697357168SThomas Weißschuh }
84797357168SThomas Weißschuh 
8481a5454f6SWilly Tarreau /* prepare what needs to be prepared for pid 1 (stdio, /dev, /proc, etc) */
8491a5454f6SWilly Tarreau int prepare(void)
8501a5454f6SWilly Tarreau {
8511a5454f6SWilly Tarreau 	struct stat stat_buf;
8521a5454f6SWilly Tarreau 
8531a5454f6SWilly Tarreau 	/* It's possible that /dev doesn't even exist or was not mounted, so
8541a5454f6SWilly Tarreau 	 * we'll try to create it, mount it, or create minimal entries into it.
8551a5454f6SWilly Tarreau 	 * We want at least /dev/null and /dev/console.
8561a5454f6SWilly Tarreau 	 */
8571a5454f6SWilly Tarreau 	if (stat("/dev/.", &stat_buf) == 0 || mkdir("/dev", 0755) == 0) {
8581a5454f6SWilly Tarreau 		if (stat("/dev/console", &stat_buf) != 0 ||
8591a5454f6SWilly Tarreau 		    stat("/dev/null", &stat_buf) != 0) {
8601a5454f6SWilly Tarreau 			/* try devtmpfs first, otherwise fall back to manual creation */
8611a5454f6SWilly Tarreau 			if (mount("/dev", "/dev", "devtmpfs", 0, 0) != 0) {
8621a5454f6SWilly Tarreau 				mknod("/dev/console", 0600 | S_IFCHR, makedev(5, 1));
8631a5454f6SWilly Tarreau 				mknod("/dev/null",    0666 | S_IFCHR, makedev(1, 3));
8641a5454f6SWilly Tarreau 			}
8651a5454f6SWilly Tarreau 		}
8661a5454f6SWilly Tarreau 	}
8671a5454f6SWilly Tarreau 
8681a5454f6SWilly Tarreau 	/* If no /dev/console was found before calling init, stdio is closed so
8691a5454f6SWilly Tarreau 	 * we need to reopen it from /dev/console. If it failed above, it will
8701a5454f6SWilly Tarreau 	 * still fail here and we cannot emit a message anyway.
8711a5454f6SWilly Tarreau 	 */
8721a5454f6SWilly Tarreau 	if (close(dup(1)) == -1) {
8731a5454f6SWilly Tarreau 		int fd = open("/dev/console", O_RDWR);
8741a5454f6SWilly Tarreau 
8751a5454f6SWilly Tarreau 		if (fd >= 0) {
8761a5454f6SWilly Tarreau 			if (fd != 0)
8771a5454f6SWilly Tarreau 				dup2(fd, 0);
8781a5454f6SWilly Tarreau 			if (fd != 1)
8791a5454f6SWilly Tarreau 				dup2(fd, 1);
8801a5454f6SWilly Tarreau 			if (fd != 2)
8811a5454f6SWilly Tarreau 				dup2(fd, 2);
8821a5454f6SWilly Tarreau 			if (fd > 2)
8831a5454f6SWilly Tarreau 				close(fd);
8841a5454f6SWilly Tarreau 			puts("\nSuccessfully reopened /dev/console.");
8851a5454f6SWilly Tarreau 		}
8861a5454f6SWilly Tarreau 	}
8871a5454f6SWilly Tarreau 
8881a5454f6SWilly Tarreau 	/* try to mount /proc if not mounted. Silently fail otherwise */
8891a5454f6SWilly Tarreau 	if (stat("/proc/.", &stat_buf) == 0 || mkdir("/proc", 0755) == 0) {
8901a5454f6SWilly Tarreau 		if (stat("/proc/self", &stat_buf) != 0)
8911a5454f6SWilly Tarreau 			mount("/proc", "/proc", "proc", 0, 0);
8921a5454f6SWilly Tarreau 	}
8931a5454f6SWilly Tarreau 
8941a5454f6SWilly Tarreau 	return 0;
8951a5454f6SWilly Tarreau }
896b4844fa0SWilly Tarreau 
89723da7bc9SWilly Tarreau /* This is the definition of known test names, with their functions */
898c4560bd8SThomas Weißschuh static const struct test test_names[] = {
89923da7bc9SWilly Tarreau 	/* add new tests here */
900b4844fa0SWilly Tarreau 	{ .name = "syscall",    .func = run_syscall    },
90195bc9894SWilly Tarreau 	{ .name = "stdlib",     .func = run_stdlib     },
90269f2cd9fSThomas Weißschuh 	{ .name = "vfprintf",   .func = run_vfprintf   },
90397357168SThomas Weißschuh 	{ .name = "protection", .func = run_protection },
90423da7bc9SWilly Tarreau 	{ 0 }
90523da7bc9SWilly Tarreau };
90623da7bc9SWilly Tarreau 
907362aecb2SWilly Tarreau int main(int argc, char **argv, char **envp)
908362aecb2SWilly Tarreau {
909362aecb2SWilly Tarreau 	int min = 0;
910362aecb2SWilly Tarreau 	int max = __INT_MAX__;
911362aecb2SWilly Tarreau 	int ret = 0;
91223da7bc9SWilly Tarreau 	int err;
91323da7bc9SWilly Tarreau 	int idx;
91423da7bc9SWilly Tarreau 	char *test;
915362aecb2SWilly Tarreau 
916362aecb2SWilly Tarreau 	environ = envp;
917362aecb2SWilly Tarreau 
9181a5454f6SWilly Tarreau 	/* when called as init, it's possible that no console was opened, for
9191a5454f6SWilly Tarreau 	 * example if no /dev file system was provided. We'll check that fd#1
9201a5454f6SWilly Tarreau 	 * was opened, and if not we'll attempt to create and open /dev/console
9211a5454f6SWilly Tarreau 	 * and /dev/null that we'll use for later tests.
9221a5454f6SWilly Tarreau 	 */
9231a5454f6SWilly Tarreau 	if (getpid() == 1)
9241a5454f6SWilly Tarreau 		prepare();
9251a5454f6SWilly Tarreau 
92623da7bc9SWilly Tarreau 	/* the definition of a series of tests comes from either argv[1] or the
92723da7bc9SWilly Tarreau 	 * "NOLIBC_TEST" environment variable. It's made of a comma-delimited
92823da7bc9SWilly Tarreau 	 * series of test names and optional ranges:
92923da7bc9SWilly Tarreau 	 *    syscall:5-15[:.*],stdlib:8-10
93023da7bc9SWilly Tarreau 	 */
93123da7bc9SWilly Tarreau 	test = argv[1];
93223da7bc9SWilly Tarreau 	if (!test)
93323da7bc9SWilly Tarreau 		test = getenv("NOLIBC_TEST");
93423da7bc9SWilly Tarreau 
93523da7bc9SWilly Tarreau 	if (test) {
93623da7bc9SWilly Tarreau 		char *comma, *colon, *dash, *value;
93723da7bc9SWilly Tarreau 
93823da7bc9SWilly Tarreau 		do {
93923da7bc9SWilly Tarreau 			comma = strchr(test, ',');
94023da7bc9SWilly Tarreau 			if (comma)
94123da7bc9SWilly Tarreau 				*(comma++) = '\0';
94223da7bc9SWilly Tarreau 
94323da7bc9SWilly Tarreau 			colon = strchr(test, ':');
94423da7bc9SWilly Tarreau 			if (colon)
94523da7bc9SWilly Tarreau 				*(colon++) = '\0';
94623da7bc9SWilly Tarreau 
94723da7bc9SWilly Tarreau 			for (idx = 0; test_names[idx].name; idx++) {
94823da7bc9SWilly Tarreau 				if (strcmp(test, test_names[idx].name) == 0)
94923da7bc9SWilly Tarreau 					break;
95023da7bc9SWilly Tarreau 			}
95123da7bc9SWilly Tarreau 
95223da7bc9SWilly Tarreau 			if (test_names[idx].name) {
95323da7bc9SWilly Tarreau 				/* The test was named, it will be called at least
95423da7bc9SWilly Tarreau 				 * once. We may have an optional range at <colon>
95523da7bc9SWilly Tarreau 				 * here, which defaults to the full range.
95623da7bc9SWilly Tarreau 				 */
95723da7bc9SWilly Tarreau 				do {
95823da7bc9SWilly Tarreau 					min = 0; max = __INT_MAX__;
95923da7bc9SWilly Tarreau 					value = colon;
96023da7bc9SWilly Tarreau 					if (value && *value) {
96123da7bc9SWilly Tarreau 						colon = strchr(value, ':');
96223da7bc9SWilly Tarreau 						if (colon)
96323da7bc9SWilly Tarreau 							*(colon++) = '\0';
96423da7bc9SWilly Tarreau 
96523da7bc9SWilly Tarreau 						dash = strchr(value, '-');
96623da7bc9SWilly Tarreau 						if (dash)
96723da7bc9SWilly Tarreau 							*(dash++) = '\0';
96823da7bc9SWilly Tarreau 
96923da7bc9SWilly Tarreau 						/* support :val: :min-max: :min-: :-max: */
97023da7bc9SWilly Tarreau 						if (*value)
97123da7bc9SWilly Tarreau 							min = atoi(value);
97223da7bc9SWilly Tarreau 						if (!dash)
97323da7bc9SWilly Tarreau 							max = min;
97423da7bc9SWilly Tarreau 						else if (*dash)
97523da7bc9SWilly Tarreau 							max = atoi(dash);
97623da7bc9SWilly Tarreau 
97723da7bc9SWilly Tarreau 						value = colon;
97823da7bc9SWilly Tarreau 					}
97923da7bc9SWilly Tarreau 
98023da7bc9SWilly Tarreau 					/* now's time to call the test */
98123da7bc9SWilly Tarreau 					printf("Running test '%s'\n", test_names[idx].name);
98223da7bc9SWilly Tarreau 					err = test_names[idx].func(min, max);
98323da7bc9SWilly Tarreau 					ret += err;
98423da7bc9SWilly Tarreau 					printf("Errors during this test: %d\n\n", err);
98523da7bc9SWilly Tarreau 				} while (colon && *colon);
98623da7bc9SWilly Tarreau 			} else
98723da7bc9SWilly Tarreau 				printf("Ignoring unknown test name '%s'\n", test);
98823da7bc9SWilly Tarreau 
98923da7bc9SWilly Tarreau 			test = comma;
99023da7bc9SWilly Tarreau 		} while (test && *test);
99123da7bc9SWilly Tarreau 	} else {
99223da7bc9SWilly Tarreau 		/* no test mentioned, run everything */
99323da7bc9SWilly Tarreau 		for (idx = 0; test_names[idx].name; idx++) {
99423da7bc9SWilly Tarreau 			printf("Running test '%s'\n", test_names[idx].name);
99523da7bc9SWilly Tarreau 			err = test_names[idx].func(min, max);
99623da7bc9SWilly Tarreau 			ret += err;
99723da7bc9SWilly Tarreau 			printf("Errors during this test: %d\n\n", err);
99823da7bc9SWilly Tarreau 		}
99923da7bc9SWilly Tarreau 	}
100023da7bc9SWilly Tarreau 
1001362aecb2SWilly Tarreau 	printf("Total number of errors: %d\n", ret);
1002f49896d7SWilly Tarreau 
1003f49896d7SWilly Tarreau 	if (getpid() == 1) {
1004f49896d7SWilly Tarreau 		/* we're running as init, there's no other process on the
1005f49896d7SWilly Tarreau 		 * system, thus likely started from a VM for a quick check.
1006f49896d7SWilly Tarreau 		 * Exiting will provoke a kernel panic that may be reported
1007f49896d7SWilly Tarreau 		 * as an error by Qemu or the hypervisor, while stopping
1008f49896d7SWilly Tarreau 		 * cleanly will often be reported as a success. This allows
1009f49896d7SWilly Tarreau 		 * to use the output of this program for bisecting kernels.
1010f49896d7SWilly Tarreau 		 */
1011f49896d7SWilly Tarreau 		printf("Leaving init with final status: %d\n", !!ret);
1012f49896d7SWilly Tarreau 		if (ret == 0)
1013f49896d7SWilly Tarreau 			reboot(LINUX_REBOOT_CMD_POWER_OFF);
1014aa73a86cSWilly Tarreau #if defined(__x86_64__)
1015aa73a86cSWilly Tarreau 		/* QEMU started with "-device isa-debug-exit -no-reboot" will
1016aa73a86cSWilly Tarreau 		 * exit with status code 2N+1 when N is written to 0x501. We
1017aa73a86cSWilly Tarreau 		 * hard-code the syscall here as it's arch-dependent.
1018aa73a86cSWilly Tarreau 		 */
10191da02f51SWilly Tarreau #if defined(_NOLIBC_SYS_H)
1020aa73a86cSWilly Tarreau 		else if (my_syscall3(__NR_ioperm, 0x501, 1, 1) == 0)
10211da02f51SWilly Tarreau #else
10221da02f51SWilly Tarreau 		else if (ioperm(0x501, 1, 1) == 0)
10231da02f51SWilly Tarreau #endif
10247f291cfaSThomas Weißschuh 			__asm__ volatile ("outb %%al, %%dx" :: "d"(0x501), "a"(0));
1025aa73a86cSWilly Tarreau 		/* if it does nothing, fall back to the regular panic */
1026aa73a86cSWilly Tarreau #endif
1027f49896d7SWilly Tarreau 	}
1028f49896d7SWilly Tarreau 
1029362aecb2SWilly Tarreau 	printf("Exiting with status %d\n", !!ret);
1030362aecb2SWilly Tarreau 	return !!ret;
1031362aecb2SWilly Tarreau }
1032