kunit: expect failures from dynamic analysis tools
Subsystems (4)
PATCH 1/2 kunit: expect failures from dynamic analysis tools
Adds support to KUnit for failure expectation for specific tools. Uses named KUnit resources to keep track of whether or not a failure expectation has been set by the user.
- Adds a generic KUNIT_EXPECT_TOOL_FAIL macro that can expect failure from any supported tool that uses the same identifying name
- Adds kunit_fail_from_tool which is used to flag failures for specific tools.
Requires "kunit: suppport failure from dynamic analysis tools":
https://lore.kernel.org/linux-kselftest/[email protected]/
--- include/kunit/test-bug.h | 53 ++++++++++++++++++++++++++++++++++++++++ include/kunit/test.h | 5 ++++ lib/kunit/test.c | 46 +++++++++++++++++++++++++++++----- 3 files changed, 98 insertions(+), 6 deletions(-)
include/kunit/test-bug.h +53 -0
@@ -9,16 +9,69 @@#ifndef _KUNIT_TEST_BUG_H#define _KUNIT_TEST_BUG_H +/** +* struct kunit_expectation - represents expectations in KUnit, specifically +* used to keep track of failure expectations from analysis tools +*/ +struct kunit_expectation { +bool expected; +bool found; +}; +#if IS_ENABLED (CONFIG_KUNIT )extern void kunit_fail_current_test (void ); +/** +* kunit_fail_from_tool() - Fails the currently running KUnit tests under the +* given tool name. +* +* Note: Uses a named KUnit resource to track state. Do not use the name +* KUNIT_TOOL_{tool} for KUnit resources outside of here. +*/ +#define kunit_fail_from_tool (tool) \ + if (current->kunit_test) \ + kunit_tool_fail(current->kunit_test, #tool,\ + "KUNIT_TOOL_" #tool) + +/** +* kunit_tool_expectation() - Returns the kunit_expectation for the given +* tool. If it cannot find the expectation, it creates an expectation and +* returns it. +* +* Note: Uses a named KUnit resource to track state. Do not use the name +* KUNIT_TOOL_{tool} for KUnit resources outside of here. +*/ +#define kunit_tool_expectation (test, tool) \ + kunit_find_expectation(test, "KUNIT_TOOL_" #tool) + + +/** +* KUNIT_EXPECT_TOOL_FAIL() - Fails the currently running KUnit test if the +* condition does not cause an error within the given tool. +* +* Note: 'tool' must be consistent with the name specified in +* kunit_fail_from_tool(). If the tool fails KUnit using another name, KUnit +* will treat it as a separate tool. +*/ +#define KUNIT_EXPECT_TOOL_FAIL (test, condition, tool) do { \ + struct kunit_expectation *data = kunit_tool_expectation(test, tool);\ + data->expected = true; \ + data->found = false; \ + condition; \ + KUNIT_EXPECT_EQ(test, data->expected, data->found); \ + data->expected = false; \ + data->found = false; \ +} while (0) +#else static inline void kunit_fail_current_test (void ) { } +#define kunit_fail_from_tool (tool) do { } while (0) +#endif #endif /* _KUNIT_TEST_BUG_H */
include/kunit/test.h +5 -0
@@ -511,6 +511,11 @@ static inline int kunit_destroy_named_resource(struct kunit *test,*/ void kunit_remove_resource (struct kunit *test,struct kunit_resource *res); +void kunit_tool_fail (struct kunit *test,char *tool_name,char *resource_name); + +struct kunit_expectation *kunit_find_expectation (struct kunit *test, +char *resource_name); +/** * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*. * @test: The test context object.
lib/kunit/test.c +40 -6
64 lines not on this page · raw message
Signature · 1 line
2.28.0.297.g1956fa8f8d-goog
PATCH 2/2 kunit: port UBSAN tests to KUnit
From: Uriel Guajardo <[email protected]>
UBSAN tests are ported to the KUnit testing framework using the tool failure expectation feature added to KUnit.
I've commented out test_ubsan_divrem_overflow and test_ubsan_out_of_bounds since they both consistently crash the kernel. The former gives me a divide error and the latter gives a corrupted kernel stack error. If this is only on my end, let me know.
Kconfig file is reformatted according the KUnit naming guidelines:
https://lore.kernel.org/linux-kselftest/[email protected]/
Requires "kunit: UBSAN integration":
https://lore.kernel.org/linux-kselftest/[email protected]/
--- lib/Kconfig.ubsan | 12 ++++-- lib/Makefile | 2 +- lib/test_ubsan.c | 96 ++++++++++++++++++++++++----------------------- lib/ubsan.c | 2 +- 4 files changed, 59 insertions(+), 53 deletions(-)
lib/Kconfig.ubsan +8 -4
@@ -80,11 +80,15 @@ config UBSAN_ALIGNMENT Enabling this option on architectures that support unaligned accesses may produce a lot of false positives. -config TEST_UBSAN - tristate "Module for testing for undefined behavior detection" - depends on m +config UBSAN_KUNIT_TEST + tristate "Tests for undefined behavior detection" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS help - This is a test module for UBSAN. + This is a test suite for UBSAN using the KUnit testing framework. It triggers various undefined behavior, and detect it. + For more information on KUnit and unit tests in general, please refer + to the KUnit documentation in Documentation/dev-tools/kunit + endif # if UBSAN
lib/Makefile +1 -1
@@ -67,7 +67,7 @@ obj-$(CONFIG_TEST_IDA) += test_ida.o obj-$(CONFIG_TEST_KASAN) += test_kasan.o CFLAGS_test_kasan.o += -fno-builtin CFLAGS_test_kasan.o += $(call cc-disable-warning, vla) -obj-$(CONFIG_TEST_UBSAN) += test_ubsan.o +obj-$(CONFIG_UBSAN_KUNIT_TEST) += test_ubsan.o CFLAGS_test_ubsan.o += $(call cc-disable-warning, vla) UBSAN_SANITIZE_test_ubsan.o := y obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o
lib/test_ubsan.c +49 -47
178 lines not on this page · raw message
lib/ubsan.c +1 -1
@@ -138,7 +138,7 @@ static void ubsan_prologue(struct source_location *loc, const char *reason) { current->in_ubsan++; -kunit_fail_current_test (); +kunit_fail_from_tool (UBSAN );pr_err ("========================================" "========================================\n" ); pr_err("UBSAN: %s in %s:%d:%d\n", reason, loc->file_name,
Signature · 1 line
2.28.0.297.g1956fa8f8d-goog