diff mbox series

[bpf-next,v5,25/25] selftests/bpf: Add BTF sanity tests

Message ID 20221107230950.7117-26-memxor@gmail.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series Local kptrs, BPF linked lists | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR pending PR summary
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count fail Series longer than 15 patches (and no cover letter)
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 11 maintainers not CCed: sdf@google.com kpsingh@kernel.org mykolal@fb.com haoluo@google.com linux-kselftest@vger.kernel.org yhs@fb.com shuah@kernel.org jolsa@kernel.org martin.lau@linux.dev song@kernel.org john.fastabend@gmail.com
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch fail ERROR: Macros with complex values should be enclosed in parentheses WARNING: Prefer __aligned(8) over __attribute__((aligned(8))) WARNING: line length of 82 exceeds 80 columns WARNING: line length of 83 exceeds 80 columns WARNING: line length of 84 exceeds 80 columns WARNING: line length of 93 exceeds 80 columns WARNING: line length of 94 exceeds 80 columns WARNING: line length of 96 exceeds 80 columns WARNING: quoted string split across lines WARNING: void function return statements are not generally useful
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-2 fail Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-1 pending Logs for ${{ matrix.test }} on ${{ matrix.arch }} with ${{ matrix.toolchain }}
bpf/vmtest-bpf-next-VM_Test-3 fail Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 fail Logs for build for aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-5 fail Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-6 fail Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 fail Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-8 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-9 success Logs for set-matrix

Commit Message

Kumar Kartikeya Dwivedi Nov. 7, 2022, 11:09 p.m. UTC
Preparing the metadata for bpf_list_head involves a complicated parsing
step and type resolution for the contained value. Ensure that corner
cases are tested against and invalid specifications in source are duly
rejected. Also include tests for incorrect ownership relationships in
the BTF.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
---
 .../selftests/bpf/prog_tests/linked_list.c    | 271 ++++++++++++++++++
 1 file changed, 271 insertions(+)

Comments

Andrii Nakryiko Nov. 9, 2022, 12:18 a.m. UTC | #1
On Mon, Nov 7, 2022 at 3:11 PM Kumar Kartikeya Dwivedi <memxor@gmail.com> wrote:
>
> Preparing the metadata for bpf_list_head involves a complicated parsing
> step and type resolution for the contained value. Ensure that corner
> cases are tested against and invalid specifications in source are duly
> rejected. Also include tests for incorrect ownership relationships in
> the BTF.
>
> Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
> ---
>  .../selftests/bpf/prog_tests/linked_list.c    | 271 ++++++++++++++++++
>  1 file changed, 271 insertions(+)
>

Have you considered using BTW write API to construct BTFs?
btf__new_empty() + btf__add_xxx()?

> diff --git a/tools/testing/selftests/bpf/prog_tests/linked_list.c b/tools/testing/selftests/bpf/prog_tests/linked_list.c
> index 669ef4bb9b87..40070e2d22f2 100644
> --- a/tools/testing/selftests/bpf/prog_tests/linked_list.c
> +++ b/tools/testing/selftests/bpf/prog_tests/linked_list.c
> @@ -1,4 +1,7 @@
>  // SPDX-License-Identifier: GPL-2.0
> +#include <bpf/btf.h>
> +#include <test_btf.h>
> +#include <linux/btf.h>
>  #include <test_progs.h>
>  #include <network_helpers.h>
>

[...]
Kumar Kartikeya Dwivedi Nov. 9, 2022, 4:33 p.m. UTC | #2
On Wed, Nov 09, 2022 at 05:48:00AM IST, Andrii Nakryiko wrote:
> On Mon, Nov 7, 2022 at 3:11 PM Kumar Kartikeya Dwivedi <memxor@gmail.com> wrote:
> >
> > Preparing the metadata for bpf_list_head involves a complicated parsing
> > step and type resolution for the contained value. Ensure that corner
> > cases are tested against and invalid specifications in source are duly
> > rejected. Also include tests for incorrect ownership relationships in
> > the BTF.
> >
> > Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
> > ---
> >  .../selftests/bpf/prog_tests/linked_list.c    | 271 ++++++++++++++++++
> >  1 file changed, 271 insertions(+)
> >
>
> Have you considered using BTW write API to construct BTFs?
> btf__new_empty() + btf__add_xxx()?
>

