Skip to contentSkip to conversation

Multi-split BTF fixes and test

PATCH Superseded Alan Maguire 3 participants 6 messages Oct 28, 2025 – Nov 4, 2025

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Multi-split BTF fixes and test

This small series consists of a fix to multi-split BTF parsing (patch 1) and a test which exercises (multi-)split BTF parsing (patch 2).

Changes since v2 [1]

- fix Fixes: tag formatting (Andrii, patch 1)
- BPF code-review bot saw we were doing ASSERT_OK_PTR() on wrong BTF (not multisplit) in patch 2
- ensure cleanup is correctly handled for BTF, unlink in split tests (Andrii, patch 2)

Changes since v1 [2]

- BPF code-review bot spotted another place that the string offset needed to be adjusted based upon base start string offset + header string offset.
- added selftests to extend split BTF testing to parsing

[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/bpf/[email protected]/

Alan Maguire (2):
libbpf: Fix parsing of multi-split BTF selftests/bpf: Test parsing of (multi-)split BTF

 tools/lib/bpf/btf.c                           |  4 +-
 .../selftests/bpf/prog_tests/btf_split.c      | 80 ++++++++++++++++++-
 2 files changed, 80 insertions(+), 4 deletions(-)
Signature · 1 line
2.39.3
#1 lore raw

PATCH v3 1/2 libbpf: Fix parsing of multi-split BTF

When creating multi-split BTF we correctly set the start string offset to be the size of the base string section plus the base BTF start string offset; the latter is needed for multi-split BTF since the offset is non-zero there.

Unfortunately the BTF parsing case needed that logic and it was missed.

Fixes 4e29128a9ace ("libbpf/btf: Fix string handling to support multi-split BTF")
---
 tools/lib/bpf/btf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/lib/bpf/btf.c +2 -2
index 18907f0fcf9f..9f141395c074 100644
@@ -1061,7 +1061,7 @@ static struct btf *btf_new(const void *data, __u32 size, struct btf *base_btf, b
 	if (base_btf) {
 		btf->base_btf = base_btf;
 		btf->start_id = btf__type_cnt(base_btf);
-		btf->start_str_off = base_btf->hdr->str_len;
+		btf->start_str_off = base_btf->hdr->str_len + base_btf->start_str_off;
 	}
 
 	if (is_mmap) {
@@ -5818,7 +5818,7 @@ void btf_set_base_btf(struct btf *btf, const struct btf *base_btf)
 {
 	btf->base_btf = (struct btf *)base_btf;
 	btf->start_id = btf__type_cnt(base_btf);
-	btf->start_str_off = base_btf->hdr->str_len;
+	btf->start_str_off = base_btf->hdr->str_len + base_btf->start_str_off;
 }
 
 int btf__relocate(struct btf *btf, const struct btf *base_btf)
Signature · 1 line
2.39.3
#2 lore raw

PATCH v3 2/2 selftests/bpf: Test parsing of (multi-)split BTF

In reply to Alan Maguire

Write raw BTF to files, parse it and compare to original; this allows us to test parsing of (multi-)split BTF code.

---
 .../selftests/bpf/prog_tests/btf_split.c      | 80 ++++++++++++++++++-
 1 file changed, 78 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/btf_split.c +78 -2
index 3696fb9a05ed..b07a91ff28ff 100644
@@ -12,11 +12,45 @@ static void btf_dump_printf(void *ctx, const char *fmt, va_list args)
 	vfprintf(ctx, fmt, args);
 }
 
+/* Write raw BTF to file, return number of bytes written or negative errno */
+static ssize_t btf_raw_write(struct btf *btf, char *file)
+{
+	ssize_t written = 0;
+	const void *data;
+	__u32 size = 0;
+	int fd, ret;
+
+	fd = mkstemp(file);
+	if (!ASSERT_GE(fd, 0, "create_file"))
+		return -errno;
+
+	data = btf__raw_data(btf, &size);
+	if (!ASSERT_OK_PTR(data, "btf__raw_data")) {
+		close(fd);
+		return -EINVAL;
+	}
+	while (written < size) {
+		ret = write(fd, data + written, size - written);
+		if (!ASSERT_GE(ret, 0, "write succeeded")) {
+			close(fd);
+			return -errno;
+		}
+		written += ret;
+	}
+	close(fd);
+	return written;
+}
+
 static void __test_btf_split(bool multi)
 {
+	char multisplit_btf_file[] = "/tmp/test_btf_multisplit.XXXXXX";
+	char split_btf_file[] = "/tmp/test_btf_split.XXXXXX";
+	char base_btf_file[] = "/tmp/test_btf_base.XXXXXX";
+	ssize_t multisplit_btf_sz = 0, split_btf_sz = 0, base_btf_sz = 0;
 	struct btf_dump *d = NULL;
-	const struct btf_type *t;
-	struct btf *btf1, *btf2, *btf3 = NULL;
+	const struct btf_type *t, *ot;
+	struct btf *btf1 = NULL, *btf2 = NULL, *btf3 = NULL;
+	struct btf *btf4 = NULL, *btf5 = NULL, *btf6 = NULL;
 	int str_off, i, err;
 
 	btf1 = btf__new_empty();
@@ -123,6 +157,38 @@ static void __test_btf_split(bool multi)
 "	int uf2;\n"
 "};\n\n", "c_dump");
 
+	/* write base, split BTFs to files and ensure parsing succeeds */
+	base_btf_sz = btf_raw_write(btf1, base_btf_file);
+	if (base_btf_sz < 0)
+		goto cleanup;
+	split_btf_sz = btf_raw_write(btf2, split_btf_file);
+	if (split_btf_sz < 0)
+		goto cleanup;
+	btf4 = btf__parse(base_btf_file, NULL);
+	if (!ASSERT_OK_PTR(btf4, "parse_base"))
+		goto cleanup;
+	btf5 = btf__parse_split(split_btf_file, btf4);
+	if (!ASSERT_OK_PTR(btf5, "parse_split"))
+		goto cleanup;
+	if (multi) {
+		multisplit_btf_sz = btf_raw_write(btf3, multisplit_btf_file);
+		if (multisplit_btf_sz < 0)
+			goto cleanup;
+		btf6 = btf__parse_split(multisplit_btf_file, btf5);
+		if (!ASSERT_OK_PTR(btf6, "parse_multisplit"))
+			goto cleanup;
+	} else {
+		btf6 = btf5;
+	}
+
+	/* compare parsed to original BTF */
+	for (i = 1; i < btf__type_cnt(btf6); i++) {
+		t = btf__type_by_id(btf6, i);
+		ot = btf__type_by_id(btf3, i);
+		if (!ASSERT_EQ(memcmp(t, ot, sizeof(*ot)), 0, "cmp_parsed_orig_btf"))
+			goto cleanup;
+	}
+
 cleanup:
 	if (dump_buf_file)
 		fclose(dump_buf_file);
@@ -132,6 +198,16 @@ static void __test_btf_split(bool multi)
 	btf__free(btf2);
 	if (btf2 != btf3)
 		btf__free(btf3);
+	btf__free(btf4);
+	btf__free(btf5);
+	if (btf5 != btf6)
+		btf__free(btf6);
+	if (base_btf_sz > 0)
+		unlink(base_btf_file);
+	if (split_btf_sz > 0)
+		unlink(split_btf_file);
+	if (multisplit_btf_sz > 0)
+		unlink(multisplit_btf_file);
 }
 
 void test_btf_split(void)
Signature · 1 line
2.39.3
#3 lore raw

Re: [PATCH v3 bpf-next 2/2] selftests/bpf: Test parsing of (multi-)split BTF

AI reviewed your patch. Please fix the bug or email reply why it's not a bug. See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
```
commit 9da54f1ee8589391857dcb28c0dbc65a8e6632ea
Author: Alan Maguire <[email protected]>

selftests/bpf: Test parsing of (multi-)split BTF

This patch adds a test that writes raw BTF data to files, parses it
back, and compares the parsed BTF to the original to verify correctness
of the BTF parsing code.

> diff --git a/tools/testing/selftests/bpf/prog_tests/btf_split.c b/tools/testing/selftests/bpf/prog_tests/btf_split.c
> index 3696fb9a0..b07a91ff2 100644
> --- a/tools/testing/selftests/bpf/prog_tests/btf_split.c
> +++ b/tools/testing/selftests/bpf/prog_tests/btf_split.c

[ ... ]

> @@ -123,6 +157,38 @@ static void __test_btf_split(bool multi)
>  "	int uf2;\n"
>  "};\n\n", "c_dump");
>
> +	/* write base, split BTFs to files and ensure parsing succeeds */
> +	base_btf_sz = btf_raw_write(btf1, base_btf_file);
> +	if (base_btf_sz < 0)
> +		goto cleanup;
> +	split_btf_sz = btf_raw_write(btf2, split_btf_file);
> +	if (split_btf_sz < 0)
> +		goto cleanup;
> +	btf4 = btf__parse(base_btf_file, NULL);
> +	if (!ASSERT_OK_PTR(btf4, "parse_base"))
> +		goto cleanup;
> +	btf5 = btf__parse_split(split_btf_file, btf4);
> +	if (!ASSERT_OK_PTR(btf5, "parse_split"))
> +		goto cleanup;
> +	if (multi) {
> +		multisplit_btf_sz = btf_raw_write(btf3, multisplit_btf_file);
> +		if (multisplit_btf_sz < 0)
> +			goto cleanup;
> +		btf6 = btf__parse_split(multisplit_btf_file, btf5);
> +		if (!ASSERT_OK_PTR(btf6, "parse_multisplit"))
> +			goto cleanup;
> +	} else {
> +		btf6 = btf5;
> +	}
> +
> +	/* compare parsed to original BTF */
> +	for (i = 1; i < btf__type_cnt(btf6); i++) {
> +		t = btf__type_by_id(btf6, i);
> +		ot = btf__type_by_id(btf3, i);
> +		if (!ASSERT_EQ(memcmp(t, ot, sizeof(*ot)), 0, "cmp_parsed_orig_btf"))
> +			goto cleanup;
> +	}
> +

Can btf__type_by_id return NULL here in the comparison loop? The loop
iterates based on btf6's type count, but accesses types in both btf6
and btf3. If btf6 has more types than btf3 (indicating a parsing bug),
then btf__type_by_id(btf3, i) will return NULL when i exceeds btf3's
type count, leading to a NULL dereference in memcmp.

Other btf__type_by_id calls in this function (see earlier uses) check
the return value with ASSERT_OK_PTR before dereferencing. Should this
code either verify the type counts match first, or check these return
values to maintain consistency with the rest of the function?

>  cleanup:
> 	if (dump_buf_file)
> 		fclose(dump_buf_file);

[ ... ]


```

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

In-Reply-To-Subject: selftests/bpf: Test parsing of (multi-)split BTF CI run summary: https://github.com/kernel-patches/bpf/actions/runs/18891682588

#4 lore raw

Re: [PATCH v3 bpf-next 2/2] selftests/bpf: Test parsing of (multi-)split BTF

Quoted 61 lines · On 28/10/2025 23:25, [email protected] wrote:

```
commit 9da54f1ee8589391857dcb28c0dbc65a8e6632ea
Author: Alan Maguire <[email protected]>

selftests/bpf: Test parsing of (multi-)split BTF

This patch adds a test that writes raw BTF data to files, parses it back, and compares the parsed BTF to the original to verify correctness of the BTF parsing code.

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/btf_split.c +0 -0
index 3696fb9a0..b07a91ff2 100644

[ ... ]

Quoted 26 lines
tools/testing/selftests/bpf/prog_tests/btf_split.c +22 -0
@@ -123,6 +157,38 @@ static void __test_btf_split(bool multi)
 "	int uf2;\n"
 "};\n\n", "c_dump");
 
+	/* write base, split BTFs to files and ensure parsing succeeds */
+	base_btf_sz = btf_raw_write(btf1, base_btf_file);
+	if (base_btf_sz < 0)
+		goto cleanup;
+	split_btf_sz = btf_raw_write(btf2, split_btf_file);
+	if (split_btf_sz < 0)
+		goto cleanup;
+	btf4 = btf__parse(base_btf_file, NULL);
+	if (!ASSERT_OK_PTR(btf4, "parse_base"))
+		goto cleanup;
+	btf5 = btf__parse_split(split_btf_file, btf4);
+	if (!ASSERT_OK_PTR(btf5, "parse_split"))
+		goto cleanup;
+	if (multi) {
+		multisplit_btf_sz = btf_raw_write(btf3, multisplit_btf_file);
+		if (multisplit_btf_sz < 0)
+			goto cleanup;
+		btf6 = btf__parse_split(multisplit_btf_file, btf5);
+		if (!ASSERT_OK_PTR(btf6, "parse_multisplit"))
+			goto cleanup;
+	} else {
+		btf6 = btf5;

tools/testing/selftests/bpf/prog_tests/btf_split.c static void __test_btf_split(bool multi) · line 191

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/btf_split.c +10 -0
+	}
+
+	/* compare parsed to original BTF */
+	for (i = 1; i < btf__type_cnt(btf6); i++) {
+		t = btf__type_by_id(btf6, i);
+		ot = btf__type_by_id(btf3, i);
+		if (!ASSERT_EQ(memcmp(t, ot, sizeof(*ot)), 0, "cmp_parsed_orig_btf"))
+			goto cleanup;
+	}
+

Can btf__type_by_id return NULL here in the comparison loop? The loop iterates based on btf6's type count, but accesses types in both btf6 and btf3. If btf6 has more types than btf3 (indicating a parsing bug), then btf__type_by_id(btf3, i) will return NULL when i exceeds btf3's type count, leading to a NULL dereference in memcmp.

Quoted 5 lines

Other btf__type_by_id calls in this function (see earlier uses) check the return value with ASSERT_OK_PTR before dereferencing. Should this code either verify the type counts match first, or check these return values to maintain consistency with the rest of the function?

Should I respin a v4 with the bot's suggestion here? Perhaps along with an ASSERT_EQ() that btf__type_cnt(btf3) == btf__type_cnt(btf6) ? Thanks!

Alan

#5 lore raw

Re: [PATCH v3 bpf-next 2/2] selftests/bpf: Test parsing of (multi-)split BTF

Quoted 63 lines · Alan Maguire
Quoted 61 lines · On 28/10/2025 23:25, [email protected] wrote:

```
commit 9da54f1ee8589391857dcb28c0dbc65a8e6632ea
Author: Alan Maguire <[email protected]>

selftests/bpf: Test parsing of (multi-)split BTF

This patch adds a test that writes raw BTF data to files, parses it back, and compares the parsed BTF to the original to verify correctness of the BTF parsing code.

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/btf_split.c +0 -0
index 3696fb9a0..b07a91ff2 100644

[ ... ]

Quoted 26 lines
tools/testing/selftests/bpf/prog_tests/btf_split.c +22 -0
@@ -123,6 +157,38 @@ static void __test_btf_split(bool multi)
 "   int uf2;\n"
 "};\n\n", "c_dump");
 
+    /* write base, split BTFs to files and ensure parsing succeeds */
+    base_btf_sz = btf_raw_write(btf1, base_btf_file);
+    if (base_btf_sz < 0)
+            goto cleanup;
+    split_btf_sz = btf_raw_write(btf2, split_btf_file);
+    if (split_btf_sz < 0)
+            goto cleanup;
+    btf4 = btf__parse(base_btf_file, NULL);
+    if (!ASSERT_OK_PTR(btf4, "parse_base"))
+            goto cleanup;
+    btf5 = btf__parse_split(split_btf_file, btf4);
+    if (!ASSERT_OK_PTR(btf5, "parse_split"))
+            goto cleanup;
+    if (multi) {
+            multisplit_btf_sz = btf_raw_write(btf3, multisplit_btf_file);
+            if (multisplit_btf_sz < 0)
+                    goto cleanup;
+            btf6 = btf__parse_split(multisplit_btf_file, btf5);
+            if (!ASSERT_OK_PTR(btf6, "parse_multisplit"))
+                    goto cleanup;
+    } else {
+            btf6 = btf5;

tools/testing/selftests/bpf/prog_tests/btf_split.c static void __test_btf_split(bool multi) · line 191

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/btf_split.c +10 -0
+    }
+
+    /* compare parsed to original BTF */
+    for (i = 1; i < btf__type_cnt(btf6); i++) {
+            t = btf__type_by_id(btf6, i);
+            ot = btf__type_by_id(btf3, i);
+            if (!ASSERT_EQ(memcmp(t, ot, sizeof(*ot)), 0, "cmp_parsed_orig_btf"))
+                    goto cleanup;
+    }
+

Can btf__type_by_id return NULL here in the comparison loop? The loop iterates based on btf6's type count, but accesses types in both btf6 and btf3. If btf6 has more types than btf3 (indicating a parsing bug), then btf__type_by_id(btf3, i) will return NULL when i exceeds btf3's type count, leading to a NULL dereference in memcmp.

Quoted 8 lines
Quoted 5 lines

Other btf__type_by_id calls in this function (see earlier uses) check the return value with ASSERT_OK_PTR before dereferencing. Should this code either verify the type counts match first, or check these return values to maintain consistency with the rest of the function?

Should I respin a v4 with the bot's suggestion here? Perhaps along with an ASSERT_EQ() that btf__type_cnt(btf3) == btf__type_cnt(btf6) ? Thanks!

Yes. The assert for equal length will surely help.

pw-bot: cr