On 13 March 2024 17:39:14 GMT, Andrii Nakryiko <andrii.nakryiko@xxxxxxxxx> wrote: >On Wed, Mar 13, 2024 at 10:24 AM Quentin Monnet <quentin@xxxxxxxxxxxxx> wrote: >> >> This reverts commit df7c3f7d3a3ddab31ca8cfa9b86a8729ec43fd2e. >> >> There's no guarantee that obj->btf is non-NULL when passing it to >> btf__fd(), and this function doesn't perform any check before >> dereferencing its argument. As a consequence, this commit caused >> segmentation faults in bpftool (for example) when trying to load >> programs that come without BTF info. >> >> Signed-off-by: Quentin Monnet <quentin@xxxxxxxxxxxxx> >> --- >> tools/lib/bpf/libbpf.c | 2 +- >> 1 file changed, 1 insertion(+), 1 deletion(-) >> >> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c >> index efab29b8935b..0f9a4d232468 100644 >> --- a/tools/lib/bpf/libbpf.c >> +++ b/tools/lib/bpf/libbpf.c >> @@ -7343,7 +7343,7 @@ static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog >> load_attr.prog_ifindex = prog->prog_ifindex; >> >> /* specify func_info/line_info only if kernel supports them */ >> - btf_fd = btf__fd(obj->btf); >> + btf_fd = bpf_object__btf_fd(obj); > >oh, my bad, sorry about that. But I'd still like to stick to using >btf__fd(), so maybe let's fix forward by dropping btf_fd variable here >and doing what bpf_object__create_map() does: > >if (obj->btf && btf__fd(obj->btf) >= 0 && kernel_supports(...)) { > load_attr.prog_btf_fd = btf__fd(obj->btf); > ... >} > >? I half-expected that :). I don't mind at all, I'll send an updated version tomorrow. Thanks!