diff mbox series

[v2,2/4] kmsan: prevent optimizations in memcpy tests

Message ID 20230911145702.2663753-2-glider@google.com (mailing list archive)
State New
Headers show
Series [v2,1/4] kmsan: simplify kmsan_internal_memmove_metadata() | expand

Commit Message

Alexander Potapenko Sept. 11, 2023, 2:57 p.m. UTC
Clang 18 learned to optimize away memcpy() calls of small uninitialized
scalar values. To ensure that memcpy tests in kmsan_test.c still perform
calls to memcpy() (which KMSAN replaces with __msan_memcpy()), declare a
separate memcpy_noinline() function with volatile parameters, which
won't be optimized.

Also retire DO_NOT_OPTIMIZE(), as memcpy_noinline() is apparently
enough.

Signed-off-by: Alexander Potapenko <glider@google.com>

---
v2:
 - fix W=1 warnings reported by LKP test robot
---
 mm/kmsan/kmsan_test.c | 41 ++++++++++++++++-------------------------
 1 file changed, 16 insertions(+), 25 deletions(-)

Comments

Marco Elver Sept. 11, 2023, 3:07 p.m. UTC | #1
On Mon, 11 Sept 2023 at 16:57, Alexander Potapenko <glider@google.com> wrote:
>
> Clang 18 learned to optimize away memcpy() calls of small uninitialized
> scalar values. To ensure that memcpy tests in kmsan_test.c still perform
> calls to memcpy() (which KMSAN replaces with __msan_memcpy()), declare a
> separate memcpy_noinline() function with volatile parameters, which
> won't be optimized.
>
> Also retire DO_NOT_OPTIMIZE(), as memcpy_noinline() is apparently
> enough.
>
> Signed-off-by: Alexander Potapenko <glider@google.com>

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

