]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
compiler-context-analysis: Add test stub
authorMarco Elver <elver@google.com>
Fri, 19 Dec 2025 15:39:52 +0000 (16:39 +0100)
committerPeter Zijlstra <peterz@infradead.org>
Mon, 5 Jan 2026 15:43:27 +0000 (16:43 +0100)
Add a simple test stub where we will add common supported patterns that
should not generate false positives for each new supported context lock.

Signed-off-by: Marco Elver <elver@google.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://patch.msgid.link/20251219154418.3592607-4-elver@google.com
lib/Kconfig.debug
lib/Makefile
lib/test_context-analysis.c [new file with mode: 0644]

index cd557e7653a410742a380cf2a0b4a66be1c04e30..8ca42526ee43ad23f2fa5055521b81bfa842a75e 100644 (file)
@@ -2835,6 +2835,20 @@ config LINEAR_RANGES_TEST
 
          If unsure, say N.
 
+config CONTEXT_ANALYSIS_TEST
+       bool "Compiler context-analysis warnings test"
+       depends on EXPERT
+       help
+         This builds the test for compiler-based context analysis. The test
+         does not add executable code to the kernel, but is meant to test that
+         common patterns supported by the analysis do not result in false
+         positive warnings.
+
+         When adding support for new context locks, it is strongly recommended
+         to add supported patterns to this test.
+
+         If unsure, say N.
+
 config CMDLINE_KUNIT_TEST
        tristate "KUnit test for cmdline API" if !KUNIT_ALL_TESTS
        depends on KUNIT
index aaf677cf4527e8f0889b06d5ff6746a1dd1263a7..89defefbf6c07ee9916cc9b12a1e3e24fca5db7a 100644 (file)
@@ -331,4 +331,7 @@ obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o
 
 obj-$(CONFIG_FIRMWARE_TABLE) += fw_table.o
 
+CONTEXT_ANALYSIS_test_context-analysis.o := y
+obj-$(CONFIG_CONTEXT_ANALYSIS_TEST) += test_context-analysis.o
+
 subdir-$(CONFIG_FORTIFY_SOURCE) += test_fortify
diff --git a/lib/test_context-analysis.c b/lib/test_context-analysis.c
new file mode 100644 (file)
index 0000000..68f075d
--- /dev/null
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Compile-only tests for common patterns that should not generate false
+ * positive errors when compiled with Clang's context analysis.
+ */
+
+#include <linux/build_bug.h>
+
+/*
+ * Test that helper macros work as expected.
+ */
+static void __used test_common_helpers(void)
+{
+       BUILD_BUG_ON(context_unsafe(3) != 3); /* plain expression */
+       BUILD_BUG_ON(context_unsafe((void)2; 3) != 3); /* does not swallow semi-colon */
+       BUILD_BUG_ON(context_unsafe((void)2, 3) != 3); /* does not swallow commas */
+       context_unsafe(do { } while (0)); /* works with void statements */
+}