Skip to content

Commit ea2ef3c

Browse files
aspskKernel Patches Daemon
authored andcommitted
selftests/bpf: add selftests for indirect jumps
Add selftests for indirect jumps. All the indirect jumps are generated from C switch statements, so, if compiled by a compiler which doesn't support indirect jumps, then should pass as well. Signed-off-by: Anton Protopopov <[email protected]>
1 parent ef1aaec commit ea2ef3c

File tree

3 files changed

+591
-1
lines changed

3 files changed

+591
-1
lines changed

tools/testing/selftests/bpf/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,9 @@ BPF_CFLAGS = -g -Wall -Werror -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) \
453453
-I$(abspath $(OUTPUT)/../usr/include) \
454454
-std=gnu11 \
455455
-fno-strict-aliasing \
456-
-Wno-compare-distinct-pointer-types
456+
-Wno-compare-distinct-pointer-types \
457+
-Wno-initializer-overrides \
458+
#
457459
# TODO: enable me -Wsign-compare
458460

459461
CLANG_CFLAGS = $(CLANG_SYS_INCLUDES)
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <test_progs.h>
4+
5+
#include <linux/if_ether.h>
6+
#include <linux/in.h>
7+
#include <linux/ip.h>
8+
#include <linux/ipv6.h>
9+
#include <linux/in6.h>
10+
#include <linux/udp.h>
11+
#include <linux/tcp.h>
12+
13+
#include <sys/syscall.h>
14+
#include <bpf/bpf.h>
15+
16+
#include "bpf_gotox.skel.h"
17+
18+
static void __test_run(struct bpf_program *prog, void *ctx_in, size_t ctx_size_in)
19+
{
20+
LIBBPF_OPTS(bpf_test_run_opts, topts,
21+
.ctx_in = ctx_in,
22+
.ctx_size_in = ctx_size_in,
23+
);
24+
int err, prog_fd;
25+
26+
prog_fd = bpf_program__fd(prog);
27+
err = bpf_prog_test_run_opts(prog_fd, &topts);
28+
ASSERT_OK(err, "test_run_opts err");
29+
}
30+
31+
static void check_simple(struct bpf_gotox *skel,
32+
struct bpf_program *prog,
33+
__u64 ctx_in,
34+
__u64 expected)
35+
{
36+
skel->bss->ret_user = 0;
37+
38+
__test_run(prog, &ctx_in, sizeof(ctx_in));
39+
40+
if (!ASSERT_EQ(skel->bss->ret_user, expected, "skel->bss->ret_user"))
41+
return;
42+
}
43+
44+
static void check_simple_fentry(struct bpf_gotox *skel,
45+
struct bpf_program *prog,
46+
__u64 ctx_in,
47+
__u64 expected)
48+
{
49+
skel->bss->in_user = ctx_in;
50+
skel->bss->ret_user = 0;
51+
52+
/* trigger */
53+
usleep(1);
54+
55+
if (!ASSERT_EQ(skel->bss->ret_user, expected, "skel->bss->ret_user"))
56+
return;
57+
}
58+
59+
/* validate that for two loads of the same jump table libbpf generates only one map */
60+
static void check_one_map_two_jumps(struct bpf_gotox *skel)
61+
{
62+
struct bpf_prog_info prog_info;
63+
struct bpf_map_info map_info;
64+
__u32 len;
65+
__u32 map_ids[16];
66+
int prog_fd, map_fd;
67+
int ret;
68+
int i;
69+
bool seen = false;
70+
71+
memset(&prog_info, 0, sizeof(prog_info));
72+
prog_info.map_ids = (long)map_ids;
73+
prog_info.nr_map_ids = ARRAY_SIZE(map_ids);
74+
prog_fd = bpf_program__fd(skel->progs.one_map_two_jumps);
75+
if (!ASSERT_GE(prog_fd, 0, "bpf_program__fd(one_map_two_jumps)"))
76+
return;
77+
78+
len = sizeof(prog_info);
79+
ret = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &len);
80+
if (!ASSERT_OK(ret, "bpf_obj_get_info_by_fd(prog_fd)"))
81+
return;
82+
83+
for (i = 0; i < prog_info.nr_map_ids; i++) {
84+
map_fd = bpf_map_get_fd_by_id(map_ids[i]);
85+
if (!ASSERT_GE(map_fd, 0, "bpf_program__fd(one_map_two_jumps)"))
86+
return;
87+
88+
len = sizeof(map_info);
89+
ret = bpf_obj_get_info_by_fd(map_fd, &map_info, &len);
90+
if (!ASSERT_OK(ret, "bpf_obj_get_info_by_fd(map_fd)")) {
91+
close(map_fd);
92+
return;
93+
}
94+
95+
if (map_info.type == BPF_MAP_TYPE_INSN_ARRAY) {
96+
if (!ASSERT_EQ(seen, false, "more than one INSN_ARRAY map")) {
97+
close(map_fd);
98+
return;
99+
}
100+
seen = true;
101+
}
102+
close(map_fd);
103+
}
104+
105+
ASSERT_EQ(seen, true, "no INSN_ARRAY map");
106+
}
107+
108+
static void check_gotox_skel(struct bpf_gotox *skel)
109+
{
110+
int i;
111+
__u64 in[] = {0, 1, 2, 3, 4, 5, 77};
112+
__u64 out[] = {2, 3, 4, 5, 7, 19, 19};
113+
__u64 out2[] = {103, 104, 107, 205, 115, 1019, 1019};
114+
__u64 in3[] = {0, 11, 27, 31, 22, 45, 99};
115+
__u64 out3[] = {2, 3, 4, 5, 19, 19, 19};
116+
__u64 in4[] = {0, 1, 2, 3, 4, 5, 77};
117+
__u64 out4[] = {12, 15, 7 , 15, 12, 15, 15};
118+
119+
for (i = 0; i < ARRAY_SIZE(in); i++)
120+
check_simple(skel, skel->progs.simple_test, in[i], out[i]);
121+
122+
for (i = 0; i < ARRAY_SIZE(in); i++)
123+
check_simple(skel, skel->progs.simple_test2, in[i], out[i]);
124+
125+
for (i = 0; i < ARRAY_SIZE(in); i++)
126+
check_simple(skel, skel->progs.two_switches, in[i], out2[i]);
127+
128+
if (0) for (i = 0; i < ARRAY_SIZE(in); i++)
129+
check_simple(skel, skel->progs.big_jump_table, in3[i], out3[i]);
130+
131+
if (0) for (i = 0; i < ARRAY_SIZE(in); i++)
132+
check_simple(skel, skel->progs.one_jump_two_maps, in4[i], out4[i]);
133+
134+
for (i = 0; i < ARRAY_SIZE(in); i++)
135+
check_simple(skel, skel->progs.use_static_global1, in[i], out[i]);
136+
137+
for (i = 0; i < ARRAY_SIZE(in); i++)
138+
check_simple(skel, skel->progs.use_static_global2, in[i], out[i]);
139+
140+
for (i = 0; i < ARRAY_SIZE(in); i++)
141+
check_simple(skel, skel->progs.use_nonstatic_global1, in[i], out[i]);
142+
143+
for (i = 0; i < ARRAY_SIZE(in); i++)
144+
check_simple(skel, skel->progs.use_nonstatic_global2, in[i], out[i]);
145+
146+
bpf_program__attach(skel->progs.simple_test_other_sec);
147+
for (i = 0; i < ARRAY_SIZE(in); i++)
148+
check_simple_fentry(skel, skel->progs.simple_test_other_sec, in[i], out[i]);
149+
150+
bpf_program__attach(skel->progs.use_static_global_other_sec);
151+
for (i = 0; i < ARRAY_SIZE(in); i++)
152+
check_simple_fentry(skel, skel->progs.use_static_global_other_sec, in[i], out[i]);
153+
154+
bpf_program__attach(skel->progs.use_nonstatic_global_other_sec);
155+
for (i = 0; i < ARRAY_SIZE(in); i++)
156+
check_simple_fentry(skel, skel->progs.use_nonstatic_global_other_sec, in[i], out[i]);
157+
158+
if (0) check_one_map_two_jumps(skel);
159+
}
160+
161+
void test_bpf_gotox(void)
162+
{
163+
struct bpf_gotox *skel;
164+
int ret;
165+
166+
skel = bpf_gotox__open();
167+
if (!ASSERT_NEQ(skel, NULL, "bpf_gotox__open"))
168+
return;
169+
170+
ret = bpf_gotox__load(skel);
171+
if (!ASSERT_OK(ret, "bpf_gotox__load"))
172+
return;
173+
174+
check_gotox_skel(skel);
175+
176+
bpf_gotox__destroy(skel);
177+
}

0 commit comments

Comments
 (0)