bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf 0/2] fix btf_dedup()'s type graph equivalence check
@ 2019-03-27  5:00 andrii.nakryiko
  2019-03-27  5:00 ` [PATCH bpf 1/2] libbpf: fix btf_dedup equivalence check handling of different kinds andrii.nakryiko
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: andrii.nakryiko @ 2019-03-27  5:00 UTC (permalink / raw)
  To: andrii.nakryiko, ast, yhs, bpf, netdev, acme, kernel-team, daniel
  Cc: Andrii Nakryiko

From: Andrii Nakryiko <andriin@fb.com>

This patch set fixes bug in btf_dedup_is_equiv() check mishandling equivalence
comparison between VOID kind in candidate type graph versus anonymous non-VOID
kind in canonical type graph.

Patch #1 fixes bug, by comparing candidate and canonical kinds for equality,
before proceeding to kind-specific checks.
Patch #2 adds a test case testing this specific scenario.

Andrii Nakryiko (2):
  libbpf: fix btf_dedup equivalence check handling of different kinds
  selftests/bpf: add btf_dedup test for VOID equivalence check

 tools/lib/bpf/btf.c                    |  3 ++
 tools/testing/selftests/bpf/test_btf.c | 47 ++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)

-- 
2.17.1


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

* [PATCH bpf 1/2] libbpf: fix btf_dedup equivalence check handling of different kinds
  2019-03-27  5:00 [PATCH bpf 0/2] fix btf_dedup()'s type graph equivalence check andrii.nakryiko
@ 2019-03-27  5:00 ` andrii.nakryiko
  2019-03-27  5:00 ` [PATCH bpf 2/2] selftests/bpf: add btf_dedup test for VOID equivalence check andrii.nakryiko
  2019-03-27 15:04 ` [PATCH bpf 0/2] fix btf_dedup()'s type graph " Alexei Starovoitov
  2 siblings, 0 replies; 4+ messages in thread
From: andrii.nakryiko @ 2019-03-27  5:00 UTC (permalink / raw)
  To: andrii.nakryiko, ast, yhs, bpf, netdev, acme, kernel-team, daniel
  Cc: Andrii Nakryiko

From: Andrii Nakryiko <andriin@fb.com>

btf_dedup_is_equiv() used to compare btf_type->info fields, before doing
kind-specific equivalence check. This comparsion implicitly verified
that candidate and canonical types are of the same kind. With enum fwd
resolution logic this check couldn't be done generically anymore, as for
enums info contains vlen, which differs between enum fwd and
fully-defined enum, so this check was subsumed by kind-specific
equivalence checks.

This change caused btf_dedup_is_equiv() to let through VOID vs other
types check to reach switch, which was never meant to be handing VOID
kind, as VOID kind is always pre-resolved to itself and is only
equivalent to itself, which is checked early in btf_dedup_is_equiv().

This change adds back BTF kind equality check in place of more generic
btf_type->info check, still defering further kind-specific checks to
a per-kind switch.

Fixes: 9768095ba97c ("btf: resolve enum fwds in btf_dedup")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/lib/bpf/btf.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index 87e3020ac1bc..cf119c9b6f27 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -2107,6 +2107,9 @@ static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,
 		return fwd_kind == real_kind;
 	}
 
