1

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 <eddyz87@gmail.com>
Signed-off-by: Vadim Fedorenko <vadfed@meta.com>
Link: https://lore.kernel.org/r/20240613211817.1551967-6-vadfed@meta.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
Vadim Fedorenko 2024-06-13 14:18:17 -07:00 committed by Alexei Starovoitov
parent 9b560751f7
commit 2d45ab1eda
4 changed files with 61 additions and 0 deletions

View File

@ -154,6 +154,11 @@ __bpf_kfunc void bpf_kfunc_common_test(void)
{
}
__bpf_kfunc void bpf_kfunc_dynptr_test(struct bpf_dynptr *ptr,
struct bpf_dynptr *ptr__nullable)
{
}
struct bpf_testmod_btf_type_tag_1 {
int a;
};
@ -363,6 +368,7 @@ BTF_ID_FLAGS(func, bpf_iter_testmod_seq_new, KF_ITER_NEW)
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_next, KF_ITER_NEXT | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_destroy, KF_ITER_DESTROY)
BTF_ID_FLAGS(func, bpf_kfunc_common_test)
BTF_ID_FLAGS(func, bpf_kfunc_dynptr_test)
BTF_KFUNCS_END(bpf_testmod_common_kfunc_ids)
static const struct btf_kfunc_id_set bpf_testmod_common_kfunc_set = {

View File

@ -134,4 +134,5 @@ int bpf_kfunc_call_sock_sendmsg(struct sendmsg_args *args) __ksym;
int bpf_kfunc_call_kernel_getsockname(struct addr_args *args) __ksym;
int bpf_kfunc_call_kernel_getpeername(struct addr_args *args) __ksym;
void bpf_kfunc_dynptr_test(struct bpf_dynptr *ptr, struct bpf_dynptr *ptr__nullable) __ksym;
#endif /* _BPF_TESTMOD_KFUNC_H */

View File

@ -0,0 +1,11 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2024 Meta Platforms, Inc */
#include <test_progs.h>
#include "test_kfunc_param_nullable.skel.h"
void test_kfunc_param_nullable(void)
{
RUN_TESTS(test_kfunc_param_nullable);
}

View File

@ -0,0 +1,43 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2024 Meta Platforms, Inc */
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
#include "bpf_kfuncs.h"
#include "../bpf_testmod/bpf_testmod_kfunc.h"
SEC("tc")
int kfunc_dynptr_nullable_test1(struct __sk_buff *skb)
{
struct bpf_dynptr data;
bpf_dynptr_from_skb(skb, 0, &data);
bpf_kfunc_dynptr_test(&data, NULL);
return 0;
}
SEC("tc")
int kfunc_dynptr_nullable_test2(struct __sk_buff *skb)
{
struct bpf_dynptr data;
bpf_dynptr_from_skb(skb, 0, &data);
bpf_kfunc_dynptr_test(&data, &data);
return 0;
}
SEC("tc")
__failure __msg("expected pointer to stack or dynptr_ptr")
int kfunc_dynptr_nullable_test3(struct __sk_buff *skb)
{
struct bpf_dynptr data;
bpf_dynptr_from_skb(skb, 0, &data);
bpf_kfunc_dynptr_test(NULL, &data);
return 0;
}
char _license[] SEC("license") = "GPL";