Linux 4.19.133
[linux/fpc-iii.git] / tools / testing / selftests / bpf / test_btf.c
blob29116366a9fcefea1f5d147b99e139e3675ca8c8
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2018 Facebook */
4 #include <linux/bpf.h>
5 #include <linux/btf.h>
6 #include <linux/err.h>
7 #include <bpf/bpf.h>
8 #include <sys/resource.h>
9 #include <libelf.h>
10 #include <gelf.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <stdarg.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <errno.h>
18 #include <bpf/libbpf.h>
19 #include <bpf/btf.h>
21 #include "bpf_rlimit.h"
22 #include "bpf_util.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); \
30 if (__ret) { \
31 fprintf(stderr, "%s:%d:FAIL ", __func__, __LINE__); \
32 fprintf(stderr, format); \
33 } \
34 __ret; \
37 static int count_result(int err)
39 if (err)
40 error_cnt++;
41 else
42 pass_cnt++;
44 fprintf(stderr, "\n");
45 return err;
48 #define min(a, b) ((a) < (b) ? (a) : (b))
49 #define __printf(a, b) __attribute__((format(printf, a, b)))
51 __printf(1, 2)
52 static int __base_pr(const char *format, ...)
54 va_list args;
55 int err;
57 va_start(args, format);
58 err = vfprintf(stderr, format, args);
59 va_end(args);
60 return err;
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
97 static struct args {
98 unsigned int raw_test_num;
99 unsigned int file_test_num;
100 unsigned int get_info_test_num;
101 bool raw_test;
102 bool file_test;
103 bool get_info_test;
104 bool pprint_test;
105 bool always_log;
106 } args;
108 static char btf_log_buf[BTF_LOG_BUF_SIZE];
110 static struct btf_header hdr_tmpl = {
111 .magic = BTF_MAGIC,
112 .version = BTF_VERSION,
113 .hdr_len = sizeof(struct btf_header),
116 struct btf_raw_test {
117 const char *descr;
118 const char *str_sec;
119 const char *map_name;
120 const char *err_str;
121 __u32 raw_types[MAX_NR_RAW_TYPES];
122 __u32 str_sec_size;
123 enum bpf_map_type map_type;
124 __u32 key_size;
125 __u32 value_size;
126 __u32 key_type_id;
127 __u32 value_type_id;
128 __u32 max_entries;
129 bool btf_load_err;
130 bool map_create_err;
131 bool ordered_map;
132 bool lossless_map;
133 int hdr_len_delta;
134 int type_off_delta;
135 int str_off_delta;
136 int str_len_delta;
139 static struct btf_raw_test raw_tests[] = {
140 /* enum E {
141 * E0,
142 * E1,
143 * };
145 * struct A {
146 * unsigned long long m;
147 * int n;
148 * char o;
149 * [3 bytes hole]
150 * int p[8];
151 * int q[4][8];
152 * enum E r;
153 * };
156 .descr = "struct test #1",
157 .raw_types = {
158 /* int */
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] */
162 /* char */
163 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1), /* [3] */
164 /* int[8] */
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 */
174 /* } */
175 /* int[4][8] */
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),
181 BTF_END_RAW,
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),
188 .value_size = 180,
189 .key_type_id = 1,
190 .value_type_id = 5,
191 .max_entries = 4,
194 /* typedef struct b Struct_B;
196 * struct A {
197 * int m;
198 * struct b n[4];
199 * const Struct_B o[4];
200 * };
202 * struct B {
203 * int m;
204 * int n;
205 * };
208 .descr = "struct test #2",
209 .raw_types = {
210 /* int */ /* [1] */
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];*/
220 /* } */
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; */
226 /* } */
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),
236 BTF_END_RAW,
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),
243 .value_size = 68,
244 .key_type_id = 1,
245 .value_type_id = 3,
246 .max_entries = 4,
250 .descr = "struct test #3 Invalid member offset",
251 .raw_types = {
252 /* int */ /* [1] */
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; */
261 /* } */
262 BTF_END_RAW,
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),
269 .value_size = 16,
270 .key_type_id = 1,
271 .value_type_id = 3,
272 .max_entries = 4,
273 .btf_load_err = true,
274 .err_str = "Invalid member bits_offset",
277 /* Test member exceeds the size of struct.
279 * struct A {
280 * int m;
281 * int n;
282 * };
285 .descr = "size check test #1",
286 .raw_types = {
287 /* int */ /* [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; */
293 /* } */
294 BTF_END_RAW,
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),
301 .value_size = 1,
302 .key_type_id = 1,
303 .value_type_id = 2,
304 .max_entries = 4,
305 .btf_load_err = true,
306 .err_str = "Member exceeds struct_size",
309 /* Test member exeeds the size of struct
311 * struct A {
312 * int m;
313 * int n[2];
314 * };
317 .descr = "size check test #2",
318 .raw_types = {
319 /* int */ /* [1] */
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]; */
327 /* } */
328 BTF_END_RAW,
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),
335 .value_size = 1,
336 .key_type_id = 1,
337 .value_type_id = 3,
338 .max_entries = 4,
339 .btf_load_err = true,
340 .err_str = "Member exceeds struct_size",
343 /* Test member exeeds the size of struct
345 * struct A {
346 * int m;
347 * void *n;
348 * };
351 .descr = "size check test #3",
352 .raw_types = {
353 /* int */ /* [1] */
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; */
361 /* } */
362 BTF_END_RAW,
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),
369 .value_size = 1,
370 .key_type_id = 1,
371 .value_type_id = 3,
372 .max_entries = 4,
373 .btf_load_err = true,
374 .err_str = "Member exceeds struct_size",
377 /* Test member exceeds the size of struct
379 * enum E {
380 * E0,
381 * E1,
382 * };
384 * struct A {
385 * int m;
386 * enum E n;
387 * };
390 .descr = "size check test #4",
391 .raw_types = {
392 /* int */ /* [1] */
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),
398 /* } */
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; */
403 /* } */
404 BTF_END_RAW,
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),
411 .value_size = 1,
412 .key_type_id = 1,
413 .value_type_id = 3,
414 .max_entries = 4,
415 .btf_load_err = true,
416 .err_str = "Member exceeds struct_size",
419 /* typedef const void * const_void_ptr;
420 * struct A {
421 * const_void_ptr m;
422 * };
425 .descr = "void test #1",
426 .raw_types = {
427 /* int */ /* [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),
439 /* } */
440 BTF_END_RAW,
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 *),
448 .key_type_id = 1,
449 .value_type_id = 4,
450 .max_entries = 4,
453 /* struct A {
454 * const void m;
455 * };
458 .descr = "void test #2",
459 .raw_types = {
460 /* int */ /* [1] */
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),
466 /* const void m; */
467 BTF_MEMBER_ENC(NAME_TBD, 2, 0),
468 /* } */
469 BTF_END_RAW,
471 .str_sec = "\0A\0m",
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 *),
477 .key_type_id = 1,
478 .value_type_id = 3,
479 .max_entries = 4,
480 .btf_load_err = true,
481 .err_str = "Invalid member",
484 /* typedef const void * const_void_ptr;
485 * const_void_ptr[4]
488 .descr = "void test #3",
489 .raw_types = {
490 /* int */ /* [1] */
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] */
500 BTF_END_RAW,
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,
508 .key_type_id = 1,
509 .value_type_id = 5,
510 .max_entries = 4,
513 /* const void[4] */
515 .descr = "void test #4",
516 .raw_types = {
517 /* int */ /* [1] */
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),
523 BTF_END_RAW,
525 .str_sec = "\0A\0m",
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,
531 .key_type_id = 1,
532 .value_type_id = 3,
533 .max_entries = 4,
534 .btf_load_err = true,
535 .err_str = "Invalid elem",
538 /* Array_A <------------------+
539 * elem_type == Array_B |
540 * | |
541 * | |
542 * Array_B <-------- + |
543 * elem_type == Array A --+
546 .descr = "loop test #1",
547 .raw_types = {
548 /* int */ /* [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),
554 BTF_END_RAW,
556 .str_sec = "",
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),
562 .key_type_id = 1,
563 .value_type_id = 2,
564 .max_entries = 4,
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 |
575 * | |
576 * | |
577 * Array_B <-------- + |
578 * elem_type == Array_A --+
581 .descr = "loop test #2",
582 .raw_types = {
583 /* int */
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] */
587 /* Array_A */
588 BTF_TYPE_ARRAY_ENC(2, 1, 8), /* [3] */
589 /* Array_B */
590 BTF_TYPE_ARRAY_ENC(3, 1, 8), /* [4] */
591 BTF_END_RAW,
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),
599 .key_type_id = 1,
600 .value_type_id = 2,
601 .max_entries = 4,
602 .btf_load_err = true,
603 .err_str = "Loop detected",
606 /* Array_A <------------------+
607 * elem_type == Array_B |
608 * | |
609 * | |
610 * Array_B <-------- + |
611 * elem_type == Array_A --+
614 .descr = "loop test #3",
615 .raw_types = {
616 /* int */ /* [1] */
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),
622 BTF_END_RAW,
624 .str_sec = "",
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),
630 .key_type_id = 1,
631 .value_type_id = 2,
632 .max_entries = 4,
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 |
643 * | |
644 * | |
645 * Array_B <-------- + |
646 * elem_type == Array_A --+
649 .descr = "loop test #4",
650 .raw_types = {
651 /* int */ /* [1] */
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),
659 BTF_END_RAW,
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),
667 .key_type_id = 1,
668 .value_type_id = 2,
669 .max_entries = 4,
670 .btf_load_err = true,
671 .err_str = "Loop detected",
674 /* typedef struct B Struct_B
676 * struct A {
677 * int x;
678 * Struct_B y;
679 * };
681 * struct B {
682 * int x;
683 * struct A y;
684 * };
687 .descr = "loop test #5",
688 .raw_types = {
689 /* int */
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; */
701 BTF_END_RAW,
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),
708 .value_size = 8,
709 .key_type_id = 1,
710 .value_type_id = 2,
711 .max_entries = 4,
712 .btf_load_err = true,
713 .err_str = "Loop detected",
716 /* struct A {
717 * int x;
718 * struct A array_a[4];
719 * };
722 .descr = "loop test #6",
723 .raw_types = {
724 /* int */
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]; */
731 BTF_END_RAW,
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),
738 .value_size = 8,
739 .key_type_id = 1,
740 .value_type_id = 2,
741 .max_entries = 4,
742 .btf_load_err = true,
743 .err_str = "Loop detected",
747 .descr = "loop test #7",
748 .raw_types = {
749 /* int */ /* [1] */
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 *)),
753 /* const void *m; */
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),
759 BTF_END_RAW,
761 .str_sec = "\0A\0m",
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 *),
767 .key_type_id = 1,
768 .value_type_id = 2,
769 .max_entries = 4,
770 .btf_load_err = true,
771 .err_str = "Loop detected",
775 .descr = "loop test #8",
776 .raw_types = {
777 /* int */ /* [1] */
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 *)),
781 /* const void *m; */
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 *)),
785 /* const void *n; */
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),
795 BTF_END_RAW,
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 *),
803 .key_type_id = 1,
804 .value_type_id = 2,
805 .max_entries = 4,
806 .btf_load_err = true,
807 .err_str = "Loop detected",
811 .descr = "string section does not end with null",
812 .raw_types = {
813 /* int */ /* [1] */
814 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
815 BTF_END_RAW,
817 .str_sec = "\0int",
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),
823 .key_type_id = 1,
824 .value_type_id = 1,
825 .max_entries = 4,
826 .btf_load_err = true,
827 .err_str = "Invalid string section",
831 .descr = "empty string section",
832 .raw_types = {
833 /* int */ /* [1] */
834 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
835 BTF_END_RAW,
837 .str_sec = "",
838 .str_sec_size = 0,
839 .map_type = BPF_MAP_TYPE_ARRAY,
840 .map_name = "hdr_test_map",
841 .key_size = sizeof(int),
842 .value_size = sizeof(int),
843 .key_type_id = 1,
844 .value_type_id = 1,
845 .max_entries = 4,
846 .btf_load_err = true,
847 .err_str = "Invalid string section",
851 .descr = "empty type section",
852 .raw_types = {
853 BTF_END_RAW,
855 .str_sec = "\0int",
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),
861 .key_type_id = 1,
862 .value_type_id = 1,
863 .max_entries = 4,
864 .btf_load_err = true,
865 .err_str = "No type found",
869 .descr = "btf_header test. Longer hdr_len",
870 .raw_types = {
871 /* int */ /* [1] */
872 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
873 BTF_END_RAW,
875 .str_sec = "\0int",
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),
881 .key_type_id = 1,
882 .value_type_id = 1,
883 .max_entries = 4,
884 .btf_load_err = true,
885 .hdr_len_delta = 4,
886 .err_str = "Unsupported btf_header",
890 .descr = "btf_header test. Gap between hdr and type",
891 .raw_types = {
892 /* int */ /* [1] */
893 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
894 BTF_END_RAW,
896 .str_sec = "\0int",
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),
902 .key_type_id = 1,
903 .value_type_id = 1,
904 .max_entries = 4,
905 .btf_load_err = true,
906 .type_off_delta = 4,
907 .err_str = "Unsupported section found",
911 .descr = "btf_header test. Gap between type and str",
912 .raw_types = {
913 /* int */ /* [1] */
914 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
915 BTF_END_RAW,
917 .str_sec = "\0int",
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),
923 .key_type_id = 1,
924 .value_type_id = 1,
925 .max_entries = 4,
926 .btf_load_err = true,
927 .str_off_delta = 4,
928 .err_str = "Unsupported section found",
932 .descr = "btf_header test. Overlap between type and str",
933 .raw_types = {
934 /* int */ /* [1] */
935 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
936 BTF_END_RAW,
938 .str_sec = "\0int",
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),
944 .key_type_id = 1,
945 .value_type_id = 1,
946 .max_entries = 4,
947 .btf_load_err = true,
948 .str_off_delta = -4,
949 .err_str = "Section overlap found",
953 .descr = "btf_header test. Larger BTF size",
954 .raw_types = {
955 /* int */ /* [1] */
956 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
957 BTF_END_RAW,
959 .str_sec = "\0int",
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),
965 .key_type_id = 1,
966 .value_type_id = 1,
967 .max_entries = 4,
968 .btf_load_err = true,
969 .str_len_delta = -4,
970 .err_str = "Unsupported section found",
974 .descr = "btf_header test. Smaller BTF size",
975 .raw_types = {
976 /* int */ /* [1] */
977 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
978 BTF_END_RAW,
980 .str_sec = "\0int",
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),
986 .key_type_id = 1,
987 .value_type_id = 1,
988 .max_entries = 4,
989 .btf_load_err = true,
990 .str_len_delta = 4,
991 .err_str = "Total section length too long",
995 .descr = "array test. index_type/elem_type \"int\"",
996 .raw_types = {
997 /* int */ /* [1] */
998 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
999 /* int[16] */ /* [2] */
1000 BTF_TYPE_ARRAY_ENC(1, 1, 16),
1001 BTF_END_RAW,
1003 .str_sec = "",
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),
1009 .key_type_id = 1,
1010 .value_type_id = 1,
1011 .max_entries = 4,
1015 .descr = "array test. index_type/elem_type \"const int\"",
1016 .raw_types = {
1017 /* int */ /* [1] */
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),
1023 BTF_END_RAW,
1025 .str_sec = "",
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),
1031 .key_type_id = 1,
1032 .value_type_id = 1,
1033 .max_entries = 4,
1037 .descr = "array test. index_type \"const int:31\"",
1038 .raw_types = {
1039 /* int */ /* [1] */
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),
1047 BTF_END_RAW,
1049 .str_sec = "",
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),
1055 .key_type_id = 1,
1056 .value_type_id = 1,
1057 .max_entries = 4,
1058 .btf_load_err = true,
1059 .err_str = "Invalid index",
1063 .descr = "array test. elem_type \"const int:31\"",
1064 .raw_types = {
1065 /* int */ /* [1] */
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),
1073 BTF_END_RAW,
1075 .str_sec = "",
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),
1081 .key_type_id = 1,
1082 .value_type_id = 1,
1083 .max_entries = 4,
1084 .btf_load_err = true,
1085 .err_str = "Invalid array of int",
1089 .descr = "array test. index_type \"void\"",
1090 .raw_types = {
1091 /* int */ /* [1] */
1092 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1093 /* int[16] */ /* [2] */
1094 BTF_TYPE_ARRAY_ENC(1, 0, 16),
1095 BTF_END_RAW,
1097 .str_sec = "",
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),
1103 .key_type_id = 1,
1104 .value_type_id = 1,
1105 .max_entries = 4,
1106 .btf_load_err = true,
1107 .err_str = "Invalid index",
1111 .descr = "array test. index_type \"const void\"",
1112 .raw_types = {
1113 /* int */ /* [1] */
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),
1119 BTF_END_RAW,
1121 .str_sec = "",
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),
1127 .key_type_id = 1,
1128 .value_type_id = 1,
1129 .max_entries = 4,
1130 .btf_load_err = true,
1131 .err_str = "Invalid index",
1135 .descr = "array test. elem_type \"const void\"",
1136 .raw_types = {
1137 /* int */ /* [1] */
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),
1143 BTF_END_RAW,
1145 .str_sec = "",
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),
1151 .key_type_id = 1,
1152 .value_type_id = 1,
1153 .max_entries = 4,
1154 .btf_load_err = true,
1155 .err_str = "Invalid elem",
1159 .descr = "array test. elem_type \"const void *\"",
1160 .raw_types = {
1161 /* int */ /* [1] */
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),
1169 BTF_END_RAW,
1171 .str_sec = "",
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),
1177 .key_type_id = 1,
1178 .value_type_id = 1,
1179 .max_entries = 4,
1183 .descr = "array test. index_type \"const void *\"",
1184 .raw_types = {
1185 /* int */ /* [1] */
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),
1193 BTF_END_RAW,
1195 .str_sec = "",
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),
1201 .key_type_id = 1,
1202 .value_type_id = 1,
1203 .max_entries = 4,
1204 .btf_load_err = true,
1205 .err_str = "Invalid index",
1209 .descr = "array test. t->size != 0\"",
1210 .raw_types = {
1211 /* int */ /* [1] */
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),
1216 BTF_END_RAW,
1218 .str_sec = "",
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),
1224 .key_type_id = 1,
1225 .value_type_id = 1,
1226 .max_entries = 4,
1227 .btf_load_err = true,
1228 .err_str = "size != 0",
1232 .descr = "int test. invalid int_data",
1233 .raw_types = {
1234 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), 4),
1235 0x10000000,
1236 BTF_END_RAW,
1238 .str_sec = "",
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),
1244 .key_type_id = 1,
1245 .value_type_id = 1,
1246 .max_entries = 4,
1247 .btf_load_err = true,
1248 .err_str = "Invalid int_data",
1252 .descr = "invalid BTF_INFO",
1253 .raw_types = {
1254 /* int */ /* [1] */
1255 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1256 BTF_TYPE_ENC(0, 0x10000000, 4),
1257 BTF_END_RAW,
1259 .str_sec = "",
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),
1265 .key_type_id = 1,
1266 .value_type_id = 1,
1267 .max_entries = 4,
1268 .btf_load_err = true,
1269 .err_str = "Invalid btf_info",
1273 .descr = "fwd test. t->type != 0\"",
1274 .raw_types = {
1275 /* int */ /* [1] */
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),
1279 BTF_END_RAW,
1281 .str_sec = "",
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),
1287 .key_type_id = 1,
1288 .value_type_id = 1,
1289 .max_entries = 4,
1290 .btf_load_err = true,
1291 .err_str = "type != 0",
1295 .descr = "typedef (invalid name, name_off = 0)",
1296 .raw_types = {
1297 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
1298 BTF_TYPEDEF_ENC(0, 1), /* [2] */
1299 BTF_END_RAW,
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),
1307 .key_type_id = 1,
1308 .value_type_id = 1,
1309 .max_entries = 4,
1310 .btf_load_err = true,
1311 .err_str = "Invalid name",
1315 .descr = "typedef (invalid name, invalid identifier)",
1316 .raw_types = {
1317 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
1318 BTF_TYPEDEF_ENC(NAME_TBD, 1), /* [2] */
1319 BTF_END_RAW,
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),
1327 .key_type_id = 1,
1328 .value_type_id = 1,
1329 .max_entries = 4,
1330 .btf_load_err = true,
1331 .err_str = "Invalid name",
1335 .descr = "ptr type (invalid name, name_off <> 0)",
1336 .raw_types = {
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] */
1340 BTF_END_RAW,
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),
1348 .key_type_id = 1,
1349 .value_type_id = 1,
1350 .max_entries = 4,
1351 .btf_load_err = true,
1352 .err_str = "Invalid name",
1356 .descr = "volatile type (invalid name, name_off <> 0)",
1357 .raw_types = {
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] */
1361 BTF_END_RAW,
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),
1369 .key_type_id = 1,
1370 .value_type_id = 1,
1371 .max_entries = 4,
1372 .btf_load_err = true,
1373 .err_str = "Invalid name",
1377 .descr = "const type (invalid name, name_off <> 0)",
1378 .raw_types = {
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] */
1382 BTF_END_RAW,
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),
1390 .key_type_id = 1,
1391 .value_type_id = 1,
1392 .max_entries = 4,
1393 .btf_load_err = true,
1394 .err_str = "Invalid name",
1398 .descr = "restrict type (invalid name, name_off <> 0)",
1399 .raw_types = {
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] */
1404 BTF_END_RAW,
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),
1412 .key_type_id = 1,
1413 .value_type_id = 1,
1414 .max_entries = 4,
1415 .btf_load_err = true,
1416 .err_str = "Invalid name",
1420 .descr = "fwd type (invalid name, name_off = 0)",
1421 .raw_types = {
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] */
1424 BTF_END_RAW,
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),
1432 .key_type_id = 1,
1433 .value_type_id = 1,
1434 .max_entries = 4,
1435 .btf_load_err = true,
1436 .err_str = "Invalid name",
1440 .descr = "fwd type (invalid name, invalid identifier)",
1441 .raw_types = {
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] */
1445 BTF_END_RAW,
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),
1453 .key_type_id = 1,
1454 .value_type_id = 1,
1455 .max_entries = 4,
1456 .btf_load_err = true,
1457 .err_str = "Invalid name",
1461 .descr = "array type (invalid name, name_off <> 0)",
1462 .raw_types = {
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),
1467 BTF_END_RAW,
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),
1475 .key_type_id = 1,
1476 .value_type_id = 1,
1477 .max_entries = 4,
1478 .btf_load_err = true,
1479 .err_str = "Invalid name",
1483 .descr = "struct type (name_off = 0)",
1484 .raw_types = {
1485 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
1486 BTF_TYPE_ENC(0,
1487 BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4), /* [2] */
1488 BTF_MEMBER_ENC(NAME_TBD, 1, 0),
1489 BTF_END_RAW,
1491 .str_sec = "\0A",
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),
1497 .key_type_id = 1,
1498 .value_type_id = 1,
1499 .max_entries = 4,
1503 .descr = "struct type (invalid name, invalid identifier)",
1504 .raw_types = {
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),
1509 BTF_END_RAW,
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),
1517 .key_type_id = 1,
1518 .value_type_id = 1,
1519 .max_entries = 4,
1520 .btf_load_err = true,
1521 .err_str = "Invalid name",
1525 .descr = "struct member (name_off = 0)",
1526 .raw_types = {
1527 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
1528 BTF_TYPE_ENC(0,
1529 BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4), /* [2] */
1530 BTF_MEMBER_ENC(NAME_TBD, 1, 0),
1531 BTF_END_RAW,
1533 .str_sec = "\0A",
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),
1539 .key_type_id = 1,
1540 .value_type_id = 1,
1541 .max_entries = 4,
1545 .descr = "struct member (invalid name, invalid identifier)",
1546 .raw_types = {
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),
1551 BTF_END_RAW,
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),
1559 .key_type_id = 1,
1560 .value_type_id = 1,
1561 .max_entries = 4,
1562 .btf_load_err = true,
1563 .err_str = "Invalid name",
1567 .descr = "enum type (name_off = 0)",
1568 .raw_types = {
1569 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
1570 BTF_TYPE_ENC(0,
1571 BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1),
1572 sizeof(int)), /* [2] */
1573 BTF_ENUM_ENC(NAME_TBD, 0),
1574 BTF_END_RAW,
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),
1582 .key_type_id = 1,
1583 .value_type_id = 1,
1584 .max_entries = 4,
1588 .descr = "enum type (invalid name, invalid identifier)",
1589 .raw_types = {
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),
1595 BTF_END_RAW,
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),
1603 .key_type_id = 1,
1604 .value_type_id = 1,
1605 .max_entries = 4,
1606 .btf_load_err = true,
1607 .err_str = "Invalid name",
1611 .descr = "enum member (invalid name, name_off = 0)",
1612 .raw_types = {
1613 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
1614 BTF_TYPE_ENC(0,
1615 BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1),
1616 sizeof(int)), /* [2] */
1617 BTF_ENUM_ENC(0, 0),
1618 BTF_END_RAW,
1620 .str_sec = "",
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),
1626 .key_type_id = 1,
1627 .value_type_id = 1,
1628 .max_entries = 4,
1629 .btf_load_err = true,
1630 .err_str = "Invalid name",
1634 .descr = "enum member (invalid name, invalid identifier)",
1635 .raw_types = {
1636 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
1637 BTF_TYPE_ENC(0,
1638 BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1),
1639 sizeof(int)), /* [2] */
1640 BTF_ENUM_ENC(NAME_TBD, 0),
1641 BTF_END_RAW,
1643 .str_sec = "\0A!",
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),
1649 .key_type_id = 1,
1650 .value_type_id = 1,
1651 .max_entries = 4,
1652 .btf_load_err = true,
1653 .err_str = "Invalid name",
1656 .descr = "arraymap invalid btf key (a bit field)",
1657 .raw_types = {
1658 /* int */ /* [1] */
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),
1662 BTF_END_RAW,
1664 .str_sec = "",
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),
1670 .key_type_id = 2,
1671 .value_type_id = 1,
1672 .max_entries = 4,
1673 .map_create_err = true,
1677 .descr = "arraymap invalid btf key (!= 32 bits)",
1678 .raw_types = {
1679 /* int */ /* [1] */
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),
1683 BTF_END_RAW,
1685 .str_sec = "",
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),
1691 .key_type_id = 2,
1692 .value_type_id = 1,
1693 .max_entries = 4,
1694 .map_create_err = true,
1698 .descr = "arraymap invalid btf value (too small)",
1699 .raw_types = {
1700 /* int */ /* [1] */
1701 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1702 BTF_END_RAW,
1704 .str_sec = "",
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),
1711 .key_type_id = 1,
1712 .value_type_id = 1,
1713 .max_entries = 4,
1714 .map_create_err = true,
1718 .descr = "arraymap invalid btf value (too big)",
1719 .raw_types = {
1720 /* int */ /* [1] */
1721 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1722 BTF_END_RAW,
1724 .str_sec = "",
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),
1731 .key_type_id = 1,
1732 .value_type_id = 1,
1733 .max_entries = 4,
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)
1746 int i;
1748 for (i = MAX_NR_RAW_TYPES - 1;
1749 i >= 0 && raw_types[i] != BTF_END_RAW;
1750 i--)
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,
1758 const char *str,
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;
1767 void *raw_btf;
1769 type_sec_size = get_type_sec_size(raw_types);
1770 if (CHECK(type_sec_size < 0, "Cannot get nr_raw_types"))
1771 return NULL;
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"))
1776 return NULL;
1778 /* Copy header */
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")) {
1788 free(raw_btf);
1789 return NULL;
1791 ret_types[i] = next_str - str;
1792 next_str += strlen(next_str);
1793 } else {
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;
1809 return raw_btf;
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;
1819 void *raw_btf;
1820 int err;
1822 fprintf(stderr, "BTF raw test[%u] (%s): ", test_num, test->descr);
1823 raw_btf = btf_raw_create(&hdr_tmpl,
1824 test->raw_types,
1825 test->str_sec,
1826 test->str_sec_size,
1827 &raw_btf_size);
1829 if (!raw_btf)
1830 return -1;
1832 hdr = raw_btf;
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,
1842 args.always_log);
1843 free(raw_btf);
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)) {
1850 err = -1;
1851 goto done;
1854 if (err || btf_fd == -1)
1855 goto done;
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);
1872 done:
1873 if (!err)
1874 fprintf(stderr, "OK");
1876 if (*btf_log_buf && (err || args.always_log))
1877 fprintf(stderr, "\n%s", btf_log_buf);
1879 if (btf_fd != -1)
1880 close(btf_fd);
1881 if (map_fd != -1)
1882 close(map_fd);
1884 return err;
1887 static int test_raw(void)
1889 unsigned int i;
1890 int err = 0;
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));
1898 return err;
1901 struct btf_get_info_test {
1902 const char *descr;
1903 const char *str_sec;
1904 __u32 raw_types[MAX_NR_RAW_TYPES];
1905 __u32 str_sec_size;
1906 int btf_size_delta;
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",
1916 .raw_types = {
1917 /* int */ /* [1] */
1918 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1919 BTF_END_RAW,
1921 .str_sec = "",
1922 .str_sec_size = sizeof(""),
1923 .btf_size_delta = 1,
1926 .descr = "== raw_btf_size-3",
1927 .raw_types = {
1928 /* int */ /* [1] */
1929 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1930 BTF_END_RAW,
1932 .str_sec = "",
1933 .str_sec_size = sizeof(""),
1934 .btf_size_delta = -3,
1937 .descr = "Large bpf_btf_info",
1938 .raw_types = {
1939 /* int */ /* [1] */
1940 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1941 BTF_END_RAW,
1943 .str_sec = "",
1944 .str_sec_size = sizeof(""),
1945 .special_test = test_big_btf_info,
1948 .descr = "BTF ID",
1949 .raw_types = {
1950 /* int */ /* [1] */
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),
1954 BTF_END_RAW,
1956 .str_sec = "",
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;
1972 struct {
1973 struct bpf_btf_info info;
1974 uint64_t garbage;
1975 } info_garbage;
1976 struct bpf_btf_info *info;
1977 int btf_fd = -1, err;
1978 uint32_t info_len;
1980 raw_btf = btf_raw_create(&hdr_tmpl,
1981 test->raw_types,
1982 test->str_sec,
1983 test->str_sec_size,
1984 &raw_btf_size);
1986 if (!raw_btf)
1987 return -1;
1989 *btf_log_buf = '\0';
1991 user_btf = malloc(raw_btf_size);
1992 if (CHECK(!user_btf, "!user_btf")) {
1993 err = -1;
1994 goto done;
1997 btf_fd = bpf_load_btf(raw_btf, raw_btf_size,
1998 btf_log_buf, BTF_LOG_BUF_SIZE,
1999 args.always_log);
2000 if (CHECK(btf_fd == -1, "errno:%d", errno)) {
2001 err = -1;
2002 goto done;
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")) {
2018 err = -1;
2019 goto done;
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
2026 * to userspace.
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))) {
2033 err = -1;
2034 goto done;
2037 fprintf(stderr, "OK");
2039 done:
2040 if (*btf_log_buf && (err || args.always_log))
2041 fprintf(stderr, "\n%s", btf_log_buf);
2043 free(raw_btf);
2044 free(user_btf);
2046 if (btf_fd != -1)
2047 close(btf_fd);
2049 return err;
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;
2061 uint32_t info_len;
2062 int err, i, ret;
2064 raw_btf = btf_raw_create(&hdr_tmpl,
2065 test->raw_types,
2066 test->str_sec,
2067 test->str_sec_size,
2068 &raw_btf_size);
2070 if (!raw_btf)
2071 return -1;
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)) {
2078 err = -1;
2079 goto done;
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,
2087 args.always_log);
2088 if (CHECK(btf_fd[0] == -1, "errno:%d", errno)) {
2089 err = -1;
2090 goto done;
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)) {
2097 err = -1;
2098 goto done;
2101 btf_fd[1] = bpf_btf_get_fd_by_id(info[0].id);
2102 if (CHECK(btf_fd[1] == -1, "errno:%d", errno)) {
2103 err = -1;
2104 goto done;
2107 ret = 0;
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)) {
2115 err = -1;
2116 goto done;
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)) {
2131 err = -1;
2132 goto done;
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)) {
2142 err = -1;
2143 goto done;
2146 for (i = 0; i < 2; i++) {
2147 close(btf_fd[i]);
2148 btf_fd[i] = -1;
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)) {
2154 err = -1;
2155 goto done;
2157 close(btf_fd[0]);
2158 btf_fd[0] = -1;
2160 /* The map holds the last ref to BTF and its btf_id */
2161 close(map_fd);
2162 map_fd = -1;
2163 btf_fd[0] = bpf_btf_get_fd_by_id(map_info.btf_id);
2164 if (CHECK(btf_fd[0] != -1, "BTF lingers")) {
2165 err = -1;
2166 goto done;
2169 fprintf(stderr, "OK");
2171 done:
2172 if (*btf_log_buf && (err || args.always_log))
2173 fprintf(stderr, "\n%s", btf_log_buf);
2175 free(raw_btf);
2176 if (map_fd != -1)
2177 close(map_fd);
2178 for (i = 0; i < 2; i++) {
2179 free(user_btf[i]);
2180 if (btf_fd[i] != -1)
2181 close(btf_fd[i]);
2184 return err;
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;
2194 uint32_t info_len;
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,
2203 test->raw_types,
2204 test->str_sec,
2205 test->str_sec_size,
2206 &raw_btf_size);
2208 if (!raw_btf)
2209 return -1;
2211 *btf_log_buf = '\0';
2213 user_btf = malloc(raw_btf_size);
2214 if (CHECK(!user_btf, "!user_btf")) {
2215 err = -1;
2216 goto done;
2219 btf_fd = bpf_load_btf(raw_btf, raw_btf_size,
2220 btf_log_buf, BTF_LOG_BUF_SIZE,
2221 args.always_log);
2222 if (CHECK(btf_fd == -1, "errno:%d", errno)) {
2223 err = -1;
2224 goto done;
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;
2237 ret = 0;
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)) {
2245 err = -1;
2246 goto done;
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])) {
2254 err = -1;
2255 goto done;
2259 fprintf(stderr, "OK");
2261 done:
2262 if (*btf_log_buf && (err || args.always_log))
2263 fprintf(stderr, "\n%s", btf_log_buf);
2265 free(raw_btf);
2266 free(user_btf);
2268 if (btf_fd != -1)
2269 close(btf_fd);
2271 return err;
2274 static int test_get_info(void)
2276 unsigned int i;
2277 int err = 0;
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));
2285 return err;
2288 struct btf_file_test {
2289 const char *file;
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;
2306 GElf_Ehdr ehdr;
2307 int elf_fd;
2308 Elf *elf;
2309 int ret;
2311 if (CHECK(elf_version(EV_CURRENT) == EV_NONE,
2312 "elf_version(EV_CURRENT) == EV_NONE"))
2313 return -1;
2315 elf_fd = open(fn, O_RDONLY);
2316 if (CHECK(elf_fd == -1, "open(%s): errno:%d", fn, errno))
2317 return -1;
2319 elf = elf_begin(elf_fd, ELF_C_READ, NULL);
2320 if (CHECK(!elf, "elf_begin(%s): %s", fn, elf_errmsg(elf_errno()))) {
2321 ret = -1;
2322 goto done;
2325 if (CHECK(!gelf_getehdr(elf, &ehdr), "!gelf_getehdr(%s)", fn)) {
2326 ret = -1;
2327 goto done;
2330 while ((scn = elf_nextscn(elf, scn))) {
2331 const char *sh_name;
2332 GElf_Shdr sh;
2334 if (CHECK(gelf_getshdr(scn, &sh) != &sh,
2335 "file:%s gelf_getshdr != &sh", fn)) {
2336 ret = -1;
2337 goto done;
2340 sh_name = elf_strptr(elf, ehdr.e_shstrndx, sh.sh_name);
2341 if (!strcmp(sh_name, BTF_ELF_SEC)) {
2342 ret = 1;
2343 goto done;
2347 ret = 0;
2349 done:
2350 close(elf_fd);
2351 elf_end(elf);
2352 return ret;
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;
2361 int err;
2363 fprintf(stderr, "BTF libbpf test[%u] (%s): ", test_num,
2364 test->file);
2366 err = file_has_btf_elf(test->file);
2367 if (err == -1)
2368 return err;
2370 if (err == 0) {
2371 fprintf(stderr, "SKIP. No ELF %s found", BTF_ELF_SEC);
2372 skip_cnt++;
2373 return 0;
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"))
2382 goto done;
2384 prog = bpf_program__next(NULL, obj);
2385 if (CHECK(!prog, "Cannot find bpf_prog")) {
2386 err = -1;
2387 goto done;
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))
2393 goto done;
2395 map = bpf_object__find_map_by_name(obj, "btf_map");
2396 if (CHECK(!map, "btf_map not found")) {
2397 err = -1;
2398 goto done;
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))
2406 goto done;
2408 fprintf(stderr, "OK");
2410 done:
2411 bpf_object__close(obj);
2412 return err;
2415 static int test_file(void)
2417 unsigned int i;
2418 int err = 0;
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));
2426 return err;
2429 const char *pprint_enum_str[] = {
2430 "ENUM_ZERO",
2431 "ENUM_ONE",
2432 "ENUM_TWO",
2433 "ENUM_THREE",
2436 struct pprint_mapv {
2437 uint32_t ui32;
2438 uint16_t ui16;
2439 /* 2 bytes hole */
2440 int32_t si32;
2441 uint32_t unused_bits2a:2,
2442 bits28:28,
2443 unused_bits2b:2;
2444 union {
2445 uint64_t ui64;
2446 uint8_t ui8a[8];
2448 enum {
2449 ENUM_ZERO,
2450 ENUM_ONE,
2451 ENUM_TWO,
2452 ENUM_THREE,
2453 } aenum;
2456 static struct btf_raw_test pprint_test_template = {
2457 .raw_types = {
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),
2464 /* int */ /* [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 */
2504 BTF_END_RAW,
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 {
2516 const char *descr;
2517 enum bpf_map_type map_type;
2518 const char *map_name;
2519 bool ordered_map;
2520 bool lossless_map;
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)
2551 v->ui32 = i;
2552 v->si32 = -i;
2553 v->unused_bits2a = 3;
2554 v->bits28 = i;
2555 v->unused_bits2b = 3;
2556 v->ui64 = i;
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;
2571 char pin_path[255];
2572 size_t line_len = 0;
2573 char *line = NULL;
2574 uint8_t *raw_btf;
2575 ssize_t nread;
2576 int err, ret;
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,
2581 &raw_btf_size);
2583 if (!raw_btf)
2584 return -1;
2586 *btf_log_buf = '\0';
2587 btf_fd = bpf_load_btf(raw_btf, raw_btf_size,
2588 btf_log_buf, BTF_LOG_BUF_SIZE,
2589 args.always_log);
2590 free(raw_btf);
2592 if (CHECK(btf_fd == -1, "errno:%d", errno)) {
2593 err = -1;
2594 goto done;
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)) {
2608 err = -1;
2609 goto done;
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)) {
2617 err = -1;
2618 goto done;
2621 err = bpf_obj_pin(map_fd, pin_path);
2622 if (CHECK(err, "bpf_obj_pin(%s): errno:%d.", pin_path, errno))
2623 goto done;
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)) {
2632 err = -1;
2633 goto done;
2636 /* Skip lines start with '#' */
2637 while ((nread = getline(&line, &line_len, pin_file)) > 0 &&
2638 *line == '#')
2641 if (CHECK(nread <= 0, "Unexpected EOF")) {
2642 err = -1;
2643 goto done;
2646 nr_read_elems = 0;
2647 ordered_map = test->ordered_map;
2648 lossless_map = test->lossless_map;
2649 do {
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",
2657 next_key,
2658 mapv.ui32, mapv.si32,
2659 mapv.unused_bits2a, mapv.bits28, mapv.unused_bits2b,
2660 mapv.ui64,
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")) {
2667 err = -1;
2668 goto done;
2671 if (strcmp(expected_line, line)) {
2672 err = -1;
2673 fprintf(stderr, "unexpected pprint output\n");
2674 fprintf(stderr, "expected: %s", expected_line);
2675 fprintf(stderr, " read: %s", line);
2676 goto done;
2679 nread = getline(&line, &line_len, pin_file);
2680 } while (++nr_read_elems < test->max_entries && nread > 0);
2682 if (lossless_map &&
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)) {
2686 err = -1;
2687 goto done;
2690 if (CHECK(nread > 0, "Unexpected extra pprint output: %s", line)) {
2691 err = -1;
2692 goto done;
2695 err = 0;
2697 done:
2698 if (!err)
2699 fprintf(stderr, "OK");
2700 if (*btf_log_buf && (err || args.always_log))
2701 fprintf(stderr, "\n%s", btf_log_buf);
2702 if (btf_fd != -1)
2703 close(btf_fd);
2704 if (map_fd != -1)
2705 close(map_fd);
2706 if (pin_file)
2707 fclose(pin_file);
2708 unlink(pin_path);
2709 free(line);
2711 return err;
2714 static int test_pprint(void)
2716 unsigned int i;
2717 int err = 0;
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());
2729 return err;
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:";
2742 int opt;
2744 while ((opt = getopt(argc, argv, optstr)) != -1) {
2745 switch (opt) {
2746 case 'l':
2747 args.always_log = true;
2748 break;
2749 case 'f':
2750 args.file_test_num = atoi(optarg);
2751 args.file_test = true;
2752 break;
2753 case 'r':
2754 args.raw_test_num = atoi(optarg);
2755 args.raw_test = true;
2756 break;
2757 case 'g':
2758 args.get_info_test_num = atoi(optarg);
2759 args.get_info_test = true;
2760 break;
2761 case 'p':
2762 args.pprint_test = true;
2763 break;
2764 case 'h':
2765 usage(argv[0]);
2766 exit(0);
2767 default:
2768 usage(argv[0]);
2769 return -1;
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));
2778 return -1;
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));
2786 return -1;
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));
2794 return -1;
2797 return 0;
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)
2808 int err = 0;
2810 err = parse_args(argc, argv);
2811 if (err)
2812 return err;
2814 if (args.always_log)
2815 libbpf_set_print(__base_pr, __base_pr, __base_pr);
2817 if (args.raw_test)
2818 err |= test_raw();
2820 if (args.get_info_test)
2821 err |= test_get_info();
2823 if (args.file_test)
2824 err |= test_file();
2826 if (args.pprint_test)
2827 err |= test_pprint();
2829 if (args.raw_test || args.get_info_test || args.file_test ||
2830 args.pprint_test)
2831 goto done;
2833 err |= test_raw();
2834 err |= test_get_info();
2835 err |= test_file();
2837 done:
2838 print_summary();
2839 return err;