tools/testing/selftests/x86/test_shadow_stack.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/x86/test_shadow_stack.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/x86/test_shadow_stack.c- Extension
.c- Size
- 23346 bytes
- Lines
- 1089
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
sys/syscall.hasm/mman.hsys/mman.hsys/stat.hsys/wait.hstdio.hstdlib.hfcntl.hunistd.hstring.herrno.hstdbool.hx86intrin.hasm/prctl.hsys/prctl.hstdint.hsignal.hpthread.hsys/ioctl.hlinux/userfaultfd.hsetjmp.hsys/ptrace.hsys/signal.hlinux/elf.hlinux/perf_event.h
Detected Declarations
struct nodefunction arch_prctlfunction write_shstkfunction __attribute__function free_shstkfunction reset_shstkfunction try_shstkfunction test_shstk_pivotfunction test_shstk_faultsfunction __attribute__function segv_handlerfunction test_shstk_violationfunction reset_test_shstkfunction test_access_fix_handlerfunction test_shstk_accessfunction test_write_accessfunction gup_writefunction gup_readfunction test_gupfunction test_mprotectfunction test_userfaultfdfunction test_guard_gap_other_gapsfunction test_guard_gap_new_mappings_gapsfunction sigaction32function segv_gp_handlerfunction test_32bitfunction parse_uint_from_filefunction determine_uprobe_perf_typefunction determine_uprobe_retprobe_bitfunction get_uprobe_offsetfunction __attribute__function test_uretprobefunction segv_handler_ptracefunction test_ptracefunction main
Annotated Snippet
struct node {
struct node *next;
void *mapping;
};
/*
* This tests whether mmap will place other mappings in a shadow stack's guard
* gap. The steps are:
* 1. Finds an empty place by mapping and unmapping something.
* 2. Map a shadow stack in the middle of the known empty area.
* 3. Map a bunch of PAGE_SIZE mappings. These will use the search down
* direction, filling any gaps until it encounters the shadow stack's
* guard gap.
* 4. When a mapping lands below the shadow stack from step 2, then all
* of the above gaps are filled. The search down algorithm will have
* looked at the shadow stack gaps.
* 5. See if it landed in the gap.
*/
int test_guard_gap_other_gaps(void)
{
void *free_area, *shstk, *test_map = (void *)0xFFFFFFFFFFFFFFFF;
struct node *head = NULL, *cur;
free_area = mmap(0, SS_SIZE * 3, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
munmap(free_area, SS_SIZE * 3);
shstk = create_shstk(free_area + SS_SIZE);
if (shstk == MAP_FAILED)
return 1;
while (test_map > shstk) {
test_map = mmap(0, PAGE_SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (test_map == MAP_FAILED)
return 1;
cur = malloc(sizeof(*cur));
cur->mapping = test_map;
cur->next = head;
head = cur;
}
while (head) {
cur = head;
head = cur->next;
munmap(cur->mapping, PAGE_SIZE);
free(cur);
}
free_shstk(shstk);
if (shstk - test_map - PAGE_SIZE != PAGE_SIZE)
return 1;
printf("[OK]\tGuard gap test, other mapping's gaps\n");
return 0;
}
/* Tests respecting the guard gap of the mapping getting placed */
int test_guard_gap_new_mappings_gaps(void)
{
void *free_area, *shstk_start, *test_map = (void *)0xFFFFFFFFFFFFFFFF;
struct node *head = NULL, *cur;
int ret = 0;
free_area = mmap(0, PAGE_SIZE * 4, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
munmap(free_area, PAGE_SIZE * 4);
/* Test letting map_shadow_stack find a free space */
shstk_start = mmap(free_area, PAGE_SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (shstk_start == MAP_FAILED || shstk_start != free_area)
return 1;
while (test_map > shstk_start) {
test_map = (void *)syscall(__NR_map_shadow_stack, 0, PAGE_SIZE, 0);
if (test_map == MAP_FAILED) {
printf("[INFO]\tmap_shadow_stack MAP_FAILED\n");
ret = 1;
break;
}
cur = malloc(sizeof(*cur));
cur->mapping = test_map;
cur->next = head;
head = cur;
Annotation
- Immediate include surface: `sys/syscall.h`, `asm/mman.h`, `sys/mman.h`, `sys/stat.h`, `sys/wait.h`, `stdio.h`, `stdlib.h`, `fcntl.h`.
- Detected declarations: `struct node`, `function arch_prctl`, `function write_shstk`, `function __attribute__`, `function free_shstk`, `function reset_shstk`, `function try_shstk`, `function test_shstk_pivot`, `function test_shstk_faults`, `function __attribute__`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
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.