]> git.baikalelectronics.ru Git - kernel.git/commitdiff
overflow: Refactor test skips for Clang-specific issues
authorKees Cook <keescook@chromium.org>
Thu, 6 Oct 2022 23:00:17 +0000 (16:00 -0700)
committerKees Cook <keescook@chromium.org>
Tue, 25 Oct 2022 21:57:42 +0000 (14:57 -0700)
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 <ndesaulniers@google.com>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Tom Rix <trix@redhat.com>
Cc: Daniel Latypov <dlatypov@google.com>
Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org>
Cc: Gwan-gyeong Mun <gwan-gyeong.mun@intel.com>
Cc: llvm@lists.linux.dev
Signed-off-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Tested-by: Nick Desaulniers <ndesaulniers@google.com>
Link: https://lore.kernel.org/r/20221006230017.1833458-1-keescook@chromium.org
lib/overflow_kunit.c

index ca5f1aa3e91d97732e6d37bb422714d866f46829..b8556a2e7bb1dde364579bf7d2142397f29d1538 100644 (file)
 #include <linux/types.h>
 #include <linux/vmalloc.h>
 
+#define SKIP(cond, reason)             do {                    \
+       if (cond) {                                             \
+               kunit_skip(test, reason);                       \
+               return;                                         \
+       }                                                       \
+} while (0)
+
+/*
+ * Clang 11 and earlier generate unwanted libcalls for signed output
+ * on unsigned input.
+ */
+#if defined(CONFIG_CC_IS_CLANG) && __clang_major__ <= 11
+# define SKIP_SIGN_MISMATCH(t) SKIP(t, "Clang 11 unwanted libcalls")
+#else
+# define SKIP_SIGN_MISMATCH(t) do { } while (0)
+#endif
+
+/*
+ * Clang 13 and earlier generate unwanted libcalls for 64-bit tests on
+ * 32-bit hosts.
+ */
+#if defined(CONFIG_CC_IS_CLANG) && __clang_major__ <= 13 &&    \
+    BITS_PER_LONG != 64
+# define SKIP_64_ON_32(t)      SKIP(t, "Clang 13 unwanted libcalls")
+#else
+# define SKIP_64_ON_32(t)      do { } while (0)
+#endif
+
 #define DEFINE_TEST_ARRAY_TYPED(t1, t2, t)                     \
        static const struct test_ ## t1 ## _ ## t2 ## __ ## t { \
                t1 a;                                           \
@@ -94,7 +122,6 @@ DEFINE_TEST_ARRAY(u32) = {
        {-4U, 5U, 1U, -9U, -20U, true, false, true},
 };
 
-#if BITS_PER_LONG == 64
 DEFINE_TEST_ARRAY(u64) = {
        {0, 0, 0, 0, 0, false, false, false},
        {1, 1, 2, 0, 1, false, false, false},
@@ -118,7 +145,6 @@ DEFINE_TEST_ARRAY(u64) = {
         false, true, false},
        {-15ULL, 10ULL, -5ULL, -25ULL, -150ULL, false, false, true},
 };
-#endif
 
 DEFINE_TEST_ARRAY(s8) = {
        {0, 0, 0, 0, 0, false, false, false},
@@ -194,7 +220,6 @@ DEFINE_TEST_ARRAY(s32) = {
        {S32_MAX, S32_MAX, -2, 0, 1, true, false, true},
 };
 
-#if BITS_PER_LONG == 64
 DEFINE_TEST_ARRAY(s64) = {
        {0, 0, 0, 0, 0, false, false, false},
 
@@ -223,7 +248,6 @@ DEFINE_TEST_ARRAY(s64) = {
        {-128, -1, -129, -127, 128, false, false, false},
        {0, -S64_MAX, -S64_MAX, S64_MAX, 0, false, false, false},
 };
-#endif
 
 #define check_one_op(t, fmt, op, sym, a, b, r, of) do {                        \
        int _a_orig = a, _a_bump = a + 1;                               \
@@ -246,7 +270,7 @@ DEFINE_TEST_ARRAY(s64) = {
 
 #define DEFINE_TEST_FUNC_TYPED(n, t, fmt)                              \
 static void do_test_ ## n(struct kunit *test, const struct test_ ## n *p) \
-{                                                                      \
+{                                                                      \
        check_one_op(t, fmt, add, "+", p->a, p->b, p->sum, p->s_of);    \
        check_one_op(t, fmt, add, "+", p->b, p->a, p->sum, p->s_of);    \
        check_one_op(t, fmt, sub, "-", p->a, p->b, p->diff, p->d_of);   \
@@ -254,10 +278,15 @@ static void do_test_ ## n(struct kunit *test, const struct test_ ## n *p) \
        check_one_op(t, fmt, mul, "*", p->b, p->a, p->prod, p->p_of);   \
 }                                                                      \
                                                                        \
-__maybe_unused                                                         \
 static void n ## _overflow_test(struct kunit *test) {                  \
        unsigned i;                                                     \
                                                                        \
+       SKIP_64_ON_32(__same_type(t, u64));                             \
+       SKIP_64_ON_32(__same_type(t, s64));                             \
+       SKIP_SIGN_MISMATCH(__same_type(n ## _tests[0].a, u32) &&        \
+                          __same_type(n ## _tests[0].b, u32) &&        \
+                          __same_type(n ## _tests[0].sum, int));       \
+                                                                       \
        for (i = 0; i < ARRAY_SIZE(n ## _tests); ++i)                   \
                do_test_ ## n(test, &n ## _tests[i]);                   \
        kunit_info(test, "%zu %s arithmetic tests finished\n",          \
@@ -273,10 +302,8 @@ DEFINE_TEST_FUNC(u16, "%d");
 DEFINE_TEST_FUNC(s16, "%d");
 DEFINE_TEST_FUNC(u32, "%u");
 DEFINE_TEST_FUNC(s32, "%d");
-#if BITS_PER_LONG == 64
 DEFINE_TEST_FUNC(u64, "%llu");
 DEFINE_TEST_FUNC(s64, "%lld");
-#endif
 
 DEFINE_TEST_ARRAY_TYPED(u32, u32, u8) = {
        {0, 0, 0, 0, 0, false, false, false},
@@ -716,18 +743,9 @@ static struct kunit_case overflow_test_cases[] = {
        KUNIT_CASE(s16_s16__s16_overflow_test),
        KUNIT_CASE(u32_u32__u32_overflow_test),
        KUNIT_CASE(s32_s32__s32_overflow_test),
-/* Clang 13 and earlier generate unwanted libcalls on 32-bit. */
-#if BITS_PER_LONG == 64
        KUNIT_CASE(u64_u64__u64_overflow_test),
        KUNIT_CASE(s64_s64__s64_overflow_test),
-#endif
-/*
- * Clang 11 and earlier generate unwanted libcalls for signed output, unsigned
- * input.
- */
-#if !(defined(CONFIG_CC_IS_CLANG) && __clang_major__ <= 11)
        KUNIT_CASE(u32_u32__int_overflow_test),
-#endif
        KUNIT_CASE(u32_u32__u8_overflow_test),
        KUNIT_CASE(u8_u8__int_overflow_test),
        KUNIT_CASE(int_int__u8_overflow_test),