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