tools/testing/selftests/bpf/progs/test_stack_var_off.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/test_stack_var_off.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/progs/test_stack_var_off.c- Extension
.c- Size
- 1405 bytes
- Lines
- 52
- 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.
Dependency Surface
linux/bpf.hbpf/bpf_helpers.h
Detected Declarations
function probe
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
int probe_res;
char input[4] = {};
int test_pid;
SEC("tracepoint/syscalls/sys_enter_nanosleep")
int probe(void *ctx)
{
/* This BPF program performs variable-offset reads and writes on a
* stack-allocated buffer.
*/
char stack_buf[16];
unsigned long len;
unsigned long last;
if ((bpf_get_current_pid_tgid() >> 32) != test_pid)
return 0;
/* Copy the input to the stack. */
__builtin_memcpy(stack_buf, input, 4);
/* The first byte in the buffer indicates the length. */
len = stack_buf[0] & 0xf;
last = (len - 1) & 0xf;
/* Append something to the buffer. The offset where we write is not
* statically known; this is a variable-offset stack write.
*/
stack_buf[len] = 42;
/* Index into the buffer at an unknown offset. This is a
* variable-offset stack read.
*
* Note that if it wasn't for the preceding variable-offset write, this
* read would be rejected because the stack slot cannot be verified as
* being initialized. With the preceding variable-offset write, the
* stack slot still cannot be verified, but the write inhibits the
* respective check on the reasoning that, if there was a
* variable-offset to a higher-or-equal spot, we're probably reading
* what we just wrote.
*/
probe_res = stack_buf[last];
return 0;
}
char _license[] SEC("license") = "GPL";
Annotation
- Immediate include surface: `linux/bpf.h`, `bpf/bpf_helpers.h`.
- Detected declarations: `function probe`.
- 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.