diff mbox series

[v1,bpf-next,4/9] selftests/bpf: Add tests for instructions mappings

Message ID 20240202162813.4184616-5-aspsk@isovalent.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series BPF static branches | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR fail PR summary
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit fail Errors and warnings before: 29 this patch: 21
netdev/build_tools success Errors and warnings before: 1 this patch: 0
netdev/cc_maintainers success CCed 0 of 0 maintainers
netdev/build_clang fail Errors and warnings before: 22 this patch: 22
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn fail Errors and warnings before: 52 this patch: 21
netdev/checkpatch warning CHECK: Alignment should match open parenthesis WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? WARNING: line length of 84 exceeds 80 columns WARNING: line length of 95 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline fail Was 0 now: 5
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-4 fail Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test
bpf/vmtest-bpf-next-VM_Test-10 success Logs for s390x-gcc / test
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-12 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-13 fail Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-15 success Logs for x86_64-gcc / test
bpf/vmtest-bpf-next-VM_Test-16 success Logs for x86_64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-17 fail Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-llvm-17 / test
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-21 fail Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-8 fail Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-llvm-18 / test
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-22 fail Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18 and -O2 optimization
bpf/vmtest-bpf-next-VM_Test-18 fail Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17 and -O2 optimization

Commit Message

Anton Protopopov Feb. 2, 2024, 4:28 p.m. UTC
Add several self-tests to test the instructions mappings from xlated
to original:

    * check that mappings work for a program without patches
    * same for a program with patches to insns
    * same for a program with patches to insns and bpf-to-bpf calls
    * same for a program with patches and deletions
    * same for a program with patches, deletions, and bpf-to-bpf calls

