Skip to contentSkip to conversation

kasan/test: avoid gcc warning for intentional overflow

PATCH Applied Arnd Bergmann 2 participants 2 messages Feb 12, 2024 – Feb 14, 2024

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

PATCH 1/1 kasan/test: avoid gcc warning for intentional overflow

From: Arnd Bergmann <[email protected]>

The out-of-bounds test allocates an object that is three bytes too short in order to validate the bounds checking. Starting with gcc-14, this causes a compile-time warning as gcc has grown smart enough to understand the sizeof() logic:

mm/kasan/kasan_test.c: In function 'kmalloc_oob_16':
mm/kasan/kasan_test.c:443:14: error: allocation of insufficient size '13' for type 'struct <anonymous>' with size '16' [-Werror=alloc-size]
443 | ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
| ^

Hide the actual computation behind a RELOC_HIDE() that ensures the compiler misses the intentional bug.

Fixes 3f15801cdc23 ("lib: add kasan test module")
---
 mm/kasan/kasan_test.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/kasan/kasan_test.c +2 -1
index 318d9cec111a..2d8ae4fbe63b 100644
@@ -440,7 +440,8 @@ static void kmalloc_oob_16(struct kunit *test)
 	/* This test is specifically crafted for the generic mode. */
 	KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
 
-	ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
+	/* RELOC_HIDE to prevent gcc from warning about short alloc */
+	ptr1 = RELOC_HIDE(kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL), 0);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
 
 	ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
Signature · 1 line
2.39.2
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 37 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The out-of-bounds test allocates an object that is three bytes too short in order to validate the bounds checking. Starting with gcc-14, this causes a compile-time warning as gcc has grown smart enough to understand the sizeof() logic:

mm/kasan/kasan_test.c: In function 'kmalloc_oob_16':
mm/kasan/kasan_test.c:443:14: error: allocation of insufficient size '13' for type 'struct <anonymous>' with size '16' [-Werror=alloc-size]
443 | ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
| ^

Hide the actual computation behind a RELOC_HIDE() that ensures the compiler misses the intentional bug.

Fixes 3f15801cdc23 ("lib: add kasan test module")
---
 mm/kasan/kasan_test.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/kasan/kasan_test.c +2 -2
index 318d9cec111a..2d8ae4fbe63b 100644
@@ -440,7 +440,8 @@ static void kmalloc_oob_16(struct kunit *test)
        /* This test is specifically crafted for the generic mode. */
        KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_GENERIC);
 
-       ptr1 = kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL);
+       /* RELOC_HIDE to prevent gcc from warning about short alloc */
+       ptr1 = RELOC_HIDE(kmalloc(sizeof(*ptr1) - 3, GFP_KERNEL), 0);
        KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr1);
 
        ptr2 = kmalloc(sizeof(*ptr2), GFP_KERNEL);
--
Quoted 2 lines

2.39.2