-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftests/bpf: Add global subprog context passing tests
Add tests validating that it's possible to pass context arguments into global subprogs for various types of programs, including a particularly tricky KPROBE programs (which cover kprobes, uprobes, USDTs, a vast and important class of programs). Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Stanislav Fomichev <sdf@google.com> Link: https://lore.kernel.org/bpf/20230216045954.3002473-4-andrii@kernel.org
- Loading branch information
Andrii Nakryiko
authored and
Daniel Borkmann
committed
Feb 17, 2023
1 parent
95ebb37
commit e2b5cfc
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
tools/testing/selftests/bpf/progs/test_global_func_ctx_args.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */ | ||
#include "vmlinux.h" | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_tracing.h> | ||
#include <bpf/bpf_core_read.h> | ||
#include "bpf_misc.h" | ||
|
||
char _license[] SEC("license") = "GPL"; | ||
|
||
static long stack[256]; | ||
|
||
/* | ||
* KPROBE contexts | ||
*/ | ||
|
||
__weak int kprobe_typedef_ctx_subprog(bpf_user_pt_regs_t *ctx) | ||
{ | ||
return bpf_get_stack(ctx, &stack, sizeof(stack), 0); | ||
} | ||
|
||
SEC("?kprobe") | ||
__success | ||
int kprobe_typedef_ctx(void *ctx) | ||
{ | ||
return kprobe_typedef_ctx_subprog(ctx); | ||
} | ||
|
||
#define pt_regs_struct_t typeof(*(__PT_REGS_CAST((struct pt_regs *)NULL))) | ||
|
||
__weak int kprobe_struct_ctx_subprog(pt_regs_struct_t *ctx) | ||
{ | ||
return bpf_get_stack((void *)ctx, &stack, sizeof(stack), 0); | ||
} | ||
|
||
SEC("?kprobe") | ||
__success | ||
int kprobe_resolved_ctx(void *ctx) | ||
{ | ||
return kprobe_struct_ctx_subprog(ctx); | ||
} | ||
|
||
/* this is current hack to make this work on old kernels */ | ||
struct bpf_user_pt_regs_t {}; | ||
|
||
__weak int kprobe_workaround_ctx_subprog(struct bpf_user_pt_regs_t *ctx) | ||
{ | ||
return bpf_get_stack(ctx, &stack, sizeof(stack), 0); | ||
} | ||
|
||
SEC("?kprobe") | ||
__success | ||
int kprobe_workaround_ctx(void *ctx) | ||
{ | ||
return kprobe_workaround_ctx_subprog(ctx); | ||
} | ||
|
||
/* | ||
* RAW_TRACEPOINT contexts | ||
*/ | ||
|
||
__weak int raw_tp_ctx_subprog(struct bpf_raw_tracepoint_args *ctx) | ||
{ | ||
return bpf_get_stack(ctx, &stack, sizeof(stack), 0); | ||
} | ||
|
||
SEC("?raw_tp") | ||
__success | ||
int raw_tp_ctx(void *ctx) | ||
{ | ||
return raw_tp_ctx_subprog(ctx); | ||
} | ||
|
||
/* | ||
* RAW_TRACEPOINT_WRITABLE contexts | ||
*/ | ||
|
||
__weak int raw_tp_writable_ctx_subprog(struct bpf_raw_tracepoint_args *ctx) | ||
{ | ||
return bpf_get_stack(ctx, &stack, sizeof(stack), 0); | ||
} | ||
|
||
SEC("?raw_tp") | ||
__success | ||
int raw_tp_writable_ctx(void *ctx) | ||
{ | ||
return raw_tp_writable_ctx_subprog(ctx); | ||
} | ||
|
||
/* | ||
* PERF_EVENT contexts | ||
*/ | ||
|
||
__weak int perf_event_ctx_subprog(struct bpf_perf_event_data *ctx) | ||
{ | ||
return bpf_get_stack(ctx, &stack, sizeof(stack), 0); | ||
} | ||
|
||
SEC("?perf_event") | ||
__success | ||
int perf_event_ctx(void *ctx) | ||
{ | ||
return perf_event_ctx_subprog(ctx); | ||
} |