diff mbox series

[RFC,bpf-next,06/12] selftests/bpf: Tests for header guards printing in BTF dump

Message ID 20221025222802.2295103-7-eddyz87@gmail.com (mailing list archive)
State RFC
Delegated to: BPF
Headers show
Series Use uapi kernel headers with vmlinux.h | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next-VM_Test-1 pending Logs for ${{ matrix.test }} on ${{ matrix.arch }} with ${{ matrix.toolchain }}
bpf/vmtest-bpf-next-VM_Test-2 fail Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-3 fail Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 fail Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-5 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-6 success Logs for set-matrix
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 success Link
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 john.fastabend@gmail.com haoluo@google.com linux-kselftest@vger.kernel.org jolsa@kernel.org kpsingh@kernel.org song@kernel.org shuah@kernel.org mykolal@fb.com martin.lau@linux.dev xukuohai@huawei.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 warning WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? WARNING: do not add new typedefs WARNING: line length of 84 exceeds 80 columns WARNING: line length of 93 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Eduard Zingerman Oct. 25, 2022, 10:27 p.m. UTC
Verify that `btf_dump__dump_type` emits header guard brackets for
various types when `btf_dump_opts.emit_header_guards` is set to true.

Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
---
 .../selftests/bpf/prog_tests/btf_dump.c       | 10 +-
 .../progs/btf_dump_test_case_header_guards.c  | 94 +++++++++++++++++++
 2 files changed, 101 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/btf_dump_test_case_header_guards.c
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/btf_dump.c b/tools/testing/selftests/bpf/prog_tests/btf_dump.c
index 5f6ce7f1a801..a3db352e61c7 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_dump.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_dump.c
@@ -13,6 +13,7 @@  static struct btf_dump_test_case {
 	const char *name;
 	const char *file;
 	bool known_ptr_sz;
+	bool emit_header_guards;
 } btf_dump_test_cases[] = {
 	{"btf_dump: syntax", "btf_dump_test_case_syntax", true},
 	{"btf_dump: ordering", "btf_dump_test_case_ordering", false},
@@ -22,15 +23,18 @@  static struct btf_dump_test_case {
 	{"btf_dump: multidim", "btf_dump_test_case_multidim", false},
 	{"btf_dump: namespacing", "btf_dump_test_case_namespacing", false},
 	{"btf_dump: decl_tag", "btf_dump_test_case_decl_tag", true},
+	{"btf_dump: header guards", "btf_dump_test_case_header_guards", true, true},
 };
 
-static int btf_dump_all_types(const struct btf *btf, void *ctx)
+static int btf_dump_all_types(const struct btf *btf, void *ctx, struct btf_dump_test_case *t)
 {
 	size_t type_cnt = btf__type_cnt(btf);
+	LIBBPF_OPTS(btf_dump_opts, opts);
 	struct btf_dump *d;
 	int err = 0, id;
 
-	d = btf_dump__new(btf, btf_dump_printf, ctx, NULL);
+	opts.emit_header_guards = t->emit_header_guards;
+	d = btf_dump__new(btf, btf_dump_printf, ctx, &opts);
 	err = libbpf_get_error(d);
 	if (err)
 		return err;
@@ -87,7 +91,7 @@  static int test_btf_dump_case(int n, struct btf_dump_test_case *t)
 		goto done;
 	}
 
-	err = btf_dump_all_types(btf, f);
+	err = btf_dump_all_types(btf, f, t);
 	fclose(f);
 	close(fd);
 	if (CHECK(err, "btf_dump", "failure during C dumping: %d\n", err)) {
diff --git a/tools/testing/selftests/bpf/progs/btf_dump_test_case_header_guards.c b/tools/testing/selftests/bpf/progs/btf_dump_test_case_header_guards.c
new file mode 100644
index 000000000000..3ee8aaba9e0a
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/btf_dump_test_case_header_guards.c
@@ -0,0 +1,94 @@ 
+// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
+
+/*
+ * BTF-to-C dumper test for header guards.
+ */
+struct hg_struct {
+	int x;
+} __attribute__((btf_decl_tag("header_guard:S")));
+
+union hg_union {
+	int x;
+} __attribute__((btf_decl_tag("header_guard:U")));
+
+typedef int hg_typedef __attribute__((btf_decl_tag("header_guard:T")));
+
+struct hg_fwd_a;
+
+struct hg_fwd_b {
+	struct hg_fwd_a *loop;
+} __attribute__((btf_decl_tag("header_guard:FWD")));
+
+struct hg_fwd_a {
+	struct hg_fwd_b *loop;
+} __attribute__((btf_decl_tag("header_guard:FWD")));
+
+struct root_struct {
+	struct hg_struct a;
+	union hg_union b;
+	hg_typedef c;
+	struct hg_fwd_a d;
+	struct hg_fwd_b e;
+};
+
+/* ----- START-EXPECTED-OUTPUT ----- */
+/*
+ *#ifndef S
+ *
+ *struct hg_struct {
+ *	int x;
+ *};
+ *
+ *#endif
+ *
+ *#ifndef U
+ *
+ *union hg_union {
+ *	int x;
+ *};
+ *
+ *#endif
+ *
+ *#ifndef T
+ *
+ *typedef int hg_typedef;
+ *
+ *#endif
+ *
+ *#ifndef FWD
+ *
+ *struct hg_fwd_b;
+ *
+ *#endif
+ *
+ *#ifndef FWD
+ *
+ *struct hg_fwd_a {
+ *	struct hg_fwd_b *loop;
+ *};
+ *
+ *#endif
+ *
+ *#ifndef FWD
+ *
+ *struct hg_fwd_b {
+ *	struct hg_fwd_a *loop;
+ *};
+ *
+ *#endif
+ *
+ *struct root_struct {
+ *	struct hg_struct a;
+ *	union hg_union b;
+ *	hg_typedef c;
+ *	struct hg_fwd_a d;
+ *	struct hg_fwd_b e;
+ *};
+ *
+ */
+/* ------ END-EXPECTED-OUTPUT ------ */
+
+int f(struct root_struct *s)
+{
+	return 0;
+}