linux/lib/overflow_kunit.c

// SPDX-License-Identifier: GPL-2.0 OR MIT
/*
 * Test cases for arithmetic overflow checks. See:
 * "Running tests with kunit_tool" at Documentation/dev-tools/kunit/start.rst
 *	./tools/testing/kunit/kunit.py run overflow [--raw_output]
 */
#define pr_fmt(fmt)

#include <kunit/device.h>
#include <kunit/test.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/overflow.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/vmalloc.h>

#define SKIP(cond, reason)

/*
 * 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
#else
#define SKIP_SIGN_MISMATCH(t)
#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
#else
#define SKIP_64_ON_32(t)
#endif

#define DEFINE_TEST_ARRAY_TYPED(t1, t2, t)

#define DEFINE_TEST_ARRAY(t)

DEFINE_TEST_ARRAY(u8) =;
DEFINE_TEST_ARRAY(u16) =;
DEFINE_TEST_ARRAY(u32) =;

DEFINE_TEST_ARRAY(u64) =;

DEFINE_TEST_ARRAY(s8) =;

DEFINE_TEST_ARRAY(s16) =;
DEFINE_TEST_ARRAY(s32) =;

DEFINE_TEST_ARRAY(s64) =;

#define check_one_op(t, fmt, op, sym, a, b, r, of)

static int global_counter;
static void bump_counter(void)
{}

static int get_index(void)
{}

#define check_self_op(fmt, op, sym, a, b)

#define DEFINE_TEST_FUNC_TYPED(n, t, fmt)

#define DEFINE_TEST_FUNC(t, fmt)

DEFINE_TEST_FUNC(u8, "%d");
DEFINE_TEST_FUNC(s8, "%d");
DEFINE_TEST_FUNC(u16, "%d");
DEFINE_TEST_FUNC(s16, "%d");
DEFINE_TEST_FUNC(u32, "%u");
DEFINE_TEST_FUNC(s32, "%d");
DEFINE_TEST_FUNC(u64, "%llu");
DEFINE_TEST_FUNC(s64, "%lld");

DEFINE_TEST_ARRAY_TYPED(u32, u32, u8) =;
DEFINE_TEST_FUNC_TYPED(u32_u32__u8, u8, "%d");

DEFINE_TEST_ARRAY_TYPED(u32, u32, int) =;
DEFINE_TEST_FUNC_TYPED(u32_u32__int, int, "%d");

DEFINE_TEST_ARRAY_TYPED(u8, u8, int) =;
DEFINE_TEST_FUNC_TYPED(u8_u8__int, int, "%d");

DEFINE_TEST_ARRAY_TYPED(int, int, u8) =;
DEFINE_TEST_FUNC_TYPED(int_int__u8, u8, "%d");

/* Args are: value, shift, type, expected result, overflow expected */
#define TEST_ONE_SHIFT

static void shift_sane_test(struct kunit *test)
{}

static void shift_overflow_test(struct kunit *test)
{}

static void shift_truncate_test(struct kunit *test)
{}

static void shift_nonsense_test(struct kunit *test)
{}
#undef TEST_ONE_SHIFT

/*
 * Deal with the various forms of allocator arguments. See comments above
 * the DEFINE_TEST_ALLOC() instances for mapping of the "bits".
 */
#define alloc_GFP
#define alloc010(alloc, arg, sz)
#define alloc011(alloc, arg, sz)
#define alloc000(alloc, arg, sz)
#define alloc001(alloc, arg, sz)
#define alloc110(alloc, arg, sz)
#define free0(free, arg, ptr)
#define free1(free, arg, ptr)

/* Wrap around to 16K */
#define TEST_SIZE

#define DEFINE_TEST_ALLOC(func, free_func, want_arg, want_gfp, want_node)

/*
 * Allocator uses a trailing node argument --------+  (e.g. kmalloc_node())
 * Allocator uses the gfp_t argument -----------+  |  (e.g. kmalloc())
 * Allocator uses a special leading argument +  |  |  (e.g. devm_kmalloc())
 *                                           |  |  |
 */
DEFINE_TEST_ALLOC(kmalloc,	 kfree,	     0, 1, 0);
DEFINE_TEST_ALLOC(kmalloc_node,	 kfree,	     0, 1, 1);
DEFINE_TEST_ALLOC(kzalloc,	 kfree,	     0, 1, 0);
DEFINE_TEST_ALLOC(kzalloc_node,  kfree,	     0, 1, 1);
DEFINE_TEST_ALLOC(__vmalloc,	 vfree,	     0, 1, 0);
DEFINE_TEST_ALLOC(kvmalloc,	 kvfree,     0, 1, 0);
DEFINE_TEST_ALLOC(kvmalloc_node, kvfree,     0, 1, 1);
DEFINE_TEST_ALLOC(kvzalloc,	 kvfree,     0, 1, 0);
DEFINE_TEST_ALLOC(kvzalloc_node, kvfree,     0, 1, 1);
DEFINE_TEST_ALLOC(devm_kmalloc,  devm_kfree, 1, 1, 0);
DEFINE_TEST_ALLOC(devm_kzalloc,  devm_kfree, 1, 1, 0);

static void overflow_allocation_test(struct kunit *test)
{}

struct __test_flex_array {};

static void overflow_size_helpers_test(struct kunit *test)
{}

static void overflows_type_test(struct kunit *test)
{}

static void same_type_test(struct kunit *test)
{}

static void castable_to_type_test(struct kunit *test)
{}

struct foo {};

struct bar {};

static void DEFINE_FLEX_test(struct kunit *test)
{}

static struct kunit_case overflow_test_cases[] =;

static struct kunit_suite overflow_test_suite =;

kunit_test_suite();

MODULE_DESCRIPTION();
MODULE_LICENSE();