Commit a10c7949 authored by Linus Torvalds's avatar Linus Torvalds
Browse files

Merge tag 'linux_kselftest-kunit-6.15-rc1' of...

Merge tag 'linux_kselftest-kunit-6.15-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest

Pull kunit updates from Shuah Khan:
 "kunit tool:
   - Changes to kunit tool to use qboot on QEMU x86_64, and build GDB
     scripts
   - Fixes kunit tool bug in parsing test plan
   - Adds test to kunit tool to check parsing late test plan

  kunit:
   - Clarifies kunit_skip() argument name
   - Adds Kunit check for the longest symbol length
   - Changes qemu_configs for sparc to use Zilog console"

* tag 'linux_kselftest-kunit-6.15-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest:
  kunit: tool: add test to check parsing late test plan
  kunit: tool: Fix bug in parsing test plan
  Kunit to check the longest symbol length
  kunit: Clarify kunit_skip() argument name
  kunit: tool: Build GDB scripts
  kunit: qemu_configs: sparc: use Zilog console
  kunit: tool: Use qboot on QEMU x86_64
parents 8e324a5c 2e0cf2b3
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@
#include <assert.h>
#include <unistd.h>
#include <stdarg.h>
#include <linux/kallsyms.h>

#define unlikely(cond) (cond)

@@ -106,7 +107,7 @@ static void parse_args(int argc, char **argv)
	}
}

#define BUFSIZE 256
#define BUFSIZE (256 + KSYM_NAME_LEN)

int main(int argc, char **argv)
{
+10 −10
Original line number Diff line number Diff line
@@ -553,9 +553,9 @@ void kunit_cleanup(struct kunit *test);
void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt, ...);

/**
 * kunit_mark_skipped() - Marks @test_or_suite as skipped
 * kunit_mark_skipped() - Marks @test as skipped
 *
 * @test_or_suite: The test context object.
 * @test: The test context object.
 * @fmt:  A printk() style format string.
 *
 * Marks the test as skipped. @fmt is given output as the test status
@@ -563,18 +563,18 @@ void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt,
 *
 * Test execution continues after kunit_mark_skipped() is called.
 */
