Skip to content

Commit c634e4c

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. Note that now there two different bpf_obj_new_impl() declarations: one with __must_check from bpf_experimental.h, and one without from vmlinux.h. According to the GCC doc [1] this is fine and has the desired effect: Compatible attribute specifications on distinct declarations of the same function are merged. [1] https://gcc.gnu.org/onlinedocs/gcc-12.4.0/gcc/Function-Attributes.html 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 c9c941d commit c634e4c

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

tools/testing/selftests/bpf/bpf_experimental.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
#define __contains(name, node) __attribute__((btf_decl_tag("contains:" #name ":" #node)))
1010

11+
#ifndef __must_check
12+
#define __must_check __attribute__((__warn_unused_result__))
13+
#endif
14+
1115
/* Description
1216
* Allocates an object of the type represented by 'local_type_id' in
1317
* program BTF. User may use the bpf_core_type_id_local macro to pass the
@@ -20,7 +24,7 @@
2024
* A pointer to an object of the type corresponding to the passed in
2125
* 'local_type_id', or NULL on failure.
2226
*/
23-
extern void *bpf_obj_new_impl(__u64 local_type_id, void *meta) __ksym;
27+
extern __must_check void *bpf_obj_new_impl(__u64 local_type_id, void *meta) __ksym;
2428

2529
/* Convenience macro to wrap over bpf_obj_new_impl */
2630
#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: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,22 +212,33 @@ 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 *f;
216+
217+
f = bpf_obj_new_impl(~0UL, NULL);
218+
(void)f;
219+
216220
return 0;
217221
}
218222

219223
SEC("?tc")
220224
int obj_new_no_composite(void *ctx)
221225
{
222-
bpf_obj_new_impl(bpf_core_type_id_local(int), (void *)42);
226+
void *f;
227+
228+
f = bpf_obj_new_impl(bpf_core_type_id_local(int), (void *)42);
229+
(void)f;
230+
223231
return 0;
224232
}
225233

226234
SEC("?tc")
227235
int obj_new_no_struct(void *ctx)
228236
{
237+
void *f;
238+
239+
f = bpf_obj_new(union { int data; unsigned udata; });
240+
(void)f;
229241

230-
bpf_obj_new(union { int data; unsigned udata; });
231242
return 0;
232243
}
233244

@@ -252,7 +263,11 @@ int new_null_ret(void *ctx)
252263
SEC("?tc")
253264
int obj_new_acq(void *ctx)
254265
{
255-
bpf_obj_new(struct foo);
266+
void *f;
267+
268+
f = bpf_obj_new(struct foo);
269+
(void)f;
270+
256271
return 0;
257272
}
258273

0 commit comments

Comments
 (0)