I didn't know about these. Let me give them a shot!
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/linked_list.c b/tools/testing/selftests/bpf/prog_tests/linked_list.c
index 669ef4bb9b87..40070e2d22f2 100644
--- a/tools/testing/selftests/bpf/prog_tests/linked_list.c
+++ b/tools/testing/selftests/bpf/prog_tests/linked_list.c
@@ -1,4 +1,7 @@ 
 // SPDX-License-Identifier: GPL-2.0
+#include <bpf/btf.h>
+#include <test_btf.h>
+#include <linux/btf.h>
 #include <test_progs.h>
 #include <network_helpers.h>
 
@@ -233,6 +236,273 @@  static void test_linked_list_success(int mode, bool leave_in_map)
 	linked_list__destroy(skel);
 }
 
+/* struct bpf_spin_lock {
+ *   int foo;
+ * };
+ * struct bpf_list_head {
+ *   __u64 :64;
+ *   __u64 :64;
+ * } __attribute__((aligned(8)));
+ * struct bpf_list_node {
+ *   __u64 :64;
+ *   __u64 :64;
+ * } __attribute__((aligned(8)));
+ */
+static const char btf_str_sec[] = "\0bpf_spin_lock\0bpf_list_head\0bpf_list_node\0foo\0bar\0baz"
+				  "\0contains:foo:foo\0contains:bar:bar\0contains:baz:baz\0bam"
+				  "\0contains:bam:bam";
+
+#define INIT_BTF_TILL_4							\
+	/* int */							\
+	BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */	\
+	/* struct bpf_spin_lock */                      /* [2] */	\
+	BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4),	\
+	BTF_MEMBER_ENC(43, 1, 0),					\
+	/* struct bpf_list_head */                      /* [3] */	\
+	BTF_TYPE_ENC(15, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 0), 16),	\
+	/* struct bpf_list_node */                      /* [4] */	\
+	BTF_TYPE_ENC(29, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 0), 16),
+
+static void check_btf(u32 *types, u32 types_len, int error)
+{
+	LIBBPF_OPTS(bpf_btf_load_opts, opts,
+		    .log_buf = log_buf,
+		    .log_size = sizeof(log_buf),
+	);
+	struct btf_header hdr = {
+		.magic = BTF_MAGIC,
+		.version = BTF_VERSION,
+		.hdr_len = sizeof(struct btf_header),
+		.type_len = types_len,
+		.str_off = types_len,
+		.str_len = sizeof(btf_str_sec),
+	};
+	void *ptr, *raw_btf;
+	int fd, ret;
+
+	raw_btf = malloc(sizeof(hdr) + hdr.type_len + hdr.str_len);
+	if (!ASSERT_OK_PTR(raw_btf, "malloc(raw_btf)"))
+		return;
+
+	ptr = raw_btf;
+	memcpy(ptr, &hdr, sizeof(hdr));
+	ptr += sizeof(hdr);
+	memcpy(ptr, types, hdr.type_len);
+	ptr += hdr.type_len;
+	memcpy(ptr, btf_str_sec, hdr.str_len);
+	ptr += hdr.str_len;
+
+	fd = bpf_btf_load(raw_btf, ptr - raw_btf, &opts);
+	ret = fd < 0 ? -errno : 0;
+	if (fd >= 0)
+		close(fd);
+	if (error)
+		ASSERT_LT(fd, 0, "bpf_btf_load");
+	else
+		ASSERT_GE(fd, 0, "bpf_btf_load");
+	if (!ASSERT_EQ(ret, error, "-errno == error"))
+		printf("BTF Log:\n%s\n", log_buf);
+	free(raw_btf);
+	return;
+}
+
+#define SPIN_LOCK 2
+#define LIST_HEAD 3
+#define LIST_NODE 4
+#define FOO 43
+#define BAR 47
+#define BAZ 51
+#define BAM 106
+#define CONT_FOO_FOO 55
+#define CONT_BAR_BAR 72
+#define CONT_BAZ_BAZ 89
+#define CONT_BAM_BAM 110
+
+static void test_btf(void)
+{
+	if (test__start_subtest("btf: too many locks")) {
+		u32 types[] = {
+			INIT_BTF_TILL_4
+			BTF_TYPE_ENC(FOO, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 3), 24), /* [5] */
+			BTF_MEMBER_ENC(BAZ, SPIN_LOCK, 0),
+			BTF_MEMBER_ENC(FOO, SPIN_LOCK, 32),
+			BTF_MEMBER_ENC(BAR, LIST_HEAD, 64),
+		};
+		check_btf(types, sizeof(types), -E2BIG);
+	}
+	if (test__start_subtest("btf: missing lock")) {
+		u32 types[] = {
+			INIT_BTF_TILL_4
+			BTF_TYPE_ENC(FOO, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 16), /* [5] */
+			BTF_MEMBER_ENC(BAR, LIST_HEAD, 0),
+			BTF_DECL_TAG_ENC(CONT_BAZ_BAZ, 5, 0),
+			BTF_TYPE_ENC(BAZ, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 16),
+			BTF_MEMBER_ENC(BAZ, LIST_NODE, 0),
+		};
+		check_btf(types, sizeof(types), -EINVAL);
+	}
+	if (test__start_subtest("btf: bad offset")) {
+		u32 types[] = {
+			INIT_BTF_TILL_4
+			BTF_TYPE_ENC(FOO, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 3), 36), /* [5] */
+			BTF_MEMBER_ENC(BAR, LIST_HEAD, 0),
+			BTF_MEMBER_ENC(FOO, LIST_NODE, 0),
+			BTF_MEMBER_ENC(BAZ, SPIN_LOCK, 0),
+			BTF_DECL_TAG_ENC(CONT_FOO_FOO, 5, 0),
+		};
+		check_btf(types, sizeof(types), -EFAULT);
+	}
+	if (test__start_subtest("btf: missing contains:")) {
+		u32 types[] = {
+			INIT_BTF_TILL_4
+			BTF_TYPE_ENC(FOO, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 24), /* [5] */
+			BTF_MEMBER_ENC(BAZ, SPIN_LOCK, 0),
+			BTF_MEMBER_ENC(BAR, LIST_HEAD, 64),
+		};
+		check_btf(types, sizeof(types), -EINVAL);
+	}
+	if (test__start_subtest("btf: missing struct")) {
+		u32 types[] = {
+			INIT_BTF_TILL_4
+			BTF_TYPE_ENC(FOO, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 24), /* [5] */
+			BTF_MEMBER_ENC(BAZ, SPIN_LOCK, 0),
+			BTF_MEMBER_ENC(BAR, LIST_HEAD, 64),
+			BTF_DECL_TAG_ENC(CONT_BAR_BAR, 5, 1),
+		};
+		check_btf(types, sizeof(types), -ENOENT);
+	}
+	if (test__start_subtest("btf: missing node")) {
+		u32 types[] = {
+			INIT_BTF_TILL_4
+			BTF_TYPE_ENC(FOO, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 24), /* [5] */
+			BTF_MEMBER_ENC(BAZ, SPIN_LOCK, 0),
+			BTF_MEMBER_ENC(BAR, LIST_HEAD, 64),
+			BTF_DECL_TAG_ENC(CONT_FOO_FOO, 5, 1),
+		};
+		check_btf(types, sizeof(types), -ENOENT);
+	}
+	if (test__start_subtest("btf: node incorrect type")) {
+		u32 types[] = {
+			INIT_BTF_TILL_4
+			BTF_TYPE_ENC(FOO, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 20), /* [5] */
+			BTF_MEMBER_ENC(BAR, LIST_HEAD, 0),
+			BTF_MEMBER_ENC(BAR, SPIN_LOCK, 128),
+			BTF_DECL_TAG_ENC(CONT_BAZ_BAZ, 5, 0),
+			BTF_TYPE_ENC(BAZ, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4),
+			BTF_MEMBER_ENC(BAZ, SPIN_LOCK, 0),
+		};
+		check_btf(types, sizeof(types), -EINVAL);
+	}
+	if (test__start_subtest("btf: multiple bpf_list_node with name foo")) {
+		u32 types[] = {
+			INIT_BTF_TILL_4
+			BTF_TYPE_ENC(FOO, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 3), 52), /* [5] */
+			BTF_MEMBER_ENC(BAR, LIST_HEAD, 0),
+			BTF_MEMBER_ENC(FOO, LIST_NODE, 128),
+			BTF_MEMBER_ENC(FOO, LIST_NODE, 256),
+			BTF_MEMBER_ENC(BAZ, SPIN_LOCK, 384),
+			BTF_DECL_TAG_ENC(CONT_FOO_FOO, 5, 0),
+		};
+		check_btf(types, sizeof(types), -EINVAL);
+	}
+	if (test__start_subtest("btf: owning | owned AA cycle")) {
+		u32 types[] = {
+			INIT_BTF_TILL_4
+			BTF_TYPE_ENC(FOO, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 3), 36), /* [5] */
+			BTF_MEMBER_ENC(BAR, LIST_HEAD, 0),
+			BTF_MEMBER_ENC(FOO, LIST_NODE, 128),
+			BTF_MEMBER_ENC(BAZ, SPIN_LOCK, 256),
+			BTF_DECL_TAG_ENC(CONT_FOO_FOO, 5, 0),
+		};
+		check_btf(types, sizeof(types), -ELOOP);
+	}
+	if (test__start_subtest("btf: owning | owned ABA cycle")) {
+		u32 types[] = {
+			INIT_BTF_TILL_4
+			BTF_TYPE_ENC(FOO, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 3), 36), /* [5] */
+			BTF_MEMBER_ENC(BAR, LIST_HEAD, 0),
+			BTF_MEMBER_ENC(FOO, LIST_NODE, 128),
+			BTF_MEMBER_ENC(BAZ, SPIN_LOCK, 256),
+			BTF_DECL_TAG_ENC(CONT_BAR_BAR, 5, 0),			    /* [6] */
+			BTF_TYPE_ENC(BAR, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 3), 36), /* [7] */
+			BTF_MEMBER_ENC(FOO, LIST_HEAD, 0),
+			BTF_MEMBER_ENC(BAR, LIST_NODE, 128),
+			BTF_MEMBER_ENC(BAZ, SPIN_LOCK, 256),
+			BTF_DECL_TAG_ENC(CONT_FOO_FOO, 7, 0),
+		};
+		check_btf(types, sizeof(types), -ELOOP);
+	}
+	if (test__start_subtest("btf: owning -> owned")) {
+		u32 types[] = {
+			INIT_BTF_TILL_4
+			BTF_TYPE_ENC(FOO, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 20), /* [5] */
+			BTF_MEMBER_ENC(BAR, LIST_HEAD, 0),
+			BTF_MEMBER_ENC(BAR, SPIN_LOCK, 128),
+			BTF_DECL_TAG_ENC(CONT_BAZ_BAZ, 5, 0),
+			BTF_TYPE_ENC(BAZ, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 16),
+			BTF_MEMBER_ENC(BAZ, LIST_NODE, 0),
+		};
+		check_btf(types, sizeof(types), 0);
+	}
+	if (test__start_subtest("btf: owning -> owning | owned -> owned")) {
+		u32 types[] = {
+			INIT_BTF_TILL_4
+			BTF_TYPE_ENC(FOO, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 20), /* [5] */
+			BTF_MEMBER_ENC(BAR, LIST_HEAD, 0),
+			BTF_MEMBER_ENC(BAZ, SPIN_LOCK, 128),
+			BTF_DECL_TAG_ENC(CONT_BAR_BAR, 5, 0),			    /* [6] */
+			BTF_TYPE_ENC(BAR, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 3), 36), /* [7] */
+			BTF_MEMBER_ENC(FOO, LIST_HEAD, 0),
+			BTF_MEMBER_ENC(BAR, LIST_NODE, 128),
+			BTF_MEMBER_ENC(BAZ, SPIN_LOCK, 256),
+			BTF_DECL_TAG_ENC(CONT_BAZ_BAZ, 7, 0),
+			BTF_TYPE_ENC(BAZ, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 16),
+			BTF_MEMBER_ENC(BAZ, LIST_NODE, 0),
+		};
+		check_btf(types, sizeof(types), 0);
+	}
+	if (test__start_subtest("btf: owning | owned -> owning | owned -> owned")) {
+		u32 types[] = {
+			INIT_BTF_TILL_4
+			BTF_TYPE_ENC(FOO, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 3), 36), /* [5] */
+			BTF_MEMBER_ENC(BAR, LIST_HEAD, 0),
+			BTF_MEMBER_ENC(FOO, LIST_NODE, 128),
+			BTF_MEMBER_ENC(BAZ, SPIN_LOCK, 256),
+			BTF_DECL_TAG_ENC(CONT_BAR_BAR, 5, 0),			    /* [6] */
+			BTF_TYPE_ENC(BAR, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 3), 36), /* [7] */
+			BTF_MEMBER_ENC(FOO, LIST_HEAD, 0),
+			BTF_MEMBER_ENC(BAR, LIST_NODE, 128),
+			BTF_MEMBER_ENC(BAZ, SPIN_LOCK, 256),
+			BTF_DECL_TAG_ENC(CONT_BAZ_BAZ, 7, 0),
+			BTF_TYPE_ENC(BAZ, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 16),
+			BTF_MEMBER_ENC(BAZ, LIST_NODE, 0),
+		};
+		check_btf(types, sizeof(types), -ELOOP);
+	}
+	if (test__start_subtest("btf: owning -> owning | owned -> owning | owned -> owned")) {
+		u32 types[] = {
+			INIT_BTF_TILL_4
+			BTF_TYPE_ENC(FOO, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 20), /* [5] */
+			BTF_MEMBER_ENC(BAR, LIST_HEAD, 0),
+			BTF_MEMBER_ENC(BAZ, SPIN_LOCK, 128),
+			BTF_DECL_TAG_ENC(CONT_BAR_BAR, 5, 0),			    /* [6] */
+			BTF_TYPE_ENC(BAR, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 3), 36), /* [7] */
+			BTF_MEMBER_ENC(FOO, LIST_HEAD, 0),
+			BTF_MEMBER_ENC(BAR, LIST_NODE, 128),
+			BTF_MEMBER_ENC(BAZ, SPIN_LOCK, 256),
+			BTF_DECL_TAG_ENC(CONT_BAZ_BAZ, 7, 0),
+			BTF_TYPE_ENC(BAZ, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 3), 36), /* [9] */
+			BTF_MEMBER_ENC(BAR, LIST_HEAD, 0),
+			BTF_MEMBER_ENC(BAZ, LIST_NODE, 128),
+			BTF_MEMBER_ENC(FOO, SPIN_LOCK, 256),
+			BTF_DECL_TAG_ENC(CONT_BAM_BAM, 9, 0),
+			BTF_TYPE_ENC(BAM, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 16),
+			BTF_MEMBER_ENC(BAM, LIST_NODE, 0),
+		};
+		check_btf(types, sizeof(types), -ELOOP);
+	}
+}
+
 void test_linked_list(void)
 {
 	int i;
@@ -243,6 +513,7 @@  void test_linked_list(void)
 		test_linked_list_fail_prog(linked_list_fail_tests[i].prog_name,
 					   linked_list_fail_tests[i].err_msg);
 	}
+	test_btf();
 	test_linked_list_success(PUSH_POP, false);
 	test_linked_list_success(PUSH_POP, true);
 	test_linked_list_success(PUSH_POP_MULT, false);