Skip to content

Commit e78cfcc

Browse files
committed
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 c2af6a7 commit e78cfcc

File tree

3 files changed

+477
-1
lines changed

3 files changed

+477
-1
lines changed

tools/testing/selftests/bpf/Makefile

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

460462
CLANG_CFLAGS = $(CLANG_SYS_INCLUDES)
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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_goto_x.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_goto_x *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_goto_x *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+
static void check_goto_x_skel(struct bpf_goto_x *skel)
60+
{
61+
int i;
62+
__u64 in[] = {0, 1, 2, 3, 4, 5, 77};
63+
__u64 out[] = {2, 3, 4, 5, 7, 19, 19};
64+
__u64 out2[] = {103, 104, 107, 205, 115, 1019, 1019};
65+
__u64 in3[] = {0, 11, 27, 31, 447, 22, 45, 999};
66+
__u64 out3[] = {2, 3, 4, 5, 7, 19, 19, 19};
67+
68+
for (i = 0; i < ARRAY_SIZE(in); i++)
69+
check_simple(skel, skel->progs.simple_test, in[i], out[i]);
70+
71+
for (i = 0; i < ARRAY_SIZE(in); i++)
72+
check_simple(skel, skel->progs.simple_test2, in[i], out[i]);
73+
74+
for (i = 0; i < ARRAY_SIZE(in); i++)
75+
check_simple(skel, skel->progs.two_switches, in[i], out2[i]);
76+
77+
for (i = 0; i < ARRAY_SIZE(in); i++)
78+
check_simple(skel, skel->progs.big_jump_table, in3[i], out3[i]);
79+
80+
for (i = 0; i < ARRAY_SIZE(in); i++)
81+
check_simple(skel, skel->progs.use_static_global1, in[i], out[i]);
82+
83+
for (i = 0; i < ARRAY_SIZE(in); i++)
84+
check_simple(skel, skel->progs.use_static_global2, in[i], out[i]);
85+
86+
for (i = 0; i < ARRAY_SIZE(in); i++)
87+
check_simple(skel, skel->progs.use_nonstatic_global1, in[i], out[i]);
88+
89+
for (i = 0; i < ARRAY_SIZE(in); i++)
90+
check_simple(skel, skel->progs.use_nonstatic_global2, in[i], out[i]);
91+
92+
bpf_program__attach(skel->progs.simple_test_other_sec);
93+
for (i = 0; i < ARRAY_SIZE(in); i++)
94+
check_simple_fentry(skel, skel->progs.simple_test_other_sec, in[i], out[i]);
95+
96+
bpf_program__attach(skel->progs.use_static_global_other_sec);
97+
for (i = 0; i < ARRAY_SIZE(in); i++)
98+
check_simple_fentry(skel, skel->progs.use_static_global_other_sec, in[i], out[i]);
99+
100+
bpf_program__attach(skel->progs.use_nonstatic_global_other_sec);
101+
for (i = 0; i < ARRAY_SIZE(in); i++)
102+
check_simple_fentry(skel, skel->progs.use_nonstatic_global_other_sec, in[i], out[i]);
103+
}
104+
105+
void goto_x_skel(void)
106+
{
107+
struct bpf_goto_x *skel;
108+
int ret;
109+
110+
skel = bpf_goto_x__open();
111+
if (!ASSERT_NEQ(skel, NULL, "bpf_goto_x__open"))
112+
return;
113+
114+
ret = bpf_goto_x__load(skel);
115+
if (!ASSERT_OK(ret, "bpf_goto_x__load"))
116+
return;
117+
118+
check_goto_x_skel(skel);
119+
120+
bpf_goto_x__destroy(skel);
121+
}
122+
123+
void test_bpf_goto_x(void)
124+
{
125+
if (test__start_subtest("goto_x_skel"))
126+
goto_x_skel();
127+
}

0 commit comments

Comments
 (0)