mm/kmsan/kmsan_test.c
Source file repositories/reference/linux-study-clean/mm/kmsan/kmsan_test.c
File Facts
- System
- Linux kernel
- Corpus path
mm/kmsan/kmsan_test.c- Extension
.c- Size
- 22764 bytes
- Lines
- 814
- 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.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
kunit/test.hkmsan.hlinux/jiffies.hlinux/kernel.hlinux/kmsan.hlinux/mm.hlinux/random.hlinux/slab.hlinux/spinlock.hlinux/string.hlinux/tracepoint.hlinux/vmalloc.htrace/events/printk.h
Detected Declarations
struct expect_reportfunction probe_consolefunction report_availablefunction report_resetfunction report_matchesfunction check_truefunction check_falsefunction test_uninit_kmallocfunction test_init_kmallocfunction test_init_kzallocfunction test_uninit_stack_varfunction test_init_stack_varfunction two_param_fn_2function one_param_fnfunction two_param_fnfunction test_paramsfunction signed_sum3function test_uninit_multiple_paramsfunction do_uninit_local_arrayfunction test_uninit_kmsan_check_memoryfunction test_init_kmsan_vmap_vunmapfunction vmallocfunction test_uaffunction test_uninit_pagefunction test_uaf_pagesfunction test_uaf_high_order_pagesfunction test_percpu_propagatefunction test_printkfunction test_init_memcpyfunction test_memcpy_aligned_to_alignedfunction test_memcpy_aligned_to_unalignedfunction memcpyfunction test_memset_on_guarded_bufferfunction fibonaccifunction test_long_origin_chainfunction featuresfunction test_unpoison_memoryfunction test_copy_from_kernel_nofaultfunction test_initfunction test_exitfunction kmsan_suite_initfunction kmsan_suite_exit
Annotated Snippet
struct expect_report {
const char *error_type; /* Error type. */
/*
* Kernel symbol from the error header, or NULL if no report is
* expected.
*/
const char *symbol;
};
/* Check observed report matches information in @r. */
static bool report_matches(const struct expect_report *r)
{
typeof(observed.header) expected_header;
unsigned long flags;
bool ret = false;
const char *end;
char *cur;
/* Doubled-checked locking. */
if (!report_available() || !r->symbol)
return (!report_available() && !r->symbol);
/* Generate expected report contents. */
/* Title */
cur = expected_header;
end = ARRAY_END(expected_header);
cur += scnprintf(cur, end - cur, "BUG: KMSAN: %s", r->error_type);
scnprintf(cur, end - cur, " in %s", r->symbol);
/* The exact offset won't match, remove it; also strip module name. */
cur = strchr(expected_header, '+');
if (cur)
*cur = '\0';
spin_lock_irqsave(&observed.lock, flags);
if (!report_available())
goto out; /* A new report is being captured. */
/* Finally match expected output to what we actually observed. */
ret = strstr(observed.header, expected_header);
out:
spin_unlock_irqrestore(&observed.lock, flags);
return ret;
}
/* ===== Test cases ===== */
/* Prevent replacing branch with select in LLVM. */
static noinline void check_true(char *arg)
{
pr_info("%s is true\n", arg);
}
static noinline void check_false(char *arg)
{
pr_info("%s is false\n", arg);
}
#define USE(x) \
do { \
if (x) \
check_true(#x); \
else \
check_false(#x); \
} while (0)
#define EXPECTATION_ETYPE_FN(e, reason, fn) \
struct expect_report e = { \
.error_type = reason, \
.symbol = fn, \
}
#define EXPECTATION_NO_REPORT(e) EXPECTATION_ETYPE_FN(e, NULL, NULL)
#define EXPECTATION_UNINIT_VALUE_FN(e, fn) \
EXPECTATION_ETYPE_FN(e, "uninit-value", fn)
#define EXPECTATION_UNINIT_VALUE(e) EXPECTATION_UNINIT_VALUE_FN(e, __func__)
#define EXPECTATION_USE_AFTER_FREE(e) \
EXPECTATION_ETYPE_FN(e, "use-after-free", __func__)
/* Test case: ensure that kmalloc() returns uninitialized memory. */
static void test_uninit_kmalloc(struct kunit *test)
{
EXPECTATION_UNINIT_VALUE(expect);
int *ptr;
kunit_info(test, "uninitialized kmalloc test (UMR report)\n");
ptr = kmalloc_obj(*ptr);
Annotation
- Immediate include surface: `kunit/test.h`, `kmsan.h`, `linux/jiffies.h`, `linux/kernel.h`, `linux/kmsan.h`, `linux/mm.h`, `linux/random.h`, `linux/slab.h`.
- Detected declarations: `struct expect_report`, `function probe_console`, `function report_available`, `function report_reset`, `function report_matches`, `function check_true`, `function check_false`, `function test_uninit_kmalloc`, `function test_init_kmalloc`, `function test_init_kzalloc`.
- Atlas domain: Core OS / Memory Management.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.