mm/kfence/kfence_test.c
Source file repositories/reference/linux-study-clean/mm/kfence/kfence_test.c
File Facts
- System
- Linux kernel
- Corpus path
mm/kfence/kfence_test.c- Extension
.c- Size
- 24490 bytes
- Lines
- 875
- 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.hlinux/jiffies.hlinux/kernel.hlinux/kfence.hlinux/mm.hlinux/random.hlinux/slab.hlinux/spinlock.hlinux/string.hlinux/string_choices.hlinux/tracepoint.htrace/events/printk.hasm/kfence.hkfence.h
Detected Declarations
struct expect_reportenum allocation_policyfunction probe_consolefunction report_availablefunction report_matchesfunction setup_test_cachefunction test_cache_destroyfunction kmalloc_cache_alignmentfunction test_freefunction test_out_of_bounds_readfunction test_out_of_bounds_writefunction test_use_after_free_readfunction test_use_after_free_read_nofaultfunction test_double_freefunction test_invalid_addr_freefunction test_corruptionfunction test_kmalloc_aligned_oob_readfunction test_kmalloc_aligned_oob_writefunction test_shrink_memcachefunction ctor_set_xfunction test_free_bulkfunction test_init_on_freefunction test_memcache_ctorfunction test_gfpzerofunction test_invalid_accessfunction test_memcache_typesafe_by_rcufunction test_kreallocfunction test_memcache_alloc_bulkfunction test_initfunction test_exitfunction kfence_suite_initfunction kfence_suite_exit
Annotated Snippet
struct expect_report {
enum kfence_error_type type; /* The type or error. */
void *fn; /* Function pointer to expected function where access occurred. */
char *addr; /* Address at which the bad access occurred. */
bool is_write; /* Is access a write. */
};
static const char *get_access_type(const struct expect_report *r)
{
return str_write_read(r->is_write);
}
/* Check observed report matches information in @r. */
static bool report_matches(const struct expect_report *r)
{
unsigned long addr = (unsigned long)r->addr;
bool ret = false;
unsigned long flags;
typeof(observed.lines) expect;
const char *end;
char *cur;
/* Doubled-checked locking. */
if (!report_available())
return false;
/* Generate expected report contents. */
/* Title */
cur = expect[0];
end = ARRAY_END(expect[0]);
switch (r->type) {
case KFENCE_ERROR_OOB:
cur += scnprintf(cur, end - cur, "BUG: KFENCE: out-of-bounds %s",
get_access_type(r));
break;
case KFENCE_ERROR_UAF:
cur += scnprintf(cur, end - cur, "BUG: KFENCE: use-after-free %s",
get_access_type(r));
break;
case KFENCE_ERROR_CORRUPTION:
cur += scnprintf(cur, end - cur, "BUG: KFENCE: memory corruption");
break;
case KFENCE_ERROR_INVALID:
cur += scnprintf(cur, end - cur, "BUG: KFENCE: invalid %s",
get_access_type(r));
break;
case KFENCE_ERROR_INVALID_FREE:
cur += scnprintf(cur, end - cur, "BUG: KFENCE: invalid free");
break;
}
scnprintf(cur, end - cur, " in %pS", r->fn);
/* The exact offset won't match, remove it; also strip module name. */
cur = strchr(expect[0], '+');
if (cur)
*cur = '\0';
/* Access information */
cur = expect[1];
end = ARRAY_END(expect[1]);
switch (r->type) {
case KFENCE_ERROR_OOB:
cur += scnprintf(cur, end - cur, "Out-of-bounds %s at", get_access_type(r));
addr = arch_kfence_test_address(addr);
break;
case KFENCE_ERROR_UAF:
cur += scnprintf(cur, end - cur, "Use-after-free %s at", get_access_type(r));
addr = arch_kfence_test_address(addr);
break;
case KFENCE_ERROR_CORRUPTION:
cur += scnprintf(cur, end - cur, "Corrupted memory at");
break;
case KFENCE_ERROR_INVALID:
cur += scnprintf(cur, end - cur, "Invalid %s at", get_access_type(r));
addr = arch_kfence_test_address(addr);
break;
case KFENCE_ERROR_INVALID_FREE:
cur += scnprintf(cur, end - cur, "Invalid free of");
break;
}
cur += scnprintf(cur, end - cur, " 0x%p", (void *)addr);
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. */
Annotation
- Immediate include surface: `kunit/test.h`, `linux/jiffies.h`, `linux/kernel.h`, `linux/kfence.h`, `linux/mm.h`, `linux/random.h`, `linux/slab.h`, `linux/spinlock.h`.
- Detected declarations: `struct expect_report`, `enum allocation_policy`, `function probe_console`, `function report_available`, `function report_matches`, `function setup_test_cache`, `function test_cache_destroy`, `function kmalloc_cache_alignment`, `function test_free`, `function test_out_of_bounds_read`.
- 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.