summaryrefslogtreecommitdiffstats
path: root/lib/tests/randstruct_kunit.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2026-08-30 09:22:00 -0700
committerLinus Torvalds <torvalds@linux-foundation.org>2026-08-30 09:22:00 -0700
commit034dd340b08be1f2f0477ad16131d609f9dbd53c (patch)
tree2536f4b2d7893ccd916b5abdf3c454ad6edc03c2 /lib/tests/randstruct_kunit.c
downloadlinux-stable-034dd340b08be1f2f0477ad16131d609f9dbd53c.tar.gz
linux-stable-034dd340b08be1f2f0477ad16131d609f9dbd53c.zip
Merge tag 'trace-v7.3-2' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-tracegrafted
Pull tracing fixes from Steven Rostedt: - Fix error output of boot instance creation failure Currently if a boot instance creation fails, instead of printing out the name of the instance that failed, it prints "(null)". That is because it prints "cur_str" that had already been processed by strsep(). Print the saved name instead. While at it, print the error code of the failure. - Fix use-after-free for same named historgrams Histograms can be named so that they can be used in multiple events. But if the named histogram has a variable attached, the second event that uses the named histogram which duplicates it and needs to free the original after duplication leaves the old variable in place and still visible. If another histogram uses than variable, it will use the stale one which will try to reference the freed duplicate histogram and crash the kernel. Free the duplicate variables along with the duplicated histogram data. - Check return value of kthread_run() in event self test The events self tests uses a kthread for testing but does not check if it succeeded in creating a kthread. If the kthread creation were to fail, the code will still try to call kthread_stop() on the error returned. - Fix race between reading trace_pipe and updating subbuffer size If a user is reading the trace_pipe file at the same time they update the ring buffer sub-buffer size, can cause the trace_pipe read to read stale data. Add trace_access_lock() around updating the ring buffer sub-buffer size. - Fix eventfs_inode on failure path in creation of the events directory In the creation of the "events" directory, if after allocating the eventfs_inode a failure is detected, it calls cleanup_ei() which calls free_ei(). The free_ei() will test if eventfs_inode being freed has no children. It is a bug if it does. But on the failure case of the creation of the "events" directory, the children lists have not yet been initialized and the free will trigger a warning because list_empty() on an uninitialized list returns false. Move the initialization into init_ei() where it makes more sense and makes sure that a created eventfs_inode has its lists initialized upon creation. - Check return value of kthread_run() in ftrace direct sample code The sample code that shows how to use the ftrace direct calls does not test the return of kthread_run() to see if it succeeds. Return a failure if the kthread_run() doesn't succeed. - Clear user events state on fork in case of alloc failure On fork, the child gets a pointer to the parent's user events state. It makes a copy of it then updates the child's pointer to it. But if the allocation fails, the duplication function leaves the child with a pointer to its parent's descriptor. When the child cleans up its data, it will free the parent's descriptor while the parent is still using it. In the duplication function, set the child's user_event_mm to NULL before testing if the allocation succeeded, and when it exits it will not free the parent's descriptor. - Fix retry exhaustion in simple ring buffer reader swap simple_ring_buffer_swap_reader_page() starts with retry set to 8 and post-decrements it only after a failed link replacement. On the final attempt, a successful replacement leaves retry at zero, while a failed replacement leaves it at -1. But the check for success expects the retry value to be non-zero and exits with an error on zero. This is the opposite result. Fix it. - Fail nicely when the remote swap_reader_page() returns an error Currently, if the swap_reader_page() of a remote buffer fails, it triggers a WARN_ON_ONCE() and continues normally. Instead, have it exit with an error and a pr_warn() print instead of a full WARNING. * tag 'trace-v7.3-2' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace: ring-buffer: Stop remote reader update when page swap fails tracing: Fix retry exhaustion in simple ring buffer reader swap tracing/user_events: Clear copied tracing state before fork duplication samples/ftrace: Fix kthread_stop() on ERR_PTR in ftrace-direct-multi-modify samples/ftrace: Fix kthread_stop() on ERR_PTR in ftrace-direct-modify eventfs: Initialize ei->children and ei->list in init_ei() tracing: Fix use-after-free in trace_pipe read on sub-buffer order change tracing: Fix crash passing ERR_PTR to kthread_stop() tracing: Fix use-after-free with same-name named triggers tracing: Fix logged instance name on creation failure
Diffstat (limited to 'lib/tests/randstruct_kunit.c')
-rw-r--r--lib/tests/randstruct_kunit.c334
1 files changed, 334 insertions, 0 deletions
diff --git a/lib/tests/randstruct_kunit.c b/lib/tests/randstruct_kunit.c
new file mode 100644
index 000000000..f3a2d63c4
--- /dev/null
+++ b/lib/tests/randstruct_kunit.c
@@ -0,0 +1,334 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Test cases for struct randomization, i.e. CONFIG_RANDSTRUCT=y.
+ *
+ * For example, see:
+ * "Running tests with kunit_tool" at Documentation/dev-tools/kunit/start.rst
+ * ./tools/testing/kunit/kunit.py run randstruct [--raw_output] \
+ * [--make_option LLVM=1] \
+ * --kconfig_add CONFIG_RANDSTRUCT_FULL=y
+ *
+ */
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <kunit/test.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/string.h>
+
+#define DO_MANY_MEMBERS(macro, args...) \
+ macro(a, args) \
+ macro(b, args) \
+ macro(c, args) \
+ macro(d, args) \
+ macro(e, args) \
+ macro(f, args) \
+ macro(g, args) \
+ macro(h, args)
+
+#define do_enum(x, ignored) MEMBER_NAME_ ## x,
+enum randstruct_member_names {
+ DO_MANY_MEMBERS(do_enum)
+ MEMBER_NAME_MAX,
+};
+/* Make sure the macros are working: want 8 test members. */
+_Static_assert(MEMBER_NAME_MAX == 8, "Number of test members changed?!");
+
+/* This is an unsigned long member to match the function pointer size */
+#define unsigned_long_member(x, ignored) unsigned long x;
+struct randstruct_untouched {
+ DO_MANY_MEMBERS(unsigned_long_member)
+};
+
+/* Struct explicitly marked with __randomize_layout. */
+struct randstruct_shuffled {
+ DO_MANY_MEMBERS(unsigned_long_member)
+} __randomize_layout;
+#undef unsigned_long_member
+
+/* Struct implicitly randomized from being all func ptrs. */
+#define func_member(x, ignored) size_t (*x)(int);
+struct randstruct_funcs_untouched {
+ DO_MANY_MEMBERS(func_member)
+} __no_randomize_layout;
+
+struct randstruct_funcs_shuffled {
+ DO_MANY_MEMBERS(func_member)
+};
+
+#define func_body(x, ignored) \
+static noinline size_t func_##x(int arg) \
+{ \
+ return offsetof(struct randstruct_funcs_untouched, x); \
+}
+DO_MANY_MEMBERS(func_body)
+
+/* Various mixed types. */
+#define mixed_members \
+ bool a; \
+ short b; \
+ unsigned int c __aligned(16); \
+ size_t d; \
+ char e; \
+ u64 f; \
+ union { \
+ struct randstruct_shuffled shuffled; \
+ uintptr_t g; \
+ }; \
+ union { \
+ void *ptr; \
+ char h; \
+ };
+
+struct randstruct_mixed_untouched {
+ mixed_members
+};
+
+struct randstruct_mixed_shuffled {
+ mixed_members
+} __randomize_layout;
+#undef mixed_members
+
+struct contains_randstruct_untouched {
+ int before;
+ struct randstruct_untouched untouched;
+ int after;
+};
+
+struct contains_randstruct_shuffled {
+ int before;
+ struct randstruct_shuffled shuffled;
+ int after;
+};
+
+struct contains_func_untouched {
+ struct randstruct_funcs_shuffled inner;
+ DO_MANY_MEMBERS(func_member)
+} __no_randomize_layout;
+
+struct contains_func_shuffled {
+ struct randstruct_funcs_shuffled inner;
+ DO_MANY_MEMBERS(func_member)
+};
+#undef func_member
+
+#define check_mismatch(x, untouched, shuffled) \
+ if (offsetof(untouched, x) != offsetof(shuffled, x)) \
+ mismatches++; \
+ kunit_info(test, #shuffled "::" #x " @ %zu (vs %zu)\n", \
+ offsetof(shuffled, x), \
+ offsetof(untouched, x)); \
+
+#define check_pair(outcome, untouched, shuffled, checker...) \
+ mismatches = 0; \
+ DO_MANY_MEMBERS(checker, untouched, shuffled) \
+ kunit_info(test, "Differing " #untouched " vs " #shuffled " member positions: %d\n", \
+ mismatches); \
+ KUNIT_##outcome##_MSG(test, mismatches, 0, \
+ #untouched " vs " #shuffled " layouts: unlucky or broken?\n");
+
+static void randstruct_layout_same(struct kunit *test)
+{
+ int mismatches;
+
+ check_pair(EXPECT_EQ, struct randstruct_untouched, struct randstruct_untouched,
+ check_mismatch)
+ check_pair(EXPECT_GT, struct randstruct_untouched, struct randstruct_shuffled,
+ check_mismatch)
+}
+
+static void randstruct_layout_mixed(struct kunit *test)
+{
+ int mismatches;
+
+ check_pair(EXPECT_EQ, struct randstruct_mixed_untouched, struct randstruct_mixed_untouched,
+ check_mismatch)
+ check_pair(EXPECT_GT, struct randstruct_mixed_untouched, struct randstruct_mixed_shuffled,
+ check_mismatch)
+}
+
+static void randstruct_layout_fptr(struct kunit *test)
+{
+ int mismatches;
+
+ check_pair(EXPECT_EQ, struct randstruct_untouched, struct randstruct_untouched,
+ check_mismatch)
+ check_pair(EXPECT_GT, struct randstruct_untouched, struct randstruct_funcs_shuffled,
+ check_mismatch)
+ check_pair(EXPECT_GT, struct randstruct_funcs_untouched, struct randstruct_funcs_shuffled,
+ check_mismatch)
+}
+
+#define check_mismatch_prefixed(x, prefix, untouched, shuffled) \
+ check_mismatch(prefix.x, untouched, shuffled)
+
+static void randstruct_layout_fptr_deep(struct kunit *test)
+{
+ int mismatches;
+
+ if (IS_ENABLED(CONFIG_CC_IS_CLANG))
+ kunit_skip(test, "Clang randstruct misses inner functions: https://github.com/llvm/llvm-project/issues/138355");
+
+ check_pair(EXPECT_EQ, struct contains_func_untouched, struct contains_func_untouched,
+ check_mismatch_prefixed, inner)
+
+ check_pair(EXPECT_GT, struct contains_func_untouched, struct contains_func_shuffled,
+ check_mismatch_prefixed, inner)
+}
+
+#undef check_pair
+#undef check_mismatch
+
+#define check_mismatch(x, ignore) \
+ KUNIT_EXPECT_EQ_MSG(test, untouched->x, shuffled->x, \
+ "Mismatched member value in %s initializer\n", \
+ name);
+
+static void test_check_init(struct kunit *test, const char *name,
+ struct randstruct_untouched *untouched,
+ struct randstruct_shuffled *shuffled)
+{
+ DO_MANY_MEMBERS(check_mismatch)
+}
+
+static void test_check_mixed_init(struct kunit *test, const char *name,
+ struct randstruct_mixed_untouched *untouched,
+ struct randstruct_mixed_shuffled *shuffled)
+{
+ DO_MANY_MEMBERS(check_mismatch)
+}
+#undef check_mismatch
+
+#define check_mismatch(x, ignore) \
+ KUNIT_EXPECT_EQ_MSG(test, untouched->untouched.x, \
+ shuffled->shuffled.x, \
+ "Mismatched member value in %s initializer\n", \
+ name);
+static void test_check_contained_init(struct kunit *test, const char *name,
+ struct contains_randstruct_untouched *untouched,
+ struct contains_randstruct_shuffled *shuffled)
+{
+ DO_MANY_MEMBERS(check_mismatch)
+}
+#undef check_mismatch
+
+#define check_mismatch(x, ignore) \
+ KUNIT_EXPECT_PTR_EQ_MSG(test, untouched->x, shuffled->x, \
+ "Mismatched member value in %s initializer\n", \
+ name);
+
+static void test_check_funcs_init(struct kunit *test, const char *name,
+ struct randstruct_funcs_untouched *untouched,
+ struct randstruct_funcs_shuffled *shuffled)
+{
+ DO_MANY_MEMBERS(check_mismatch)
+}
+#undef check_mismatch
+
+static void randstruct_initializers(struct kunit *test)
+{
+#define init_members \
+ .a = 1, \
+ .b = 3, \
+ .c = 5, \
+ .d = 7, \
+ .e = 11, \
+ .f = 13, \
+ .g = 17, \
+ .h = 19,
+ struct randstruct_untouched untouched = {
+ init_members
+ };
+ struct randstruct_shuffled shuffled = {
+ init_members
+ };
+ struct randstruct_mixed_untouched mixed_untouched = {
+ init_members
+ };
+ struct randstruct_mixed_shuffled mixed_shuffled = {
+ init_members
+ };
+ struct contains_randstruct_untouched contains_untouched = {
+ .untouched = {
+ init_members
+ },
+ };
+ struct contains_randstruct_shuffled contains_shuffled = {
+ .shuffled = {
+ init_members
+ },
+ };
+#define func_member(x, ignored) \
+ .x = func_##x,
+ struct randstruct_funcs_untouched funcs_untouched = {
+ DO_MANY_MEMBERS(func_member)
+ };
+ struct randstruct_funcs_shuffled funcs_shuffled = {
+ DO_MANY_MEMBERS(func_member)
+ };
+
+ test_check_init(test, "named", &untouched, &shuffled);
+ test_check_init(test, "unnamed", &untouched,
+ &(struct randstruct_shuffled){
+ init_members
+ });
+
+ test_check_contained_init(test, "named", &contains_untouched, &contains_shuffled);
+ test_check_contained_init(test, "unnamed", &contains_untouched,
+ &(struct contains_randstruct_shuffled){
+ .shuffled = (struct randstruct_shuffled){
+ init_members
+ },
+ });
+
+ test_check_contained_init(test, "named", &contains_untouched, &contains_shuffled);
+ test_check_contained_init(test, "unnamed copy", &contains_untouched,
+ &(struct contains_randstruct_shuffled){
+ /* full struct copy initializer */
+ .shuffled = shuffled,
+ });
+
+ test_check_mixed_init(test, "named", &mixed_untouched, &mixed_shuffled);
+ test_check_mixed_init(test, "unnamed", &mixed_untouched,
+ &(struct randstruct_mixed_shuffled){
+ init_members
+ });
+
+ test_check_funcs_init(test, "named", &funcs_untouched, &funcs_shuffled);
+ test_check_funcs_init(test, "unnamed", &funcs_untouched,
+ &(struct randstruct_funcs_shuffled){
+ DO_MANY_MEMBERS(func_member)
+ });
+
+#undef func_member
+#undef init_members
+}
+
+static int randstruct_test_init(struct kunit *test)
+{
+ if (!IS_ENABLED(CONFIG_RANDSTRUCT))
+ kunit_skip(test, "Not built with CONFIG_RANDSTRUCT=y");
+
+ return 0;
+}
+
+static struct kunit_case randstruct_test_cases[] = {
+ KUNIT_CASE(randstruct_layout_same),
+ KUNIT_CASE(randstruct_layout_mixed),
+ KUNIT_CASE(randstruct_layout_fptr),
+ KUNIT_CASE(randstruct_layout_fptr_deep),
+ KUNIT_CASE(randstruct_initializers),
+ {}
+};
+
+static struct kunit_suite randstruct_test_suite = {
+ .name = "randstruct",
+ .init = randstruct_test_init,
+ .test_cases = randstruct_test_cases,
+};
+
+kunit_test_suites(&randstruct_test_suite);
+
+MODULE_DESCRIPTION("Test cases for struct randomization");
+MODULE_LICENSE("GPL");