Skip to content

Commit 1b881ee

Browse files
olsajiriAlexei Starovoitov
authored andcommitted
selftests/bpf: Add kprobe write ctx attach test
Adding test to check we can't attach standard kprobe program that writes to the context. It's x86_64 specific test. Acked-by: Andrii Nakryiko <[email protected]> Signed-off-by: Jiri Olsa <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 6a4ea0d commit 1b881ee

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

tools/testing/selftests/bpf/prog_tests/attach_probe.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "test_attach_kprobe_sleepable.skel.h"
44
#include "test_attach_probe_manual.skel.h"
55
#include "test_attach_probe.skel.h"
6+
#include "kprobe_write_ctx.skel.h"
67

78
/* this is how USDT semaphore is actually defined, except volatile modifier */
89
volatile unsigned short uprobe_ref_ctr __attribute__((unused)) __attribute((section(".probes")));
@@ -201,6 +202,31 @@ static void test_attach_kprobe_long_event_name(void)
201202
test_attach_probe_manual__destroy(skel);
202203
}
203204

205+
#ifdef __x86_64__
206+
/* attach kprobe/kretprobe long event name testings */
207+
static void test_attach_kprobe_write_ctx(void)
208+
{
209+
struct kprobe_write_ctx *skel = NULL;
210+
struct bpf_link *link = NULL;
211+
212+
skel = kprobe_write_ctx__open_and_load();
213+
if (!ASSERT_OK_PTR(skel, "kprobe_write_ctx__open_and_load"))
214+
return;
215+
216+
link = bpf_program__attach_kprobe_opts(skel->progs.kprobe_write_ctx,
217+
"bpf_fentry_test1", NULL);
218+
if (!ASSERT_ERR_PTR(link, "bpf_program__attach_kprobe_opts"))
219+
bpf_link__destroy(link);
220+
221+
kprobe_write_ctx__destroy(skel);
222+
}
223+
#else
224+
static void test_attach_kprobe_write_ctx(void)
225+
{
226+
test__skip();
227+
}
228+
#endif
229+
204230
static void test_attach_probe_auto(struct test_attach_probe *skel)
205231
{
206232
struct bpf_link *uprobe_err_link;
@@ -406,6 +432,8 @@ void test_attach_probe(void)
406432
test_attach_uprobe_long_event_name();
407433
if (test__start_subtest("kprobe-long_name"))
408434
test_attach_kprobe_long_event_name();
435+
if (test__start_subtest("kprobe-write-ctx"))
436+
test_attach_kprobe_write_ctx();
409437

410438
cleanup:
411439
test_attach_probe__destroy(skel);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include "vmlinux.h"
3+
#include <bpf/bpf_helpers.h>
4+
#include <bpf/bpf_tracing.h>
5+
6+
char _license[] SEC("license") = "GPL";
7+
8+
#if defined(__TARGET_ARCH_x86)
9+
SEC("kprobe")
10+
int kprobe_write_ctx(struct pt_regs *ctx)
11+
{
12+
ctx->ax = 0;
13+
return 0;
14+
}
15+
#endif

0 commit comments

Comments
 (0)