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 struct core_reloc_test_case
{
178 const char *case_name
;
179 const char *bpf_obj_file
;
180 const char *btf_src_file
;
188 static struct core_reloc_test_case test_cases
[] = {
189 /* validate we can find kernel image and use its BTF for relocs */
191 .case_name
= "kernel",
192 .bpf_obj_file
= "test_core_reloc_kernel.o",
193 .btf_src_file
= NULL
, /* load from /lib/modules/$(uname -r) */
196 .output
= "\1", /* true */
200 /* validate BPF program can use multiple flavors to match against
201 * single target BTF type
203 FLAVORS_CASE(flavors
),
205 FLAVORS_ERR_CASE(flavors__err_wrong_name
),
207 /* various struct/enum nesting and resolution scenarios */
208 NESTING_CASE(nesting
),
209 NESTING_CASE(nesting___anon_embed
),
210 NESTING_CASE(nesting___struct_union_mixup
),
211 NESTING_CASE(nesting___extra_nesting
),
212 NESTING_CASE(nesting___dup_compat_types
),
214 NESTING_ERR_CASE(nesting___err_missing_field
),
215 NESTING_ERR_CASE(nesting___err_array_field
),
216 NESTING_ERR_CASE(nesting___err_missing_container
),
217 NESTING_ERR_CASE(nesting___err_nonstruct_container
),
218 NESTING_ERR_CASE(nesting___err_array_container
),
219 NESTING_ERR_CASE(nesting___err_dup_incompat_types
),
220 NESTING_ERR_CASE(nesting___err_partial_match_dups
),
221 NESTING_ERR_CASE(nesting___err_too_deep
),
223 /* various array access relocation scenarios */
225 ARRAYS_CASE(arrays___diff_arr_dim
),
226 ARRAYS_CASE(arrays___diff_arr_val_sz
),
228 ARRAYS_ERR_CASE(arrays___err_too_small
),
229 ARRAYS_ERR_CASE(arrays___err_too_shallow
),
230 ARRAYS_ERR_CASE(arrays___err_non_array
),
231 ARRAYS_ERR_CASE(arrays___err_wrong_val_type1
),
232 ARRAYS_ERR_CASE(arrays___err_wrong_val_type2
),
234 /* enum/ptr/int handling scenarios */
235 PRIMITIVES_CASE(primitives
),
236 PRIMITIVES_CASE(primitives___diff_enum_def
),
237 PRIMITIVES_CASE(primitives___diff_func_proto
),
238 PRIMITIVES_CASE(primitives___diff_ptr_type
),
240 PRIMITIVES_ERR_CASE(primitives___err_non_enum
),
241 PRIMITIVES_ERR_CASE(primitives___err_non_int
),
242 PRIMITIVES_ERR_CASE(primitives___err_non_ptr
),
244 /* const/volatile/restrict and typedefs scenarios */
246 MODS_CASE(mods___mod_swap
),
247 MODS_CASE(mods___typedefs
),
249 /* handling "ptr is an array" semantics */
250 PTR_AS_ARR_CASE(ptr_as_arr
),
251 PTR_AS_ARR_CASE(ptr_as_arr___diff_sz
),
253 /* int signedness/sizing/bitfield handling */
255 INTS_CASE(ints___bool
),
256 INTS_CASE(ints___reverse_sign
),
258 INTS_ERR_CASE(ints___err_bitfield
),
259 INTS_ERR_CASE(ints___err_wrong_sz_8
),
260 INTS_ERR_CASE(ints___err_wrong_sz_16
),
261 INTS_ERR_CASE(ints___err_wrong_sz_32
),
262 INTS_ERR_CASE(ints___err_wrong_sz_64
),
264 /* validate edge cases of capturing relocations */
267 .bpf_obj_file
= "test_core_reloc_misc.o",
268 .btf_src_file
= "btf__core_reloc_misc.o",
269 .input
= (const char *)&(struct core_reloc_misc_extensible
[]){
271 { .a
= 2 }, /* not read */
274 .input_len
= 4 * sizeof(int),
275 .output
= STRUCT_TO_CHAR_PTR(core_reloc_misc_output
) {
278 .c
= 0, /* BUG in clang, should be 3 */
280 .output_len
= sizeof(struct core_reloc_misc_output
),
289 void test_core_reloc(void)
291 const char *probe_name
= "raw_tracepoint/sys_enter";
292 struct bpf_object_load_attr load_attr
= {};
293 struct core_reloc_test_case
*test_case
;
294 int err
, duration
= 0, i
, equal
;
295 struct bpf_link
*link
= NULL
;
296 struct bpf_map
*data_map
;
297 struct bpf_program
*prog
;
298 struct bpf_object
*obj
;
302 for (i
= 0; i
< ARRAY_SIZE(test_cases
); i
++) {
303 test_case
= &test_cases
[i
];
305 if (!test__start_subtest(test_case
->case_name
))
308 obj
= bpf_object__open(test_case
->bpf_obj_file
);
309 if (CHECK(IS_ERR_OR_NULL(obj
), "obj_open",
310 "failed to open '%s': %ld\n",
311 test_case
->bpf_obj_file
, PTR_ERR(obj
)))
314 prog
= bpf_object__find_program_by_title(obj
, probe_name
);
315 if (CHECK(!prog
, "find_probe",
316 "prog '%s' not found\n", probe_name
))
318 bpf_program__set_type(prog
, BPF_PROG_TYPE_RAW_TRACEPOINT
);
321 load_attr
.log_level
= 0;
322 load_attr
.target_btf_path
= test_case
->btf_src_file
;
323 err
= bpf_object__load_xattr(&load_attr
);
324 if (test_case
->fails
) {
325 CHECK(!err
, "obj_load_fail",
326 "should fail to load prog '%s'\n", probe_name
);
329 if (CHECK(err
, "obj_load",
330 "failed to load prog '%s': %d\n",
335 link
= bpf_program__attach_raw_tracepoint(prog
, "sys_enter");
336 if (CHECK(IS_ERR(link
), "attach_raw_tp", "err %ld\n",
340 data_map
= bpf_object__find_map_by_name(obj
, "test_cor.bss");
341 if (CHECK(!data_map
, "find_data_map", "data map not found\n"))
344 memset(&data
, 0, sizeof(data
));
345 memcpy(data
.in
, test_case
->input
, test_case
->input_len
);
347 err
= bpf_map_update_elem(bpf_map__fd(data_map
),
349 if (CHECK(err
, "update_data_map",
350 "failed to update .data map: %d\n", err
))
353 /* trigger test run */
356 err
= bpf_map_lookup_elem(bpf_map__fd(data_map
), &zero
, &data
);
357 if (CHECK(err
, "get_result",
358 "failed to get output data: %d\n", err
))
361 equal
= memcmp(data
.out
, test_case
->output
,
362 test_case
->output_len
) == 0;
363 if (CHECK(!equal
, "check_result",
364 "input/output data don't match\n")) {
367 for (j
= 0; j
< test_case
->input_len
; j
++) {
368 printf("input byte #%d: 0x%02hhx\n",
369 j
, test_case
->input
[j
]);
371 for (j
= 0; j
< test_case
->output_len
; j
++) {
372 printf("output byte #%d: EXP 0x%02hhx GOT 0x%02hhx\n",
373 j
, test_case
->output
[j
], data
.out
[j
]);
379 if (!IS_ERR_OR_NULL(link
)) {
380 bpf_link__destroy(link
);
383 bpf_object__close(obj
);