Skip to content

Commit 2d45ab1

Browse files
vvfedorenkoAlexei Starovoitov
authored andcommitted
selftests: bpf: add testmod kfunc for nullable params
Add special test to be sure that only __nullable BTF params can be replaced by NULL. This patch adds fake kfuncs in bpf_testmod to properly test different params. Acked-by: Eduard Zingerman <[email protected]> Signed-off-by: Vadim Fedorenko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 9b56075 commit 2d45ab1

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ __bpf_kfunc void bpf_kfunc_common_test(void)
154154
{
155155
}
156156

157+
__bpf_kfunc void bpf_kfunc_dynptr_test(struct bpf_dynptr *ptr,
158+
struct bpf_dynptr *ptr__nullable)
159+
{
160+
}
161+
157162
struct bpf_testmod_btf_type_tag_1 {
158163
int a;
159164
};
@@ -363,6 +368,7 @@ BTF_ID_FLAGS(func, bpf_iter_testmod_seq_new, KF_ITER_NEW)
363368
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_next, KF_ITER_NEXT | KF_RET_NULL)
364369
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_destroy, KF_ITER_DESTROY)
365370
BTF_ID_FLAGS(func, bpf_kfunc_common_test)
371+
BTF_ID_FLAGS(func, bpf_kfunc_dynptr_test)
366372
BTF_KFUNCS_END(bpf_testmod_common_kfunc_ids)
367373

368374
static const struct btf_kfunc_id_set bpf_testmod_common_kfunc_set = {

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,5 @@ int bpf_kfunc_call_sock_sendmsg(struct sendmsg_args *args) __ksym;
134134
int bpf_kfunc_call_kernel_getsockname(struct addr_args *args) __ksym;
135135
int bpf_kfunc_call_kernel_getpeername(struct addr_args *args) __ksym;
136136

137+
void bpf_kfunc_dynptr_test(struct bpf_dynptr *ptr, struct bpf_dynptr *ptr__nullable) __ksym;
137138
#endif /* _BPF_TESTMOD_KFUNC_H */
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
/* Copyright (c) 2024 Meta Platforms, Inc */
4+
5+
#include <test_progs.h>
6+
#include "test_kfunc_param_nullable.skel.h"
7+
8+
void test_kfunc_param_nullable(void)
9+
{
10+
RUN_TESTS(test_kfunc_param_nullable);
11+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2024 Meta Platforms, Inc */
3+
#include <vmlinux.h>
4+
#include <bpf/bpf_helpers.h>
5+
#include "bpf_misc.h"
6+
#include "bpf_kfuncs.h"
7+
#include "../bpf_testmod/bpf_testmod_kfunc.h"
8+
9+
SEC("tc")
10+
int kfunc_dynptr_nullable_test1(struct __sk_buff *skb)
11+
{
12+
struct bpf_dynptr data;
13+
14+
bpf_dynptr_from_skb(skb, 0, &data);
15+
bpf_kfunc_dynptr_test(&data, NULL);
16+
17+
return 0;
18+
}
19+
20+
SEC("tc")
21+
int kfunc_dynptr_nullable_test2(struct __sk_buff *skb)
22+
{
23+
struct bpf_dynptr data;
24+
25+
bpf_dynptr_from_skb(skb, 0, &data);
26+
bpf_kfunc_dynptr_test(&data, &data);
27+
28+
return 0;
29+
}
30+
31+
SEC("tc")
32+
__failure __msg("expected pointer to stack or dynptr_ptr")
33+
int kfunc_dynptr_nullable_test3(struct __sk_buff *skb)
34+
{
35+
struct bpf_dynptr data;
36+
37+
bpf_dynptr_from_skb(skb, 0, &data);
38+
bpf_kfunc_dynptr_test(NULL, &data);
39+
40+
return 0;
41+
}
42+
43+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)