Skip to content

Commit aaf50b1

Browse files
committed
kasan: test: Silence GCC 12 warnings
GCC 12 continues to get smarter about array accesses. The KASAN tests are expecting to explicitly test out-of-bounds conditions at run-time, so hide the variable from GCC, to avoid warnings like: ../lib/test_kasan.c: In function 'ksize_uaf': ../lib/test_kasan.c:790:61: warning: array subscript 120 is outside array bounds of 'void[120]' [-Warray-bounds] 790 | KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[size]); | ~~~~~~~~~~~~~~~~~~~~~~^~~~~~ ../lib/test_kasan.c:97:9: note: in definition of macro 'KUNIT_EXPECT_KASAN_FAIL' 97 | expression; \ | ^~~~~~~~~~ Cc: Andrey Ryabinin <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: Andrey Konovalov <[email protected]> Cc: Dmitry Vyukov <[email protected]> Cc: Vincenzo Frascino <[email protected]> Cc: [email protected] Signed-off-by: Kees Cook <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent b490925 commit aaf50b1

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

lib/test_kasan.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ static void kmalloc_oob_right(struct kunit *test)
131131
ptr = kmalloc(size, GFP_KERNEL);
132132
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
133133

134+
OPTIMIZER_HIDE_VAR(ptr);
134135
/*
135136
* An unaligned access past the requested kmalloc size.
136137
* Only generic KASAN can precisely detect these.
@@ -159,6 +160,7 @@ static void kmalloc_oob_left(struct kunit *test)
159160
ptr = kmalloc(size, GFP_KERNEL);
160161
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
161162

163+
OPTIMIZER_HIDE_VAR(ptr);
162164
KUNIT_EXPECT_KASAN_FAIL(test, *ptr = *(ptr - 1));
163165
kfree(ptr);
164166
}
@@ -171,6 +173,7 @@ static void kmalloc_node_oob_right(struct kunit *test)
171173
ptr = kmalloc_node(size, GFP_KERNEL, 0);
172174
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
173175

176+
OPTIMIZER_HIDE_VAR(ptr);
174177
KUNIT_EXPECT_KASAN_FAIL(test, ptr[0] = ptr[size]);
175178
kfree(ptr);
176179
}
@@ -191,6 +194,7 @@ static void kmalloc_pagealloc_oob_right(struct kunit *test)
191194
ptr = kmalloc(size, GFP_KERNEL);
192195
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
193196

197+
OPTIMIZER_HIDE_VAR(ptr);
194198
KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
195199

196200
kfree(ptr);
@@ -271,6 +275,7 @@ static void kmalloc_large_oob_right(struct kunit *test)
271275
ptr = kmalloc(size, GFP_KERNEL);
272276
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
273277

278+
OPTIMIZER_HIDE_VAR(ptr);
274279
KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 0);
275280
kfree(ptr);
276281
}
@@ -410,6 +415,8 @@ static void kmalloc_oob_16(struct kunit *test)
410415
ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
411416
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr2);
412417

418+
OPTIMIZER_HIDE_VAR(ptr1);
419+
OPTIMIZER_HIDE_VAR(ptr2);
413420
KUNIT_EXPECT_KASAN_FAIL(test, *ptr1 = *ptr2);
414421
kfree(ptr1);
415422
kfree(ptr2);
@@ -756,6 +763,8 @@ static void ksize_unpoisons_memory(struct kunit *test)
756763
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
757764
real_size = ksize(ptr);
758765

766+
OPTIMIZER_HIDE_VAR(ptr);
767+
759768
/* This access shouldn't trigger a KASAN report. */
760769
ptr[size] = 'x';
761770

@@ -778,6 +787,7 @@ static void ksize_uaf(struct kunit *test)
778787
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
779788
kfree(ptr);
780789

790+
OPTIMIZER_HIDE_VAR(ptr);
781791
KUNIT_EXPECT_KASAN_FAIL(test, ksize(ptr));
782792
KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[0]);
783793
KUNIT_EXPECT_KASAN_FAIL(test, ((volatile char *)ptr)[size]);

0 commit comments

Comments
 (0)