Skip to contentSkip to conversation

kcsan: test: Adjust "expect" allocation type for kmalloc_obj

PATCH In review Kees Cook 2 participants 2 messages Feb 24 – Feb 25 KCSAN

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH v2 1/1 kcsan: test: Adjust "expect" allocation type for kmalloc_obj

The call to kmalloc_obj(observed.lines) returns "char (*)[3][512]", a pointer to the whole 2D array. But "expect" wants to be "char (*)[512]", the decayed pointer type, as if it were observed.lines itself (though without the "3" bounds). This produces the following build error:

../kernel/kcsan/kcsan_test.c: In function '__report_matches':
../kernel/kcsan/kcsan_test.c:171:16: error: assignment to 'char (*)[512]' from incompatible pointer type 'char (*)[3][512]' [-Wincompatible-pointer-types]
171 | expect = kmalloc_obj(observed.lines);
| ^

Instead of changing the "expect" type to "char (*)[3][512]" and requiring a dereference at each use (e.g. "(expect*)[0]"), just explicitly cast the return to the desired type.

Tested with:

$ ./tools/testing/kunit/kunit.py run \
	--kconfig_add CONFIG_DEBUG_KERNEL=y \
	--kconfig_add CONFIG_KCSAN=y \
	--kconfig_add CONFIG_KCSAN_KUNIT_TEST=y \
	--arch=x86_64 --qemu_args '-smp 2' kcsan
Fixes 69050f8d6d07 ("treewide: Replace kmalloc with kmalloc_obj for non-scalar types")

---
Cc: Marco Elver <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: <[email protected]>

---
 kernel/kcsan/kcsan_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/kcsan/kcsan_test.c +1 -1
index 79e655ea4ca1..ae758150ccb9 100644
@@ -168,7 +168,7 @@ static bool __report_matches(const struct expect_report *r)
 	if (!report_available())
 		return false;
 
-	expect = kmalloc_obj(observed.lines);
+	expect = (typeof(expect))kmalloc_obj(observed.lines);
 	if (WARN_ON(!expect))
 		return false;
 
Signature · 1 line
2.34.1
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 24 lines · Kees Cook

The call to kmalloc_obj(observed.lines) returns "char (*)[3][512]", a pointer to the whole 2D array. But "expect" wants to be "char (*)[512]", the decayed pointer type, as if it were observed.lines itself (though without the "3" bounds). This produces the following build error:

../kernel/kcsan/kcsan_test.c: In function '__report_matches':
../kernel/kcsan/kcsan_test.c:171:16: error: assignment to 'char (*)[512]' from incompatible pointer type 'char (*)[3][512]' [-Wincompatible-pointer-types]
171 | expect = kmalloc_obj(observed.lines);
| ^

Instead of changing the "expect" type to "char (*)[3][512]" and requiring a dereference at each use (e.g. "(expect*)[0]"), just explicitly cast the return to the desired type.

Tested with:

$ ./tools/testing/kunit/kunit.py run \
        --kconfig_add CONFIG_DEBUG_KERNEL=y \
        --kconfig_add CONFIG_KCSAN=y \
        --kconfig_add CONFIG_KCSAN_KUNIT_TEST=y \
        --arch=x86_64 --qemu_args '-smp 2' kcsan
Quoted 3 lines
Fixes 69050f8d6d07 ("treewide: Replace kmalloc with kmalloc_obj for non-scalar types")

I'm assuming you'll take it through your tree.

Thanks!

Quoted 24 lines

---
Cc: Marco Elver <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: <[email protected]>

---
 kernel/kcsan/kcsan_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/kcsan/kcsan_test.c +1 -2
index 79e655ea4ca1..ae758150ccb9 100644
@@ -168,7 +168,7 @@ static bool __report_matches(const struct expect_report *r)
        if (!report_available())
                return false;
 
-       expect = kmalloc_obj(observed.lines);
+       expect = (typeof(expect))kmalloc_obj(observed.lines);
        if (WARN_ON(!expect))
                return false;
 
--

2.34.1