Home | History | Annotate | Line # | Download | only in sys
t_ptrace_register_wait.h revision 1.1
      1 /*      $NetBSD: t_ptrace_register_wait.h,v 1.1 2020/05/04 20:55:48 kamil Exp $   */
      2 
      3 /*-
      4  * Copyright (c) 2016, 2017, 2018, 2019, 2020 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 
     30 #if defined(HAVE_GPREGS) || defined(HAVE_FPREGS)
     31 static void
     32 access_regs(const char *regset, const char *aux)
     33 {
     34 	const int exitval = 5;
     35 	const int sigval = SIGSTOP;
     36 	pid_t child, wpid;
     37 #if defined(TWAIT_HAVE_STATUS)
     38 	int status;
     39 #endif
     40 #if defined(HAVE_GPREGS)
     41 	struct reg gpr;
     42 	register_t rgstr;
     43 #endif
     44 #if defined(HAVE_FPREGS)
     45 	struct fpreg fpr;
     46 #endif
     47 
     48 #if !defined(HAVE_GPREGS)
     49 	if (strcmp(regset, "regs") == 0)
     50 		atf_tc_fail("Impossible test scenario!");
     51 #endif
     52 
     53 #if !defined(HAVE_FPREGS)
     54 	if (strcmp(regset, "fpregs") == 0)
     55 		atf_tc_fail("Impossible test scenario!");
     56 #endif
     57 
     58 	DPRINTF("Before forking process PID=%d\n", getpid());
     59 	SYSCALL_REQUIRE((child = fork()) != -1);
     60 	if (child == 0) {
     61 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
     62 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
     63 
     64 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
     65 		FORKEE_ASSERT(raise(sigval) == 0);
     66 
     67 		DPRINTF("Before exiting of the child process\n");
     68 		_exit(exitval);
     69 	}
     70 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
     71 
     72 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
     73 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
     74 
     75 	validate_status_stopped(status, sigval);
     76 
     77 #if defined(HAVE_GPREGS)
     78 	if (strcmp(regset, "regs") == 0) {
     79 		DPRINTF("Call GETREGS for the child process\n");
     80 		SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &gpr, 0) != -1);
     81 
     82 		if (strcmp(aux, "none") == 0) {
     83 			DPRINTF("Retrieved registers\n");
     84 		} else if (strcmp(aux, "pc") == 0) {
     85 			rgstr = PTRACE_REG_PC(&gpr);
     86 			DPRINTF("Retrieved %" PRIxREGISTER "\n", rgstr);
     87 		} else if (strstr(aux, "set_pc") != NULL) {
     88 			rgstr = PTRACE_REG_PC(&gpr);
     89 			DPRINTF("Retrieved PC %" PRIxREGISTER "\n", rgstr);
     90 			if (strstr(aux, "0x1") != NULL) {
     91 				rgstr |= 0x1;
     92 			} else if (strstr(aux, "0x3") != NULL) {
     93 				rgstr |= 0x3;
     94 			} else if (strstr(aux, "0x7") != NULL) {
     95 				rgstr |= 0x7;
     96 			}
     97 			DPRINTF("Set PC %" PRIxREGISTER "\n", rgstr);
     98 			PTRACE_REG_SET_PC(&gpr, rgstr);
     99 			if (strcmp(aux, "set_pc") != 0) {
    100 				/* This call can fail with EINVAL or similar. */
    101 				ptrace(PT_SETREGS, child, &gpr, 0);
    102 			}
    103 		} else if (strcmp(aux, "sp") == 0) {
    104 			rgstr = PTRACE_REG_SP(&gpr);
    105 			DPRINTF("Retrieved %" PRIxREGISTER "\n", rgstr);
    106 		} else if (strcmp(aux, "intrv") == 0) {
    107 			rgstr = PTRACE_REG_INTRV(&gpr);
    108 			DPRINTF("Retrieved %" PRIxREGISTER "\n", rgstr);
    109 		} else if (strcmp(aux, "setregs") == 0) {
    110 			DPRINTF("Call SETREGS for the child process\n");
    111 			SYSCALL_REQUIRE(
    112 			    ptrace(PT_SETREGS, child, &gpr, 0) != -1);
    113 		}
    114 	}
    115 #endif
    116 
    117 #if defined(HAVE_FPREGS)
    118 	if (strcmp(regset, "fpregs") == 0) {
    119 		DPRINTF("Call GETFPREGS for the child process\n");
    120 		SYSCALL_REQUIRE(ptrace(PT_GETFPREGS, child, &fpr, 0) != -1);
    121 
    122 		if (strcmp(aux, "getfpregs") == 0) {
    123 			DPRINTF("Retrieved FP registers\n");
    124 		} else if (strcmp(aux, "setfpregs") == 0) {
    125 			DPRINTF("Call SETFPREGS for the child\n");
    126 			SYSCALL_REQUIRE(
    127 			    ptrace(PT_SETFPREGS, child, &fpr, 0) != -1);
    128 		}
    129 	}
    130 #endif
    131 
    132 	DPRINTF("Before resuming the child process where it left off and "
    133 	    "without signal to be sent\n");
    134 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
    135 
    136 	if (strstr(aux, "unaligned") != NULL) {
    137 		DPRINTF("Before resuming the child process where it left off "
    138 		    "and without signal to be sent\n");
    139 		SYSCALL_REQUIRE(ptrace(PT_KILL, child, NULL, 0) != -1);
    140 
    141 		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    142 		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
    143 		    child);
    144 
    145 		validate_status_signaled(status, SIGKILL, 0);
    146 
    147 		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    148 		TWAIT_REQUIRE_FAILURE(ECHILD,
    149 		    wpid = TWAIT_GENERIC(child, &status, 0));
    150 	} else {
    151 		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    152 		TWAIT_REQUIRE_SUCCESS(
    153 		    wpid = TWAIT_GENERIC(child, &status, 0), child);
    154 
    155 		validate_status_exited(status, exitval);
    156 
    157 		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    158 		TWAIT_REQUIRE_FAILURE(ECHILD,
    159 		    wpid = TWAIT_GENERIC(child, &status, 0));
    160 	}
    161 }
    162 
    163 #define ACCESS_REGS(test, regset, aux)					\
    164 ATF_TC(test);								\
    165 ATF_TC_HEAD(test, tc)							\
    166 {									\
    167         atf_tc_set_md_var(tc, "descr",					\
    168             "Verify " regset " with auxiliary operation: " aux);	\
    169 }									\
    170 									\
    171 ATF_TC_BODY(test, tc)							\
    172 {									\
    173 									\
    174         access_regs(regset, aux);					\
    175 }
    176 #endif
    177 
    178 #if defined(HAVE_GPREGS)
    179 ACCESS_REGS(access_regs1, "regs", "none")
    180 ACCESS_REGS(access_regs2, "regs", "pc")
    181 ACCESS_REGS(access_regs3, "regs", "set_pc")
    182 ACCESS_REGS(access_regs4, "regs", "sp")
    183 ACCESS_REGS(access_regs5, "regs", "intrv")
    184 ACCESS_REGS(access_regs6, "regs", "setregs")
    185 ACCESS_REGS(access_regs_set_unaligned_pc_0x1, "regs", "set_pc+unaligned+0x1")
    186 ACCESS_REGS(access_regs_set_unaligned_pc_0x3, "regs", "set_pc+unaligned+0x3")
    187 ACCESS_REGS(access_regs_set_unaligned_pc_0x7, "regs", "set_pc+unaligned+0x7")
    188 #endif
    189 #if defined(HAVE_FPREGS)
    190 ACCESS_REGS(access_fpregs1, "fpregs", "getfpregs")
    191 ACCESS_REGS(access_fpregs2, "fpregs", "setfpregs")
    192 #endif
    193 
    194 #define ATF_TP_ADD_TCS_PTRACE_WAIT_REGISTER() \
    195         ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs1); \
    196 	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs2); \
    197 	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs3); \
    198 	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs4); \
    199 	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs5); \
    200 	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs6); \
    201 	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs_set_unaligned_pc_0x1); \
    202 	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs_set_unaligned_pc_0x3); \
    203 	ATF_TP_ADD_TC_HAVE_GPREGS(tp, access_regs_set_unaligned_pc_0x7); \
    204 	ATF_TP_ADD_TC_HAVE_FPREGS(tp, access_fpregs1); \
    205 	ATF_TP_ADD_TC_HAVE_FPREGS(tp, access_fpregs2);
    206