1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2018 Facebook */
7 #include <linux/kernel.h>
9 #include <sys/resource.h>
19 #include <bpf/libbpf.h>
22 #include "bpf_rlimit.h"
25 static uint32_t pass_cnt;
26 static uint32_t error_cnt;
27 static uint32_t skip_cnt;
29 #define CHECK(condition, format...) ({ \
30 int __ret = !!(condition); \
32 fprintf(stderr, "%s:%d:FAIL ", __func__, __LINE__); \
33 fprintf(stderr, format); \
38 static int count_result(int err)
45 fprintf(stderr, "\n");
49 #define __printf(a, b) __attribute__((format(printf, a, b)))
52 static int __base_pr(const char *format, ...)
57 va_start(args, format);
58 err = vfprintf(stderr, format, args);
63 #define BTF_INFO_ENC(kind, root, vlen) \
64 ((!!(root) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))
66 #define BTF_TYPE_ENC(name, info, size_or_type) \
67 (name), (info), (size_or_type)
69 #define BTF_INT_ENC(encoding, bits_offset, nr_bits) \
70 ((encoding) << 24 | (bits_offset) << 16 | (nr_bits))
71 #define BTF_TYPE_INT_ENC(name, encoding, bits_offset, bits, sz) \
72 BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), sz), \
73 BTF_INT_ENC(encoding, bits_offset, bits)
75 #define BTF_ARRAY_ENC(type, index_type, nr_elems) \
76 (type), (index_type), (nr_elems)
77 #define BTF_TYPE_ARRAY_ENC(type, index_type, nr_elems) \
78 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ARRAY, 0, 0), 0), \
79 BTF_ARRAY_ENC(type, index_type, nr_elems)
81 #define BTF_MEMBER_ENC(name, type, bits_offset) \
82 (name), (type), (bits_offset)
83 #define BTF_ENUM_ENC(name, val) (name), (val)
85 #define BTF_TYPEDEF_ENC(name, type) \
86 BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), type)
88 #define BTF_PTR_ENC(name, type) \
89 BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), type)
91 #define BTF_END_RAW 0xdeadbeef
92 #define NAME_TBD 0xdeadb33f
94 #define MAX_NR_RAW_TYPES 1024
95 #define BTF_LOG_BUF_SIZE 65535
98 unsigned int raw_test_num;
99 unsigned int file_test_num;
100 unsigned int get_info_test_num;
108 static char btf_log_buf[BTF_LOG_BUF_SIZE];
110 static struct btf_header hdr_tmpl = {
112 .version = BTF_VERSION,
113 .hdr_len = sizeof(struct btf_header),
116 struct btf_raw_test {
119 const char *map_name;
121 __u32 raw_types[MAX_NR_RAW_TYPES];
123 enum bpf_map_type map_type;
140 static struct btf_raw_test raw_tests[] = {
147 * unsigned long long m;
157 .descr = "struct test #1",
160 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
161 /* unsigned long long */
162 BTF_TYPE_INT_ENC(0, 0, 0, 64, 8), /* [2] */
164 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1), /* [3] */
166 BTF_TYPE_ARRAY_ENC(1, 1, 8), /* [4] */
167 /* struct A { */ /* [5] */
168 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 6), 180),
169 BTF_MEMBER_ENC(NAME_TBD, 2, 0), /* unsigned long long m;*/
170 BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n; */
171 BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o; */
172 BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8] */
173 BTF_MEMBER_ENC(NAME_TBD, 6, 384),/* int q[4][8] */
174 BTF_MEMBER_ENC(NAME_TBD, 7, 1408), /* enum E r */
177 BTF_TYPE_ARRAY_ENC(4, 1, 4), /* [6] */
178 /* enum E */ /* [7] */
179 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), sizeof(int)),
180 BTF_ENUM_ENC(NAME_TBD, 0),
181 BTF_ENUM_ENC(NAME_TBD, 1),
184 .str_sec = "\0A\0m\0n\0o\0p\0q\0r\0E\0E0\0E1",
185 .str_sec_size = sizeof("\0A\0m\0n\0o\0p\0q\0r\0E\0E0\0E1"),
186 .map_type = BPF_MAP_TYPE_ARRAY,
187 .map_name = "struct_test1_map",
188 .key_size = sizeof(int),
195 /* typedef struct b Struct_B;
200 * const Struct_B o[4];
209 .descr = "struct test #2",
212 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
213 /* struct b [4] */ /* [2] */
214 BTF_TYPE_ARRAY_ENC(4, 1, 4),
216 /* struct A { */ /* [3] */
217 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 3), 68),
218 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
219 BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* struct B n[4] */
220 BTF_MEMBER_ENC(NAME_TBD, 8, 288),/* const Struct_B o[4];*/
223 /* struct B { */ /* [4] */
224 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
225 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
226 BTF_MEMBER_ENC(NAME_TBD, 1, 32),/* int n; */
229 /* const int */ /* [5] */
230 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 1),
231 /* typedef struct b Struct_B */ /* [6] */
232 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), 4),
233 /* const Struct_B */ /* [7] */
234 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 6),
235 /* const Struct_B [4] */ /* [8] */
236 BTF_TYPE_ARRAY_ENC(7, 1, 4),
239 .str_sec = "\0A\0m\0n\0o\0B\0m\0n\0Struct_B",
240 .str_sec_size = sizeof("\0A\0m\0n\0o\0B\0m\0n\0Struct_B"),
241 .map_type = BPF_MAP_TYPE_ARRAY,
242 .map_name = "struct_test2_map",
243 .key_size = sizeof(int),
251 .descr = "struct test #3 Invalid member offset",
254 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
255 /* int64 */ /* [2] */
256 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 64, 8),
258 /* struct A { */ /* [3] */
259 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 16),
260 BTF_MEMBER_ENC(NAME_TBD, 1, 64), /* int m; */
261 BTF_MEMBER_ENC(NAME_TBD, 2, 0), /* int64 n; */
265 .str_sec = "\0A\0m\0n\0",
266 .str_sec_size = sizeof("\0A\0m\0n\0"),
267 .map_type = BPF_MAP_TYPE_ARRAY,
268 .map_name = "struct_test3_map",
269 .key_size = sizeof(int),
274 .btf_load_err = true,
275 .err_str = "Invalid member bits_offset",
278 /* Test member exceeds the size of struct.
286 .descr = "size check test #1",
289 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
290 /* struct A { */ /* [2] */
291 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 2 - 1),
292 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
293 BTF_MEMBER_ENC(NAME_TBD, 1, 32),/* int n; */
297 .str_sec = "\0A\0m\0n",
298 .str_sec_size = sizeof("\0A\0m\0n"),
299 .map_type = BPF_MAP_TYPE_ARRAY,
300 .map_name = "size_check1_map",
301 .key_size = sizeof(int),
306 .btf_load_err = true,
307 .err_str = "Member exceeds struct_size",
310 /* Test member exeeds the size of struct
318 .descr = "size check test #2",
321 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
322 /* int[2] */ /* [2] */
323 BTF_TYPE_ARRAY_ENC(1, 1, 2),
324 /* struct A { */ /* [3] */
325 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 3 - 1),
326 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
327 BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* int n[2]; */
331 .str_sec = "\0A\0m\0n",
332 .str_sec_size = sizeof("\0A\0m\0n"),
333 .map_type = BPF_MAP_TYPE_ARRAY,
334 .map_name = "size_check2_map",
335 .key_size = sizeof(int),
340 .btf_load_err = true,
341 .err_str = "Member exceeds struct_size",
344 /* Test member exeeds the size of struct
352 .descr = "size check test #3",
355 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
356 /* void* */ /* [2] */
357 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
358 /* struct A { */ /* [3] */
359 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) + sizeof(void *) - 1),
360 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
361 BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* void *n; */
365 .str_sec = "\0A\0m\0n",
366 .str_sec_size = sizeof("\0A\0m\0n"),
367 .map_type = BPF_MAP_TYPE_ARRAY,
368 .map_name = "size_check3_map",
369 .key_size = sizeof(int),
374 .btf_load_err = true,
375 .err_str = "Member exceeds struct_size",
378 /* Test member exceeds the size of struct
391 .descr = "size check test #4",
394 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
395 /* enum E { */ /* [2] */
396 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), sizeof(int)),
397 BTF_ENUM_ENC(NAME_TBD, 0),
398 BTF_ENUM_ENC(NAME_TBD, 1),
400 /* struct A { */ /* [3] */
401 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 2 - 1),
402 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
403 BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* enum E n; */
407 .str_sec = "\0E\0E0\0E1\0A\0m\0n",
408 .str_sec_size = sizeof("\0E\0E0\0E1\0A\0m\0n"),
409 .map_type = BPF_MAP_TYPE_ARRAY,
410 .map_name = "size_check4_map",
411 .key_size = sizeof(int),
416 .btf_load_err = true,
417 .err_str = "Member exceeds struct_size",
420 /* typedef const void * const_void_ptr;
426 .descr = "void test #1",
429 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
430 /* const void */ /* [2] */
431 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
432 /* const void* */ /* [3] */
433 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 2),
434 /* typedef const void * const_void_ptr */
435 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 3),
436 /* struct A { */ /* [4] */
437 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
438 /* const_void_ptr m; */
439 BTF_MEMBER_ENC(NAME_TBD, 3, 0),
443 .str_sec = "\0const_void_ptr\0A\0m",
444 .str_sec_size = sizeof("\0const_void_ptr\0A\0m"),
445 .map_type = BPF_MAP_TYPE_ARRAY,
446 .map_name = "void_test1_map",
447 .key_size = sizeof(int),
448 .value_size = sizeof(void *),
459 .descr = "void test #2",
462 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
463 /* const void */ /* [2] */
464 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
465 /* struct A { */ /* [3] */
466 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 8),
468 BTF_MEMBER_ENC(NAME_TBD, 2, 0),
473 .str_sec_size = sizeof("\0A\0m"),
474 .map_type = BPF_MAP_TYPE_ARRAY,
475 .map_name = "void_test2_map",
476 .key_size = sizeof(int),
477 .value_size = sizeof(void *),
481 .btf_load_err = true,
482 .err_str = "Invalid member",
485 /* typedef const void * const_void_ptr;
489 .descr = "void test #3",
492 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
493 /* const void */ /* [2] */
494 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
495 /* const void* */ /* [3] */
496 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 2),
497 /* typedef const void * const_void_ptr */ /* [4] */
498 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 3),
499 /* const_void_ptr[4] */ /* [5] */
500 BTF_TYPE_ARRAY_ENC(3, 1, 4),
503 .str_sec = "\0const_void_ptr",
504 .str_sec_size = sizeof("\0const_void_ptr"),
505 .map_type = BPF_MAP_TYPE_ARRAY,
506 .map_name = "void_test3_map",
507 .key_size = sizeof(int),
508 .value_size = sizeof(void *) * 4,
516 .descr = "void test #4",
519 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
520 /* const void */ /* [2] */
521 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
522 /* const void[4] */ /* [3] */
523 BTF_TYPE_ARRAY_ENC(2, 1, 4),
527 .str_sec_size = sizeof("\0A\0m"),
528 .map_type = BPF_MAP_TYPE_ARRAY,
529 .map_name = "void_test4_map",
530 .key_size = sizeof(int),
531 .value_size = sizeof(void *) * 4,
535 .btf_load_err = true,
536 .err_str = "Invalid elem",
539 /* Array_A <------------------+
540 * elem_type == Array_B |
543 * Array_B <-------- + |
544 * elem_type == Array A --+
547 .descr = "loop test #1",
550 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
551 /* Array_A */ /* [2] */
552 BTF_TYPE_ARRAY_ENC(3, 1, 8),
553 /* Array_B */ /* [3] */
554 BTF_TYPE_ARRAY_ENC(2, 1, 8),
558 .str_sec_size = sizeof(""),
559 .map_type = BPF_MAP_TYPE_ARRAY,
560 .map_name = "loop_test1_map",
561 .key_size = sizeof(int),
562 .value_size = sizeof(sizeof(int) * 8),
566 .btf_load_err = true,
567 .err_str = "Loop detected",
570 /* typedef is _before_ the BTF type of Array_A and Array_B
572 * typedef Array_B int_array;
574 * Array_A <------------------+
575 * elem_type == int_array |
578 * Array_B <-------- + |
579 * elem_type == Array_A --+
582 .descr = "loop test #2",
585 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
586 /* typedef Array_B int_array */
587 BTF_TYPEDEF_ENC(1, 4), /* [2] */
589 BTF_TYPE_ARRAY_ENC(2, 1, 8), /* [3] */
591 BTF_TYPE_ARRAY_ENC(3, 1, 8), /* [4] */
594 .str_sec = "\0int_array\0",
595 .str_sec_size = sizeof("\0int_array"),
596 .map_type = BPF_MAP_TYPE_ARRAY,
597 .map_name = "loop_test2_map",
598 .key_size = sizeof(int),
599 .value_size = sizeof(sizeof(int) * 8),
603 .btf_load_err = true,
604 .err_str = "Loop detected",
607 /* Array_A <------------------+
608 * elem_type == Array_B |
611 * Array_B <-------- + |
612 * elem_type == Array_A --+
615 .descr = "loop test #3",
618 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
619 /* Array_A */ /* [2] */
620 BTF_TYPE_ARRAY_ENC(3, 1, 8),
621 /* Array_B */ /* [3] */
622 BTF_TYPE_ARRAY_ENC(2, 1, 8),
626 .str_sec_size = sizeof(""),
627 .map_type = BPF_MAP_TYPE_ARRAY,
628 .map_name = "loop_test3_map",
629 .key_size = sizeof(int),
630 .value_size = sizeof(sizeof(int) * 8),
634 .btf_load_err = true,
635 .err_str = "Loop detected",
638 /* typedef is _between_ the BTF type of Array_A and Array_B
640 * typedef Array_B int_array;
642 * Array_A <------------------+
643 * elem_type == int_array |
646 * Array_B <-------- + |
647 * elem_type == Array_A --+
650 .descr = "loop test #4",
653 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
654 /* Array_A */ /* [2] */
655 BTF_TYPE_ARRAY_ENC(3, 1, 8),
656 /* typedef Array_B int_array */ /* [3] */
657 BTF_TYPEDEF_ENC(NAME_TBD, 4),
658 /* Array_B */ /* [4] */
659 BTF_TYPE_ARRAY_ENC(2, 1, 8),
662 .str_sec = "\0int_array\0",
663 .str_sec_size = sizeof("\0int_array"),
664 .map_type = BPF_MAP_TYPE_ARRAY,
665 .map_name = "loop_test4_map",
666 .key_size = sizeof(int),
667 .value_size = sizeof(sizeof(int) * 8),
671 .btf_load_err = true,
672 .err_str = "Loop detected",
675 /* typedef struct B Struct_B
688 .descr = "loop test #5",
691 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
692 /* struct A */ /* [2] */
693 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
694 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int x; */
695 BTF_MEMBER_ENC(NAME_TBD, 3, 32),/* Struct_B y; */
696 /* typedef struct B Struct_B */
697 BTF_TYPEDEF_ENC(NAME_TBD, 4), /* [3] */
698 /* struct B */ /* [4] */
699 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
700 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int x; */
701 BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* struct A y; */
704 .str_sec = "\0A\0x\0y\0Struct_B\0B\0x\0y",
705 .str_sec_size = sizeof("\0A\0x\0y\0Struct_B\0B\0x\0y"),
706 .map_type = BPF_MAP_TYPE_ARRAY,
707 .map_name = "loop_test5_map",
708 .key_size = sizeof(int),
713 .btf_load_err = true,
714 .err_str = "Loop detected",
719 * struct A array_a[4];
723 .descr = "loop test #6",
726 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
727 BTF_TYPE_ARRAY_ENC(3, 1, 4), /* [2] */
728 /* struct A */ /* [3] */
729 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
730 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int x; */
731 BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* struct A array_a[4]; */
734 .str_sec = "\0A\0x\0y",
735 .str_sec_size = sizeof("\0A\0x\0y"),
736 .map_type = BPF_MAP_TYPE_ARRAY,
737 .map_name = "loop_test6_map",
738 .key_size = sizeof(int),
743 .btf_load_err = true,
744 .err_str = "Loop detected",
748 .descr = "loop test #7",
751 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
752 /* struct A { */ /* [2] */
753 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
755 BTF_MEMBER_ENC(NAME_TBD, 3, 0),
756 /* CONST type_id=3 */ /* [3] */
757 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 4),
758 /* PTR type_id=2 */ /* [4] */
759 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 3),
763 .str_sec_size = sizeof("\0A\0m"),
764 .map_type = BPF_MAP_TYPE_ARRAY,
765 .map_name = "loop_test7_map",
766 .key_size = sizeof(int),
767 .value_size = sizeof(void *),
771 .btf_load_err = true,
772 .err_str = "Loop detected",
776 .descr = "loop test #8",
779 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
780 /* struct A { */ /* [2] */
781 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
783 BTF_MEMBER_ENC(NAME_TBD, 4, 0),
784 /* struct B { */ /* [3] */
785 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
787 BTF_MEMBER_ENC(NAME_TBD, 6, 0),
788 /* CONST type_id=5 */ /* [4] */
789 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 5),
790 /* PTR type_id=6 */ /* [5] */
791 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 6),
792 /* CONST type_id=7 */ /* [6] */
793 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 7),
794 /* PTR type_id=4 */ /* [7] */
795 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 4),
798 .str_sec = "\0A\0m\0B\0n",
799 .str_sec_size = sizeof("\0A\0m\0B\0n"),
800 .map_type = BPF_MAP_TYPE_ARRAY,
801 .map_name = "loop_test8_map",
802 .key_size = sizeof(int),
803 .value_size = sizeof(void *),
807 .btf_load_err = true,
808 .err_str = "Loop detected",
812 .descr = "string section does not end with null",
815 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
819 .str_sec_size = sizeof("\0int") - 1,
820 .map_type = BPF_MAP_TYPE_ARRAY,
821 .map_name = "hdr_test_map",
822 .key_size = sizeof(int),
823 .value_size = sizeof(int),
827 .btf_load_err = true,
828 .err_str = "Invalid string section",
832 .descr = "empty string section",
835 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
840 .map_type = BPF_MAP_TYPE_ARRAY,
841 .map_name = "hdr_test_map",
842 .key_size = sizeof(int),
843 .value_size = sizeof(int),
847 .btf_load_err = true,
848 .err_str = "Invalid string section",
852 .descr = "empty type section",
857 .str_sec_size = sizeof("\0int"),
858 .map_type = BPF_MAP_TYPE_ARRAY,
859 .map_name = "hdr_test_map",
860 .key_size = sizeof(int),
861 .value_size = sizeof(int),
865 .btf_load_err = true,
866 .err_str = "No type found",
870 .descr = "btf_header test. Longer hdr_len",
873 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
877 .str_sec_size = sizeof("\0int"),
878 .map_type = BPF_MAP_TYPE_ARRAY,
879 .map_name = "hdr_test_map",
880 .key_size = sizeof(int),
881 .value_size = sizeof(int),
885 .btf_load_err = true,
887 .err_str = "Unsupported btf_header",
891 .descr = "btf_header test. Gap between hdr and type",
894 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
898 .str_sec_size = sizeof("\0int"),
899 .map_type = BPF_MAP_TYPE_ARRAY,
900 .map_name = "hdr_test_map",
901 .key_size = sizeof(int),
902 .value_size = sizeof(int),
906 .btf_load_err = true,
908 .err_str = "Unsupported section found",
912 .descr = "btf_header test. Gap between type and str",
915 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
919 .str_sec_size = sizeof("\0int"),
920 .map_type = BPF_MAP_TYPE_ARRAY,
921 .map_name = "hdr_test_map",
922 .key_size = sizeof(int),
923 .value_size = sizeof(int),
927 .btf_load_err = true,
929 .err_str = "Unsupported section found",
933 .descr = "btf_header test. Overlap between type and str",
936 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
940 .str_sec_size = sizeof("\0int"),
941 .map_type = BPF_MAP_TYPE_ARRAY,
942 .map_name = "hdr_test_map",
943 .key_size = sizeof(int),
944 .value_size = sizeof(int),
948 .btf_load_err = true,
950 .err_str = "Section overlap found",
954 .descr = "btf_header test. Larger BTF size",
957 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
961 .str_sec_size = sizeof("\0int"),
962 .map_type = BPF_MAP_TYPE_ARRAY,
963 .map_name = "hdr_test_map",
964 .key_size = sizeof(int),
965 .value_size = sizeof(int),
969 .btf_load_err = true,
971 .err_str = "Unsupported section found",
975 .descr = "btf_header test. Smaller BTF size",
978 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
982 .str_sec_size = sizeof("\0int"),
983 .map_type = BPF_MAP_TYPE_ARRAY,
984 .map_name = "hdr_test_map",
985 .key_size = sizeof(int),
986 .value_size = sizeof(int),
990 .btf_load_err = true,
992 .err_str = "Total section length too long",
996 .descr = "array test. index_type/elem_type \"int\"",
999 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1000 /* int[16] */ /* [2] */
1001 BTF_TYPE_ARRAY_ENC(1, 1, 16),
1005 .str_sec_size = sizeof(""),
1006 .map_type = BPF_MAP_TYPE_ARRAY,
1007 .map_name = "array_test_map",
1008 .key_size = sizeof(int),
1009 .value_size = sizeof(int),
1016 .descr = "array test. index_type/elem_type \"const int\"",
1019 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1020 /* int[16] */ /* [2] */
1021 BTF_TYPE_ARRAY_ENC(3, 3, 16),
1022 /* CONST type_id=1 */ /* [3] */
1023 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 1),
1027 .str_sec_size = sizeof(""),
1028 .map_type = BPF_MAP_TYPE_ARRAY,
1029 .map_name = "array_test_map",
1030 .key_size = sizeof(int),
1031 .value_size = sizeof(int),
1038 .descr = "array test. index_type \"const int:31\"",
1041 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1042 /* int:31 */ /* [2] */
1043 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 31, 4),
1044 /* int[16] */ /* [3] */
1045 BTF_TYPE_ARRAY_ENC(1, 4, 16),
1046 /* CONST type_id=2 */ /* [4] */
1047 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 2),
1051 .str_sec_size = sizeof(""),
1052 .map_type = BPF_MAP_TYPE_ARRAY,
1053 .map_name = "array_test_map",
1054 .key_size = sizeof(int),
1055 .value_size = sizeof(int),
1059 .btf_load_err = true,
1060 .err_str = "Invalid index",
1064 .descr = "array test. elem_type \"const int:31\"",
1067 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1068 /* int:31 */ /* [2] */
1069 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 31, 4),
1070 /* int[16] */ /* [3] */
1071 BTF_TYPE_ARRAY_ENC(4, 1, 16),
1072 /* CONST type_id=2 */ /* [4] */
1073 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 2),
1077 .str_sec_size = sizeof(""),
1078 .map_type = BPF_MAP_TYPE_ARRAY,
1079 .map_name = "array_test_map",
1080 .key_size = sizeof(int),
1081 .value_size = sizeof(int),
1085 .btf_load_err = true,
1086 .err_str = "Invalid array of int",
1090 .descr = "array test. index_type \"void\"",
1093 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1094 /* int[16] */ /* [2] */
1095 BTF_TYPE_ARRAY_ENC(1, 0, 16),
1099 .str_sec_size = sizeof(""),
1100 .map_type = BPF_MAP_TYPE_ARRAY,
1101 .map_name = "array_test_map",
1102 .key_size = sizeof(int),
1103 .value_size = sizeof(int),
1107 .btf_load_err = true,
1108 .err_str = "Invalid index",
1112 .descr = "array test. index_type \"const void\"",
1115 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1116 /* int[16] */ /* [2] */
1117 BTF_TYPE_ARRAY_ENC(1, 3, 16),
1118 /* CONST type_id=0 (void) */ /* [3] */
1119 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1123 .str_sec_size = sizeof(""),
1124 .map_type = BPF_MAP_TYPE_ARRAY,
1125 .map_name = "array_test_map",
1126 .key_size = sizeof(int),
1127 .value_size = sizeof(int),
1131 .btf_load_err = true,
1132 .err_str = "Invalid index",
1136 .descr = "array test. elem_type \"const void\"",
1139 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1140 /* int[16] */ /* [2] */
1141 BTF_TYPE_ARRAY_ENC(3, 1, 16),
1142 /* CONST type_id=0 (void) */ /* [3] */
1143 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1147 .str_sec_size = sizeof(""),
1148 .map_type = BPF_MAP_TYPE_ARRAY,
1149 .map_name = "array_test_map",
1150 .key_size = sizeof(int),
1151 .value_size = sizeof(int),
1155 .btf_load_err = true,
1156 .err_str = "Invalid elem",
1160 .descr = "array test. elem_type \"const void *\"",
1163 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1164 /* const void *[16] */ /* [2] */
1165 BTF_TYPE_ARRAY_ENC(3, 1, 16),
1166 /* CONST type_id=4 */ /* [3] */
1167 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 4),
1168 /* void* */ /* [4] */
1169 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
1173 .str_sec_size = sizeof(""),
1174 .map_type = BPF_MAP_TYPE_ARRAY,
1175 .map_name = "array_test_map",
1176 .key_size = sizeof(int),
1177 .value_size = sizeof(int),
1184 .descr = "array test. index_type \"const void *\"",
1187 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1188 /* const void *[16] */ /* [2] */
1189 BTF_TYPE_ARRAY_ENC(3, 3, 16),
1190 /* CONST type_id=4 */ /* [3] */
1191 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 4),
1192 /* void* */ /* [4] */
1193 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
1197 .str_sec_size = sizeof(""),
1198 .map_type = BPF_MAP_TYPE_ARRAY,
1199 .map_name = "array_test_map",
1200 .key_size = sizeof(int),
1201 .value_size = sizeof(int),
1205 .btf_load_err = true,
1206 .err_str = "Invalid index",
1210 .descr = "array test. t->size != 0\"",
1213 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1214 /* int[16] */ /* [2] */
1215 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ARRAY, 0, 0), 1),
1216 BTF_ARRAY_ENC(1, 1, 16),
1220 .str_sec_size = sizeof(""),
1221 .map_type = BPF_MAP_TYPE_ARRAY,
1222 .map_name = "array_test_map",
1223 .key_size = sizeof(int),
1224 .value_size = sizeof(int),
1228 .btf_load_err = true,
1229 .err_str = "size != 0",
1233 .descr = "int test. invalid int_data",
1235 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), 4),
1240 .str_sec_size = sizeof(""),
1241 .map_type = BPF_MAP_TYPE_ARRAY,
1242 .map_name = "array_test_map",
1243 .key_size = sizeof(int),
1244 .value_size = sizeof(int),
1248 .btf_load_err = true,
1249 .err_str = "Invalid int_data",
1253 .descr = "invalid BTF_INFO",
1256 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1257 BTF_TYPE_ENC(0, 0x10000000, 4),
1261 .str_sec_size = sizeof(""),
1262 .map_type = BPF_MAP_TYPE_ARRAY,
1263 .map_name = "array_test_map",
1264 .key_size = sizeof(int),
1265 .value_size = sizeof(int),
1269 .btf_load_err = true,
1270 .err_str = "Invalid btf_info",
1274 .descr = "fwd test. t->type != 0\"",
1277 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1278 /* fwd type */ /* [2] */
1279 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FWD, 0, 0), 1),
1283 .str_sec_size = sizeof(""),
1284 .map_type = BPF_MAP_TYPE_ARRAY,
1285 .map_name = "fwd_test_map",
1286 .key_size = sizeof(int),
1287 .value_size = sizeof(int),
1291 .btf_load_err = true,
1292 .err_str = "type != 0",
1296 .descr = "arraymap invalid btf key (a bit field)",
1299 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1300 /* 32 bit int with 32 bit offset */ /* [2] */
1301 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 32, 32, 8),
1305 .str_sec_size = sizeof(""),
1306 .map_type = BPF_MAP_TYPE_ARRAY,
1307 .map_name = "array_map_check_btf",
1308 .key_size = sizeof(int),
1309 .value_size = sizeof(int),
1313 .map_create_err = true,
1317 .descr = "arraymap invalid btf key (!= 32 bits)",
1320 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1321 /* 16 bit int with 0 bit offset */ /* [2] */
1322 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 16, 2),
1326 .str_sec_size = sizeof(""),
1327 .map_type = BPF_MAP_TYPE_ARRAY,
1328 .map_name = "array_map_check_btf",
1329 .key_size = sizeof(int),
1330 .value_size = sizeof(int),
1334 .map_create_err = true,
1338 .descr = "arraymap invalid btf value (too small)",
1341 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1345 .str_sec_size = sizeof(""),
1346 .map_type = BPF_MAP_TYPE_ARRAY,
1347 .map_name = "array_map_check_btf",
1348 .key_size = sizeof(int),
1349 /* btf_value_size < map->value_size */
1350 .value_size = sizeof(__u64),
1354 .map_create_err = true,
1358 .descr = "arraymap invalid btf value (too big)",
1361 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1365 .str_sec_size = sizeof(""),
1366 .map_type = BPF_MAP_TYPE_ARRAY,
1367 .map_name = "array_map_check_btf",
1368 .key_size = sizeof(int),
1369 /* btf_value_size > map->value_size */
1370 .value_size = sizeof(__u16),
1374 .map_create_err = true,
1377 }; /* struct btf_raw_test raw_tests[] */
1379 static const char *get_next_str(const char *start, const char *end)
1381 return start < end - 1 ? start + 1 : NULL;
1384 static int get_type_sec_size(const __u32 *raw_types)
1388 for (i = MAX_NR_RAW_TYPES - 1;
1389 i >= 0 && raw_types[i] != BTF_END_RAW;
1393 return i < 0 ? i : i * sizeof(raw_types[0]);
1396 static void *btf_raw_create(const struct btf_header *hdr,
1397 const __u32 *raw_types,
1399 unsigned int str_sec_size,
1400 unsigned int *btf_size)
1402 const char *next_str = str, *end_str = str + str_sec_size;
1403 unsigned int size_needed, offset;
1404 struct btf_header *ret_hdr;
1405 int i, type_sec_size;
1406 uint32_t *ret_types;
1409 type_sec_size = get_type_sec_size(raw_types);
1410 if (CHECK(type_sec_size < 0, "Cannot get nr_raw_types"))
1413 size_needed = sizeof(*hdr) + type_sec_size + str_sec_size;
1414 raw_btf = malloc(size_needed);
1415 if (CHECK(!raw_btf, "Cannot allocate memory for raw_btf"))
1419 memcpy(raw_btf, hdr, sizeof(*hdr));
1420 offset = sizeof(*hdr);
1422 /* Copy type section */
1423 ret_types = raw_btf + offset;
1424 for (i = 0; i < type_sec_size / sizeof(raw_types[0]); i++) {
1425 if (raw_types[i] == NAME_TBD) {
1426 next_str = get_next_str(next_str, end_str);
1427 if (CHECK(!next_str, "Error in getting next_str")) {
1431 ret_types[i] = next_str - str;
1432 next_str += strlen(next_str);
1434 ret_types[i] = raw_types[i];
1437 offset += type_sec_size;
1439 /* Copy string section */
1440 memcpy(raw_btf + offset, str, str_sec_size);
1442 ret_hdr = (struct btf_header *)raw_btf;
1443 ret_hdr->type_len = type_sec_size;
1444 ret_hdr->str_off = type_sec_size;
1445 ret_hdr->str_len = str_sec_size;
1447 *btf_size = size_needed;
1452 static int do_test_raw(unsigned int test_num)
1454 struct btf_raw_test *test = &raw_tests[test_num - 1];
1455 struct bpf_create_map_attr create_attr = {};
1456 int map_fd = -1, btf_fd = -1;
1457 unsigned int raw_btf_size;
1458 struct btf_header *hdr;
1462 fprintf(stderr, "BTF raw test[%u] (%s): ", test_num, test->descr);
1463 raw_btf = btf_raw_create(&hdr_tmpl,
1474 hdr->hdr_len = (int)hdr->hdr_len + test->hdr_len_delta;
1475 hdr->type_off = (int)hdr->type_off + test->type_off_delta;
1476 hdr->str_off = (int)hdr->str_off + test->str_off_delta;
1477 hdr->str_len = (int)hdr->str_len + test->str_len_delta;
1479 *btf_log_buf = '\0';
1480 btf_fd = bpf_load_btf(raw_btf, raw_btf_size,
1481 btf_log_buf, BTF_LOG_BUF_SIZE,
1485 err = ((btf_fd == -1) != test->btf_load_err);
1486 if (CHECK(err, "btf_fd:%d test->btf_load_err:%u",
1487 btf_fd, test->btf_load_err) ||
1488 CHECK(test->err_str && !strstr(btf_log_buf, test->err_str),
1489 "expected err_str:%s", test->err_str)) {
1494 if (err || btf_fd == -1)
1497 create_attr.name = test->map_name;
1498 create_attr.map_type = test->map_type;
1499 create_attr.key_size = test->key_size;
1500 create_attr.value_size = test->value_size;
1501 create_attr.max_entries = test->max_entries;
1502 create_attr.btf_fd = btf_fd;
1503 create_attr.btf_key_type_id = test->key_type_id;
1504 create_attr.btf_value_type_id = test->value_type_id;
1506 map_fd = bpf_create_map_xattr(&create_attr);
1508 err = ((map_fd == -1) != test->map_create_err);
1509 CHECK(err, "map_fd:%d test->map_create_err:%u",
1510 map_fd, test->map_create_err);
1514 fprintf(stderr, "OK");
1516 if (*btf_log_buf && (err || args.always_log))
1517 fprintf(stderr, "\n%s", btf_log_buf);
1527 static int test_raw(void)
1532 if (args.raw_test_num)
1533 return count_result(do_test_raw(args.raw_test_num));
1535 for (i = 1; i <= ARRAY_SIZE(raw_tests); i++)
1536 err |= count_result(do_test_raw(i));
1541 struct btf_get_info_test {
1543 const char *str_sec;
1544 __u32 raw_types[MAX_NR_RAW_TYPES];
1547 int (*special_test)(unsigned int test_num);
1550 static int test_big_btf_info(unsigned int test_num);
1551 static int test_btf_id(unsigned int test_num);
1553 const struct btf_get_info_test get_info_tests[] = {
1555 .descr = "== raw_btf_size+1",
1558 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1562 .str_sec_size = sizeof(""),
1563 .btf_size_delta = 1,
1566 .descr = "== raw_btf_size-3",
1569 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1573 .str_sec_size = sizeof(""),
1574 .btf_size_delta = -3,
1577 .descr = "Large bpf_btf_info",
1580 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1584 .str_sec_size = sizeof(""),
1585 .special_test = test_big_btf_info,
1591 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1592 /* unsigned int */ /* [2] */
1593 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),
1597 .str_sec_size = sizeof(""),
1598 .special_test = test_btf_id,
1602 static inline __u64 ptr_to_u64(const void *ptr)
1604 return (__u64)(unsigned long)ptr;
1607 static int test_big_btf_info(unsigned int test_num)
1609 const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
1610 uint8_t *raw_btf = NULL, *user_btf = NULL;
1611 unsigned int raw_btf_size;
1613 struct bpf_btf_info info;
1616 struct bpf_btf_info *info;
1617 int btf_fd = -1, err;
1620 raw_btf = btf_raw_create(&hdr_tmpl,
1629 *btf_log_buf = '\0';
1631 user_btf = malloc(raw_btf_size);
1632 if (CHECK(!user_btf, "!user_btf")) {
1637 btf_fd = bpf_load_btf(raw_btf, raw_btf_size,
1638 btf_log_buf, BTF_LOG_BUF_SIZE,
1640 if (CHECK(btf_fd == -1, "errno:%d", errno)) {
1646 * GET_INFO should error out if the userspace info
1647 * has non zero tailing bytes.
1649 info = &info_garbage.info;
1650 memset(info, 0, sizeof(*info));
1651 info_garbage.garbage = 0xdeadbeef;
1652 info_len = sizeof(info_garbage);
1653 info->btf = ptr_to_u64(user_btf);
1654 info->btf_size = raw_btf_size;
1656 err = bpf_obj_get_info_by_fd(btf_fd, info, &info_len);
1657 if (CHECK(!err, "!err")) {
1663 * GET_INFO should succeed even info_len is larger than
1664 * the kernel supported as long as tailing bytes are zero.
1665 * The kernel supported info len should also be returned
1668 info_garbage.garbage = 0;
1669 err = bpf_obj_get_info_by_fd(btf_fd, info, &info_len);
1670 if (CHECK(err || info_len != sizeof(*info),
1671 "err:%d errno:%d info_len:%u sizeof(*info):%lu",
1672 err, errno, info_len, sizeof(*info))) {
1677 fprintf(stderr, "OK");
1680 if (*btf_log_buf && (err || args.always_log))
1681 fprintf(stderr, "\n%s", btf_log_buf);
1692 static int test_btf_id(unsigned int test_num)
1694 const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
1695 struct bpf_create_map_attr create_attr = {};
1696 uint8_t *raw_btf = NULL, *user_btf[2] = {};
1697 int btf_fd[2] = {-1, -1}, map_fd = -1;
1698 struct bpf_map_info map_info = {};
1699 struct bpf_btf_info info[2] = {};
1700 unsigned int raw_btf_size;
1704 raw_btf = btf_raw_create(&hdr_tmpl,
1713 *btf_log_buf = '\0';
1715 for (i = 0; i < 2; i++) {
1716 user_btf[i] = malloc(raw_btf_size);
1717 if (CHECK(!user_btf[i], "!user_btf[%d]", i)) {
1721 info[i].btf = ptr_to_u64(user_btf[i]);
1722 info[i].btf_size = raw_btf_size;
1725 btf_fd[0] = bpf_load_btf(raw_btf, raw_btf_size,
1726 btf_log_buf, BTF_LOG_BUF_SIZE,
1728 if (CHECK(btf_fd[0] == -1, "errno:%d", errno)) {
1733 /* Test BPF_OBJ_GET_INFO_BY_ID on btf_id */
1734 info_len = sizeof(info[0]);
1735 err = bpf_obj_get_info_by_fd(btf_fd[0], &info[0], &info_len);
1736 if (CHECK(err, "errno:%d", errno)) {
1741 btf_fd[1] = bpf_btf_get_fd_by_id(info[0].id);
1742 if (CHECK(btf_fd[1] == -1, "errno:%d", errno)) {
1748 err = bpf_obj_get_info_by_fd(btf_fd[1], &info[1], &info_len);
1749 if (CHECK(err || info[0].id != info[1].id ||
1750 info[0].btf_size != info[1].btf_size ||
1751 (ret = memcmp(user_btf[0], user_btf[1], info[0].btf_size)),
1752 "err:%d errno:%d id0:%u id1:%u btf_size0:%u btf_size1:%u memcmp:%d",
1753 err, errno, info[0].id, info[1].id,
1754 info[0].btf_size, info[1].btf_size, ret)) {
1759 /* Test btf members in struct bpf_map_info */
1760 create_attr.name = "test_btf_id";
1761 create_attr.map_type = BPF_MAP_TYPE_ARRAY;
1762 create_attr.key_size = sizeof(int);
1763 create_attr.value_size = sizeof(unsigned int);
1764 create_attr.max_entries = 4;
1765 create_attr.btf_fd = btf_fd[0];
1766 create_attr.btf_key_type_id = 1;
1767 create_attr.btf_value_type_id = 2;
1769 map_fd = bpf_create_map_xattr(&create_attr);
1770 if (CHECK(map_fd == -1, "errno:%d", errno)) {
1775 info_len = sizeof(map_info);
1776 err = bpf_obj_get_info_by_fd(map_fd, &map_info, &info_len);
1777 if (CHECK(err || map_info.btf_id != info[0].id ||
1778 map_info.btf_key_type_id != 1 || map_info.btf_value_type_id != 2,
1779 "err:%d errno:%d info.id:%u btf_id:%u btf_key_type_id:%u btf_value_type_id:%u",
1780 err, errno, info[0].id, map_info.btf_id, map_info.btf_key_type_id,
1781 map_info.btf_value_type_id)) {
1786 for (i = 0; i < 2; i++) {
1791 /* Test BTF ID is removed from the kernel */
1792 btf_fd[0] = bpf_btf_get_fd_by_id(map_info.btf_id);
1793 if (CHECK(btf_fd[0] == -1, "errno:%d", errno)) {
1800 /* The map holds the last ref to BTF and its btf_id */
1803 btf_fd[0] = bpf_btf_get_fd_by_id(map_info.btf_id);
1804 if (CHECK(btf_fd[0] != -1, "BTF lingers")) {
1809 fprintf(stderr, "OK");
1812 if (*btf_log_buf && (err || args.always_log))
1813 fprintf(stderr, "\n%s", btf_log_buf);
1818 for (i = 0; i < 2; i++) {
1820 if (btf_fd[i] != -1)
1827 static int do_test_get_info(unsigned int test_num)
1829 const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
1830 unsigned int raw_btf_size, user_btf_size, expected_nbytes;
1831 uint8_t *raw_btf = NULL, *user_btf = NULL;
1832 struct bpf_btf_info info = {};
1833 int btf_fd = -1, err, ret;
1836 fprintf(stderr, "BTF GET_INFO test[%u] (%s): ",
1837 test_num, test->descr);
1839 if (test->special_test)
1840 return test->special_test(test_num);
1842 raw_btf = btf_raw_create(&hdr_tmpl,
1851 *btf_log_buf = '\0';
1853 user_btf = malloc(raw_btf_size);
1854 if (CHECK(!user_btf, "!user_btf")) {
1859 btf_fd = bpf_load_btf(raw_btf, raw_btf_size,
1860 btf_log_buf, BTF_LOG_BUF_SIZE,
1862 if (CHECK(btf_fd == -1, "errno:%d", errno)) {
1867 user_btf_size = (int)raw_btf_size + test->btf_size_delta;
1868 expected_nbytes = min(raw_btf_size, user_btf_size);
1869 if (raw_btf_size > expected_nbytes)
1870 memset(user_btf + expected_nbytes, 0xff,
1871 raw_btf_size - expected_nbytes);
1873 info_len = sizeof(info);
1874 info.btf = ptr_to_u64(user_btf);
1875 info.btf_size = user_btf_size;
1878 err = bpf_obj_get_info_by_fd(btf_fd, &info, &info_len);
1879 if (CHECK(err || !info.id || info_len != sizeof(info) ||
1880 info.btf_size != raw_btf_size ||
1881 (ret = memcmp(raw_btf, user_btf, expected_nbytes)),
1882 "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",
1883 err, errno, info.id, info_len, sizeof(info),
1884 raw_btf_size, info.btf_size, expected_nbytes, ret)) {
1889 while (expected_nbytes < raw_btf_size) {
1890 fprintf(stderr, "%u...", expected_nbytes);
1891 if (CHECK(user_btf[expected_nbytes++] != 0xff,
1892 "user_btf[%u]:%x != 0xff", expected_nbytes - 1,
1893 user_btf[expected_nbytes - 1])) {
1899 fprintf(stderr, "OK");
1902 if (*btf_log_buf && (err || args.always_log))
1903 fprintf(stderr, "\n%s", btf_log_buf);
1914 static int test_get_info(void)
1919 if (args.get_info_test_num)
1920 return count_result(do_test_get_info(args.get_info_test_num));
1922 for (i = 1; i <= ARRAY_SIZE(get_info_tests); i++)
1923 err |= count_result(do_test_get_info(i));
1928 struct btf_file_test {
1930 bool btf_kv_notfound;
1933 static struct btf_file_test file_tests[] = {
1935 .file = "test_btf_haskv.o",
1938 .file = "test_btf_nokv.o",
1939 .btf_kv_notfound = true,
1943 static int file_has_btf_elf(const char *fn)
1945 Elf_Scn *scn = NULL;
1951 if (CHECK(elf_version(EV_CURRENT) == EV_NONE,
1952 "elf_version(EV_CURRENT) == EV_NONE"))
1955 elf_fd = open(fn, O_RDONLY);
1956 if (CHECK(elf_fd == -1, "open(%s): errno:%d", fn, errno))
1959 elf = elf_begin(elf_fd, ELF_C_READ, NULL);
1960 if (CHECK(!elf, "elf_begin(%s): %s", fn, elf_errmsg(elf_errno()))) {
1965 if (CHECK(!gelf_getehdr(elf, &ehdr), "!gelf_getehdr(%s)", fn)) {
1970 while ((scn = elf_nextscn(elf, scn))) {
1971 const char *sh_name;
1974 if (CHECK(gelf_getshdr(scn, &sh) != &sh,
1975 "file:%s gelf_getshdr != &sh", fn)) {
1980 sh_name = elf_strptr(elf, ehdr.e_shstrndx, sh.sh_name);
1981 if (!strcmp(sh_name, BTF_ELF_SEC)) {
1995 static int do_test_file(unsigned int test_num)
1997 const struct btf_file_test *test = &file_tests[test_num - 1];
1998 struct bpf_object *obj = NULL;
1999 struct bpf_program *prog;
2000 struct bpf_map *map;
2003 fprintf(stderr, "BTF libbpf test[%u] (%s): ", test_num,
2006 err = file_has_btf_elf(test->file);
2011 fprintf(stderr, "SKIP. No ELF %s found", BTF_ELF_SEC);
2016 obj = bpf_object__open(test->file);
2017 if (CHECK(IS_ERR(obj), "obj: %ld", PTR_ERR(obj)))
2018 return PTR_ERR(obj);
2020 err = bpf_object__btf_fd(obj);
2021 if (CHECK(err == -1, "bpf_object__btf_fd: -1"))
2024 prog = bpf_program__next(NULL, obj);
2025 if (CHECK(!prog, "Cannot find bpf_prog")) {
2030 bpf_program__set_type(prog, BPF_PROG_TYPE_TRACEPOINT);
2031 err = bpf_object__load(obj);
2032 if (CHECK(err < 0, "bpf_object__load: %d", err))
2035 map = bpf_object__find_map_by_name(obj, "btf_map");
2036 if (CHECK(!map, "btf_map not found")) {
2041 err = (bpf_map__btf_key_type_id(map) == 0 || bpf_map__btf_value_type_id(map) == 0)
2042 != test->btf_kv_notfound;
2043 if (CHECK(err, "btf_key_type_id:%u btf_value_type_id:%u test->btf_kv_notfound:%u",
2044 bpf_map__btf_key_type_id(map), bpf_map__btf_value_type_id(map),
2045 test->btf_kv_notfound))
2048 fprintf(stderr, "OK");
2051 bpf_object__close(obj);
2055 static int test_file(void)
2060 if (args.file_test_num)
2061 return count_result(do_test_file(args.file_test_num));
2063 for (i = 1; i <= ARRAY_SIZE(file_tests); i++)
2064 err |= count_result(do_test_file(i));
2069 const char *pprint_enum_str[] = {
2076 struct pprint_mapv {
2081 uint32_t unused_bits2a:2,
2096 static struct btf_raw_test pprint_test_template = {
2098 /* unsighed char */ /* [1] */
2099 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 8, 1),
2100 /* unsigned short */ /* [2] */
2101 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 16, 2),
2102 /* unsigned int */ /* [3] */
2103 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),
2105 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
2106 /* unsigned long long */ /* [5] */
2107 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 64, 8),
2108 /* 2 bits */ /* [6] */
2109 BTF_TYPE_INT_ENC(0, 0, 0, 2, 2),
2110 /* 28 bits */ /* [7] */
2111 BTF_TYPE_INT_ENC(0, 0, 0, 28, 4),
2112 /* uint8_t[8] */ /* [8] */
2113 BTF_TYPE_ARRAY_ENC(9, 1, 8),
2114 /* typedef unsigned char uint8_t */ /* [9] */
2115 BTF_TYPEDEF_ENC(NAME_TBD, 1),
2116 /* typedef unsigned short uint16_t */ /* [10] */
2117 BTF_TYPEDEF_ENC(NAME_TBD, 2),
2118 /* typedef unsigned int uint32_t */ /* [11] */
2119 BTF_TYPEDEF_ENC(NAME_TBD, 3),
2120 /* typedef int int32_t */ /* [12] */
2121 BTF_TYPEDEF_ENC(NAME_TBD, 4),
2122 /* typedef unsigned long long uint64_t *//* [13] */
2123 BTF_TYPEDEF_ENC(NAME_TBD, 5),
2124 /* union (anon) */ /* [14] */
2125 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 0, 2), 8),
2126 BTF_MEMBER_ENC(NAME_TBD, 13, 0),/* uint64_t ui64; */
2127 BTF_MEMBER_ENC(NAME_TBD, 8, 0), /* uint8_t ui8a[8]; */
2128 /* enum (anon) */ /* [15] */
2129 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 4), 4),
2130 BTF_ENUM_ENC(NAME_TBD, 0),
2131 BTF_ENUM_ENC(NAME_TBD, 1),
2132 BTF_ENUM_ENC(NAME_TBD, 2),
2133 BTF_ENUM_ENC(NAME_TBD, 3),
2134 /* struct pprint_mapv */ /* [16] */
2135 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 8), 32),
2136 BTF_MEMBER_ENC(NAME_TBD, 11, 0), /* uint32_t ui32 */
2137 BTF_MEMBER_ENC(NAME_TBD, 10, 32), /* uint16_t ui16 */
2138 BTF_MEMBER_ENC(NAME_TBD, 12, 64), /* int32_t si32 */
2139 BTF_MEMBER_ENC(NAME_TBD, 6, 96), /* unused_bits2a */
2140 BTF_MEMBER_ENC(NAME_TBD, 7, 98), /* bits28 */
2141 BTF_MEMBER_ENC(NAME_TBD, 6, 126), /* unused_bits2b */
2142 BTF_MEMBER_ENC(0, 14, 128), /* union (anon) */
2143 BTF_MEMBER_ENC(NAME_TBD, 15, 192), /* aenum */
2146 .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",
2147 .str_sec_size = sizeof("\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"),
2148 .key_size = sizeof(unsigned int),
2149 .value_size = sizeof(struct pprint_mapv),
2150 .key_type_id = 3, /* unsigned int */
2151 .value_type_id = 16, /* struct pprint_mapv */
2152 .max_entries = 128 * 1024,
2155 static struct btf_pprint_test_meta {
2157 enum bpf_map_type map_type;
2158 const char *map_name;
2162 } pprint_tests_meta[] = {
2164 .descr = "BTF pretty print array",
2165 .map_type = BPF_MAP_TYPE_ARRAY,
2166 .map_name = "pprint_test_array",
2167 .ordered_map = true,
2168 .lossless_map = true,
2169 .percpu_map = false,
2173 .descr = "BTF pretty print hash",
2174 .map_type = BPF_MAP_TYPE_HASH,
2175 .map_name = "pprint_test_hash",
2176 .ordered_map = false,
2177 .lossless_map = true,
2178 .percpu_map = false,
2182 .descr = "BTF pretty print lru hash",
2183 .map_type = BPF_MAP_TYPE_LRU_HASH,
2184 .map_name = "pprint_test_lru_hash",
2185 .ordered_map = false,
2186 .lossless_map = false,
2187 .percpu_map = false,
2191 .descr = "BTF pretty print percpu array",
2192 .map_type = BPF_MAP_TYPE_PERCPU_ARRAY,
2193 .map_name = "pprint_test_percpu_array",
2194 .ordered_map = true,
2195 .lossless_map = true,
2200 .descr = "BTF pretty print percpu hash",
2201 .map_type = BPF_MAP_TYPE_PERCPU_HASH,
2202 .map_name = "pprint_test_percpu_hash",
2203 .ordered_map = false,
2204 .lossless_map = true,
2209 .descr = "BTF pretty print lru percpu hash",
2210 .map_type = BPF_MAP_TYPE_LRU_PERCPU_HASH,
2211 .map_name = "pprint_test_lru_percpu_hash",
2212 .ordered_map = false,
2213 .lossless_map = false,
2220 static void set_pprint_mapv(struct pprint_mapv *v, uint32_t i,
2221 int num_cpus, int rounded_value_size)
2225 for (cpu = 0; cpu < num_cpus; cpu++) {
2228 v->unused_bits2a = 3;
2230 v->unused_bits2b = 3;
2232 v->aenum = i & 0x03;
2233 v = (void *)v + rounded_value_size;
2237 static int check_line(const char *expected_line, int nexpected_line,
2238 int expected_line_len, const char *line)
2240 if (CHECK(nexpected_line == expected_line_len,
2241 "expected_line is too long"))
2244 if (strcmp(expected_line, line)) {
2245 fprintf(stderr, "unexpected pprint output\n");
2246 fprintf(stderr, "expected: %s", expected_line);
2247 fprintf(stderr, " read: %s", line);
2255 static int do_test_pprint(void)
2257 const struct btf_raw_test *test = &pprint_test_template;
2258 struct bpf_create_map_attr create_attr = {};
2259 bool ordered_map, lossless_map, percpu_map;
2260 int err, ret, num_cpus, rounded_value_size;
2261 struct pprint_mapv *mapv = NULL;
2262 unsigned int key, nr_read_elems;
2263 int map_fd = -1, btf_fd = -1;
2264 unsigned int raw_btf_size;
2265 char expected_line[255];
2266 FILE *pin_file = NULL;
2268 size_t line_len = 0;
2273 fprintf(stderr, "%s......", test->descr);
2274 raw_btf = btf_raw_create(&hdr_tmpl, test->raw_types,
2275 test->str_sec, test->str_sec_size,
2281 *btf_log_buf = '\0';
2282 btf_fd = bpf_load_btf(raw_btf, raw_btf_size,
2283 btf_log_buf, BTF_LOG_BUF_SIZE,
2287 if (CHECK(btf_fd == -1, "errno:%d", errno)) {
2292 create_attr.name = test->map_name;
2293 create_attr.map_type = test->map_type;
2294 create_attr.key_size = test->key_size;
2295 create_attr.value_size = test->value_size;
2296 create_attr.max_entries = test->max_entries;
2297 create_attr.btf_fd = btf_fd;
2298 create_attr.btf_key_type_id = test->key_type_id;
2299 create_attr.btf_value_type_id = test->value_type_id;
2301 map_fd = bpf_create_map_xattr(&create_attr);
2302 if (CHECK(map_fd == -1, "errno:%d", errno)) {
2307 ret = snprintf(pin_path, sizeof(pin_path), "%s/%s",
2308 "/sys/fs/bpf", test->map_name);
2310 if (CHECK(ret == sizeof(pin_path), "pin_path %s/%s is too long",
2311 "/sys/fs/bpf", test->map_name)) {
2316 err = bpf_obj_pin(map_fd, pin_path);
2317 if (CHECK(err, "bpf_obj_pin(%s): errno:%d.", pin_path, errno))
2320 percpu_map = test->percpu_map;
2321 num_cpus = percpu_map ? bpf_num_possible_cpus() : 1;
2322 rounded_value_size = round_up(sizeof(struct pprint_mapv), 8);
2323 mapv = calloc(num_cpus, rounded_value_size);
2324 if (CHECK(!mapv, "mapv allocation failure")) {
2329 for (key = 0; key < test->max_entries; key++) {
2330 set_pprint_mapv(mapv, key, num_cpus, rounded_value_size);
2331 bpf_map_update_elem(map_fd, &key, mapv, 0);
2334 pin_file = fopen(pin_path, "r");
2335 if (CHECK(!pin_file, "fopen(%s): errno:%d", pin_path, errno)) {
2340 /* Skip lines start with '#' */
2341 while ((nread = getline(&line, &line_len, pin_file)) > 0 &&
2345 if (CHECK(nread <= 0, "Unexpected EOF")) {
2351 ordered_map = test->ordered_map;
2352 lossless_map = test->lossless_map;
2354 struct pprint_mapv *cmapv;
2355 ssize_t nexpected_line;
2356 unsigned int next_key;
2359 next_key = ordered_map ? nr_read_elems : atoi(line);
2360 set_pprint_mapv(mapv, next_key, num_cpus, rounded_value_size);
2363 for (cpu = 0; cpu < num_cpus; cpu++) {
2365 /* for percpu map, the format looks like:
2367 * cpu0: <value_on_cpu0>
2368 * cpu1: <value_on_cpu1>
2370 * cpun: <value_on_cpun>
2373 * let us verify the line containing the key here.
2376 nexpected_line = snprintf(expected_line,
2377 sizeof(expected_line),
2381 err = check_line(expected_line, nexpected_line,
2382 sizeof(expected_line), line);
2387 /* read value@cpu */
2388 nread = getline(&line, &line_len, pin_file);
2393 nexpected_line = snprintf(expected_line, sizeof(expected_line),
2394 "%s%u: {%u,0,%d,0x%x,0x%x,0x%x,"
2395 "{%lu|[%u,%u,%u,%u,%u,%u,%u,%u]},%s}\n",
2396 percpu_map ? "\tcpu" : "",
2397 percpu_map ? cpu : next_key,
2398 cmapv->ui32, cmapv->si32,
2399 cmapv->unused_bits2a,
2401 cmapv->unused_bits2b,
2403 cmapv->ui8a[0], cmapv->ui8a[1],
2404 cmapv->ui8a[2], cmapv->ui8a[3],
2405 cmapv->ui8a[4], cmapv->ui8a[5],
2406 cmapv->ui8a[6], cmapv->ui8a[7],
2407 pprint_enum_str[cmapv->aenum]);
2409 err = check_line(expected_line, nexpected_line,
2410 sizeof(expected_line), line);
2414 cmapv = (void *)cmapv + rounded_value_size;
2418 /* skip the last bracket for the percpu map */
2419 nread = getline(&line, &line_len, pin_file);
2424 nread = getline(&line, &line_len, pin_file);
2425 } while (++nr_read_elems < test->max_entries && nread > 0);
2428 CHECK(nr_read_elems < test->max_entries,
2429 "Unexpected EOF. nr_read_elems:%u test->max_entries:%u",
2430 nr_read_elems, test->max_entries)) {
2435 if (CHECK(nread > 0, "Unexpected extra pprint output: %s", line)) {
2446 fprintf(stderr, "OK");
2447 if (*btf_log_buf && (err || args.always_log))
2448 fprintf(stderr, "\n%s", btf_log_buf);
2461 static int test_pprint(void)
2466 for (i = 0; i < ARRAY_SIZE(pprint_tests_meta); i++) {
2467 pprint_test_template.descr = pprint_tests_meta[i].descr;
2468 pprint_test_template.map_type = pprint_tests_meta[i].map_type;
2469 pprint_test_template.map_name = pprint_tests_meta[i].map_name;
2470 pprint_test_template.ordered_map = pprint_tests_meta[i].ordered_map;
2471 pprint_test_template.lossless_map = pprint_tests_meta[i].lossless_map;
2472 pprint_test_template.percpu_map = pprint_tests_meta[i].percpu_map;
2474 err |= count_result(do_test_pprint());
2480 static void usage(const char *cmd)
2482 fprintf(stderr, "Usage: %s [-l] [[-r test_num (1 - %zu)] | [-g test_num (1 - %zu)] | [-f test_num (1 - %zu)] | [-p]]\n",
2483 cmd, ARRAY_SIZE(raw_tests), ARRAY_SIZE(get_info_tests),
2484 ARRAY_SIZE(file_tests));
2487 static int parse_args(int argc, char **argv)
2489 const char *optstr = "lpf:r:g:";
2492 while ((opt = getopt(argc, argv, optstr)) != -1) {
2495 args.always_log = true;
2498 args.file_test_num = atoi(optarg);
2499 args.file_test = true;
2502 args.raw_test_num = atoi(optarg);
2503 args.raw_test = true;
2506 args.get_info_test_num = atoi(optarg);
2507 args.get_info_test = true;
2510 args.pprint_test = true;
2521 if (args.raw_test_num &&
2522 (args.raw_test_num < 1 ||
2523 args.raw_test_num > ARRAY_SIZE(raw_tests))) {
2524 fprintf(stderr, "BTF raw test number must be [1 - %zu]\n",
2525 ARRAY_SIZE(raw_tests));
2529 if (args.file_test_num &&
2530 (args.file_test_num < 1 ||
2531 args.file_test_num > ARRAY_SIZE(file_tests))) {
2532 fprintf(stderr, "BTF file test number must be [1 - %zu]\n",
2533 ARRAY_SIZE(file_tests));
2537 if (args.get_info_test_num &&
2538 (args.get_info_test_num < 1 ||
2539 args.get_info_test_num > ARRAY_SIZE(get_info_tests))) {
2540 fprintf(stderr, "BTF get info test number must be [1 - %zu]\n",
2541 ARRAY_SIZE(get_info_tests));
2548 static void print_summary(void)
2550 fprintf(stderr, "PASS:%u SKIP:%u FAIL:%u\n",
2551 pass_cnt - skip_cnt, skip_cnt, error_cnt);
2554 int main(int argc, char **argv)
2558 err = parse_args(argc, argv);
2562 if (args.always_log)
2563 libbpf_set_print(__base_pr, __base_pr, __base_pr);
2568 if (args.get_info_test)
2569 err |= test_get_info();
2574 if (args.pprint_test)
2575 err |= test_pprint();
2577 if (args.raw_test || args.get_info_test || args.file_test ||
2582 err |= test_get_info();