Skip to contentSkip to conversation

kcsan: add targeted watchpoint selftest

RFC PATCH In review Hui Su 1 participant 1 message Oct 14

Subsystems (3)
lore
Share & tools
lore

Hui Su

author
#0 lore raw

RFC PATCH 3/4 kcsan: add targeted watchpoint selftest

Test targeted address matching for exact and containing access ranges, adjacent ranges, zero-sized accesses, and boundary addresses at ULONG_MAX. These cases cover subtraction-based matching without runtime debugfs setup.

---
 kernel/kcsan/selftest.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
kernel/kcsan/selftest.c +29 -0
index 84a1200271af..8da1ba2ba25a 100644
@@ -12,6 +12,7 @@
 #include <linux/init.h>
 #include <linux/kcsan-checks.h>
 #include <linux/kernel.h>
+#include <linux/limits.h>
 #include <linux/printk.h>
 #include <linux/random.h>
 #include <linux/sched.h>
@@ -101,6 +102,33 @@ static bool __init test_matching_access(void)
 	return true;
 }
 
+/* Test whether an access contains the selected target byte. */
+static bool __init test_target_matching(void)
+{
+	const unsigned long target = 0x1004;
+
+	if (WARN_ON(!kcsan_target_matches(target, 0x1004, 1)))
+		return false;
+	if (WARN_ON(!kcsan_target_matches(target, 0x1000, 8)))
+		return false;
+	if (WARN_ON(kcsan_target_matches(target, 0x1000, 4)))
+		return false;
+	if (WARN_ON(kcsan_target_matches(target, 0x1005, 1)))
+		return false;
+	if (WARN_ON(kcsan_target_matches(target, 0x1004, 0)))
+		return false;
+	if (WARN_ON(!kcsan_target_matches(ULONG_MAX, ULONG_MAX, 1)))
+		return false;
+	if (WARN_ON(!kcsan_target_matches(ULONG_MAX, ULONG_MAX - 3, 4)))
+		return false;
+	if (WARN_ON(kcsan_target_matches(ULONG_MAX, ULONG_MAX - 1, 1)))
+		return false;
+	if (WARN_ON(kcsan_target_matches(0, ULONG_MAX - 1, 4)))
+		return false;
+
+	return true;
+}
+
 /*
  * Correct memory barrier instrumentation is critical to avoiding false
  * positives: simple test to check at boot certain barriers are always properly
@@ -251,6 +279,7 @@ static int __init kcsan_selftest(void)
 
 	RUN_TEST(test_encode_decode);
 	RUN_TEST(test_matching_access);
+	RUN_TEST(test_target_matching);
 	RUN_TEST(test_barrier);
 
 	pr_info("selftest: %d/%d tests passed\n", passed, total);
Signature · 1 line
2.55.0