diff mbox series

[v2,04/14] kasan: add macros to simplify checking test constraints

Message ID 0afed913e43017575794de0777b15ef6b2bdd486.1610554432.git.andreyknvl@google.com (mailing list archive)
State New, archived
Headers show
Series kasan: HW_TAGS tests support and fixes | expand

Commit Message

Andrey Konovalov Jan. 13, 2021, 4:21 p.m. UTC
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>
---
 lib/test_kasan.c | 101 +++++++++++++++--------------------------------
 1 file changed, 31 insertions(+), 70 deletions(-)

Comments

Marco Elver Jan. 13, 2021, 4:25 p.m. UTC | #1
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
>
Alexander Potapenko Jan. 13, 2021, 4:45 p.m. UTC | #2
On Wed, Jan 13, 2021 at 5:25 PM Marco Elver <elver@google.com> wrote:
>
> 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>
Reviewed-by: Alexander Potapenko <glider@google.com>
diff mbox series

Patch

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.