lib/tests/randstruct_kunit.c

Source file repositories/reference/linux-study-clean/lib/tests/randstruct_kunit.c

File Facts

System
Linux kernel
Corpus path
lib/tests/randstruct_kunit.c
Extension
.c
Size
9157 bytes
Lines
335
Domain
Kernel Services
Bucket
lib
Inferred role
Kernel Services: implementation source
Status
source implementation candidate

Why This File Exists

Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.

Dependency Surface

Detected Declarations

Annotated Snippet

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");

Annotation

Implementation Notes