linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Andrei Vagin <avagin@gmail.com>
To: Will Deacon <will@kernel.org>, Catalin Marinas <catalin.marinas@arm.com>
Cc: Oleg Nesterov <oleg@redhat.com>,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, Andrei Vagin <avagin@gmail.com>,
	Dave Martin <Dave.Martin@arm.com>,
	Keno Fischer <keno@juliacomputing.com>
Subject: [PATCH 3/4] selftest/arm64/ptrace: add a test for orig_x0
Date: Mon, 22 Mar 2021 15:50:52 -0700	[thread overview]
Message-ID: <20210322225053.428615-4-avagin@gmail.com> (raw)
In-Reply-To: <20210322225053.428615-1-avagin@gmail.com>

The test creates two processes where one traces another one.  The tracee
executes a system call, the tracer traps it, changes orig_x0, triggers a
signal and checks that the syscall is restarted with the setted
argument.

Test output:
 $ ./ptrace_restart_syscall_test
 1..3
 ok 1 orig_x0: 0x3
 ok 2 x0: 0x5
 ok 3 The child exited with code 0.
 # Totals: pass:3 fail:0 xfail:0 xpass:0 skip:0 error:0

Signed-off-by: Andrei Vagin <avagin@gmail.com>
---
 tools/testing/selftests/arm64/ptrace/Makefile |   6 +
 tools/testing/selftests/arm64/ptrace/lib.h    |  36 ++++++
 .../ptrace/ptrace_restart_syscall_test.c      | 122 ++++++++++++++++++
 3 files changed, 164 insertions(+)
 create mode 100644 tools/testing/selftests/arm64/ptrace/Makefile
 create mode 100644 tools/testing/selftests/arm64/ptrace/lib.h
 create mode 100644 tools/testing/selftests/arm64/ptrace/ptrace_restart_syscall_test.c

