1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2018 Facebook */
8 #include <sys/resource.h>
18 #include <bpf/libbpf.h>
21 #include "bpf_rlimit.h"
24 static uint32_t pass_cnt
;
25 static uint32_t error_cnt
;
26 static uint32_t skip_cnt
;
28 #define CHECK(condition, format...) ({ \
29 int __ret = !!(condition); \
31 fprintf(stderr, "%s:%d:FAIL ", __func__, __LINE__); \
32 fprintf(stderr, format); \
37 static int count_result(int err
)
44 fprintf(stderr
, "\n");
48 #define min(a, b) ((a) < (b) ? (a) : (b))
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
;
139 static struct btf_raw_test raw_tests
[] = {
146 * unsigned long long m;
156 .descr
= "struct test #1",
159 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
160 /* unsigned long long */
161 BTF_TYPE_INT_ENC(0, 0, 0, 64, 8), /* [2] */
163 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 8, 1), /* [3] */
165 BTF_TYPE_ARRAY_ENC(1, 1, 8), /* [4] */
166 /* struct A { */ /* [5] */
167 BTF_TYPE_ENC(NAME_TBD
, BTF_INFO_ENC(BTF_KIND_STRUCT
, 0, 6), 180),
168 BTF_MEMBER_ENC(NAME_TBD
, 2, 0), /* unsigned long long m;*/
169 BTF_MEMBER_ENC(NAME_TBD
, 1, 64),/* int n; */
170 BTF_MEMBER_ENC(NAME_TBD
, 3, 96),/* char o; */
171 BTF_MEMBER_ENC(NAME_TBD
, 4, 128),/* int p[8] */
172 BTF_MEMBER_ENC(NAME_TBD
, 6, 384),/* int q[4][8] */
173 BTF_MEMBER_ENC(NAME_TBD
, 7, 1408), /* enum E r */
176 BTF_TYPE_ARRAY_ENC(4, 1, 4), /* [6] */
177 /* enum E */ /* [7] */
178 BTF_TYPE_ENC(NAME_TBD
, BTF_INFO_ENC(BTF_KIND_ENUM
, 0, 2), sizeof(int)),
179 BTF_ENUM_ENC(NAME_TBD
, 0),
180 BTF_ENUM_ENC(NAME_TBD
, 1),
183 .str_sec
= "\0A\0m\0n\0o\0p\0q\0r\0E\0E0\0E1",
184 .str_sec_size
= sizeof("\0A\0m\0n\0o\0p\0q\0r\0E\0E0\0E1"),
185 .map_type
= BPF_MAP_TYPE_ARRAY
,
186 .map_name
= "struct_test1_map",
187 .key_size
= sizeof(int),
194 /* typedef struct b Struct_B;
199 * const Struct_B o[4];
208 .descr
= "struct test #2",
211 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
212 /* struct b [4] */ /* [2] */
213 BTF_TYPE_ARRAY_ENC(4, 1, 4),
215 /* struct A { */ /* [3] */
216 BTF_TYPE_ENC(NAME_TBD
, BTF_INFO_ENC(BTF_KIND_STRUCT
, 0, 3), 68),
217 BTF_MEMBER_ENC(NAME_TBD
, 1, 0), /* int m; */
218 BTF_MEMBER_ENC(NAME_TBD
, 2, 32),/* struct B n[4] */
219 BTF_MEMBER_ENC(NAME_TBD
, 8, 288),/* const Struct_B o[4];*/
222 /* struct B { */ /* [4] */
223 BTF_TYPE_ENC(NAME_TBD
, BTF_INFO_ENC(BTF_KIND_STRUCT
, 0, 2), 8),
224 BTF_MEMBER_ENC(NAME_TBD
, 1, 0), /* int m; */
225 BTF_MEMBER_ENC(NAME_TBD
, 1, 32),/* int n; */
228 /* const int */ /* [5] */
229 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST
, 0, 0), 1),
230 /* typedef struct b Struct_B */ /* [6] */
231 BTF_TYPE_ENC(NAME_TBD
, BTF_INFO_ENC(BTF_KIND_TYPEDEF
, 0, 0), 4),
232 /* const Struct_B */ /* [7] */
233 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST
, 0, 0), 6),
234 /* const Struct_B [4] */ /* [8] */
235 BTF_TYPE_ARRAY_ENC(7, 1, 4),
238 .str_sec
= "\0A\0m\0n\0o\0B\0m\0n\0Struct_B",
239 .str_sec_size
= sizeof("\0A\0m\0n\0o\0B\0m\0n\0Struct_B"),
240 .map_type
= BPF_MAP_TYPE_ARRAY
,
241 .map_name
= "struct_test2_map",
242 .key_size
= sizeof(int),
250 .descr
= "struct test #3 Invalid member offset",
253 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
254 /* int64 */ /* [2] */
255 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 64, 8),
257 /* struct A { */ /* [3] */
258 BTF_TYPE_ENC(NAME_TBD
, BTF_INFO_ENC(BTF_KIND_STRUCT
, 0, 2), 16),
259 BTF_MEMBER_ENC(NAME_TBD
, 1, 64), /* int m; */
260 BTF_MEMBER_ENC(NAME_TBD
, 2, 0), /* int64 n; */
264 .str_sec
= "\0A\0m\0n\0",
265 .str_sec_size
= sizeof("\0A\0m\0n\0"),
266 .map_type
= BPF_MAP_TYPE_ARRAY
,
267 .map_name
= "struct_test3_map",
268 .key_size
= sizeof(int),
273 .btf_load_err
= true,
274 .err_str
= "Invalid member bits_offset",
277 /* Test member exceeds the size of struct.
285 .descr
= "size check test #1",
288 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
289 /* struct A { */ /* [2] */
290 BTF_TYPE_ENC(NAME_TBD
, BTF_INFO_ENC(BTF_KIND_STRUCT
, 0, 2), sizeof(int) * 2 - 1),
291 BTF_MEMBER_ENC(NAME_TBD
, 1, 0), /* int m; */
292 BTF_MEMBER_ENC(NAME_TBD
, 1, 32),/* int n; */
296 .str_sec
= "\0A\0m\0n",
297 .str_sec_size
= sizeof("\0A\0m\0n"),
298 .map_type
= BPF_MAP_TYPE_ARRAY
,
299 .map_name
= "size_check1_map",
300 .key_size
= sizeof(int),
305 .btf_load_err
= true,
306 .err_str
= "Member exceeds struct_size",
309 /* Test member exeeds the size of struct
317 .descr
= "size check test #2",
320 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, sizeof(int)),
321 /* int[2] */ /* [2] */
322 BTF_TYPE_ARRAY_ENC(1, 1, 2),
323 /* struct A { */ /* [3] */
324 BTF_TYPE_ENC(NAME_TBD
, BTF_INFO_ENC(BTF_KIND_STRUCT
, 0, 2), sizeof(int) * 3 - 1),
325 BTF_MEMBER_ENC(NAME_TBD
, 1, 0), /* int m; */
326 BTF_MEMBER_ENC(NAME_TBD
, 2, 32),/* int n[2]; */
330 .str_sec
= "\0A\0m\0n",
331 .str_sec_size
= sizeof("\0A\0m\0n"),
332 .map_type
= BPF_MAP_TYPE_ARRAY
,
333 .map_name
= "size_check2_map",
334 .key_size
= sizeof(int),
339 .btf_load_err
= true,
340 .err_str
= "Member exceeds struct_size",
343 /* Test member exeeds the size of struct
351 .descr
= "size check test #3",
354 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, sizeof(int)),
355 /* void* */ /* [2] */
356 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR
, 0, 0), 0),
357 /* struct A { */ /* [3] */
358 BTF_TYPE_ENC(NAME_TBD
, BTF_INFO_ENC(BTF_KIND_STRUCT
, 0, 2), sizeof(int) + sizeof(void *) - 1),
359 BTF_MEMBER_ENC(NAME_TBD
, 1, 0), /* int m; */
360 BTF_MEMBER_ENC(NAME_TBD
, 2, 32),/* void *n; */
364 .str_sec
= "\0A\0m\0n",
365 .str_sec_size
= sizeof("\0A\0m\0n"),
366 .map_type
= BPF_MAP_TYPE_ARRAY
,
367 .map_name
= "size_check3_map",
368 .key_size
= sizeof(int),
373 .btf_load_err
= true,
374 .err_str
= "Member exceeds struct_size",
377 /* Test member exceeds the size of struct
390 .descr
= "size check test #4",
393 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, sizeof(int)),
394 /* enum E { */ /* [2] */
395 BTF_TYPE_ENC(NAME_TBD
, BTF_INFO_ENC(BTF_KIND_ENUM
, 0, 2), sizeof(int)),
396 BTF_ENUM_ENC(NAME_TBD
, 0),
397 BTF_ENUM_ENC(NAME_TBD
, 1),
399 /* struct A { */ /* [3] */
400 BTF_TYPE_ENC(NAME_TBD
, BTF_INFO_ENC(BTF_KIND_STRUCT
, 0, 2), sizeof(int) * 2 - 1),
401 BTF_MEMBER_ENC(NAME_TBD
, 1, 0), /* int m; */
402 BTF_MEMBER_ENC(NAME_TBD
, 2, 32),/* enum E n; */
406 .str_sec
= "\0E\0E0\0E1\0A\0m\0n",
407 .str_sec_size
= sizeof("\0E\0E0\0E1\0A\0m\0n"),
408 .map_type
= BPF_MAP_TYPE_ARRAY
,
409 .map_name
= "size_check4_map",
410 .key_size
= sizeof(int),
415 .btf_load_err
= true,
416 .err_str
= "Member exceeds struct_size",
419 /* typedef const void * const_void_ptr;
425 .descr
= "void test #1",
428 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
429 /* const void */ /* [2] */
430 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST
, 0, 0), 0),
431 /* const void* */ /* [3] */
432 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR
, 0, 0), 2),
433 /* typedef const void * const_void_ptr */
434 BTF_TYPEDEF_ENC(NAME_TBD
, 3), /* [4] */
435 /* struct A { */ /* [5] */
436 BTF_TYPE_ENC(NAME_TBD
, BTF_INFO_ENC(BTF_KIND_STRUCT
, 0, 1), sizeof(void *)),
437 /* const_void_ptr m; */
438 BTF_MEMBER_ENC(NAME_TBD
, 4, 0),
442 .str_sec
= "\0const_void_ptr\0A\0m",
443 .str_sec_size
= sizeof("\0const_void_ptr\0A\0m"),
444 .map_type
= BPF_MAP_TYPE_ARRAY
,
445 .map_name
= "void_test1_map",
446 .key_size
= sizeof(int),
447 .value_size
= sizeof(void *),
458 .descr
= "void test #2",
461 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
462 /* const void */ /* [2] */
463 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST
, 0, 0), 0),
464 /* struct A { */ /* [3] */
465 BTF_TYPE_ENC(NAME_TBD
, BTF_INFO_ENC(BTF_KIND_STRUCT
, 0, 1), 8),
467 BTF_MEMBER_ENC(NAME_TBD
, 2, 0),
472 .str_sec_size
= sizeof("\0A\0m"),
473 .map_type
= BPF_MAP_TYPE_ARRAY
,
474 .map_name
= "void_test2_map",
475 .key_size
= sizeof(int),
476 .value_size
= sizeof(void *),
480 .btf_load_err
= true,
481 .err_str
= "Invalid member",
484 /* typedef const void * const_void_ptr;
488 .descr
= "void test #3",
491 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
492 /* const void */ /* [2] */
493 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST
, 0, 0), 0),
494 /* const void* */ /* [3] */
495 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR
, 0, 0), 2),
496 /* typedef const void * const_void_ptr */
497 BTF_TYPEDEF_ENC(NAME_TBD
, 3), /* [4] */
498 /* const_void_ptr[4] */
499 BTF_TYPE_ARRAY_ENC(4, 1, 4), /* [5] */
502 .str_sec
= "\0const_void_ptr",
503 .str_sec_size
= sizeof("\0const_void_ptr"),
504 .map_type
= BPF_MAP_TYPE_ARRAY
,
505 .map_name
= "void_test3_map",
506 .key_size
= sizeof(int),
507 .value_size
= sizeof(void *) * 4,
515 .descr
= "void test #4",
518 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
519 /* const void */ /* [2] */
520 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST
, 0, 0), 0),
521 /* const void[4] */ /* [3] */
522 BTF_TYPE_ARRAY_ENC(2, 1, 4),
526 .str_sec_size
= sizeof("\0A\0m"),
527 .map_type
= BPF_MAP_TYPE_ARRAY
,
528 .map_name
= "void_test4_map",
529 .key_size
= sizeof(int),
530 .value_size
= sizeof(void *) * 4,
534 .btf_load_err
= true,
535 .err_str
= "Invalid elem",
538 /* Array_A <------------------+
539 * elem_type == Array_B |
542 * Array_B <-------- + |
543 * elem_type == Array A --+
546 .descr
= "loop test #1",
549 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
550 /* Array_A */ /* [2] */
551 BTF_TYPE_ARRAY_ENC(3, 1, 8),
552 /* Array_B */ /* [3] */
553 BTF_TYPE_ARRAY_ENC(2, 1, 8),
557 .str_sec_size
= sizeof(""),
558 .map_type
= BPF_MAP_TYPE_ARRAY
,
559 .map_name
= "loop_test1_map",
560 .key_size
= sizeof(int),
561 .value_size
= sizeof(sizeof(int) * 8),
565 .btf_load_err
= true,
566 .err_str
= "Loop detected",
569 /* typedef is _before_ the BTF type of Array_A and Array_B
571 * typedef Array_B int_array;
573 * Array_A <------------------+
574 * elem_type == int_array |
577 * Array_B <-------- + |
578 * elem_type == Array_A --+
581 .descr
= "loop test #2",
584 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
585 /* typedef Array_B int_array */
586 BTF_TYPEDEF_ENC(1, 4), /* [2] */
588 BTF_TYPE_ARRAY_ENC(2, 1, 8), /* [3] */
590 BTF_TYPE_ARRAY_ENC(3, 1, 8), /* [4] */
593 .str_sec
= "\0int_array\0",
594 .str_sec_size
= sizeof("\0int_array"),
595 .map_type
= BPF_MAP_TYPE_ARRAY
,
596 .map_name
= "loop_test2_map",
597 .key_size
= sizeof(int),
598 .value_size
= sizeof(sizeof(int) * 8),
602 .btf_load_err
= true,
603 .err_str
= "Loop detected",
606 /* Array_A <------------------+
607 * elem_type == Array_B |
610 * Array_B <-------- + |
611 * elem_type == Array_A --+
614 .descr
= "loop test #3",
617 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
618 /* Array_A */ /* [2] */
619 BTF_TYPE_ARRAY_ENC(3, 1, 8),
620 /* Array_B */ /* [3] */
621 BTF_TYPE_ARRAY_ENC(2, 1, 8),
625 .str_sec_size
= sizeof(""),
626 .map_type
= BPF_MAP_TYPE_ARRAY
,
627 .map_name
= "loop_test3_map",
628 .key_size
= sizeof(int),
629 .value_size
= sizeof(sizeof(int) * 8),
633 .btf_load_err
= true,
634 .err_str
= "Loop detected",
637 /* typedef is _between_ the BTF type of Array_A and Array_B
639 * typedef Array_B int_array;
641 * Array_A <------------------+
642 * elem_type == int_array |
645 * Array_B <-------- + |
646 * elem_type == Array_A --+
649 .descr
= "loop test #4",
652 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
653 /* Array_A */ /* [2] */
654 BTF_TYPE_ARRAY_ENC(3, 1, 8),
655 /* typedef Array_B int_array */ /* [3] */
656 BTF_TYPEDEF_ENC(NAME_TBD
, 4),
657 /* Array_B */ /* [4] */
658 BTF_TYPE_ARRAY_ENC(2, 1, 8),
661 .str_sec
= "\0int_array\0",
662 .str_sec_size
= sizeof("\0int_array"),
663 .map_type
= BPF_MAP_TYPE_ARRAY
,
664 .map_name
= "loop_test4_map",
665 .key_size
= sizeof(int),
666 .value_size
= sizeof(sizeof(int) * 8),
670 .btf_load_err
= true,
671 .err_str
= "Loop detected",
674 /* typedef struct B Struct_B
687 .descr
= "loop test #5",
690 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
691 /* struct A */ /* [2] */
692 BTF_TYPE_ENC(NAME_TBD
, BTF_INFO_ENC(BTF_KIND_STRUCT
, 0, 2), 8),
693 BTF_MEMBER_ENC(NAME_TBD
, 1, 0), /* int x; */
694 BTF_MEMBER_ENC(NAME_TBD
, 3, 32),/* Struct_B y; */
695 /* typedef struct B Struct_B */
696 BTF_TYPEDEF_ENC(NAME_TBD
, 4), /* [3] */
697 /* struct B */ /* [4] */
698 BTF_TYPE_ENC(NAME_TBD
, BTF_INFO_ENC(BTF_KIND_STRUCT
, 0, 2), 8),
699 BTF_MEMBER_ENC(NAME_TBD
, 1, 0), /* int x; */
700 BTF_MEMBER_ENC(NAME_TBD
, 2, 32),/* struct A y; */
703 .str_sec
= "\0A\0x\0y\0Struct_B\0B\0x\0y",
704 .str_sec_size
= sizeof("\0A\0x\0y\0Struct_B\0B\0x\0y"),
705 .map_type
= BPF_MAP_TYPE_ARRAY
,
706 .map_name
= "loop_test5_map",
707 .key_size
= sizeof(int),
712 .btf_load_err
= true,
713 .err_str
= "Loop detected",
718 * struct A array_a[4];
722 .descr
= "loop test #6",
725 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
726 BTF_TYPE_ARRAY_ENC(3, 1, 4), /* [2] */
727 /* struct A */ /* [3] */
728 BTF_TYPE_ENC(NAME_TBD
, BTF_INFO_ENC(BTF_KIND_STRUCT
, 0, 2), 8),
729 BTF_MEMBER_ENC(NAME_TBD
, 1, 0), /* int x; */
730 BTF_MEMBER_ENC(NAME_TBD
, 2, 32),/* struct A array_a[4]; */
733 .str_sec
= "\0A\0x\0y",
734 .str_sec_size
= sizeof("\0A\0x\0y"),
735 .map_type
= BPF_MAP_TYPE_ARRAY
,
736 .map_name
= "loop_test6_map",
737 .key_size
= sizeof(int),
742 .btf_load_err
= true,
743 .err_str
= "Loop detected",
747 .descr
= "loop test #7",
750 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
751 /* struct A { */ /* [2] */
752 BTF_TYPE_ENC(NAME_TBD
, BTF_INFO_ENC(BTF_KIND_STRUCT
, 0, 1), sizeof(void *)),
754 BTF_MEMBER_ENC(NAME_TBD
, 3, 0),
755 /* CONST type_id=3 */ /* [3] */
756 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST
, 0, 0), 4),
757 /* PTR type_id=2 */ /* [4] */
758 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR
, 0, 0), 3),
762 .str_sec_size
= sizeof("\0A\0m"),
763 .map_type
= BPF_MAP_TYPE_ARRAY
,
764 .map_name
= "loop_test7_map",
765 .key_size
= sizeof(int),
766 .value_size
= sizeof(void *),
770 .btf_load_err
= true,
771 .err_str
= "Loop detected",
775 .descr
= "loop test #8",
778 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
779 /* struct A { */ /* [2] */
780 BTF_TYPE_ENC(NAME_TBD
, BTF_INFO_ENC(BTF_KIND_STRUCT
, 0, 1), sizeof(void *)),
782 BTF_MEMBER_ENC(NAME_TBD
, 4, 0),
783 /* struct B { */ /* [3] */
784 BTF_TYPE_ENC(NAME_TBD
, BTF_INFO_ENC(BTF_KIND_STRUCT
, 0, 1), sizeof(void *)),
786 BTF_MEMBER_ENC(NAME_TBD
, 6, 0),
787 /* CONST type_id=5 */ /* [4] */
788 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST
, 0, 0), 5),
789 /* PTR type_id=6 */ /* [5] */
790 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR
, 0, 0), 6),
791 /* CONST type_id=7 */ /* [6] */
792 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST
, 0, 0), 7),
793 /* PTR type_id=4 */ /* [7] */
794 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR
, 0, 0), 4),
797 .str_sec
= "\0A\0m\0B\0n",
798 .str_sec_size
= sizeof("\0A\0m\0B\0n"),
799 .map_type
= BPF_MAP_TYPE_ARRAY
,
800 .map_name
= "loop_test8_map",
801 .key_size
= sizeof(int),
802 .value_size
= sizeof(void *),
806 .btf_load_err
= true,
807 .err_str
= "Loop detected",
811 .descr
= "string section does not end with null",
814 BTF_TYPE_INT_ENC(NAME_TBD
, BTF_INT_SIGNED
, 0, 32, 4),
818 .str_sec_size
= sizeof("\0int") - 1,
819 .map_type
= BPF_MAP_TYPE_ARRAY
,
820 .map_name
= "hdr_test_map",
821 .key_size
= sizeof(int),
822 .value_size
= sizeof(int),
826 .btf_load_err
= true,
827 .err_str
= "Invalid string section",
831 .descr
= "empty string section",
834 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
839 .map_type
= BPF_MAP_TYPE_ARRAY
,
840 .map_name
= "hdr_test_map",
841 .key_size
= sizeof(int),
842 .value_size
= sizeof(int),
846 .btf_load_err
= true,
847 .err_str
= "Invalid string section",
851 .descr
= "empty type section",
856 .str_sec_size
= sizeof("\0int"),
857 .map_type
= BPF_MAP_TYPE_ARRAY
,
858 .map_name
= "hdr_test_map",
859 .key_size
= sizeof(int),
860 .value_size
= sizeof(int),
864 .btf_load_err
= true,
865 .err_str
= "No type found",
869 .descr
= "btf_header test. Longer hdr_len",
872 BTF_TYPE_INT_ENC(NAME_TBD
, BTF_INT_SIGNED
, 0, 32, 4),
876 .str_sec_size
= sizeof("\0int"),
877 .map_type
= BPF_MAP_TYPE_ARRAY
,
878 .map_name
= "hdr_test_map",
879 .key_size
= sizeof(int),
880 .value_size
= sizeof(int),
884 .btf_load_err
= true,
886 .err_str
= "Unsupported btf_header",
890 .descr
= "btf_header test. Gap between hdr and type",
893 BTF_TYPE_INT_ENC(NAME_TBD
, BTF_INT_SIGNED
, 0, 32, 4),
897 .str_sec_size
= sizeof("\0int"),
898 .map_type
= BPF_MAP_TYPE_ARRAY
,
899 .map_name
= "hdr_test_map",
900 .key_size
= sizeof(int),
901 .value_size
= sizeof(int),
905 .btf_load_err
= true,
907 .err_str
= "Unsupported section found",
911 .descr
= "btf_header test. Gap between type and str",
914 BTF_TYPE_INT_ENC(NAME_TBD
, BTF_INT_SIGNED
, 0, 32, 4),
918 .str_sec_size
= sizeof("\0int"),
919 .map_type
= BPF_MAP_TYPE_ARRAY
,
920 .map_name
= "hdr_test_map",
921 .key_size
= sizeof(int),
922 .value_size
= sizeof(int),
926 .btf_load_err
= true,
928 .err_str
= "Unsupported section found",
932 .descr
= "btf_header test. Overlap between type and str",
935 BTF_TYPE_INT_ENC(NAME_TBD
, BTF_INT_SIGNED
, 0, 32, 4),
939 .str_sec_size
= sizeof("\0int"),
940 .map_type
= BPF_MAP_TYPE_ARRAY
,
941 .map_name
= "hdr_test_map",
942 .key_size
= sizeof(int),
943 .value_size
= sizeof(int),
947 .btf_load_err
= true,
949 .err_str
= "Section overlap found",
953 .descr
= "btf_header test. Larger BTF size",
956 BTF_TYPE_INT_ENC(NAME_TBD
, BTF_INT_SIGNED
, 0, 32, 4),
960 .str_sec_size
= sizeof("\0int"),
961 .map_type
= BPF_MAP_TYPE_ARRAY
,
962 .map_name
= "hdr_test_map",
963 .key_size
= sizeof(int),
964 .value_size
= sizeof(int),
968 .btf_load_err
= true,
970 .err_str
= "Unsupported section found",
974 .descr
= "btf_header test. Smaller BTF size",
977 BTF_TYPE_INT_ENC(NAME_TBD
, BTF_INT_SIGNED
, 0, 32, 4),
981 .str_sec_size
= sizeof("\0int"),
982 .map_type
= BPF_MAP_TYPE_ARRAY
,
983 .map_name
= "hdr_test_map",
984 .key_size
= sizeof(int),
985 .value_size
= sizeof(int),
989 .btf_load_err
= true,
991 .err_str
= "Total section length too long",
995 .descr
= "array test. index_type/elem_type \"int\"",
998 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
999 /* int[16] */ /* [2] */
1000 BTF_TYPE_ARRAY_ENC(1, 1, 16),
1004 .str_sec_size
= sizeof(""),
1005 .map_type
= BPF_MAP_TYPE_ARRAY
,
1006 .map_name
= "array_test_map",
1007 .key_size
= sizeof(int),
1008 .value_size
= sizeof(int),
1015 .descr
= "array test. index_type/elem_type \"const int\"",
1018 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
1019 /* int[16] */ /* [2] */
1020 BTF_TYPE_ARRAY_ENC(3, 3, 16),
1021 /* CONST type_id=1 */ /* [3] */
1022 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST
, 0, 0), 1),
1026 .str_sec_size
= sizeof(""),
1027 .map_type
= BPF_MAP_TYPE_ARRAY
,
1028 .map_name
= "array_test_map",
1029 .key_size
= sizeof(int),
1030 .value_size
= sizeof(int),
1037 .descr
= "array test. index_type \"const int:31\"",
1040 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
1041 /* int:31 */ /* [2] */
1042 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 31, 4),
1043 /* int[16] */ /* [3] */
1044 BTF_TYPE_ARRAY_ENC(1, 4, 16),
1045 /* CONST type_id=2 */ /* [4] */
1046 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST
, 0, 0), 2),
1050 .str_sec_size
= sizeof(""),
1051 .map_type
= BPF_MAP_TYPE_ARRAY
,
1052 .map_name
= "array_test_map",
1053 .key_size
= sizeof(int),
1054 .value_size
= sizeof(int),
1058 .btf_load_err
= true,
1059 .err_str
= "Invalid index",
1063 .descr
= "array test. elem_type \"const int:31\"",
1066 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
1067 /* int:31 */ /* [2] */
1068 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 31, 4),
1069 /* int[16] */ /* [3] */
1070 BTF_TYPE_ARRAY_ENC(4, 1, 16),
1071 /* CONST type_id=2 */ /* [4] */
1072 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST
, 0, 0), 2),
1076 .str_sec_size
= sizeof(""),
1077 .map_type
= BPF_MAP_TYPE_ARRAY
,
1078 .map_name
= "array_test_map",
1079 .key_size
= sizeof(int),
1080 .value_size
= sizeof(int),
1084 .btf_load_err
= true,
1085 .err_str
= "Invalid array of int",
1089 .descr
= "array test. index_type \"void\"",
1092 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
1093 /* int[16] */ /* [2] */
1094 BTF_TYPE_ARRAY_ENC(1, 0, 16),
1098 .str_sec_size
= sizeof(""),
1099 .map_type
= BPF_MAP_TYPE_ARRAY
,
1100 .map_name
= "array_test_map",
1101 .key_size
= sizeof(int),
1102 .value_size
= sizeof(int),
1106 .btf_load_err
= true,
1107 .err_str
= "Invalid index",
1111 .descr
= "array test. index_type \"const void\"",
1114 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
1115 /* int[16] */ /* [2] */
1116 BTF_TYPE_ARRAY_ENC(1, 3, 16),
1117 /* CONST type_id=0 (void) */ /* [3] */
1118 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST
, 0, 0), 0),
1122 .str_sec_size
= sizeof(""),
1123 .map_type
= BPF_MAP_TYPE_ARRAY
,
1124 .map_name
= "array_test_map",
1125 .key_size
= sizeof(int),
1126 .value_size
= sizeof(int),
1130 .btf_load_err
= true,
1131 .err_str
= "Invalid index",
1135 .descr
= "array test. elem_type \"const void\"",
1138 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
1139 /* int[16] */ /* [2] */
1140 BTF_TYPE_ARRAY_ENC(3, 1, 16),
1141 /* CONST type_id=0 (void) */ /* [3] */
1142 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST
, 0, 0), 0),
1146 .str_sec_size
= sizeof(""),
1147 .map_type
= BPF_MAP_TYPE_ARRAY
,
1148 .map_name
= "array_test_map",
1149 .key_size
= sizeof(int),
1150 .value_size
= sizeof(int),
1154 .btf_load_err
= true,
1155 .err_str
= "Invalid elem",
1159 .descr
= "array test. elem_type \"const void *\"",
1162 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
1163 /* const void *[16] */ /* [2] */
1164 BTF_TYPE_ARRAY_ENC(3, 1, 16),
1165 /* CONST type_id=4 */ /* [3] */
1166 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST
, 0, 0), 4),
1167 /* void* */ /* [4] */
1168 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR
, 0, 0), 0),
1172 .str_sec_size
= sizeof(""),
1173 .map_type
= BPF_MAP_TYPE_ARRAY
,
1174 .map_name
= "array_test_map",
1175 .key_size
= sizeof(int),
1176 .value_size
= sizeof(int),
1183 .descr
= "array test. index_type \"const void *\"",
1186 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
1187 /* const void *[16] */ /* [2] */
1188 BTF_TYPE_ARRAY_ENC(3, 3, 16),
1189 /* CONST type_id=4 */ /* [3] */
1190 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST
, 0, 0), 4),
1191 /* void* */ /* [4] */
1192 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR
, 0, 0), 0),
1196 .str_sec_size
= sizeof(""),
1197 .map_type
= BPF_MAP_TYPE_ARRAY
,
1198 .map_name
= "array_test_map",
1199 .key_size
= sizeof(int),
1200 .value_size
= sizeof(int),
1204 .btf_load_err
= true,
1205 .err_str
= "Invalid index",
1209 .descr
= "array test. t->size != 0\"",
1212 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
1213 /* int[16] */ /* [2] */
1214 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ARRAY
, 0, 0), 1),
1215 BTF_ARRAY_ENC(1, 1, 16),
1219 .str_sec_size
= sizeof(""),
1220 .map_type
= BPF_MAP_TYPE_ARRAY
,
1221 .map_name
= "array_test_map",
1222 .key_size
= sizeof(int),
1223 .value_size
= sizeof(int),
1227 .btf_load_err
= true,
1228 .err_str
= "size != 0",
1232 .descr
= "int test. invalid int_data",
1234 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_INT
, 0, 0), 4),
1239 .str_sec_size
= sizeof(""),
1240 .map_type
= BPF_MAP_TYPE_ARRAY
,
1241 .map_name
= "array_test_map",
1242 .key_size
= sizeof(int),
1243 .value_size
= sizeof(int),
1247 .btf_load_err
= true,
1248 .err_str
= "Invalid int_data",
1252 .descr
= "invalid BTF_INFO",
1255 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
1256 BTF_TYPE_ENC(0, 0x10000000, 4),
1260 .str_sec_size
= sizeof(""),
1261 .map_type
= BPF_MAP_TYPE_ARRAY
,
1262 .map_name
= "array_test_map",
1263 .key_size
= sizeof(int),
1264 .value_size
= sizeof(int),
1268 .btf_load_err
= true,
1269 .err_str
= "Invalid btf_info",
1273 .descr
= "fwd test. t->type != 0\"",
1276 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
1277 /* fwd type */ /* [2] */
1278 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FWD
, 0, 0), 1),
1282 .str_sec_size
= sizeof(""),
1283 .map_type
= BPF_MAP_TYPE_ARRAY
,
1284 .map_name
= "fwd_test_map",
1285 .key_size
= sizeof(int),
1286 .value_size
= sizeof(int),
1290 .btf_load_err
= true,
1291 .err_str
= "type != 0",
1295 .descr
= "typedef (invalid name, name_off = 0)",
1297 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
1298 BTF_TYPEDEF_ENC(0, 1), /* [2] */
1301 .str_sec
= "\0__int",
1302 .str_sec_size
= sizeof("\0__int"),
1303 .map_type
= BPF_MAP_TYPE_ARRAY
,
1304 .map_name
= "typedef_check_btf",
1305 .key_size
= sizeof(int),
1306 .value_size
= sizeof(int),
1310 .btf_load_err
= true,
1311 .err_str
= "Invalid name",
1315 .descr
= "typedef (invalid name, invalid identifier)",
1317 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
1318 BTF_TYPEDEF_ENC(NAME_TBD
, 1), /* [2] */
1321 .str_sec
= "\0__!int",
1322 .str_sec_size
= sizeof("\0__!int"),
1323 .map_type
= BPF_MAP_TYPE_ARRAY
,
1324 .map_name
= "typedef_check_btf",
1325 .key_size
= sizeof(int),
1326 .value_size
= sizeof(int),
1330 .btf_load_err
= true,
1331 .err_str
= "Invalid name",
1335 .descr
= "ptr type (invalid name, name_off <> 0)",
1337 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
1338 BTF_TYPE_ENC(NAME_TBD
,
1339 BTF_INFO_ENC(BTF_KIND_PTR
, 0, 0), 1), /* [2] */
1342 .str_sec
= "\0__int",
1343 .str_sec_size
= sizeof("\0__int"),
1344 .map_type
= BPF_MAP_TYPE_ARRAY
,
1345 .map_name
= "ptr_type_check_btf",
1346 .key_size
= sizeof(int),
1347 .value_size
= sizeof(int),
1351 .btf_load_err
= true,
1352 .err_str
= "Invalid name",
1356 .descr
= "volatile type (invalid name, name_off <> 0)",
1358 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
1359 BTF_TYPE_ENC(NAME_TBD
,
1360 BTF_INFO_ENC(BTF_KIND_VOLATILE
, 0, 0), 1), /* [2] */
1363 .str_sec
= "\0__int",
1364 .str_sec_size
= sizeof("\0__int"),
1365 .map_type
= BPF_MAP_TYPE_ARRAY
,
1366 .map_name
= "volatile_type_check_btf",
1367 .key_size
= sizeof(int),
1368 .value_size
= sizeof(int),
1372 .btf_load_err
= true,
1373 .err_str
= "Invalid name",
1377 .descr
= "const type (invalid name, name_off <> 0)",
1379 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
1380 BTF_TYPE_ENC(NAME_TBD
,
1381 BTF_INFO_ENC(BTF_KIND_CONST
, 0, 0), 1), /* [2] */
1384 .str_sec
= "\0__int",
1385 .str_sec_size
= sizeof("\0__int"),
1386 .map_type
= BPF_MAP_TYPE_ARRAY
,
1387 .map_name
= "const_type_check_btf",
1388 .key_size
= sizeof(int),
1389 .value_size
= sizeof(int),
1393 .btf_load_err
= true,
1394 .err_str
= "Invalid name",
1398 .descr
= "restrict type (invalid name, name_off <> 0)",
1400 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
1401 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR
, 0, 0), 1), /* [2] */
1402 BTF_TYPE_ENC(NAME_TBD
,
1403 BTF_INFO_ENC(BTF_KIND_RESTRICT
, 0, 0), 2), /* [3] */
1406 .str_sec
= "\0__int",
1407 .str_sec_size
= sizeof("\0__int"),
1408 .map_type
= BPF_MAP_TYPE_ARRAY
,
1409 .map_name
= "restrict_type_check_btf",
1410 .key_size
= sizeof(int),
1411 .value_size
= sizeof(int),
1415 .btf_load_err
= true,
1416 .err_str
= "Invalid name",
1420 .descr
= "fwd type (invalid name, name_off = 0)",
1422 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
1423 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FWD
, 0, 0), 0), /* [2] */
1426 .str_sec
= "\0__skb",
1427 .str_sec_size
= sizeof("\0__skb"),
1428 .map_type
= BPF_MAP_TYPE_ARRAY
,
1429 .map_name
= "fwd_type_check_btf",
1430 .key_size
= sizeof(int),
1431 .value_size
= sizeof(int),
1435 .btf_load_err
= true,
1436 .err_str
= "Invalid name",
1440 .descr
= "fwd type (invalid name, invalid identifier)",
1442 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
1443 BTF_TYPE_ENC(NAME_TBD
,
1444 BTF_INFO_ENC(BTF_KIND_FWD
, 0, 0), 0), /* [2] */
1447 .str_sec
= "\0__!skb",
1448 .str_sec_size
= sizeof("\0__!skb"),
1449 .map_type
= BPF_MAP_TYPE_ARRAY
,
1450 .map_name
= "fwd_type_check_btf",
1451 .key_size
= sizeof(int),
1452 .value_size
= sizeof(int),
1456 .btf_load_err
= true,
1457 .err_str
= "Invalid name",
1461 .descr
= "array type (invalid name, name_off <> 0)",
1463 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
1464 BTF_TYPE_ENC(NAME_TBD
,
1465 BTF_INFO_ENC(BTF_KIND_ARRAY
, 0, 0), 0), /* [2] */
1466 BTF_ARRAY_ENC(1, 1, 4),
1469 .str_sec
= "\0__skb",
1470 .str_sec_size
= sizeof("\0__skb"),
1471 .map_type
= BPF_MAP_TYPE_ARRAY
,
1472 .map_name
= "array_type_check_btf",
1473 .key_size
= sizeof(int),
1474 .value_size
= sizeof(int),
1478 .btf_load_err
= true,
1479 .err_str
= "Invalid name",
1483 .descr
= "struct type (name_off = 0)",
1485 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
1487 BTF_INFO_ENC(BTF_KIND_STRUCT
, 0, 1), 4), /* [2] */
1488 BTF_MEMBER_ENC(NAME_TBD
, 1, 0),
1492 .str_sec_size
= sizeof("\0A"),
1493 .map_type
= BPF_MAP_TYPE_ARRAY
,
1494 .map_name
= "struct_type_check_btf",
1495 .key_size
= sizeof(int),
1496 .value_size
= sizeof(int),
1503 .descr
= "struct type (invalid name, invalid identifier)",
1505 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
1506 BTF_TYPE_ENC(NAME_TBD
,
1507 BTF_INFO_ENC(BTF_KIND_STRUCT
, 0, 1), 4), /* [2] */
1508 BTF_MEMBER_ENC(NAME_TBD
, 1, 0),
1511 .str_sec
= "\0A!\0B",
1512 .str_sec_size
= sizeof("\0A!\0B"),
1513 .map_type
= BPF_MAP_TYPE_ARRAY
,
1514 .map_name
= "struct_type_check_btf",
1515 .key_size
= sizeof(int),
1516 .value_size
= sizeof(int),
1520 .btf_load_err
= true,
1521 .err_str
= "Invalid name",
1525 .descr
= "struct member (name_off = 0)",
1527 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
1529 BTF_INFO_ENC(BTF_KIND_STRUCT
, 0, 1), 4), /* [2] */
1530 BTF_MEMBER_ENC(NAME_TBD
, 1, 0),
1534 .str_sec_size
= sizeof("\0A"),
1535 .map_type
= BPF_MAP_TYPE_ARRAY
,
1536 .map_name
= "struct_type_check_btf",
1537 .key_size
= sizeof(int),
1538 .value_size
= sizeof(int),
1545 .descr
= "struct member (invalid name, invalid identifier)",
1547 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
1548 BTF_TYPE_ENC(NAME_TBD
,
1549 BTF_INFO_ENC(BTF_KIND_STRUCT
, 0, 1), 4), /* [2] */
1550 BTF_MEMBER_ENC(NAME_TBD
, 1, 0),
1553 .str_sec
= "\0A\0B*",
1554 .str_sec_size
= sizeof("\0A\0B*"),
1555 .map_type
= BPF_MAP_TYPE_ARRAY
,
1556 .map_name
= "struct_type_check_btf",
1557 .key_size
= sizeof(int),
1558 .value_size
= sizeof(int),
1562 .btf_load_err
= true,
1563 .err_str
= "Invalid name",
1567 .descr
= "enum type (name_off = 0)",
1569 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
1571 BTF_INFO_ENC(BTF_KIND_ENUM
, 0, 1),
1572 sizeof(int)), /* [2] */
1573 BTF_ENUM_ENC(NAME_TBD
, 0),
1576 .str_sec
= "\0A\0B",
1577 .str_sec_size
= sizeof("\0A\0B"),
1578 .map_type
= BPF_MAP_TYPE_ARRAY
,
1579 .map_name
= "enum_type_check_btf",
1580 .key_size
= sizeof(int),
1581 .value_size
= sizeof(int),
1588 .descr
= "enum type (invalid name, invalid identifier)",
1590 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
1591 BTF_TYPE_ENC(NAME_TBD
,
1592 BTF_INFO_ENC(BTF_KIND_ENUM
, 0, 1),
1593 sizeof(int)), /* [2] */
1594 BTF_ENUM_ENC(NAME_TBD
, 0),
1597 .str_sec
= "\0A!\0B",
1598 .str_sec_size
= sizeof("\0A!\0B"),
1599 .map_type
= BPF_MAP_TYPE_ARRAY
,
1600 .map_name
= "enum_type_check_btf",
1601 .key_size
= sizeof(int),
1602 .value_size
= sizeof(int),
1606 .btf_load_err
= true,
1607 .err_str
= "Invalid name",
1611 .descr
= "enum member (invalid name, name_off = 0)",
1613 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
1615 BTF_INFO_ENC(BTF_KIND_ENUM
, 0, 1),
1616 sizeof(int)), /* [2] */
1621 .str_sec_size
= sizeof(""),
1622 .map_type
= BPF_MAP_TYPE_ARRAY
,
1623 .map_name
= "enum_type_check_btf",
1624 .key_size
= sizeof(int),
1625 .value_size
= sizeof(int),
1629 .btf_load_err
= true,
1630 .err_str
= "Invalid name",
1634 .descr
= "enum member (invalid name, invalid identifier)",
1636 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4), /* [1] */
1638 BTF_INFO_ENC(BTF_KIND_ENUM
, 0, 1),
1639 sizeof(int)), /* [2] */
1640 BTF_ENUM_ENC(NAME_TBD
, 0),
1644 .str_sec_size
= sizeof("\0A!"),
1645 .map_type
= BPF_MAP_TYPE_ARRAY
,
1646 .map_name
= "enum_type_check_btf",
1647 .key_size
= sizeof(int),
1648 .value_size
= sizeof(int),
1652 .btf_load_err
= true,
1653 .err_str
= "Invalid name",
1656 .descr
= "arraymap invalid btf key (a bit field)",
1659 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
1660 /* 32 bit int with 32 bit offset */ /* [2] */
1661 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 32, 32, 8),
1665 .str_sec_size
= sizeof(""),
1666 .map_type
= BPF_MAP_TYPE_ARRAY
,
1667 .map_name
= "array_map_check_btf",
1668 .key_size
= sizeof(int),
1669 .value_size
= sizeof(int),
1673 .map_create_err
= true,
1677 .descr
= "arraymap invalid btf key (!= 32 bits)",
1680 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
1681 /* 16 bit int with 0 bit offset */ /* [2] */
1682 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 16, 2),
1686 .str_sec_size
= sizeof(""),
1687 .map_type
= BPF_MAP_TYPE_ARRAY
,
1688 .map_name
= "array_map_check_btf",
1689 .key_size
= sizeof(int),
1690 .value_size
= sizeof(int),
1694 .map_create_err
= true,
1698 .descr
= "arraymap invalid btf value (too small)",
1701 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
1705 .str_sec_size
= sizeof(""),
1706 .map_type
= BPF_MAP_TYPE_ARRAY
,
1707 .map_name
= "array_map_check_btf",
1708 .key_size
= sizeof(int),
1709 /* btf_value_size < map->value_size */
1710 .value_size
= sizeof(__u64
),
1714 .map_create_err
= true,
1718 .descr
= "arraymap invalid btf value (too big)",
1721 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
1725 .str_sec_size
= sizeof(""),
1726 .map_type
= BPF_MAP_TYPE_ARRAY
,
1727 .map_name
= "array_map_check_btf",
1728 .key_size
= sizeof(int),
1729 /* btf_value_size > map->value_size */
1730 .value_size
= sizeof(__u16
),
1734 .map_create_err
= true,
1737 }; /* struct btf_raw_test raw_tests[] */
1739 static const char *get_next_str(const char *start
, const char *end
)
1741 return start
< end
- 1 ? start
+ 1 : NULL
;
1744 static int get_type_sec_size(const __u32
*raw_types
)
1748 for (i
= MAX_NR_RAW_TYPES
- 1;
1749 i
>= 0 && raw_types
[i
] != BTF_END_RAW
;
1753 return i
< 0 ? i
: i
* sizeof(raw_types
[0]);
1756 static void *btf_raw_create(const struct btf_header
*hdr
,
1757 const __u32
*raw_types
,
1759 unsigned int str_sec_size
,
1760 unsigned int *btf_size
)
1762 const char *next_str
= str
, *end_str
= str
+ str_sec_size
;
1763 unsigned int size_needed
, offset
;
1764 struct btf_header
*ret_hdr
;
1765 int i
, type_sec_size
;
1766 uint32_t *ret_types
;
1769 type_sec_size
= get_type_sec_size(raw_types
);
1770 if (CHECK(type_sec_size
< 0, "Cannot get nr_raw_types"))
1773 size_needed
= sizeof(*hdr
) + type_sec_size
+ str_sec_size
;
1774 raw_btf
= malloc(size_needed
);
1775 if (CHECK(!raw_btf
, "Cannot allocate memory for raw_btf"))
1779 memcpy(raw_btf
, hdr
, sizeof(*hdr
));
1780 offset
= sizeof(*hdr
);
1782 /* Copy type section */
1783 ret_types
= raw_btf
+ offset
;
1784 for (i
= 0; i
< type_sec_size
/ sizeof(raw_types
[0]); i
++) {
1785 if (raw_types
[i
] == NAME_TBD
) {
1786 next_str
= get_next_str(next_str
, end_str
);
1787 if (CHECK(!next_str
, "Error in getting next_str")) {
1791 ret_types
[i
] = next_str
- str
;
1792 next_str
+= strlen(next_str
);
1794 ret_types
[i
] = raw_types
[i
];
1797 offset
+= type_sec_size
;
1799 /* Copy string section */
1800 memcpy(raw_btf
+ offset
, str
, str_sec_size
);
1802 ret_hdr
= (struct btf_header
*)raw_btf
;
1803 ret_hdr
->type_len
= type_sec_size
;
1804 ret_hdr
->str_off
= type_sec_size
;
1805 ret_hdr
->str_len
= str_sec_size
;
1807 *btf_size
= size_needed
;
1812 static int do_test_raw(unsigned int test_num
)
1814 struct btf_raw_test
*test
= &raw_tests
[test_num
- 1];
1815 struct bpf_create_map_attr create_attr
= {};
1816 int map_fd
= -1, btf_fd
= -1;
1817 unsigned int raw_btf_size
;
1818 struct btf_header
*hdr
;
1822 fprintf(stderr
, "BTF raw test[%u] (%s): ", test_num
, test
->descr
);
1823 raw_btf
= btf_raw_create(&hdr_tmpl
,
1834 hdr
->hdr_len
= (int)hdr
->hdr_len
+ test
->hdr_len_delta
;
1835 hdr
->type_off
= (int)hdr
->type_off
+ test
->type_off_delta
;
1836 hdr
->str_off
= (int)hdr
->str_off
+ test
->str_off_delta
;
1837 hdr
->str_len
= (int)hdr
->str_len
+ test
->str_len_delta
;
1839 *btf_log_buf
= '\0';
1840 btf_fd
= bpf_load_btf(raw_btf
, raw_btf_size
,
1841 btf_log_buf
, BTF_LOG_BUF_SIZE
,
1845 err
= ((btf_fd
== -1) != test
->btf_load_err
);
1846 if (CHECK(err
, "btf_fd:%d test->btf_load_err:%u",
1847 btf_fd
, test
->btf_load_err
) ||
1848 CHECK(test
->err_str
&& !strstr(btf_log_buf
, test
->err_str
),
1849 "expected err_str:%s", test
->err_str
)) {
1854 if (err
|| btf_fd
== -1)
1857 create_attr
.name
= test
->map_name
;
1858 create_attr
.map_type
= test
->map_type
;
1859 create_attr
.key_size
= test
->key_size
;
1860 create_attr
.value_size
= test
->value_size
;
1861 create_attr
.max_entries
= test
->max_entries
;
1862 create_attr
.btf_fd
= btf_fd
;
1863 create_attr
.btf_key_type_id
= test
->key_type_id
;
1864 create_attr
.btf_value_type_id
= test
->value_type_id
;
1866 map_fd
= bpf_create_map_xattr(&create_attr
);
1868 err
= ((map_fd
== -1) != test
->map_create_err
);
1869 CHECK(err
, "map_fd:%d test->map_create_err:%u",
1870 map_fd
, test
->map_create_err
);
1874 fprintf(stderr
, "OK");
1876 if (*btf_log_buf
&& (err
|| args
.always_log
))
1877 fprintf(stderr
, "\n%s", btf_log_buf
);
1887 static int test_raw(void)
1892 if (args
.raw_test_num
)
1893 return count_result(do_test_raw(args
.raw_test_num
));
1895 for (i
= 1; i
<= ARRAY_SIZE(raw_tests
); i
++)
1896 err
|= count_result(do_test_raw(i
));
1901 struct btf_get_info_test
{
1903 const char *str_sec
;
1904 __u32 raw_types
[MAX_NR_RAW_TYPES
];
1907 int (*special_test
)(unsigned int test_num
);
1910 static int test_big_btf_info(unsigned int test_num
);
1911 static int test_btf_id(unsigned int test_num
);
1913 const struct btf_get_info_test get_info_tests
[] = {
1915 .descr
= "== raw_btf_size+1",
1918 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
1922 .str_sec_size
= sizeof(""),
1923 .btf_size_delta
= 1,
1926 .descr
= "== raw_btf_size-3",
1929 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
1933 .str_sec_size
= sizeof(""),
1934 .btf_size_delta
= -3,
1937 .descr
= "Large bpf_btf_info",
1940 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
1944 .str_sec_size
= sizeof(""),
1945 .special_test
= test_big_btf_info
,
1951 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED
, 0, 32, 4),
1952 /* unsigned int */ /* [2] */
1953 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),
1957 .str_sec_size
= sizeof(""),
1958 .special_test
= test_btf_id
,
1962 static inline __u64
ptr_to_u64(const void *ptr
)
1964 return (__u64
)(unsigned long)ptr
;
1967 static int test_big_btf_info(unsigned int test_num
)
1969 const struct btf_get_info_test
*test
= &get_info_tests
[test_num
- 1];
1970 uint8_t *raw_btf
= NULL
, *user_btf
= NULL
;
1971 unsigned int raw_btf_size
;
1973 struct bpf_btf_info info
;
1976 struct bpf_btf_info
*info
;
1977 int btf_fd
= -1, err
;
1980 raw_btf
= btf_raw_create(&hdr_tmpl
,
1989 *btf_log_buf
= '\0';
1991 user_btf
= malloc(raw_btf_size
);
1992 if (CHECK(!user_btf
, "!user_btf")) {
1997 btf_fd
= bpf_load_btf(raw_btf
, raw_btf_size
,
1998 btf_log_buf
, BTF_LOG_BUF_SIZE
,
2000 if (CHECK(btf_fd
== -1, "errno:%d", errno
)) {
2006 * GET_INFO should error out if the userspace info
2007 * has non zero tailing bytes.
2009 info
= &info_garbage
.info
;
2010 memset(info
, 0, sizeof(*info
));
2011 info_garbage
.garbage
= 0xdeadbeef;
2012 info_len
= sizeof(info_garbage
);
2013 info
->btf
= ptr_to_u64(user_btf
);
2014 info
->btf_size
= raw_btf_size
;
2016 err
= bpf_obj_get_info_by_fd(btf_fd
, info
, &info_len
);
2017 if (CHECK(!err
, "!err")) {
2023 * GET_INFO should succeed even info_len is larger than
2024 * the kernel supported as long as tailing bytes are zero.
2025 * The kernel supported info len should also be returned
2028 info_garbage
.garbage
= 0;
2029 err
= bpf_obj_get_info_by_fd(btf_fd
, info
, &info_len
);
2030 if (CHECK(err
|| info_len
!= sizeof(*info
),
2031 "err:%d errno:%d info_len:%u sizeof(*info):%lu",
2032 err
, errno
, info_len
, sizeof(*info
))) {
2037 fprintf(stderr
, "OK");
2040 if (*btf_log_buf
&& (err
|| args
.always_log
))
2041 fprintf(stderr
, "\n%s", btf_log_buf
);
2052 static int test_btf_id(unsigned int test_num
)
2054 const struct btf_get_info_test
*test
= &get_info_tests
[test_num
- 1];
2055 struct bpf_create_map_attr create_attr
= {};
2056 uint8_t *raw_btf
= NULL
, *user_btf
[2] = {};
2057 int btf_fd
[2] = {-1, -1}, map_fd
= -1;
2058 struct bpf_map_info map_info
= {};
2059 struct bpf_btf_info info
[2] = {};
2060 unsigned int raw_btf_size
;
2064 raw_btf
= btf_raw_create(&hdr_tmpl
,
2073 *btf_log_buf
= '\0';
2075 for (i
= 0; i
< 2; i
++) {
2076 user_btf
[i
] = malloc(raw_btf_size
);
2077 if (CHECK(!user_btf
[i
], "!user_btf[%d]", i
)) {
2081 info
[i
].btf
= ptr_to_u64(user_btf
[i
]);
2082 info
[i
].btf_size
= raw_btf_size
;
2085 btf_fd
[0] = bpf_load_btf(raw_btf
, raw_btf_size
,
2086 btf_log_buf
, BTF_LOG_BUF_SIZE
,
2088 if (CHECK(btf_fd
[0] == -1, "errno:%d", errno
)) {
2093 /* Test BPF_OBJ_GET_INFO_BY_ID on btf_id */
2094 info_len
= sizeof(info
[0]);
2095 err
= bpf_obj_get_info_by_fd(btf_fd
[0], &info
[0], &info_len
);
2096 if (CHECK(err
, "errno:%d", errno
)) {
2101 btf_fd
[1] = bpf_btf_get_fd_by_id(info
[0].id
);
2102 if (CHECK(btf_fd
[1] == -1, "errno:%d", errno
)) {
2108 err
= bpf_obj_get_info_by_fd(btf_fd
[1], &info
[1], &info_len
);
2109 if (CHECK(err
|| info
[0].id
!= info
[1].id
||
2110 info
[0].btf_size
!= info
[1].btf_size
||
2111 (ret
= memcmp(user_btf
[0], user_btf
[1], info
[0].btf_size
)),
2112 "err:%d errno:%d id0:%u id1:%u btf_size0:%u btf_size1:%u memcmp:%d",
2113 err
, errno
, info
[0].id
, info
[1].id
,
2114 info
[0].btf_size
, info
[1].btf_size
, ret
)) {
2119 /* Test btf members in struct bpf_map_info */
2120 create_attr
.name
= "test_btf_id";
2121 create_attr
.map_type
= BPF_MAP_TYPE_ARRAY
;
2122 create_attr
.key_size
= sizeof(int);
2123 create_attr
.value_size
= sizeof(unsigned int);
2124 create_attr
.max_entries
= 4;
2125 create_attr
.btf_fd
= btf_fd
[0];
2126 create_attr
.btf_key_type_id
= 1;
2127 create_attr
.btf_value_type_id
= 2;
2129 map_fd
= bpf_create_map_xattr(&create_attr
);
2130 if (CHECK(map_fd
== -1, "errno:%d", errno
)) {
2135 info_len
= sizeof(map_info
);
2136 err
= bpf_obj_get_info_by_fd(map_fd
, &map_info
, &info_len
);
2137 if (CHECK(err
|| map_info
.btf_id
!= info
[0].id
||
2138 map_info
.btf_key_type_id
!= 1 || map_info
.btf_value_type_id
!= 2,
2139 "err:%d errno:%d info.id:%u btf_id:%u btf_key_type_id:%u btf_value_type_id:%u",
2140 err
, errno
, info
[0].id
, map_info
.btf_id
, map_info
.btf_key_type_id
,
2141 map_info
.btf_value_type_id
)) {
2146 for (i
= 0; i
< 2; i
++) {
2151 /* Test BTF ID is removed from the kernel */
2152 btf_fd
[0] = bpf_btf_get_fd_by_id(map_info
.btf_id
);
2153 if (CHECK(btf_fd
[0] == -1, "errno:%d", errno
)) {
2160 /* The map holds the last ref to BTF and its btf_id */
2163 btf_fd
[0] = bpf_btf_get_fd_by_id(map_info
.btf_id
);
2164 if (CHECK(btf_fd
[0] != -1, "BTF lingers")) {
2169 fprintf(stderr
, "OK");
2172 if (*btf_log_buf
&& (err
|| args
.always_log
))
2173 fprintf(stderr
, "\n%s", btf_log_buf
);
2178 for (i
= 0; i
< 2; i
++) {
2180 if (btf_fd
[i
] != -1)
2187 static int do_test_get_info(unsigned int test_num
)
2189 const struct btf_get_info_test
*test
= &get_info_tests
[test_num
- 1];
2190 unsigned int raw_btf_size
, user_btf_size
, expected_nbytes
;
2191 uint8_t *raw_btf
= NULL
, *user_btf
= NULL
;
2192 struct bpf_btf_info info
= {};
2193 int btf_fd
= -1, err
, ret
;
2196 fprintf(stderr
, "BTF GET_INFO test[%u] (%s): ",
2197 test_num
, test
->descr
);
2199 if (test
->special_test
)
2200 return test
->special_test(test_num
);
2202 raw_btf
= btf_raw_create(&hdr_tmpl
,
2211 *btf_log_buf
= '\0';
2213 user_btf
= malloc(raw_btf_size
);
2214 if (CHECK(!user_btf
, "!user_btf")) {
2219 btf_fd
= bpf_load_btf(raw_btf
, raw_btf_size
,
2220 btf_log_buf
, BTF_LOG_BUF_SIZE
,
2222 if (CHECK(btf_fd
== -1, "errno:%d", errno
)) {
2227 user_btf_size
= (int)raw_btf_size
+ test
->btf_size_delta
;
2228 expected_nbytes
= min(raw_btf_size
, user_btf_size
);
2229 if (raw_btf_size
> expected_nbytes
)
2230 memset(user_btf
+ expected_nbytes
, 0xff,
2231 raw_btf_size
- expected_nbytes
);
2233 info_len
= sizeof(info
);
2234 info
.btf
= ptr_to_u64(user_btf
);
2235 info
.btf_size
= user_btf_size
;
2238 err
= bpf_obj_get_info_by_fd(btf_fd
, &info
, &info_len
);
2239 if (CHECK(err
|| !info
.id
|| info_len
!= sizeof(info
) ||
2240 info
.btf_size
!= raw_btf_size
||
2241 (ret
= memcmp(raw_btf
, user_btf
, expected_nbytes
)),
2242 "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",
2243 err
, errno
, info
.id
, info_len
, sizeof(info
),
2244 raw_btf_size
, info
.btf_size
, expected_nbytes
, ret
)) {
2249 while (expected_nbytes
< raw_btf_size
) {
2250 fprintf(stderr
, "%u...", expected_nbytes
);
2251 if (CHECK(user_btf
[expected_nbytes
++] != 0xff,
2252 "user_btf[%u]:%x != 0xff", expected_nbytes
- 1,
2253 user_btf
[expected_nbytes
- 1])) {
2259 fprintf(stderr
, "OK");
2262 if (*btf_log_buf
&& (err
|| args
.always_log
))
2263 fprintf(stderr
, "\n%s", btf_log_buf
);
2274 static int test_get_info(void)
2279 if (args
.get_info_test_num
)
2280 return count_result(do_test_get_info(args
.get_info_test_num
));
2282 for (i
= 1; i
<= ARRAY_SIZE(get_info_tests
); i
++)
2283 err
|= count_result(do_test_get_info(i
));
2288 struct btf_file_test
{
2290 bool btf_kv_notfound
;
2293 static struct btf_file_test file_tests
[] = {
2295 .file
= "test_btf_haskv.o",
2298 .file
= "test_btf_nokv.o",
2299 .btf_kv_notfound
= true,
2303 static int file_has_btf_elf(const char *fn
)
2305 Elf_Scn
*scn
= NULL
;
2311 if (CHECK(elf_version(EV_CURRENT
) == EV_NONE
,
2312 "elf_version(EV_CURRENT) == EV_NONE"))
2315 elf_fd
= open(fn
, O_RDONLY
);
2316 if (CHECK(elf_fd
== -1, "open(%s): errno:%d", fn
, errno
))
2319 elf
= elf_begin(elf_fd
, ELF_C_READ
, NULL
);
2320 if (CHECK(!elf
, "elf_begin(%s): %s", fn
, elf_errmsg(elf_errno()))) {
2325 if (CHECK(!gelf_getehdr(elf
, &ehdr
), "!gelf_getehdr(%s)", fn
)) {
2330 while ((scn
= elf_nextscn(elf
, scn
))) {
2331 const char *sh_name
;
2334 if (CHECK(gelf_getshdr(scn
, &sh
) != &sh
,
2335 "file:%s gelf_getshdr != &sh", fn
)) {
2340 sh_name
= elf_strptr(elf
, ehdr
.e_shstrndx
, sh
.sh_name
);
2341 if (!strcmp(sh_name
, BTF_ELF_SEC
)) {
2355 static int do_test_file(unsigned int test_num
)
2357 const struct btf_file_test
*test
= &file_tests
[test_num
- 1];
2358 struct bpf_object
*obj
= NULL
;
2359 struct bpf_program
*prog
;
2360 struct bpf_map
*map
;
2363 fprintf(stderr
, "BTF libbpf test[%u] (%s): ", test_num
,
2366 err
= file_has_btf_elf(test
->file
);
2371 fprintf(stderr
, "SKIP. No ELF %s found", BTF_ELF_SEC
);
2376 obj
= bpf_object__open(test
->file
);
2377 if (CHECK(IS_ERR(obj
), "obj: %ld", PTR_ERR(obj
)))
2378 return PTR_ERR(obj
);
2380 err
= bpf_object__btf_fd(obj
);
2381 if (CHECK(err
== -1, "bpf_object__btf_fd: -1"))
2384 prog
= bpf_program__next(NULL
, obj
);
2385 if (CHECK(!prog
, "Cannot find bpf_prog")) {
2390 bpf_program__set_type(prog
, BPF_PROG_TYPE_TRACEPOINT
);
2391 err
= bpf_object__load(obj
);
2392 if (CHECK(err
< 0, "bpf_object__load: %d", err
))
2395 map
= bpf_object__find_map_by_name(obj
, "btf_map");
2396 if (CHECK(!map
, "btf_map not found")) {
2401 err
= (bpf_map__btf_key_type_id(map
) == 0 || bpf_map__btf_value_type_id(map
) == 0)
2402 != test
->btf_kv_notfound
;
2403 if (CHECK(err
, "btf_key_type_id:%u btf_value_type_id:%u test->btf_kv_notfound:%u",
2404 bpf_map__btf_key_type_id(map
), bpf_map__btf_value_type_id(map
),
2405 test
->btf_kv_notfound
))
2408 fprintf(stderr
, "OK");
2411 bpf_object__close(obj
);
2415 static int test_file(void)
2420 if (args
.file_test_num
)
2421 return count_result(do_test_file(args
.file_test_num
));
2423 for (i
= 1; i
<= ARRAY_SIZE(file_tests
); i
++)
2424 err
|= count_result(do_test_file(i
));
2429 const char *pprint_enum_str
[] = {
2436 struct pprint_mapv
{
2441 uint32_t unused_bits2a
:2,
2456 static struct btf_raw_test pprint_test_template
= {
2458 /* unsighed char */ /* [1] */
2459 BTF_TYPE_INT_ENC(NAME_TBD
, 0, 0, 8, 1),
2460 /* unsigned short */ /* [2] */
2461 BTF_TYPE_INT_ENC(NAME_TBD
, 0, 0, 16, 2),
2462 /* unsigned int */ /* [3] */
2463 BTF_TYPE_INT_ENC(NAME_TBD
, 0, 0, 32, 4),
2465 BTF_TYPE_INT_ENC(NAME_TBD
, BTF_INT_SIGNED
, 0, 32, 4),
2466 /* unsigned long long */ /* [5] */
2467 BTF_TYPE_INT_ENC(NAME_TBD
, 0, 0, 64, 8),
2468 /* 2 bits */ /* [6] */
2469 BTF_TYPE_INT_ENC(0, 0, 0, 2, 2),
2470 /* 28 bits */ /* [7] */
2471 BTF_TYPE_INT_ENC(0, 0, 0, 28, 4),
2472 /* uint8_t[8] */ /* [8] */
2473 BTF_TYPE_ARRAY_ENC(9, 1, 8),
2474 /* typedef unsigned char uint8_t */ /* [9] */
2475 BTF_TYPEDEF_ENC(NAME_TBD
, 1),
2476 /* typedef unsigned short uint16_t */ /* [10] */
2477 BTF_TYPEDEF_ENC(NAME_TBD
, 2),
2478 /* typedef unsigned int uint32_t */ /* [11] */
2479 BTF_TYPEDEF_ENC(NAME_TBD
, 3),
2480 /* typedef int int32_t */ /* [12] */
2481 BTF_TYPEDEF_ENC(NAME_TBD
, 4),
2482 /* typedef unsigned long long uint64_t *//* [13] */
2483 BTF_TYPEDEF_ENC(NAME_TBD
, 5),
2484 /* union (anon) */ /* [14] */
2485 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION
, 0, 2), 8),
2486 BTF_MEMBER_ENC(NAME_TBD
, 13, 0),/* uint64_t ui64; */
2487 BTF_MEMBER_ENC(NAME_TBD
, 8, 0), /* uint8_t ui8a[8]; */
2488 /* enum (anon) */ /* [15] */
2489 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM
, 0, 4), 4),
2490 BTF_ENUM_ENC(NAME_TBD
, 0),
2491 BTF_ENUM_ENC(NAME_TBD
, 1),
2492 BTF_ENUM_ENC(NAME_TBD
, 2),
2493 BTF_ENUM_ENC(NAME_TBD
, 3),
2494 /* struct pprint_mapv */ /* [16] */
2495 BTF_TYPE_ENC(NAME_TBD
, BTF_INFO_ENC(BTF_KIND_STRUCT
, 0, 8), 32),
2496 BTF_MEMBER_ENC(NAME_TBD
, 11, 0), /* uint32_t ui32 */
2497 BTF_MEMBER_ENC(NAME_TBD
, 10, 32), /* uint16_t ui16 */
2498 BTF_MEMBER_ENC(NAME_TBD
, 12, 64), /* int32_t si32 */
2499 BTF_MEMBER_ENC(NAME_TBD
, 6, 96), /* unused_bits2a */
2500 BTF_MEMBER_ENC(NAME_TBD
, 7, 98), /* bits28 */
2501 BTF_MEMBER_ENC(NAME_TBD
, 6, 126), /* unused_bits2b */
2502 BTF_MEMBER_ENC(0, 14, 128), /* union (anon) */
2503 BTF_MEMBER_ENC(NAME_TBD
, 15, 192), /* aenum */
2506 .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",
2507 .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"),
2508 .key_size
= sizeof(unsigned int),
2509 .value_size
= sizeof(struct pprint_mapv
),
2510 .key_type_id
= 3, /* unsigned int */
2511 .value_type_id
= 16, /* struct pprint_mapv */
2512 .max_entries
= 128 * 1024,
2515 static struct btf_pprint_test_meta
{
2517 enum bpf_map_type map_type
;
2518 const char *map_name
;
2521 } pprint_tests_meta
[] = {
2523 .descr
= "BTF pretty print array",
2524 .map_type
= BPF_MAP_TYPE_ARRAY
,
2525 .map_name
= "pprint_test_array",
2526 .ordered_map
= true,
2527 .lossless_map
= true,
2531 .descr
= "BTF pretty print hash",
2532 .map_type
= BPF_MAP_TYPE_HASH
,
2533 .map_name
= "pprint_test_hash",
2534 .ordered_map
= false,
2535 .lossless_map
= true,
2539 .descr
= "BTF pretty print lru hash",
2540 .map_type
= BPF_MAP_TYPE_LRU_HASH
,
2541 .map_name
= "pprint_test_lru_hash",
2542 .ordered_map
= false,
2543 .lossless_map
= false,
2549 static void set_pprint_mapv(struct pprint_mapv
*v
, uint32_t i
)
2553 v
->unused_bits2a
= 3;
2555 v
->unused_bits2b
= 3;
2557 v
->aenum
= i
& 0x03;
2560 static int do_test_pprint(void)
2562 const struct btf_raw_test
*test
= &pprint_test_template
;
2563 struct bpf_create_map_attr create_attr
= {};
2564 unsigned int key
, nr_read_elems
;
2565 bool ordered_map
, lossless_map
;
2566 int map_fd
= -1, btf_fd
= -1;
2567 struct pprint_mapv mapv
= {};
2568 unsigned int raw_btf_size
;
2569 char expected_line
[255];
2570 FILE *pin_file
= NULL
;
2572 size_t line_len
= 0;
2578 fprintf(stderr
, "%s......", test
->descr
);
2579 raw_btf
= btf_raw_create(&hdr_tmpl
, test
->raw_types
,
2580 test
->str_sec
, test
->str_sec_size
,
2586 *btf_log_buf
= '\0';
2587 btf_fd
= bpf_load_btf(raw_btf
, raw_btf_size
,
2588 btf_log_buf
, BTF_LOG_BUF_SIZE
,
2592 if (CHECK(btf_fd
== -1, "errno:%d", errno
)) {
2597 create_attr
.name
= test
->map_name
;
2598 create_attr
.map_type
= test
->map_type
;
2599 create_attr
.key_size
= test
->key_size
;
2600 create_attr
.value_size
= test
->value_size
;
2601 create_attr
.max_entries
= test
->max_entries
;
2602 create_attr
.btf_fd
= btf_fd
;
2603 create_attr
.btf_key_type_id
= test
->key_type_id
;
2604 create_attr
.btf_value_type_id
= test
->value_type_id
;
2606 map_fd
= bpf_create_map_xattr(&create_attr
);
2607 if (CHECK(map_fd
== -1, "errno:%d", errno
)) {
2612 ret
= snprintf(pin_path
, sizeof(pin_path
), "%s/%s",
2613 "/sys/fs/bpf", test
->map_name
);
2615 if (CHECK(ret
== sizeof(pin_path
), "pin_path %s/%s is too long",
2616 "/sys/fs/bpf", test
->map_name
)) {
2621 err
= bpf_obj_pin(map_fd
, pin_path
);
2622 if (CHECK(err
, "bpf_obj_pin(%s): errno:%d.", pin_path
, errno
))
2625 for (key
= 0; key
< test
->max_entries
; key
++) {
2626 set_pprint_mapv(&mapv
, key
);
2627 bpf_map_update_elem(map_fd
, &key
, &mapv
, 0);
2630 pin_file
= fopen(pin_path
, "r");
2631 if (CHECK(!pin_file
, "fopen(%s): errno:%d", pin_path
, errno
)) {
2636 /* Skip lines start with '#' */
2637 while ((nread
= getline(&line
, &line_len
, pin_file
)) > 0 &&
2641 if (CHECK(nread
<= 0, "Unexpected EOF")) {
2647 ordered_map
= test
->ordered_map
;
2648 lossless_map
= test
->lossless_map
;
2650 ssize_t nexpected_line
;
2651 unsigned int next_key
;
2653 next_key
= ordered_map
? nr_read_elems
: atoi(line
);
2654 set_pprint_mapv(&mapv
, next_key
);
2655 nexpected_line
= snprintf(expected_line
, sizeof(expected_line
),
2656 "%u: {%u,0,%d,0x%x,0x%x,0x%x,{%lu|[%u,%u,%u,%u,%u,%u,%u,%u]},%s}\n",
2658 mapv
.ui32
, mapv
.si32
,
2659 mapv
.unused_bits2a
, mapv
.bits28
, mapv
.unused_bits2b
,
2661 mapv
.ui8a
[0], mapv
.ui8a
[1], mapv
.ui8a
[2], mapv
.ui8a
[3],
2662 mapv
.ui8a
[4], mapv
.ui8a
[5], mapv
.ui8a
[6], mapv
.ui8a
[7],
2663 pprint_enum_str
[mapv
.aenum
]);
2665 if (CHECK(nexpected_line
== sizeof(expected_line
),
2666 "expected_line is too long")) {
2671 if (strcmp(expected_line
, line
)) {
2673 fprintf(stderr
, "unexpected pprint output\n");
2674 fprintf(stderr
, "expected: %s", expected_line
);
2675 fprintf(stderr
, " read: %s", line
);
2679 nread
= getline(&line
, &line_len
, pin_file
);
2680 } while (++nr_read_elems
< test
->max_entries
&& nread
> 0);
2683 CHECK(nr_read_elems
< test
->max_entries
,
2684 "Unexpected EOF. nr_read_elems:%u test->max_entries:%u",
2685 nr_read_elems
, test
->max_entries
)) {
2690 if (CHECK(nread
> 0, "Unexpected extra pprint output: %s", line
)) {
2699 fprintf(stderr
, "OK");
2700 if (*btf_log_buf
&& (err
|| args
.always_log
))
2701 fprintf(stderr
, "\n%s", btf_log_buf
);
2714 static int test_pprint(void)
2719 for (i
= 0; i
< ARRAY_SIZE(pprint_tests_meta
); i
++) {
2720 pprint_test_template
.descr
= pprint_tests_meta
[i
].descr
;
2721 pprint_test_template
.map_type
= pprint_tests_meta
[i
].map_type
;
2722 pprint_test_template
.map_name
= pprint_tests_meta
[i
].map_name
;
2723 pprint_test_template
.ordered_map
= pprint_tests_meta
[i
].ordered_map
;
2724 pprint_test_template
.lossless_map
= pprint_tests_meta
[i
].lossless_map
;
2726 err
|= count_result(do_test_pprint());
2732 static void usage(const char *cmd
)
2734 fprintf(stderr
, "Usage: %s [-l] [[-r test_num (1 - %zu)] | [-g test_num (1 - %zu)] | [-f test_num (1 - %zu)] | [-p]]\n",
2735 cmd
, ARRAY_SIZE(raw_tests
), ARRAY_SIZE(get_info_tests
),
2736 ARRAY_SIZE(file_tests
));
2739 static int parse_args(int argc
, char **argv
)
2741 const char *optstr
= "lpf:r:g:";
2744 while ((opt
= getopt(argc
, argv
, optstr
)) != -1) {
2747 args
.always_log
= true;
2750 args
.file_test_num
= atoi(optarg
);
2751 args
.file_test
= true;
2754 args
.raw_test_num
= atoi(optarg
);
2755 args
.raw_test
= true;
2758 args
.get_info_test_num
= atoi(optarg
);
2759 args
.get_info_test
= true;
2762 args
.pprint_test
= true;
2773 if (args
.raw_test_num
&&
2774 (args
.raw_test_num
< 1 ||
2775 args
.raw_test_num
> ARRAY_SIZE(raw_tests
))) {
2776 fprintf(stderr
, "BTF raw test number must be [1 - %zu]\n",
2777 ARRAY_SIZE(raw_tests
));
2781 if (args
.file_test_num
&&
2782 (args
.file_test_num
< 1 ||
2783 args
.file_test_num
> ARRAY_SIZE(file_tests
))) {
2784 fprintf(stderr
, "BTF file test number must be [1 - %zu]\n",
2785 ARRAY_SIZE(file_tests
));
2789 if (args
.get_info_test_num
&&
2790 (args
.get_info_test_num
< 1 ||
2791 args
.get_info_test_num
> ARRAY_SIZE(get_info_tests
))) {
2792 fprintf(stderr
, "BTF get info test number must be [1 - %zu]\n",
2793 ARRAY_SIZE(get_info_tests
));
2800 static void print_summary(void)
2802 fprintf(stderr
, "PASS:%u SKIP:%u FAIL:%u\n",
2803 pass_cnt
- skip_cnt
, skip_cnt
, error_cnt
);
2806 int main(int argc
, char **argv
)
2810 err
= parse_args(argc
, argv
);
2814 if (args
.always_log
)
2815 libbpf_set_print(__base_pr
, __base_pr
, __base_pr
);
2820 if (args
.get_info_test
)
2821 err
|= test_get_info();
2826 if (args
.pprint_test
)
2827 err
|= test_pprint();
2829 if (args
.raw_test
|| args
.get_info_test
|| args
.file_test
||
2834 err
|= test_get_info();