selftests/bpf: Add field existence CO-RE relocs tests
[linux-2.6-microblaze.git] / tools / testing / selftests / bpf / prog_tests / core_reloc.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include "progs/core_reloc_types.h"
4
5 #define STRUCT_TO_CHAR_PTR(struct_name) (const char *)&(struct struct_name)
6
7 #define FLAVORS_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) {     \
8         .a = 42,                                                        \
9         .b = 0xc001,                                                    \
10         .c = 0xbeef,                                                    \
11 }
12
13 #define FLAVORS_CASE_COMMON(name)                                       \
14         .case_name = #name,                                             \
15         .bpf_obj_file = "test_core_reloc_flavors.o",                    \
16         .btf_src_file = "btf__core_reloc_" #name ".o"                   \
17
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),                \
24 }
25
26 #define FLAVORS_ERR_CASE(name) {                                        \
27         FLAVORS_CASE_COMMON(name),                                      \
28         .fails = true,                                                  \
29 }
30
31 #define NESTING_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) {     \
32         .a = { .a = { .a = 42 } },                                      \
33         .b = { .b = { .b = 0xc001 } },                                  \
34 }
35
36 #define NESTING_CASE_COMMON(name)                                       \
37         .case_name = #name,                                             \
38         .bpf_obj_file = "test_core_reloc_nesting.o",                    \
39         .btf_src_file = "btf__core_reloc_" #name ".o"
40
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)                 \
47 }
48
49 #define NESTING_ERR_CASE(name) {                                        \
50         NESTING_CASE_COMMON(name),                                      \
51         .fails = true,                                                  \
52 }
53
54 #define ARRAYS_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) {      \
55         .a = { [2] = 1 },                                               \
56         .b = { [1] = { [2] = { [3] = 2 } } },                           \
57         .c = { [1] = { .c =  3 } },                                     \
58         .d = { [0] = { [0] = { .d = 4 } } },                            \
59 }
60
61 #define ARRAYS_CASE_COMMON(name)                                        \
62         .case_name = #name,                                             \
63         .bpf_obj_file = "test_core_reloc_arrays.o",                     \
64         .btf_src_file = "btf__core_reloc_" #name ".o"
65
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) {        \
71                 .a2   = 1,                                              \
72                 .b123 = 2,                                              \
73                 .c1c  = 3,                                              \
74                 .d00d = 4,                                              \
75         },                                                              \
76         .output_len = sizeof(struct core_reloc_arrays_output)           \
77 }
78
79 #define ARRAYS_ERR_CASE(name) {                                         \
80         ARRAYS_CASE_COMMON(name),                                       \
81         .fails = true,                                                  \
82 }
83
84 #define PRIMITIVES_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) {  \
85         .a = 1,                                                         \
86         .b = 2,                                                         \
87         .c = 3,                                                         \
88         .d = (void *)4,                                                 \
89         .f = (void *)5,                                                 \
90 }
91
92 #define PRIMITIVES_CASE_COMMON(name)                                    \
93         .case_name = #name,                                             \
94         .bpf_obj_file = "test_core_reloc_primitives.o",                 \
95         .btf_src_file = "btf__core_reloc_" #name ".o"
96
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),             \
103 }
104
105 #define PRIMITIVES_ERR_CASE(name) {                                     \
106         PRIMITIVES_CASE_COMMON(name),                                   \
107         .fails = true,                                                  \
108 }
109
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) {                \
115                 .a = 1,                                                 \
116                 .b = 2,                                                 \
117                 .c = (void *)3,                                         \
118                 .d = (void *)4,                                         \
119                 .e = { [2] = 5 },                                       \
120                 .f = { [1] = 6 },                                       \
121                 .g = { .x = 7 },                                        \
122                 .h = { .y = 8 },                                        \
123         },                                                              \
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,                         \
128         },                                                              \
129         .output_len = sizeof(struct core_reloc_mods_output),            \
130 }
131
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 []){          \
137                 { .a = 1 },                                             \
138                 { .a = 2 },                                             \
139                 { .a = 3 },                                             \
140         },                                                              \
141         .input_len = 3 * sizeof(struct core_reloc_##name),              \
142         .output = STRUCT_TO_CHAR_PTR(core_reloc_ptr_as_arr) {           \
143                 .a = 3,                                                 \
144         },                                                              \
145         .output_len = sizeof(struct core_reloc_ptr_as_arr),             \
146 }
147
148 #define INTS_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) {        \
149         .u8_field = 1,                                                  \
150         .s8_field = 2,                                                  \
151         .u16_field = 3,                                                 \
152         .s16_field = 4,                                                 \
153         .u32_field = 5,                                                 \
154         .s32_field = 6,                                                 \
155         .u64_field = 7,                                                 \
156         .s64_field = 8,                                                 \
157 }
158
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"
163
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),                   \
170 }
171
172 #define INTS_ERR_CASE(name) {                                           \
173         INTS_CASE_COMMON(name),                                         \
174         .fails = true,                                                  \
175 }
176
177 #define EXISTENCE_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) {   \
178         .a = 42,                                                        \
179 }
180
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                                     \
186
187 #define EXISTENCE_ERR_CASE(name) {                                      \
188         EXISTENCE_CASE_COMMON(name),                                    \
189         .fails = true,                                                  \
190 }
191
192 struct core_reloc_test_case {
193         const char *case_name;
194         const char *bpf_obj_file;
195         const char *btf_src_file;
196         const char *input;
197         int input_len;
198         const char *output;
199         int output_len;
200         bool fails;
201         bool relaxed_core_relocs;
202 };
203
204 static struct core_reloc_test_case test_cases[] = {
205         /* validate we can find kernel image and use its BTF for relocs */
206         {
207                 .case_name = "kernel",
208                 .bpf_obj_file = "test_core_reloc_kernel.o",
209                 .btf_src_file = NULL, /* load from /lib/modules/$(uname -r) */
210                 .input = "",
211                 .input_len = 0,
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",
215                         .comm_len = 11,
216                 },
217                 .output_len = sizeof(struct core_reloc_kernel_output),
218         },
219
220         /* validate BPF program can use multiple flavors to match against
221          * single target BTF type
222          */
223         FLAVORS_CASE(flavors),
224
225         FLAVORS_ERR_CASE(flavors__err_wrong_name),
226
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),
233
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),
242
243         /* various array access relocation scenarios */
244         ARRAYS_CASE(arrays),
245         ARRAYS_CASE(arrays___diff_arr_dim),
246         ARRAYS_CASE(arrays___diff_arr_val_sz),
247
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),
253
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),
259
260         PRIMITIVES_ERR_CASE(primitives___err_non_enum),
261         PRIMITIVES_ERR_CASE(primitives___err_non_int),
262         PRIMITIVES_ERR_CASE(primitives___err_non_ptr),
263
264         /* const/volatile/restrict and typedefs scenarios */
265         MODS_CASE(mods),
266         MODS_CASE(mods___mod_swap),
267         MODS_CASE(mods___typedefs),
268
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),
272
273         /* int signedness/sizing/bitfield handling */
274         INTS_CASE(ints),
275         INTS_CASE(ints___bool),
276         INTS_CASE(ints___reverse_sign),
277
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),
283         
284         /* validate edge cases of capturing relocations */
285         {
286                 .case_name = "misc",
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[]){
290                         { .a = 1 },
291                         { .a = 2 }, /* not read */
292                         { .a = 3 },
293                 },
294                 .input_len = 4 * sizeof(int),
295                 .output = STRUCT_TO_CHAR_PTR(core_reloc_misc_output) {
296                         .a = 1,
297                         .b = 1,
298                         .c = 0, /* BUG in clang, should be 3 */
299                 },
300                 .output_len = sizeof(struct core_reloc_misc_output),
301         },
302
303         /* validate field existence checks */
304         {
305                 EXISTENCE_CASE_COMMON(existence),
306                 .input = STRUCT_TO_CHAR_PTR(core_reloc_existence) {
307                         .a = 1,
308                         .b = 2,
309                         .c = 3,
310                         .arr = { 4 },
311                         .s = { .x = 5 },
312                 },
313                 .input_len = sizeof(struct core_reloc_existence),
314                 .output = STRUCT_TO_CHAR_PTR(core_reloc_existence_output) {
315                         .a_exists = 1,
316                         .b_exists = 1,
317                         .c_exists = 1,
318                         .arr_exists = 1,
319                         .s_exists = 1,
320                         .a_value = 1,
321                         .b_value = 2,
322                         .c_value = 3,
323                         .arr_value = 4,
324                         .s_value = 5,
325                 },
326                 .output_len = sizeof(struct core_reloc_existence_output),
327         },
328         {
329                 EXISTENCE_CASE_COMMON(existence___minimal),
330                 .input = STRUCT_TO_CHAR_PTR(core_reloc_existence___minimal) {
331                         .a = 42,
332                 },
333                 .input_len = sizeof(struct core_reloc_existence),
334                 .output = STRUCT_TO_CHAR_PTR(core_reloc_existence_output) {
335                         .a_exists = 1,
336                         .b_exists = 0,
337                         .c_exists = 0,
338                         .arr_exists = 0,
339                         .s_exists = 0,
340                         .a_value = 42,
341                         .b_value = 0xff000002u,
342                         .c_value = 0xff000003u,
343                         .arr_value = 0xff000004u,
344                         .s_value = 0xff000005u,
345                 },
346                 .output_len = sizeof(struct core_reloc_existence_output),
347         },
348
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),
355 };
356
357 struct data {
358         char in[256];
359         char out[256];
360 };
361
362 void test_core_reloc(void)
363 {
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;
372         const int zero = 0;
373         struct data data;
374
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))
378                         continue;
379
380                 LIBBPF_OPTS(bpf_object_open_opts, opts,
381                         .relaxed_core_relocs = test_case->relaxed_core_relocs,
382                 );
383
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)))
388                         continue;
389
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))
393                         goto cleanup;
394                 bpf_program__set_type(prog, BPF_PROG_TYPE_RAW_TRACEPOINT);
395
396                 load_attr.obj = obj;
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);
403                         goto cleanup;
404                 } else {
405                         if (CHECK(err, "obj_load",
406                                   "failed to load prog '%s': %d\n",
407                                   probe_name, err))
408                                 goto cleanup;
409                 }
410
411                 link = bpf_program__attach_raw_tracepoint(prog, "sys_enter");
412                 if (CHECK(IS_ERR(link), "attach_raw_tp", "err %ld\n",
413                           PTR_ERR(link)))
414                         goto cleanup;
415
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"))
418                         goto cleanup;
419
420                 memset(&data, 0, sizeof(data));
421                 memcpy(data.in, test_case->input, test_case->input_len);
422
423                 err = bpf_map_update_elem(bpf_map__fd(data_map),
424                                           &zero, &data, 0);
425                 if (CHECK(err, "update_data_map",
426                           "failed to update .data map: %d\n", err))
427                         goto cleanup;
428
429                 /* trigger test run */
430                 usleep(1);
431
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))
435                         goto cleanup;
436
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")) {
441                         int j;
442
443                         for (j = 0; j < test_case->input_len; j++) {
444                                 printf("input byte #%d: 0x%02hhx\n",
445                                        j, test_case->input[j]);
446                         }
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]);
450                         }
451                         goto cleanup;
452                 }
453
454 cleanup:
455                 if (!IS_ERR_OR_NULL(link)) {
456                         bpf_link__destroy(link);
457                         link = NULL;
458                 }
459                 bpf_object__close(obj);
460         }
461 }