kasan: test: Improve failure message in KUNIT_EXPECT_KASAN_FAIL()
Subsystems (2)
PATCH v2 1/1 kasan: test: Improve failure message in KUNIT_EXPECT_KASAN_FAIL()
The KUNIT_EXPECT_KASAN_FAIL() macro currently uses KUNIT_EXPECT_EQ() to compare fail_data.report_expected and fail_data.report_found. This always gave a somewhat useless error message on failure, but the addition of extra compile-time checking with READ_ONCE() has caused it to get much longer, and be truncated before anything useful is displayed.
Instead, just check fail_data.report_found by hand (we've just set report_expected to 'true'), and print a better failure message with KUNIT_FAIL(). Because of this, report_expected is no longer used anywhere, and can be removed.
Beforehand, a failure in:KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]); would have looked like:
[22:00:34] [FAILED] vmalloc_oob
[22:00:34] # vmalloc_oob: EXPECTATION FAILED at lib/test_kasan.c:991
[22:00:34] Expected ({ do { extern void __compiletime_assert_705(void) __attribute__((__error__("Unsupported access size for {READ,WRITE}_ONCE()."))); if (!((sizeof(fail_data.report_expected) == sizeof(char) || sizeof(fail_data.repp
[22:00:34] not ok 45 - vmalloc_oobWith this change, it instead looks like:
[22:04:04] [FAILED] vmalloc_oob
[22:04:04] # vmalloc_oob: EXPECTATION FAILED at lib/test_kasan.c:993 [22:04:04] KASAN failure expected in "((volatile char *)area)[3100]", but none occurred [22:04:04] not ok 45 - vmalloc_oob
---
Changes since v1:
https://groups.google.com/g/kasan-dev/c/CbabdwoXGlE
- Remove fail_data.report_expected now that it's unused.
- Use '!' instead of '== false' in the comparison.
- Minor typo fixes in the commit message.
The test failure being used as an example is tracked in:
https://bugzilla.kernel.org/show_bug.cgi?id=213335
Cheers,
-- David
include/linux/kasan.h | 1 - lib/test_kasan.c | 11 +++++------ 2 files changed, 5 insertions(+), 7 deletions(-)
include/linux/kasan.h +0 -1
@@ -17,7 +17,6 @@ struct task_struct;/* kasan_data struct is used in KUnit tests for KASAN expected failures */ struct kunit_kasan_expectation { -bool report_expected;bool report_found; };
lib/test_kasan.c +5 -6
@@ -55,7 +55,6 @@ static int kasan_test_init(struct kunit *test) multishot =kasan_save_enable_multi_shot ();kasan_set_tagging_report_once (false); fail_data.report_found = false; - fail_data.report_expected = false;kunit_add_named_resource (test,NULL ,NULL , &resource,"kasan_data" , &fail_data);return 0 ;
@@ -94,20 +93,20 @@ static void kasan_test_exit(struct kunit *test) !kasan_async_mode_enabled()) \migrate_disable (); \KUNIT_EXPECT_FALSE (test,READ_ONCE (fail_data.report_found)); \ -WRITE_ONCE (fail_data.report_expected, true); \barrier (); \ expression; \barrier (); \ -KUNIT_EXPECT_EQ (test, \ -READ_ONCE (fail_data.report_expected), \ -READ_ONCE (fail_data.report_found)); \ +if (!READ_ONCE (fail_data.report_found)) { \ +KUNIT_FAIL (test,KUNIT_SUBTEST_INDENT "KASAN failure " \ + "expected in \"" #expression \ +"\", but none occurred" ); \ + } \if (IS_ENABLED (CONFIG_KASAN_HW_TAGS )) { \if (READ_ONCE (fail_data.report_found)) \kasan_enable_tagging_sync (); \migrate_enable (); \ } \WRITE_ONCE (fail_data.report_found, false); \ -WRITE_ONCE (fail_data.report_expected, false); \ } while (0)#define KASAN_TEST_NEEDS_CONFIG_ON (test, config) do { \
Signature · 1 line
2.32.0.rc1.229.g3e70b5a671-goog
Andrey Konovalov
reviewerQuoted 20 lines · David Gow' via kasan-dev
The
KUNIT_EXPECT_KASAN_FAIL()macro currently usesKUNIT_EXPECT_EQ()to compare fail_data.report_expected and fail_data.report_found. This always gave a somewhat useless error message on failure, but the addition of extra compile-time checking withREAD_ONCE()has caused it to get much longer, and be truncated before anything useful is displayed.Instead, just check fail_data.report_found by hand (we've just set
report_expectedto 'true'), and print a better failure message withKUNIT_FAIL(). Because of this,report_expectedis no longer used anywhere, and can be removed.Beforehand, a failure in:
KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)area)[3100]); would have looked like:
[22:00:34] [FAILED]vmalloc_oob[22:00:34] # vmalloc_oob: EXPECTATION FAILED at lib/test_kasan.c:991 [22:00:34] Expected ({ do { extern void __compiletime_assert_705(void) __attribute__((__error__("Unsupported access size for {READ,WRITE}_ONCE()."))); if (!((sizeof(fail_data.report_expected) == sizeof(char) || sizeof(fail_data.repp [22:00:34] not ok 45 - vmalloc_oob
Quoted 7 lines
With this change, it instead looks like:
[22:04:04] [FAILED]vmalloc_oob[22:04:04] # vmalloc_oob: EXPECTATION FAILED at lib/test_kasan.c:993 [22:04:04] KASAN failure expected in "((volatile char *)area)[3100]", but none occurred [22:04:04] not ok 45 - vmalloc_oobSigned-off-by David Gow <[email protected]>
Hi David,
Please also update the failure message in Documentation/dev-tools/kasan.rst.
Thank you!
Quoted 74 lines
---
Changes since v1:
https://groups.google.com/g/kasan-dev/c/CbabdwoXGlE
- Remove fail_data.report_expected now that it's unused.
- Use '!' instead of '== false' in the comparison.
- Minor typo fixes in the commit message.The test failure being used as an example is tracked in:
https://bugzilla.kernel.org/show_bug.cgi?id=213335Cheers,
-- Davidinclude/linux/kasan.h | 1 - lib/test_kasan.c | 11 +++++------ 2 files changed, 5 insertions(+), 7 deletions(-)include/linux/kasan.h +0
-1@@ -17,7 +17,6 @@ struct task_struct;/* kasan_data struct is used in KUnit tests for KASAN expected failures */ struct kunit_kasan_expectation { -bool report_expected;bool report_found; };lib/test_kasan.c +5
-7@@ -55,7 +55,6 @@ static int kasan_test_init(struct kunit *test) multishot =kasan_save_enable_multi_shot ();kasan_set_tagging_report_once (false); fail_data.report_found = false; - fail_data.report_expected = false;kunit_add_named_resource (test,NULL ,NULL , &resource,"kasan_data" , &fail_data);return 0 ;@@ -94,20 +93,20 @@ static void kasan_test_exit(struct kunit *test) !kasan_async_mode_enabled()) \migrate_disable (); \KUNIT_EXPECT_FALSE (test,READ_ONCE (fail_data.report_found)); \ -WRITE_ONCE (fail_data.report_expected, true); \barrier (); \ expression; \barrier (); \ -KUNIT_EXPECT_EQ (test, \ -READ_ONCE (fail_data.report_expected), \ -READ_ONCE (fail_data.report_found)); \ +if (!READ_ONCE (fail_data.report_found)) { \ +KUNIT_FAIL (test,KUNIT_SUBTEST_INDENT "KASAN failure " \ + "expected in \"" #expression \ +"\", but none occurred" ); \ + } \if (IS_ENABLED (CONFIG_KASAN_HW_TAGS )) { \if (READ_ONCE (fail_data.report_found)) \kasan_enable_tagging_sync (); \migrate_enable (); \ } \WRITE_ONCE (fail_data.report_found, false); \ -WRITE_ONCE (fail_data.report_expected, false); \ } while (0)#define KASAN_TEST_NEEDS_CONFIG_ON (test, config) do { \ --2.32.0.rc1.229.g3e70b5a671-goog
--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20210605034821.2098034-1-davidgow%40google.com.