Skip to contentSkip to conversation

selftests/breakpoints: fix optimization and aliasing issues

PATCH New Eva Kurchatova 1 participant 1 message Apr 29 Kselftest

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/breakpoints: fix optimization and aliasing issues

When compiled with optimizations, the compiler optimizes out loads and stores to dummy_var and the trapped flag, failing the watchpoint tests.

Use READ_ONCE()/WRITE_ONCE() macros to force these accesses to occur, similar to other selftests and kernel itself.

Also fix unused/uninitialized variable warnings and loop iterator types.

Set CFLAGS += -O2 -fno-strict-aliasing -Wall -Wextra in Makefile to reinforce this and catch future regressions at build time.

Strict aliasing is disabled to match the kernel build practice, as tests intentionally cast between incompatible pointer types.

---
 tools/testing/selftests/breakpoints/Makefile  |  2 +-
 .../selftests/breakpoints/breakpoint_test.c   | 63 ++++++++-----------
 .../breakpoints/breakpoint_test_arm64.c       | 21 ++++---
 3 files changed, 39 insertions(+), 47 deletions(-)
tools/testing/selftests/breakpoints/Makefile +1 -1
index 0b8f5acf7c78..27d69d7006e5 100644
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 # Taken from perf makefile
+CFLAGS += -O2 -fno-strict-aliasing -Wall -Wextra
 ARCH ?= $(shell uname -m 2>/dev/null || echo not)
 override ARCH := $(shell echo $(ARCH) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
 
@@ -13,4 +14,3 @@ TEST_GEN_PROGS += breakpoint_test_arm64
 endif
 
 include ../lib.mk
-
tools/testing/selftests/breakpoints/breakpoint_test.c +26 -37

163 lines not on this page · raw message

tools/testing/selftests/breakpoints/breakpoint_test_arm64.c +12 -9
index 5fc0f37f3fd4..b0b908f4de9e 100644
@@ -28,11 +28,14 @@
 
 #include "kselftest.h"
 
-static volatile uint8_t var[96] __attribute__((__aligned__(32)));
+/* Force watchpoint access to actually occur */
+#define WRITE_ONCE(x, val) (*(volatile typeof(x) *)&(x) = (val))
+
+static uint8_t var[96] __attribute__((__aligned__(32)));
 
 static void child(int size, int wr)
 {
-	volatile uint8_t *addr = &var[32 + wr];
+	uint8_t *addr = &var[32 + wr];
 
 	if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != 0) {
 		ksft_print_msg(
@@ -56,16 +59,16 @@ static void child(int size, int wr)
 
 	switch (size) {
 	case 1:
-		*addr = 47;
+		WRITE_ONCE(*addr, 47);
 		break;
 	case 2:
-		*(uint16_t *)addr = 47;
+		WRITE_ONCE(*(uint16_t *)addr, 47);
 		break;
 	case 4:
-		*(uint32_t *)addr = 47;
+		WRITE_ONCE(*(uint32_t *)addr, 47);
 		break;
 	case 8:
-		*(uint64_t *)addr = 47;
+		WRITE_ONCE(*(uint64_t *)addr, 47);
 		break;
 	case 16:
 		__asm__ volatile ("stp x29, x30, %0" : "=m" (addr[0]));
@@ -80,7 +83,7 @@ static void child(int size, int wr)
 
 static bool set_watchpoint(pid_t pid, int size, int wp)
 {
-	const volatile uint8_t *addr = &var[32 + wp];
+	const uint8_t *addr = &var[32 + wp];
 	const int offset = (uintptr_t)addr % 8;
 	const unsigned int byte_mask = ((1 << size) - 1) << offset;
 	const unsigned int type = 2; /* Write */
@@ -194,11 +197,11 @@ static bool run_test(int wr_size, int wp_size, int wr, int wp)
 
 static void sigalrm(int sig)
 {
+	(void)sig;
 }
 
-int main(int argc, char **argv)
+int main(void)
 {
-	int opt;
 	bool succeeded = true;
 	struct sigaction act;
 	int wr, wp, size;
Signature · 1 line
2.54.0