mm/kasan/kasan_test_c.c

Source file repositories/reference/linux-study-clean/mm/kasan/kasan_test_c.c

File Facts

System
Linux kernel
Corpus path
mm/kasan/kasan_test_c.c
Extension
.c
Size
63932 bytes
Lines
2298
Domain
Core OS
Bucket
Memory Management
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

kasan_sync_fault_possible()) {				\
		if (READ_ONCE(test_status.report_found) &&		\
		    !READ_ONCE(test_status.async_fault))		\
			kasan_enable_hw_tags();				\
		migrate_enable();					\
	}								\
	WRITE_ONCE(test_status.report_found, false);			\
	WRITE_ONCE(test_status.async_fault, false);			\
} while (0)

/*
 * KUNIT_EXPECT_KASAN_FAIL - check that the executed expression produces a
 * KASAN report; causes a KUnit test failure otherwise.
 *
 * @test: Currently executing KUnit test.
 * @expr: Expression that must produce a KASAN report.
 */
#define KUNIT_EXPECT_KASAN_FAIL(test, expr)			\
	KUNIT_EXPECT_KASAN_RESULT(test, expr, #expr, true)

/*
 * KUNIT_EXPECT_KASAN_FAIL_READ - check that the executed expression
 * produces a KASAN report when the write-only mode is not enabled;
 * causes a KUnit test failure otherwise.
 *
 * Note: At the moment, this macro does not check whether the produced
 * KASAN report is a report about a bad read access. It is only intended
 * for checking the write-only KASAN mode functionality without failing
 * KASAN tests.
 *
 * @test: Currently executing KUnit test.
 * @expr: Expression that must only produce a KASAN report
 *        when the write-only mode is not enabled.
 */
#define KUNIT_EXPECT_KASAN_FAIL_READ(test, expr)			\
	KUNIT_EXPECT_KASAN_RESULT(test, expr, #expr,			\
			!kasan_write_only_enabled())			\

#define KASAN_TEST_NEEDS_CONFIG_ON(test, config) do {			\
	if (!IS_ENABLED(config))					\
		kunit_skip((test), "Test requires " #config "=y");	\
} while (0)

#define KASAN_TEST_NEEDS_CONFIG_OFF(test, config) do {			\
	if (IS_ENABLED(config))						\
		kunit_skip((test), "Test requires " #config "=n");	\
} while (0)

#define KASAN_TEST_NEEDS_CHECKED_MEMINTRINSICS(test) do {		\
	if (IS_ENABLED(CONFIG_KASAN_HW_TAGS))				\
		break;  /* No compiler instrumentation. */		\
	if (IS_ENABLED(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX))	\
		break;  /* Should always be instrumented! */		\
	if (IS_ENABLED(CONFIG_GENERIC_ENTRY))				\
		kunit_skip((test), "Test requires checked mem*()");	\
} while (0)

static void kmalloc_oob_right(struct kunit *test)
{
	char *ptr;
	size_t size = 128 - KASAN_GRANULE_SIZE - 5;

	ptr = kmalloc(size, GFP_KERNEL);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);

	OPTIMIZER_HIDE_VAR(ptr);
	/*
	 * An unaligned access past the requested kmalloc size.
	 * Only generic KASAN can precisely detect these.
	 */
	if (IS_ENABLED(CONFIG_KASAN_GENERIC))
		KUNIT_EXPECT_KASAN_FAIL(test, ptr[size] = 'x');

	/*
	 * An aligned access into the first out-of-bounds granule that falls
	 * within the aligned kmalloc object.
	 */
	KUNIT_EXPECT_KASAN_FAIL(test, ptr[size + 5] = 'y');

	/* Out-of-bounds access past the aligned kmalloc object. */
	KUNIT_EXPECT_KASAN_FAIL_READ(test, ptr[0] =
			ptr[size + KASAN_GRANULE_SIZE + 5]);

	kfree(ptr);
}

static void kmalloc_oob_left(struct kunit *test)
{
	char *ptr;
	size_t size = 15;

Annotation

Implementation Notes