> ---
> v2:
>  - fix W=1 warnings reported by LKP test robot
> ---
>  mm/kmsan/kmsan_test.c | 41 ++++++++++++++++-------------------------
>  1 file changed, 16 insertions(+), 25 deletions(-)
>
> diff --git a/mm/kmsan/kmsan_test.c b/mm/kmsan/kmsan_test.c
> index 312989aa2865c..a8d4ca4a1066d 100644
> --- a/mm/kmsan/kmsan_test.c
> +++ b/mm/kmsan/kmsan_test.c
> @@ -407,33 +407,25 @@ static void test_printk(struct kunit *test)
>         KUNIT_EXPECT_TRUE(test, report_matches(&expect));
>  }
>
> -/*
> - * Prevent the compiler from optimizing @var away. Without this, Clang may
> - * notice that @var is uninitialized and drop memcpy() calls that use it.
> - *
> - * There is OPTIMIZER_HIDE_VAR() in linux/compier.h that we cannot use here,
> - * because it is implemented as inline assembly receiving @var as a parameter
> - * and will enforce a KMSAN check. Same is true for e.g. barrier_data(var).
> - */
> -#define DO_NOT_OPTIMIZE(var) barrier()
> +/* Prevent the compiler from inlining a memcpy() call. */
> +static noinline void *memcpy_noinline(volatile void *dst,
> +                                     const volatile void *src, size_t size)
> +{
> +       return memcpy((void *)dst, (const void *)src, size);
> +}
>
> -/*
> - * Test case: ensure that memcpy() correctly copies initialized values.
> - * Also serves as a regression test to ensure DO_NOT_OPTIMIZE() does not cause
> - * extra checks.
> - */
> +/* Test case: ensure that memcpy() correctly copies initialized values. */
>  static void test_init_memcpy(struct kunit *test)
>  {
>         EXPECTATION_NO_REPORT(expect);
> -       volatile int src;
> -       volatile int dst = 0;
> +       volatile long long src;
> +       volatile long long dst = 0;
>
> -       DO_NOT_OPTIMIZE(src);
>         src = 1;
>         kunit_info(
>                 test,
>                 "memcpy()ing aligned initialized src to aligned dst (no reports)\n");
> -       memcpy((void *)&dst, (void *)&src, sizeof(src));
> +       memcpy_noinline((void *)&dst, (void *)&src, sizeof(src));
>         kmsan_check_memory((void *)&dst, sizeof(dst));
>         KUNIT_EXPECT_TRUE(test, report_matches(&expect));
>  }
> @@ -451,8 +443,7 @@ static void test_memcpy_aligned_to_aligned(struct kunit *test)
>         kunit_info(
>                 test,
>                 "memcpy()ing aligned uninit src to aligned dst (UMR report)\n");
> -       DO_NOT_OPTIMIZE(uninit_src);
> -       memcpy((void *)&dst, (void *)&uninit_src, sizeof(uninit_src));
> +       memcpy_noinline((void *)&dst, (void *)&uninit_src, sizeof(uninit_src));
>         kmsan_check_memory((void *)&dst, sizeof(dst));
>         KUNIT_EXPECT_TRUE(test, report_matches(&expect));
>  }
> @@ -474,8 +465,9 @@ static void test_memcpy_aligned_to_unaligned(struct kunit *test)
>         kunit_info(
>                 test,
>                 "memcpy()ing aligned uninit src to unaligned dst (UMR report)\n");
> -       DO_NOT_OPTIMIZE(uninit_src);
> -       memcpy((void *)&dst[1], (void *)&uninit_src, sizeof(uninit_src));
> +       kmsan_check_memory((void *)&uninit_src, sizeof(uninit_src));
> +       memcpy_noinline((void *)&dst[1], (void *)&uninit_src,
> +                       sizeof(uninit_src));
>         kmsan_check_memory((void *)dst, 4);
>         KUNIT_EXPECT_TRUE(test, report_matches(&expect));
>  }
> @@ -498,8 +490,8 @@ static void test_memcpy_aligned_to_unaligned2(struct kunit *test)
>         kunit_info(
>                 test,
>                 "memcpy()ing aligned uninit src to unaligned dst - part 2 (UMR report)\n");
> -       DO_NOT_OPTIMIZE(uninit_src);
> -       memcpy((void *)&dst[1], (void *)&uninit_src, sizeof(uninit_src));
> +       memcpy_noinline((void *)&dst[1], (void *)&uninit_src,
> +                       sizeof(uninit_src));
>         kmsan_check_memory((void *)&dst[4], sizeof(uninit_src));
>         KUNIT_EXPECT_TRUE(test, report_matches(&expect));
>  }
> @@ -513,7 +505,6 @@ static void test_memcpy_aligned_to_unaligned2(struct kunit *test)
>                                                                              \
>                 kunit_info(test,                                            \
>                            "memset" #size "() should initialize memory\n"); \
> -               DO_NOT_OPTIMIZE(uninit);                                    \
>                 memset##size((uint##size##_t *)&uninit, 0, 1);              \
>                 kmsan_check_memory((void *)&uninit, sizeof(uninit));        \
>                 KUNIT_EXPECT_TRUE(test, report_matches(&expect));           \
> --
> 2.42.0.283.g2d96d420d3-goog
>
diff mbox series

Patch

diff --git a/mm/kmsan/kmsan_test.c b/mm/kmsan/kmsan_test.c
index 312989aa2865c..a8d4ca4a1066d 100644
--- a/mm/kmsan/kmsan_test.c
+++ b/mm/kmsan/kmsan_test.c
@@ -407,33 +407,25 @@  static void test_printk(struct kunit *test)
 	KUNIT_EXPECT_TRUE(test, report_matches(&expect));
 }
 
-/*
- * Prevent the compiler from optimizing @var away. Without this, Clang may
- * notice that @var is uninitialized and drop memcpy() calls that use it.
- *
- * There is OPTIMIZER_HIDE_VAR() in linux/compier.h that we cannot use here,
- * because it is implemented as inline assembly receiving @var as a parameter
- * and will enforce a KMSAN check. Same is true for e.g. barrier_data(var).
- */
-#define DO_NOT_OPTIMIZE(var) barrier()
+/* Prevent the compiler from inlining a memcpy() call. */
+static noinline void *memcpy_noinline(volatile void *dst,
+				      const volatile void *src, size_t size)
+{
+	return memcpy((void *)dst, (const void *)src, size);
+}
 
