Skip to content

Commit 8aca600

Browse files
Kui-Feng LeeKernel Patches Daemon
authored andcommitted
selftest/bpf: The test cses of BPF cookie for fentry/fexit/fmod_ret.
Make sure BPF cookies are correct for fentry/fexit/fmod_ret. Signed-off-by: Kui-Feng Lee <[email protected]>
1 parent c6eb018 commit 8aca600

File tree

2 files changed

+85
-8
lines changed

2 files changed

+85
-8
lines changed

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,57 @@ static void pe_subtest(struct test_bpf_cookie *skel)
410410
bpf_link__destroy(link);
411411
}
412412

413+
static void tracing_subtest(struct test_bpf_cookie *skel)
414+
{
415+
__u64 cookie;
416+
int prog_fd;
417+
int fentry_fd = -1, fexit_fd = -1, fmod_ret_fd = -1;
418+
LIBBPF_OPTS(bpf_test_run_opts, opts);
419+
LIBBPF_OPTS(bpf_link_create_opts, link_opts);
420+
421+
skel->bss->fentry_res = 0;
422+
skel->bss->fexit_res = 0;
423+
424+
cookie = 0x10000000000000L;
425+
prog_fd = bpf_program__fd(skel->progs.fentry_test1);
426+
link_opts.tracing.cookie = cookie;
427+
fentry_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_FENTRY, &link_opts);
428+
if (!ASSERT_GE(fentry_fd, 0, "fentry.link_create"))
429+
goto cleanup;
430+
431+
cookie = 0x20000000000000L;
432+
prog_fd = bpf_program__fd(skel->progs.fexit_test1);
433+
link_opts.tracing.cookie = cookie;
434+
fexit_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_FEXIT, &link_opts);
435+
if (!ASSERT_GE(fexit_fd, 0, "fexit.link_create"))
436+
goto cleanup;
437+
438+
cookie = 0x30000000000000L;
439+
prog_fd = bpf_program__fd(skel->progs.fmod_ret_test);
440+
link_opts.tracing.cookie = cookie;
441+
fmod_ret_fd = bpf_link_create(prog_fd, 0, BPF_MODIFY_RETURN, &link_opts);
442+
if (!ASSERT_GE(fmod_ret_fd, 0, "fmod_ret.link_create"))
443+
goto cleanup;
444+
445+
prog_fd = bpf_program__fd(skel->progs.fentry_test1);
446+
bpf_prog_test_run_opts(prog_fd, &opts);
447+
448+
prog_fd = bpf_program__fd(skel->progs.fmod_ret_test);
449+
bpf_prog_test_run_opts(prog_fd, &opts);
450+
451+
ASSERT_EQ(skel->bss->fentry_res, 0x10000000000000L, "fentry_res");
452+
ASSERT_EQ(skel->bss->fexit_res, 0x20000000000000L, "fexit_res");
453+
ASSERT_EQ(skel->bss->fmod_ret_res, 0x30000000000000L, "fmod_ret_res");
454+
455+
cleanup:
456+
if (fentry_fd >= 0)
457+
close(fentry_fd);
458+
if (fexit_fd >= 0)
459+
close(fexit_fd);
460+
if (fmod_ret_fd >= 0)
461+
close(fmod_ret_fd);
462+
}
463+
413464
void test_bpf_cookie(void)
414465
{
415466
struct test_bpf_cookie *skel;
@@ -432,6 +483,8 @@ void test_bpf_cookie(void)
432483
tp_subtest(skel);
433484
if (test__start_subtest("perf_event"))
434485
pe_subtest(skel);
486+
if (test__start_subtest("trampoline"))
487+
tracing_subtest(skel);
435488

436489
test_bpf_cookie__destroy(skel);
437490
}

tools/testing/selftests/bpf/progs/test_bpf_cookie.c

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77

88
int my_tid;
99

10-
int kprobe_res;
11-
int kprobe_multi_res;
12-
int kretprobe_res;
13-
int uprobe_res;
14-
int uretprobe_res;
15-
int tp_res;
16-
int pe_res;
10+
__u64 kprobe_res;
11+
__u64 kprobe_multi_res;
12+
__u64 kretprobe_res;
13+
__u64 uprobe_res;
14+
__u64 uretprobe_res;
15+
__u64 tp_res;
16+
__u64 pe_res;
17+
__u64 fentry_res;
18+
__u64 fexit_res;
19+
__u64 fmod_ret_res;
1720

18-
static void update(void *ctx, int *res)
21+
static void update(void *ctx, __u64 *res)
1922
{
2023
if (my_tid != (u32)bpf_get_current_pid_tgid())
2124
return;
@@ -82,4 +85,25 @@ int handle_pe(struct pt_regs *ctx)
8285
return 0;
8386
}
8487

88+
SEC("fentry/bpf_fentry_test1")
89+
int BPF_PROG(fentry_test1, int a)
90+
{
91+
update(ctx, &fentry_res);
92+
return 0;
93+
}
94+
95+
SEC("fexit/bpf_fentry_test1")
96+
int BPF_PROG(fexit_test1, int a, int ret)
97+
{
98+
update(ctx, &fexit_res);
99+
return 0;
100+
}
101+
102+
SEC("fmod_ret/bpf_modify_return_test")
103+
int BPF_PROG(fmod_ret_test, int _a, int *_b, int _ret)
104+
{
105+
update(ctx, &fmod_ret_res);
106+
return 1234;
107+
}
108+
85109
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)