Commit 2d45ab1e authored by Vadim Fedorenko's avatar Vadim Fedorenko Committed by Alexei Starovoitov
Browse files

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: default avatarEduard Zingerman <eddyz87@gmail.com>
Signed-off-by: default avatarVadim Fedorenko <vadfed@meta.com>
Link: https://lore.kernel.org/r/20240613211817.1551967-6-vadfed@meta.com


Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 9b560751
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -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 = {
+1 −0
Original line number Diff line number Diff line
@@ -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 */
+11 −0
Original line number Diff line number Diff line
// 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);
}
+43 −0
Original line number Diff line number Diff line
// 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";