Skip to content

Commit 83c4e7a

Browse files
Patricia Alfonsotorvalds
authored andcommitted
KUnit: KASAN Integration
Integrate KASAN into KUnit testing framework. - Fail tests when KASAN reports an error that is not expected - Use KUNIT_EXPECT_KASAN_FAIL to expect a KASAN error in KASAN tests - Expected KASAN reports pass tests and are still printed when run without kunit_tool (kunit_tool still bypasses the report due to the test passing) - KUnit struct in current task used to keep track of the current test from KASAN code Make use of "[PATCH v3 kunit-next 1/2] kunit: generalize kunit_resource API beyond allocated resources" and "[PATCH v3 kunit-next 2/2] kunit: add support for named resources" from Alan Maguire [1] - A named resource is added to a test when a KASAN report is expected - This resource contains a struct for kasan_data containing booleans representing if a KASAN report is expected and if a KASAN report is found [1] (https://lore.kernel.org/linux-kselftest/1583251361-12748-1-git-send-email-alan.maguire@oracle.com/T/#t) Signed-off-by: Patricia Alfonso <trishalfonso@google.com> Signed-off-by: David Gow <davidgow@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Tested-by: Andrey Konovalov <andreyknvl@google.com> Reviewed-by: Andrey Konovalov <andreyknvl@google.com> Reviewed-by: Dmitry Vyukov <dvyukov@google.com> Acked-by: Brendan Higgins <brendanhiggins@google.com> Cc: Andrey Ryabinin <aryabinin@virtuozzo.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Juri Lelli <juri.lelli@redhat.com> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Shuah Khan <shuah@kernel.org> Cc: Vincent Guittot <vincent.guittot@linaro.org> Link: https://lkml.kernel.org/r/20200915035828.570483-3-davidgow@google.com Link: https://lkml.kernel.org/r/20200910070331.3358048-3-davidgow@google.com Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 393824f commit 83c4e7a

5 files changed

Lines changed: 96 additions & 7 deletions

File tree

‎include/kunit/test.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ struct kunit {
224224
struct list_head resources; /* Protected by lock. */
225225
};
226226

227+
static inline void kunit_set_failure(struct kunit *test)
228+
{
229+
WRITE_ONCE(test->success, false);
230+
}
231+
227232
void kunit_init_test(struct kunit *test, const char *name, char *log);
228233

229234
int kunit_run_tests(struct kunit_suite *suite);

‎include/linux/kasan.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ struct task_struct;
1414
#include <linux/pgtable.h>
1515
#include <asm/kasan.h>
1616

17+
/* kasan_data struct is used in KUnit tests for KASAN expected failures */
18+
struct kunit_kasan_expectation {
19+
bool report_expected;
20+
bool report_found;
21+
};
22+
1723
extern unsigned char kasan_early_shadow_page[PAGE_SIZE];
1824
extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE];
1925
extern pmd_t kasan_early_shadow_pmd[PTRS_PER_PMD];

‎lib/kunit/test.c‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,12 @@
1010
#include <linux/kernel.h>
1111
#include <linux/kref.h>
1212
#include <linux/sched/debug.h>
13+
#include <linux/sched.h>
1314

1415
#include "debugfs.h"
1516
#include "string-stream.h"
1617
#include "try-catch-impl.h"
1718

18-
static void kunit_set_failure(struct kunit *test)
19-
{
20-
WRITE_ONCE(test->success, false);
21-
}
22-
2319
static void kunit_print_tap_version(void)
2420
{
2521
static bool kunit_has_printed_tap_version;
@@ -288,6 +284,10 @@ static void kunit_try_run_case(void *data)
288284
struct kunit_suite *suite = ctx->suite;
289285
struct kunit_case *test_case = ctx->test_case;
290286

287+
#if (IS_ENABLED(CONFIG_KASAN) && IS_ENABLED(CONFIG_KUNIT))
288+
current->kunit_test = test;
289+
#endif /* IS_ENABLED(CONFIG_KASAN) && IS_ENABLED(CONFIG_KUNIT) */
290+
291291
/*
292292
* kunit_run_case_internal may encounter a fatal error; if it does,
293293
* abort will be called, this thread will exit, and finally the parent
@@ -602,6 +602,9 @@ void kunit_cleanup(struct kunit *test)
602602
spin_unlock(&test->lock);
603603
kunit_remove_resource(test, res);
604604
}
605+
#if (IS_ENABLED(CONFIG_KASAN) && IS_ENABLED(CONFIG_KUNIT))
606+
current->kunit_test = NULL;
607+
#endif /* IS_ENABLED(CONFIG_KASAN) && IS_ENABLED(CONFIG_KUNIT)*/
605608
}
606609
EXPORT_SYMBOL_GPL(kunit_cleanup);
607610

