Skip to content

Commit cecfd36

Browse files
iii-iKernel Patches Daemon
authored andcommitted
selftests/bpf: Annotate bpf_obj_new_impl() with __must_check
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 <[email protected]> Signed-off-by: Ilya Leoshkevich <[email protected]>
1 parent 8f9c214 commit cecfd36

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

tools/lib/bpf/bpf_helpers.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@
6969
*/
7070
#define __hidden __attribute__((visibility("hidden")))
7171

72+
#ifndef __must_check
73+
#define __must_check __attribute__((__warn_unused_result__))
74+
#endif
75+
7276
/* When utilizing vmlinux.h with BPF CO-RE, user BPF programs can't include
7377
* any system-level headers (such as stddef.h, linux/version.h, etc), and
7478
* commonly-used macros like NULL and KERNEL_VERSION aren't available through

tools/testing/selftests/bpf/bpf_experimental.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* A pointer to an object of the type corresponding to the passed in
2121
* 'local_type_id', or NULL on failure.
2222
*/
23-
extern void *bpf_obj_new_impl(__u64 local_type_id, void *meta) __ksym;
23+
extern __must_check void *bpf_obj_new_impl(__u64 local_type_id, void *meta) __ksym;
2424

2525
/* Convenience macro to wrap over bpf_obj_new_impl */
2626
#define bpf_obj_new(type) ((type *)bpf_obj_new_impl(bpf_core_type_id_local(type), NULL))

tools/testing/selftests/bpf/progs/linked_list_fail.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,22 +212,22 @@ int map_compat_raw_tp_w(void *ctx)
212212
SEC("?tc")
213213
int obj_type_id_oor(void *ctx)
214214
{
215-
bpf_obj_new_impl(~0UL, NULL);
215+
(void)bpf_obj_new_impl(~0UL, NULL);
216216
return 0;
217217
}
218218

219219
SEC("?tc")
220220
int obj_new_no_composite(void *ctx)
221221
{
222-
bpf_obj_new_impl(bpf_core_type_id_local(int), (void *)42);
222+
(void)bpf_obj_new_impl(bpf_core_type_id_local(int), (void *)42);
223223
return 0;
224224
}
225225

226226
SEC("?tc")
227227
int obj_new_no_struct(void *ctx)
228228
{
229229

230-
bpf_obj_new(union { int data; unsigned udata; });
230+
(void)bpf_obj_new(union { int data; unsigned udata; });
231231
return 0;
232232
}
233233

@@ -252,7 +252,7 @@ int new_null_ret(void *ctx)
252252
SEC("?tc")
253253
int obj_new_acq(void *ctx)
254254
{
255-
bpf_obj_new(struct foo);
255+
(void)bpf_obj_new(struct foo);
256256
return 0;
257257
}
258258

0 commit comments

Comments
 (0)