kernel/crash_core_test.c

Source file repositories/reference/linux-study-clean/kernel/crash_core_test.c

File Facts

System
Linux kernel
Corpus path
kernel/crash_core_test.c
Extension
.c
Size
11298 bytes
Lines
344
Domain
Core OS
Bucket
Scheduler, Processes, Timers, Sync, And Syscalls
Inferred role
Core OS: implementation source
Status
source implementation candidate

Why This File Exists

Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.

Dependency Surface

Detected Declarations

Annotated Snippet

struct exclude_test_param {
	const char *description;
	unsigned long long exclude_start;
	unsigned long long exclude_end;
	unsigned int initial_max_ranges;
	const struct range *initial_ranges;
	unsigned int initial_nr_ranges;
	const struct range *expected_ranges;
	unsigned int expected_nr_ranges;
	int expected_ret;
};

static void run_exclude_test_case(struct kunit *test, const struct exclude_test_param *params)
{
	struct crash_mem *mem;
	int ret;

	kunit_info(test, "%s", params->description);

	mem = create_crash_mem(test, params->initial_max_ranges,
			       params->initial_nr_ranges, params->initial_ranges);
	if (!mem)
		return; // Error already logged by create_crash_mem or kunit_kzalloc

	ret = crash_exclude_mem_range(mem, params->exclude_start, params->exclude_end);

	KUNIT_ASSERT_EQ_MSG(test, params->expected_ret, ret,
			    "%s: Return value mismatch.", params->description);

	if (params->expected_ret == 0) {
		assert_ranges_equal(test, mem->ranges, mem->nr_ranges,
				    params->expected_ranges, params->expected_nr_ranges,
				    params->description);
	} else {
		// If an error is expected, nr_ranges might still be relevant to check
		// depending on the exact point of failure. For ENOMEM on split,
		// nr_ranges shouldn't have changed.
		KUNIT_ASSERT_EQ_MSG(test, params->initial_nr_ranges,
				    mem->nr_ranges,
				    "%s: Number of ranges mismatch on error.",
				    params->description);
	}
}

/*
 * Test Strategy 1: One to-be-excluded range A and one existing range B.
 *
 * Exhaust all possibilities of the position of A regarding B.
 */

static const struct range single_range_b = { .start = 100, .end = 199 };

static const struct exclude_test_param exclude_single_range_test_data[] = {
	{
		.description = "1.1: A is left of B, no overlap",
		.exclude_start = 10, .exclude_end = 50,
		.initial_max_ranges = 1,
		.initial_ranges = &single_range_b, .initial_nr_ranges = 1,
		.expected_ranges = &single_range_b, .expected_nr_ranges = 1,
		.expected_ret = 0,
	},
	{
		.description = "1.2: A's right boundary touches B's left boundary",
		.exclude_start = 10, .exclude_end = 99,
		.initial_max_ranges = 1,
		.initial_ranges = &single_range_b, .initial_nr_ranges = 1,
		.expected_ranges = &single_range_b, .expected_nr_ranges = 1,
		.expected_ret = 0,
	},
	{
		.description = "1.3: A overlaps B's left part",
		.exclude_start = 50, .exclude_end = 149,
		.initial_max_ranges = 1,
		.initial_ranges = &single_range_b, .initial_nr_ranges = 1,
		.expected_ranges = (const struct range[]){{ .start = 150, .end = 199 }},
		.expected_nr_ranges = 1,
		.expected_ret = 0,
	},
	{
		.description = "1.4: A is completely inside B",
		.exclude_start = 120, .exclude_end = 179,
		.initial_max_ranges = 2, // Needs space for split
		.initial_ranges = &single_range_b, .initial_nr_ranges = 1,
		.expected_ranges = (const struct range[]){
			{ .start = 100, .end = 119 },
			{ .start = 180, .end = 199 }
		},
		.expected_nr_ranges = 2,
		.expected_ret = 0,
	},

Annotation

Implementation Notes