1fddc8f81SThomas Weißschuh /* SPDX-License-Identifier: GPL-2.0 */
2362aecb2SWilly Tarreau
31da02f51SWilly Tarreau #define _GNU_SOURCE
45f2de00eSZhangjin Wu #define _LARGEFILE64_SOURCE
51da02f51SWilly Tarreau
6362aecb2SWilly Tarreau /* libc-specific include files
71da02f51SWilly Tarreau * The program may be built in 3 ways:
8362aecb2SWilly Tarreau * $(CC) -nostdlib -include /path/to/nolibc.h => NOLIBC already defined
91da02f51SWilly Tarreau * $(CC) -nostdlib -I/path/to/nolibc/sysroot => _NOLIBC_* guards are present
101da02f51SWilly Tarreau * $(CC) with default libc => NOLIBC* never defined
11362aecb2SWilly Tarreau */
12362aecb2SWilly Tarreau #ifndef NOLIBC
13362aecb2SWilly Tarreau #include <stdio.h>
14362aecb2SWilly Tarreau #include <stdlib.h>
15362aecb2SWilly Tarreau #include <string.h>
161da02f51SWilly Tarreau #ifndef _NOLIBC_STDIO_H
171da02f51SWilly Tarreau /* standard libcs need more includes */
1848967b73SZhangjin Wu #include <sys/auxv.h>
191da02f51SWilly Tarreau #include <sys/io.h>
201da02f51SWilly Tarreau #include <sys/ioctl.h>
2169f2cd9fSThomas Weißschuh #include <sys/mman.h>
221da02f51SWilly Tarreau #include <sys/mount.h>
23208aa9d9SThomas Weißschuh #include <sys/prctl.h>
241da02f51SWilly Tarreau #include <sys/reboot.h>
251da02f51SWilly Tarreau #include <sys/stat.h>
261da02f51SWilly Tarreau #include <sys/syscall.h>
271da02f51SWilly Tarreau #include <sys/sysmacros.h>
281da02f51SWilly Tarreau #include <sys/time.h>
291da02f51SWilly Tarreau #include <sys/wait.h>
301da02f51SWilly Tarreau #include <dirent.h>
311da02f51SWilly Tarreau #include <errno.h>
321da02f51SWilly Tarreau #include <fcntl.h>
331da02f51SWilly Tarreau #include <poll.h>
341da02f51SWilly Tarreau #include <sched.h>
351da02f51SWilly Tarreau #include <signal.h>
361da02f51SWilly Tarreau #include <stdarg.h>
372df07fc5SWilly Tarreau #include <stddef.h>
382df07fc5SWilly Tarreau #include <stdint.h>
391da02f51SWilly Tarreau #include <unistd.h>
40bd27fef3SZhangjin Wu #include <limits.h>
411da02f51SWilly Tarreau #endif
42362aecb2SWilly Tarreau #endif
43362aecb2SWilly Tarreau
44989abf1cSZhangjin Wu /* for the type of int_fast16_t and int_fast32_t, musl differs from glibc and nolibc */
45989abf1cSZhangjin Wu #define SINT_MAX_OF_TYPE(type) (((type)1 << (sizeof(type) * 8 - 2)) - (type)1 + ((type)1 << (sizeof(type) * 8 - 2)))
46989abf1cSZhangjin Wu #define SINT_MIN_OF_TYPE(type) (-SINT_MAX_OF_TYPE(type) - 1)
47989abf1cSZhangjin Wu
4867d108e2SThomas Weißschuh /* will be used to test initialization of environ */
4967d108e2SThomas Weißschuh static char **test_envp;
50362aecb2SWilly Tarreau
5148967b73SZhangjin Wu /* will be used to test initialization of argv */
5248967b73SZhangjin Wu static char **test_argv;
5348967b73SZhangjin Wu
5448967b73SZhangjin Wu /* will be used to test initialization of argc */
5548967b73SZhangjin Wu static int test_argc;
5648967b73SZhangjin Wu
57938b5b98SZhangjin Wu /* will be used by some test cases as readable file, please don't write it */
58938b5b98SZhangjin Wu static const char *argv0;
59362aecb2SWilly Tarreau
6023da7bc9SWilly Tarreau /* definition of a series of tests */
6123da7bc9SWilly Tarreau struct test {
62fddc8f81SThomas Weißschuh const char *name; /* test name */
63fddc8f81SThomas Weißschuh int (*func)(int min, int max); /* handler */
6423da7bc9SWilly Tarreau };
6523da7bc9SWilly Tarreau
661da02f51SWilly Tarreau #ifndef _NOLIBC_STDLIB_H
itoa(int i)671da02f51SWilly Tarreau char *itoa(int i)
681da02f51SWilly Tarreau {
691da02f51SWilly Tarreau static char buf[12];
701da02f51SWilly Tarreau int ret;
711da02f51SWilly Tarreau
721da02f51SWilly Tarreau ret = snprintf(buf, sizeof(buf), "%d", i);
731da02f51SWilly Tarreau return (ret >= 0 && ret < sizeof(buf)) ? buf : "#err";
741da02f51SWilly Tarreau }
751da02f51SWilly Tarreau #endif
761da02f51SWilly Tarreau
77362aecb2SWilly Tarreau #define CASE_ERR(err) \
78362aecb2SWilly Tarreau case err: return #err
79362aecb2SWilly Tarreau
80362aecb2SWilly Tarreau /* returns the error name (e.g. "ENOENT") for common errors, "SUCCESS" for 0,
81362aecb2SWilly Tarreau * or the decimal value for less common ones.
82362aecb2SWilly Tarreau */
errorname(int err)8317e66f23SThomas Weißschuh static const char *errorname(int err)
84362aecb2SWilly Tarreau {
85362aecb2SWilly Tarreau switch (err) {
86362aecb2SWilly Tarreau case 0: return "SUCCESS";
87362aecb2SWilly Tarreau CASE_ERR(EPERM);
88362aecb2SWilly Tarreau CASE_ERR(ENOENT);
89362aecb2SWilly Tarreau CASE_ERR(ESRCH);
90362aecb2SWilly Tarreau CASE_ERR(EINTR);
91362aecb2SWilly Tarreau CASE_ERR(EIO);
92362aecb2SWilly Tarreau CASE_ERR(ENXIO);
93362aecb2SWilly Tarreau CASE_ERR(E2BIG);
94362aecb2SWilly Tarreau CASE_ERR(ENOEXEC);
95362aecb2SWilly Tarreau CASE_ERR(EBADF);
96362aecb2SWilly Tarreau CASE_ERR(ECHILD);
97362aecb2SWilly Tarreau CASE_ERR(EAGAIN);
98362aecb2SWilly Tarreau CASE_ERR(ENOMEM);
99362aecb2SWilly Tarreau CASE_ERR(EACCES);
100362aecb2SWilly Tarreau CASE_ERR(EFAULT);
101362aecb2SWilly Tarreau CASE_ERR(ENOTBLK);
102362aecb2SWilly Tarreau CASE_ERR(EBUSY);
103362aecb2SWilly Tarreau CASE_ERR(EEXIST);
104362aecb2SWilly Tarreau CASE_ERR(EXDEV);
105362aecb2SWilly Tarreau CASE_ERR(ENODEV);
106362aecb2SWilly Tarreau CASE_ERR(ENOTDIR);
107362aecb2SWilly Tarreau CASE_ERR(EISDIR);
108362aecb2SWilly Tarreau CASE_ERR(EINVAL);
109362aecb2SWilly Tarreau CASE_ERR(ENFILE);
110362aecb2SWilly Tarreau CASE_ERR(EMFILE);
111362aecb2SWilly Tarreau CASE_ERR(ENOTTY);
112362aecb2SWilly Tarreau CASE_ERR(ETXTBSY);
113362aecb2SWilly Tarreau CASE_ERR(EFBIG);
114362aecb2SWilly Tarreau CASE_ERR(ENOSPC);
115362aecb2SWilly Tarreau CASE_ERR(ESPIPE);
116362aecb2SWilly Tarreau CASE_ERR(EROFS);
117362aecb2SWilly Tarreau CASE_ERR(EMLINK);
118362aecb2SWilly Tarreau CASE_ERR(EPIPE);
119362aecb2SWilly Tarreau CASE_ERR(EDOM);
120362aecb2SWilly Tarreau CASE_ERR(ERANGE);
121362aecb2SWilly Tarreau CASE_ERR(ENOSYS);
122758f970fSZhangjin Wu CASE_ERR(EOVERFLOW);
123362aecb2SWilly Tarreau default:
124362aecb2SWilly Tarreau return itoa(err);
125362aecb2SWilly Tarreau }
126362aecb2SWilly Tarreau }
127362aecb2SWilly Tarreau
putcharn(char c,size_t n)128443de903SThomas Weißschuh static void putcharn(char c, size_t n)
129443de903SThomas Weißschuh {
130443de903SThomas Weißschuh char buf[64];
131443de903SThomas Weißschuh
132443de903SThomas Weißschuh memset(buf, c, n);
133443de903SThomas Weißschuh buf[n] = '\0';
134443de903SThomas Weißschuh fputs(buf, stdout);
135443de903SThomas Weißschuh }
136443de903SThomas Weißschuh
137b184a261SThomas Weißschuh enum RESULT {
138b184a261SThomas Weißschuh OK,
139b184a261SThomas Weißschuh FAIL,
140b184a261SThomas Weißschuh SKIPPED,
141b184a261SThomas Weißschuh };
142b184a261SThomas Weißschuh
result(int llen,enum RESULT r)143b184a261SThomas Weißschuh static void result(int llen, enum RESULT r)
144362aecb2SWilly Tarreau {
145b184a261SThomas Weißschuh const char *msg;
146362aecb2SWilly Tarreau
147b184a261SThomas Weißschuh if (r == OK)
148b184a261SThomas Weißschuh msg = " [OK]";
149b184a261SThomas Weißschuh else if (r == SKIPPED)
150b184a261SThomas Weißschuh msg = "[SKIPPED]";
151b184a261SThomas Weißschuh else
152b184a261SThomas Weißschuh msg = " [FAIL]";
153362aecb2SWilly Tarreau
154447e5602SThomas Weißschuh if (llen < 64)
155b184a261SThomas Weißschuh putcharn(' ', 64 - llen);
156b184a261SThomas Weißschuh puts(msg);
157362aecb2SWilly Tarreau }
158362aecb2SWilly Tarreau
159362aecb2SWilly Tarreau /* The tests below are intended to be used by the macroes, which evaluate
160362aecb2SWilly Tarreau * expression <expr>, print the status to stdout, and update the "ret"
161362aecb2SWilly Tarreau * variable to count failures. The functions themselves return the number
162362aecb2SWilly Tarreau * of failures, thus either 0 or 1.
163362aecb2SWilly Tarreau */
164362aecb2SWilly Tarreau
165362aecb2SWilly Tarreau #define EXPECT_ZR(cond, expr) \
166b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_zr(expr, llen); } while (0)
167362aecb2SWilly Tarreau
16810874f20SThomas Weißschuh static __attribute__((unused))
expect_zr(int expr,int llen)16910874f20SThomas Weißschuh int expect_zr(int expr, int llen)
170362aecb2SWilly Tarreau {
171362aecb2SWilly Tarreau int ret = !(expr == 0);
172362aecb2SWilly Tarreau
173362aecb2SWilly Tarreau llen += printf(" = %d ", expr);
174b184a261SThomas Weißschuh result(llen, ret ? FAIL : OK);
175362aecb2SWilly Tarreau return ret;
176362aecb2SWilly Tarreau }
177362aecb2SWilly Tarreau
178362aecb2SWilly Tarreau
179362aecb2SWilly Tarreau #define EXPECT_NZ(cond, expr, val) \
180b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_nz(expr, llen; } while (0)
181362aecb2SWilly Tarreau
18210874f20SThomas Weißschuh static __attribute__((unused))
expect_nz(int expr,int llen)18310874f20SThomas Weißschuh int expect_nz(int expr, int llen)
184362aecb2SWilly Tarreau {
185362aecb2SWilly Tarreau int ret = !(expr != 0);
186362aecb2SWilly Tarreau
187362aecb2SWilly Tarreau llen += printf(" = %d ", expr);
188b184a261SThomas Weißschuh result(llen, ret ? FAIL : OK);
189362aecb2SWilly Tarreau return ret;
190362aecb2SWilly Tarreau }
191362aecb2SWilly Tarreau
192362aecb2SWilly Tarreau
193362aecb2SWilly Tarreau #define EXPECT_EQ(cond, expr, val) \
194b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_eq(expr, llen, val); } while (0)
195362aecb2SWilly Tarreau
19610874f20SThomas Weißschuh static __attribute__((unused))
expect_eq(uint64_t expr,int llen,uint64_t val)19710874f20SThomas Weißschuh int expect_eq(uint64_t expr, int llen, uint64_t val)
198362aecb2SWilly Tarreau {
199362aecb2SWilly Tarreau int ret = !(expr == val);
200362aecb2SWilly Tarreau
2010858aec4SZhangjin Wu llen += printf(" = %lld ", (long long)expr);
202b184a261SThomas Weißschuh result(llen, ret ? FAIL : OK);
203362aecb2SWilly Tarreau return ret;
204362aecb2SWilly Tarreau }
205362aecb2SWilly Tarreau
206362aecb2SWilly Tarreau
207362aecb2SWilly Tarreau #define EXPECT_NE(cond, expr, val) \
208b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ne(expr, llen, val); } while (0)
209362aecb2SWilly Tarreau
21010874f20SThomas Weißschuh static __attribute__((unused))
expect_ne(int expr,int llen,int val)21110874f20SThomas Weißschuh int expect_ne(int expr, int llen, int val)
212362aecb2SWilly Tarreau {
213362aecb2SWilly Tarreau int ret = !(expr != val);
214362aecb2SWilly Tarreau
215362aecb2SWilly Tarreau llen += printf(" = %d ", expr);
216b184a261SThomas Weißschuh result(llen, ret ? FAIL : OK);
217362aecb2SWilly Tarreau return ret;
218362aecb2SWilly Tarreau }
219362aecb2SWilly Tarreau
220362aecb2SWilly Tarreau
221362aecb2SWilly Tarreau #define EXPECT_GE(cond, expr, val) \
222b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ge(expr, llen, val); } while (0)
223362aecb2SWilly Tarreau
22410874f20SThomas Weißschuh static __attribute__((unused))
expect_ge(int expr,int llen,int val)22510874f20SThomas Weißschuh int expect_ge(int expr, int llen, int val)
226362aecb2SWilly Tarreau {
227362aecb2SWilly Tarreau int ret = !(expr >= val);
228362aecb2SWilly Tarreau
229362aecb2SWilly Tarreau llen += printf(" = %d ", expr);
230b184a261SThomas Weißschuh result(llen, ret ? FAIL : OK);
231362aecb2SWilly Tarreau return ret;
232362aecb2SWilly Tarreau }
233362aecb2SWilly Tarreau
234362aecb2SWilly Tarreau
235362aecb2SWilly Tarreau #define EXPECT_GT(cond, expr, val) \
236b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_gt(expr, llen, val); } while (0)
237362aecb2SWilly Tarreau
23810874f20SThomas Weißschuh static __attribute__((unused))
expect_gt(int expr,int llen,int val)23910874f20SThomas Weißschuh int expect_gt(int expr, int llen, int val)
240362aecb2SWilly Tarreau {
241362aecb2SWilly Tarreau int ret = !(expr > val);
242362aecb2SWilly Tarreau
243362aecb2SWilly Tarreau llen += printf(" = %d ", expr);
244b184a261SThomas Weißschuh result(llen, ret ? FAIL : OK);
245362aecb2SWilly Tarreau return ret;
246362aecb2SWilly Tarreau }
247362aecb2SWilly Tarreau
248362aecb2SWilly Tarreau
249362aecb2SWilly Tarreau #define EXPECT_LE(cond, expr, val) \
250b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_le(expr, llen, val); } while (0)
251362aecb2SWilly Tarreau
25210874f20SThomas Weißschuh static __attribute__((unused))
expect_le(int expr,int llen,int val)25310874f20SThomas Weißschuh int expect_le(int expr, int llen, int val)
254362aecb2SWilly Tarreau {
255362aecb2SWilly Tarreau int ret = !(expr <= val);
256362aecb2SWilly Tarreau
257362aecb2SWilly Tarreau llen += printf(" = %d ", expr);
258b184a261SThomas Weißschuh result(llen, ret ? FAIL : OK);
259362aecb2SWilly Tarreau return ret;
260362aecb2SWilly Tarreau }
261362aecb2SWilly Tarreau
262362aecb2SWilly Tarreau
263362aecb2SWilly Tarreau #define EXPECT_LT(cond, expr, val) \
264b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_lt(expr, llen, val); } while (0)
265362aecb2SWilly Tarreau
26610874f20SThomas Weißschuh static __attribute__((unused))
expect_lt(int expr,int llen,int val)26710874f20SThomas Weißschuh int expect_lt(int expr, int llen, int val)
268362aecb2SWilly Tarreau {
269362aecb2SWilly Tarreau int ret = !(expr < val);
270362aecb2SWilly Tarreau
271362aecb2SWilly Tarreau llen += printf(" = %d ", expr);
272b184a261SThomas Weißschuh result(llen, ret ? FAIL : OK);
273362aecb2SWilly Tarreau return ret;
274362aecb2SWilly Tarreau }
275362aecb2SWilly Tarreau
276362aecb2SWilly Tarreau
277362aecb2SWilly Tarreau #define EXPECT_SYSZR(cond, expr) \
278b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_syszr(expr, llen); } while (0)
279362aecb2SWilly Tarreau
28010874f20SThomas Weißschuh static __attribute__((unused))
expect_syszr(int expr,int llen)28110874f20SThomas Weißschuh int expect_syszr(int expr, int llen)
282362aecb2SWilly Tarreau {
283362aecb2SWilly Tarreau int ret = 0;
284362aecb2SWilly Tarreau
285362aecb2SWilly Tarreau if (expr) {
286362aecb2SWilly Tarreau ret = 1;
287362aecb2SWilly Tarreau llen += printf(" = %d %s ", expr, errorname(errno));
288b184a261SThomas Weißschuh result(llen, FAIL);
289362aecb2SWilly Tarreau } else {
290362aecb2SWilly Tarreau llen += printf(" = %d ", expr);
291b184a261SThomas Weißschuh result(llen, OK);
292362aecb2SWilly Tarreau }
293362aecb2SWilly Tarreau return ret;
294362aecb2SWilly Tarreau }
295362aecb2SWilly Tarreau
296362aecb2SWilly Tarreau
297362aecb2SWilly Tarreau #define EXPECT_SYSEQ(cond, expr, val) \
298b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_syseq(expr, llen, val); } while (0)
299362aecb2SWilly Tarreau
30010874f20SThomas Weißschuh static __attribute__((unused))
expect_syseq(int expr,int llen,int val)30110874f20SThomas Weißschuh int expect_syseq(int expr, int llen, int val)
302362aecb2SWilly Tarreau {
303362aecb2SWilly Tarreau int ret = 0;
304362aecb2SWilly Tarreau
305362aecb2SWilly Tarreau if (expr != val) {
306362aecb2SWilly Tarreau ret = 1;
307362aecb2SWilly Tarreau llen += printf(" = %d %s ", expr, errorname(errno));
308b184a261SThomas Weißschuh result(llen, FAIL);
309362aecb2SWilly Tarreau } else {
310362aecb2SWilly Tarreau llen += printf(" = %d ", expr);
311b184a261SThomas Weißschuh result(llen, OK);
312362aecb2SWilly Tarreau }
313362aecb2SWilly Tarreau return ret;
314362aecb2SWilly Tarreau }
315362aecb2SWilly Tarreau
316362aecb2SWilly Tarreau
317362aecb2SWilly Tarreau #define EXPECT_SYSNE(cond, expr, val) \
318b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_sysne(expr, llen, val); } while (0)
319362aecb2SWilly Tarreau
32010874f20SThomas Weißschuh static __attribute__((unused))
expect_sysne(int expr,int llen,int val)32110874f20SThomas Weißschuh int expect_sysne(int expr, int llen, int val)
322362aecb2SWilly Tarreau {
323362aecb2SWilly Tarreau int ret = 0;
324362aecb2SWilly Tarreau
325362aecb2SWilly Tarreau if (expr == val) {
326362aecb2SWilly Tarreau ret = 1;
327362aecb2SWilly Tarreau llen += printf(" = %d %s ", expr, errorname(errno));
328b184a261SThomas Weißschuh result(llen, FAIL);
329362aecb2SWilly Tarreau } else {
330362aecb2SWilly Tarreau llen += printf(" = %d ", expr);
331b184a261SThomas Weißschuh result(llen, OK);
332362aecb2SWilly Tarreau }
333362aecb2SWilly Tarreau return ret;
334362aecb2SWilly Tarreau }
335362aecb2SWilly Tarreau
336362aecb2SWilly Tarreau
33775d75a7bSZhangjin Wu #define EXPECT_SYSER2(cond, expr, expret, experr1, experr2) \
338b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_syserr2(expr, expret, experr1, experr2, llen); } while (0)
339362aecb2SWilly Tarreau
34075d75a7bSZhangjin Wu #define EXPECT_SYSER(cond, expr, expret, experr) \
34175d75a7bSZhangjin Wu EXPECT_SYSER2(cond, expr, expret, experr, 0)
34275d75a7bSZhangjin Wu
34310874f20SThomas Weißschuh static __attribute__((unused))
expect_syserr2(int expr,int expret,int experr1,int experr2,int llen)34410874f20SThomas Weißschuh int expect_syserr2(int expr, int expret, int experr1, int experr2, int llen)
345362aecb2SWilly Tarreau {
346362aecb2SWilly Tarreau int ret = 0;
347362aecb2SWilly Tarreau int _errno = errno;
348362aecb2SWilly Tarreau
349362aecb2SWilly Tarreau llen += printf(" = %d %s ", expr, errorname(_errno));
35075d75a7bSZhangjin Wu if (expr != expret || (_errno != experr1 && _errno != experr2)) {
351362aecb2SWilly Tarreau ret = 1;
35275d75a7bSZhangjin Wu if (experr2 == 0)
35375d75a7bSZhangjin Wu llen += printf(" != (%d %s) ", expret, errorname(experr1));
35475d75a7bSZhangjin Wu else
35575d75a7bSZhangjin Wu llen += printf(" != (%d %s %s) ", expret, errorname(experr1), errorname(experr2));
356b184a261SThomas Weißschuh result(llen, FAIL);
357362aecb2SWilly Tarreau } else {
358b184a261SThomas Weißschuh result(llen, OK);
359362aecb2SWilly Tarreau }
360362aecb2SWilly Tarreau return ret;
361362aecb2SWilly Tarreau }
362362aecb2SWilly Tarreau
363362aecb2SWilly Tarreau
364362aecb2SWilly Tarreau #define EXPECT_PTRZR(cond, expr) \
365b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrzr(expr, llen); } while (0)
366362aecb2SWilly Tarreau
36710874f20SThomas Weißschuh static __attribute__((unused))
expect_ptrzr(const void * expr,int llen)36810874f20SThomas Weißschuh int expect_ptrzr(const void *expr, int llen)
369362aecb2SWilly Tarreau {
370362aecb2SWilly Tarreau int ret = 0;
371362aecb2SWilly Tarreau
372362aecb2SWilly Tarreau llen += printf(" = <%p> ", expr);
373362aecb2SWilly Tarreau if (expr) {
374362aecb2SWilly Tarreau ret = 1;
375b184a261SThomas Weißschuh result(llen, FAIL);
376362aecb2SWilly Tarreau } else {
377b184a261SThomas Weißschuh result(llen, OK);
378362aecb2SWilly Tarreau }
379362aecb2SWilly Tarreau return ret;
380362aecb2SWilly Tarreau }
381362aecb2SWilly Tarreau
382362aecb2SWilly Tarreau
383362aecb2SWilly Tarreau #define EXPECT_PTRNZ(cond, expr) \
384b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrnz(expr, llen); } while (0)
385362aecb2SWilly Tarreau
38610874f20SThomas Weißschuh static __attribute__((unused))
expect_ptrnz(const void * expr,int llen)38710874f20SThomas Weißschuh int expect_ptrnz(const void *expr, int llen)
388362aecb2SWilly Tarreau {
389362aecb2SWilly Tarreau int ret = 0;
390362aecb2SWilly Tarreau
391362aecb2SWilly Tarreau llen += printf(" = <%p> ", expr);
392362aecb2SWilly Tarreau if (!expr) {
393362aecb2SWilly Tarreau ret = 1;
394b184a261SThomas Weißschuh result(llen, FAIL);
395362aecb2SWilly Tarreau } else {
396b184a261SThomas Weißschuh result(llen, OK);
397362aecb2SWilly Tarreau }
398362aecb2SWilly Tarreau return ret;
399362aecb2SWilly Tarreau }
400362aecb2SWilly Tarreau
40129f5540bSZhangjin Wu #define EXPECT_PTREQ(cond, expr, cmp) \
402b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptreq(expr, llen, cmp); } while (0)
40329f5540bSZhangjin Wu
40410874f20SThomas Weißschuh static __attribute__((unused))
expect_ptreq(const void * expr,int llen,const void * cmp)40510874f20SThomas Weißschuh int expect_ptreq(const void *expr, int llen, const void *cmp)
40629f5540bSZhangjin Wu {
40729f5540bSZhangjin Wu int ret = 0;
40829f5540bSZhangjin Wu
40929f5540bSZhangjin Wu llen += printf(" = <%p> ", expr);
41029f5540bSZhangjin Wu if (expr != cmp) {
41129f5540bSZhangjin Wu ret = 1;
412b184a261SThomas Weißschuh result(llen, FAIL);
41329f5540bSZhangjin Wu } else {
414b184a261SThomas Weißschuh result(llen, OK);
41529f5540bSZhangjin Wu }
41629f5540bSZhangjin Wu return ret;
41729f5540bSZhangjin Wu }
41829f5540bSZhangjin Wu
41929f5540bSZhangjin Wu #define EXPECT_PTRNE(cond, expr, cmp) \
420b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrne(expr, llen, cmp); } while (0)
42129f5540bSZhangjin Wu
42210874f20SThomas Weißschuh static __attribute__((unused))
expect_ptrne(const void * expr,int llen,const void * cmp)42310874f20SThomas Weißschuh int expect_ptrne(const void *expr, int llen, const void *cmp)
42429f5540bSZhangjin Wu {
42529f5540bSZhangjin Wu int ret = 0;
42629f5540bSZhangjin Wu
42729f5540bSZhangjin Wu llen += printf(" = <%p> ", expr);
42829f5540bSZhangjin Wu if (expr == cmp) {
42929f5540bSZhangjin Wu ret = 1;
430b184a261SThomas Weißschuh result(llen, FAIL);
43129f5540bSZhangjin Wu } else {
432b184a261SThomas Weißschuh result(llen, OK);
43329f5540bSZhangjin Wu }
43429f5540bSZhangjin Wu return ret;
43529f5540bSZhangjin Wu }
43629f5540bSZhangjin Wu
437fd3a9efdSZhangjin Wu #define EXPECT_PTRGE(cond, expr, cmp) \
438fd3a9efdSZhangjin Wu do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrge(expr, llen, cmp); } while (0)
439fd3a9efdSZhangjin Wu
44010874f20SThomas Weißschuh static __attribute__((unused))
expect_ptrge(const void * expr,int llen,const void * cmp)44110874f20SThomas Weißschuh int expect_ptrge(const void *expr, int llen, const void *cmp)
442fd3a9efdSZhangjin Wu {
443fd3a9efdSZhangjin Wu int ret = !(expr >= cmp);
444fd3a9efdSZhangjin Wu
445fd3a9efdSZhangjin Wu llen += printf(" = <%p> ", expr);
446fd3a9efdSZhangjin Wu result(llen, ret ? FAIL : OK);
447fd3a9efdSZhangjin Wu return ret;
448fd3a9efdSZhangjin Wu }
449fd3a9efdSZhangjin Wu
450fd3a9efdSZhangjin Wu #define EXPECT_PTRGT(cond, expr, cmp) \
451fd3a9efdSZhangjin Wu do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrgt(expr, llen, cmp); } while (0)
452fd3a9efdSZhangjin Wu
45310874f20SThomas Weißschuh static __attribute__((unused))
expect_ptrgt(const void * expr,int llen,const void * cmp)45410874f20SThomas Weißschuh int expect_ptrgt(const void *expr, int llen, const void *cmp)
455fd3a9efdSZhangjin Wu {
456fd3a9efdSZhangjin Wu int ret = !(expr > cmp);
457fd3a9efdSZhangjin Wu
458fd3a9efdSZhangjin Wu llen += printf(" = <%p> ", expr);
459fd3a9efdSZhangjin Wu result(llen, ret ? FAIL : OK);
460fd3a9efdSZhangjin Wu return ret;
461fd3a9efdSZhangjin Wu }
462fd3a9efdSZhangjin Wu
463fd3a9efdSZhangjin Wu
464fd3a9efdSZhangjin Wu #define EXPECT_PTRLE(cond, expr, cmp) \
465fd3a9efdSZhangjin Wu do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrle(expr, llen, cmp); } while (0)
466fd3a9efdSZhangjin Wu
46710874f20SThomas Weißschuh static __attribute__((unused))
expect_ptrle(const void * expr,int llen,const void * cmp)46810874f20SThomas Weißschuh int expect_ptrle(const void *expr, int llen, const void *cmp)
469fd3a9efdSZhangjin Wu {
470fd3a9efdSZhangjin Wu int ret = !(expr <= cmp);
471fd3a9efdSZhangjin Wu
472fd3a9efdSZhangjin Wu llen += printf(" = <%p> ", expr);
473fd3a9efdSZhangjin Wu result(llen, ret ? FAIL : OK);
474fd3a9efdSZhangjin Wu return ret;
475fd3a9efdSZhangjin Wu }
476fd3a9efdSZhangjin Wu
477fd3a9efdSZhangjin Wu
478fd3a9efdSZhangjin Wu #define EXPECT_PTRLT(cond, expr, cmp) \
479fd3a9efdSZhangjin Wu do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrlt(expr, llen, cmp); } while (0)
480fd3a9efdSZhangjin Wu
48110874f20SThomas Weißschuh static __attribute__((unused))
expect_ptrlt(const void * expr,int llen,const void * cmp)48210874f20SThomas Weißschuh int expect_ptrlt(const void *expr, int llen, const void *cmp)
483fd3a9efdSZhangjin Wu {
484fd3a9efdSZhangjin Wu int ret = !(expr < cmp);
485fd3a9efdSZhangjin Wu
486fd3a9efdSZhangjin Wu llen += printf(" = <%p> ", expr);
487fd3a9efdSZhangjin Wu result(llen, ret ? FAIL : OK);
488fd3a9efdSZhangjin Wu return ret;
489fd3a9efdSZhangjin Wu }
490fd3a9efdSZhangjin Wu
49129f5540bSZhangjin Wu #define EXPECT_PTRER2(cond, expr, expret, experr1, experr2) \
492b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrerr2(expr, expret, experr1, experr2, llen); } while (0)
49329f5540bSZhangjin Wu
49429f5540bSZhangjin Wu #define EXPECT_PTRER(cond, expr, expret, experr) \
49529f5540bSZhangjin Wu EXPECT_PTRER2(cond, expr, expret, experr, 0)
49629f5540bSZhangjin Wu
49710874f20SThomas Weißschuh static __attribute__((unused))
expect_ptrerr2(const void * expr,const void * expret,int experr1,int experr2,int llen)49810874f20SThomas Weißschuh int expect_ptrerr2(const void *expr, const void *expret, int experr1, int experr2, int llen)
49929f5540bSZhangjin Wu {
50029f5540bSZhangjin Wu int ret = 0;
50129f5540bSZhangjin Wu int _errno = errno;
50229f5540bSZhangjin Wu
50329f5540bSZhangjin Wu llen += printf(" = <%p> %s ", expr, errorname(_errno));
50429f5540bSZhangjin Wu if (expr != expret || (_errno != experr1 && _errno != experr2)) {
50529f5540bSZhangjin Wu ret = 1;
50629f5540bSZhangjin Wu if (experr2 == 0)
50729f5540bSZhangjin Wu llen += printf(" != (<%p> %s) ", expret, errorname(experr1));
50829f5540bSZhangjin Wu else
50929f5540bSZhangjin Wu llen += printf(" != (<%p> %s %s) ", expret, errorname(experr1), errorname(experr2));
510b184a261SThomas Weißschuh result(llen, FAIL);
51129f5540bSZhangjin Wu } else {
512b184a261SThomas Weißschuh result(llen, OK);
51329f5540bSZhangjin Wu }
51429f5540bSZhangjin Wu return ret;
51529f5540bSZhangjin Wu }
516362aecb2SWilly Tarreau
517362aecb2SWilly Tarreau #define EXPECT_STRZR(cond, expr) \
518b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_strzr(expr, llen); } while (0)
519362aecb2SWilly Tarreau
52010874f20SThomas Weißschuh static __attribute__((unused))
expect_strzr(const char * expr,int llen)52110874f20SThomas Weißschuh int expect_strzr(const char *expr, int llen)
522362aecb2SWilly Tarreau {
523362aecb2SWilly Tarreau int ret = 0;
524362aecb2SWilly Tarreau
525*e9df4c61SThomas Weißschuh llen += printf(" = <%s> ", expr ? expr : "(null)");
526362aecb2SWilly Tarreau if (expr) {
527362aecb2SWilly Tarreau ret = 1;
528b184a261SThomas Weißschuh result(llen, FAIL);
529362aecb2SWilly Tarreau } else {
530b184a261SThomas Weißschuh result(llen, OK);
531362aecb2SWilly Tarreau }
532362aecb2SWilly Tarreau return ret;
533362aecb2SWilly Tarreau }
534362aecb2SWilly Tarreau
535362aecb2SWilly Tarreau
536362aecb2SWilly Tarreau #define EXPECT_STRNZ(cond, expr) \
537b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_strnz(expr, llen); } while (0)
538362aecb2SWilly Tarreau
53910874f20SThomas Weißschuh static __attribute__((unused))
expect_strnz(const char * expr,int llen)54010874f20SThomas Weißschuh int expect_strnz(const char *expr, int llen)
541362aecb2SWilly Tarreau {
542362aecb2SWilly Tarreau int ret = 0;
543362aecb2SWilly Tarreau
544*e9df4c61SThomas Weißschuh llen += printf(" = <%s> ", expr ? expr : "(null)");
545362aecb2SWilly Tarreau if (!expr) {
546362aecb2SWilly Tarreau ret = 1;
547b184a261SThomas Weißschuh result(llen, FAIL);
548362aecb2SWilly Tarreau } else {
549b184a261SThomas Weißschuh result(llen, OK);
550362aecb2SWilly Tarreau }
551362aecb2SWilly Tarreau return ret;
552362aecb2SWilly Tarreau }
553362aecb2SWilly Tarreau
554362aecb2SWilly Tarreau
555362aecb2SWilly Tarreau #define EXPECT_STREQ(cond, expr, cmp) \
556b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_streq(expr, llen, cmp); } while (0)
557362aecb2SWilly Tarreau
55810874f20SThomas Weißschuh static __attribute__((unused))
expect_streq(const char * expr,int llen,const char * cmp)55910874f20SThomas Weißschuh int expect_streq(const char *expr, int llen, const char *cmp)
560362aecb2SWilly Tarreau {
561362aecb2SWilly Tarreau int ret = 0;
562362aecb2SWilly Tarreau
563362aecb2SWilly Tarreau llen += printf(" = <%s> ", expr);
564362aecb2SWilly Tarreau if (strcmp(expr, cmp) != 0) {
565362aecb2SWilly Tarreau ret = 1;
566b184a261SThomas Weißschuh result(llen, FAIL);
567362aecb2SWilly Tarreau } else {
568b184a261SThomas Weißschuh result(llen, OK);
569362aecb2SWilly Tarreau }
570362aecb2SWilly Tarreau return ret;
571362aecb2SWilly Tarreau }
572362aecb2SWilly Tarreau
573362aecb2SWilly Tarreau
574362aecb2SWilly Tarreau #define EXPECT_STRNE(cond, expr, cmp) \
575b184a261SThomas Weißschuh do { if (!(cond)) result(llen, SKIPPED); else ret += expect_strne(expr, llen, cmp); } while (0)
576362aecb2SWilly Tarreau
57710874f20SThomas Weißschuh static __attribute__((unused))
expect_strne(const char * expr,int llen,const char * cmp)57810874f20SThomas Weißschuh int expect_strne(const char *expr, int llen, const char *cmp)
579362aecb2SWilly Tarreau {
580362aecb2SWilly Tarreau int ret = 0;
581362aecb2SWilly Tarreau
582362aecb2SWilly Tarreau llen += printf(" = <%s> ", expr);
583362aecb2SWilly Tarreau if (strcmp(expr, cmp) == 0) {
584362aecb2SWilly Tarreau ret = 1;
585b184a261SThomas Weißschuh result(llen, FAIL);
586362aecb2SWilly Tarreau } else {
587b184a261SThomas Weißschuh result(llen, OK);
588362aecb2SWilly Tarreau }
589362aecb2SWilly Tarreau return ret;
590362aecb2SWilly Tarreau }
591362aecb2SWilly Tarreau
59223da7bc9SWilly Tarreau
593362aecb2SWilly Tarreau /* declare tests based on line numbers. There must be exactly one test per line. */
594362aecb2SWilly Tarreau #define CASE_TEST(name) \
595362aecb2SWilly Tarreau case __LINE__: llen += printf("%d %s", test, #name);
596362aecb2SWilly Tarreau
run_startup(int min,int max)59748967b73SZhangjin Wu int run_startup(int min, int max)
59848967b73SZhangjin Wu {
59948967b73SZhangjin Wu int test;
60048967b73SZhangjin Wu int ret = 0;
60148967b73SZhangjin Wu /* kernel at least passes HOME and TERM, shell passes more */
60248967b73SZhangjin Wu int env_total = 2;
60348967b73SZhangjin Wu /* checking NULL for argv/argv0, environ and _auxv is not enough, let's compare with sbrk(0) or &end */
60448967b73SZhangjin Wu extern char end;
60548967b73SZhangjin Wu char *brk = sbrk(0) != (void *)-1 ? sbrk(0) : &end;
60648967b73SZhangjin Wu /* differ from nolibc, both glibc and musl have no global _auxv */
60748967b73SZhangjin Wu const unsigned long *test_auxv = (void *)-1;
60848967b73SZhangjin Wu #ifdef NOLIBC
60948967b73SZhangjin Wu test_auxv = _auxv;
61048967b73SZhangjin Wu #endif
61148967b73SZhangjin Wu
61248967b73SZhangjin Wu for (test = min; test >= 0 && test <= max; test++) {
61348967b73SZhangjin Wu int llen = 0; /* line length */
61448967b73SZhangjin Wu
61548967b73SZhangjin Wu /* avoid leaving empty lines below, this will insert holes into
61648967b73SZhangjin Wu * test numbers.
61748967b73SZhangjin Wu */
61848967b73SZhangjin Wu switch (test + __LINE__ + 1) {
61948967b73SZhangjin Wu CASE_TEST(argc); EXPECT_GE(1, test_argc, 1); break;
62048967b73SZhangjin Wu CASE_TEST(argv_addr); EXPECT_PTRGT(1, test_argv, brk); break;
62148967b73SZhangjin Wu CASE_TEST(argv_environ); EXPECT_PTRLT(1, test_argv, environ); break;
62248967b73SZhangjin Wu CASE_TEST(argv_total); EXPECT_EQ(1, environ - test_argv - 1, test_argc ?: 1); break;
62348967b73SZhangjin Wu CASE_TEST(argv0_addr); EXPECT_PTRGT(1, argv0, brk); break;
62448967b73SZhangjin Wu CASE_TEST(argv0_str); EXPECT_STRNZ(1, argv0 > brk ? argv0 : NULL); break;
62548967b73SZhangjin Wu CASE_TEST(argv0_len); EXPECT_GE(1, argv0 > brk ? strlen(argv0) : 0, 1); break;
62648967b73SZhangjin Wu CASE_TEST(environ_addr); EXPECT_PTRGT(1, environ, brk); break;
62748967b73SZhangjin Wu CASE_TEST(environ_envp); EXPECT_PTREQ(1, environ, test_envp); break;
62848967b73SZhangjin Wu CASE_TEST(environ_auxv); EXPECT_PTRLT(test_auxv != (void *)-1, environ, test_auxv); break;
62948967b73SZhangjin Wu CASE_TEST(environ_total); EXPECT_GE(test_auxv != (void *)-1, (void *)test_auxv - (void *)environ - 1, env_total); break;
63048967b73SZhangjin Wu CASE_TEST(environ_HOME); EXPECT_PTRNZ(1, getenv("HOME")); break;
63148967b73SZhangjin Wu CASE_TEST(auxv_addr); EXPECT_PTRGT(test_auxv != (void *)-1, test_auxv, brk); break;
63248967b73SZhangjin Wu CASE_TEST(auxv_AT_UID); EXPECT_EQ(1, getauxval(AT_UID), getuid()); break;
63348967b73SZhangjin Wu CASE_TEST(auxv_AT_PAGESZ); EXPECT_GE(1, getauxval(AT_PAGESZ), 4096); break;
63448967b73SZhangjin Wu case __LINE__:
63548967b73SZhangjin Wu return ret; /* must be last */
63648967b73SZhangjin Wu /* note: do not set any defaults so as to permit holes above */
63748967b73SZhangjin Wu }
63848967b73SZhangjin Wu }
63948967b73SZhangjin Wu return ret;
64048967b73SZhangjin Wu }
64148967b73SZhangjin Wu
642362aecb2SWilly Tarreau
643b4844fa0SWilly Tarreau /* used by some syscall tests below */
test_getdents64(const char * dir)644b4844fa0SWilly Tarreau int test_getdents64(const char *dir)
645b4844fa0SWilly Tarreau {
646b4844fa0SWilly Tarreau char buffer[4096];
647b4844fa0SWilly Tarreau int fd, ret;
648b4844fa0SWilly Tarreau int err;
649b4844fa0SWilly Tarreau
650b4844fa0SWilly Tarreau ret = fd = open(dir, O_RDONLY | O_DIRECTORY, 0);
651b4844fa0SWilly Tarreau if (ret < 0)
652b4844fa0SWilly Tarreau return ret;
653b4844fa0SWilly Tarreau
654b4844fa0SWilly Tarreau ret = getdents64(fd, (void *)buffer, sizeof(buffer));
655b4844fa0SWilly Tarreau err = errno;
656b4844fa0SWilly Tarreau close(fd);
657b4844fa0SWilly Tarreau
658b4844fa0SWilly Tarreau errno = err;
659b4844fa0SWilly Tarreau return ret;
660b4844fa0SWilly Tarreau }
661b4844fa0SWilly Tarreau
test_getpagesize(void)66217e66f23SThomas Weißschuh int test_getpagesize(void)
663a290296aSAmmar Faizi {
66464077502SThomas Weißschuh int x = getpagesize();
665a290296aSAmmar Faizi int c;
666a290296aSAmmar Faizi
667a290296aSAmmar Faizi if (x < 0)
668a290296aSAmmar Faizi return x;
669a290296aSAmmar Faizi
670a290296aSAmmar Faizi #if defined(__x86_64__) || defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__)
671a290296aSAmmar Faizi /*
672a290296aSAmmar Faizi * x86 family is always 4K page.
673a290296aSAmmar Faizi */
674a290296aSAmmar Faizi c = (x == 4096);
675a290296aSAmmar Faizi #elif defined(__aarch64__)
676a290296aSAmmar Faizi /*
677a290296aSAmmar Faizi * Linux aarch64 supports three values of page size: 4K, 16K, and 64K
678a290296aSAmmar Faizi * which are selected at kernel compilation time.
679a290296aSAmmar Faizi */
680a290296aSAmmar Faizi c = (x == 4096 || x == (16 * 1024) || x == (64 * 1024));
681a290296aSAmmar Faizi #else
682a290296aSAmmar Faizi /*
683a290296aSAmmar Faizi * Assuming other architectures must have at least 4K page.
684a290296aSAmmar Faizi */
685a290296aSAmmar Faizi c = (x >= 4096);
686a290296aSAmmar Faizi #endif
687a290296aSAmmar Faizi
688a290296aSAmmar Faizi return !c;
689a290296aSAmmar Faizi }
690a290296aSAmmar Faizi
test_fork(void)69117e66f23SThomas Weißschuh int test_fork(void)
6923ad09d72SThomas Weißschuh {
6933ad09d72SThomas Weißschuh int status;
694ed495f09SZhangjin Wu pid_t pid;
695ed495f09SZhangjin Wu
696ed495f09SZhangjin Wu /* flush the printf buffer to avoid child flush it */
697ed495f09SZhangjin Wu fflush(stdout);
698ed495f09SZhangjin Wu fflush(stderr);
699ed495f09SZhangjin Wu
700ed495f09SZhangjin Wu pid = fork();
7013ad09d72SThomas Weißschuh
7023ad09d72SThomas Weißschuh switch (pid) {
7033ad09d72SThomas Weißschuh case -1:
7043ad09d72SThomas Weißschuh return 1;
7053ad09d72SThomas Weißschuh
7063ad09d72SThomas Weißschuh case 0:
7073ad09d72SThomas Weißschuh exit(123);
7083ad09d72SThomas Weißschuh
7093ad09d72SThomas Weißschuh default:
7103ad09d72SThomas Weißschuh pid = waitpid(pid, &status, 0);
7113ad09d72SThomas Weißschuh
7123ad09d72SThomas Weißschuh return pid == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 123;
7133ad09d72SThomas Weißschuh }
7143ad09d72SThomas Weißschuh }
7153ad09d72SThomas Weißschuh
test_stat_timestamps(void)71617e66f23SThomas Weißschuh int test_stat_timestamps(void)
71787b9fa66SThomas Weißschuh {
71887b9fa66SThomas Weißschuh struct stat st;
71987b9fa66SThomas Weißschuh
72087b9fa66SThomas Weißschuh if (sizeof(st.st_atim.tv_sec) != sizeof(st.st_atime))
72187b9fa66SThomas Weißschuh return 1;
72287b9fa66SThomas Weißschuh
723f576d3c0SZhangjin Wu if (stat("/proc/self/", &st) && stat(argv0, &st) && stat("/", &st))
72487b9fa66SThomas Weißschuh return 1;
72587b9fa66SThomas Weißschuh
72687b9fa66SThomas Weißschuh if (st.st_atim.tv_sec != st.st_atime || st.st_atim.tv_nsec > 1000000000)
72787b9fa66SThomas Weißschuh return 1;
72887b9fa66SThomas Weißschuh
72987b9fa66SThomas Weißschuh if (st.st_mtim.tv_sec != st.st_mtime || st.st_mtim.tv_nsec > 1000000000)
73087b9fa66SThomas Weißschuh return 1;
73187b9fa66SThomas Weißschuh
73287b9fa66SThomas Weißschuh if (st.st_ctim.tv_sec != st.st_ctime || st.st_ctim.tv_nsec > 1000000000)
73387b9fa66SThomas Weißschuh return 1;
73487b9fa66SThomas Weißschuh
73587b9fa66SThomas Weißschuh return 0;
73687b9fa66SThomas Weißschuh }
73787b9fa66SThomas Weißschuh
test_mmap_munmap(void)738fcdbf5ddSZhangjin Wu int test_mmap_munmap(void)
739fcdbf5ddSZhangjin Wu {
740711f91fdSThomas Weißschuh int ret, fd, i, page_size;
741fcdbf5ddSZhangjin Wu void *mem;
742711f91fdSThomas Weißschuh size_t file_size, length;
743fcdbf5ddSZhangjin Wu off_t offset, pa_offset;
744fcdbf5ddSZhangjin Wu struct stat stat_buf;
745fcdbf5ddSZhangjin Wu const char * const files[] = {
746fcdbf5ddSZhangjin Wu "/dev/zero",
747fcdbf5ddSZhangjin Wu "/proc/1/exe", "/proc/self/exe",
748fcdbf5ddSZhangjin Wu argv0,
749fcdbf5ddSZhangjin Wu NULL
750fcdbf5ddSZhangjin Wu };
751fcdbf5ddSZhangjin Wu
752fcdbf5ddSZhangjin Wu page_size = getpagesize();
753fcdbf5ddSZhangjin Wu if (page_size < 0)
754e7d0129dSZhangjin Wu return 1;
755fcdbf5ddSZhangjin Wu
756fcdbf5ddSZhangjin Wu /* find a right file to mmap, existed and accessible */
757fcdbf5ddSZhangjin Wu for (i = 0; files[i] != NULL; i++) {
758fcdbf5ddSZhangjin Wu ret = fd = open(files[i], O_RDONLY);
759fcdbf5ddSZhangjin Wu if (ret == -1)
760fcdbf5ddSZhangjin Wu continue;
761fcdbf5ddSZhangjin Wu else
762fcdbf5ddSZhangjin Wu break;
763fcdbf5ddSZhangjin Wu }
764fcdbf5ddSZhangjin Wu if (ret == -1)
765e7d0129dSZhangjin Wu return 1;
766fcdbf5ddSZhangjin Wu
767fcdbf5ddSZhangjin Wu ret = stat(files[i], &stat_buf);
768fcdbf5ddSZhangjin Wu if (ret == -1)
769fcdbf5ddSZhangjin Wu goto end;
770fcdbf5ddSZhangjin Wu
771fcdbf5ddSZhangjin Wu /* file size of the special /dev/zero is 0, let's assign one manually */
772fcdbf5ddSZhangjin Wu if (i == 0)
773fcdbf5ddSZhangjin Wu file_size = 3*page_size;
774fcdbf5ddSZhangjin Wu else
775fcdbf5ddSZhangjin Wu file_size = stat_buf.st_size;
776fcdbf5ddSZhangjin Wu
777fcdbf5ddSZhangjin Wu offset = file_size - 1;
778fcdbf5ddSZhangjin Wu if (offset < 0)
779fcdbf5ddSZhangjin Wu offset = 0;
780fcdbf5ddSZhangjin Wu length = file_size - offset;
781fcdbf5ddSZhangjin Wu pa_offset = offset & ~(page_size - 1);
782fcdbf5ddSZhangjin Wu
783fcdbf5ddSZhangjin Wu mem = mmap(NULL, length + offset - pa_offset, PROT_READ, MAP_SHARED, fd, pa_offset);
784fcdbf5ddSZhangjin Wu if (mem == MAP_FAILED) {
785e7d0129dSZhangjin Wu ret = 1;
786fcdbf5ddSZhangjin Wu goto end;
787fcdbf5ddSZhangjin Wu }
788fcdbf5ddSZhangjin Wu
789fcdbf5ddSZhangjin Wu ret = munmap(mem, length + offset - pa_offset);
790fcdbf5ddSZhangjin Wu
791fcdbf5ddSZhangjin Wu end:
792fcdbf5ddSZhangjin Wu close(fd);
793e7d0129dSZhangjin Wu return !!ret;
794fcdbf5ddSZhangjin Wu }
795fcdbf5ddSZhangjin Wu
test_pipe(void)79617e66f23SThomas Weißschuh int test_pipe(void)
7975c01259bSYuan Tan {
7985c01259bSYuan Tan const char *const msg = "hello, nolibc";
7995c01259bSYuan Tan int pipefd[2];
8005c01259bSYuan Tan char buf[32];
8015c01259bSYuan Tan size_t len;
8025c01259bSYuan Tan
8035c01259bSYuan Tan if (pipe(pipefd) == -1)
8045c01259bSYuan Tan return 1;
8055c01259bSYuan Tan
8065c01259bSYuan Tan write(pipefd[1], msg, strlen(msg));
8075c01259bSYuan Tan close(pipefd[1]);
8085c01259bSYuan Tan len = read(pipefd[0], buf, sizeof(buf));
8095c01259bSYuan Tan close(pipefd[0]);
8105c01259bSYuan Tan
8115c01259bSYuan Tan if (len != strlen(msg))
8125c01259bSYuan Tan return 1;
8135c01259bSYuan Tan
8145c01259bSYuan Tan return !!memcmp(buf, msg, len);
8155c01259bSYuan Tan }
8165c01259bSYuan Tan
817fcdbf5ddSZhangjin Wu
818b4844fa0SWilly Tarreau /* Run syscall tests between IDs <min> and <max>.
819b4844fa0SWilly Tarreau * Return 0 on success, non-zero on failure.
820b4844fa0SWilly Tarreau */
run_syscall(int min,int max)821b4844fa0SWilly Tarreau int run_syscall(int min, int max)
822b4844fa0SWilly Tarreau {
823957bfa31SZhangjin Wu struct timeval tv;
824957bfa31SZhangjin Wu struct timezone tz;
825b4844fa0SWilly Tarreau struct stat stat_buf;
8263e2d337bSWilly Tarreau int euid0;
8277172f1c6SWilly Tarreau int proc;
828b4844fa0SWilly Tarreau int test;
829b4844fa0SWilly Tarreau int tmp;
830b4844fa0SWilly Tarreau int ret = 0;
831b4844fa0SWilly Tarreau void *p1, *p2;
83279b4f68eSZhangjin Wu int has_gettid = 1;
833b4844fa0SWilly Tarreau
8347172f1c6SWilly Tarreau /* <proc> indicates whether or not /proc is mounted */
8357172f1c6SWilly Tarreau proc = stat("/proc", &stat_buf) == 0;
8367172f1c6SWilly Tarreau
8373e2d337bSWilly Tarreau /* this will be used to skip certain tests that can't be run unprivileged */
8383e2d337bSWilly Tarreau euid0 = geteuid() == 0;
8393e2d337bSWilly Tarreau
84079b4f68eSZhangjin Wu /* from 2.30, glibc provides gettid() */
84179b4f68eSZhangjin Wu #if defined(__GLIBC_MINOR__) && defined(__GLIBC__)
84279b4f68eSZhangjin Wu has_gettid = __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 30);
84379b4f68eSZhangjin Wu #endif
84479b4f68eSZhangjin Wu
845b4844fa0SWilly Tarreau for (test = min; test >= 0 && test <= max; test++) {
846fddc8f81SThomas Weißschuh int llen = 0; /* line length */
847b4844fa0SWilly Tarreau
848b4844fa0SWilly Tarreau /* avoid leaving empty lines below, this will insert holes into
849b4844fa0SWilly Tarreau * test numbers.
850b4844fa0SWilly Tarreau */
851b4844fa0SWilly Tarreau switch (test + __LINE__ + 1) {
852b4844fa0SWilly Tarreau CASE_TEST(getpid); EXPECT_SYSNE(1, getpid(), -1); break;
853b4844fa0SWilly Tarreau CASE_TEST(getppid); EXPECT_SYSNE(1, getppid(), -1); break;
85479b4f68eSZhangjin Wu CASE_TEST(gettid); EXPECT_SYSNE(has_gettid, gettid(), -1); break;
855b4844fa0SWilly Tarreau CASE_TEST(getpgid_self); EXPECT_SYSNE(1, getpgid(0), -1); break;
856b4844fa0SWilly Tarreau CASE_TEST(getpgid_bad); EXPECT_SYSER(1, getpgid(-1), -1, ESRCH); break;
857b4844fa0SWilly Tarreau CASE_TEST(kill_0); EXPECT_SYSZR(1, kill(getpid(), 0)); break;
858b4844fa0SWilly Tarreau CASE_TEST(kill_CONT); EXPECT_SYSZR(1, kill(getpid(), 0)); break;
859b4844fa0SWilly Tarreau CASE_TEST(kill_BADPID); EXPECT_SYSER(1, kill(INT_MAX, 0), -1, ESRCH); break;
860f193ecbfSZhangjin Wu CASE_TEST(sbrk_0); EXPECT_PTRNE(1, sbrk(0), (void *)-1); break;
861b4844fa0SWilly Tarreau CASE_TEST(sbrk); if ((p1 = p2 = sbrk(4096)) != (void *)-1) p2 = sbrk(-4096); EXPECT_SYSZR(1, (p2 == (void *)-1) || p2 == p1); break;
862b4844fa0SWilly Tarreau CASE_TEST(brk); EXPECT_SYSZR(1, brk(sbrk(0))); break;
86338fc0a35SZhangjin Wu CASE_TEST(chdir_root); EXPECT_SYSZR(1, chdir("/")); chdir(getenv("PWD")); break;
864b4844fa0SWilly Tarreau CASE_TEST(chdir_dot); EXPECT_SYSZR(1, chdir(".")); break;
865b4844fa0SWilly Tarreau CASE_TEST(chdir_blah); EXPECT_SYSER(1, chdir("/blah"), -1, ENOENT); break;
866148e9718SZhangjin Wu CASE_TEST(chmod_argv0); EXPECT_SYSZR(1, chmod(argv0, 0555)); break;
8677172f1c6SWilly Tarreau CASE_TEST(chmod_self); EXPECT_SYSER(proc, chmod("/proc/self", 0555), -1, EPERM); break;
8687172f1c6SWilly Tarreau CASE_TEST(chown_self); EXPECT_SYSER(proc, chown("/proc/self", 0, 0), -1, EPERM); break;
8693e2d337bSWilly Tarreau CASE_TEST(chroot_root); EXPECT_SYSZR(euid0, chroot("/")); break;
870b4844fa0SWilly Tarreau CASE_TEST(chroot_blah); EXPECT_SYSER(1, chroot("/proc/self/blah"), -1, ENOENT); break;
871135b622eSZhangjin Wu CASE_TEST(chroot_exe); EXPECT_SYSER(1, chroot(argv0), -1, ENOTDIR); break;
872b4844fa0SWilly Tarreau CASE_TEST(close_m1); EXPECT_SYSER(1, close(-1), -1, EBADF); break;
873b4844fa0SWilly Tarreau CASE_TEST(close_dup); EXPECT_SYSZR(1, close(dup(0))); break;
874b4844fa0SWilly Tarreau CASE_TEST(dup_0); tmp = dup(0); EXPECT_SYSNE(1, tmp, -1); close(tmp); break;
875b4844fa0SWilly Tarreau CASE_TEST(dup_m1); tmp = dup(-1); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break;
876b4844fa0SWilly Tarreau CASE_TEST(dup2_0); tmp = dup2(0, 100); EXPECT_SYSNE(1, tmp, -1); close(tmp); break;
877b4844fa0SWilly Tarreau CASE_TEST(dup2_m1); tmp = dup2(-1, 100); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break;
878b4844fa0SWilly Tarreau CASE_TEST(dup3_0); tmp = dup3(0, 100, 0); EXPECT_SYSNE(1, tmp, -1); close(tmp); break;
879b4844fa0SWilly Tarreau CASE_TEST(dup3_m1); tmp = dup3(-1, 100, 0); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break;
880b4844fa0SWilly Tarreau CASE_TEST(execve_root); EXPECT_SYSER(1, execve("/", (char*[]){ [0] = "/", [1] = NULL }, NULL), -1, EACCES); break;
8813ad09d72SThomas Weißschuh CASE_TEST(fork); EXPECT_SYSZR(1, test_fork()); break;
882b4844fa0SWilly Tarreau CASE_TEST(getdents64_root); EXPECT_SYSNE(1, test_getdents64("/"), -1); break;
883b4844fa0SWilly Tarreau CASE_TEST(getdents64_null); EXPECT_SYSER(1, test_getdents64("/dev/null"), -1, ENOTDIR); break;
884957bfa31SZhangjin Wu CASE_TEST(gettimeofday_tv); EXPECT_SYSZR(1, gettimeofday(&tv, NULL)); break;
885957bfa31SZhangjin Wu CASE_TEST(gettimeofday_tv_tz);EXPECT_SYSZR(1, gettimeofday(&tv, &tz)); break;
886a290296aSAmmar Faizi CASE_TEST(getpagesize); EXPECT_SYSZR(1, test_getpagesize()); break;
887b4844fa0SWilly Tarreau CASE_TEST(ioctl_tiocinq); EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); break;
888b4844fa0SWilly Tarreau CASE_TEST(ioctl_tiocinq); EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); break;
889b4844fa0SWilly Tarreau CASE_TEST(link_root1); EXPECT_SYSER(1, link("/", "/"), -1, EEXIST); break;
890b4844fa0SWilly Tarreau CASE_TEST(link_blah); EXPECT_SYSER(1, link("/proc/self/blah", "/blah"), -1, ENOENT); break;
8913e2d337bSWilly Tarreau CASE_TEST(link_dir); EXPECT_SYSER(euid0, link("/", "/blah"), -1, EPERM); break;
892f7a419e3SZhangjin Wu CASE_TEST(link_cross); EXPECT_SYSER(proc, link("/proc/self/cmdline", "/blah"), -1, EXDEV); break;
893b4844fa0SWilly Tarreau CASE_TEST(lseek_m1); EXPECT_SYSER(1, lseek(-1, 0, SEEK_SET), -1, EBADF); break;
894b4844fa0SWilly Tarreau CASE_TEST(lseek_0); EXPECT_SYSER(1, lseek(0, 0, SEEK_SET), -1, ESPIPE); break;
895b4844fa0SWilly Tarreau CASE_TEST(mkdir_root); EXPECT_SYSER(1, mkdir("/", 0755), -1, EEXIST); break;
896d4a3b2b9SZhangjin Wu CASE_TEST(mmap_bad); EXPECT_PTRER(1, mmap(NULL, 0, PROT_READ, MAP_PRIVATE, 0, 0), MAP_FAILED, EINVAL); break;
897ba3d0892SZhangjin Wu CASE_TEST(munmap_bad); EXPECT_SYSER(1, munmap((void *)1, 0), -1, EINVAL); break;
898fcdbf5ddSZhangjin Wu CASE_TEST(mmap_munmap_good); EXPECT_SYSZR(1, test_mmap_munmap()); break;
899b4844fa0SWilly Tarreau CASE_TEST(open_tty); EXPECT_SYSNE(1, tmp = open("/dev/null", 0), -1); if (tmp != -1) close(tmp); break;
900b4844fa0SWilly Tarreau CASE_TEST(open_blah); EXPECT_SYSER(1, tmp = open("/proc/self/blah", 0), -1, ENOENT); if (tmp != -1) close(tmp); break;
9015c01259bSYuan Tan CASE_TEST(pipe); EXPECT_SYSZR(1, test_pipe()); break;
902b4844fa0SWilly Tarreau CASE_TEST(poll_null); EXPECT_SYSZR(1, poll(NULL, 0, 0)); break;
903b4844fa0SWilly Tarreau CASE_TEST(poll_stdout); EXPECT_SYSNE(1, ({ struct pollfd fds = { 1, POLLOUT, 0}; poll(&fds, 1, 0); }), -1); break;
904b4844fa0SWilly Tarreau CASE_TEST(poll_fault); EXPECT_SYSER(1, poll((void *)1, 1, 0), -1, EFAULT); break;
905208aa9d9SThomas Weißschuh CASE_TEST(prctl); EXPECT_SYSER(1, prctl(PR_SET_NAME, (unsigned long)NULL, 0, 0, 0), -1, EFAULT); break;
906b4844fa0SWilly Tarreau CASE_TEST(read_badf); EXPECT_SYSER(1, read(-1, &tmp, 1), -1, EBADF); break;
9074e14e844SZhangjin Wu CASE_TEST(rmdir_blah); EXPECT_SYSER(1, rmdir("/blah"), -1, ENOENT); break;
908b4844fa0SWilly Tarreau CASE_TEST(sched_yield); EXPECT_SYSZR(1, sched_yield()); break;
909b4844fa0SWilly Tarreau CASE_TEST(select_null); EXPECT_SYSZR(1, ({ struct timeval tv = { 0 }; select(0, NULL, NULL, NULL, &tv); })); break;
910b4844fa0SWilly 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;
911b4844fa0SWilly Tarreau CASE_TEST(select_fault); EXPECT_SYSER(1, select(1, (void *)1, NULL, NULL, 0), -1, EFAULT); break;
912b4844fa0SWilly Tarreau CASE_TEST(stat_blah); EXPECT_SYSER(1, stat("/proc/self/blah", &stat_buf), -1, ENOENT); break;
91346cf630cSZhangjin Wu CASE_TEST(stat_fault); EXPECT_SYSER(1, stat((void *)1, &stat_buf), -1, EFAULT); break;
91487b9fa66SThomas Weißschuh CASE_TEST(stat_timestamps); EXPECT_SYSZR(1, test_stat_timestamps()); break;
915b4844fa0SWilly Tarreau CASE_TEST(symlink_root); EXPECT_SYSER(1, symlink("/", "/"), -1, EEXIST); break;
916b4844fa0SWilly Tarreau CASE_TEST(unlink_root); EXPECT_SYSER(1, unlink("/"), -1, EISDIR); break;
917b4844fa0SWilly Tarreau CASE_TEST(unlink_blah); EXPECT_SYSER(1, unlink("/proc/self/blah"), -1, ENOENT); break;
918b4844fa0SWilly Tarreau CASE_TEST(wait_child); EXPECT_SYSER(1, wait(&tmp), -1, ECHILD); break;
919b4844fa0SWilly Tarreau CASE_TEST(waitpid_min); EXPECT_SYSER(1, waitpid(INT_MIN, &tmp, WNOHANG), -1, ESRCH); break;
920b4844fa0SWilly Tarreau CASE_TEST(waitpid_child); EXPECT_SYSER(1, waitpid(getpid(), &tmp, WNOHANG), -1, ECHILD); break;
921b4844fa0SWilly Tarreau CASE_TEST(write_badf); EXPECT_SYSER(1, write(-1, &tmp, 1), -1, EBADF); break;
922b4844fa0SWilly Tarreau CASE_TEST(write_zero); EXPECT_SYSZR(1, write(1, &tmp, 0)); break;
92353fcfafaSThomas Weißschuh CASE_TEST(syscall_noargs); EXPECT_SYSEQ(1, syscall(__NR_getpid), getpid()); break;
924ec8e1b73SZhangjin Wu CASE_TEST(syscall_args); EXPECT_SYSER(1, syscall(__NR_statx, 0, NULL, 0, 0, NULL), -1, EFAULT); break;
925b4844fa0SWilly Tarreau case __LINE__:
926b4844fa0SWilly Tarreau return ret; /* must be last */
927b4844fa0SWilly Tarreau /* note: do not set any defaults so as to permit holes above */
928b4844fa0SWilly Tarreau }
929b4844fa0SWilly Tarreau }
930b4844fa0SWilly Tarreau return ret;
931b4844fa0SWilly Tarreau }
932b4844fa0SWilly Tarreau
run_stdlib(int min,int max)93395bc9894SWilly Tarreau int run_stdlib(int min, int max)
93495bc9894SWilly Tarreau {
93595bc9894SWilly Tarreau int test;
93695bc9894SWilly Tarreau int ret = 0;
93795bc9894SWilly Tarreau
93895bc9894SWilly Tarreau for (test = min; test >= 0 && test <= max; test++) {
939fddc8f81SThomas Weißschuh int llen = 0; /* line length */
94095bc9894SWilly Tarreau
94195bc9894SWilly Tarreau /* avoid leaving empty lines below, this will insert holes into
94295bc9894SWilly Tarreau * test numbers.
94395bc9894SWilly Tarreau */
94495bc9894SWilly Tarreau switch (test + __LINE__ + 1) {
94595bc9894SWilly Tarreau CASE_TEST(getenv_TERM); EXPECT_STRNZ(1, getenv("TERM")); break;
94695bc9894SWilly Tarreau CASE_TEST(getenv_blah); EXPECT_STRZR(1, getenv("blah")); break;
94795bc9894SWilly Tarreau CASE_TEST(setcmp_blah_blah); EXPECT_EQ(1, strcmp("blah", "blah"), 0); break;
94895bc9894SWilly Tarreau CASE_TEST(setcmp_blah_blah2); EXPECT_NE(1, strcmp("blah", "blah2"), 0); break;
94995bc9894SWilly Tarreau CASE_TEST(setncmp_blah_blah); EXPECT_EQ(1, strncmp("blah", "blah", 10), 0); break;
95095bc9894SWilly Tarreau CASE_TEST(setncmp_blah_blah4); EXPECT_EQ(1, strncmp("blah", "blah4", 4), 0); break;
95195bc9894SWilly Tarreau CASE_TEST(setncmp_blah_blah5); EXPECT_NE(1, strncmp("blah", "blah5", 5), 0); break;
95295bc9894SWilly Tarreau CASE_TEST(setncmp_blah_blah6); EXPECT_NE(1, strncmp("blah", "blah6", 6), 0); break;
95395bc9894SWilly Tarreau CASE_TEST(strchr_foobar_o); EXPECT_STREQ(1, strchr("foobar", 'o'), "oobar"); break;
95495bc9894SWilly Tarreau CASE_TEST(strchr_foobar_z); EXPECT_STRZR(1, strchr("foobar", 'z')); break;
95595bc9894SWilly Tarreau CASE_TEST(strrchr_foobar_o); EXPECT_STREQ(1, strrchr("foobar", 'o'), "obar"); break;
95695bc9894SWilly Tarreau CASE_TEST(strrchr_foobar_z); EXPECT_STRZR(1, strrchr("foobar", 'z')); break;
957c80b5a0aSWilly Tarreau CASE_TEST(memcmp_20_20); EXPECT_EQ(1, memcmp("aaa\x20", "aaa\x20", 4), 0); break;
958c80b5a0aSWilly Tarreau CASE_TEST(memcmp_20_60); EXPECT_LT(1, memcmp("aaa\x20", "aaa\x60", 4), 0); break;
959c80b5a0aSWilly Tarreau CASE_TEST(memcmp_60_20); EXPECT_GT(1, memcmp("aaa\x60", "aaa\x20", 4), 0); break;
960c80b5a0aSWilly Tarreau CASE_TEST(memcmp_20_e0); EXPECT_LT(1, memcmp("aaa\x20", "aaa\xe0", 4), 0); break;
961c80b5a0aSWilly Tarreau CASE_TEST(memcmp_e0_20); EXPECT_GT(1, memcmp("aaa\xe0", "aaa\x20", 4), 0); break;
962c80b5a0aSWilly Tarreau CASE_TEST(memcmp_80_e0); EXPECT_LT(1, memcmp("aaa\x80", "aaa\xe0", 4), 0); break;
963c80b5a0aSWilly Tarreau CASE_TEST(memcmp_e0_80); EXPECT_GT(1, memcmp("aaa\xe0", "aaa\x80", 4), 0); break;
964d1209597SVincent Dagonneau CASE_TEST(limit_int8_max); EXPECT_EQ(1, INT8_MAX, (int8_t) 0x7f); break;
965d1209597SVincent Dagonneau CASE_TEST(limit_int8_min); EXPECT_EQ(1, INT8_MIN, (int8_t) 0x80); break;
966d1209597SVincent Dagonneau CASE_TEST(limit_uint8_max); EXPECT_EQ(1, UINT8_MAX, (uint8_t) 0xff); break;
967d1209597SVincent Dagonneau CASE_TEST(limit_int16_max); EXPECT_EQ(1, INT16_MAX, (int16_t) 0x7fff); break;
968d1209597SVincent Dagonneau CASE_TEST(limit_int16_min); EXPECT_EQ(1, INT16_MIN, (int16_t) 0x8000); break;
969d1209597SVincent Dagonneau CASE_TEST(limit_uint16_max); EXPECT_EQ(1, UINT16_MAX, (uint16_t) 0xffff); break;
970d1209597SVincent Dagonneau CASE_TEST(limit_int32_max); EXPECT_EQ(1, INT32_MAX, (int32_t) 0x7fffffff); break;
971d1209597SVincent Dagonneau CASE_TEST(limit_int32_min); EXPECT_EQ(1, INT32_MIN, (int32_t) 0x80000000); break;
972d1209597SVincent Dagonneau CASE_TEST(limit_uint32_max); EXPECT_EQ(1, UINT32_MAX, (uint32_t) 0xffffffff); break;
973d1209597SVincent Dagonneau CASE_TEST(limit_int64_max); EXPECT_EQ(1, INT64_MAX, (int64_t) 0x7fffffffffffffff); break;
974d1209597SVincent Dagonneau CASE_TEST(limit_int64_min); EXPECT_EQ(1, INT64_MIN, (int64_t) 0x8000000000000000); break;
975d1209597SVincent Dagonneau CASE_TEST(limit_uint64_max); EXPECT_EQ(1, UINT64_MAX, (uint64_t) 0xffffffffffffffff); break;
976d1209597SVincent Dagonneau CASE_TEST(limit_int_least8_max); EXPECT_EQ(1, INT_LEAST8_MAX, (int_least8_t) 0x7f); break;
977d1209597SVincent Dagonneau CASE_TEST(limit_int_least8_min); EXPECT_EQ(1, INT_LEAST8_MIN, (int_least8_t) 0x80); break;
978d1209597SVincent Dagonneau CASE_TEST(limit_uint_least8_max); EXPECT_EQ(1, UINT_LEAST8_MAX, (uint_least8_t) 0xff); break;
979d1209597SVincent Dagonneau CASE_TEST(limit_int_least16_max); EXPECT_EQ(1, INT_LEAST16_MAX, (int_least16_t) 0x7fff); break;
980d1209597SVincent Dagonneau CASE_TEST(limit_int_least16_min); EXPECT_EQ(1, INT_LEAST16_MIN, (int_least16_t) 0x8000); break;
981d1209597SVincent Dagonneau CASE_TEST(limit_uint_least16_max); EXPECT_EQ(1, UINT_LEAST16_MAX, (uint_least16_t) 0xffff); break;
982d1209597SVincent Dagonneau CASE_TEST(limit_int_least32_max); EXPECT_EQ(1, INT_LEAST32_MAX, (int_least32_t) 0x7fffffff); break;
983d1209597SVincent Dagonneau CASE_TEST(limit_int_least32_min); EXPECT_EQ(1, INT_LEAST32_MIN, (int_least32_t) 0x80000000); break;
984d1209597SVincent Dagonneau CASE_TEST(limit_uint_least32_max); EXPECT_EQ(1, UINT_LEAST32_MAX, (uint_least32_t) 0xffffffffU); break;
985d1209597SVincent Dagonneau CASE_TEST(limit_int_least64_min); EXPECT_EQ(1, INT_LEAST64_MIN, (int_least64_t) 0x8000000000000000LL); break;
986d1209597SVincent Dagonneau CASE_TEST(limit_int_least64_max); EXPECT_EQ(1, INT_LEAST64_MAX, (int_least64_t) 0x7fffffffffffffffLL); break;
987d1209597SVincent Dagonneau CASE_TEST(limit_uint_least64_max); EXPECT_EQ(1, UINT_LEAST64_MAX, (uint_least64_t) 0xffffffffffffffffULL); break;
988d1209597SVincent Dagonneau CASE_TEST(limit_int_fast8_max); EXPECT_EQ(1, INT_FAST8_MAX, (int_fast8_t) 0x7f); break;
989d1209597SVincent Dagonneau CASE_TEST(limit_int_fast8_min); EXPECT_EQ(1, INT_FAST8_MIN, (int_fast8_t) 0x80); break;
990d1209597SVincent Dagonneau CASE_TEST(limit_uint_fast8_max); EXPECT_EQ(1, UINT_FAST8_MAX, (uint_fast8_t) 0xff); break;
991989abf1cSZhangjin Wu CASE_TEST(limit_int_fast16_min); EXPECT_EQ(1, INT_FAST16_MIN, (int_fast16_t) SINT_MIN_OF_TYPE(int_fast16_t)); break;
992989abf1cSZhangjin Wu CASE_TEST(limit_int_fast16_max); EXPECT_EQ(1, INT_FAST16_MAX, (int_fast16_t) SINT_MAX_OF_TYPE(int_fast16_t)); break;
993d1209597SVincent Dagonneau CASE_TEST(limit_uint_fast16_max); EXPECT_EQ(1, UINT_FAST16_MAX, (uint_fast16_t) UINTPTR_MAX); break;
994989abf1cSZhangjin Wu CASE_TEST(limit_int_fast32_min); EXPECT_EQ(1, INT_FAST32_MIN, (int_fast32_t) SINT_MIN_OF_TYPE(int_fast32_t)); break;
995989abf1cSZhangjin Wu CASE_TEST(limit_int_fast32_max); EXPECT_EQ(1, INT_FAST32_MAX, (int_fast32_t) SINT_MAX_OF_TYPE(int_fast32_t)); break;
996d1209597SVincent Dagonneau CASE_TEST(limit_uint_fast32_max); EXPECT_EQ(1, UINT_FAST32_MAX, (uint_fast32_t) UINTPTR_MAX); break;
997f9bf5944SThomas Weißschuh CASE_TEST(limit_int_fast64_min); EXPECT_EQ(1, INT_FAST64_MIN, (int_fast64_t) INT64_MIN); break;
998f9bf5944SThomas Weißschuh CASE_TEST(limit_int_fast64_max); EXPECT_EQ(1, INT_FAST64_MAX, (int_fast64_t) INT64_MAX); break;
999f9bf5944SThomas Weißschuh CASE_TEST(limit_uint_fast64_max); EXPECT_EQ(1, UINT_FAST64_MAX, (uint_fast64_t) UINT64_MAX); break;
1000ceb528feSThomas Weißschuh CASE_TEST(sizeof_long_sane); EXPECT_EQ(1, sizeof(long) == 8 || sizeof(long) == 4, 1); break;
1001ca283457SWilly Tarreau CASE_TEST(limit_intptr_min); EXPECT_EQ(1, INTPTR_MIN, sizeof(long) == 8 ? (intptr_t) 0x8000000000000000LL : (intptr_t) 0x80000000); break;
1002ca283457SWilly Tarreau CASE_TEST(limit_intptr_max); EXPECT_EQ(1, INTPTR_MAX, sizeof(long) == 8 ? (intptr_t) 0x7fffffffffffffffLL : (intptr_t) 0x7fffffff); break;
1003ca283457SWilly Tarreau CASE_TEST(limit_uintptr_max); EXPECT_EQ(1, UINTPTR_MAX, sizeof(long) == 8 ? (uintptr_t) 0xffffffffffffffffULL : (uintptr_t) 0xffffffffU); break;
1004ca283457SWilly Tarreau CASE_TEST(limit_ptrdiff_min); EXPECT_EQ(1, PTRDIFF_MIN, sizeof(long) == 8 ? (ptrdiff_t) 0x8000000000000000LL : (ptrdiff_t) 0x80000000); break;
1005ca283457SWilly Tarreau CASE_TEST(limit_ptrdiff_max); EXPECT_EQ(1, PTRDIFF_MAX, sizeof(long) == 8 ? (ptrdiff_t) 0x7fffffffffffffffLL : (ptrdiff_t) 0x7fffffff); break;
1006ca283457SWilly Tarreau CASE_TEST(limit_size_max); EXPECT_EQ(1, SIZE_MAX, sizeof(long) == 8 ? (size_t) 0xffffffffffffffffULL : (size_t) 0xffffffffU); break;
1007ca283457SWilly Tarreau
100895bc9894SWilly Tarreau case __LINE__:
100995bc9894SWilly Tarreau return ret; /* must be last */
101095bc9894SWilly Tarreau /* note: do not set any defaults so as to permit holes above */
101195bc9894SWilly Tarreau }
101295bc9894SWilly Tarreau }
101395bc9894SWilly Tarreau return ret;
101495bc9894SWilly Tarreau }
101595bc9894SWilly Tarreau
101669f2cd9fSThomas Weißschuh #define EXPECT_VFPRINTF(c, expected, fmt, ...) \
101769f2cd9fSThomas Weißschuh ret += expect_vfprintf(llen, c, expected, fmt, ##__VA_ARGS__)
101869f2cd9fSThomas Weißschuh
expect_vfprintf(int llen,int c,const char * expected,const char * fmt,...)1019711f91fdSThomas Weißschuh static int expect_vfprintf(int llen, int c, const char *expected, const char *fmt, ...)
102069f2cd9fSThomas Weißschuh {
102137266a9eSThomas Weißschuh int ret, fd;
102237266a9eSThomas Weißschuh ssize_t w, r;
102369f2cd9fSThomas Weißschuh char buf[100];
102469f2cd9fSThomas Weißschuh FILE *memfile;
102569f2cd9fSThomas Weißschuh va_list args;
102669f2cd9fSThomas Weißschuh
10276861b1a3SZhangjin Wu fd = open("/tmp", O_TMPFILE | O_EXCL | O_RDWR, 0600);
102869f2cd9fSThomas Weißschuh if (fd == -1) {
1029b184a261SThomas Weißschuh result(llen, SKIPPED);
10306861b1a3SZhangjin Wu return 0;
103169f2cd9fSThomas Weißschuh }
103269f2cd9fSThomas Weißschuh
103369f2cd9fSThomas Weißschuh memfile = fdopen(fd, "w+");
103469f2cd9fSThomas Weißschuh if (!memfile) {
1035b184a261SThomas Weißschuh result(llen, FAIL);
103669f2cd9fSThomas Weißschuh return 1;
103769f2cd9fSThomas Weißschuh }
103869f2cd9fSThomas Weißschuh
103969f2cd9fSThomas Weißschuh va_start(args, fmt);
104069f2cd9fSThomas Weißschuh w = vfprintf(memfile, fmt, args);
104169f2cd9fSThomas Weißschuh va_end(args);
104269f2cd9fSThomas Weißschuh
104369f2cd9fSThomas Weißschuh if (w != c) {
104437266a9eSThomas Weißschuh llen += printf(" written(%d) != %d", (int)w, c);
1045b184a261SThomas Weißschuh result(llen, FAIL);
104669f2cd9fSThomas Weißschuh return 1;
104769f2cd9fSThomas Weißschuh }
104869f2cd9fSThomas Weißschuh
104969f2cd9fSThomas Weißschuh fflush(memfile);
105069f2cd9fSThomas Weißschuh lseek(fd, 0, SEEK_SET);
105169f2cd9fSThomas Weißschuh
105269f2cd9fSThomas Weißschuh r = read(fd, buf, sizeof(buf) - 1);
105369f2cd9fSThomas Weißschuh
105469f2cd9fSThomas Weißschuh fclose(memfile);
105569f2cd9fSThomas Weißschuh
105669f2cd9fSThomas Weißschuh if (r != w) {
105737266a9eSThomas Weißschuh llen += printf(" written(%d) != read(%d)", (int)w, (int)r);
1058b184a261SThomas Weißschuh result(llen, FAIL);
105969f2cd9fSThomas Weißschuh return 1;
106069f2cd9fSThomas Weißschuh }
106169f2cd9fSThomas Weißschuh
10629c5e4900SThomas Weißschuh buf[r] = '\0';
106369f2cd9fSThomas Weißschuh llen += printf(" \"%s\" = \"%s\"", expected, buf);
106469f2cd9fSThomas Weißschuh ret = strncmp(expected, buf, c);
106569f2cd9fSThomas Weißschuh
1066b184a261SThomas Weißschuh result(llen, ret ? FAIL : OK);
106769f2cd9fSThomas Weißschuh return ret;
106869f2cd9fSThomas Weißschuh }
106969f2cd9fSThomas Weißschuh
run_vfprintf(int min,int max)107069f2cd9fSThomas Weißschuh static int run_vfprintf(int min, int max)
107169f2cd9fSThomas Weißschuh {
107269f2cd9fSThomas Weißschuh int test;
107369f2cd9fSThomas Weißschuh int ret = 0;
107469f2cd9fSThomas Weißschuh
107569f2cd9fSThomas Weißschuh for (test = min; test >= 0 && test <= max; test++) {
1076fddc8f81SThomas Weißschuh int llen = 0; /* line length */
107769f2cd9fSThomas Weißschuh
107869f2cd9fSThomas Weißschuh /* avoid leaving empty lines below, this will insert holes into
107969f2cd9fSThomas Weißschuh * test numbers.
108069f2cd9fSThomas Weißschuh */
108169f2cd9fSThomas Weißschuh switch (test + __LINE__ + 1) {
108269f2cd9fSThomas Weißschuh CASE_TEST(empty); EXPECT_VFPRINTF(0, "", ""); break;
108369f2cd9fSThomas Weißschuh CASE_TEST(simple); EXPECT_VFPRINTF(3, "foo", "foo"); break;
108469f2cd9fSThomas Weißschuh CASE_TEST(string); EXPECT_VFPRINTF(3, "foo", "%s", "foo"); break;
108569f2cd9fSThomas Weißschuh CASE_TEST(number); EXPECT_VFPRINTF(4, "1234", "%d", 1234); break;
108669f2cd9fSThomas Weißschuh CASE_TEST(negnumber); EXPECT_VFPRINTF(5, "-1234", "%d", -1234); break;
108769f2cd9fSThomas Weißschuh CASE_TEST(unsigned); EXPECT_VFPRINTF(5, "12345", "%u", 12345); break;
108869f2cd9fSThomas Weißschuh CASE_TEST(char); EXPECT_VFPRINTF(1, "c", "%c", 'c'); break;
108969f2cd9fSThomas Weißschuh CASE_TEST(hex); EXPECT_VFPRINTF(1, "f", "%x", 0xf); break;
109069f2cd9fSThomas Weißschuh CASE_TEST(pointer); EXPECT_VFPRINTF(3, "0x1", "%p", (void *) 0x1); break;
109169f2cd9fSThomas Weißschuh case __LINE__:
109269f2cd9fSThomas Weißschuh return ret; /* must be last */
109369f2cd9fSThomas Weißschuh /* note: do not set any defaults so as to permit holes above */
109469f2cd9fSThomas Weißschuh }
109569f2cd9fSThomas Weißschuh }
109669f2cd9fSThomas Weißschuh return ret;
109769f2cd9fSThomas Weißschuh }
109869f2cd9fSThomas Weißschuh
smash_stack(void)109997357168SThomas Weißschuh static int smash_stack(void)
110097357168SThomas Weißschuh {
110197357168SThomas Weißschuh char buf[100];
1102e7654c3fSThomas Weißschuh volatile char *ptr = buf;
1103aa662d12SWilly Tarreau size_t i;
110497357168SThomas Weißschuh
1105aa662d12SWilly Tarreau for (i = 0; i < 200; i++)
1106e7654c3fSThomas Weißschuh ptr[i] = 'P';
110797357168SThomas Weißschuh
110897357168SThomas Weißschuh return 1;
110997357168SThomas Weißschuh }
111097357168SThomas Weißschuh
run_protection(int min,int max)1111c8d07815SThomas Weißschuh static int run_protection(int min __attribute__((unused)),
1112c8d07815SThomas Weißschuh int max __attribute__((unused)))
111397357168SThomas Weißschuh {
111497357168SThomas Weißschuh pid_t pid;
111597357168SThomas Weißschuh int llen = 0, status;
111697357168SThomas Weißschuh
111797357168SThomas Weißschuh llen += printf("0 -fstackprotector ");
111897357168SThomas Weißschuh
1119818924d1SThomas Weißschuh #if !defined(_NOLIBC_STACKPROTECTOR)
112097357168SThomas Weißschuh llen += printf("not supported");
1121b184a261SThomas Weißschuh result(llen, SKIPPED);
112297357168SThomas Weißschuh return 0;
112397357168SThomas Weißschuh #endif
112497357168SThomas Weißschuh
1125818924d1SThomas Weißschuh #if defined(_NOLIBC_STACKPROTECTOR)
112685250921SThomas Weißschuh if (!__stack_chk_guard) {
112785250921SThomas Weißschuh llen += printf("__stack_chk_guard not initialized");
1128b184a261SThomas Weißschuh result(llen, FAIL);
112985250921SThomas Weißschuh return 1;
113085250921SThomas Weißschuh }
113185250921SThomas Weißschuh #endif
113285250921SThomas Weißschuh
113397357168SThomas Weißschuh pid = -1;
113497357168SThomas Weißschuh pid = fork();
113597357168SThomas Weißschuh
113697357168SThomas Weißschuh switch (pid) {
113797357168SThomas Weißschuh case -1:
113897357168SThomas Weißschuh llen += printf("fork()");
1139b184a261SThomas Weißschuh result(llen, FAIL);
114097357168SThomas Weißschuh return 1;
114197357168SThomas Weißschuh
114297357168SThomas Weißschuh case 0:
114397357168SThomas Weißschuh close(STDOUT_FILENO);
114497357168SThomas Weißschuh close(STDERR_FILENO);
114597357168SThomas Weißschuh
11469a75575bSThomas Weißschuh prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
114797357168SThomas Weißschuh smash_stack();
114897357168SThomas Weißschuh return 1;
114997357168SThomas Weißschuh
115097357168SThomas Weißschuh default:
115197357168SThomas Weißschuh pid = waitpid(pid, &status, 0);
115297357168SThomas Weißschuh
115397357168SThomas Weißschuh if (pid == -1 || !WIFSIGNALED(status) || WTERMSIG(status) != SIGABRT) {
115497357168SThomas Weißschuh llen += printf("waitpid()");
1155b184a261SThomas Weißschuh result(llen, FAIL);
115697357168SThomas Weißschuh return 1;
115797357168SThomas Weißschuh }
1158b184a261SThomas Weißschuh result(llen, OK);
115997357168SThomas Weißschuh return 0;
116097357168SThomas Weißschuh }
116197357168SThomas Weißschuh }
116297357168SThomas Weißschuh
11631a5454f6SWilly Tarreau /* prepare what needs to be prepared for pid 1 (stdio, /dev, /proc, etc) */
prepare(void)11641a5454f6SWilly Tarreau int prepare(void)
11651a5454f6SWilly Tarreau {
11661a5454f6SWilly Tarreau struct stat stat_buf;
11671a5454f6SWilly Tarreau
11681a5454f6SWilly Tarreau /* It's possible that /dev doesn't even exist or was not mounted, so
11691a5454f6SWilly Tarreau * we'll try to create it, mount it, or create minimal entries into it.
11701a5454f6SWilly Tarreau * We want at least /dev/null and /dev/console.
11711a5454f6SWilly Tarreau */
11721a5454f6SWilly Tarreau if (stat("/dev/.", &stat_buf) == 0 || mkdir("/dev", 0755) == 0) {
11731a5454f6SWilly Tarreau if (stat("/dev/console", &stat_buf) != 0 ||
117482e339c2SZhangjin Wu stat("/dev/null", &stat_buf) != 0 ||
117582e339c2SZhangjin Wu stat("/dev/zero", &stat_buf) != 0) {
11761a5454f6SWilly Tarreau /* try devtmpfs first, otherwise fall back to manual creation */
11771a5454f6SWilly Tarreau if (mount("/dev", "/dev", "devtmpfs", 0, 0) != 0) {
11781a5454f6SWilly Tarreau mknod("/dev/console", 0600 | S_IFCHR, makedev(5, 1));
11791a5454f6SWilly Tarreau mknod("/dev/null", 0666 | S_IFCHR, makedev(1, 3));
118082e339c2SZhangjin Wu mknod("/dev/zero", 0666 | S_IFCHR, makedev(1, 5));
11811a5454f6SWilly Tarreau }
11821a5454f6SWilly Tarreau }
11831a5454f6SWilly Tarreau }
11841a5454f6SWilly Tarreau
11851a5454f6SWilly Tarreau /* If no /dev/console was found before calling init, stdio is closed so
11861a5454f6SWilly Tarreau * we need to reopen it from /dev/console. If it failed above, it will
11871a5454f6SWilly Tarreau * still fail here and we cannot emit a message anyway.
11881a5454f6SWilly Tarreau */
11891a5454f6SWilly Tarreau if (close(dup(1)) == -1) {
11901a5454f6SWilly Tarreau int fd = open("/dev/console", O_RDWR);
11911a5454f6SWilly Tarreau
11921a5454f6SWilly Tarreau if (fd >= 0) {
11931a5454f6SWilly Tarreau if (fd != 0)
11941a5454f6SWilly Tarreau dup2(fd, 0);
11951a5454f6SWilly Tarreau if (fd != 1)
11961a5454f6SWilly Tarreau dup2(fd, 1);
11971a5454f6SWilly Tarreau if (fd != 2)
11981a5454f6SWilly Tarreau dup2(fd, 2);
11991a5454f6SWilly Tarreau if (fd > 2)
12001a5454f6SWilly Tarreau close(fd);
12011a5454f6SWilly Tarreau puts("\nSuccessfully reopened /dev/console.");
12021a5454f6SWilly Tarreau }
12031a5454f6SWilly Tarreau }
12041a5454f6SWilly Tarreau
12051a5454f6SWilly Tarreau /* try to mount /proc if not mounted. Silently fail otherwise */
12061a5454f6SWilly Tarreau if (stat("/proc/.", &stat_buf) == 0 || mkdir("/proc", 0755) == 0) {
1207b8b26108SZhangjin Wu if (stat("/proc/self", &stat_buf) != 0) {
1208b8b26108SZhangjin Wu /* If not mountable, remove /proc completely to avoid misuse */
1209b8b26108SZhangjin Wu if (mount("none", "/proc", "proc", 0, 0) != 0)
1210b8b26108SZhangjin Wu rmdir("/proc");
12111a5454f6SWilly Tarreau }
12121a5454f6SWilly Tarreau }
12131a5454f6SWilly Tarreau
1214bbb14546SZhangjin Wu /* some tests rely on a writable /tmp */
1215bbb14546SZhangjin Wu mkdir("/tmp", 0755);
12161a5454f6SWilly Tarreau
12171a5454f6SWilly Tarreau return 0;
12181a5454f6SWilly Tarreau }
1219b4844fa0SWilly Tarreau
122023da7bc9SWilly Tarreau /* This is the definition of known test names, with their functions */
1221c4560bd8SThomas Weißschuh static const struct test test_names[] = {
122223da7bc9SWilly Tarreau /* add new tests here */
122348967b73SZhangjin Wu { .name = "startup", .func = run_startup },
1224b4844fa0SWilly Tarreau { .name = "syscall", .func = run_syscall },
122595bc9894SWilly Tarreau { .name = "stdlib", .func = run_stdlib },
122669f2cd9fSThomas Weißschuh { .name = "vfprintf", .func = run_vfprintf },
122797357168SThomas Weißschuh { .name = "protection", .func = run_protection },
122823da7bc9SWilly Tarreau { 0 }
122923da7bc9SWilly Tarreau };
123023da7bc9SWilly Tarreau
is_setting_valid(char * test)123117e66f23SThomas Weißschuh static int is_setting_valid(char *test)
1232c388c992SZhangjin Wu {
1233c388c992SZhangjin Wu int idx, len, test_len, valid = 0;
1234c388c992SZhangjin Wu char delimiter;
1235c388c992SZhangjin Wu
1236c388c992SZhangjin Wu if (!test)
1237c388c992SZhangjin Wu return valid;
1238c388c992SZhangjin Wu
1239c388c992SZhangjin Wu test_len = strlen(test);
1240c388c992SZhangjin Wu
1241c388c992SZhangjin Wu for (idx = 0; test_names[idx].name; idx++) {
1242c388c992SZhangjin Wu len = strlen(test_names[idx].name);
1243c388c992SZhangjin Wu if (test_len < len)
1244c388c992SZhangjin Wu continue;
1245c388c992SZhangjin Wu
1246c388c992SZhangjin Wu if (strncmp(test, test_names[idx].name, len) != 0)
1247c388c992SZhangjin Wu continue;
1248c388c992SZhangjin Wu
1249c388c992SZhangjin Wu delimiter = test[len];
1250c388c992SZhangjin Wu if (delimiter != ':' && delimiter != ',' && delimiter != '\0')
1251c388c992SZhangjin Wu continue;
1252c388c992SZhangjin Wu
1253c388c992SZhangjin Wu valid = 1;
1254c388c992SZhangjin Wu break;
1255c388c992SZhangjin Wu }
1256c388c992SZhangjin Wu
1257c388c992SZhangjin Wu return valid;
1258c388c992SZhangjin Wu }
1259c388c992SZhangjin Wu
main(int argc,char ** argv,char ** envp)1260362aecb2SWilly Tarreau int main(int argc, char **argv, char **envp)
1261362aecb2SWilly Tarreau {
1262362aecb2SWilly Tarreau int min = 0;
1263a36cfc5eSZhangjin Wu int max = INT_MAX;
1264362aecb2SWilly Tarreau int ret = 0;
126523da7bc9SWilly Tarreau int err;
126623da7bc9SWilly Tarreau int idx;
126723da7bc9SWilly Tarreau char *test;
1268362aecb2SWilly Tarreau
1269938b5b98SZhangjin Wu argv0 = argv[0];
127048967b73SZhangjin Wu test_argc = argc;
127148967b73SZhangjin Wu test_argv = argv;
127267d108e2SThomas Weißschuh test_envp = envp;
1273362aecb2SWilly Tarreau
12741a5454f6SWilly Tarreau /* when called as init, it's possible that no console was opened, for
12751a5454f6SWilly Tarreau * example if no /dev file system was provided. We'll check that fd#1
12761a5454f6SWilly Tarreau * was opened, and if not we'll attempt to create and open /dev/console
12771a5454f6SWilly Tarreau * and /dev/null that we'll use for later tests.
12781a5454f6SWilly Tarreau */
12791a5454f6SWilly Tarreau if (getpid() == 1)
12801a5454f6SWilly Tarreau prepare();
12811a5454f6SWilly Tarreau
128223da7bc9SWilly Tarreau /* the definition of a series of tests comes from either argv[1] or the
128323da7bc9SWilly Tarreau * "NOLIBC_TEST" environment variable. It's made of a comma-delimited
128423da7bc9SWilly Tarreau * series of test names and optional ranges:
128523da7bc9SWilly Tarreau * syscall:5-15[:.*],stdlib:8-10
128623da7bc9SWilly Tarreau */
128723da7bc9SWilly Tarreau test = argv[1];
1288c388c992SZhangjin Wu if (!is_setting_valid(test))
128923da7bc9SWilly Tarreau test = getenv("NOLIBC_TEST");
129023da7bc9SWilly Tarreau
1291c388c992SZhangjin Wu if (is_setting_valid(test)) {
129223da7bc9SWilly Tarreau char *comma, *colon, *dash, *value;
129323da7bc9SWilly Tarreau
129423da7bc9SWilly Tarreau do {
129523da7bc9SWilly Tarreau comma = strchr(test, ',');
129623da7bc9SWilly Tarreau if (comma)
129723da7bc9SWilly Tarreau *(comma++) = '\0';
129823da7bc9SWilly Tarreau
129923da7bc9SWilly Tarreau colon = strchr(test, ':');
130023da7bc9SWilly Tarreau if (colon)
130123da7bc9SWilly Tarreau *(colon++) = '\0';
130223da7bc9SWilly Tarreau
130323da7bc9SWilly Tarreau for (idx = 0; test_names[idx].name; idx++) {
130423da7bc9SWilly Tarreau if (strcmp(test, test_names[idx].name) == 0)
130523da7bc9SWilly Tarreau break;
130623da7bc9SWilly Tarreau }
130723da7bc9SWilly Tarreau
130823da7bc9SWilly Tarreau if (test_names[idx].name) {
130923da7bc9SWilly Tarreau /* The test was named, it will be called at least
131023da7bc9SWilly Tarreau * once. We may have an optional range at <colon>
131123da7bc9SWilly Tarreau * here, which defaults to the full range.
131223da7bc9SWilly Tarreau */
131323da7bc9SWilly Tarreau do {
1314a36cfc5eSZhangjin Wu min = 0; max = INT_MAX;
131523da7bc9SWilly Tarreau value = colon;
131623da7bc9SWilly Tarreau if (value && *value) {
131723da7bc9SWilly Tarreau colon = strchr(value, ':');
131823da7bc9SWilly Tarreau if (colon)
131923da7bc9SWilly Tarreau *(colon++) = '\0';
132023da7bc9SWilly Tarreau
132123da7bc9SWilly Tarreau dash = strchr(value, '-');
132223da7bc9SWilly Tarreau if (dash)
132323da7bc9SWilly Tarreau *(dash++) = '\0';
132423da7bc9SWilly Tarreau
132523da7bc9SWilly Tarreau /* support :val: :min-max: :min-: :-max: */
132623da7bc9SWilly Tarreau if (*value)
132723da7bc9SWilly Tarreau min = atoi(value);
132823da7bc9SWilly Tarreau if (!dash)
132923da7bc9SWilly Tarreau max = min;
133023da7bc9SWilly Tarreau else if (*dash)
133123da7bc9SWilly Tarreau max = atoi(dash);
133223da7bc9SWilly Tarreau
133323da7bc9SWilly Tarreau value = colon;
133423da7bc9SWilly Tarreau }
133523da7bc9SWilly Tarreau
133623da7bc9SWilly Tarreau /* now's time to call the test */
133723da7bc9SWilly Tarreau printf("Running test '%s'\n", test_names[idx].name);
133823da7bc9SWilly Tarreau err = test_names[idx].func(min, max);
133923da7bc9SWilly Tarreau ret += err;
134023da7bc9SWilly Tarreau printf("Errors during this test: %d\n\n", err);
134123da7bc9SWilly Tarreau } while (colon && *colon);
134223da7bc9SWilly Tarreau } else
134323da7bc9SWilly Tarreau printf("Ignoring unknown test name '%s'\n", test);
134423da7bc9SWilly Tarreau
134523da7bc9SWilly Tarreau test = comma;
134623da7bc9SWilly Tarreau } while (test && *test);
134723da7bc9SWilly Tarreau } else {
134823da7bc9SWilly Tarreau /* no test mentioned, run everything */
134923da7bc9SWilly Tarreau for (idx = 0; test_names[idx].name; idx++) {
135023da7bc9SWilly Tarreau printf("Running test '%s'\n", test_names[idx].name);
135123da7bc9SWilly Tarreau err = test_names[idx].func(min, max);
135223da7bc9SWilly Tarreau ret += err;
135323da7bc9SWilly Tarreau printf("Errors during this test: %d\n\n", err);
135423da7bc9SWilly Tarreau }
135523da7bc9SWilly Tarreau }
135623da7bc9SWilly Tarreau
1357362aecb2SWilly Tarreau printf("Total number of errors: %d\n", ret);
1358f49896d7SWilly Tarreau
1359f49896d7SWilly Tarreau if (getpid() == 1) {
1360f49896d7SWilly Tarreau /* we're running as init, there's no other process on the
1361f49896d7SWilly Tarreau * system, thus likely started from a VM for a quick check.
1362f49896d7SWilly Tarreau * Exiting will provoke a kernel panic that may be reported
1363f49896d7SWilly Tarreau * as an error by Qemu or the hypervisor, while stopping
1364f49896d7SWilly Tarreau * cleanly will often be reported as a success. This allows
1365f49896d7SWilly Tarreau * to use the output of this program for bisecting kernels.
1366f49896d7SWilly Tarreau */
1367f49896d7SWilly Tarreau printf("Leaving init with final status: %d\n", !!ret);
1368f49896d7SWilly Tarreau if (ret == 0)
1369950add28SZhangjin Wu reboot(RB_POWER_OFF);
1370aa73a86cSWilly Tarreau #if defined(__x86_64__)
1371aa73a86cSWilly Tarreau /* QEMU started with "-device isa-debug-exit -no-reboot" will
1372aa73a86cSWilly Tarreau * exit with status code 2N+1 when N is written to 0x501. We
1373aa73a86cSWilly Tarreau * hard-code the syscall here as it's arch-dependent.
1374aa73a86cSWilly Tarreau */
137567eb617aSThomas Weißschuh else if (syscall(__NR_ioperm, 0x501, 1, 1) == 0)
13767f291cfaSThomas Weißschuh __asm__ volatile ("outb %%al, %%dx" :: "d"(0x501), "a"(0));
1377aa73a86cSWilly Tarreau /* if it does nothing, fall back to the regular panic */
1378aa73a86cSWilly Tarreau #endif
1379f49896d7SWilly Tarreau }
1380f49896d7SWilly Tarreau
1381362aecb2SWilly Tarreau printf("Exiting with status %d\n", !!ret);
1382362aecb2SWilly Tarreau return !!ret;
1383362aecb2SWilly Tarreau }
1384