Home | History | Annotate | Line # | Download | only in sys
t_ptrace_i386_wait.h revision 1.6
      1  1.6    mgorny /*	$NetBSD: t_ptrace_i386_wait.h,v 1.6 2019/05/05 18:15:40 mgorny Exp $	*/
      2  1.1     kamil 
      3  1.1     kamil /*-
      4  1.3     kamil  * Copyright (c) 2016, 2017, 2018, 2019 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 #if defined(__i386__)
     30  1.1     kamil ATF_TC(i386_regs1);
     31  1.1     kamil ATF_TC_HEAD(i386_regs1, tc)
     32  1.1     kamil {
     33  1.1     kamil 	atf_tc_set_md_var(tc, "descr",
     34  1.1     kamil 	    "Call PT_GETREGS and iterate over General Purpose registers");
     35  1.1     kamil }
     36  1.1     kamil 
     37  1.1     kamil ATF_TC_BODY(i386_regs1, tc)
     38  1.1     kamil {
     39  1.1     kamil 	const int exitval = 5;
     40  1.1     kamil 	const int sigval = SIGSTOP;
     41  1.1     kamil 	pid_t child, wpid;
     42  1.1     kamil #if defined(TWAIT_HAVE_STATUS)
     43  1.1     kamil 	int status;
     44  1.1     kamil #endif
     45  1.1     kamil 	struct reg r;
     46  1.1     kamil 
     47  1.2  christos 	DPRINTF("Before forking process PID=%d\n", getpid());
     48  1.2  christos 	SYSCALL_REQUIRE((child = fork()) != -1);
     49  1.1     kamil 	if (child == 0) {
     50  1.2  christos 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
     51  1.1     kamil 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
     52  1.1     kamil 
     53  1.2  christos 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
     54  1.1     kamil 		FORKEE_ASSERT(raise(sigval) == 0);
     55  1.1     kamil 
     56  1.2  christos 		DPRINTF("Before exiting of the child process\n");
     57  1.1     kamil 		_exit(exitval);
     58  1.1     kamil 	}
     59  1.2  christos 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
     60  1.1     kamil 
     61  1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
     62  1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
     63  1.1     kamil 
     64  1.1     kamil 	validate_status_stopped(status, sigval);
     65  1.1     kamil 
     66  1.2  christos 	DPRINTF("Call GETREGS for the child process\n");
     67  1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
     68  1.1     kamil 
     69  1.2  christos 	DPRINTF("EAX=%#" PRIxREGISTER "\n", r.r_eax);
     70  1.2  christos 	DPRINTF("EBX=%#" PRIxREGISTER "\n", r.r_ebx);
     71  1.2  christos 	DPRINTF("ECX=%#" PRIxREGISTER "\n", r.r_ecx);
     72  1.2  christos 	DPRINTF("EDX=%#" PRIxREGISTER "\n", r.r_edx);
     73  1.1     kamil 
     74  1.2  christos 	DPRINTF("ESP=%#" PRIxREGISTER "\n", r.r_esp);
     75  1.2  christos 	DPRINTF("EBP=%#" PRIxREGISTER "\n", r.r_ebp);
     76  1.1     kamil 
     77  1.2  christos 	DPRINTF("ESI=%#" PRIxREGISTER "\n", r.r_esi);
     78  1.2  christos 	DPRINTF("EDI=%#" PRIxREGISTER "\n", r.r_edi);
     79  1.1     kamil 
     80  1.2  christos 	DPRINTF("EIP=%#" PRIxREGISTER "\n", r.r_eip);
     81  1.1     kamil 
     82  1.2  christos 	DPRINTF("EFLAGS=%#" PRIxREGISTER "\n", r.r_eflags);
     83  1.1     kamil 
     84  1.2  christos 	DPRINTF("CS=%#" PRIxREGISTER "\n", r.r_cs);
     85  1.2  christos 	DPRINTF("SS=%#" PRIxREGISTER "\n", r.r_ss);
     86  1.2  christos 	DPRINTF("DS=%#" PRIxREGISTER "\n", r.r_ds);
     87  1.2  christos 	DPRINTF("ES=%#" PRIxREGISTER "\n", r.r_es);
     88  1.2  christos 	DPRINTF("FS=%#" PRIxREGISTER "\n", r.r_fs);
     89  1.2  christos 	DPRINTF("GS=%#" PRIxREGISTER "\n", r.r_gs);
     90  1.1     kamil 
     91  1.2  christos 	DPRINTF("Before resuming the child process where it left off and "
     92  1.1     kamil 	    "without signal to be sent\n");
     93  1.2  christos 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
     94  1.1     kamil 
     95  1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
     96  1.1     kamil 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
     97  1.1     kamil 
     98  1.1     kamil 	validate_status_exited(status, exitval);
     99  1.1     kamil 
    100  1.2  christos 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    101  1.1     kamil 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
    102  1.1     kamil }
    103  1.6    mgorny 
    104  1.6    mgorny ATF_TC(i386_regs_gp_read);
    105  1.6    mgorny ATF_TC_HEAD(i386_regs_gp_read, tc)
    106  1.6    mgorny {
    107  1.6    mgorny 	atf_tc_set_md_var(tc, "descr",
    108  1.6    mgorny 		"Set general-purpose reg values from debugged program and read "
    109  1.6    mgorny 		"them via PT_GETREGS, comparing values against expected.");
    110  1.6    mgorny }
    111  1.6    mgorny 
    112  1.6    mgorny ATF_TC_BODY(i386_regs_gp_read, tc)
    113  1.6    mgorny {
    114  1.6    mgorny 	const int exitval = 5;
    115  1.6    mgorny 	const int sigval = SIGTRAP;
    116  1.6    mgorny 	pid_t child, wpid;
    117  1.6    mgorny #if defined(TWAIT_HAVE_STATUS)
    118  1.6    mgorny 	int status;
    119  1.6    mgorny #endif
    120  1.6    mgorny 	struct reg gpr;
    121  1.6    mgorny 
    122  1.6    mgorny 	const uint32_t eax = 0x00010203;
    123  1.6    mgorny 	const uint32_t ebx = 0x10111213;
    124  1.6    mgorny 	const uint32_t ecx = 0x20212223;
    125  1.6    mgorny 	const uint32_t edx = 0x30313233;
    126  1.6    mgorny 	const uint32_t esi = 0x40414243;
    127  1.6    mgorny 	const uint32_t edi = 0x50515253;
    128  1.6    mgorny 
    129  1.6    mgorny 	DPRINTF("Before forking process PID=%d\n", getpid());
    130  1.6    mgorny 	SYSCALL_REQUIRE((child = fork()) != -1);
    131  1.6    mgorny 	if (child == 0) {
    132  1.6    mgorny 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
    133  1.6    mgorny 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
    134  1.6    mgorny 
    135  1.6    mgorny 		DPRINTF("Before running assembly from child\n");
    136  1.6    mgorny 
    137  1.6    mgorny 		__asm__ __volatile__(
    138  1.6    mgorny 			"int3\n\t"
    139  1.6    mgorny 			:
    140  1.6    mgorny 			: "a"(eax), "b"(ebx), "c"(ecx), "d"(edx), "S"(esi), "D"(edi)
    141  1.6    mgorny 			:
    142  1.6    mgorny 		);
    143  1.6    mgorny 
    144  1.6    mgorny 		DPRINTF("Before exiting of the child process\n");
    145  1.6    mgorny 		_exit(exitval);
    146  1.6    mgorny 	}
    147  1.6    mgorny 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
    148  1.6    mgorny 
    149  1.6    mgorny 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    150  1.6    mgorny 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
    151  1.6    mgorny 
    152  1.6    mgorny 	validate_status_stopped(status, sigval);
    153  1.6    mgorny 
    154  1.6    mgorny 	DPRINTF("Call GETREGS for the child process\n");
    155  1.6    mgorny 	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &gpr, 0) != -1);
    156  1.6    mgorny 
    157  1.6    mgorny 	ATF_CHECK_EQ((uint32_t)gpr.r_eax, eax);
    158  1.6    mgorny 	ATF_CHECK_EQ((uint32_t)gpr.r_ebx, ebx);
    159  1.6    mgorny 	ATF_CHECK_EQ((uint32_t)gpr.r_ecx, ecx);
    160  1.6    mgorny 	ATF_CHECK_EQ((uint32_t)gpr.r_edx, edx);
    161  1.6    mgorny 	ATF_CHECK_EQ((uint32_t)gpr.r_esi, esi);
    162  1.6    mgorny 	ATF_CHECK_EQ((uint32_t)gpr.r_edi, edi);
    163  1.6    mgorny 
    164  1.6    mgorny 	DPRINTF("Before resuming the child process where it left off and "
    165  1.6    mgorny 	    "without signal to be sent\n");
    166  1.6    mgorny 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
    167  1.6    mgorny 
    168  1.6    mgorny 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    169  1.6    mgorny 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
    170  1.6    mgorny 
    171  1.6    mgorny 	validate_status_exited(status, exitval);
    172  1.6    mgorny 
    173  1.6    mgorny 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    174  1.6    mgorny 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
    175  1.6    mgorny }
    176  1.6    mgorny 
    177  1.6    mgorny ATF_TC(i386_regs_ebp_esp_read);
    178  1.6    mgorny ATF_TC_HEAD(i386_regs_ebp_esp_read, tc)
    179  1.6    mgorny {
    180  1.6    mgorny 	atf_tc_set_md_var(tc, "descr",
    181  1.6    mgorny 		"Set EBP & ESP reg values from debugged program and read "
    182  1.6    mgorny 		"them via PT_GETREGS, comparing values against expected.");
    183  1.6    mgorny }
    184  1.6    mgorny 
    185  1.6    mgorny ATF_TC_BODY(i386_regs_ebp_esp_read, tc)
    186  1.6    mgorny {
    187  1.6    mgorny 	const int exitval = 5;
    188  1.6    mgorny 	const int sigval = SIGTRAP;
    189  1.6    mgorny 	pid_t child, wpid;
    190  1.6    mgorny #if defined(TWAIT_HAVE_STATUS)
    191  1.6    mgorny 	int status;
    192  1.6    mgorny #endif
    193  1.6    mgorny 	struct reg gpr;
    194  1.6    mgorny 
    195  1.6    mgorny 	const uint32_t esp = 0x60616263;
    196  1.6    mgorny 	const uint32_t ebp = 0x70717273;
    197  1.6    mgorny 
    198  1.6    mgorny 	DPRINTF("Before forking process PID=%d\n", getpid());
    199  1.6    mgorny 	SYSCALL_REQUIRE((child = fork()) != -1);
    200  1.6    mgorny 	if (child == 0) {
    201  1.6    mgorny 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
    202  1.6    mgorny 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
    203  1.6    mgorny 
    204  1.6    mgorny 		DPRINTF("Before running assembly from child\n");
    205  1.6    mgorny 
    206  1.6    mgorny 		__asm__ __volatile__(
    207  1.6    mgorny 			/* ebp & ebp are a bit tricky, we must not clobber them */
    208  1.6    mgorny 			"movl    %%esp, %%eax\n\t"
    209  1.6    mgorny 			"movl    %%ebp, %%ebx\n\t"
    210  1.6    mgorny 			"movl    %0, %%esp\n\t"
    211  1.6    mgorny 			"movl    %1, %%ebp\n\t"
    212  1.6    mgorny 			"\n\t"
    213  1.6    mgorny 			"int3\n\t"
    214  1.6    mgorny 			"\n\t"
    215  1.6    mgorny 			"movl    %%eax, %%esp\n\t"
    216  1.6    mgorny 			"movl    %%ebx, %%ebp\n\t"
    217  1.6    mgorny 			:
    218  1.6    mgorny 			: "ri"(esp), "ri"(ebp)
    219  1.6    mgorny 			: "%eax", "%ebx"
    220  1.6    mgorny 		);
    221  1.6    mgorny 
    222  1.6    mgorny 		DPRINTF("Before exiting of the child process\n");
    223  1.6    mgorny 		_exit(exitval);
    224  1.6    mgorny 	}
    225  1.6    mgorny 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
    226  1.6    mgorny 
    227  1.6    mgorny 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    228  1.6    mgorny 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
    229  1.6    mgorny 
    230  1.6    mgorny 	validate_status_stopped(status, sigval);
    231  1.6    mgorny 
    232  1.6    mgorny 	DPRINTF("Call GETREGS for the child process\n");
    233  1.6    mgorny 	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &gpr, 0) != -1);
    234  1.6    mgorny 
    235  1.6    mgorny 	ATF_CHECK_EQ((uint32_t)gpr.r_esp, esp);
    236  1.6    mgorny 	ATF_CHECK_EQ((uint32_t)gpr.r_ebp, ebp);
    237  1.6    mgorny 
    238  1.6    mgorny 	DPRINTF("Before resuming the child process where it left off and "
    239  1.6    mgorny 	    "without signal to be sent\n");
    240  1.6    mgorny 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
    241  1.6    mgorny 
    242  1.6    mgorny 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    243  1.6    mgorny 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
    244  1.6    mgorny 
    245  1.6    mgorny 	validate_status_exited(status, exitval);
    246  1.6    mgorny 
    247  1.6    mgorny 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
    248  1.6    mgorny 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
    249  1.6    mgorny }
    250  1.6    mgorny 
    251  1.1     kamil #define ATF_TP_ADD_TCS_PTRACE_WAIT_I386() \
    252  1.6    mgorny 	ATF_TP_ADD_TC_HAVE_GPREGS(tp, i386_regs1); \
    253  1.6    mgorny 	ATF_TP_ADD_TC_HAVE_GPREGS(tp, i386_regs_gp_read); \
    254  1.6    mgorny 	ATF_TP_ADD_TC_HAVE_GPREGS(tp, i386_regs_ebp_esp_read);
    255  1.1     kamil #else
    256  1.1     kamil #define ATF_TP_ADD_TCS_PTRACE_WAIT_I386()
    257  1.1     kamil #endif
    258