1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include "progs/core_reloc_types.h"
5 #include <sys/syscall.h>
7 #define STRUCT_TO_CHAR_PTR(struct_name) (const char *)&(struct struct_name)
9 #define FLAVORS_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) { \
15 #define FLAVORS_CASE_COMMON(name) \
17 .bpf_obj_file = "test_core_reloc_flavors.o", \
18 .btf_src_file = "btf__core_reloc_" #name ".o" \
20 #define FLAVORS_CASE(name) { \
21 FLAVORS_CASE_COMMON(name), \
22 .input = FLAVORS_DATA(core_reloc_##name), \
23 .input_len = sizeof(struct core_reloc_##name), \
24 .output = FLAVORS_DATA(core_reloc_flavors), \
25 .output_len = sizeof(struct core_reloc_flavors), \
28 #define FLAVORS_ERR_CASE(name) { \
29 FLAVORS_CASE_COMMON(name), \
33 #define NESTING_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) { \
34 .a = { .a = { .a = 42 } }, \
35 .b = { .b = { .b = 0xc001 } }, \
38 #define NESTING_CASE_COMMON(name) \
40 .bpf_obj_file = "test_core_reloc_nesting.o", \
41 .btf_src_file = "btf__core_reloc_" #name ".o"
43 #define NESTING_CASE(name) { \
44 NESTING_CASE_COMMON(name), \
45 .input = NESTING_DATA(core_reloc_##name), \
46 .input_len = sizeof(struct core_reloc_##name), \
47 .output = NESTING_DATA(core_reloc_nesting), \
48 .output_len = sizeof(struct core_reloc_nesting) \
51 #define NESTING_ERR_CASE(name) { \
52 NESTING_CASE_COMMON(name), \
56 #define ARRAYS_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) { \
58 .b = { [1] = { [2] = { [3] = 2 } } }, \
59 .c = { [1] = { .c = 3 } }, \
60 .d = { [0] = { [0] = { .d = 4 } } }, \
63 #define ARRAYS_CASE_COMMON(name) \
65 .bpf_obj_file = "test_core_reloc_arrays.o", \
66 .btf_src_file = "btf__core_reloc_" #name ".o"
68 #define ARRAYS_CASE(name) { \
69 ARRAYS_CASE_COMMON(name), \
70 .input = ARRAYS_DATA(core_reloc_##name), \
71 .input_len = sizeof(struct core_reloc_##name), \
72 .output = STRUCT_TO_CHAR_PTR(core_reloc_arrays_output) { \
78 .output_len = sizeof(struct core_reloc_arrays_output) \
81 #define ARRAYS_ERR_CASE(name) { \
82 ARRAYS_CASE_COMMON(name), \
86 #define PRIMITIVES_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) { \
94 #define PRIMITIVES_CASE_COMMON(name) \
96 .bpf_obj_file = "test_core_reloc_primitives.o", \
97 .btf_src_file = "btf__core_reloc_" #name ".o"
99 #define PRIMITIVES_CASE(name) { \
100 PRIMITIVES_CASE_COMMON(name), \
101 .input = PRIMITIVES_DATA(core_reloc_##name), \
102 .input_len = sizeof(struct core_reloc_##name), \
103 .output = PRIMITIVES_DATA(core_reloc_primitives), \
104 .output_len = sizeof(struct core_reloc_primitives), \
107 #define PRIMITIVES_ERR_CASE(name) { \
108 PRIMITIVES_CASE_COMMON(name), \
112 #define MODS_CASE(name) { \
113 .case_name = #name, \
114 .bpf_obj_file = "test_core_reloc_mods.o", \
115 .btf_src_file = "btf__core_reloc_" #name ".o", \
116 .input = STRUCT_TO_CHAR_PTR(core_reloc_##name) { \
126 .input_len = sizeof(struct core_reloc_##name), \
127 .output = STRUCT_TO_CHAR_PTR(core_reloc_mods_output) { \
128 .a = 1, .b = 2, .c = 3, .d = 4, \
129 .e = 5, .f = 6, .g = 7, .h = 8, \
131 .output_len = sizeof(struct core_reloc_mods_output), \
134 #define PTR_AS_ARR_CASE(name) { \
135 .case_name = #name, \
136 .bpf_obj_file = "test_core_reloc_ptr_as_arr.o", \
137 .btf_src_file = "btf__core_reloc_" #name ".o", \
138 .input = (const char *)&(struct core_reloc_##name []){ \
143 .input_len = 3 * sizeof(struct core_reloc_##name), \
144 .output = STRUCT_TO_CHAR_PTR(core_reloc_ptr_as_arr) { \
147 .output_len = sizeof(struct core_reloc_ptr_as_arr), \
150 #define INTS_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) { \
161 #define INTS_CASE_COMMON(name) \
162 .case_name = #name, \
163 .bpf_obj_file = "test_core_reloc_ints.o", \
164 .btf_src_file = "btf__core_reloc_" #name ".o"
166 #define INTS_CASE(name) { \
167 INTS_CASE_COMMON(name), \
168 .input = INTS_DATA(core_reloc_##name), \
169 .input_len = sizeof(struct core_reloc_##name), \
170 .output = INTS_DATA(core_reloc_ints), \
171 .output_len = sizeof(struct core_reloc_ints), \
174 #define INTS_ERR_CASE(name) { \
175 INTS_CASE_COMMON(name), \
179 #define EXISTENCE_CASE_COMMON(name) \
180 .case_name = #name, \
181 .bpf_obj_file = "test_core_reloc_existence.o", \
182 .btf_src_file = "btf__core_reloc_" #name ".o", \
183 .relaxed_core_relocs = true
185 #define EXISTENCE_ERR_CASE(name) { \
186 EXISTENCE_CASE_COMMON(name), \
190 #define BITFIELDS_CASE_COMMON(objfile, test_name_prefix, name) \
191 .case_name = test_name_prefix#name, \
192 .bpf_obj_file = objfile, \
193 .btf_src_file = "btf__core_reloc_" #name ".o"
195 #define BITFIELDS_CASE(name, ...) { \
196 BITFIELDS_CASE_COMMON("test_core_reloc_bitfields_probed.o", \
198 .input = STRUCT_TO_CHAR_PTR(core_reloc_##name) __VA_ARGS__, \
199 .input_len = sizeof(struct core_reloc_##name), \
200 .output = STRUCT_TO_CHAR_PTR(core_reloc_bitfields_output) \
202 .output_len = sizeof(struct core_reloc_bitfields_output), \
204 BITFIELDS_CASE_COMMON("test_core_reloc_bitfields_direct.o", \
206 .input = STRUCT_TO_CHAR_PTR(core_reloc_##name) __VA_ARGS__, \
207 .input_len = sizeof(struct core_reloc_##name), \
208 .output = STRUCT_TO_CHAR_PTR(core_reloc_bitfields_output) \
210 .output_len = sizeof(struct core_reloc_bitfields_output), \
211 .direct_raw_tp = true, \
215 #define BITFIELDS_ERR_CASE(name) { \
216 BITFIELDS_CASE_COMMON("test_core_reloc_bitfields_probed.o", \
220 BITFIELDS_CASE_COMMON("test_core_reloc_bitfields_direct.o", \
222 .direct_raw_tp = true, \
226 #define SIZE_CASE_COMMON(name) \
227 .case_name = #name, \
228 .bpf_obj_file = "test_core_reloc_size.o", \
229 .btf_src_file = "btf__core_reloc_" #name ".o", \
230 .relaxed_core_relocs = true
232 #define SIZE_OUTPUT_DATA(type) \
233 STRUCT_TO_CHAR_PTR(core_reloc_size_output) { \
234 .int_sz = sizeof(((type *)0)->int_field), \
235 .struct_sz = sizeof(((type *)0)->struct_field), \
236 .union_sz = sizeof(((type *)0)->union_field), \
237 .arr_sz = sizeof(((type *)0)->arr_field), \
238 .arr_elem_sz = sizeof(((type *)0)->arr_field[0]), \
239 .ptr_sz = sizeof(((type *)0)->ptr_field), \
240 .enum_sz = sizeof(((type *)0)->enum_field), \
243 #define SIZE_CASE(name) { \
244 SIZE_CASE_COMMON(name), \
246 .output = SIZE_OUTPUT_DATA(struct core_reloc_##name), \
247 .output_len = sizeof(struct core_reloc_size_output), \
250 #define SIZE_ERR_CASE(name) { \
251 SIZE_CASE_COMMON(name), \
255 struct core_reloc_test_case
{
256 const char *case_name
;
257 const char *bpf_obj_file
;
258 const char *btf_src_file
;
264 bool relaxed_core_relocs
;
268 static struct core_reloc_test_case test_cases
[] = {
269 /* validate we can find kernel image and use its BTF for relocs */
271 .case_name
= "kernel",
272 .bpf_obj_file
= "test_core_reloc_kernel.o",
273 .btf_src_file
= NULL
, /* load from /lib/modules/$(uname -r) */
276 .output
= STRUCT_TO_CHAR_PTR(core_reloc_kernel_output
) {
277 .valid
= { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, },
278 .comm
= "test_progs",
279 .comm_len
= sizeof("test_progs"),
281 .output_len
= sizeof(struct core_reloc_kernel_output
),
284 /* validate BPF program can use multiple flavors to match against
285 * single target BTF type
287 FLAVORS_CASE(flavors
),
289 FLAVORS_ERR_CASE(flavors__err_wrong_name
),
291 /* various struct/enum nesting and resolution scenarios */
292 NESTING_CASE(nesting
),
293 NESTING_CASE(nesting___anon_embed
),
294 NESTING_CASE(nesting___struct_union_mixup
),
295 NESTING_CASE(nesting___extra_nesting
),
296 NESTING_CASE(nesting___dup_compat_types
),
298 NESTING_ERR_CASE(nesting___err_missing_field
),
299 NESTING_ERR_CASE(nesting___err_array_field
),
300 NESTING_ERR_CASE(nesting___err_missing_container
),
301 NESTING_ERR_CASE(nesting___err_nonstruct_container
),
302 NESTING_ERR_CASE(nesting___err_array_container
),
303 NESTING_ERR_CASE(nesting___err_dup_incompat_types
),
304 NESTING_ERR_CASE(nesting___err_partial_match_dups
),
305 NESTING_ERR_CASE(nesting___err_too_deep
),
307 /* various array access relocation scenarios */
309 ARRAYS_CASE(arrays___diff_arr_dim
),
310 ARRAYS_CASE(arrays___diff_arr_val_sz
),
312 ARRAYS_ERR_CASE(arrays___err_too_small
),
313 ARRAYS_ERR_CASE(arrays___err_too_shallow
),
314 ARRAYS_ERR_CASE(arrays___err_non_array
),
315 ARRAYS_ERR_CASE(arrays___err_wrong_val_type1
),
316 ARRAYS_ERR_CASE(arrays___err_wrong_val_type2
),
318 /* enum/ptr/int handling scenarios */
319 PRIMITIVES_CASE(primitives
),
320 PRIMITIVES_CASE(primitives___diff_enum_def
),
321 PRIMITIVES_CASE(primitives___diff_func_proto
),
322 PRIMITIVES_CASE(primitives___diff_ptr_type
),
324 PRIMITIVES_ERR_CASE(primitives___err_non_enum
),
325 PRIMITIVES_ERR_CASE(primitives___err_non_int
),
326 PRIMITIVES_ERR_CASE(primitives___err_non_ptr
),
328 /* const/volatile/restrict and typedefs scenarios */
330 MODS_CASE(mods___mod_swap
),
331 MODS_CASE(mods___typedefs
),
333 /* handling "ptr is an array" semantics */
334 PTR_AS_ARR_CASE(ptr_as_arr
),
335 PTR_AS_ARR_CASE(ptr_as_arr___diff_sz
),
337 /* int signedness/sizing/bitfield handling */
339 INTS_CASE(ints___bool
),
340 INTS_CASE(ints___reverse_sign
),
342 /* validate edge cases of capturing relocations */
345 .bpf_obj_file
= "test_core_reloc_misc.o",
346 .btf_src_file
= "btf__core_reloc_misc.o",
347 .input
= (const char *)&(struct core_reloc_misc_extensible
[]){
349 { .a
= 2 }, /* not read */
352 .input_len
= 4 * sizeof(int),
353 .output
= STRUCT_TO_CHAR_PTR(core_reloc_misc_output
) {
356 .c
= 0, /* BUG in clang, should be 3 */
358 .output_len
= sizeof(struct core_reloc_misc_output
),
361 /* validate field existence checks */
363 EXISTENCE_CASE_COMMON(existence
),
364 .input
= STRUCT_TO_CHAR_PTR(core_reloc_existence
) {
371 .input_len
= sizeof(struct core_reloc_existence
),
372 .output
= STRUCT_TO_CHAR_PTR(core_reloc_existence_output
) {
384 .output_len
= sizeof(struct core_reloc_existence_output
),
387 EXISTENCE_CASE_COMMON(existence___minimal
),
388 .input
= STRUCT_TO_CHAR_PTR(core_reloc_existence___minimal
) {
391 .input_len
= sizeof(struct core_reloc_existence
),
392 .output
= STRUCT_TO_CHAR_PTR(core_reloc_existence_output
) {
399 .b_value
= 0xff000002u
,
400 .c_value
= 0xff000003u
,
401 .arr_value
= 0xff000004u
,
402 .s_value
= 0xff000005u
,
404 .output_len
= sizeof(struct core_reloc_existence_output
),
407 EXISTENCE_ERR_CASE(existence__err_int_sz
),
408 EXISTENCE_ERR_CASE(existence__err_int_type
),
409 EXISTENCE_ERR_CASE(existence__err_int_kind
),
410 EXISTENCE_ERR_CASE(existence__err_arr_kind
),
411 EXISTENCE_ERR_CASE(existence__err_arr_value_type
),
412 EXISTENCE_ERR_CASE(existence__err_struct_type
),
414 /* bitfield relocation checks */
415 BITFIELDS_CASE(bitfields
, {
424 BITFIELDS_CASE(bitfields___bit_sz_change
, {
431 .s32
= -0x3FEDCBA987654321,
433 BITFIELDS_CASE(bitfields___bitfield_vs_int
, {
434 .ub1
= 0xFEDCBA9876543210,
436 .ub7
= -0x7EDCBA987654321,
437 .sb4
= -0x6123456789ABCDE,
440 .s32
= 0x0ADEADBEEFBADB0B,
442 BITFIELDS_CASE(bitfields___just_big_enough
, {
444 .ub2
= 0x0812345678FEDCBA,
446 BITFIELDS_ERR_CASE(bitfields___err_too_big_bitfield
),
448 /* size relocation checks */
450 SIZE_CASE(size___diff_sz
),
456 uint64_t my_pid_tgid
;
459 static size_t roundup_page(size_t sz
)
461 long page_size
= sysconf(_SC_PAGE_SIZE
);
462 return (sz
+ page_size
- 1) / page_size
* page_size
;
465 void test_core_reloc(void)
467 const size_t mmap_sz
= roundup_page(sizeof(struct data
));
468 struct bpf_object_load_attr load_attr
= {};
469 struct core_reloc_test_case
*test_case
;
470 const char *tp_name
, *probe_name
;
471 int err
, duration
= 0, i
, equal
;
472 struct bpf_link
*link
= NULL
;
473 struct bpf_map
*data_map
;
474 struct bpf_program
*prog
;
475 struct bpf_object
*obj
;
476 uint64_t my_pid_tgid
;
478 void *mmap_data
= NULL
;
480 my_pid_tgid
= getpid() | ((uint64_t)syscall(SYS_gettid
) << 32);
482 for (i
= 0; i
< ARRAY_SIZE(test_cases
); i
++) {
483 test_case
= &test_cases
[i
];
484 if (!test__start_subtest(test_case
->case_name
))
487 DECLARE_LIBBPF_OPTS(bpf_object_open_opts
, opts
,
488 .relaxed_core_relocs
= test_case
->relaxed_core_relocs
,
491 obj
= bpf_object__open_file(test_case
->bpf_obj_file
, &opts
);
492 if (CHECK(IS_ERR(obj
), "obj_open", "failed to open '%s': %ld\n",
493 test_case
->bpf_obj_file
, PTR_ERR(obj
)))
496 /* for typed raw tracepoints, NULL should be specified */
497 if (test_case
->direct_raw_tp
) {
498 probe_name
= "tp_btf/sys_enter";
501 probe_name
= "raw_tracepoint/sys_enter";
502 tp_name
= "sys_enter";
505 prog
= bpf_object__find_program_by_title(obj
, probe_name
);
506 if (CHECK(!prog
, "find_probe",
507 "prog '%s' not found\n", probe_name
))
511 load_attr
.log_level
= 0;
512 load_attr
.target_btf_path
= test_case
->btf_src_file
;
513 err
= bpf_object__load_xattr(&load_attr
);
514 if (test_case
->fails
) {
515 CHECK(!err
, "obj_load_fail",
516 "should fail to load prog '%s'\n", probe_name
);
519 if (CHECK(err
, "obj_load",
520 "failed to load prog '%s': %d\n",
525 data_map
= bpf_object__find_map_by_name(obj
, "test_cor.bss");
526 if (CHECK(!data_map
, "find_data_map", "data map not found\n"))
529 mmap_data
= mmap(NULL
, mmap_sz
, PROT_READ
| PROT_WRITE
,
530 MAP_SHARED
, bpf_map__fd(data_map
), 0);
531 if (CHECK(mmap_data
== MAP_FAILED
, "mmap",
532 ".bss mmap failed: %d", errno
)) {
538 memset(mmap_data
, 0, sizeof(*data
));
539 memcpy(data
->in
, test_case
->input
, test_case
->input_len
);
540 data
->my_pid_tgid
= my_pid_tgid
;
542 link
= bpf_program__attach_raw_tracepoint(prog
, tp_name
);
543 if (CHECK(IS_ERR(link
), "attach_raw_tp", "err %ld\n",
547 /* trigger test run */
550 equal
= memcmp(data
->out
, test_case
->output
,
551 test_case
->output_len
) == 0;
552 if (CHECK(!equal
, "check_result",
553 "input/output data don't match\n")) {
556 for (j
= 0; j
< test_case
->input_len
; j
++) {
557 printf("input byte #%d: 0x%02hhx\n",
558 j
, test_case
->input
[j
]);
560 for (j
= 0; j
< test_case
->output_len
; j
++) {
561 printf("output byte #%d: EXP 0x%02hhx GOT 0x%02hhx\n",
562 j
, test_case
->output
[j
], data
->out
[j
]);
569 CHECK_FAIL(munmap(mmap_data
, mmap_sz
));
572 if (!IS_ERR_OR_NULL(link
)) {
573 bpf_link__destroy(link
);
576 bpf_object__close(obj
);