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