2f92feb809be8c4a1f4392f7908ede5055a0aca1
[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 #include "bpf_testmod/bpf_testmod.h"
5 #include <linux/limits.h>
6 #include <sys/mman.h>
7 #include <sys/syscall.h>
8 #include <bpf/btf.h>
9
10 static int duration = 0;
11
12 #define STRUCT_TO_CHAR_PTR(struct_name) (const char *)&(struct struct_name)
13
14 #define MODULES_CASE(name, pg_name, tp_name) {                          \
15         .case_name = name,                                              \
16         .bpf_obj_file = "test_core_reloc_module.o",                     \
17         .btf_src_file = NULL, /* find in kernel module BTFs */          \
18         .input = "",                                                    \
19         .input_len = 0,                                                 \
20         .output = STRUCT_TO_CHAR_PTR(core_reloc_module_output) {        \
21                 .read_ctx_sz = sizeof(struct bpf_testmod_test_read_ctx),\
22                 .read_ctx_exists = true,                                \
23                 .buf_exists = true,                                     \
24                 .len_exists = true,                                     \
25                 .off_exists = true,                                     \
26                 .len = 123,                                             \
27                 .off = 0,                                               \
28                 .comm = "test_progs",                                   \
29                 .comm_len = sizeof("test_progs"),                       \
30         },                                                              \
31         .output_len = sizeof(struct core_reloc_module_output),          \
32         .prog_name = pg_name,                                           \
33         .raw_tp_name = tp_name,                                         \
34         .trigger = __trigger_module_test_read,                          \
35         .needs_testmod = true,                                          \
36 }
37
38 #define FLAVORS_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) {     \
39         .a = 42,                                                        \
40         .b = 0xc001,                                                    \
41         .c = 0xbeef,                                                    \
42 }
43
44 #define FLAVORS_CASE_COMMON(name)                                       \
45         .case_name = #name,                                             \
46         .bpf_obj_file = "test_core_reloc_flavors.o",                    \
47         .btf_src_file = "btf__core_reloc_" #name ".o",                  \
48         .raw_tp_name = "sys_enter",                                     \
49         .prog_name = "test_core_flavors"                                \
50
51 #define FLAVORS_CASE(name) {                                            \
52         FLAVORS_CASE_COMMON(name),                                      \
53         .input = FLAVORS_DATA(core_reloc_##name),                       \
54         .input_len = sizeof(struct core_reloc_##name),                  \
55         .output = FLAVORS_DATA(core_reloc_flavors),                     \
56         .output_len = sizeof(struct core_reloc_flavors),                \
57 }
58
59 #define FLAVORS_ERR_CASE(name) {                                        \
60         FLAVORS_CASE_COMMON(name),                                      \
61         .fails = true,                                                  \
62 }
63
64 #define NESTING_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) {     \
65         .a = { .a = { .a = 42 } },                                      \
66         .b = { .b = { .b = 0xc001 } },                                  \
67 }
68
69 #define NESTING_CASE_COMMON(name)                                       \
70         .case_name = #name,                                             \
71         .bpf_obj_file = "test_core_reloc_nesting.o",                    \
72         .btf_src_file = "btf__core_reloc_" #name ".o",                  \
73         .raw_tp_name = "sys_enter",                                     \
74         .prog_name = "test_core_nesting"                                \
75
76 #define NESTING_CASE(name) {                                            \
77         NESTING_CASE_COMMON(name),                                      \
78         .input = NESTING_DATA(core_reloc_##name),                       \
79         .input_len = sizeof(struct core_reloc_##name),                  \
80         .output = NESTING_DATA(core_reloc_nesting),                     \
81         .output_len = sizeof(struct core_reloc_nesting)                 \
82 }
83
84 #define NESTING_ERR_CASE(name) {                                        \
85         NESTING_CASE_COMMON(name),                                      \
86         .fails = true,                                                  \
87         .run_btfgen_fails = true,                                                       \
88 }
89
90 #define ARRAYS_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) {      \
91         .a = { [2] = 1 },                                               \
92         .b = { [1] = { [2] = { [3] = 2 } } },                           \
93         .c = { [1] = { .c =  3 } },                                     \
94         .d = { [0] = { [0] = { .d = 4 } } },                            \
95 }
96
97 #define ARRAYS_CASE_COMMON(name)                                        \
98         .case_name = #name,                                             \
99         .bpf_obj_file = "test_core_reloc_arrays.o",                     \
100         .btf_src_file = "btf__core_reloc_" #name ".o",                  \
101         .raw_tp_name = "sys_enter",                                     \
102         .prog_name = "test_core_arrays"                                 \
103
104 #define ARRAYS_CASE(name) {                                             \
105         ARRAYS_CASE_COMMON(name),                                       \
106         .input = ARRAYS_DATA(core_reloc_##name),                        \
107         .input_len = sizeof(struct core_reloc_##name),                  \
108         .output = STRUCT_TO_CHAR_PTR(core_reloc_arrays_output) {        \
109                 .a2   = 1,                                              \
110                 .b123 = 2,                                              \
111                 .c1c  = 3,                                              \
112                 .d00d = 4,                                              \
113                 .f10c = 0,                                              \
114         },                                                              \
115         .output_len = sizeof(struct core_reloc_arrays_output)           \
116 }
117
118 #define ARRAYS_ERR_CASE(name) {                                         \
119         ARRAYS_CASE_COMMON(name),                                       \
120         .fails = true,                                                  \
121 }
122
123 #define PRIMITIVES_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) {  \
124         .a = 1,                                                         \
125         .b = 2,                                                         \
126         .c = 3,                                                         \
127         .d = (void *)4,                                                 \
128         .f = (void *)5,                                                 \
129 }
130
131 #define PRIMITIVES_CASE_COMMON(name)                                    \
132         .case_name = #name,                                             \
133         .bpf_obj_file = "test_core_reloc_primitives.o",                 \
134         .btf_src_file = "btf__core_reloc_" #name ".o",                  \
135         .raw_tp_name = "sys_enter",                                     \
136         .prog_name = "test_core_primitives"                             \
137
138 #define PRIMITIVES_CASE(name) {                                         \
139         PRIMITIVES_CASE_COMMON(name),                                   \
140         .input = PRIMITIVES_DATA(core_reloc_##name),                    \
141         .input_len = sizeof(struct core_reloc_##name),                  \
142         .output = PRIMITIVES_DATA(core_reloc_primitives),               \
143         .output_len = sizeof(struct core_reloc_primitives),             \
144 }
145
146 #define PRIMITIVES_ERR_CASE(name) {                                     \
147         PRIMITIVES_CASE_COMMON(name),                                   \
148         .fails = true,                                                  \
149 }
150
151 #define MODS_CASE(name) {                                               \
152         .case_name = #name,                                             \
153         .bpf_obj_file = "test_core_reloc_mods.o",                       \
154         .btf_src_file = "btf__core_reloc_" #name ".o",                  \
155         .input = STRUCT_TO_CHAR_PTR(core_reloc_##name) {                \
156                 .a = 1,                                                 \
157                 .b = 2,                                                 \
158                 .c = (void *)3,                                         \
159                 .d = (void *)4,                                         \
160                 .e = { [2] = 5 },                                       \
161                 .f = { [1] = 6 },                                       \
162                 .g = { .x = 7 },                                        \
163                 .h = { .y = 8 },                                        \
164         },                                                              \
165         .input_len = sizeof(struct core_reloc_##name),                  \
166         .output = STRUCT_TO_CHAR_PTR(core_reloc_mods_output) {          \
167                 .a = 1, .b = 2, .c = 3, .d = 4,                         \
168                 .e = 5, .f = 6, .g = 7, .h = 8,                         \
169         },                                                              \
170         .output_len = sizeof(struct core_reloc_mods_output),            \
171         .raw_tp_name = "sys_enter",                                     \
172         .prog_name = "test_core_mods",                                  \
173 }
174
175 #define PTR_AS_ARR_CASE(name) {                                         \
176         .case_name = #name,                                             \
177         .bpf_obj_file = "test_core_reloc_ptr_as_arr.o",                 \
178         .btf_src_file = "btf__core_reloc_" #name ".o",                  \
179         .input = (const char *)&(struct core_reloc_##name []){          \
180                 { .a = 1 },                                             \
181                 { .a = 2 },                                             \
182                 { .a = 3 },                                             \
183         },                                                              \
184         .input_len = 3 * sizeof(struct core_reloc_##name),              \
185         .output = STRUCT_TO_CHAR_PTR(core_reloc_ptr_as_arr) {           \
186                 .a = 3,                                                 \
187         },                                                              \
188         .output_len = sizeof(struct core_reloc_ptr_as_arr),             \
189         .raw_tp_name = "sys_enter",                                     \
190         .prog_name = "test_core_ptr_as_arr",                            \
191 }
192
193 #define INTS_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) {        \
194         .u8_field = 1,                                                  \
195         .s8_field = 2,                                                  \
196         .u16_field = 3,                                                 \
197         .s16_field = 4,                                                 \
198         .u32_field = 5,                                                 \
199         .s32_field = 6,                                                 \
200         .u64_field = 7,                                                 \
201         .s64_field = 8,                                                 \
202 }
203
204 #define INTS_CASE_COMMON(name)                                          \
205         .case_name = #name,                                             \
206         .bpf_obj_file = "test_core_reloc_ints.o",                       \
207         .btf_src_file = "btf__core_reloc_" #name ".o",                  \
208         .raw_tp_name = "sys_enter",                                     \
209         .prog_name = "test_core_ints"
210
211 #define INTS_CASE(name) {                                               \
212         INTS_CASE_COMMON(name),                                         \
213         .input = INTS_DATA(core_reloc_##name),                          \
214         .input_len = sizeof(struct core_reloc_##name),                  \
215         .output = INTS_DATA(core_reloc_ints),                           \
216         .output_len = sizeof(struct core_reloc_ints),                   \
217 }
218
219 #define INTS_ERR_CASE(name) {                                           \
220         INTS_CASE_COMMON(name),                                         \
221         .fails = true,                                                  \
222 }
223
224 #define FIELD_EXISTS_CASE_COMMON(name)                                  \
225         .case_name = #name,                                             \
226         .bpf_obj_file = "test_core_reloc_existence.o",                  \
227         .btf_src_file = "btf__core_reloc_" #name ".o",                  \
228         .raw_tp_name = "sys_enter",                                     \
229         .prog_name = "test_core_existence"
230
231 #define BITFIELDS_CASE_COMMON(objfile, test_name_prefix,  name)         \
232         .case_name = test_name_prefix#name,                             \
233         .bpf_obj_file = objfile,                                        \
234         .btf_src_file = "btf__core_reloc_" #name ".o"
235
236 #define BITFIELDS_CASE(name, ...) {                                     \
237         BITFIELDS_CASE_COMMON("test_core_reloc_bitfields_probed.o",     \
238                               "probed:", name),                         \
239         .input = STRUCT_TO_CHAR_PTR(core_reloc_##name) __VA_ARGS__,     \
240         .input_len = sizeof(struct core_reloc_##name),                  \
241         .output = STRUCT_TO_CHAR_PTR(core_reloc_bitfields_output)       \
242                 __VA_ARGS__,                                            \
243         .output_len = sizeof(struct core_reloc_bitfields_output),       \
244         .raw_tp_name = "sys_enter",                                     \
245         .prog_name = "test_core_bitfields",                             \
246 }, {                                                                    \
247         BITFIELDS_CASE_COMMON("test_core_reloc_bitfields_direct.o",     \
248                               "direct:", name),                         \
249         .input = STRUCT_TO_CHAR_PTR(core_reloc_##name) __VA_ARGS__,     \
250         .input_len = sizeof(struct core_reloc_##name),                  \
251         .output = STRUCT_TO_CHAR_PTR(core_reloc_bitfields_output)       \
252                 __VA_ARGS__,                                            \
253         .output_len = sizeof(struct core_reloc_bitfields_output),       \
254         .prog_name = "test_core_bitfields_direct",                      \
255 }
256
257
258 #define BITFIELDS_ERR_CASE(name) {                                      \
259         BITFIELDS_CASE_COMMON("test_core_reloc_bitfields_probed.o",     \
260                               "probed:", name),                         \
261         .fails = true,                                                  \
262         .run_btfgen_fails = true,                                                       \
263         .raw_tp_name = "sys_enter",                                     \
264         .prog_name = "test_core_bitfields",                             \
265 }, {                                                                    \
266         BITFIELDS_CASE_COMMON("test_core_reloc_bitfields_direct.o",     \
267                               "direct:", name),                         \
268         .fails = true,                                                  \
269         .run_btfgen_fails = true,                                                       \
270         .prog_name = "test_core_bitfields_direct",                      \
271 }
272
273 #define SIZE_CASE_COMMON(name)                                          \
274         .case_name = #name,                                             \
275         .bpf_obj_file = "test_core_reloc_size.o",                       \
276         .btf_src_file = "btf__core_reloc_" #name ".o",                  \
277         .raw_tp_name = "sys_enter",                                     \
278         .prog_name = "test_core_size"
279
280 #define SIZE_OUTPUT_DATA(type)                                          \
281         STRUCT_TO_CHAR_PTR(core_reloc_size_output) {                    \
282                 .int_sz = sizeof(((type *)0)->int_field),               \
283                 .int_off = offsetof(type, int_field),                   \
284                 .struct_sz = sizeof(((type *)0)->struct_field),         \
285                 .struct_off = offsetof(type, struct_field),             \
286                 .union_sz = sizeof(((type *)0)->union_field),           \
287                 .union_off = offsetof(type, union_field),               \
288                 .arr_sz = sizeof(((type *)0)->arr_field),               \
289                 .arr_off = offsetof(type, arr_field),                   \
290                 .arr_elem_sz = sizeof(((type *)0)->arr_field[1]),       \
291                 .arr_elem_off = offsetof(type, arr_field[1]),           \
292                 .ptr_sz = 8, /* always 8-byte pointer for BPF */        \
293                 .ptr_off = offsetof(type, ptr_field),                   \
294                 .enum_sz = sizeof(((type *)0)->enum_field),             \
295                 .enum_off = offsetof(type, enum_field),                 \
296                 .float_sz = sizeof(((type *)0)->float_field),           \
297                 .float_off = offsetof(type, float_field),               \
298         }
299
300 #define SIZE_CASE(name) {                                               \
301         SIZE_CASE_COMMON(name),                                         \
302         .input_len = 0,                                                 \
303         .output = SIZE_OUTPUT_DATA(struct core_reloc_##name),           \
304         .output_len = sizeof(struct core_reloc_size_output),            \
305 }
306
307 #define SIZE_ERR_CASE(name) {                                           \
308         SIZE_CASE_COMMON(name),                                         \
309         .fails = true,                                                  \
310         .run_btfgen_fails = true,                                                       \
311 }
312
313 #define TYPE_BASED_CASE_COMMON(name)                                    \
314         .case_name = #name,                                             \
315         .bpf_obj_file = "test_core_reloc_type_based.o",                 \
316         .btf_src_file = "btf__core_reloc_" #name ".o",                  \
317         .raw_tp_name = "sys_enter",                                     \
318         .prog_name = "test_core_type_based"
319
320 #define TYPE_BASED_CASE(name, ...) {                                    \
321         TYPE_BASED_CASE_COMMON(name),                                   \
322         .output = STRUCT_TO_CHAR_PTR(core_reloc_type_based_output)      \
323                         __VA_ARGS__,                                    \
324         .output_len = sizeof(struct core_reloc_type_based_output),      \
325 }
326
327 #define TYPE_BASED_ERR_CASE(name) {                                     \
328         TYPE_BASED_CASE_COMMON(name),                                   \
329         .fails = true,                                                  \
330 }
331
332 #define TYPE_ID_CASE_COMMON(name)                                       \
333         .case_name = #name,                                             \
334         .bpf_obj_file = "test_core_reloc_type_id.o",                    \
335         .btf_src_file = "btf__core_reloc_" #name ".o",                  \
336         .raw_tp_name = "sys_enter",                                     \
337         .prog_name = "test_core_type_id"
338
339 #define TYPE_ID_CASE(name, setup_fn) {                                  \
340         TYPE_ID_CASE_COMMON(name),                                      \
341         .output = STRUCT_TO_CHAR_PTR(core_reloc_type_id_output) {},     \
342         .output_len = sizeof(struct core_reloc_type_id_output),         \
343         .setup = setup_fn,                                              \
344 }
345
346 #define TYPE_ID_ERR_CASE(name) {                                        \
347         TYPE_ID_CASE_COMMON(name),                                      \
348         .fails = true,                                                  \
349 }
350
351 #define ENUMVAL_CASE_COMMON(name)                                       \
352         .case_name = #name,                                             \
353         .bpf_obj_file = "test_core_reloc_enumval.o",                    \
354         .btf_src_file = "btf__core_reloc_" #name ".o",                  \
355         .raw_tp_name = "sys_enter",                                     \
356         .prog_name = "test_core_enumval"
357
358 #define ENUMVAL_CASE(name, ...) {                                       \
359         ENUMVAL_CASE_COMMON(name),                                      \
360         .output = STRUCT_TO_CHAR_PTR(core_reloc_enumval_output)         \
361                         __VA_ARGS__,                                    \
362         .output_len = sizeof(struct core_reloc_enumval_output),         \
363 }
364
365 #define ENUMVAL_ERR_CASE(name) {                                        \
366         ENUMVAL_CASE_COMMON(name),                                      \
367         .fails = true,                                                  \
368 }
369
370 #define ENUM64VAL_CASE_COMMON(name)                                     \
371         .case_name = #name,                                             \
372         .bpf_obj_file = "test_core_reloc_enum64val.o",                  \
373         .btf_src_file = "btf__core_reloc_" #name ".o",                  \
374         .raw_tp_name = "sys_enter",                                     \
375         .prog_name = "test_core_enum64val"
376
377 #define ENUM64VAL_CASE(name, ...) {                                     \
378         ENUM64VAL_CASE_COMMON(name),                                    \
379         .output = STRUCT_TO_CHAR_PTR(core_reloc_enum64val_output)       \
380                         __VA_ARGS__,                                    \
381         .output_len = sizeof(struct core_reloc_enum64val_output),       \
382 }
383
384 #define ENUM64VAL_ERR_CASE(name) {                                      \
385         ENUM64VAL_CASE_COMMON(name),                                    \
386         .fails = true,                                                  \
387 }
388
389 struct core_reloc_test_case;
390
391 typedef int (*setup_test_fn)(struct core_reloc_test_case *test);
392 typedef int (*trigger_test_fn)(const struct core_reloc_test_case *test);
393
394 struct core_reloc_test_case {
395         const char *case_name;
396         const char *bpf_obj_file;
397         const char *btf_src_file;
398         const char *input;
399         int input_len;
400         const char *output;
401         int output_len;
402         bool fails;
403         bool run_btfgen_fails;
404         bool needs_testmod;
405         bool relaxed_core_relocs;
406         const char *prog_name;
407         const char *raw_tp_name;
408         setup_test_fn setup;
409         trigger_test_fn trigger;
410 };
411
412 static int find_btf_type(const struct btf *btf, const char *name, __u32 kind)
413 {
414         int id;
415
416         id = btf__find_by_name_kind(btf, name, kind);
417         if (CHECK(id <= 0, "find_type_id", "failed to find '%s', kind %d: %d\n", name, kind, id))
418                 return -1;
419
420         return id;
421 }
422
423 static int setup_type_id_case_local(struct core_reloc_test_case *test)
424 {
425         struct core_reloc_type_id_output *exp = (void *)test->output;
426         struct btf *local_btf = btf__parse(test->bpf_obj_file, NULL);
427         struct btf *targ_btf = btf__parse(test->btf_src_file, NULL);
428         const struct btf_type *t;
429         const char *name;
430         int i;
431
432         if (!ASSERT_OK_PTR(local_btf, "local_btf") || !ASSERT_OK_PTR(targ_btf, "targ_btf")) {
433                 btf__free(local_btf);
434                 btf__free(targ_btf);
435                 return -EINVAL;
436         }
437
438         exp->local_anon_struct = -1;
439         exp->local_anon_union = -1;
440         exp->local_anon_enum = -1;
441         exp->local_anon_func_proto_ptr = -1;
442         exp->local_anon_void_ptr = -1;
443         exp->local_anon_arr = -1;
444
445         for (i = 1; i < btf__type_cnt(local_btf); i++)
446         {
447                 t = btf__type_by_id(local_btf, i);
448                 /* we are interested only in anonymous types */
449                 if (t->name_off)
450                         continue;
451
452                 if (btf_is_struct(t) && btf_vlen(t) &&
453                     (name = btf__name_by_offset(local_btf, btf_members(t)[0].name_off)) &&
454                     strcmp(name, "marker_field") == 0) {
455                         exp->local_anon_struct = i;
456                 } else if (btf_is_union(t) && btf_vlen(t) &&
457                          (name = btf__name_by_offset(local_btf, btf_members(t)[0].name_off)) &&
458                          strcmp(name, "marker_field") == 0) {
459                         exp->local_anon_union = i;
460                 } else if (btf_is_enum(t) && btf_vlen(t) &&
461                          (name = btf__name_by_offset(local_btf, btf_enum(t)[0].name_off)) &&
462                          strcmp(name, "MARKER_ENUM_VAL") == 0) {
463                         exp->local_anon_enum = i;
464                 } else if (btf_is_ptr(t) && (t = btf__type_by_id(local_btf, t->type))) {
465                         if (btf_is_func_proto(t) && (t = btf__type_by_id(local_btf, t->type)) &&
466                             btf_is_int(t) && (name = btf__name_by_offset(local_btf, t->name_off)) &&
467                             strcmp(name, "_Bool") == 0) {
468                                 /* ptr -> func_proto -> _Bool */
469                                 exp->local_anon_func_proto_ptr = i;
470                         } else if (btf_is_void(t)) {
471                                 /* ptr -> void */
472                                 exp->local_anon_void_ptr = i;
473                         }
474                 } else if (btf_is_array(t) && (t = btf__type_by_id(local_btf, btf_array(t)->type)) &&
475                            btf_is_int(t) && (name = btf__name_by_offset(local_btf, t->name_off)) &&
476                            strcmp(name, "_Bool") == 0) {
477                         /* _Bool[] */
478                         exp->local_anon_arr = i;
479                 }
480         }
481
482         exp->local_struct = find_btf_type(local_btf, "a_struct", BTF_KIND_STRUCT);
483         exp->local_union = find_btf_type(local_btf, "a_union", BTF_KIND_UNION);
484         exp->local_enum = find_btf_type(local_btf, "an_enum", BTF_KIND_ENUM);
485         exp->local_int = find_btf_type(local_btf, "int", BTF_KIND_INT);
486         exp->local_struct_typedef = find_btf_type(local_btf, "named_struct_typedef", BTF_KIND_TYPEDEF);
487         exp->local_func_proto_typedef = find_btf_type(local_btf, "func_proto_typedef", BTF_KIND_TYPEDEF);
488         exp->local_arr_typedef = find_btf_type(local_btf, "arr_typedef", BTF_KIND_TYPEDEF);
489
490         btf__free(local_btf);
491         btf__free(targ_btf);
492         return 0;
493 }
494
495 static int setup_type_id_case_success(struct core_reloc_test_case *test) {
496         struct core_reloc_type_id_output *exp = (void *)test->output;
497         struct btf *targ_btf;
498         int err;
499
500         err = setup_type_id_case_local(test);
501         if (err)
502                 return err;
503
504         targ_btf = btf__parse(test->btf_src_file, NULL);
505
506         exp->targ_struct = find_btf_type(targ_btf, "a_struct", BTF_KIND_STRUCT);
507         exp->targ_union = find_btf_type(targ_btf, "a_union", BTF_KIND_UNION);
508         exp->targ_enum = find_btf_type(targ_btf, "an_enum", BTF_KIND_ENUM);
509         exp->targ_int = find_btf_type(targ_btf, "int", BTF_KIND_INT);
510         exp->targ_struct_typedef = find_btf_type(targ_btf, "named_struct_typedef", BTF_KIND_TYPEDEF);
511         exp->targ_func_proto_typedef = find_btf_type(targ_btf, "func_proto_typedef", BTF_KIND_TYPEDEF);
512         exp->targ_arr_typedef = find_btf_type(targ_btf, "arr_typedef", BTF_KIND_TYPEDEF);
513
514         btf__free(targ_btf);
515         return 0;
516 }
517
518 static int setup_type_id_case_failure(struct core_reloc_test_case *test)
519 {
520         struct core_reloc_type_id_output *exp = (void *)test->output;
521         int err;
522
523         err = setup_type_id_case_local(test);
524         if (err)
525                 return err;
526
527         exp->targ_struct = 0;
528         exp->targ_union = 0;
529         exp->targ_enum = 0;
530         exp->targ_int = 0;
531         exp->targ_struct_typedef = 0;
532         exp->targ_func_proto_typedef = 0;
533         exp->targ_arr_typedef = 0;
534
535         return 0;
536 }
537
538 static int __trigger_module_test_read(const struct core_reloc_test_case *test)
539 {
540         struct core_reloc_module_output *exp = (void *)test->output;
541
542         trigger_module_test_read(exp->len);
543         return 0;
544 }
545
546
547 static const struct core_reloc_test_case test_cases[] = {
548         /* validate we can find kernel image and use its BTF for relocs */
549         {
550                 .case_name = "kernel",
551                 .bpf_obj_file = "test_core_reloc_kernel.o",
552                 .btf_src_file = NULL, /* load from /lib/modules/$(uname -r) */
553                 .input = "",
554                 .input_len = 0,
555                 .output = STRUCT_TO_CHAR_PTR(core_reloc_kernel_output) {
556                         .valid = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, },
557                         .comm = "test_progs",
558                         .comm_len = sizeof("test_progs"),
559                 },
560                 .output_len = sizeof(struct core_reloc_kernel_output),
561                 .raw_tp_name = "sys_enter",
562                 .prog_name = "test_core_kernel",
563         },
564
565         /* validate we can find kernel module BTF types for relocs/attach */
566         MODULES_CASE("module_probed", "test_core_module_probed", "bpf_testmod_test_read"),
567         MODULES_CASE("module_direct", "test_core_module_direct", NULL),
568
569         /* validate BPF program can use multiple flavors to match against
570          * single target BTF type
571          */
572         FLAVORS_CASE(flavors),
573
574         FLAVORS_ERR_CASE(flavors__err_wrong_name),
575
576         /* various struct/enum nesting and resolution scenarios */
577         NESTING_CASE(nesting),
578         NESTING_CASE(nesting___anon_embed),
579         NESTING_CASE(nesting___struct_union_mixup),
580         NESTING_CASE(nesting___extra_nesting),
581         NESTING_CASE(nesting___dup_compat_types),
582
583         NESTING_ERR_CASE(nesting___err_missing_field),
584         NESTING_ERR_CASE(nesting___err_array_field),
585         NESTING_ERR_CASE(nesting___err_missing_container),
586         NESTING_ERR_CASE(nesting___err_nonstruct_container),
587         NESTING_ERR_CASE(nesting___err_array_container),
588         NESTING_ERR_CASE(nesting___err_dup_incompat_types),
589         NESTING_ERR_CASE(nesting___err_partial_match_dups),
590         NESTING_ERR_CASE(nesting___err_too_deep),
591
592         /* various array access relocation scenarios */
593         ARRAYS_CASE(arrays),
594         ARRAYS_CASE(arrays___diff_arr_dim),
595         ARRAYS_CASE(arrays___diff_arr_val_sz),
596         ARRAYS_CASE(arrays___equiv_zero_sz_arr),
597         ARRAYS_CASE(arrays___fixed_arr),
598
599         ARRAYS_ERR_CASE(arrays___err_too_small),
600         ARRAYS_ERR_CASE(arrays___err_too_shallow),
601         ARRAYS_ERR_CASE(arrays___err_non_array),
602         ARRAYS_ERR_CASE(arrays___err_wrong_val_type),
603         ARRAYS_ERR_CASE(arrays___err_bad_zero_sz_arr),
604
605         /* enum/ptr/int handling scenarios */
606         PRIMITIVES_CASE(primitives),
607         PRIMITIVES_CASE(primitives___diff_enum_def),
608         PRIMITIVES_CASE(primitives___diff_func_proto),
609         PRIMITIVES_CASE(primitives___diff_ptr_type),
610
611         PRIMITIVES_ERR_CASE(primitives___err_non_enum),
612         PRIMITIVES_ERR_CASE(primitives___err_non_int),
613         PRIMITIVES_ERR_CASE(primitives___err_non_ptr),
614
615         /* const/volatile/restrict and typedefs scenarios */
616         MODS_CASE(mods),
617         MODS_CASE(mods___mod_swap),
618         MODS_CASE(mods___typedefs),
619
620         /* handling "ptr is an array" semantics */
621         PTR_AS_ARR_CASE(ptr_as_arr),
622         PTR_AS_ARR_CASE(ptr_as_arr___diff_sz),
623
624         /* int signedness/sizing/bitfield handling */
625         INTS_CASE(ints),
626         INTS_CASE(ints___bool),
627         INTS_CASE(ints___reverse_sign),
628
629         /* validate edge cases of capturing relocations */
630         {
631                 .case_name = "misc",
632                 .bpf_obj_file = "test_core_reloc_misc.o",
633                 .btf_src_file = "btf__core_reloc_misc.o",
634                 .input = (const char *)&(struct core_reloc_misc_extensible[]){
635                         { .a = 1 },
636                         { .a = 2 }, /* not read */
637                         { .a = 3 },
638                 },
639                 .input_len = 4 * sizeof(int),
640                 .output = STRUCT_TO_CHAR_PTR(core_reloc_misc_output) {
641                         .a = 1,
642                         .b = 1,
643                         .c = 0, /* BUG in clang, should be 3 */
644                 },
645                 .output_len = sizeof(struct core_reloc_misc_output),
646                 .raw_tp_name = "sys_enter",
647                 .prog_name = "test_core_misc",
648         },
649
650         /* validate field existence checks */
651         {
652                 FIELD_EXISTS_CASE_COMMON(existence),
653                 .input = STRUCT_TO_CHAR_PTR(core_reloc_existence) {
654                         .a = 1,
655                         .b = 2,
656                         .c = 3,
657                         .arr = { 4 },
658                         .s = { .x = 5 },
659                 },
660                 .input_len = sizeof(struct core_reloc_existence),
661                 .output = STRUCT_TO_CHAR_PTR(core_reloc_existence_output) {
662                         .a_exists = 1,
663                         .b_exists = 1,
664                         .c_exists = 1,
665                         .arr_exists = 1,
666                         .s_exists = 1,
667                         .a_value = 1,
668                         .b_value = 2,
669                         .c_value = 3,
670                         .arr_value = 4,
671                         .s_value = 5,
672                 },
673                 .output_len = sizeof(struct core_reloc_existence_output),
674         },
675         {
676                 FIELD_EXISTS_CASE_COMMON(existence___minimal),
677                 .input = STRUCT_TO_CHAR_PTR(core_reloc_existence___minimal) {
678                         .a = 42,
679                 },
680                 .input_len = sizeof(struct core_reloc_existence___minimal),
681                 .output = STRUCT_TO_CHAR_PTR(core_reloc_existence_output) {
682                         .a_exists = 1,
683                         .b_exists = 0,
684                         .c_exists = 0,
685                         .arr_exists = 0,
686                         .s_exists = 0,
687                         .a_value = 42,
688                         .b_value = 0xff000002u,
689                         .c_value = 0xff000003u,
690                         .arr_value = 0xff000004u,
691                         .s_value = 0xff000005u,
692                 },
693                 .output_len = sizeof(struct core_reloc_existence_output),
694         },
695         {
696                 FIELD_EXISTS_CASE_COMMON(existence___wrong_field_defs),
697                 .input = STRUCT_TO_CHAR_PTR(core_reloc_existence___wrong_field_defs) {
698                 },
699                 .input_len = sizeof(struct core_reloc_existence___wrong_field_defs),
700                 .output = STRUCT_TO_CHAR_PTR(core_reloc_existence_output) {
701                         .a_exists = 0,
702                         .b_exists = 0,
703                         .c_exists = 0,
704                         .arr_exists = 0,
705                         .s_exists = 0,
706                         .a_value = 0xff000001u,
707                         .b_value = 0xff000002u,
708                         .c_value = 0xff000003u,
709                         .arr_value = 0xff000004u,
710                         .s_value = 0xff000005u,
711                 },
712                 .output_len = sizeof(struct core_reloc_existence_output),
713         },
714
715         /* bitfield relocation checks */
716         BITFIELDS_CASE(bitfields, {
717                 .ub1 = 1,
718                 .ub2 = 2,
719                 .ub7 = 96,
720                 .sb4 = -7,
721                 .sb20 = -0x76543,
722                 .u32 = 0x80000000,
723                 .s32 = -0x76543210,
724         }),
725         BITFIELDS_CASE(bitfields___bit_sz_change, {
726                 .ub1 = 6,
727                 .ub2 = 0xABCDE,
728                 .ub7 = 1,
729                 .sb4 = -1,
730                 .sb20 = -0x17654321,
731                 .u32 = 0xBEEF,
732                 .s32 = -0x3FEDCBA987654321LL,
733         }),
734         BITFIELDS_CASE(bitfields___bitfield_vs_int, {
735                 .ub1 = 0xFEDCBA9876543210LL,
736                 .ub2 = 0xA6,
737                 .ub7 = -0x7EDCBA987654321LL,
738                 .sb4 = -0x6123456789ABCDELL,
739                 .sb20 = 0xD00DLL,
740                 .u32 = -0x76543,
741                 .s32 = 0x0ADEADBEEFBADB0BLL,
742         }),
743         BITFIELDS_CASE(bitfields___just_big_enough, {
744                 .ub1 = 0xFLL,
745                 .ub2 = 0x0812345678FEDCBALL,
746         }),
747         BITFIELDS_ERR_CASE(bitfields___err_too_big_bitfield),
748
749         /* field size and offset relocation checks */
750         SIZE_CASE(size),
751         SIZE_CASE(size___diff_sz),
752         SIZE_CASE(size___diff_offs),
753         SIZE_ERR_CASE(size___err_ambiguous),
754
755         /* validate type existence and size relocations */
756         TYPE_BASED_CASE(type_based, {
757                 .struct_exists = 1,
758                 .union_exists = 1,
759                 .enum_exists = 1,
760                 .typedef_named_struct_exists = 1,
761                 .typedef_anon_struct_exists = 1,
762                 .typedef_struct_ptr_exists = 1,
763                 .typedef_int_exists = 1,
764                 .typedef_enum_exists = 1,
765                 .typedef_void_ptr_exists = 1,
766                 .typedef_func_proto_exists = 1,
767                 .typedef_arr_exists = 1,
768                 .struct_sz = sizeof(struct a_struct),
769                 .union_sz = sizeof(union a_union),
770                 .enum_sz = sizeof(enum an_enum),
771                 .typedef_named_struct_sz = sizeof(named_struct_typedef),
772                 .typedef_anon_struct_sz = sizeof(anon_struct_typedef),
773                 .typedef_struct_ptr_sz = sizeof(struct_ptr_typedef),
774                 .typedef_int_sz = sizeof(int_typedef),
775                 .typedef_enum_sz = sizeof(enum_typedef),
776                 .typedef_void_ptr_sz = sizeof(void_ptr_typedef),
777                 .typedef_func_proto_sz = sizeof(func_proto_typedef),
778                 .typedef_arr_sz = sizeof(arr_typedef),
779         }),
780         TYPE_BASED_CASE(type_based___all_missing, {
781                 /* all zeros */
782         }),
783         TYPE_BASED_CASE(type_based___diff_sz, {
784                 .struct_exists = 1,
785                 .union_exists = 1,
786                 .enum_exists = 1,
787                 .typedef_named_struct_exists = 1,
788                 .typedef_anon_struct_exists = 1,
789                 .typedef_struct_ptr_exists = 1,
790                 .typedef_int_exists = 1,
791                 .typedef_enum_exists = 1,
792                 .typedef_void_ptr_exists = 1,
793                 .typedef_func_proto_exists = 1,
794                 .typedef_arr_exists = 1,
795                 .struct_sz = sizeof(struct a_struct___diff_sz),
796                 .union_sz = sizeof(union a_union___diff_sz),
797                 .enum_sz = sizeof(enum an_enum___diff_sz),
798                 .typedef_named_struct_sz = sizeof(named_struct_typedef___diff_sz),
799                 .typedef_anon_struct_sz = sizeof(anon_struct_typedef___diff_sz),
800                 .typedef_struct_ptr_sz = sizeof(struct_ptr_typedef___diff_sz),
801                 .typedef_int_sz = sizeof(int_typedef___diff_sz),
802                 .typedef_enum_sz = sizeof(enum_typedef___diff_sz),
803                 .typedef_void_ptr_sz = sizeof(void_ptr_typedef___diff_sz),
804                 .typedef_func_proto_sz = sizeof(func_proto_typedef___diff_sz),
805                 .typedef_arr_sz = sizeof(arr_typedef___diff_sz),
806         }),
807         TYPE_BASED_CASE(type_based___incompat, {
808                 .enum_exists = 1,
809                 .enum_sz = sizeof(enum an_enum),
810         }),
811         TYPE_BASED_CASE(type_based___fn_wrong_args, {
812                 .struct_exists = 1,
813                 .struct_sz = sizeof(struct a_struct),
814         }),
815
816         /* BTF_TYPE_ID_LOCAL/BTF_TYPE_ID_TARGET tests */
817         TYPE_ID_CASE(type_id, setup_type_id_case_success),
818         TYPE_ID_CASE(type_id___missing_targets, setup_type_id_case_failure),
819
820         /* Enumerator value existence and value relocations */
821         ENUMVAL_CASE(enumval, {
822                 .named_val1_exists = true,
823                 .named_val2_exists = true,
824                 .named_val3_exists = true,
825                 .anon_val1_exists = true,
826                 .anon_val2_exists = true,
827                 .anon_val3_exists = true,
828                 .named_val1 = 1,
829                 .named_val2 = 2,
830                 .anon_val1 = 0x10,
831                 .anon_val2 = 0x20,
832         }),
833         ENUMVAL_CASE(enumval___diff, {
834                 .named_val1_exists = true,
835                 .named_val2_exists = true,
836                 .named_val3_exists = true,
837                 .anon_val1_exists = true,
838                 .anon_val2_exists = true,
839                 .anon_val3_exists = true,
840                 .named_val1 = 101,
841                 .named_val2 = 202,
842                 .anon_val1 = 0x11,
843                 .anon_val2 = 0x22,
844         }),
845         ENUMVAL_CASE(enumval___val3_missing, {
846                 .named_val1_exists = true,
847                 .named_val2_exists = true,
848                 .named_val3_exists = false,
849                 .anon_val1_exists = true,
850                 .anon_val2_exists = true,
851                 .anon_val3_exists = false,
852                 .named_val1 = 111,
853                 .named_val2 = 222,
854                 .anon_val1 = 0x111,
855                 .anon_val2 = 0x222,
856         }),
857         ENUMVAL_ERR_CASE(enumval___err_missing),
858
859         /* 64bit enumerator value existence and value relocations */
860         ENUM64VAL_CASE(enum64val, {
861                 .unsigned_val1_exists = true,
862                 .unsigned_val2_exists = true,
863                 .unsigned_val3_exists = true,
864                 .signed_val1_exists = true,
865                 .signed_val2_exists = true,
866                 .signed_val3_exists = true,
867                 .unsigned_val1 = 0x1ffffffffULL,
868                 .unsigned_val2 = 0x2,
869                 .signed_val1 = 0x1ffffffffLL,
870                 .signed_val2 = -2,
871         }),
872         ENUM64VAL_CASE(enum64val___diff, {
873                 .unsigned_val1_exists = true,
874                 .unsigned_val2_exists = true,
875                 .unsigned_val3_exists = true,
876                 .signed_val1_exists = true,
877                 .signed_val2_exists = true,
878                 .signed_val3_exists = true,
879                 .unsigned_val1 = 0x101ffffffffULL,
880                 .unsigned_val2 = 0x202ffffffffULL,
881                 .signed_val1 = -101,
882                 .signed_val2 = -202,
883         }),
884         ENUM64VAL_CASE(enum64val___val3_missing, {
885                 .unsigned_val1_exists = true,
886                 .unsigned_val2_exists = true,
887                 .unsigned_val3_exists = false,
888                 .signed_val1_exists = true,
889                 .signed_val2_exists = true,
890                 .signed_val3_exists = false,
891                 .unsigned_val1 = 0x111ffffffffULL,
892                 .unsigned_val2 = 0x222,
893                 .signed_val1 = 0x111ffffffffLL,
894                 .signed_val2 = -222,
895         }),
896         ENUM64VAL_ERR_CASE(enum64val___err_missing),
897 };
898
899 struct data {
900         char in[256];
901         char out[256];
902         bool skip;
903         uint64_t my_pid_tgid;
904 };
905
906 static size_t roundup_page(size_t sz)
907 {
908         long page_size = sysconf(_SC_PAGE_SIZE);
909         return (sz + page_size - 1) / page_size * page_size;
910 }
911
912 static int run_btfgen(const char *src_btf, const char *dst_btf, const char *objpath)
913 {
914         char command[4096];
915         int n;
916
917         n = snprintf(command, sizeof(command),
918                      "./bpftool gen min_core_btf %s %s %s",
919                      src_btf, dst_btf, objpath);
920         if (n < 0 || n >= sizeof(command))
921                 return -1;
922
923         return system(command);
924 }
925
926 static void run_core_reloc_tests(bool use_btfgen)
927 {
928         const size_t mmap_sz = roundup_page(sizeof(struct data));
929         DECLARE_LIBBPF_OPTS(bpf_object_open_opts, open_opts);
930         struct core_reloc_test_case *test_case, test_case_copy;
931         const char *tp_name, *probe_name;
932         int err, i, equal, fd;
933         struct bpf_link *link = NULL;
934         struct bpf_map *data_map;
935         struct bpf_program *prog;
936         struct bpf_object *obj;
937         uint64_t my_pid_tgid;
938         struct data *data;
939         void *mmap_data = NULL;
940
941         my_pid_tgid = getpid() | ((uint64_t)syscall(SYS_gettid) << 32);
942
943         for (i = 0; i < ARRAY_SIZE(test_cases); i++) {
944                 char btf_file[] = "/tmp/core_reloc.btf.XXXXXX";
945
946                 test_case_copy = test_cases[i];
947                 test_case = &test_case_copy;
948
949                 if (!test__start_subtest(test_case->case_name))
950                         continue;
951
952                 if (test_case->needs_testmod && !env.has_testmod) {
953                         test__skip();
954                         continue;
955                 }
956
957                 /* generate a "minimal" BTF file and use it as source */
958                 if (use_btfgen) {
959
960                         if (!test_case->btf_src_file || test_case->run_btfgen_fails) {
961                                 test__skip();
962                                 continue;
963                         }
964
965                         fd = mkstemp(btf_file);
966                         if (!ASSERT_GE(fd, 0, "btf_tmp"))
967                                 continue;
968                         close(fd); /* we only need the path */
969                         err = run_btfgen(test_case->btf_src_file, btf_file,
970                                          test_case->bpf_obj_file);
971                         if (!ASSERT_OK(err, "run_btfgen"))
972                                 continue;
973
974                         test_case->btf_src_file = btf_file;
975                 }
976
977                 if (test_case->setup) {
978                         err = test_case->setup(test_case);
979                         if (CHECK(err, "test_setup", "test #%d setup failed: %d\n", i, err))
980                                 continue;
981                 }
982
983                 if (test_case->btf_src_file) {
984                         err = access(test_case->btf_src_file, R_OK);
985                         if (!ASSERT_OK(err, "btf_src_file"))
986                                 continue;
987                 }
988
989                 open_opts.btf_custom_path = test_case->btf_src_file;
990                 obj = bpf_object__open_file(test_case->bpf_obj_file, &open_opts);
991                 if (!ASSERT_OK_PTR(obj, "obj_open"))
992                         goto cleanup;
993
994                 probe_name = test_case->prog_name;
995                 tp_name = test_case->raw_tp_name; /* NULL for tp_btf */
996                 prog = bpf_object__find_program_by_name(obj, probe_name);
997                 if (CHECK(!prog, "find_probe",
998                           "prog '%s' not found\n", probe_name))
999                         goto cleanup;
1000
1001                 err = bpf_object__load(obj);
1002                 if (err) {
1003                         if (!test_case->fails)
1004                                 ASSERT_OK(err, "obj_load");
1005                         goto cleanup;
1006                 }
1007
1008                 data_map = bpf_object__find_map_by_name(obj, ".bss");
1009                 if (CHECK(!data_map, "find_data_map", "data map not found\n"))
1010                         goto cleanup;
1011
1012                 mmap_data = mmap(NULL, mmap_sz, PROT_READ | PROT_WRITE,
1013                                  MAP_SHARED, bpf_map__fd(data_map), 0);
1014                 if (CHECK(mmap_data == MAP_FAILED, "mmap",
1015                           ".bss mmap failed: %d", errno)) {
1016                         mmap_data = NULL;
1017                         goto cleanup;
1018                 }
1019                 data = mmap_data;
1020
1021                 memset(mmap_data, 0, sizeof(*data));
1022                 if (test_case->input_len)
1023                         memcpy(data->in, test_case->input, test_case->input_len);
1024                 data->my_pid_tgid = my_pid_tgid;
1025
1026                 link = bpf_program__attach_raw_tracepoint(prog, tp_name);
1027                 if (!ASSERT_OK_PTR(link, "attach_raw_tp"))
1028                         goto cleanup;
1029
1030                 /* trigger test run */
1031                 if (test_case->trigger) {
1032                         if (!ASSERT_OK(test_case->trigger(test_case), "test_trigger"))
1033                                 goto cleanup;
1034                 } else {
1035                         usleep(1);
1036                 }
1037
1038                 if (data->skip) {
1039                         test__skip();
1040                         goto cleanup;
1041                 }
1042
1043                 if (!ASSERT_FALSE(test_case->fails, "obj_load_should_fail"))
1044                         goto cleanup;
1045
1046                 equal = memcmp(data->out, test_case->output,
1047                                test_case->output_len) == 0;
1048                 if (CHECK(!equal, "check_result",
1049                           "input/output data don't match\n")) {
1050                         int j;
1051
1052                         for (j = 0; j < test_case->input_len; j++) {
1053                                 printf("input byte #%d: 0x%02hhx\n",
1054                                        j, test_case->input[j]);
1055                         }
1056                         for (j = 0; j < test_case->output_len; j++) {
1057                                 printf("output byte #%d: EXP 0x%02hhx GOT 0x%02hhx\n",
1058                                        j, test_case->output[j], data->out[j]);
1059                         }
1060                         goto cleanup;
1061                 }
1062
1063 cleanup:
1064                 if (mmap_data) {
1065                         CHECK_FAIL(munmap(mmap_data, mmap_sz));
1066                         mmap_data = NULL;
1067                 }
1068                 if (use_btfgen)
1069                         remove(test_case->btf_src_file);
1070                 bpf_link__destroy(link);
1071                 link = NULL;
1072                 bpf_object__close(obj);
1073         }
1074 }
1075
1076 void test_core_reloc(void)
1077 {
1078         run_core_reloc_tests(false);
1079 }
1080
1081 void test_core_reloc_btfgen(void)
1082 {
1083         run_core_reloc_tests(true);
1084 }