Merge branch 'bpf_func_info-improvements'
[linux-2.6-block.git] / tools / testing / selftests / bpf / test_btf.c
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2018 Facebook */
3
4 #include <linux/bpf.h>
5 #include <linux/btf.h>
6 #include <linux/err.h>
7 #include <linux/kernel.h>
8 #include <linux/filter.h>
9 #include <bpf/bpf.h>
10 #include <sys/resource.h>
11 #include <libelf.h>
12 #include <gelf.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <stdarg.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <errno.h>
20 #include <bpf/libbpf.h>
21 #include <bpf/btf.h>
22
23 #include "bpf_rlimit.h"
24 #include "bpf_util.h"
25
26 #define MAX_INSNS       512
27 #define MAX_SUBPROGS    16
28
29 static uint32_t pass_cnt;
30 static uint32_t error_cnt;
31 static uint32_t skip_cnt;
32
33 #define CHECK(condition, format...) ({                                  \
34         int __ret = !!(condition);                                      \
35         if (__ret) {                                                    \
36                 fprintf(stderr, "%s:%d:FAIL ", __func__, __LINE__);     \
37                 fprintf(stderr, format);                                \
38         }                                                               \
39         __ret;                                                          \
40 })
41
42 static int count_result(int err)
43 {
44         if (err)
45                 error_cnt++;
46         else
47                 pass_cnt++;
48
49         fprintf(stderr, "\n");
50         return err;
51 }
52
53 #define __printf(a, b)  __attribute__((format(printf, a, b)))
54
55 __printf(1, 2)
56 static int __base_pr(const char *format, ...)
57 {
58         va_list args;
59         int err;
60
61         va_start(args, format);
62         err = vfprintf(stderr, format, args);
63         va_end(args);
64         return err;
65 }
66
67 #define BTF_INFO_ENC(kind, root, vlen)                  \
68         ((!!(root) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))
69
70 #define BTF_TYPE_ENC(name, info, size_or_type)  \
71         (name), (info), (size_or_type)
72
73 #define BTF_INT_ENC(encoding, bits_offset, nr_bits)     \
74         ((encoding) << 24 | (bits_offset) << 16 | (nr_bits))
75 #define BTF_TYPE_INT_ENC(name, encoding, bits_offset, bits, sz) \
76         BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), sz),       \
77         BTF_INT_ENC(encoding, bits_offset, bits)
78
79 #define BTF_ARRAY_ENC(type, index_type, nr_elems)       \
80         (type), (index_type), (nr_elems)
81 #define BTF_TYPE_ARRAY_ENC(type, index_type, nr_elems) \
82         BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ARRAY, 0, 0), 0), \
83         BTF_ARRAY_ENC(type, index_type, nr_elems)
84
85 #define BTF_MEMBER_ENC(name, type, bits_offset) \
86         (name), (type), (bits_offset)
87 #define BTF_ENUM_ENC(name, val) (name), (val)
88
89 #define BTF_TYPEDEF_ENC(name, type) \
90         BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), type)
91
92 #define BTF_PTR_ENC(type) \
93         BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), type)
94
95 #define BTF_CONST_ENC(type) \
96         BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), type)
97
98 #define BTF_FUNC_PROTO_ENC(ret_type, nargs) \
99         BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, nargs), ret_type)
100
101 #define BTF_FUNC_PROTO_ARG_ENC(name, type) \
102         (name), (type)
103
104 #define BTF_FUNC_ENC(name, func_proto) \
105         BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), func_proto)
106
107 #define BTF_END_RAW 0xdeadbeef
108 #define NAME_TBD 0xdeadb33f
109
110 #define MAX_NR_RAW_TYPES 1024
111 #define BTF_LOG_BUF_SIZE 65535
112
113 static struct args {
114         unsigned int raw_test_num;
115         unsigned int file_test_num;
116         unsigned int get_info_test_num;
117         bool raw_test;
118         bool file_test;
119         bool get_info_test;
120         bool pprint_test;
121         bool always_log;
122         bool func_type_test;
123 } args;
124
125 static char btf_log_buf[BTF_LOG_BUF_SIZE];
126
127 static struct btf_header hdr_tmpl = {
128         .magic = BTF_MAGIC,
129         .version = BTF_VERSION,
130         .hdr_len = sizeof(struct btf_header),
131 };
132
133 struct btf_raw_test {
134         const char *descr;
135         const char *str_sec;
136         const char *map_name;
137         const char *err_str;
138         __u32 raw_types[MAX_NR_RAW_TYPES];
139         __u32 str_sec_size;
140         enum bpf_map_type map_type;
141         __u32 key_size;
142         __u32 value_size;
143         __u32 key_type_id;
144         __u32 value_type_id;
145         __u32 max_entries;
146         bool btf_load_err;
147         bool map_create_err;
148         bool ordered_map;
149         bool lossless_map;
150         bool percpu_map;
151         int hdr_len_delta;
152         int type_off_delta;
153         int str_off_delta;
154         int str_len_delta;
155 };
156
157 static struct btf_raw_test raw_tests[] = {
158 /* enum E {
159  *     E0,
160  *     E1,
161  * };
162  *
163  * struct A {
164  *      unsigned long long m;
165  *      int n;
166  *      char o;
167  *      [3 bytes hole]
168  *      int p[8];
169  *      int q[4][8];
170  *      enum E r;
171  * };
172  */
173 {
174         .descr = "struct test #1",
175         .raw_types = {
176                 /* int */
177                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
178                 /* unsigned long long */
179                 BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),               /* [2] */
180                 /* char */
181                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),   /* [3] */
182                 /* int[8] */
183                 BTF_TYPE_ARRAY_ENC(1, 1, 8),                    /* [4] */
184                 /* struct A { */                                /* [5] */
185                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 6), 180),
186                 BTF_MEMBER_ENC(NAME_TBD, 2, 0), /* unsigned long long m;*/
187                 BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;               */
188                 BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;              */
189                 BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]            */
190                 BTF_MEMBER_ENC(NAME_TBD, 6, 384),/* int q[4][8]         */
191                 BTF_MEMBER_ENC(NAME_TBD, 7, 1408), /* enum E r          */
192                 /* } */
193                 /* int[4][8] */
194                 BTF_TYPE_ARRAY_ENC(4, 1, 4),                    /* [6] */
195                 /* enum E */                                    /* [7] */
196                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), sizeof(int)),
197                 BTF_ENUM_ENC(NAME_TBD, 0),
198                 BTF_ENUM_ENC(NAME_TBD, 1),
199                 BTF_END_RAW,
200         },
201         .str_sec = "\0A\0m\0n\0o\0p\0q\0r\0E\0E0\0E1",
202         .str_sec_size = sizeof("\0A\0m\0n\0o\0p\0q\0r\0E\0E0\0E1"),
203         .map_type = BPF_MAP_TYPE_ARRAY,
204         .map_name = "struct_test1_map",
205         .key_size = sizeof(int),
206         .value_size = 180,
207         .key_type_id = 1,
208         .value_type_id = 5,
209         .max_entries = 4,
210 },
211
212 /* typedef struct b Struct_B;
213  *
214  * struct A {
215  *     int m;
216  *     struct b n[4];
217  *     const Struct_B o[4];
218  * };
219  *
220  * struct B {
221  *     int m;
222  *     int n;
223  * };
224  */
225 {
226         .descr = "struct test #2",
227         .raw_types = {
228                 /* int */                                       /* [1] */
229                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
230                 /* struct b [4] */                              /* [2] */
231                 BTF_TYPE_ARRAY_ENC(4, 1, 4),
232
233                 /* struct A { */                                /* [3] */
234                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 3), 68),
235                 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m;               */
236                 BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* struct B n[4]        */
237                 BTF_MEMBER_ENC(NAME_TBD, 8, 288),/* const Struct_B o[4];*/
238                 /* } */
239
240                 /* struct B { */                                /* [4] */
241                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
242                 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
243                 BTF_MEMBER_ENC(NAME_TBD, 1, 32),/* int n; */
244                 /* } */
245
246                 /* const int */                                 /* [5] */
247                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 1),
248                 /* typedef struct b Struct_B */ /* [6] */
249                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), 4),
250                 /* const Struct_B */                            /* [7] */
251                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 6),
252                 /* const Struct_B [4] */                        /* [8] */
253                 BTF_TYPE_ARRAY_ENC(7, 1, 4),
254                 BTF_END_RAW,
255         },
256         .str_sec = "\0A\0m\0n\0o\0B\0m\0n\0Struct_B",
257         .str_sec_size = sizeof("\0A\0m\0n\0o\0B\0m\0n\0Struct_B"),
258         .map_type = BPF_MAP_TYPE_ARRAY,
259         .map_name = "struct_test2_map",
260         .key_size = sizeof(int),
261         .value_size = 68,
262         .key_type_id = 1,
263         .value_type_id = 3,
264         .max_entries = 4,
265 },
266
267 {
268         .descr = "struct test #3 Invalid member offset",
269         .raw_types = {
270                 /* int */                                       /* [1] */
271                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
272                 /* int64 */                                     /* [2] */
273                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 64, 8),
274
275                 /* struct A { */                                /* [3] */
276                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 16),
277                 BTF_MEMBER_ENC(NAME_TBD, 1, 64),        /* int m;               */
278                 BTF_MEMBER_ENC(NAME_TBD, 2, 0),         /* int64 n; */
279                 /* } */
280                 BTF_END_RAW,
281         },
282         .str_sec = "\0A\0m\0n\0",
283         .str_sec_size = sizeof("\0A\0m\0n\0"),
284         .map_type = BPF_MAP_TYPE_ARRAY,
285         .map_name = "struct_test3_map",
286         .key_size = sizeof(int),
287         .value_size = 16,
288         .key_type_id = 1,
289         .value_type_id = 3,
290         .max_entries = 4,
291         .btf_load_err = true,
292         .err_str = "Invalid member bits_offset",
293 },
294
295 /* Test member exceeds the size of struct.
296  *
297  * struct A {
298  *     int m;
299  *     int n;
300  * };
301  */
302 {
303         .descr = "size check test #1",
304         .raw_types = {
305                 /* int */                                       /* [1] */
306                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
307                 /* struct A { */                                /* [2] */
308                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 2 -  1),
309                 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
310                 BTF_MEMBER_ENC(NAME_TBD, 1, 32),/* int n; */
311                 /* } */
312                 BTF_END_RAW,
313         },
314         .str_sec = "\0A\0m\0n",
315         .str_sec_size = sizeof("\0A\0m\0n"),
316         .map_type = BPF_MAP_TYPE_ARRAY,
317         .map_name = "size_check1_map",
318         .key_size = sizeof(int),
319         .value_size = 1,
320         .key_type_id = 1,
321         .value_type_id = 2,
322         .max_entries = 4,
323         .btf_load_err = true,
324         .err_str = "Member exceeds struct_size",
325 },
326
327 /* Test member exeeds the size of struct
328  *
329  * struct A {
330  *     int m;
331  *     int n[2];
332  * };
333  */
334 {
335         .descr = "size check test #2",
336         .raw_types = {
337                 /* int */                                       /* [1] */
338                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
339                 /* int[2] */                                    /* [2] */
340                 BTF_TYPE_ARRAY_ENC(1, 1, 2),
341                 /* struct A { */                                /* [3] */
342                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 3 - 1),
343                 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
344                 BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* int n[2]; */
345                 /* } */
346                 BTF_END_RAW,
347         },
348         .str_sec = "\0A\0m\0n",
349         .str_sec_size = sizeof("\0A\0m\0n"),
350         .map_type = BPF_MAP_TYPE_ARRAY,
351         .map_name = "size_check2_map",
352         .key_size = sizeof(int),
353         .value_size = 1,
354         .key_type_id = 1,
355         .value_type_id = 3,
356         .max_entries = 4,
357         .btf_load_err = true,
358         .err_str = "Member exceeds struct_size",
359 },
360
361 /* Test member exeeds the size of struct
362  *
363  * struct A {
364  *     int m;
365  *     void *n;
366  * };
367  */
368 {
369         .descr = "size check test #3",
370         .raw_types = {
371                 /* int */                                       /* [1] */
372                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
373                 /* void* */                                     /* [2] */
374                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
375                 /* struct A { */                                /* [3] */
376                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) + sizeof(void *) - 1),
377                 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
378                 BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* void *n; */
379                 /* } */
380                 BTF_END_RAW,
381         },
382         .str_sec = "\0A\0m\0n",
383         .str_sec_size = sizeof("\0A\0m\0n"),
384         .map_type = BPF_MAP_TYPE_ARRAY,
385         .map_name = "size_check3_map",
386         .key_size = sizeof(int),
387         .value_size = 1,
388         .key_type_id = 1,
389         .value_type_id = 3,
390         .max_entries = 4,
391         .btf_load_err = true,
392         .err_str = "Member exceeds struct_size",
393 },
394
395 /* Test member exceeds the size of struct
396  *
397  * enum E {
398  *     E0,
399  *     E1,
400  * };
401  *
402  * struct A {
403  *     int m;
404  *     enum E n;
405  * };
406  */
407 {
408         .descr = "size check test #4",
409         .raw_types = {
410                 /* int */                       /* [1] */
411                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
412                 /* enum E { */                  /* [2] */
413                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), sizeof(int)),
414                 BTF_ENUM_ENC(NAME_TBD, 0),
415                 BTF_ENUM_ENC(NAME_TBD, 1),
416                 /* } */
417                 /* struct A { */                /* [3] */
418                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 2 - 1),
419                 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
420                 BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* enum E n; */
421                 /* } */
422                 BTF_END_RAW,
423         },
424         .str_sec = "\0E\0E0\0E1\0A\0m\0n",
425         .str_sec_size = sizeof("\0E\0E0\0E1\0A\0m\0n"),
426         .map_type = BPF_MAP_TYPE_ARRAY,
427         .map_name = "size_check4_map",
428         .key_size = sizeof(int),
429         .value_size = 1,
430         .key_type_id = 1,
431         .value_type_id = 3,
432         .max_entries = 4,
433         .btf_load_err = true,
434         .err_str = "Member exceeds struct_size",
435 },
436
437 /* typedef const void * const_void_ptr;
438  * struct A {
439  *      const_void_ptr m;
440  * };
441  */
442 {
443         .descr = "void test #1",
444         .raw_types = {
445                 /* int */               /* [1] */
446                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
447                 /* const void */        /* [2] */
448                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
449                 /* const void* */       /* [3] */
450                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 2),
451                 /* typedef const void * const_void_ptr */
452                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 3),
453                 /* struct A { */        /* [4] */
454                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
455                 /* const_void_ptr m; */
456                 BTF_MEMBER_ENC(NAME_TBD, 3, 0),
457                 /* } */
458                 BTF_END_RAW,
459         },
460         .str_sec = "\0const_void_ptr\0A\0m",
461         .str_sec_size = sizeof("\0const_void_ptr\0A\0m"),
462         .map_type = BPF_MAP_TYPE_ARRAY,
463         .map_name = "void_test1_map",
464         .key_size = sizeof(int),
465         .value_size = sizeof(void *),
466         .key_type_id = 1,
467         .value_type_id = 4,
468         .max_entries = 4,
469 },
470
471 /* struct A {
472  *     const void m;
473  * };
474  */
475 {
476         .descr = "void test #2",
477         .raw_types = {
478                 /* int */               /* [1] */
479                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
480                 /* const void */        /* [2] */
481                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
482                 /* struct A { */        /* [3] */
483                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 8),
484                 /* const void m; */
485                 BTF_MEMBER_ENC(NAME_TBD, 2, 0),
486                 /* } */
487                 BTF_END_RAW,
488         },
489         .str_sec = "\0A\0m",
490         .str_sec_size = sizeof("\0A\0m"),
491         .map_type = BPF_MAP_TYPE_ARRAY,
492         .map_name = "void_test2_map",
493         .key_size = sizeof(int),
494         .value_size = sizeof(void *),
495         .key_type_id = 1,
496         .value_type_id = 3,
497         .max_entries = 4,
498         .btf_load_err = true,
499         .err_str = "Invalid member",
500 },
501
502 /* typedef const void * const_void_ptr;
503  * const_void_ptr[4]
504  */
505 {
506         .descr = "void test #3",
507         .raw_types = {
508                 /* int */               /* [1] */
509                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
510                 /* const void */        /* [2] */
511                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
512                 /* const void* */       /* [3] */
513                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 2),
514                 /* typedef const void * const_void_ptr */       /* [4] */
515                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 3),
516                 /* const_void_ptr[4] */ /* [5] */
517                 BTF_TYPE_ARRAY_ENC(3, 1, 4),
518                 BTF_END_RAW,
519         },
520         .str_sec = "\0const_void_ptr",
521         .str_sec_size = sizeof("\0const_void_ptr"),
522         .map_type = BPF_MAP_TYPE_ARRAY,
523         .map_name = "void_test3_map",
524         .key_size = sizeof(int),
525         .value_size = sizeof(void *) * 4,
526         .key_type_id = 1,
527         .value_type_id = 5,
528         .max_entries = 4,
529 },
530
531 /* const void[4]  */
532 {
533         .descr = "void test #4",
534         .raw_types = {
535                 /* int */               /* [1] */
536                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
537                 /* const void */        /* [2] */
538                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
539                 /* const void[4] */     /* [3] */
540                 BTF_TYPE_ARRAY_ENC(2, 1, 4),
541                 BTF_END_RAW,
542         },
543         .str_sec = "\0A\0m",
544         .str_sec_size = sizeof("\0A\0m"),
545         .map_type = BPF_MAP_TYPE_ARRAY,
546         .map_name = "void_test4_map",
547         .key_size = sizeof(int),
548         .value_size = sizeof(void *) * 4,
549         .key_type_id = 1,
550         .value_type_id = 3,
551         .max_entries = 4,
552         .btf_load_err = true,
553         .err_str = "Invalid elem",
554 },
555
556 /* Array_A  <------------------+
557  *     elem_type == Array_B    |
558  *                    |        |
559  *                    |        |
560  * Array_B  <-------- +        |
561  *      elem_type == Array A --+
562  */
563 {
564         .descr = "loop test #1",
565         .raw_types = {
566                 /* int */                       /* [1] */
567                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
568                 /* Array_A */                   /* [2] */
569                 BTF_TYPE_ARRAY_ENC(3, 1, 8),
570                 /* Array_B */                   /* [3] */
571                 BTF_TYPE_ARRAY_ENC(2, 1, 8),
572                 BTF_END_RAW,
573         },
574         .str_sec = "",
575         .str_sec_size = sizeof(""),
576         .map_type = BPF_MAP_TYPE_ARRAY,
577         .map_name = "loop_test1_map",
578         .key_size = sizeof(int),
579         .value_size = sizeof(sizeof(int) * 8),
580         .key_type_id = 1,
581         .value_type_id = 2,
582         .max_entries = 4,
583         .btf_load_err = true,
584         .err_str = "Loop detected",
585 },
586
587 /* typedef is _before_ the BTF type of Array_A and Array_B
588  *
589  * typedef Array_B int_array;
590  *
591  * Array_A  <------------------+
592  *     elem_type == int_array  |
593  *                    |        |
594  *                    |        |
595  * Array_B  <-------- +        |
596  *      elem_type == Array_A --+
597  */
598 {
599         .descr = "loop test #2",
600         .raw_types = {
601                 /* int */
602                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
603                 /* typedef Array_B int_array */
604                 BTF_TYPEDEF_ENC(1, 4),                          /* [2] */
605                 /* Array_A */
606                 BTF_TYPE_ARRAY_ENC(2, 1, 8),                    /* [3] */
607                 /* Array_B */
608                 BTF_TYPE_ARRAY_ENC(3, 1, 8),                    /* [4] */
609                 BTF_END_RAW,
610         },
611         .str_sec = "\0int_array\0",
612         .str_sec_size = sizeof("\0int_array"),
613         .map_type = BPF_MAP_TYPE_ARRAY,
614         .map_name = "loop_test2_map",
615         .key_size = sizeof(int),
616         .value_size = sizeof(sizeof(int) * 8),
617         .key_type_id = 1,
618         .value_type_id = 2,
619         .max_entries = 4,
620         .btf_load_err = true,
621         .err_str = "Loop detected",
622 },
623
624 /* Array_A  <------------------+
625  *     elem_type == Array_B    |
626  *                    |        |
627  *                    |        |
628  * Array_B  <-------- +        |
629  *      elem_type == Array_A --+
630  */
631 {
632         .descr = "loop test #3",
633         .raw_types = {
634                 /* int */                               /* [1] */
635                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
636                 /* Array_A */                           /* [2] */
637                 BTF_TYPE_ARRAY_ENC(3, 1, 8),
638                 /* Array_B */                           /* [3] */
639                 BTF_TYPE_ARRAY_ENC(2, 1, 8),
640                 BTF_END_RAW,
641         },
642         .str_sec = "",
643         .str_sec_size = sizeof(""),
644         .map_type = BPF_MAP_TYPE_ARRAY,
645         .map_name = "loop_test3_map",
646         .key_size = sizeof(int),
647         .value_size = sizeof(sizeof(int) * 8),
648         .key_type_id = 1,
649         .value_type_id = 2,
650         .max_entries = 4,
651         .btf_load_err = true,
652         .err_str = "Loop detected",
653 },
654
655 /* typedef is _between_ the BTF type of Array_A and Array_B
656  *
657  * typedef Array_B int_array;
658  *
659  * Array_A  <------------------+
660  *     elem_type == int_array  |
661  *                    |        |
662  *                    |        |
663  * Array_B  <-------- +        |
664  *      elem_type == Array_A --+
665  */
666 {
667         .descr = "loop test #4",
668         .raw_types = {
669                 /* int */                               /* [1] */
670                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
671                 /* Array_A */                           /* [2] */
672                 BTF_TYPE_ARRAY_ENC(3, 1, 8),
673                 /* typedef Array_B int_array */         /* [3] */
674                 BTF_TYPEDEF_ENC(NAME_TBD, 4),
675                 /* Array_B */                           /* [4] */
676                 BTF_TYPE_ARRAY_ENC(2, 1, 8),
677                 BTF_END_RAW,
678         },
679         .str_sec = "\0int_array\0",
680         .str_sec_size = sizeof("\0int_array"),
681         .map_type = BPF_MAP_TYPE_ARRAY,
682         .map_name = "loop_test4_map",
683         .key_size = sizeof(int),
684         .value_size = sizeof(sizeof(int) * 8),
685         .key_type_id = 1,
686         .value_type_id = 2,
687         .max_entries = 4,
688         .btf_load_err = true,
689         .err_str = "Loop detected",
690 },
691
692 /* typedef struct B Struct_B
693  *
694  * struct A {
695  *     int x;
696  *     Struct_B y;
697  * };
698  *
699  * struct B {
700  *     int x;
701  *     struct A y;
702  * };
703  */
704 {
705         .descr = "loop test #5",
706         .raw_types = {
707                 /* int */
708                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
709                 /* struct A */                                  /* [2] */
710                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
711                 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int x;       */
712                 BTF_MEMBER_ENC(NAME_TBD, 3, 32),/* Struct_B y;  */
713                 /* typedef struct B Struct_B */
714                 BTF_TYPEDEF_ENC(NAME_TBD, 4),                   /* [3] */
715                 /* struct B */                                  /* [4] */
716                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
717                 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int x;       */
718                 BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* struct A y;  */
719                 BTF_END_RAW,
720         },
721         .str_sec = "\0A\0x\0y\0Struct_B\0B\0x\0y",
722         .str_sec_size = sizeof("\0A\0x\0y\0Struct_B\0B\0x\0y"),
723         .map_type = BPF_MAP_TYPE_ARRAY,
724         .map_name = "loop_test5_map",
725         .key_size = sizeof(int),
726         .value_size = 8,
727         .key_type_id = 1,
728         .value_type_id = 2,
729         .max_entries = 4,
730         .btf_load_err = true,
731         .err_str = "Loop detected",
732 },
733
734 /* struct A {
735  *     int x;
736  *     struct A array_a[4];
737  * };
738  */
739 {
740         .descr = "loop test #6",
741         .raw_types = {
742                 /* int */
743                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
744                 BTF_TYPE_ARRAY_ENC(3, 1, 4),                    /* [2] */
745                 /* struct A */                                  /* [3] */
746                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
747                 BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int x;               */
748                 BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* struct A array_a[4]; */
749                 BTF_END_RAW,
750         },
751         .str_sec = "\0A\0x\0y",
752         .str_sec_size = sizeof("\0A\0x\0y"),
753         .map_type = BPF_MAP_TYPE_ARRAY,
754         .map_name = "loop_test6_map",
755         .key_size = sizeof(int),
756         .value_size = 8,
757         .key_type_id = 1,
758         .value_type_id = 2,
759         .max_entries = 4,
760         .btf_load_err = true,
761         .err_str = "Loop detected",
762 },
763
764 {
765         .descr = "loop test #7",
766         .raw_types = {
767                 /* int */                               /* [1] */
768                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
769                 /* struct A { */                        /* [2] */
770                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
771                 /*     const void *m;   */
772                 BTF_MEMBER_ENC(NAME_TBD, 3, 0),
773                 /* CONST type_id=3      */              /* [3] */
774                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 4),
775                 /* PTR type_id=2        */              /* [4] */
776                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 3),
777                 BTF_END_RAW,
778         },
779         .str_sec = "\0A\0m",
780         .str_sec_size = sizeof("\0A\0m"),
781         .map_type = BPF_MAP_TYPE_ARRAY,
782         .map_name = "loop_test7_map",
783         .key_size = sizeof(int),
784         .value_size = sizeof(void *),
785         .key_type_id = 1,
786         .value_type_id = 2,
787         .max_entries = 4,
788         .btf_load_err = true,
789         .err_str = "Loop detected",
790 },
791
792 {
793         .descr = "loop test #8",
794         .raw_types = {
795                 /* int */                               /* [1] */
796                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
797                 /* struct A { */                        /* [2] */
798                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
799                 /*     const void *m;   */
800                 BTF_MEMBER_ENC(NAME_TBD, 4, 0),
801                 /* struct B { */                        /* [3] */
802                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
803                 /*     const void *n;   */
804                 BTF_MEMBER_ENC(NAME_TBD, 6, 0),
805                 /* CONST type_id=5      */              /* [4] */
806                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 5),
807                 /* PTR type_id=6        */              /* [5] */
808                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 6),
809                 /* CONST type_id=7      */              /* [6] */
810                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 7),
811                 /* PTR type_id=4        */              /* [7] */
812                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 4),
813                 BTF_END_RAW,
814         },
815         .str_sec = "\0A\0m\0B\0n",
816         .str_sec_size = sizeof("\0A\0m\0B\0n"),
817         .map_type = BPF_MAP_TYPE_ARRAY,
818         .map_name = "loop_test8_map",
819         .key_size = sizeof(int),
820         .value_size = sizeof(void *),
821         .key_type_id = 1,
822         .value_type_id = 2,
823         .max_entries = 4,
824         .btf_load_err = true,
825         .err_str = "Loop detected",
826 },
827
828 {
829         .descr = "string section does not end with null",
830         .raw_types = {
831                 /* int */                               /* [1] */
832                 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
833                 BTF_END_RAW,
834         },
835         .str_sec = "\0int",
836         .str_sec_size = sizeof("\0int") - 1,
837         .map_type = BPF_MAP_TYPE_ARRAY,
838         .map_name = "hdr_test_map",
839         .key_size = sizeof(int),
840         .value_size = sizeof(int),
841         .key_type_id = 1,
842         .value_type_id = 1,
843         .max_entries = 4,
844         .btf_load_err = true,
845         .err_str = "Invalid string section",
846 },
847
848 {
849         .descr = "empty string section",
850         .raw_types = {
851                 /* int */                               /* [1] */
852                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
853                 BTF_END_RAW,
854         },
855         .str_sec = "",
856         .str_sec_size = 0,
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 = "Invalid string section",
866 },
867
868 {
869         .descr = "empty type section",
870         .raw_types = {
871                 BTF_END_RAW,
872         },
873         .str_sec = "\0int",
874         .str_sec_size = sizeof("\0int"),
875         .map_type = BPF_MAP_TYPE_ARRAY,
876         .map_name = "hdr_test_map",
877         .key_size = sizeof(int),
878         .value_size = sizeof(int),
879         .key_type_id = 1,
880         .value_type_id = 1,
881         .max_entries = 4,
882         .btf_load_err = true,
883         .err_str = "No type found",
884 },
885
886 {
887         .descr = "btf_header test. Longer hdr_len",
888         .raw_types = {
889                 /* int */                               /* [1] */
890                 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
891                 BTF_END_RAW,
892         },
893         .str_sec = "\0int",
894         .str_sec_size = sizeof("\0int"),
895         .map_type = BPF_MAP_TYPE_ARRAY,
896         .map_name = "hdr_test_map",
897         .key_size = sizeof(int),
898         .value_size = sizeof(int),
899         .key_type_id = 1,
900         .value_type_id = 1,
901         .max_entries = 4,
902         .btf_load_err = true,
903         .hdr_len_delta = 4,
904         .err_str = "Unsupported btf_header",
905 },
906
907 {
908         .descr = "btf_header test. Gap between hdr and type",
909         .raw_types = {
910                 /* int */                               /* [1] */
911                 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
912                 BTF_END_RAW,
913         },
914         .str_sec = "\0int",
915         .str_sec_size = sizeof("\0int"),
916         .map_type = BPF_MAP_TYPE_ARRAY,
917         .map_name = "hdr_test_map",
918         .key_size = sizeof(int),
919         .value_size = sizeof(int),
920         .key_type_id = 1,
921         .value_type_id = 1,
922         .max_entries = 4,
923         .btf_load_err = true,
924         .type_off_delta = 4,
925         .err_str = "Unsupported section found",
926 },
927
928 {
929         .descr = "btf_header test. Gap between type and str",
930         .raw_types = {
931                 /* int */                               /* [1] */
932                 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
933                 BTF_END_RAW,
934         },
935         .str_sec = "\0int",
936         .str_sec_size = sizeof("\0int"),
937         .map_type = BPF_MAP_TYPE_ARRAY,
938         .map_name = "hdr_test_map",
939         .key_size = sizeof(int),
940         .value_size = sizeof(int),
941         .key_type_id = 1,
942         .value_type_id = 1,
943         .max_entries = 4,
944         .btf_load_err = true,
945         .str_off_delta = 4,
946         .err_str = "Unsupported section found",
947 },
948
949 {
950         .descr = "btf_header test. Overlap between type and str",
951         .raw_types = {
952                 /* int */                               /* [1] */
953                 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
954                 BTF_END_RAW,
955         },
956         .str_sec = "\0int",
957         .str_sec_size = sizeof("\0int"),
958         .map_type = BPF_MAP_TYPE_ARRAY,
959         .map_name = "hdr_test_map",
960         .key_size = sizeof(int),
961         .value_size = sizeof(int),
962         .key_type_id = 1,
963         .value_type_id = 1,
964         .max_entries = 4,
965         .btf_load_err = true,
966         .str_off_delta = -4,
967         .err_str = "Section overlap found",
968 },
969
970 {
971         .descr = "btf_header test. Larger BTF size",
972         .raw_types = {
973                 /* int */                               /* [1] */
974                 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
975                 BTF_END_RAW,
976         },
977         .str_sec = "\0int",
978         .str_sec_size = sizeof("\0int"),
979         .map_type = BPF_MAP_TYPE_ARRAY,
980         .map_name = "hdr_test_map",
981         .key_size = sizeof(int),
982         .value_size = sizeof(int),
983         .key_type_id = 1,
984         .value_type_id = 1,
985         .max_entries = 4,
986         .btf_load_err = true,
987         .str_len_delta = -4,
988         .err_str = "Unsupported section found",
989 },
990
991 {
992         .descr = "btf_header test. Smaller BTF size",
993         .raw_types = {
994                 /* int */                               /* [1] */
995                 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
996                 BTF_END_RAW,
997         },
998         .str_sec = "\0int",
999         .str_sec_size = sizeof("\0int"),
1000         .map_type = BPF_MAP_TYPE_ARRAY,
1001         .map_name = "hdr_test_map",
1002         .key_size = sizeof(int),
1003         .value_size = sizeof(int),
1004         .key_type_id = 1,
1005         .value_type_id = 1,
1006         .max_entries = 4,
1007         .btf_load_err = true,
1008         .str_len_delta = 4,
1009         .err_str = "Total section length too long",
1010 },
1011
1012 {
1013         .descr = "array test. index_type/elem_type \"int\"",
1014         .raw_types = {
1015                 /* int */                               /* [1] */
1016                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1017                 /* int[16] */                           /* [2] */
1018                 BTF_TYPE_ARRAY_ENC(1, 1, 16),
1019                 BTF_END_RAW,
1020         },
1021         .str_sec = "",
1022         .str_sec_size = sizeof(""),
1023         .map_type = BPF_MAP_TYPE_ARRAY,
1024         .map_name = "array_test_map",
1025         .key_size = sizeof(int),
1026         .value_size = sizeof(int),
1027         .key_type_id = 1,
1028         .value_type_id = 1,
1029         .max_entries = 4,
1030 },
1031
1032 {
1033         .descr = "array test. index_type/elem_type \"const int\"",
1034         .raw_types = {
1035                 /* int */                               /* [1] */
1036                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1037                 /* int[16] */                           /* [2] */
1038                 BTF_TYPE_ARRAY_ENC(3, 3, 16),
1039                 /* CONST type_id=1 */                   /* [3] */
1040                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 1),
1041                 BTF_END_RAW,
1042         },
1043         .str_sec = "",
1044         .str_sec_size = sizeof(""),
1045         .map_type = BPF_MAP_TYPE_ARRAY,
1046         .map_name = "array_test_map",
1047         .key_size = sizeof(int),
1048         .value_size = sizeof(int),
1049         .key_type_id = 1,
1050         .value_type_id = 1,
1051         .max_entries = 4,
1052 },
1053
1054 {
1055         .descr = "array test. index_type \"const int:31\"",
1056         .raw_types = {
1057                 /* int */                               /* [1] */
1058                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1059                 /* int:31 */                            /* [2] */
1060                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 31, 4),
1061                 /* int[16] */                           /* [3] */
1062                 BTF_TYPE_ARRAY_ENC(1, 4, 16),
1063                 /* CONST type_id=2 */                   /* [4] */
1064                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 2),
1065                 BTF_END_RAW,
1066         },
1067         .str_sec = "",
1068         .str_sec_size = sizeof(""),
1069         .map_type = BPF_MAP_TYPE_ARRAY,
1070         .map_name = "array_test_map",
1071         .key_size = sizeof(int),
1072         .value_size = sizeof(int),
1073         .key_type_id = 1,
1074         .value_type_id = 1,
1075         .max_entries = 4,
1076         .btf_load_err = true,
1077         .err_str = "Invalid index",
1078 },
1079
1080 {
1081         .descr = "array test. elem_type \"const int:31\"",
1082         .raw_types = {
1083                 /* int */                               /* [1] */
1084                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1085                 /* int:31 */                            /* [2] */
1086                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 31, 4),
1087                 /* int[16] */                           /* [3] */
1088                 BTF_TYPE_ARRAY_ENC(4, 1, 16),
1089                 /* CONST type_id=2 */                   /* [4] */
1090                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 2),
1091                 BTF_END_RAW,
1092         },
1093         .str_sec = "",
1094         .str_sec_size = sizeof(""),
1095         .map_type = BPF_MAP_TYPE_ARRAY,
1096         .map_name = "array_test_map",
1097         .key_size = sizeof(int),
1098         .value_size = sizeof(int),
1099         .key_type_id = 1,
1100         .value_type_id = 1,
1101         .max_entries = 4,
1102         .btf_load_err = true,
1103         .err_str = "Invalid array of int",
1104 },
1105
1106 {
1107         .descr = "array test. index_type \"void\"",
1108         .raw_types = {
1109                 /* int */                               /* [1] */
1110                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1111                 /* int[16] */                           /* [2] */
1112                 BTF_TYPE_ARRAY_ENC(1, 0, 16),
1113                 BTF_END_RAW,
1114         },
1115         .str_sec = "",
1116         .str_sec_size = sizeof(""),
1117         .map_type = BPF_MAP_TYPE_ARRAY,
1118         .map_name = "array_test_map",
1119         .key_size = sizeof(int),
1120         .value_size = sizeof(int),
1121         .key_type_id = 1,
1122         .value_type_id = 1,
1123         .max_entries = 4,
1124         .btf_load_err = true,
1125         .err_str = "Invalid index",
1126 },
1127
1128 {
1129         .descr = "array test. index_type \"const void\"",
1130         .raw_types = {
1131                 /* int */                               /* [1] */
1132                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1133                 /* int[16] */                           /* [2] */
1134                 BTF_TYPE_ARRAY_ENC(1, 3, 16),
1135                 /* CONST type_id=0 (void) */            /* [3] */
1136                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1137                 BTF_END_RAW,
1138         },
1139         .str_sec = "",
1140         .str_sec_size = sizeof(""),
1141         .map_type = BPF_MAP_TYPE_ARRAY,
1142         .map_name = "array_test_map",
1143         .key_size = sizeof(int),
1144         .value_size = sizeof(int),
1145         .key_type_id = 1,
1146         .value_type_id = 1,
1147         .max_entries = 4,
1148         .btf_load_err = true,
1149         .err_str = "Invalid index",
1150 },
1151
1152 {
1153         .descr = "array test. elem_type \"const void\"",
1154         .raw_types = {
1155                 /* int */                               /* [1] */
1156                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1157                 /* int[16] */                           /* [2] */
1158                 BTF_TYPE_ARRAY_ENC(3, 1, 16),
1159                 /* CONST type_id=0 (void) */            /* [3] */
1160                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1161                 BTF_END_RAW,
1162         },
1163         .str_sec = "",
1164         .str_sec_size = sizeof(""),
1165         .map_type = BPF_MAP_TYPE_ARRAY,
1166         .map_name = "array_test_map",
1167         .key_size = sizeof(int),
1168         .value_size = sizeof(int),
1169         .key_type_id = 1,
1170         .value_type_id = 1,
1171         .max_entries = 4,
1172         .btf_load_err = true,
1173         .err_str = "Invalid elem",
1174 },
1175
1176 {
1177         .descr = "array test. elem_type \"const void *\"",
1178         .raw_types = {
1179                 /* int */                               /* [1] */
1180                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1181                 /* const void *[16] */                  /* [2] */
1182                 BTF_TYPE_ARRAY_ENC(3, 1, 16),
1183                 /* CONST type_id=4 */                   /* [3] */
1184                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 4),
1185                 /* void* */                             /* [4] */
1186                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
1187                 BTF_END_RAW,
1188         },
1189         .str_sec = "",
1190         .str_sec_size = sizeof(""),
1191         .map_type = BPF_MAP_TYPE_ARRAY,
1192         .map_name = "array_test_map",
1193         .key_size = sizeof(int),
1194         .value_size = sizeof(int),
1195         .key_type_id = 1,
1196         .value_type_id = 1,
1197         .max_entries = 4,
1198 },
1199
1200 {
1201         .descr = "array test. index_type \"const void *\"",
1202         .raw_types = {
1203                 /* int */                               /* [1] */
1204                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1205                 /* const void *[16] */                  /* [2] */
1206                 BTF_TYPE_ARRAY_ENC(3, 3, 16),
1207                 /* CONST type_id=4 */                   /* [3] */
1208                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 4),
1209                 /* void* */                             /* [4] */
1210                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
1211                 BTF_END_RAW,
1212         },
1213         .str_sec = "",
1214         .str_sec_size = sizeof(""),
1215         .map_type = BPF_MAP_TYPE_ARRAY,
1216         .map_name = "array_test_map",
1217         .key_size = sizeof(int),
1218         .value_size = sizeof(int),
1219         .key_type_id = 1,
1220         .value_type_id = 1,
1221         .max_entries = 4,
1222         .btf_load_err = true,
1223         .err_str = "Invalid index",
1224 },
1225
1226 {
1227         .descr = "array test. t->size != 0\"",
1228         .raw_types = {
1229                 /* int */                               /* [1] */
1230                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1231                 /* int[16] */                           /* [2] */
1232                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ARRAY, 0, 0), 1),
1233                 BTF_ARRAY_ENC(1, 1, 16),
1234                 BTF_END_RAW,
1235         },
1236         .str_sec = "",
1237         .str_sec_size = sizeof(""),
1238         .map_type = BPF_MAP_TYPE_ARRAY,
1239         .map_name = "array_test_map",
1240         .key_size = sizeof(int),
1241         .value_size = sizeof(int),
1242         .key_type_id = 1,
1243         .value_type_id = 1,
1244         .max_entries = 4,
1245         .btf_load_err = true,
1246         .err_str = "size != 0",
1247 },
1248
1249 {
1250         .descr = "int test. invalid int_data",
1251         .raw_types = {
1252                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), 4),
1253                 0x10000000,
1254                 BTF_END_RAW,
1255         },
1256         .str_sec = "",
1257         .str_sec_size = sizeof(""),
1258         .map_type = BPF_MAP_TYPE_ARRAY,
1259         .map_name = "array_test_map",
1260         .key_size = sizeof(int),
1261         .value_size = sizeof(int),
1262         .key_type_id = 1,
1263         .value_type_id = 1,
1264         .max_entries = 4,
1265         .btf_load_err = true,
1266         .err_str = "Invalid int_data",
1267 },
1268
1269 {
1270         .descr = "invalid BTF_INFO",
1271         .raw_types = {
1272                 /* int */                               /* [1] */
1273                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1274                 BTF_TYPE_ENC(0, 0x10000000, 4),
1275                 BTF_END_RAW,
1276         },
1277         .str_sec = "",
1278         .str_sec_size = sizeof(""),
1279         .map_type = BPF_MAP_TYPE_ARRAY,
1280         .map_name = "array_test_map",
1281         .key_size = sizeof(int),
1282         .value_size = sizeof(int),
1283         .key_type_id = 1,
1284         .value_type_id = 1,
1285         .max_entries = 4,
1286         .btf_load_err = true,
1287         .err_str = "Invalid btf_info",
1288 },
1289
1290 {
1291         .descr = "fwd test. t->type != 0\"",
1292         .raw_types = {
1293                 /* int */                               /* [1] */
1294                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1295                 /* fwd type */                          /* [2] */
1296                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FWD, 0, 0), 1),
1297                 BTF_END_RAW,
1298         },
1299         .str_sec = "",
1300         .str_sec_size = sizeof(""),
1301         .map_type = BPF_MAP_TYPE_ARRAY,
1302         .map_name = "fwd_test_map",
1303         .key_size = sizeof(int),
1304         .value_size = sizeof(int),
1305         .key_type_id = 1,
1306         .value_type_id = 1,
1307         .max_entries = 4,
1308         .btf_load_err = true,
1309         .err_str = "type != 0",
1310 },
1311
1312 {
1313         .descr = "arraymap invalid btf key (a bit field)",
1314         .raw_types = {
1315                 /* int */                               /* [1] */
1316                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1317                 /* 32 bit int with 32 bit offset */     /* [2] */
1318                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 32, 32, 8),
1319                 BTF_END_RAW,
1320         },
1321         .str_sec = "",
1322         .str_sec_size = sizeof(""),
1323         .map_type = BPF_MAP_TYPE_ARRAY,
1324         .map_name = "array_map_check_btf",
1325         .key_size = sizeof(int),
1326         .value_size = sizeof(int),
1327         .key_type_id = 2,
1328         .value_type_id = 1,
1329         .max_entries = 4,
1330         .map_create_err = true,
1331 },
1332
1333 {
1334         .descr = "arraymap invalid btf key (!= 32 bits)",
1335         .raw_types = {
1336                 /* int */                               /* [1] */
1337                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1338                 /* 16 bit int with 0 bit offset */      /* [2] */
1339                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 16, 2),
1340                 BTF_END_RAW,
1341         },
1342         .str_sec = "",
1343         .str_sec_size = sizeof(""),
1344         .map_type = BPF_MAP_TYPE_ARRAY,
1345         .map_name = "array_map_check_btf",
1346         .key_size = sizeof(int),
1347         .value_size = sizeof(int),
1348         .key_type_id = 2,
1349         .value_type_id = 1,
1350         .max_entries = 4,
1351         .map_create_err = true,
1352 },
1353
1354 {
1355         .descr = "arraymap invalid btf value (too small)",
1356         .raw_types = {
1357                 /* int */                               /* [1] */
1358                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1359                 BTF_END_RAW,
1360         },
1361         .str_sec = "",
1362         .str_sec_size = sizeof(""),
1363         .map_type = BPF_MAP_TYPE_ARRAY,
1364         .map_name = "array_map_check_btf",
1365         .key_size = sizeof(int),
1366         /* btf_value_size < map->value_size */
1367         .value_size = sizeof(__u64),
1368         .key_type_id = 1,
1369         .value_type_id = 1,
1370         .max_entries = 4,
1371         .map_create_err = true,
1372 },
1373
1374 {
1375         .descr = "arraymap invalid btf value (too big)",
1376         .raw_types = {
1377                 /* int */                               /* [1] */
1378                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1379                 BTF_END_RAW,
1380         },
1381         .str_sec = "",
1382         .str_sec_size = sizeof(""),
1383         .map_type = BPF_MAP_TYPE_ARRAY,
1384         .map_name = "array_map_check_btf",
1385         .key_size = sizeof(int),
1386         /* btf_value_size > map->value_size */
1387         .value_size = sizeof(__u16),
1388         .key_type_id = 1,
1389         .value_type_id = 1,
1390         .max_entries = 4,
1391         .map_create_err = true,
1392 },
1393
1394 {
1395         .descr = "func proto (int (*)(int, unsigned int))",
1396         .raw_types = {
1397                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
1398                 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),               /* [2] */
1399                 /* int (*)(int, unsigned int) */
1400                 BTF_FUNC_PROTO_ENC(1, 2),                       /* [3] */
1401                         BTF_FUNC_PROTO_ARG_ENC(0, 1),
1402                         BTF_FUNC_PROTO_ARG_ENC(0, 2),
1403                 BTF_END_RAW,
1404         },
1405         .str_sec = "",
1406         .str_sec_size = sizeof(""),
1407         .map_type = BPF_MAP_TYPE_ARRAY,
1408         .map_name = "func_proto_type_check_btf",
1409         .key_size = sizeof(int),
1410         .value_size = sizeof(int),
1411         .key_type_id = 1,
1412         .value_type_id = 1,
1413         .max_entries = 4,
1414 },
1415
1416 {
1417         .descr = "func proto (vararg)",
1418         .raw_types = {
1419                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
1420                 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),               /* [2] */
1421                 /* void (*)(int, unsigned int, ...) */
1422                 BTF_FUNC_PROTO_ENC(0, 3),                       /* [3] */
1423                         BTF_FUNC_PROTO_ARG_ENC(0, 1),
1424                         BTF_FUNC_PROTO_ARG_ENC(0, 2),
1425                         BTF_FUNC_PROTO_ARG_ENC(0, 0),
1426                 BTF_END_RAW,
1427         },
1428         .str_sec = "",
1429         .str_sec_size = sizeof(""),
1430         .map_type = BPF_MAP_TYPE_ARRAY,
1431         .map_name = "func_proto_type_check_btf",
1432         .key_size = sizeof(int),
1433         .value_size = sizeof(int),
1434         .key_type_id = 1,
1435         .value_type_id = 1,
1436         .max_entries = 4,
1437 },
1438
1439 {
1440         .descr = "func proto (vararg with name)",
1441         .raw_types = {
1442                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
1443                 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),               /* [2] */
1444                 /* void (*)(int a, unsigned int b, ... c) */
1445                 BTF_FUNC_PROTO_ENC(0, 3),                       /* [3] */
1446                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
1447                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
1448                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 0),
1449                 BTF_END_RAW,
1450         },
1451         .str_sec = "\0a\0b\0c",
1452         .str_sec_size = sizeof("\0a\0b\0c"),
1453         .map_type = BPF_MAP_TYPE_ARRAY,
1454         .map_name = "func_proto_type_check_btf",
1455         .key_size = sizeof(int),
1456         .value_size = sizeof(int),
1457         .key_type_id = 1,
1458         .value_type_id = 1,
1459         .max_entries = 4,
1460         .btf_load_err = true,
1461         .err_str = "Invalid arg#3",
1462 },
1463
1464 {
1465         .descr = "func proto (arg after vararg)",
1466         .raw_types = {
1467                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
1468                 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),               /* [2] */
1469                 /* void (*)(int a, ..., unsigned int b) */
1470                 BTF_FUNC_PROTO_ENC(0, 3),                       /* [3] */
1471                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
1472                         BTF_FUNC_PROTO_ARG_ENC(0, 0),
1473                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
1474                 BTF_END_RAW,
1475         },
1476         .str_sec = "\0a\0b",
1477         .str_sec_size = sizeof("\0a\0b"),
1478         .map_type = BPF_MAP_TYPE_ARRAY,
1479         .map_name = "func_proto_type_check_btf",
1480         .key_size = sizeof(int),
1481         .value_size = sizeof(int),
1482         .key_type_id = 1,
1483         .value_type_id = 1,
1484         .max_entries = 4,
1485         .btf_load_err = true,
1486         .err_str = "Invalid arg#2",
1487 },
1488
1489 {
1490         .descr = "func proto (CONST=>TYPEDEF=>PTR=>FUNC_PROTO)",
1491         .raw_types = {
1492                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
1493                 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),               /* [2] */
1494                 /* typedef void (*func_ptr)(int, unsigned int) */
1495                 BTF_TYPEDEF_ENC(NAME_TBD, 5),                   /* [3] */
1496                 /* const func_ptr */
1497                 BTF_CONST_ENC(3),                               /* [4] */
1498                 BTF_PTR_ENC(6),                                 /* [5] */
1499                 BTF_FUNC_PROTO_ENC(0, 2),                       /* [6] */
1500                         BTF_FUNC_PROTO_ARG_ENC(0, 1),
1501                         BTF_FUNC_PROTO_ARG_ENC(0, 2),
1502                 BTF_END_RAW,
1503         },
1504         .str_sec = "\0func_ptr",
1505         .str_sec_size = sizeof("\0func_ptr"),
1506         .map_type = BPF_MAP_TYPE_ARRAY,
1507         .map_name = "func_proto_type_check_btf",
1508         .key_size = sizeof(int),
1509         .value_size = sizeof(int),
1510         .key_type_id = 1,
1511         .value_type_id = 1,
1512         .max_entries = 4,
1513 },
1514
1515 {
1516         .descr = "func proto (CONST=>TYPEDEF=>FUNC_PROTO)",
1517         .raw_types = {
1518                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
1519                 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),               /* [2] */
1520                 BTF_CONST_ENC(4),                               /* [3] */
1521                 BTF_TYPEDEF_ENC(NAME_TBD, 5),                   /* [4] */
1522                 BTF_FUNC_PROTO_ENC(0, 2),                       /* [5] */
1523                         BTF_FUNC_PROTO_ARG_ENC(0, 1),
1524                         BTF_FUNC_PROTO_ARG_ENC(0, 2),
1525                 BTF_END_RAW,
1526         },
1527         .str_sec = "\0func_typedef",
1528         .str_sec_size = sizeof("\0func_typedef"),
1529         .map_type = BPF_MAP_TYPE_ARRAY,
1530         .map_name = "func_proto_type_check_btf",
1531         .key_size = sizeof(int),
1532         .value_size = sizeof(int),
1533         .key_type_id = 1,
1534         .value_type_id = 1,
1535         .max_entries = 4,
1536         .btf_load_err = true,
1537         .err_str = "Invalid type_id",
1538 },
1539
1540 {
1541         .descr = "func proto (btf_resolve(arg))",
1542         .raw_types = {
1543                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
1544                 /* void (*)(const void *) */
1545                 BTF_FUNC_PROTO_ENC(0, 1),                       /* [2] */
1546                         BTF_FUNC_PROTO_ARG_ENC(0, 3),
1547                 BTF_CONST_ENC(4),                               /* [3] */
1548                 BTF_PTR_ENC(0),                                 /* [4] */
1549                 BTF_END_RAW,
1550         },
1551         .str_sec = "",
1552         .str_sec_size = sizeof(""),
1553         .map_type = BPF_MAP_TYPE_ARRAY,
1554         .map_name = "func_proto_type_check_btf",
1555         .key_size = sizeof(int),
1556         .value_size = sizeof(int),
1557         .key_type_id = 1,
1558         .value_type_id = 1,
1559         .max_entries = 4,
1560 },
1561
1562 {
1563         .descr = "func proto (Not all arg has name)",
1564         .raw_types = {
1565                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
1566                 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),               /* [2] */
1567                 /* void (*)(int, unsigned int b) */
1568                 BTF_FUNC_PROTO_ENC(0, 2),                       /* [3] */
1569                         BTF_FUNC_PROTO_ARG_ENC(0, 1),
1570                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
1571                 BTF_END_RAW,
1572         },
1573         .str_sec = "\0b",
1574         .str_sec_size = sizeof("\0b"),
1575         .map_type = BPF_MAP_TYPE_ARRAY,
1576         .map_name = "func_proto_type_check_btf",
1577         .key_size = sizeof(int),
1578         .value_size = sizeof(int),
1579         .key_type_id = 1,
1580         .value_type_id = 1,
1581         .max_entries = 4,
1582 },
1583
1584 {
1585         .descr = "func proto (Bad arg name_off)",
1586         .raw_types = {
1587                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
1588                 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),               /* [2] */
1589                 /* void (*)(int a, unsigned int <bad_name_off>) */
1590                 BTF_FUNC_PROTO_ENC(0, 2),                       /* [3] */
1591                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
1592                         BTF_FUNC_PROTO_ARG_ENC(0xffffffff, 2),
1593                 BTF_END_RAW,
1594         },
1595         .str_sec = "\0a",
1596         .str_sec_size = sizeof("\0a"),
1597         .map_type = BPF_MAP_TYPE_ARRAY,
1598         .map_name = "func_proto_type_check_btf",
1599         .key_size = sizeof(int),
1600         .value_size = sizeof(int),
1601         .key_type_id = 1,
1602         .value_type_id = 1,
1603         .max_entries = 4,
1604         .btf_load_err = true,
1605         .err_str = "Invalid arg#2",
1606 },
1607
1608 {
1609         .descr = "func proto (Bad arg name)",
1610         .raw_types = {
1611                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
1612                 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),               /* [2] */
1613                 /* void (*)(int a, unsigned int !!!) */
1614                 BTF_FUNC_PROTO_ENC(0, 2),                       /* [3] */
1615                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
1616                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
1617                 BTF_END_RAW,
1618         },
1619         .str_sec = "\0a\0!!!",
1620         .str_sec_size = sizeof("\0a\0!!!"),
1621         .map_type = BPF_MAP_TYPE_ARRAY,
1622         .map_name = "func_proto_type_check_btf",
1623         .key_size = sizeof(int),
1624         .value_size = sizeof(int),
1625         .key_type_id = 1,
1626         .value_type_id = 1,
1627         .max_entries = 4,
1628         .btf_load_err = true,
1629         .err_str = "Invalid arg#2",
1630 },
1631
1632 {
1633         .descr = "func proto (Invalid return type)",
1634         .raw_types = {
1635                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
1636                 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),               /* [2] */
1637                 /* <bad_ret_type> (*)(int, unsigned int) */
1638                 BTF_FUNC_PROTO_ENC(100, 2),                     /* [3] */
1639                         BTF_FUNC_PROTO_ARG_ENC(0, 1),
1640                         BTF_FUNC_PROTO_ARG_ENC(0, 2),
1641                 BTF_END_RAW,
1642         },
1643         .str_sec = "",
1644         .str_sec_size = sizeof(""),
1645         .map_type = BPF_MAP_TYPE_ARRAY,
1646         .map_name = "func_proto_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 return type",
1654 },
1655
1656 {
1657         .descr = "func proto (with func name)",
1658         .raw_types = {
1659                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
1660                 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),               /* [2] */
1661                 /* void func_proto(int, unsigned int) */
1662                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 2), 0),     /* [3] */
1663                         BTF_FUNC_PROTO_ARG_ENC(0, 1),
1664                         BTF_FUNC_PROTO_ARG_ENC(0, 2),
1665                 BTF_END_RAW,
1666         },
1667         .str_sec = "\0func_proto",
1668         .str_sec_size = sizeof("\0func_proto"),
1669         .map_type = BPF_MAP_TYPE_ARRAY,
1670         .map_name = "func_proto_type_check_btf",
1671         .key_size = sizeof(int),
1672         .value_size = sizeof(int),
1673         .key_type_id = 1,
1674         .value_type_id = 1,
1675         .max_entries = 4,
1676         .btf_load_err = true,
1677         .err_str = "Invalid name",
1678 },
1679
1680 {
1681         .descr = "func proto (const void arg)",
1682         .raw_types = {
1683                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
1684                 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),               /* [2] */
1685                 /* void (*)(const void) */
1686                 BTF_FUNC_PROTO_ENC(0, 1),                       /* [3] */
1687                         BTF_FUNC_PROTO_ARG_ENC(0, 4),
1688                 BTF_CONST_ENC(0),                               /* [4] */
1689                 BTF_END_RAW,
1690         },
1691         .str_sec = "",
1692         .str_sec_size = sizeof(""),
1693         .map_type = BPF_MAP_TYPE_ARRAY,
1694         .map_name = "func_proto_type_check_btf",
1695         .key_size = sizeof(int),
1696         .value_size = sizeof(int),
1697         .key_type_id = 1,
1698         .value_type_id = 1,
1699         .max_entries = 4,
1700         .btf_load_err = true,
1701         .err_str = "Invalid arg#1",
1702 },
1703
1704 {
1705         .descr = "func (void func(int a, unsigned int b))",
1706         .raw_types = {
1707                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
1708                 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),               /* [2] */
1709                 /* void (*)(int a, unsigned int b) */
1710                 BTF_FUNC_PROTO_ENC(0, 2),                       /* [3] */
1711                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
1712                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
1713                 /* void func(int a, unsigned int b) */
1714                 BTF_FUNC_ENC(NAME_TBD, 3),                      /* [4] */
1715                 BTF_END_RAW,
1716         },
1717         .str_sec = "\0a\0b\0func",
1718         .str_sec_size = sizeof("\0a\0b\0func"),
1719         .map_type = BPF_MAP_TYPE_ARRAY,
1720         .map_name = "func_type_check_btf",
1721         .key_size = sizeof(int),
1722         .value_size = sizeof(int),
1723         .key_type_id = 1,
1724         .value_type_id = 1,
1725         .max_entries = 4,
1726 },
1727
1728 {
1729         .descr = "func (No func name)",
1730         .raw_types = {
1731                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
1732                 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),               /* [2] */
1733                 /* void (*)(int a, unsigned int b) */
1734                 BTF_FUNC_PROTO_ENC(0, 2),                       /* [3] */
1735                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
1736                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
1737                 /* void <no_name>(int a, unsigned int b) */
1738                 BTF_FUNC_ENC(0, 3),                             /* [4] */
1739                 BTF_END_RAW,
1740         },
1741         .str_sec = "\0a\0b",
1742         .str_sec_size = sizeof("\0a\0b"),
1743         .map_type = BPF_MAP_TYPE_ARRAY,
1744         .map_name = "func_type_check_btf",
1745         .key_size = sizeof(int),
1746         .value_size = sizeof(int),
1747         .key_type_id = 1,
1748         .value_type_id = 1,
1749         .max_entries = 4,
1750         .btf_load_err = true,
1751         .err_str = "Invalid name",
1752 },
1753
1754 {
1755         .descr = "func (Invalid func name)",
1756         .raw_types = {
1757                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
1758                 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),               /* [2] */
1759                 /* void (*)(int a, unsigned int b) */
1760                 BTF_FUNC_PROTO_ENC(0, 2),                       /* [3] */
1761                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
1762                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
1763                 /* void !!!(int a, unsigned int b) */
1764                 BTF_FUNC_ENC(NAME_TBD, 3),                      /* [4] */
1765                 BTF_END_RAW,
1766         },
1767         .str_sec = "\0a\0b\0!!!",
1768         .str_sec_size = sizeof("\0a\0b\0!!!"),
1769         .map_type = BPF_MAP_TYPE_ARRAY,
1770         .map_name = "func_type_check_btf",
1771         .key_size = sizeof(int),
1772         .value_size = sizeof(int),
1773         .key_type_id = 1,
1774         .value_type_id = 1,
1775         .max_entries = 4,
1776         .btf_load_err = true,
1777         .err_str = "Invalid name",
1778 },
1779
1780 {
1781         .descr = "func (Some arg has no name)",
1782         .raw_types = {
1783                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
1784                 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),               /* [2] */
1785                 /* void (*)(int a, unsigned int) */
1786                 BTF_FUNC_PROTO_ENC(0, 2),                       /* [3] */
1787                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
1788                         BTF_FUNC_PROTO_ARG_ENC(0, 2),
1789                 /* void func(int a, unsigned int) */
1790                 BTF_FUNC_ENC(NAME_TBD, 3),                      /* [4] */
1791                 BTF_END_RAW,
1792         },
1793         .str_sec = "\0a\0func",
1794         .str_sec_size = sizeof("\0a\0func"),
1795         .map_type = BPF_MAP_TYPE_ARRAY,
1796         .map_name = "func_type_check_btf",
1797         .key_size = sizeof(int),
1798         .value_size = sizeof(int),
1799         .key_type_id = 1,
1800         .value_type_id = 1,
1801         .max_entries = 4,
1802         .btf_load_err = true,
1803         .err_str = "Invalid arg#2",
1804 },
1805
1806 {
1807         .descr = "func (Non zero vlen)",
1808         .raw_types = {
1809                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
1810                 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),               /* [2] */
1811                 /* void (*)(int a, unsigned int b) */
1812                 BTF_FUNC_PROTO_ENC(0, 2),                       /* [3] */
1813                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
1814                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
1815                 /* void func(int a, unsigned int b) */
1816                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 2), 3),   /* [4] */
1817                 BTF_END_RAW,
1818         },
1819         .str_sec = "\0a\0b\0func",
1820         .str_sec_size = sizeof("\0a\0b\0func"),
1821         .map_type = BPF_MAP_TYPE_ARRAY,
1822         .map_name = "func_type_check_btf",
1823         .key_size = sizeof(int),
1824         .value_size = sizeof(int),
1825         .key_type_id = 1,
1826         .value_type_id = 1,
1827         .max_entries = 4,
1828         .btf_load_err = true,
1829         .err_str = "vlen != 0",
1830 },
1831
1832 {
1833         .descr = "func (Not referring to FUNC_PROTO)",
1834         .raw_types = {
1835                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
1836                 BTF_FUNC_ENC(NAME_TBD, 1),                      /* [2] */
1837                 BTF_END_RAW,
1838         },
1839         .str_sec = "\0func",
1840         .str_sec_size = sizeof("\0func"),
1841         .map_type = BPF_MAP_TYPE_ARRAY,
1842         .map_name = "func_type_check_btf",
1843         .key_size = sizeof(int),
1844         .value_size = sizeof(int),
1845         .key_type_id = 1,
1846         .value_type_id = 1,
1847         .max_entries = 4,
1848         .btf_load_err = true,
1849         .err_str = "Invalid type_id",
1850 },
1851
1852 }; /* struct btf_raw_test raw_tests[] */
1853
1854 static const char *get_next_str(const char *start, const char *end)
1855 {
1856         return start < end - 1 ? start + 1 : NULL;
1857 }
1858
1859 static int get_type_sec_size(const __u32 *raw_types)
1860 {
1861         int i;
1862
1863         for (i = MAX_NR_RAW_TYPES - 1;
1864              i >= 0 && raw_types[i] != BTF_END_RAW;
1865              i--)
1866                 ;
1867
1868         return i < 0 ? i : i * sizeof(raw_types[0]);
1869 }
1870
1871 static void *btf_raw_create(const struct btf_header *hdr,
1872                             const __u32 *raw_types,
1873                             const char *str,
1874                             unsigned int str_sec_size,
1875                             unsigned int *btf_size)
1876 {
1877         const char *next_str = str, *end_str = str + str_sec_size;
1878         unsigned int size_needed, offset;
1879         struct btf_header *ret_hdr;
1880         int i, type_sec_size;
1881         uint32_t *ret_types;
1882         void *raw_btf;
1883
1884         type_sec_size = get_type_sec_size(raw_types);
1885         if (CHECK(type_sec_size < 0, "Cannot get nr_raw_types"))
1886                 return NULL;
1887
1888         size_needed = sizeof(*hdr) + type_sec_size + str_sec_size;
1889         raw_btf = malloc(size_needed);
1890         if (CHECK(!raw_btf, "Cannot allocate memory for raw_btf"))
1891                 return NULL;
1892
1893         /* Copy header */
1894         memcpy(raw_btf, hdr, sizeof(*hdr));
1895         offset = sizeof(*hdr);
1896
1897         /* Copy type section */
1898         ret_types = raw_btf + offset;
1899         for (i = 0; i < type_sec_size / sizeof(raw_types[0]); i++) {
1900                 if (raw_types[i] == NAME_TBD) {
1901                         next_str = get_next_str(next_str, end_str);
1902                         if (CHECK(!next_str, "Error in getting next_str")) {
1903                                 free(raw_btf);
1904                                 return NULL;
1905                         }
1906                         ret_types[i] = next_str - str;
1907                         next_str += strlen(next_str);
1908                 } else {
1909                         ret_types[i] = raw_types[i];
1910                 }
1911         }
1912         offset += type_sec_size;
1913
1914         /* Copy string section */
1915         memcpy(raw_btf + offset, str, str_sec_size);
1916
1917         ret_hdr = (struct btf_header *)raw_btf;
1918         ret_hdr->type_len = type_sec_size;
1919         ret_hdr->str_off = type_sec_size;
1920         ret_hdr->str_len = str_sec_size;
1921
1922         *btf_size = size_needed;
1923
1924         return raw_btf;
1925 }
1926
1927 static int do_test_raw(unsigned int test_num)
1928 {
1929         struct btf_raw_test *test = &raw_tests[test_num - 1];
1930         struct bpf_create_map_attr create_attr = {};
1931         int map_fd = -1, btf_fd = -1;
1932         unsigned int raw_btf_size;
1933         struct btf_header *hdr;
1934         void *raw_btf;
1935         int err;
1936
1937         fprintf(stderr, "BTF raw test[%u] (%s): ", test_num, test->descr);
1938         raw_btf = btf_raw_create(&hdr_tmpl,
1939                                  test->raw_types,
1940                                  test->str_sec,
1941                                  test->str_sec_size,
1942                                  &raw_btf_size);
1943
1944         if (!raw_btf)
1945                 return -1;
1946
1947         hdr = raw_btf;
1948
1949         hdr->hdr_len = (int)hdr->hdr_len + test->hdr_len_delta;
1950         hdr->type_off = (int)hdr->type_off + test->type_off_delta;
1951         hdr->str_off = (int)hdr->str_off + test->str_off_delta;
1952         hdr->str_len = (int)hdr->str_len + test->str_len_delta;
1953
1954         *btf_log_buf = '\0';
1955         btf_fd = bpf_load_btf(raw_btf, raw_btf_size,
1956                               btf_log_buf, BTF_LOG_BUF_SIZE,
1957                               args.always_log);
1958         free(raw_btf);
1959
1960         err = ((btf_fd == -1) != test->btf_load_err);
1961         if (CHECK(err, "btf_fd:%d test->btf_load_err:%u",
1962                   btf_fd, test->btf_load_err) ||
1963             CHECK(test->err_str && !strstr(btf_log_buf, test->err_str),
1964                   "expected err_str:%s", test->err_str)) {
1965                 err = -1;
1966                 goto done;
1967         }
1968
1969         if (err || btf_fd == -1)
1970                 goto done;
1971
1972         create_attr.name = test->map_name;
1973         create_attr.map_type = test->map_type;
1974         create_attr.key_size = test->key_size;
1975         create_attr.value_size = test->value_size;
1976         create_attr.max_entries = test->max_entries;
1977         create_attr.btf_fd = btf_fd;
1978         create_attr.btf_key_type_id = test->key_type_id;
1979         create_attr.btf_value_type_id = test->value_type_id;
1980
1981         map_fd = bpf_create_map_xattr(&create_attr);
1982
1983         err = ((map_fd == -1) != test->map_create_err);
1984         CHECK(err, "map_fd:%d test->map_create_err:%u",
1985               map_fd, test->map_create_err);
1986
1987 done:
1988         if (!err)
1989                 fprintf(stderr, "OK");
1990
1991         if (*btf_log_buf && (err || args.always_log))
1992                 fprintf(stderr, "\n%s", btf_log_buf);
1993
1994         if (btf_fd != -1)
1995                 close(btf_fd);
1996         if (map_fd != -1)
1997                 close(map_fd);
1998
1999         return err;
2000 }
2001
2002 static int test_raw(void)
2003 {
2004         unsigned int i;
2005         int err = 0;
2006
2007         if (args.raw_test_num)
2008                 return count_result(do_test_raw(args.raw_test_num));
2009
2010         for (i = 1; i <= ARRAY_SIZE(raw_tests); i++)
2011                 err |= count_result(do_test_raw(i));
2012
2013         return err;
2014 }
2015
2016 struct btf_get_info_test {
2017         const char *descr;
2018         const char *str_sec;
2019         __u32 raw_types[MAX_NR_RAW_TYPES];
2020         __u32 str_sec_size;
2021         int btf_size_delta;
2022         int (*special_test)(unsigned int test_num);
2023 };
2024
2025 static int test_big_btf_info(unsigned int test_num);
2026 static int test_btf_id(unsigned int test_num);
2027
2028 const struct btf_get_info_test get_info_tests[] = {
2029 {
2030         .descr = "== raw_btf_size+1",
2031         .raw_types = {
2032                 /* int */                               /* [1] */
2033                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
2034                 BTF_END_RAW,
2035         },
2036         .str_sec = "",
2037         .str_sec_size = sizeof(""),
2038         .btf_size_delta = 1,
2039 },
2040 {
2041         .descr = "== raw_btf_size-3",
2042         .raw_types = {
2043                 /* int */                               /* [1] */
2044                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
2045                 BTF_END_RAW,
2046         },
2047         .str_sec = "",
2048         .str_sec_size = sizeof(""),
2049         .btf_size_delta = -3,
2050 },
2051 {
2052         .descr = "Large bpf_btf_info",
2053         .raw_types = {
2054                 /* int */                               /* [1] */
2055                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
2056                 BTF_END_RAW,
2057         },
2058         .str_sec = "",
2059         .str_sec_size = sizeof(""),
2060         .special_test = test_big_btf_info,
2061 },
2062 {
2063         .descr = "BTF ID",
2064         .raw_types = {
2065                 /* int */                               /* [1] */
2066                 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
2067                 /* unsigned int */                      /* [2] */
2068                 BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),
2069                 BTF_END_RAW,
2070         },
2071         .str_sec = "",
2072         .str_sec_size = sizeof(""),
2073         .special_test = test_btf_id,
2074 },
2075 };
2076
2077 static inline __u64 ptr_to_u64(const void *ptr)
2078 {
2079         return (__u64)(unsigned long)ptr;
2080 }
2081
2082 static int test_big_btf_info(unsigned int test_num)
2083 {
2084         const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
2085         uint8_t *raw_btf = NULL, *user_btf = NULL;
2086         unsigned int raw_btf_size;
2087         struct {
2088                 struct bpf_btf_info info;
2089                 uint64_t garbage;
2090         } info_garbage;
2091         struct bpf_btf_info *info;
2092         int btf_fd = -1, err;
2093         uint32_t info_len;
2094
2095         raw_btf = btf_raw_create(&hdr_tmpl,
2096                                  test->raw_types,
2097                                  test->str_sec,
2098                                  test->str_sec_size,
2099                                  &raw_btf_size);
2100
2101         if (!raw_btf)
2102                 return -1;
2103
2104         *btf_log_buf = '\0';
2105
2106         user_btf = malloc(raw_btf_size);
2107         if (CHECK(!user_btf, "!user_btf")) {
2108                 err = -1;
2109                 goto done;
2110         }
2111
2112         btf_fd = bpf_load_btf(raw_btf, raw_btf_size,
2113                               btf_log_buf, BTF_LOG_BUF_SIZE,
2114                               args.always_log);
2115         if (CHECK(btf_fd == -1, "errno:%d", errno)) {
2116                 err = -1;
2117                 goto done;
2118         }
2119
2120         /*
2121          * GET_INFO should error out if the userspace info
2122          * has non zero tailing bytes.
2123          */
2124         info = &info_garbage.info;
2125         memset(info, 0, sizeof(*info));
2126         info_garbage.garbage = 0xdeadbeef;
2127         info_len = sizeof(info_garbage);
2128         info->btf = ptr_to_u64(user_btf);
2129         info->btf_size = raw_btf_size;
2130
2131         err = bpf_obj_get_info_by_fd(btf_fd, info, &info_len);
2132         if (CHECK(!err, "!err")) {
2133                 err = -1;
2134                 goto done;
2135         }
2136
2137         /*
2138          * GET_INFO should succeed even info_len is larger than
2139          * the kernel supported as long as tailing bytes are zero.
2140          * The kernel supported info len should also be returned
2141          * to userspace.
2142          */
2143         info_garbage.garbage = 0;
2144         err = bpf_obj_get_info_by_fd(btf_fd, info, &info_len);
2145         if (CHECK(err || info_len != sizeof(*info),
2146                   "err:%d errno:%d info_len:%u sizeof(*info):%lu",
2147                   err, errno, info_len, sizeof(*info))) {
2148                 err = -1;
2149                 goto done;
2150         }
2151
2152         fprintf(stderr, "OK");
2153
2154 done:
2155         if (*btf_log_buf && (err || args.always_log))
2156                 fprintf(stderr, "\n%s", btf_log_buf);
2157
2158         free(raw_btf);
2159         free(user_btf);
2160
2161         if (btf_fd != -1)
2162                 close(btf_fd);
2163
2164         return err;
2165 }
2166
2167 static int test_btf_id(unsigned int test_num)
2168 {
2169         const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
2170         struct bpf_create_map_attr create_attr = {};
2171         uint8_t *raw_btf = NULL, *user_btf[2] = {};
2172         int btf_fd[2] = {-1, -1}, map_fd = -1;
2173         struct bpf_map_info map_info = {};
2174         struct bpf_btf_info info[2] = {};
2175         unsigned int raw_btf_size;
2176         uint32_t info_len;
2177         int err, i, ret;
2178
2179         raw_btf = btf_raw_create(&hdr_tmpl,
2180                                  test->raw_types,
2181                                  test->str_sec,
2182                                  test->str_sec_size,
2183                                  &raw_btf_size);
2184
2185         if (!raw_btf)
2186                 return -1;
2187
2188         *btf_log_buf = '\0';
2189
2190         for (i = 0; i < 2; i++) {
2191                 user_btf[i] = malloc(raw_btf_size);
2192                 if (CHECK(!user_btf[i], "!user_btf[%d]", i)) {
2193                         err = -1;
2194                         goto done;
2195                 }
2196                 info[i].btf = ptr_to_u64(user_btf[i]);
2197                 info[i].btf_size = raw_btf_size;
2198         }
2199
2200         btf_fd[0] = bpf_load_btf(raw_btf, raw_btf_size,
2201                                  btf_log_buf, BTF_LOG_BUF_SIZE,
2202                                  args.always_log);
2203         if (CHECK(btf_fd[0] == -1, "errno:%d", errno)) {
2204                 err = -1;
2205                 goto done;
2206         }
2207
2208         /* Test BPF_OBJ_GET_INFO_BY_ID on btf_id */
2209         info_len = sizeof(info[0]);
2210         err = bpf_obj_get_info_by_fd(btf_fd[0], &info[0], &info_len);
2211         if (CHECK(err, "errno:%d", errno)) {
2212                 err = -1;
2213                 goto done;
2214         }
2215
2216         btf_fd[1] = bpf_btf_get_fd_by_id(info[0].id);
2217         if (CHECK(btf_fd[1] == -1, "errno:%d", errno)) {
2218                 err = -1;
2219                 goto done;
2220         }
2221
2222         ret = 0;
2223         err = bpf_obj_get_info_by_fd(btf_fd[1], &info[1], &info_len);
2224         if (CHECK(err || info[0].id != info[1].id ||
2225                   info[0].btf_size != info[1].btf_size ||
2226                   (ret = memcmp(user_btf[0], user_btf[1], info[0].btf_size)),
2227                   "err:%d errno:%d id0:%u id1:%u btf_size0:%u btf_size1:%u memcmp:%d",
2228                   err, errno, info[0].id, info[1].id,
2229                   info[0].btf_size, info[1].btf_size, ret)) {
2230                 err = -1;
2231                 goto done;
2232         }
2233
2234         /* Test btf members in struct bpf_map_info */
2235         create_attr.name = "test_btf_id";
2236         create_attr.map_type = BPF_MAP_TYPE_ARRAY;
2237         create_attr.key_size = sizeof(int);
2238         create_attr.value_size = sizeof(unsigned int);
2239         create_attr.max_entries = 4;
2240         create_attr.btf_fd = btf_fd[0];
2241         create_attr.btf_key_type_id = 1;
2242         create_attr.btf_value_type_id = 2;
2243
2244         map_fd = bpf_create_map_xattr(&create_attr);
2245         if (CHECK(map_fd == -1, "errno:%d", errno)) {
2246                 err = -1;
2247                 goto done;
2248         }
2249
2250         info_len = sizeof(map_info);
2251         err = bpf_obj_get_info_by_fd(map_fd, &map_info, &info_len);
2252         if (CHECK(err || map_info.btf_id != info[0].id ||
2253                   map_info.btf_key_type_id != 1 || map_info.btf_value_type_id != 2,
2254                   "err:%d errno:%d info.id:%u btf_id:%u btf_key_type_id:%u btf_value_type_id:%u",
2255                   err, errno, info[0].id, map_info.btf_id, map_info.btf_key_type_id,
2256                   map_info.btf_value_type_id)) {
2257                 err = -1;
2258                 goto done;
2259         }
2260
2261         for (i = 0; i < 2; i++) {
2262                 close(btf_fd[i]);
2263                 btf_fd[i] = -1;
2264         }
2265
2266         /* Test BTF ID is removed from the kernel */
2267         btf_fd[0] = bpf_btf_get_fd_by_id(map_info.btf_id);
2268         if (CHECK(btf_fd[0] == -1, "errno:%d", errno)) {
2269                 err = -1;
2270                 goto done;
2271         }
2272         close(btf_fd[0]);
2273         btf_fd[0] = -1;
2274
2275         /* The map holds the last ref to BTF and its btf_id */
2276         close(map_fd);
2277         map_fd = -1;
2278         btf_fd[0] = bpf_btf_get_fd_by_id(map_info.btf_id);
2279         if (CHECK(btf_fd[0] != -1, "BTF lingers")) {
2280                 err = -1;
2281                 goto done;
2282         }
2283
2284         fprintf(stderr, "OK");
2285
2286 done:
2287         if (*btf_log_buf && (err || args.always_log))
2288                 fprintf(stderr, "\n%s", btf_log_buf);
2289
2290         free(raw_btf);
2291         if (map_fd != -1)
2292                 close(map_fd);
2293         for (i = 0; i < 2; i++) {
2294                 free(user_btf[i]);
2295                 if (btf_fd[i] != -1)
2296                         close(btf_fd[i]);
2297         }
2298
2299         return err;
2300 }
2301
2302 static int do_test_get_info(unsigned int test_num)
2303 {
2304         const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
2305         unsigned int raw_btf_size, user_btf_size, expected_nbytes;
2306         uint8_t *raw_btf = NULL, *user_btf = NULL;
2307         struct bpf_btf_info info = {};
2308         int btf_fd = -1, err, ret;
2309         uint32_t info_len;
2310
2311         fprintf(stderr, "BTF GET_INFO test[%u] (%s): ",
2312                 test_num, test->descr);
2313
2314         if (test->special_test)
2315                 return test->special_test(test_num);
2316
2317         raw_btf = btf_raw_create(&hdr_tmpl,
2318                                  test->raw_types,
2319                                  test->str_sec,
2320                                  test->str_sec_size,
2321                                  &raw_btf_size);
2322
2323         if (!raw_btf)
2324                 return -1;
2325
2326         *btf_log_buf = '\0';
2327
2328         user_btf = malloc(raw_btf_size);
2329         if (CHECK(!user_btf, "!user_btf")) {
2330                 err = -1;
2331                 goto done;
2332         }
2333
2334         btf_fd = bpf_load_btf(raw_btf, raw_btf_size,
2335                               btf_log_buf, BTF_LOG_BUF_SIZE,
2336                               args.always_log);
2337         if (CHECK(btf_fd == -1, "errno:%d", errno)) {
2338                 err = -1;
2339                 goto done;
2340         }
2341
2342         user_btf_size = (int)raw_btf_size + test->btf_size_delta;
2343         expected_nbytes = min(raw_btf_size, user_btf_size);
2344         if (raw_btf_size > expected_nbytes)
2345                 memset(user_btf + expected_nbytes, 0xff,
2346                        raw_btf_size - expected_nbytes);
2347
2348         info_len = sizeof(info);
2349         info.btf = ptr_to_u64(user_btf);
2350         info.btf_size = user_btf_size;
2351
2352         ret = 0;
2353         err = bpf_obj_get_info_by_fd(btf_fd, &info, &info_len);
2354         if (CHECK(err || !info.id || info_len != sizeof(info) ||
2355                   info.btf_size != raw_btf_size ||
2356                   (ret = memcmp(raw_btf, user_btf, expected_nbytes)),
2357                   "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",
2358                   err, errno, info.id, info_len, sizeof(info),
2359                   raw_btf_size, info.btf_size, expected_nbytes, ret)) {
2360                 err = -1;
2361                 goto done;
2362         }
2363
2364         while (expected_nbytes < raw_btf_size) {
2365                 fprintf(stderr, "%u...", expected_nbytes);
2366                 if (CHECK(user_btf[expected_nbytes++] != 0xff,
2367                           "user_btf[%u]:%x != 0xff", expected_nbytes - 1,
2368                           user_btf[expected_nbytes - 1])) {
2369                         err = -1;
2370                         goto done;
2371                 }
2372         }
2373
2374         fprintf(stderr, "OK");
2375
2376 done:
2377         if (*btf_log_buf && (err || args.always_log))
2378                 fprintf(stderr, "\n%s", btf_log_buf);
2379
2380         free(raw_btf);
2381         free(user_btf);
2382
2383         if (btf_fd != -1)
2384                 close(btf_fd);
2385
2386         return err;
2387 }
2388
2389 static int test_get_info(void)
2390 {
2391         unsigned int i;
2392         int err = 0;
2393
2394         if (args.get_info_test_num)
2395                 return count_result(do_test_get_info(args.get_info_test_num));
2396
2397         for (i = 1; i <= ARRAY_SIZE(get_info_tests); i++)
2398                 err |= count_result(do_test_get_info(i));
2399
2400         return err;
2401 }
2402
2403 struct btf_file_test {
2404         const char *file;
2405         bool btf_kv_notfound;
2406 };
2407
2408 static struct btf_file_test file_tests[] = {
2409 {
2410         .file = "test_btf_haskv.o",
2411 },
2412 {
2413         .file = "test_btf_nokv.o",
2414         .btf_kv_notfound = true,
2415 },
2416 };
2417
2418 static int file_has_btf_elf(const char *fn, bool *has_btf_ext)
2419 {
2420         Elf_Scn *scn = NULL;
2421         GElf_Ehdr ehdr;
2422         int ret = 0;
2423         int elf_fd;
2424         Elf *elf;
2425
2426         if (CHECK(elf_version(EV_CURRENT) == EV_NONE,
2427                   "elf_version(EV_CURRENT) == EV_NONE"))
2428                 return -1;
2429
2430         elf_fd = open(fn, O_RDONLY);
2431         if (CHECK(elf_fd == -1, "open(%s): errno:%d", fn, errno))
2432                 return -1;
2433
2434         elf = elf_begin(elf_fd, ELF_C_READ, NULL);
2435         if (CHECK(!elf, "elf_begin(%s): %s", fn, elf_errmsg(elf_errno()))) {
2436                 ret = -1;
2437                 goto done;
2438         }
2439
2440         if (CHECK(!gelf_getehdr(elf, &ehdr), "!gelf_getehdr(%s)", fn)) {
2441                 ret = -1;
2442                 goto done;
2443         }
2444
2445         while ((scn = elf_nextscn(elf, scn))) {
2446                 const char *sh_name;
2447                 GElf_Shdr sh;
2448
2449                 if (CHECK(gelf_getshdr(scn, &sh) != &sh,
2450                           "file:%s gelf_getshdr != &sh", fn)) {
2451                         ret = -1;
2452                         goto done;
2453                 }
2454
2455                 sh_name = elf_strptr(elf, ehdr.e_shstrndx, sh.sh_name);
2456                 if (!strcmp(sh_name, BTF_ELF_SEC))
2457                         ret = 1;
2458                 if (!strcmp(sh_name, BTF_EXT_ELF_SEC))
2459                         *has_btf_ext = true;
2460         }
2461
2462 done:
2463         close(elf_fd);
2464         elf_end(elf);
2465         return ret;
2466 }
2467
2468 static int do_test_file(unsigned int test_num)
2469 {
2470         const struct btf_file_test *test = &file_tests[test_num - 1];
2471         const char *expected_fnames[] = {"_dummy_tracepoint",
2472                                          "test_long_fname_1",
2473                                          "test_long_fname_2"};
2474         struct bpf_prog_info info = {};
2475         struct bpf_object *obj = NULL;
2476         struct bpf_func_info *finfo;
2477         struct bpf_program *prog;
2478         __u32 info_len, rec_size;
2479         bool has_btf_ext = false;
2480         struct btf *btf = NULL;
2481         void *func_info = NULL;
2482         struct bpf_map *map;
2483         int i, err, prog_fd;
2484
2485         fprintf(stderr, "BTF libbpf test[%u] (%s): ", test_num,
2486                 test->file);
2487
2488         err = file_has_btf_elf(test->file, &has_btf_ext);
2489         if (err == -1)
2490                 return err;
2491
2492         if (err == 0) {
2493                 fprintf(stderr, "SKIP. No ELF %s found", BTF_ELF_SEC);
2494                 skip_cnt++;
2495                 return 0;
2496         }
2497
2498         obj = bpf_object__open(test->file);
2499         if (CHECK(IS_ERR(obj), "obj: %ld", PTR_ERR(obj)))
2500                 return PTR_ERR(obj);
2501
2502         err = bpf_object__btf_fd(obj);
2503         if (CHECK(err == -1, "bpf_object__btf_fd: -1"))
2504                 goto done;
2505
2506         prog = bpf_program__next(NULL, obj);
2507         if (CHECK(!prog, "Cannot find bpf_prog")) {
2508                 err = -1;
2509                 goto done;
2510         }
2511
2512         bpf_program__set_type(prog, BPF_PROG_TYPE_TRACEPOINT);
2513         err = bpf_object__load(obj);
2514         if (CHECK(err < 0, "bpf_object__load: %d", err))
2515                 goto done;
2516         prog_fd = bpf_program__fd(prog);
2517
2518         map = bpf_object__find_map_by_name(obj, "btf_map");
2519         if (CHECK(!map, "btf_map not found")) {
2520                 err = -1;
2521                 goto done;
2522         }
2523
2524         err = (bpf_map__btf_key_type_id(map) == 0 || bpf_map__btf_value_type_id(map) == 0)
2525                 != test->btf_kv_notfound;
2526         if (CHECK(err, "btf_key_type_id:%u btf_value_type_id:%u test->btf_kv_notfound:%u",
2527                   bpf_map__btf_key_type_id(map), bpf_map__btf_value_type_id(map),
2528                   test->btf_kv_notfound))
2529                 goto done;
2530
2531         if (!has_btf_ext)
2532                 goto skip;
2533
2534         /* get necessary program info */
2535         info_len = sizeof(struct bpf_prog_info);
2536         err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
2537
2538         if (CHECK(err == -1, "invalid get info (1st) errno:%d", errno)) {
2539                 fprintf(stderr, "%s\n", btf_log_buf);
2540                 err = -1;
2541                 goto done;
2542         }
2543         if (CHECK(info.func_info_cnt != 3,
2544                   "incorrect info.func_info_cnt (1st) %d",
2545                   info.func_info_cnt)) {
2546                 err = -1;
2547                 goto done;
2548         }
2549         rec_size = info.func_info_rec_size;
2550         if (CHECK(rec_size < 4,
2551                   "incorrect info.func_info_rec_size (1st) %d\n", rec_size)) {
2552                 err = -1;
2553                 goto done;
2554         }
2555
2556         func_info = malloc(info.func_info_cnt * rec_size);
2557         if (CHECK(!func_info, "out of memory")) {
2558                 err = -1;
2559                 goto done;
2560         }
2561
2562         /* reset info to only retrieve func_info related data */
2563         memset(&info, 0, sizeof(info));
2564         info.func_info_cnt = 3;
2565         info.func_info_rec_size = rec_size;
2566         info.func_info = ptr_to_u64(func_info);
2567
2568         err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
2569
2570         if (CHECK(err == -1, "invalid get info (2nd) errno:%d", errno)) {
2571                 fprintf(stderr, "%s\n", btf_log_buf);
2572                 err = -1;
2573                 goto done;
2574         }
2575         if (CHECK(info.func_info_cnt != 3,
2576                   "incorrect info.func_info_cnt (2nd) %d",
2577                   info.func_info_cnt)) {
2578                 err = -1;
2579                 goto done;
2580         }
2581         if (CHECK(info.func_info_rec_size != rec_size,
2582                   "incorrect info.func_info_rec_size (2nd) %d",
2583                   info.func_info_rec_size)) {
2584                 err = -1;
2585                 goto done;
2586         }
2587
2588         err = btf__get_from_id(info.btf_id, &btf);
2589         if (CHECK(err, "cannot get btf from kernel, err: %d", err))
2590                 goto done;
2591
2592         /* check three functions */
2593         finfo = func_info;
2594         for (i = 0; i < 3; i++) {
2595                 const struct btf_type *t;
2596                 const char *fname;
2597
2598                 t = btf__type_by_id(btf, finfo->type_id);
2599                 if (CHECK(!t, "btf__type_by_id failure: id %u",
2600                           finfo->type_id)) {
2601                         err = -1;
2602                         goto done;
2603                 }
2604
2605                 fname = btf__name_by_offset(btf, t->name_off);
2606                 err = strcmp(fname, expected_fnames[i]);
2607                 /* for the second and third functions in .text section,
2608                  * the compiler may order them either way.
2609                  */
2610                 if (i && err)
2611                         err = strcmp(fname, expected_fnames[3 - i]);
2612                 if (CHECK(err, "incorrect fname %s", fname ? : "")) {
2613                         err = -1;
2614                         goto done;
2615                 }
2616
2617                 finfo = (void *)finfo + rec_size;
2618         }
2619
2620 skip:
2621         fprintf(stderr, "OK");
2622
2623 done:
2624         free(func_info);
2625         bpf_object__close(obj);
2626         return err;
2627 }
2628
2629 static int test_file(void)
2630 {
2631         unsigned int i;
2632         int err = 0;
2633
2634         if (args.file_test_num)
2635                 return count_result(do_test_file(args.file_test_num));
2636
2637         for (i = 1; i <= ARRAY_SIZE(file_tests); i++)
2638                 err |= count_result(do_test_file(i));
2639
2640         return err;
2641 }
2642
2643 const char *pprint_enum_str[] = {
2644         "ENUM_ZERO",
2645         "ENUM_ONE",
2646         "ENUM_TWO",
2647         "ENUM_THREE",
2648 };
2649
2650 struct pprint_mapv {
2651         uint32_t ui32;
2652         uint16_t ui16;
2653         /* 2 bytes hole */
2654         int32_t si32;
2655         uint32_t unused_bits2a:2,
2656                 bits28:28,
2657                 unused_bits2b:2;
2658         union {
2659                 uint64_t ui64;
2660                 uint8_t ui8a[8];
2661         };
2662         enum {
2663                 ENUM_ZERO,
2664                 ENUM_ONE,
2665                 ENUM_TWO,
2666                 ENUM_THREE,
2667         } aenum;
2668 };
2669
2670 static struct btf_raw_test pprint_test_template = {
2671         .raw_types = {
2672                 /* unsighed char */                     /* [1] */
2673                 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 8, 1),
2674                 /* unsigned short */                    /* [2] */
2675                 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 16, 2),
2676                 /* unsigned int */                      /* [3] */
2677                 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),
2678                 /* int */                               /* [4] */
2679                 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
2680                 /* unsigned long long */                /* [5] */
2681                 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 64, 8),
2682                 /* 2 bits */                            /* [6] */
2683                 BTF_TYPE_INT_ENC(0, 0, 0, 2, 2),
2684                 /* 28 bits */                           /* [7] */
2685                 BTF_TYPE_INT_ENC(0, 0, 0, 28, 4),
2686                 /* uint8_t[8] */                        /* [8] */
2687                 BTF_TYPE_ARRAY_ENC(9, 1, 8),
2688                 /* typedef unsigned char uint8_t */     /* [9] */
2689                 BTF_TYPEDEF_ENC(NAME_TBD, 1),
2690                 /* typedef unsigned short uint16_t */   /* [10] */
2691                 BTF_TYPEDEF_ENC(NAME_TBD, 2),
2692                 /* typedef unsigned int uint32_t */     /* [11] */
2693                 BTF_TYPEDEF_ENC(NAME_TBD, 3),
2694                 /* typedef int int32_t */               /* [12] */
2695                 BTF_TYPEDEF_ENC(NAME_TBD, 4),
2696                 /* typedef unsigned long long uint64_t *//* [13] */
2697                 BTF_TYPEDEF_ENC(NAME_TBD, 5),
2698                 /* union (anon) */                      /* [14] */
2699                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 0, 2), 8),
2700                 BTF_MEMBER_ENC(NAME_TBD, 13, 0),/* uint64_t ui64; */
2701                 BTF_MEMBER_ENC(NAME_TBD, 8, 0), /* uint8_t ui8a[8]; */
2702                 /* enum (anon) */                       /* [15] */
2703                 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 4), 4),
2704                 BTF_ENUM_ENC(NAME_TBD, 0),
2705                 BTF_ENUM_ENC(NAME_TBD, 1),
2706                 BTF_ENUM_ENC(NAME_TBD, 2),
2707                 BTF_ENUM_ENC(NAME_TBD, 3),
2708                 /* struct pprint_mapv */                /* [16] */
2709                 BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 8), 32),
2710                 BTF_MEMBER_ENC(NAME_TBD, 11, 0),        /* uint32_t ui32 */
2711                 BTF_MEMBER_ENC(NAME_TBD, 10, 32),       /* uint16_t ui16 */
2712                 BTF_MEMBER_ENC(NAME_TBD, 12, 64),       /* int32_t si32 */
2713                 BTF_MEMBER_ENC(NAME_TBD, 6, 96),        /* unused_bits2a */
2714                 BTF_MEMBER_ENC(NAME_TBD, 7, 98),        /* bits28 */
2715                 BTF_MEMBER_ENC(NAME_TBD, 6, 126),       /* unused_bits2b */
2716                 BTF_MEMBER_ENC(0, 14, 128),             /* union (anon) */
2717                 BTF_MEMBER_ENC(NAME_TBD, 15, 192),      /* aenum */
2718                 BTF_END_RAW,
2719         },
2720         .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",
2721         .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"),
2722         .key_size = sizeof(unsigned int),
2723         .value_size = sizeof(struct pprint_mapv),
2724         .key_type_id = 3,       /* unsigned int */
2725         .value_type_id = 16,    /* struct pprint_mapv */
2726         .max_entries = 128 * 1024,
2727 };
2728
2729 static struct btf_pprint_test_meta {
2730         const char *descr;
2731         enum bpf_map_type map_type;
2732         const char *map_name;
2733         bool ordered_map;
2734         bool lossless_map;
2735         bool percpu_map;
2736 } pprint_tests_meta[] = {
2737 {
2738         .descr = "BTF pretty print array",
2739         .map_type = BPF_MAP_TYPE_ARRAY,
2740         .map_name = "pprint_test_array",
2741         .ordered_map = true,
2742         .lossless_map = true,
2743         .percpu_map = false,
2744 },
2745
2746 {
2747         .descr = "BTF pretty print hash",
2748         .map_type = BPF_MAP_TYPE_HASH,
2749         .map_name = "pprint_test_hash",
2750         .ordered_map = false,
2751         .lossless_map = true,
2752         .percpu_map = false,
2753 },
2754
2755 {
2756         .descr = "BTF pretty print lru hash",
2757         .map_type = BPF_MAP_TYPE_LRU_HASH,
2758         .map_name = "pprint_test_lru_hash",
2759         .ordered_map = false,
2760         .lossless_map = false,
2761         .percpu_map = false,
2762 },
2763
2764 {
2765         .descr = "BTF pretty print percpu array",
2766         .map_type = BPF_MAP_TYPE_PERCPU_ARRAY,
2767         .map_name = "pprint_test_percpu_array",
2768         .ordered_map = true,
2769         .lossless_map = true,
2770         .percpu_map = true,
2771 },
2772
2773 {
2774         .descr = "BTF pretty print percpu hash",
2775         .map_type = BPF_MAP_TYPE_PERCPU_HASH,
2776         .map_name = "pprint_test_percpu_hash",
2777         .ordered_map = false,
2778         .lossless_map = true,
2779         .percpu_map = true,
2780 },
2781
2782 {
2783         .descr = "BTF pretty print lru percpu hash",
2784         .map_type = BPF_MAP_TYPE_LRU_PERCPU_HASH,
2785         .map_name = "pprint_test_lru_percpu_hash",
2786         .ordered_map = false,
2787         .lossless_map = false,
2788         .percpu_map = true,
2789 },
2790
2791 };
2792
2793
2794 static void set_pprint_mapv(struct pprint_mapv *v, uint32_t i,
2795                             int num_cpus, int rounded_value_size)
2796 {
2797         int cpu;
2798
2799         for (cpu = 0; cpu < num_cpus; cpu++) {
2800                 v->ui32 = i + cpu;
2801                 v->si32 = -i;
2802                 v->unused_bits2a = 3;
2803                 v->bits28 = i;
2804                 v->unused_bits2b = 3;
2805                 v->ui64 = i;
2806                 v->aenum = i & 0x03;
2807                 v = (void *)v + rounded_value_size;
2808         }
2809 }
2810
2811 static int check_line(const char *expected_line, int nexpected_line,
2812                       int expected_line_len, const char *line)
2813 {
2814         if (CHECK(nexpected_line == expected_line_len,
2815                   "expected_line is too long"))
2816                 return -1;
2817
2818         if (strcmp(expected_line, line)) {
2819                 fprintf(stderr, "unexpected pprint output\n");
2820                 fprintf(stderr, "expected: %s", expected_line);
2821                 fprintf(stderr, "    read: %s", line);
2822                 return -1;
2823         }
2824
2825         return 0;
2826 }
2827
2828
2829 static int do_test_pprint(void)
2830 {
2831         const struct btf_raw_test *test = &pprint_test_template;
2832         struct bpf_create_map_attr create_attr = {};
2833         bool ordered_map, lossless_map, percpu_map;
2834         int err, ret, num_cpus, rounded_value_size;
2835         struct pprint_mapv *mapv = NULL;
2836         unsigned int key, nr_read_elems;
2837         int map_fd = -1, btf_fd = -1;
2838         unsigned int raw_btf_size;
2839         char expected_line[255];
2840         FILE *pin_file = NULL;
2841         char pin_path[255];
2842         size_t line_len = 0;
2843         char *line = NULL;
2844         uint8_t *raw_btf;
2845         ssize_t nread;
2846
2847         fprintf(stderr, "%s......", test->descr);
2848         raw_btf = btf_raw_create(&hdr_tmpl, test->raw_types,
2849                                  test->str_sec, test->str_sec_size,
2850                                  &raw_btf_size);
2851
2852         if (!raw_btf)
2853                 return -1;
2854
2855         *btf_log_buf = '\0';
2856         btf_fd = bpf_load_btf(raw_btf, raw_btf_size,
2857                               btf_log_buf, BTF_LOG_BUF_SIZE,
2858                               args.always_log);
2859         free(raw_btf);
2860
2861         if (CHECK(btf_fd == -1, "errno:%d", errno)) {
2862                 err = -1;
2863                 goto done;
2864         }
2865
2866         create_attr.name = test->map_name;
2867         create_attr.map_type = test->map_type;
2868         create_attr.key_size = test->key_size;
2869         create_attr.value_size = test->value_size;
2870         create_attr.max_entries = test->max_entries;
2871         create_attr.btf_fd = btf_fd;
2872         create_attr.btf_key_type_id = test->key_type_id;
2873         create_attr.btf_value_type_id = test->value_type_id;
2874
2875         map_fd = bpf_create_map_xattr(&create_attr);
2876         if (CHECK(map_fd == -1, "errno:%d", errno)) {
2877                 err = -1;
2878                 goto done;
2879         }
2880
2881         ret = snprintf(pin_path, sizeof(pin_path), "%s/%s",
2882                        "/sys/fs/bpf", test->map_name);
2883
2884         if (CHECK(ret == sizeof(pin_path), "pin_path %s/%s is too long",
2885                   "/sys/fs/bpf", test->map_name)) {
2886                 err = -1;
2887                 goto done;
2888         }
2889
2890         err = bpf_obj_pin(map_fd, pin_path);
2891         if (CHECK(err, "bpf_obj_pin(%s): errno:%d.", pin_path, errno))
2892                 goto done;
2893
2894         percpu_map = test->percpu_map;
2895         num_cpus = percpu_map ? bpf_num_possible_cpus() : 1;
2896         rounded_value_size = round_up(sizeof(struct pprint_mapv), 8);
2897         mapv = calloc(num_cpus, rounded_value_size);
2898         if (CHECK(!mapv, "mapv allocation failure")) {
2899                 err = -1;
2900                 goto done;
2901         }
2902
2903         for (key = 0; key < test->max_entries; key++) {
2904                 set_pprint_mapv(mapv, key, num_cpus, rounded_value_size);
2905                 bpf_map_update_elem(map_fd, &key, mapv, 0);
2906         }
2907
2908         pin_file = fopen(pin_path, "r");
2909         if (CHECK(!pin_file, "fopen(%s): errno:%d", pin_path, errno)) {
2910                 err = -1;
2911                 goto done;
2912         }
2913
2914         /* Skip lines start with '#' */
2915         while ((nread = getline(&line, &line_len, pin_file)) > 0 &&
2916                *line == '#')
2917                 ;
2918
2919         if (CHECK(nread <= 0, "Unexpected EOF")) {
2920                 err = -1;
2921                 goto done;
2922         }
2923
2924         nr_read_elems = 0;
2925         ordered_map = test->ordered_map;
2926         lossless_map = test->lossless_map;
2927         do {
2928                 struct pprint_mapv *cmapv;
2929                 ssize_t nexpected_line;
2930                 unsigned int next_key;
2931                 int cpu;
2932
2933                 next_key = ordered_map ? nr_read_elems : atoi(line);
2934                 set_pprint_mapv(mapv, next_key, num_cpus, rounded_value_size);
2935                 cmapv = mapv;
2936
2937                 for (cpu = 0; cpu < num_cpus; cpu++) {
2938                         if (percpu_map) {
2939                                 /* for percpu map, the format looks like:
2940                                  * <key>: {
2941                                  *      cpu0: <value_on_cpu0>
2942                                  *      cpu1: <value_on_cpu1>
2943                                  *      ...
2944                                  *      cpun: <value_on_cpun>
2945                                  * }
2946                                  *
2947                                  * let us verify the line containing the key here.
2948                                  */
2949                                 if (cpu == 0) {
2950                                         nexpected_line = snprintf(expected_line,
2951                                                                   sizeof(expected_line),
2952                                                                   "%u: {\n",
2953                                                                   next_key);
2954
2955                                         err = check_line(expected_line, nexpected_line,
2956                                                          sizeof(expected_line), line);
2957                                         if (err == -1)
2958                                                 goto done;
2959                                 }
2960
2961                                 /* read value@cpu */
2962                                 nread = getline(&line, &line_len, pin_file);
2963                                 if (nread < 0)
2964                                         break;
2965                         }
2966
2967                         nexpected_line = snprintf(expected_line, sizeof(expected_line),
2968                                                   "%s%u: {%u,0,%d,0x%x,0x%x,0x%x,"
2969                                                   "{%lu|[%u,%u,%u,%u,%u,%u,%u,%u]},%s}\n",
2970                                                   percpu_map ? "\tcpu" : "",
2971                                                   percpu_map ? cpu : next_key,
2972                                                   cmapv->ui32, cmapv->si32,
2973                                                   cmapv->unused_bits2a,
2974                                                   cmapv->bits28,
2975                                                   cmapv->unused_bits2b,
2976                                                   cmapv->ui64,
2977                                                   cmapv->ui8a[0], cmapv->ui8a[1],
2978                                                   cmapv->ui8a[2], cmapv->ui8a[3],
2979                                                   cmapv->ui8a[4], cmapv->ui8a[5],
2980                                                   cmapv->ui8a[6], cmapv->ui8a[7],
2981                                                   pprint_enum_str[cmapv->aenum]);
2982
2983                         err = check_line(expected_line, nexpected_line,
2984                                          sizeof(expected_line), line);
2985                         if (err == -1)
2986                                 goto done;
2987
2988                         cmapv = (void *)cmapv + rounded_value_size;
2989                 }
2990
2991                 if (percpu_map) {
2992                         /* skip the last bracket for the percpu map */
2993                         nread = getline(&line, &line_len, pin_file);
2994                         if (nread < 0)
2995                                 break;
2996                 }
2997
2998                 nread = getline(&line, &line_len, pin_file);
2999         } while (++nr_read_elems < test->max_entries && nread > 0);
3000
3001         if (lossless_map &&
3002             CHECK(nr_read_elems < test->max_entries,
3003                   "Unexpected EOF. nr_read_elems:%u test->max_entries:%u",
3004                   nr_read_elems, test->max_entries)) {
3005                 err = -1;
3006                 goto done;
3007         }
3008
3009         if (CHECK(nread > 0, "Unexpected extra pprint output: %s", line)) {
3010                 err = -1;
3011                 goto done;
3012         }
3013
3014         err = 0;
3015
3016 done:
3017         if (mapv)
3018                 free(mapv);
3019         if (!err)
3020                 fprintf(stderr, "OK");
3021         if (*btf_log_buf && (err || args.always_log))
3022                 fprintf(stderr, "\n%s", btf_log_buf);
3023         if (btf_fd != -1)
3024                 close(btf_fd);
3025         if (map_fd != -1)
3026                 close(map_fd);
3027         if (pin_file)
3028                 fclose(pin_file);
3029         unlink(pin_path);
3030         free(line);
3031
3032         return err;
3033 }
3034
3035 static int test_pprint(void)
3036 {
3037         unsigned int i;
3038         int err = 0;
3039
3040         for (i = 0; i < ARRAY_SIZE(pprint_tests_meta); i++) {
3041                 pprint_test_template.descr = pprint_tests_meta[i].descr;
3042                 pprint_test_template.map_type = pprint_tests_meta[i].map_type;
3043                 pprint_test_template.map_name = pprint_tests_meta[i].map_name;
3044                 pprint_test_template.ordered_map = pprint_tests_meta[i].ordered_map;
3045                 pprint_test_template.lossless_map = pprint_tests_meta[i].lossless_map;
3046                 pprint_test_template.percpu_map = pprint_tests_meta[i].percpu_map;
3047
3048                 err |= count_result(do_test_pprint());
3049         }
3050
3051         return err;
3052 }
3053
3054 static struct btf_func_type_test {
3055         const char *descr;
3056         const char *str_sec;
3057         __u32 raw_types[MAX_NR_RAW_TYPES];
3058         __u32 str_sec_size;
3059         struct bpf_insn insns[MAX_INSNS];
3060         __u32 prog_type;
3061         __u32 func_info[MAX_SUBPROGS][2];
3062         __u32 func_info_rec_size;
3063         __u32 func_info_cnt;
3064         bool expected_prog_load_failure;
3065 } func_type_test[] = {
3066 {
3067         .descr = "func_type (main func + one sub)",
3068         .raw_types = {
3069                 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),   /* [1] */
3070                 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),        /* [2] */
3071                 BTF_FUNC_PROTO_ENC(1, 2),                       /* [3] */
3072                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3073                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
3074                 BTF_FUNC_PROTO_ENC(1, 2),                       /* [4] */
3075                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
3076                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3077                 BTF_FUNC_ENC(NAME_TBD, 3),                      /* [5] */
3078                 BTF_FUNC_ENC(NAME_TBD, 4),                      /* [6] */
3079                 BTF_END_RAW,
3080         },
3081         .str_sec = "\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB",
3082         .str_sec_size = sizeof("\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB"),
3083         .insns = {
3084                 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
3085                 BPF_MOV64_IMM(BPF_REG_0, 1),
3086                 BPF_EXIT_INSN(),
3087                 BPF_MOV64_IMM(BPF_REG_0, 2),
3088                 BPF_EXIT_INSN(),
3089         },
3090         .prog_type = BPF_PROG_TYPE_TRACEPOINT,
3091         .func_info = { {0, 5}, {3, 6} },
3092         .func_info_rec_size = 8,
3093         .func_info_cnt = 2,
3094 },
3095
3096 {
3097         .descr = "func_type (Incorrect func_info_rec_size)",
3098         .raw_types = {
3099                 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),   /* [1] */
3100                 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),        /* [2] */
3101                 BTF_FUNC_PROTO_ENC(1, 2),                       /* [3] */
3102                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3103                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
3104                 BTF_FUNC_PROTO_ENC(1, 2),                       /* [4] */
3105                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
3106                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3107                 BTF_FUNC_ENC(NAME_TBD, 3),                      /* [5] */
3108                 BTF_FUNC_ENC(NAME_TBD, 4),                      /* [6] */
3109                 BTF_END_RAW,
3110         },
3111         .str_sec = "\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB",
3112         .str_sec_size = sizeof("\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB"),
3113         .insns = {
3114                 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
3115                 BPF_MOV64_IMM(BPF_REG_0, 1),
3116                 BPF_EXIT_INSN(),
3117                 BPF_MOV64_IMM(BPF_REG_0, 2),
3118                 BPF_EXIT_INSN(),
3119         },
3120         .prog_type = BPF_PROG_TYPE_TRACEPOINT,
3121         .func_info = { {0, 5}, {3, 6} },
3122         .func_info_rec_size = 4,
3123         .func_info_cnt = 2,
3124         .expected_prog_load_failure = true,
3125 },
3126
3127 {
3128         .descr = "func_type (Incorrect func_info_cnt)",
3129         .raw_types = {
3130                 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),   /* [1] */
3131                 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),        /* [2] */
3132                 BTF_FUNC_PROTO_ENC(1, 2),                       /* [3] */
3133                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3134                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
3135                 BTF_FUNC_PROTO_ENC(1, 2),                       /* [4] */
3136                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
3137                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3138                 BTF_FUNC_ENC(NAME_TBD, 3),                      /* [5] */
3139                 BTF_FUNC_ENC(NAME_TBD, 4),                      /* [6] */
3140                 BTF_END_RAW,
3141         },
3142         .str_sec = "\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB",
3143         .str_sec_size = sizeof("\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB"),
3144         .insns = {
3145                 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
3146                 BPF_MOV64_IMM(BPF_REG_0, 1),
3147                 BPF_EXIT_INSN(),
3148                 BPF_MOV64_IMM(BPF_REG_0, 2),
3149                 BPF_EXIT_INSN(),
3150         },
3151         .prog_type = BPF_PROG_TYPE_TRACEPOINT,
3152         .func_info = { {0, 5}, {3, 6} },
3153         .func_info_rec_size = 8,
3154         .func_info_cnt = 1,
3155         .expected_prog_load_failure = true,
3156 },
3157
3158 {
3159         .descr = "func_type (Incorrect bpf_func_info.insn_off)",
3160         .raw_types = {
3161                 BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),   /* [1] */
3162                 BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),        /* [2] */
3163                 BTF_FUNC_PROTO_ENC(1, 2),                       /* [3] */
3164                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3165                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
3166                 BTF_FUNC_PROTO_ENC(1, 2),                       /* [4] */
3167                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
3168                         BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3169                 BTF_FUNC_ENC(NAME_TBD, 3),                      /* [5] */
3170                 BTF_FUNC_ENC(NAME_TBD, 4),                      /* [6] */
3171                 BTF_END_RAW,
3172         },
3173         .str_sec = "\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB",
3174         .str_sec_size = sizeof("\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB"),
3175         .insns = {
3176                 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
3177                 BPF_MOV64_IMM(BPF_REG_0, 1),
3178                 BPF_EXIT_INSN(),
3179                 BPF_MOV64_IMM(BPF_REG_0, 2),
3180                 BPF_EXIT_INSN(),
3181         },
3182         .prog_type = BPF_PROG_TYPE_TRACEPOINT,
3183         .func_info = { {0, 5}, {2, 6} },
3184         .func_info_rec_size = 8,
3185         .func_info_cnt = 2,
3186         .expected_prog_load_failure = true,
3187 },
3188
3189 };
3190
3191 static size_t probe_prog_length(const struct bpf_insn *fp)
3192 {
3193         size_t len;
3194
3195         for (len = MAX_INSNS - 1; len > 0; --len)
3196                 if (fp[len].code != 0 || fp[len].imm != 0)
3197                         break;
3198         return len + 1;
3199 }
3200
3201 static int do_test_func_type(int test_num)
3202 {
3203         const struct btf_func_type_test *test = &func_type_test[test_num];
3204         unsigned int raw_btf_size, info_len, rec_size;
3205         int i, btf_fd = -1, prog_fd = -1, err = 0;
3206         struct bpf_load_program_attr attr = {};
3207         void *raw_btf, *func_info = NULL;
3208         struct bpf_prog_info info = {};
3209         struct bpf_func_info *finfo;
3210
3211         fprintf(stderr, "%s......", test->descr);
3212         raw_btf = btf_raw_create(&hdr_tmpl, test->raw_types,
3213                                  test->str_sec, test->str_sec_size,
3214                                  &raw_btf_size);
3215
3216         if (!raw_btf)
3217                 return -1;
3218
3219         *btf_log_buf = '\0';
3220         btf_fd = bpf_load_btf(raw_btf, raw_btf_size,
3221                               btf_log_buf, BTF_LOG_BUF_SIZE,
3222                               args.always_log);
3223         free(raw_btf);
3224
3225         if (CHECK(btf_fd == -1, "invalid btf_fd errno:%d", errno)) {
3226                 err = -1;
3227                 goto done;
3228         }
3229
3230         if (*btf_log_buf && args.always_log)
3231                 fprintf(stderr, "\n%s", btf_log_buf);
3232
3233         attr.prog_type = test->prog_type;
3234         attr.insns = test->insns;
3235         attr.insns_cnt = probe_prog_length(attr.insns);
3236         attr.license = "GPL";
3237         attr.prog_btf_fd = btf_fd;
3238         attr.func_info_rec_size = test->func_info_rec_size;
3239         attr.func_info_cnt = test->func_info_cnt;
3240         attr.func_info = test->func_info;
3241
3242         *btf_log_buf = '\0';
3243         prog_fd = bpf_load_program_xattr(&attr, btf_log_buf,
3244                                          BTF_LOG_BUF_SIZE);
3245         if (test->expected_prog_load_failure && prog_fd == -1) {
3246                 err = 0;
3247                 goto done;
3248         }
3249         if (CHECK(prog_fd == -1, "invalid prog_id errno:%d", errno)) {
3250                 fprintf(stderr, "%s\n", btf_log_buf);
3251                 err = -1;
3252                 goto done;
3253         }
3254
3255         /* get necessary lens */
3256         info_len = sizeof(struct bpf_prog_info);
3257         err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
3258         if (CHECK(err == -1, "invalid get info (1st) errno:%d", errno)) {
3259                 fprintf(stderr, "%s\n", btf_log_buf);
3260                 err = -1;
3261                 goto done;
3262         }
3263         if (CHECK(info.func_info_cnt != 2,
3264                   "incorrect info.func_info_cnt (1st) %d\n",
3265                   info.func_info_cnt)) {
3266                 err = -1;
3267                 goto done;
3268         }
3269         rec_size = info.func_info_rec_size;
3270         if (CHECK(rec_size < 4,
3271                   "incorrect info.func_info_rec_size (1st) %d\n", rec_size)) {
3272                 err = -1;
3273                 goto done;
3274         }
3275
3276         func_info = malloc(info.func_info_cnt * rec_size);
3277         if (CHECK(!func_info, "out of memory")) {
3278                 err = -1;
3279                 goto done;
3280         }
3281
3282         /* reset info to only retrieve func_info related data */
3283         memset(&info, 0, sizeof(info));
3284         info.func_info_cnt = 2;
3285         info.func_info_rec_size = rec_size;
3286         info.func_info = ptr_to_u64(func_info);
3287         err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
3288         if (CHECK(err == -1, "invalid get info (2nd) errno:%d", errno)) {
3289                 fprintf(stderr, "%s\n", btf_log_buf);
3290                 err = -1;
3291                 goto done;
3292         }
3293         if (CHECK(info.func_info_cnt != 2,
3294                   "incorrect info.func_info_cnt (2nd) %d\n",
3295                   info.func_info_cnt)) {
3296                 err = -1;
3297                 goto done;
3298         }
3299         if (CHECK(info.func_info_rec_size != rec_size,
3300                   "incorrect info.func_info_rec_size (2nd) %d\n",
3301                   info.func_info_rec_size)) {
3302                 err = -1;
3303                 goto done;
3304         }
3305
3306         if (CHECK(!info.func_info,
3307                   "info.func_info == 0. kernel.kptr_restrict is set?")) {
3308                 err = -1;
3309                 goto done;
3310         }
3311
3312         finfo = func_info;
3313         for (i = 0; i < 2; i++) {
3314                 if (CHECK(finfo->type_id != test->func_info[i][1],
3315                           "incorrect func_type %u expected %u",
3316                           finfo->type_id, test->func_info[i][1])) {
3317                         err = -1;
3318                         goto done;
3319                 }
3320                 finfo = (void *)finfo + rec_size;
3321         }
3322
3323 done:
3324         if (*btf_log_buf && (err || args.always_log))
3325                 fprintf(stderr, "\n%s", btf_log_buf);
3326
3327         if (btf_fd != -1)
3328                 close(btf_fd);
3329         if (prog_fd != -1)
3330                 close(prog_fd);
3331         free(func_info);
3332         return err;
3333 }
3334
3335 static int test_func_type(void)
3336 {
3337         unsigned int i;
3338         int err = 0;
3339
3340         for (i = 0; i < ARRAY_SIZE(func_type_test); i++)
3341                 err |= count_result(do_test_func_type(i));
3342
3343         return err;
3344 }
3345
3346 static void usage(const char *cmd)
3347 {
3348         fprintf(stderr, "Usage: %s [-l] [[-r test_num (1 - %zu)] |"
3349                         " [-g test_num (1 - %zu)] |"
3350                         " [-f test_num (1 - %zu)] | [-p] | [-k] ]\n",
3351                 cmd, ARRAY_SIZE(raw_tests), ARRAY_SIZE(get_info_tests),
3352                 ARRAY_SIZE(file_tests));
3353 }
3354
3355 static int parse_args(int argc, char **argv)
3356 {
3357         const char *optstr = "lpkf:r:g:";
3358         int opt;
3359
3360         while ((opt = getopt(argc, argv, optstr)) != -1) {
3361                 switch (opt) {
3362                 case 'l':
3363                         args.always_log = true;
3364                         break;
3365                 case 'f':
3366                         args.file_test_num = atoi(optarg);
3367                         args.file_test = true;
3368                         break;
3369                 case 'r':
3370                         args.raw_test_num = atoi(optarg);
3371                         args.raw_test = true;
3372                         break;
3373                 case 'g':
3374                         args.get_info_test_num = atoi(optarg);
3375                         args.get_info_test = true;
3376                         break;
3377                 case 'p':
3378                         args.pprint_test = true;
3379                         break;
3380                 case 'k':
3381                         args.func_type_test = true;
3382                         break;
3383                 case 'h':
3384                         usage(argv[0]);
3385                         exit(0);
3386                 default:
3387                                 usage(argv[0]);
3388                                 return -1;
3389                 }
3390         }
3391
3392         if (args.raw_test_num &&
3393             (args.raw_test_num < 1 ||
3394              args.raw_test_num > ARRAY_SIZE(raw_tests))) {
3395                 fprintf(stderr, "BTF raw test number must be [1 - %zu]\n",
3396                         ARRAY_SIZE(raw_tests));
3397                 return -1;
3398         }
3399
3400         if (args.file_test_num &&
3401             (args.file_test_num < 1 ||
3402              args.file_test_num > ARRAY_SIZE(file_tests))) {
3403                 fprintf(stderr, "BTF file test number must be [1 - %zu]\n",
3404                         ARRAY_SIZE(file_tests));
3405                 return -1;
3406         }
3407
3408         if (args.get_info_test_num &&
3409             (args.get_info_test_num < 1 ||
3410              args.get_info_test_num > ARRAY_SIZE(get_info_tests))) {
3411                 fprintf(stderr, "BTF get info test number must be [1 - %zu]\n",
3412                         ARRAY_SIZE(get_info_tests));
3413                 return -1;
3414         }
3415
3416         return 0;
3417 }
3418
3419 static void print_summary(void)
3420 {
3421         fprintf(stderr, "PASS:%u SKIP:%u FAIL:%u\n",
3422                 pass_cnt - skip_cnt, skip_cnt, error_cnt);
3423 }
3424
3425 int main(int argc, char **argv)
3426 {
3427         int err = 0;
3428
3429         err = parse_args(argc, argv);
3430         if (err)
3431                 return err;
3432
3433         if (args.always_log)
3434                 libbpf_set_print(__base_pr, __base_pr, __base_pr);
3435
3436         if (args.raw_test)
3437                 err |= test_raw();
3438
3439         if (args.get_info_test)
3440                 err |= test_get_info();
3441
3442         if (args.file_test)
3443                 err |= test_file();
3444
3445         if (args.pprint_test)
3446                 err |= test_pprint();
3447
3448         if (args.func_type_test)
3449                 err |= test_func_type();
3450
3451         if (args.raw_test || args.get_info_test || args.file_test ||
3452             args.pprint_test || args.func_type_test)
3453                 goto done;
3454
3455         err |= test_raw();
3456         err |= test_get_info();
3457         err |= test_file();
3458
3459 done:
3460         print_summary();
3461         return err;
3462 }