linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Marco Elver <elver@google.com>
To: Andrey Konovalov <andreyknvl@google.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>,
	Vincenzo Frascino <vincenzo.frascino@arm.com>,
	 Dmitry Vyukov <dvyukov@google.com>,
	Alexander Potapenko <glider@google.com>,
	 Andrew Morton <akpm@linux-foundation.org>,
	Will Deacon <will.deacon@arm.com>,
	 Andrey Ryabinin <aryabinin@virtuozzo.com>,
	Peter Collingbourne <pcc@google.com>,
	 Evgenii Stepanov <eugenis@google.com>,
	Branislav Rankov <Branislav.Rankov@arm.com>,
	 Kevin Brodsky <kevin.brodsky@arm.com>,
	kasan-dev <kasan-dev@googlegroups.com>,
	 Linux ARM <linux-arm-kernel@lists.infradead.org>,
	 Linux Memory Management List <linux-mm@kvack.org>,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v2 04/14] kasan: add macros to simplify checking test constraints
Date: Wed, 13 Jan 2021 17:25:30 +0100	[thread overview]
Message-ID: <CANpmjNMZHiwKDTyBdHzHB6CexJTfN9TUjk=q6zmj_nebtq9=mg@mail.gmail.com> (raw)
In-Reply-To: <0afed913e43017575794de0777b15ef6b2bdd486.1610554432.git.andreyknvl@google.com>

On Wed, 13 Jan 2021 at 17:21, Andrey Konovalov <andreyknvl@google.com> wrote:
>
> Some KASAN tests require specific kernel configs to be enabled.
> Instead of copy-pasting the checks for these configs add a few helper
> macros and use them.
>
> Link: https://linux-review.googlesource.com/id/I237484a7fddfedf4a4aae9cc61ecbcdbe85a0a63
> Suggested-by: Alexander Potapenko <glider@google.com>
> Signed-off-by: Andrey Konovalov <andreyknvl@google.com>

Nice!

Reviewed-by: Marco Elver <elver@google.com>

