Skip to content

Commit 04e403e

Browse files
committed
kunit/overflow: Fix DEFINE_FLEX tests for counted_by
Unfortunately, __builtin_dynamic_object_size() does not take into account flexible array sizes, even when they are sized by __counted_by. As a result, the size tests for the flexible arrays need to be separated to get an accurate check of the compiler's behavior. While at it, fully test sizeof, __struct_size (bdos(..., 0)), and __member_size (bdos(..., 1)). I still think this is a compiler design issue, but there's not much to be done about it currently beyond adjusting these tests. GCC and Clang agree on this behavior at least. :) Reported-by: "Thomas Weißschuh" <linux@weissschuh.net> Closes: https://lore.kernel.org/lkml/e1a1531d-6968-4ae8-a3b5-5ea0547ec4b3@t-8ch.de/ Fixes: 9dd5134 ("kunit/overflow: Adjust for __counted_by with DEFINE_RAW_FLEX()") Signed-off-by: Kees Cook <kees@kernel.org>
1 parent ba6be7b commit 04e403e

1 file changed

Lines changed: 28 additions & 10 deletions

File tree

lib/tests/overflow_kunit.c

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,22 +1185,40 @@ struct bar {
11851185

11861186
static void DEFINE_FLEX_test(struct kunit *test)
11871187
{
1188-
/* Using _RAW_ on a __counted_by struct will initialize "counter" to zero */
1189-
DEFINE_RAW_FLEX(struct foo, two_but_zero, array, 2);
1190-
#ifdef CONFIG_CC_HAS_COUNTED_BY
1191-
int expected_raw_size = sizeof(struct foo);
1192-
#else
1193-
int expected_raw_size = sizeof(struct foo) + 2 * sizeof(s16);
1194-
#endif
1195-
/* Without annotation, it will always be on-stack size. */
11961188
DEFINE_RAW_FLEX(struct bar, two, array, 2);
11971189
DEFINE_FLEX(struct foo, eight, array, counter, 8);
11981190
DEFINE_FLEX(struct foo, empty, array, counter, 0);
1191+
/* Using _RAW_ on a __counted_by struct will initialize "counter" to zero */
1192+
DEFINE_RAW_FLEX(struct foo, two_but_zero, array, 2);
1193+
int array_size_override = 0;
11991194

1200-
KUNIT_EXPECT_EQ(test, __struct_size(two_but_zero), expected_raw_size);
1195+
KUNIT_EXPECT_EQ(test, sizeof(*two), sizeof(struct bar));
12011196
KUNIT_EXPECT_EQ(test, __struct_size(two), sizeof(struct bar) + 2 * sizeof(s16));
1202-
KUNIT_EXPECT_EQ(test, __struct_size(eight), 24);
1197+
KUNIT_EXPECT_EQ(test, __member_size(two), sizeof(struct bar) + 2 * sizeof(s16));
1198+
KUNIT_EXPECT_EQ(test, __struct_size(two->array), 2 * sizeof(s16));
1199+
KUNIT_EXPECT_EQ(test, __member_size(two->array), 2 * sizeof(s16));
1200+
1201+
KUNIT_EXPECT_EQ(test, sizeof(*eight), sizeof(struct foo));
1202+
KUNIT_EXPECT_EQ(test, __struct_size(eight), sizeof(struct foo) + 8 * sizeof(s16));
1203+
KUNIT_EXPECT_EQ(test, __member_size(eight), sizeof(struct foo) + 8 * sizeof(s16));
1204+
KUNIT_EXPECT_EQ(test, __struct_size(eight->array), 8 * sizeof(s16));
1205+
KUNIT_EXPECT_EQ(test, __member_size(eight->array), 8 * sizeof(s16));
1206+
1207+
KUNIT_EXPECT_EQ(test, sizeof(*empty), sizeof(struct foo));
12031208
KUNIT_EXPECT_EQ(test, __struct_size(empty), sizeof(struct foo));
1209+
KUNIT_EXPECT_EQ(test, __member_size(empty), sizeof(struct foo));
1210+
KUNIT_EXPECT_EQ(test, __struct_size(empty->array), 0);
1211+
KUNIT_EXPECT_EQ(test, __member_size(empty->array), 0);
1212+
1213+
/* If __counted_by is not being used, array size will have the on-stack size. */
1214+
if (!IS_ENABLED(CONFIG_CC_HAS_COUNTED_BY))
1215+
array_size_override = 2 * sizeof(s16);
1216+
1217+
KUNIT_EXPECT_EQ(test, sizeof(*two_but_zero), sizeof(struct foo));
1218+
KUNIT_EXPECT_EQ(test, __struct_size(two_but_zero), sizeof(struct foo) + 2 * sizeof(s16));
1219+
KUNIT_EXPECT_EQ(test, __member_size(two_but_zero), sizeof(struct foo) + 2 * sizeof(s16));
1220+
KUNIT_EXPECT_EQ(test, __struct_size(two_but_zero->array), array_size_override);
1221+
KUNIT_EXPECT_EQ(test, __member_size(two_but_zero->array), array_size_override);
12041222
}
12051223

12061224
static struct kunit_case overflow_test_cases[] = {

0 commit comments

Comments
 (0)