bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf v2 0/2] libbpf: Fix BTF dump of pointer-to-array-of-struct
@ 2021-03-19 11:25 Jean-Philippe Brucker
  2021-03-19 11:25 ` [PATCH bpf v2 1/2] " Jean-Philippe Brucker
  2021-03-19 11:25 ` [PATCH bpf v2 2/2] selftests/bpf: Add selftest for pointer-to-array-of-struct BTF dump Jean-Philippe Brucker
  0 siblings, 2 replies; 3+ messages in thread
From: Jean-Philippe Brucker @ 2021-03-19 11:25 UTC (permalink / raw)
  To: ast, daniel, andrii
  Cc: kafai, songliubraving, yhs, john.fastabend, kpsingh, bpf,
	Jean-Philippe Brucker

Fix an issue with the libbpf BTF dump, see patch 1 for details.

Since [v1] I added the selftest in patch 2, though I couldn't figure out
a way to make it independent from the order in which debug info is
issued by the compiler.

[v1]: https://lore.kernel.org/bpf/20210318122700.396574-1-jean-philippe@linaro.org/

Jean-Philippe Brucker (2):
  libbpf: Fix BTF dump of pointer-to-array-of-struct
  selftests/bpf: Add selftest for pointer-to-array-of-struct BTF dump

 tools/lib/bpf/btf_dump.c                                  | 2 +-
 .../selftests/bpf/progs/btf_dump_test_case_syntax.c       | 8 ++++++++
 2 files changed, 9 insertions(+), 1 deletion(-)

-- 
2.30.2


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH bpf v2 1/2] libbpf: Fix BTF dump of pointer-to-array-of-struct
  2021-03-19 11:25 [PATCH bpf v2 0/2] libbpf: Fix BTF dump of pointer-to-array-of-struct Jean-Philippe Brucker
@ 2021-03-19 11:25 ` Jean-Philippe Brucker
  2021-03-19 11:25 ` [PATCH bpf v2 2/2] selftests/bpf: Add selftest for pointer-to-array-of-struct BTF dump Jean-Philippe Brucker
  1 sibling, 0 replies; 3+ messages in thread
From: Jean-Philippe Brucker @ 2021-03-19 11:25 UTC (permalink / raw)
  To: ast, daniel, andrii
  Cc: kafai, songliubraving, yhs, john.fastabend, kpsingh, bpf,
	Jean-Philippe Brucker

The vmlinux.h generated from BTF is invalid when building
drivers/phy/ti/phy-gmii-sel.c with clang:

vmlinux.h:61702:27: error: array type has incomplete element type ‘struct reg_field’
61702 |  const struct reg_field (*regfields)[3];
      |                           ^~~~~~~~~

bpftool generates a forward declaration for this struct regfield, which
compilers aren't happy about. Here's a simplified reproducer:

	struct inner {
		int val;
	};
	struct outer {
		struct inner (*ptr_to_array)[2];
	} A;

After build with clang -> bpftool btf dump c -> clang/gcc:
./def-clang.h:11:23: error: array has incomplete element type 'struct inner'
        struct inner (*ptr_to_array)[2];

Member ptr_to_array of struct outer is a pointer to an array of struct
inner. In the DWARF generated by clang, struct outer appears before
struct inner, so when converting BTF of struct outer into C, bpftool
issues a forward declaration to struct inner. With GCC the DWARF info is
reversed so struct inner gets fully defined.

That forward declaration is not sufficient when compilers handle an
array of the struct, even when it's only used through a pointer. Note
that we can trigger the same issue with an intermediate typedef:

	struct inner {
	        int val;
	};
	typedef struct inner inner2_t[2];
	struct outer {
	        inner2_t *ptr_to_array;
	} A;

Becomes:

	struct inner;
	typedef struct inner inner2_t[2];

And causes:

./def-clang.h:10:30: error: array has incomplete element type 'struct inner'
	typedef struct inner inner2_t[2];

