From patchwork Mon Mar 22 22:50:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrei Vagin X-Patchwork-Id: 12156291 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.7 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 51946C433C1 for ; Mon, 22 Mar 2021 22:55:30 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D112861972 for ; Mon, 22 Mar 2021 22:55:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D112861972 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=248UsfHFL1f5x/dMldky7syCRB1RXPjpyy4QX0kswl8=; b=IDvNV9GFHrUPN5zpxOLj+sSVs tg3F4LqnwWZoOmW50LAMhnntTxxgmc6bNJkiT3nYzUbsIhCSm5iCVImm9yp3JPOJzd3SRRdZi7Om6 ya2mJa+t8/5jgjKRlWQxiaf7ymHDDMo+lV9+HTXzwOToxhYwFNTuIopen0LzW7uKmqB9lFt3N+SNt /h4hilnGA8eOl7k3LE0cJ2gnrY5wtIKCtSJJV68wlBfcwOMUyjmdwIAGJAMnPety+G9rS3aqzyT9O Ac0MJCUPwXbF0l42opLhvR9IQlHUK1cm76+McqKZZNidJzSSvYhBnzjecZneJGiVexo6pcn9Pblim AehAIbHOQ==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lOTQd-00ClFz-HX; Mon, 22 Mar 2021 22:53:43 +0000 Received: from mail-pf1-x433.google.com ([2607:f8b0:4864:20::433]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lOTQG-00ClAN-9C for linux-arm-kernel@lists.infradead.org; Mon, 22 Mar 2021 22:53:22 +0000 Received: by mail-pf1-x433.google.com with SMTP id c17so5341776pfn.6 for ; Mon, 22 Mar 2021 15:53:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=972JgSIF2IIo8wEKlvKAl0BK+0XizAKsSmH5/nQePh4=; b=kyFRzjmyQNZ2Bjps4L5M0EhjRIv/RBgAPaxob3JGH3c9LC2wAXReZaM5iNFpVBAp+p oJM5qZQjZZQwFcR8eFRhltYD68lbnwWZPjn28HPgNI2OgK13kT77b9RMobozUskZL8MW 41QvaM7ZoQUL+1lZpR1oJUqMhFDZWNFhAn5LCVnX5Sk6kiirqiUGjVTtwP3ncP5eXqM1 RGiuniUiRDmJ3gzjoZ/SvDXMr7ezlJ5hrJ6qwZTa9J/1niNMtKNhVe+1My9NiKTvGl6g t/wBDHOpO97ENzhqOhJVJ8JmiVo3IT2drAG4lpt/YSbN9yEi7VJfmFuA+58ZSoTuDpmC F1WA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=972JgSIF2IIo8wEKlvKAl0BK+0XizAKsSmH5/nQePh4=; b=CQvi8VsUfzadhfsmmyteABM4CuBOuzktDFhFKRFqQs1tSjjtxG9FtuUCd3VCVxs/8D 0ue8dhE6uS6AUXDgokCD1iGsBOzqCwRwXTHYYc4EDoIBDMlHC/nOah/CErrX8dFtW+Hr 2BfyG6dK0uA9K5DyjsZ0m0L4KIok5LIakAUMrkQqBGVOKh4GVuQ5TQw0pKMpk610/JnZ dB7wkc+t+K2HW/8TeF7nKjfEONx+9vVZdrpRaaBa9mRqu3OTemSvDAP0PYH4u42F29yj SYBT66txV7INPC8ITXZqxfsjNEHz7PUWYrDLIQ/OhpSzQGsJxmBiyiY+kX5tLLW4eU0O 1I0A== X-Gm-Message-State: AOAM531+ka55arx40xem87Q2jv6RE91CcsaP4dHI/rtkv/4onLqMHaxf sLVt8DAGbJW2DnYt3qq5jgy5lt5Cku8mew== X-Google-Smtp-Source: ABdhPJz52tT2nDR6QszbXNEnhRIexqy8hazMx7QWwYZkK74s9rdjv9EC4jYt7FLr0O944sg0Cr404w== X-Received: by 2002:a17:903:1c3:b029:e6:a15:751f with SMTP id e3-20020a17090301c3b02900e60a15751fmr1888188plh.44.1616453598632; Mon, 22 Mar 2021 15:53:18 -0700 (PDT) Received: from laptop.hsd1.wa.comcast.net ([2601:600:8500:5f14:d627:c51e:516e:a105]) by smtp.gmail.com with ESMTPSA id j13sm8878211pfn.103.2021.03.22.15.53.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Mar 2021 15:53:17 -0700 (PDT) From: Andrei Vagin To: Will Deacon , Catalin Marinas Cc: Oleg Nesterov , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Andrei Vagin , Dave Martin , Keno Fischer Subject: [PATCH 3/4] selftest/arm64/ptrace: add a test for orig_x0 Date: Mon, 22 Mar 2021 15:50:52 -0700 Message-Id: <20210322225053.428615-4-avagin@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210322225053.428615-1-avagin@gmail.com> References: <20210322225053.428615-1-avagin@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210322_225320_398543_99861D23 X-CRM114-Status: GOOD ( 20.99 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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 --- 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 +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#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 = ®s, + .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; +} +