1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2018 Facebook */
7 #include <linux/kernel.h>
8 #include <linux/filter.h>
9 #include <linux/unistd.h>
11 #include <sys/resource.h>
22 #include <bpf/libbpf.h>
25 #include "bpf_rlimit.h"
30 #define MAX_SUBPROGS 16
32 static uint32_t pass_cnt;
33 static uint32_t error_cnt;
34 static uint32_t skip_cnt;
36 #define CHECK(condition, format...) ({ \
37 int __ret = !!(condition); \
39 fprintf(stderr, "%s:%d:FAIL ", __func__, __LINE__); \
40 fprintf(stderr, format); \
45 static int count_result(int err)
52 fprintf(stderr, "\n");
56 static int __base_pr(enum libbpf_print_level level __attribute__((unused)),
57 const char *format, va_list args)
59 return vfprintf(stderr, format, args);
62 #define BTF_END_RAW 0xdeadbeef
63 #define NAME_TBD 0xdeadb33f
65 #define NAME_NTH(N) (0xffff0000 | N)
66 #define IS_NAME_NTH(X) ((X & 0xffff0000) == 0xffff0000)
67 #define GET_NAME_NTH_IDX(X) (X & 0x0000ffff)
69 #define MAX_NR_RAW_U32 1024
70 #define BTF_LOG_BUF_SIZE 65535
73 unsigned int raw_test_num;
74 unsigned int file_test_num;
75 unsigned int get_info_test_num;
76 unsigned int info_raw_test_num;
77 unsigned int dedup_test_num;
87 static char btf_log_buf[BTF_LOG_BUF_SIZE];
89 static struct btf_header hdr_tmpl = {
91 .version = BTF_VERSION,
92 .hdr_len = sizeof(struct btf_header),
95 /* several different mapv kinds(types) supported by pprint */
96 enum pprint_mapv_kind_t {
97 PPRINT_MAPV_KIND_BASIC = 0,
98 PPRINT_MAPV_KIND_INT128,
101 struct btf_raw_test {
104 const char *map_name;
106 __u32 raw_types[MAX_NR_RAW_U32];
108 enum bpf_map_type map_type;
123 enum pprint_mapv_kind_t mapv_kind;
126 #define BTF_STR_SEC(str) \
127 .str_sec = str, .str_sec_size = sizeof(str)
129 static struct btf_raw_test raw_tests[] = {
136 * unsigned long long m;
146 .descr = "struct test #1",
149 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
150 /* unsigned long long */
151 BTF_TYPE_INT_ENC(0, 0, 0, 64, 8), /* [2] */
153 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1), /* [3] */
155 BTF_TYPE_ARRAY_ENC(1, 1, 8), /* [4] */
156 /* struct A { */ /* [5] */
157 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 6), 180),
158 BTF_MEMBER_ENC(NAME_TBD, 2, 0), /* unsigned long long m;*/
159 BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n; */
160 BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o; */
161 BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8] */
162 BTF_MEMBER_ENC(NAME_TBD, 6, 384),/* int q[4][8] */
163 BTF_MEMBER_ENC(NAME_TBD, 7, 1408), /* enum E r */
166 BTF_TYPE_ARRAY_ENC(4, 1, 4), /* [6] */
167 /* enum E */ /* [7] */
168 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), sizeof(int)),
169 BTF_ENUM_ENC(NAME_TBD, 0),
170 BTF_ENUM_ENC(NAME_TBD, 1),
173 .str_sec = "\0A\0m\0n\0o\0p\0q\0r\0E\0E0\0E1",
174 .str_sec_size = sizeof("\0A\0m\0n\0o\0p\0q\0r\0E\0E0\0E1"),
175 .map_type = BPF_MAP_TYPE_ARRAY,
176 .map_name = "struct_test1_map",
177 .key_size = sizeof(int),
184 /* typedef struct b Struct_B;
189 * const Struct_B o[4];
198 .descr = "struct test #2",
201 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
202 /* struct b [4] */ /* [2] */
203 BTF_TYPE_ARRAY_ENC(4, 1, 4),
205 /* struct A { */ /* [3] */
206 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 3), 68),
207 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
208 BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* struct B n[4] */
209 BTF_MEMBER_ENC(NAME_TBD, 8, 288),/* const Struct_B o[4];*/
212 /* struct B { */ /* [4] */
213 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
214 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
215 BTF_MEMBER_ENC(NAME_TBD, 1, 32),/* int n; */
218 /* const int */ /* [5] */
219 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 1),
220 /* typedef struct b Struct_B */ /* [6] */
221 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), 4),
222 /* const Struct_B */ /* [7] */
223 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 6),
224 /* const Struct_B [4] */ /* [8] */
225 BTF_TYPE_ARRAY_ENC(7, 1, 4),
228 .str_sec = "\0A\0m\0n\0o\0B\0m\0n\0Struct_B",
229 .str_sec_size = sizeof("\0A\0m\0n\0o\0B\0m\0n\0Struct_B"),
230 .map_type = BPF_MAP_TYPE_ARRAY,
231 .map_name = "struct_test2_map",
232 .key_size = sizeof(int),
239 .descr = "struct test #3 Invalid member offset",
242 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
243 /* int64 */ /* [2] */
244 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 64, 8),
246 /* struct A { */ /* [3] */
247 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 16),
248 BTF_MEMBER_ENC(NAME_TBD, 1, 64), /* int m; */
249 BTF_MEMBER_ENC(NAME_TBD, 2, 0), /* int64 n; */
253 .str_sec = "\0A\0m\0n\0",
254 .str_sec_size = sizeof("\0A\0m\0n\0"),
255 .map_type = BPF_MAP_TYPE_ARRAY,
256 .map_name = "struct_test3_map",
257 .key_size = sizeof(int),
262 .btf_load_err = true,
263 .err_str = "Invalid member bits_offset",
267 * unsigned long long m;
275 .descr = "global data test #1",
278 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
279 /* unsigned long long */
280 BTF_TYPE_INT_ENC(0, 0, 0, 64, 8), /* [2] */
282 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1), /* [3] */
284 BTF_TYPE_ARRAY_ENC(1, 1, 8), /* [4] */
285 /* struct A { */ /* [5] */
286 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
287 BTF_MEMBER_ENC(NAME_TBD, 2, 0), /* unsigned long long m;*/
288 BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n; */
289 BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o; */
290 BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8] */
294 .str_sec = "\0A\0m\0n\0o\0p",
295 .str_sec_size = sizeof("\0A\0m\0n\0o\0p"),
296 .map_type = BPF_MAP_TYPE_ARRAY,
297 .map_name = "struct_test1_map",
298 .key_size = sizeof(int),
306 * unsigned long long m;
312 * static struct A t; <- in .bss
315 .descr = "global data test #2",
318 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
319 /* unsigned long long */
320 BTF_TYPE_INT_ENC(0, 0, 0, 64, 8), /* [2] */
322 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1), /* [3] */
324 BTF_TYPE_ARRAY_ENC(1, 1, 8), /* [4] */
325 /* struct A { */ /* [5] */
326 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
327 BTF_MEMBER_ENC(NAME_TBD, 2, 0), /* unsigned long long m;*/
328 BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n; */
329 BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o; */
330 BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8] */
332 /* static struct A t */
333 BTF_VAR_ENC(NAME_TBD, 5, 0), /* [6] */
334 /* .bss section */ /* [7] */
335 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 48),
336 BTF_VAR_SECINFO_ENC(6, 0, 48),
339 .str_sec = "\0A\0m\0n\0o\0p\0t\0.bss",
340 .str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0.bss"),
341 .map_type = BPF_MAP_TYPE_ARRAY,
343 .key_size = sizeof(int),
350 .descr = "global data test #3",
353 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
355 BTF_VAR_ENC(NAME_TBD, 1, 0), /* [2] */
356 /* .bss section */ /* [3] */
357 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
358 BTF_VAR_SECINFO_ENC(2, 0, 4),
361 .str_sec = "\0t\0.bss",
362 .str_sec_size = sizeof("\0t\0.bss"),
363 .map_type = BPF_MAP_TYPE_ARRAY,
365 .key_size = sizeof(int),
372 .descr = "global data test #4, unsupported linkage",
375 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
377 BTF_VAR_ENC(NAME_TBD, 1, 2), /* [2] */
378 /* .bss section */ /* [3] */
379 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
380 BTF_VAR_SECINFO_ENC(2, 0, 4),
383 .str_sec = "\0t\0.bss",
384 .str_sec_size = sizeof("\0t\0.bss"),
385 .map_type = BPF_MAP_TYPE_ARRAY,
387 .key_size = sizeof(int),
392 .btf_load_err = true,
393 .err_str = "Linkage not supported",
396 .descr = "global data test #5, invalid var type",
399 BTF_VAR_ENC(NAME_TBD, 0, 0), /* [1] */
400 /* .bss section */ /* [2] */
401 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
402 BTF_VAR_SECINFO_ENC(1, 0, 4),
405 .str_sec = "\0t\0.bss",
406 .str_sec_size = sizeof("\0t\0.bss"),
407 .map_type = BPF_MAP_TYPE_ARRAY,
409 .key_size = sizeof(int),
414 .btf_load_err = true,
415 .err_str = "Invalid type_id",
418 .descr = "global data test #6, invalid var type (fwd type)",
421 BTF_TYPE_ENC(NAME_TBD,
422 BTF_INFO_ENC(BTF_KIND_FWD, 1, 0), 0), /* [1] */
423 /* static union A t */
424 BTF_VAR_ENC(NAME_TBD, 1, 0), /* [2] */
425 /* .bss section */ /* [3] */
426 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
427 BTF_VAR_SECINFO_ENC(2, 0, 4),
430 .str_sec = "\0A\0t\0.bss",
431 .str_sec_size = sizeof("\0A\0t\0.bss"),
432 .map_type = BPF_MAP_TYPE_ARRAY,
434 .key_size = sizeof(int),
439 .btf_load_err = true,
440 .err_str = "Invalid type",
443 .descr = "global data test #7, invalid var type (fwd type)",
446 BTF_TYPE_ENC(NAME_TBD,
447 BTF_INFO_ENC(BTF_KIND_FWD, 1, 0), 0), /* [1] */
448 /* static union A t */
449 BTF_VAR_ENC(NAME_TBD, 1, 0), /* [2] */
450 /* .bss section */ /* [3] */
451 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
452 BTF_VAR_SECINFO_ENC(1, 0, 4),
455 .str_sec = "\0A\0t\0.bss",
456 .str_sec_size = sizeof("\0A\0t\0.bss"),
457 .map_type = BPF_MAP_TYPE_ARRAY,
459 .key_size = sizeof(int),
464 .btf_load_err = true,
465 .err_str = "Invalid type",
468 .descr = "global data test #8, invalid var size",
471 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
472 /* unsigned long long */
473 BTF_TYPE_INT_ENC(0, 0, 0, 64, 8), /* [2] */
475 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1), /* [3] */
477 BTF_TYPE_ARRAY_ENC(1, 1, 8), /* [4] */
478 /* struct A { */ /* [5] */
479 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
480 BTF_MEMBER_ENC(NAME_TBD, 2, 0), /* unsigned long long m;*/
481 BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n; */
482 BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o; */
483 BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8] */
485 /* static struct A t */
486 BTF_VAR_ENC(NAME_TBD, 5, 0), /* [6] */
487 /* .bss section */ /* [7] */
488 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 48),
489 BTF_VAR_SECINFO_ENC(6, 0, 47),
492 .str_sec = "\0A\0m\0n\0o\0p\0t\0.bss",
493 .str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0.bss"),
494 .map_type = BPF_MAP_TYPE_ARRAY,
496 .key_size = sizeof(int),
501 .btf_load_err = true,
502 .err_str = "Invalid size",
505 .descr = "global data test #9, invalid var size",
508 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
509 /* unsigned long long */
510 BTF_TYPE_INT_ENC(0, 0, 0, 64, 8), /* [2] */
512 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1), /* [3] */
514 BTF_TYPE_ARRAY_ENC(1, 1, 8), /* [4] */
515 /* struct A { */ /* [5] */
516 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
517 BTF_MEMBER_ENC(NAME_TBD, 2, 0), /* unsigned long long m;*/
518 BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n; */
519 BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o; */
520 BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8] */
522 /* static struct A t */
523 BTF_VAR_ENC(NAME_TBD, 5, 0), /* [6] */
524 /* .bss section */ /* [7] */
525 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 46),
526 BTF_VAR_SECINFO_ENC(6, 0, 48),
529 .str_sec = "\0A\0m\0n\0o\0p\0t\0.bss",
530 .str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0.bss"),
531 .map_type = BPF_MAP_TYPE_ARRAY,
533 .key_size = sizeof(int),
538 .btf_load_err = true,
539 .err_str = "Invalid size",
542 .descr = "global data test #10, invalid var size",
545 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
546 /* unsigned long long */
547 BTF_TYPE_INT_ENC(0, 0, 0, 64, 8), /* [2] */
549 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1), /* [3] */
551 BTF_TYPE_ARRAY_ENC(1, 1, 8), /* [4] */
552 /* struct A { */ /* [5] */
553 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
554 BTF_MEMBER_ENC(NAME_TBD, 2, 0), /* unsigned long long m;*/
555 BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n; */
556 BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o; */
557 BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8] */
559 /* static struct A t */
560 BTF_VAR_ENC(NAME_TBD, 5, 0), /* [6] */
561 /* .bss section */ /* [7] */
562 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 46),
563 BTF_VAR_SECINFO_ENC(6, 0, 46),
566 .str_sec = "\0A\0m\0n\0o\0p\0t\0.bss",
567 .str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0.bss"),
568 .map_type = BPF_MAP_TYPE_ARRAY,
570 .key_size = sizeof(int),
575 .btf_load_err = true,
576 .err_str = "Invalid size",
579 .descr = "global data test #11, multiple section members",
582 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
583 /* unsigned long long */
584 BTF_TYPE_INT_ENC(0, 0, 0, 64, 8), /* [2] */
586 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1), /* [3] */
588 BTF_TYPE_ARRAY_ENC(1, 1, 8), /* [4] */
589 /* struct A { */ /* [5] */
590 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
591 BTF_MEMBER_ENC(NAME_TBD, 2, 0), /* unsigned long long m;*/
592 BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n; */
593 BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o; */
594 BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8] */
596 /* static struct A t */
597 BTF_VAR_ENC(NAME_TBD, 5, 0), /* [6] */
599 BTF_VAR_ENC(NAME_TBD, 1, 0), /* [7] */
600 /* .bss section */ /* [8] */
601 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 2), 62),
602 BTF_VAR_SECINFO_ENC(6, 10, 48),
603 BTF_VAR_SECINFO_ENC(7, 58, 4),
606 .str_sec = "\0A\0m\0n\0o\0p\0t\0u\0.bss",
607 .str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0u\0.bss"),
608 .map_type = BPF_MAP_TYPE_ARRAY,
610 .key_size = sizeof(int),
617 .descr = "global data test #12, invalid offset",
620 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
621 /* unsigned long long */
622 BTF_TYPE_INT_ENC(0, 0, 0, 64, 8), /* [2] */
624 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1), /* [3] */
626 BTF_TYPE_ARRAY_ENC(1, 1, 8), /* [4] */
627 /* struct A { */ /* [5] */
628 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
629 BTF_MEMBER_ENC(NAME_TBD, 2, 0), /* unsigned long long m;*/
630 BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n; */
631 BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o; */
632 BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8] */
634 /* static struct A t */
635 BTF_VAR_ENC(NAME_TBD, 5, 0), /* [6] */
637 BTF_VAR_ENC(NAME_TBD, 1, 0), /* [7] */
638 /* .bss section */ /* [8] */
639 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 2), 62),
640 BTF_VAR_SECINFO_ENC(6, 10, 48),
641 BTF_VAR_SECINFO_ENC(7, 60, 4),
644 .str_sec = "\0A\0m\0n\0o\0p\0t\0u\0.bss",
645 .str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0u\0.bss"),
646 .map_type = BPF_MAP_TYPE_ARRAY,
648 .key_size = sizeof(int),
653 .btf_load_err = true,
654 .err_str = "Invalid offset+size",
657 .descr = "global data test #13, invalid offset",
660 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
661 /* unsigned long long */
662 BTF_TYPE_INT_ENC(0, 0, 0, 64, 8), /* [2] */
664 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1), /* [3] */
666 BTF_TYPE_ARRAY_ENC(1, 1, 8), /* [4] */
667 /* struct A { */ /* [5] */
668 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
669 BTF_MEMBER_ENC(NAME_TBD, 2, 0), /* unsigned long long m;*/
670 BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n; */
671 BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o; */
672 BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8] */
674 /* static struct A t */
675 BTF_VAR_ENC(NAME_TBD, 5, 0), /* [6] */
677 BTF_VAR_ENC(NAME_TBD, 1, 0), /* [7] */
678 /* .bss section */ /* [8] */
679 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 2), 62),
680 BTF_VAR_SECINFO_ENC(6, 10, 48),
681 BTF_VAR_SECINFO_ENC(7, 12, 4),
684 .str_sec = "\0A\0m\0n\0o\0p\0t\0u\0.bss",
685 .str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0u\0.bss"),
686 .map_type = BPF_MAP_TYPE_ARRAY,
688 .key_size = sizeof(int),
693 .btf_load_err = true,
694 .err_str = "Invalid offset",
697 .descr = "global data test #14, invalid offset",
700 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
701 /* unsigned long long */
702 BTF_TYPE_INT_ENC(0, 0, 0, 64, 8), /* [2] */
704 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1), /* [3] */
706 BTF_TYPE_ARRAY_ENC(1, 1, 8), /* [4] */
707 /* struct A { */ /* [5] */
708 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
709 BTF_MEMBER_ENC(NAME_TBD, 2, 0), /* unsigned long long m;*/
710 BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n; */
711 BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o; */
712 BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8] */
714 /* static struct A t */
715 BTF_VAR_ENC(NAME_TBD, 5, 0), /* [6] */
717 BTF_VAR_ENC(NAME_TBD, 1, 0), /* [7] */
718 /* .bss section */ /* [8] */
719 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 2), 62),
720 BTF_VAR_SECINFO_ENC(7, 58, 4),
721 BTF_VAR_SECINFO_ENC(6, 10, 48),
724 .str_sec = "\0A\0m\0n\0o\0p\0t\0u\0.bss",
725 .str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0u\0.bss"),
726 .map_type = BPF_MAP_TYPE_ARRAY,
728 .key_size = sizeof(int),
733 .btf_load_err = true,
734 .err_str = "Invalid offset",
737 .descr = "global data test #15, not var kind",
740 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
741 BTF_VAR_ENC(NAME_TBD, 1, 0), /* [2] */
742 /* .bss section */ /* [3] */
743 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
744 BTF_VAR_SECINFO_ENC(1, 0, 4),
747 .str_sec = "\0A\0t\0.bss",
748 .str_sec_size = sizeof("\0A\0t\0.bss"),
749 .map_type = BPF_MAP_TYPE_ARRAY,
751 .key_size = sizeof(int),
756 .btf_load_err = true,
757 .err_str = "Not a VAR kind member",
760 .descr = "global data test #16, invalid var referencing sec",
763 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
764 BTF_VAR_ENC(NAME_TBD, 5, 0), /* [2] */
765 BTF_VAR_ENC(NAME_TBD, 2, 0), /* [3] */
766 /* a section */ /* [4] */
767 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
768 BTF_VAR_SECINFO_ENC(3, 0, 4),
769 /* a section */ /* [5] */
770 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
771 BTF_VAR_SECINFO_ENC(6, 0, 4),
772 BTF_VAR_ENC(NAME_TBD, 1, 0), /* [6] */
775 .str_sec = "\0A\0t\0s\0a\0a",
776 .str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
777 .map_type = BPF_MAP_TYPE_ARRAY,
779 .key_size = sizeof(int),
784 .btf_load_err = true,
785 .err_str = "Invalid type_id",
788 .descr = "global data test #17, invalid var referencing var",
791 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
792 BTF_VAR_ENC(NAME_TBD, 1, 0), /* [2] */
793 BTF_VAR_ENC(NAME_TBD, 2, 0), /* [3] */
794 /* a section */ /* [4] */
795 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
796 BTF_VAR_SECINFO_ENC(3, 0, 4),
799 .str_sec = "\0A\0t\0s\0a\0a",
800 .str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
801 .map_type = BPF_MAP_TYPE_ARRAY,
803 .key_size = sizeof(int),
808 .btf_load_err = true,
809 .err_str = "Invalid type_id",
812 .descr = "global data test #18, invalid var loop",
815 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
816 BTF_VAR_ENC(NAME_TBD, 2, 0), /* [2] */
817 /* .bss section */ /* [3] */
818 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
819 BTF_VAR_SECINFO_ENC(2, 0, 4),
822 .str_sec = "\0A\0t\0aaa",
823 .str_sec_size = sizeof("\0A\0t\0aaa"),
824 .map_type = BPF_MAP_TYPE_ARRAY,
826 .key_size = sizeof(int),
831 .btf_load_err = true,
832 .err_str = "Invalid type_id",
835 .descr = "global data test #19, invalid var referencing var",
838 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
839 BTF_VAR_ENC(NAME_TBD, 3, 0), /* [2] */
840 BTF_VAR_ENC(NAME_TBD, 1, 0), /* [3] */
843 .str_sec = "\0A\0t\0s\0a\0a",
844 .str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
845 .map_type = BPF_MAP_TYPE_ARRAY,
847 .key_size = sizeof(int),
852 .btf_load_err = true,
853 .err_str = "Invalid type_id",
856 .descr = "global data test #20, invalid ptr referencing var",
859 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
860 /* PTR type_id=3 */ /* [2] */
861 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 3),
862 BTF_VAR_ENC(NAME_TBD, 1, 0), /* [3] */
865 .str_sec = "\0A\0t\0s\0a\0a",
866 .str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
867 .map_type = BPF_MAP_TYPE_ARRAY,
869 .key_size = sizeof(int),
874 .btf_load_err = true,
875 .err_str = "Invalid type_id",
878 .descr = "global data test #21, var included in struct",
881 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
882 /* struct A { */ /* [2] */
883 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 2),
884 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
885 BTF_MEMBER_ENC(NAME_TBD, 3, 32),/* VAR type_id=3; */
887 BTF_VAR_ENC(NAME_TBD, 1, 0), /* [3] */
890 .str_sec = "\0A\0t\0s\0a\0a",
891 .str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
892 .map_type = BPF_MAP_TYPE_ARRAY,
894 .key_size = sizeof(int),
899 .btf_load_err = true,
900 .err_str = "Invalid member",
903 .descr = "global data test #22, array of var",
906 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
907 BTF_TYPE_ARRAY_ENC(3, 1, 4), /* [2] */
908 BTF_VAR_ENC(NAME_TBD, 1, 0), /* [3] */
911 .str_sec = "\0A\0t\0s\0a\0a",
912 .str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
913 .map_type = BPF_MAP_TYPE_ARRAY,
915 .key_size = sizeof(int),
920 .btf_load_err = true,
921 .err_str = "Invalid elem",
923 /* Test member exceeds the size of struct.
931 .descr = "size check test #1",
934 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
935 /* struct A { */ /* [2] */
936 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 2 - 1),
937 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
938 BTF_MEMBER_ENC(NAME_TBD, 1, 32),/* int n; */
942 .str_sec = "\0A\0m\0n",
943 .str_sec_size = sizeof("\0A\0m\0n"),
944 .map_type = BPF_MAP_TYPE_ARRAY,
945 .map_name = "size_check1_map",
946 .key_size = sizeof(int),
951 .btf_load_err = true,
952 .err_str = "Member exceeds struct_size",
955 /* Test member exeeds the size of struct
963 .descr = "size check test #2",
966 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
967 /* int[2] */ /* [2] */
968 BTF_TYPE_ARRAY_ENC(1, 1, 2),
969 /* struct A { */ /* [3] */
970 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 3 - 1),
971 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
972 BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* int n[2]; */
976 .str_sec = "\0A\0m\0n",
977 .str_sec_size = sizeof("\0A\0m\0n"),
978 .map_type = BPF_MAP_TYPE_ARRAY,
979 .map_name = "size_check2_map",
980 .key_size = sizeof(int),
985 .btf_load_err = true,
986 .err_str = "Member exceeds struct_size",
989 /* Test member exeeds the size of struct
997 .descr = "size check test #3",
1000 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
1001 /* void* */ /* [2] */
1002 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
1003 /* struct A { */ /* [3] */
1004 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) + sizeof(void *) - 1),
1005 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
1006 BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* void *n; */
1010 .str_sec = "\0A\0m\0n",
1011 .str_sec_size = sizeof("\0A\0m\0n"),
1012 .map_type = BPF_MAP_TYPE_ARRAY,
1013 .map_name = "size_check3_map",
1014 .key_size = sizeof(int),
1019 .btf_load_err = true,
1020 .err_str = "Member exceeds struct_size",
1023 /* Test member exceeds the size of struct
1036 .descr = "size check test #4",
1039 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
1040 /* enum E { */ /* [2] */
1041 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), sizeof(int)),
1042 BTF_ENUM_ENC(NAME_TBD, 0),
1043 BTF_ENUM_ENC(NAME_TBD, 1),
1045 /* struct A { */ /* [3] */
1046 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 2 - 1),
1047 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
1048 BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* enum E n; */
1052 .str_sec = "\0E\0E0\0E1\0A\0m\0n",
1053 .str_sec_size = sizeof("\0E\0E0\0E1\0A\0m\0n"),
1054 .map_type = BPF_MAP_TYPE_ARRAY,
1055 .map_name = "size_check4_map",
1056 .key_size = sizeof(int),
1061 .btf_load_err = true,
1062 .err_str = "Member exceeds struct_size",
1065 /* typedef const void * const_void_ptr;
1071 .descr = "void test #1",
1074 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1075 /* const void */ /* [2] */
1076 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1077 /* const void* */ /* [3] */
1078 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 2),
1079 /* typedef const void * const_void_ptr */
1080 BTF_TYPEDEF_ENC(NAME_TBD, 3), /* [4] */
1081 /* struct A { */ /* [5] */
1082 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
1083 /* const_void_ptr m; */
1084 BTF_MEMBER_ENC(NAME_TBD, 4, 0),
1088 .str_sec = "\0const_void_ptr\0A\0m",
1089 .str_sec_size = sizeof("\0const_void_ptr\0A\0m"),
1090 .map_type = BPF_MAP_TYPE_ARRAY,
1091 .map_name = "void_test1_map",
1092 .key_size = sizeof(int),
1093 .value_size = sizeof(void *),
1104 .descr = "void test #2",
1107 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1108 /* const void */ /* [2] */
1109 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1110 /* struct A { */ /* [3] */
1111 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 8),
1113 BTF_MEMBER_ENC(NAME_TBD, 2, 0),
1117 .str_sec = "\0A\0m",
1118 .str_sec_size = sizeof("\0A\0m"),
1119 .map_type = BPF_MAP_TYPE_ARRAY,
1120 .map_name = "void_test2_map",
1121 .key_size = sizeof(int),
1122 .value_size = sizeof(void *),
1126 .btf_load_err = true,
1127 .err_str = "Invalid member",
1130 /* typedef const void * const_void_ptr;
1134 .descr = "void test #3",
1137 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1138 /* const void */ /* [2] */
1139 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1140 /* const void* */ /* [3] */
1141 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 2),
1142 /* typedef const void * const_void_ptr */
1143 BTF_TYPEDEF_ENC(NAME_TBD, 3), /* [4] */
1144 /* const_void_ptr[4] */
1145 BTF_TYPE_ARRAY_ENC(4, 1, 4), /* [5] */
1148 .str_sec = "\0const_void_ptr",
1149 .str_sec_size = sizeof("\0const_void_ptr"),
1150 .map_type = BPF_MAP_TYPE_ARRAY,
1151 .map_name = "void_test3_map",
1152 .key_size = sizeof(int),
1153 .value_size = sizeof(void *) * 4,
1161 .descr = "void test #4",
1164 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1165 /* const void */ /* [2] */
1166 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1167 /* const void[4] */ /* [3] */
1168 BTF_TYPE_ARRAY_ENC(2, 1, 4),
1171 .str_sec = "\0A\0m",
1172 .str_sec_size = sizeof("\0A\0m"),
1173 .map_type = BPF_MAP_TYPE_ARRAY,
1174 .map_name = "void_test4_map",
1175 .key_size = sizeof(int),
1176 .value_size = sizeof(void *) * 4,
1180 .btf_load_err = true,
1181 .err_str = "Invalid elem",
1184 /* Array_A <------------------+
1185 * elem_type == Array_B |
1188 * Array_B <-------- + |
1189 * elem_type == Array A --+
1192 .descr = "loop test #1",
1195 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1196 /* Array_A */ /* [2] */
1197 BTF_TYPE_ARRAY_ENC(3, 1, 8),
1198 /* Array_B */ /* [3] */
1199 BTF_TYPE_ARRAY_ENC(2, 1, 8),
1203 .str_sec_size = sizeof(""),
1204 .map_type = BPF_MAP_TYPE_ARRAY,
1205 .map_name = "loop_test1_map",
1206 .key_size = sizeof(int),
1207 .value_size = sizeof(sizeof(int) * 8),
1211 .btf_load_err = true,
1212 .err_str = "Loop detected",
1215 /* typedef is _before_ the BTF type of Array_A and Array_B
1217 * typedef Array_B int_array;
1219 * Array_A <------------------+
1220 * elem_type == int_array |
1223 * Array_B <-------- + |
1224 * elem_type == Array_A --+
1227 .descr = "loop test #2",
1230 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
1231 /* typedef Array_B int_array */
1232 BTF_TYPEDEF_ENC(1, 4), /* [2] */
1234 BTF_TYPE_ARRAY_ENC(2, 1, 8), /* [3] */
1236 BTF_TYPE_ARRAY_ENC(3, 1, 8), /* [4] */
1239 .str_sec = "\0int_array\0",
1240 .str_sec_size = sizeof("\0int_array"),
1241 .map_type = BPF_MAP_TYPE_ARRAY,
1242 .map_name = "loop_test2_map",
1243 .key_size = sizeof(int),
1244 .value_size = sizeof(sizeof(int) * 8),
1248 .btf_load_err = true,
1249 .err_str = "Loop detected",
1252 /* Array_A <------------------+
1253 * elem_type == Array_B |
1256 * Array_B <-------- + |
1257 * elem_type == Array_A --+
1260 .descr = "loop test #3",
1263 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1264 /* Array_A */ /* [2] */
1265 BTF_TYPE_ARRAY_ENC(3, 1, 8),
1266 /* Array_B */ /* [3] */
1267 BTF_TYPE_ARRAY_ENC(2, 1, 8),
1271 .str_sec_size = sizeof(""),
1272 .map_type = BPF_MAP_TYPE_ARRAY,
1273 .map_name = "loop_test3_map",
1274 .key_size = sizeof(int),
1275 .value_size = sizeof(sizeof(int) * 8),
1279 .btf_load_err = true,
1280 .err_str = "Loop detected",
1283 /* typedef is _between_ the BTF type of Array_A and Array_B
1285 * typedef Array_B int_array;
1287 * Array_A <------------------+
1288 * elem_type == int_array |
1291 * Array_B <-------- + |
1292 * elem_type == Array_A --+
1295 .descr = "loop test #4",
1298 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1299 /* Array_A */ /* [2] */
1300 BTF_TYPE_ARRAY_ENC(3, 1, 8),
1301 /* typedef Array_B int_array */ /* [3] */
1302 BTF_TYPEDEF_ENC(NAME_TBD, 4),
1303 /* Array_B */ /* [4] */
1304 BTF_TYPE_ARRAY_ENC(2, 1, 8),
1307 .str_sec = "\0int_array\0",
1308 .str_sec_size = sizeof("\0int_array"),
1309 .map_type = BPF_MAP_TYPE_ARRAY,
1310 .map_name = "loop_test4_map",
1311 .key_size = sizeof(int),
1312 .value_size = sizeof(sizeof(int) * 8),
1316 .btf_load_err = true,
1317 .err_str = "Loop detected",
1320 /* typedef struct B Struct_B
1333 .descr = "loop test #5",
1336 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
1337 /* struct A */ /* [2] */
1338 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
1339 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int x; */
1340 BTF_MEMBER_ENC(NAME_TBD, 3, 32),/* Struct_B y; */
1341 /* typedef struct B Struct_B */
1342 BTF_TYPEDEF_ENC(NAME_TBD, 4), /* [3] */
1343 /* struct B */ /* [4] */
1344 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
1345 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int x; */
1346 BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* struct A y; */
1349 .str_sec = "\0A\0x\0y\0Struct_B\0B\0x\0y",
1350 .str_sec_size = sizeof("\0A\0x\0y\0Struct_B\0B\0x\0y"),
1351 .map_type = BPF_MAP_TYPE_ARRAY,
1352 .map_name = "loop_test5_map",
1353 .key_size = sizeof(int),
1358 .btf_load_err = true,
1359 .err_str = "Loop detected",
1364 * struct A array_a[4];
1368 .descr = "loop test #6",
1371 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
1372 BTF_TYPE_ARRAY_ENC(3, 1, 4), /* [2] */
1373 /* struct A */ /* [3] */
1374 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
1375 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int x; */
1376 BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* struct A array_a[4]; */
1379 .str_sec = "\0A\0x\0y",
1380 .str_sec_size = sizeof("\0A\0x\0y"),
1381 .map_type = BPF_MAP_TYPE_ARRAY,
1382 .map_name = "loop_test6_map",
1383 .key_size = sizeof(int),
1388 .btf_load_err = true,
1389 .err_str = "Loop detected",
1393 .descr = "loop test #7",
1396 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1397 /* struct A { */ /* [2] */
1398 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
1399 /* const void *m; */
1400 BTF_MEMBER_ENC(NAME_TBD, 3, 0),
1401 /* CONST type_id=3 */ /* [3] */
1402 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 4),
1403 /* PTR type_id=2 */ /* [4] */
1404 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 3),
1407 .str_sec = "\0A\0m",
1408 .str_sec_size = sizeof("\0A\0m"),
1409 .map_type = BPF_MAP_TYPE_ARRAY,
1410 .map_name = "loop_test7_map",
1411 .key_size = sizeof(int),
1412 .value_size = sizeof(void *),
1416 .btf_load_err = true,
1417 .err_str = "Loop detected",
1421 .descr = "loop test #8",
1424 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1425 /* struct A { */ /* [2] */
1426 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
1427 /* const void *m; */
1428 BTF_MEMBER_ENC(NAME_TBD, 4, 0),
1429 /* struct B { */ /* [3] */
1430 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
1431 /* const void *n; */
1432 BTF_MEMBER_ENC(NAME_TBD, 6, 0),
1433 /* CONST type_id=5 */ /* [4] */
1434 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 5),
1435 /* PTR type_id=6 */ /* [5] */
1436 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 6),
1437 /* CONST type_id=7 */ /* [6] */
1438 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 7),
1439 /* PTR type_id=4 */ /* [7] */
1440 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 4),
1443 .str_sec = "\0A\0m\0B\0n",
1444 .str_sec_size = sizeof("\0A\0m\0B\0n"),
1445 .map_type = BPF_MAP_TYPE_ARRAY,
1446 .map_name = "loop_test8_map",
1447 .key_size = sizeof(int),
1448 .value_size = sizeof(void *),
1452 .btf_load_err = true,
1453 .err_str = "Loop detected",
1457 .descr = "string section does not end with null",
1460 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1464 .str_sec_size = sizeof("\0int") - 1,
1465 .map_type = BPF_MAP_TYPE_ARRAY,
1466 .map_name = "hdr_test_map",
1467 .key_size = sizeof(int),
1468 .value_size = sizeof(int),
1472 .btf_load_err = true,
1473 .err_str = "Invalid string section",
1477 .descr = "empty string section",
1480 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1485 .map_type = BPF_MAP_TYPE_ARRAY,
1486 .map_name = "hdr_test_map",
1487 .key_size = sizeof(int),
1488 .value_size = sizeof(int),
1492 .btf_load_err = true,
1493 .err_str = "Invalid string section",
1497 .descr = "empty type section",
1502 .str_sec_size = sizeof("\0int"),
1503 .map_type = BPF_MAP_TYPE_ARRAY,
1504 .map_name = "hdr_test_map",
1505 .key_size = sizeof(int),
1506 .value_size = sizeof(int),
1510 .btf_load_err = true,
1511 .err_str = "No type found",
1515 .descr = "btf_header test. Longer hdr_len",
1518 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1522 .str_sec_size = sizeof("\0int"),
1523 .map_type = BPF_MAP_TYPE_ARRAY,
1524 .map_name = "hdr_test_map",
1525 .key_size = sizeof(int),
1526 .value_size = sizeof(int),
1530 .btf_load_err = true,
1532 .err_str = "Unsupported btf_header",
1536 .descr = "btf_header test. Gap between hdr and type",
1539 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1543 .str_sec_size = sizeof("\0int"),
1544 .map_type = BPF_MAP_TYPE_ARRAY,
1545 .map_name = "hdr_test_map",
1546 .key_size = sizeof(int),
1547 .value_size = sizeof(int),
1551 .btf_load_err = true,
1552 .type_off_delta = 4,
1553 .err_str = "Unsupported section found",
1557 .descr = "btf_header test. Gap between type and str",
1560 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1564 .str_sec_size = sizeof("\0int"),
1565 .map_type = BPF_MAP_TYPE_ARRAY,
1566 .map_name = "hdr_test_map",
1567 .key_size = sizeof(int),
1568 .value_size = sizeof(int),
1572 .btf_load_err = true,
1574 .err_str = "Unsupported section found",
1578 .descr = "btf_header test. Overlap between type and str",
1581 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1585 .str_sec_size = sizeof("\0int"),
1586 .map_type = BPF_MAP_TYPE_ARRAY,
1587 .map_name = "hdr_test_map",
1588 .key_size = sizeof(int),
1589 .value_size = sizeof(int),
1593 .btf_load_err = true,
1594 .str_off_delta = -4,
1595 .err_str = "Section overlap found",
1599 .descr = "btf_header test. Larger BTF size",
1602 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1606 .str_sec_size = sizeof("\0int"),
1607 .map_type = BPF_MAP_TYPE_ARRAY,
1608 .map_name = "hdr_test_map",
1609 .key_size = sizeof(int),
1610 .value_size = sizeof(int),
1614 .btf_load_err = true,
1615 .str_len_delta = -4,
1616 .err_str = "Unsupported section found",
1620 .descr = "btf_header test. Smaller BTF size",
1623 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1627 .str_sec_size = sizeof("\0int"),
1628 .map_type = BPF_MAP_TYPE_ARRAY,
1629 .map_name = "hdr_test_map",
1630 .key_size = sizeof(int),
1631 .value_size = sizeof(int),
1635 .btf_load_err = true,
1637 .err_str = "Total section length too long",
1641 .descr = "array test. index_type/elem_type \"int\"",
1644 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1645 /* int[16] */ /* [2] */
1646 BTF_TYPE_ARRAY_ENC(1, 1, 16),
1650 .str_sec_size = sizeof(""),
1651 .map_type = BPF_MAP_TYPE_ARRAY,
1652 .map_name = "array_test_map",
1653 .key_size = sizeof(int),
1654 .value_size = sizeof(int),
1661 .descr = "array test. index_type/elem_type \"const int\"",
1664 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1665 /* int[16] */ /* [2] */
1666 BTF_TYPE_ARRAY_ENC(3, 3, 16),
1667 /* CONST type_id=1 */ /* [3] */
1668 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 1),
1672 .str_sec_size = sizeof(""),
1673 .map_type = BPF_MAP_TYPE_ARRAY,
1674 .map_name = "array_test_map",
1675 .key_size = sizeof(int),
1676 .value_size = sizeof(int),
1683 .descr = "array test. index_type \"const int:31\"",
1686 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1687 /* int:31 */ /* [2] */
1688 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 31, 4),
1689 /* int[16] */ /* [3] */
1690 BTF_TYPE_ARRAY_ENC(1, 4, 16),
1691 /* CONST type_id=2 */ /* [4] */
1692 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 2),
1696 .str_sec_size = sizeof(""),
1697 .map_type = BPF_MAP_TYPE_ARRAY,
1698 .map_name = "array_test_map",
1699 .key_size = sizeof(int),
1700 .value_size = sizeof(int),
1704 .btf_load_err = true,
1705 .err_str = "Invalid index",
1709 .descr = "array test. elem_type \"const int:31\"",
1712 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1713 /* int:31 */ /* [2] */
1714 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 31, 4),
1715 /* int[16] */ /* [3] */
1716 BTF_TYPE_ARRAY_ENC(4, 1, 16),
1717 /* CONST type_id=2 */ /* [4] */
1718 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 2),
1722 .str_sec_size = sizeof(""),
1723 .map_type = BPF_MAP_TYPE_ARRAY,
1724 .map_name = "array_test_map",
1725 .key_size = sizeof(int),
1726 .value_size = sizeof(int),
1730 .btf_load_err = true,
1731 .err_str = "Invalid array of int",
1735 .descr = "array test. index_type \"void\"",
1738 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1739 /* int[16] */ /* [2] */
1740 BTF_TYPE_ARRAY_ENC(1, 0, 16),
1744 .str_sec_size = sizeof(""),
1745 .map_type = BPF_MAP_TYPE_ARRAY,
1746 .map_name = "array_test_map",
1747 .key_size = sizeof(int),
1748 .value_size = sizeof(int),
1752 .btf_load_err = true,
1753 .err_str = "Invalid index",
1757 .descr = "array test. index_type \"const void\"",
1760 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1761 /* int[16] */ /* [2] */
1762 BTF_TYPE_ARRAY_ENC(1, 3, 16),
1763 /* CONST type_id=0 (void) */ /* [3] */
1764 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1768 .str_sec_size = sizeof(""),
1769 .map_type = BPF_MAP_TYPE_ARRAY,
1770 .map_name = "array_test_map",
1771 .key_size = sizeof(int),
1772 .value_size = sizeof(int),
1776 .btf_load_err = true,
1777 .err_str = "Invalid index",
1781 .descr = "array test. elem_type \"const void\"",
1784 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1785 /* int[16] */ /* [2] */
1786 BTF_TYPE_ARRAY_ENC(3, 1, 16),
1787 /* CONST type_id=0 (void) */ /* [3] */
1788 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1792 .str_sec_size = sizeof(""),
1793 .map_type = BPF_MAP_TYPE_ARRAY,
1794 .map_name = "array_test_map",
1795 .key_size = sizeof(int),
1796 .value_size = sizeof(int),
1800 .btf_load_err = true,
1801 .err_str = "Invalid elem",
1805 .descr = "array test. elem_type \"const void *\"",
1808 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1809 /* const void *[16] */ /* [2] */
1810 BTF_TYPE_ARRAY_ENC(3, 1, 16),
1811 /* CONST type_id=4 */ /* [3] */
1812 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 4),
1813 /* void* */ /* [4] */
1814 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
1818 .str_sec_size = sizeof(""),
1819 .map_type = BPF_MAP_TYPE_ARRAY,
1820 .map_name = "array_test_map",
1821 .key_size = sizeof(int),
1822 .value_size = sizeof(int),
1829 .descr = "array test. index_type \"const void *\"",
1832 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1833 /* const void *[16] */ /* [2] */
1834 BTF_TYPE_ARRAY_ENC(3, 3, 16),
1835 /* CONST type_id=4 */ /* [3] */
1836 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 4),
1837 /* void* */ /* [4] */
1838 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
1842 .str_sec_size = sizeof(""),
1843 .map_type = BPF_MAP_TYPE_ARRAY,
1844 .map_name = "array_test_map",
1845 .key_size = sizeof(int),
1846 .value_size = sizeof(int),
1850 .btf_load_err = true,
1851 .err_str = "Invalid index",
1855 .descr = "array test. t->size != 0\"",
1858 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1859 /* int[16] */ /* [2] */
1860 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ARRAY, 0, 0), 1),
1861 BTF_ARRAY_ENC(1, 1, 16),
1865 .str_sec_size = sizeof(""),
1866 .map_type = BPF_MAP_TYPE_ARRAY,
1867 .map_name = "array_test_map",
1868 .key_size = sizeof(int),
1869 .value_size = sizeof(int),
1873 .btf_load_err = true,
1874 .err_str = "size != 0",
1878 .descr = "int test. invalid int_data",
1880 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), 4),
1885 .str_sec_size = sizeof(""),
1886 .map_type = BPF_MAP_TYPE_ARRAY,
1887 .map_name = "array_test_map",
1888 .key_size = sizeof(int),
1889 .value_size = sizeof(int),
1893 .btf_load_err = true,
1894 .err_str = "Invalid int_data",
1898 .descr = "invalid BTF_INFO",
1901 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1902 BTF_TYPE_ENC(0, 0x10000000, 4),
1906 .str_sec_size = sizeof(""),
1907 .map_type = BPF_MAP_TYPE_ARRAY,
1908 .map_name = "array_test_map",
1909 .key_size = sizeof(int),
1910 .value_size = sizeof(int),
1914 .btf_load_err = true,
1915 .err_str = "Invalid btf_info",
1919 .descr = "fwd test. t->type != 0\"",
1922 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1923 /* fwd type */ /* [2] */
1924 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FWD, 0, 0), 1),
1928 .str_sec_size = sizeof(""),
1929 .map_type = BPF_MAP_TYPE_ARRAY,
1930 .map_name = "fwd_test_map",
1931 .key_size = sizeof(int),
1932 .value_size = sizeof(int),
1936 .btf_load_err = true,
1937 .err_str = "type != 0",
1941 .descr = "typedef (invalid name, name_off = 0)",
1943 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
1944 BTF_TYPEDEF_ENC(0, 1), /* [2] */
1947 .str_sec = "\0__int",
1948 .str_sec_size = sizeof("\0__int"),
1949 .map_type = BPF_MAP_TYPE_ARRAY,
1950 .map_name = "typedef_check_btf",
1951 .key_size = sizeof(int),
1952 .value_size = sizeof(int),
1956 .btf_load_err = true,
1957 .err_str = "Invalid name",
1961 .descr = "typedef (invalid name, invalid identifier)",
1963 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
1964 BTF_TYPEDEF_ENC(NAME_TBD, 1), /* [2] */
1967 .str_sec = "\0__!int",
1968 .str_sec_size = sizeof("\0__!int"),
1969 .map_type = BPF_MAP_TYPE_ARRAY,
1970 .map_name = "typedef_check_btf",
1971 .key_size = sizeof(int),
1972 .value_size = sizeof(int),
1976 .btf_load_err = true,
1977 .err_str = "Invalid name",
1981 .descr = "ptr type (invalid name, name_off <> 0)",
1983 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
1984 BTF_TYPE_ENC(NAME_TBD,
1985 BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 1), /* [2] */
1988 .str_sec = "\0__int",
1989 .str_sec_size = sizeof("\0__int"),
1990 .map_type = BPF_MAP_TYPE_ARRAY,
1991 .map_name = "ptr_type_check_btf",
1992 .key_size = sizeof(int),
1993 .value_size = sizeof(int),
1997 .btf_load_err = true,
1998 .err_str = "Invalid name",
2002 .descr = "volatile type (invalid name, name_off <> 0)",
2004 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2005 BTF_TYPE_ENC(NAME_TBD,
2006 BTF_INFO_ENC(BTF_KIND_VOLATILE, 0, 0), 1), /* [2] */
2009 .str_sec = "\0__int",
2010 .str_sec_size = sizeof("\0__int"),
2011 .map_type = BPF_MAP_TYPE_ARRAY,
2012 .map_name = "volatile_type_check_btf",
2013 .key_size = sizeof(int),
2014 .value_size = sizeof(int),
2018 .btf_load_err = true,
2019 .err_str = "Invalid name",
2023 .descr = "const type (invalid name, name_off <> 0)",
2025 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2026 BTF_TYPE_ENC(NAME_TBD,
2027 BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 1), /* [2] */
2030 .str_sec = "\0__int",
2031 .str_sec_size = sizeof("\0__int"),
2032 .map_type = BPF_MAP_TYPE_ARRAY,
2033 .map_name = "const_type_check_btf",
2034 .key_size = sizeof(int),
2035 .value_size = sizeof(int),
2039 .btf_load_err = true,
2040 .err_str = "Invalid name",
2044 .descr = "restrict type (invalid name, name_off <> 0)",
2046 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2047 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 1), /* [2] */
2048 BTF_TYPE_ENC(NAME_TBD,
2049 BTF_INFO_ENC(BTF_KIND_RESTRICT, 0, 0), 2), /* [3] */
2052 .str_sec = "\0__int",
2053 .str_sec_size = sizeof("\0__int"),
2054 .map_type = BPF_MAP_TYPE_ARRAY,
2055 .map_name = "restrict_type_check_btf",
2056 .key_size = sizeof(int),
2057 .value_size = sizeof(int),
2061 .btf_load_err = true,
2062 .err_str = "Invalid name",
2066 .descr = "fwd type (invalid name, name_off = 0)",
2068 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2069 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FWD, 0, 0), 0), /* [2] */
2072 .str_sec = "\0__skb",
2073 .str_sec_size = sizeof("\0__skb"),
2074 .map_type = BPF_MAP_TYPE_ARRAY,
2075 .map_name = "fwd_type_check_btf",
2076 .key_size = sizeof(int),
2077 .value_size = sizeof(int),
2081 .btf_load_err = true,
2082 .err_str = "Invalid name",
2086 .descr = "fwd type (invalid name, invalid identifier)",
2088 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2089 BTF_TYPE_ENC(NAME_TBD,
2090 BTF_INFO_ENC(BTF_KIND_FWD, 0, 0), 0), /* [2] */
2093 .str_sec = "\0__!skb",
2094 .str_sec_size = sizeof("\0__!skb"),
2095 .map_type = BPF_MAP_TYPE_ARRAY,
2096 .map_name = "fwd_type_check_btf",
2097 .key_size = sizeof(int),
2098 .value_size = sizeof(int),
2102 .btf_load_err = true,
2103 .err_str = "Invalid name",
2107 .descr = "array type (invalid name, name_off <> 0)",
2109 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2110 BTF_TYPE_ENC(NAME_TBD,
2111 BTF_INFO_ENC(BTF_KIND_ARRAY, 0, 0), 0), /* [2] */
2112 BTF_ARRAY_ENC(1, 1, 4),
2115 .str_sec = "\0__skb",
2116 .str_sec_size = sizeof("\0__skb"),
2117 .map_type = BPF_MAP_TYPE_ARRAY,
2118 .map_name = "array_type_check_btf",
2119 .key_size = sizeof(int),
2120 .value_size = sizeof(int),
2124 .btf_load_err = true,
2125 .err_str = "Invalid name",
2129 .descr = "struct type (name_off = 0)",
2131 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2133 BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4), /* [2] */
2134 BTF_MEMBER_ENC(NAME_TBD, 1, 0),
2138 .str_sec_size = sizeof("\0A"),
2139 .map_type = BPF_MAP_TYPE_ARRAY,
2140 .map_name = "struct_type_check_btf",
2141 .key_size = sizeof(int),
2142 .value_size = sizeof(int),
2149 .descr = "struct type (invalid name, invalid identifier)",
2151 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2152 BTF_TYPE_ENC(NAME_TBD,
2153 BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4), /* [2] */
2154 BTF_MEMBER_ENC(NAME_TBD, 1, 0),
2157 .str_sec = "\0A!\0B",
2158 .str_sec_size = sizeof("\0A!\0B"),
2159 .map_type = BPF_MAP_TYPE_ARRAY,
2160 .map_name = "struct_type_check_btf",
2161 .key_size = sizeof(int),
2162 .value_size = sizeof(int),
2166 .btf_load_err = true,
2167 .err_str = "Invalid name",
2171 .descr = "struct member (name_off = 0)",
2173 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2175 BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4), /* [2] */
2176 BTF_MEMBER_ENC(NAME_TBD, 1, 0),
2180 .str_sec_size = sizeof("\0A"),
2181 .map_type = BPF_MAP_TYPE_ARRAY,
2182 .map_name = "struct_type_check_btf",
2183 .key_size = sizeof(int),
2184 .value_size = sizeof(int),
2191 .descr = "struct member (invalid name, invalid identifier)",
2193 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2194 BTF_TYPE_ENC(NAME_TBD,
2195 BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4), /* [2] */
2196 BTF_MEMBER_ENC(NAME_TBD, 1, 0),
2199 .str_sec = "\0A\0B*",
2200 .str_sec_size = sizeof("\0A\0B*"),
2201 .map_type = BPF_MAP_TYPE_ARRAY,
2202 .map_name = "struct_type_check_btf",
2203 .key_size = sizeof(int),
2204 .value_size = sizeof(int),
2208 .btf_load_err = true,
2209 .err_str = "Invalid name",
2213 .descr = "enum type (name_off = 0)",
2215 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2217 BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1),
2218 sizeof(int)), /* [2] */
2219 BTF_ENUM_ENC(NAME_TBD, 0),
2222 .str_sec = "\0A\0B",
2223 .str_sec_size = sizeof("\0A\0B"),
2224 .map_type = BPF_MAP_TYPE_ARRAY,
2225 .map_name = "enum_type_check_btf",
2226 .key_size = sizeof(int),
2227 .value_size = sizeof(int),
2234 .descr = "enum type (invalid name, invalid identifier)",
2236 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2237 BTF_TYPE_ENC(NAME_TBD,
2238 BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1),
2239 sizeof(int)), /* [2] */
2240 BTF_ENUM_ENC(NAME_TBD, 0),
2243 .str_sec = "\0A!\0B",
2244 .str_sec_size = sizeof("\0A!\0B"),
2245 .map_type = BPF_MAP_TYPE_ARRAY,
2246 .map_name = "enum_type_check_btf",
2247 .key_size = sizeof(int),
2248 .value_size = sizeof(int),
2252 .btf_load_err = true,
2253 .err_str = "Invalid name",
2257 .descr = "enum member (invalid name, name_off = 0)",
2259 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2261 BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1),
2262 sizeof(int)), /* [2] */
2267 .str_sec_size = sizeof(""),
2268 .map_type = BPF_MAP_TYPE_ARRAY,
2269 .map_name = "enum_type_check_btf",
2270 .key_size = sizeof(int),
2271 .value_size = sizeof(int),
2275 .btf_load_err = true,
2276 .err_str = "Invalid name",
2280 .descr = "enum member (invalid name, invalid identifier)",
2282 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2284 BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1),
2285 sizeof(int)), /* [2] */
2286 BTF_ENUM_ENC(NAME_TBD, 0),
2290 .str_sec_size = sizeof("\0A!"),
2291 .map_type = BPF_MAP_TYPE_ARRAY,
2292 .map_name = "enum_type_check_btf",
2293 .key_size = sizeof(int),
2294 .value_size = sizeof(int),
2298 .btf_load_err = true,
2299 .err_str = "Invalid name",
2302 .descr = "arraymap invalid btf key (a bit field)",
2305 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
2306 /* 32 bit int with 32 bit offset */ /* [2] */
2307 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 32, 32, 8),
2311 .str_sec_size = sizeof(""),
2312 .map_type = BPF_MAP_TYPE_ARRAY,
2313 .map_name = "array_map_check_btf",
2314 .key_size = sizeof(int),
2315 .value_size = sizeof(int),
2319 .map_create_err = true,
2323 .descr = "arraymap invalid btf key (!= 32 bits)",
2326 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
2327 /* 16 bit int with 0 bit offset */ /* [2] */
2328 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 16, 2),
2332 .str_sec_size = sizeof(""),
2333 .map_type = BPF_MAP_TYPE_ARRAY,
2334 .map_name = "array_map_check_btf",
2335 .key_size = sizeof(int),
2336 .value_size = sizeof(int),
2340 .map_create_err = true,
2344 .descr = "arraymap invalid btf value (too small)",
2347 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
2351 .str_sec_size = sizeof(""),
2352 .map_type = BPF_MAP_TYPE_ARRAY,
2353 .map_name = "array_map_check_btf",
2354 .key_size = sizeof(int),
2355 /* btf_value_size < map->value_size */
2356 .value_size = sizeof(__u64),
2360 .map_create_err = true,
2364 .descr = "arraymap invalid btf value (too big)",
2367 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
2371 .str_sec_size = sizeof(""),
2372 .map_type = BPF_MAP_TYPE_ARRAY,
2373 .map_name = "array_map_check_btf",
2374 .key_size = sizeof(int),
2375 /* btf_value_size > map->value_size */
2376 .value_size = sizeof(__u16),
2380 .map_create_err = true,
2384 .descr = "func proto (int (*)(int, unsigned int))",
2386 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2387 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [2] */
2388 /* int (*)(int, unsigned int) */
2389 BTF_FUNC_PROTO_ENC(1, 2), /* [3] */
2390 BTF_FUNC_PROTO_ARG_ENC(0, 1),
2391 BTF_FUNC_PROTO_ARG_ENC(0, 2),
2395 .str_sec_size = sizeof(""),
2396 .map_type = BPF_MAP_TYPE_ARRAY,
2397 .map_name = "func_proto_type_check_btf",
2398 .key_size = sizeof(int),
2399 .value_size = sizeof(int),
2406 .descr = "func proto (vararg)",
2408 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2409 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [2] */
2410 /* void (*)(int, unsigned int, ...) */
2411 BTF_FUNC_PROTO_ENC(0, 3), /* [3] */
2412 BTF_FUNC_PROTO_ARG_ENC(0, 1),
2413 BTF_FUNC_PROTO_ARG_ENC(0, 2),
2414 BTF_FUNC_PROTO_ARG_ENC(0, 0),
2418 .str_sec_size = sizeof(""),
2419 .map_type = BPF_MAP_TYPE_ARRAY,
2420 .map_name = "func_proto_type_check_btf",
2421 .key_size = sizeof(int),
2422 .value_size = sizeof(int),
2429 .descr = "func proto (vararg with name)",
2431 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2432 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [2] */
2433 /* void (*)(int a, unsigned int b, ... c) */
2434 BTF_FUNC_PROTO_ENC(0, 3), /* [3] */
2435 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2436 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2437 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 0),
2440 .str_sec = "\0a\0b\0c",
2441 .str_sec_size = sizeof("\0a\0b\0c"),
2442 .map_type = BPF_MAP_TYPE_ARRAY,
2443 .map_name = "func_proto_type_check_btf",
2444 .key_size = sizeof(int),
2445 .value_size = sizeof(int),
2449 .btf_load_err = true,
2450 .err_str = "Invalid arg#3",
2454 .descr = "func proto (arg after vararg)",
2456 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2457 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [2] */
2458 /* void (*)(int a, ..., unsigned int b) */
2459 BTF_FUNC_PROTO_ENC(0, 3), /* [3] */
2460 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2461 BTF_FUNC_PROTO_ARG_ENC(0, 0),
2462 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2465 .str_sec = "\0a\0b",
2466 .str_sec_size = sizeof("\0a\0b"),
2467 .map_type = BPF_MAP_TYPE_ARRAY,
2468 .map_name = "func_proto_type_check_btf",
2469 .key_size = sizeof(int),
2470 .value_size = sizeof(int),
2474 .btf_load_err = true,
2475 .err_str = "Invalid arg#2",
2479 .descr = "func proto (CONST=>TYPEDEF=>PTR=>FUNC_PROTO)",
2481 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2482 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [2] */
2483 /* typedef void (*func_ptr)(int, unsigned int) */
2484 BTF_TYPEDEF_ENC(NAME_TBD, 5), /* [3] */
2485 /* const func_ptr */
2486 BTF_CONST_ENC(3), /* [4] */
2487 BTF_PTR_ENC(6), /* [5] */
2488 BTF_FUNC_PROTO_ENC(0, 2), /* [6] */
2489 BTF_FUNC_PROTO_ARG_ENC(0, 1),
2490 BTF_FUNC_PROTO_ARG_ENC(0, 2),
2493 .str_sec = "\0func_ptr",
2494 .str_sec_size = sizeof("\0func_ptr"),
2495 .map_type = BPF_MAP_TYPE_ARRAY,
2496 .map_name = "func_proto_type_check_btf",
2497 .key_size = sizeof(int),
2498 .value_size = sizeof(int),
2505 .descr = "func proto (TYPEDEF=>FUNC_PROTO)",
2507 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2508 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [2] */
2509 BTF_TYPEDEF_ENC(NAME_TBD, 4), /* [3] */
2510 BTF_FUNC_PROTO_ENC(0, 2), /* [4] */
2511 BTF_FUNC_PROTO_ARG_ENC(0, 1),
2512 BTF_FUNC_PROTO_ARG_ENC(0, 2),
2515 .str_sec = "\0func_typedef",
2516 .str_sec_size = sizeof("\0func_typedef"),
2517 .map_type = BPF_MAP_TYPE_ARRAY,
2518 .map_name = "func_proto_type_check_btf",
2519 .key_size = sizeof(int),
2520 .value_size = sizeof(int),
2527 .descr = "func proto (btf_resolve(arg))",
2529 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2530 /* void (*)(const void *) */
2531 BTF_FUNC_PROTO_ENC(0, 1), /* [2] */
2532 BTF_FUNC_PROTO_ARG_ENC(0, 3),
2533 BTF_CONST_ENC(4), /* [3] */
2534 BTF_PTR_ENC(0), /* [4] */
2538 .str_sec_size = sizeof(""),
2539 .map_type = BPF_MAP_TYPE_ARRAY,
2540 .map_name = "func_proto_type_check_btf",
2541 .key_size = sizeof(int),
2542 .value_size = sizeof(int),
2549 .descr = "func proto (Not all arg has name)",
2551 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2552 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [2] */
2553 /* void (*)(int, unsigned int b) */
2554 BTF_FUNC_PROTO_ENC(0, 2), /* [3] */
2555 BTF_FUNC_PROTO_ARG_ENC(0, 1),
2556 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2560 .str_sec_size = sizeof("\0b"),
2561 .map_type = BPF_MAP_TYPE_ARRAY,
2562 .map_name = "func_proto_type_check_btf",
2563 .key_size = sizeof(int),
2564 .value_size = sizeof(int),
2571 .descr = "func proto (Bad arg name_off)",
2573 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2574 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [2] */
2575 /* void (*)(int a, unsigned int <bad_name_off>) */
2576 BTF_FUNC_PROTO_ENC(0, 2), /* [3] */
2577 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2578 BTF_FUNC_PROTO_ARG_ENC(0x0fffffff, 2),
2582 .str_sec_size = sizeof("\0a"),
2583 .map_type = BPF_MAP_TYPE_ARRAY,
2584 .map_name = "func_proto_type_check_btf",
2585 .key_size = sizeof(int),
2586 .value_size = sizeof(int),
2590 .btf_load_err = true,
2591 .err_str = "Invalid arg#2",
2595 .descr = "func proto (Bad arg name)",
2597 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2598 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [2] */
2599 /* void (*)(int a, unsigned int !!!) */
2600 BTF_FUNC_PROTO_ENC(0, 2), /* [3] */
2601 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2602 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2605 .str_sec = "\0a\0!!!",
2606 .str_sec_size = sizeof("\0a\0!!!"),
2607 .map_type = BPF_MAP_TYPE_ARRAY,
2608 .map_name = "func_proto_type_check_btf",
2609 .key_size = sizeof(int),
2610 .value_size = sizeof(int),
2614 .btf_load_err = true,
2615 .err_str = "Invalid arg#2",
2619 .descr = "func proto (Invalid return type)",
2621 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2622 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [2] */
2623 /* <bad_ret_type> (*)(int, unsigned int) */
2624 BTF_FUNC_PROTO_ENC(100, 2), /* [3] */
2625 BTF_FUNC_PROTO_ARG_ENC(0, 1),
2626 BTF_FUNC_PROTO_ARG_ENC(0, 2),
2630 .str_sec_size = sizeof(""),
2631 .map_type = BPF_MAP_TYPE_ARRAY,
2632 .map_name = "func_proto_type_check_btf",
2633 .key_size = sizeof(int),
2634 .value_size = sizeof(int),
2638 .btf_load_err = true,
2639 .err_str = "Invalid return type",
2643 .descr = "func proto (with func name)",
2645 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2646 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [2] */
2647 /* void func_proto(int, unsigned int) */
2648 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 2), 0), /* [3] */
2649 BTF_FUNC_PROTO_ARG_ENC(0, 1),
2650 BTF_FUNC_PROTO_ARG_ENC(0, 2),
2653 .str_sec = "\0func_proto",
2654 .str_sec_size = sizeof("\0func_proto"),
2655 .map_type = BPF_MAP_TYPE_ARRAY,
2656 .map_name = "func_proto_type_check_btf",
2657 .key_size = sizeof(int),
2658 .value_size = sizeof(int),
2662 .btf_load_err = true,
2663 .err_str = "Invalid name",
2667 .descr = "func proto (const void arg)",
2669 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2670 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [2] */
2671 /* void (*)(const void) */
2672 BTF_FUNC_PROTO_ENC(0, 1), /* [3] */
2673 BTF_FUNC_PROTO_ARG_ENC(0, 4),
2674 BTF_CONST_ENC(0), /* [4] */
2678 .str_sec_size = sizeof(""),
2679 .map_type = BPF_MAP_TYPE_ARRAY,
2680 .map_name = "func_proto_type_check_btf",
2681 .key_size = sizeof(int),
2682 .value_size = sizeof(int),
2686 .btf_load_err = true,
2687 .err_str = "Invalid arg#1",
2691 .descr = "func (void func(int a, unsigned int b))",
2693 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2694 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [2] */
2695 /* void (*)(int a, unsigned int b) */
2696 BTF_FUNC_PROTO_ENC(0, 2), /* [3] */
2697 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2698 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2699 /* void func(int a, unsigned int b) */
2700 BTF_FUNC_ENC(NAME_TBD, 3), /* [4] */
2703 .str_sec = "\0a\0b\0func",
2704 .str_sec_size = sizeof("\0a\0b\0func"),
2705 .map_type = BPF_MAP_TYPE_ARRAY,
2706 .map_name = "func_type_check_btf",
2707 .key_size = sizeof(int),
2708 .value_size = sizeof(int),
2715 .descr = "func (No func name)",
2717 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2718 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [2] */
2719 /* void (*)(int a, unsigned int b) */
2720 BTF_FUNC_PROTO_ENC(0, 2), /* [3] */
2721 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2722 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2723 /* void <no_name>(int a, unsigned int b) */
2724 BTF_FUNC_ENC(0, 3), /* [4] */
2727 .str_sec = "\0a\0b",
2728 .str_sec_size = sizeof("\0a\0b"),
2729 .map_type = BPF_MAP_TYPE_ARRAY,
2730 .map_name = "func_type_check_btf",
2731 .key_size = sizeof(int),
2732 .value_size = sizeof(int),
2736 .btf_load_err = true,
2737 .err_str = "Invalid name",
2741 .descr = "func (Invalid func name)",
2743 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2744 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [2] */
2745 /* void (*)(int a, unsigned int b) */
2746 BTF_FUNC_PROTO_ENC(0, 2), /* [3] */
2747 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2748 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2749 /* void !!!(int a, unsigned int b) */
2750 BTF_FUNC_ENC(NAME_TBD, 3), /* [4] */
2753 .str_sec = "\0a\0b\0!!!",
2754 .str_sec_size = sizeof("\0a\0b\0!!!"),
2755 .map_type = BPF_MAP_TYPE_ARRAY,
2756 .map_name = "func_type_check_btf",
2757 .key_size = sizeof(int),
2758 .value_size = sizeof(int),
2762 .btf_load_err = true,
2763 .err_str = "Invalid name",
2767 .descr = "func (Some arg has no name)",
2769 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2770 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [2] */
2771 /* void (*)(int a, unsigned int) */
2772 BTF_FUNC_PROTO_ENC(0, 2), /* [3] */
2773 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2774 BTF_FUNC_PROTO_ARG_ENC(0, 2),
2775 /* void func(int a, unsigned int) */
2776 BTF_FUNC_ENC(NAME_TBD, 3), /* [4] */
2779 .str_sec = "\0a\0func",
2780 .str_sec_size = sizeof("\0a\0func"),
2781 .map_type = BPF_MAP_TYPE_ARRAY,
2782 .map_name = "func_type_check_btf",
2783 .key_size = sizeof(int),
2784 .value_size = sizeof(int),
2788 .btf_load_err = true,
2789 .err_str = "Invalid arg#2",
2793 .descr = "func (Non zero vlen)",
2795 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2796 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [2] */
2797 /* void (*)(int a, unsigned int b) */
2798 BTF_FUNC_PROTO_ENC(0, 2), /* [3] */
2799 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2800 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2801 /* void func(int a, unsigned int b) */
2802 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 2), 3), /* [4] */
2805 .str_sec = "\0a\0b\0func",
2806 .str_sec_size = sizeof("\0a\0b\0func"),
2807 .map_type = BPF_MAP_TYPE_ARRAY,
2808 .map_name = "func_type_check_btf",
2809 .key_size = sizeof(int),
2810 .value_size = sizeof(int),
2814 .btf_load_err = true,
2815 .err_str = "vlen != 0",
2819 .descr = "func (Not referring to FUNC_PROTO)",
2821 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2822 BTF_FUNC_ENC(NAME_TBD, 1), /* [2] */
2825 .str_sec = "\0func",
2826 .str_sec_size = sizeof("\0func"),
2827 .map_type = BPF_MAP_TYPE_ARRAY,
2828 .map_name = "func_type_check_btf",
2829 .key_size = sizeof(int),
2830 .value_size = sizeof(int),
2834 .btf_load_err = true,
2835 .err_str = "Invalid type_id",
2839 .descr = "invalid int kind_flag",
2841 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2842 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_INT, 1, 0), 4), /* [2] */
2843 BTF_INT_ENC(0, 0, 32),
2847 .map_type = BPF_MAP_TYPE_ARRAY,
2848 .map_name = "int_type_check_btf",
2849 .key_size = sizeof(int),
2850 .value_size = sizeof(int),
2854 .btf_load_err = true,
2855 .err_str = "Invalid btf_info kind_flag",
2859 .descr = "invalid ptr kind_flag",
2861 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2862 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 1, 0), 1), /* [2] */
2866 .map_type = BPF_MAP_TYPE_ARRAY,
2867 .map_name = "ptr_type_check_btf",
2868 .key_size = sizeof(int),
2869 .value_size = sizeof(int),
2873 .btf_load_err = true,
2874 .err_str = "Invalid btf_info kind_flag",
2878 .descr = "invalid array kind_flag",
2880 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2881 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ARRAY, 1, 0), 0), /* [2] */
2882 BTF_ARRAY_ENC(1, 1, 1),
2886 .map_type = BPF_MAP_TYPE_ARRAY,
2887 .map_name = "array_type_check_btf",
2888 .key_size = sizeof(int),
2889 .value_size = sizeof(int),
2893 .btf_load_err = true,
2894 .err_str = "Invalid btf_info kind_flag",
2898 .descr = "invalid enum kind_flag",
2900 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2901 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 1, 1), 4), /* [2] */
2902 BTF_ENUM_ENC(NAME_TBD, 0),
2906 .map_type = BPF_MAP_TYPE_ARRAY,
2907 .map_name = "enum_type_check_btf",
2908 .key_size = sizeof(int),
2909 .value_size = sizeof(int),
2913 .btf_load_err = true,
2914 .err_str = "Invalid btf_info kind_flag",
2918 .descr = "valid fwd kind_flag",
2920 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2921 BTF_TYPE_ENC(NAME_TBD,
2922 BTF_INFO_ENC(BTF_KIND_FWD, 1, 0), 0), /* [2] */
2926 .map_type = BPF_MAP_TYPE_ARRAY,
2927 .map_name = "fwd_type_check_btf",
2928 .key_size = sizeof(int),
2929 .value_size = sizeof(int),
2936 .descr = "invalid typedef kind_flag",
2938 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2939 BTF_TYPE_ENC(NAME_TBD,
2940 BTF_INFO_ENC(BTF_KIND_TYPEDEF, 1, 0), 1), /* [2] */
2944 .map_type = BPF_MAP_TYPE_ARRAY,
2945 .map_name = "typedef_type_check_btf",
2946 .key_size = sizeof(int),
2947 .value_size = sizeof(int),
2951 .btf_load_err = true,
2952 .err_str = "Invalid btf_info kind_flag",
2956 .descr = "invalid volatile kind_flag",
2958 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2959 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_VOLATILE, 1, 0), 1), /* [2] */
2963 .map_type = BPF_MAP_TYPE_ARRAY,
2964 .map_name = "volatile_type_check_btf",
2965 .key_size = sizeof(int),
2966 .value_size = sizeof(int),
2970 .btf_load_err = true,
2971 .err_str = "Invalid btf_info kind_flag",
2975 .descr = "invalid const kind_flag",
2977 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2978 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 1, 0), 1), /* [2] */
2982 .map_type = BPF_MAP_TYPE_ARRAY,
2983 .map_name = "const_type_check_btf",
2984 .key_size = sizeof(int),
2985 .value_size = sizeof(int),
2989 .btf_load_err = true,
2990 .err_str = "Invalid btf_info kind_flag",
2994 .descr = "invalid restrict kind_flag",
2996 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2997 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_RESTRICT, 1, 0), 1), /* [2] */
3001 .map_type = BPF_MAP_TYPE_ARRAY,
3002 .map_name = "restrict_type_check_btf",
3003 .key_size = sizeof(int),
3004 .value_size = sizeof(int),
3008 .btf_load_err = true,
3009 .err_str = "Invalid btf_info kind_flag",
3013 .descr = "invalid func kind_flag",
3015 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3016 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 0), 0), /* [2] */
3017 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FUNC, 1, 0), 2), /* [3] */
3021 .map_type = BPF_MAP_TYPE_ARRAY,
3022 .map_name = "func_type_check_btf",
3023 .key_size = sizeof(int),
3024 .value_size = sizeof(int),
3028 .btf_load_err = true,
3029 .err_str = "Invalid btf_info kind_flag",
3033 .descr = "invalid func_proto kind_flag",
3035 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3036 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 1, 0), 0), /* [2] */
3040 .map_type = BPF_MAP_TYPE_ARRAY,
3041 .map_name = "func_proto_type_check_btf",
3042 .key_size = sizeof(int),
3043 .value_size = sizeof(int),
3047 .btf_load_err = true,
3048 .err_str = "Invalid btf_info kind_flag",
3052 .descr = "valid struct, kind_flag, bitfield_size = 0",
3054 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3055 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 8), /* [2] */
3056 BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(0, 0)),
3057 BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(0, 32)),
3060 BTF_STR_SEC("\0A\0B"),
3061 .map_type = BPF_MAP_TYPE_ARRAY,
3062 .map_name = "struct_type_check_btf",
3063 .key_size = sizeof(int),
3064 .value_size = sizeof(int),
3071 .descr = "valid struct, kind_flag, int member, bitfield_size != 0",
3073 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3074 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 4), /* [2] */
3075 BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(4, 0)),
3076 BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(4, 4)),
3079 BTF_STR_SEC("\0A\0B"),
3080 .map_type = BPF_MAP_TYPE_ARRAY,
3081 .map_name = "struct_type_check_btf",
3082 .key_size = sizeof(int),
3083 .value_size = sizeof(int),
3090 .descr = "valid union, kind_flag, int member, bitfield_size != 0",
3092 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3093 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 1, 2), 4), /* [2] */
3094 BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(4, 0)),
3095 BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(4, 0)),
3098 BTF_STR_SEC("\0A\0B"),
3099 .map_type = BPF_MAP_TYPE_ARRAY,
3100 .map_name = "union_type_check_btf",
3101 .key_size = sizeof(int),
3102 .value_size = sizeof(int),
3109 .descr = "valid struct, kind_flag, enum member, bitfield_size != 0",
3111 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3112 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4), /* [2] */
3113 BTF_ENUM_ENC(NAME_TBD, 0),
3114 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 4),/* [3] */
3115 BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(4, 0)),
3116 BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(4, 4)),
3119 BTF_STR_SEC("\0A\0B\0C"),
3120 .map_type = BPF_MAP_TYPE_ARRAY,
3121 .map_name = "struct_type_check_btf",
3122 .key_size = sizeof(int),
3123 .value_size = sizeof(int),
3130 .descr = "valid union, kind_flag, enum member, bitfield_size != 0",
3132 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3133 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4), /* [2] */
3134 BTF_ENUM_ENC(NAME_TBD, 0),
3135 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 1, 2), 4), /* [3] */
3136 BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(4, 0)),
3137 BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(4, 0)),
3140 BTF_STR_SEC("\0A\0B\0C"),
3141 .map_type = BPF_MAP_TYPE_ARRAY,
3142 .map_name = "union_type_check_btf",
3143 .key_size = sizeof(int),
3144 .value_size = sizeof(int),
3151 .descr = "valid struct, kind_flag, typedef member, bitfield_size != 0",
3153 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3154 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4), /* [2] */
3155 BTF_ENUM_ENC(NAME_TBD, 0),
3156 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 4),/* [3] */
3157 BTF_MEMBER_ENC(NAME_TBD, 4, BTF_MEMBER_OFFSET(4, 0)),
3158 BTF_MEMBER_ENC(NAME_TBD, 5, BTF_MEMBER_OFFSET(4, 4)),
3159 BTF_TYPEDEF_ENC(NAME_TBD, 1), /* [4] */
3160 BTF_TYPEDEF_ENC(NAME_TBD, 2), /* [5] */
3163 BTF_STR_SEC("\0A\0B\0C\0D\0E"),
3164 .map_type = BPF_MAP_TYPE_ARRAY,
3165 .map_name = "struct_type_check_btf",
3166 .key_size = sizeof(int),
3167 .value_size = sizeof(int),
3174 .descr = "valid union, kind_flag, typedef member, bitfield_size != 0",
3176 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3177 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4), /* [2] */
3178 BTF_ENUM_ENC(NAME_TBD, 0),
3179 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 1, 2), 4), /* [3] */
3180 BTF_MEMBER_ENC(NAME_TBD, 4, BTF_MEMBER_OFFSET(4, 0)),
3181 BTF_MEMBER_ENC(NAME_TBD, 5, BTF_MEMBER_OFFSET(4, 0)),
3182 BTF_TYPEDEF_ENC(NAME_TBD, 1), /* [4] */
3183 BTF_TYPEDEF_ENC(NAME_TBD, 2), /* [5] */
3186 BTF_STR_SEC("\0A\0B\0C\0D\0E"),
3187 .map_type = BPF_MAP_TYPE_ARRAY,
3188 .map_name = "union_type_check_btf",
3189 .key_size = sizeof(int),
3190 .value_size = sizeof(int),
3197 .descr = "invalid struct, kind_flag, bitfield_size greater than struct size",
3199 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3200 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 4), /* [2] */
3201 BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(20, 0)),
3202 BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(20, 20)),
3205 BTF_STR_SEC("\0A\0B"),
3206 .map_type = BPF_MAP_TYPE_ARRAY,
3207 .map_name = "struct_type_check_btf",
3208 .key_size = sizeof(int),
3209 .value_size = sizeof(int),
3213 .btf_load_err = true,
3214 .err_str = "Member exceeds struct_size",
3218 .descr = "invalid struct, kind_flag, bitfield base_type int not regular",
3220 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3221 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 20, 4), /* [2] */
3222 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 4), /* [3] */
3223 BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(20, 0)),
3224 BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(20, 20)),
3227 BTF_STR_SEC("\0A\0B"),
3228 .map_type = BPF_MAP_TYPE_ARRAY,
3229 .map_name = "struct_type_check_btf",
3230 .key_size = sizeof(int),
3231 .value_size = sizeof(int),
3235 .btf_load_err = true,
3236 .err_str = "Invalid member base type",
3240 .descr = "invalid struct, kind_flag, base_type int not regular",
3242 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3243 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 12, 4), /* [2] */
3244 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 4), /* [3] */
3245 BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(8, 0)),
3246 BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(8, 8)),
3249 BTF_STR_SEC("\0A\0B"),
3250 .map_type = BPF_MAP_TYPE_ARRAY,
3251 .map_name = "struct_type_check_btf",
3252 .key_size = sizeof(int),
3253 .value_size = sizeof(int),
3257 .btf_load_err = true,
3258 .err_str = "Invalid member base type",
3262 .descr = "invalid union, kind_flag, bitfield_size greater than struct size",
3264 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3265 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 1, 2), 2), /* [2] */
3266 BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(8, 0)),
3267 BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(20, 0)),
3270 BTF_STR_SEC("\0A\0B"),
3271 .map_type = BPF_MAP_TYPE_ARRAY,
3272 .map_name = "union_type_check_btf",
3273 .key_size = sizeof(int),
3274 .value_size = sizeof(int),
3278 .btf_load_err = true,
3279 .err_str = "Member exceeds struct_size",
3283 .descr = "invalid struct, kind_flag, int member, bitfield_size = 0, wrong byte alignment",
3285 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3286 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */
3287 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 12), /* [3] */
3288 BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 0)),
3289 BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 36)),
3292 BTF_STR_SEC("\0A\0B"),
3293 .map_type = BPF_MAP_TYPE_ARRAY,
3294 .map_name = "struct_type_check_btf",
3295 .key_size = sizeof(int),
3296 .value_size = sizeof(int),
3300 .btf_load_err = true,
3301 .err_str = "Invalid member offset",
3305 .descr = "invalid struct, kind_flag, enum member, bitfield_size = 0, wrong byte alignment",
3307 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3308 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */
3309 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4), /* [2] */
3310 BTF_ENUM_ENC(NAME_TBD, 0),
3311 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 12), /* [3] */
3312 BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 0)),
3313 BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 36)),
3316 BTF_STR_SEC("\0A\0B\0C"),
3317 .map_type = BPF_MAP_TYPE_ARRAY,
3318 .map_name = "struct_type_check_btf",
3319 .key_size = sizeof(int),
3320 .value_size = sizeof(int),
3324 .btf_load_err = true,
3325 .err_str = "Invalid member offset",
3329 .descr = "128-bit int",
3331 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3332 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 128, 16), /* [2] */
3336 .map_type = BPF_MAP_TYPE_ARRAY,
3337 .map_name = "int_type_check_btf",
3338 .key_size = sizeof(int),
3339 .value_size = sizeof(int),
3346 .descr = "struct, 128-bit int member",
3348 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3349 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 128, 16), /* [2] */
3350 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 16), /* [3] */
3351 BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3355 .map_type = BPF_MAP_TYPE_ARRAY,
3356 .map_name = "struct_type_check_btf",
3357 .key_size = sizeof(int),
3358 .value_size = sizeof(int),
3365 .descr = "struct, 120-bit int member bitfield",
3367 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3368 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 120, 16), /* [2] */
3369 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 16), /* [3] */
3370 BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3374 .map_type = BPF_MAP_TYPE_ARRAY,
3375 .map_name = "struct_type_check_btf",
3376 .key_size = sizeof(int),
3377 .value_size = sizeof(int),
3384 .descr = "struct, kind_flag, 128-bit int member",
3386 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3387 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 128, 16), /* [2] */
3388 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 1), 16), /* [3] */
3389 BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 0)),
3393 .map_type = BPF_MAP_TYPE_ARRAY,
3394 .map_name = "struct_type_check_btf",
3395 .key_size = sizeof(int),
3396 .value_size = sizeof(int),
3403 .descr = "struct, kind_flag, 120-bit int member bitfield",
3405 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
3406 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 128, 16), /* [2] */
3407 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 1), 16), /* [3] */
3408 BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(120, 0)),
3412 .map_type = BPF_MAP_TYPE_ARRAY,
3413 .map_name = "struct_type_check_btf",
3414 .key_size = sizeof(int),
3415 .value_size = sizeof(int),
3421 }; /* struct btf_raw_test raw_tests[] */
3423 static const char *get_next_str(const char *start, const char *end)
3425 return start < end - 1 ? start + 1 : NULL;
3428 static int get_raw_sec_size(const __u32 *raw_types)
3432 for (i = MAX_NR_RAW_U32 - 1;
3433 i >= 0 && raw_types[i] != BTF_END_RAW;
3437 return i < 0 ? i : i * sizeof(raw_types[0]);
3440 static void *btf_raw_create(const struct btf_header *hdr,
3441 const __u32 *raw_types,
3443 unsigned int str_sec_size,
3444 unsigned int *btf_size,
3445 const char **ret_next_str)
3447 const char *next_str = str, *end_str = str + str_sec_size;
3448 const char **strs_idx = NULL, **tmp_strs_idx;
3449 int strs_cap = 0, strs_cnt = 0, next_str_idx = 0;
3450 unsigned int size_needed, offset;
3451 struct btf_header *ret_hdr;
3452 int i, type_sec_size, err = 0;
3453 uint32_t *ret_types;
3454 void *raw_btf = NULL;
3456 type_sec_size = get_raw_sec_size(raw_types);
3457 if (CHECK(type_sec_size < 0, "Cannot get nr_raw_types"))
3460 size_needed = sizeof(*hdr) + type_sec_size + str_sec_size;
3461 raw_btf = malloc(size_needed);
3462 if (CHECK(!raw_btf, "Cannot allocate memory for raw_btf"))
3466 memcpy(raw_btf, hdr, sizeof(*hdr));
3467 offset = sizeof(*hdr);
3470 while ((next_str = get_next_str(next_str, end_str))) {
3471 if (strs_cnt == strs_cap) {
3472 strs_cap += max(16, strs_cap / 2);
3473 tmp_strs_idx = realloc(strs_idx,
3474 sizeof(*strs_idx) * strs_cap);
3475 if (CHECK(!tmp_strs_idx,
3476 "Cannot allocate memory for strs_idx")) {
3480 strs_idx = tmp_strs_idx;
3482 strs_idx[strs_cnt++] = next_str;
3483 next_str += strlen(next_str);
3486 /* Copy type section */
3487 ret_types = raw_btf + offset;
3488 for (i = 0; i < type_sec_size / sizeof(raw_types[0]); i++) {
3489 if (raw_types[i] == NAME_TBD) {
3490 if (CHECK(next_str_idx == strs_cnt,
3491 "Error in getting next_str #%d",
3496 ret_types[i] = strs_idx[next_str_idx++] - str;
3497 } else if (IS_NAME_NTH(raw_types[i])) {
3498 int idx = GET_NAME_NTH_IDX(raw_types[i]);
3500 if (CHECK(idx <= 0 || idx > strs_cnt,
3501 "Error getting string #%d, strs_cnt:%d",
3506 ret_types[i] = strs_idx[idx-1] - str;
3508 ret_types[i] = raw_types[i];
3511 offset += type_sec_size;
3513 /* Copy string section */
3514 memcpy(raw_btf + offset, str, str_sec_size);
3516 ret_hdr = (struct btf_header *)raw_btf;
3517 ret_hdr->type_len = type_sec_size;
3518 ret_hdr->str_off = type_sec_size;
3519 ret_hdr->str_len = str_sec_size;
3521 *btf_size = size_needed;
3524 next_str_idx < strs_cnt ? strs_idx[next_str_idx] : NULL;
3537 static int do_test_raw(unsigned int test_num)
3539 struct btf_raw_test *test = &raw_tests[test_num - 1];
3540 struct bpf_create_map_attr create_attr = {};
3541 int map_fd = -1, btf_fd = -1;
3542 unsigned int raw_btf_size;
3543 struct btf_header *hdr;
3547 fprintf(stderr, "BTF raw test[%u] (%s): ", test_num, test->descr);
3548 raw_btf = btf_raw_create(&hdr_tmpl,
3552 &raw_btf_size, NULL);
3559 hdr->hdr_len = (int)hdr->hdr_len + test->hdr_len_delta;
3560 hdr->type_off = (int)hdr->type_off + test->type_off_delta;
3561 hdr->str_off = (int)hdr->str_off + test->str_off_delta;
3562 hdr->str_len = (int)hdr->str_len + test->str_len_delta;
3564 *btf_log_buf = '\0';
3565 btf_fd = bpf_load_btf(raw_btf, raw_btf_size,
3566 btf_log_buf, BTF_LOG_BUF_SIZE,
3570 err = ((btf_fd == -1) != test->btf_load_err);
3571 if (CHECK(err, "btf_fd:%d test->btf_load_err:%u",
3572 btf_fd, test->btf_load_err) ||
3573 CHECK(test->err_str && !strstr(btf_log_buf, test->err_str),
3574 "expected err_str:%s", test->err_str)) {
3579 if (err || btf_fd == -1)
3582 create_attr.name = test->map_name;
3583 create_attr.map_type = test->map_type;
3584 create_attr.key_size = test->key_size;
3585 create_attr.value_size = test->value_size;
3586 create_attr.max_entries = test->max_entries;
3587 create_attr.btf_fd = btf_fd;
3588 create_attr.btf_key_type_id = test->key_type_id;
3589 create_attr.btf_value_type_id = test->value_type_id;
3591 map_fd = bpf_create_map_xattr(&create_attr);
3593 err = ((map_fd == -1) != test->map_create_err);
3594 CHECK(err, "map_fd:%d test->map_create_err:%u",
3595 map_fd, test->map_create_err);
3599 fprintf(stderr, "OK");
3601 if (*btf_log_buf && (err || args.always_log))
3602 fprintf(stderr, "\n%s", btf_log_buf);
3612 static int test_raw(void)
3617 if (args.raw_test_num)
3618 return count_result(do_test_raw(args.raw_test_num));
3620 for (i = 1; i <= ARRAY_SIZE(raw_tests); i++)
3621 err |= count_result(do_test_raw(i));
3626 struct btf_get_info_test {
3628 const char *str_sec;
3629 __u32 raw_types[MAX_NR_RAW_U32];
3632 int (*special_test)(unsigned int test_num);
3635 static int test_big_btf_info(unsigned int test_num);
3636 static int test_btf_id(unsigned int test_num);
3638 const struct btf_get_info_test get_info_tests[] = {
3640 .descr = "== raw_btf_size+1",
3643 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
3647 .str_sec_size = sizeof(""),
3648 .btf_size_delta = 1,
3651 .descr = "== raw_btf_size-3",
3654 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
3658 .str_sec_size = sizeof(""),
3659 .btf_size_delta = -3,
3662 .descr = "Large bpf_btf_info",
3665 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
3669 .str_sec_size = sizeof(""),
3670 .special_test = test_big_btf_info,
3676 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
3677 /* unsigned int */ /* [2] */
3678 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),
3682 .str_sec_size = sizeof(""),
3683 .special_test = test_btf_id,
3687 static inline __u64 ptr_to_u64(const void *ptr)
3689 return (__u64)(unsigned long)ptr;
3692 static int test_big_btf_info(unsigned int test_num)
3694 const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
3695 uint8_t *raw_btf = NULL, *user_btf = NULL;
3696 unsigned int raw_btf_size;
3698 struct bpf_btf_info info;
3701 struct bpf_btf_info *info;
3702 int btf_fd = -1, err;
3705 raw_btf = btf_raw_create(&hdr_tmpl,
3709 &raw_btf_size, NULL);
3714 *btf_log_buf = '\0';
3716 user_btf = malloc(raw_btf_size);
3717 if (CHECK(!user_btf, "!user_btf")) {
3722 btf_fd = bpf_load_btf(raw_btf, raw_btf_size,
3723 btf_log_buf, BTF_LOG_BUF_SIZE,
3725 if (CHECK(btf_fd == -1, "errno:%d", errno)) {
3731 * GET_INFO should error out if the userspace info
3732 * has non zero tailing bytes.
3734 info = &info_garbage.info;
3735 memset(info, 0, sizeof(*info));
3736 info_garbage.garbage = 0xdeadbeef;
3737 info_len = sizeof(info_garbage);
3738 info->btf = ptr_to_u64(user_btf);
3739 info->btf_size = raw_btf_size;
3741 err = bpf_obj_get_info_by_fd(btf_fd, info, &info_len);
3742 if (CHECK(!err, "!err")) {
3748 * GET_INFO should succeed even info_len is larger than
3749 * the kernel supported as long as tailing bytes are zero.
3750 * The kernel supported info len should also be returned
3753 info_garbage.garbage = 0;
3754 err = bpf_obj_get_info_by_fd(btf_fd, info, &info_len);
3755 if (CHECK(err || info_len != sizeof(*info),
3756 "err:%d errno:%d info_len:%u sizeof(*info):%lu",
3757 err, errno, info_len, sizeof(*info))) {
3762 fprintf(stderr, "OK");
3765 if (*btf_log_buf && (err || args.always_log))
3766 fprintf(stderr, "\n%s", btf_log_buf);
3777 static int test_btf_id(unsigned int test_num)
3779 const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
3780 struct bpf_create_map_attr create_attr = {};
3781 uint8_t *raw_btf = NULL, *user_btf[2] = {};
3782 int btf_fd[2] = {-1, -1}, map_fd = -1;
3783 struct bpf_map_info map_info = {};
3784 struct bpf_btf_info info[2] = {};
3785 unsigned int raw_btf_size;
3789 raw_btf = btf_raw_create(&hdr_tmpl,
3793 &raw_btf_size, NULL);
3798 *btf_log_buf = '\0';
3800 for (i = 0; i < 2; i++) {
3801 user_btf[i] = malloc(raw_btf_size);
3802 if (CHECK(!user_btf[i], "!user_btf[%d]", i)) {
3806 info[i].btf = ptr_to_u64(user_btf[i]);
3807 info[i].btf_size = raw_btf_size;
3810 btf_fd[0] = bpf_load_btf(raw_btf, raw_btf_size,
3811 btf_log_buf, BTF_LOG_BUF_SIZE,
3813 if (CHECK(btf_fd[0] == -1, "errno:%d", errno)) {
3818 /* Test BPF_OBJ_GET_INFO_BY_ID on btf_id */
3819 info_len = sizeof(info[0]);
3820 err = bpf_obj_get_info_by_fd(btf_fd[0], &info[0], &info_len);
3821 if (CHECK(err, "errno:%d", errno)) {
3826 btf_fd[1] = bpf_btf_get_fd_by_id(info[0].id);
3827 if (CHECK(btf_fd[1] == -1, "errno:%d", errno)) {
3833 err = bpf_obj_get_info_by_fd(btf_fd[1], &info[1], &info_len);
3834 if (CHECK(err || info[0].id != info[1].id ||
3835 info[0].btf_size != info[1].btf_size ||
3836 (ret = memcmp(user_btf[0], user_btf[1], info[0].btf_size)),
3837 "err:%d errno:%d id0:%u id1:%u btf_size0:%u btf_size1:%u memcmp:%d",
3838 err, errno, info[0].id, info[1].id,
3839 info[0].btf_size, info[1].btf_size, ret)) {
3844 /* Test btf members in struct bpf_map_info */
3845 create_attr.name = "test_btf_id";
3846 create_attr.map_type = BPF_MAP_TYPE_ARRAY;
3847 create_attr.key_size = sizeof(int);
3848 create_attr.value_size = sizeof(unsigned int);
3849 create_attr.max_entries = 4;
3850 create_attr.btf_fd = btf_fd[0];
3851 create_attr.btf_key_type_id = 1;
3852 create_attr.btf_value_type_id = 2;
3854 map_fd = bpf_create_map_xattr(&create_attr);
3855 if (CHECK(map_fd == -1, "errno:%d", errno)) {
3860 info_len = sizeof(map_info);
3861 err = bpf_obj_get_info_by_fd(map_fd, &map_info, &info_len);
3862 if (CHECK(err || map_info.btf_id != info[0].id ||
3863 map_info.btf_key_type_id != 1 || map_info.btf_value_type_id != 2,
3864 "err:%d errno:%d info.id:%u btf_id:%u btf_key_type_id:%u btf_value_type_id:%u",
3865 err, errno, info[0].id, map_info.btf_id, map_info.btf_key_type_id,
3866 map_info.btf_value_type_id)) {
3871 for (i = 0; i < 2; i++) {
3876 /* Test BTF ID is removed from the kernel */
3877 btf_fd[0] = bpf_btf_get_fd_by_id(map_info.btf_id);
3878 if (CHECK(btf_fd[0] == -1, "errno:%d", errno)) {
3885 /* The map holds the last ref to BTF and its btf_id */
3888 btf_fd[0] = bpf_btf_get_fd_by_id(map_info.btf_id);
3889 if (CHECK(btf_fd[0] != -1, "BTF lingers")) {
3894 fprintf(stderr, "OK");
3897 if (*btf_log_buf && (err || args.always_log))
3898 fprintf(stderr, "\n%s", btf_log_buf);
3903 for (i = 0; i < 2; i++) {
3905 if (btf_fd[i] != -1)
3912 static int do_test_get_info(unsigned int test_num)
3914 const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
3915 unsigned int raw_btf_size, user_btf_size, expected_nbytes;
3916 uint8_t *raw_btf = NULL, *user_btf = NULL;
3917 struct bpf_btf_info info = {};
3918 int btf_fd = -1, err, ret;
3921 fprintf(stderr, "BTF GET_INFO test[%u] (%s): ",
3922 test_num, test->descr);
3924 if (test->special_test)
3925 return test->special_test(test_num);
3927 raw_btf = btf_raw_create(&hdr_tmpl,
3931 &raw_btf_size, NULL);
3936 *btf_log_buf = '\0';
3938 user_btf = malloc(raw_btf_size);
3939 if (CHECK(!user_btf, "!user_btf")) {
3944 btf_fd = bpf_load_btf(raw_btf, raw_btf_size,
3945 btf_log_buf, BTF_LOG_BUF_SIZE,
3947 if (CHECK(btf_fd == -1, "errno:%d", errno)) {
3952 user_btf_size = (int)raw_btf_size + test->btf_size_delta;
3953 expected_nbytes = min(raw_btf_size, user_btf_size);
3954 if (raw_btf_size > expected_nbytes)
3955 memset(user_btf + expected_nbytes, 0xff,
3956 raw_btf_size - expected_nbytes);
3958 info_len = sizeof(info);
3959 info.btf = ptr_to_u64(user_btf);
3960 info.btf_size = user_btf_size;
3963 err = bpf_obj_get_info_by_fd(btf_fd, &info, &info_len);
3964 if (CHECK(err || !info.id || info_len != sizeof(info) ||
3965 info.btf_size != raw_btf_size ||
3966 (ret = memcmp(raw_btf, user_btf, expected_nbytes)),
3967 "err:%d errno:%d info.id:%u info_len:%u sizeof(info):%lu raw_btf_size:%u info.btf_size:%u expected_nbytes:%u memcmp:%d",
3968 err, errno, info.id, info_len, sizeof(info),
3969 raw_btf_size, info.btf_size, expected_nbytes, ret)) {
3974 while (expected_nbytes < raw_btf_size) {
3975 fprintf(stderr, "%u...", expected_nbytes);
3976 if (CHECK(user_btf[expected_nbytes++] != 0xff,
3977 "user_btf[%u]:%x != 0xff", expected_nbytes - 1,
3978 user_btf[expected_nbytes - 1])) {
3984 fprintf(stderr, "OK");
3987 if (*btf_log_buf && (err || args.always_log))
3988 fprintf(stderr, "\n%s", btf_log_buf);
3999 static int test_get_info(void)
4004 if (args.get_info_test_num)
4005 return count_result(do_test_get_info(args.get_info_test_num));
4007 for (i = 1; i <= ARRAY_SIZE(get_info_tests); i++)
4008 err |= count_result(do_test_get_info(i));
4013 struct btf_file_test {
4015 bool btf_kv_notfound;
4018 static struct btf_file_test file_tests[] = {
4019 { .file = "test_btf_haskv.o", },
4020 { .file = "test_btf_newkv.o", },
4021 { .file = "test_btf_nokv.o", .btf_kv_notfound = true, },
4024 static int do_test_file(unsigned int test_num)
4026 const struct btf_file_test *test = &file_tests[test_num - 1];
4027 const char *expected_fnames[] = {"_dummy_tracepoint",
4028 "test_long_fname_1",
4029 "test_long_fname_2"};
4030 struct btf_ext *btf_ext = NULL;
4031 struct bpf_prog_info info = {};
4032 struct bpf_object *obj = NULL;
4033 struct bpf_func_info *finfo;
4034 struct bpf_program *prog;
4035 __u32 info_len, rec_size;
4036 bool has_btf_ext = false;
4037 struct btf *btf = NULL;
4038 void *func_info = NULL;
4039 struct bpf_map *map;
4040 int i, err, prog_fd;
4042 fprintf(stderr, "BTF libbpf test[%u] (%s): ", test_num,
4045 btf = btf__parse_elf(test->file, &btf_ext);
4047 if (PTR_ERR(btf) == -ENOENT) {
4048 fprintf(stderr, "SKIP. No ELF %s found", BTF_ELF_SEC);
4052 return PTR_ERR(btf);
4056 has_btf_ext = btf_ext != NULL;
4057 btf_ext__free(btf_ext);
4059 obj = bpf_object__open(test->file);
4060 if (CHECK(IS_ERR(obj), "obj: %ld", PTR_ERR(obj)))
4061 return PTR_ERR(obj);
4063 err = bpf_object__btf_fd(obj);
4064 if (CHECK(err == -1, "bpf_object__btf_fd: -1"))
4067 prog = bpf_program__next(NULL, obj);
4068 if (CHECK(!prog, "Cannot find bpf_prog")) {
4073 bpf_program__set_type(prog, BPF_PROG_TYPE_TRACEPOINT);
4074 err = bpf_object__load(obj);
4075 if (CHECK(err < 0, "bpf_object__load: %d", err))
4077 prog_fd = bpf_program__fd(prog);
4079 map = bpf_object__find_map_by_name(obj, "btf_map");
4080 if (CHECK(!map, "btf_map not found")) {
4085 err = (bpf_map__btf_key_type_id(map) == 0 || bpf_map__btf_value_type_id(map) == 0)
4086 != test->btf_kv_notfound;
4087 if (CHECK(err, "btf_key_type_id:%u btf_value_type_id:%u test->btf_kv_notfound:%u",
4088 bpf_map__btf_key_type_id(map), bpf_map__btf_value_type_id(map),
4089 test->btf_kv_notfound))
4095 /* get necessary program info */
4096 info_len = sizeof(struct bpf_prog_info);
4097 err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
4099 if (CHECK(err == -1, "invalid get info (1st) errno:%d", errno)) {
4100 fprintf(stderr, "%s\n", btf_log_buf);
4104 if (CHECK(info.nr_func_info != 3,
4105 "incorrect info.nr_func_info (1st) %d",
4106 info.nr_func_info)) {
4110 rec_size = info.func_info_rec_size;
4111 if (CHECK(rec_size != sizeof(struct bpf_func_info),
4112 "incorrect info.func_info_rec_size (1st) %d\n", rec_size)) {
4117 func_info = malloc(info.nr_func_info * rec_size);
4118 if (CHECK(!func_info, "out of memory")) {
4123 /* reset info to only retrieve func_info related data */
4124 memset(&info, 0, sizeof(info));
4125 info.nr_func_info = 3;
4126 info.func_info_rec_size = rec_size;
4127 info.func_info = ptr_to_u64(func_info);
4129 err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
4131 if (CHECK(err == -1, "invalid get info (2nd) errno:%d", errno)) {
4132 fprintf(stderr, "%s\n", btf_log_buf);
4136 if (CHECK(info.nr_func_info != 3,
4137 "incorrect info.nr_func_info (2nd) %d",
4138 info.nr_func_info)) {
4142 if (CHECK(info.func_info_rec_size != rec_size,
4143 "incorrect info.func_info_rec_size (2nd) %d",
4144 info.func_info_rec_size)) {
4149 err = btf__get_from_id(info.btf_id, &btf);
4150 if (CHECK(err, "cannot get btf from kernel, err: %d", err))
4153 /* check three functions */
4155 for (i = 0; i < 3; i++) {
4156 const struct btf_type *t;
4159 t = btf__type_by_id(btf, finfo->type_id);
4160 if (CHECK(!t, "btf__type_by_id failure: id %u",
4166 fname = btf__name_by_offset(btf, t->name_off);
4167 err = strcmp(fname, expected_fnames[i]);
4168 /* for the second and third functions in .text section,
4169 * the compiler may order them either way.
4172 err = strcmp(fname, expected_fnames[3 - i]);
4173 if (CHECK(err, "incorrect fname %s", fname ? : "")) {
4178 finfo = (void *)finfo + rec_size;
4182 fprintf(stderr, "OK");
4186 bpf_object__close(obj);
4190 static int test_file(void)
4195 if (args.file_test_num)
4196 return count_result(do_test_file(args.file_test_num));
4198 for (i = 1; i <= ARRAY_SIZE(file_tests); i++)
4199 err |= count_result(do_test_file(i));
4204 const char *pprint_enum_str[] = {
4211 struct pprint_mapv {
4216 uint32_t unused_bits2a:2,
4231 uint8_t si8_4[2][2];
4234 #ifdef __SIZEOF_INT128__
4235 struct pprint_mapv_int128 {
4238 unsigned __int128 bits3:3;
4239 unsigned __int128 bits80:80;
4240 unsigned __int128 ui128;
4244 static struct btf_raw_test pprint_test_template[] = {
4247 /* unsighed char */ /* [1] */
4248 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 8, 1),
4249 /* unsigned short */ /* [2] */
4250 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 16, 2),
4251 /* unsigned int */ /* [3] */
4252 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),
4254 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
4255 /* unsigned long long */ /* [5] */
4256 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 64, 8),
4257 /* 2 bits */ /* [6] */
4258 BTF_TYPE_INT_ENC(0, 0, 0, 2, 2),
4259 /* 28 bits */ /* [7] */
4260 BTF_TYPE_INT_ENC(0, 0, 0, 28, 4),
4261 /* uint8_t[8] */ /* [8] */
4262 BTF_TYPE_ARRAY_ENC(9, 1, 8),
4263 /* typedef unsigned char uint8_t */ /* [9] */
4264 BTF_TYPEDEF_ENC(NAME_TBD, 1),
4265 /* typedef unsigned short uint16_t */ /* [10] */
4266 BTF_TYPEDEF_ENC(NAME_TBD, 2),
4267 /* typedef unsigned int uint32_t */ /* [11] */
4268 BTF_TYPEDEF_ENC(NAME_TBD, 3),
4269 /* typedef int int32_t */ /* [12] */
4270 BTF_TYPEDEF_ENC(NAME_TBD, 4),
4271 /* typedef unsigned long long uint64_t *//* [13] */
4272 BTF_TYPEDEF_ENC(NAME_TBD, 5),
4273 /* union (anon) */ /* [14] */
4274 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 0, 2), 8),
4275 BTF_MEMBER_ENC(NAME_TBD, 13, 0),/* uint64_t ui64; */
4276 BTF_MEMBER_ENC(NAME_TBD, 8, 0), /* uint8_t ui8a[8]; */
4277 /* enum (anon) */ /* [15] */
4278 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 4), 4),
4279 BTF_ENUM_ENC(NAME_TBD, 0),
4280 BTF_ENUM_ENC(NAME_TBD, 1),
4281 BTF_ENUM_ENC(NAME_TBD, 2),
4282 BTF_ENUM_ENC(NAME_TBD, 3),
4283 /* struct pprint_mapv */ /* [16] */
4284 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 11), 40),
4285 BTF_MEMBER_ENC(NAME_TBD, 11, 0), /* uint32_t ui32 */
4286 BTF_MEMBER_ENC(NAME_TBD, 10, 32), /* uint16_t ui16 */
4287 BTF_MEMBER_ENC(NAME_TBD, 12, 64), /* int32_t si32 */
4288 BTF_MEMBER_ENC(NAME_TBD, 6, 96), /* unused_bits2a */
4289 BTF_MEMBER_ENC(NAME_TBD, 7, 98), /* bits28 */
4290 BTF_MEMBER_ENC(NAME_TBD, 6, 126), /* unused_bits2b */
4291 BTF_MEMBER_ENC(0, 14, 128), /* union (anon) */
4292 BTF_MEMBER_ENC(NAME_TBD, 15, 192), /* aenum */
4293 BTF_MEMBER_ENC(NAME_TBD, 11, 224), /* uint32_t ui32b */
4294 BTF_MEMBER_ENC(NAME_TBD, 6, 256), /* bits2c */
4295 BTF_MEMBER_ENC(NAME_TBD, 17, 264), /* si8_4 */
4296 BTF_TYPE_ARRAY_ENC(18, 1, 2), /* [17] */
4297 BTF_TYPE_ARRAY_ENC(1, 1, 2), /* [18] */
4300 BTF_STR_SEC("\0unsigned char\0unsigned short\0unsigned int\0int\0unsigned long long\0uint8_t\0uint16_t\0uint32_t\0int32_t\0uint64_t\0ui64\0ui8a\0ENUM_ZERO\0ENUM_ONE\0ENUM_TWO\0ENUM_THREE\0pprint_mapv\0ui32\0ui16\0si32\0unused_bits2a\0bits28\0unused_bits2b\0aenum\0ui32b\0bits2c\0si8_4"),
4301 .key_size = sizeof(unsigned int),
4302 .value_size = sizeof(struct pprint_mapv),
4303 .key_type_id = 3, /* unsigned int */
4304 .value_type_id = 16, /* struct pprint_mapv */
4305 .max_entries = 128 * 1024,
4309 /* this type will have the same type as the
4310 * first .raw_types definition, but struct type will
4311 * be encoded with kind_flag set.
4314 /* unsighed char */ /* [1] */
4315 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 8, 1),
4316 /* unsigned short */ /* [2] */
4317 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 16, 2),
4318 /* unsigned int */ /* [3] */
4319 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),
4321 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
4322 /* unsigned long long */ /* [5] */
4323 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 64, 8),
4324 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [6] */
4325 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [7] */
4326 /* uint8_t[8] */ /* [8] */
4327 BTF_TYPE_ARRAY_ENC(9, 1, 8),
4328 /* typedef unsigned char uint8_t */ /* [9] */
4329 BTF_TYPEDEF_ENC(NAME_TBD, 1),
4330 /* typedef unsigned short uint16_t */ /* [10] */
4331 BTF_TYPEDEF_ENC(NAME_TBD, 2),
4332 /* typedef unsigned int uint32_t */ /* [11] */
4333 BTF_TYPEDEF_ENC(NAME_TBD, 3),
4334 /* typedef int int32_t */ /* [12] */
4335 BTF_TYPEDEF_ENC(NAME_TBD, 4),
4336 /* typedef unsigned long long uint64_t *//* [13] */
4337 BTF_TYPEDEF_ENC(NAME_TBD, 5),
4338 /* union (anon) */ /* [14] */
4339 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 0, 2), 8),
4340 BTF_MEMBER_ENC(NAME_TBD, 13, 0),/* uint64_t ui64; */
4341 BTF_MEMBER_ENC(NAME_TBD, 8, 0), /* uint8_t ui8a[8]; */
4342 /* enum (anon) */ /* [15] */
4343 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 4), 4),
4344 BTF_ENUM_ENC(NAME_TBD, 0),
4345 BTF_ENUM_ENC(NAME_TBD, 1),
4346 BTF_ENUM_ENC(NAME_TBD, 2),
4347 BTF_ENUM_ENC(NAME_TBD, 3),
4348 /* struct pprint_mapv */ /* [16] */
4349 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 11), 40),
4350 BTF_MEMBER_ENC(NAME_TBD, 11, BTF_MEMBER_OFFSET(0, 0)), /* uint32_t ui32 */
4351 BTF_MEMBER_ENC(NAME_TBD, 10, BTF_MEMBER_OFFSET(0, 32)), /* uint16_t ui16 */
4352 BTF_MEMBER_ENC(NAME_TBD, 12, BTF_MEMBER_OFFSET(0, 64)), /* int32_t si32 */
4353 BTF_MEMBER_ENC(NAME_TBD, 6, BTF_MEMBER_OFFSET(2, 96)), /* unused_bits2a */
4354 BTF_MEMBER_ENC(NAME_TBD, 7, BTF_MEMBER_OFFSET(28, 98)), /* bits28 */
4355 BTF_MEMBER_ENC(NAME_TBD, 6, BTF_MEMBER_OFFSET(2, 126)), /* unused_bits2b */
4356 BTF_MEMBER_ENC(0, 14, BTF_MEMBER_OFFSET(0, 128)), /* union (anon) */
4357 BTF_MEMBER_ENC(NAME_TBD, 15, BTF_MEMBER_OFFSET(0, 192)), /* aenum */
4358 BTF_MEMBER_ENC(NAME_TBD, 11, BTF_MEMBER_OFFSET(0, 224)), /* uint32_t ui32b */
4359 BTF_MEMBER_ENC(NAME_TBD, 6, BTF_MEMBER_OFFSET(2, 256)), /* bits2c */
4360 BTF_MEMBER_ENC(NAME_TBD, 17, 264), /* si8_4 */
4361 BTF_TYPE_ARRAY_ENC(18, 1, 2), /* [17] */
4362 BTF_TYPE_ARRAY_ENC(1, 1, 2), /* [18] */
4365 BTF_STR_SEC("\0unsigned char\0unsigned short\0unsigned int\0int\0unsigned long long\0uint8_t\0uint16_t\0uint32_t\0int32_t\0uint64_t\0ui64\0ui8a\0ENUM_ZERO\0ENUM_ONE\0ENUM_TWO\0ENUM_THREE\0pprint_mapv\0ui32\0ui16\0si32\0unused_bits2a\0bits28\0unused_bits2b\0aenum\0ui32b\0bits2c\0si8_4"),
4366 .key_size = sizeof(unsigned int),
4367 .value_size = sizeof(struct pprint_mapv),
4368 .key_type_id = 3, /* unsigned int */
4369 .value_type_id = 16, /* struct pprint_mapv */
4370 .max_entries = 128 * 1024,
4374 /* this type will have the same layout as the
4375 * first .raw_types definition. The struct type will
4376 * be encoded with kind_flag set, bitfield members
4377 * are added typedef/const/volatile, and bitfield members
4378 * will have both int and enum types.
4381 /* unsighed char */ /* [1] */
4382 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 8, 1),
4383 /* unsigned short */ /* [2] */
4384 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 16, 2),
4385 /* unsigned int */ /* [3] */
4386 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),
4388 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
4389 /* unsigned long long */ /* [5] */
4390 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 64, 8),
4391 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [6] */
4392 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4), /* [7] */
4393 /* uint8_t[8] */ /* [8] */
4394 BTF_TYPE_ARRAY_ENC(9, 1, 8),
4395 /* typedef unsigned char uint8_t */ /* [9] */
4396 BTF_TYPEDEF_ENC(NAME_TBD, 1),
4397 /* typedef unsigned short uint16_t */ /* [10] */
4398 BTF_TYPEDEF_ENC(NAME_TBD, 2),
4399 /* typedef unsigned int uint32_t */ /* [11] */
4400 BTF_TYPEDEF_ENC(NAME_TBD, 3),
4401 /* typedef int int32_t */ /* [12] */
4402 BTF_TYPEDEF_ENC(NAME_TBD, 4),
4403 /* typedef unsigned long long uint64_t *//* [13] */
4404 BTF_TYPEDEF_ENC(NAME_TBD, 5),
4405 /* union (anon) */ /* [14] */
4406 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 0, 2), 8),
4407 BTF_MEMBER_ENC(NAME_TBD, 13, 0),/* uint64_t ui64; */
4408 BTF_MEMBER_ENC(NAME_TBD, 8, 0), /* uint8_t ui8a[8]; */
4409 /* enum (anon) */ /* [15] */
4410 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 4), 4),
4411 BTF_ENUM_ENC(NAME_TBD, 0),
4412 BTF_ENUM_ENC(NAME_TBD, 1),
4413 BTF_ENUM_ENC(NAME_TBD, 2),
4414 BTF_ENUM_ENC(NAME_TBD, 3),
4415 /* struct pprint_mapv */ /* [16] */
4416 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 11), 40),
4417 BTF_MEMBER_ENC(NAME_TBD, 11, BTF_MEMBER_OFFSET(0, 0)), /* uint32_t ui32 */
4418 BTF_MEMBER_ENC(NAME_TBD, 10, BTF_MEMBER_OFFSET(0, 32)), /* uint16_t ui16 */
4419 BTF_MEMBER_ENC(NAME_TBD, 12, BTF_MEMBER_OFFSET(0, 64)), /* int32_t si32 */
4420 BTF_MEMBER_ENC(NAME_TBD, 17, BTF_MEMBER_OFFSET(2, 96)), /* unused_bits2a */
4421 BTF_MEMBER_ENC(NAME_TBD, 7, BTF_MEMBER_OFFSET(28, 98)), /* bits28 */
4422 BTF_MEMBER_ENC(NAME_TBD, 19, BTF_MEMBER_OFFSET(2, 126)),/* unused_bits2b */
4423 BTF_MEMBER_ENC(0, 14, BTF_MEMBER_OFFSET(0, 128)), /* union (anon) */
4424 BTF_MEMBER_ENC(NAME_TBD, 15, BTF_MEMBER_OFFSET(0, 192)), /* aenum */
4425 BTF_MEMBER_ENC(NAME_TBD, 11, BTF_MEMBER_OFFSET(0, 224)), /* uint32_t ui32b */
4426 BTF_MEMBER_ENC(NAME_TBD, 17, BTF_MEMBER_OFFSET(2, 256)), /* bits2c */
4427 BTF_MEMBER_ENC(NAME_TBD, 20, BTF_MEMBER_OFFSET(0, 264)), /* si8_4 */
4428 /* typedef unsigned int ___int */ /* [17] */
4429 BTF_TYPEDEF_ENC(NAME_TBD, 18),
4430 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_VOLATILE, 0, 0), 6), /* [18] */
4431 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 15), /* [19] */
4432 BTF_TYPE_ARRAY_ENC(21, 1, 2), /* [20] */
4433 BTF_TYPE_ARRAY_ENC(1, 1, 2), /* [21] */
4436 BTF_STR_SEC("\0unsigned char\0unsigned short\0unsigned int\0int\0unsigned long long\0uint8_t\0uint16_t\0uint32_t\0int32_t\0uint64_t\0ui64\0ui8a\0ENUM_ZERO\0ENUM_ONE\0ENUM_TWO\0ENUM_THREE\0pprint_mapv\0ui32\0ui16\0si32\0unused_bits2a\0bits28\0unused_bits2b\0aenum\0ui32b\0bits2c\0___int\0si8_4"),
4437 .key_size = sizeof(unsigned int),
4438 .value_size = sizeof(struct pprint_mapv),
4439 .key_type_id = 3, /* unsigned int */
4440 .value_type_id = 16, /* struct pprint_mapv */
4441 .max_entries = 128 * 1024,
4444 #ifdef __SIZEOF_INT128__
4448 /* unsigned int */ /* [1] */
4449 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),
4450 /* __int128 */ /* [2] */
4451 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 128, 16),
4452 /* unsigned __int128 */ /* [3] */
4453 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 128, 16),
4454 /* struct pprint_mapv_int128 */ /* [4] */
4455 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 5), 64),
4456 BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 0)), /* si128a */
4457 BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 128)), /* si128b */
4458 BTF_MEMBER_ENC(NAME_TBD, 3, BTF_MEMBER_OFFSET(3, 256)), /* bits3 */
4459 BTF_MEMBER_ENC(NAME_TBD, 3, BTF_MEMBER_OFFSET(80, 259)), /* bits80 */
4460 BTF_MEMBER_ENC(NAME_TBD, 3, BTF_MEMBER_OFFSET(0, 384)), /* ui128 */
4463 BTF_STR_SEC("\0unsigned int\0__int128\0unsigned __int128\0pprint_mapv_int128\0si128a\0si128b\0bits3\0bits80\0ui128"),
4464 .key_size = sizeof(unsigned int),
4465 .value_size = sizeof(struct pprint_mapv_int128),
4468 .max_entries = 128 * 1024,
4469 .mapv_kind = PPRINT_MAPV_KIND_INT128,
4475 static struct btf_pprint_test_meta {
4477 enum bpf_map_type map_type;
4478 const char *map_name;
4482 } pprint_tests_meta[] = {
4484 .descr = "BTF pretty print array",
4485 .map_type = BPF_MAP_TYPE_ARRAY,
4486 .map_name = "pprint_test_array",
4487 .ordered_map = true,
4488 .lossless_map = true,
4489 .percpu_map = false,
4493 .descr = "BTF pretty print hash",
4494 .map_type = BPF_MAP_TYPE_HASH,
4495 .map_name = "pprint_test_hash",
4496 .ordered_map = false,
4497 .lossless_map = true,
4498 .percpu_map = false,
4502 .descr = "BTF pretty print lru hash",
4503 .map_type = BPF_MAP_TYPE_LRU_HASH,
4504 .map_name = "pprint_test_lru_hash",
4505 .ordered_map = false,
4506 .lossless_map = false,
4507 .percpu_map = false,
4511 .descr = "BTF pretty print percpu array",
4512 .map_type = BPF_MAP_TYPE_PERCPU_ARRAY,
4513 .map_name = "pprint_test_percpu_array",
4514 .ordered_map = true,
4515 .lossless_map = true,
4520 .descr = "BTF pretty print percpu hash",
4521 .map_type = BPF_MAP_TYPE_PERCPU_HASH,
4522 .map_name = "pprint_test_percpu_hash",
4523 .ordered_map = false,
4524 .lossless_map = true,
4529 .descr = "BTF pretty print lru percpu hash",
4530 .map_type = BPF_MAP_TYPE_LRU_PERCPU_HASH,
4531 .map_name = "pprint_test_lru_percpu_hash",
4532 .ordered_map = false,
4533 .lossless_map = false,
4539 static size_t get_pprint_mapv_size(enum pprint_mapv_kind_t mapv_kind)
4541 if (mapv_kind == PPRINT_MAPV_KIND_BASIC)
4542 return sizeof(struct pprint_mapv);
4544 #ifdef __SIZEOF_INT128__
4545 if (mapv_kind == PPRINT_MAPV_KIND_INT128)
4546 return sizeof(struct pprint_mapv_int128);
4552 static void set_pprint_mapv(enum pprint_mapv_kind_t mapv_kind,
4553 void *mapv, uint32_t i,
4554 int num_cpus, int rounded_value_size)
4558 if (mapv_kind == PPRINT_MAPV_KIND_BASIC) {
4559 struct pprint_mapv *v = mapv;
4561 for (cpu = 0; cpu < num_cpus; cpu++) {
4564 v->unused_bits2a = 3;
4566 v->unused_bits2b = 3;
4568 v->aenum = i & 0x03;
4571 v->si8_4[0][0] = (cpu + i) & 0xff;
4572 v->si8_4[0][1] = (cpu + i + 1) & 0xff;
4573 v->si8_4[1][0] = (cpu + i + 2) & 0xff;
4574 v->si8_4[1][1] = (cpu + i + 3) & 0xff;
4575 v = (void *)v + rounded_value_size;
4579 #ifdef __SIZEOF_INT128__
4580 if (mapv_kind == PPRINT_MAPV_KIND_INT128) {
4581 struct pprint_mapv_int128 *v = mapv;
4583 for (cpu = 0; cpu < num_cpus; cpu++) {
4586 v->bits3 = i & 0x07;
4587 v->bits80 = (((unsigned __int128)1) << 64) + i;
4588 v->ui128 = (((unsigned __int128)2) << 64) + i;
4589 v = (void *)v + rounded_value_size;
4595 ssize_t get_pprint_expected_line(enum pprint_mapv_kind_t mapv_kind,
4596 char *expected_line, ssize_t line_size,
4597 bool percpu_map, unsigned int next_key,
4598 int cpu, void *mapv)
4600 ssize_t nexpected_line = -1;
4602 if (mapv_kind == PPRINT_MAPV_KIND_BASIC) {
4603 struct pprint_mapv *v = mapv;
4605 nexpected_line = snprintf(expected_line, line_size,
4606 "%s%u: {%u,0,%d,0x%x,0x%x,0x%x,"
4607 "{%lu|[%u,%u,%u,%u,%u,%u,%u,%u]},%s,"
4608 "%u,0x%x,[[%d,%d],[%d,%d]]}\n",
4609 percpu_map ? "\tcpu" : "",
4610 percpu_map ? cpu : next_key,
4616 v->ui8a[0], v->ui8a[1],
4617 v->ui8a[2], v->ui8a[3],
4618 v->ui8a[4], v->ui8a[5],
4619 v->ui8a[6], v->ui8a[7],
4620 pprint_enum_str[v->aenum],
4623 v->si8_4[0][0], v->si8_4[0][1],
4624 v->si8_4[1][0], v->si8_4[1][1]);
4627 #ifdef __SIZEOF_INT128__
4628 if (mapv_kind == PPRINT_MAPV_KIND_INT128) {
4629 struct pprint_mapv_int128 *v = mapv;
4631 nexpected_line = snprintf(expected_line, line_size,
4632 "%s%u: {0x%lx,0x%lx,0x%lx,"
4633 "0x%lx%016lx,0x%lx%016lx}\n",
4634 percpu_map ? "\tcpu" : "",
4635 percpu_map ? cpu : next_key,
4636 (uint64_t)v->si128a,
4637 (uint64_t)v->si128b,
4639 (uint64_t)(v->bits80 >> 64),
4640 (uint64_t)v->bits80,
4641 (uint64_t)(v->ui128 >> 64),
4642 (uint64_t)v->ui128);
4646 return nexpected_line;
4649 static int check_line(const char *expected_line, int nexpected_line,
4650 int expected_line_len, const char *line)
4652 if (CHECK(nexpected_line == expected_line_len,
4653 "expected_line is too long"))
4656 if (strcmp(expected_line, line)) {
4657 fprintf(stderr, "unexpected pprint output\n");
4658 fprintf(stderr, "expected: %s", expected_line);
4659 fprintf(stderr, " read: %s", line);
4667 static int do_test_pprint(int test_num)
4669 const struct btf_raw_test *test = &pprint_test_template[test_num];
4670 enum pprint_mapv_kind_t mapv_kind = test->mapv_kind;
4671 struct bpf_create_map_attr create_attr = {};
4672 bool ordered_map, lossless_map, percpu_map;
4673 int err, ret, num_cpus, rounded_value_size;
4674 unsigned int key, nr_read_elems;
4675 int map_fd = -1, btf_fd = -1;
4676 unsigned int raw_btf_size;
4677 char expected_line[255];
4678 FILE *pin_file = NULL;
4680 size_t line_len = 0;
4686 fprintf(stderr, "%s(#%d)......", test->descr, test_num);
4687 raw_btf = btf_raw_create(&hdr_tmpl, test->raw_types,
4688 test->str_sec, test->str_sec_size,
4689 &raw_btf_size, NULL);
4694 *btf_log_buf = '\0';
4695 btf_fd = bpf_load_btf(raw_btf, raw_btf_size,
4696 btf_log_buf, BTF_LOG_BUF_SIZE,
4700 if (CHECK(btf_fd == -1, "errno:%d", errno)) {
4705 create_attr.name = test->map_name;
4706 create_attr.map_type = test->map_type;
4707 create_attr.key_size = test->key_size;
4708 create_attr.value_size = test->value_size;
4709 create_attr.max_entries = test->max_entries;
4710 create_attr.btf_fd = btf_fd;
4711 create_attr.btf_key_type_id = test->key_type_id;
4712 create_attr.btf_value_type_id = test->value_type_id;
4714 map_fd = bpf_create_map_xattr(&create_attr);
4715 if (CHECK(map_fd == -1, "errno:%d", errno)) {
4720 ret = snprintf(pin_path, sizeof(pin_path), "%s/%s",
4721 "/sys/fs/bpf", test->map_name);
4723 if (CHECK(ret == sizeof(pin_path), "pin_path %s/%s is too long",
4724 "/sys/fs/bpf", test->map_name)) {
4729 err = bpf_obj_pin(map_fd, pin_path);
4730 if (CHECK(err, "bpf_obj_pin(%s): errno:%d.", pin_path, errno))
4733 percpu_map = test->percpu_map;
4734 num_cpus = percpu_map ? bpf_num_possible_cpus() : 1;
4735 rounded_value_size = round_up(get_pprint_mapv_size(mapv_kind), 8);
4736 mapv = calloc(num_cpus, rounded_value_size);
4737 if (CHECK(!mapv, "mapv allocation failure")) {
4742 for (key = 0; key < test->max_entries; key++) {
4743 set_pprint_mapv(mapv_kind, mapv, key, num_cpus, rounded_value_size);
4744 bpf_map_update_elem(map_fd, &key, mapv, 0);
4747 pin_file = fopen(pin_path, "r");
4748 if (CHECK(!pin_file, "fopen(%s): errno:%d", pin_path, errno)) {
4753 /* Skip lines start with '#' */
4754 while ((nread = getline(&line, &line_len, pin_file)) > 0 &&
4758 if (CHECK(nread <= 0, "Unexpected EOF")) {
4764 ordered_map = test->ordered_map;
4765 lossless_map = test->lossless_map;
4767 ssize_t nexpected_line;
4768 unsigned int next_key;
4772 next_key = ordered_map ? nr_read_elems : atoi(line);
4773 set_pprint_mapv(mapv_kind, mapv, next_key, num_cpus, rounded_value_size);
4776 for (cpu = 0; cpu < num_cpus; cpu++) {
4778 /* for percpu map, the format looks like:
4780 * cpu0: <value_on_cpu0>
4781 * cpu1: <value_on_cpu1>
4783 * cpun: <value_on_cpun>
4786 * let us verify the line containing the key here.
4789 nexpected_line = snprintf(expected_line,
4790 sizeof(expected_line),
4794 err = check_line(expected_line, nexpected_line,
4795 sizeof(expected_line), line);
4800 /* read value@cpu */
4801 nread = getline(&line, &line_len, pin_file);
4806 nexpected_line = get_pprint_expected_line(mapv_kind, expected_line,
4807 sizeof(expected_line),
4808 percpu_map, next_key,
4810 err = check_line(expected_line, nexpected_line,
4811 sizeof(expected_line), line);
4815 cmapv = cmapv + rounded_value_size;
4819 /* skip the last bracket for the percpu map */
4820 nread = getline(&line, &line_len, pin_file);
4825 nread = getline(&line, &line_len, pin_file);
4826 } while (++nr_read_elems < test->max_entries && nread > 0);
4829 CHECK(nr_read_elems < test->max_entries,
4830 "Unexpected EOF. nr_read_elems:%u test->max_entries:%u",
4831 nr_read_elems, test->max_entries)) {
4836 if (CHECK(nread > 0, "Unexpected extra pprint output: %s", line)) {
4847 fprintf(stderr, "OK");
4848 if (*btf_log_buf && (err || args.always_log))
4849 fprintf(stderr, "\n%s", btf_log_buf);
4862 static int test_pprint(void)
4867 /* test various maps with the first test template */
4868 for (i = 0; i < ARRAY_SIZE(pprint_tests_meta); i++) {
4869 pprint_test_template[0].descr = pprint_tests_meta[i].descr;
4870 pprint_test_template[0].map_type = pprint_tests_meta[i].map_type;
4871 pprint_test_template[0].map_name = pprint_tests_meta[i].map_name;
4872 pprint_test_template[0].ordered_map = pprint_tests_meta[i].ordered_map;
4873 pprint_test_template[0].lossless_map = pprint_tests_meta[i].lossless_map;
4874 pprint_test_template[0].percpu_map = pprint_tests_meta[i].percpu_map;
4876 err |= count_result(do_test_pprint(0));
4879 /* test rest test templates with the first map */
4880 for (i = 1; i < ARRAY_SIZE(pprint_test_template); i++) {
4881 pprint_test_template[i].descr = pprint_tests_meta[0].descr;
4882 pprint_test_template[i].map_type = pprint_tests_meta[0].map_type;
4883 pprint_test_template[i].map_name = pprint_tests_meta[0].map_name;
4884 pprint_test_template[i].ordered_map = pprint_tests_meta[0].ordered_map;
4885 pprint_test_template[i].lossless_map = pprint_tests_meta[0].lossless_map;
4886 pprint_test_template[i].percpu_map = pprint_tests_meta[0].percpu_map;
4887 err |= count_result(do_test_pprint(i));
4893 #define BPF_LINE_INFO_ENC(insn_off, file_off, line_off, line_num, line_col) \
4894 (insn_off), (file_off), (line_off), ((line_num) << 10 | ((line_col) & 0x3ff))
4896 static struct prog_info_raw_test {
4898 const char *str_sec;
4899 const char *err_str;
4900 __u32 raw_types[MAX_NR_RAW_U32];
4902 struct bpf_insn insns[MAX_INSNS];
4904 __u32 func_info[MAX_SUBPROGS][2];
4905 __u32 func_info_rec_size;
4906 __u32 func_info_cnt;
4907 __u32 line_info[MAX_NR_RAW_U32];
4908 __u32 line_info_rec_size;
4909 __u32 nr_jited_ksyms;
4910 bool expected_prog_load_failure;
4911 __u32 dead_code_cnt;
4912 __u32 dead_code_mask;
4913 __u32 dead_func_cnt;
4914 __u32 dead_func_mask;
4915 } info_raw_tests[] = {
4917 .descr = "func_type (main func + one sub)",
4919 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
4920 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4), /* [2] */
4921 BTF_FUNC_PROTO_ENC(1, 2), /* [3] */
4922 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
4923 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
4924 BTF_FUNC_PROTO_ENC(1, 2), /* [4] */
4925 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
4926 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
4927 BTF_FUNC_ENC(NAME_TBD, 3), /* [5] */
4928 BTF_FUNC_ENC(NAME_TBD, 4), /* [6] */
4931 .str_sec = "\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB",
4932 .str_sec_size = sizeof("\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB"),
4934 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
4935 BPF_MOV64_IMM(BPF_REG_0, 1),
4937 BPF_MOV64_IMM(BPF_REG_0, 2),
4940 .prog_type = BPF_PROG_TYPE_TRACEPOINT,
4941 .func_info = { {0, 5}, {3, 6} },
4942 .func_info_rec_size = 8,
4944 .line_info = { BTF_END_RAW },
4948 .descr = "func_type (Incorrect func_info_rec_size)",
4950 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
4951 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4), /* [2] */
4952 BTF_FUNC_PROTO_ENC(1, 2), /* [3] */
4953 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
4954 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
4955 BTF_FUNC_PROTO_ENC(1, 2), /* [4] */
4956 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
4957 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
4958 BTF_FUNC_ENC(NAME_TBD, 3), /* [5] */
4959 BTF_FUNC_ENC(NAME_TBD, 4), /* [6] */
4962 .str_sec = "\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB",
4963 .str_sec_size = sizeof("\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB"),
4965 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
4966 BPF_MOV64_IMM(BPF_REG_0, 1),
4968 BPF_MOV64_IMM(BPF_REG_0, 2),
4971 .prog_type = BPF_PROG_TYPE_TRACEPOINT,
4972 .func_info = { {0, 5}, {3, 6} },
4973 .func_info_rec_size = 4,
4975 .line_info = { BTF_END_RAW },
4976 .expected_prog_load_failure = true,
4980 .descr = "func_type (Incorrect func_info_cnt)",
4982 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
4983 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4), /* [2] */
4984 BTF_FUNC_PROTO_ENC(1, 2), /* [3] */
4985 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
4986 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
4987 BTF_FUNC_PROTO_ENC(1, 2), /* [4] */
4988 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
4989 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
4990 BTF_FUNC_ENC(NAME_TBD, 3), /* [5] */
4991 BTF_FUNC_ENC(NAME_TBD, 4), /* [6] */
4994 .str_sec = "\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB",
4995 .str_sec_size = sizeof("\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB"),
4997 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
4998 BPF_MOV64_IMM(BPF_REG_0, 1),
5000 BPF_MOV64_IMM(BPF_REG_0, 2),
5003 .prog_type = BPF_PROG_TYPE_TRACEPOINT,
5004 .func_info = { {0, 5}, {3, 6} },
5005 .func_info_rec_size = 8,
5007 .line_info = { BTF_END_RAW },
5008 .expected_prog_load_failure = true,
5012 .descr = "func_type (Incorrect bpf_func_info.insn_off)",
5014 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
5015 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4), /* [2] */
5016 BTF_FUNC_PROTO_ENC(1, 2), /* [3] */
5017 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5018 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5019 BTF_FUNC_PROTO_ENC(1, 2), /* [4] */
5020 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5021 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5022 BTF_FUNC_ENC(NAME_TBD, 3), /* [5] */
5023 BTF_FUNC_ENC(NAME_TBD, 4), /* [6] */
5026 .str_sec = "\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB",
5027 .str_sec_size = sizeof("\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB"),
5029 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
5030 BPF_MOV64_IMM(BPF_REG_0, 1),
5032 BPF_MOV64_IMM(BPF_REG_0, 2),
5035 .prog_type = BPF_PROG_TYPE_TRACEPOINT,
5036 .func_info = { {0, 5}, {2, 6} },
5037 .func_info_rec_size = 8,
5039 .line_info = { BTF_END_RAW },
5040 .expected_prog_load_failure = true,
5044 .descr = "line_info (No subprog)",
5046 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
5049 BTF_STR_SEC("\0int\0int a=1;\0int b=2;\0return a + b;\0return a + b;"),
5051 BPF_MOV64_IMM(BPF_REG_0, 1),
5052 BPF_MOV64_IMM(BPF_REG_1, 2),
5053 BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
5056 .prog_type = BPF_PROG_TYPE_TRACEPOINT,
5059 BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5060 BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 9),
5061 BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8),
5062 BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 7),
5065 .line_info_rec_size = sizeof(struct bpf_line_info),
5066 .nr_jited_ksyms = 1,
5070 .descr = "line_info (No subprog. insn_off >= prog->len)",
5072 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
5075 BTF_STR_SEC("\0int\0int a=1;\0int b=2;\0return a + b;\0return a + b;"),
5077 BPF_MOV64_IMM(BPF_REG_0, 1),
5078 BPF_MOV64_IMM(BPF_REG_1, 2),
5079 BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
5082 .prog_type = BPF_PROG_TYPE_TRACEPOINT,
5085 BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5086 BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 9),
5087 BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8),
5088 BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 7),
5089 BPF_LINE_INFO_ENC(4, 0, 0, 5, 6),
5092 .line_info_rec_size = sizeof(struct bpf_line_info),
5093 .nr_jited_ksyms = 1,
5094 .err_str = "line_info[4].insn_off",
5095 .expected_prog_load_failure = true,
5099 .descr = "line_info (Zero bpf insn code)",
5101 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
5102 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 64, 8), /* [2] */
5103 BTF_TYPEDEF_ENC(NAME_TBD, 2), /* [3] */
5106 BTF_STR_SEC("\0int\0unsigned long\0u64\0u64 a=1;\0return a;"),
5108 BPF_LD_IMM64(BPF_REG_0, 1),
5111 .prog_type = BPF_PROG_TYPE_TRACEPOINT,
5114 BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5115 BPF_LINE_INFO_ENC(1, 0, 0, 2, 9),
5116 BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8),
5119 .line_info_rec_size = sizeof(struct bpf_line_info),
5120 .nr_jited_ksyms = 1,
5121 .err_str = "Invalid insn code at line_info[1]",
5122 .expected_prog_load_failure = true,
5126 .descr = "line_info (No subprog. zero tailing line_info",
5128 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
5131 BTF_STR_SEC("\0int\0int a=1;\0int b=2;\0return a + b;\0return a + b;"),
5133 BPF_MOV64_IMM(BPF_REG_0, 1),
5134 BPF_MOV64_IMM(BPF_REG_1, 2),
5135 BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
5138 .prog_type = BPF_PROG_TYPE_TRACEPOINT,
5141 BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10), 0,
5142 BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 9), 0,
5143 BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8), 0,
5144 BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 7), 0,
5147 .line_info_rec_size = sizeof(struct bpf_line_info) + sizeof(__u32),
5148 .nr_jited_ksyms = 1,
5152 .descr = "line_info (No subprog. nonzero tailing line_info)",
5154 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
5157 BTF_STR_SEC("\0int\0int a=1;\0int b=2;\0return a + b;\0return a + b;"),
5159 BPF_MOV64_IMM(BPF_REG_0, 1),
5160 BPF_MOV64_IMM(BPF_REG_1, 2),
5161 BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
5164 .prog_type = BPF_PROG_TYPE_TRACEPOINT,
5167 BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10), 0,
5168 BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 9), 0,
5169 BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8), 0,
5170 BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 7), 1,
5173 .line_info_rec_size = sizeof(struct bpf_line_info) + sizeof(__u32),
5174 .nr_jited_ksyms = 1,
5175 .err_str = "nonzero tailing record in line_info",
5176 .expected_prog_load_failure = true,
5180 .descr = "line_info (subprog)",
5182 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
5185 BTF_STR_SEC("\0int\0int a=1+1;\0return func(a);\0b+=1;\0return b;"),
5187 BPF_MOV64_IMM(BPF_REG_2, 1),
5188 BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
5189 BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
5192 BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
5193 BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
5196 .prog_type = BPF_PROG_TYPE_TRACEPOINT,
5199 BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5200 BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 2, 9),
5201 BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 3, 8),
5202 BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 4, 7),
5205 .line_info_rec_size = sizeof(struct bpf_line_info),
5206 .nr_jited_ksyms = 2,
5210 .descr = "line_info (subprog + func_info)",
5212 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
5213 BTF_FUNC_PROTO_ENC(1, 1), /* [2] */
5214 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5215 BTF_FUNC_ENC(NAME_TBD, 2), /* [3] */
5216 BTF_FUNC_ENC(NAME_TBD, 2), /* [4] */
5219 BTF_STR_SEC("\0int\0x\0sub\0main\0int a=1+1;\0return func(a);\0b+=1;\0return b;"),
5221 BPF_MOV64_IMM(BPF_REG_2, 1),
5222 BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
5223 BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
5226 BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
5227 BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
5230 .prog_type = BPF_PROG_TYPE_TRACEPOINT,
5232 .func_info_rec_size = 8,
5233 .func_info = { {0, 4}, {5, 3} },
5235 BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5236 BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 2, 9),
5237 BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 3, 8),
5238 BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 4, 7),
5241 .line_info_rec_size = sizeof(struct bpf_line_info),
5242 .nr_jited_ksyms = 2,
5246 .descr = "line_info (subprog. missing 1st func line info)",
5248 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
5251 BTF_STR_SEC("\0int\0int a=1+1;\0return func(a);\0b+=1;\0return b;"),
5253 BPF_MOV64_IMM(BPF_REG_2, 1),
5254 BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
5255 BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
5258 BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
5259 BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
5262 .prog_type = BPF_PROG_TYPE_TRACEPOINT,
5265 BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 1, 10),
5266 BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 2, 9),
5267 BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 3, 8),
5268 BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 4, 7),
5271 .line_info_rec_size = sizeof(struct bpf_line_info),
5272 .nr_jited_ksyms = 2,
5273 .err_str = "missing bpf_line_info for func#0",
5274 .expected_prog_load_failure = true,
5278 .descr = "line_info (subprog. missing 2nd func line info)",
5280 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
5283 BTF_STR_SEC("\0int\0int a=1+1;\0return func(a);\0b+=1;\0return b;"),
5285 BPF_MOV64_IMM(BPF_REG_2, 1),
5286 BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
5287 BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
5290 BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
5291 BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
5294 .prog_type = BPF_PROG_TYPE_TRACEPOINT,
5297 BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5298 BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 2, 9),
5299 BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 3, 8),
5300 BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 4, 7),
5303 .line_info_rec_size = sizeof(struct bpf_line_info),
5304 .nr_jited_ksyms = 2,
5305 .err_str = "missing bpf_line_info for func#1",
5306 .expected_prog_load_failure = true,
5310 .descr = "line_info (subprog. unordered insn offset)",
5312 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
5315 BTF_STR_SEC("\0int\0int a=1+1;\0return func(a);\0b+=1;\0return b;"),
5317 BPF_MOV64_IMM(BPF_REG_2, 1),
5318 BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
5319 BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
5322 BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
5323 BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
5326 .prog_type = BPF_PROG_TYPE_TRACEPOINT,
5329 BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5330 BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 2, 9),
5331 BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8),
5332 BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 4, 7),
5335 .line_info_rec_size = sizeof(struct bpf_line_info),
5336 .nr_jited_ksyms = 2,
5337 .err_str = "Invalid line_info[2].insn_off",
5338 .expected_prog_load_failure = true,
5342 .descr = "line_info (dead start)",
5344 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
5347 BTF_STR_SEC("\0int\0/* dead jmp */\0int a=1;\0int b=2;\0return a + b;\0return a + b;"),
5349 BPF_JMP_IMM(BPF_JA, 0, 0, 0),
5350 BPF_MOV64_IMM(BPF_REG_0, 1),
5351 BPF_MOV64_IMM(BPF_REG_1, 2),
5352 BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
5355 .prog_type = BPF_PROG_TYPE_TRACEPOINT,
5358 BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5359 BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 9),
5360 BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8),
5361 BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 7),
5362 BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 5, 6),
5365 .line_info_rec_size = sizeof(struct bpf_line_info),
5366 .nr_jited_ksyms = 1,
5368 .dead_code_mask = 0x01,
5372 .descr = "line_info (dead end)",
5374 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
5377 BTF_STR_SEC("\0int\0int a=1;\0int b=2;\0return a + b;\0/* dead jmp */\0return a + b;\0/* dead exit */"),
5379 BPF_MOV64_IMM(BPF_REG_0, 1),
5380 BPF_MOV64_IMM(BPF_REG_1, 2),
5381 BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
5382 BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 10, 1),
5386 .prog_type = BPF_PROG_TYPE_TRACEPOINT,
5389 BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 12),
5390 BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 11),
5391 BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 10),
5392 BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 9),
5393 BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 5, 8),
5394 BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 6, 7),
5397 .line_info_rec_size = sizeof(struct bpf_line_info),
5398 .nr_jited_ksyms = 1,
5400 .dead_code_mask = 0x28,
5404 .descr = "line_info (dead code + subprog + func_info)",
5406 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
5407 BTF_FUNC_PROTO_ENC(1, 1), /* [2] */
5408 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5409 BTF_FUNC_ENC(NAME_TBD, 2), /* [3] */
5410 BTF_FUNC_ENC(NAME_TBD, 2), /* [4] */
5413 BTF_STR_SEC("\0int\0x\0sub\0main\0int a=1+1;\0/* dead jmp */"
5414 "\0/* dead */\0/* dead */\0/* dead */\0/* dead */"
5415 "\0/* dead */\0/* dead */\0/* dead */\0/* dead */"
5416 "\0return func(a);\0b+=1;\0return b;"),
5418 BPF_MOV64_IMM(BPF_REG_2, 1),
5419 BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
5420 BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
5421 BPF_JMP_IMM(BPF_JGE, BPF_REG_2, 0, 8),
5422 BPF_MOV64_IMM(BPF_REG_2, 1),
5423 BPF_MOV64_IMM(BPF_REG_2, 1),
5424 BPF_MOV64_IMM(BPF_REG_2, 1),
5425 BPF_MOV64_IMM(BPF_REG_2, 1),
5426 BPF_MOV64_IMM(BPF_REG_2, 1),
5427 BPF_MOV64_IMM(BPF_REG_2, 1),
5428 BPF_MOV64_IMM(BPF_REG_2, 1),
5429 BPF_MOV64_IMM(BPF_REG_2, 1),
5432 BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
5433 BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
5436 .prog_type = BPF_PROG_TYPE_TRACEPOINT,
5438 .func_info_rec_size = 8,
5439 .func_info = { {0, 4}, {14, 3} },
5441 BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5442 BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 1, 10),
5443 BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 1, 10),
5444 BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 1, 10),
5445 BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
5446 BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 1, 10),
5447 BPF_LINE_INFO_ENC(8, 0, NAME_TBD, 1, 10),
5448 BPF_LINE_INFO_ENC(9, 0, NAME_TBD, 1, 10),
5449 BPF_LINE_INFO_ENC(10, 0, NAME_TBD, 1, 10),
5450 BPF_LINE_INFO_ENC(11, 0, NAME_TBD, 2, 9),
5451 BPF_LINE_INFO_ENC(12, 0, NAME_TBD, 2, 9),
5452 BPF_LINE_INFO_ENC(14, 0, NAME_TBD, 3, 8),
5453 BPF_LINE_INFO_ENC(16, 0, NAME_TBD, 4, 7),
5456 .line_info_rec_size = sizeof(struct bpf_line_info),
5457 .nr_jited_ksyms = 2,
5459 .dead_code_mask = 0x3fe,
5463 .descr = "line_info (dead subprog)",
5465 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
5466 BTF_FUNC_PROTO_ENC(1, 1), /* [2] */
5467 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5468 BTF_FUNC_ENC(NAME_TBD, 2), /* [3] */
5469 BTF_FUNC_ENC(NAME_TBD, 2), /* [4] */
5470 BTF_FUNC_ENC(NAME_TBD, 2), /* [5] */
5473 BTF_STR_SEC("\0int\0x\0dead\0main\0func\0int a=1+1;\0/* live call */"
5474 "\0return 0;\0return 0;\0/* dead */\0/* dead */"
5475 "\0/* dead */\0return bla + 1;\0return bla + 1;"
5476 "\0return bla + 1;\0return func(a);\0b+=1;\0return b;"),
5478 BPF_MOV64_IMM(BPF_REG_2, 1),
5479 BPF_JMP_IMM(BPF_JGE, BPF_REG_2, 0, 1),
5482 BPF_MOV64_IMM(BPF_REG_0, 0),
5484 BPF_MOV64_IMM(BPF_REG_0, 0),
5487 BPF_MOV64_REG(BPF_REG_0, 2),
5488 BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
5491 .prog_type = BPF_PROG_TYPE_TRACEPOINT,
5493 .func_info_rec_size = 8,
5494 .func_info = { {0, 4}, {6, 3}, {9, 5} },
5496 BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5497 BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 1, 10),
5498 BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 1, 10),
5499 BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 1, 10),
5500 BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
5501 BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 1, 10),
5502 BPF_LINE_INFO_ENC(8, 0, NAME_TBD, 1, 10),
5503 BPF_LINE_INFO_ENC(9, 0, NAME_TBD, 1, 10),
5504 BPF_LINE_INFO_ENC(10, 0, NAME_TBD, 1, 10),
5505 BPF_LINE_INFO_ENC(11, 0, NAME_TBD, 2, 9),
5508 .line_info_rec_size = sizeof(struct bpf_line_info),
5509 .nr_jited_ksyms = 2,
5511 .dead_code_mask = 0x70,
5513 .dead_func_mask = 0x2,
5517 .descr = "line_info (dead last subprog)",
5519 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
5520 BTF_FUNC_PROTO_ENC(1, 1), /* [2] */
5521 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5522 BTF_FUNC_ENC(NAME_TBD, 2), /* [3] */
5523 BTF_FUNC_ENC(NAME_TBD, 2), /* [5] */
5526 BTF_STR_SEC("\0int\0x\0dead\0main\0int a=1+1;\0/* live call */"
5527 "\0return 0;\0/* dead */\0/* dead */"),
5529 BPF_MOV64_IMM(BPF_REG_2, 1),
5530 BPF_JMP_IMM(BPF_JGE, BPF_REG_2, 0, 1),
5532 BPF_MOV64_IMM(BPF_REG_0, 0),
5534 BPF_MOV64_IMM(BPF_REG_0, 0),
5537 .prog_type = BPF_PROG_TYPE_TRACEPOINT,
5539 .func_info_rec_size = 8,
5540 .func_info = { {0, 4}, {5, 3} },
5542 BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5543 BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 1, 10),
5544 BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 1, 10),
5545 BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 1, 10),
5546 BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
5549 .line_info_rec_size = sizeof(struct bpf_line_info),
5550 .nr_jited_ksyms = 1,
5552 .dead_code_mask = 0x18,
5554 .dead_func_mask = 0x2,
5558 .descr = "line_info (dead subprog + dead start)",
5560 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
5561 BTF_FUNC_PROTO_ENC(1, 1), /* [2] */
5562 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5563 BTF_FUNC_ENC(NAME_TBD, 2), /* [3] */
5564 BTF_FUNC_ENC(NAME_TBD, 2), /* [4] */
5565 BTF_FUNC_ENC(NAME_TBD, 2), /* [5] */
5568 BTF_STR_SEC("\0int\0x\0dead\0main\0func\0int a=1+1;\0/* dead */"
5569 "\0return 0;\0return 0;\0return 0;"
5570 "\0/* dead */\0/* dead */\0/* dead */\0/* dead */"
5571 "\0return b + 1;\0return b + 1;\0return b + 1;"),
5573 BPF_JMP_IMM(BPF_JA, 0, 0, 0),
5574 BPF_MOV64_IMM(BPF_REG_2, 1),
5575 BPF_JMP_IMM(BPF_JGE, BPF_REG_2, 0, 1),
5578 BPF_MOV64_IMM(BPF_REG_0, 0),
5580 BPF_MOV64_IMM(BPF_REG_0, 0),
5583 BPF_JMP_IMM(BPF_JA, 0, 0, 0),
5584 BPF_MOV64_REG(BPF_REG_0, 2),
5585 BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
5588 .prog_type = BPF_PROG_TYPE_TRACEPOINT,
5590 .func_info_rec_size = 8,
5591 .func_info = { {0, 4}, {7, 3}, {10, 5} },
5593 BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5594 BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 1, 10),
5595 BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 1, 10),
5596 BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 1, 10),
5597 BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
5598 BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 1, 10),
5599 BPF_LINE_INFO_ENC(8, 0, NAME_TBD, 1, 10),
5600 BPF_LINE_INFO_ENC(9, 0, NAME_TBD, 1, 10),
5601 BPF_LINE_INFO_ENC(10, 0, NAME_TBD, 1, 10),
5602 BPF_LINE_INFO_ENC(11, 0, NAME_TBD, 2, 9),
5603 BPF_LINE_INFO_ENC(12, 0, NAME_TBD, 2, 9),
5604 BPF_LINE_INFO_ENC(13, 0, NAME_TBD, 2, 9),
5607 .line_info_rec_size = sizeof(struct bpf_line_info),
5608 .nr_jited_ksyms = 2,
5610 .dead_code_mask = 0x1e2,
5612 .dead_func_mask = 0x2,
5616 .descr = "line_info (dead subprog + dead start w/ move)",
5618 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
5619 BTF_FUNC_PROTO_ENC(1, 1), /* [2] */
5620 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5621 BTF_FUNC_ENC(NAME_TBD, 2), /* [3] */
5622 BTF_FUNC_ENC(NAME_TBD, 2), /* [4] */
5623 BTF_FUNC_ENC(NAME_TBD, 2), /* [5] */
5626 BTF_STR_SEC("\0int\0x\0dead\0main\0func\0int a=1+1;\0/* live call */"
5627 "\0return 0;\0return 0;\0/* dead */\0/* dead */"
5628 "\0/* dead */\0return bla + 1;\0return bla + 1;"
5629 "\0return bla + 1;\0return func(a);\0b+=1;\0return b;"),
5631 BPF_MOV64_IMM(BPF_REG_2, 1),
5632 BPF_JMP_IMM(BPF_JGE, BPF_REG_2, 0, 1),
5635 BPF_MOV64_IMM(BPF_REG_0, 0),
5637 BPF_MOV64_IMM(BPF_REG_0, 0),
5640 BPF_JMP_IMM(BPF_JA, 0, 0, 0),
5641 BPF_MOV64_REG(BPF_REG_0, 2),
5642 BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
5645 .prog_type = BPF_PROG_TYPE_TRACEPOINT,
5647 .func_info_rec_size = 8,
5648 .func_info = { {0, 4}, {6, 3}, {9, 5} },
5650 BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5651 BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 1, 10),
5652 BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 1, 10),
5653 BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 1, 10),
5654 BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
5655 BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 1, 10),
5656 BPF_LINE_INFO_ENC(8, 0, NAME_TBD, 1, 10),
5657 BPF_LINE_INFO_ENC(9, 0, NAME_TBD, 1, 10),
5658 BPF_LINE_INFO_ENC(11, 0, NAME_TBD, 1, 10),
5659 BPF_LINE_INFO_ENC(12, 0, NAME_TBD, 2, 9),
5662 .line_info_rec_size = sizeof(struct bpf_line_info),
5663 .nr_jited_ksyms = 2,
5665 .dead_code_mask = 0x70,
5667 .dead_func_mask = 0x2,
5671 .descr = "line_info (dead end + subprog start w/ no linfo)",
5673 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
5674 BTF_FUNC_PROTO_ENC(1, 1), /* [2] */
5675 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5676 BTF_FUNC_ENC(NAME_TBD, 2), /* [3] */
5677 BTF_FUNC_ENC(NAME_TBD, 2), /* [4] */
5680 BTF_STR_SEC("\0int\0x\0main\0func\0/* main linfo */\0/* func linfo */"),
5682 BPF_MOV64_IMM(BPF_REG_0, 0),
5683 BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 1, 3),
5685 BPF_MOV64_IMM(BPF_REG_0, 0),
5688 BPF_JMP_IMM(BPF_JA, 0, 0, 0),
5691 .prog_type = BPF_PROG_TYPE_TRACEPOINT,
5693 .func_info_rec_size = 8,
5694 .func_info = { {0, 3}, {6, 4}, },
5696 BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5697 BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
5700 .line_info_rec_size = sizeof(struct bpf_line_info),
5701 .nr_jited_ksyms = 2,
5706 static size_t probe_prog_length(const struct bpf_insn *fp)
5710 for (len = MAX_INSNS - 1; len > 0; --len)
5711 if (fp[len].code != 0 || fp[len].imm != 0)
5716 static __u32 *patch_name_tbd(const __u32 *raw_u32,
5717 const char *str, __u32 str_off,
5718 unsigned int str_sec_size,
5719 unsigned int *ret_size)
5721 int i, raw_u32_size = get_raw_sec_size(raw_u32);
5722 const char *end_str = str + str_sec_size;
5723 const char *next_str = str + str_off;
5724 __u32 *new_u32 = NULL;
5726 if (raw_u32_size == -1)
5727 return ERR_PTR(-EINVAL);
5729 if (!raw_u32_size) {
5734 new_u32 = malloc(raw_u32_size);
5736 return ERR_PTR(-ENOMEM);
5738 for (i = 0; i < raw_u32_size / sizeof(raw_u32[0]); i++) {
5739 if (raw_u32[i] == NAME_TBD) {
5740 next_str = get_next_str(next_str, end_str);
5741 if (CHECK(!next_str, "Error in getting next_str\n")) {
5743 return ERR_PTR(-EINVAL);
5745 new_u32[i] = next_str - str;
5746 next_str += strlen(next_str);
5748 new_u32[i] = raw_u32[i];
5752 *ret_size = raw_u32_size;
5756 static int test_get_finfo(const struct prog_info_raw_test *test,
5759 struct bpf_prog_info info = {};
5760 struct bpf_func_info *finfo;
5761 __u32 info_len, rec_size, i;
5762 void *func_info = NULL;
5766 /* get necessary lens */
5767 info_len = sizeof(struct bpf_prog_info);
5768 err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
5769 if (CHECK(err == -1, "invalid get info (1st) errno:%d", errno)) {
5770 fprintf(stderr, "%s\n", btf_log_buf);
5773 nr_func_info = test->func_info_cnt - test->dead_func_cnt;
5774 if (CHECK(info.nr_func_info != nr_func_info,
5775 "incorrect info.nr_func_info (1st) %d",
5776 info.nr_func_info)) {
5780 rec_size = info.func_info_rec_size;
5781 if (CHECK(rec_size != sizeof(struct bpf_func_info),
5782 "incorrect info.func_info_rec_size (1st) %d", rec_size)) {
5786 if (!info.nr_func_info)
5789 func_info = malloc(info.nr_func_info * rec_size);
5790 if (CHECK(!func_info, "out of memory"))
5793 /* reset info to only retrieve func_info related data */
5794 memset(&info, 0, sizeof(info));
5795 info.nr_func_info = nr_func_info;
5796 info.func_info_rec_size = rec_size;
5797 info.func_info = ptr_to_u64(func_info);
5798 err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
5799 if (CHECK(err == -1, "invalid get info (2nd) errno:%d", errno)) {
5800 fprintf(stderr, "%s\n", btf_log_buf);
5804 if (CHECK(info.nr_func_info != nr_func_info,
5805 "incorrect info.nr_func_info (2nd) %d",
5806 info.nr_func_info)) {
5810 if (CHECK(info.func_info_rec_size != rec_size,
5811 "incorrect info.func_info_rec_size (2nd) %d",
5812 info.func_info_rec_size)) {
5818 for (i = 0; i < nr_func_info; i++) {
5819 if (test->dead_func_mask & (1 << i))
5821 if (CHECK(finfo->type_id != test->func_info[i][1],
5822 "incorrect func_type %u expected %u",
5823 finfo->type_id, test->func_info[i][1])) {
5827 finfo = (void *)finfo + rec_size;
5837 static int test_get_linfo(const struct prog_info_raw_test *test,
5838 const void *patched_linfo,
5839 __u32 cnt, int prog_fd)
5841 __u32 i, info_len, nr_jited_ksyms, nr_jited_func_lens;
5842 __u64 *jited_linfo = NULL, *jited_ksyms = NULL;
5843 __u32 rec_size, jited_rec_size, jited_cnt;
5844 struct bpf_line_info *linfo = NULL;
5845 __u32 cur_func_len, ksyms_found;
5846 struct bpf_prog_info info = {};
5847 __u32 *jited_func_lens = NULL;
5848 __u64 cur_func_ksyms;
5853 rec_size = sizeof(*linfo);
5854 jited_rec_size = sizeof(*jited_linfo);
5855 if (test->nr_jited_ksyms)
5856 nr_jited_ksyms = test->nr_jited_ksyms;
5858 nr_jited_ksyms = test->func_info_cnt - test->dead_func_cnt;
5859 nr_jited_func_lens = nr_jited_ksyms;
5861 info_len = sizeof(struct bpf_prog_info);
5862 err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
5863 if (CHECK(err == -1, "err:%d errno:%d", err, errno)) {
5868 if (!info.jited_prog_len) {
5869 /* prog is not jited */
5872 nr_jited_func_lens = 1;
5875 if (CHECK(info.nr_line_info != cnt ||
5876 info.nr_jited_line_info != jited_cnt ||
5877 info.nr_jited_ksyms != nr_jited_ksyms ||
5878 info.nr_jited_func_lens != nr_jited_func_lens ||
5879 (!info.nr_line_info && info.nr_jited_line_info),
5880 "info: nr_line_info:%u(expected:%u) nr_jited_line_info:%u(expected:%u) nr_jited_ksyms:%u(expected:%u) nr_jited_func_lens:%u(expected:%u)",
5881 info.nr_line_info, cnt,
5882 info.nr_jited_line_info, jited_cnt,
5883 info.nr_jited_ksyms, nr_jited_ksyms,
5884 info.nr_jited_func_lens, nr_jited_func_lens)) {
5889 if (CHECK(info.line_info_rec_size != sizeof(struct bpf_line_info) ||
5890 info.jited_line_info_rec_size != sizeof(__u64),
5891 "info: line_info_rec_size:%u(userspace expected:%u) jited_line_info_rec_size:%u(userspace expected:%u)",
5892 info.line_info_rec_size, rec_size,
5893 info.jited_line_info_rec_size, jited_rec_size)) {
5901 rec_size = info.line_info_rec_size;
5902 jited_rec_size = info.jited_line_info_rec_size;
5904 memset(&info, 0, sizeof(info));
5906 linfo = calloc(cnt, rec_size);
5907 if (CHECK(!linfo, "!linfo")) {
5911 info.nr_line_info = cnt;
5912 info.line_info_rec_size = rec_size;
5913 info.line_info = ptr_to_u64(linfo);
5916 jited_linfo = calloc(jited_cnt, jited_rec_size);
5917 jited_ksyms = calloc(nr_jited_ksyms, sizeof(*jited_ksyms));
5918 jited_func_lens = calloc(nr_jited_func_lens,
5919 sizeof(*jited_func_lens));
5920 if (CHECK(!jited_linfo || !jited_ksyms || !jited_func_lens,
5921 "jited_linfo:%p jited_ksyms:%p jited_func_lens:%p",
5922 jited_linfo, jited_ksyms, jited_func_lens)) {
5927 info.nr_jited_line_info = jited_cnt;
5928 info.jited_line_info_rec_size = jited_rec_size;
5929 info.jited_line_info = ptr_to_u64(jited_linfo);
5930 info.nr_jited_ksyms = nr_jited_ksyms;
5931 info.jited_ksyms = ptr_to_u64(jited_ksyms);
5932 info.nr_jited_func_lens = nr_jited_func_lens;
5933 info.jited_func_lens = ptr_to_u64(jited_func_lens);
5936 err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
5939 * Only recheck the info.*line_info* fields.
5940 * Other fields are not the concern of this test.
5942 if (CHECK(err == -1 ||
5943 info.nr_line_info != cnt ||
5944 (jited_cnt && !info.jited_line_info) ||
5945 info.nr_jited_line_info != jited_cnt ||
5946 info.line_info_rec_size != rec_size ||
5947 info.jited_line_info_rec_size != jited_rec_size,
5948 "err:%d errno:%d info: nr_line_info:%u(expected:%u) nr_jited_line_info:%u(expected:%u) line_info_rec_size:%u(expected:%u) jited_linfo_rec_size:%u(expected:%u) line_info:%p jited_line_info:%p",
5950 info.nr_line_info, cnt,
5951 info.nr_jited_line_info, jited_cnt,
5952 info.line_info_rec_size, rec_size,
5953 info.jited_line_info_rec_size, jited_rec_size,
5954 (void *)(long)info.line_info,
5955 (void *)(long)info.jited_line_info)) {
5961 while (test->dead_code_mask & (1 << dead_insns))
5964 CHECK(linfo[0].insn_off, "linfo[0].insn_off:%u",
5966 for (i = 1; i < cnt; i++) {
5967 const struct bpf_line_info *expected_linfo;
5969 while (test->dead_code_mask & (1 << (i + dead_insns)))
5972 expected_linfo = patched_linfo +
5973 ((i + dead_insns) * test->line_info_rec_size);
5974 if (CHECK(linfo[i].insn_off <= linfo[i - 1].insn_off,
5975 "linfo[%u].insn_off:%u <= linfo[%u].insn_off:%u",
5976 i, linfo[i].insn_off,
5977 i - 1, linfo[i - 1].insn_off)) {
5981 if (CHECK(linfo[i].file_name_off != expected_linfo->file_name_off ||
5982 linfo[i].line_off != expected_linfo->line_off ||
5983 linfo[i].line_col != expected_linfo->line_col,
5984 "linfo[%u] (%u, %u, %u) != (%u, %u, %u)", i,
5985 linfo[i].file_name_off,
5988 expected_linfo->file_name_off,
5989 expected_linfo->line_off,
5990 expected_linfo->line_col)) {
5997 fprintf(stderr, "not jited. skipping jited_line_info check. ");
6002 if (CHECK(jited_linfo[0] != jited_ksyms[0],
6003 "jited_linfo[0]:%lx != jited_ksyms[0]:%lx",
6004 (long)(jited_linfo[0]), (long)(jited_ksyms[0]))) {
6010 cur_func_len = jited_func_lens[0];
6011 cur_func_ksyms = jited_ksyms[0];
6012 for (i = 1; i < jited_cnt; i++) {
6013 if (ksyms_found < nr_jited_ksyms &&
6014 jited_linfo[i] == jited_ksyms[ksyms_found]) {
6015 cur_func_ksyms = jited_ksyms[ksyms_found];
6016 cur_func_len = jited_ksyms[ksyms_found];
6021 if (CHECK(jited_linfo[i] <= jited_linfo[i - 1],
6022 "jited_linfo[%u]:%lx <= jited_linfo[%u]:%lx",
6023 i, (long)jited_linfo[i],
6024 i - 1, (long)(jited_linfo[i - 1]))) {
6029 if (CHECK(jited_linfo[i] - cur_func_ksyms > cur_func_len,
6030 "jited_linfo[%u]:%lx - %lx > %u",
6031 i, (long)jited_linfo[i], (long)cur_func_ksyms,
6038 if (CHECK(ksyms_found != nr_jited_ksyms,
6039 "ksyms_found:%u != nr_jited_ksyms:%u",
6040 ksyms_found, nr_jited_ksyms)) {
6051 free(jited_func_lens);
6055 static int do_test_info_raw(unsigned int test_num)
6057 const struct prog_info_raw_test *test = &info_raw_tests[test_num - 1];
6058 unsigned int raw_btf_size, linfo_str_off, linfo_size;
6059 int btf_fd = -1, prog_fd = -1, err = 0;
6060 void *raw_btf, *patched_linfo = NULL;
6061 const char *ret_next_str;
6062 union bpf_attr attr = {};
6064 fprintf(stderr, "BTF prog info raw test[%u] (%s): ", test_num, test->descr);
6065 raw_btf = btf_raw_create(&hdr_tmpl, test->raw_types,
6066 test->str_sec, test->str_sec_size,
6067 &raw_btf_size, &ret_next_str);
6072 *btf_log_buf = '\0';
6073 btf_fd = bpf_load_btf(raw_btf, raw_btf_size,
6074 btf_log_buf, BTF_LOG_BUF_SIZE,
6078 if (CHECK(btf_fd == -1, "invalid btf_fd errno:%d", errno)) {
6083 if (*btf_log_buf && args.always_log)
6084 fprintf(stderr, "\n%s", btf_log_buf);
6085 *btf_log_buf = '\0';
6087 linfo_str_off = ret_next_str - test->str_sec;
6088 patched_linfo = patch_name_tbd(test->line_info,
6089 test->str_sec, linfo_str_off,
6090 test->str_sec_size, &linfo_size);
6091 if (IS_ERR(patched_linfo)) {
6092 fprintf(stderr, "error in creating raw bpf_line_info");
6097 attr.prog_type = test->prog_type;
6098 attr.insns = ptr_to_u64(test->insns);
6099 attr.insn_cnt = probe_prog_length(test->insns);
6100 attr.license = ptr_to_u64("GPL");
6101 attr.prog_btf_fd = btf_fd;
6102 attr.func_info_rec_size = test->func_info_rec_size;
6103 attr.func_info_cnt = test->func_info_cnt;
6104 attr.func_info = ptr_to_u64(test->func_info);
6105 attr.log_buf = ptr_to_u64(btf_log_buf);
6106 attr.log_size = BTF_LOG_BUF_SIZE;
6109 attr.line_info_rec_size = test->line_info_rec_size;
6110 attr.line_info = ptr_to_u64(patched_linfo);
6111 attr.line_info_cnt = linfo_size / attr.line_info_rec_size;
6114 prog_fd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
6115 err = ((prog_fd == -1) != test->expected_prog_load_failure);
6116 if (CHECK(err, "prog_fd:%d expected_prog_load_failure:%u errno:%d",
6117 prog_fd, test->expected_prog_load_failure, errno) ||
6118 CHECK(test->err_str && !strstr(btf_log_buf, test->err_str),
6119 "expected err_str:%s", test->err_str)) {
6127 err = test_get_finfo(test, prog_fd);
6131 err = test_get_linfo(test, patched_linfo,
6132 attr.line_info_cnt - test->dead_code_cnt,
6139 fprintf(stderr, "OK");
6141 if (*btf_log_buf && (err || args.always_log))
6142 fprintf(stderr, "\n%s", btf_log_buf);
6149 if (!IS_ERR(patched_linfo))
6150 free(patched_linfo);
6155 static int test_info_raw(void)
6160 if (args.info_raw_test_num)
6161 return count_result(do_test_info_raw(args.info_raw_test_num));
6163 for (i = 1; i <= ARRAY_SIZE(info_raw_tests); i++)
6164 err |= count_result(do_test_info_raw(i));
6169 struct btf_raw_data {
6170 __u32 raw_types[MAX_NR_RAW_U32];
6171 const char *str_sec;
6175 struct btf_dedup_test {
6177 struct btf_raw_data input;
6178 struct btf_raw_data expect;
6179 struct btf_dedup_opts opts;
6182 const struct btf_dedup_test dedup_tests[] = {
6185 .descr = "dedup: unused strings filtering",
6188 BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 32, 4),
6189 BTF_TYPE_INT_ENC(NAME_NTH(5), BTF_INT_SIGNED, 0, 64, 8),
6192 BTF_STR_SEC("\0unused\0int\0foo\0bar\0long"),
6196 BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),
6197 BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 64, 8),
6200 BTF_STR_SEC("\0int\0long"),
6203 .dont_resolve_fwds = false,
6207 .descr = "dedup: strings deduplication",
6210 BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),
6211 BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 64, 8),
6212 BTF_TYPE_INT_ENC(NAME_NTH(3), BTF_INT_SIGNED, 0, 32, 4),
6213 BTF_TYPE_INT_ENC(NAME_NTH(4), BTF_INT_SIGNED, 0, 64, 8),
6214 BTF_TYPE_INT_ENC(NAME_NTH(5), BTF_INT_SIGNED, 0, 32, 4),
6217 BTF_STR_SEC("\0int\0long int\0int\0long int\0int"),
6221 BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),
6222 BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 64, 8),
6225 BTF_STR_SEC("\0int\0long int"),
6228 .dont_resolve_fwds = false,
6232 .descr = "dedup: struct example #1",
6244 BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4), /* [1] */
6246 BTF_TYPE_ARRAY_ENC(1, 1, 16), /* [2] */
6248 BTF_STRUCT_ENC(NAME_NTH(2), 4, 84), /* [3] */
6249 BTF_MEMBER_ENC(NAME_NTH(3), 4, 0), /* struct s *next; */
6250 BTF_MEMBER_ENC(NAME_NTH(4), 5, 64), /* const int *a; */
6251 BTF_MEMBER_ENC(NAME_NTH(5), 2, 128), /* int b[16]; */
6252 BTF_MEMBER_ENC(NAME_NTH(6), 1, 640), /* int c; */
6253 /* ptr -> [3] struct s */
6254 BTF_PTR_ENC(3), /* [4] */
6255 /* ptr -> [6] const int */
6256 BTF_PTR_ENC(6), /* [5] */
6257 /* const -> [1] int */
6258 BTF_CONST_ENC(1), /* [6] */
6260 /* full copy of the above */
6261 BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4), /* [7] */
6262 BTF_TYPE_ARRAY_ENC(7, 7, 16), /* [8] */
6263 BTF_STRUCT_ENC(NAME_NTH(2), 4, 84), /* [9] */
6264 BTF_MEMBER_ENC(NAME_NTH(3), 10, 0),
6265 BTF_MEMBER_ENC(NAME_NTH(4), 11, 64),
6266 BTF_MEMBER_ENC(NAME_NTH(5), 8, 128),
6267 BTF_MEMBER_ENC(NAME_NTH(6), 7, 640),
6268 BTF_PTR_ENC(9), /* [10] */
6269 BTF_PTR_ENC(12), /* [11] */
6270 BTF_CONST_ENC(7), /* [12] */
6273 BTF_STR_SEC("\0int\0s\0next\0a\0b\0c\0"),
6278 BTF_TYPE_INT_ENC(NAME_NTH(4), BTF_INT_SIGNED, 0, 32, 4), /* [1] */
6280 BTF_TYPE_ARRAY_ENC(1, 1, 16), /* [2] */
6282 BTF_STRUCT_ENC(NAME_NTH(6), 4, 84), /* [3] */
6283 BTF_MEMBER_ENC(NAME_NTH(5), 4, 0), /* struct s *next; */
6284 BTF_MEMBER_ENC(NAME_NTH(1), 5, 64), /* const int *a; */
6285 BTF_MEMBER_ENC(NAME_NTH(2), 2, 128), /* int b[16]; */
6286 BTF_MEMBER_ENC(NAME_NTH(3), 1, 640), /* int c; */
6287 /* ptr -> [3] struct s */
6288 BTF_PTR_ENC(3), /* [4] */
6289 /* ptr -> [6] const int */
6290 BTF_PTR_ENC(6), /* [5] */
6291 /* const -> [1] int */
6292 BTF_CONST_ENC(1), /* [6] */
6295 BTF_STR_SEC("\0a\0b\0c\0int\0next\0s"),
6298 .dont_resolve_fwds = false,
6302 .descr = "dedup: struct <-> fwd resolution w/ hash collision",
6318 BTF_FWD_ENC(NAME_TBD, 0 /* struct fwd */), /* [1] fwd x */
6319 BTF_PTR_ENC(1), /* [2] ptr -> [1] */
6320 BTF_STRUCT_ENC(NAME_TBD, 1, 8), /* [3] struct s */
6321 BTF_MEMBER_ENC(NAME_TBD, 2, 0),
6323 BTF_STRUCT_ENC(NAME_TBD, 0, 0), /* [4] struct x */
6324 BTF_PTR_ENC(4), /* [5] ptr -> [4] */
6325 BTF_STRUCT_ENC(NAME_TBD, 1, 8), /* [6] struct s */
6326 BTF_MEMBER_ENC(NAME_TBD, 5, 0),
6329 BTF_STR_SEC("\0x\0s\0x\0x\0s\0x\0"),
6333 BTF_PTR_ENC(3), /* [1] ptr -> [3] */
6334 BTF_STRUCT_ENC(NAME_TBD, 1, 8), /* [2] struct s */
6335 BTF_MEMBER_ENC(NAME_TBD, 1, 0),
6336 BTF_STRUCT_ENC(NAME_NTH(2), 0, 0), /* [3] struct x */
6339 BTF_STR_SEC("\0s\0x"),
6342 .dont_resolve_fwds = false,
6343 .dedup_table_size = 1, /* force hash collisions */
6347 .descr = "dedup: void equiv check",
6361 BTF_STRUCT_ENC(0, 0, 1), /* [1] struct {} */
6362 BTF_PTR_ENC(1), /* [2] ptr -> [1] */
6363 BTF_STRUCT_ENC(NAME_NTH(1), 1, 8), /* [3] struct s */
6364 BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
6366 BTF_PTR_ENC(0), /* [4] ptr -> void */
6367 BTF_STRUCT_ENC(NAME_NTH(1), 1, 8), /* [5] struct s */
6368 BTF_MEMBER_ENC(NAME_NTH(2), 4, 0),
6371 BTF_STR_SEC("\0s\0x"),
6376 BTF_STRUCT_ENC(0, 0, 1), /* [1] struct {} */
6377 BTF_PTR_ENC(1), /* [2] ptr -> [1] */
6378 BTF_STRUCT_ENC(NAME_NTH(1), 1, 8), /* [3] struct s */
6379 BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
6381 BTF_PTR_ENC(0), /* [4] ptr -> void */
6382 BTF_STRUCT_ENC(NAME_NTH(1), 1, 8), /* [5] struct s */
6383 BTF_MEMBER_ENC(NAME_NTH(2), 4, 0),
6386 BTF_STR_SEC("\0s\0x"),
6389 .dont_resolve_fwds = false,
6390 .dedup_table_size = 1, /* force hash collisions */
6394 .descr = "dedup: all possible kinds (no duplicates)",
6397 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 8), /* [1] int */
6398 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), 4), /* [2] enum */
6399 BTF_ENUM_ENC(NAME_TBD, 0),
6400 BTF_ENUM_ENC(NAME_TBD, 1),
6401 BTF_FWD_ENC(NAME_TBD, 1 /* union kind_flag */), /* [3] fwd */
6402 BTF_TYPE_ARRAY_ENC(2, 1, 7), /* [4] array */
6403 BTF_STRUCT_ENC(NAME_TBD, 1, 4), /* [5] struct */
6404 BTF_MEMBER_ENC(NAME_TBD, 1, 0),
6405 BTF_UNION_ENC(NAME_TBD, 1, 4), /* [6] union */
6406 BTF_MEMBER_ENC(NAME_TBD, 1, 0),
6407 BTF_TYPEDEF_ENC(NAME_TBD, 1), /* [7] typedef */
6408 BTF_PTR_ENC(0), /* [8] ptr */
6409 BTF_CONST_ENC(8), /* [9] const */
6410 BTF_VOLATILE_ENC(8), /* [10] volatile */
6411 BTF_RESTRICT_ENC(8), /* [11] restrict */
6412 BTF_FUNC_PROTO_ENC(1, 2), /* [12] func_proto */
6413 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
6414 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 8),
6415 BTF_FUNC_ENC(NAME_TBD, 12), /* [13] func */
6418 BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M"),
6422 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 8), /* [1] int */
6423 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), 4), /* [2] enum */
6424 BTF_ENUM_ENC(NAME_TBD, 0),
6425 BTF_ENUM_ENC(NAME_TBD, 1),
6426 BTF_FWD_ENC(NAME_TBD, 1 /* union kind_flag */), /* [3] fwd */
6427 BTF_TYPE_ARRAY_ENC(2, 1, 7), /* [4] array */
6428 BTF_STRUCT_ENC(NAME_TBD, 1, 4), /* [5] struct */
6429 BTF_MEMBER_ENC(NAME_TBD, 1, 0),
6430 BTF_UNION_ENC(NAME_TBD, 1, 4), /* [6] union */
6431 BTF_MEMBER_ENC(NAME_TBD, 1, 0),
6432 BTF_TYPEDEF_ENC(NAME_TBD, 1), /* [7] typedef */
6433 BTF_PTR_ENC(0), /* [8] ptr */
6434 BTF_CONST_ENC(8), /* [9] const */
6435 BTF_VOLATILE_ENC(8), /* [10] volatile */
6436 BTF_RESTRICT_ENC(8), /* [11] restrict */
6437 BTF_FUNC_PROTO_ENC(1, 2), /* [12] func_proto */
6438 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
6439 BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 8),
6440 BTF_FUNC_ENC(NAME_TBD, 12), /* [13] func */
6443 BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M"),
6446 .dont_resolve_fwds = false,
6450 .descr = "dedup: no int duplicates",
6453 BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 8),
6454 /* different name */
6455 BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 32, 8),
6456 /* different encoding */
6457 BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_CHAR, 0, 32, 8),
6458 BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_BOOL, 0, 32, 8),
6459 /* different bit offset */
6460 BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 8, 32, 8),
6461 /* different bit size */
6462 BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 27, 8),
6463 /* different byte size */
6464 BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),
6467 BTF_STR_SEC("\0int\0some other int"),
6471 BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 8),
6472 /* different name */
6473 BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 32, 8),
6474 /* different encoding */
6475 BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_CHAR, 0, 32, 8),
6476 BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_BOOL, 0, 32, 8),
6477 /* different bit offset */
6478 BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 8, 32, 8),
6479 /* different bit size */
6480 BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 27, 8),
6481 /* different byte size */
6482 BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),
6485 BTF_STR_SEC("\0int\0some other int"),
6488 .dont_resolve_fwds = false,
6492 .descr = "dedup: enum fwd resolution",
6495 /* [1] fwd enum 'e1' before full enum */
6496 BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 0), 4),
6497 /* [2] full enum 'e1' after fwd */
6498 BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
6499 BTF_ENUM_ENC(NAME_NTH(2), 123),
6500 /* [3] full enum 'e2' before fwd */
6501 BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
6502 BTF_ENUM_ENC(NAME_NTH(4), 456),
6503 /* [4] fwd enum 'e2' after full enum */
6504 BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 0), 4),
6505 /* [5] incompatible fwd enum with different size */
6506 BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 0), 1),
6507 /* [6] incompatible full enum with different value */
6508 BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
6509 BTF_ENUM_ENC(NAME_NTH(2), 321),
6512 BTF_STR_SEC("\0e1\0e1_val\0e2\0e2_val"),
6516 /* [1] full enum 'e1' */
6517 BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
6518 BTF_ENUM_ENC(NAME_NTH(2), 123),
6519 /* [2] full enum 'e2' */
6520 BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
6521 BTF_ENUM_ENC(NAME_NTH(4), 456),
6522 /* [3] incompatible fwd enum with different size */
6523 BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 0), 1),
6524 /* [4] incompatible full enum with different value */
6525 BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
6526 BTF_ENUM_ENC(NAME_NTH(2), 321),
6529 BTF_STR_SEC("\0e1\0e1_val\0e2\0e2_val"),
6532 .dont_resolve_fwds = false,
6536 .descr = "dedup: datasec and vars pass-through",
6540 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
6542 BTF_VAR_ENC(NAME_NTH(2), 1, 0), /* [2] */
6543 /* .bss section */ /* [3] */
6544 BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
6545 BTF_VAR_SECINFO_ENC(2, 0, 4),
6546 /* int, referenced from [5] */
6547 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [4] */
6548 /* another static int t */
6549 BTF_VAR_ENC(NAME_NTH(2), 4, 0), /* [5] */
6550 /* another .bss section */ /* [6] */
6551 BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
6552 BTF_VAR_SECINFO_ENC(5, 0, 4),
6555 BTF_STR_SEC("\0.bss\0t"),
6560 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
6562 BTF_VAR_ENC(NAME_NTH(2), 1, 0), /* [2] */
6563 /* .bss section */ /* [3] */
6564 BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
6565 BTF_VAR_SECINFO_ENC(2, 0, 4),
6566 /* another static int t */
6567 BTF_VAR_ENC(NAME_NTH(2), 1, 0), /* [4] */
6568 /* another .bss section */ /* [5] */
6569 BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
6570 BTF_VAR_SECINFO_ENC(4, 0, 4),
6573 BTF_STR_SEC("\0.bss\0t"),
6576 .dont_resolve_fwds = false,
6577 .dedup_table_size = 1
6583 static int btf_type_size(const struct btf_type *t)
6585 int base_size = sizeof(struct btf_type);
6586 __u16 vlen = BTF_INFO_VLEN(t->info);
6587 __u16 kind = BTF_INFO_KIND(t->info);
6591 case BTF_KIND_CONST:
6592 case BTF_KIND_VOLATILE:
6593 case BTF_KIND_RESTRICT:
6595 case BTF_KIND_TYPEDEF:
6599 return base_size + sizeof(__u32);
6601 return base_size + vlen * sizeof(struct btf_enum);
6602 case BTF_KIND_ARRAY:
6603 return base_size + sizeof(struct btf_array);
6604 case BTF_KIND_STRUCT:
6605 case BTF_KIND_UNION:
6606 return base_size + vlen * sizeof(struct btf_member);
6607 case BTF_KIND_FUNC_PROTO:
6608 return base_size + vlen * sizeof(struct btf_param);
6610 return base_size + sizeof(struct btf_var);
6611 case BTF_KIND_DATASEC:
6612 return base_size + vlen * sizeof(struct btf_var_secinfo);
6614 fprintf(stderr, "Unsupported BTF_KIND:%u\n", kind);
6619 static void dump_btf_strings(const char *strs, __u32 len)
6621 const char *cur = strs;
6624 while (cur < strs + len) {
6625 fprintf(stderr, "string #%d: '%s'\n", i, cur);
6626 cur += strlen(cur) + 1;
6631 static int do_test_dedup(unsigned int test_num)
6633 const struct btf_dedup_test *test = &dedup_tests[test_num - 1];
6634 __u32 test_nr_types, expect_nr_types, test_btf_size, expect_btf_size;
6635 const struct btf_header *test_hdr, *expect_hdr;
6636 struct btf *test_btf = NULL, *expect_btf = NULL;
6637 const void *test_btf_data, *expect_btf_data;
6638 const char *ret_test_next_str, *ret_expect_next_str;
6639 const char *test_strs, *expect_strs;
6640 const char *test_str_cur, *test_str_end;
6641 const char *expect_str_cur, *expect_str_end;
6642 unsigned int raw_btf_size;
6646 fprintf(stderr, "BTF dedup test[%u] (%s):", test_num, test->descr);
6648 raw_btf = btf_raw_create(&hdr_tmpl, test->input.raw_types,
6649 test->input.str_sec, test->input.str_sec_size,
6650 &raw_btf_size, &ret_test_next_str);
6653 test_btf = btf__new((__u8 *)raw_btf, raw_btf_size);
6655 if (CHECK(IS_ERR(test_btf), "invalid test_btf errno:%ld",
6656 PTR_ERR(test_btf))) {
6661 raw_btf = btf_raw_create(&hdr_tmpl, test->expect.raw_types,
6662 test->expect.str_sec,
6663 test->expect.str_sec_size,
6664 &raw_btf_size, &ret_expect_next_str);
6667 expect_btf = btf__new((__u8 *)raw_btf, raw_btf_size);
6669 if (CHECK(IS_ERR(expect_btf), "invalid expect_btf errno:%ld",
6670 PTR_ERR(expect_btf))) {
6675 err = btf__dedup(test_btf, NULL, &test->opts);
6676 if (CHECK(err, "btf_dedup failed errno:%d", err)) {
6681 test_btf_data = btf__get_raw_data(test_btf, &test_btf_size);
6682 expect_btf_data = btf__get_raw_data(expect_btf, &expect_btf_size);
6683 if (CHECK(test_btf_size != expect_btf_size,
6684 "test_btf_size:%u != expect_btf_size:%u",
6685 test_btf_size, expect_btf_size)) {
6690 test_hdr = test_btf_data;
6691 test_strs = test_btf_data + sizeof(*test_hdr) + test_hdr->str_off;
6692 expect_hdr = expect_btf_data;
6693 expect_strs = expect_btf_data + sizeof(*test_hdr) + expect_hdr->str_off;
6694 if (CHECK(test_hdr->str_len != expect_hdr->str_len,
6695 "test_hdr->str_len:%u != expect_hdr->str_len:%u",
6696 test_hdr->str_len, expect_hdr->str_len)) {
6697 fprintf(stderr, "\ntest strings:\n");
6698 dump_btf_strings(test_strs, test_hdr->str_len);
6699 fprintf(stderr, "\nexpected strings:\n");
6700 dump_btf_strings(expect_strs, expect_hdr->str_len);
6705 test_str_cur = test_strs;
6706 test_str_end = test_strs + test_hdr->str_len;
6707 expect_str_cur = expect_strs;
6708 expect_str_end = expect_strs + expect_hdr->str_len;
6709 while (test_str_cur < test_str_end && expect_str_cur < expect_str_end) {
6710 size_t test_len, expect_len;
6712 test_len = strlen(test_str_cur);
6713 expect_len = strlen(expect_str_cur);
6714 if (CHECK(test_len != expect_len,
6715 "test_len:%zu != expect_len:%zu "
6716 "(test_str:%s, expect_str:%s)",
6717 test_len, expect_len, test_str_cur, expect_str_cur)) {
6721 if (CHECK(strcmp(test_str_cur, expect_str_cur),
6722 "test_str:%s != expect_str:%s",
6723 test_str_cur, expect_str_cur)) {
6727 test_str_cur += test_len + 1;
6728 expect_str_cur += expect_len + 1;
6730 if (CHECK(test_str_cur != test_str_end,
6731 "test_str_cur:%p != test_str_end:%p",
6732 test_str_cur, test_str_end)) {
6737 test_nr_types = btf__get_nr_types(test_btf);
6738 expect_nr_types = btf__get_nr_types(expect_btf);
6739 if (CHECK(test_nr_types != expect_nr_types,
6740 "test_nr_types:%u != expect_nr_types:%u",
6741 test_nr_types, expect_nr_types)) {
6746 for (i = 1; i <= test_nr_types; i++) {
6747 const struct btf_type *test_type, *expect_type;
6748 int test_size, expect_size;
6750 test_type = btf__type_by_id(test_btf, i);
6751 expect_type = btf__type_by_id(expect_btf, i);
6752 test_size = btf_type_size(test_type);
6753 expect_size = btf_type_size(expect_type);
6755 if (CHECK(test_size != expect_size,
6756 "type #%d: test_size:%d != expect_size:%u",
6757 i, test_size, expect_size)) {
6761 if (CHECK(memcmp((void *)test_type,
6762 (void *)expect_type,
6764 "type #%d: contents differ", i)) {
6772 fprintf(stderr, "OK");
6773 if (!IS_ERR(test_btf))
6774 btf__free(test_btf);
6775 if (!IS_ERR(expect_btf))
6776 btf__free(expect_btf);
6781 static int test_dedup(void)
6786 if (args.dedup_test_num)
6787 return count_result(do_test_dedup(args.dedup_test_num));
6789 for (i = 1; i <= ARRAY_SIZE(dedup_tests); i++)
6790 err |= count_result(do_test_dedup(i));
6795 static void usage(const char *cmd)
6797 fprintf(stderr, "Usage: %s [-l] [[-r btf_raw_test_num (1 - %zu)] |\n"
6798 "\t[-g btf_get_info_test_num (1 - %zu)] |\n"
6799 "\t[-f btf_file_test_num (1 - %zu)] |\n"
6800 "\t[-k btf_prog_info_raw_test_num (1 - %zu)] |\n"
6801 "\t[-p (pretty print test)] |\n"
6802 "\t[-d btf_dedup_test_num (1 - %zu)]]\n",
6803 cmd, ARRAY_SIZE(raw_tests), ARRAY_SIZE(get_info_tests),
6804 ARRAY_SIZE(file_tests), ARRAY_SIZE(info_raw_tests),
6805 ARRAY_SIZE(dedup_tests));
6808 static int parse_args(int argc, char **argv)
6810 const char *optstr = "hlpk:f:r:g:d:";
6813 while ((opt = getopt(argc, argv, optstr)) != -1) {
6816 args.always_log = true;
6819 args.file_test_num = atoi(optarg);
6820 args.file_test = true;
6823 args.raw_test_num = atoi(optarg);
6824 args.raw_test = true;
6827 args.get_info_test_num = atoi(optarg);
6828 args.get_info_test = true;
6831 args.pprint_test = true;
6834 args.info_raw_test_num = atoi(optarg);
6835 args.info_raw_test = true;
6838 args.dedup_test_num = atoi(optarg);
6839 args.dedup_test = true;
6850 if (args.raw_test_num &&
6851 (args.raw_test_num < 1 ||
6852 args.raw_test_num > ARRAY_SIZE(raw_tests))) {
6853 fprintf(stderr, "BTF raw test number must be [1 - %zu]\n",
6854 ARRAY_SIZE(raw_tests));
6858 if (args.file_test_num &&
6859 (args.file_test_num < 1 ||
6860 args.file_test_num > ARRAY_SIZE(file_tests))) {
6861 fprintf(stderr, "BTF file test number must be [1 - %zu]\n",
6862 ARRAY_SIZE(file_tests));
6866 if (args.get_info_test_num &&
6867 (args.get_info_test_num < 1 ||
6868 args.get_info_test_num > ARRAY_SIZE(get_info_tests))) {
6869 fprintf(stderr, "BTF get info test number must be [1 - %zu]\n",
6870 ARRAY_SIZE(get_info_tests));
6874 if (args.info_raw_test_num &&
6875 (args.info_raw_test_num < 1 ||
6876 args.info_raw_test_num > ARRAY_SIZE(info_raw_tests))) {
6877 fprintf(stderr, "BTF prog info raw test number must be [1 - %zu]\n",
6878 ARRAY_SIZE(info_raw_tests));
6882 if (args.dedup_test_num &&
6883 (args.dedup_test_num < 1 ||
6884 args.dedup_test_num > ARRAY_SIZE(dedup_tests))) {
6885 fprintf(stderr, "BTF dedup test number must be [1 - %zu]\n",
6886 ARRAY_SIZE(dedup_tests));
6893 static void print_summary(void)
6895 fprintf(stderr, "PASS:%u SKIP:%u FAIL:%u\n",
6896 pass_cnt - skip_cnt, skip_cnt, error_cnt);
6899 int main(int argc, char **argv)
6903 err = parse_args(argc, argv);
6907 if (args.always_log)
6908 libbpf_set_print(__base_pr);
6913 if (args.get_info_test)
6914 err |= test_get_info();
6919 if (args.pprint_test)
6920 err |= test_pprint();
6922 if (args.info_raw_test)
6923 err |= test_info_raw();
6925 if (args.dedup_test)
6926 err |= test_dedup();
6928 if (args.raw_test || args.get_info_test || args.file_test ||
6929 args.pprint_test || args.info_raw_test || args.dedup_test)
6933 err |= test_get_info();
6935 err |= test_info_raw();
6936 err |= test_dedup();