1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
3 #include <test_progs.h>
4 #include <time.h>
5 
6 #include "struct_ops_module.skel.h"
7 
8 static void check_map_info(struct bpf_map_info *info)
9 {
10 	struct bpf_btf_info btf_info;
11 	char btf_name[256];
12 	u32 btf_info_len = sizeof(btf_info);
13 	int err, fd;
14 
15 	fd = bpf_btf_get_fd_by_id(info->btf_vmlinux_id);
16 	if (!ASSERT_GE(fd, 0, "get_value_type_btf_obj_fd"))
17 		return;
18 
19 	memset(&btf_info, 0, sizeof(btf_info));
20 	btf_info.name = ptr_to_u64(btf_name);
21 	btf_info.name_len = sizeof(btf_name);
22 	err = bpf_btf_get_info_by_fd(fd, &btf_info, &btf_info_len);
23 	if (!ASSERT_OK(err, "get_value_type_btf_obj_info"))
24 		goto cleanup;
25 
26 	if (!ASSERT_EQ(strcmp(btf_name, "bpf_testmod"), 0, "get_value_type_btf_obj_name"))
27 		goto cleanup;
28 
29 cleanup:
30 	close(fd);
31 }
32 
33 static int attach_ops_and_check(struct struct_ops_module *skel,
34 				struct bpf_map *map,
35 				int expected_test_2_result)
36 {
37 	struct bpf_link *link;
38 
39 	link = bpf_map__attach_struct_ops(map);
40 	ASSERT_OK_PTR(link, "attach_test_mod_1");
41 	if (!link)
42 		return -1;
43 
44 	/* test_{1,2}() would be called from bpf_dummy_reg() in bpf_testmod.c */
45 	ASSERT_EQ(skel->bss->test_1_result, 0xdeadbeef, "test_1_result");
46 	ASSERT_EQ(skel->bss->test_2_result, expected_test_2_result, "test_2_result");
47 
48 	bpf_link__destroy(link);
49 	return 0;
50 }
51 
52 static void test_struct_ops_load(void)
53 {
54 	struct struct_ops_module *skel;
55 	struct bpf_map_info info = {};
56 	int err;
57 	u32 len;
58 
59 	skel = struct_ops_module__open();
60 	if (!ASSERT_OK_PTR(skel, "struct_ops_module_open"))
61 		return;
62 
63 	skel->struct_ops.testmod_1->data = 13;
64 	skel->struct_ops.testmod_1->test_2 = skel->progs.test_3;
65 	/* Since test_2() is not being used, it should be disabled from
66 	 * auto-loading, or it will fail to load.
67 	 */
68 	bpf_program__set_autoload(skel->progs.test_2, false);
69 
70 	err = struct_ops_module__load(skel);
71 	if (!ASSERT_OK(err, "struct_ops_module_load"))
72 		goto cleanup;
73 
74 	len = sizeof(info);
75 	err = bpf_map_get_info_by_fd(bpf_map__fd(skel->maps.testmod_1), &info,
76 				     &len);
77 	if (!ASSERT_OK(err, "bpf_map_get_info_by_fd"))
78 		goto cleanup;
79 
80 	check_map_info(&info);
81 	/* test_3() will be called from bpf_dummy_reg() in bpf_testmod.c
82 	 *
83 	 * In bpf_testmod.c it will pass 4 and 13 (the value of data) to
84 	 * .test_2.  So, the value of test_2_result should be 20 (4 + 13 +
85 	 * 3).
86 	 */
87 	if (!attach_ops_and_check(skel, skel->maps.testmod_1, 20))
88 		goto cleanup;
89 	if (!attach_ops_and_check(skel, skel->maps.testmod_2, 12))
90 		goto cleanup;
91 
92 cleanup:
93 	struct_ops_module__destroy(skel);
94 }
95 
96 static void test_struct_ops_not_zeroed(void)
97 {
98 	struct struct_ops_module *skel;
99 	int err;
100 
101 	/* zeroed is 0, and zeroed_op is null */
102 	skel = struct_ops_module__open();
103 	if (!ASSERT_OK_PTR(skel, "struct_ops_module_open"))
104 		return;
105 
106 	err = struct_ops_module__load(skel);
107 	ASSERT_OK(err, "struct_ops_module_load");
108 
109 	struct_ops_module__destroy(skel);
110 
111 	/* zeroed is not 0 */
112 	skel = struct_ops_module__open();
113 	if (!ASSERT_OK_PTR(skel, "struct_ops_module_open_not_zeroed"))
114 		return;
115 
116 	/* libbpf should reject the testmod_zeroed since struct
117 	 * bpf_testmod_ops in the kernel has no "zeroed" field and the
118 	 * value of "zeroed" is non-zero.
119 	 */
120 	skel->struct_ops.testmod_zeroed->zeroed = 0xdeadbeef;
121 	err = struct_ops_module__load(skel);
122 	ASSERT_ERR(err, "struct_ops_module_load_not_zeroed");
123 
124 	struct_ops_module__destroy(skel);
125 
126 	/* zeroed_op is not null */
127 	skel = struct_ops_module__open();
128 	if (!ASSERT_OK_PTR(skel, "struct_ops_module_open_not_zeroed_op"))
129 		return;
130 
131 	/* libbpf should reject the testmod_zeroed since the value of its
132 	 * "zeroed_op" is not null.
133 	 */
134 	skel->struct_ops.testmod_zeroed->zeroed_op = skel->progs.test_3;
135 	err = struct_ops_module__load(skel);
136 	ASSERT_ERR(err, "struct_ops_module_load_not_zeroed_op");
137 
138 	struct_ops_module__destroy(skel);
139 }
140 
141 /* The signature of an implementation might not match the signature of the
142  * function pointer prototype defined in the BPF program. This mismatch
143  * should be allowed as long as the behavior of the operator program
144  * adheres to the signature in the kernel. Libbpf should not enforce the
145  * signature; rather, let the kernel verifier handle the enforcement.
146  */
147 static void test_struct_ops_incompatible(void)
148 {
149 	struct struct_ops_module *skel;
150 	struct bpf_link *link;
151 
152 	skel = struct_ops_module__open_and_load();
153 	if (!ASSERT_OK_PTR(skel, "open_and_load"))
154 		return;
155 
156 	link = bpf_map__attach_struct_ops(skel->maps.testmod_incompatible);
157 	if (ASSERT_OK_PTR(link, "attach_struct_ops"))
158 		bpf_link__destroy(link);
159 
160 	struct_ops_module__destroy(skel);
161 }
162 
163 void serial_test_struct_ops_module(void)
164 {
165 	if (test__start_subtest("test_struct_ops_load"))
166 		test_struct_ops_load();
167 	if (test__start_subtest("test_struct_ops_not_zeroed"))
168 		test_struct_ops_not_zeroed();
169 	if (test__start_subtest("test_struct_ops_incompatible"))
170 		test_struct_ops_incompatible();
171 }
172 
173