-/*
- * Test case: ensure that memcpy() correctly copies initialized values.
- * Also serves as a regression test to ensure DO_NOT_OPTIMIZE() does not cause
- * extra checks.
- */
+/* Test case: ensure that memcpy() correctly copies initialized values. */
 static void test_init_memcpy(struct kunit *test)
 {
 	EXPECTATION_NO_REPORT(expect);
-	volatile int src;
-	volatile int dst = 0;
+	volatile long long src;
+	volatile long long dst = 0;
 
-	DO_NOT_OPTIMIZE(src);
 	src = 1;
 	kunit_info(
 		test,
 		"memcpy()ing aligned initialized src to aligned dst (no reports)\n");
-	memcpy((void *)&dst, (void *)&src, sizeof(src));
+	memcpy_noinline((void *)&dst, (void *)&src, sizeof(src));
 	kmsan_check_memory((void *)&dst, sizeof(dst));
 	KUNIT_EXPECT_TRUE(test, report_matches(&expect));
 }
@@ -451,8 +443,7 @@  static void test_memcpy_aligned_to_aligned(struct kunit *test)
 	kunit_info(
 		test,
 		"memcpy()ing aligned uninit src to aligned dst (UMR report)\n");
-	DO_NOT_OPTIMIZE(uninit_src);
-	memcpy((void *)&dst, (void *)&uninit_src, sizeof(uninit_src));
+	memcpy_noinline((void *)&dst, (void *)&uninit_src, sizeof(uninit_src));
 	kmsan_check_memory((void *)&dst, sizeof(dst));
 	KUNIT_EXPECT_TRUE(test, report_matches(&expect));
 }
@@ -474,8 +465,9 @@  static void test_memcpy_aligned_to_unaligned(struct kunit *test)
 	kunit_info(
 		test,
 		"memcpy()ing aligned uninit src to unaligned dst (UMR report)\n");
-	DO_NOT_OPTIMIZE(uninit_src);
-	memcpy((void *)&dst[1], (void *)&uninit_src, sizeof(uninit_src));
+	kmsan_check_memory((void *)&uninit_src, sizeof(uninit_src));
+	memcpy_noinline((void *)&dst[1], (void *)&uninit_src,
+			sizeof(uninit_src));
 	kmsan_check_memory((void *)dst, 4);
 	KUNIT_EXPECT_TRUE(test, report_matches(&expect));
 }
@@ -498,8 +490,8 @@  static void test_memcpy_aligned_to_unaligned2(struct kunit *test)
 	kunit_info(
 		test,
 		"memcpy()ing aligned uninit src to unaligned dst - part 2 (UMR report)\n");
-	DO_NOT_OPTIMIZE(uninit_src);
-	memcpy((void *)&dst[1], (void *)&uninit_src, sizeof(uninit_src));
+	memcpy_noinline((void *)&dst[1], (void *)&uninit_src,
+			sizeof(uninit_src));
 	kmsan_check_memory((void *)&dst[4], sizeof(uninit_src));
 	KUNIT_EXPECT_TRUE(test, report_matches(&expect));
 }
@@ -513,7 +505,6 @@  static void test_memcpy_aligned_to_unaligned2(struct kunit *test)
                                                                             \
 		kunit_info(test,                                            \
 			   "memset" #size "() should initialize memory\n"); \
-		DO_NOT_OPTIMIZE(uninit);                                    \
 		memset##size((uint##size##_t *)&uninit, 0, 1);              \
 		kmsan_check_memory((void *)&uninit, sizeof(uninit));        \
 		KUNIT_EXPECT_TRUE(test, report_matches(&expect));           \