Skip to content

Commit 72c3ebe

Browse files
committed
overflow: Refactor test skips for Clang-specific issues
Convert test exclusion into test skipping. This brings the logic for why a test is being skipped into the test itself, instead of having to spread ifdefs around the code. This will make cleanup easier as minimum tests get raised. Drop __maybe_unused so missed tests will be noticed again and clean up whitespace. For example, clang-11 on i386: [15:52:32] ================== overflow (18 subtests) ================== [15:52:32] [PASSED] u8_u8__u8_overflow_test [15:52:32] [PASSED] s8_s8__s8_overflow_test [15:52:32] [PASSED] u16_u16__u16_overflow_test [15:52:32] [PASSED] s16_s16__s16_overflow_test [15:52:32] [PASSED] u32_u32__u32_overflow_test [15:52:32] [PASSED] s32_s32__s32_overflow_test [15:52:32] [SKIPPED] u64_u64__u64_overflow_test [15:52:32] [SKIPPED] s64_s64__s64_overflow_test [15:52:32] [SKIPPED] u32_u32__int_overflow_test [15:52:32] [PASSED] u32_u32__u8_overflow_test [15:52:32] [PASSED] u8_u8__int_overflow_test [15:52:32] [PASSED] int_int__u8_overflow_test [15:52:32] [PASSED] shift_sane_test [15:52:32] [PASSED] shift_overflow_test [15:52:32] [PASSED] shift_truncate_test [15:52:32] [PASSED] shift_nonsense_test [15:52:32] [PASSED] overflow_allocation_test [15:52:32] [PASSED] overflow_size_helpers_test [15:52:32] ==================== [PASSED] overflow ===================== [15:52:32] ============================================================ [15:52:32] Testing complete. Ran 18 tests: passed: 15, skipped: 3 Cc: Nick Desaulniers <[email protected]> Cc: Nathan Chancellor <[email protected]> Cc: Tom Rix <[email protected]> Cc: Daniel Latypov <[email protected]> Cc: "Gustavo A. R. Silva" <[email protected]> Cc: Gwan-gyeong Mun <[email protected]> Cc: [email protected] Signed-off-by: Kees Cook <[email protected]> Reviewed-by: Nick Desaulniers <[email protected]> Tested-by: Nick Desaulniers <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 0e5b9f2 commit 72c3ebe

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

lib/overflow_kunit.c

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,34 @@
1616
#include <linux/types.h>
1717
#include <linux/vmalloc.h>
1818

19+
#define SKIP(cond, reason) do { \
20+
if (cond) { \
21+
kunit_skip(test, reason); \
22+
return; \
23+
} \
24+
} while (0)
25+
26+
/*
27+
* Clang 11 and earlier generate unwanted libcalls for signed output
28+
* on unsigned input.
29+
*/
30+
#if defined(CONFIG_CC_IS_CLANG) && __clang_major__ <= 11
31+
# define SKIP_SIGN_MISMATCH(t) SKIP(t, "Clang 11 unwanted libcalls")
32+
#else
33+
# define SKIP_SIGN_MISMATCH(t) do { } while (0)
34+
#endif
35+
36+
/*
37+
* Clang 13 and earlier generate unwanted libcalls for 64-bit tests on
38+
* 32-bit hosts.
39+
*/
40+
#if defined(CONFIG_CC_IS_CLANG) && __clang_major__ <= 13 && \
41+
BITS_PER_LONG != 64
42+
# define SKIP_64_ON_32(t) SKIP(t, "Clang 13 unwanted libcalls")
43+
#else
44+
# define SKIP_64_ON_32(t) do { } while (0)
45+
#endif
46+
1947
#define DEFINE_TEST_ARRAY_TYPED(t1, t2, t) \
2048
static const struct test_ ## t1 ## _ ## t2 ## __ ## t { \
2149
t1 a; \
@@ -94,7 +122,6 @@ DEFINE_TEST_ARRAY(u32) = {
94122
{-4U, 5U, 1U, -9U, -20U, true, false, true},
95123
};
96124

97-
#if BITS_PER_LONG == 64
98125
DEFINE_TEST_ARRAY(u64) = {
99126
{0, 0, 0, 0, 0, false, false, false},
100127
{1, 1, 2, 0, 1, false, false, false},
@@ -118,7 +145,6 @@ DEFINE_TEST_ARRAY(u64) = {
118145
false, true, false},
119146
{-15ULL, 10ULL, -5ULL, -25ULL, -150ULL, false, false, true},
120147
};
121-
#endif
122148