To fix this, clear through_ptr whenever we encounter an intermediate
array, to make the inner struct part of a strong link and force full
declaration.

Fixes: 351131b51c7a ("libbpf: add btf_dump API for BTF-to-C conversion")
Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
---
 tools/lib/bpf/btf_dump.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
index 2f9d685bd522..0911aea4cdbe 100644
--- a/tools/lib/bpf/btf_dump.c
+++ b/tools/lib/bpf/btf_dump.c
@@ -462,7 +462,7 @@ static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr)
 		return err;
 
 	case BTF_KIND_ARRAY:
-		return btf_dump_order_type(d, btf_array(t)->type, through_ptr);
+		return btf_dump_order_type(d, btf_array(t)->type, false);
 
 	case BTF_KIND_STRUCT:
 	case BTF_KIND_UNION: {
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH bpf v2 2/2] selftests/bpf: Add selftest for pointer-to-array-of-struct BTF dump
  2021-03-19 11:25 [PATCH bpf v2 0/2] libbpf: Fix BTF dump of pointer-to-array-of-struct Jean-Philippe Brucker
  2021-03-19 11:25 ` [PATCH bpf v2 1/2] " Jean-Philippe Brucker
@ 2021-03-19 11:25 ` Jean-Philippe Brucker
  1 sibling, 0 replies; 3+ messages in thread
From: Jean-Philippe Brucker @ 2021-03-19 11:25 UTC (permalink / raw)
  To: ast, daniel, andrii
  Cc: kafai, songliubraving, yhs, john.fastabend, kpsingh, bpf,
	Jean-Philippe Brucker, Andrii Nakryiko

Bpftool used to issue forward declarations for a struct used as part of
a pointer to array, which is invalid. Add a test to check that the
struct is fully defined in this case:

	@@ -134,9 +134,9 @@
	 	};
	 };

	-struct struct_in_array {};
	+struct struct_in_array;

	-struct struct_in_array_typed {};
	+struct struct_in_array_typed;

	 typedef struct struct_in_array_typed struct_in_array_t[2];

	@@ -189,3 +189,7 @@
	 	struct struct_with_embedded_stuff _14;
	 };

	+struct struct_in_array {};
	+
	+struct struct_in_array_typed {};
	+
	...
	#13/1 btf_dump: syntax:FAIL

Suggested-by: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
---
 .../selftests/bpf/progs/btf_dump_test_case_syntax.c       | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/tools/testing/selftests/bpf/progs/btf_dump_test_case_syntax.c b/tools/testing/selftests/bpf/progs/btf_dump_test_case_syntax.c
index 31975c96e2c9..3ac0c9afc35a 100644
--- a/tools/testing/selftests/bpf/progs/btf_dump_test_case_syntax.c
+++ b/tools/testing/selftests/bpf/progs/btf_dump_test_case_syntax.c
@@ -174,6 +174,12 @@ struct struct_in_struct {
 	};
 };
 
+struct struct_in_array {};
+
+struct struct_in_array_typed {};
+
+typedef struct struct_in_array_typed struct_in_array_t[2];
+
 struct struct_with_embedded_stuff {
 	int a;
 	struct {
@@ -203,6 +209,8 @@ struct struct_with_embedded_stuff {
 	} r[5];
 	struct struct_in_struct s[10];
 	int t[11];
+	struct struct_in_array (*u)[2];
+	struct_in_array_t *v;
 };
 
 struct root_struct {
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-03-19 11:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-19 11:25 [PATCH bpf v2 0/2] libbpf: Fix BTF dump of pointer-to-array-of-struct Jean-Philippe Brucker
2021-03-19 11:25 ` [PATCH bpf v2 1/2] " Jean-Philippe Brucker
2021-03-19 11:25 ` [PATCH bpf v2 2/2] selftests/bpf: Add selftest for pointer-to-array-of-struct BTF dump Jean-Philippe Brucker

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).