linux/lib/stackinit_kunit.c

// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Test cases for compiler-based stack variable zeroing via
 * -ftrivial-auto-var-init={zero,pattern} or CONFIG_GCC_PLUGIN_STRUCTLEAK*.
 * For example, see:
 * "Running tests with kunit_tool" at Documentation/dev-tools/kunit/start.rst
 *	./tools/testing/kunit/kunit.py run stackinit [--raw_output] \
 *		--make_option LLVM=1 \
 *		--kconfig_add CONFIG_INIT_STACK_ALL_ZERO=y
 *
 */
#define pr_fmt(fmt)

#include <kunit/test.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>

/* Exfiltration buffer. */
#define MAX_VAR_SIZE
static u8 check_buf[MAX_VAR_SIZE];

/* Character array to trigger stack protector in all functions. */
#define VAR_BUFFER

/* Volatile mask to convince compiler to copy memory with 0xff. */
static volatile u8 forced_mask =;

/* Location and size tracking to validate fill and test are colocated. */
static void *fill_start, *target_start;
static size_t fill_size, target_size;

static bool stackinit_range_contains(char *haystack_start, size_t haystack_size,
				     char *needle_start, size_t needle_size)
{}

/* Whether the test is expected to fail. */
#define WANT_SUCCESS
#define XFAIL

#define DO_NOTHING_TYPE_SCALAR(var_type)
#define DO_NOTHING_TYPE_STRING(var_type)
#define DO_NOTHING_TYPE_STRUCT(var_type)

#define DO_NOTHING_RETURN_SCALAR(ptr)
#define DO_NOTHING_RETURN_STRING(ptr)
#define DO_NOTHING_RETURN_STRUCT(ptr)

#define DO_NOTHING_CALL_SCALAR(var, name)
#define DO_NOTHING_CALL_STRING(var, name)
#define DO_NOTHING_CALL_STRUCT(var, name)

#define FETCH_ARG_SCALAR(var)
#define FETCH_ARG_STRING(var)
#define FETCH_ARG_STRUCT(var)

/*
 * On m68k, if the leaf function test variable is longer than 8 bytes,
 * the start of the stack frame moves. 8 is sufficiently large to
 * test m68k char arrays, but leave it at 16 for other architectures.
 */
#ifdef CONFIG_M68K
#define FILL_SIZE_STRING
#else
#define FILL_SIZE_STRING
#endif

#define INIT_CLONE_SCALAR
#define INIT_CLONE_STRING
#define INIT_CLONE_STRUCT

#define ZERO_CLONE_SCALAR(zero)
#define ZERO_CLONE_STRING(zero)
/*
 * For the struct, intentionally poison padding to see if it gets
 * copied out in direct assignments.
 * */
#define ZERO_CLONE_STRUCT(zero)

#define INIT_SCALAR_none(var_type)
#define INIT_SCALAR_zero(var_type)

#define INIT_STRING_none(var_type)
#define INIT_STRING_zero(var_type)

#define INIT_STRUCT_none(var_type)
#define INIT_STRUCT_zero(var_type)


#define __static_partial
#define __static_all
#define __dynamic_partial
#define __dynamic_all
#define __runtime_partial
#define __runtime_all

#define INIT_STRUCT_static_partial(var_type)
#define INIT_STRUCT_static_all(var_type)
#define INIT_STRUCT_dynamic_partial(var_type)
#define INIT_STRUCT_dynamic_all(var_type)
#define INIT_STRUCT_runtime_partial(var_type)
#define INIT_STRUCT_runtime_all(var_type)

#define INIT_STRUCT_assigned_static_partial(var_type)
#define INIT_STRUCT_assigned_static_all(var_type)
#define INIT_STRUCT_assigned_dynamic_partial(var_type)
#define INIT_STRUCT_assigned_dynamic_all(var_type)

#define INIT_STRUCT_assigned_copy(var_type)