Signed-off-by: Anton Protopopov <aspsk@isovalent.com>
---
 .../bpf/prog_tests/bpf_insns_mappings.c       | 156 ++++++++++++++++++
 .../selftests/bpf/progs/bpf_insns_mappings.c  | 155 +++++++++++++++++
 2 files changed, 311 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_insns_mappings.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_insns_mappings.c
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_insns_mappings.c b/tools/testing/selftests/bpf/prog_tests/bpf_insns_mappings.c
new file mode 100644
index 000000000000..2a7b53231080
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_insns_mappings.c
@@ -0,0 +1,156 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2024 Isovalent */
+
+#include <test_progs.h>
+#include "bpf_insns_mappings.skel.h"
+
+#define MAX_INSNS 4096
+
+static struct bpf_prog_info *prog_info_and_mappings(int prog_fd)
+{
+	static __thread struct bpf_prog_info prog_info;
+	static __thread char xlated_insns[MAX_INSNS];
+	static __thread __u32 orig_idx[MAX_INSNS];
+	__u32 prog_info_len;
+	__u32 orig_idx_len;
+	int err;
+
+	prog_info_len = sizeof(prog_info);
+
+	memset(&prog_info, 0, sizeof(prog_info));
+	err = bpf_prog_get_info_by_fd(prog_fd, &prog_info, &prog_info_len);
+	if (!ASSERT_GE(err, 0, "bpf_prog_get_info_by_fd"))
+		return NULL;
+
+	orig_idx_len = prog_info.orig_idx_len;
+	memset(&prog_info, 0, sizeof(prog_info));
+
+	if (orig_idx_len) {
+		prog_info.orig_idx_len = orig_idx_len;
+		prog_info.orig_idx = ptr_to_u64(orig_idx);
+	}
+
+	prog_info.xlated_prog_insns = ptr_to_u64(xlated_insns);
+	prog_info.xlated_prog_len = sizeof(xlated_insns);
+
+	err = bpf_prog_get_info_by_fd(prog_fd, &prog_info, &prog_info_len);
+	if (!ASSERT_GE(err, 0, "bpf_prog_get_info_by_fd"))
+		return NULL;
+
+	return &prog_info;
+}
+
+static int beef_search_original(const struct bpf_insn *insns, int n_insns, int *idx, int n_max)
+{
+	int i, n_found = 0;
+
+	for (i = 0; i < n_insns; i++) {
+		if (insns[i].imm == 0xbeef) {
+			if (!ASSERT_LT(n_found, n_max, "beef"))
+				return -1;
+			idx[n_found++] = i;
+		}
+	}
+
+	return n_found;
+}
+
+static int beef_search_xlated(struct bpf_prog_info *info, int *idx, int len)
+{
+	struct bpf_insn *insns = u64_to_ptr(info->xlated_prog_insns);
+	int tot = info->xlated_prog_len / 8;
+	int i, n = 0;
+
+	for (i = 0; i < tot; i++) {
+		if (insns[i].imm == 0xbeef) {
+			if (!ASSERT_LT(n, len, "beef"))
+				return -1;
+			idx[n++] = ((__u32 *)u64_to_ptr(info->orig_idx))[i];
+		}
+	}
+
+	return n;
+}
+
+static void beef_check(const struct bpf_program *prog, int n_expected)
+{
+	struct bpf_prog_info *prog_info;
+	int idx_expected[MAX_INSNS];
+	int idx[MAX_INSNS];
+	int prog_fd;
+	int n, i;
+
+	/*
+	 * Find all beef instructions in the original program
+	 */
+
+	n = beef_search_original(bpf_program__insns(prog),
+				 bpf_program__insn_cnt(prog),
+				 idx_expected, MAX_INSNS);
+	if (!ASSERT_EQ(n, n_expected, "search original insns"))
+		return;
+
+	/*
+	 * Now find all the beef instructions in the xlated program and extract
+	 * corresponding orig_idx mappings
+	 */
+	prog_fd = bpf_program__fd(prog);
+	if (!ASSERT_GE(prog_fd, 0, "bpf_program__fd"))
+		return;
+
+	prog_info = prog_info_and_mappings(prog_fd);
+	if (!ASSERT_OK_PTR(prog_info, "prog_info_and_mappings"))
+		return;
+
+	if (!ASSERT_EQ(beef_search_xlated(prog_info, idx, n), n, "total # of beef"))
+		return;
+
+	/*
+	 * Check that the orig_idx points to the correct original indexes
+	 */
+	for (i = 0; i < n; i++)
+		ASSERT_EQ(idx[i], idx_expected[i], "beef index");
+}
+
+static void check_prog(const struct bpf_program *prog, int n_expected)
+{
+	struct bpf_link *link;
+
+	link = bpf_program__attach(prog);
+	if (!ASSERT_OK_PTR(link, "link"))
+		return;
+
+	beef_check(prog, n_expected);
+
+	bpf_link__destroy(link);
+}
+
+void test_bpf_insns_mappings(void)
+{
+	struct bpf_insns_mappings *skel;
+
+	skel = bpf_insns_mappings__open();
+	if (!ASSERT_OK_PTR(skel, "bpf_insns_mappings__open"))
+		return;
+
+	if (!ASSERT_OK(bpf_insns_mappings__load(skel),
+		  "bpf_insns_mappings__load"))
+		return;
+
+	if (test__start_subtest("check_trivial_prog"))
+		check_prog(skel->progs.check_trivial_prog, 3);
+
+	if (test__start_subtest("check_simple_prog"))
+		check_prog(skel->progs.check_simple_prog, 3);
+
+	if (test__start_subtest("check_bpf_to_bpf"))
+		check_prog(skel->progs.check_bpf_to_bpf, 6);
+
+	if (test__start_subtest("check_prog_dead_code"))
+		check_prog(skel->progs.check_prog_dead_code, 13);
+
+	if (test__start_subtest("check_prog_dead_code_bpf_to_bpf"))
+		check_prog(skel->progs.check_prog_dead_code_bpf_to_bpf, 26);
+
+	bpf_insns_mappings__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/bpf_insns_mappings.c b/tools/testing/selftests/bpf/progs/bpf_insns_mappings.c
new file mode 100644
index 000000000000..f6ff690801ea
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/bpf_insns_mappings.c
@@ -0,0 +1,155 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2024 Isovalent */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, u32);
+	__type(value, u64);
+} just_a_map SEC(".maps");
+
+static inline void beef(void)
+{
+	asm volatile("r8 = 0xbeef" ::: "r8");
+}
+
+static inline void cafe(void)
+{
+	asm volatile("r7 = 0xcafe" ::: "r7");
+}
+
+/*
+ * Trivial program: every insn maps to the original index
+ */
+SEC("fentry/" SYS_PREFIX "sys_nanosleep")
+int check_trivial_prog(void *ctx)
+{
+	beef();
+	cafe();
+	beef();
+	cafe();
+	beef();
+
+	return 0;
+}
+
+/* Some random instructions which will be patched for sure */
+static inline void beefify(void)
+{
+	__u32 key = 0;
+	__u64 *x;
+
+	beef();
+	bpf_printk("%llx", bpf_jiffies64());
+	beef();
+
+	key = !!bpf_jiffies64();
+	x = bpf_map_lookup_elem(&just_a_map, &key);
+	if (!x)
+		return;
+
+	beef();
+}
+
+/*
+ * Simple program: one section, no bpf-to-bpf calls, some patches
+ */
+SEC("fentry/" SYS_PREFIX "sys_nanosleep")
+int check_simple_prog(void *ctx)
+{
+	beefify();
+	return 0;
+}
+
+int __noinline foobar(int x)
+{
+	beefify();
+	return x;
+}
+
+/*
+ * Same simple program + a bpf-to-bpf call
+ */
+SEC("fentry/" SYS_PREFIX "sys_nanosleep")
+int check_bpf_to_bpf(void *ctx)
+{
+	beefify();
+
+	return foobar(0);
+}
+
+static inline void dead_code1(void)
+{
+	asm volatile("goto +0");
+}
+
+static inline void dead_code100(void)
+{
+#	if defined(__clang__)
+#		pragma clang loop unroll_count(100)
+#	elif defined(__GNUC__)
+#		pragma GCC unroll 100
+#	else
+#		error "unroll this loop, please"
+#	endif
+	for (int i = 0; i < 100; i++)
+		asm volatile("goto +0");
+}
+
+/*
+ * Some beef instructions, patches, plus dead code
+ */
+static __always_inline void dead_beef(void)
+{
+	beef();		/* 1 beef */
+	dead_code1();
+	beef();		/* 1 beef */
+	dead_code1();
+	beef();		/* 1 beef */
+	dead_code100();
+	beef();		/* 1 beef */
+
+	dead_code100();
+	beefify();	/* 3 beef */
+	dead_code100();
+	beefify();	/* 3 beef */
+	dead_code1();
+	beefify();	/* 3 beef */
+
+	/* 13 beef insns total */
+}
+
+/*
+ * A program with some nops to be removed
+ */
+SEC("fentry/" SYS_PREFIX "sys_nanosleep")
+int check_prog_dead_code(void *ctx)
+{
+	dead_beef();
+
+	return 0;
+}
+
+int __noinline foobar2(int x)
+{
+	dead_beef();
+
+	return x;
+}
+
+/*
+ * A program with some nops to be removed + a bpf-to-bpf call to a similar func
+ */
+SEC("fentry/" SYS_PREFIX "sys_nanosleep")
+int check_prog_dead_code_bpf_to_bpf(void *ctx)
+{
+	dead_beef();
+
+	return foobar2(0);
+}
+
+char _license[] SEC("license") = "GPL";