Skip to content

Commit 5d8b583

Browse files
Jiri OlsaAlexei Starovoitov
Jiri Olsa
authored and
Alexei Starovoitov
committed
selftests/bpf: Add test for bpf_get_func_ip helper
Adding test for bpf_get_func_ip helper for fentry, fexit, kprobe, kretprobe and fmod_ret programs. Signed-off-by: Jiri Olsa <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 9ffd9f3 commit 5d8b583

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <test_progs.h>
3+
#include "get_func_ip_test.skel.h"
4+
5+
void test_get_func_ip_test(void)
6+
{
7+
struct get_func_ip_test *skel = NULL;
8+
__u32 duration = 0, retval;
9+
int err, prog_fd;
10+
11+
skel = get_func_ip_test__open_and_load();
12+
if (!ASSERT_OK_PTR(skel, "get_func_ip_test__open_and_load"))
13+
return;
14+
15+
err = get_func_ip_test__attach(skel);
16+
if (!ASSERT_OK(err, "get_func_ip_test__attach"))
17+
goto cleanup;
18+
19+
prog_fd = bpf_program__fd(skel->progs.test1);
20+
err = bpf_prog_test_run(prog_fd, 1, NULL, 0,
21+
NULL, NULL, &retval, &duration);
22+
ASSERT_OK(err, "test_run");
23+
ASSERT_EQ(retval, 0, "test_run");
24+
25+
prog_fd = bpf_program__fd(skel->progs.test5);
26+
err = bpf_prog_test_run(prog_fd, 1, NULL, 0,
27+
NULL, NULL, &retval, &duration);
28+
29+
ASSERT_OK(err, "test_run");
30+
31+
ASSERT_EQ(skel->bss->test1_result, 1, "test1_result");
32+
ASSERT_EQ(skel->bss->test2_result, 1, "test2_result");
33+
ASSERT_EQ(skel->bss->test3_result, 1, "test3_result");
34+
ASSERT_EQ(skel->bss->test4_result, 1, "test4_result");
35+
ASSERT_EQ(skel->bss->test5_result, 1, "test5_result");
36+
37+
cleanup:
38+
get_func_ip_test__destroy(skel);
39+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <linux/bpf.h>
3+
#include <bpf/bpf_helpers.h>
4+
#include <bpf/bpf_tracing.h>
5+
6+
char _license[] SEC("license") = "GPL";
7+
8+
extern const void bpf_fentry_test1 __ksym;
9+
extern const void bpf_fentry_test2 __ksym;
10+
extern const void bpf_fentry_test3 __ksym;
11+
extern const void bpf_fentry_test4 __ksym;
12+
extern const void bpf_modify_return_test __ksym;
13+
14+
__u64 test1_result = 0;
15+
SEC("fentry/bpf_fentry_test1")
16+
int BPF_PROG(test1, int a)
17+
{
18+
__u64 addr = bpf_get_func_ip(ctx);
19+
20+
test1_result = (const void *) addr == &bpf_fentry_test1;
21+
return 0;
22+
}
23+
24+
__u64 test2_result = 0;
25+
SEC("fexit/bpf_fentry_test2")
26+
int BPF_PROG(test2, int a)
27+
{
28+
__u64 addr = bpf_get_func_ip(ctx);
29+
30+
test2_result = (const void *) addr == &bpf_fentry_test2;
31+
return 0;
32+
}
33+
34+
__u64 test3_result = 0;
35+
SEC("kprobe/bpf_fentry_test3")
36+
int test3(struct pt_regs *ctx)
37+
{
38+
__u64 addr = bpf_get_func_ip(ctx);
39+
40+
test3_result = (const void *) addr == &bpf_fentry_test3;
41+
return 0;
42+
}
43+
44+
__u64 test4_result = 0;
45+
SEC("kretprobe/bpf_fentry_test4")
46+
int BPF_KRETPROBE(test4)
47+
{
48+
__u64 addr = bpf_get_func_ip(ctx);
49+
50+
test4_result = (const void *) addr == &bpf_fentry_test4;
51+
return 0;
52+
}
53+
54+
__u64 test5_result = 0;
55+
SEC("fmod_ret/bpf_modify_return_test")
56+
int BPF_PROG(test5, int a, int *b, int ret)
57+
{
58+
__u64 addr = bpf_get_func_ip(ctx);
59+
60+
test5_result = (const void *) addr == &bpf_modify_return_test;
61+
return ret;
62+
}

0 commit comments

Comments
 (0)