summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/prog_tests/test_struct_ops_nullable.c
blob: 028f070f2e5f4576ee296860a6b6391040706ba9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
#include <test_progs.h>

#include "struct_ops_nullable.skel.h"
#include "struct_ops_nullable_fail.skel.h"
#include "struct_ops_nullable_fail__percpu.skel.h"
#include "struct_ops_nullable_probed__plain.skel.h"
#include "struct_ops_nullable_probed__scalar.skel.h"
#include "struct_ops_nullable_probed__untrusted.skel.h"

/*
 * Attach a callback that gets a trusted and a trusted __nullable task pointer
 * through its argument struct and drive it through the
 * bpf_testmod_ops3_call_test_nullable() kfunc.
 */
static void nullable_member(void)
{
	LIBBPF_OPTS(bpf_test_run_opts, topts);
	struct struct_ops_nullable *skel;
	struct bpf_link *link;
	int err;

	skel = struct_ops_nullable__open_and_load();
	if (!ASSERT_OK_PTR(skel, "struct_ops_nullable__open_and_load"))
		return;

	link = bpf_map__attach_struct_ops(skel->maps.testmod_nullable);
	if (!ASSERT_OK_PTR(link, "attach_struct_ops"))
		goto out;

	err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.trigger), &topts);
	ASSERT_OK(err, "test_run");
	ASSERT_EQ(topts.retval, 0, "trigger_retval");

	bpf_link__destroy(link);
out:
	struct_ops_nullable__destroy(skel);
}

/* Serialized because it attaches the singleton bpf_testmod_ops3. */
void serial_test_struct_ops_nullable(void)
{
	int has_tag;

	/* the tag reaches the module BTF only from a toolchain with btf_type_tag support */
	has_tag = testmod_member_has_type_tag("bpf_testmod_nullable_args", "task_or_null", "nullable");
	if (!ASSERT_GE(has_tag, 0, "testmod_btf"))
		return;
	if (!has_tag) {
		printf("%s:SKIP: no nullable type tag in the bpf_testmod BTF\n", __func__);
		test__skip();
		return;
	}
	if (test__start_subtest("nullable_member"))
		nullable_member();
	RUN_TESTS(struct_ops_nullable_fail);
	RUN_TESTS(struct_ops_nullable_fail__percpu);
	RUN_TESTS(struct_ops_nullable_probed__plain);
	RUN_TESTS(struct_ops_nullable_probed__scalar);
	RUN_TESTS(struct_ops_nullable_probed__untrusted);
}