1 // SPDX-License-Identifier: GPL-2.0
3 * A ptrace test for testing PTRACE_SYSEMU, PTRACE_SETREGS and
4 * PTRACE_GETREG. This test basically create a child process that executes
5 * syscalls and the parent process check if it is being traced appropriated.
7 * This test is heavily based on tools/testing/selftests/x86/ptrace_syscall.c
8 * test, and it was adapted to run on Powerpc by
9 * Breno Leitao <leitao@debian.org>
13 #include <sys/ptrace.h>
14 #include <sys/types.h>
16 #include <sys/syscall.h>
27 /* Bitness-agnostic defines for user_regs_struct fields. */
28 #define user_syscall_nr gpr[0]
29 #define user_arg0 gpr[3]
30 #define user_arg1 gpr[4]
31 #define user_arg2 gpr[5]
32 #define user_arg3 gpr[6]
33 #define user_arg4 gpr[7]
34 #define user_arg5 gpr[8]
37 #define PTRACE_SYSEMU 0x1d
41 static void wait_trap(pid_t chld)
45 if (waitid(P_PID, chld, &si, WEXITED|WSTOPPED) != 0)
47 if (si.si_pid != chld)
48 errx(1, "got unexpected pid in event\n");
49 if (si.si_code != CLD_TRAPPED)
50 errx(1, "got unexpected event type %d\n", si.si_code);
53 static void test_ptrace_syscall_restart(void)
59 printf("[RUN]\tptrace-induced syscall restart\n");
66 * Child process is running 4 syscalls after ptrace.
70 * 3) tgkill() -> Send SIGSTOP
71 * 4) gettid() -> Where the tests will happen essentially
74 if (ptrace(PTRACE_TRACEME, 0, 0, 0) != 0)
75 err(1, "PTRACE_TRACEME");
77 pid_t pid = getpid(), tid = syscall(SYS_gettid);
79 printf("\tChild will make one syscall\n");
80 syscall(SYS_tgkill, pid, tid, SIGSTOP);
82 syscall(SYS_gettid, 10, 11, 12, 13, 14, 15);
85 /* Parent process below */
87 /* Wait for SIGSTOP sent by tgkill above. */
88 if (waitpid(chld, &status, 0) != chld || !WIFSTOPPED(status))
91 printf("[RUN]\tSYSEMU\n");
92 if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
93 err(1, "PTRACE_SYSEMU");
96 if (ptrace(PTRACE_GETREGS, chld, 0, ®s) != 0)
97 err(1, "PTRACE_GETREGS");
100 * Ptrace trapped prior to executing the syscall, thus r3 still has
101 * the syscall number instead of the sys_gettid() result
103 if (regs.user_syscall_nr != SYS_gettid ||
104 regs.user_arg0 != 10 || regs.user_arg1 != 11 ||
105 regs.user_arg2 != 12 || regs.user_arg3 != 13 ||
106 regs.user_arg4 != 14 || regs.user_arg5 != 15) {
107 printf("[FAIL]\tInitial args are wrong (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n",
108 (unsigned long)regs.user_syscall_nr,
109 (unsigned long)regs.user_arg0,
110 (unsigned long)regs.user_arg1,
111 (unsigned long)regs.user_arg2,
112 (unsigned long)regs.user_arg3,
113 (unsigned long)regs.user_arg4,
114 (unsigned long)regs.user_arg5);
117 printf("[OK]\tInitial nr and args are correct\n"); }
119 printf("[RUN]\tRestart the syscall (ip = 0x%lx)\n",
120 (unsigned long)regs.user_ip);
123 * Rewind to retry the same syscall again. This will basically test
124 * the rewind process together with PTRACE_SETREGS and PTRACE_GETREGS.
127 if (ptrace(PTRACE_SETREGS, chld, 0, ®s) != 0)
128 err(1, "PTRACE_SETREGS");
130 if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
131 err(1, "PTRACE_SYSEMU");
134 if (ptrace(PTRACE_GETREGS, chld, 0, ®s) != 0)
135 err(1, "PTRACE_GETREGS");
137 if (regs.user_syscall_nr != SYS_gettid ||
138 regs.user_arg0 != 10 || regs.user_arg1 != 11 ||
139 regs.user_arg2 != 12 || regs.user_arg3 != 13 ||
140 regs.user_arg4 != 14 || regs.user_arg5 != 15) {
141 printf("[FAIL]\tRestart nr or args are wrong (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n",
142 (unsigned long)regs.user_syscall_nr,
143 (unsigned long)regs.user_arg0,
144 (unsigned long)regs.user_arg1,
145 (unsigned long)regs.user_arg2,
146 (unsigned long)regs.user_arg3,
147 (unsigned long)regs.user_arg4,
148 (unsigned long)regs.user_arg5);
151 printf("[OK]\tRestarted nr and args are correct\n");
154 printf("[RUN]\tChange nr and args and restart the syscall (ip = 0x%lx)\n",
155 (unsigned long)regs.user_ip);
158 * Inject a new syscall (getpid) in the same place the previous
159 * syscall (gettid), rewind and re-execute.
161 regs.user_syscall_nr = SYS_getpid;
170 if (ptrace(PTRACE_SETREGS, chld, 0, ®s) != 0)
171 err(1, "PTRACE_SETREGS");
173 if (ptrace(PTRACE_SYSEMU, chld, 0, 0) != 0)
174 err(1, "PTRACE_SYSEMU");
177 if (ptrace(PTRACE_GETREGS, chld, 0, ®s) != 0)
178 err(1, "PTRACE_GETREGS");
180 /* Check that ptrace stopped at the new syscall that was
181 * injected, and guarantee that it haven't executed, i.e, user_args
182 * contain the arguments and not the syscall return value, for
185 if (regs.user_syscall_nr != SYS_getpid
186 || regs.user_arg0 != 20 || regs.user_arg1 != 21
187 || regs.user_arg2 != 22 || regs.user_arg3 != 23
188 || regs.user_arg4 != 24 || regs.user_arg5 != 25) {
190 printf("[FAIL]\tRestart nr or args are wrong (nr=%lu, args=%lu %lu %lu %lu %lu %lu)\n",
191 (unsigned long)regs.user_syscall_nr,
192 (unsigned long)regs.user_arg0,
193 (unsigned long)regs.user_arg1,
194 (unsigned long)regs.user_arg2,
195 (unsigned long)regs.user_arg3,
196 (unsigned long)regs.user_arg4,
197 (unsigned long)regs.user_arg5);
200 printf("[OK]\tReplacement nr and args are correct\n");
203 if (ptrace(PTRACE_CONT, chld, 0, 0) != 0)
204 err(1, "PTRACE_CONT");
206 if (waitpid(chld, &status, 0) != chld)
209 /* Guarantee that the process executed properly, returning 0 */
210 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
211 printf("[FAIL]\tChild failed\n");
214 printf("[OK]\tChild exited cleanly\n");
218 int ptrace_syscall(void)
220 test_ptrace_syscall_restart();
227 return test_harness(ptrace_syscall, "ptrace_syscall");