xref: /openbsd/regress/sys/ptrace/ptrace.c (revision 0a6283e4)
1*0a6283e4Sderaadt /*	$OpenBSD: ptrace.c,v 1.10 2021/05/17 15:28:24 deraadt Exp $	*/
269812e89Smiod /*
369812e89Smiod  * Copyright (c) 2004, Mark Kettenis.
469812e89Smiod  * Copyright (c) 2004, Miodrag Vallat.
569812e89Smiod  * All rights reserved.
669812e89Smiod  *
769812e89Smiod  * Redistribution and use in source and binary forms, with or without
869812e89Smiod  * modification, are permitted provided that the following conditions
969812e89Smiod  * are met:
1069812e89Smiod  * 1. Redistributions of source code must retain the above copyright
1169812e89Smiod  *    notice, this list of conditions and the following disclaimer.
1269812e89Smiod  * 2. Redistributions in binary form must reproduce the above copyright
1369812e89Smiod  *    notice, this list of conditions and the following disclaimer in the
1469812e89Smiod  *    documentation and/or other materials provided with the distribution.
1569812e89Smiod  *
1669812e89Smiod  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1769812e89Smiod  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1869812e89Smiod  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1969812e89Smiod  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2069812e89Smiod  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2169812e89Smiod  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2269812e89Smiod  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2369812e89Smiod  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2469812e89Smiod  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2569812e89Smiod  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2669812e89Smiod  */
2769812e89Smiod 
2869812e89Smiod #include <sys/types.h>
2969812e89Smiod #include <sys/ptrace.h>
3069812e89Smiod #include <sys/wait.h>
3169812e89Smiod 
3269812e89Smiod #include <machine/reg.h>
3369812e89Smiod 
3469812e89Smiod #include <signal.h>
3569812e89Smiod #include <stdio.h>
3669812e89Smiod #include <stdlib.h>
3769812e89Smiod #include <unistd.h>
3869812e89Smiod 
3969812e89Smiod /*
4069812e89Smiod  * This tests checks whether ptrace will correctly cope with unaligned pc.
4169812e89Smiod  */
4269812e89Smiod int
main(void)4369812e89Smiod main(void)
4469812e89Smiod {
4569812e89Smiod 	pid_t pid;
4669812e89Smiod 	int status;
4769812e89Smiod 
4869812e89Smiod 	pid = fork();
4969812e89Smiod 	if (pid == 0) {
5069812e89Smiod 		ptrace(PT_TRACE_ME, 0, 0, 0);
5169812e89Smiod 		raise(SIGTRAP);
5269812e89Smiod 		exit(EXIT_FAILURE);
5369812e89Smiod 	} else {
5469812e89Smiod 		struct reg regs;
5569812e89Smiod 
5669812e89Smiod 		waitpid(pid, &status, 0);
5769812e89Smiod 		ptrace(PT_GETREGS, pid, (caddr_t)&regs, 0);
5869812e89Smiod 
5969812e89Smiod 		/*
60376829fcSguenther 		 * Make sure amd64 is tested before i386
6169812e89Smiod 		 */
6269812e89Smiod 
633eb61a78Smiod #if defined(__alpha__)
643eb61a78Smiod 		regs.r_regs[R_ZERO]|= 0x07;
653eb61a78Smiod #elif defined(__x86_64__)
66c6ae7357Sderaadt 		regs.r_rip |= 0x07;
67dba5eb8fSmiod #elif defined(__arm__)
68dba5eb8fSmiod 		regs.r_pc |= 0x03;
6969812e89Smiod #elif defined(__hppa__)
705dda08d5Sjsing 		regs.r_pcoqh |= 0x03;
715dda08d5Sjsing 		regs.r_pcoqt |= 0x03;
7269812e89Smiod #elif defined(__i386__)
7369812e89Smiod 		regs.r_eip |= 0x03;
742cfc9e28Smiod #elif defined(__mips64__)
752cfc9e28Smiod 		regs.r_regs[PC] |= 0x03;
76e6e24012Skettenis #elif defined(__powerpc64__)
77e6e24012Skettenis 		regs.r_pc |= 0x03;
7869812e89Smiod #elif defined(__powerpc__)
7969812e89Smiod 		regs.pc |= 0x03;
80*0a6283e4Sderaadt #elif defined(__riscv)
81*0a6283e4Sderaadt 		regs.r_sepc |= 0x01;
822cfc9e28Smiod #elif defined(__sh__)
832cfc9e28Smiod 		regs.r_spc |= 0x01;
8469812e89Smiod #elif defined( __sparcv9__)
8569812e89Smiod 		regs.r_pc |= 0x07;
8669812e89Smiod 		regs.r_npc |= 0x07;
8769812e89Smiod #elif defined( __m88k__)
8869812e89Smiod 		/*
8969812e89Smiod 		 * The following code is for 88100 only, but should work with
902cfc9e28Smiod 		 * 88110 too, even though it sets the DELAY bit in exip.
9169812e89Smiod 		 */
9269812e89Smiod 		regs.sxip |= 0x03;
9369812e89Smiod 		regs.snip |= 0x03;
9469812e89Smiod 		regs.sfip |= 0x03;
9569812e89Smiod #endif
9669812e89Smiod 		ptrace(PT_SETREGS, pid, (caddr_t)&regs, 0);
9769812e89Smiod 		ptrace(PT_CONTINUE, pid, (caddr_t)1, 0);
9869812e89Smiod 	}
9969812e89Smiod 	exit(EXIT_SUCCESS);
10069812e89Smiod }
101