From: Marco Elver <elver@google.com>
To: Andrey Konovalov <andreyknvl@google.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Alexander Potapenko <glider@google.com>,
Dmitry Vyukov <dvyukov@google.com>,
Andrey Ryabinin <aryabinin@virtuozzo.com>,
Andrey Konovalov <andreyknvl@gmail.com>,
kasan-dev <kasan-dev@googlegroups.com>,
Linux Memory Management List <linux-mm@kvack.org>,
LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] kasan: detect false-positives in tests
Date: Wed, 31 Mar 2021 18:58:52 +0200 [thread overview]
Message-ID: <CANpmjNMpT0rYKfywkGvqLy8tk3iP6wAuGxHpHVJA77+EG4c5Gg@mail.gmail.com> (raw)
In-Reply-To: <48079c52cc329fbc52f4386996598d58022fb872.1617207873.git.andreyknvl@google.com>
On Wed, 31 Mar 2021 at 18:25, Andrey Konovalov <andreyknvl@google.com> wrote:
>
> Currently, KASAN-KUnit tests can check that a particular annotated part
> of code causes a KASAN report. However, they do not check that no unwanted
> reports happen between the annotated parts.
>
> This patch implements these checks.
>
> It is done by setting report_data.report_found to false in
> kasan_test_init() and at the end of KUNIT_EXPECT_KASAN_FAIL() and then
> checking that it remains false at the beginning of
> KUNIT_EXPECT_KASAN_FAIL() and in kasan_test_exit().
>
> kunit_add_named_resource() call is moved to kasan_test_init(), and the
> value of fail_data.report_expected is kept as false in between
> KUNIT_EXPECT_KASAN_FAIL() annotations for consistency.
>
> Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
Reviewed-by: Marco Elver <elver@google.com>
Thank you!
> ---
> lib/test_kasan.c | 49 +++++++++++++++++++++++++++---------------------
> 1 file changed, 28 insertions(+), 21 deletions(-)
>
> diff --git a/lib/test_kasan.c b/lib/test_kasan.c
> index d77c45edc7cd..bf9225002a7e 100644
> --- a/lib/test_kasan.c
> +++ b/lib/test_kasan.c
> @@ -54,6 +54,10 @@ 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;
> }
>
> @@ -61,6 +65,7 @@ static void kasan_test_exit(struct kunit *test)
> {
> kasan_set_tagging_report_once(true);
> kasan_restore_multi_shot(multishot);
> + KUNIT_EXPECT_FALSE(test, fail_data.report_found);
> }
>
> /**
> @@ -78,28 +83,30 @@ static void kasan_test_exit(struct kunit *test)
> * fields, it can reorder or optimize away the accesses to those fields.
> * Use READ/WRITE_ONCE() for the accesses and compiler barriers around the
> * expression to prevent that.
> + *
> + * In between KUNIT_EXPECT_KASAN_FAIL checks, fail_data.report_found is kept as
> + * false. This allows detecting KASAN reports that happen outside of the checks
> + * by asserting !fail_data.report_found at the start of KUNIT_EXPECT_KASAN_FAIL
> + * and in kasan_test_exit.
> */
> -#define KUNIT_EXPECT_KASAN_FAIL(test, expression) do { \
> - if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) \
> - migrate_disable(); \
> - WRITE_ONCE(fail_data.report_expected, true); \
> - WRITE_ONCE(fail_data.report_found, false); \
> - kunit_add_named_resource(test, \
> - NULL, \
> - NULL, \
> - &resource, \
> - "kasan_data", &fail_data); \
> - barrier(); \
> - expression; \
> - barrier(); \
> - KUNIT_EXPECT_EQ(test, \
> - READ_ONCE(fail_data.report_expected), \
> - READ_ONCE(fail_data.report_found)); \
> - if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) { \
> - if (READ_ONCE(fail_data.report_found)) \
> - kasan_enable_tagging(); \
> - migrate_enable(); \
> - } \
> +#define KUNIT_EXPECT_KASAN_FAIL(test, expression) do { \
> + if (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) \
> + 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 (IS_ENABLED(CONFIG_KASAN_HW_TAGS)) { \
> + if (READ_ONCE(fail_data.report_found)) \
> + kasan_enable_tagging(); \
> + 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.31.0.291.g576ba9dcdaf-goog
>
prev parent reply other threads:[~2021-03-31 16:59 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-31 16:24 Andrey Konovalov
2021-03-31 16:58 ` Marco Elver [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CANpmjNMpT0rYKfywkGvqLy8tk3iP6wAuGxHpHVJA77+EG4c5Gg@mail.gmail.com \
--to=elver@google.com \
--cc=akpm@linux-foundation.org \
--cc=andreyknvl@gmail.com \
--cc=andreyknvl@google.com \
--cc=aryabinin@virtuozzo.com \
--cc=dvyukov@google.com \
--cc=glider@google.com \
--cc=kasan-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox