Multi-split BTF fixes and test
Subsystems (4)
v4 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 v3 [1]
- add asserts to ensure number of types in original and parsed BTF are identical, and the calls to btf__type_by_id() return valid pointers (code review bot, patch 2)
Changes since v2 [2]
- 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 [3]
- 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]/
[3] 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 | 87 ++++++++++++++++++- 2 files changed, 87 insertions(+), 4 deletions(-)
Signature · 1 line
2.39.3
PATCH v4 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.
--- tools/lib/bpf/btf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/lib/bpf/btf.c +2 -2
@@ -1061,7 +1061,7 @@ static struct btf *btf_new(const void *data, __u32 size, struct btf *base_btf, bif (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
PATCH v4 2/2 selftests/bpf: Test parsing of (multi-)split BTF
In reply to Alan MaguireWrite 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 | 87 ++++++++++++++++++- 1 file changed, 85 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/btf_split.c +85 -2
@@ -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,45 @@ 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; + } + +if (!ASSERT_EQ (btf__type_cnt (btf3),btf__type_cnt (btf6),"cmp_type_cnt" )) + goto cleanup; + +/* compare parsed to original BTF */ +for (i =1 ; i <btf__type_cnt (btf6); i++) { + t =btf__type_by_id (btf6, i); +if (!ASSERT_OK_PTR (t,"type_in_parsed_btf" )) + goto cleanup; + ot =btf__type_by_id (btf3, i); +if (!ASSERT_OK_PTR (ot,"type_in_orig_btf" )) + goto cleanup; +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 +205,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
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Quoted 10 lines · On Tue, 4 Nov 2025 20:33:07 +0000 you wrote:
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 v3 [1]
- add asserts to ensure number of types in original and parsed BTF are identical, and the calls tobtf__type_by_id()return valid pointers (code review bot, patch 2)[...]
Here is the summary with links:
- [v4,bpf-next,1/2] libbpf: Fix parsing of multi-split BTF
https://git.kernel.org/bpf/bpf-next/c/4f596acc260e - [v4,bpf-next,2/2] selftests/bpf: Test parsing of (multi-)split BTF https://git.kernel.org/bpf/bpf-next/c/cc77a203896e
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html