Skip to content

Commit

Permalink
libbpf: disassociate section handler on explicit bpf_program__set_typ…
Browse files Browse the repository at this point in the history
…e() call

If user explicitly overrides programs's type with
bpf_program__set_type() API call, we need to disassociate whatever
SEC_DEF handler libbpf determined initially based on program's SEC()
definition, as it's not goind to be valid anymore and could lead to
crashes and/or confusing failures.

Also, fix up bpf_prog_test_load() helper in selftests/bpf, which is
force-setting program type (even if that's completely unnecessary; this
is quite a legacy piece of code), and thus should expect auto-attach to
not work, yet one of the tests explicitly relies on auto-attach for
testing.

Instead, force-set program type only if it differs from the desired one.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20230327185202.1929145-2-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
  • Loading branch information
Andrii Nakryiko authored and Alexei Starovoitov committed Mar 30, 2023
1 parent 8b52cc2 commit d6e6286
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
1 change: 1 addition & 0 deletions tools/lib/bpf/libbpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -8468,6 +8468,7 @@ int bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
return libbpf_err(-EBUSY);

prog->type = type;
prog->sec_def = NULL;
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/testing_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ int bpf_prog_test_load(const char *file, enum bpf_prog_type type,
goto err_out;
}

if (type != BPF_PROG_TYPE_UNSPEC)
if (type != BPF_PROG_TYPE_UNSPEC && bpf_program__type(prog) != type)
bpf_program__set_type(prog, type);

flags = bpf_program__flags(prog) | BPF_F_TEST_RND_HI32;
Expand Down

0 comments on commit d6e6286

Please sign in to comment.