[RFC bpf-next 9/9] selftests/bpf: add selftests for indirect jumps

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 <a.s.protopopov@xxxxxxxxx>
---
 tools/testing/selftests/bpf/Makefile          |   4 +-
 .../selftests/bpf/prog_tests/bpf_goto_x.c     | 127 +++++++
 .../testing/selftests/bpf/progs/bpf_goto_x.c  | 336 ++++++++++++++++++
 3 files changed, 466 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_goto_x.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_goto_x.c

diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index 97013c49920b..53ec703ba713 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -453,7 +453,9 @@ BPF_CFLAGS = -g -Wall -Werror -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN)	\
 	     -I$(abspath $(OUTPUT)/../usr/include)			\
 	     -std=gnu11		 					\
 	     -fno-strict-aliasing 					\
-	     -Wno-compare-distinct-pointer-types
+	     -Wno-compare-distinct-pointer-types			\
+	     -Wno-initializer-overrides					\
+	     #
 # TODO: enable me -Wsign-compare
 
 CLANG_CFLAGS = $(CLANG_SYS_INCLUDES)
diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_goto_x.c b/tools/testing/selftests/bpf/prog_tests/bpf_goto_x.c
new file mode 100644
index 000000000000..15781b6f8249
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_goto_x.c
@@ -0,0 +1,127 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+
+#include <linux/if_ether.h>
+#include <linux/in.h>
+#include <linux/ip.h>
+#include <linux/ipv6.h>
+#include <linux/in6.h>
+#include <linux/udp.h>
+#include <linux/tcp.h>
+
+#include <sys/syscall.h>
+#include <bpf/bpf.h>
+
+#include "bpf_goto_x.skel.h"
+
+static void __test_run(struct bpf_program *prog, void *ctx_in, size_t ctx_size_in)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, topts,
+			    .ctx_in = ctx_in,
+			    .ctx_size_in = ctx_size_in,
+		   );
+	int err, prog_fd;
+
+	prog_fd = bpf_program__fd(prog);
+	err = bpf_prog_test_run_opts(prog_fd, &topts);
+	ASSERT_OK(err, "test_run_opts err");
+}
+
+static void check_simple(struct bpf_goto_x *skel,
+			 struct bpf_program *prog,
+			 __u64 ctx_in,
+			 __u64 expected)
+{
+	skel->bss->ret_user = 0;
+
+	__test_run(prog, &ctx_in, sizeof(ctx_in));
+
+	if (!ASSERT_EQ(skel->bss->ret_user, expected, "skel->bss->ret_user"))
+		return;
+}
+
+static void check_simple_fentry(struct bpf_goto_x *skel,
+				struct bpf_program *prog,
+				__u64 ctx_in,
+				__u64 expected)
+{
+	skel->bss->in_user = ctx_in;
+	skel->bss->ret_user = 0;
+
+	/* trigger */
+	usleep(1);
+
+	if (!ASSERT_EQ(skel->bss->ret_user, expected, "skel->bss->ret_user"))
+		return;
+}
+
+static void check_goto_x_skel(struct bpf_goto_x *skel)
+{
+	int i;
+	__u64 in[]   = {0, 1, 2, 3, 4,  5, 77};
+	__u64 out[]  = {2, 3, 4, 5, 7, 19, 19};
+	__u64 out2[] = {103, 104, 107, 205, 115, 1019, 1019};
+	__u64 in3[]  = {0, 11, 27, 31, 447, 22, 45, 999};
+	__u64 out3[] = {2,  3,  4,  5,   7, 19, 19,  19};
+
+	for (i = 0; i < ARRAY_SIZE(in); i++)
+		check_simple(skel, skel->progs.simple_test, in[i], out[i]);
+
+	for (i = 0; i < ARRAY_SIZE(in); i++)
+		check_simple(skel, skel->progs.simple_test2, in[i], out[i]);
+
+	for (i = 0; i < ARRAY_SIZE(in); i++)
+		check_simple(skel, skel->progs.two_towers, in[i], out2[i]);
+
+	for (i = 0; i < ARRAY_SIZE(in); i++)
+		check_simple(skel, skel->progs.the_return_of_the_king, in3[i], out3[i]);
+
+	for (i = 0; i < ARRAY_SIZE(in); i++)
+		check_simple(skel, skel->progs.use_static_global1, in[i], out[i]);
+
+	for (i = 0; i < ARRAY_SIZE(in); i++)
+		check_simple(skel, skel->progs.use_static_global2, in[i], out[i]);
+
+	for (i = 0; i < ARRAY_SIZE(in); i++)
+		check_simple(skel, skel->progs.use_nonstatic_global1, in[i], out[i]);
+
+	for (i = 0; i < ARRAY_SIZE(in); i++)
+		check_simple(skel, skel->progs.use_nonstatic_global2, in[i], out[i]);
+
+	bpf_program__attach(skel->progs.simple_test_other_sec);
+	for (i = 0; i < ARRAY_SIZE(in); i++)
+		check_simple_fentry(skel, skel->progs.simple_test_other_sec, in[i], out[i]);
+
+	bpf_program__attach(skel->progs.use_static_global_other_sec);
+	for (i = 0; i < ARRAY_SIZE(in); i++)
+		check_simple_fentry(skel, skel->progs.use_static_global_other_sec, in[i], out[i]);
+
+	bpf_program__attach(skel->progs.use_nonstatic_global_other_sec);
+	for (i = 0; i < ARRAY_SIZE(in); i++)
+		check_simple_fentry(skel, skel->progs.use_nonstatic_global_other_sec, in[i], out[i]);
+}
+
+void goto_x_skel(void)
+{
+	struct bpf_goto_x *skel;
+	int ret;
+
+	skel = bpf_goto_x__open();
+	if (!ASSERT_NEQ(skel, NULL, "bpf_goto_x__open"))
+		return;
+
+	ret = bpf_goto_x__load(skel);
+	if (!ASSERT_OK(ret, "bpf_goto_x__load"))
+		return;
+
+	check_goto_x_skel(skel);
+
+	bpf_goto_x__destroy(skel);
+}
+
+void test_bpf_goto_x(void)
+{
+	if (test__start_subtest("goto_x_skel"))
+		goto_x_skel();
+}
diff --git a/tools/testing/selftests/bpf/progs/bpf_goto_x.c b/tools/testing/selftests/bpf/progs/bpf_goto_x.c
new file mode 100644
index 000000000000..ebe4239cfd24
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/bpf_goto_x.c
@@ -0,0 +1,336 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_core_read.h>
+#include "bpf_misc.h"
+
+__u64 in_user;
+__u64 ret_user;
+
+struct simple_ctx {
+	__u64 x;
+};
+
+SEC("syscall")
+int simple_test(struct simple_ctx *ctx)
+{
+	switch (ctx->x) {
+	case 0:
+		bpf_printk("%lu\n", ctx->x + 1);
+		ret_user = 2;
+		break;
+	case 1:
+		bpf_printk("%lu\n", ctx->x + 7);
+		ret_user = 3;
+		break;
+	case 2:
+		bpf_printk("%lu\n", ctx->x + 9);
+		ret_user = 4;
+		break;
+	case 3:
+		bpf_printk("%lu\n", ctx->x + 11);
+		ret_user = 5;
+		break;
+	case 4:
+		bpf_printk("%lu\n", ctx->x + 17);
+		ret_user = 7;
+		break;
+	default:
+		bpf_printk("%lu\n", ctx->x + 177);
+		ret_user = 19;
+		break;
+	}
+
+	return 0;
+}
+
+SEC("syscall")
+int simple_test2(struct simple_ctx *ctx)
+{
+	switch (ctx->x) {
+	case 0:
+		bpf_printk("%lu\n", ctx->x + 1);
+		ret_user = 2;
+		break;
+	case 1:
+		bpf_printk("%lu\n", ctx->x + 7);
+		ret_user = 3;
+		break;
+	case 2:
+		bpf_printk("%lu\n", ctx->x + 9);
+		ret_user = 4;
+		break;
+	case 3:
+		bpf_printk("%lu\n", ctx->x + 11);
+		ret_user = 5;
+		break;
+	case 4:
+		bpf_printk("%lu\n", ctx->x + 17);
+		ret_user = 7;
+		break;
+	default:
+		bpf_printk("%lu\n", ctx->x + 177);
+		ret_user = 19;
+		break;
+	}
+
+	return 0;
+}
+
+SEC("fentry/" SYS_PREFIX "sys_nanosleep")
+int simple_test_other_sec(struct pt_regs *ctx)
+{
+	__u64 x = in_user;
+
+	switch (x) {
+	case 0:
+		bpf_printk("%lu\n", x + 1);
+		ret_user = 2;
+		break;
+	case 1:
+		bpf_printk("%lu\n", x + 7);
+		ret_user = 3;
+		break;
+	case 2:
+		bpf_printk("%lu\n", x + 9);
+		ret_user = 4;
+		break;
+	case 3:
+		bpf_printk("%lu\n", x + 11);
+		ret_user = 5;
+		break;
+	case 4:
+		bpf_printk("%lu\n", x + 17);
+		ret_user = 7;
+		break;
+	default:
+		bpf_printk("%lu\n", x + 177);
+		ret_user = 19;
+		break;
+	}
+
+	return 0;
+}
+
+SEC("syscall")
+int two_towers(struct simple_ctx *ctx)
+{
+	switch (ctx->x) {
+	case 0:
+		bpf_printk("%lu\n", ctx->x + 1);
+		ret_user = 2;
+		break;
+	case 1:
+		bpf_printk("%lu\n", ctx->x + 7);
+		ret_user = 3;
+		break;
+	case 2:
+		bpf_printk("%lu\n", ctx->x + 9);
+		ret_user = 4;
+		break;
+	case 3:
+		bpf_printk("%lu\n", ctx->x + 11);
+		ret_user = 5;
+		break;
+	case 4:
+		bpf_printk("%lu\n", ctx->x + 17);
+		ret_user = 7;
+		break;
+	default:
+		bpf_printk("%lu\n", ctx->x + 177);
+		ret_user = 19;
+		break;
+	}
+
+	switch (ctx->x + !!ret_user) {
+	case 0: /* never happens */
+		bpf_printk("%lu\n", ctx->x + 1);
+		ret_user = 102;
+		break;
+	case 1:
+		bpf_printk("%lu\n", ctx->x + 7);
+		ret_user = 103;
+		break;
+	case 2:
+		bpf_printk("%lu\n", ctx->x + 9);
+		ret_user = 104;
+		break;
+	case 3:
+		bpf_printk("%lu\n", ctx->x + 11);
+		ret_user = 107;
+		break;
+	case 4:
+		bpf_printk("%lu\n", ctx->x + 11);
+		ret_user = 205;
+		break;
+	case 5:
+		bpf_printk("%lu\n", ctx->x + 11);
+		ret_user = 115;
+		break;
+	default:
+		bpf_printk("%lu\n", ctx->x + 177);
+		ret_user = 1019;
+		break;
+	}
+
+	return 0;
+}
+
+/* this actually creates a big insn_set map */
+SEC("syscall")
+int the_return_of_the_king(struct simple_ctx *ctx)
+{
+	switch (ctx->x) {
+	case 0:
+		bpf_printk("%lu\n", ctx->x + 1);
+		ret_user = 2;
+		break;
+	case 11:
+		bpf_printk("%lu\n", ctx->x + 7);
+		ret_user = 3;
+		break;
+	case 27:
+		bpf_printk("%lu\n", ctx->x + 9);
+		ret_user = 4;
+		break;
+	case 31:
+		bpf_printk("%lu\n", ctx->x + 11);
+		ret_user = 5;
+		break;
+	case 447:
+		bpf_printk("%lu\n", ctx->x + 17);
+		ret_user = 7;
+		break;
+	default:
+		bpf_printk("%lu\n", ctx->x + 177);
+		ret_user = 19;
+		break;
+	}
+
+	return 0;
+}
+
+/* Just to introduce some non-zero offsets in .text */
+static __noinline int i_am_a_little_tiny_foo(volatile struct simple_ctx *ctx __arg_ctx)
+{
+	if (ctx)
+		return 1;
+	else
+		return 13;
+}
+
+SEC("syscall") int just_me(struct simple_ctx *ctx)
+{
+	ret_user = 0;
+	return i_am_a_little_tiny_foo(ctx);
+}
+
+static __noinline int __static_global(__u64 x)
+{
+	switch (x) {
+	case 0:
+		bpf_printk("%lu\n", x + 1);
+		ret_user = 2;
+		break;
+	case 1:
+		bpf_printk("%lu\n", x + 7);
+		ret_user = 3;
+		break;
+	case 2:
+		bpf_printk("%lu\n", x + 9);
+		ret_user = 4;
+		break;
+	case 3:
+		bpf_printk("%lu\n", x + 11);
+		ret_user = 5;
+		break;
+	case 4:
+		bpf_printk("%lu\n", x + 17);
+		ret_user = 7;
+		break;
+	default:
+		bpf_printk("%lu\n", x + 177);
+		ret_user = 19;
+		break;
+	}
+
+	return 0;
+}
+
+SEC("syscall")
+int use_static_global1(struct simple_ctx *ctx)
+{
+	ret_user = 0;
+	return __static_global(ctx->x);
+}
+
+SEC("syscall")
+int use_static_global2(struct simple_ctx *ctx)
+{
+	ret_user = 0;
+	bpf_printk("%lu\n", ctx->x + 1);
+	return __static_global(ctx->x);
+}
+
+SEC("fentry/" SYS_PREFIX "sys_nanosleep")
+int use_static_global_other_sec(void *ctx)
+{
+	return __static_global(in_user);
+}
+
+__noinline int __gobble_till_you_global(__u64 x)
+{
+	switch (x) {
+	case 0:
+		bpf_printk("%lu\n", x + 1);
+		ret_user = 2;
+		break;
+	case 1:
+		bpf_printk("%lu\n", x + 7);
+		ret_user = 3;
+		break;
+	case 2:
+		bpf_printk("%lu\n", x + 9);
+		ret_user = 4;
+		break;
+	case 3:
+		bpf_printk("%lu\n", x + 11);
+		ret_user = 5;
+		break;
+	case 4:
+		bpf_printk("%lu\n", x + 17);
+		ret_user = 7;
+		break;
+	default:
+		bpf_printk("%lu\n", x + 177);
+		ret_user = 19;
+		break;
+	}
+
+	return 0;
+}
+
+SEC("syscall")
+int use_nonstatic_global1(struct simple_ctx *ctx)
+{
+	ret_user = 0;
+	return __gobble_till_you_global(ctx->x);
+}
+
+SEC("syscall")
+int use_nonstatic_global2(struct simple_ctx *ctx)
+{
+	ret_user = 0;
+	bpf_printk("%lu\n", ctx->x + 1);
+	return __gobble_till_you_global(ctx->x);
+}
+
+SEC("fentry/" SYS_PREFIX "sys_nanosleep")
+int use_nonstatic_global_other_sec(void *ctx)
+{
+	return __gobble_till_you_global(in_user);
+}
+
+char _license[] SEC("license") = "GPL";
-- 
2.34.1





[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux