xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/ptrace/ptrace04.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 /*
2  * make sure PEEKUSER matches GETREGS
3  *
4  * Copyright (c) 2008 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2 or later
7  */
8 
9 #define _GNU_SOURCE
10 
11 #include <errno.h>
12 #include <stdbool.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <sys/ptrace.h>
17 
18 #include "test.h"
19 #include "spawn_ptrace_child.h"
20 
21 char *TCID = "ptrace04";
22 
23 static void cleanup();
24 
25 #define R(r) { .name = "PT_" #r, .off = PT_##r },
26 static struct {
27 	const char *name;
28 	long off;
29 } regs[] = {
30 #ifdef __bfin__
31 	R(ORIG_R0) R(ORIG_P0)
32 	    R(R0) R(R1) R(R2) R(R3) R(R4) R(R5) R(R6) R(R7)
33 	    R(P0) R(P1) R(P2) R(P3) R(P4) R(P5) R(FP) R(USP)
34 	    R(I0) R(I1) R(I2) R(I3)
35 	    R(M0) R(M1) R(M2) R(M3)
36 	    R(L0) R(L1) R(L2) R(L3)
37 	    R(B0) R(B1) R(B2) R(B3)
38 	    R(A0X) R(A0W) R(A1X) R(A1W)
39 	    R(LC0) R(LC1) R(LT0) R(LT1) R(LB0) R(LB1)
40 	    R(ASTAT)
41 	    R(RETS) R(PC) R(RETX) R(RETN) R(RETE)
42 	    R(SEQSTAT) R(IPEND) R(SYSCFG)
43 #endif
44 };
45 
46 int TST_TOTAL = 2;
47 
compare_registers(unsigned char poison)48 void compare_registers(unsigned char poison)
49 {
50 #if defined(HAVE_STRUCT_PTRACE_REGS) && defined(PTRACE_GETREGS)
51 	ptrace_regs _pt_regs;
52 	size_t i;
53 	long ret;
54 	bool failed = false;
55 
56 	memset(&_pt_regs, poison, sizeof(_pt_regs));
57 	errno = 0;
58 	ret = ptrace(PTRACE_GETREGS, pid, NULL, &_pt_regs);
59 	if (ret && errno) {
60 		tst_resm(TFAIL | TERRNO, "PTRACE_GETREGS failed");
61 	} else {
62 
63 		for (i = 0; i < ARRAY_SIZE(regs); ++i) {
64 			errno = 0;
65 			ret = ptrace(PTRACE_PEEKUSER, pid,
66 				     (void *)regs[i].off, NULL);
67 			if (ret && errno) {
68 				tst_resm(TFAIL | TERRNO,
69 					 "PTRACE_PEEKUSER: register %s "
70 					 "(offset %li) failed",
71 					 regs[i].name, regs[i].off);
72 				failed = true;
73 				continue;
74 			}
75 
76 			long *pt_val = (void *)&_pt_regs + regs[i].off;
77 			if (*pt_val != ret) {
78 				tst_resm(TFAIL,
79 					 "register %s (offset %li) did not "
80 					 "match\n\tGETREGS: 0x%08lx "
81 					 "PEEKUSER: 0x%08lx",
82 					 regs[i].name, regs[i].off, *pt_val,
83 					 ret);
84 				failed = true;
85 			}
86 
87 		}
88 
89 	}
90 
91 	tst_resm((failed ? TFAIL : TPASS),
92 		 "PTRACE PEEKUSER/GETREGS (poison 0x%02x)", poison);
93 #else
94 	tst_brkm(TCONF, cleanup, "System doesn't have ptrace_regs structure");
95 #endif
96 }
97 
main(int argc,char * argv[])98 int main(int argc, char *argv[])
99 {
100 	tst_parse_opts(argc, argv, NULL, NULL);
101 
102 	if (ARRAY_SIZE(regs) == 0)
103 		tst_brkm(TCONF, NULL, "test not supported for your arch (yet)");
104 
105 	make_a_baby(argc, argv);
106 
107 	/* first compare register states when execl() syscall starts */
108 	tst_resm(TINFO, "Before exec() in child");
109 	compare_registers(0x00);
110 	compare_registers(0xff);
111 
112 	/* then compare register states after execl() syscall finishes */
113 	tst_resm(TINFO, "After exec() in child");
114 	errno = 0;
115 	if (ptrace(PTRACE_SYSCALL, pid, NULL, NULL) && errno) {
116 		tst_brkm(TFAIL, NULL, "PTRACE_SYSCALL failed: %s",
117 			 strerror(errno));
118 	}
119 	compare_registers(0x00);
120 	compare_registers(0xff);
121 
122 	/* hopefully this worked */
123 	ptrace(PTRACE_KILL, pid, NULL, NULL);
124 
125 	tst_exit();
126 }
127 
cleanup(void)128 static void cleanup(void)
129 {
130 }
131