Skip to content

Commit

Permalink
kmsan: merge test_memcpy_aligned_to_unaligned{,2}() together
Browse files Browse the repository at this point in the history
Introduce report_reset() that allows checking for more than one KMSAN
report per testcase.

Fold test_memcpy_aligned_to_unaligned2() into
test_memcpy_aligned_to_unaligned(), so that they share the setup phase and
check the behavior of a single memcpy() call.

Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: Alexander Potapenko <[email protected]>
Acked-by: Marco Elver <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
  • Loading branch information
ramosian-glider authored and akpm00 committed Oct 4, 2023
1 parent 0be7b2c commit c3ab487
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions mm/kmsan/kmsan_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ static bool report_available(void)
return READ_ONCE(observed.available);
}

/* Reset observed.available, so that the test can trigger another report. */
static void report_reset(void)
{
unsigned long flags;

spin_lock_irqsave(&observed.lock, flags);
WRITE_ONCE(observed.available, false);
observed.ignore = false;
spin_unlock_irqrestore(&observed.lock, flags);
}

/* Information we expect in a report. */
struct expect_report {
const char *error_type; /* Error type. */
Expand Down Expand Up @@ -454,7 +465,7 @@ static void test_memcpy_aligned_to_aligned(struct kunit *test)
*
* Copying aligned 4-byte value to an unaligned one leads to touching two
* aligned 4-byte values. This test case checks that KMSAN correctly reports an
* error on the first of the two values.
* error on the mentioned two values.
*/
static void test_memcpy_aligned_to_unaligned(struct kunit *test)
{
Expand All @@ -470,28 +481,7 @@ static void test_memcpy_aligned_to_unaligned(struct kunit *test)
sizeof(uninit_src));
kmsan_check_memory((void *)dst, 4);
KUNIT_EXPECT_TRUE(test, report_matches(&expect));
}

/*
* Test case: ensure that memcpy() correctly copies uninitialized values between
* aligned `src` and unaligned `dst`.
*
* Copying aligned 4-byte value to an unaligned one leads to touching two
* aligned 4-byte values. This test case checks that KMSAN correctly reports an
* error on the second of the two values.
*/
static void test_memcpy_aligned_to_unaligned2(struct kunit *test)
{
EXPECTATION_UNINIT_VALUE_FN(expect,
"test_memcpy_aligned_to_unaligned2");
volatile int uninit_src;
volatile char dst[8] = { 0 };

kunit_info(
test,
"memcpy()ing aligned uninit src to unaligned dst - part 2 (UMR report)\n");
memcpy_noinline((void *)&dst[1], (void *)&uninit_src,
sizeof(uninit_src));
report_reset();
kmsan_check_memory((void *)&dst[4], sizeof(uninit_src));
KUNIT_EXPECT_TRUE(test, report_matches(&expect));
}
Expand Down Expand Up @@ -589,7 +579,6 @@ static struct kunit_case kmsan_test_cases[] = {
KUNIT_CASE(test_init_memcpy),
KUNIT_CASE(test_memcpy_aligned_to_aligned),
KUNIT_CASE(test_memcpy_aligned_to_unaligned),
KUNIT_CASE(test_memcpy_aligned_to_unaligned2),
KUNIT_CASE(test_memset16),
KUNIT_CASE(test_memset32),
KUNIT_CASE(test_memset64),
Expand Down

0 comments on commit c3ab487

Please sign in to comment.