1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include "progs/core_reloc_types.h"
5 #define STRUCT_TO_CHAR_PTR(struct_name) (const char *)&(struct struct_name)
7 #define FLAVORS_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) { \
13 #define FLAVORS_CASE_COMMON(name) \
15 .bpf_obj_file = "test_core_reloc_flavors.o", \
16 .btf_src_file = "btf__core_reloc_" #name ".o" \
18 #define FLAVORS_CASE(name) { \
19 FLAVORS_CASE_COMMON(name), \
20 .input = FLAVORS_DATA(core_reloc_##name), \
21 .input_len = sizeof(struct core_reloc_##name), \
22 .output = FLAVORS_DATA(core_reloc_flavors), \
23 .output_len = sizeof(struct core_reloc_flavors), \
26 #define FLAVORS_ERR_CASE(name) { \
27 FLAVORS_CASE_COMMON(name), \
31 #define NESTING_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) { \
32 .a = { .a = { .a = 42 } }, \
33 .b = { .b = { .b = 0xc001 } }, \
36 #define NESTING_CASE_COMMON(name) \
38 .bpf_obj_file = "test_core_reloc_nesting.o", \
39 .btf_src_file = "btf__core_reloc_" #name ".o"
41 #define NESTING_CASE(name) { \
42 NESTING_CASE_COMMON(name), \
43 .input = NESTING_DATA(core_reloc_##name), \
44 .input_len = sizeof(struct core_reloc_##name), \
45 .output = NESTING_DATA(core_reloc_nesting), \
46 .output_len = sizeof(struct core_reloc_nesting) \
49 #define NESTING_ERR_CASE(name) { \
50 NESTING_CASE_COMMON(name), \
54 #define ARRAYS_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) { \
56 .b = { [1] = { [2] = { [3] = 2 } } }, \
57 .c = { [1] = { .c = 3 } }, \
58 .d = { [0] = { [0] = { .d = 4 } } }, \
61 #define ARRAYS_CASE_COMMON(name) \
63 .bpf_obj_file = "test_core_reloc_arrays.o", \
64 .btf_src_file = "btf__core_reloc_" #name ".o"
66 #define ARRAYS_CASE(name) { \
67 ARRAYS_CASE_COMMON(name), \
68 .input = ARRAYS_DATA(core_reloc_##name), \
69 .input_len = sizeof(struct core_reloc_##name), \
70 .output = STRUCT_TO_CHAR_PTR(core_reloc_arrays_output) { \
76 .output_len = sizeof(struct core_reloc_arrays_output) \
79 #define ARRAYS_ERR_CASE(name) { \
80 ARRAYS_CASE_COMMON(name), \
84 #define PRIMITIVES_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) { \
92 #define PRIMITIVES_CASE_COMMON(name) \
94 .bpf_obj_file = "test_core_reloc_primitives.o", \
95 .btf_src_file = "btf__core_reloc_" #name ".o"
97 #define PRIMITIVES_CASE(name) { \
98 PRIMITIVES_CASE_COMMON(name), \
99 .input = PRIMITIVES_DATA(core_reloc_##name), \
100 .input_len = sizeof(struct core_reloc_##name), \
101 .output = PRIMITIVES_DATA(core_reloc_primitives), \
102 .output_len = sizeof(struct core_reloc_primitives), \
105 #define PRIMITIVES_ERR_CASE(name) { \
106 PRIMITIVES_CASE_COMMON(name), \
110 #define MODS_CASE(name) { \
111 .case_name = #name, \
112 .bpf_obj_file = "test_core_reloc_mods.o", \
113 .btf_src_file = "btf__core_reloc_" #name ".o", \
114 .input = STRUCT_TO_CHAR_PTR(core_reloc_##name) { \
124 .input_len = sizeof(struct core_reloc_##name), \
125 .output = STRUCT_TO_CHAR_PTR(core_reloc_mods_output) { \
126 .a = 1, .b = 2, .c = 3, .d = 4, \
127 .e = 5, .f = 6, .g = 7, .h = 8, \
129 .output_len = sizeof(struct core_reloc_mods_output), \
132 #define PTR_AS_ARR_CASE(name) { \
133 .case_name = #name, \
134 .bpf_obj_file = "test_core_reloc_ptr_as_arr.o", \
135 .btf_src_file = "btf__core_reloc_" #name ".o", \
136 .input = (const char *)&(struct core_reloc_##name []){ \
141 .input_len = 3 * sizeof(struct core_reloc_##name), \
142 .output = STRUCT_TO_CHAR_PTR(core_reloc_ptr_as_arr) { \
145 .output_len = sizeof(struct core_reloc_ptr_as_arr), \
148 #define INTS_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) { \
159 #define INTS_CASE_COMMON(name) \
160 .case_name = #name, \
161 .bpf_obj_file = "test_core_reloc_ints.o", \
162 .btf_src_file = "btf__core_reloc_" #name ".o"
164 #define INTS_CASE(name) { \
165 INTS_CASE_COMMON(name), \
166 .input = INTS_DATA(core_reloc_##name), \
167 .input_len = sizeof(struct core_reloc_##name), \
168 .output = INTS_DATA(core_reloc_ints), \
169 .output_len = sizeof(struct core_reloc_ints), \
172 #define INTS_ERR_CASE(name) { \
173 INTS_CASE_COMMON(name), \
177 #define EXISTENCE_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) { \
181 #define EXISTENCE_CASE_COMMON(name) \
182 .case_name = #name, \
183 .bpf_obj_file = "test_core_reloc_existence.o", \
184 .btf_src_file = "btf__core_reloc_" #name ".o", \
185 .relaxed_core_relocs = true \
187 #define EXISTENCE_ERR_CASE(name) { \
188 EXISTENCE_CASE_COMMON(name), \
192 struct core_reloc_test_case {
193 const char *case_name;
194 const char *bpf_obj_file;
195 const char *btf_src_file;
201 bool relaxed_core_relocs;
204 static struct core_reloc_test_case test_cases[] = {
205 /* validate we can find kernel image and use its BTF for relocs */
207 .case_name = "kernel",
208 .bpf_obj_file = "test_core_reloc_kernel.o",
209 .btf_src_file = NULL, /* load from /lib/modules/$(uname -r) */
212 .output = STRUCT_TO_CHAR_PTR(core_reloc_kernel_output) {
213 .valid = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, },
214 .comm = "test_progs\0\0\0\0\0",
217 .output_len = sizeof(struct core_reloc_kernel_output),
220 /* validate BPF program can use multiple flavors to match against
221 * single target BTF type
223 FLAVORS_CASE(flavors),
225 FLAVORS_ERR_CASE(flavors__err_wrong_name),
227 /* various struct/enum nesting and resolution scenarios */
228 NESTING_CASE(nesting),
229 NESTING_CASE(nesting___anon_embed),
230 NESTING_CASE(nesting___struct_union_mixup),
231 NESTING_CASE(nesting___extra_nesting),
232 NESTING_CASE(nesting___dup_compat_types),
234 NESTING_ERR_CASE(nesting___err_missing_field),
235 NESTING_ERR_CASE(nesting___err_array_field),
236 NESTING_ERR_CASE(nesting___err_missing_container),
237 NESTING_ERR_CASE(nesting___err_nonstruct_container),
238 NESTING_ERR_CASE(nesting___err_array_container),
239 NESTING_ERR_CASE(nesting___err_dup_incompat_types),
240 NESTING_ERR_CASE(nesting___err_partial_match_dups),
241 NESTING_ERR_CASE(nesting___err_too_deep),
243 /* various array access relocation scenarios */
245 ARRAYS_CASE(arrays___diff_arr_dim),
246 ARRAYS_CASE(arrays___diff_arr_val_sz),
248 ARRAYS_ERR_CASE(arrays___err_too_small),
249 ARRAYS_ERR_CASE(arrays___err_too_shallow),
250 ARRAYS_ERR_CASE(arrays___err_non_array),
251 ARRAYS_ERR_CASE(arrays___err_wrong_val_type1),
252 ARRAYS_ERR_CASE(arrays___err_wrong_val_type2),
254 /* enum/ptr/int handling scenarios */
255 PRIMITIVES_CASE(primitives),
256 PRIMITIVES_CASE(primitives___diff_enum_def),
257 PRIMITIVES_CASE(primitives___diff_func_proto),
258 PRIMITIVES_CASE(primitives___diff_ptr_type),
260 PRIMITIVES_ERR_CASE(primitives___err_non_enum),
261 PRIMITIVES_ERR_CASE(primitives___err_non_int),
262 PRIMITIVES_ERR_CASE(primitives___err_non_ptr),
264 /* const/volatile/restrict and typedefs scenarios */
266 MODS_CASE(mods___mod_swap),
267 MODS_CASE(mods___typedefs),
269 /* handling "ptr is an array" semantics */
270 PTR_AS_ARR_CASE(ptr_as_arr),
271 PTR_AS_ARR_CASE(ptr_as_arr___diff_sz),
273 /* int signedness/sizing/bitfield handling */
275 INTS_CASE(ints___bool),
276 INTS_CASE(ints___reverse_sign),
278 INTS_ERR_CASE(ints___err_bitfield),
279 INTS_ERR_CASE(ints___err_wrong_sz_8),
280 INTS_ERR_CASE(ints___err_wrong_sz_16),
281 INTS_ERR_CASE(ints___err_wrong_sz_32),
282 INTS_ERR_CASE(ints___err_wrong_sz_64),
284 /* validate edge cases of capturing relocations */
287 .bpf_obj_file = "test_core_reloc_misc.o",
288 .btf_src_file = "btf__core_reloc_misc.o",
289 .input = (const char *)&(struct core_reloc_misc_extensible[]){
291 { .a = 2 }, /* not read */
294 .input_len = 4 * sizeof(int),
295 .output = STRUCT_TO_CHAR_PTR(core_reloc_misc_output) {
298 .c = 0, /* BUG in clang, should be 3 */
300 .output_len = sizeof(struct core_reloc_misc_output),
303 /* validate field existence checks */
305 EXISTENCE_CASE_COMMON(existence),
306 .input = STRUCT_TO_CHAR_PTR(core_reloc_existence) {
313 .input_len = sizeof(struct core_reloc_existence),
314 .output = STRUCT_TO_CHAR_PTR(core_reloc_existence_output) {
326 .output_len = sizeof(struct core_reloc_existence_output),
329 EXISTENCE_CASE_COMMON(existence___minimal),
330 .input = STRUCT_TO_CHAR_PTR(core_reloc_existence___minimal) {
333 .input_len = sizeof(struct core_reloc_existence),
334 .output = STRUCT_TO_CHAR_PTR(core_reloc_existence_output) {
341 .b_value = 0xff000002u,
342 .c_value = 0xff000003u,
343 .arr_value = 0xff000004u,
344 .s_value = 0xff000005u,
346 .output_len = sizeof(struct core_reloc_existence_output),
349 EXISTENCE_ERR_CASE(existence__err_int_sz),
350 EXISTENCE_ERR_CASE(existence__err_int_type),
351 EXISTENCE_ERR_CASE(existence__err_int_kind),
352 EXISTENCE_ERR_CASE(existence__err_arr_kind),
353 EXISTENCE_ERR_CASE(existence__err_arr_value_type),
354 EXISTENCE_ERR_CASE(existence__err_struct_type),
362 void test_core_reloc(void)
364 const char *probe_name = "raw_tracepoint/sys_enter";
365 struct bpf_object_load_attr load_attr = {};
366 struct core_reloc_test_case *test_case;
367 int err, duration = 0, i, equal;
368 struct bpf_link *link = NULL;
369 struct bpf_map *data_map;
370 struct bpf_program *prog;
371 struct bpf_object *obj;
375 for (i = 0; i < ARRAY_SIZE(test_cases); i++) {
376 test_case = &test_cases[i];
377 if (!test__start_subtest(test_case->case_name))
380 LIBBPF_OPTS(bpf_object_open_opts, opts,
381 .relaxed_core_relocs = test_case->relaxed_core_relocs,
384 obj = bpf_object__open_file(test_case->bpf_obj_file, &opts);
385 if (CHECK(IS_ERR_OR_NULL(obj), "obj_open",
386 "failed to open '%s': %ld\n",
387 test_case->bpf_obj_file, PTR_ERR(obj)))
390 prog = bpf_object__find_program_by_title(obj, probe_name);
391 if (CHECK(!prog, "find_probe",
392 "prog '%s' not found\n", probe_name))
394 bpf_program__set_type(prog, BPF_PROG_TYPE_RAW_TRACEPOINT);
397 load_attr.log_level = 0;
398 load_attr.target_btf_path = test_case->btf_src_file;
399 err = bpf_object__load_xattr(&load_attr);
400 if (test_case->fails) {
401 CHECK(!err, "obj_load_fail",
402 "should fail to load prog '%s'\n", probe_name);
405 if (CHECK(err, "obj_load",
406 "failed to load prog '%s': %d\n",
411 link = bpf_program__attach_raw_tracepoint(prog, "sys_enter");
412 if (CHECK(IS_ERR(link), "attach_raw_tp", "err %ld\n",
416 data_map = bpf_object__find_map_by_name(obj, "test_cor.bss");
417 if (CHECK(!data_map, "find_data_map", "data map not found\n"))
420 memset(&data, 0, sizeof(data));
421 memcpy(data.in, test_case->input, test_case->input_len);
423 err = bpf_map_update_elem(bpf_map__fd(data_map),
425 if (CHECK(err, "update_data_map",
426 "failed to update .data map: %d\n", err))
429 /* trigger test run */
432 err = bpf_map_lookup_elem(bpf_map__fd(data_map), &zero, &data);
433 if (CHECK(err, "get_result",
434 "failed to get output data: %d\n", err))
437 equal = memcmp(data.out, test_case->output,
438 test_case->output_len) == 0;
439 if (CHECK(!equal, "check_result",
440 "input/output data don't match\n")) {
443 for (j = 0; j < test_case->input_len; j++) {
444 printf("input byte #%d: 0x%02hhx\n",
445 j, test_case->input[j]);
447 for (j = 0; j < test_case->output_len; j++) {
448 printf("output byte #%d: EXP 0x%02hhx GOT 0x%02hhx\n",
449 j, test_case->output[j], data.out[j]);
455 if (!IS_ERR_OR_NULL(link)) {
456 bpf_link__destroy(link);
459 bpf_object__close(obj);