[PATCH 3/3] selftests: riscv: Check vector ptrace payload and header-only writes
From: Ze Huang
Date: Sun Dec 06 2026 - 05:06:24 EST
The vstate_ptrace test checks only the first 32-bit element of v31
in the NT_RISCV_VECTOR payload and does not exercise header-only
writes.
Add a test to verify that patterns written to all 32 vector registers
remain intact after tracee resumes. Then invert the userspace payload
and verify that a header-only SETREGSET changes vl without altering the
registers.
Signed-off-by: Ze Huang <ze.huang@xxxxxxxxxxxxxxxx>
---
.../selftests/riscv/vector/validate_v_ptrace.c | 115 +++++++++++++++++++++
1 file changed, 115 insertions(+)
diff --git a/tools/testing/selftests/riscv/vector/validate_v_ptrace.c b/tools/testing/selftests/riscv/vector/validate_v_ptrace.c
index b1703094ff78..5eb493df801f 100644
--- a/tools/testing/selftests/riscv/vector/validate_v_ptrace.c
+++ b/tools/testing/selftests/riscv/vector/validate_v_ptrace.c
@@ -362,6 +362,121 @@ TEST(ptrace_v_syscall_clobbering)
}
}
+TEST(ptrace_v_full_and_header_update)
+{
+ pid_t pid;
+
+ if (!is_vector_supported() && !is_xtheadvector_supported())
+ SKIP(return, "Vector not supported");
+
+ pid = fork();
+ ASSERT_LE(0, pid)
+ TH_LOG("fork: %m");
+
+ if (pid == 0) {
+ unsigned long vl;
+
+ ASSERT_EQ(0, ptrace(PTRACE_TRACEME, 0, NULL, NULL))
+ TH_LOG("PTRACE_TRACEME: %m");
+
+ if (is_xtheadvector_supported()) {
+ /* vsetvli t4, x0, e16, m2, d1 */
+ asm volatile (
+ ".4byte 0b00000000010100000111111011010111\n"
+ "mv %[new_vl], t4\n"
+ : [new_vl] "=r" (vl) : : "t4");
+ } else {
+ asm volatile (
+ ".option push\n"
+ ".option arch, +zve32x\n"
+ "vsetvli %[new_vl], x0, e16, m2, tu, mu\n"
+ ".option pop\n"
+ : [new_vl] "=r"(vl) : : );
+ }
+
+ asm volatile (
+ ".option push\n"
+ ".option norvc\n"
+ "ebreak\n"
+ "nop\n"
+ "ebreak\n"
+ "nop\n"
+ "ebreak\n"
+ ".option pop\n");
+ } else {
+ struct __riscv_v_regset_state *full_data;
+ struct __riscv_v_regset_state *read_data;
+ unsigned long vlenb = get_vr_len();
+ size_t regset_size;
+ size_t payload_size = vlenb * 32;
+ struct iovec iov;
+ int status;
+ size_t reg, byte;
+
+ /* Wait for the 1st ebreak. */
+ ASSERT_EQ(pid, waitpid(pid, &status, 0));
+ ASSERT_TRUE(WIFSTOPPED(status));
+ ASSERT_EQ(SIGTRAP, WSTOPSIG(status));
+
+ regset_size = sizeof(*full_data) + payload_size;
+ full_data = calloc(1, regset_size);
+ read_data = calloc(1, regset_size);
+ ASSERT_NE(NULL, full_data);
+ ASSERT_NE(NULL, read_data);
+
+ iov.iov_base = full_data;
+ iov.iov_len = sizeof(*full_data);
+
+ /* Preserve the tracee's vector configuration for the full write. */
+ ASSERT_EQ(0, ptrace(PTRACE_GETREGSET, pid, NT_RISCV_VECTOR, &iov));
+ ASSERT_EQ(sizeof(*full_data), iov.iov_len);
+
+ /* Use distinct patterns to detect register swaps. */
+ for (reg = 0; reg < 32; reg++)
+ for (byte = 0; byte < vlenb; byte++)
+ full_data->vreg[reg * vlenb + byte] =
+ (char)(reg + byte * 37 + (byte >> 8));
+
+ iov.iov_len = regset_size;
+ ASSERT_EQ(0, ptrace(PTRACE_SETREGSET, pid, NT_RISCV_VECTOR, &iov));
+
+ continue_to_next_ebreak(_metadata, pid);
+
+ iov.iov_base = read_data;
+ iov.iov_len = regset_size;
+ ASSERT_EQ(0, ptrace(PTRACE_GETREGSET, pid, NT_RISCV_VECTOR, &iov));
+
+ ASSERT_EQ(regset_size, iov.iov_len);
+ ASSERT_EQ(0, memcmp(full_data->vreg, read_data->vreg, payload_size))
+ TH_LOG("Full vector payload did not round-trip");
+
+ /*
+ * Change vl to verify the header write takes effect. Invert the
+ * payload beyond iov_len so an unintended copy is detectable.
+ */
+ ASSERT_NE(1UL, read_data->vl);
+ read_data->vl = 1;
+ for (byte = 0; byte < payload_size; byte++)
+ read_data->vreg[byte] = (char)~(unsigned char)full_data->vreg[byte];
+ iov.iov_len = sizeof(*read_data);
+ ASSERT_EQ(0, ptrace(PTRACE_SETREGSET, pid, NT_RISCV_VECTOR, &iov));
+
+ continue_to_next_ebreak(_metadata, pid);
+
+ iov.iov_base = read_data;
+ iov.iov_len = regset_size;
+ ASSERT_EQ(0, ptrace(PTRACE_GETREGSET, pid, NT_RISCV_VECTOR, &iov));
+ ASSERT_EQ(regset_size, iov.iov_len);
+ EXPECT_EQ(1UL, read_data->vl);
+ EXPECT_EQ(0, memcmp(full_data->vreg, read_data->vreg, payload_size))
+ TH_LOG("Header-only write changed the vector payload");
+
+ free(full_data);
+ free(read_data);
+ ASSERT_EQ(0, kill(pid, SIGKILL));
+ }
+}
+
FIXTURE(v_csr_invalid)
{
};
--
2.56.1