All of lore.kernel.org
 help / color / mirror / Atom feed
From: Michael Ellerman <mpe@ellerman.id.au>
To: <linuxppc-dev@lists.ozlabs.org>
Subject: [PATCH 12/13] selftests/powerpc/ptrace: Use more interesting values
Date: Tue, 28 Jun 2022 00:02:38 +1000	[thread overview]
Message-ID: <20220627140239.2464900-12-mpe@ellerman.id.au> (raw)
In-Reply-To: <20220627140239.2464900-1-mpe@ellerman.id.au>

The ptrace-gpr test uses fixed values to test that registers can be
read/written via ptrace. In particular it sets all GPRs to 1, which
means the test could miss some types of bugs - eg. if the kernel was
only returning the low word.

So generate some random values at startup and use those instead.

Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
---
 .../selftests/powerpc/ptrace/ptrace-gpr.c     | 71 +++++++++++++++----
 1 file changed, 57 insertions(+), 14 deletions(-)

diff --git a/tools/testing/selftests/powerpc/ptrace/ptrace-gpr.c b/tools/testing/selftests/powerpc/ptrace/ptrace-gpr.c
index b574ea26395c..c5dcb8c02616 100644
--- a/tools/testing/selftests/powerpc/ptrace/ptrace-gpr.c
+++ b/tools/testing/selftests/powerpc/ptrace/ptrace-gpr.c
@@ -7,18 +7,18 @@
 #include "ptrace.h"
 #include "ptrace-gpr.h"
 #include "reg.h"
+#include <time.h>
 
 /* Tracer and Tracee Shared Data */
 int shm_id;
 int *cptr, *pptr;
 
-double a = FPR_1;
-double b = FPR_2;
-double c = FPR_3;
-
 extern void gpr_child_loop(int *read_flag, int *write_flag,
 			   unsigned long *gpr_buf, double *fpr_buf);
 
+unsigned long child_gpr_val, parent_gpr_val;
+double child_fpr_val, parent_fpr_val;
+
 static int child(void)
 {
 	unsigned long gpr_buf[32];
@@ -30,16 +30,16 @@ static int child(void)
 	memset(fpr_buf, 0, sizeof(fpr_buf));
 
 	for (i = 0; i < 32; i++) {
-		gpr_buf[i] = GPR_1;
-		fpr_buf[i] = a;
+		gpr_buf[i] = child_gpr_val;
+		fpr_buf[i] = child_fpr_val;
 	}
 
 	gpr_child_loop(&cptr[0], &cptr[1], gpr_buf, fpr_buf);
 
 	shmdt((void *)cptr);
 
-	FAIL_IF(validate_gpr(gpr_buf, GPR_3));
-	FAIL_IF(validate_fpr_double(fpr_buf, c));
+	FAIL_IF(validate_gpr(gpr_buf, parent_gpr_val));
+	FAIL_IF(validate_fpr_double(fpr_buf, parent_fpr_val));
 
 	return 0;
 }
@@ -47,24 +47,67 @@ static int child(void)
 int trace_gpr(pid_t child)
 {
 	unsigned long gpr[18];
-	__u64 fpr[32];
+	__u64 tmp, fpr[32];
 
 	FAIL_IF(start_trace(child));
 	FAIL_IF(show_gpr(child, gpr));
-	FAIL_IF(validate_gpr(gpr, GPR_1));
+	FAIL_IF(validate_gpr(gpr, child_gpr_val));
 	FAIL_IF(show_fpr(child, fpr));
-	FAIL_IF(validate_fpr(fpr, FPR_1_REP));
-	FAIL_IF(write_gpr(child, GPR_3));
-	FAIL_IF(write_fpr(child, FPR_3_REP));
+
+	memcpy(&tmp, &child_fpr_val, sizeof(tmp));
+	FAIL_IF(validate_fpr(fpr, tmp));
+
+	FAIL_IF(write_gpr(child, parent_gpr_val));
+
+	memcpy(&tmp, &parent_fpr_val, sizeof(tmp));
+	FAIL_IF(write_fpr(child, tmp));
+
 	FAIL_IF(stop_trace(child));
 
 	return TEST_PASS;
 }
 
+#ifndef __LONG_WIDTH__
+#define __LONG_WIDTH__ (sizeof(long) * 8)
+#endif
+
+static uint64_t rand_reg(void)
+{
+	uint64_t result;
+	long r;
+
+	r = random();
+
+	// Small values are typical
+	result = r & 0xffff;
+	if (r & 0x10000)
+		return result;
+
+	// Pointers tend to have high bits set
+	result |= random() << (__LONG_WIDTH__ - 31);
+	if (r & 0x100000)
+		return result;
+
+	// And sometimes we want a full 64-bit value
+	result ^= random() << 16;
+
+	return result;
+}
+
 int ptrace_gpr(void)
 {
-	pid_t pid;
+	unsigned long seed;
 	int ret, status;
+	pid_t pid;
+
+	seed = getpid() ^ time(NULL);
+	printf("srand(%lu)\n", seed);
+	srand(seed);
+
+	child_gpr_val = rand_reg();
+	child_fpr_val = rand_reg();
+	parent_gpr_val = rand_reg();
+	parent_fpr_val = rand_reg();
 
 	shm_id = shmget(IPC_PRIVATE, sizeof(int) * 2, 0777|IPC_CREAT);
 	pid = fork();
-- 
2.35.3


  parent reply	other threads:[~2022-06-27 14:09 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-27 14:02 [PATCH 01/13] selftests/powerpc: Ensure 16-byte stack pointer alignment Michael Ellerman
2022-06-27 14:02 ` [PATCH 02/13] selftests/powerpc/ptrace: Set LOCAL_HDRS Michael Ellerman
2022-06-27 14:02 ` [PATCH 03/13] selftests/powerpc/ptrace: Split CFLAGS better Michael Ellerman
2022-06-27 14:02 ` [PATCH 04/13] selftests/powerpc: Don't save CR by default in asm helpers Michael Ellerman
2022-06-27 14:02 ` [PATCH 05/13] selftests/powerpc: Don't save TOC " Michael Ellerman
2022-06-27 14:02 ` [PATCH 06/13] selftests/powerpc: Add 32-bit support to " Michael Ellerman
2022-06-27 14:02 ` [PATCH 07/13] selftests/powerpc/ptrace: Drop unused load_fpr_single_precision() Michael Ellerman
2022-06-27 14:02 ` [PATCH 08/13] selftests/powerpc/ptrace: Convert to load/store doubles Michael Ellerman
2022-06-27 14:02 ` [PATCH 09/13] selftests/powerpc/ptrace: Build the ptrace-gpr test as 32-bit when possible Michael Ellerman
2022-06-27 14:02 ` [PATCH 10/13] selftests/powerpc/ptrace: Do more of ptrace-gpr in asm Michael Ellerman
2022-06-27 14:02 ` [PATCH 11/13] selftests/powerpc/ptrace: Make child errors more obvious Michael Ellerman
2022-06-27 14:02 ` Michael Ellerman [this message]
2022-06-27 14:02 ` [PATCH 13/13] selftests/powerpc/ptrace: Add peek/poke of FPRs Michael Ellerman
2022-07-29 13:02 ` [PATCH 01/13] selftests/powerpc: Ensure 16-byte stack pointer alignment Michael Ellerman

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=20220627140239.2464900-12-mpe@ellerman.id.au \
    --to=mpe@ellerman.id.au \
    --cc=linuxppc-dev@lists.ozlabs.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.