Re: [PATCH v13 bpf-next 2/8] libbpf: Support layout section handling in BTF

Andrii Nakryiko <[email protected]>
Newsgroups org.kernel.vger.dwarves,org.kernel.vger.bpf
Message-ID <CAEf4Bza_kJowGmin4Hh2iOpwXZVgNoeGNniidSpZEjRjqWnVyA@mail.gmail.com>
On Fri, Mar 6, 2026 at 3:37 AM Alan Maguire <[email protected]> wrote:
>
> Support reading in layout fixing endian issues on reading;
> also support writing layout section to raw BTF object.
> There is not yet an API to populate the layout with meaningful
> information.
>
> As part of this, we need to consider multiple valid BTF header
> sizes; the original or the layout-extended headers.
> So to support this, the "struct btf" representation is modified
> to contain a "struct btf_header" and we copy the valid
> portion from the raw data to it; this means we can always safely
> check fields like btf->hdr.layout_len
>
> Signed-off-by: Alan Maguire <[email protected]>
> ---
>  tools/lib/bpf/btf.c | 309 ++++++++++++++++++++++++++++----------------
>  1 file changed, 200 insertions(+), 109 deletions(-)
>

[...]

>
> -       memcpy(p, hdr, hdr->hdr_len);
> +       memcpy(p, hdr, min((__u32)sizeof(struct btf_header), hdr->hdr_len));

in all these cases where we have sizeof(struct btf_header), this
smells like a potential issue, tbh. We are silently truncating the
original header here, potentially. Which is ok for read-only case, but
do we error out if someone is trying to modify/write such BTF where we
don't really understand all of it? Should we?

>         if (swap_endian)
> -               btf_bswap_hdr(p);
> +               btf_bswap_hdr(p, hdr->hdr_len);
>         p += hdr->hdr_len;
>
>         memcpy(p, btf->types_data, hdr->type_len);

[..]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.