/*
 * @name: unique string name for the test
 * @var_type: type to be tested for zeroing initialization
 * @which: is this a SCALAR, STRING, or STRUCT type?
 * @init_level: what kind of initialization is performed
 * @xfail: is this test expected to fail?
 */
#define DEFINE_TEST_DRIVER(name, var_type, which, xfail)
#define DEFINE_TEST(name, var_type, which, init_level, xfail)

/* Structure with no padding. */
struct test_packed {};

/* Simple structure with padding likely to be covered by compiler. */
struct test_small_hole {};

/* Trigger unhandled padding in a structure. */
struct test_big_hole {} __aligned();

struct test_trailing_hole {};

/* Test if STRUCTLEAK is clearing structs with __user fields. */
struct test_user {};

#define ALWAYS_PASS
#define ALWAYS_FAIL

#ifdef CONFIG_INIT_STACK_NONE
#define USER_PASS
#define BYREF_PASS
#define STRONG_PASS
#elif defined(CONFIG_GCC_PLUGIN_STRUCTLEAK_USER)
#define USER_PASS
#define BYREF_PASS
#define STRONG_PASS
#elif defined(CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF)
#define USER_PASS
#define BYREF_PASS
#define STRONG_PASS
#else
#define USER_PASS
#define BYREF_PASS
#define STRONG_PASS
#endif

#define DEFINE_SCALAR_TEST(name, init, xfail)

#define DEFINE_SCALAR_TESTS(init, xfail)

#define DEFINE_STRUCT_TEST(name, init, xfail)

#define DEFINE_STRUCT_TESTS(init, xfail)

#define DEFINE_STRUCT_INITIALIZER_TESTS(base, xfail)

/* These should be fully initialized all the time! */
DEFINE_SCALAR_TESTS(zero, ALWAYS_PASS);
DEFINE_STRUCT_TESTS(zero, ALWAYS_PASS);
/* Struct initializers: padding may be left uninitialized. */
DEFINE_STRUCT_INITIALIZER_TESTS(static, STRONG_PASS);
DEFINE_STRUCT_INITIALIZER_TESTS(dynamic, STRONG_PASS);
DEFINE_STRUCT_INITIALIZER_TESTS(runtime, STRONG_PASS);
DEFINE_STRUCT_INITIALIZER_TESTS(assigned_static, STRONG_PASS);
DEFINE_STRUCT_INITIALIZER_TESTS(assigned_dynamic, STRONG_PASS);
DEFINE_STRUCT_TESTS(assigned_copy, ALWAYS_FAIL);
/* No initialization without compiler instrumentation. */
DEFINE_SCALAR_TESTS(none, STRONG_PASS);
DEFINE_STRUCT_TESTS(none, BYREF_PASS);
/* Initialization of members with __user attribute. */
DEFINE_TEST(user, struct test_user, STRUCT, none, USER_PASS);

/*
 * Check two uses through a variable declaration outside either path,
 * which was noticed as a special case in porting earlier stack init
 * compiler logic.
 */
static int noinline __leaf_switch_none(int path, bool fill)
{}

static noinline int leaf_switch_1_none(unsigned long sp, bool fill,
					      uint64_t *arg)
{}

static noinline int leaf_switch_2_none(unsigned long sp, bool fill,
					      uint64_t *arg)
{}

/*
 * These are expected to fail for most configurations because neither
 * GCC nor Clang have a way to perform initialization of variables in
 * non-code areas (i.e. in a switch statement before the first "case").
 * https://llvm.org/pr44916
 */
DEFINE_TEST_DRIVER(switch_1_none, uint64_t, SCALAR, ALWAYS_FAIL);
DEFINE_TEST_DRIVER(switch_2_none, uint64_t, SCALAR, ALWAYS_FAIL);

#define KUNIT_test_scalars(init)

#define KUNIT_test_structs(init)	\

static struct kunit_case stackinit_test_cases[] =;

static struct kunit_suite stackinit_test_suite =;

kunit_test_suites();

MODULE_DESCRIPTION();
MODULE_LICENSE();