The verifier requires that pointers returned by bpf_obj_new_impl() are either dropped or stored in a map. Therefore programs that do not use its return values will fail to load. Make the compiler point out these issues. Adjust selftests that check that the verifier does indeed spot these bugs. Link: https://lore.kernel.org/bpf/CAADnVQL6Q+QRv3_JwEd26biwGpFYcwD_=BjBJWLAtpgOP9CKRw@mail.gmail.com/ Suggested-by: Alexei Starovoitov Signed-off-by: Ilya Leoshkevich --- tools/lib/bpf/bpf_helpers.h | 4 ++++ tools/testing/selftests/bpf/bpf_experimental.h | 2 +- tools/testing/selftests/bpf/progs/linked_list_fail.c | 8 ++++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h index 80c028540656..e1496a328e3f 100644 --- a/tools/lib/bpf/bpf_helpers.h +++ b/tools/lib/bpf/bpf_helpers.h @@ -69,6 +69,10 @@ */ #define __hidden __attribute__((visibility("hidden"))) +#ifndef __must_check +#define __must_check __attribute__((__warn_unused_result__)) +#endif + /* When utilizing vmlinux.h with BPF CO-RE, user BPF programs can't include * any system-level headers (such as stddef.h, linux/version.h, etc), and * commonly-used macros like NULL and KERNEL_VERSION aren't available through diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing/selftests/bpf/bpf_experimental.h index da7e230f2781..e5ef4792da42 100644 --- a/tools/testing/selftests/bpf/bpf_experimental.h +++ b/tools/testing/selftests/bpf/bpf_experimental.h @@ -20,7 +20,7 @@ * A pointer to an object of the type corresponding to the passed in * 'local_type_id', or NULL on failure. */ -extern void *bpf_obj_new_impl(__u64 local_type_id, void *meta) __ksym; +extern __must_check void *bpf_obj_new_impl(__u64 local_type_id, void *meta) __ksym; /* Convenience macro to wrap over bpf_obj_new_impl */ #define bpf_obj_new(type) ((type *)bpf_obj_new_impl(bpf_core_type_id_local(type), NULL)) diff --git a/tools/testing/selftests/bpf/progs/linked_list_fail.c b/tools/testing/selftests/bpf/progs/linked_list_fail.c index 6438982b928b..84883f04d58b 100644 --- a/tools/testing/selftests/bpf/progs/linked_list_fail.c +++ b/tools/testing/selftests/bpf/progs/linked_list_fail.c @@ -212,14 +212,14 @@ int map_compat_raw_tp_w(void *ctx) SEC("?tc") int obj_type_id_oor(void *ctx) { - bpf_obj_new_impl(~0UL, NULL); + (void)bpf_obj_new_impl(~0UL, NULL); return 0; } SEC("?tc") int obj_new_no_composite(void *ctx) { - bpf_obj_new_impl(bpf_core_type_id_local(int), (void *)42); + (void)bpf_obj_new_impl(bpf_core_type_id_local(int), (void *)42); return 0; } @@ -227,7 +227,7 @@ SEC("?tc") int obj_new_no_struct(void *ctx) { - bpf_obj_new(union { int data; unsigned udata; }); + (void)bpf_obj_new(union { int data; unsigned udata; }); return 0; } @@ -252,7 +252,7 @@ int new_null_ret(void *ctx) SEC("?tc") int obj_new_acq(void *ctx) { - bpf_obj_new(struct foo); + (void)bpf_obj_new(struct foo); return 0; } -- 2.50.1