diff --git a/tools/testing/selftests/arm64/ptrace/Makefile b/tools/testing/selftests/arm64/ptrace/Makefile
new file mode 100644
index 000000000000..1bc10e2d2ac8
--- /dev/null
+++ b/tools/testing/selftests/arm64/ptrace/Makefile
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0
+
+CFLAGS += -g -I../../../../../usr/include/
+TEST_GEN_PROGS := ptrace_restart_syscall_test
+
+include ../../lib.mk
diff --git a/tools/testing/selftests/arm64/ptrace/lib.h b/tools/testing/selftests/arm64/ptrace/lib.h
new file mode 100644
index 000000000000..14f4737188a3
--- /dev/null
+++ b/tools/testing/selftests/arm64/ptrace/lib.h
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#ifndef __PTRACE_TEST_LOG_H
+#define __PTRACE_TEST_LOG_H
+
+#define pr_p(func, fmt, ...) func("%s:%d: " fmt ": %m", \
+				  __func__, __LINE__, ##__VA_ARGS__)
+
+#define pr_err(fmt, ...)						\
+	({								\
+		ksft_test_result_error(fmt "\n", ##__VA_ARGS__);	\
+		-1;							\
+	})
+
+#define pr_fail(fmt, ...)					\
+	({							\
+		ksft_test_result_fail(fmt "\n", ##__VA_ARGS__);	\
+		-1;						\
+	})
+
+#define pr_perror(fmt, ...)	pr_p(pr_err, fmt, ##__VA_ARGS__)
+
+static inline int ptrace_and_wait(pid_t pid, int cmd, int sig)
+{
+	int status;
+
+	/* Stop on syscall-exit. */
+	if (ptrace(cmd, pid, 0, 0))
+		return pr_perror("Can't resume the child %d", pid);
+	if (waitpid(pid, &status, 0) != pid)
+		return pr_perror("Can't wait for the child %d", pid);
+	if (!WIFSTOPPED(status) || WSTOPSIG(status) != sig)
+		return pr_err("Unexpected status: %x", status);
+	return 0;
+}
+
+#endif
diff --git a/tools/testing/selftests/arm64/ptrace/ptrace_restart_syscall_test.c b/tools/testing/selftests/arm64/ptrace/ptrace_restart_syscall_test.c
new file mode 100644
index 000000000000..ce59657f41be
--- /dev/null
+++ b/tools/testing/selftests/arm64/ptrace/ptrace_restart_syscall_test.c
@@ -0,0 +1,122 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <sys/types.h>
+#include <sys/ptrace.h>
+#include <sys/stat.h>
+#include <sys/user.h>
+#include <sys/wait.h>
+#include <sys/uio.h>
+#include <linux/elf.h>
+#include <linux/unistd.h>
+
+#include "../../kselftest.h"
+#include "lib.h"
+
+static int child(int fd)
+{
+	char c;
+
+	if (read(fd, &c, 1) != 1)
+		return 1;
+
+	return 0;
+}
+
+int main(int argc, void **argv)
+{
+	union {
+		struct user_regs_struct r;
+		struct {
+			char __regs[272];
+			unsigned long long orig_x0;
+			unsigned long long orig_x7;
+		};
+	} regs = {};
+	struct iovec iov = {
+		.iov_base = &regs,
+		.iov_len = sizeof(regs),
+	};
+	int status;
+	pid_t pid;
+	int p[2], fdzero;
+
+	ksft_set_plan(3);
+
+	if (pipe(p))
+		return pr_perror("Can't create a pipe");
+	fdzero = open("/dev/zero", O_RDONLY);
+	if (fdzero < 0)
+		return pr_perror("Can't open /dev/zero");
+
+	pid = fork();
+	if (pid == 0) {
+		kill(getpid(), SIGSTOP);
+		return child(p[0]);
+	}
+	if (pid < 0)
+		return 1;
+
+	if (ptrace(PTRACE_ATTACH, pid, 0, 0))
+		return pr_perror("Can't attach to the child %d", pid);
+	if (waitpid(pid, &status, 0) != pid)
+		return pr_perror("Can't wait for the child %d", pid);
+	/* Skip SIGSTOP */
+	if (ptrace_and_wait(pid, PTRACE_CONT, SIGSTOP))
+		return 1;
+
+	/* Resume the child to the next system call. */
+	if (ptrace_and_wait(pid, PTRACE_SYSCALL, SIGTRAP))
+		return 1;
+
+	/* Send a signal to interrupt the system call. */
+	kill(pid, SIGUSR1);
+
+	/* Stop on syscall-exit. */
+	if (ptrace_and_wait(pid, PTRACE_SYSCALL, SIGTRAP))
+		return 1;
+
+	if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov))
+		return pr_perror("Can't get child registers");
+	if (regs.orig_x0 != p[0])
+		return pr_fail("Unexpected x0: 0x%lx", regs.r.regs[0]);
+	ksft_test_result_pass("orig_x0: 0x%llx\n", regs.orig_x0);
+
+	/* Change orig_x0 that will be x0 for the restarted system call. */
+	regs.orig_x0 = fdzero;
+	if (ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &iov))
+		return pr_perror("Can't get child registers");
+
+	/* Trap the signal and skip it. */
+	if (ptrace_and_wait(pid, PTRACE_SYSCALL, SIGUSR1))
+		return 1;
+
+	/* Trap the restarted system call. */
+	if (ptrace_and_wait(pid, PTRACE_SYSCALL, SIGTRAP))
+		return 1;
+
+	/* Check that the syscall is started with the right first argument. */
+	if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov))
+		return pr_perror("Can't get child registers");
+	if (regs.r.regs[0] != fdzero)
+		return pr_fail("unexpected x0: %lx", regs.r.regs[0]);
+	ksft_test_result_pass("x0: 0x%llx\n", regs.r.regs[0]);
+
+	if (ptrace(PTRACE_CONT, pid, 0, 0))
+		return pr_perror("Can't resume the child %d", pid);
+	if (waitpid(pid, &status, 0) != pid)
+		return pr_perror("Can't wait for the child %d", pid);
+	if (status != 0)
+		return pr_fail("Child exited with code %d.", status);
+
+	ksft_test_result_pass("The child exited with code 0.\n");
+	ksft_exit_pass();
+	return 0;
+}
+
-- 
2.29.2


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

  parent reply	other threads:[~2021-03-22 22:55 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-22 22:50 [PATCH 0/4 v3] arm64/ptrace: allow to get all registers on syscall traps Andrei Vagin
2021-03-22 22:50 ` [PATCH 1/4] arm64: expose orig_x0 in the user_pt_regs structure Andrei Vagin
2021-03-26 18:28   ` Catalin Marinas
2021-03-27  0:35     ` Andrei Vagin
2021-03-27 13:01       ` Catalin Marinas
2021-03-22 22:50 ` [PATCH 2/4] arm64/ptrace: introduce orig_x7 " Andrei Vagin
2021-03-26 18:39   ` Catalin Marinas
2021-03-22 22:50 ` Andrei Vagin [this message]
2021-03-22 22:50 ` [PATCH 4/4] selftest/arm64/ptrace: add a test for orig_x7 Andrei Vagin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210322225053.428615-4-avagin@gmail.com \
    --to=avagin@gmail.com \
    --cc=Dave.Martin@arm.com \
    --cc=catalin.marinas@arm.com \
    --cc=keno@juliacomputing.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oleg@redhat.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).