Home | History | Annotate | Line # | Download | only in sys
t_ptrace_wait.h revision 1.23
      1 /*	$NetBSD: t_ptrace_wait.h,v 1.23 2020/03/03 17:09:22 kamil Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2016, 2017, 2018, 2019 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /* Detect plain wait(2) use-case */
     30 #if !defined(TWAIT_WAITPID) && \
     31     !defined(TWAIT_WAITID) && \
     32     !defined(TWAIT_WAIT3) && \
     33     !defined(TWAIT_WAIT4) && \
     34     !defined(TWAIT_WAIT6)
     35 #define TWAIT_WAIT
     36 #endif
     37 
     38 /*
     39  * There are two classes of wait(2)-like functions:
     40  * - wait4(2)-like accepting pid_t, optional options parameter, struct rusage*
     41  * - wait6(2)-like accepting idtype_t, id_t, struct wrusage, mandatory options
     42  *
     43  * The TWAIT_FNAME value is to be used for convenience in debug messages.
     44  *
     45  * The TWAIT_GENERIC() macro is designed to reuse the same unmodified
     46  * code with as many wait(2)-like functions as possible.
     47  *
     48  * In a common use-case wait4(2) and wait6(2)-like function can work the almost
     49  * the same way, however there are few important differences:
     50  * wait6(2) must specify P_PID for idtype to match wpid from wait4(2).
     51  * To behave like wait4(2), wait6(2) the 'options' to wait must include
     52  * WEXITED|WTRUNCATED.
     53  *
     54  * There are two helper macros (they purpose it to mach more than one
     55  * wait(2)-like function):
     56  * The TWAIT_HAVE_STATUS - specifies whether a function can retrieve
     57  *                         status (as integer value).
     58  * The TWAIT_HAVE_PID    - specifies whether a function can request
     59  *                         exact process identifier
     60  * The TWAIT_HAVE_RUSAGE - specifies whether a function can request
     61  *                         the struct rusage value
     62  *
     63  */
     64 
     65 #if defined(TWAIT_WAIT)
     66 #	define TWAIT_FNAME			"wait"
     67 #	define TWAIT_WAIT4TYPE(a,b,c,d)		wait((b))
     68 #	define TWAIT_GENERIC(a,b,c)		wait((b))
     69 #	define TWAIT_HAVE_STATUS		1
     70 #elif defined(TWAIT_WAITPID)
     71 #	define TWAIT_FNAME			"waitpid"
     72 #	define TWAIT_WAIT4TYPE(a,b,c,d)		waitpid((a),(b),(c))
     73 #	define TWAIT_GENERIC(a,b,c)		waitpid((a),(b),(c))
     74 #	define TWAIT_HAVE_PID			1
     75 #	define TWAIT_HAVE_STATUS		1
     76 #	define TWAIT_HAVE_OPTIONS		1
     77 #elif defined(TWAIT_WAITID)
     78 #	define TWAIT_FNAME			"waitid"
     79 #	define TWAIT_GENERIC(a,b,c)		\
     80 		waitid(P_PID,(a),NULL,(c)|WEXITED|WTRAPPED)
     81 #	define TWAIT_WAIT6TYPE(a,b,c,d,e,f)	waitid((a),(b),(f),(d))
     82 #	define TWAIT_HAVE_PID			1
     83 #	define TWAIT_HAVE_OPTIONS		1
     84 #elif defined(TWAIT_WAIT3)
     85 #	define TWAIT_FNAME			"wait3"
     86 #	define TWAIT_WAIT4TYPE(a,b,c,d)		wait3((b),(c),(d))
     87 #	define TWAIT_GENERIC(a,b,c)		wait3((b),(c),NULL)
     88 #	define TWAIT_HAVE_STATUS		1
     89 #	define TWAIT_HAVE_RUSAGE		1
     90 #	define TWAIT_HAVE_OPTIONS		1
     91 #elif defined(TWAIT_WAIT4)
     92 #	define TWAIT_FNAME			"wait4"
     93 #	define TWAIT_WAIT4TYPE(a,b,c,d)		wait4((a),(b),(c),(d))
     94 #	define TWAIT_GENERIC(a,b,c)		wait4((a),(b),(c),NULL)
     95 #	define TWAIT_HAVE_PID			1
     96 #	define TWAIT_HAVE_STATUS		1
     97 #	define TWAIT_HAVE_RUSAGE		1
     98 #	define TWAIT_HAVE_OPTIONS		1
     99 #elif defined(TWAIT_WAIT6)
    100 #	define TWAIT_FNAME			"wait6"
    101 #	define TWAIT_WAIT6TYPE(a,b,c,d,e,f)	wait6((a),(b),(c),(d),(e),(f))
    102 #	define TWAIT_GENERIC(a,b,c)		\
    103 		wait6(P_PID,(a),(b),(c)|WEXITED|WTRAPPED,NULL,NULL)
    104 #	define TWAIT_HAVE_PID			1
    105 #	define TWAIT_HAVE_STATUS		1
    106 #	define TWAIT_HAVE_OPTIONS		1
    107 #endif
    108 
    109 /*
    110  * There are 3 groups of tests:
    111  * - TWAIT_GENERIC()	(wait, wait2, waitpid, wait3, wait4, wait6)
    112  * - TWAIT_WAIT4TYPE()	(wait2, waitpid, wait3, wait4)
    113  * - TWAIT_WAIT6TYPE()	(waitid, wait6)
    114  *
    115  * Tests only in the above categories are allowed. However some tests are not
    116  * possible in the context requested functionality to be verified, therefore
    117  * there are helper macros:
    118  * - TWAIT_HAVE_PID	(wait2, waitpid, waitid, wait4, wait6)
    119  * - TWAIT_HAVE_STATUS	(wait, wait2, waitpid, wait3, wait4, wait6)
    120  * - TWAIT_HAVE_RUSAGE	(wait3, wait4)
    121  * - TWAIT_HAVE_RETPID	(wait, wait2, waitpid, wait3, wait4, wait6)
    122  *
    123  * If there is an intention to test e.g. wait6(2) specific features in the
    124  * ptrace(2) context, find the most matching group and with #ifdefs reduce
    125  * functionality of less featured than wait6(2) interface (TWAIT_WAIT6TYPE).
    126  *
    127  * For clarity never use negative preprocessor checks, like:
    128  *     #if !defined(TWAIT_WAIT4)
    129  * always refer to checks for positive values.
    130  */
    131 
    132 #define TEST_REQUIRE_EQ(x, y)						\
    133 do {									\
    134 	uintmax_t vx = (x);						\
    135 	uintmax_t vy = (y);						\
    136 	int ret = vx == vy;						\
    137 	if (!ret)							\
    138 		ATF_REQUIRE_EQ_MSG(vx, vy, "%s(%ju) == %s(%ju)", 	\
    139 		    #x, vx, #y, vy);					\
    140 } while (/*CONSTCOND*/0)
    141 
    142 /*
    143  * A child process cannot call atf functions and expect them to magically
    144  * work like in the parent.
    145  * The printf(3) messaging from a child will not work out of the box as well
    146  * without estabilishing a communication protocol with its parent. To not
    147  * overcomplicate the tests - do not log from a child and use err(3)/errx(3)
    148  * wrapped with FORKEE_ASSERT()/FORKEE_ASSERTX() as that is guaranteed to work.
    149  */
    150 #define FORKEE_ASSERT_EQ(x, y)						\
    151 do {									\
    152 	uintmax_t vx = (x);						\
    153 	uintmax_t vy = (y);						\
    154 	int ret = vx == vy;						\
    155 	if (!ret)							\
    156 		errx(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: "	\
    157 		    "%s(%ju) == %s(%ju)", __FILE__, __LINE__, __func__,	\
    158 		    #x, vx, #y, vy);					\
    159 } while (/*CONSTCOND*/0)
    160 
    161 #define FORKEE_ASSERT_NEQ(x, y)						\
    162 do {									\
    163 	uintmax_t vx = (x);						\
    164 	uintmax_t vy = (y);						\
    165 	int ret = vx != vy;						\
    166 	if (!ret)							\
    167 		errx(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: "	\
    168 		    "%s(%ju) != %s(%ju)", __FILE__, __LINE__, __func__,	\
    169 		    #x, vx, #y, vy);					\
    170 } while (/*CONSTCOND*/0)
    171 
    172 #define FORKEE_ASSERTX(x)						\
    173 do {									\
    174 	int ret = (x);							\
    175 	if (!ret)							\
    176 		errx(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: %s",\
    177 		     __FILE__, __LINE__, __func__, #x);			\
    178 } while (/*CONSTCOND*/0)
    179 
    180 #define FORKEE_ASSERT(x)						\
    181 do {									\
    182 	int ret = (x);							\
    183 	if (!ret)							\
    184 		err(EXIT_FAILURE, "%s:%d %s(): Assertion failed for: %s",\
    185 		     __FILE__, __LINE__, __func__, #x);			\
    186 } while (/*CONSTCOND*/0)
    187 
    188 /*
    189  * Simplify logic for functions using general purpose registers add HAVE_GPREGS
    190  *
    191  * For platforms that do not implement all needed calls for simplicity assume
    192  * that they are unsupported at all.
    193  */
    194 #if defined(PT_GETREGS)			\
    195     && defined(PT_SETREGS)		\
    196     && defined(PTRACE_REG_PC)		\
    197     && defined(PTRACE_REG_SET_PC)	\
    198     && defined(PTRACE_REG_SP)		\
    199     && defined(PTRACE_REG_INTRV)
    200 #define HAVE_GPREGS
    201 #endif
    202 
    203 /* Add guards for floating point registers */
    204 #if defined(PT_GETFPREGS)		\
    205     && defined(PT_SETFPREGS)
    206 #define HAVE_FPREGS
    207 #endif
    208 
    209 /* Add guards for cpu debug registers */
    210 #if defined(PT_GETDBREGS)		\
    211     && defined(PT_SETDBREGS)
    212 #define HAVE_DBREGS
    213 #endif
    214 
    215 /*
    216  * If waitid(2) returns because one or more processes have a state change to
    217  * report, 0 is returned.  If an error is detected, a value of -1 is returned
    218  * and errno is set to indicate the error. If WNOHANG is specified and there
    219  * are no stopped, continued or exited children, 0 is returned.
    220  */
    221 #if defined(TWAIT_WAITID)
    222 #define TWAIT_REQUIRE_SUCCESS(a,b)	TEST_REQUIRE_EQ((a), 0)
    223 #define TWAIT_REQUIRE_FAILURE(a,b)	ATF_REQUIRE_ERRNO((a),(b) == -1)
    224 #define FORKEE_REQUIRE_SUCCESS(a,b)	FORKEE_ASSERT_EQ(a, 0)
    225 #define FORKEE_REQUIRE_FAILURE(a,b)	\
    226 	FORKEE_ASSERTX(((a) == errno) && ((b) == -1))
    227 #else
    228 #define TWAIT_REQUIRE_SUCCESS(a,b)	TEST_REQUIRE_EQ((a), (b))
    229 #define TWAIT_REQUIRE_FAILURE(a,b)	ATF_REQUIRE_ERRNO((a),(b) == -1)
    230 #define FORKEE_REQUIRE_SUCCESS(a,b)	FORKEE_ASSERT_EQ(a, b)
    231 #define FORKEE_REQUIRE_FAILURE(a,b)	\
    232 	FORKEE_ASSERTX(((a) == errno) && ((b) == -1))
    233 #endif
    234 
    235 /*
    236  * Helper tools to verify whether status reports exited value
    237  */
    238 #if TWAIT_HAVE_STATUS
    239 static void __used
    240 validate_status_exited(int status, int expected)
    241 {
    242         ATF_REQUIRE_MSG(WIFEXITED(status), "Reported !exited process");
    243         ATF_REQUIRE_MSG(!WIFCONTINUED(status), "Reported continued process");
    244         ATF_REQUIRE_MSG(!WIFSIGNALED(status), "Reported signaled process");
    245         ATF_REQUIRE_MSG(!WIFSTOPPED(status), "Reported stopped process");
    246 
    247 	ATF_REQUIRE_EQ_MSG(WEXITSTATUS(status), expected,
    248 	    "The process has exited with invalid value %d != %d",
    249 	    WEXITSTATUS(status), expected);
    250 }
    251 
    252 static void __used
    253 forkee_status_exited(int status, int expected)
    254 {
    255 	FORKEE_ASSERTX(WIFEXITED(status));
    256 	FORKEE_ASSERTX(!WIFCONTINUED(status));
    257 	FORKEE_ASSERTX(!WIFSIGNALED(status));
    258 	FORKEE_ASSERTX(!WIFSTOPPED(status));
    259 
    260 	FORKEE_ASSERT_EQ(WEXITSTATUS(status), expected);
    261 }
    262 
    263 static void __used
    264 validate_status_continued(int status)
    265 {
    266 	ATF_REQUIRE_MSG(!WIFEXITED(status), "Reported exited process");
    267 	ATF_REQUIRE_MSG(WIFCONTINUED(status), "Reported !continued process");
    268 	ATF_REQUIRE_MSG(!WIFSIGNALED(status), "Reported signaled process");
    269 	ATF_REQUIRE_MSG(!WIFSTOPPED(status), "Reported stopped process");
    270 }
    271 
    272 static void __used
    273 forkee_status_continued(int status)
    274 {
    275 	FORKEE_ASSERTX(!WIFEXITED(status));
    276 	FORKEE_ASSERTX(WIFCONTINUED(status));
    277 	FORKEE_ASSERTX(!WIFSIGNALED(status));
    278 	FORKEE_ASSERTX(!WIFSTOPPED(status));
    279 }
    280 
    281 static void __used
    282 validate_status_signaled(int status, int expected_termsig, int expected_core)
    283 {
    284 	ATF_REQUIRE_MSG(!WIFEXITED(status), "Reported exited process");
    285 	ATF_REQUIRE_MSG(!WIFCONTINUED(status), "Reported continued process");
    286 	ATF_REQUIRE_MSG(WIFSIGNALED(status), "Reported !signaled process");
    287 	ATF_REQUIRE_MSG(!WIFSTOPPED(status), "Reported stopped process");
    288 
    289 	ATF_REQUIRE_EQ_MSG(WTERMSIG(status), expected_termsig,
    290 	    "Unexpected signal received");
    291 
    292 	ATF_REQUIRE_EQ_MSG(!!WCOREDUMP(status), expected_core,
    293 	    "Unexpectedly core file %s generated", expected_core ? "not" : "");
    294 }
    295 
    296 static void __used
    297 forkee_status_signaled(int status, int expected_termsig, int expected_core)
    298 {
    299 	FORKEE_ASSERTX(!WIFEXITED(status));
    300 	FORKEE_ASSERTX(!WIFCONTINUED(status));
    301 	FORKEE_ASSERTX(WIFSIGNALED(status));
    302 	FORKEE_ASSERTX(!WIFSTOPPED(status));
    303 
    304 	FORKEE_ASSERT_EQ(WTERMSIG(status), expected_termsig);
    305 	FORKEE_ASSERT_EQ(!!WCOREDUMP(status), expected_core);
    306 }
    307 
    308 static void __used
    309 validate_status_stopped(int status, int expected)
    310 {
    311 	ATF_REQUIRE_MSG(!WIFEXITED(status), "Reported exited process");
    312 	ATF_REQUIRE_MSG(!WIFCONTINUED(status), "Reported continued process");
    313 	ATF_REQUIRE_MSG(!WIFSIGNALED(status), "Reported signaled process");
    314 	ATF_REQUIRE_MSG(WIFSTOPPED(status), "Reported !stopped process");
    315 
    316 	char st[128], ex[128];
    317 	strlcpy(st, strsignal(WSTOPSIG(status)), sizeof(st));
    318 	strlcpy(ex, strsignal(expected), sizeof(ex));
    319 
    320 	ATF_REQUIRE_EQ_MSG(WSTOPSIG(status), expected,
    321 	    "Unexpected stop signal received [%s] != [%s]", st, ex);
    322 }
    323 
    324 static void __used
    325 forkee_status_stopped(int status, int expected)
    326 {
    327 	FORKEE_ASSERTX(!WIFEXITED(status));
    328 	FORKEE_ASSERTX(!WIFCONTINUED(status));
    329 	FORKEE_ASSERTX(!WIFSIGNALED(status));
    330 	FORKEE_ASSERTX(WIFSTOPPED(status));
    331 
    332 	FORKEE_ASSERT_EQ(WSTOPSIG(status), expected);
    333 }
    334 #else
    335 #define validate_status_exited(a,b)
    336 #define forkee_status_exited(a,b)
    337 #define validate_status_continued(a,b)
    338 #define forkee_status_continued(a,b)
    339 #define validate_status_signaled(a,b,c)
    340 #define forkee_status_signaled(a,b,c)
    341 #define validate_status_stopped(a,b)
    342 #define forkee_status_stopped(a,b)
    343 #endif
    344 
    345 /* This function is currently designed to be run in the main/parent process */
    346 static void __used
    347 await_zombie_raw(pid_t process, useconds_t ms)
    348 {
    349 	struct kinfo_proc2 p;
    350 	size_t len = sizeof(p);
    351 
    352 	const int name[] = {
    353 		[0] = CTL_KERN,
    354 		[1] = KERN_PROC2,
    355 		[2] = KERN_PROC_PID,
    356 		[3] = process,
    357 		[4] = sizeof(p),
    358 		[5] = 1
    359 	};
    360 
    361 	const size_t namelen = __arraycount(name);
    362 
    363 	/* Await the process becoming a zombie */
    364 	while(1) {
    365 		ATF_REQUIRE(sysctl(name, namelen, &p, &len, NULL, 0) == 0);
    366 
    367 		if (p.p_stat == LSZOMB)
    368 			break;
    369 
    370 		if (ms > 0) {
    371 			ATF_REQUIRE(usleep(ms) == 0);
    372 		}
    373 	}
    374 }
    375 
    376 static void __used
    377 await_zombie(pid_t process)
    378 {
    379 
    380 	await_zombie_raw(process, 1000);
    381 }
    382 
    383 static void __used
    384 await_stopped(pid_t process)
    385 {
    386 	struct kinfo_proc2 p;
    387 	size_t len = sizeof(p);
    388 
    389 	const int name[] = {
    390 		[0] = CTL_KERN,
    391 		[1] = KERN_PROC2,
    392 		[2] = KERN_PROC_PID,
    393 		[3] = process,
    394 		[4] = sizeof(p),
    395 		[5] = 1
    396 	};
    397 
    398 	const size_t namelen = __arraycount(name);
    399 
    400 	/* Await the process becoming a zombie */
    401 	while(1) {
    402 		ATF_REQUIRE(sysctl(name, namelen, &p, &len, NULL, 0) == 0);
    403 
    404 		if (p.p_stat == LSSTOP)
    405 			break;
    406 
    407 		ATF_REQUIRE(usleep(1000) == 0);
    408 	}
    409 }
    410 
    411 static pid_t __used
    412 await_stopped_child(pid_t process)
    413 {
    414 	struct kinfo_proc2 *p = NULL;
    415 	size_t i, len;
    416 	pid_t child = -1;
    417 
    418 	int name[] = {
    419 		[0] = CTL_KERN,
    420 		[1] = KERN_PROC2,
    421 		[2] = KERN_PROC_ALL,
    422 		[3] = 0,
    423 		[4] = sizeof(struct kinfo_proc2),
    424 		[5] = 0
    425 	};
    426 
    427 	const size_t namelen = __arraycount(name);
    428 
    429 	/* Await the process becoming a zombie */
    430 	while(1) {
    431 		name[5] = 0;
    432 
    433 		FORKEE_ASSERT_EQ(sysctl(name, namelen, 0, &len, NULL, 0), 0);
    434 
    435 		FORKEE_ASSERT_EQ(reallocarr(&p,
    436 		                            len,
    437 		                            sizeof(struct kinfo_proc2)), 0);
    438 
    439 		name[5] = len;
    440 
    441 		FORKEE_ASSERT_EQ(sysctl(name, namelen, p, &len, NULL, 0), 0);
    442 
    443 		for (i = 0; i < len/sizeof(struct kinfo_proc2); i++) {
    444 			if (p[i].p_pid == getpid())
    445 				continue;
    446 			if (p[i].p_ppid != process)
    447 				continue;
    448 			if (p[i].p_stat != LSSTOP)
    449 				continue;
    450 			child = p[i].p_pid;
    451 			break;
    452 		}
    453 
    454 		if (child != -1)
    455 			break;
    456 
    457 		FORKEE_ASSERT_EQ(usleep(1000), 0);
    458 	}
    459 
    460 	/* Free the buffer */
    461 	FORKEE_ASSERT_EQ(reallocarr(&p, 0, sizeof(struct kinfo_proc2)), 0);
    462 
    463 	return child;
    464 }
    465 
    466 /* Happy number sequence -- this function is used to just consume cpu cycles */
    467 #define	HAPPY_NUMBER	1
    468 
    469 /* If n is not happy then its sequence ends in the cycle:
    470  * 4, 16, 37, 58, 89, 145, 42, 20, 4, ... */
    471 #define	SAD_NUMBER	4
    472 
    473 /* Calculate the sum of the squares of the digits of n */
    474 static unsigned __used
    475 dsum(unsigned n)
    476 {
    477 	unsigned sum, x;
    478 	for (sum = 0; n; n /= 10) {
    479 		x = n % 10;
    480 		sum += x * x;
    481 	}
    482 	return sum;
    483 }
    484 
    485 /*
    486  * XXX: Disabled optimization is required to make tests for hardware assisted
    487  * traps in .text functional
    488  *
    489  * Tested with GCC 5.4 on NetBSD 7.99.47 amd64
    490  */
    491 static int __used
    492 #ifdef __clang__
    493 __attribute__((__optnone__))
    494 #else
    495 __attribute__((__optimize__("O0")))
    496 #endif
    497 check_happy(unsigned n)
    498 {
    499 	for (;;) {
    500 		unsigned total = dsum(n);
    501 
    502 		if (total == HAPPY_NUMBER)
    503 			return 1;
    504 		if (total == SAD_NUMBER)
    505 			return 0;
    506 
    507 		n = total;
    508 	}
    509 }
    510 
    511 static void * __used
    512 infinite_thread(void *arg __unused)
    513 {
    514 
    515         while (true)
    516                 continue;
    517 
    518         __unreachable();
    519 }
    520 
    521 static int __used
    522 clone_func(void *arg)
    523 {
    524 	int ret;
    525 
    526 	ret = (int)(intptr_t)arg;
    527 
    528 	return ret;
    529 }
    530 
    531 #if defined(HAVE_DBREGS)
    532 static bool __used
    533 can_we_set_dbregs(void)
    534 {
    535 	static long euid = -1;
    536 	static int user_set_dbregs  = -1;
    537 	size_t user_set_dbregs_len = sizeof(user_set_dbregs);
    538 
    539 	if (euid == -1)
    540 		euid = geteuid();
    541 
    542 	if (euid == 0)
    543 		return true;
    544 
    545 	if (user_set_dbregs == -1) {
    546 		if (sysctlbyname("security.models.extensions.user_set_dbregs",
    547 			&user_set_dbregs, &user_set_dbregs_len, NULL, 0)
    548 			== -1) {
    549 			return false;
    550 		}
    551 	}
    552 
    553 	if (user_set_dbregs > 0)
    554 		return true;
    555 	else
    556 		return false;
    557 }
    558 #endif
    559 
    560 static bool __used
    561 get_user_va0_disable(void)
    562 {
    563 	static int user_va0_disable = -1;
    564 	size_t user_va0_disable_len = sizeof(user_va0_disable);
    565 
    566 	if (user_va0_disable == -1) {
    567 		if (sysctlbyname("vm.user_va0_disable",
    568 			&user_va0_disable, &user_va0_disable_len, NULL, 0)
    569 			== -1) {
    570 			return true;
    571 		}
    572 	}
    573 
    574 	if (user_va0_disable > 0)
    575 		return true;
    576 	else
    577 		return false;
    578 }
    579 
    580 static bool __used
    581 can_we_write_to_text(pid_t pid)
    582 {
    583 	int mib[3];
    584 	int paxflags;
    585 	size_t len = sizeof(int);
    586 
    587 	mib[0] = CTL_PROC;
    588 	mib[1] = pid;
    589 	mib[2] = PROC_PID_PAXFLAGS;
    590 
    591 	if (sysctl(mib, 3, &paxflags, &len, NULL, 0) == -1)
    592 		return false;
    593 
    594 	return !(paxflags & CTL_PROC_PAXFLAGS_MPROTECT);
    595 }
    596 
    597 static void __used
    598 trigger_trap(void)
    599 {
    600 
    601 	/* Software breakpoint causes CPU trap, translated to SIGTRAP */
    602 #ifdef PTRACE_BREAKPOINT_ASM
    603 	PTRACE_BREAKPOINT_ASM;
    604 #else
    605 	/* port me */
    606 #endif
    607 }
    608 
    609 static void __used
    610 trigger_segv(void)
    611 {
    612 	static volatile char *ptr = NULL;
    613 
    614 	/* Access to unmapped memory causes CPU trap, translated to SIGSEGV */
    615 	*ptr = 1;
    616 }
    617 
    618 static void __used
    619 trigger_ill(void)
    620 {
    621 
    622 	/* Illegal instruction causes CPU trap, translated to SIGILL */
    623 #ifdef PTRACE_ILLEGAL_ASM
    624 	PTRACE_ILLEGAL_ASM;
    625 #else
    626 	/* port me */
    627 #endif
    628 }
    629 
    630 static bool __used
    631 are_fpu_exceptions_supported(void)
    632 {
    633 #if (__arm__ && !__SOFTFP__) || __aarch64__
    634 	/*
    635 	 * Some NEON fpus do not trap on IEEE 754 FP exceptions.
    636 	 * Skip these tests if running on them and compiled for
    637 	 * hard float.
    638 	 */
    639 	if (0 == fpsetmask(fpsetmask(FP_X_INV)))
    640 		return false;
    641 #endif
    642 	return true;
    643 }
    644 
    645 static void __used
    646 trigger_fpe(void)
    647 {
    648 	volatile int a = getpid();
    649 	volatile int b = atoi("0");
    650 
    651 #ifdef __HAVE_FENV
    652 	feenableexcept(FE_ALL_EXCEPT);
    653 #endif
    654 
    655 	/* Division by zero causes CPU trap, translated to SIGFPE */
    656 	usleep(a / b);
    657 }
    658 
    659 static void __used
    660 trigger_bus(void)
    661 {
    662 	FILE *fp;
    663 	char *p;
    664 
    665 	/* Open an empty file for writing. */
    666 	fp = tmpfile();
    667 	FORKEE_ASSERT_NEQ((uintptr_t)fp, (uintptr_t)NULL);
    668 
    669 	/*
    670 	 * Map an empty file with mmap(2) to a pointer.
    671 	 *
    672 	 * PROT_READ handles read-modify-write sequences emitted for
    673 	 * certain combinations of CPUs and compilers (e.g. Alpha AXP).
    674 	 */
    675 	p = mmap(0, 1, PROT_READ|PROT_WRITE, MAP_PRIVATE, fileno(fp), 0);
    676 	FORKEE_ASSERT_NEQ((uintptr_t)p, (uintptr_t)MAP_FAILED);
    677 
    678 	/* Invalid memory access causes CPU trap, translated to SIGBUS */
    679 	*p = 'a';
    680 }
    681 
    682 struct lwp_event_count {
    683 	lwpid_t lec_lwp;
    684 	int lec_count;
    685 };
    686 
    687 static int * __used
    688 find_event_count(struct lwp_event_count list[], lwpid_t lwp, size_t max_lwps)
    689 {
    690 	size_t i;
    691 
    692 	for (i = 0; i < max_lwps; i++) {
    693 		if (list[i].lec_lwp == 0)
    694 			list[i].lec_lwp = lwp;
    695 		if (list[i].lec_lwp == lwp)
    696 			return &list[i].lec_count;
    697 	}
    698 
    699 	atf_tc_fail("More LWPs reported than expected");
    700 }
    701 
    702 #define FIND_EVENT_COUNT(list, lwp)			\
    703 	find_event_count(list, lwp, __arraycount(list))
    704 
    705 
    706 #if defined(TWAIT_HAVE_PID)
    707 #define ATF_TP_ADD_TC_HAVE_PID(a,b)	ATF_TP_ADD_TC(a,b)
    708 #else
    709 #define ATF_TP_ADD_TC_HAVE_PID(a,b)
    710 #endif
    711 
    712 #if defined(HAVE_GPREGS)
    713 #define ATF_TP_ADD_TC_HAVE_GPREGS(a,b)	ATF_TP_ADD_TC(a,b)
    714 #else
    715 #define ATF_TP_ADD_TC_HAVE_GPREGS(a,b)
    716 #endif
    717 
    718 #if defined(HAVE_FPREGS)
    719 #define ATF_TP_ADD_TC_HAVE_FPREGS(a,b)	ATF_TP_ADD_TC(a,b)
    720 #else
    721 #define ATF_TP_ADD_TC_HAVE_FPREGS(a,b)
    722 #endif
    723 
    724 #if defined(HAVE_DBREGS)
    725 #define ATF_TP_ADD_TC_HAVE_DBREGS(a,b) ATF_TP_ADD_TC(a,b)
    726 #else
    727 #define ATF_TP_ADD_TC_HAVE_DBREGS(a,b)
    728 #endif
    729 
    730 #if defined(PT_STEP)
    731 #define ATF_TP_ADD_TC_PT_STEP(a,b)	ATF_TP_ADD_TC(a,b)
    732 #else
    733 #define ATF_TP_ADD_TC_PT_STEP(a,b)
    734 #endif
    735