tools/testing/selftests/mm/pkey_sighandler_tests.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/mm/pkey_sighandler_tests.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/mm/pkey_sighandler_tests.c- Extension
.c- Size
- 14829 bytes
- Lines
- 547
- 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/mman.herrno.hsys/syscall.hstring.hstdio.hstdint.hstdbool.hsignal.hassert.hstdlib.hsys/mman.hsys/types.hsys/stat.hunistd.hpthread.hlimits.hpkey-helpers.h
Detected Declarations
function syscall_rawfunction clone_rawfunction pkey_reg_restrictive_defaultfunction sigsegv_handlerfunction sigusr1_handlerfunction sigusr2_handlerfunction raise_sigusr2function test_sigsegv_handler_with_pkey0_disabledfunction test_sigsegv_handler_cannot_access_stackfunction test_sigsegv_handler_with_different_pkey_for_stackfunction test_pkru_preserved_after_sigusr1function test_pkru_sigreturnfunction main
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/*
* Tests Memory Protection Keys (see Documentation/core-api/protection-keys.rst)
*
* The testcases in this file exercise various flows related to signal handling,
* using an alternate signal stack, with the default pkey (pkey 0) disabled.
*
* Compile with:
* gcc -mxsave -o pkey_sighandler_tests -O2 -g -std=gnu99 -pthread -Wall pkey_sighandler_tests.c -I../../../../tools/include -lrt -ldl -lm
* gcc -mxsave -m32 -o pkey_sighandler_tests -O2 -g -std=gnu99 -pthread -Wall pkey_sighandler_tests.c -I../../../../tools/include -lrt -ldl -lm
*/
#define _GNU_SOURCE
#define __SANE_USERSPACE_TYPES__
#include <linux/mman.h>
#include <errno.h>
#include <sys/syscall.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <signal.h>
#include <assert.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pthread.h>
#include <limits.h>
#include "pkey-helpers.h"
#define STACK_SIZE PTHREAD_STACK_MIN
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
static siginfo_t siginfo = {0};
/*
* We need to use inline assembly instead of glibc's syscall because glibc's
* syscall will attempt to access the PLT in order to call a library function
* which is protected by MPK 0 which we don't have access to.
*/
static __always_inline
long syscall_raw(long n, long a1, long a2, long a3, long a4, long a5, long a6)
{
unsigned long ret;
#ifdef __x86_64__
register long r10 asm("r10") = a4;
register long r8 asm("r8") = a5;
register long r9 asm("r9") = a6;
asm volatile ("syscall"
: "=a"(ret)
: "a"(n), "D"(a1), "S"(a2), "d"(a3), "r"(r10), "r"(r8), "r"(r9)
: "rcx", "r11", "memory");
#elif defined __i386__
asm volatile ("int $0x80"
: "=a"(ret)
: "a"(n), "b"(a1), "c"(a2), "d"(a3), "S"(a4), "D"(a5)
: "memory");
#elif defined __aarch64__
register long x0 asm("x0") = a1;
register long x1 asm("x1") = a2;
register long x2 asm("x2") = a3;
register long x3 asm("x3") = a4;
register long x4 asm("x4") = a5;
register long x5 asm("x5") = a6;
register long x8 asm("x8") = n;
asm volatile ("svc #0"
: "=r"(x0)
: "r"(x0), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5), "r"(x8)
: "memory");
ret = x0;
#else
# error syscall_raw() not implemented
#endif
return ret;
}
static inline long clone_raw(unsigned long flags, void *stack,
int *parent_tid, int *child_tid)
{
long a1 = flags;
long a2 = (long)stack;
long a3 = (long)parent_tid;
#if defined(__x86_64__) || defined(__i386)
long a4 = (long)child_tid;
long a5 = 0;
#elif defined(__aarch64__)
long a4 = 0;
Annotation
- Immediate include surface: `linux/mman.h`, `errno.h`, `sys/syscall.h`, `string.h`, `stdio.h`, `stdint.h`, `stdbool.h`, `signal.h`.
- Detected declarations: `function syscall_raw`, `function clone_raw`, `function pkey_reg_restrictive_default`, `function sigsegv_handler`, `function sigusr1_handler`, `function sigusr2_handler`, `function raise_sigusr2`, `function test_sigsegv_handler_with_pkey0_disabled`, `function test_sigsegv_handler_cannot_access_stack`, `function test_sigsegv_handler_with_different_pkey_for_stack`.
- Atlas domain: Support Tooling And Documentation / tools.
- 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.