‎lib/test_kasan.c‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
#include <asm/page.h>
2525

26+
#include <kunit/test.h>
27+
2628
#include "../mm/kasan/kasan.h"
2729

2830
#define OOB_TAG_OFF (IS_ENABLED(CONFIG_KASAN_GENERIC) ? 0 : KASAN_SHADOW_SCALE_SIZE)
@@ -32,14 +34,55 @@
3234
* are not eliminated as dead code.
3335
*/
3436

35-
int kasan_int_result;
3637
void *kasan_ptr_result;
38+
int kasan_int_result;
39+
40+
static struct kunit_resource resource;
41+
static struct kunit_kasan_expectation fail_data;
42+
static bool multishot;
43+
44+
static int kasan_test_init(struct kunit *test)
45+
{
46+
/*
47+
* Temporarily enable multi-shot mode and set panic_on_warn=0.
48+
* Otherwise, we'd only get a report for the first case.
49+
*/
50+
multishot = kasan_save_enable_multi_shot();
51+
52+
return 0;
53+
}
54+
55+
static void kasan_test_exit(struct kunit *test)
56+
{
57+
kasan_restore_multi_shot(multishot);
58+
}
59+
60+
/**
61+
* KUNIT_EXPECT_KASAN_FAIL() - Causes a test failure when the expression does
62+
* not cause a KASAN error. This uses a KUnit resource named "kasan_data." Do
63+
* Do not use this name for a KUnit resource outside here.
64+
*
65+
*/
66+
#define KUNIT_EXPECT_KASAN_FAIL(test, condition) do { \
67+
fail_data.report_expected = true; \
68+
fail_data.report_found = false; \
69+
kunit_add_named_resource(test, \
70+
NULL, \
71+
NULL, \
72+
&resource, \
73+
"kasan_data", &fail_data); \
74+
condition; \
75+
KUNIT_EXPECT_EQ(test, \
76+
fail_data.report_expected, \
77+
fail_data.report_found); \
78+
} while (0)
79+
80+
3781

3882
/*
3983
* Note: test functions are marked noinline so that their names appear in
4084
* reports.
4185
*/
42-
4386
static noinline void __init kmalloc_oob_right(void)
4487
{
4588
char *ptr;

‎mm/kasan/report.c‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333

3434
#include <asm/sections.h>
3535

36+
#include <kunit/test.h>
37+
3638
#include "kasan.h"
3739
#include "../slab.h"
3840

@@ -464,12 +466,37 @@ static bool report_enabled(void)
464466
return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags);
465467
}
466468

469+
#if IS_ENABLED(CONFIG_KUNIT)
470+
static void kasan_update_kunit_status(struct kunit *cur_test)
471+
{
472+
struct kunit_resource *resource;
473+
struct kunit_kasan_expectation *kasan_data;
474+
475+
resource = kunit_find_named_resource(cur_test, "kasan_data");
476+
477+
if (!resource) {
478+
kunit_set_failure(cur_test);
479+
return;
480+
}
481+
482+
kasan_data = (struct kunit_kasan_expectation *)resource->data;
483+
kasan_data->report_found = true;
484+
kunit_put_resource(resource);
485+
}
486+
#endif /* IS_ENABLED(CONFIG_KUNIT) */
487+
467488
void kasan_report_invalid_free(void *object, unsigned long ip)
468489
{
469490
unsigned long flags;
470491
u8 tag = get_tag(object);
471492

472493
object = reset_tag(object);
494+
495+
#if IS_ENABLED(CONFIG_KUNIT)
496+
if (current->kunit_test)
497+
kasan_update_kunit_status(current->kunit_test);
498+
#endif /* IS_ENABLED(CONFIG_KUNIT) */
499+
473500
start_report(&flags);
474501
pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip);
475502
print_tags(tag, object);
@@ -488,6 +515,11 @@ static void __kasan_report(unsigned long addr, size_t size, bool is_write,
488515
void *untagged_addr;
489516
unsigned long flags;
490517

518+
#if IS_ENABLED(CONFIG_KUNIT)
519+
if (current->kunit_test)
520+
kasan_update_kunit_status(current->kunit_test);
521+
#endif /* IS_ENABLED(CONFIG_KUNIT) */
522+
491523
disable_trace_on_warning();
492524

493525
tagged_addr = (void *)addr;

0 commit comments

Comments
 (0)