[PATCH v2 2/2] selftests/bpf: add BTF dedup tests for recursive typedef definitions

Paul Houssel posted 2 patches 2 months, 4 weeks ago
There is a newer version of this series
[PATCH v2 2/2] selftests/bpf: add BTF dedup tests for recursive typedef definitions
Posted by Paul Houssel 2 months, 4 weeks ago
Add several ./test_progs tests:
    1.  btf/dedup:recursive typedef ensures that deduplication no
	longer fails on recursive typedefs.
    2.  btf/dedup:typedef ensures that typedefs are deduplicated correctly
	just as they were before this patch.

Signed-off-by: Paul Houssel <paul.houssel@orange.com>
---
 tools/testing/selftests/bpf/prog_tests/btf.c | 61 ++++++++++++++++++++
 1 file changed, 61 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/btf.c b/tools/testing/selftests/bpf/prog_tests/btf.c
index 8a9ba4292109..a19db159475a 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf.c
@@ -7495,6 +7495,67 @@ static struct btf_dedup_test dedup_tests[] = {
 		BTF_STR_SEC("\0t\0m1\0m2\0tag1\0tag2\0tag3"),
 	},
 },
+{
+	.descr = "dedup: recursive typedef",
+	/*
+	 * This test simulates a recursive typedef, which in GO is defined as such:
+	 *
+	 *   type Foo func() Foo
+	 *
+	 * In BTF terms, this is represented as a TYPEDEF referencing
+	 * a FUNC_PROTO that returns the same TYPEDEF.
+	 */
+	.input = {
+		.raw_types = {
+			/*
+			 * [1] typedef Foo -> func() Foo
+			 * [2] func_proto() -> Foo
+			 */
+			BTF_TYPEDEF_ENC(NAME_NTH(1), 2),	/* [1] */
+			BTF_FUNC_PROTO_ENC(1, 0),		/* [2] */
+			BTF_END_RAW,
+		},
+		BTF_STR_SEC("\0Foo"),
+	},
+	.expect = {
+		.raw_types = {
+			BTF_TYPEDEF_ENC(NAME_NTH(1), 2),	/* [1] */
+			BTF_FUNC_PROTO_ENC(1, 0),		/* [2] */
+			BTF_END_RAW,
+		},
+		BTF_STR_SEC("\0Foo"),
+	},
+},
+{
+	.descr = "dedup: typedef",
+    /*
+     * // CU 1:
+     * typedef int foo;
+     *
+     * // CU 2:
+     * typedef int foo;
+     */
+	.input = {
+		.raw_types = {
+			/* CU 1 */
+			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
+			BTF_TYPEDEF_ENC(NAME_NTH(1), 1),		/* [2] */
+			/* CU 2 */
+			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [3] */
+			BTF_TYPEDEF_ENC(NAME_NTH(1), 3),		/* [4] */
+			BTF_END_RAW,
+		},
+		BTF_STR_SEC("\0foo"),
+	},
+	.expect = {
+		.raw_types = {
+			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
+			BTF_TYPEDEF_ENC(NAME_NTH(1), 1),		/* [2] */
+			BTF_END_RAW,
+		},
+		BTF_STR_SEC("\0foo"),
+	},
+},
 {
 	.descr = "dedup: typedef tags",
 	.input = {
-- 
2.51.0
Re: [PATCH v2 2/2] selftests/bpf: add BTF dedup tests for recursive typedef definitions
Posted by Eduard Zingerman 2 months, 4 weeks ago
On Wed, 2025-11-12 at 15:11 +0100, Paul Houssel wrote:

Lgtm, one nit below:

> +{
> +	.descr = "dedup: recursive typedef",
> +	/*
> +	 * This test simulates a recursive typedef, which in GO is defined as such:
> +	 *
> +	 *   type Foo func() Foo
> +	 *
> +	 * In BTF terms, this is represented as a TYPEDEF referencing
> +	 * a FUNC_PROTO that returns the same TYPEDEF.
> +	 */
> +	.input = {
> +		.raw_types = {
> +			/*
> +			 * [1] typedef Foo -> func() Foo
> +			 * [2] func_proto() -> Foo
> +			 */
> +			BTF_TYPEDEF_ENC(NAME_NTH(1), 2),	/* [1] */
> +			BTF_FUNC_PROTO_ENC(1, 0),		/* [2] */

Nit:
Maybe repeat the above two types, just to make sure that deduplication happens?

> +			BTF_END_RAW,
> +		},
> +		BTF_STR_SEC("\0Foo"),
> +	},
> +	.expect = {
> +		.raw_types = {
> +			BTF_TYPEDEF_ENC(NAME_NTH(1), 2),	/* [1] */
> +			BTF_FUNC_PROTO_ENC(1, 0),		/* [2] */
> +			BTF_END_RAW,
> +		},
> +		BTF_STR_SEC("\0Foo"),
> +	},
> +},

[...]