#define kunit_mark_skipped(test_or_suite, fmt, ...)			\
#define kunit_mark_skipped(test, fmt, ...)				\
	do {								\
		WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED);	\
		scnprintf((test_or_suite)->status_comment,		\
		WRITE_ONCE((test)->status, KUNIT_SKIPPED);		\
		scnprintf((test)->status_comment,			\
			  KUNIT_STATUS_COMMENT_SIZE,			\
			  fmt, ##__VA_ARGS__);				\
	} while (0)

/**
 * kunit_skip() - Marks @test_or_suite as skipped
 * kunit_skip() - Marks @test as skipped
 *
 * @test_or_suite: The test context object.
 * @test: The test context object.
 * @fmt:  A printk() style format string.
 *
 * Skips the test. @fmt is given output as the test status
@@ -582,10 +582,10 @@ void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt,
 *
 * Test execution is halted after kunit_skip() is called.
 */
#define kunit_skip(test_or_suite, fmt, ...)				\
#define kunit_skip(test, fmt, ...)					\
	do {								\
		kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\
		kunit_try_catch_throw(&((test_or_suite)->try_catch));	\
		kunit_mark_skipped((test), fmt, ##__VA_ARGS__);		\
		kunit_try_catch_throw(&((test)->try_catch));		\
	} while (0)

/*
+9 −0
Original line number Diff line number Diff line
@@ -2866,6 +2866,15 @@ config FORTIFY_KUNIT_TEST
	  by the str*() and mem*() family of functions. For testing runtime
	  traps of FORTIFY_SOURCE, see LKDTM's "FORTIFY_*" tests.

config LONGEST_SYM_KUNIT_TEST
	tristate "Test the longest symbol possible" if !KUNIT_ALL_TESTS
	depends on KUNIT && KPROBES
	default KUNIT_ALL_TESTS
	help
	  Tests the longest symbol possible

	  If unsure, say N.

config HW_BREAKPOINT_KUNIT_TEST
	bool "Test hw_breakpoint constraints accounting" if !KUNIT_ALL_TESTS
	depends on HAVE_HW_BREAKPOINT
+4 −0
Original line number Diff line number Diff line
@@ -27,6 +27,10 @@ obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
obj-$(CONFIG_KFIFO_KUNIT_TEST) += kfifo_kunit.o
obj-$(CONFIG_TEST_LIST_SORT) += test_list_sort.o
obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o

CFLAGS_longest_symbol_kunit.o += $(call cc-disable-warning, missing-prototypes)
obj-$(CONFIG_LONGEST_SYM_KUNIT_TEST) += longest_symbol_kunit.o

obj-$(CONFIG_MEMCPY_KUNIT_TEST) += memcpy_kunit.o
CFLAGS_overflow_kunit.o = $(call cc-disable-warning, tautological-constant-out-of-range-compare)
obj-$(CONFIG_OVERFLOW_KUNIT_TEST) += overflow_kunit.o
+82 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0
/*
 * Test the longest symbol length. Execute with:
 *  ./tools/testing/kunit/kunit.py run longest-symbol
 *  --arch=x86_64 --kconfig_add CONFIG_KPROBES=y --kconfig_add CONFIG_MODULES=y
 *  --kconfig_add CONFIG_RETPOLINE=n --kconfig_add CONFIG_CFI_CLANG=n
 *  --kconfig_add CONFIG_MITIGATION_RETPOLINE=n
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <kunit/test.h>
#include <linux/stringify.h>
#include <linux/kprobes.h>
#include <linux/kallsyms.h>

#define DI(name) s##name##name
#define DDI(name) DI(n##name##name)
#define DDDI(name) DDI(n##name##name)
#define DDDDI(name) DDDI(n##name##name)
#define DDDDDI(name) DDDDI(n##name##name)

/*Generate a symbol whose name length is 511 */
#define LONGEST_SYM_NAME  DDDDDI(g1h2i3j4k5l6m7n)

#define RETURN_LONGEST_SYM 0xAAAAA

noinline int LONGEST_SYM_NAME(void);
noinline int LONGEST_SYM_NAME(void)
{
	return RETURN_LONGEST_SYM;
}

_Static_assert(sizeof(__stringify(LONGEST_SYM_NAME)) == KSYM_NAME_LEN,
"Incorrect symbol length found. Expected KSYM_NAME_LEN: "
__stringify(KSYM_NAME_LEN) ", but found: "
__stringify(sizeof(LONGEST_SYM_NAME)));

static void test_longest_symbol(struct kunit *test)
{
	KUNIT_EXPECT_EQ(test, RETURN_LONGEST_SYM, LONGEST_SYM_NAME());
};

static void test_longest_symbol_kallsyms(struct kunit *test)
{
	unsigned long (*kallsyms_lookup_name)(const char *name);
	static int (*longest_sym)(void);

	struct kprobe kp = {
		.symbol_name = "kallsyms_lookup_name",
	};

	if (register_kprobe(&kp) < 0) {
		pr_info("%s: kprobe not registered", __func__);
		KUNIT_FAIL(test, "test_longest_symbol kallsyms: kprobe not registered\n");
		return;
	}

	kunit_warn(test, "test_longest_symbol kallsyms: kprobe registered\n");
	kallsyms_lookup_name = (unsigned long (*)(const char *name))kp.addr;
	unregister_kprobe(&kp);

	longest_sym =
		(void *) kallsyms_lookup_name(__stringify(LONGEST_SYM_NAME));
	KUNIT_EXPECT_EQ(test, RETURN_LONGEST_SYM, longest_sym());
};

static struct kunit_case longest_symbol_test_cases[] = {
	KUNIT_CASE(test_longest_symbol),
	KUNIT_CASE(test_longest_symbol_kallsyms),
	{}
};

static struct kunit_suite longest_symbol_test_suite = {
	.name = "longest-symbol",
	.test_cases = longest_symbol_test_cases,
};
kunit_test_suite(longest_symbol_test_suite);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Test the longest symbol length");
MODULE_AUTHOR("Sergio González Collado");
Loading