+	if (cand_kind != canon_kind)
+		return 0;
+
 	switch (cand_kind) {
 	case BTF_KIND_INT:
 		return btf_equal_int(cand_type, canon_type);
-- 
2.17.1


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

* [PATCH bpf 2/2] selftests/bpf: add btf_dedup test for VOID equivalence check
  2019-03-27  5:00 [PATCH bpf 0/2] fix btf_dedup()'s type graph equivalence check andrii.nakryiko
  2019-03-27  5:00 ` [PATCH bpf 1/2] libbpf: fix btf_dedup equivalence check handling of different kinds andrii.nakryiko
@ 2019-03-27  5:00 ` andrii.nakryiko
  2019-03-27 15:04 ` [PATCH bpf 0/2] fix btf_dedup()'s type graph " Alexei Starovoitov
  2 siblings, 0 replies; 4+ messages in thread
From: andrii.nakryiko @ 2019-03-27  5:00 UTC (permalink / raw)
  To: andrii.nakryiko, ast, yhs, bpf, netdev, acme, kernel-team, daniel
  Cc: Andrii Nakryiko

From: Andrii Nakryiko <andriin@fb.com>

This patch adds specific test exposing bug in btf_dedup_is_equiv() when
comparing candidate VOID type to a non-VOID canonical type. It's
important for canonical type to be anonymous, otherwise name equality
check will do the right thing and will exit early.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/testing/selftests/bpf/test_btf.c | 47 ++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/tools/testing/selftests/bpf/test_btf.c b/tools/testing/selftests/bpf/test_btf.c
index 23e3b314ca60..ec5794e4205b 100644
--- a/tools/testing/selftests/bpf/test_btf.c
+++ b/tools/testing/selftests/bpf/test_btf.c
@@ -5776,6 +5776,53 @@ const struct btf_dedup_test dedup_tests[] = {
 		.dedup_table_size = 1, /* force hash collisions */
 	},
 },
+{
+	.descr = "dedup: void equiv check",
+	/*
+	 * // CU 1:
+	 * struct s {
+	 *	struct {} *x;
+	 * };
+	 * // CU 2:
+	 * struct s {
+	 *	int *x;
+	 * };
+	 */
+	.input = {
+		.raw_types = {
+			/* CU 1 */
+			BTF_STRUCT_ENC(0, 0, 1),				/* [1] struct {}  */
+			BTF_PTR_ENC(1),						/* [2] ptr -> [1] */
+			BTF_STRUCT_ENC(NAME_NTH(1), 1, 8),			/* [3] struct s   */
+				BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
+			/* CU 2 */
+			BTF_PTR_ENC(0),						/* [4] ptr -> void */
+			BTF_STRUCT_ENC(NAME_NTH(1), 1, 8),			/* [5] struct s   */
+				BTF_MEMBER_ENC(NAME_NTH(2), 4, 0),
+			BTF_END_RAW,
+		},
+		BTF_STR_SEC("\0s\0x"),
+	},
+	.expect = {
+		.raw_types = {
+			/* CU 1 */
+			BTF_STRUCT_ENC(0, 0, 1),				/* [1] struct {}  */
+			BTF_PTR_ENC(1),						/* [2] ptr -> [1] */
+			BTF_STRUCT_ENC(NAME_NTH(1), 1, 8),			/* [3] struct s   */
+				BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
+			/* CU 2 */
+			BTF_PTR_ENC(0),						/* [4] ptr -> void */
+			BTF_STRUCT_ENC(NAME_NTH(1), 1, 8),			/* [5] struct s   */
+				BTF_MEMBER_ENC(NAME_NTH(2), 4, 0),
+			BTF_END_RAW,
+		},
+		BTF_STR_SEC("\0s\0x"),
+	},
+	.opts = {
+		.dont_resolve_fwds = false,
+		.dedup_table_size = 1, /* force hash collisions */
+	},
+},
 {
 	.descr = "dedup: all possible kinds (no duplicates)",
 	.input = {
-- 
2.17.1


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

* Re: [PATCH bpf 0/2] fix btf_dedup()'s type graph equivalence check
  2019-03-27  5:00 [PATCH bpf 0/2] fix btf_dedup()'s type graph equivalence check andrii.nakryiko
  2019-03-27  5:00 ` [PATCH bpf 1/2] libbpf: fix btf_dedup equivalence check handling of different kinds andrii.nakryiko
  2019-03-27  5:00 ` [PATCH bpf 2/2] selftests/bpf: add btf_dedup test for VOID equivalence check andrii.nakryiko
@ 2019-03-27 15:04 ` Alexei Starovoitov
  2 siblings, 0 replies; 4+ messages in thread
From: Alexei Starovoitov @ 2019-03-27 15:04 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: Alexei Starovoitov, Yonghong Song, bpf, Network Development,
	Arnaldo Carvalho de Melo, Kernel Team, Daniel Borkmann,
	Andrii Nakryiko

On Tue, Mar 26, 2019 at 10:00 PM <andrii.nakryiko@gmail.com> wrote:
>
> From: Andrii Nakryiko <andriin@fb.com>
>
> This patch set fixes bug in btf_dedup_is_equiv() check mishandling equivalence
> comparison between VOID kind in candidate type graph versus anonymous non-VOID
> kind in canonical type graph.
>
> Patch #1 fixes bug, by comparing candidate and canonical kinds for equality,
> before proceeding to kind-specific checks.
> Patch #2 adds a test case testing this specific scenario.

Applied. Thanks

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

end of thread, other threads:[~2019-03-27 15:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-03-27  5:00 [PATCH bpf 0/2] fix btf_dedup()'s type graph equivalence check andrii.nakryiko
2019-03-27  5:00 ` [PATCH bpf 1/2] libbpf: fix btf_dedup equivalence check handling of different kinds andrii.nakryiko
2019-03-27  5:00 ` [PATCH bpf 2/2] selftests/bpf: add btf_dedup test for VOID equivalence check andrii.nakryiko
2019-03-27 15:04 ` [PATCH bpf 0/2] fix btf_dedup()'s type graph " Alexei Starovoitov

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).