Skip to contentSkip to conversation

kasan: fix more unit tests with CONFIG_UBSAN_LOCAL_BOUNDS enabled

PATCH In review Peter Collingbourne 3 participants 3 messages Feb 24, 2022 – Mar 8, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 kasan: fix more unit tests with CONFIG_UBSAN_LOCAL_BOUNDS enabled

This is a followup to commit f649dc0e0d7b ("kasan: fix unit tests with CONFIG_UBSAN_LOCAL_BOUNDS enabled") that fixes tests that fail as a result of __alloc_size annotations being added to the kernel allocator functions.

Cc <[email protected]> # 5.16.x
Fixes c37495d6254c ("slab: add __alloc_size attributes for better bounds checking")

---
v2:
- use OPTIMIZER_HIDE_VAR instead of volatile

 lib/test_kasan.c | 4 ++++
 1 file changed, 4 insertions(+)
lib/test_kasan.c +4 -0
index 26a5c9007653..7c3dfb569445 100644
@@ -185,6 +185,7 @@ static void kmalloc_pagealloc_oob_right(struct kunit *test)
 	ptr = kmalloc(size, GFP_KERNEL);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
 
+	OPTIMIZER_HIDE_VAR(ptr);
 	KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
 
 	kfree(ptr);
@@ -295,6 +296,7 @@ static void krealloc_more_oob_helper(struct kunit *test,
 		KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
 
 	/* For all modes first aligned offset after size2 must be inaccessible. */
+	OPTIMIZER_HIDE_VAR(ptr2);
 	KUNIT_EXPECT_KASAN_FAIL(test,
 		ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
 
@@ -319,6 +321,8 @@ static void krealloc_less_oob_helper(struct kunit *test,
 	/* Must be accessible for all modes. */
 	ptr2[size2 - 1] = 'x';
 
+	OPTIMIZER_HIDE_VAR(ptr2);
+
 	/* Generic mode is precise, so unaligned size2 must be inaccessible. */
 	if (IS_ENABLED(CONFIG_KASAN_GENERIC))
 		KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
Reviewed-by on v2 1/1
#1 lore raw
Quoted 10 lines · Peter Collingbourne

This is a followup to commit f649dc0e0d7b ("kasan: fix unit tests with CONFIG_UBSAN_LOCAL_BOUNDS enabled") that fixes tests that fail as a result of __alloc_size annotations being added to the kernel allocator functions.

Cc <[email protected]> # 5.16.x
Fixes c37495d6254c ("slab: add __alloc_size attributes for better bounds checking")

Thanks!

Quoted 39 lines

---
v2:
- use OPTIMIZER_HIDE_VAR instead of volatile

 lib/test_kasan.c | 4 ++++
 1 file changed, 4 insertions(+)
lib/test_kasan.c +4 -1
index 26a5c9007653..7c3dfb569445 100644
@@ -185,6 +185,7 @@ static void kmalloc_pagealloc_oob_right(struct kunit *test)
        ptr = kmalloc(size, GFP_KERNEL);
        KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
 
+       OPTIMIZER_HIDE_VAR(ptr);
        KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
 
        kfree(ptr);
@@ -295,6 +296,7 @@ static void krealloc_more_oob_helper(struct kunit *test,
                KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
 
        /* For all modes first aligned offset after size2 must be inaccessible. */
+       OPTIMIZER_HIDE_VAR(ptr2);
        KUNIT_EXPECT_KASAN_FAIL(test,
                ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
 
@@ -319,6 +321,8 @@ static void krealloc_less_oob_helper(struct kunit *test,
        /* Must be accessible for all modes. */
        ptr2[size2 - 1] = 'x';
 
+       OPTIMIZER_HIDE_VAR(ptr2);
+
        /* Generic mode is precise, so unaligned size2 must be inaccessible. */
        if (IS_ENABLED(CONFIG_KASAN_GENERIC))
                KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
--

2.35.1.473.g83b2b277ed-goog

Acked-by on v2 1/1
#2 lore raw
In reply to Peter Collingbourne
Quoted 47 lines · Peter Collingbourne

This is a followup to commit f649dc0e0d7b ("kasan: fix unit tests with CONFIG_UBSAN_LOCAL_BOUNDS enabled") that fixes tests that fail as a result of __alloc_size annotations being added to the kernel allocator functions.

Cc <[email protected]> # 5.16.x
Fixes c37495d6254c ("slab: add __alloc_size attributes for better bounds checking")

---
v2:
- use OPTIMIZER_HIDE_VAR instead of volatile

 lib/test_kasan.c | 4 ++++
 1 file changed, 4 insertions(+)
lib/test_kasan.c +4 -1
index 26a5c9007653..7c3dfb569445 100644
@@ -185,6 +185,7 @@ static void kmalloc_pagealloc_oob_right(struct kunit *test)
        ptr = kmalloc(size, GFP_KERNEL);
        KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
 
+       OPTIMIZER_HIDE_VAR(ptr);
        KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + OOB_TAG_OFF] = 0);
 
        kfree(ptr);
@@ -295,6 +296,7 @@ static void krealloc_more_oob_helper(struct kunit *test,
                KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
 
        /* For all modes first aligned offset after size2 must be inaccessible. */
+       OPTIMIZER_HIDE_VAR(ptr2);
        KUNIT_EXPECT_KASAN_FAIL(test,
                ptr2[round_up(size2, KASAN_GRANULE_SIZE)] = 'x');
 
@@ -319,6 +321,8 @@ static void krealloc_less_oob_helper(struct kunit *test,
        /* Must be accessible for all modes. */
        ptr2[size2 - 1] = 'x';
 
+       OPTIMIZER_HIDE_VAR(ptr2);
+
        /* Generic mode is precise, so unaligned size2 must be inaccessible. */
        if (IS_ENABLED(CONFIG_KASAN_GENERIC))
                KUNIT_EXPECT_KASAN_FAIL(test, ptr2[size2] = 'x');
--
Quoted 2 lines

2.35.1.473.g83b2b277ed-goog

This patch seems to be in partial conflict with the "kasan: test:
Silence allocation warnings from GCC 12" patch by Kees, which is already in mm.

Thanks!