123149
DEFINE_TEST_ARRAY(s8) = {
124150
{0, 0, 0, 0, 0, false, false, false},
@@ -194,7 +220,6 @@ DEFINE_TEST_ARRAY(s32) = {
194220
{S32_MAX, S32_MAX, -2, 0, 1, true, false, true},
195221
};
196222

197-
#if BITS_PER_LONG == 64
198223
DEFINE_TEST_ARRAY(s64) = {
199224
{0, 0, 0, 0, 0, false, false, false},
200225

@@ -223,7 +248,6 @@ DEFINE_TEST_ARRAY(s64) = {
223248
{-128, -1, -129, -127, 128, false, false, false},
224249
{0, -S64_MAX, -S64_MAX, S64_MAX, 0, false, false, false},
225250
};
226-
#endif
227251

228252
#define check_one_op(t, fmt, op, sym, a, b, r, of) do { \
229253
int _a_orig = a, _a_bump = a + 1; \
@@ -246,18 +270,23 @@ DEFINE_TEST_ARRAY(s64) = {
246270

247271
#define DEFINE_TEST_FUNC_TYPED(n, t, fmt) \
248272
static void do_test_ ## n(struct kunit *test, const struct test_ ## n *p) \
249-
{ \
273+
{ \
250274
check_one_op(t, fmt, add, "+", p->a, p->b, p->sum, p->s_of); \
251275
check_one_op(t, fmt, add, "+", p->b, p->a, p->sum, p->s_of); \
252276
check_one_op(t, fmt, sub, "-", p->a, p->b, p->diff, p->d_of); \
253277
check_one_op(t, fmt, mul, "*", p->a, p->b, p->prod, p->p_of); \
254278
check_one_op(t, fmt, mul, "*", p->b, p->a, p->prod, p->p_of); \
255279
} \
256280
\
257-
__maybe_unused \
258281
static void n ## _overflow_test(struct kunit *test) { \
259282
unsigned i; \
260283
\
284+
SKIP_64_ON_32(__same_type(t, u64)); \
285+
SKIP_64_ON_32(__same_type(t, s64)); \
286+
SKIP_SIGN_MISMATCH(__same_type(n ## _tests[0].a, u32) && \
287+
__same_type(n ## _tests[0].b, u32) && \
288+
__same_type(n ## _tests[0].sum, int)); \
289+
\
261290
for (i = 0; i < ARRAY_SIZE(n ## _tests); ++i) \
262291
do_test_ ## n(test, &n ## _tests[i]); \
263292
kunit_info(test, "%zu %s arithmetic tests finished\n", \
@@ -273,10 +302,8 @@ DEFINE_TEST_FUNC(u16, "%d");
273302
DEFINE_TEST_FUNC(s16, "%d");
274303
DEFINE_TEST_FUNC(u32, "%u");
275304
DEFINE_TEST_FUNC(s32, "%d");
276-
#if BITS_PER_LONG == 64
277305
DEFINE_TEST_FUNC(u64, "%llu");
278306
DEFINE_TEST_FUNC(s64, "%lld");
279-
#endif
280307

281308
DEFINE_TEST_ARRAY_TYPED(u32, u32, u8) = {
282309
{0, 0, 0, 0, 0, false, false, false},
@@ -716,18 +743,9 @@ static struct kunit_case overflow_test_cases[] = {
716743
KUNIT_CASE(s16_s16__s16_overflow_test),
717744
KUNIT_CASE(u32_u32__u32_overflow_test),
718745
KUNIT_CASE(s32_s32__s32_overflow_test),
719-
/* Clang 13 and earlier generate unwanted libcalls on 32-bit. */
720-
#if BITS_PER_LONG == 64
721746
KUNIT_CASE(u64_u64__u64_overflow_test),
722747
KUNIT_CASE(s64_s64__s64_overflow_test),
723-
#endif
724-
/*
725-
* Clang 11 and earlier generate unwanted libcalls for signed output, unsigned
726-
* input.
727-
*/
728-
#if !(defined(CONFIG_CC_IS_CLANG) && __clang_major__ <= 11)
729748
KUNIT_CASE(u32_u32__int_overflow_test),
730-
#endif
731749
KUNIT_CASE(u32_u32__u8_overflow_test),
732750
KUNIT_CASE(u8_u8__int_overflow_test),
733751
KUNIT_CASE(int_int__u8_overflow_test),

0 commit comments

Comments
 (0)