libtraceevent: Accept BTF headers without the layout section
PATCH 1/1 libtraceevent: Accept BTF headers without the layout section
tep_load_btf() rejects BTF if the header length is smaller than sizeof(struct btf_header). But the size of that structure depends on the version of the kernel headers libtraceevent is built with: newer headers added the layout_off and layout_len fields (32 bytes in total), while the BTF of kernels without a layout section has the 24 bytes header.
So if libtraceevent is built against newer kernel headers, it can not load the BTF of such kernels at all:
Header (24) smaller than expected header 32
This also makes the "read BTF" unit test fail, for example on amd64 with linux-libc-dev 7.3 and a 6.17 kernel, or on ppc64el with a 7.3 kernel whose BTF has no layout section:
Test: read BTF ...FAILED
1. traceevent-utest.c:692 - tep_load_btf(test_tep, buf, st.st_size) == 0
2. traceevent-utest.c:715 - strcmp(s->buffer, expected) == 0
The check is there since the BTF support was added (first released in 1.9.0), but with 1.9.1 it also hits build environments where mmap() of the BTF file fails, like chroots: since 5b44859765fc ("libtraceevent utest: Read btf file in utest if mmap fails") the test no longer skips in that case, but loads the file. In Ubuntu this made the build of libtraceevent 1.9.1 fail on arm64, armhf, ppc64el and s390x, as the unit tests are run during the build.
Only the fields up to str_len are used, so only require these, no matter how the kernel headers define struct btf_header.
---
Notes:
Found while updating libtraceevent to 1.9.1 in Ubuntu 27.04 (tenacious, linux-libc-dev 7.3): the "read BTF" unit test failed on arm64, armhf, ppc64el and s390x (the build runs the unit tests), and also on amd64 when the build chroot can read /sys/kernel/btf/vmlinux.
Tested on top of 1.9.1 (master) with linux-libc-dev 7.3, on amd64 (6.17 kernel) and ppc64el (7.3 kernel, 64K pages), tep_load_btf() of /sys/kernel/btf/vmlinux:
before: Header (24) smaller than expected header 32
tep_load_btf() = -1, "read BTF" FAILED, 19/20 unit tests pass
after: tep_load_btf() = 0, "read BTF" passed, 20/20 unit tests pass
I can also send a unit test for this that does not depend on the BTF of the machine it runs on, if that is wanted.
src/trace-btf.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
src/trace-btf.c +12 -2
@@ -6,6 +6,7 @@*/ #include <stdio.h> #include <stdlib.h> +#include <stddef.h> #include <stdbool.h> #include <linux/btf.h>
@@ -17,6 +18,15 @@struct btf_header ;struct btf_type ; +/* +* Minimum length of a BTF header: all the fields that are used here. +* More fields can be added at the end of struct btf_header (the kernel +* added layout_off and layout_len), and the BTF of kernels that do not have +* them has a smaller header. Do not use sizeof(struct btf_header), as it +* depends on the version of the kernel headers this is built with. +*/ +#define BTF_HDR_MIN_LEN (offsetof(struct btf_header, str_len) + sizeof(__u32)) +struct tep_btf {struct btf_header *hdr;const char *strings;
@@ -257,9 +267,9 @@ static struct tep_btf *btf_init(void *raw_data, size_t data_size) goto fail; } -if (btf->hdr->hdr_len <sizeof (*btf->hdr)) { +if (btf->hdr->hdr_len <BTF_HDR_MIN_LEN ) {tep_warning ("Header (%d) smaller than expected header %zd" , - btf->hdr->hdr_len,sizeof (*btf->hdr)); + btf->hdr->hdr_len,BTF_HDR_MIN_LEN ); goto fail; }
Signature · 1 line
2.53.0