Skip to content

Commit d6e5040

Browse files
xairyakpm00
authored andcommitted
kasan: fix array-bounds warnings in tests
GCC's -Warray-bounds option detects out-of-bounds accesses to statically-sized allocations in krealloc out-of-bounds tests. Use OPTIMIZER_HIDE_VAR to suppress the warning. Also change kmalloc_memmove_invalid_size to use OPTIMIZER_HIDE_VAR instead of a volatile variable. Link: https://lkml.kernel.org/r/e94399242d32e00bba6fd0d9ec4c897f188128e8.1664215688.git.andreyknvl@google.com Signed-off-by: Andrey Konovalov <[email protected]> Reported-by: kernel test robot <[email protected]> Reviewed-by: Kees Cook <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: Andrey Ryabinin <[email protected]> Cc: Dmitry Vyukov <[email protected]> Cc: Marco Elver <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent ad4c365 commit d6e5040

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

mm/kasan/kasan_test.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,9 @@ static void krealloc_more_oob_helper(struct kunit *test,
295295
ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
296296
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
297297

298+
/* Suppress -Warray-bounds warnings. */
299+
OPTIMIZER_HIDE_VAR(ptr2);
300+
298301
/* All offsets up to size2 must be accessible. */
299302
ptr2[size1 - 1] = 'x';
300303
ptr2[size1] = 'x';
@@ -327,6 +330,9 @@ static void krealloc_less_oob_helper(struct kunit *test,
327330
ptr2 = krealloc(ptr1, size2, GFP_KERNEL);
328331
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
329332

333+
/* Suppress -Warray-bounds warnings. */
334+
OPTIMIZER_HIDE_VAR(ptr2);
335+
330336
/* Must be accessible for all modes. */
331337
ptr2[size2 - 1] = 'x';
332338

@@ -540,13 +546,14 @@ static void kmalloc_memmove_invalid_size(struct kunit *test)
540546
{
541547
char *ptr;
542548
size_t size = 64;
543-
volatile size_t invalid_size = size;
549+
size_t invalid_size = size;
544550

545551
ptr = kmalloc(size, GFP_KERNEL);
546552
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
547553

548554
memset((char *)ptr, 0, 64);
549555
OPTIMIZER_HIDE_VAR(ptr);
556+
OPTIMIZER_HIDE_VAR(invalid_size);
550557
KUNIT_EXPECT_KASAN_FAIL(test,
551558
memmove((char *)ptr, (char *)ptr + 4, invalid_size));
552559
kfree(ptr);

0 commit comments

Comments
 (0)