> ---
>  lib/test_kasan.c | 101 +++++++++++++++--------------------------------
>  1 file changed, 31 insertions(+), 70 deletions(-)
>
> diff --git a/lib/test_kasan.c b/lib/test_kasan.c
> index 6f46e27c2af7..714ea27fcc3e 100644
> --- a/lib/test_kasan.c
> +++ b/lib/test_kasan.c
> @@ -73,6 +73,20 @@ static void kasan_test_exit(struct kunit *test)
>                         fail_data.report_found); \
>  } while (0)
>
> +#define KASAN_TEST_NEEDS_CONFIG_ON(test, config) do {                  \
> +       if (!IS_ENABLED(config)) {                                      \
> +               kunit_info((test), "skipping, " #config " required");   \
> +               return;                                                 \
> +       }                                                               \
> +} while (0)
> +
> +#define KASAN_TEST_NEEDS_CONFIG_OFF(test, config) do {                 \
> +       if (IS_ENABLED(config)) {                                       \
> +               kunit_info((test), "skipping, " #config " enabled");    \
> +               return;                                                 \
> +       }                                                               \
> +} while (0)
> +
>  static void kmalloc_oob_right(struct kunit *test)
>  {
>         char *ptr;
> @@ -114,10 +128,7 @@ static void kmalloc_pagealloc_oob_right(struct kunit *test)
>         char *ptr;
>         size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
>
> -       if (!IS_ENABLED(CONFIG_SLUB)) {
> -               kunit_info(test, "CONFIG_SLUB is not enabled.");
> -               return;
> -       }
> +       KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
>
>         /*
>          * Allocate a chunk that does not fit into a SLUB cache to trigger
> @@ -135,10 +146,7 @@ static void kmalloc_pagealloc_uaf(struct kunit *test)
>         char *ptr;
>         size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
>
> -       if (!IS_ENABLED(CONFIG_SLUB)) {
> -               kunit_info(test, "CONFIG_SLUB is not enabled.");
> -               return;
> -       }
> +       KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
>
>         ptr = kmalloc(size, GFP_KERNEL);
>         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
> @@ -152,10 +160,7 @@ static void kmalloc_pagealloc_invalid_free(struct kunit *test)
>         char *ptr;
>         size_t size = KMALLOC_MAX_CACHE_SIZE + 10;
>
> -       if (!IS_ENABLED(CONFIG_SLUB)) {
> -               kunit_info(test, "CONFIG_SLUB is not enabled.");
> -               return;
> -       }
> +       KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_SLUB);
>
>         ptr = kmalloc(size, GFP_KERNEL);
>         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
> @@ -218,10 +223,7 @@ static void kmalloc_oob_16(struct kunit *test)
>         } *ptr1, *ptr2;
>
>         /* This test is specifically crafted for the generic mode. */
> -       if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
> -               kunit_info(test, "CONFIG_KASAN_GENERIC required\n");
> -               return;
> -       }
> +       KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
>
>         ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
>         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
> @@ -454,10 +456,7 @@ static void kasan_global_oob(struct kunit *test)
>         char *p = &global_array[ARRAY_SIZE(global_array) + i];
>
>         /* Only generic mode instruments globals. */
> -       if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
> -               kunit_info(test, "CONFIG_KASAN_GENERIC required");
> -               return;
> -       }
> +       KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
>
>         KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
>  }
> @@ -486,10 +485,7 @@ static void kasan_stack_oob(struct kunit *test)
>         volatile int i = OOB_TAG_OFF;
>         char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
>
> -       if (!IS_ENABLED(CONFIG_KASAN_STACK)) {
> -               kunit_info(test, "CONFIG_KASAN_STACK is not enabled");
> -               return;
> -       }
> +       KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
>
>         KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
>  }
> @@ -501,15 +497,8 @@ static void kasan_alloca_oob_left(struct kunit *test)
>         char *p = alloca_array - 1;
>
>         /* Only generic mode instruments dynamic allocas. */
> -       if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
> -               kunit_info(test, "CONFIG_KASAN_GENERIC required");
> -               return;
> -       }
> -
> -       if (!IS_ENABLED(CONFIG_KASAN_STACK)) {
> -               kunit_info(test, "CONFIG_KASAN_STACK is not enabled");
> -               return;
> -       }
> +       KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
> +       KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
>
>         KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
>  }
> @@ -521,15 +510,8 @@ static void kasan_alloca_oob_right(struct kunit *test)
>         char *p = alloca_array + i;
>
>         /* Only generic mode instruments dynamic allocas. */
> -       if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
> -               kunit_info(test, "CONFIG_KASAN_GENERIC required");
> -               return;
> -       }
> -
> -       if (!IS_ENABLED(CONFIG_KASAN_STACK)) {
> -               kunit_info(test, "CONFIG_KASAN_STACK is not enabled");
> -               return;
> -       }
> +       KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
> +       KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_STACK);
>
>         KUNIT_EXPECT_KASAN_FAIL(test, *(volatile char *)p);
>  }
> @@ -593,11 +575,7 @@ static void kasan_memchr(struct kunit *test)
>          * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
>          * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
>          */
> -       if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
> -               kunit_info(test,
> -                       "str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT");
> -               return;
> -       }
> +       KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
>
>         if (OOB_TAG_OFF)
>                 size = round_up(size, OOB_TAG_OFF);
> @@ -621,11 +599,7 @@ static void kasan_memcmp(struct kunit *test)
>          * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
>          * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
>          */
> -       if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
> -               kunit_info(test,
> -                       "str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT");
> -               return;
> -       }
> +       KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
>
>         if (OOB_TAG_OFF)
>                 size = round_up(size, OOB_TAG_OFF);
> @@ -648,11 +622,7 @@ static void kasan_strings(struct kunit *test)
>          * str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT.
>          * See https://bugzilla.kernel.org/show_bug.cgi?id=206337 for details.
>          */
> -       if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) {
> -               kunit_info(test,
> -                       "str* functions are not instrumented with CONFIG_AMD_MEM_ENCRYPT");
> -               return;
> -       }
> +       KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_AMD_MEM_ENCRYPT);
>
>         ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
>         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
> @@ -713,10 +683,7 @@ static void kasan_bitops_generic(struct kunit *test)
>         long *bits;
>
>         /* This test is specifically crafted for the generic mode. */
> -       if (!IS_ENABLED(CONFIG_KASAN_GENERIC)) {
> -               kunit_info(test, "CONFIG_KASAN_GENERIC required\n");
> -               return;
> -       }
> +       KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
>
>         /*
>          * Allocate 1 more byte, which causes kzalloc to round up to 16 bytes;
> @@ -744,11 +711,8 @@ static void kasan_bitops_tags(struct kunit *test)
>  {
>         long *bits;
>
> -       /* This test is specifically crafted for the tag-based mode. */
> -       if (IS_ENABLED(CONFIG_KASAN_GENERIC)) {
> -               kunit_info(test, "CONFIG_KASAN_SW_TAGS required\n");
> -               return;
> -       }
> +       /* This test is specifically crafted for tag-based modes. */
> +       KASAN_TEST_NEEDS_CONFIG_OFF(test, CONFIG_KASAN_GENERIC);
>
>         /* Allocation size will be rounded to up granule size, which is 16. */
>         bits = kzalloc(sizeof(*bits), GFP_KERNEL);
> @@ -777,10 +741,7 @@ static void vmalloc_oob(struct kunit *test)
>  {
>         void *area;
>
> -       if (!IS_ENABLED(CONFIG_KASAN_VMALLOC)) {
> -               kunit_info(test, "CONFIG_KASAN_VMALLOC is not enabled.");
> -               return;
> -       }
> +       KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_VMALLOC);
>
>         /*
>          * We have to be careful not to hit the guard page.
> --
> 2.30.0.284.gd98b1dd5eaa7-goog
>


  reply	other threads:[~2021-01-13 16:25 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-13 16:21 [PATCH v2 00/14] kasan: HW_TAGS tests support and fixes Andrey Konovalov
2021-01-13 16:21 ` [PATCH v2 01/14] kasan: prefix global functions with kasan_ Andrey Konovalov
2021-01-13 16:21 ` [PATCH v2 02/14] kasan: clarify HW_TAGS impact on TBI Andrey Konovalov
2021-01-13 16:21 ` [PATCH v2 03/14] kasan: clean up comments in tests Andrey Konovalov
2021-01-13 16:24   ` Marco Elver
2021-01-13 16:21 ` [PATCH v2 04/14] kasan: add macros to simplify checking test constraints Andrey Konovalov
2021-01-13 16:25   ` Marco Elver [this message]
2021-01-13 16:45     ` Alexander Potapenko
2021-01-13 16:21 ` [PATCH v2 05/14] kasan: add match-all tag tests Andrey Konovalov
2021-01-13 16:26   ` Marco Elver
2021-01-13 16:21 ` [PATCH v2 06/14] kasan, arm64: allow using KUnit tests with HW_TAGS mode Andrey Konovalov
2021-01-13 16:21 ` [PATCH v2 07/14] kasan: rename CONFIG_TEST_KASAN_MODULE Andrey Konovalov
2021-01-13 16:21 ` [PATCH v2 08/14] kasan: add compiler barriers to KUNIT_EXPECT_KASAN_FAIL Andrey Konovalov
2021-01-13 16:28   ` Marco Elver
2021-01-13 16:21 ` [PATCH v2 09/14] kasan: adapt kmalloc_uaf2 test to HW_TAGS mode Andrey Konovalov
2021-01-13 16:31   ` Marco Elver
2021-01-13 16:32   ` Alexander Potapenko
2021-01-13 16:21 ` [PATCH v2 10/14] kasan: fix memory corruption in kasan_bitops_tags test Andrey Konovalov
2021-01-13 16:21 ` [PATCH v2 11/14] kasan: fix bug detection via ksize for HW_TAGS mode Andrey Konovalov
2021-01-13 16:54   ` Marco Elver
2021-01-14 17:58     ` Andrey Konovalov
2021-01-14 18:01     ` Andrey Konovalov
2021-01-14 19:56       ` Marco Elver
2021-01-13 16:21 ` [PATCH v2 12/14] kasan: add proper page allocator tests Andrey Konovalov
2021-01-13 16:21 ` [PATCH v2 13/14] kasan: add a test for kmem_cache_alloc/free_bulk Andrey Konovalov
2021-01-13 16:37   ` Marco Elver
2021-01-14 15:32     ` Andrey Konovalov
2021-01-13 16:21 ` [PATCH v2 14/14] kasan: don't run tests when KASAN is not enabled Andrey Konovalov
2021-01-13 16:39   ` Marco Elver
2021-01-14 15:32     ` Andrey Konovalov

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='CANpmjNMZHiwKDTyBdHzHB6CexJTfN9TUjk=q6zmj_nebtq9=mg@mail.gmail.com' \
    --to=elver@google.com \
    --cc=Branislav.Rankov@arm.com \
    --cc=akpm@linux-foundation.org \
    --cc=andreyknvl@google.com \
    --cc=aryabinin@virtuozzo.com \
    --cc=catalin.marinas@arm.com \
    --cc=dvyukov@google.com \
    --cc=eugenis@google.com \
    --cc=glider@google.com \
    --cc=kasan-dev@googlegroups.com \
    --cc=kevin.brodsky@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=pcc@google.com \
    --cc=vincenzo.frascino@arm.com \
    --cc=will.deacon@arm.com \
    /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