xref: /linux/tools/testing/selftests/bpf/prog_tests/btf.c (revision 1f8d99de1d1b4b3764203ae02db57041475dab84)
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_util.h"
26 #include "../test_btf.h"
27 #include "test_progs.h"
28 
29 #define MAX_INSNS	512
30 #define MAX_SUBPROGS	16
31 
32 static int duration = 0;
33 static bool always_log;
34 
35 #undef CHECK
36 #define CHECK(condition, format...) _CHECK(condition, "check", duration, format)
37 
38 #define BTF_END_RAW 0xdeadbeef
39 #define NAME_TBD 0xdeadb33f
40 
41 #define NAME_NTH(N) (0xfffe0000 | N)
42 #define IS_NAME_NTH(X) ((X & 0xffff0000) == 0xfffe0000)
43 #define GET_NAME_NTH_IDX(X) (X & 0x0000ffff)
44 
45 #define MAX_NR_RAW_U32 1024
46 #define BTF_LOG_BUF_SIZE 65535
47 
48 static char btf_log_buf[BTF_LOG_BUF_SIZE];
49 
50 static struct btf_header hdr_tmpl = {
51 	.magic = BTF_MAGIC,
52 	.version = BTF_VERSION,
53 	.hdr_len = sizeof(struct btf_header),
54 };
55 
56 /* several different mapv kinds(types) supported by pprint */
57 enum pprint_mapv_kind_t {
58 	PPRINT_MAPV_KIND_BASIC = 0,
59 	PPRINT_MAPV_KIND_INT128,
60 };
61 
62 struct btf_raw_test {
63 	const char *descr;
64 	const char *str_sec;
65 	const char *map_name;
66 	const char *err_str;
67 	__u32 raw_types[MAX_NR_RAW_U32];
68 	__u32 str_sec_size;
69 	enum bpf_map_type map_type;
70 	__u32 key_size;
71 	__u32 value_size;
72 	__u32 key_type_id;
73 	__u32 value_type_id;
74 	__u32 max_entries;
75 	bool btf_load_err;
76 	bool map_create_err;
77 	bool ordered_map;
78 	bool lossless_map;
79 	bool percpu_map;
80 	int hdr_len_delta;
81 	int type_off_delta;
82 	int str_off_delta;
83 	int str_len_delta;
84 	enum pprint_mapv_kind_t mapv_kind;
85 };
86 
87 #define BTF_STR_SEC(str) \
88 	.str_sec = str, .str_sec_size = sizeof(str)
89 
90 static struct btf_raw_test raw_tests[] = {
91 /* enum E {
92  *     E0,
93  *     E1,
94  * };
95  *
96  * struct A {
97  *	unsigned long long m;
98  *	int n;
99  *	char o;
100  *	[3 bytes hole]
101  *	int p[8];
102  *	int q[4][8];
103  *	enum E r;
104  * };
105  */
106 {
107 	.descr = "struct test #1",
108 	.raw_types = {
109 		/* int */
110 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
111 		/* unsigned long long */
112 		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
113 		/* char */
114 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
115 		/* int[8] */
116 		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
117 		/* struct A { */				/* [5] */
118 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 6), 180),
119 		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
120 		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
121 		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
122 		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
123 		BTF_MEMBER_ENC(NAME_TBD, 6, 384),/* int q[4][8]		*/
124 		BTF_MEMBER_ENC(NAME_TBD, 7, 1408), /* enum E r		*/
125 		/* } */
126 		/* int[4][8] */
127 		BTF_TYPE_ARRAY_ENC(4, 1, 4),			/* [6] */
128 		/* enum E */					/* [7] */
129 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), sizeof(int)),
130 		BTF_ENUM_ENC(NAME_TBD, 0),
131 		BTF_ENUM_ENC(NAME_TBD, 1),
132 		BTF_END_RAW,
133 	},
134 	.str_sec = "\0A\0m\0n\0o\0p\0q\0r\0E\0E0\0E1",
135 	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0q\0r\0E\0E0\0E1"),
136 	.map_type = BPF_MAP_TYPE_ARRAY,
137 	.map_name = "struct_test1_map",
138 	.key_size = sizeof(int),
139 	.value_size = 180,
140 	.key_type_id = 1,
141 	.value_type_id = 5,
142 	.max_entries = 4,
143 },
144 
145 /* typedef struct b Struct_B;
146  *
147  * struct A {
148  *     int m;
149  *     struct b n[4];
150  *     const Struct_B o[4];
151  * };
152  *
153  * struct B {
154  *     int m;
155  *     int n;
156  * };
157  */
158 {
159 	.descr = "struct test #2",
160 	.raw_types = {
161 		/* int */					/* [1] */
162 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
163 		/* struct b [4] */				/* [2] */
164 		BTF_TYPE_ARRAY_ENC(4, 1, 4),
165 
166 		/* struct A { */				/* [3] */
167 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 3), 68),
168 		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int m;		*/
169 		BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* struct B n[4]	*/
170 		BTF_MEMBER_ENC(NAME_TBD, 8, 288),/* const Struct_B o[4];*/
171 		/* } */
172 
173 		/* struct B { */				/* [4] */
174 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
175 		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int m; */
176 		BTF_MEMBER_ENC(NAME_TBD, 1, 32),/* int n; */
177 		/* } */
178 
179 		/* const int */					/* [5] */
180 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 1),
181 		/* typedef struct b Struct_B */	/* [6] */
182 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), 4),
183 		/* const Struct_B */				/* [7] */
184 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 6),
185 		/* const Struct_B [4] */			/* [8] */
186 		BTF_TYPE_ARRAY_ENC(7, 1, 4),
187 		BTF_END_RAW,
188 	},
189 	.str_sec = "\0A\0m\0n\0o\0B\0m\0n\0Struct_B",
190 	.str_sec_size = sizeof("\0A\0m\0n\0o\0B\0m\0n\0Struct_B"),
191 	.map_type = BPF_MAP_TYPE_ARRAY,
192 	.map_name = "struct_test2_map",
193 	.key_size = sizeof(int),
194 	.value_size = 68,
195 	.key_type_id = 1,
196 	.value_type_id = 3,
197 	.max_entries = 4,
198 },
199 {
200 	.descr = "struct test #3 Invalid member offset",
201 	.raw_types = {
202 		/* int */					/* [1] */
203 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
204 		/* int64 */					/* [2] */
205 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 64, 8),
206 
207 		/* struct A { */				/* [3] */
208 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 16),
209 		BTF_MEMBER_ENC(NAME_TBD, 1, 64),	/* int m;		*/
210 		BTF_MEMBER_ENC(NAME_TBD, 2, 0),		/* int64 n; */
211 		/* } */
212 		BTF_END_RAW,
213 	},
214 	.str_sec = "\0A\0m\0n\0",
215 	.str_sec_size = sizeof("\0A\0m\0n\0"),
216 	.map_type = BPF_MAP_TYPE_ARRAY,
217 	.map_name = "struct_test3_map",
218 	.key_size = sizeof(int),
219 	.value_size = 16,
220 	.key_type_id = 1,
221 	.value_type_id = 3,
222 	.max_entries = 4,
223 	.btf_load_err = true,
224 	.err_str = "Invalid member bits_offset",
225 },
226 /*
227  * struct A {
228  *	unsigned long long m;
229  *	int n;
230  *	char o;
231  *	[3 bytes hole]
232  *	int p[8];
233  * };
234  */
235 {
236 	.descr = "global data test #1",
237 	.raw_types = {
238 		/* int */
239 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
240 		/* unsigned long long */
241 		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
242 		/* char */
243 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
244 		/* int[8] */
245 		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
246 		/* struct A { */				/* [5] */
247 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
248 		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
249 		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
250 		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
251 		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
252 		/* } */
253 		BTF_END_RAW,
254 	},
255 	.str_sec = "\0A\0m\0n\0o\0p",
256 	.str_sec_size = sizeof("\0A\0m\0n\0o\0p"),
257 	.map_type = BPF_MAP_TYPE_ARRAY,
258 	.map_name = "struct_test1_map",
259 	.key_size = sizeof(int),
260 	.value_size = 48,
261 	.key_type_id = 1,
262 	.value_type_id = 5,
263 	.max_entries = 4,
264 },
265 /*
266  * struct A {
267  *	unsigned long long m;
268  *	int n;
269  *	char o;
270  *	[3 bytes hole]
271  *	int p[8];
272  * };
273  * static struct A t; <- in .bss
274  */
275 {
276 	.descr = "global data test #2",
277 	.raw_types = {
278 		/* int */
279 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
280 		/* unsigned long long */
281 		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
282 		/* char */
283 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
284 		/* int[8] */
285 		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
286 		/* struct A { */				/* [5] */
287 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
288 		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
289 		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
290 		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
291 		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
292 		/* } */
293 		/* static struct A t */
294 		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
295 		/* .bss section */				/* [7] */
296 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 48),
297 		BTF_VAR_SECINFO_ENC(6, 0, 48),
298 		BTF_END_RAW,
299 	},
300 	.str_sec = "\0A\0m\0n\0o\0p\0t\0.bss",
301 	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0.bss"),
302 	.map_type = BPF_MAP_TYPE_ARRAY,
303 	.map_name = ".bss",
304 	.key_size = sizeof(int),
305 	.value_size = 48,
306 	.key_type_id = 0,
307 	.value_type_id = 7,
308 	.max_entries = 1,
309 },
310 {
311 	.descr = "global data test #3",
312 	.raw_types = {
313 		/* int */
314 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
315 		/* static int t */
316 		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
317 		/* .bss section */				/* [3] */
318 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
319 		BTF_VAR_SECINFO_ENC(2, 0, 4),
320 		BTF_END_RAW,
321 	},
322 	.str_sec = "\0t\0.bss",
323 	.str_sec_size = sizeof("\0t\0.bss"),
324 	.map_type = BPF_MAP_TYPE_ARRAY,
325 	.map_name = ".bss",
326 	.key_size = sizeof(int),
327 	.value_size = 4,
328 	.key_type_id = 0,
329 	.value_type_id = 3,
330 	.max_entries = 1,
331 },
332 {
333 	.descr = "global data test #4, unsupported linkage",
334 	.raw_types = {
335 		/* int */
336 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
337 		/* static int t */
338 		BTF_VAR_ENC(NAME_TBD, 1, 2),			/* [2] */
339 		/* .bss section */				/* [3] */
340 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
341 		BTF_VAR_SECINFO_ENC(2, 0, 4),
342 		BTF_END_RAW,
343 	},
344 	.str_sec = "\0t\0.bss",
345 	.str_sec_size = sizeof("\0t\0.bss"),
346 	.map_type = BPF_MAP_TYPE_ARRAY,
347 	.map_name = ".bss",
348 	.key_size = sizeof(int),
349 	.value_size = 4,
350 	.key_type_id = 0,
351 	.value_type_id = 3,
352 	.max_entries = 1,
353 	.btf_load_err = true,
354 	.err_str = "Linkage not supported",
355 },
356 {
357 	.descr = "global data test #5, invalid var type",
358 	.raw_types = {
359 		/* static void t */
360 		BTF_VAR_ENC(NAME_TBD, 0, 0),			/* [1] */
361 		/* .bss section */				/* [2] */
362 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
363 		BTF_VAR_SECINFO_ENC(1, 0, 4),
364 		BTF_END_RAW,
365 	},
366 	.str_sec = "\0t\0.bss",
367 	.str_sec_size = sizeof("\0t\0.bss"),
368 	.map_type = BPF_MAP_TYPE_ARRAY,
369 	.map_name = ".bss",
370 	.key_size = sizeof(int),
371 	.value_size = 4,
372 	.key_type_id = 0,
373 	.value_type_id = 2,
374 	.max_entries = 1,
375 	.btf_load_err = true,
376 	.err_str = "Invalid type_id",
377 },
378 {
379 	.descr = "global data test #6, invalid var type (fwd type)",
380 	.raw_types = {
381 		/* union A */
382 		BTF_TYPE_ENC(NAME_TBD,
383 			     BTF_INFO_ENC(BTF_KIND_FWD, 1, 0), 0), /* [1] */
384 		/* static union A t */
385 		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
386 		/* .bss section */				/* [3] */
387 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
388 		BTF_VAR_SECINFO_ENC(2, 0, 4),
389 		BTF_END_RAW,
390 	},
391 	.str_sec = "\0A\0t\0.bss",
392 	.str_sec_size = sizeof("\0A\0t\0.bss"),
393 	.map_type = BPF_MAP_TYPE_ARRAY,
394 	.map_name = ".bss",
395 	.key_size = sizeof(int),
396 	.value_size = 4,
397 	.key_type_id = 0,
398 	.value_type_id = 2,
399 	.max_entries = 1,
400 	.btf_load_err = true,
401 	.err_str = "Invalid type",
402 },
403 {
404 	.descr = "global data test #7, invalid var type (fwd type)",
405 	.raw_types = {
406 		/* union A */
407 		BTF_TYPE_ENC(NAME_TBD,
408 			     BTF_INFO_ENC(BTF_KIND_FWD, 1, 0), 0), /* [1] */
409 		/* static union A t */
410 		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
411 		/* .bss section */				/* [3] */
412 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
413 		BTF_VAR_SECINFO_ENC(1, 0, 4),
414 		BTF_END_RAW,
415 	},
416 	.str_sec = "\0A\0t\0.bss",
417 	.str_sec_size = sizeof("\0A\0t\0.bss"),
418 	.map_type = BPF_MAP_TYPE_ARRAY,
419 	.map_name = ".bss",
420 	.key_size = sizeof(int),
421 	.value_size = 4,
422 	.key_type_id = 0,
423 	.value_type_id = 2,
424 	.max_entries = 1,
425 	.btf_load_err = true,
426 	.err_str = "Invalid type",
427 },
428 {
429 	.descr = "global data test #8, invalid var size",
430 	.raw_types = {
431 		/* int */
432 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
433 		/* unsigned long long */
434 		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
435 		/* char */
436 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
437 		/* int[8] */
438 		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
439 		/* struct A { */				/* [5] */
440 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
441 		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
442 		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
443 		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
444 		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
445 		/* } */
446 		/* static struct A t */
447 		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
448 		/* .bss section */				/* [7] */
449 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 48),
450 		BTF_VAR_SECINFO_ENC(6, 0, 47),
451 		BTF_END_RAW,
452 	},
453 	.str_sec = "\0A\0m\0n\0o\0p\0t\0.bss",
454 	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0.bss"),
455 	.map_type = BPF_MAP_TYPE_ARRAY,
456 	.map_name = ".bss",
457 	.key_size = sizeof(int),
458 	.value_size = 48,
459 	.key_type_id = 0,
460 	.value_type_id = 7,
461 	.max_entries = 1,
462 	.btf_load_err = true,
463 	.err_str = "Invalid size",
464 },
465 {
466 	.descr = "global data test #9, invalid var size",
467 	.raw_types = {
468 		/* int */
469 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
470 		/* unsigned long long */
471 		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
472 		/* char */
473 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
474 		/* int[8] */
475 		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
476 		/* struct A { */				/* [5] */
477 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
478 		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
479 		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
480 		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
481 		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
482 		/* } */
483 		/* static struct A t */
484 		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
485 		/* .bss section */				/* [7] */
486 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 46),
487 		BTF_VAR_SECINFO_ENC(6, 0, 48),
488 		BTF_END_RAW,
489 	},
490 	.str_sec = "\0A\0m\0n\0o\0p\0t\0.bss",
491 	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0.bss"),
492 	.map_type = BPF_MAP_TYPE_ARRAY,
493 	.map_name = ".bss",
494 	.key_size = sizeof(int),
495 	.value_size = 48,
496 	.key_type_id = 0,
497 	.value_type_id = 7,
498 	.max_entries = 1,
499 	.btf_load_err = true,
500 	.err_str = "Invalid size",
501 },
502 {
503 	.descr = "global data test #10, invalid var size",
504 	.raw_types = {
505 		/* int */
506 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
507 		/* unsigned long long */
508 		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
509 		/* char */
510 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
511 		/* int[8] */
512 		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
513 		/* struct A { */				/* [5] */
514 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
515 		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
516 		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
517 		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
518 		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
519 		/* } */
520 		/* static struct A t */
521 		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
522 		/* .bss section */				/* [7] */
523 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 46),
524 		BTF_VAR_SECINFO_ENC(6, 0, 46),
525 		BTF_END_RAW,
526 	},
527 	.str_sec = "\0A\0m\0n\0o\0p\0t\0.bss",
528 	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0.bss"),
529 	.map_type = BPF_MAP_TYPE_ARRAY,
530 	.map_name = ".bss",
531 	.key_size = sizeof(int),
532 	.value_size = 48,
533 	.key_type_id = 0,
534 	.value_type_id = 7,
535 	.max_entries = 1,
536 	.btf_load_err = true,
537 	.err_str = "Invalid size",
538 },
539 {
540 	.descr = "global data test #11, multiple section members",
541 	.raw_types = {
542 		/* int */
543 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
544 		/* unsigned long long */
545 		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
546 		/* char */
547 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
548 		/* int[8] */
549 		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
550 		/* struct A { */				/* [5] */
551 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
552 		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
553 		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
554 		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
555 		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
556 		/* } */
557 		/* static struct A t */
558 		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
559 		/* static int u */
560 		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [7] */
561 		/* .bss section */				/* [8] */
562 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 2), 62),
563 		BTF_VAR_SECINFO_ENC(6, 10, 48),
564 		BTF_VAR_SECINFO_ENC(7, 58, 4),
565 		BTF_END_RAW,
566 	},
567 	.str_sec = "\0A\0m\0n\0o\0p\0t\0u\0.bss",
568 	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0u\0.bss"),
569 	.map_type = BPF_MAP_TYPE_ARRAY,
570 	.map_name = ".bss",
571 	.key_size = sizeof(int),
572 	.value_size = 62,
573 	.key_type_id = 0,
574 	.value_type_id = 8,
575 	.max_entries = 1,
576 },
577 {
578 	.descr = "global data test #12, invalid offset",
579 	.raw_types = {
580 		/* int */
581 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
582 		/* unsigned long long */
583 		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
584 		/* char */
585 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
586 		/* int[8] */
587 		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
588 		/* struct A { */				/* [5] */
589 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
590 		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
591 		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
592 		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
593 		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
594 		/* } */
595 		/* static struct A t */
596 		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
597 		/* static int u */
598 		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [7] */
599 		/* .bss section */				/* [8] */
600 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 2), 62),
601 		BTF_VAR_SECINFO_ENC(6, 10, 48),
602 		BTF_VAR_SECINFO_ENC(7, 60, 4),
603 		BTF_END_RAW,
604 	},
605 	.str_sec = "\0A\0m\0n\0o\0p\0t\0u\0.bss",
606 	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0u\0.bss"),
607 	.map_type = BPF_MAP_TYPE_ARRAY,
608 	.map_name = ".bss",
609 	.key_size = sizeof(int),
610 	.value_size = 62,
611 	.key_type_id = 0,
612 	.value_type_id = 8,
613 	.max_entries = 1,
614 	.btf_load_err = true,
615 	.err_str = "Invalid offset+size",
616 },
617 {
618 	.descr = "global data test #13, invalid offset",
619 	.raw_types = {
620 		/* int */
621 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
622 		/* unsigned long long */
623 		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
624 		/* char */
625 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
626 		/* int[8] */
627 		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
628 		/* struct A { */				/* [5] */
629 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
630 		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
631 		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
632 		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
633 		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
634 		/* } */
635 		/* static struct A t */
636 		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
637 		/* static int u */
638 		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [7] */
639 		/* .bss section */				/* [8] */
640 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 2), 62),
641 		BTF_VAR_SECINFO_ENC(6, 10, 48),
642 		BTF_VAR_SECINFO_ENC(7, 12, 4),
643 		BTF_END_RAW,
644 	},
645 	.str_sec = "\0A\0m\0n\0o\0p\0t\0u\0.bss",
646 	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0u\0.bss"),
647 	.map_type = BPF_MAP_TYPE_ARRAY,
648 	.map_name = ".bss",
649 	.key_size = sizeof(int),
650 	.value_size = 62,
651 	.key_type_id = 0,
652 	.value_type_id = 8,
653 	.max_entries = 1,
654 	.btf_load_err = true,
655 	.err_str = "Invalid offset",
656 },
657 {
658 	.descr = "global data test #14, invalid offset",
659 	.raw_types = {
660 		/* int */
661 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
662 		/* unsigned long long */
663 		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
664 		/* char */
665 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
666 		/* int[8] */
667 		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
668 		/* struct A { */				/* [5] */
669 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
670 		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
671 		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
672 		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
673 		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
674 		/* } */
675 		/* static struct A t */
676 		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
677 		/* static int u */
678 		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [7] */
679 		/* .bss section */				/* [8] */
680 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 2), 62),
681 		BTF_VAR_SECINFO_ENC(7, 58, 4),
682 		BTF_VAR_SECINFO_ENC(6, 10, 48),
683 		BTF_END_RAW,
684 	},
685 	.str_sec = "\0A\0m\0n\0o\0p\0t\0u\0.bss",
686 	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0u\0.bss"),
687 	.map_type = BPF_MAP_TYPE_ARRAY,
688 	.map_name = ".bss",
689 	.key_size = sizeof(int),
690 	.value_size = 62,
691 	.key_type_id = 0,
692 	.value_type_id = 8,
693 	.max_entries = 1,
694 	.btf_load_err = true,
695 	.err_str = "Invalid offset",
696 },
697 {
698 	.descr = "global data test #15, not var kind",
699 	.raw_types = {
700 		/* int */
701 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
702 		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
703 		/* .bss section */				/* [3] */
704 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
705 		BTF_VAR_SECINFO_ENC(1, 0, 4),
706 		BTF_END_RAW,
707 	},
708 	.str_sec = "\0A\0t\0.bss",
709 	.str_sec_size = sizeof("\0A\0t\0.bss"),
710 	.map_type = BPF_MAP_TYPE_ARRAY,
711 	.map_name = ".bss",
712 	.key_size = sizeof(int),
713 	.value_size = 4,
714 	.key_type_id = 0,
715 	.value_type_id = 3,
716 	.max_entries = 1,
717 	.btf_load_err = true,
718 	.err_str = "Not a VAR kind member",
719 },
720 {
721 	.descr = "global data test #16, invalid var referencing sec",
722 	.raw_types = {
723 		/* int */
724 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
725 		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [2] */
726 		BTF_VAR_ENC(NAME_TBD, 2, 0),			/* [3] */
727 		/* a section */					/* [4] */
728 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
729 		BTF_VAR_SECINFO_ENC(3, 0, 4),
730 		/* a section */					/* [5] */
731 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
732 		BTF_VAR_SECINFO_ENC(6, 0, 4),
733 		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [6] */
734 		BTF_END_RAW,
735 	},
736 	.str_sec = "\0A\0t\0s\0a\0a",
737 	.str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
738 	.map_type = BPF_MAP_TYPE_ARRAY,
739 	.map_name = ".bss",
740 	.key_size = sizeof(int),
741 	.value_size = 4,
742 	.key_type_id = 0,
743 	.value_type_id = 4,
744 	.max_entries = 1,
745 	.btf_load_err = true,
746 	.err_str = "Invalid type_id",
747 },
748 {
749 	.descr = "global data test #17, invalid var referencing var",
750 	.raw_types = {
751 		/* int */
752 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
753 		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
754 		BTF_VAR_ENC(NAME_TBD, 2, 0),			/* [3] */
755 		/* a section */					/* [4] */
756 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
757 		BTF_VAR_SECINFO_ENC(3, 0, 4),
758 		BTF_END_RAW,
759 	},
760 	.str_sec = "\0A\0t\0s\0a\0a",
761 	.str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
762 	.map_type = BPF_MAP_TYPE_ARRAY,
763 	.map_name = ".bss",
764 	.key_size = sizeof(int),
765 	.value_size = 4,
766 	.key_type_id = 0,
767 	.value_type_id = 4,
768 	.max_entries = 1,
769 	.btf_load_err = true,
770 	.err_str = "Invalid type_id",
771 },
772 {
773 	.descr = "global data test #18, invalid var loop",
774 	.raw_types = {
775 		/* int */
776 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
777 		BTF_VAR_ENC(NAME_TBD, 2, 0),			/* [2] */
778 		/* .bss section */				/* [3] */
779 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
780 		BTF_VAR_SECINFO_ENC(2, 0, 4),
781 		BTF_END_RAW,
782 	},
783 	.str_sec = "\0A\0t\0aaa",
784 	.str_sec_size = sizeof("\0A\0t\0aaa"),
785 	.map_type = BPF_MAP_TYPE_ARRAY,
786 	.map_name = ".bss",
787 	.key_size = sizeof(int),
788 	.value_size = 4,
789 	.key_type_id = 0,
790 	.value_type_id = 4,
791 	.max_entries = 1,
792 	.btf_load_err = true,
793 	.err_str = "Invalid type_id",
794 },
795 {
796 	.descr = "global data test #19, invalid var referencing var",
797 	.raw_types = {
798 		/* int */
799 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
800 		BTF_VAR_ENC(NAME_TBD, 3, 0),			/* [2] */
801 		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [3] */
802 		BTF_END_RAW,
803 	},
804 	.str_sec = "\0A\0t\0s\0a\0a",
805 	.str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
806 	.map_type = BPF_MAP_TYPE_ARRAY,
807 	.map_name = ".bss",
808 	.key_size = sizeof(int),
809 	.value_size = 4,
810 	.key_type_id = 0,
811 	.value_type_id = 4,
812 	.max_entries = 1,
813 	.btf_load_err = true,
814 	.err_str = "Invalid type_id",
815 },
816 {
817 	.descr = "global data test #20, invalid ptr referencing var",
818 	.raw_types = {
819 		/* int */
820 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
821 		/* PTR type_id=3	*/			/* [2] */
822 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 3),
823 		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [3] */
824 		BTF_END_RAW,
825 	},
826 	.str_sec = "\0A\0t\0s\0a\0a",
827 	.str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
828 	.map_type = BPF_MAP_TYPE_ARRAY,
829 	.map_name = ".bss",
830 	.key_size = sizeof(int),
831 	.value_size = 4,
832 	.key_type_id = 0,
833 	.value_type_id = 4,
834 	.max_entries = 1,
835 	.btf_load_err = true,
836 	.err_str = "Invalid type_id",
837 },
838 {
839 	.descr = "global data test #21, var included in struct",
840 	.raw_types = {
841 		/* int */
842 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
843 		/* struct A { */				/* [2] */
844 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 2),
845 		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int m; */
846 		BTF_MEMBER_ENC(NAME_TBD, 3, 32),/* VAR type_id=3; */
847 		/* } */
848 		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [3] */
849 		BTF_END_RAW,
850 	},
851 	.str_sec = "\0A\0t\0s\0a\0a",
852 	.str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
853 	.map_type = BPF_MAP_TYPE_ARRAY,
854 	.map_name = ".bss",
855 	.key_size = sizeof(int),
856 	.value_size = 4,
857 	.key_type_id = 0,
858 	.value_type_id = 4,
859 	.max_entries = 1,
860 	.btf_load_err = true,
861 	.err_str = "Invalid member",
862 },
863 {
864 	.descr = "global data test #22, array of var",
865 	.raw_types = {
866 		/* int */
867 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
868 		BTF_TYPE_ARRAY_ENC(3, 1, 4),			/* [2] */
869 		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [3] */
870 		BTF_END_RAW,
871 	},
872 	.str_sec = "\0A\0t\0s\0a\0a",
873 	.str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
874 	.map_type = BPF_MAP_TYPE_ARRAY,
875 	.map_name = ".bss",
876 	.key_size = sizeof(int),
877 	.value_size = 4,
878 	.key_type_id = 0,
879 	.value_type_id = 4,
880 	.max_entries = 1,
881 	.btf_load_err = true,
882 	.err_str = "Invalid elem",
883 },
884 /* Test member exceeds the size of struct.
885  *
886  * struct A {
887  *     int m;
888  *     int n;
889  * };
890  */
891 {
892 	.descr = "size check test #1",
893 	.raw_types = {
894 		/* int */					/* [1] */
895 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
896 		/* struct A { */				/* [2] */
897 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 2 -  1),
898 		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int m; */
899 		BTF_MEMBER_ENC(NAME_TBD, 1, 32),/* int n; */
900 		/* } */
901 		BTF_END_RAW,
902 	},
903 	.str_sec = "\0A\0m\0n",
904 	.str_sec_size = sizeof("\0A\0m\0n"),
905 	.map_type = BPF_MAP_TYPE_ARRAY,
906 	.map_name = "size_check1_map",
907 	.key_size = sizeof(int),
908 	.value_size = 1,
909 	.key_type_id = 1,
910 	.value_type_id = 2,
911 	.max_entries = 4,
912 	.btf_load_err = true,
913 	.err_str = "Member exceeds struct_size",
914 },
915 
916 /* Test member exceeds the size of struct
917  *
918  * struct A {
919  *     int m;
920  *     int n[2];
921  * };
922  */
923 {
924 	.descr = "size check test #2",
925 	.raw_types = {
926 		/* int */					/* [1] */
927 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
928 		/* int[2] */					/* [2] */
929 		BTF_TYPE_ARRAY_ENC(1, 1, 2),
930 		/* struct A { */				/* [3] */
931 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 3 - 1),
932 		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int m; */
933 		BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* int n[2]; */
934 		/* } */
935 		BTF_END_RAW,
936 	},
937 	.str_sec = "\0A\0m\0n",
938 	.str_sec_size = sizeof("\0A\0m\0n"),
939 	.map_type = BPF_MAP_TYPE_ARRAY,
940 	.map_name = "size_check2_map",
941 	.key_size = sizeof(int),
942 	.value_size = 1,
943 	.key_type_id = 1,
944 	.value_type_id = 3,
945 	.max_entries = 4,
946 	.btf_load_err = true,
947 	.err_str = "Member exceeds struct_size",
948 },
949 
950 /* Test member exceeds the size of struct
951  *
952  * struct A {
953  *     int m;
954  *     void *n;
955  * };
956  */
957 {
958 	.descr = "size check test #3",
959 	.raw_types = {
960 		/* int */					/* [1] */
961 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
962 		/* void* */					/* [2] */
963 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
964 		/* struct A { */				/* [3] */
965 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) + sizeof(void *) - 1),
966 		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int m; */
967 		BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* void *n; */
968 		/* } */
969 		BTF_END_RAW,
970 	},
971 	.str_sec = "\0A\0m\0n",
972 	.str_sec_size = sizeof("\0A\0m\0n"),
973 	.map_type = BPF_MAP_TYPE_ARRAY,
974 	.map_name = "size_check3_map",
975 	.key_size = sizeof(int),
976 	.value_size = 1,
977 	.key_type_id = 1,
978 	.value_type_id = 3,
979 	.max_entries = 4,
980 	.btf_load_err = true,
981 	.err_str = "Member exceeds struct_size",
982 },
983 
984 /* Test member exceeds the size of struct
985  *
986  * enum E {
987  *     E0,
988  *     E1,
989  * };
990  *
991  * struct A {
992  *     int m;
993  *     enum E n;
994  * };
995  */
996 {
997 	.descr = "size check test #4",
998 	.raw_types = {
999 		/* int */			/* [1] */
1000 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
1001 		/* enum E { */			/* [2] */
1002 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), sizeof(int)),
1003 		BTF_ENUM_ENC(NAME_TBD, 0),
1004 		BTF_ENUM_ENC(NAME_TBD, 1),
1005 		/* } */
1006 		/* struct A { */		/* [3] */
1007 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 2 - 1),
1008 		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int m; */
1009 		BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* enum E n; */
1010 		/* } */
1011 		BTF_END_RAW,
1012 	},
1013 	.str_sec = "\0E\0E0\0E1\0A\0m\0n",
1014 	.str_sec_size = sizeof("\0E\0E0\0E1\0A\0m\0n"),
1015 	.map_type = BPF_MAP_TYPE_ARRAY,
1016 	.map_name = "size_check4_map",
1017 	.key_size = sizeof(int),
1018 	.value_size = 1,
1019 	.key_type_id = 1,
1020 	.value_type_id = 3,
1021 	.max_entries = 4,
1022 	.btf_load_err = true,
1023 	.err_str = "Member exceeds struct_size",
1024 },
1025 
1026 /* Test member unexceeds the size of struct
1027  *
1028  * enum E {
1029  *     E0,
1030  *     E1,
1031  * };
1032  *
1033  * struct A {
1034  *     char m;
1035  *     enum E __attribute__((packed)) n;
1036  * };
1037  */
1038 {
1039 	.descr = "size check test #5",
1040 	.raw_types = {
1041 		/* int */			/* [1] */
1042 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
1043 		/* char */			/* [2] */
1044 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),
1045 		/* enum E { */			/* [3] */
1046 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), 1),
1047 		BTF_ENUM_ENC(NAME_TBD, 0),
1048 		BTF_ENUM_ENC(NAME_TBD, 1),
1049 		/* } */
1050 		/* struct A { */		/* [4] */
1051 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 2),
1052 		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* char m; */
1053 		BTF_MEMBER_ENC(NAME_TBD, 3, 8),/* enum E __attribute__((packed)) n; */
1054 		/* } */
1055 		BTF_END_RAW,
1056 	},
1057 	.str_sec = "\0E\0E0\0E1\0A\0m\0n",
1058 	.str_sec_size = sizeof("\0E\0E0\0E1\0A\0m\0n"),
1059 	.map_type = BPF_MAP_TYPE_ARRAY,
1060 	.map_name = "size_check5_map",
1061 	.key_size = sizeof(int),
1062 	.value_size = 2,
1063 	.key_type_id = 1,
1064 	.value_type_id = 4,
1065 	.max_entries = 4,
1066 },
1067 
1068 /* typedef const void * const_void_ptr;
1069  * struct A {
1070  *	const_void_ptr m;
1071  * };
1072  */
1073 {
1074 	.descr = "void test #1",
1075 	.raw_types = {
1076 		/* int */		/* [1] */
1077 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1078 		/* const void */	/* [2] */
1079 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1080 		/* const void* */	/* [3] */
1081 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 2),
1082 		/* typedef const void * const_void_ptr */
1083 		BTF_TYPEDEF_ENC(NAME_TBD, 3),	/* [4] */
1084 		/* struct A { */	/* [5] */
1085 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
1086 		/* const_void_ptr m; */
1087 		BTF_MEMBER_ENC(NAME_TBD, 4, 0),
1088 		/* } */
1089 		BTF_END_RAW,
1090 	},
1091 	.str_sec = "\0const_void_ptr\0A\0m",
1092 	.str_sec_size = sizeof("\0const_void_ptr\0A\0m"),
1093 	.map_type = BPF_MAP_TYPE_ARRAY,
1094 	.map_name = "void_test1_map",
1095 	.key_size = sizeof(int),
1096 	.value_size = sizeof(void *),
1097 	.key_type_id = 1,
1098 	.value_type_id = 4,
1099 	.max_entries = 4,
1100 },
1101 
1102 /* struct A {
1103  *     const void m;
1104  * };
1105  */
1106 {
1107 	.descr = "void test #2",
1108 	.raw_types = {
1109 		/* int */		/* [1] */
1110 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1111 		/* const void */	/* [2] */
1112 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1113 		/* struct A { */	/* [3] */
1114 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 8),
1115 		/* const void m; */
1116 		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
1117 		/* } */
1118 		BTF_END_RAW,
1119 	},
1120 	.str_sec = "\0A\0m",
1121 	.str_sec_size = sizeof("\0A\0m"),
1122 	.map_type = BPF_MAP_TYPE_ARRAY,
1123 	.map_name = "void_test2_map",
1124 	.key_size = sizeof(int),
1125 	.value_size = sizeof(void *),
1126 	.key_type_id = 1,
1127 	.value_type_id = 3,
1128 	.max_entries = 4,
1129 	.btf_load_err = true,
1130 	.err_str = "Invalid member",
1131 },
1132 
1133 /* typedef const void * const_void_ptr;
1134  * const_void_ptr[4]
1135  */
1136 {
1137 	.descr = "void test #3",
1138 	.raw_types = {
1139 		/* int */		/* [1] */
1140 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1141 		/* const void */	/* [2] */
1142 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1143 		/* const void* */	/* [3] */
1144 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 2),
1145 		/* typedef const void * const_void_ptr */
1146 		BTF_TYPEDEF_ENC(NAME_TBD, 3),	/* [4] */
1147 		/* const_void_ptr[4] */
1148 		BTF_TYPE_ARRAY_ENC(4, 1, 4),	/* [5] */
1149 		BTF_END_RAW,
1150 	},
1151 	.str_sec = "\0const_void_ptr",
1152 	.str_sec_size = sizeof("\0const_void_ptr"),
1153 	.map_type = BPF_MAP_TYPE_ARRAY,
1154 	.map_name = "void_test3_map",
1155 	.key_size = sizeof(int),
1156 	.value_size = sizeof(void *) * 4,
1157 	.key_type_id = 1,
1158 	.value_type_id = 5,
1159 	.max_entries = 4,
1160 },
1161 
1162 /* const void[4]  */
1163 {
1164 	.descr = "void test #4",
1165 	.raw_types = {
1166 		/* int */		/* [1] */
1167 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1168 		/* const void */	/* [2] */
1169 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1170 		/* const void[4] */	/* [3] */
1171 		BTF_TYPE_ARRAY_ENC(2, 1, 4),
1172 		BTF_END_RAW,
1173 	},
1174 	.str_sec = "\0A\0m",
1175 	.str_sec_size = sizeof("\0A\0m"),
1176 	.map_type = BPF_MAP_TYPE_ARRAY,
1177 	.map_name = "void_test4_map",
1178 	.key_size = sizeof(int),
1179 	.value_size = sizeof(void *) * 4,
1180 	.key_type_id = 1,
1181 	.value_type_id = 3,
1182 	.max_entries = 4,
1183 	.btf_load_err = true,
1184 	.err_str = "Invalid elem",
1185 },
1186 
1187 /* Array_A  <------------------+
1188  *     elem_type == Array_B    |
1189  *                    |        |
1190  *                    |        |
1191  * Array_B  <-------- +        |
1192  *      elem_type == Array A --+
1193  */
1194 {
1195 	.descr = "loop test #1",
1196 	.raw_types = {
1197 		/* int */			/* [1] */
1198 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1199 		/* Array_A */			/* [2] */
1200 		BTF_TYPE_ARRAY_ENC(3, 1, 8),
1201 		/* Array_B */			/* [3] */
1202 		BTF_TYPE_ARRAY_ENC(2, 1, 8),
1203 		BTF_END_RAW,
1204 	},
1205 	.str_sec = "",
1206 	.str_sec_size = sizeof(""),
1207 	.map_type = BPF_MAP_TYPE_ARRAY,
1208 	.map_name = "loop_test1_map",
1209 	.key_size = sizeof(int),
1210 	.value_size = sizeof(sizeof(int) * 8),
1211 	.key_type_id = 1,
1212 	.value_type_id = 2,
1213 	.max_entries = 4,
1214 	.btf_load_err = true,
1215 	.err_str = "Loop detected",
1216 },
1217 
1218 /* typedef is _before_ the BTF type of Array_A and Array_B
1219  *
1220  * typedef Array_B int_array;
1221  *
1222  * Array_A  <------------------+
1223  *     elem_type == int_array  |
1224  *                    |        |
1225  *                    |        |
1226  * Array_B  <-------- +        |
1227  *      elem_type == Array_A --+
1228  */
1229 {
1230 	.descr = "loop test #2",
1231 	.raw_types = {
1232 		/* int */
1233 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
1234 		/* typedef Array_B int_array */
1235 		BTF_TYPEDEF_ENC(1, 4),				/* [2] */
1236 		/* Array_A */
1237 		BTF_TYPE_ARRAY_ENC(2, 1, 8),			/* [3] */
1238 		/* Array_B */
1239 		BTF_TYPE_ARRAY_ENC(3, 1, 8),			/* [4] */
1240 		BTF_END_RAW,
1241 	},
1242 	.str_sec = "\0int_array\0",
1243 	.str_sec_size = sizeof("\0int_array"),
1244 	.map_type = BPF_MAP_TYPE_ARRAY,
1245 	.map_name = "loop_test2_map",
1246 	.key_size = sizeof(int),
1247 	.value_size = sizeof(sizeof(int) * 8),
1248 	.key_type_id = 1,
1249 	.value_type_id = 2,
1250 	.max_entries = 4,
1251 	.btf_load_err = true,
1252 	.err_str = "Loop detected",
1253 },
1254 
1255 /* Array_A  <------------------+
1256  *     elem_type == Array_B    |
1257  *                    |        |
1258  *                    |        |
1259  * Array_B  <-------- +        |
1260  *      elem_type == Array_A --+
1261  */
1262 {
1263 	.descr = "loop test #3",
1264 	.raw_types = {
1265 		/* int */				/* [1] */
1266 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1267 		/* Array_A */				/* [2] */
1268 		BTF_TYPE_ARRAY_ENC(3, 1, 8),
1269 		/* Array_B */				/* [3] */
1270 		BTF_TYPE_ARRAY_ENC(2, 1, 8),
1271 		BTF_END_RAW,
1272 	},
1273 	.str_sec = "",
1274 	.str_sec_size = sizeof(""),
1275 	.map_type = BPF_MAP_TYPE_ARRAY,
1276 	.map_name = "loop_test3_map",
1277 	.key_size = sizeof(int),
1278 	.value_size = sizeof(sizeof(int) * 8),
1279 	.key_type_id = 1,
1280 	.value_type_id = 2,
1281 	.max_entries = 4,
1282 	.btf_load_err = true,
1283 	.err_str = "Loop detected",
1284 },
1285 
1286 /* typedef is _between_ the BTF type of Array_A and Array_B
1287  *
1288  * typedef Array_B int_array;
1289  *
1290  * Array_A  <------------------+
1291  *     elem_type == int_array  |
1292  *                    |        |
1293  *                    |        |
1294  * Array_B  <-------- +        |
1295  *      elem_type == Array_A --+
1296  */
1297 {
1298 	.descr = "loop test #4",
1299 	.raw_types = {
1300 		/* int */				/* [1] */
1301 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1302 		/* Array_A */				/* [2] */
1303 		BTF_TYPE_ARRAY_ENC(3, 1, 8),
1304 		/* typedef Array_B int_array */		/* [3] */
1305 		BTF_TYPEDEF_ENC(NAME_TBD, 4),
1306 		/* Array_B */				/* [4] */
1307 		BTF_TYPE_ARRAY_ENC(2, 1, 8),
1308 		BTF_END_RAW,
1309 	},
1310 	.str_sec = "\0int_array\0",
1311 	.str_sec_size = sizeof("\0int_array"),
1312 	.map_type = BPF_MAP_TYPE_ARRAY,
1313 	.map_name = "loop_test4_map",
1314 	.key_size = sizeof(int),
1315 	.value_size = sizeof(sizeof(int) * 8),
1316 	.key_type_id = 1,
1317 	.value_type_id = 2,
1318 	.max_entries = 4,
1319 	.btf_load_err = true,
1320 	.err_str = "Loop detected",
1321 },
1322 
1323 /* typedef struct B Struct_B
1324  *
1325  * struct A {
1326  *     int x;
1327  *     Struct_B y;
1328  * };
1329  *
1330  * struct B {
1331  *     int x;
1332  *     struct A y;
1333  * };
1334  */
1335 {
1336 	.descr = "loop test #5",
1337 	.raw_types = {
1338 		/* int */
1339 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
1340 		/* struct A */					/* [2] */
1341 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
1342 		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int x;	*/
1343 		BTF_MEMBER_ENC(NAME_TBD, 3, 32),/* Struct_B y;	*/
1344 		/* typedef struct B Struct_B */
1345 		BTF_TYPEDEF_ENC(NAME_TBD, 4),			/* [3] */
1346 		/* struct B */					/* [4] */
1347 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
1348 		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int x;	*/
1349 		BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* struct A y;	*/
1350 		BTF_END_RAW,
1351 	},
1352 	.str_sec = "\0A\0x\0y\0Struct_B\0B\0x\0y",
1353 	.str_sec_size = sizeof("\0A\0x\0y\0Struct_B\0B\0x\0y"),
1354 	.map_type = BPF_MAP_TYPE_ARRAY,
1355 	.map_name = "loop_test5_map",
1356 	.key_size = sizeof(int),
1357 	.value_size = 8,
1358 	.key_type_id = 1,
1359 	.value_type_id = 2,
1360 	.max_entries = 4,
1361 	.btf_load_err = true,
1362 	.err_str = "Loop detected",
1363 },
1364 
1365 /* struct A {
1366  *     int x;
1367  *     struct A array_a[4];
1368  * };
1369  */
1370 {
1371 	.descr = "loop test #6",
1372 	.raw_types = {
1373 		/* int */
1374 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
1375 		BTF_TYPE_ARRAY_ENC(3, 1, 4),			/* [2] */
1376 		/* struct A */					/* [3] */
1377 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
1378 		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int x;		*/
1379 		BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* struct A array_a[4];	*/
1380 		BTF_END_RAW,
1381 	},
1382 	.str_sec = "\0A\0x\0y",
1383 	.str_sec_size = sizeof("\0A\0x\0y"),
1384 	.map_type = BPF_MAP_TYPE_ARRAY,
1385 	.map_name = "loop_test6_map",
1386 	.key_size = sizeof(int),
1387 	.value_size = 8,
1388 	.key_type_id = 1,
1389 	.value_type_id = 2,
1390 	.max_entries = 4,
1391 	.btf_load_err = true,
1392 	.err_str = "Loop detected",
1393 },
1394 
1395 {
1396 	.descr = "loop test #7",
1397 	.raw_types = {
1398 		/* int */				/* [1] */
1399 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1400 		/* struct A { */			/* [2] */
1401 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
1402 		/*     const void *m;	*/
1403 		BTF_MEMBER_ENC(NAME_TBD, 3, 0),
1404 		/* CONST type_id=3	*/		/* [3] */
1405 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 4),
1406 		/* PTR type_id=2	*/		/* [4] */
1407 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 3),
1408 		BTF_END_RAW,
1409 	},
1410 	.str_sec = "\0A\0m",
1411 	.str_sec_size = sizeof("\0A\0m"),
1412 	.map_type = BPF_MAP_TYPE_ARRAY,
1413 	.map_name = "loop_test7_map",
1414 	.key_size = sizeof(int),
1415 	.value_size = sizeof(void *),
1416 	.key_type_id = 1,
1417 	.value_type_id = 2,
1418 	.max_entries = 4,
1419 	.btf_load_err = true,
1420 	.err_str = "Loop detected",
1421 },
1422 
1423 {
1424 	.descr = "loop test #8",
1425 	.raw_types = {
1426 		/* int */				/* [1] */
1427 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1428 		/* struct A { */			/* [2] */
1429 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
1430 		/*     const void *m;	*/
1431 		BTF_MEMBER_ENC(NAME_TBD, 4, 0),
1432 		/* struct B { */			/* [3] */
1433 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
1434 		/*     const void *n;	*/
1435 		BTF_MEMBER_ENC(NAME_TBD, 6, 0),
1436 		/* CONST type_id=5	*/		/* [4] */
1437 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 5),
1438 		/* PTR type_id=6	*/		/* [5] */
1439 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 6),
1440 		/* CONST type_id=7	*/		/* [6] */
1441 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 7),
1442 		/* PTR type_id=4	*/		/* [7] */
1443 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 4),
1444 		BTF_END_RAW,
1445 	},
1446 	.str_sec = "\0A\0m\0B\0n",
1447 	.str_sec_size = sizeof("\0A\0m\0B\0n"),
1448 	.map_type = BPF_MAP_TYPE_ARRAY,
1449 	.map_name = "loop_test8_map",
1450 	.key_size = sizeof(int),
1451 	.value_size = sizeof(void *),
1452 	.key_type_id = 1,
1453 	.value_type_id = 2,
1454 	.max_entries = 4,
1455 	.btf_load_err = true,
1456 	.err_str = "Loop detected",
1457 },
1458 
1459 {
1460 	.descr = "string section does not end with null",
1461 	.raw_types = {
1462 		/* int */				/* [1] */
1463 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1464 		BTF_END_RAW,
1465 	},
1466 	.str_sec = "\0int",
1467 	.str_sec_size = sizeof("\0int") - 1,
1468 	.map_type = BPF_MAP_TYPE_ARRAY,
1469 	.map_name = "hdr_test_map",
1470 	.key_size = sizeof(int),
1471 	.value_size = sizeof(int),
1472 	.key_type_id = 1,
1473 	.value_type_id = 1,
1474 	.max_entries = 4,
1475 	.btf_load_err = true,
1476 	.err_str = "Invalid string section",
1477 },
1478 
1479 {
1480 	.descr = "empty string section",
1481 	.raw_types = {
1482 		/* int */				/* [1] */
1483 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1484 		BTF_END_RAW,
1485 	},
1486 	.str_sec = "",
1487 	.str_sec_size = 0,
1488 	.map_type = BPF_MAP_TYPE_ARRAY,
1489 	.map_name = "hdr_test_map",
1490 	.key_size = sizeof(int),
1491 	.value_size = sizeof(int),
1492 	.key_type_id = 1,
1493 	.value_type_id = 1,
1494 	.max_entries = 4,
1495 	.btf_load_err = true,
1496 	.err_str = "Invalid string section",
1497 },
1498 
1499 {
1500 	.descr = "empty type section",
1501 	.raw_types = {
1502 		BTF_END_RAW,
1503 	},
1504 	.str_sec = "\0int",
1505 	.str_sec_size = sizeof("\0int"),
1506 	.map_type = BPF_MAP_TYPE_ARRAY,
1507 	.map_name = "hdr_test_map",
1508 	.key_size = sizeof(int),
1509 	.value_size = sizeof(int),
1510 	.key_type_id = 1,
1511 	.value_type_id = 1,
1512 	.max_entries = 4,
1513 	.btf_load_err = true,
1514 	.err_str = "No type found",
1515 },
1516 
1517 {
1518 	.descr = "btf_header test. Longer hdr_len",
1519 	.raw_types = {
1520 		/* int */				/* [1] */
1521 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1522 		BTF_END_RAW,
1523 	},
1524 	.str_sec = "\0int",
1525 	.str_sec_size = sizeof("\0int"),
1526 	.map_type = BPF_MAP_TYPE_ARRAY,
1527 	.map_name = "hdr_test_map",
1528 	.key_size = sizeof(int),
1529 	.value_size = sizeof(int),
1530 	.key_type_id = 1,
1531 	.value_type_id = 1,
1532 	.max_entries = 4,
1533 	.btf_load_err = true,
1534 	.hdr_len_delta = 4,
1535 	.err_str = "Unsupported btf_header",
1536 },
1537 
1538 {
1539 	.descr = "btf_header test. Gap between hdr and type",
1540 	.raw_types = {
1541 		/* int */				/* [1] */
1542 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1543 		BTF_END_RAW,
1544 	},
1545 	.str_sec = "\0int",
1546 	.str_sec_size = sizeof("\0int"),
1547 	.map_type = BPF_MAP_TYPE_ARRAY,
1548 	.map_name = "hdr_test_map",
1549 	.key_size = sizeof(int),
1550 	.value_size = sizeof(int),
1551 	.key_type_id = 1,
1552 	.value_type_id = 1,
1553 	.max_entries = 4,
1554 	.btf_load_err = true,
1555 	.type_off_delta = 4,
1556 	.err_str = "Unsupported section found",
1557 },
1558 
1559 {
1560 	.descr = "btf_header test. Gap between type and str",
1561 	.raw_types = {
1562 		/* int */				/* [1] */
1563 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1564 		BTF_END_RAW,
1565 	},
1566 	.str_sec = "\0int",
1567 	.str_sec_size = sizeof("\0int"),
1568 	.map_type = BPF_MAP_TYPE_ARRAY,
1569 	.map_name = "hdr_test_map",
1570 	.key_size = sizeof(int),
1571 	.value_size = sizeof(int),
1572 	.key_type_id = 1,
1573 	.value_type_id = 1,
1574 	.max_entries = 4,
1575 	.btf_load_err = true,
1576 	.str_off_delta = 4,
1577 	.err_str = "Unsupported section found",
1578 },
1579 
1580 {
1581 	.descr = "btf_header test. Overlap between type and str",
1582 	.raw_types = {
1583 		/* int */				/* [1] */
1584 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1585 		BTF_END_RAW,
1586 	},
1587 	.str_sec = "\0int",
1588 	.str_sec_size = sizeof("\0int"),
1589 	.map_type = BPF_MAP_TYPE_ARRAY,
1590 	.map_name = "hdr_test_map",
1591 	.key_size = sizeof(int),
1592 	.value_size = sizeof(int),
1593 	.key_type_id = 1,
1594 	.value_type_id = 1,
1595 	.max_entries = 4,
1596 	.btf_load_err = true,
1597 	.str_off_delta = -4,
1598 	.err_str = "Section overlap found",
1599 },
1600 
1601 {
1602 	.descr = "btf_header test. Larger BTF size",
1603 	.raw_types = {
1604 		/* int */				/* [1] */
1605 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1606 		BTF_END_RAW,
1607 	},
1608 	.str_sec = "\0int",
1609 	.str_sec_size = sizeof("\0int"),
1610 	.map_type = BPF_MAP_TYPE_ARRAY,
1611 	.map_name = "hdr_test_map",
1612 	.key_size = sizeof(int),
1613 	.value_size = sizeof(int),
1614 	.key_type_id = 1,
1615 	.value_type_id = 1,
1616 	.max_entries = 4,
1617 	.btf_load_err = true,
1618 	.str_len_delta = -4,
1619 	.err_str = "Unsupported section found",
1620 },
1621 
1622 {
1623 	.descr = "btf_header test. Smaller BTF size",
1624 	.raw_types = {
1625 		/* int */				/* [1] */
1626 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1627 		BTF_END_RAW,
1628 	},
1629 	.str_sec = "\0int",
1630 	.str_sec_size = sizeof("\0int"),
1631 	.map_type = BPF_MAP_TYPE_ARRAY,
1632 	.map_name = "hdr_test_map",
1633 	.key_size = sizeof(int),
1634 	.value_size = sizeof(int),
1635 	.key_type_id = 1,
1636 	.value_type_id = 1,
1637 	.max_entries = 4,
1638 	.btf_load_err = true,
1639 	.str_len_delta = 4,
1640 	.err_str = "Total section length too long",
1641 },
1642 
1643 {
1644 	.descr = "array test. index_type/elem_type \"int\"",
1645 	.raw_types = {
1646 		/* int */				/* [1] */
1647 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1648 		/* int[16] */				/* [2] */
1649 		BTF_TYPE_ARRAY_ENC(1, 1, 16),
1650 		BTF_END_RAW,
1651 	},
1652 	.str_sec = "",
1653 	.str_sec_size = sizeof(""),
1654 	.map_type = BPF_MAP_TYPE_ARRAY,
1655 	.map_name = "array_test_map",
1656 	.key_size = sizeof(int),
1657 	.value_size = sizeof(int),
1658 	.key_type_id = 1,
1659 	.value_type_id = 1,
1660 	.max_entries = 4,
1661 },
1662 
1663 {
1664 	.descr = "array test. index_type/elem_type \"const int\"",
1665 	.raw_types = {
1666 		/* int */				/* [1] */
1667 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1668 		/* int[16] */				/* [2] */
1669 		BTF_TYPE_ARRAY_ENC(3, 3, 16),
1670 		/* CONST type_id=1 */			/* [3] */
1671 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 1),
1672 		BTF_END_RAW,
1673 	},
1674 	.str_sec = "",
1675 	.str_sec_size = sizeof(""),
1676 	.map_type = BPF_MAP_TYPE_ARRAY,
1677 	.map_name = "array_test_map",
1678 	.key_size = sizeof(int),
1679 	.value_size = sizeof(int),
1680 	.key_type_id = 1,
1681 	.value_type_id = 1,
1682 	.max_entries = 4,
1683 },
1684 
1685 {
1686 	.descr = "array test. index_type \"const int:31\"",
1687 	.raw_types = {
1688 		/* int */				/* [1] */
1689 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1690 		/* int:31 */				/* [2] */
1691 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 31, 4),
1692 		/* int[16] */				/* [3] */
1693 		BTF_TYPE_ARRAY_ENC(1, 4, 16),
1694 		/* CONST type_id=2 */			/* [4] */
1695 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 2),
1696 		BTF_END_RAW,
1697 	},
1698 	.str_sec = "",
1699 	.str_sec_size = sizeof(""),
1700 	.map_type = BPF_MAP_TYPE_ARRAY,
1701 	.map_name = "array_test_map",
1702 	.key_size = sizeof(int),
1703 	.value_size = sizeof(int),
1704 	.key_type_id = 1,
1705 	.value_type_id = 1,
1706 	.max_entries = 4,
1707 	.btf_load_err = true,
1708 	.err_str = "Invalid index",
1709 },
1710 
1711 {
1712 	.descr = "array test. elem_type \"const int:31\"",
1713 	.raw_types = {
1714 		/* int */				/* [1] */
1715 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1716 		/* int:31 */				/* [2] */
1717 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 31, 4),
1718 		/* int[16] */				/* [3] */
1719 		BTF_TYPE_ARRAY_ENC(4, 1, 16),
1720 		/* CONST type_id=2 */			/* [4] */
1721 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 2),
1722 		BTF_END_RAW,
1723 	},
1724 	.str_sec = "",
1725 	.str_sec_size = sizeof(""),
1726 	.map_type = BPF_MAP_TYPE_ARRAY,
1727 	.map_name = "array_test_map",
1728 	.key_size = sizeof(int),
1729 	.value_size = sizeof(int),
1730 	.key_type_id = 1,
1731 	.value_type_id = 1,
1732 	.max_entries = 4,
1733 	.btf_load_err = true,
1734 	.err_str = "Invalid array of int",
1735 },
1736 
1737 {
1738 	.descr = "array test. index_type \"void\"",
1739 	.raw_types = {
1740 		/* int */				/* [1] */
1741 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1742 		/* int[16] */				/* [2] */
1743 		BTF_TYPE_ARRAY_ENC(1, 0, 16),
1744 		BTF_END_RAW,
1745 	},
1746 	.str_sec = "",
1747 	.str_sec_size = sizeof(""),
1748 	.map_type = BPF_MAP_TYPE_ARRAY,
1749 	.map_name = "array_test_map",
1750 	.key_size = sizeof(int),
1751 	.value_size = sizeof(int),
1752 	.key_type_id = 1,
1753 	.value_type_id = 1,
1754 	.max_entries = 4,
1755 	.btf_load_err = true,
1756 	.err_str = "Invalid index",
1757 },
1758 
1759 {
1760 	.descr = "array test. index_type \"const void\"",
1761 	.raw_types = {
1762 		/* int */				/* [1] */
1763 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1764 		/* int[16] */				/* [2] */
1765 		BTF_TYPE_ARRAY_ENC(1, 3, 16),
1766 		/* CONST type_id=0 (void) */		/* [3] */
1767 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1768 		BTF_END_RAW,
1769 	},
1770 	.str_sec = "",
1771 	.str_sec_size = sizeof(""),
1772 	.map_type = BPF_MAP_TYPE_ARRAY,
1773 	.map_name = "array_test_map",
1774 	.key_size = sizeof(int),
1775 	.value_size = sizeof(int),
1776 	.key_type_id = 1,
1777 	.value_type_id = 1,
1778 	.max_entries = 4,
1779 	.btf_load_err = true,
1780 	.err_str = "Invalid index",
1781 },
1782 
1783 {
1784 	.descr = "array test. elem_type \"const void\"",
1785 	.raw_types = {
1786 		/* int */				/* [1] */
1787 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1788 		/* int[16] */				/* [2] */
1789 		BTF_TYPE_ARRAY_ENC(3, 1, 16),
1790 		/* CONST type_id=0 (void) */		/* [3] */
1791 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1792 		BTF_END_RAW,
1793 	},
1794 	.str_sec = "",
1795 	.str_sec_size = sizeof(""),
1796 	.map_type = BPF_MAP_TYPE_ARRAY,
1797 	.map_name = "array_test_map",
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 	.btf_load_err = true,
1804 	.err_str = "Invalid elem",
1805 },
1806 
1807 {
1808 	.descr = "array test. elem_type \"const void *\"",
1809 	.raw_types = {
1810 		/* int */				/* [1] */
1811 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1812 		/* const void *[16] */			/* [2] */
1813 		BTF_TYPE_ARRAY_ENC(3, 1, 16),
1814 		/* CONST type_id=4 */			/* [3] */
1815 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 4),
1816 		/* void* */				/* [4] */
1817 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
1818 		BTF_END_RAW,
1819 	},
1820 	.str_sec = "",
1821 	.str_sec_size = sizeof(""),
1822 	.map_type = BPF_MAP_TYPE_ARRAY,
1823 	.map_name = "array_test_map",
1824 	.key_size = sizeof(int),
1825 	.value_size = sizeof(int),
1826 	.key_type_id = 1,
1827 	.value_type_id = 1,
1828 	.max_entries = 4,
1829 },
1830 
1831 {
1832 	.descr = "array test. index_type \"const void *\"",
1833 	.raw_types = {
1834 		/* int */				/* [1] */
1835 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1836 		/* const void *[16] */			/* [2] */
1837 		BTF_TYPE_ARRAY_ENC(3, 3, 16),
1838 		/* CONST type_id=4 */			/* [3] */
1839 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 4),
1840 		/* void* */				/* [4] */
1841 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
1842 		BTF_END_RAW,
1843 	},
1844 	.str_sec = "",
1845 	.str_sec_size = sizeof(""),
1846 	.map_type = BPF_MAP_TYPE_ARRAY,
1847 	.map_name = "array_test_map",
1848 	.key_size = sizeof(int),
1849 	.value_size = sizeof(int),
1850 	.key_type_id = 1,
1851 	.value_type_id = 1,
1852 	.max_entries = 4,
1853 	.btf_load_err = true,
1854 	.err_str = "Invalid index",
1855 },
1856 
1857 {
1858 	.descr = "array test. t->size != 0\"",
1859 	.raw_types = {
1860 		/* int */				/* [1] */
1861 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1862 		/* int[16] */				/* [2] */
1863 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ARRAY, 0, 0), 1),
1864 		BTF_ARRAY_ENC(1, 1, 16),
1865 		BTF_END_RAW,
1866 	},
1867 	.str_sec = "",
1868 	.str_sec_size = sizeof(""),
1869 	.map_type = BPF_MAP_TYPE_ARRAY,
1870 	.map_name = "array_test_map",
1871 	.key_size = sizeof(int),
1872 	.value_size = sizeof(int),
1873 	.key_type_id = 1,
1874 	.value_type_id = 1,
1875 	.max_entries = 4,
1876 	.btf_load_err = true,
1877 	.err_str = "size != 0",
1878 },
1879 
1880 {
1881 	.descr = "int test. invalid int_data",
1882 	.raw_types = {
1883 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), 4),
1884 		0x10000000,
1885 		BTF_END_RAW,
1886 	},
1887 	.str_sec = "",
1888 	.str_sec_size = sizeof(""),
1889 	.map_type = BPF_MAP_TYPE_ARRAY,
1890 	.map_name = "array_test_map",
1891 	.key_size = sizeof(int),
1892 	.value_size = sizeof(int),
1893 	.key_type_id = 1,
1894 	.value_type_id = 1,
1895 	.max_entries = 4,
1896 	.btf_load_err = true,
1897 	.err_str = "Invalid int_data",
1898 },
1899 
1900 {
1901 	.descr = "invalid BTF_INFO",
1902 	.raw_types = {
1903 		/* int */				/* [1] */
1904 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1905 		BTF_TYPE_ENC(0, 0x20000000, 4),
1906 		BTF_END_RAW,
1907 	},
1908 	.str_sec = "",
1909 	.str_sec_size = sizeof(""),
1910 	.map_type = BPF_MAP_TYPE_ARRAY,
1911 	.map_name = "array_test_map",
1912 	.key_size = sizeof(int),
1913 	.value_size = sizeof(int),
1914 	.key_type_id = 1,
1915 	.value_type_id = 1,
1916 	.max_entries = 4,
1917 	.btf_load_err = true,
1918 	.err_str = "Invalid btf_info",
1919 },
1920 
1921 {
1922 	.descr = "fwd test. t->type != 0\"",
1923 	.raw_types = {
1924 		/* int */				/* [1] */
1925 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1926 		/* fwd type */				/* [2] */
1927 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FWD, 0, 0), 1),
1928 		BTF_END_RAW,
1929 	},
1930 	.str_sec = "",
1931 	.str_sec_size = sizeof(""),
1932 	.map_type = BPF_MAP_TYPE_ARRAY,
1933 	.map_name = "fwd_test_map",
1934 	.key_size = sizeof(int),
1935 	.value_size = sizeof(int),
1936 	.key_type_id = 1,
1937 	.value_type_id = 1,
1938 	.max_entries = 4,
1939 	.btf_load_err = true,
1940 	.err_str = "type != 0",
1941 },
1942 
1943 {
1944 	.descr = "typedef (invalid name, name_off = 0)",
1945 	.raw_types = {
1946 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
1947 		BTF_TYPEDEF_ENC(0, 1),				/* [2] */
1948 		BTF_END_RAW,
1949 	},
1950 	.str_sec = "\0__int",
1951 	.str_sec_size = sizeof("\0__int"),
1952 	.map_type = BPF_MAP_TYPE_ARRAY,
1953 	.map_name = "typedef_check_btf",
1954 	.key_size = sizeof(int),
1955 	.value_size = sizeof(int),
1956 	.key_type_id = 1,
1957 	.value_type_id = 1,
1958 	.max_entries = 4,
1959 	.btf_load_err = true,
1960 	.err_str = "Invalid name",
1961 },
1962 
1963 {
1964 	.descr = "typedef (invalid name, invalid identifier)",
1965 	.raw_types = {
1966 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
1967 		BTF_TYPEDEF_ENC(NAME_TBD, 1),			/* [2] */
1968 		BTF_END_RAW,
1969 	},
1970 	.str_sec = "\0__!int",
1971 	.str_sec_size = sizeof("\0__!int"),
1972 	.map_type = BPF_MAP_TYPE_ARRAY,
1973 	.map_name = "typedef_check_btf",
1974 	.key_size = sizeof(int),
1975 	.value_size = sizeof(int),
1976 	.key_type_id = 1,
1977 	.value_type_id = 1,
1978 	.max_entries = 4,
1979 	.btf_load_err = true,
1980 	.err_str = "Invalid name",
1981 },
1982 
1983 {
1984 	.descr = "ptr type (invalid name, name_off <> 0)",
1985 	.raw_types = {
1986 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
1987 		BTF_TYPE_ENC(NAME_TBD,
1988 			     BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 1),	/* [2] */
1989 		BTF_END_RAW,
1990 	},
1991 	.str_sec = "\0__int",
1992 	.str_sec_size = sizeof("\0__int"),
1993 	.map_type = BPF_MAP_TYPE_ARRAY,
1994 	.map_name = "ptr_type_check_btf",
1995 	.key_size = sizeof(int),
1996 	.value_size = sizeof(int),
1997 	.key_type_id = 1,
1998 	.value_type_id = 1,
1999 	.max_entries = 4,
2000 	.btf_load_err = true,
2001 	.err_str = "Invalid name",
2002 },
2003 
2004 {
2005 	.descr = "volatile type (invalid name, name_off <> 0)",
2006 	.raw_types = {
2007 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2008 		BTF_TYPE_ENC(NAME_TBD,
2009 			     BTF_INFO_ENC(BTF_KIND_VOLATILE, 0, 0), 1),	/* [2] */
2010 		BTF_END_RAW,
2011 	},
2012 	.str_sec = "\0__int",
2013 	.str_sec_size = sizeof("\0__int"),
2014 	.map_type = BPF_MAP_TYPE_ARRAY,
2015 	.map_name = "volatile_type_check_btf",
2016 	.key_size = sizeof(int),
2017 	.value_size = sizeof(int),
2018 	.key_type_id = 1,
2019 	.value_type_id = 1,
2020 	.max_entries = 4,
2021 	.btf_load_err = true,
2022 	.err_str = "Invalid name",
2023 },
2024 
2025 {
2026 	.descr = "const type (invalid name, name_off <> 0)",
2027 	.raw_types = {
2028 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2029 		BTF_TYPE_ENC(NAME_TBD,
2030 			     BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 1),	/* [2] */
2031 		BTF_END_RAW,
2032 	},
2033 	.str_sec = "\0__int",
2034 	.str_sec_size = sizeof("\0__int"),
2035 	.map_type = BPF_MAP_TYPE_ARRAY,
2036 	.map_name = "const_type_check_btf",
2037 	.key_size = sizeof(int),
2038 	.value_size = sizeof(int),
2039 	.key_type_id = 1,
2040 	.value_type_id = 1,
2041 	.max_entries = 4,
2042 	.btf_load_err = true,
2043 	.err_str = "Invalid name",
2044 },
2045 
2046 {
2047 	.descr = "restrict type (invalid name, name_off <> 0)",
2048 	.raw_types = {
2049 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2050 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 1),	/* [2] */
2051 		BTF_TYPE_ENC(NAME_TBD,
2052 			     BTF_INFO_ENC(BTF_KIND_RESTRICT, 0, 0), 2),	/* [3] */
2053 		BTF_END_RAW,
2054 	},
2055 	.str_sec = "\0__int",
2056 	.str_sec_size = sizeof("\0__int"),
2057 	.map_type = BPF_MAP_TYPE_ARRAY,
2058 	.map_name = "restrict_type_check_btf",
2059 	.key_size = sizeof(int),
2060 	.value_size = sizeof(int),
2061 	.key_type_id = 1,
2062 	.value_type_id = 1,
2063 	.max_entries = 4,
2064 	.btf_load_err = true,
2065 	.err_str = "Invalid name",
2066 },
2067 
2068 {
2069 	.descr = "fwd type (invalid name, name_off = 0)",
2070 	.raw_types = {
2071 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2072 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FWD, 0, 0), 0),	/* [2] */
2073 		BTF_END_RAW,
2074 	},
2075 	.str_sec = "\0__skb",
2076 	.str_sec_size = sizeof("\0__skb"),
2077 	.map_type = BPF_MAP_TYPE_ARRAY,
2078 	.map_name = "fwd_type_check_btf",
2079 	.key_size = sizeof(int),
2080 	.value_size = sizeof(int),
2081 	.key_type_id = 1,
2082 	.value_type_id = 1,
2083 	.max_entries = 4,
2084 	.btf_load_err = true,
2085 	.err_str = "Invalid name",
2086 },
2087 
2088 {
2089 	.descr = "fwd type (invalid name, invalid identifier)",
2090 	.raw_types = {
2091 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2092 		BTF_TYPE_ENC(NAME_TBD,
2093 			     BTF_INFO_ENC(BTF_KIND_FWD, 0, 0), 0),	/* [2] */
2094 		BTF_END_RAW,
2095 	},
2096 	.str_sec = "\0__!skb",
2097 	.str_sec_size = sizeof("\0__!skb"),
2098 	.map_type = BPF_MAP_TYPE_ARRAY,
2099 	.map_name = "fwd_type_check_btf",
2100 	.key_size = sizeof(int),
2101 	.value_size = sizeof(int),
2102 	.key_type_id = 1,
2103 	.value_type_id = 1,
2104 	.max_entries = 4,
2105 	.btf_load_err = true,
2106 	.err_str = "Invalid name",
2107 },
2108 
2109 {
2110 	.descr = "array type (invalid name, name_off <> 0)",
2111 	.raw_types = {
2112 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2113 		BTF_TYPE_ENC(NAME_TBD,
2114 			     BTF_INFO_ENC(BTF_KIND_ARRAY, 0, 0), 0),	/* [2] */
2115 		BTF_ARRAY_ENC(1, 1, 4),
2116 		BTF_END_RAW,
2117 	},
2118 	.str_sec = "\0__skb",
2119 	.str_sec_size = sizeof("\0__skb"),
2120 	.map_type = BPF_MAP_TYPE_ARRAY,
2121 	.map_name = "array_type_check_btf",
2122 	.key_size = sizeof(int),
2123 	.value_size = sizeof(int),
2124 	.key_type_id = 1,
2125 	.value_type_id = 1,
2126 	.max_entries = 4,
2127 	.btf_load_err = true,
2128 	.err_str = "Invalid name",
2129 },
2130 
2131 {
2132 	.descr = "struct type (name_off = 0)",
2133 	.raw_types = {
2134 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2135 		BTF_TYPE_ENC(0,
2136 			     BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4),	/* [2] */
2137 		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
2138 		BTF_END_RAW,
2139 	},
2140 	.str_sec = "\0A",
2141 	.str_sec_size = sizeof("\0A"),
2142 	.map_type = BPF_MAP_TYPE_ARRAY,
2143 	.map_name = "struct_type_check_btf",
2144 	.key_size = sizeof(int),
2145 	.value_size = sizeof(int),
2146 	.key_type_id = 1,
2147 	.value_type_id = 1,
2148 	.max_entries = 4,
2149 },
2150 
2151 {
2152 	.descr = "struct type (invalid name, invalid identifier)",
2153 	.raw_types = {
2154 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2155 		BTF_TYPE_ENC(NAME_TBD,
2156 			     BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4),	/* [2] */
2157 		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
2158 		BTF_END_RAW,
2159 	},
2160 	.str_sec = "\0A!\0B",
2161 	.str_sec_size = sizeof("\0A!\0B"),
2162 	.map_type = BPF_MAP_TYPE_ARRAY,
2163 	.map_name = "struct_type_check_btf",
2164 	.key_size = sizeof(int),
2165 	.value_size = sizeof(int),
2166 	.key_type_id = 1,
2167 	.value_type_id = 1,
2168 	.max_entries = 4,
2169 	.btf_load_err = true,
2170 	.err_str = "Invalid name",
2171 },
2172 
2173 {
2174 	.descr = "struct member (name_off = 0)",
2175 	.raw_types = {
2176 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2177 		BTF_TYPE_ENC(0,
2178 			     BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4),	/* [2] */
2179 		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
2180 		BTF_END_RAW,
2181 	},
2182 	.str_sec = "\0A",
2183 	.str_sec_size = sizeof("\0A"),
2184 	.map_type = BPF_MAP_TYPE_ARRAY,
2185 	.map_name = "struct_type_check_btf",
2186 	.key_size = sizeof(int),
2187 	.value_size = sizeof(int),
2188 	.key_type_id = 1,
2189 	.value_type_id = 1,
2190 	.max_entries = 4,
2191 },
2192 
2193 {
2194 	.descr = "struct member (invalid name, invalid identifier)",
2195 	.raw_types = {
2196 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2197 		BTF_TYPE_ENC(NAME_TBD,
2198 			     BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4),	/* [2] */
2199 		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
2200 		BTF_END_RAW,
2201 	},
2202 	.str_sec = "\0A\0B*",
2203 	.str_sec_size = sizeof("\0A\0B*"),
2204 	.map_type = BPF_MAP_TYPE_ARRAY,
2205 	.map_name = "struct_type_check_btf",
2206 	.key_size = sizeof(int),
2207 	.value_size = sizeof(int),
2208 	.key_type_id = 1,
2209 	.value_type_id = 1,
2210 	.max_entries = 4,
2211 	.btf_load_err = true,
2212 	.err_str = "Invalid name",
2213 },
2214 
2215 {
2216 	.descr = "enum type (name_off = 0)",
2217 	.raw_types = {
2218 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2219 		BTF_TYPE_ENC(0,
2220 			     BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1),
2221 			     sizeof(int)),				/* [2] */
2222 		BTF_ENUM_ENC(NAME_TBD, 0),
2223 		BTF_END_RAW,
2224 	},
2225 	.str_sec = "\0A\0B",
2226 	.str_sec_size = sizeof("\0A\0B"),
2227 	.map_type = BPF_MAP_TYPE_ARRAY,
2228 	.map_name = "enum_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 },
2235 
2236 {
2237 	.descr = "enum type (invalid name, invalid identifier)",
2238 	.raw_types = {
2239 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2240 		BTF_TYPE_ENC(NAME_TBD,
2241 			     BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1),
2242 			     sizeof(int)),				/* [2] */
2243 		BTF_ENUM_ENC(NAME_TBD, 0),
2244 		BTF_END_RAW,
2245 	},
2246 	.str_sec = "\0A!\0B",
2247 	.str_sec_size = sizeof("\0A!\0B"),
2248 	.map_type = BPF_MAP_TYPE_ARRAY,
2249 	.map_name = "enum_type_check_btf",
2250 	.key_size = sizeof(int),
2251 	.value_size = sizeof(int),
2252 	.key_type_id = 1,
2253 	.value_type_id = 1,
2254 	.max_entries = 4,
2255 	.btf_load_err = true,
2256 	.err_str = "Invalid name",
2257 },
2258 
2259 {
2260 	.descr = "enum member (invalid name, name_off = 0)",
2261 	.raw_types = {
2262 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2263 		BTF_TYPE_ENC(0,
2264 			     BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1),
2265 			     sizeof(int)),				/* [2] */
2266 		BTF_ENUM_ENC(0, 0),
2267 		BTF_END_RAW,
2268 	},
2269 	.str_sec = "",
2270 	.str_sec_size = sizeof(""),
2271 	.map_type = BPF_MAP_TYPE_ARRAY,
2272 	.map_name = "enum_type_check_btf",
2273 	.key_size = sizeof(int),
2274 	.value_size = sizeof(int),
2275 	.key_type_id = 1,
2276 	.value_type_id = 1,
2277 	.max_entries = 4,
2278 	.btf_load_err = true,
2279 	.err_str = "Invalid name",
2280 },
2281 
2282 {
2283 	.descr = "enum member (invalid name, invalid identifier)",
2284 	.raw_types = {
2285 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2286 		BTF_TYPE_ENC(0,
2287 			     BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1),
2288 			     sizeof(int)),				/* [2] */
2289 		BTF_ENUM_ENC(NAME_TBD, 0),
2290 		BTF_END_RAW,
2291 	},
2292 	.str_sec = "\0A!",
2293 	.str_sec_size = sizeof("\0A!"),
2294 	.map_type = BPF_MAP_TYPE_ARRAY,
2295 	.map_name = "enum_type_check_btf",
2296 	.key_size = sizeof(int),
2297 	.value_size = sizeof(int),
2298 	.key_type_id = 1,
2299 	.value_type_id = 1,
2300 	.max_entries = 4,
2301 	.btf_load_err = true,
2302 	.err_str = "Invalid name",
2303 },
2304 {
2305 	.descr = "arraymap invalid btf key (a bit field)",
2306 	.raw_types = {
2307 		/* int */				/* [1] */
2308 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
2309 		/* 32 bit int with 32 bit offset */	/* [2] */
2310 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 32, 32, 8),
2311 		BTF_END_RAW,
2312 	},
2313 	.str_sec = "",
2314 	.str_sec_size = sizeof(""),
2315 	.map_type = BPF_MAP_TYPE_ARRAY,
2316 	.map_name = "array_map_check_btf",
2317 	.key_size = sizeof(int),
2318 	.value_size = sizeof(int),
2319 	.key_type_id = 2,
2320 	.value_type_id = 1,
2321 	.max_entries = 4,
2322 	.map_create_err = true,
2323 },
2324 
2325 {
2326 	.descr = "arraymap invalid btf key (!= 32 bits)",
2327 	.raw_types = {
2328 		/* int */				/* [1] */
2329 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
2330 		/* 16 bit int with 0 bit offset */	/* [2] */
2331 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 16, 2),
2332 		BTF_END_RAW,
2333 	},
2334 	.str_sec = "",
2335 	.str_sec_size = sizeof(""),
2336 	.map_type = BPF_MAP_TYPE_ARRAY,
2337 	.map_name = "array_map_check_btf",
2338 	.key_size = sizeof(int),
2339 	.value_size = sizeof(int),
2340 	.key_type_id = 2,
2341 	.value_type_id = 1,
2342 	.max_entries = 4,
2343 	.map_create_err = true,
2344 },
2345 
2346 {
2347 	.descr = "arraymap invalid btf value (too small)",
2348 	.raw_types = {
2349 		/* int */				/* [1] */
2350 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
2351 		BTF_END_RAW,
2352 	},
2353 	.str_sec = "",
2354 	.str_sec_size = sizeof(""),
2355 	.map_type = BPF_MAP_TYPE_ARRAY,
2356 	.map_name = "array_map_check_btf",
2357 	.key_size = sizeof(int),
2358 	/* btf_value_size < map->value_size */
2359 	.value_size = sizeof(__u64),
2360 	.key_type_id = 1,
2361 	.value_type_id = 1,
2362 	.max_entries = 4,
2363 	.map_create_err = true,
2364 },
2365 
2366 {
2367 	.descr = "arraymap invalid btf value (too big)",
2368 	.raw_types = {
2369 		/* int */				/* [1] */
2370 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
2371 		BTF_END_RAW,
2372 	},
2373 	.str_sec = "",
2374 	.str_sec_size = sizeof(""),
2375 	.map_type = BPF_MAP_TYPE_ARRAY,
2376 	.map_name = "array_map_check_btf",
2377 	.key_size = sizeof(int),
2378 	/* btf_value_size > map->value_size */
2379 	.value_size = sizeof(__u16),
2380 	.key_type_id = 1,
2381 	.value_type_id = 1,
2382 	.max_entries = 4,
2383 	.map_create_err = true,
2384 },
2385 
2386 {
2387 	.descr = "func proto (int (*)(int, unsigned int))",
2388 	.raw_types = {
2389 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2390 		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2391 		/* int (*)(int, unsigned int) */
2392 		BTF_FUNC_PROTO_ENC(1, 2),			/* [3] */
2393 			BTF_FUNC_PROTO_ARG_ENC(0, 1),
2394 			BTF_FUNC_PROTO_ARG_ENC(0, 2),
2395 		BTF_END_RAW,
2396 	},
2397 	.str_sec = "",
2398 	.str_sec_size = sizeof(""),
2399 	.map_type = BPF_MAP_TYPE_ARRAY,
2400 	.map_name = "func_proto_type_check_btf",
2401 	.key_size = sizeof(int),
2402 	.value_size = sizeof(int),
2403 	.key_type_id = 1,
2404 	.value_type_id = 1,
2405 	.max_entries = 4,
2406 },
2407 
2408 {
2409 	.descr = "func proto (vararg)",
2410 	.raw_types = {
2411 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2412 		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2413 		/* void (*)(int, unsigned int, ...) */
2414 		BTF_FUNC_PROTO_ENC(0, 3),			/* [3] */
2415 			BTF_FUNC_PROTO_ARG_ENC(0, 1),
2416 			BTF_FUNC_PROTO_ARG_ENC(0, 2),
2417 			BTF_FUNC_PROTO_ARG_ENC(0, 0),
2418 		BTF_END_RAW,
2419 	},
2420 	.str_sec = "",
2421 	.str_sec_size = sizeof(""),
2422 	.map_type = BPF_MAP_TYPE_ARRAY,
2423 	.map_name = "func_proto_type_check_btf",
2424 	.key_size = sizeof(int),
2425 	.value_size = sizeof(int),
2426 	.key_type_id = 1,
2427 	.value_type_id = 1,
2428 	.max_entries = 4,
2429 },
2430 
2431 {
2432 	.descr = "func proto (vararg with name)",
2433 	.raw_types = {
2434 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2435 		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2436 		/* void (*)(int a, unsigned int b, ... c) */
2437 		BTF_FUNC_PROTO_ENC(0, 3),			/* [3] */
2438 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2439 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2440 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 0),
2441 		BTF_END_RAW,
2442 	},
2443 	.str_sec = "\0a\0b\0c",
2444 	.str_sec_size = sizeof("\0a\0b\0c"),
2445 	.map_type = BPF_MAP_TYPE_ARRAY,
2446 	.map_name = "func_proto_type_check_btf",
2447 	.key_size = sizeof(int),
2448 	.value_size = sizeof(int),
2449 	.key_type_id = 1,
2450 	.value_type_id = 1,
2451 	.max_entries = 4,
2452 	.btf_load_err = true,
2453 	.err_str = "Invalid arg#3",
2454 },
2455 
2456 {
2457 	.descr = "func proto (arg after vararg)",
2458 	.raw_types = {
2459 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2460 		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2461 		/* void (*)(int a, ..., unsigned int b) */
2462 		BTF_FUNC_PROTO_ENC(0, 3),			/* [3] */
2463 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2464 			BTF_FUNC_PROTO_ARG_ENC(0, 0),
2465 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2466 		BTF_END_RAW,
2467 	},
2468 	.str_sec = "\0a\0b",
2469 	.str_sec_size = sizeof("\0a\0b"),
2470 	.map_type = BPF_MAP_TYPE_ARRAY,
2471 	.map_name = "func_proto_type_check_btf",
2472 	.key_size = sizeof(int),
2473 	.value_size = sizeof(int),
2474 	.key_type_id = 1,
2475 	.value_type_id = 1,
2476 	.max_entries = 4,
2477 	.btf_load_err = true,
2478 	.err_str = "Invalid arg#2",
2479 },
2480 
2481 {
2482 	.descr = "func proto (CONST=>TYPEDEF=>PTR=>FUNC_PROTO)",
2483 	.raw_types = {
2484 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2485 		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2486 		/* typedef void (*func_ptr)(int, unsigned int) */
2487 		BTF_TYPEDEF_ENC(NAME_TBD, 5),			/* [3] */
2488 		/* const func_ptr */
2489 		BTF_CONST_ENC(3),				/* [4] */
2490 		BTF_PTR_ENC(6),					/* [5] */
2491 		BTF_FUNC_PROTO_ENC(0, 2),			/* [6] */
2492 			BTF_FUNC_PROTO_ARG_ENC(0, 1),
2493 			BTF_FUNC_PROTO_ARG_ENC(0, 2),
2494 		BTF_END_RAW,
2495 	},
2496 	.str_sec = "\0func_ptr",
2497 	.str_sec_size = sizeof("\0func_ptr"),
2498 	.map_type = BPF_MAP_TYPE_ARRAY,
2499 	.map_name = "func_proto_type_check_btf",
2500 	.key_size = sizeof(int),
2501 	.value_size = sizeof(int),
2502 	.key_type_id = 1,
2503 	.value_type_id = 1,
2504 	.max_entries = 4,
2505 },
2506 
2507 {
2508 	.descr = "func proto (TYPEDEF=>FUNC_PROTO)",
2509 	.raw_types = {
2510 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2511 		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2512 		BTF_TYPEDEF_ENC(NAME_TBD, 4),			/* [3] */
2513 		BTF_FUNC_PROTO_ENC(0, 2),			/* [4] */
2514 			BTF_FUNC_PROTO_ARG_ENC(0, 1),
2515 			BTF_FUNC_PROTO_ARG_ENC(0, 2),
2516 		BTF_END_RAW,
2517 	},
2518 	.str_sec = "\0func_typedef",
2519 	.str_sec_size = sizeof("\0func_typedef"),
2520 	.map_type = BPF_MAP_TYPE_ARRAY,
2521 	.map_name = "func_proto_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 = "func proto (btf_resolve(arg))",
2531 	.raw_types = {
2532 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2533 		/* void (*)(const void *) */
2534 		BTF_FUNC_PROTO_ENC(0, 1),			/* [2] */
2535 			BTF_FUNC_PROTO_ARG_ENC(0, 3),
2536 		BTF_CONST_ENC(4),				/* [3] */
2537 		BTF_PTR_ENC(0),					/* [4] */
2538 		BTF_END_RAW,
2539 	},
2540 	.str_sec = "",
2541 	.str_sec_size = sizeof(""),
2542 	.map_type = BPF_MAP_TYPE_ARRAY,
2543 	.map_name = "func_proto_type_check_btf",
2544 	.key_size = sizeof(int),
2545 	.value_size = sizeof(int),
2546 	.key_type_id = 1,
2547 	.value_type_id = 1,
2548 	.max_entries = 4,
2549 },
2550 
2551 {
2552 	.descr = "func proto (Not all arg has name)",
2553 	.raw_types = {
2554 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2555 		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2556 		/* void (*)(int, unsigned int b) */
2557 		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2558 			BTF_FUNC_PROTO_ARG_ENC(0, 1),
2559 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2560 		BTF_END_RAW,
2561 	},
2562 	.str_sec = "\0b",
2563 	.str_sec_size = sizeof("\0b"),
2564 	.map_type = BPF_MAP_TYPE_ARRAY,
2565 	.map_name = "func_proto_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 = "func proto (Bad arg name_off)",
2575 	.raw_types = {
2576 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2577 		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2578 		/* void (*)(int a, unsigned int <bad_name_off>) */
2579 		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2580 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2581 			BTF_FUNC_PROTO_ARG_ENC(0x0fffffff, 2),
2582 		BTF_END_RAW,
2583 	},
2584 	.str_sec = "\0a",
2585 	.str_sec_size = sizeof("\0a"),
2586 	.map_type = BPF_MAP_TYPE_ARRAY,
2587 	.map_name = "func_proto_type_check_btf",
2588 	.key_size = sizeof(int),
2589 	.value_size = sizeof(int),
2590 	.key_type_id = 1,
2591 	.value_type_id = 1,
2592 	.max_entries = 4,
2593 	.btf_load_err = true,
2594 	.err_str = "Invalid arg#2",
2595 },
2596 
2597 {
2598 	.descr = "func proto (Bad arg name)",
2599 	.raw_types = {
2600 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2601 		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2602 		/* void (*)(int a, unsigned int !!!) */
2603 		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2604 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2605 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2606 		BTF_END_RAW,
2607 	},
2608 	.str_sec = "\0a\0!!!",
2609 	.str_sec_size = sizeof("\0a\0!!!"),
2610 	.map_type = BPF_MAP_TYPE_ARRAY,
2611 	.map_name = "func_proto_type_check_btf",
2612 	.key_size = sizeof(int),
2613 	.value_size = sizeof(int),
2614 	.key_type_id = 1,
2615 	.value_type_id = 1,
2616 	.max_entries = 4,
2617 	.btf_load_err = true,
2618 	.err_str = "Invalid arg#2",
2619 },
2620 
2621 {
2622 	.descr = "func proto (Invalid return type)",
2623 	.raw_types = {
2624 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2625 		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2626 		/* <bad_ret_type> (*)(int, unsigned int) */
2627 		BTF_FUNC_PROTO_ENC(100, 2),			/* [3] */
2628 			BTF_FUNC_PROTO_ARG_ENC(0, 1),
2629 			BTF_FUNC_PROTO_ARG_ENC(0, 2),
2630 		BTF_END_RAW,
2631 	},
2632 	.str_sec = "",
2633 	.str_sec_size = sizeof(""),
2634 	.map_type = BPF_MAP_TYPE_ARRAY,
2635 	.map_name = "func_proto_type_check_btf",
2636 	.key_size = sizeof(int),
2637 	.value_size = sizeof(int),
2638 	.key_type_id = 1,
2639 	.value_type_id = 1,
2640 	.max_entries = 4,
2641 	.btf_load_err = true,
2642 	.err_str = "Invalid return type",
2643 },
2644 
2645 {
2646 	.descr = "func proto (with func name)",
2647 	.raw_types = {
2648 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2649 		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2650 		/* void func_proto(int, unsigned int) */
2651 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 2), 0),	/* [3] */
2652 			BTF_FUNC_PROTO_ARG_ENC(0, 1),
2653 			BTF_FUNC_PROTO_ARG_ENC(0, 2),
2654 		BTF_END_RAW,
2655 	},
2656 	.str_sec = "\0func_proto",
2657 	.str_sec_size = sizeof("\0func_proto"),
2658 	.map_type = BPF_MAP_TYPE_ARRAY,
2659 	.map_name = "func_proto_type_check_btf",
2660 	.key_size = sizeof(int),
2661 	.value_size = sizeof(int),
2662 	.key_type_id = 1,
2663 	.value_type_id = 1,
2664 	.max_entries = 4,
2665 	.btf_load_err = true,
2666 	.err_str = "Invalid name",
2667 },
2668 
2669 {
2670 	.descr = "func proto (const void arg)",
2671 	.raw_types = {
2672 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2673 		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2674 		/* void (*)(const void) */
2675 		BTF_FUNC_PROTO_ENC(0, 1),			/* [3] */
2676 			BTF_FUNC_PROTO_ARG_ENC(0, 4),
2677 		BTF_CONST_ENC(0),				/* [4] */
2678 		BTF_END_RAW,
2679 	},
2680 	.str_sec = "",
2681 	.str_sec_size = sizeof(""),
2682 	.map_type = BPF_MAP_TYPE_ARRAY,
2683 	.map_name = "func_proto_type_check_btf",
2684 	.key_size = sizeof(int),
2685 	.value_size = sizeof(int),
2686 	.key_type_id = 1,
2687 	.value_type_id = 1,
2688 	.max_entries = 4,
2689 	.btf_load_err = true,
2690 	.err_str = "Invalid arg#1",
2691 },
2692 
2693 {
2694 	.descr = "func (void func(int a, unsigned int b))",
2695 	.raw_types = {
2696 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2697 		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2698 		/* void (*)(int a, unsigned int b) */
2699 		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2700 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2701 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2702 		/* void func(int a, unsigned int b) */
2703 		BTF_FUNC_ENC(NAME_TBD, 3),			/* [4] */
2704 		BTF_END_RAW,
2705 	},
2706 	.str_sec = "\0a\0b\0func",
2707 	.str_sec_size = sizeof("\0a\0b\0func"),
2708 	.map_type = BPF_MAP_TYPE_ARRAY,
2709 	.map_name = "func_type_check_btf",
2710 	.key_size = sizeof(int),
2711 	.value_size = sizeof(int),
2712 	.key_type_id = 1,
2713 	.value_type_id = 1,
2714 	.max_entries = 4,
2715 },
2716 
2717 {
2718 	.descr = "func (No func name)",
2719 	.raw_types = {
2720 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2721 		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2722 		/* void (*)(int a, unsigned int b) */
2723 		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2724 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2725 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2726 		/* void <no_name>(int a, unsigned int b) */
2727 		BTF_FUNC_ENC(0, 3),				/* [4] */
2728 		BTF_END_RAW,
2729 	},
2730 	.str_sec = "\0a\0b",
2731 	.str_sec_size = sizeof("\0a\0b"),
2732 	.map_type = BPF_MAP_TYPE_ARRAY,
2733 	.map_name = "func_type_check_btf",
2734 	.key_size = sizeof(int),
2735 	.value_size = sizeof(int),
2736 	.key_type_id = 1,
2737 	.value_type_id = 1,
2738 	.max_entries = 4,
2739 	.btf_load_err = true,
2740 	.err_str = "Invalid name",
2741 },
2742 
2743 {
2744 	.descr = "func (Invalid func name)",
2745 	.raw_types = {
2746 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2747 		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2748 		/* void (*)(int a, unsigned int b) */
2749 		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2750 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2751 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2752 		/* void !!!(int a, unsigned int b) */
2753 		BTF_FUNC_ENC(NAME_TBD, 3),			/* [4] */
2754 		BTF_END_RAW,
2755 	},
2756 	.str_sec = "\0a\0b\0!!!",
2757 	.str_sec_size = sizeof("\0a\0b\0!!!"),
2758 	.map_type = BPF_MAP_TYPE_ARRAY,
2759 	.map_name = "func_type_check_btf",
2760 	.key_size = sizeof(int),
2761 	.value_size = sizeof(int),
2762 	.key_type_id = 1,
2763 	.value_type_id = 1,
2764 	.max_entries = 4,
2765 	.btf_load_err = true,
2766 	.err_str = "Invalid name",
2767 },
2768 
2769 {
2770 	.descr = "func (Some arg has no name)",
2771 	.raw_types = {
2772 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2773 		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2774 		/* void (*)(int a, unsigned int) */
2775 		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2776 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2777 			BTF_FUNC_PROTO_ARG_ENC(0, 2),
2778 		/* void func(int a, unsigned int) */
2779 		BTF_FUNC_ENC(NAME_TBD, 3),			/* [4] */
2780 		BTF_END_RAW,
2781 	},
2782 	.str_sec = "\0a\0func",
2783 	.str_sec_size = sizeof("\0a\0func"),
2784 	.map_type = BPF_MAP_TYPE_ARRAY,
2785 	.map_name = "func_type_check_btf",
2786 	.key_size = sizeof(int),
2787 	.value_size = sizeof(int),
2788 	.key_type_id = 1,
2789 	.value_type_id = 1,
2790 	.max_entries = 4,
2791 	.btf_load_err = true,
2792 	.err_str = "Invalid arg#2",
2793 },
2794 
2795 {
2796 	.descr = "func (Non zero vlen)",
2797 	.raw_types = {
2798 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2799 		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2800 		/* void (*)(int a, unsigned int b) */
2801 		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2802 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2803 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2804 		/* void func(int a, unsigned int b) */
2805 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 2), 3), 	/* [4] */
2806 		BTF_END_RAW,
2807 	},
2808 	.str_sec = "\0a\0b\0func",
2809 	.str_sec_size = sizeof("\0a\0b\0func"),
2810 	.map_type = BPF_MAP_TYPE_ARRAY,
2811 	.map_name = "func_type_check_btf",
2812 	.key_size = sizeof(int),
2813 	.value_size = sizeof(int),
2814 	.key_type_id = 1,
2815 	.value_type_id = 1,
2816 	.max_entries = 4,
2817 	.btf_load_err = true,
2818 	.err_str = "Invalid func linkage",
2819 },
2820 
2821 {
2822 	.descr = "func (Not referring to FUNC_PROTO)",
2823 	.raw_types = {
2824 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2825 		BTF_FUNC_ENC(NAME_TBD, 1),			/* [2] */
2826 		BTF_END_RAW,
2827 	},
2828 	.str_sec = "\0func",
2829 	.str_sec_size = sizeof("\0func"),
2830 	.map_type = BPF_MAP_TYPE_ARRAY,
2831 	.map_name = "func_type_check_btf",
2832 	.key_size = sizeof(int),
2833 	.value_size = sizeof(int),
2834 	.key_type_id = 1,
2835 	.value_type_id = 1,
2836 	.max_entries = 4,
2837 	.btf_load_err = true,
2838 	.err_str = "Invalid type_id",
2839 },
2840 
2841 {
2842 	.descr = "invalid int kind_flag",
2843 	.raw_types = {
2844 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2845 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_INT, 1, 0), 4),	/* [2] */
2846 		BTF_INT_ENC(0, 0, 32),
2847 		BTF_END_RAW,
2848 	},
2849 	BTF_STR_SEC(""),
2850 	.map_type = BPF_MAP_TYPE_ARRAY,
2851 	.map_name = "int_type_check_btf",
2852 	.key_size = sizeof(int),
2853 	.value_size = sizeof(int),
2854 	.key_type_id = 1,
2855 	.value_type_id = 1,
2856 	.max_entries = 4,
2857 	.btf_load_err = true,
2858 	.err_str = "Invalid btf_info kind_flag",
2859 },
2860 
2861 {
2862 	.descr = "invalid ptr kind_flag",
2863 	.raw_types = {
2864 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2865 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 1, 0), 1),	/* [2] */
2866 		BTF_END_RAW,
2867 	},
2868 	BTF_STR_SEC(""),
2869 	.map_type = BPF_MAP_TYPE_ARRAY,
2870 	.map_name = "ptr_type_check_btf",
2871 	.key_size = sizeof(int),
2872 	.value_size = sizeof(int),
2873 	.key_type_id = 1,
2874 	.value_type_id = 1,
2875 	.max_entries = 4,
2876 	.btf_load_err = true,
2877 	.err_str = "Invalid btf_info kind_flag",
2878 },
2879 
2880 {
2881 	.descr = "invalid array kind_flag",
2882 	.raw_types = {
2883 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2884 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ARRAY, 1, 0), 0),	/* [2] */
2885 		BTF_ARRAY_ENC(1, 1, 1),
2886 		BTF_END_RAW,
2887 	},
2888 	BTF_STR_SEC(""),
2889 	.map_type = BPF_MAP_TYPE_ARRAY,
2890 	.map_name = "array_type_check_btf",
2891 	.key_size = sizeof(int),
2892 	.value_size = sizeof(int),
2893 	.key_type_id = 1,
2894 	.value_type_id = 1,
2895 	.max_entries = 4,
2896 	.btf_load_err = true,
2897 	.err_str = "Invalid btf_info kind_flag",
2898 },
2899 
2900 {
2901 	.descr = "invalid enum kind_flag",
2902 	.raw_types = {
2903 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2904 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 1, 1), 4),	/* [2] */
2905 		BTF_ENUM_ENC(NAME_TBD, 0),
2906 		BTF_END_RAW,
2907 	},
2908 	BTF_STR_SEC("\0A"),
2909 	.map_type = BPF_MAP_TYPE_ARRAY,
2910 	.map_name = "enum_type_check_btf",
2911 	.key_size = sizeof(int),
2912 	.value_size = sizeof(int),
2913 	.key_type_id = 1,
2914 	.value_type_id = 1,
2915 	.max_entries = 4,
2916 	.btf_load_err = true,
2917 	.err_str = "Invalid btf_info kind_flag",
2918 },
2919 
2920 {
2921 	.descr = "valid fwd kind_flag",
2922 	.raw_types = {
2923 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2924 		BTF_TYPE_ENC(NAME_TBD,
2925 			     BTF_INFO_ENC(BTF_KIND_FWD, 1, 0), 0),	/* [2] */
2926 		BTF_END_RAW,
2927 	},
2928 	BTF_STR_SEC("\0A"),
2929 	.map_type = BPF_MAP_TYPE_ARRAY,
2930 	.map_name = "fwd_type_check_btf",
2931 	.key_size = sizeof(int),
2932 	.value_size = sizeof(int),
2933 	.key_type_id = 1,
2934 	.value_type_id = 1,
2935 	.max_entries = 4,
2936 },
2937 
2938 {
2939 	.descr = "invalid typedef kind_flag",
2940 	.raw_types = {
2941 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2942 		BTF_TYPE_ENC(NAME_TBD,
2943 			     BTF_INFO_ENC(BTF_KIND_TYPEDEF, 1, 0), 1),	/* [2] */
2944 		BTF_END_RAW,
2945 	},
2946 	BTF_STR_SEC("\0A"),
2947 	.map_type = BPF_MAP_TYPE_ARRAY,
2948 	.map_name = "typedef_type_check_btf",
2949 	.key_size = sizeof(int),
2950 	.value_size = sizeof(int),
2951 	.key_type_id = 1,
2952 	.value_type_id = 1,
2953 	.max_entries = 4,
2954 	.btf_load_err = true,
2955 	.err_str = "Invalid btf_info kind_flag",
2956 },
2957 
2958 {
2959 	.descr = "invalid volatile kind_flag",
2960 	.raw_types = {
2961 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
2962 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_VOLATILE, 1, 0), 1),	/* [2] */
2963 		BTF_END_RAW,
2964 	},
2965 	BTF_STR_SEC(""),
2966 	.map_type = BPF_MAP_TYPE_ARRAY,
2967 	.map_name = "volatile_type_check_btf",
2968 	.key_size = sizeof(int),
2969 	.value_size = sizeof(int),
2970 	.key_type_id = 1,
2971 	.value_type_id = 1,
2972 	.max_entries = 4,
2973 	.btf_load_err = true,
2974 	.err_str = "Invalid btf_info kind_flag",
2975 },
2976 
2977 {
2978 	.descr = "invalid const kind_flag",
2979 	.raw_types = {
2980 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2981 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 1, 0), 1),	/* [2] */
2982 		BTF_END_RAW,
2983 	},
2984 	BTF_STR_SEC(""),
2985 	.map_type = BPF_MAP_TYPE_ARRAY,
2986 	.map_name = "const_type_check_btf",
2987 	.key_size = sizeof(int),
2988 	.value_size = sizeof(int),
2989 	.key_type_id = 1,
2990 	.value_type_id = 1,
2991 	.max_entries = 4,
2992 	.btf_load_err = true,
2993 	.err_str = "Invalid btf_info kind_flag",
2994 },
2995 
2996 {
2997 	.descr = "invalid restrict kind_flag",
2998 	.raw_types = {
2999 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3000 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_RESTRICT, 1, 0), 1),	/* [2] */
3001 		BTF_END_RAW,
3002 	},
3003 	BTF_STR_SEC(""),
3004 	.map_type = BPF_MAP_TYPE_ARRAY,
3005 	.map_name = "restrict_type_check_btf",
3006 	.key_size = sizeof(int),
3007 	.value_size = sizeof(int),
3008 	.key_type_id = 1,
3009 	.value_type_id = 1,
3010 	.max_entries = 4,
3011 	.btf_load_err = true,
3012 	.err_str = "Invalid btf_info kind_flag",
3013 },
3014 
3015 {
3016 	.descr = "invalid func kind_flag",
3017 	.raw_types = {
3018 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3019 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 0), 0),	/* [2] */
3020 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FUNC, 1, 0), 2),	/* [3] */
3021 		BTF_END_RAW,
3022 	},
3023 	BTF_STR_SEC("\0A"),
3024 	.map_type = BPF_MAP_TYPE_ARRAY,
3025 	.map_name = "func_type_check_btf",
3026 	.key_size = sizeof(int),
3027 	.value_size = sizeof(int),
3028 	.key_type_id = 1,
3029 	.value_type_id = 1,
3030 	.max_entries = 4,
3031 	.btf_load_err = true,
3032 	.err_str = "Invalid btf_info kind_flag",
3033 },
3034 
3035 {
3036 	.descr = "invalid func_proto kind_flag",
3037 	.raw_types = {
3038 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3039 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 1, 0), 0),	/* [2] */
3040 		BTF_END_RAW,
3041 	},
3042 	BTF_STR_SEC(""),
3043 	.map_type = BPF_MAP_TYPE_ARRAY,
3044 	.map_name = "func_proto_type_check_btf",
3045 	.key_size = sizeof(int),
3046 	.value_size = sizeof(int),
3047 	.key_type_id = 1,
3048 	.value_type_id = 1,
3049 	.max_entries = 4,
3050 	.btf_load_err = true,
3051 	.err_str = "Invalid btf_info kind_flag",
3052 },
3053 
3054 {
3055 	.descr = "valid struct, kind_flag, bitfield_size = 0",
3056 	.raw_types = {
3057 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3058 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 8),	/* [2] */
3059 		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(0, 0)),
3060 		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(0, 32)),
3061 		BTF_END_RAW,
3062 	},
3063 	BTF_STR_SEC("\0A\0B"),
3064 	.map_type = BPF_MAP_TYPE_ARRAY,
3065 	.map_name = "struct_type_check_btf",
3066 	.key_size = sizeof(int),
3067 	.value_size = sizeof(int),
3068 	.key_type_id = 1,
3069 	.value_type_id = 1,
3070 	.max_entries = 4,
3071 },
3072 
3073 {
3074 	.descr = "valid struct, kind_flag, int member, bitfield_size != 0",
3075 	.raw_types = {
3076 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3077 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 4),	/* [2] */
3078 		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(4, 0)),
3079 		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(4, 4)),
3080 		BTF_END_RAW,
3081 	},
3082 	BTF_STR_SEC("\0A\0B"),
3083 	.map_type = BPF_MAP_TYPE_ARRAY,
3084 	.map_name = "struct_type_check_btf",
3085 	.key_size = sizeof(int),
3086 	.value_size = sizeof(int),
3087 	.key_type_id = 1,
3088 	.value_type_id = 1,
3089 	.max_entries = 4,
3090 },
3091 
3092 {
3093 	.descr = "valid union, kind_flag, int member, bitfield_size != 0",
3094 	.raw_types = {
3095 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
3096 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 1, 2), 4),	/* [2] */
3097 		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(4, 0)),
3098 		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(4, 0)),
3099 		BTF_END_RAW,
3100 	},
3101 	BTF_STR_SEC("\0A\0B"),
3102 	.map_type = BPF_MAP_TYPE_ARRAY,
3103 	.map_name = "union_type_check_btf",
3104 	.key_size = sizeof(int),
3105 	.value_size = sizeof(int),
3106 	.key_type_id = 1,
3107 	.value_type_id = 1,
3108 	.max_entries = 4,
3109 },
3110 
3111 {
3112 	.descr = "valid struct, kind_flag, enum member, bitfield_size != 0",
3113 	.raw_types = {
3114 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
3115 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),	/* [2] */
3116 		BTF_ENUM_ENC(NAME_TBD, 0),
3117 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 4),/* [3] */
3118 		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(4, 0)),
3119 		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(4, 4)),
3120 		BTF_END_RAW,
3121 	},
3122 	BTF_STR_SEC("\0A\0B\0C"),
3123 	.map_type = BPF_MAP_TYPE_ARRAY,
3124 	.map_name = "struct_type_check_btf",
3125 	.key_size = sizeof(int),
3126 	.value_size = sizeof(int),
3127 	.key_type_id = 1,
3128 	.value_type_id = 1,
3129 	.max_entries = 4,
3130 },
3131 
3132 {
3133 	.descr = "valid union, kind_flag, enum member, bitfield_size != 0",
3134 	.raw_types = {
3135 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
3136 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),	/* [2] */
3137 		BTF_ENUM_ENC(NAME_TBD, 0),
3138 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 1, 2), 4),	/* [3] */
3139 		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(4, 0)),
3140 		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(4, 0)),
3141 		BTF_END_RAW,
3142 	},
3143 	BTF_STR_SEC("\0A\0B\0C"),
3144 	.map_type = BPF_MAP_TYPE_ARRAY,
3145 	.map_name = "union_type_check_btf",
3146 	.key_size = sizeof(int),
3147 	.value_size = sizeof(int),
3148 	.key_type_id = 1,
3149 	.value_type_id = 1,
3150 	.max_entries = 4,
3151 },
3152 
3153 {
3154 	.descr = "valid struct, kind_flag, typedef member, bitfield_size != 0",
3155 	.raw_types = {
3156 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
3157 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),	/* [2] */
3158 		BTF_ENUM_ENC(NAME_TBD, 0),
3159 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 4),/* [3] */
3160 		BTF_MEMBER_ENC(NAME_TBD, 4, BTF_MEMBER_OFFSET(4, 0)),
3161 		BTF_MEMBER_ENC(NAME_TBD, 5, BTF_MEMBER_OFFSET(4, 4)),
3162 		BTF_TYPEDEF_ENC(NAME_TBD, 1),				/* [4] */
3163 		BTF_TYPEDEF_ENC(NAME_TBD, 2),				/* [5] */
3164 		BTF_END_RAW,
3165 	},
3166 	BTF_STR_SEC("\0A\0B\0C\0D\0E"),
3167 	.map_type = BPF_MAP_TYPE_ARRAY,
3168 	.map_name = "struct_type_check_btf",
3169 	.key_size = sizeof(int),
3170 	.value_size = sizeof(int),
3171 	.key_type_id = 1,
3172 	.value_type_id = 1,
3173 	.max_entries = 4,
3174 },
3175 
3176 {
3177 	.descr = "valid union, kind_flag, typedef member, bitfield_size != 0",
3178 	.raw_types = {
3179 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
3180 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),	/* [2] */
3181 		BTF_ENUM_ENC(NAME_TBD, 0),
3182 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 1, 2), 4),	/* [3] */
3183 		BTF_MEMBER_ENC(NAME_TBD, 4, BTF_MEMBER_OFFSET(4, 0)),
3184 		BTF_MEMBER_ENC(NAME_TBD, 5, BTF_MEMBER_OFFSET(4, 0)),
3185 		BTF_TYPEDEF_ENC(NAME_TBD, 1),				/* [4] */
3186 		BTF_TYPEDEF_ENC(NAME_TBD, 2),				/* [5] */
3187 		BTF_END_RAW,
3188 	},
3189 	BTF_STR_SEC("\0A\0B\0C\0D\0E"),
3190 	.map_type = BPF_MAP_TYPE_ARRAY,
3191 	.map_name = "union_type_check_btf",
3192 	.key_size = sizeof(int),
3193 	.value_size = sizeof(int),
3194 	.key_type_id = 1,
3195 	.value_type_id = 1,
3196 	.max_entries = 4,
3197 },
3198 
3199 {
3200 	.descr = "invalid struct, kind_flag, bitfield_size greater than struct size",
3201 	.raw_types = {
3202 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3203 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 4),	/* [2] */
3204 		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(20, 0)),
3205 		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(20, 20)),
3206 		BTF_END_RAW,
3207 	},
3208 	BTF_STR_SEC("\0A\0B"),
3209 	.map_type = BPF_MAP_TYPE_ARRAY,
3210 	.map_name = "struct_type_check_btf",
3211 	.key_size = sizeof(int),
3212 	.value_size = sizeof(int),
3213 	.key_type_id = 1,
3214 	.value_type_id = 1,
3215 	.max_entries = 4,
3216 	.btf_load_err = true,
3217 	.err_str = "Member exceeds struct_size",
3218 },
3219 
3220 {
3221 	.descr = "invalid struct, kind_flag, bitfield base_type int not regular",
3222 	.raw_types = {
3223 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3224 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 20, 4),			/* [2] */
3225 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 4),	/* [3] */
3226 		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(20, 0)),
3227 		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(20, 20)),
3228 		BTF_END_RAW,
3229 	},
3230 	BTF_STR_SEC("\0A\0B"),
3231 	.map_type = BPF_MAP_TYPE_ARRAY,
3232 	.map_name = "struct_type_check_btf",
3233 	.key_size = sizeof(int),
3234 	.value_size = sizeof(int),
3235 	.key_type_id = 1,
3236 	.value_type_id = 1,
3237 	.max_entries = 4,
3238 	.btf_load_err = true,
3239 	.err_str = "Invalid member base type",
3240 },
3241 
3242 {
3243 	.descr = "invalid struct, kind_flag, base_type int not regular",
3244 	.raw_types = {
3245 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3246 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 12, 4),			/* [2] */
3247 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 4),	/* [3] */
3248 		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(8, 0)),
3249 		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(8, 8)),
3250 		BTF_END_RAW,
3251 	},
3252 	BTF_STR_SEC("\0A\0B"),
3253 	.map_type = BPF_MAP_TYPE_ARRAY,
3254 	.map_name = "struct_type_check_btf",
3255 	.key_size = sizeof(int),
3256 	.value_size = sizeof(int),
3257 	.key_type_id = 1,
3258 	.value_type_id = 1,
3259 	.max_entries = 4,
3260 	.btf_load_err = true,
3261 	.err_str = "Invalid member base type",
3262 },
3263 
3264 {
3265 	.descr = "invalid union, kind_flag, bitfield_size greater than struct size",
3266 	.raw_types = {
3267 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
3268 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 1, 2), 2),	/* [2] */
3269 		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(8, 0)),
3270 		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(20, 0)),
3271 		BTF_END_RAW,
3272 	},
3273 	BTF_STR_SEC("\0A\0B"),
3274 	.map_type = BPF_MAP_TYPE_ARRAY,
3275 	.map_name = "union_type_check_btf",
3276 	.key_size = sizeof(int),
3277 	.value_size = sizeof(int),
3278 	.key_type_id = 1,
3279 	.value_type_id = 1,
3280 	.max_entries = 4,
3281 	.btf_load_err = true,
3282 	.err_str = "Member exceeds struct_size",
3283 },
3284 
3285 {
3286 	.descr = "invalid struct, kind_flag, int member, bitfield_size = 0, wrong byte alignment",
3287 	.raw_types = {
3288 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3289 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [2] */
3290 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 12),	/* [3] */
3291 		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 0)),
3292 		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 36)),
3293 		BTF_END_RAW,
3294 	},
3295 	BTF_STR_SEC("\0A\0B"),
3296 	.map_type = BPF_MAP_TYPE_ARRAY,
3297 	.map_name = "struct_type_check_btf",
3298 	.key_size = sizeof(int),
3299 	.value_size = sizeof(int),
3300 	.key_type_id = 1,
3301 	.value_type_id = 1,
3302 	.max_entries = 4,
3303 	.btf_load_err = true,
3304 	.err_str = "Invalid member offset",
3305 },
3306 
3307 {
3308 	.descr = "invalid struct, kind_flag, enum member, bitfield_size = 0, wrong byte alignment",
3309 	.raw_types = {
3310 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3311 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [2] */
3312 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),	/* [2] */
3313 		BTF_ENUM_ENC(NAME_TBD, 0),
3314 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 12),	/* [3] */
3315 		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 0)),
3316 		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 36)),
3317 		BTF_END_RAW,
3318 	},
3319 	BTF_STR_SEC("\0A\0B\0C"),
3320 	.map_type = BPF_MAP_TYPE_ARRAY,
3321 	.map_name = "struct_type_check_btf",
3322 	.key_size = sizeof(int),
3323 	.value_size = sizeof(int),
3324 	.key_type_id = 1,
3325 	.value_type_id = 1,
3326 	.max_entries = 4,
3327 	.btf_load_err = true,
3328 	.err_str = "Invalid member offset",
3329 },
3330 
3331 {
3332 	.descr = "128-bit int",
3333 	.raw_types = {
3334 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3335 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 128, 16),		/* [2] */
3336 		BTF_END_RAW,
3337 	},
3338 	BTF_STR_SEC("\0A"),
3339 	.map_type = BPF_MAP_TYPE_ARRAY,
3340 	.map_name = "int_type_check_btf",
3341 	.key_size = sizeof(int),
3342 	.value_size = sizeof(int),
3343 	.key_type_id = 1,
3344 	.value_type_id = 1,
3345 	.max_entries = 4,
3346 },
3347 
3348 {
3349 	.descr = "struct, 128-bit int member",
3350 	.raw_types = {
3351 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3352 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 128, 16),		/* [2] */
3353 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 16),	/* [3] */
3354 		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3355 		BTF_END_RAW,
3356 	},
3357 	BTF_STR_SEC("\0A"),
3358 	.map_type = BPF_MAP_TYPE_ARRAY,
3359 	.map_name = "struct_type_check_btf",
3360 	.key_size = sizeof(int),
3361 	.value_size = sizeof(int),
3362 	.key_type_id = 1,
3363 	.value_type_id = 1,
3364 	.max_entries = 4,
3365 },
3366 
3367 {
3368 	.descr = "struct, 120-bit int member bitfield",
3369 	.raw_types = {
3370 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3371 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 120, 16),		/* [2] */
3372 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 16),	/* [3] */
3373 		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3374 		BTF_END_RAW,
3375 	},
3376 	BTF_STR_SEC("\0A"),
3377 	.map_type = BPF_MAP_TYPE_ARRAY,
3378 	.map_name = "struct_type_check_btf",
3379 	.key_size = sizeof(int),
3380 	.value_size = sizeof(int),
3381 	.key_type_id = 1,
3382 	.value_type_id = 1,
3383 	.max_entries = 4,
3384 },
3385 
3386 {
3387 	.descr = "struct, kind_flag, 128-bit int member",
3388 	.raw_types = {
3389 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3390 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 128, 16),		/* [2] */
3391 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 1), 16),	/* [3] */
3392 		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 0)),
3393 		BTF_END_RAW,
3394 	},
3395 	BTF_STR_SEC("\0A"),
3396 	.map_type = BPF_MAP_TYPE_ARRAY,
3397 	.map_name = "struct_type_check_btf",
3398 	.key_size = sizeof(int),
3399 	.value_size = sizeof(int),
3400 	.key_type_id = 1,
3401 	.value_type_id = 1,
3402 	.max_entries = 4,
3403 },
3404 
3405 {
3406 	.descr = "struct, kind_flag, 120-bit int member bitfield",
3407 	.raw_types = {
3408 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3409 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 128, 16),		/* [2] */
3410 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 1), 16),	/* [3] */
3411 		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(120, 0)),
3412 		BTF_END_RAW,
3413 	},
3414 	BTF_STR_SEC("\0A"),
3415 	.map_type = BPF_MAP_TYPE_ARRAY,
3416 	.map_name = "struct_type_check_btf",
3417 	.key_size = sizeof(int),
3418 	.value_size = sizeof(int),
3419 	.key_type_id = 1,
3420 	.value_type_id = 1,
3421 	.max_entries = 4,
3422 },
3423 /*
3424  * typedef int arr_t[16];
3425  * struct s {
3426  *	arr_t *a;
3427  * };
3428  */
3429 {
3430 	.descr = "struct->ptr->typedef->array->int size resolution",
3431 	.raw_types = {
3432 		BTF_STRUCT_ENC(NAME_TBD, 1, 8),			/* [1] */
3433 		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3434 		BTF_PTR_ENC(3),					/* [2] */
3435 		BTF_TYPEDEF_ENC(NAME_TBD, 4),			/* [3] */
3436 		BTF_TYPE_ARRAY_ENC(5, 5, 16),			/* [4] */
3437 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [5] */
3438 		BTF_END_RAW,
3439 	},
3440 	BTF_STR_SEC("\0s\0a\0arr_t"),
3441 	.map_type = BPF_MAP_TYPE_ARRAY,
3442 	.map_name = "ptr_mod_chain_size_resolve_map",
3443 	.key_size = sizeof(int),
3444 	.value_size = sizeof(int) * 16,
3445 	.key_type_id = 5 /* int */,
3446 	.value_type_id = 3 /* arr_t */,
3447 	.max_entries = 4,
3448 },
3449 /*
3450  * typedef int arr_t[16][8][4];
3451  * struct s {
3452  *	arr_t *a;
3453  * };
3454  */
3455 {
3456 	.descr = "struct->ptr->typedef->multi-array->int size resolution",
3457 	.raw_types = {
3458 		BTF_STRUCT_ENC(NAME_TBD, 1, 8),			/* [1] */
3459 		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3460 		BTF_PTR_ENC(3),					/* [2] */
3461 		BTF_TYPEDEF_ENC(NAME_TBD, 4),			/* [3] */
3462 		BTF_TYPE_ARRAY_ENC(5, 7, 16),			/* [4] */
3463 		BTF_TYPE_ARRAY_ENC(6, 7, 8),			/* [5] */
3464 		BTF_TYPE_ARRAY_ENC(7, 7, 4),			/* [6] */
3465 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [7] */
3466 		BTF_END_RAW,
3467 	},
3468 	BTF_STR_SEC("\0s\0a\0arr_t"),
3469 	.map_type = BPF_MAP_TYPE_ARRAY,
3470 	.map_name = "multi_arr_size_resolve_map",
3471 	.key_size = sizeof(int),
3472 	.value_size = sizeof(int) * 16 * 8 * 4,
3473 	.key_type_id = 7 /* int */,
3474 	.value_type_id = 3 /* arr_t */,
3475 	.max_entries = 4,
3476 },
3477 /*
3478  * typedef int int_t;
3479  * typedef int_t arr3_t[4];
3480  * typedef arr3_t arr2_t[8];
3481  * typedef arr2_t arr1_t[16];
3482  * struct s {
3483  *	arr1_t *a;
3484  * };
3485  */
3486 {
3487 	.descr = "typedef/multi-arr mix size resolution",
3488 	.raw_types = {
3489 		BTF_STRUCT_ENC(NAME_TBD, 1, 8),			/* [1] */
3490 		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3491 		BTF_PTR_ENC(3),					/* [2] */
3492 		BTF_TYPEDEF_ENC(NAME_TBD, 4),			/* [3] */
3493 		BTF_TYPE_ARRAY_ENC(5, 10, 16),			/* [4] */
3494 		BTF_TYPEDEF_ENC(NAME_TBD, 6),			/* [5] */
3495 		BTF_TYPE_ARRAY_ENC(7, 10, 8),			/* [6] */
3496 		BTF_TYPEDEF_ENC(NAME_TBD, 8),			/* [7] */
3497 		BTF_TYPE_ARRAY_ENC(9, 10, 4),			/* [8] */
3498 		BTF_TYPEDEF_ENC(NAME_TBD, 10),			/* [9] */
3499 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [10] */
3500 		BTF_END_RAW,
3501 	},
3502 	BTF_STR_SEC("\0s\0a\0arr1_t\0arr2_t\0arr3_t\0int_t"),
3503 	.map_type = BPF_MAP_TYPE_ARRAY,
3504 	.map_name = "typedef_arra_mix_size_resolve_map",
3505 	.key_size = sizeof(int),
3506 	.value_size = sizeof(int) * 16 * 8 * 4,
3507 	.key_type_id = 10 /* int */,
3508 	.value_type_id = 3 /* arr_t */,
3509 	.max_entries = 4,
3510 },
3511 /*
3512  * elf .rodata section size 4 and btf .rodata section vlen 0.
3513  */
3514 {
3515 	.descr = "datasec: vlen == 0",
3516 	.raw_types = {
3517 		/* int */
3518 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3519 		/* .rodata section */
3520 		BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 0), 4),
3521 								 /* [2] */
3522 		BTF_END_RAW,
3523 	},
3524 	BTF_STR_SEC("\0.rodata"),
3525 	.map_type = BPF_MAP_TYPE_ARRAY,
3526 	.key_size = sizeof(int),
3527 	.value_size = sizeof(int),
3528 	.key_type_id = 1,
3529 	.value_type_id = 1,
3530 	.max_entries = 1,
3531 },
3532 
3533 {
3534 	.descr = "float test #1, well-formed",
3535 	.raw_types = {
3536 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
3537 								/* [1] */
3538 		BTF_TYPE_FLOAT_ENC(NAME_TBD, 2),		/* [2] */
3539 		BTF_TYPE_FLOAT_ENC(NAME_TBD, 4),		/* [3] */
3540 		BTF_TYPE_FLOAT_ENC(NAME_TBD, 8),		/* [4] */
3541 		BTF_TYPE_FLOAT_ENC(NAME_TBD, 12),		/* [5] */
3542 		BTF_TYPE_FLOAT_ENC(NAME_TBD, 16),		/* [6] */
3543 		BTF_STRUCT_ENC(NAME_TBD, 5, 48),		/* [7] */
3544 		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3545 		BTF_MEMBER_ENC(NAME_TBD, 3, 32),
3546 		BTF_MEMBER_ENC(NAME_TBD, 4, 64),
3547 		BTF_MEMBER_ENC(NAME_TBD, 5, 128),
3548 		BTF_MEMBER_ENC(NAME_TBD, 6, 256),
3549 		BTF_END_RAW,
3550 	},
3551 	BTF_STR_SEC("\0int\0_Float16\0float\0double\0_Float80\0long_double"
3552 		    "\0floats\0a\0b\0c\0d\0e"),
3553 	.map_type = BPF_MAP_TYPE_ARRAY,
3554 	.map_name = "float_type_check_btf",
3555 	.key_size = sizeof(int),
3556 	.value_size = 48,
3557 	.key_type_id = 1,
3558 	.value_type_id = 7,
3559 	.max_entries = 1,
3560 },
3561 {
3562 	.descr = "float test #2, invalid vlen",
3563 	.raw_types = {
3564 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
3565 								/* [1] */
3566 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 1), 4),
3567 								/* [2] */
3568 		BTF_END_RAW,
3569 	},
3570 	BTF_STR_SEC("\0int\0float"),
3571 	.map_type = BPF_MAP_TYPE_ARRAY,
3572 	.map_name = "float_type_check_btf",
3573 	.key_size = sizeof(int),
3574 	.value_size = 4,
3575 	.key_type_id = 1,
3576 	.value_type_id = 2,
3577 	.max_entries = 1,
3578 	.btf_load_err = true,
3579 	.err_str = "vlen != 0",
3580 },
3581 {
3582 	.descr = "float test #3, invalid kind_flag",
3583 	.raw_types = {
3584 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
3585 								/* [1] */
3586 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FLOAT, 1, 0), 4),
3587 								/* [2] */
3588 		BTF_END_RAW,
3589 	},
3590 	BTF_STR_SEC("\0int\0float"),
3591 	.map_type = BPF_MAP_TYPE_ARRAY,
3592 	.map_name = "float_type_check_btf",
3593 	.key_size = sizeof(int),
3594 	.value_size = 4,
3595 	.key_type_id = 1,
3596 	.value_type_id = 2,
3597 	.max_entries = 1,
3598 	.btf_load_err = true,
3599 	.err_str = "Invalid btf_info kind_flag",
3600 },
3601 {
3602 	.descr = "float test #4, member does not fit",
3603 	.raw_types = {
3604 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
3605 								/* [1] */
3606 		BTF_TYPE_FLOAT_ENC(NAME_TBD, 4),		/* [2] */
3607 		BTF_STRUCT_ENC(NAME_TBD, 1, 2),			/* [3] */
3608 		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3609 		BTF_END_RAW,
3610 	},
3611 	BTF_STR_SEC("\0int\0float\0floats\0x"),
3612 	.map_type = BPF_MAP_TYPE_ARRAY,
3613 	.map_name = "float_type_check_btf",
3614 	.key_size = sizeof(int),
3615 	.value_size = 4,
3616 	.key_type_id = 1,
3617 	.value_type_id = 3,
3618 	.max_entries = 1,
3619 	.btf_load_err = true,
3620 	.err_str = "Member exceeds struct_size",
3621 },
3622 {
3623 	.descr = "float test #5, member is not properly aligned",
3624 	.raw_types = {
3625 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
3626 								/* [1] */
3627 		BTF_TYPE_FLOAT_ENC(NAME_TBD, 4),		/* [2] */
3628 		BTF_STRUCT_ENC(NAME_TBD, 1, 8),			/* [3] */
3629 		BTF_MEMBER_ENC(NAME_TBD, 2, 8),
3630 		BTF_END_RAW,
3631 	},
3632 	BTF_STR_SEC("\0int\0float\0floats\0x"),
3633 	.map_type = BPF_MAP_TYPE_ARRAY,
3634 	.map_name = "float_type_check_btf",
3635 	.key_size = sizeof(int),
3636 	.value_size = 4,
3637 	.key_type_id = 1,
3638 	.value_type_id = 3,
3639 	.max_entries = 1,
3640 	.btf_load_err = true,
3641 	.err_str = "Member is not properly aligned",
3642 },
3643 {
3644 	.descr = "float test #6, invalid size",
3645 	.raw_types = {
3646 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
3647 								/* [1] */
3648 		BTF_TYPE_FLOAT_ENC(NAME_TBD, 6),		/* [2] */
3649 		BTF_END_RAW,
3650 	},
3651 	BTF_STR_SEC("\0int\0float"),
3652 	.map_type = BPF_MAP_TYPE_ARRAY,
3653 	.map_name = "float_type_check_btf",
3654 	.key_size = sizeof(int),
3655 	.value_size = 6,
3656 	.key_type_id = 1,
3657 	.value_type_id = 2,
3658 	.max_entries = 1,
3659 	.btf_load_err = true,
3660 	.err_str = "Invalid type_size",
3661 },
3662 
3663 {
3664 	.descr = "decl_tag test #1, struct/member, well-formed",
3665 	.raw_types = {
3666 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3667 		BTF_STRUCT_ENC(0, 2, 8),			/* [2] */
3668 		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
3669 		BTF_MEMBER_ENC(NAME_TBD, 1, 32),
3670 		BTF_DECL_TAG_ENC(NAME_TBD, 2, -1),
3671 		BTF_DECL_TAG_ENC(NAME_TBD, 2, 0),
3672 		BTF_DECL_TAG_ENC(NAME_TBD, 2, 1),
3673 		BTF_END_RAW,
3674 	},
3675 	BTF_STR_SEC("\0m1\0m2\0tag1\0tag2\0tag3"),
3676 	.map_type = BPF_MAP_TYPE_ARRAY,
3677 	.map_name = "tag_type_check_btf",
3678 	.key_size = sizeof(int),
3679 	.value_size = 8,
3680 	.key_type_id = 1,
3681 	.value_type_id = 2,
3682 	.max_entries = 1,
3683 },
3684 {
3685 	.descr = "decl_tag test #2, union/member, well-formed",
3686 	.raw_types = {
3687 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3688 		BTF_UNION_ENC(NAME_TBD, 2, 4),			/* [2] */
3689 		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
3690 		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
3691 		BTF_DECL_TAG_ENC(NAME_TBD, 2, -1),
3692 		BTF_DECL_TAG_ENC(NAME_TBD, 2, 0),
3693 		BTF_DECL_TAG_ENC(NAME_TBD, 2, 1),
3694 		BTF_END_RAW,
3695 	},
3696 	BTF_STR_SEC("\0t\0m1\0m2\0tag1\0tag2\0tag3"),
3697 	.map_type = BPF_MAP_TYPE_ARRAY,
3698 	.map_name = "tag_type_check_btf",
3699 	.key_size = sizeof(int),
3700 	.value_size = 4,
3701 	.key_type_id = 1,
3702 	.value_type_id = 2,
3703 	.max_entries = 1,
3704 },
3705 {
3706 	.descr = "decl_tag test #3, variable, well-formed",
3707 	.raw_types = {
3708 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3709 		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
3710 		BTF_VAR_ENC(NAME_TBD, 1, 1),			/* [3] */
3711 		BTF_DECL_TAG_ENC(NAME_TBD, 2, -1),
3712 		BTF_DECL_TAG_ENC(NAME_TBD, 3, -1),
3713 		BTF_END_RAW,
3714 	},
3715 	BTF_STR_SEC("\0local\0global\0tag1\0tag2"),
3716 	.map_type = BPF_MAP_TYPE_ARRAY,
3717 	.map_name = "tag_type_check_btf",
3718 	.key_size = sizeof(int),
3719 	.value_size = 4,
3720 	.key_type_id = 1,
3721 	.value_type_id = 1,
3722 	.max_entries = 1,
3723 },
3724 {
3725 	.descr = "decl_tag test #4, func/parameter, well-formed",
3726 	.raw_types = {
3727 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3728 		BTF_FUNC_PROTO_ENC(0, 2),			/* [2] */
3729 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3730 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3731 		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
3732 		BTF_DECL_TAG_ENC(NAME_TBD, 3, -1),
3733 		BTF_DECL_TAG_ENC(NAME_TBD, 3, 0),
3734 		BTF_DECL_TAG_ENC(NAME_TBD, 3, 1),
3735 		BTF_END_RAW,
3736 	},
3737 	BTF_STR_SEC("\0arg1\0arg2\0f\0tag1\0tag2\0tag3"),
3738 	.map_type = BPF_MAP_TYPE_ARRAY,
3739 	.map_name = "tag_type_check_btf",
3740 	.key_size = sizeof(int),
3741 	.value_size = 4,
3742 	.key_type_id = 1,
3743 	.value_type_id = 1,
3744 	.max_entries = 1,
3745 },
3746 {
3747 	.descr = "decl_tag test #5, invalid value",
3748 	.raw_types = {
3749 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3750 		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
3751 		BTF_DECL_TAG_ENC(0, 2, -1),
3752 		BTF_END_RAW,
3753 	},
3754 	BTF_STR_SEC("\0local\0tag"),
3755 	.map_type = BPF_MAP_TYPE_ARRAY,
3756 	.map_name = "tag_type_check_btf",
3757 	.key_size = sizeof(int),
3758 	.value_size = 4,
3759 	.key_type_id = 1,
3760 	.value_type_id = 1,
3761 	.max_entries = 1,
3762 	.btf_load_err = true,
3763 	.err_str = "Invalid value",
3764 },
3765 {
3766 	.descr = "decl_tag test #6, invalid target type",
3767 	.raw_types = {
3768 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3769 		BTF_DECL_TAG_ENC(NAME_TBD, 1, -1),
3770 		BTF_END_RAW,
3771 	},
3772 	BTF_STR_SEC("\0tag1"),
3773 	.map_type = BPF_MAP_TYPE_ARRAY,
3774 	.map_name = "tag_type_check_btf",
3775 	.key_size = sizeof(int),
3776 	.value_size = 4,
3777 	.key_type_id = 1,
3778 	.value_type_id = 1,
3779 	.max_entries = 1,
3780 	.btf_load_err = true,
3781 	.err_str = "Invalid type",
3782 },
3783 {
3784 	.descr = "decl_tag test #7, invalid vlen",
3785 	.raw_types = {
3786 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3787 		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
3788 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DECL_TAG, 0, 1), 2), (0),
3789 		BTF_END_RAW,
3790 	},
3791 	BTF_STR_SEC("\0local\0tag1"),
3792 	.map_type = BPF_MAP_TYPE_ARRAY,
3793 	.map_name = "tag_type_check_btf",
3794 	.key_size = sizeof(int),
3795 	.value_size = 4,
3796 	.key_type_id = 1,
3797 	.value_type_id = 1,
3798 	.max_entries = 1,
3799 	.btf_load_err = true,
3800 	.err_str = "vlen != 0",
3801 },
3802 {
3803 	.descr = "decl_tag test #8, invalid kflag",
3804 	.raw_types = {
3805 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3806 		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
3807 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DECL_TAG, 1, 0), 2), (-1),
3808 		BTF_END_RAW,
3809 	},
3810 	BTF_STR_SEC("\0local\0tag1"),
3811 	.map_type = BPF_MAP_TYPE_ARRAY,
3812 	.map_name = "tag_type_check_btf",
3813 	.key_size = sizeof(int),
3814 	.value_size = 4,
3815 	.key_type_id = 1,
3816 	.value_type_id = 1,
3817 	.max_entries = 1,
3818 	.btf_load_err = true,
3819 	.err_str = "Invalid btf_info kind_flag",
3820 },
3821 {
3822 	.descr = "decl_tag test #9, var, invalid component_idx",
3823 	.raw_types = {
3824 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3825 		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
3826 		BTF_DECL_TAG_ENC(NAME_TBD, 2, 0),
3827 		BTF_END_RAW,
3828 	},
3829 	BTF_STR_SEC("\0local\0tag"),
3830 	.map_type = BPF_MAP_TYPE_ARRAY,
3831 	.map_name = "tag_type_check_btf",
3832 	.key_size = sizeof(int),
3833 	.value_size = 4,
3834 	.key_type_id = 1,
3835 	.value_type_id = 1,
3836 	.max_entries = 1,
3837 	.btf_load_err = true,
3838 	.err_str = "Invalid component_idx",
3839 },
3840 {
3841 	.descr = "decl_tag test #10, struct member, invalid component_idx",
3842 	.raw_types = {
3843 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3844 		BTF_STRUCT_ENC(0, 2, 8),			/* [2] */
3845 		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
3846 		BTF_MEMBER_ENC(NAME_TBD, 1, 32),
3847 		BTF_DECL_TAG_ENC(NAME_TBD, 2, 2),
3848 		BTF_END_RAW,
3849 	},
3850 	BTF_STR_SEC("\0m1\0m2\0tag"),
3851 	.map_type = BPF_MAP_TYPE_ARRAY,
3852 	.map_name = "tag_type_check_btf",
3853 	.key_size = sizeof(int),
3854 	.value_size = 8,
3855 	.key_type_id = 1,
3856 	.value_type_id = 2,
3857 	.max_entries = 1,
3858 	.btf_load_err = true,
3859 	.err_str = "Invalid component_idx",
3860 },
3861 {
3862 	.descr = "decl_tag test #11, func parameter, invalid component_idx",
3863 	.raw_types = {
3864 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3865 		BTF_FUNC_PROTO_ENC(0, 2),			/* [2] */
3866 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3867 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3868 		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
3869 		BTF_DECL_TAG_ENC(NAME_TBD, 3, 2),
3870 		BTF_END_RAW,
3871 	},
3872 	BTF_STR_SEC("\0arg1\0arg2\0f\0tag"),
3873 	.map_type = BPF_MAP_TYPE_ARRAY,
3874 	.map_name = "tag_type_check_btf",
3875 	.key_size = sizeof(int),
3876 	.value_size = 4,
3877 	.key_type_id = 1,
3878 	.value_type_id = 1,
3879 	.max_entries = 1,
3880 	.btf_load_err = true,
3881 	.err_str = "Invalid component_idx",
3882 },
3883 {
3884 	.descr = "decl_tag test #12, < -1 component_idx",
3885 	.raw_types = {
3886 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3887 		BTF_FUNC_PROTO_ENC(0, 2),			/* [2] */
3888 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3889 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3890 		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
3891 		BTF_DECL_TAG_ENC(NAME_TBD, 3, -2),
3892 		BTF_END_RAW,
3893 	},
3894 	BTF_STR_SEC("\0arg1\0arg2\0f\0tag"),
3895 	.map_type = BPF_MAP_TYPE_ARRAY,
3896 	.map_name = "tag_type_check_btf",
3897 	.key_size = sizeof(int),
3898 	.value_size = 4,
3899 	.key_type_id = 1,
3900 	.value_type_id = 1,
3901 	.max_entries = 1,
3902 	.btf_load_err = true,
3903 	.err_str = "Invalid component_idx",
3904 },
3905 {
3906 	.descr = "decl_tag test #13, typedef, well-formed",
3907 	.raw_types = {
3908 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3909 		BTF_TYPEDEF_ENC(NAME_TBD, 1),			/* [2] */
3910 		BTF_DECL_TAG_ENC(NAME_TBD, 2, -1),
3911 		BTF_END_RAW,
3912 	},
3913 	BTF_STR_SEC("\0t\0tag"),
3914 	.map_type = BPF_MAP_TYPE_ARRAY,
3915 	.map_name = "tag_type_check_btf",
3916 	.key_size = sizeof(int),
3917 	.value_size = 4,
3918 	.key_type_id = 1,
3919 	.value_type_id = 1,
3920 	.max_entries = 1,
3921 },
3922 {
3923 	.descr = "decl_tag test #14, typedef, invalid component_idx",
3924 	.raw_types = {
3925 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3926 		BTF_TYPEDEF_ENC(NAME_TBD, 1),			/* [2] */
3927 		BTF_DECL_TAG_ENC(NAME_TBD, 2, 0),
3928 		BTF_END_RAW,
3929 	},
3930 	BTF_STR_SEC("\0local\0tag"),
3931 	.map_type = BPF_MAP_TYPE_ARRAY,
3932 	.map_name = "tag_type_check_btf",
3933 	.key_size = sizeof(int),
3934 	.value_size = 4,
3935 	.key_type_id = 1,
3936 	.value_type_id = 1,
3937 	.max_entries = 1,
3938 	.btf_load_err = true,
3939 	.err_str = "Invalid component_idx",
3940 },
3941 {
3942 	.descr = "type_tag test #1",
3943 	.raw_types = {
3944 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3945 		BTF_TYPE_TAG_ENC(NAME_TBD, 1),			/* [2] */
3946 		BTF_PTR_ENC(2),					/* [3] */
3947 		BTF_END_RAW,
3948 	},
3949 	BTF_STR_SEC("\0tag"),
3950 	.map_type = BPF_MAP_TYPE_ARRAY,
3951 	.map_name = "tag_type_check_btf",
3952 	.key_size = sizeof(int),
3953 	.value_size = 4,
3954 	.key_type_id = 1,
3955 	.value_type_id = 1,
3956 	.max_entries = 1,
3957 },
3958 
3959 }; /* struct btf_raw_test raw_tests[] */
3960 
3961 static const char *get_next_str(const char *start, const char *end)
3962 {
3963 	return start < end - 1 ? start + 1 : NULL;
3964 }
3965 
3966 static int get_raw_sec_size(const __u32 *raw_types)
3967 {
3968 	int i;
3969 
3970 	for (i = MAX_NR_RAW_U32 - 1;
3971 	     i >= 0 && raw_types[i] != BTF_END_RAW;
3972 	     i--)
3973 		;
3974 
3975 	return i < 0 ? i : i * sizeof(raw_types[0]);
3976 }
3977 
3978 static void *btf_raw_create(const struct btf_header *hdr,
3979 			    const __u32 *raw_types,
3980 			    const char *str,
3981 			    unsigned int str_sec_size,
3982 			    unsigned int *btf_size,
3983 			    const char **ret_next_str)
3984 {
3985 	const char *next_str = str, *end_str = str + str_sec_size;
3986 	const char **strs_idx = NULL, **tmp_strs_idx;
3987 	int strs_cap = 0, strs_cnt = 0, next_str_idx = 0;
3988 	unsigned int size_needed, offset;
3989 	struct btf_header *ret_hdr;
3990 	int i, type_sec_size, err = 0;
3991 	uint32_t *ret_types;
3992 	void *raw_btf = NULL;
3993 
3994 	type_sec_size = get_raw_sec_size(raw_types);
3995 	if (CHECK(type_sec_size < 0, "Cannot get nr_raw_types"))
3996 		return NULL;
3997 
3998 	size_needed = sizeof(*hdr) + type_sec_size + str_sec_size;
3999 	raw_btf = malloc(size_needed);
4000 	if (CHECK(!raw_btf, "Cannot allocate memory for raw_btf"))
4001 		return NULL;
4002 
4003 	/* Copy header */
4004 	memcpy(raw_btf, hdr, sizeof(*hdr));
4005 	offset = sizeof(*hdr);
4006 
4007 	/* Index strings */
4008 	while ((next_str = get_next_str(next_str, end_str))) {
4009 		if (strs_cnt == strs_cap) {
4010 			strs_cap += max(16, strs_cap / 2);
4011 			tmp_strs_idx = realloc(strs_idx,
4012 					       sizeof(*strs_idx) * strs_cap);
4013 			if (CHECK(!tmp_strs_idx,
4014 				  "Cannot allocate memory for strs_idx")) {
4015 				err = -1;
4016 				goto done;
4017 			}
4018 			strs_idx = tmp_strs_idx;
4019 		}
4020 		strs_idx[strs_cnt++] = next_str;
4021 		next_str += strlen(next_str);
4022 	}
4023 
4024 	/* Copy type section */
4025 	ret_types = raw_btf + offset;
4026 	for (i = 0; i < type_sec_size / sizeof(raw_types[0]); i++) {
4027 		if (raw_types[i] == NAME_TBD) {
4028 			if (CHECK(next_str_idx == strs_cnt,
4029 				  "Error in getting next_str #%d",
4030 				  next_str_idx)) {
4031 				err = -1;
4032 				goto done;
4033 			}
4034 			ret_types[i] = strs_idx[next_str_idx++] - str;
4035 		} else if (IS_NAME_NTH(raw_types[i])) {
4036 			int idx = GET_NAME_NTH_IDX(raw_types[i]);
4037 
4038 			if (CHECK(idx <= 0 || idx > strs_cnt,
4039 				  "Error getting string #%d, strs_cnt:%d",
4040 				  idx, strs_cnt)) {
4041 				err = -1;
4042 				goto done;
4043 			}
4044 			ret_types[i] = strs_idx[idx-1] - str;
4045 		} else {
4046 			ret_types[i] = raw_types[i];
4047 		}
4048 	}
4049 	offset += type_sec_size;
4050 
4051 	/* Copy string section */
4052 	memcpy(raw_btf + offset, str, str_sec_size);
4053 
4054 	ret_hdr = (struct btf_header *)raw_btf;
4055 	ret_hdr->type_len = type_sec_size;
4056 	ret_hdr->str_off = type_sec_size;
4057 	ret_hdr->str_len = str_sec_size;
4058 
4059 	*btf_size = size_needed;
4060 	if (ret_next_str)
4061 		*ret_next_str =
4062 			next_str_idx < strs_cnt ? strs_idx[next_str_idx] : NULL;
4063 
4064 done:
4065 	free(strs_idx);
4066 	if (err) {
4067 		free(raw_btf);
4068 		return NULL;
4069 	}
4070 	return raw_btf;
4071 }
4072 
4073 static int load_raw_btf(const void *raw_data, size_t raw_size)
4074 {
4075 	LIBBPF_OPTS(bpf_btf_load_opts, opts);
4076 	int btf_fd;
4077 
4078 	if (always_log) {
4079 		opts.log_buf = btf_log_buf,
4080 		opts.log_size = BTF_LOG_BUF_SIZE,
4081 		opts.log_level = 1;
4082 	}
4083 
4084 	btf_fd = bpf_btf_load(raw_data, raw_size, &opts);
4085 	if (btf_fd < 0 && !always_log) {
4086 		opts.log_buf = btf_log_buf,
4087 		opts.log_size = BTF_LOG_BUF_SIZE,
4088 		opts.log_level = 1;
4089 		btf_fd = bpf_btf_load(raw_data, raw_size, &opts);
4090 	}
4091 
4092 	return btf_fd;
4093 }
4094 
4095 static void do_test_raw(unsigned int test_num)
4096 {
4097 	struct btf_raw_test *test = &raw_tests[test_num - 1];
4098 	LIBBPF_OPTS(bpf_map_create_opts, opts);
4099 	int map_fd = -1, btf_fd = -1;
4100 	unsigned int raw_btf_size;
4101 	struct btf_header *hdr;
4102 	void *raw_btf;
4103 	int err;
4104 
4105 	if (!test__start_subtest(test->descr))
4106 		return;
4107 
4108 	raw_btf = btf_raw_create(&hdr_tmpl,
4109 				 test->raw_types,
4110 				 test->str_sec,
4111 				 test->str_sec_size,
4112 				 &raw_btf_size, NULL);
4113 	if (!raw_btf)
4114 		return;
4115 
4116 	hdr = raw_btf;
4117 
4118 	hdr->hdr_len = (int)hdr->hdr_len + test->hdr_len_delta;
4119 	hdr->type_off = (int)hdr->type_off + test->type_off_delta;
4120 	hdr->str_off = (int)hdr->str_off + test->str_off_delta;
4121 	hdr->str_len = (int)hdr->str_len + test->str_len_delta;
4122 
4123 	*btf_log_buf = '\0';
4124 	btf_fd = load_raw_btf(raw_btf, raw_btf_size);
4125 	free(raw_btf);
4126 
4127 	err = ((btf_fd < 0) != test->btf_load_err);
4128 	if (CHECK(err, "btf_fd:%d test->btf_load_err:%u",
4129 		  btf_fd, test->btf_load_err) ||
4130 	    CHECK(test->err_str && !strstr(btf_log_buf, test->err_str),
4131 		  "expected err_str:%s\n", test->err_str)) {
4132 		err = -1;
4133 		goto done;
4134 	}
4135 
4136 	if (err || btf_fd < 0)
4137 		goto done;
4138 
4139 	opts.btf_fd = btf_fd;
4140 	opts.btf_key_type_id = test->key_type_id;
4141 	opts.btf_value_type_id = test->value_type_id;
4142 	map_fd = bpf_map_create(test->map_type, test->map_name,
4143 				test->key_size, test->value_size, test->max_entries, &opts);
4144 
4145 	err = ((map_fd < 0) != test->map_create_err);
4146 	CHECK(err, "map_fd:%d test->map_create_err:%u",
4147 	      map_fd, test->map_create_err);
4148 
4149 done:
4150 	if (*btf_log_buf && (err || always_log))
4151 		fprintf(stderr, "\n%s", btf_log_buf);
4152 	if (btf_fd >= 0)
4153 		close(btf_fd);
4154 	if (map_fd >= 0)
4155 		close(map_fd);
4156 }
4157 
4158 struct btf_get_info_test {
4159 	const char *descr;
4160 	const char *str_sec;
4161 	__u32 raw_types[MAX_NR_RAW_U32];
4162 	__u32 str_sec_size;
4163 	int btf_size_delta;
4164 	int (*special_test)(unsigned int test_num);
4165 };
4166 
4167 static int test_big_btf_info(unsigned int test_num);
4168 static int test_btf_id(unsigned int test_num);
4169 
4170 const struct btf_get_info_test get_info_tests[] = {
4171 {
4172 	.descr = "== raw_btf_size+1",
4173 	.raw_types = {
4174 		/* int */				/* [1] */
4175 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
4176 		BTF_END_RAW,
4177 	},
4178 	.str_sec = "",
4179 	.str_sec_size = sizeof(""),
4180 	.btf_size_delta = 1,
4181 },
4182 {
4183 	.descr = "== raw_btf_size-3",
4184 	.raw_types = {
4185 		/* int */				/* [1] */
4186 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
4187 		BTF_END_RAW,
4188 	},
4189 	.str_sec = "",
4190 	.str_sec_size = sizeof(""),
4191 	.btf_size_delta = -3,
4192 },
4193 {
4194 	.descr = "Large bpf_btf_info",
4195 	.raw_types = {
4196 		/* int */				/* [1] */
4197 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
4198 		BTF_END_RAW,
4199 	},
4200 	.str_sec = "",
4201 	.str_sec_size = sizeof(""),
4202 	.special_test = test_big_btf_info,
4203 },
4204 {
4205 	.descr = "BTF ID",
4206 	.raw_types = {
4207 		/* int */				/* [1] */
4208 		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
4209 		/* unsigned int */			/* [2] */
4210 		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),
4211 		BTF_END_RAW,
4212 	},
4213 	.str_sec = "",
4214 	.str_sec_size = sizeof(""),
4215 	.special_test = test_btf_id,
4216 },
4217 };
4218 
4219 static int test_big_btf_info(unsigned int test_num)
4220 {
4221 	const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
4222 	uint8_t *raw_btf = NULL, *user_btf = NULL;
4223 	unsigned int raw_btf_size;
4224 	struct {
4225 		struct bpf_btf_info info;
4226 		uint64_t garbage;
4227 	} info_garbage;
4228 	struct bpf_btf_info *info;
4229 	int btf_fd = -1, err;
4230 	uint32_t info_len;
4231 
4232 	raw_btf = btf_raw_create(&hdr_tmpl,
4233 				 test->raw_types,
4234 				 test->str_sec,
4235 				 test->str_sec_size,
4236 				 &raw_btf_size, NULL);
4237 
4238 	if (!raw_btf)
4239 		return -1;
4240 
4241 	*btf_log_buf = '\0';
4242 
4243 	user_btf = malloc(raw_btf_size);
4244 	if (CHECK(!user_btf, "!user_btf")) {
4245 		err = -1;
4246 		goto done;
4247 	}
4248 
4249 	btf_fd = load_raw_btf(raw_btf, raw_btf_size);
4250 	if (CHECK(btf_fd < 0, "errno:%d", errno)) {
4251 		err = -1;
4252 		goto done;
4253 	}
4254 
4255 	/*
4256 	 * GET_INFO should error out if the userspace info
4257 	 * has non zero tailing bytes.
4258 	 */
4259 	info = &info_garbage.info;
4260 	memset(info, 0, sizeof(*info));
4261 	info_garbage.garbage = 0xdeadbeef;
4262 	info_len = sizeof(info_garbage);
4263 	info->btf = ptr_to_u64(user_btf);
4264 	info->btf_size = raw_btf_size;
4265 
4266 	err = bpf_obj_get_info_by_fd(btf_fd, info, &info_len);
4267 	if (CHECK(!err, "!err")) {
4268 		err = -1;
4269 		goto done;
4270 	}
4271 
4272 	/*
4273 	 * GET_INFO should succeed even info_len is larger than
4274 	 * the kernel supported as long as tailing bytes are zero.
4275 	 * The kernel supported info len should also be returned
4276 	 * to userspace.
4277 	 */
4278 	info_garbage.garbage = 0;
4279 	err = bpf_obj_get_info_by_fd(btf_fd, info, &info_len);
4280 	if (CHECK(err || info_len != sizeof(*info),
4281 		  "err:%d errno:%d info_len:%u sizeof(*info):%zu",
4282 		  err, errno, info_len, sizeof(*info))) {
4283 		err = -1;
4284 		goto done;
4285 	}
4286 
4287 	fprintf(stderr, "OK");
4288 
4289 done:
4290 	if (*btf_log_buf && (err || always_log))
4291 		fprintf(stderr, "\n%s", btf_log_buf);
4292 
4293 	free(raw_btf);
4294 	free(user_btf);
4295 
4296 	if (btf_fd >= 0)
4297 		close(btf_fd);
4298 
4299 	return err;
4300 }
4301 
4302 static int test_btf_id(unsigned int test_num)
4303 {
4304 	const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
4305 	LIBBPF_OPTS(bpf_map_create_opts, opts);
4306 	uint8_t *raw_btf = NULL, *user_btf[2] = {};
4307 	int btf_fd[2] = {-1, -1}, map_fd = -1;
4308 	struct bpf_map_info map_info = {};
4309 	struct bpf_btf_info info[2] = {};
4310 	unsigned int raw_btf_size;
4311 	uint32_t info_len;
4312 	int err, i, ret;
4313 
4314 	raw_btf = btf_raw_create(&hdr_tmpl,
4315 				 test->raw_types,
4316 				 test->str_sec,
4317 				 test->str_sec_size,
4318 				 &raw_btf_size, NULL);
4319 
4320 	if (!raw_btf)
4321 		return -1;
4322 
4323 	*btf_log_buf = '\0';
4324 
4325 	for (i = 0; i < 2; i++) {
4326 		user_btf[i] = malloc(raw_btf_size);
4327 		if (CHECK(!user_btf[i], "!user_btf[%d]", i)) {
4328 			err = -1;
4329 			goto done;
4330 		}
4331 		info[i].btf = ptr_to_u64(user_btf[i]);
4332 		info[i].btf_size = raw_btf_size;
4333 	}
4334 
4335 	btf_fd[0] = load_raw_btf(raw_btf, raw_btf_size);
4336 	if (CHECK(btf_fd[0] < 0, "errno:%d", errno)) {
4337 		err = -1;
4338 		goto done;
4339 	}
4340 
4341 	/* Test BPF_OBJ_GET_INFO_BY_ID on btf_id */
4342 	info_len = sizeof(info[0]);
4343 	err = bpf_obj_get_info_by_fd(btf_fd[0], &info[0], &info_len);
4344 	if (CHECK(err, "errno:%d", errno)) {
4345 		err = -1;
4346 		goto done;
4347 	}
4348 
4349 	btf_fd[1] = bpf_btf_get_fd_by_id(info[0].id);
4350 	if (CHECK(btf_fd[1] < 0, "errno:%d", errno)) {
4351 		err = -1;
4352 		goto done;
4353 	}
4354 
4355 	ret = 0;
4356 	err = bpf_obj_get_info_by_fd(btf_fd[1], &info[1], &info_len);
4357 	if (CHECK(err || info[0].id != info[1].id ||
4358 		  info[0].btf_size != info[1].btf_size ||
4359 		  (ret = memcmp(user_btf[0], user_btf[1], info[0].btf_size)),
4360 		  "err:%d errno:%d id0:%u id1:%u btf_size0:%u btf_size1:%u memcmp:%d",
4361 		  err, errno, info[0].id, info[1].id,
4362 		  info[0].btf_size, info[1].btf_size, ret)) {
4363 		err = -1;
4364 		goto done;
4365 	}
4366 
4367 	/* Test btf members in struct bpf_map_info */
4368 	opts.btf_fd = btf_fd[0];
4369 	opts.btf_key_type_id = 1;
4370 	opts.btf_value_type_id = 2;
4371 	map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "test_btf_id",
4372 				sizeof(int), sizeof(int), 4, &opts);
4373 	if (CHECK(map_fd < 0, "errno:%d", errno)) {
4374 		err = -1;
4375 		goto done;
4376 	}
4377 
4378 	info_len = sizeof(map_info);
4379 	err = bpf_obj_get_info_by_fd(map_fd, &map_info, &info_len);
4380 	if (CHECK(err || map_info.btf_id != info[0].id ||
4381 		  map_info.btf_key_type_id != 1 || map_info.btf_value_type_id != 2,
4382 		  "err:%d errno:%d info.id:%u btf_id:%u btf_key_type_id:%u btf_value_type_id:%u",
4383 		  err, errno, info[0].id, map_info.btf_id, map_info.btf_key_type_id,
4384 		  map_info.btf_value_type_id)) {
4385 		err = -1;
4386 		goto done;
4387 	}
4388 
4389 	for (i = 0; i < 2; i++) {
4390 		close(btf_fd[i]);
4391 		btf_fd[i] = -1;
4392 	}
4393 
4394 	/* Test BTF ID is removed from the kernel */
4395 	btf_fd[0] = bpf_btf_get_fd_by_id(map_info.btf_id);
4396 	if (CHECK(btf_fd[0] < 0, "errno:%d", errno)) {
4397 		err = -1;
4398 		goto done;
4399 	}
4400 	close(btf_fd[0]);
4401 	btf_fd[0] = -1;
4402 
4403 	/* The map holds the last ref to BTF and its btf_id */
4404 	close(map_fd);
4405 	map_fd = -1;
4406 	btf_fd[0] = bpf_btf_get_fd_by_id(map_info.btf_id);
4407 	if (CHECK(btf_fd[0] >= 0, "BTF lingers")) {
4408 		err = -1;
4409 		goto done;
4410 	}
4411 
4412 	fprintf(stderr, "OK");
4413 
4414 done:
4415 	if (*btf_log_buf && (err || always_log))
4416 		fprintf(stderr, "\n%s", btf_log_buf);
4417 
4418 	free(raw_btf);
4419 	if (map_fd >= 0)
4420 		close(map_fd);
4421 	for (i = 0; i < 2; i++) {
4422 		free(user_btf[i]);
4423 		if (btf_fd[i] >= 0)
4424 			close(btf_fd[i]);
4425 	}
4426 
4427 	return err;
4428 }
4429 
4430 static void do_test_get_info(unsigned int test_num)
4431 {
4432 	const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
4433 	unsigned int raw_btf_size, user_btf_size, expected_nbytes;
4434 	uint8_t *raw_btf = NULL, *user_btf = NULL;
4435 	struct bpf_btf_info info = {};
4436 	int btf_fd = -1, err, ret;
4437 	uint32_t info_len;
4438 
4439 	if (!test__start_subtest(test->descr))
4440 		return;
4441 
4442 	if (test->special_test) {
4443 		err = test->special_test(test_num);
4444 		if (CHECK(err, "failed: %d\n", err))
4445 			return;
4446 	}
4447 
4448 	raw_btf = btf_raw_create(&hdr_tmpl,
4449 				 test->raw_types,
4450 				 test->str_sec,
4451 				 test->str_sec_size,
4452 				 &raw_btf_size, NULL);
4453 
4454 	if (!raw_btf)
4455 		return;
4456 
4457 	*btf_log_buf = '\0';
4458 
4459 	user_btf = malloc(raw_btf_size);
4460 	if (CHECK(!user_btf, "!user_btf")) {
4461 		err = -1;
4462 		goto done;
4463 	}
4464 
4465 	btf_fd = load_raw_btf(raw_btf, raw_btf_size);
4466 	if (CHECK(btf_fd <= 0, "errno:%d", errno)) {
4467 		err = -1;
4468 		goto done;
4469 	}
4470 
4471 	user_btf_size = (int)raw_btf_size + test->btf_size_delta;
4472 	expected_nbytes = min(raw_btf_size, user_btf_size);
4473 	if (raw_btf_size > expected_nbytes)
4474 		memset(user_btf + expected_nbytes, 0xff,
4475 		       raw_btf_size - expected_nbytes);
4476 
4477 	info_len = sizeof(info);
4478 	info.btf = ptr_to_u64(user_btf);
4479 	info.btf_size = user_btf_size;
4480 
4481 	ret = 0;
4482 	err = bpf_obj_get_info_by_fd(btf_fd, &info, &info_len);
4483 	if (CHECK(err || !info.id || info_len != sizeof(info) ||
4484 		  info.btf_size != raw_btf_size ||
4485 		  (ret = memcmp(raw_btf, user_btf, expected_nbytes)),
4486 		  "err:%d errno:%d info.id:%u info_len:%u sizeof(info):%zu raw_btf_size:%u info.btf_size:%u expected_nbytes:%u memcmp:%d",
4487 		  err, errno, info.id, info_len, sizeof(info),
4488 		  raw_btf_size, info.btf_size, expected_nbytes, ret)) {
4489 		err = -1;
4490 		goto done;
4491 	}
4492 
4493 	while (expected_nbytes < raw_btf_size) {
4494 		fprintf(stderr, "%u...", expected_nbytes);
4495 		if (CHECK(user_btf[expected_nbytes++] != 0xff,
4496 			  "user_btf[%u]:%x != 0xff", expected_nbytes - 1,
4497 			  user_btf[expected_nbytes - 1])) {
4498 			err = -1;
4499 			goto done;
4500 		}
4501 	}
4502 
4503 	fprintf(stderr, "OK");
4504 
4505 done:
4506 	if (*btf_log_buf && (err || always_log))
4507 		fprintf(stderr, "\n%s", btf_log_buf);
4508 
4509 	free(raw_btf);
4510 	free(user_btf);
4511 
4512 	if (btf_fd >= 0)
4513 		close(btf_fd);
4514 }
4515 
4516 struct btf_file_test {
4517 	const char *file;
4518 	bool btf_kv_notfound;
4519 };
4520 
4521 static struct btf_file_test file_tests[] = {
4522 	{ .file = "test_btf_haskv.o", },
4523 	{ .file = "test_btf_newkv.o", },
4524 	{ .file = "test_btf_nokv.o", .btf_kv_notfound = true, },
4525 };
4526 
4527 static void do_test_file(unsigned int test_num)
4528 {
4529 	const struct btf_file_test *test = &file_tests[test_num - 1];
4530 	const char *expected_fnames[] = {"_dummy_tracepoint",
4531 					 "test_long_fname_1",
4532 					 "test_long_fname_2"};
4533 	struct btf_ext *btf_ext = NULL;
4534 	struct bpf_prog_info info = {};
4535 	struct bpf_object *obj = NULL;
4536 	struct bpf_func_info *finfo;
4537 	struct bpf_program *prog;
4538 	__u32 info_len, rec_size;
4539 	bool has_btf_ext = false;
4540 	struct btf *btf = NULL;
4541 	void *func_info = NULL;
4542 	struct bpf_map *map;
4543 	int i, err, prog_fd;
4544 
4545 	if (!test__start_subtest(test->file))
4546 		return;
4547 
4548 	btf = btf__parse_elf(test->file, &btf_ext);
4549 	err = libbpf_get_error(btf);
4550 	if (err) {
4551 		if (err == -ENOENT) {
4552 			printf("%s:SKIP: No ELF %s found", __func__, BTF_ELF_SEC);
4553 			test__skip();
4554 			return;
4555 		}
4556 		return;
4557 	}
4558 	btf__free(btf);
4559 
4560 	has_btf_ext = btf_ext != NULL;
4561 	btf_ext__free(btf_ext);
4562 
4563 	/* temporary disable LIBBPF_STRICT_MAP_DEFINITIONS to test legacy maps */
4564 	libbpf_set_strict_mode((__LIBBPF_STRICT_LAST - 1) & ~LIBBPF_STRICT_MAP_DEFINITIONS);
4565 	obj = bpf_object__open(test->file);
4566 	err = libbpf_get_error(obj);
4567 	if (CHECK(err, "obj: %d", err))
4568 		return;
4569 
4570 	prog = bpf_object__next_program(obj, NULL);
4571 	if (CHECK(!prog, "Cannot find bpf_prog")) {
4572 		err = -1;
4573 		goto done;
4574 	}
4575 
4576 	bpf_program__set_type(prog, BPF_PROG_TYPE_TRACEPOINT);
4577 	err = bpf_object__load(obj);
4578 	if (CHECK(err < 0, "bpf_object__load: %d", err))
4579 		goto done;
4580 	prog_fd = bpf_program__fd(prog);
4581 
4582 	map = bpf_object__find_map_by_name(obj, "btf_map");
4583 	if (CHECK(!map, "btf_map not found")) {
4584 		err = -1;
4585 		goto done;
4586 	}
4587 
4588 	err = (bpf_map__btf_key_type_id(map) == 0 || bpf_map__btf_value_type_id(map) == 0)
4589 		!= test->btf_kv_notfound;
4590 	if (CHECK(err, "btf_key_type_id:%u btf_value_type_id:%u test->btf_kv_notfound:%u",
4591 		  bpf_map__btf_key_type_id(map), bpf_map__btf_value_type_id(map),
4592 		  test->btf_kv_notfound))
4593 		goto done;
4594 
4595 	if (!has_btf_ext)
4596 		goto skip;
4597 
4598 	/* get necessary program info */
4599 	info_len = sizeof(struct bpf_prog_info);
4600 	err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
4601 
4602 	if (CHECK(err < 0, "invalid get info (1st) errno:%d", errno)) {
4603 		fprintf(stderr, "%s\n", btf_log_buf);
4604 		err = -1;
4605 		goto done;
4606 	}
4607 	if (CHECK(info.nr_func_info != 3,
4608 		  "incorrect info.nr_func_info (1st) %d",
4609 		  info.nr_func_info)) {
4610 		err = -1;
4611 		goto done;
4612 	}
4613 	rec_size = info.func_info_rec_size;
4614 	if (CHECK(rec_size != sizeof(struct bpf_func_info),
4615 		  "incorrect info.func_info_rec_size (1st) %d\n", rec_size)) {
4616 		err = -1;
4617 		goto done;
4618 	}
4619 
4620 	func_info = malloc(info.nr_func_info * rec_size);
4621 	if (CHECK(!func_info, "out of memory")) {
4622 		err = -1;
4623 		goto done;
4624 	}
4625 
4626 	/* reset info to only retrieve func_info related data */
4627 	memset(&info, 0, sizeof(info));
4628 	info.nr_func_info = 3;
4629 	info.func_info_rec_size = rec_size;
4630 	info.func_info = ptr_to_u64(func_info);
4631 
4632 	err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
4633 
4634 	if (CHECK(err < 0, "invalid get info (2nd) errno:%d", errno)) {
4635 		fprintf(stderr, "%s\n", btf_log_buf);
4636 		err = -1;
4637 		goto done;
4638 	}
4639 	if (CHECK(info.nr_func_info != 3,
4640 		  "incorrect info.nr_func_info (2nd) %d",
4641 		  info.nr_func_info)) {
4642 		err = -1;
4643 		goto done;
4644 	}
4645 	if (CHECK(info.func_info_rec_size != rec_size,
4646 		  "incorrect info.func_info_rec_size (2nd) %d",
4647 		  info.func_info_rec_size)) {
4648 		err = -1;
4649 		goto done;
4650 	}
4651 
4652 	btf = btf__load_from_kernel_by_id(info.btf_id);
4653 	err = libbpf_get_error(btf);
4654 	if (CHECK(err, "cannot get btf from kernel, err: %d", err))
4655 		goto done;
4656 
4657 	/* check three functions */
4658 	finfo = func_info;
4659 	for (i = 0; i < 3; i++) {
4660 		const struct btf_type *t;
4661 		const char *fname;
4662 
4663 		t = btf__type_by_id(btf, finfo->type_id);
4664 		if (CHECK(!t, "btf__type_by_id failure: id %u",
4665 			  finfo->type_id)) {
4666 			err = -1;
4667 			goto done;
4668 		}
4669 
4670 		fname = btf__name_by_offset(btf, t->name_off);
4671 		err = strcmp(fname, expected_fnames[i]);
4672 		/* for the second and third functions in .text section,
4673 		 * the compiler may order them either way.
4674 		 */
4675 		if (i && err)
4676 			err = strcmp(fname, expected_fnames[3 - i]);
4677 		if (CHECK(err, "incorrect fname %s", fname ? : "")) {
4678 			err = -1;
4679 			goto done;
4680 		}
4681 
4682 		finfo = (void *)finfo + rec_size;
4683 	}
4684 
4685 skip:
4686 	fprintf(stderr, "OK");
4687 
4688 done:
4689 	libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
4690 
4691 	btf__free(btf);
4692 	free(func_info);
4693 	bpf_object__close(obj);
4694 }
4695 
4696 const char *pprint_enum_str[] = {
4697 	"ENUM_ZERO",
4698 	"ENUM_ONE",
4699 	"ENUM_TWO",
4700 	"ENUM_THREE",
4701 };
4702 
4703 struct pprint_mapv {
4704 	uint32_t ui32;
4705 	uint16_t ui16;
4706 	/* 2 bytes hole */
4707 	int32_t si32;
4708 	uint32_t unused_bits2a:2,
4709 		bits28:28,
4710 		unused_bits2b:2;
4711 	union {
4712 		uint64_t ui64;
4713 		uint8_t ui8a[8];
4714 	};
4715 	enum {
4716 		ENUM_ZERO,
4717 		ENUM_ONE,
4718 		ENUM_TWO,
4719 		ENUM_THREE,
4720 	} aenum;
4721 	uint32_t ui32b;
4722 	uint32_t bits2c:2;
4723 	uint8_t si8_4[2][2];
4724 };
4725 
4726 #ifdef __SIZEOF_INT128__
4727 struct pprint_mapv_int128 {
4728 	__int128 si128a;
4729 	__int128 si128b;
4730 	unsigned __int128 bits3:3;
4731 	unsigned __int128 bits80:80;
4732 	unsigned __int128 ui128;
4733 };
4734 #endif
4735 
4736 static struct btf_raw_test pprint_test_template[] = {
4737 {
4738 	.raw_types = {
4739 		/* unsighed char */			/* [1] */
4740 		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 8, 1),
4741 		/* unsigned short */			/* [2] */
4742 		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 16, 2),
4743 		/* unsigned int */			/* [3] */
4744 		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),
4745 		/* int */				/* [4] */
4746 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
4747 		/* unsigned long long */		/* [5] */
4748 		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 64, 8),
4749 		/* 2 bits */				/* [6] */
4750 		BTF_TYPE_INT_ENC(0, 0, 0, 2, 2),
4751 		/* 28 bits */				/* [7] */
4752 		BTF_TYPE_INT_ENC(0, 0, 0, 28, 4),
4753 		/* uint8_t[8] */			/* [8] */
4754 		BTF_TYPE_ARRAY_ENC(9, 1, 8),
4755 		/* typedef unsigned char uint8_t */	/* [9] */
4756 		BTF_TYPEDEF_ENC(NAME_TBD, 1),
4757 		/* typedef unsigned short uint16_t */	/* [10] */
4758 		BTF_TYPEDEF_ENC(NAME_TBD, 2),
4759 		/* typedef unsigned int uint32_t */	/* [11] */
4760 		BTF_TYPEDEF_ENC(NAME_TBD, 3),
4761 		/* typedef int int32_t */		/* [12] */
4762 		BTF_TYPEDEF_ENC(NAME_TBD, 4),
4763 		/* typedef unsigned long long uint64_t *//* [13] */
4764 		BTF_TYPEDEF_ENC(NAME_TBD, 5),
4765 		/* union (anon) */			/* [14] */
4766 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 0, 2), 8),
4767 		BTF_MEMBER_ENC(NAME_TBD, 13, 0),/* uint64_t ui64; */
4768 		BTF_MEMBER_ENC(NAME_TBD, 8, 0),	/* uint8_t ui8a[8]; */
4769 		/* enum (anon) */			/* [15] */
4770 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 4), 4),
4771 		BTF_ENUM_ENC(NAME_TBD, 0),
4772 		BTF_ENUM_ENC(NAME_TBD, 1),
4773 		BTF_ENUM_ENC(NAME_TBD, 2),
4774 		BTF_ENUM_ENC(NAME_TBD, 3),
4775 		/* struct pprint_mapv */		/* [16] */
4776 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 11), 40),
4777 		BTF_MEMBER_ENC(NAME_TBD, 11, 0),	/* uint32_t ui32 */
4778 		BTF_MEMBER_ENC(NAME_TBD, 10, 32),	/* uint16_t ui16 */
4779 		BTF_MEMBER_ENC(NAME_TBD, 12, 64),	/* int32_t si32 */
4780 		BTF_MEMBER_ENC(NAME_TBD, 6, 96),	/* unused_bits2a */
4781 		BTF_MEMBER_ENC(NAME_TBD, 7, 98),	/* bits28 */
4782 		BTF_MEMBER_ENC(NAME_TBD, 6, 126),	/* unused_bits2b */
4783 		BTF_MEMBER_ENC(0, 14, 128),		/* union (anon) */
4784 		BTF_MEMBER_ENC(NAME_TBD, 15, 192),	/* aenum */
4785 		BTF_MEMBER_ENC(NAME_TBD, 11, 224),	/* uint32_t ui32b */
4786 		BTF_MEMBER_ENC(NAME_TBD, 6, 256),	/* bits2c */
4787 		BTF_MEMBER_ENC(NAME_TBD, 17, 264),	/* si8_4 */
4788 		BTF_TYPE_ARRAY_ENC(18, 1, 2),		/* [17] */
4789 		BTF_TYPE_ARRAY_ENC(1, 1, 2),		/* [18] */
4790 		BTF_END_RAW,
4791 	},
4792 	BTF_STR_SEC("\0unsigned char\0unsigned short\0unsigned int\0int\0unsigned long long\0uint8_t\0uint16_t\0uint32_t\0int32_t\0uint64_t\0ui64\0ui8a\0ENUM_ZERO\0ENUM_ONE\0ENUM_TWO\0ENUM_THREE\0pprint_mapv\0ui32\0ui16\0si32\0unused_bits2a\0bits28\0unused_bits2b\0aenum\0ui32b\0bits2c\0si8_4"),
4793 	.key_size = sizeof(unsigned int),
4794 	.value_size = sizeof(struct pprint_mapv),
4795 	.key_type_id = 3,	/* unsigned int */
4796 	.value_type_id = 16,	/* struct pprint_mapv */
4797 	.max_entries = 128,
4798 },
4799 
4800 {
4801 	/* this type will have the same type as the
4802 	 * first .raw_types definition, but struct type will
4803 	 * be encoded with kind_flag set.
4804 	 */
4805 	.raw_types = {
4806 		/* unsighed char */			/* [1] */
4807 		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 8, 1),
4808 		/* unsigned short */			/* [2] */
4809 		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 16, 2),
4810 		/* unsigned int */			/* [3] */
4811 		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),
4812 		/* int */				/* [4] */
4813 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
4814 		/* unsigned long long */		/* [5] */
4815 		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 64, 8),
4816 		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),	/* [6] */
4817 		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),	/* [7] */
4818 		/* uint8_t[8] */			/* [8] */
4819 		BTF_TYPE_ARRAY_ENC(9, 1, 8),
4820 		/* typedef unsigned char uint8_t */	/* [9] */
4821 		BTF_TYPEDEF_ENC(NAME_TBD, 1),
4822 		/* typedef unsigned short uint16_t */	/* [10] */
4823 		BTF_TYPEDEF_ENC(NAME_TBD, 2),
4824 		/* typedef unsigned int uint32_t */	/* [11] */
4825 		BTF_TYPEDEF_ENC(NAME_TBD, 3),
4826 		/* typedef int int32_t */		/* [12] */
4827 		BTF_TYPEDEF_ENC(NAME_TBD, 4),
4828 		/* typedef unsigned long long uint64_t *//* [13] */
4829 		BTF_TYPEDEF_ENC(NAME_TBD, 5),
4830 		/* union (anon) */			/* [14] */
4831 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 0, 2), 8),
4832 		BTF_MEMBER_ENC(NAME_TBD, 13, 0),/* uint64_t ui64; */
4833 		BTF_MEMBER_ENC(NAME_TBD, 8, 0),	/* uint8_t ui8a[8]; */
4834 		/* enum (anon) */			/* [15] */
4835 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 4), 4),
4836 		BTF_ENUM_ENC(NAME_TBD, 0),
4837 		BTF_ENUM_ENC(NAME_TBD, 1),
4838 		BTF_ENUM_ENC(NAME_TBD, 2),
4839 		BTF_ENUM_ENC(NAME_TBD, 3),
4840 		/* struct pprint_mapv */		/* [16] */
4841 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 11), 40),
4842 		BTF_MEMBER_ENC(NAME_TBD, 11, BTF_MEMBER_OFFSET(0, 0)),	/* uint32_t ui32 */
4843 		BTF_MEMBER_ENC(NAME_TBD, 10, BTF_MEMBER_OFFSET(0, 32)),	/* uint16_t ui16 */
4844 		BTF_MEMBER_ENC(NAME_TBD, 12, BTF_MEMBER_OFFSET(0, 64)),	/* int32_t si32 */
4845 		BTF_MEMBER_ENC(NAME_TBD, 6, BTF_MEMBER_OFFSET(2, 96)),	/* unused_bits2a */
4846 		BTF_MEMBER_ENC(NAME_TBD, 7, BTF_MEMBER_OFFSET(28, 98)),	/* bits28 */
4847 		BTF_MEMBER_ENC(NAME_TBD, 6, BTF_MEMBER_OFFSET(2, 126)),	/* unused_bits2b */
4848 		BTF_MEMBER_ENC(0, 14, BTF_MEMBER_OFFSET(0, 128)),	/* union (anon) */
4849 		BTF_MEMBER_ENC(NAME_TBD, 15, BTF_MEMBER_OFFSET(0, 192)),	/* aenum */
4850 		BTF_MEMBER_ENC(NAME_TBD, 11, BTF_MEMBER_OFFSET(0, 224)),	/* uint32_t ui32b */
4851 		BTF_MEMBER_ENC(NAME_TBD, 6, BTF_MEMBER_OFFSET(2, 256)),	/* bits2c */
4852 		BTF_MEMBER_ENC(NAME_TBD, 17, 264),	/* si8_4 */
4853 		BTF_TYPE_ARRAY_ENC(18, 1, 2),		/* [17] */
4854 		BTF_TYPE_ARRAY_ENC(1, 1, 2),		/* [18] */
4855 		BTF_END_RAW,
4856 	},
4857 	BTF_STR_SEC("\0unsigned char\0unsigned short\0unsigned int\0int\0unsigned long long\0uint8_t\0uint16_t\0uint32_t\0int32_t\0uint64_t\0ui64\0ui8a\0ENUM_ZERO\0ENUM_ONE\0ENUM_TWO\0ENUM_THREE\0pprint_mapv\0ui32\0ui16\0si32\0unused_bits2a\0bits28\0unused_bits2b\0aenum\0ui32b\0bits2c\0si8_4"),
4858 	.key_size = sizeof(unsigned int),
4859 	.value_size = sizeof(struct pprint_mapv),
4860 	.key_type_id = 3,	/* unsigned int */
4861 	.value_type_id = 16,	/* struct pprint_mapv */
4862 	.max_entries = 128,
4863 },
4864 
4865 {
4866 	/* this type will have the same layout as the
4867 	 * first .raw_types definition. The struct type will
4868 	 * be encoded with kind_flag set, bitfield members
4869 	 * are added typedef/const/volatile, and bitfield members
4870 	 * will have both int and enum types.
4871 	 */
4872 	.raw_types = {
4873 		/* unsighed char */			/* [1] */
4874 		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 8, 1),
4875 		/* unsigned short */			/* [2] */
4876 		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 16, 2),
4877 		/* unsigned int */			/* [3] */
4878 		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),
4879 		/* int */				/* [4] */
4880 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
4881 		/* unsigned long long */		/* [5] */
4882 		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 64, 8),
4883 		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),	/* [6] */
4884 		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),	/* [7] */
4885 		/* uint8_t[8] */			/* [8] */
4886 		BTF_TYPE_ARRAY_ENC(9, 1, 8),
4887 		/* typedef unsigned char uint8_t */	/* [9] */
4888 		BTF_TYPEDEF_ENC(NAME_TBD, 1),
4889 		/* typedef unsigned short uint16_t */	/* [10] */
4890 		BTF_TYPEDEF_ENC(NAME_TBD, 2),
4891 		/* typedef unsigned int uint32_t */	/* [11] */
4892 		BTF_TYPEDEF_ENC(NAME_TBD, 3),
4893 		/* typedef int int32_t */		/* [12] */
4894 		BTF_TYPEDEF_ENC(NAME_TBD, 4),
4895 		/* typedef unsigned long long uint64_t *//* [13] */
4896 		BTF_TYPEDEF_ENC(NAME_TBD, 5),
4897 		/* union (anon) */			/* [14] */
4898 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 0, 2), 8),
4899 		BTF_MEMBER_ENC(NAME_TBD, 13, 0),/* uint64_t ui64; */
4900 		BTF_MEMBER_ENC(NAME_TBD, 8, 0),	/* uint8_t ui8a[8]; */
4901 		/* enum (anon) */			/* [15] */
4902 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 4), 4),
4903 		BTF_ENUM_ENC(NAME_TBD, 0),
4904 		BTF_ENUM_ENC(NAME_TBD, 1),
4905 		BTF_ENUM_ENC(NAME_TBD, 2),
4906 		BTF_ENUM_ENC(NAME_TBD, 3),
4907 		/* struct pprint_mapv */		/* [16] */
4908 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 11), 40),
4909 		BTF_MEMBER_ENC(NAME_TBD, 11, BTF_MEMBER_OFFSET(0, 0)),	/* uint32_t ui32 */
4910 		BTF_MEMBER_ENC(NAME_TBD, 10, BTF_MEMBER_OFFSET(0, 32)),	/* uint16_t ui16 */
4911 		BTF_MEMBER_ENC(NAME_TBD, 12, BTF_MEMBER_OFFSET(0, 64)),	/* int32_t si32 */
4912 		BTF_MEMBER_ENC(NAME_TBD, 17, BTF_MEMBER_OFFSET(2, 96)),	/* unused_bits2a */
4913 		BTF_MEMBER_ENC(NAME_TBD, 7, BTF_MEMBER_OFFSET(28, 98)),	/* bits28 */
4914 		BTF_MEMBER_ENC(NAME_TBD, 19, BTF_MEMBER_OFFSET(2, 126)),/* unused_bits2b */
4915 		BTF_MEMBER_ENC(0, 14, BTF_MEMBER_OFFSET(0, 128)),	/* union (anon) */
4916 		BTF_MEMBER_ENC(NAME_TBD, 15, BTF_MEMBER_OFFSET(0, 192)),	/* aenum */
4917 		BTF_MEMBER_ENC(NAME_TBD, 11, BTF_MEMBER_OFFSET(0, 224)),	/* uint32_t ui32b */
4918 		BTF_MEMBER_ENC(NAME_TBD, 17, BTF_MEMBER_OFFSET(2, 256)),	/* bits2c */
4919 		BTF_MEMBER_ENC(NAME_TBD, 20, BTF_MEMBER_OFFSET(0, 264)),	/* si8_4 */
4920 		/* typedef unsigned int ___int */	/* [17] */
4921 		BTF_TYPEDEF_ENC(NAME_TBD, 18),
4922 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_VOLATILE, 0, 0), 6),	/* [18] */
4923 		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 15),	/* [19] */
4924 		BTF_TYPE_ARRAY_ENC(21, 1, 2),					/* [20] */
4925 		BTF_TYPE_ARRAY_ENC(1, 1, 2),					/* [21] */
4926 		BTF_END_RAW,
4927 	},
4928 	BTF_STR_SEC("\0unsigned char\0unsigned short\0unsigned int\0int\0unsigned long long\0uint8_t\0uint16_t\0uint32_t\0int32_t\0uint64_t\0ui64\0ui8a\0ENUM_ZERO\0ENUM_ONE\0ENUM_TWO\0ENUM_THREE\0pprint_mapv\0ui32\0ui16\0si32\0unused_bits2a\0bits28\0unused_bits2b\0aenum\0ui32b\0bits2c\0___int\0si8_4"),
4929 	.key_size = sizeof(unsigned int),
4930 	.value_size = sizeof(struct pprint_mapv),
4931 	.key_type_id = 3,	/* unsigned int */
4932 	.value_type_id = 16,	/* struct pprint_mapv */
4933 	.max_entries = 128,
4934 },
4935 
4936 #ifdef __SIZEOF_INT128__
4937 {
4938 	/* test int128 */
4939 	.raw_types = {
4940 		/* unsigned int */				/* [1] */
4941 		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),
4942 		/* __int128 */					/* [2] */
4943 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 128, 16),
4944 		/* unsigned __int128 */				/* [3] */
4945 		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 128, 16),
4946 		/* struct pprint_mapv_int128 */			/* [4] */
4947 		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 5), 64),
4948 		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 0)),		/* si128a */
4949 		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 128)),		/* si128b */
4950 		BTF_MEMBER_ENC(NAME_TBD, 3, BTF_MEMBER_OFFSET(3, 256)),		/* bits3 */
4951 		BTF_MEMBER_ENC(NAME_TBD, 3, BTF_MEMBER_OFFSET(80, 259)),	/* bits80 */
4952 		BTF_MEMBER_ENC(NAME_TBD, 3, BTF_MEMBER_OFFSET(0, 384)),		/* ui128 */
4953 		BTF_END_RAW,
4954 	},
4955 	BTF_STR_SEC("\0unsigned int\0__int128\0unsigned __int128\0pprint_mapv_int128\0si128a\0si128b\0bits3\0bits80\0ui128"),
4956 	.key_size = sizeof(unsigned int),
4957 	.value_size = sizeof(struct pprint_mapv_int128),
4958 	.key_type_id = 1,
4959 	.value_type_id = 4,
4960 	.max_entries = 128,
4961 	.mapv_kind = PPRINT_MAPV_KIND_INT128,
4962 },
4963 #endif
4964 
4965 };
4966 
4967 static struct btf_pprint_test_meta {
4968 	const char *descr;
4969 	enum bpf_map_type map_type;
4970 	const char *map_name;
4971 	bool ordered_map;
4972 	bool lossless_map;
4973 	bool percpu_map;
4974 } pprint_tests_meta[] = {
4975 {
4976 	.descr = "BTF pretty print array",
4977 	.map_type = BPF_MAP_TYPE_ARRAY,
4978 	.map_name = "pprint_test_array",
4979 	.ordered_map = true,
4980 	.lossless_map = true,
4981 	.percpu_map = false,
4982 },
4983 
4984 {
4985 	.descr = "BTF pretty print hash",
4986 	.map_type = BPF_MAP_TYPE_HASH,
4987 	.map_name = "pprint_test_hash",
4988 	.ordered_map = false,
4989 	.lossless_map = true,
4990 	.percpu_map = false,
4991 },
4992 
4993 {
4994 	.descr = "BTF pretty print lru hash",
4995 	.map_type = BPF_MAP_TYPE_LRU_HASH,
4996 	.map_name = "pprint_test_lru_hash",
4997 	.ordered_map = false,
4998 	.lossless_map = false,
4999 	.percpu_map = false,
5000 },
5001 
5002 {
5003 	.descr = "BTF pretty print percpu array",
5004 	.map_type = BPF_MAP_TYPE_PERCPU_ARRAY,
5005 	.map_name = "pprint_test_percpu_array",
5006 	.ordered_map = true,
5007 	.lossless_map = true,
5008 	.percpu_map = true,
5009 },
5010 
5011 {
5012 	.descr = "BTF pretty print percpu hash",
5013 	.map_type = BPF_MAP_TYPE_PERCPU_HASH,
5014 	.map_name = "pprint_test_percpu_hash",
5015 	.ordered_map = false,
5016 	.lossless_map = true,
5017 	.percpu_map = true,
5018 },
5019 
5020 {
5021 	.descr = "BTF pretty print lru percpu hash",
5022 	.map_type = BPF_MAP_TYPE_LRU_PERCPU_HASH,
5023 	.map_name = "pprint_test_lru_percpu_hash",
5024 	.ordered_map = false,
5025 	.lossless_map = false,
5026 	.percpu_map = true,
5027 },
5028 
5029 };
5030 
5031 static size_t get_pprint_mapv_size(enum pprint_mapv_kind_t mapv_kind)
5032 {
5033 	if (mapv_kind == PPRINT_MAPV_KIND_BASIC)
5034 		return sizeof(struct pprint_mapv);
5035 
5036 #ifdef __SIZEOF_INT128__
5037 	if (mapv_kind == PPRINT_MAPV_KIND_INT128)
5038 		return sizeof(struct pprint_mapv_int128);
5039 #endif
5040 
5041 	assert(0);
5042 }
5043 
5044 static void set_pprint_mapv(enum pprint_mapv_kind_t mapv_kind,
5045 			    void *mapv, uint32_t i,
5046 			    int num_cpus, int rounded_value_size)
5047 {
5048 	int cpu;
5049 
5050 	if (mapv_kind == PPRINT_MAPV_KIND_BASIC) {
5051 		struct pprint_mapv *v = mapv;
5052 
5053 		for (cpu = 0; cpu < num_cpus; cpu++) {
5054 			v->ui32 = i + cpu;
5055 			v->si32 = -i;
5056 			v->unused_bits2a = 3;
5057 			v->bits28 = i;
5058 			v->unused_bits2b = 3;
5059 			v->ui64 = i;
5060 			v->aenum = i & 0x03;
5061 			v->ui32b = 4;
5062 			v->bits2c = 1;
5063 			v->si8_4[0][0] = (cpu + i) & 0xff;
5064 			v->si8_4[0][1] = (cpu + i + 1) & 0xff;
5065 			v->si8_4[1][0] = (cpu + i + 2) & 0xff;
5066 			v->si8_4[1][1] = (cpu + i + 3) & 0xff;
5067 			v = (void *)v + rounded_value_size;
5068 		}
5069 	}
5070 
5071 #ifdef __SIZEOF_INT128__
5072 	if (mapv_kind == PPRINT_MAPV_KIND_INT128) {
5073 		struct pprint_mapv_int128 *v = mapv;
5074 
5075 		for (cpu = 0; cpu < num_cpus; cpu++) {
5076 			v->si128a = i;
5077 			v->si128b = -i;
5078 			v->bits3 = i & 0x07;
5079 			v->bits80 = (((unsigned __int128)1) << 64) + i;
5080 			v->ui128 = (((unsigned __int128)2) << 64) + i;
5081 			v = (void *)v + rounded_value_size;
5082 		}
5083 	}
5084 #endif
5085 }
5086 
5087 ssize_t get_pprint_expected_line(enum pprint_mapv_kind_t mapv_kind,
5088 				 char *expected_line, ssize_t line_size,
5089 				 bool percpu_map, unsigned int next_key,
5090 				 int cpu, void *mapv)
5091 {
5092 	ssize_t nexpected_line = -1;
5093 
5094 	if (mapv_kind == PPRINT_MAPV_KIND_BASIC) {
5095 		struct pprint_mapv *v = mapv;
5096 
5097 		nexpected_line = snprintf(expected_line, line_size,
5098 					  "%s%u: {%u,0,%d,0x%x,0x%x,0x%x,"
5099 					  "{%llu|[%u,%u,%u,%u,%u,%u,%u,%u]},%s,"
5100 					  "%u,0x%x,[[%d,%d],[%d,%d]]}\n",
5101 					  percpu_map ? "\tcpu" : "",
5102 					  percpu_map ? cpu : next_key,
5103 					  v->ui32, v->si32,
5104 					  v->unused_bits2a,
5105 					  v->bits28,
5106 					  v->unused_bits2b,
5107 					  (__u64)v->ui64,
5108 					  v->ui8a[0], v->ui8a[1],
5109 					  v->ui8a[2], v->ui8a[3],
5110 					  v->ui8a[4], v->ui8a[5],
5111 					  v->ui8a[6], v->ui8a[7],
5112 					  pprint_enum_str[v->aenum],
5113 					  v->ui32b,
5114 					  v->bits2c,
5115 					  v->si8_4[0][0], v->si8_4[0][1],
5116 					  v->si8_4[1][0], v->si8_4[1][1]);
5117 	}
5118 
5119 #ifdef __SIZEOF_INT128__
5120 	if (mapv_kind == PPRINT_MAPV_KIND_INT128) {
5121 		struct pprint_mapv_int128 *v = mapv;
5122 
5123 		nexpected_line = snprintf(expected_line, line_size,
5124 					  "%s%u: {0x%lx,0x%lx,0x%lx,"
5125 					  "0x%lx%016lx,0x%lx%016lx}\n",
5126 					  percpu_map ? "\tcpu" : "",
5127 					  percpu_map ? cpu : next_key,
5128 					  (uint64_t)v->si128a,
5129 					  (uint64_t)v->si128b,
5130 					  (uint64_t)v->bits3,
5131 					  (uint64_t)(v->bits80 >> 64),
5132 					  (uint64_t)v->bits80,
5133 					  (uint64_t)(v->ui128 >> 64),
5134 					  (uint64_t)v->ui128);
5135 	}
5136 #endif
5137 
5138 	return nexpected_line;
5139 }
5140 
5141 static int check_line(const char *expected_line, int nexpected_line,
5142 		      int expected_line_len, const char *line)
5143 {
5144 	if (CHECK(nexpected_line == expected_line_len,
5145 		  "expected_line is too long"))
5146 		return -1;
5147 
5148 	if (strcmp(expected_line, line)) {
5149 		fprintf(stderr, "unexpected pprint output\n");
5150 		fprintf(stderr, "expected: %s", expected_line);
5151 		fprintf(stderr, "    read: %s", line);
5152 		return -1;
5153 	}
5154 
5155 	return 0;
5156 }
5157 
5158 
5159 static void do_test_pprint(int test_num)
5160 {
5161 	const struct btf_raw_test *test = &pprint_test_template[test_num];
5162 	enum pprint_mapv_kind_t mapv_kind = test->mapv_kind;
5163 	LIBBPF_OPTS(bpf_map_create_opts, opts);
5164 	bool ordered_map, lossless_map, percpu_map;
5165 	int err, ret, num_cpus, rounded_value_size;
5166 	unsigned int key, nr_read_elems;
5167 	int map_fd = -1, btf_fd = -1;
5168 	unsigned int raw_btf_size;
5169 	char expected_line[255];
5170 	FILE *pin_file = NULL;
5171 	char pin_path[255];
5172 	size_t line_len = 0;
5173 	char *line = NULL;
5174 	void *mapv = NULL;
5175 	uint8_t *raw_btf;
5176 	ssize_t nread;
5177 
5178 	if (!test__start_subtest(test->descr))
5179 		return;
5180 
5181 	raw_btf = btf_raw_create(&hdr_tmpl, test->raw_types,
5182 				 test->str_sec, test->str_sec_size,
5183 				 &raw_btf_size, NULL);
5184 
5185 	if (!raw_btf)
5186 		return;
5187 
5188 	*btf_log_buf = '\0';
5189 	btf_fd = load_raw_btf(raw_btf, raw_btf_size);
5190 	free(raw_btf);
5191 
5192 	if (CHECK(btf_fd < 0, "errno:%d\n", errno)) {
5193 		err = -1;
5194 		goto done;
5195 	}
5196 
5197 	opts.btf_fd = btf_fd;
5198 	opts.btf_key_type_id = test->key_type_id;
5199 	opts.btf_value_type_id = test->value_type_id;
5200 	map_fd = bpf_map_create(test->map_type, test->map_name,
5201 				test->key_size, test->value_size, test->max_entries, &opts);
5202 	if (CHECK(map_fd < 0, "errno:%d", errno)) {
5203 		err = -1;
5204 		goto done;
5205 	}
5206 
5207 	ret = snprintf(pin_path, sizeof(pin_path), "%s/%s",
5208 		       "/sys/fs/bpf", test->map_name);
5209 
5210 	if (CHECK(ret == sizeof(pin_path), "pin_path %s/%s is too long",
5211 		  "/sys/fs/bpf", test->map_name)) {
5212 		err = -1;
5213 		goto done;
5214 	}
5215 
5216 	err = bpf_obj_pin(map_fd, pin_path);
5217 	if (CHECK(err, "bpf_obj_pin(%s): errno:%d.", pin_path, errno))
5218 		goto done;
5219 
5220 	percpu_map = test->percpu_map;
5221 	num_cpus = percpu_map ? bpf_num_possible_cpus() : 1;
5222 	rounded_value_size = round_up(get_pprint_mapv_size(mapv_kind), 8);
5223 	mapv = calloc(num_cpus, rounded_value_size);
5224 	if (CHECK(!mapv, "mapv allocation failure")) {
5225 		err = -1;
5226 		goto done;
5227 	}
5228 
5229 	for (key = 0; key < test->max_entries; key++) {
5230 		set_pprint_mapv(mapv_kind, mapv, key, num_cpus, rounded_value_size);
5231 		bpf_map_update_elem(map_fd, &key, mapv, 0);
5232 	}
5233 
5234 	pin_file = fopen(pin_path, "r");
5235 	if (CHECK(!pin_file, "fopen(%s): errno:%d", pin_path, errno)) {
5236 		err = -1;
5237 		goto done;
5238 	}
5239 
5240 	/* Skip lines start with '#' */
5241 	while ((nread = getline(&line, &line_len, pin_file)) > 0 &&
5242 	       *line == '#')
5243 		;
5244 
5245 	if (CHECK(nread <= 0, "Unexpected EOF")) {
5246 		err = -1;
5247 		goto done;
5248 	}
5249 
5250 	nr_read_elems = 0;
5251 	ordered_map = test->ordered_map;
5252 	lossless_map = test->lossless_map;
5253 	do {
5254 		ssize_t nexpected_line;
5255 		unsigned int next_key;
5256 		void *cmapv;
5257 		int cpu;
5258 
5259 		next_key = ordered_map ? nr_read_elems : atoi(line);
5260 		set_pprint_mapv(mapv_kind, mapv, next_key, num_cpus, rounded_value_size);
5261 		cmapv = mapv;
5262 
5263 		for (cpu = 0; cpu < num_cpus; cpu++) {
5264 			if (percpu_map) {
5265 				/* for percpu map, the format looks like:
5266 				 * <key>: {
5267 				 *	cpu0: <value_on_cpu0>
5268 				 *	cpu1: <value_on_cpu1>
5269 				 *	...
5270 				 *	cpun: <value_on_cpun>
5271 				 * }
5272 				 *
5273 				 * let us verify the line containing the key here.
5274 				 */
5275 				if (cpu == 0) {
5276 					nexpected_line = snprintf(expected_line,
5277 								  sizeof(expected_line),
5278 								  "%u: {\n",
5279 								  next_key);
5280 
5281 					err = check_line(expected_line, nexpected_line,
5282 							 sizeof(expected_line), line);
5283 					if (err < 0)
5284 						goto done;
5285 				}
5286 
5287 				/* read value@cpu */
5288 				nread = getline(&line, &line_len, pin_file);
5289 				if (nread < 0)
5290 					break;
5291 			}
5292 
5293 			nexpected_line = get_pprint_expected_line(mapv_kind, expected_line,
5294 								  sizeof(expected_line),
5295 								  percpu_map, next_key,
5296 								  cpu, cmapv);
5297 			err = check_line(expected_line, nexpected_line,
5298 					 sizeof(expected_line), line);
5299 			if (err < 0)
5300 				goto done;
5301 
5302 			cmapv = cmapv + rounded_value_size;
5303 		}
5304 
5305 		if (percpu_map) {
5306 			/* skip the last bracket for the percpu map */
5307 			nread = getline(&line, &line_len, pin_file);
5308 			if (nread < 0)
5309 				break;
5310 		}
5311 
5312 		nread = getline(&line, &line_len, pin_file);
5313 	} while (++nr_read_elems < test->max_entries && nread > 0);
5314 
5315 	if (lossless_map &&
5316 	    CHECK(nr_read_elems < test->max_entries,
5317 		  "Unexpected EOF. nr_read_elems:%u test->max_entries:%u",
5318 		  nr_read_elems, test->max_entries)) {
5319 		err = -1;
5320 		goto done;
5321 	}
5322 
5323 	if (CHECK(nread > 0, "Unexpected extra pprint output: %s", line)) {
5324 		err = -1;
5325 		goto done;
5326 	}
5327 
5328 	err = 0;
5329 
5330 done:
5331 	if (mapv)
5332 		free(mapv);
5333 	if (!err)
5334 		fprintf(stderr, "OK");
5335 	if (*btf_log_buf && (err || always_log))
5336 		fprintf(stderr, "\n%s", btf_log_buf);
5337 	if (btf_fd >= 0)
5338 		close(btf_fd);
5339 	if (map_fd >= 0)
5340 		close(map_fd);
5341 	if (pin_file)
5342 		fclose(pin_file);
5343 	unlink(pin_path);
5344 	free(line);
5345 }
5346 
5347 static void test_pprint(void)
5348 {
5349 	unsigned int i;
5350 
5351 	/* test various maps with the first test template */
5352 	for (i = 0; i < ARRAY_SIZE(pprint_tests_meta); i++) {
5353 		pprint_test_template[0].descr = pprint_tests_meta[i].descr;
5354 		pprint_test_template[0].map_type = pprint_tests_meta[i].map_type;
5355 		pprint_test_template[0].map_name = pprint_tests_meta[i].map_name;
5356 		pprint_test_template[0].ordered_map = pprint_tests_meta[i].ordered_map;
5357 		pprint_test_template[0].lossless_map = pprint_tests_meta[i].lossless_map;
5358 		pprint_test_template[0].percpu_map = pprint_tests_meta[i].percpu_map;
5359 
5360 		do_test_pprint(0);
5361 	}
5362 
5363 	/* test rest test templates with the first map */
5364 	for (i = 1; i < ARRAY_SIZE(pprint_test_template); i++) {
5365 		pprint_test_template[i].descr = pprint_tests_meta[0].descr;
5366 		pprint_test_template[i].map_type = pprint_tests_meta[0].map_type;
5367 		pprint_test_template[i].map_name = pprint_tests_meta[0].map_name;
5368 		pprint_test_template[i].ordered_map = pprint_tests_meta[0].ordered_map;
5369 		pprint_test_template[i].lossless_map = pprint_tests_meta[0].lossless_map;
5370 		pprint_test_template[i].percpu_map = pprint_tests_meta[0].percpu_map;
5371 		do_test_pprint(i);
5372 	}
5373 }
5374 
5375 #define BPF_LINE_INFO_ENC(insn_off, file_off, line_off, line_num, line_col) \
5376 	(insn_off), (file_off), (line_off), ((line_num) << 10 | ((line_col) & 0x3ff))
5377 
5378 static struct prog_info_raw_test {
5379 	const char *descr;
5380 	const char *str_sec;
5381 	const char *err_str;
5382 	__u32 raw_types[MAX_NR_RAW_U32];
5383 	__u32 str_sec_size;
5384 	struct bpf_insn insns[MAX_INSNS];
5385 	__u32 prog_type;
5386 	__u32 func_info[MAX_SUBPROGS][2];
5387 	__u32 func_info_rec_size;
5388 	__u32 func_info_cnt;
5389 	__u32 line_info[MAX_NR_RAW_U32];
5390 	__u32 line_info_rec_size;
5391 	__u32 nr_jited_ksyms;
5392 	bool expected_prog_load_failure;
5393 	__u32 dead_code_cnt;
5394 	__u32 dead_code_mask;
5395 	__u32 dead_func_cnt;
5396 	__u32 dead_func_mask;
5397 } info_raw_tests[] = {
5398 {
5399 	.descr = "func_type (main func + one sub)",
5400 	.raw_types = {
5401 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5402 		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),	/* [2] */
5403 		BTF_FUNC_PROTO_ENC(1, 2),			/* [3] */
5404 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5405 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5406 		BTF_FUNC_PROTO_ENC(1, 2),			/* [4] */
5407 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5408 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5409 		BTF_FUNC_ENC(NAME_TBD, 3),			/* [5] */
5410 		BTF_FUNC_ENC(NAME_TBD, 4),			/* [6] */
5411 		BTF_END_RAW,
5412 	},
5413 	.str_sec = "\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB",
5414 	.str_sec_size = sizeof("\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB"),
5415 	.insns = {
5416 		BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
5417 		BPF_MOV64_IMM(BPF_REG_0, 1),
5418 		BPF_EXIT_INSN(),
5419 		BPF_MOV64_IMM(BPF_REG_0, 2),
5420 		BPF_EXIT_INSN(),
5421 	},
5422 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5423 	.func_info = { {0, 5}, {3, 6} },
5424 	.func_info_rec_size = 8,
5425 	.func_info_cnt = 2,
5426 	.line_info = { BTF_END_RAW },
5427 },
5428 
5429 {
5430 	.descr = "func_type (Incorrect func_info_rec_size)",
5431 	.raw_types = {
5432 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5433 		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),	/* [2] */
5434 		BTF_FUNC_PROTO_ENC(1, 2),			/* [3] */
5435 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5436 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5437 		BTF_FUNC_PROTO_ENC(1, 2),			/* [4] */
5438 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5439 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5440 		BTF_FUNC_ENC(NAME_TBD, 3),			/* [5] */
5441 		BTF_FUNC_ENC(NAME_TBD, 4),			/* [6] */
5442 		BTF_END_RAW,
5443 	},
5444 	.str_sec = "\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB",
5445 	.str_sec_size = sizeof("\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB"),
5446 	.insns = {
5447 		BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
5448 		BPF_MOV64_IMM(BPF_REG_0, 1),
5449 		BPF_EXIT_INSN(),
5450 		BPF_MOV64_IMM(BPF_REG_0, 2),
5451 		BPF_EXIT_INSN(),
5452 	},
5453 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5454 	.func_info = { {0, 5}, {3, 6} },
5455 	.func_info_rec_size = 4,
5456 	.func_info_cnt = 2,
5457 	.line_info = { BTF_END_RAW },
5458 	.expected_prog_load_failure = true,
5459 },
5460 
5461 {
5462 	.descr = "func_type (Incorrect func_info_cnt)",
5463 	.raw_types = {
5464 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5465 		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),	/* [2] */
5466 		BTF_FUNC_PROTO_ENC(1, 2),			/* [3] */
5467 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5468 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5469 		BTF_FUNC_PROTO_ENC(1, 2),			/* [4] */
5470 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5471 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5472 		BTF_FUNC_ENC(NAME_TBD, 3),			/* [5] */
5473 		BTF_FUNC_ENC(NAME_TBD, 4),			/* [6] */
5474 		BTF_END_RAW,
5475 	},
5476 	.str_sec = "\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB",
5477 	.str_sec_size = sizeof("\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB"),
5478 	.insns = {
5479 		BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
5480 		BPF_MOV64_IMM(BPF_REG_0, 1),
5481 		BPF_EXIT_INSN(),
5482 		BPF_MOV64_IMM(BPF_REG_0, 2),
5483 		BPF_EXIT_INSN(),
5484 	},
5485 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5486 	.func_info = { {0, 5}, {3, 6} },
5487 	.func_info_rec_size = 8,
5488 	.func_info_cnt = 1,
5489 	.line_info = { BTF_END_RAW },
5490 	.expected_prog_load_failure = true,
5491 },
5492 
5493 {
5494 	.descr = "func_type (Incorrect bpf_func_info.insn_off)",
5495 	.raw_types = {
5496 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5497 		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),	/* [2] */
5498 		BTF_FUNC_PROTO_ENC(1, 2),			/* [3] */
5499 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5500 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5501 		BTF_FUNC_PROTO_ENC(1, 2),			/* [4] */
5502 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5503 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5504 		BTF_FUNC_ENC(NAME_TBD, 3),			/* [5] */
5505 		BTF_FUNC_ENC(NAME_TBD, 4),			/* [6] */
5506 		BTF_END_RAW,
5507 	},
5508 	.str_sec = "\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB",
5509 	.str_sec_size = sizeof("\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB"),
5510 	.insns = {
5511 		BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
5512 		BPF_MOV64_IMM(BPF_REG_0, 1),
5513 		BPF_EXIT_INSN(),
5514 		BPF_MOV64_IMM(BPF_REG_0, 2),
5515 		BPF_EXIT_INSN(),
5516 	},
5517 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5518 	.func_info = { {0, 5}, {2, 6} },
5519 	.func_info_rec_size = 8,
5520 	.func_info_cnt = 2,
5521 	.line_info = { BTF_END_RAW },
5522 	.expected_prog_load_failure = true,
5523 },
5524 
5525 {
5526 	.descr = "line_info (No subprog)",
5527 	.raw_types = {
5528 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5529 		BTF_END_RAW,
5530 	},
5531 	BTF_STR_SEC("\0int\0int a=1;\0int b=2;\0return a + b;\0return a + b;"),
5532 	.insns = {
5533 		BPF_MOV64_IMM(BPF_REG_0, 1),
5534 		BPF_MOV64_IMM(BPF_REG_1, 2),
5535 		BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
5536 		BPF_EXIT_INSN(),
5537 	},
5538 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5539 	.func_info_cnt = 0,
5540 	.line_info = {
5541 		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5542 		BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 9),
5543 		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8),
5544 		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 7),
5545 		BTF_END_RAW,
5546 	},
5547 	.line_info_rec_size = sizeof(struct bpf_line_info),
5548 	.nr_jited_ksyms = 1,
5549 },
5550 
5551 {
5552 	.descr = "line_info (No subprog. insn_off >= prog->len)",
5553 	.raw_types = {
5554 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5555 		BTF_END_RAW,
5556 	},
5557 	BTF_STR_SEC("\0int\0int a=1;\0int b=2;\0return a + b;\0return a + b;"),
5558 	.insns = {
5559 		BPF_MOV64_IMM(BPF_REG_0, 1),
5560 		BPF_MOV64_IMM(BPF_REG_1, 2),
5561 		BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
5562 		BPF_EXIT_INSN(),
5563 	},
5564 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5565 	.func_info_cnt = 0,
5566 	.line_info = {
5567 		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5568 		BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 9),
5569 		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8),
5570 		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 7),
5571 		BPF_LINE_INFO_ENC(4, 0, 0, 5, 6),
5572 		BTF_END_RAW,
5573 	},
5574 	.line_info_rec_size = sizeof(struct bpf_line_info),
5575 	.nr_jited_ksyms = 1,
5576 	.err_str = "line_info[4].insn_off",
5577 	.expected_prog_load_failure = true,
5578 },
5579 
5580 {
5581 	.descr = "line_info (Zero bpf insn code)",
5582 	.raw_types = {
5583 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5584 		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 64, 8),	/* [2] */
5585 		BTF_TYPEDEF_ENC(NAME_TBD, 2),			/* [3] */
5586 		BTF_END_RAW,
5587 	},
5588 	BTF_STR_SEC("\0int\0unsigned long\0u64\0u64 a=1;\0return a;"),
5589 	.insns = {
5590 		BPF_LD_IMM64(BPF_REG_0, 1),
5591 		BPF_EXIT_INSN(),
5592 	},
5593 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5594 	.func_info_cnt = 0,
5595 	.line_info = {
5596 		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5597 		BPF_LINE_INFO_ENC(1, 0, 0, 2, 9),
5598 		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8),
5599 		BTF_END_RAW,
5600 	},
5601 	.line_info_rec_size = sizeof(struct bpf_line_info),
5602 	.nr_jited_ksyms = 1,
5603 	.err_str = "Invalid insn code at line_info[1]",
5604 	.expected_prog_load_failure = true,
5605 },
5606 
5607 {
5608 	.descr = "line_info (No subprog. zero tailing line_info",
5609 	.raw_types = {
5610 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5611 		BTF_END_RAW,
5612 	},
5613 	BTF_STR_SEC("\0int\0int a=1;\0int b=2;\0return a + b;\0return a + b;"),
5614 	.insns = {
5615 		BPF_MOV64_IMM(BPF_REG_0, 1),
5616 		BPF_MOV64_IMM(BPF_REG_1, 2),
5617 		BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
5618 		BPF_EXIT_INSN(),
5619 	},
5620 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5621 	.func_info_cnt = 0,
5622 	.line_info = {
5623 		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10), 0,
5624 		BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 9), 0,
5625 		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8), 0,
5626 		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 7), 0,
5627 		BTF_END_RAW,
5628 	},
5629 	.line_info_rec_size = sizeof(struct bpf_line_info) + sizeof(__u32),
5630 	.nr_jited_ksyms = 1,
5631 },
5632 
5633 {
5634 	.descr = "line_info (No subprog. nonzero tailing line_info)",
5635 	.raw_types = {
5636 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5637 		BTF_END_RAW,
5638 	},
5639 	BTF_STR_SEC("\0int\0int a=1;\0int b=2;\0return a + b;\0return a + b;"),
5640 	.insns = {
5641 		BPF_MOV64_IMM(BPF_REG_0, 1),
5642 		BPF_MOV64_IMM(BPF_REG_1, 2),
5643 		BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
5644 		BPF_EXIT_INSN(),
5645 	},
5646 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5647 	.func_info_cnt = 0,
5648 	.line_info = {
5649 		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10), 0,
5650 		BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 9), 0,
5651 		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8), 0,
5652 		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 7), 1,
5653 		BTF_END_RAW,
5654 	},
5655 	.line_info_rec_size = sizeof(struct bpf_line_info) + sizeof(__u32),
5656 	.nr_jited_ksyms = 1,
5657 	.err_str = "nonzero tailing record in line_info",
5658 	.expected_prog_load_failure = true,
5659 },
5660 
5661 {
5662 	.descr = "line_info (subprog)",
5663 	.raw_types = {
5664 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5665 		BTF_END_RAW,
5666 	},
5667 	BTF_STR_SEC("\0int\0int a=1+1;\0return func(a);\0b+=1;\0return b;"),
5668 	.insns = {
5669 		BPF_MOV64_IMM(BPF_REG_2, 1),
5670 		BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
5671 		BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
5672 		BPF_CALL_REL(1),
5673 		BPF_EXIT_INSN(),
5674 		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
5675 		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
5676 		BPF_EXIT_INSN(),
5677 	},
5678 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5679 	.func_info_cnt = 0,
5680 	.line_info = {
5681 		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5682 		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 2, 9),
5683 		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 3, 8),
5684 		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 4, 7),
5685 		BTF_END_RAW,
5686 	},
5687 	.line_info_rec_size = sizeof(struct bpf_line_info),
5688 	.nr_jited_ksyms = 2,
5689 },
5690 
5691 {
5692 	.descr = "line_info (subprog + func_info)",
5693 	.raw_types = {
5694 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5695 		BTF_FUNC_PROTO_ENC(1, 1),			/* [2] */
5696 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5697 		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
5698 		BTF_FUNC_ENC(NAME_TBD, 2),			/* [4] */
5699 		BTF_END_RAW,
5700 	},
5701 	BTF_STR_SEC("\0int\0x\0sub\0main\0int a=1+1;\0return func(a);\0b+=1;\0return b;"),
5702 	.insns = {
5703 		BPF_MOV64_IMM(BPF_REG_2, 1),
5704 		BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
5705 		BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
5706 		BPF_CALL_REL(1),
5707 		BPF_EXIT_INSN(),
5708 		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
5709 		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
5710 		BPF_EXIT_INSN(),
5711 	},
5712 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5713 	.func_info_cnt = 2,
5714 	.func_info_rec_size = 8,
5715 	.func_info = { {0, 4}, {5, 3} },
5716 	.line_info = {
5717 		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5718 		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 2, 9),
5719 		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 3, 8),
5720 		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 4, 7),
5721 		BTF_END_RAW,
5722 	},
5723 	.line_info_rec_size = sizeof(struct bpf_line_info),
5724 	.nr_jited_ksyms = 2,
5725 },
5726 
5727 {
5728 	.descr = "line_info (subprog. missing 1st func line info)",
5729 	.raw_types = {
5730 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5731 		BTF_END_RAW,
5732 	},
5733 	BTF_STR_SEC("\0int\0int a=1+1;\0return func(a);\0b+=1;\0return b;"),
5734 	.insns = {
5735 		BPF_MOV64_IMM(BPF_REG_2, 1),
5736 		BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
5737 		BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
5738 		BPF_CALL_REL(1),
5739 		BPF_EXIT_INSN(),
5740 		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
5741 		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
5742 		BPF_EXIT_INSN(),
5743 	},
5744 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5745 	.func_info_cnt = 0,
5746 	.line_info = {
5747 		BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 1, 10),
5748 		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 2, 9),
5749 		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 3, 8),
5750 		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 4, 7),
5751 		BTF_END_RAW,
5752 	},
5753 	.line_info_rec_size = sizeof(struct bpf_line_info),
5754 	.nr_jited_ksyms = 2,
5755 	.err_str = "missing bpf_line_info for func#0",
5756 	.expected_prog_load_failure = true,
5757 },
5758 
5759 {
5760 	.descr = "line_info (subprog. missing 2nd func line info)",
5761 	.raw_types = {
5762 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5763 		BTF_END_RAW,
5764 	},
5765 	BTF_STR_SEC("\0int\0int a=1+1;\0return func(a);\0b+=1;\0return b;"),
5766 	.insns = {
5767 		BPF_MOV64_IMM(BPF_REG_2, 1),
5768 		BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
5769 		BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
5770 		BPF_CALL_REL(1),
5771 		BPF_EXIT_INSN(),
5772 		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
5773 		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
5774 		BPF_EXIT_INSN(),
5775 	},
5776 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5777 	.func_info_cnt = 0,
5778 	.line_info = {
5779 		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5780 		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 2, 9),
5781 		BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 3, 8),
5782 		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 4, 7),
5783 		BTF_END_RAW,
5784 	},
5785 	.line_info_rec_size = sizeof(struct bpf_line_info),
5786 	.nr_jited_ksyms = 2,
5787 	.err_str = "missing bpf_line_info for func#1",
5788 	.expected_prog_load_failure = true,
5789 },
5790 
5791 {
5792 	.descr = "line_info (subprog. unordered insn offset)",
5793 	.raw_types = {
5794 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5795 		BTF_END_RAW,
5796 	},
5797 	BTF_STR_SEC("\0int\0int a=1+1;\0return func(a);\0b+=1;\0return b;"),
5798 	.insns = {
5799 		BPF_MOV64_IMM(BPF_REG_2, 1),
5800 		BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
5801 		BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
5802 		BPF_CALL_REL(1),
5803 		BPF_EXIT_INSN(),
5804 		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
5805 		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
5806 		BPF_EXIT_INSN(),
5807 	},
5808 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5809 	.func_info_cnt = 0,
5810 	.line_info = {
5811 		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5812 		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 2, 9),
5813 		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8),
5814 		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 4, 7),
5815 		BTF_END_RAW,
5816 	},
5817 	.line_info_rec_size = sizeof(struct bpf_line_info),
5818 	.nr_jited_ksyms = 2,
5819 	.err_str = "Invalid line_info[2].insn_off",
5820 	.expected_prog_load_failure = true,
5821 },
5822 
5823 {
5824 	.descr = "line_info (dead start)",
5825 	.raw_types = {
5826 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5827 		BTF_END_RAW,
5828 	},
5829 	BTF_STR_SEC("\0int\0/* dead jmp */\0int a=1;\0int b=2;\0return a + b;\0return a + b;"),
5830 	.insns = {
5831 		BPF_JMP_IMM(BPF_JA, 0, 0, 0),
5832 		BPF_MOV64_IMM(BPF_REG_0, 1),
5833 		BPF_MOV64_IMM(BPF_REG_1, 2),
5834 		BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
5835 		BPF_EXIT_INSN(),
5836 	},
5837 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5838 	.func_info_cnt = 0,
5839 	.line_info = {
5840 		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5841 		BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 9),
5842 		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8),
5843 		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 7),
5844 		BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 5, 6),
5845 		BTF_END_RAW,
5846 	},
5847 	.line_info_rec_size = sizeof(struct bpf_line_info),
5848 	.nr_jited_ksyms = 1,
5849 	.dead_code_cnt = 1,
5850 	.dead_code_mask = 0x01,
5851 },
5852 
5853 {
5854 	.descr = "line_info (dead end)",
5855 	.raw_types = {
5856 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5857 		BTF_END_RAW,
5858 	},
5859 	BTF_STR_SEC("\0int\0int a=1;\0int b=2;\0return a + b;\0/* dead jmp */\0return a + b;\0/* dead exit */"),
5860 	.insns = {
5861 		BPF_MOV64_IMM(BPF_REG_0, 1),
5862 		BPF_MOV64_IMM(BPF_REG_1, 2),
5863 		BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
5864 		BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 10, 1),
5865 		BPF_EXIT_INSN(),
5866 		BPF_EXIT_INSN(),
5867 	},
5868 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5869 	.func_info_cnt = 0,
5870 	.line_info = {
5871 		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 12),
5872 		BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 11),
5873 		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 10),
5874 		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 9),
5875 		BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 5, 8),
5876 		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 6, 7),
5877 		BTF_END_RAW,
5878 	},
5879 	.line_info_rec_size = sizeof(struct bpf_line_info),
5880 	.nr_jited_ksyms = 1,
5881 	.dead_code_cnt = 2,
5882 	.dead_code_mask = 0x28,
5883 },
5884 
5885 {
5886 	.descr = "line_info (dead code + subprog + func_info)",
5887 	.raw_types = {
5888 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5889 		BTF_FUNC_PROTO_ENC(1, 1),			/* [2] */
5890 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5891 		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
5892 		BTF_FUNC_ENC(NAME_TBD, 2),			/* [4] */
5893 		BTF_END_RAW,
5894 	},
5895 	BTF_STR_SEC("\0int\0x\0sub\0main\0int a=1+1;\0/* dead jmp */"
5896 		    "\0/* dead */\0/* dead */\0/* dead */\0/* dead */"
5897 		    "\0/* dead */\0/* dead */\0/* dead */\0/* dead */"
5898 		    "\0return func(a);\0b+=1;\0return b;"),
5899 	.insns = {
5900 		BPF_MOV64_IMM(BPF_REG_2, 1),
5901 		BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
5902 		BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
5903 		BPF_JMP_IMM(BPF_JGE, BPF_REG_2, 0, 8),
5904 		BPF_MOV64_IMM(BPF_REG_2, 1),
5905 		BPF_MOV64_IMM(BPF_REG_2, 1),
5906 		BPF_MOV64_IMM(BPF_REG_2, 1),
5907 		BPF_MOV64_IMM(BPF_REG_2, 1),
5908 		BPF_MOV64_IMM(BPF_REG_2, 1),
5909 		BPF_MOV64_IMM(BPF_REG_2, 1),
5910 		BPF_MOV64_IMM(BPF_REG_2, 1),
5911 		BPF_MOV64_IMM(BPF_REG_2, 1),
5912 		BPF_CALL_REL(1),
5913 		BPF_EXIT_INSN(),
5914 		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
5915 		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
5916 		BPF_EXIT_INSN(),
5917 	},
5918 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5919 	.func_info_cnt = 2,
5920 	.func_info_rec_size = 8,
5921 	.func_info = { {0, 4}, {14, 3} },
5922 	.line_info = {
5923 		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5924 		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 1, 10),
5925 		BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 1, 10),
5926 		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 1, 10),
5927 		BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
5928 		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 1, 10),
5929 		BPF_LINE_INFO_ENC(8, 0, NAME_TBD, 1, 10),
5930 		BPF_LINE_INFO_ENC(9, 0, NAME_TBD, 1, 10),
5931 		BPF_LINE_INFO_ENC(10, 0, NAME_TBD, 1, 10),
5932 		BPF_LINE_INFO_ENC(11, 0, NAME_TBD, 2, 9),
5933 		BPF_LINE_INFO_ENC(12, 0, NAME_TBD, 2, 9),
5934 		BPF_LINE_INFO_ENC(14, 0, NAME_TBD, 3, 8),
5935 		BPF_LINE_INFO_ENC(16, 0, NAME_TBD, 4, 7),
5936 		BTF_END_RAW,
5937 	},
5938 	.line_info_rec_size = sizeof(struct bpf_line_info),
5939 	.nr_jited_ksyms = 2,
5940 	.dead_code_cnt = 9,
5941 	.dead_code_mask = 0x3fe,
5942 },
5943 
5944 {
5945 	.descr = "line_info (dead subprog)",
5946 	.raw_types = {
5947 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5948 		BTF_FUNC_PROTO_ENC(1, 1),			/* [2] */
5949 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5950 		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
5951 		BTF_FUNC_ENC(NAME_TBD, 2),			/* [4] */
5952 		BTF_FUNC_ENC(NAME_TBD, 2),			/* [5] */
5953 		BTF_END_RAW,
5954 	},
5955 	BTF_STR_SEC("\0int\0x\0dead\0main\0func\0int a=1+1;\0/* live call */"
5956 		    "\0return 0;\0return 0;\0/* dead */\0/* dead */"
5957 		    "\0/* dead */\0return bla + 1;\0return bla + 1;"
5958 		    "\0return bla + 1;\0return func(a);\0b+=1;\0return b;"),
5959 	.insns = {
5960 		BPF_MOV64_IMM(BPF_REG_2, 1),
5961 		BPF_JMP_IMM(BPF_JGE, BPF_REG_2, 0, 1),
5962 		BPF_CALL_REL(3),
5963 		BPF_CALL_REL(5),
5964 		BPF_MOV64_IMM(BPF_REG_0, 0),
5965 		BPF_EXIT_INSN(),
5966 		BPF_MOV64_IMM(BPF_REG_0, 0),
5967 		BPF_CALL_REL(1),
5968 		BPF_EXIT_INSN(),
5969 		BPF_MOV64_REG(BPF_REG_0, 2),
5970 		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
5971 		BPF_EXIT_INSN(),
5972 	},
5973 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5974 	.func_info_cnt = 3,
5975 	.func_info_rec_size = 8,
5976 		.func_info = { {0, 4}, {6, 3}, {9, 5} },
5977 	.line_info = {
5978 		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5979 		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 1, 10),
5980 		BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 1, 10),
5981 		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 1, 10),
5982 		BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
5983 		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 1, 10),
5984 		BPF_LINE_INFO_ENC(8, 0, NAME_TBD, 1, 10),
5985 		BPF_LINE_INFO_ENC(9, 0, NAME_TBD, 1, 10),
5986 		BPF_LINE_INFO_ENC(10, 0, NAME_TBD, 1, 10),
5987 		BPF_LINE_INFO_ENC(11, 0, NAME_TBD, 2, 9),
5988 		BTF_END_RAW,
5989 	},
5990 	.line_info_rec_size = sizeof(struct bpf_line_info),
5991 	.nr_jited_ksyms = 2,
5992 	.dead_code_cnt = 3,
5993 	.dead_code_mask = 0x70,
5994 	.dead_func_cnt = 1,
5995 	.dead_func_mask = 0x2,
5996 },
5997 
5998 {
5999 	.descr = "line_info (dead last subprog)",
6000 	.raw_types = {
6001 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6002 		BTF_FUNC_PROTO_ENC(1, 1),			/* [2] */
6003 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
6004 		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
6005 		BTF_FUNC_ENC(NAME_TBD, 2),			/* [5] */
6006 		BTF_END_RAW,
6007 	},
6008 	BTF_STR_SEC("\0int\0x\0dead\0main\0int a=1+1;\0/* live call */"
6009 		    "\0return 0;\0/* dead */\0/* dead */"),
6010 	.insns = {
6011 		BPF_MOV64_IMM(BPF_REG_2, 1),
6012 		BPF_JMP_IMM(BPF_JGE, BPF_REG_2, 0, 1),
6013 		BPF_CALL_REL(2),
6014 		BPF_MOV64_IMM(BPF_REG_0, 0),
6015 		BPF_EXIT_INSN(),
6016 		BPF_MOV64_IMM(BPF_REG_0, 0),
6017 		BPF_EXIT_INSN(),
6018 	},
6019 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
6020 	.func_info_cnt = 2,
6021 	.func_info_rec_size = 8,
6022 		.func_info = { {0, 4}, {5, 3} },
6023 	.line_info = {
6024 		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
6025 		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 1, 10),
6026 		BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 1, 10),
6027 		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 1, 10),
6028 		BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
6029 		BTF_END_RAW,
6030 	},
6031 	.line_info_rec_size = sizeof(struct bpf_line_info),
6032 	.nr_jited_ksyms = 1,
6033 	.dead_code_cnt = 2,
6034 	.dead_code_mask = 0x18,
6035 	.dead_func_cnt = 1,
6036 	.dead_func_mask = 0x2,
6037 },
6038 
6039 {
6040 	.descr = "line_info (dead subprog + dead start)",
6041 	.raw_types = {
6042 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6043 		BTF_FUNC_PROTO_ENC(1, 1),			/* [2] */
6044 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
6045 		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
6046 		BTF_FUNC_ENC(NAME_TBD, 2),			/* [4] */
6047 		BTF_FUNC_ENC(NAME_TBD, 2),			/* [5] */
6048 		BTF_END_RAW,
6049 	},
6050 	BTF_STR_SEC("\0int\0x\0dead\0main\0func\0int a=1+1;\0/* dead */"
6051 		    "\0return 0;\0return 0;\0return 0;"
6052 		    "\0/* dead */\0/* dead */\0/* dead */\0/* dead */"
6053 		    "\0return b + 1;\0return b + 1;\0return b + 1;"),
6054 	.insns = {
6055 		BPF_JMP_IMM(BPF_JA, 0, 0, 0),
6056 		BPF_MOV64_IMM(BPF_REG_2, 1),
6057 		BPF_JMP_IMM(BPF_JGE, BPF_REG_2, 0, 1),
6058 		BPF_CALL_REL(3),
6059 		BPF_CALL_REL(5),
6060 		BPF_MOV64_IMM(BPF_REG_0, 0),
6061 		BPF_EXIT_INSN(),
6062 		BPF_MOV64_IMM(BPF_REG_0, 0),
6063 		BPF_CALL_REL(1),
6064 		BPF_EXIT_INSN(),
6065 		BPF_JMP_IMM(BPF_JA, 0, 0, 0),
6066 		BPF_MOV64_REG(BPF_REG_0, 2),
6067 		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
6068 		BPF_EXIT_INSN(),
6069 	},
6070 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
6071 	.func_info_cnt = 3,
6072 	.func_info_rec_size = 8,
6073 		.func_info = { {0, 4}, {7, 3}, {10, 5} },
6074 	.line_info = {
6075 		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
6076 		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 1, 10),
6077 		BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 1, 10),
6078 		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 1, 10),
6079 		BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
6080 		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 1, 10),
6081 		BPF_LINE_INFO_ENC(8, 0, NAME_TBD, 1, 10),
6082 		BPF_LINE_INFO_ENC(9, 0, NAME_TBD, 1, 10),
6083 		BPF_LINE_INFO_ENC(10, 0, NAME_TBD, 1, 10),
6084 		BPF_LINE_INFO_ENC(11, 0, NAME_TBD, 2, 9),
6085 		BPF_LINE_INFO_ENC(12, 0, NAME_TBD, 2, 9),
6086 		BPF_LINE_INFO_ENC(13, 0, NAME_TBD, 2, 9),
6087 		BTF_END_RAW,
6088 	},
6089 	.line_info_rec_size = sizeof(struct bpf_line_info),
6090 	.nr_jited_ksyms = 2,
6091 	.dead_code_cnt = 5,
6092 	.dead_code_mask = 0x1e2,
6093 	.dead_func_cnt = 1,
6094 	.dead_func_mask = 0x2,
6095 },
6096 
6097 {
6098 	.descr = "line_info (dead subprog + dead start w/ move)",
6099 	.raw_types = {
6100 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6101 		BTF_FUNC_PROTO_ENC(1, 1),			/* [2] */
6102 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
6103 		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
6104 		BTF_FUNC_ENC(NAME_TBD, 2),			/* [4] */
6105 		BTF_FUNC_ENC(NAME_TBD, 2),			/* [5] */
6106 		BTF_END_RAW,
6107 	},
6108 	BTF_STR_SEC("\0int\0x\0dead\0main\0func\0int a=1+1;\0/* live call */"
6109 		    "\0return 0;\0return 0;\0/* dead */\0/* dead */"
6110 		    "\0/* dead */\0return bla + 1;\0return bla + 1;"
6111 		    "\0return bla + 1;\0return func(a);\0b+=1;\0return b;"),
6112 	.insns = {
6113 		BPF_MOV64_IMM(BPF_REG_2, 1),
6114 		BPF_JMP_IMM(BPF_JGE, BPF_REG_2, 0, 1),
6115 		BPF_CALL_REL(3),
6116 		BPF_CALL_REL(5),
6117 		BPF_MOV64_IMM(BPF_REG_0, 0),
6118 		BPF_EXIT_INSN(),
6119 		BPF_MOV64_IMM(BPF_REG_0, 0),
6120 		BPF_CALL_REL(1),
6121 		BPF_EXIT_INSN(),
6122 		BPF_JMP_IMM(BPF_JA, 0, 0, 0),
6123 		BPF_MOV64_REG(BPF_REG_0, 2),
6124 		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
6125 		BPF_EXIT_INSN(),
6126 	},
6127 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
6128 	.func_info_cnt = 3,
6129 	.func_info_rec_size = 8,
6130 		.func_info = { {0, 4}, {6, 3}, {9, 5} },
6131 	.line_info = {
6132 		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
6133 		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 1, 10),
6134 		BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 1, 10),
6135 		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 1, 10),
6136 		BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
6137 		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 1, 10),
6138 		BPF_LINE_INFO_ENC(8, 0, NAME_TBD, 1, 10),
6139 		BPF_LINE_INFO_ENC(9, 0, NAME_TBD, 1, 10),
6140 		BPF_LINE_INFO_ENC(11, 0, NAME_TBD, 1, 10),
6141 		BPF_LINE_INFO_ENC(12, 0, NAME_TBD, 2, 9),
6142 		BTF_END_RAW,
6143 	},
6144 	.line_info_rec_size = sizeof(struct bpf_line_info),
6145 	.nr_jited_ksyms = 2,
6146 	.dead_code_cnt = 3,
6147 	.dead_code_mask = 0x70,
6148 	.dead_func_cnt = 1,
6149 	.dead_func_mask = 0x2,
6150 },
6151 
6152 {
6153 	.descr = "line_info (dead end + subprog start w/ no linfo)",
6154 	.raw_types = {
6155 		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6156 		BTF_FUNC_PROTO_ENC(1, 1),			/* [2] */
6157 			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
6158 		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
6159 		BTF_FUNC_ENC(NAME_TBD, 2),			/* [4] */
6160 		BTF_END_RAW,
6161 	},
6162 	BTF_STR_SEC("\0int\0x\0main\0func\0/* main linfo */\0/* func linfo */"),
6163 	.insns = {
6164 		BPF_MOV64_IMM(BPF_REG_0, 0),
6165 		BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 1, 3),
6166 		BPF_CALL_REL(3),
6167 		BPF_MOV64_IMM(BPF_REG_0, 0),
6168 		BPF_EXIT_INSN(),
6169 		BPF_EXIT_INSN(),
6170 		BPF_JMP_IMM(BPF_JA, 0, 0, 0),
6171 		BPF_EXIT_INSN(),
6172 	},
6173 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
6174 	.func_info_cnt = 2,
6175 	.func_info_rec_size = 8,
6176 	.func_info = { {0, 3}, {6, 4}, },
6177 	.line_info = {
6178 		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
6179 		BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
6180 		BTF_END_RAW,
6181 	},
6182 	.line_info_rec_size = sizeof(struct bpf_line_info),
6183 	.nr_jited_ksyms = 2,
6184 },
6185 
6186 };
6187 
6188 static size_t probe_prog_length(const struct bpf_insn *fp)
6189 {
6190 	size_t len;
6191 
6192 	for (len = MAX_INSNS - 1; len > 0; --len)
6193 		if (fp[len].code != 0 || fp[len].imm != 0)
6194 			break;
6195 	return len + 1;
6196 }
6197 
6198 static __u32 *patch_name_tbd(const __u32 *raw_u32,
6199 			     const char *str, __u32 str_off,
6200 			     unsigned int str_sec_size,
6201 			     unsigned int *ret_size)
6202 {
6203 	int i, raw_u32_size = get_raw_sec_size(raw_u32);
6204 	const char *end_str = str + str_sec_size;
6205 	const char *next_str = str + str_off;
6206 	__u32 *new_u32 = NULL;
6207 
6208 	if (raw_u32_size == -1)
6209 		return ERR_PTR(-EINVAL);
6210 
6211 	if (!raw_u32_size) {
6212 		*ret_size = 0;
6213 		return NULL;
6214 	}
6215 
6216 	new_u32 = malloc(raw_u32_size);
6217 	if (!new_u32)
6218 		return ERR_PTR(-ENOMEM);
6219 
6220 	for (i = 0; i < raw_u32_size / sizeof(raw_u32[0]); i++) {
6221 		if (raw_u32[i] == NAME_TBD) {
6222 			next_str = get_next_str(next_str, end_str);
6223 			if (CHECK(!next_str, "Error in getting next_str\n")) {
6224 				free(new_u32);
6225 				return ERR_PTR(-EINVAL);
6226 			}
6227 			new_u32[i] = next_str - str;
6228 			next_str += strlen(next_str);
6229 		} else {
6230 			new_u32[i] = raw_u32[i];
6231 		}
6232 	}
6233 
6234 	*ret_size = raw_u32_size;
6235 	return new_u32;
6236 }
6237 
6238 static int test_get_finfo(const struct prog_info_raw_test *test,
6239 			  int prog_fd)
6240 {
6241 	struct bpf_prog_info info = {};
6242 	struct bpf_func_info *finfo;
6243 	__u32 info_len, rec_size, i;
6244 	void *func_info = NULL;
6245 	__u32 nr_func_info;
6246 	int err;
6247 
6248 	/* get necessary lens */
6249 	info_len = sizeof(struct bpf_prog_info);
6250 	err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
6251 	if (CHECK(err < 0, "invalid get info (1st) errno:%d", errno)) {
6252 		fprintf(stderr, "%s\n", btf_log_buf);
6253 		return -1;
6254 	}
6255 	nr_func_info = test->func_info_cnt - test->dead_func_cnt;
6256 	if (CHECK(info.nr_func_info != nr_func_info,
6257 		  "incorrect info.nr_func_info (1st) %d",
6258 		  info.nr_func_info)) {
6259 		return -1;
6260 	}
6261 
6262 	rec_size = info.func_info_rec_size;
6263 	if (CHECK(rec_size != sizeof(struct bpf_func_info),
6264 		  "incorrect info.func_info_rec_size (1st) %d", rec_size)) {
6265 		return -1;
6266 	}
6267 
6268 	if (!info.nr_func_info)
6269 		return 0;
6270 
6271 	func_info = malloc(info.nr_func_info * rec_size);
6272 	if (CHECK(!func_info, "out of memory"))
6273 		return -1;
6274 
6275 	/* reset info to only retrieve func_info related data */
6276 	memset(&info, 0, sizeof(info));
6277 	info.nr_func_info = nr_func_info;
6278 	info.func_info_rec_size = rec_size;
6279 	info.func_info = ptr_to_u64(func_info);
6280 	err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
6281 	if (CHECK(err < 0, "invalid get info (2nd) errno:%d", errno)) {
6282 		fprintf(stderr, "%s\n", btf_log_buf);
6283 		err = -1;
6284 		goto done;
6285 	}
6286 	if (CHECK(info.nr_func_info != nr_func_info,
6287 		  "incorrect info.nr_func_info (2nd) %d",
6288 		  info.nr_func_info)) {
6289 		err = -1;
6290 		goto done;
6291 	}
6292 	if (CHECK(info.func_info_rec_size != rec_size,
6293 		  "incorrect info.func_info_rec_size (2nd) %d",
6294 		  info.func_info_rec_size)) {
6295 		err = -1;
6296 		goto done;
6297 	}
6298 
6299 	finfo = func_info;
6300 	for (i = 0; i < nr_func_info; i++) {
6301 		if (test->dead_func_mask & (1 << i))
6302 			continue;
6303 		if (CHECK(finfo->type_id != test->func_info[i][1],
6304 			  "incorrect func_type %u expected %u",
6305 			  finfo->type_id, test->func_info[i][1])) {
6306 			err = -1;
6307 			goto done;
6308 		}
6309 		finfo = (void *)finfo + rec_size;
6310 	}
6311 
6312 	err = 0;
6313 
6314 done:
6315 	free(func_info);
6316 	return err;
6317 }
6318 
6319 static int test_get_linfo(const struct prog_info_raw_test *test,
6320 			  const void *patched_linfo,
6321 			  __u32 cnt, int prog_fd)
6322 {
6323 	__u32 i, info_len, nr_jited_ksyms, nr_jited_func_lens;
6324 	__u64 *jited_linfo = NULL, *jited_ksyms = NULL;
6325 	__u32 rec_size, jited_rec_size, jited_cnt;
6326 	struct bpf_line_info *linfo = NULL;
6327 	__u32 cur_func_len, ksyms_found;
6328 	struct bpf_prog_info info = {};
6329 	__u32 *jited_func_lens = NULL;
6330 	__u64 cur_func_ksyms;
6331 	__u32 dead_insns;
6332 	int err;
6333 
6334 	jited_cnt = cnt;
6335 	rec_size = sizeof(*linfo);
6336 	jited_rec_size = sizeof(*jited_linfo);
6337 	if (test->nr_jited_ksyms)
6338 		nr_jited_ksyms = test->nr_jited_ksyms;
6339 	else
6340 		nr_jited_ksyms = test->func_info_cnt - test->dead_func_cnt;
6341 	nr_jited_func_lens = nr_jited_ksyms;
6342 
6343 	info_len = sizeof(struct bpf_prog_info);
6344 	err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
6345 	if (CHECK(err < 0, "err:%d errno:%d", err, errno)) {
6346 		err = -1;
6347 		goto done;
6348 	}
6349 
6350 	if (!info.jited_prog_len) {
6351 		/* prog is not jited */
6352 		jited_cnt = 0;
6353 		nr_jited_ksyms = 1;
6354 		nr_jited_func_lens = 1;
6355 	}
6356 
6357 	if (CHECK(info.nr_line_info != cnt ||
6358 		  info.nr_jited_line_info != jited_cnt ||
6359 		  info.nr_jited_ksyms != nr_jited_ksyms ||
6360 		  info.nr_jited_func_lens != nr_jited_func_lens ||
6361 		  (!info.nr_line_info && info.nr_jited_line_info),
6362 		  "info: nr_line_info:%u(expected:%u) nr_jited_line_info:%u(expected:%u) nr_jited_ksyms:%u(expected:%u) nr_jited_func_lens:%u(expected:%u)",
6363 		  info.nr_line_info, cnt,
6364 		  info.nr_jited_line_info, jited_cnt,
6365 		  info.nr_jited_ksyms, nr_jited_ksyms,
6366 		  info.nr_jited_func_lens, nr_jited_func_lens)) {
6367 		err = -1;
6368 		goto done;
6369 	}
6370 
6371 	if (CHECK(info.line_info_rec_size != sizeof(struct bpf_line_info) ||
6372 		  info.jited_line_info_rec_size != sizeof(__u64),
6373 		  "info: line_info_rec_size:%u(userspace expected:%u) jited_line_info_rec_size:%u(userspace expected:%u)",
6374 		  info.line_info_rec_size, rec_size,
6375 		  info.jited_line_info_rec_size, jited_rec_size)) {
6376 		err = -1;
6377 		goto done;
6378 	}
6379 
6380 	if (!cnt)
6381 		return 0;
6382 
6383 	rec_size = info.line_info_rec_size;
6384 	jited_rec_size = info.jited_line_info_rec_size;
6385 
6386 	memset(&info, 0, sizeof(info));
6387 
6388 	linfo = calloc(cnt, rec_size);
6389 	if (CHECK(!linfo, "!linfo")) {
6390 		err = -1;
6391 		goto done;
6392 	}
6393 	info.nr_line_info = cnt;
6394 	info.line_info_rec_size = rec_size;
6395 	info.line_info = ptr_to_u64(linfo);
6396 
6397 	if (jited_cnt) {
6398 		jited_linfo = calloc(jited_cnt, jited_rec_size);
6399 		jited_ksyms = calloc(nr_jited_ksyms, sizeof(*jited_ksyms));
6400 		jited_func_lens = calloc(nr_jited_func_lens,
6401 					 sizeof(*jited_func_lens));
6402 		if (CHECK(!jited_linfo || !jited_ksyms || !jited_func_lens,
6403 			  "jited_linfo:%p jited_ksyms:%p jited_func_lens:%p",
6404 			  jited_linfo, jited_ksyms, jited_func_lens)) {
6405 			err = -1;
6406 			goto done;
6407 		}
6408 
6409 		info.nr_jited_line_info = jited_cnt;
6410 		info.jited_line_info_rec_size = jited_rec_size;
6411 		info.jited_line_info = ptr_to_u64(jited_linfo);
6412 		info.nr_jited_ksyms = nr_jited_ksyms;
6413 		info.jited_ksyms = ptr_to_u64(jited_ksyms);
6414 		info.nr_jited_func_lens = nr_jited_func_lens;
6415 		info.jited_func_lens = ptr_to_u64(jited_func_lens);
6416 	}
6417 
6418 	err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
6419 
6420 	/*
6421 	 * Only recheck the info.*line_info* fields.
6422 	 * Other fields are not the concern of this test.
6423 	 */
6424 	if (CHECK(err < 0 ||
6425 		  info.nr_line_info != cnt ||
6426 		  (jited_cnt && !info.jited_line_info) ||
6427 		  info.nr_jited_line_info != jited_cnt ||
6428 		  info.line_info_rec_size != rec_size ||
6429 		  info.jited_line_info_rec_size != jited_rec_size,
6430 		  "err:%d errno:%d info: nr_line_info:%u(expected:%u) nr_jited_line_info:%u(expected:%u) line_info_rec_size:%u(expected:%u) jited_linfo_rec_size:%u(expected:%u) line_info:%p jited_line_info:%p",
6431 		  err, errno,
6432 		  info.nr_line_info, cnt,
6433 		  info.nr_jited_line_info, jited_cnt,
6434 		  info.line_info_rec_size, rec_size,
6435 		  info.jited_line_info_rec_size, jited_rec_size,
6436 		  (void *)(long)info.line_info,
6437 		  (void *)(long)info.jited_line_info)) {
6438 		err = -1;
6439 		goto done;
6440 	}
6441 
6442 	dead_insns = 0;
6443 	while (test->dead_code_mask & (1 << dead_insns))
6444 		dead_insns++;
6445 
6446 	CHECK(linfo[0].insn_off, "linfo[0].insn_off:%u",
6447 	      linfo[0].insn_off);
6448 	for (i = 1; i < cnt; i++) {
6449 		const struct bpf_line_info *expected_linfo;
6450 
6451 		while (test->dead_code_mask & (1 << (i + dead_insns)))
6452 			dead_insns++;
6453 
6454 		expected_linfo = patched_linfo +
6455 			((i + dead_insns) * test->line_info_rec_size);
6456 		if (CHECK(linfo[i].insn_off <= linfo[i - 1].insn_off,
6457 			  "linfo[%u].insn_off:%u <= linfo[%u].insn_off:%u",
6458 			  i, linfo[i].insn_off,
6459 			  i - 1, linfo[i - 1].insn_off)) {
6460 			err = -1;
6461 			goto done;
6462 		}
6463 		if (CHECK(linfo[i].file_name_off != expected_linfo->file_name_off ||
6464 			  linfo[i].line_off != expected_linfo->line_off ||
6465 			  linfo[i].line_col != expected_linfo->line_col,
6466 			  "linfo[%u] (%u, %u, %u) != (%u, %u, %u)", i,
6467 			  linfo[i].file_name_off,
6468 			  linfo[i].line_off,
6469 			  linfo[i].line_col,
6470 			  expected_linfo->file_name_off,
6471 			  expected_linfo->line_off,
6472 			  expected_linfo->line_col)) {
6473 			err = -1;
6474 			goto done;
6475 		}
6476 	}
6477 
6478 	if (!jited_cnt) {
6479 		fprintf(stderr, "not jited. skipping jited_line_info check. ");
6480 		err = 0;
6481 		goto done;
6482 	}
6483 
6484 	if (CHECK(jited_linfo[0] != jited_ksyms[0],
6485 		  "jited_linfo[0]:%lx != jited_ksyms[0]:%lx",
6486 		  (long)(jited_linfo[0]), (long)(jited_ksyms[0]))) {
6487 		err = -1;
6488 		goto done;
6489 	}
6490 
6491 	ksyms_found = 1;
6492 	cur_func_len = jited_func_lens[0];
6493 	cur_func_ksyms = jited_ksyms[0];
6494 	for (i = 1; i < jited_cnt; i++) {
6495 		if (ksyms_found < nr_jited_ksyms &&
6496 		    jited_linfo[i] == jited_ksyms[ksyms_found]) {
6497 			cur_func_ksyms = jited_ksyms[ksyms_found];
6498 			cur_func_len = jited_ksyms[ksyms_found];
6499 			ksyms_found++;
6500 			continue;
6501 		}
6502 
6503 		if (CHECK(jited_linfo[i] <= jited_linfo[i - 1],
6504 			  "jited_linfo[%u]:%lx <= jited_linfo[%u]:%lx",
6505 			  i, (long)jited_linfo[i],
6506 			  i - 1, (long)(jited_linfo[i - 1]))) {
6507 			err = -1;
6508 			goto done;
6509 		}
6510 
6511 		if (CHECK(jited_linfo[i] - cur_func_ksyms > cur_func_len,
6512 			  "jited_linfo[%u]:%lx - %lx > %u",
6513 			  i, (long)jited_linfo[i], (long)cur_func_ksyms,
6514 			  cur_func_len)) {
6515 			err = -1;
6516 			goto done;
6517 		}
6518 	}
6519 
6520 	if (CHECK(ksyms_found != nr_jited_ksyms,
6521 		  "ksyms_found:%u != nr_jited_ksyms:%u",
6522 		  ksyms_found, nr_jited_ksyms)) {
6523 		err = -1;
6524 		goto done;
6525 	}
6526 
6527 	err = 0;
6528 
6529 done:
6530 	free(linfo);
6531 	free(jited_linfo);
6532 	free(jited_ksyms);
6533 	free(jited_func_lens);
6534 	return err;
6535 }
6536 
6537 static void do_test_info_raw(unsigned int test_num)
6538 {
6539 	const struct prog_info_raw_test *test = &info_raw_tests[test_num - 1];
6540 	unsigned int raw_btf_size, linfo_str_off, linfo_size;
6541 	int btf_fd = -1, prog_fd = -1, err = 0;
6542 	void *raw_btf, *patched_linfo = NULL;
6543 	const char *ret_next_str;
6544 	union bpf_attr attr = {};
6545 
6546 	if (!test__start_subtest(test->descr))
6547 		return;
6548 
6549 	raw_btf = btf_raw_create(&hdr_tmpl, test->raw_types,
6550 				 test->str_sec, test->str_sec_size,
6551 				 &raw_btf_size, &ret_next_str);
6552 	if (!raw_btf)
6553 		return;
6554 
6555 	*btf_log_buf = '\0';
6556 	btf_fd = load_raw_btf(raw_btf, raw_btf_size);
6557 	free(raw_btf);
6558 
6559 	if (CHECK(btf_fd < 0, "invalid btf_fd errno:%d", errno)) {
6560 		err = -1;
6561 		goto done;
6562 	}
6563 
6564 	if (*btf_log_buf && always_log)
6565 		fprintf(stderr, "\n%s", btf_log_buf);
6566 	*btf_log_buf = '\0';
6567 
6568 	linfo_str_off = ret_next_str - test->str_sec;
6569 	patched_linfo = patch_name_tbd(test->line_info,
6570 				       test->str_sec, linfo_str_off,
6571 				       test->str_sec_size, &linfo_size);
6572 	err = libbpf_get_error(patched_linfo);
6573 	if (err) {
6574 		fprintf(stderr, "error in creating raw bpf_line_info");
6575 		err = -1;
6576 		goto done;
6577 	}
6578 
6579 	attr.prog_type = test->prog_type;
6580 	attr.insns = ptr_to_u64(test->insns);
6581 	attr.insn_cnt = probe_prog_length(test->insns);
6582 	attr.license = ptr_to_u64("GPL");
6583 	attr.prog_btf_fd = btf_fd;
6584 	attr.func_info_rec_size = test->func_info_rec_size;
6585 	attr.func_info_cnt = test->func_info_cnt;
6586 	attr.func_info = ptr_to_u64(test->func_info);
6587 	attr.log_buf = ptr_to_u64(btf_log_buf);
6588 	attr.log_size = BTF_LOG_BUF_SIZE;
6589 	attr.log_level = 1;
6590 	if (linfo_size) {
6591 		attr.line_info_rec_size = test->line_info_rec_size;
6592 		attr.line_info = ptr_to_u64(patched_linfo);
6593 		attr.line_info_cnt = linfo_size / attr.line_info_rec_size;
6594 	}
6595 
6596 	prog_fd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
6597 	err = ((prog_fd < 0) != test->expected_prog_load_failure);
6598 	if (CHECK(err, "prog_fd:%d expected_prog_load_failure:%u errno:%d",
6599 		  prog_fd, test->expected_prog_load_failure, errno) ||
6600 	    CHECK(test->err_str && !strstr(btf_log_buf, test->err_str),
6601 		  "expected err_str:%s", test->err_str)) {
6602 		err = -1;
6603 		goto done;
6604 	}
6605 
6606 	if (prog_fd < 0)
6607 		goto done;
6608 
6609 	err = test_get_finfo(test, prog_fd);
6610 	if (err)
6611 		goto done;
6612 
6613 	err = test_get_linfo(test, patched_linfo,
6614 			     attr.line_info_cnt - test->dead_code_cnt,
6615 			     prog_fd);
6616 	if (err)
6617 		goto done;
6618 
6619 done:
6620 	if (*btf_log_buf && (err || always_log))
6621 		fprintf(stderr, "\n%s", btf_log_buf);
6622 
6623 	if (btf_fd >= 0)
6624 		close(btf_fd);
6625 	if (prog_fd >= 0)
6626 		close(prog_fd);
6627 
6628 	if (!libbpf_get_error(patched_linfo))
6629 		free(patched_linfo);
6630 }
6631 
6632 struct btf_raw_data {
6633 	__u32 raw_types[MAX_NR_RAW_U32];
6634 	const char *str_sec;
6635 	__u32 str_sec_size;
6636 };
6637 
6638 struct btf_dedup_test {
6639 	const char *descr;
6640 	struct btf_raw_data input;
6641 	struct btf_raw_data expect;
6642 	struct btf_dedup_opts opts;
6643 };
6644 
6645 static struct btf_dedup_test dedup_tests[] = {
6646 
6647 {
6648 	.descr = "dedup: unused strings filtering",
6649 	.input = {
6650 		.raw_types = {
6651 			BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 32, 4),
6652 			BTF_TYPE_INT_ENC(NAME_NTH(5), BTF_INT_SIGNED, 0, 64, 8),
6653 			BTF_END_RAW,
6654 		},
6655 		BTF_STR_SEC("\0unused\0int\0foo\0bar\0long"),
6656 	},
6657 	.expect = {
6658 		.raw_types = {
6659 			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),
6660 			BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 64, 8),
6661 			BTF_END_RAW,
6662 		},
6663 		BTF_STR_SEC("\0int\0long"),
6664 	},
6665 },
6666 {
6667 	.descr = "dedup: strings deduplication",
6668 	.input = {
6669 		.raw_types = {
6670 			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),
6671 			BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 64, 8),
6672 			BTF_TYPE_INT_ENC(NAME_NTH(3), BTF_INT_SIGNED, 0, 32, 4),
6673 			BTF_TYPE_INT_ENC(NAME_NTH(4), BTF_INT_SIGNED, 0, 64, 8),
6674 			BTF_TYPE_INT_ENC(NAME_NTH(5), BTF_INT_SIGNED, 0, 32, 4),
6675 			BTF_END_RAW,
6676 		},
6677 		BTF_STR_SEC("\0int\0long int\0int\0long int\0int"),
6678 	},
6679 	.expect = {
6680 		.raw_types = {
6681 			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),
6682 			BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 64, 8),
6683 			BTF_END_RAW,
6684 		},
6685 		BTF_STR_SEC("\0int\0long int"),
6686 	},
6687 },
6688 {
6689 	.descr = "dedup: struct example #1",
6690 	/*
6691 	 * struct s {
6692 	 *	struct s *next;
6693 	 *	const int *a;
6694 	 *	int b[16];
6695 	 *	int c;
6696 	 * }
6697 	 */
6698 	.input = {
6699 		.raw_types = {
6700 			/* int */
6701 			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6702 			/* int[16] */
6703 			BTF_TYPE_ARRAY_ENC(1, 1, 16),					/* [2] */
6704 			/* struct s { */
6705 			BTF_STRUCT_ENC(NAME_NTH(2), 5, 88),				/* [3] */
6706 				BTF_MEMBER_ENC(NAME_NTH(3), 4, 0),	/* struct s *next;	*/
6707 				BTF_MEMBER_ENC(NAME_NTH(4), 5, 64),	/* const int *a;	*/
6708 				BTF_MEMBER_ENC(NAME_NTH(5), 2, 128),	/* int b[16];		*/
6709 				BTF_MEMBER_ENC(NAME_NTH(6), 1, 640),	/* int c;		*/
6710 				BTF_MEMBER_ENC(NAME_NTH(8), 15, 672),	/* float d;		*/
6711 			/* ptr -> [3] struct s */
6712 			BTF_PTR_ENC(3),							/* [4] */
6713 			/* ptr -> [6] const int */
6714 			BTF_PTR_ENC(6),							/* [5] */
6715 			/* const -> [1] int */
6716 			BTF_CONST_ENC(1),						/* [6] */
6717 			/* tag -> [3] struct s */
6718 			BTF_DECL_TAG_ENC(NAME_NTH(2), 3, -1),				/* [7] */
6719 			/* tag -> [3] struct s, member 1 */
6720 			BTF_DECL_TAG_ENC(NAME_NTH(2), 3, 1),				/* [8] */
6721 
6722 			/* full copy of the above */
6723 			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),	/* [9] */
6724 			BTF_TYPE_ARRAY_ENC(9, 9, 16),					/* [10] */
6725 			BTF_STRUCT_ENC(NAME_NTH(2), 5, 88),				/* [11] */
6726 				BTF_MEMBER_ENC(NAME_NTH(3), 12, 0),
6727 				BTF_MEMBER_ENC(NAME_NTH(4), 13, 64),
6728 				BTF_MEMBER_ENC(NAME_NTH(5), 10, 128),
6729 				BTF_MEMBER_ENC(NAME_NTH(6), 9, 640),
6730 				BTF_MEMBER_ENC(NAME_NTH(8), 15, 672),
6731 			BTF_PTR_ENC(11),						/* [12] */
6732 			BTF_PTR_ENC(14),						/* [13] */
6733 			BTF_CONST_ENC(9),						/* [14] */
6734 			BTF_TYPE_FLOAT_ENC(NAME_NTH(7), 4),				/* [15] */
6735 			BTF_DECL_TAG_ENC(NAME_NTH(2), 11, -1),				/* [16] */
6736 			BTF_DECL_TAG_ENC(NAME_NTH(2), 11, 1),				/* [17] */
6737 			BTF_END_RAW,
6738 		},
6739 		BTF_STR_SEC("\0int\0s\0next\0a\0b\0c\0float\0d"),
6740 	},
6741 	.expect = {
6742 		.raw_types = {
6743 			/* int */
6744 			BTF_TYPE_INT_ENC(NAME_NTH(5), BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6745 			/* int[16] */
6746 			BTF_TYPE_ARRAY_ENC(1, 1, 16),					/* [2] */
6747 			/* struct s { */
6748 			BTF_STRUCT_ENC(NAME_NTH(8), 5, 88),				/* [3] */
6749 				BTF_MEMBER_ENC(NAME_NTH(7), 4, 0),	/* struct s *next;	*/
6750 				BTF_MEMBER_ENC(NAME_NTH(1), 5, 64),	/* const int *a;	*/
6751 				BTF_MEMBER_ENC(NAME_NTH(2), 2, 128),	/* int b[16];		*/
6752 				BTF_MEMBER_ENC(NAME_NTH(3), 1, 640),	/* int c;		*/
6753 				BTF_MEMBER_ENC(NAME_NTH(4), 9, 672),	/* float d;		*/
6754 			/* ptr -> [3] struct s */
6755 			BTF_PTR_ENC(3),							/* [4] */
6756 			/* ptr -> [6] const int */
6757 			BTF_PTR_ENC(6),							/* [5] */
6758 			/* const -> [1] int */
6759 			BTF_CONST_ENC(1),						/* [6] */
6760 			BTF_DECL_TAG_ENC(NAME_NTH(2), 3, -1),				/* [7] */
6761 			BTF_DECL_TAG_ENC(NAME_NTH(2), 3, 1),				/* [8] */
6762 			BTF_TYPE_FLOAT_ENC(NAME_NTH(7), 4),				/* [9] */
6763 			BTF_END_RAW,
6764 		},
6765 		BTF_STR_SEC("\0a\0b\0c\0d\0int\0float\0next\0s"),
6766 	},
6767 },
6768 {
6769 	.descr = "dedup: struct <-> fwd resolution w/ hash collision",
6770 	/*
6771 	 * // CU 1:
6772 	 * struct x;
6773 	 * struct s {
6774 	 *	struct x *x;
6775 	 * };
6776 	 * // CU 2:
6777 	 * struct x {};
6778 	 * struct s {
6779 	 *	struct x *x;
6780 	 * };
6781 	 */
6782 	.input = {
6783 		.raw_types = {
6784 			/* CU 1 */
6785 			BTF_FWD_ENC(NAME_TBD, 0 /* struct fwd */),	/* [1] fwd x      */
6786 			BTF_PTR_ENC(1),					/* [2] ptr -> [1] */
6787 			BTF_STRUCT_ENC(NAME_TBD, 1, 8),			/* [3] struct s   */
6788 				BTF_MEMBER_ENC(NAME_TBD, 2, 0),
6789 			/* CU 2 */
6790 			BTF_STRUCT_ENC(NAME_TBD, 0, 0),			/* [4] struct x   */
6791 			BTF_PTR_ENC(4),					/* [5] ptr -> [4] */
6792 			BTF_STRUCT_ENC(NAME_TBD, 1, 8),			/* [6] struct s   */
6793 				BTF_MEMBER_ENC(NAME_TBD, 5, 0),
6794 			BTF_END_RAW,
6795 		},
6796 		BTF_STR_SEC("\0x\0s\0x\0x\0s\0x\0"),
6797 	},
6798 	.expect = {
6799 		.raw_types = {
6800 			BTF_PTR_ENC(3),					/* [1] ptr -> [3] */
6801 			BTF_STRUCT_ENC(NAME_TBD, 1, 8),			/* [2] struct s   */
6802 				BTF_MEMBER_ENC(NAME_TBD, 1, 0),
6803 			BTF_STRUCT_ENC(NAME_NTH(2), 0, 0),		/* [3] struct x   */
6804 			BTF_END_RAW,
6805 		},
6806 		BTF_STR_SEC("\0s\0x"),
6807 	},
6808 	.opts = {
6809 		.force_collisions = true, /* force hash collisions */
6810 	},
6811 },
6812 {
6813 	.descr = "dedup: void equiv check",
6814 	/*
6815 	 * // CU 1:
6816 	 * struct s {
6817 	 *	struct {} *x;
6818 	 * };
6819 	 * // CU 2:
6820 	 * struct s {
6821 	 *	int *x;
6822 	 * };
6823 	 */
6824 	.input = {
6825 		.raw_types = {
6826 			/* CU 1 */
6827 			BTF_STRUCT_ENC(0, 0, 1),				/* [1] struct {}  */
6828 			BTF_PTR_ENC(1),						/* [2] ptr -> [1] */
6829 			BTF_STRUCT_ENC(NAME_NTH(1), 1, 8),			/* [3] struct s   */
6830 				BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
6831 			/* CU 2 */
6832 			BTF_PTR_ENC(0),						/* [4] ptr -> void */
6833 			BTF_STRUCT_ENC(NAME_NTH(1), 1, 8),			/* [5] struct s   */
6834 				BTF_MEMBER_ENC(NAME_NTH(2), 4, 0),
6835 			BTF_END_RAW,
6836 		},
6837 		BTF_STR_SEC("\0s\0x"),
6838 	},
6839 	.expect = {
6840 		.raw_types = {
6841 			/* CU 1 */
6842 			BTF_STRUCT_ENC(0, 0, 1),				/* [1] struct {}  */
6843 			BTF_PTR_ENC(1),						/* [2] ptr -> [1] */
6844 			BTF_STRUCT_ENC(NAME_NTH(1), 1, 8),			/* [3] struct s   */
6845 				BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
6846 			/* CU 2 */
6847 			BTF_PTR_ENC(0),						/* [4] ptr -> void */
6848 			BTF_STRUCT_ENC(NAME_NTH(1), 1, 8),			/* [5] struct s   */
6849 				BTF_MEMBER_ENC(NAME_NTH(2), 4, 0),
6850 			BTF_END_RAW,
6851 		},
6852 		BTF_STR_SEC("\0s\0x"),
6853 	},
6854 	.opts = {
6855 		.force_collisions = true, /* force hash collisions */
6856 	},
6857 },
6858 {
6859 	.descr = "dedup: all possible kinds (no duplicates)",
6860 	.input = {
6861 		.raw_types = {
6862 			BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 8),		/* [1] int */
6863 			BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), 4),	/* [2] enum */
6864 				BTF_ENUM_ENC(NAME_TBD, 0),
6865 				BTF_ENUM_ENC(NAME_TBD, 1),
6866 			BTF_FWD_ENC(NAME_TBD, 1 /* union kind_flag */),			/* [3] fwd */
6867 			BTF_TYPE_ARRAY_ENC(2, 1, 7),					/* [4] array */
6868 			BTF_STRUCT_ENC(NAME_TBD, 1, 4),					/* [5] struct */
6869 				BTF_MEMBER_ENC(NAME_TBD, 1, 0),
6870 			BTF_UNION_ENC(NAME_TBD, 1, 4),					/* [6] union */
6871 				BTF_MEMBER_ENC(NAME_TBD, 1, 0),
6872 			BTF_TYPEDEF_ENC(NAME_TBD, 1),					/* [7] typedef */
6873 			BTF_PTR_ENC(0),							/* [8] ptr */
6874 			BTF_CONST_ENC(8),						/* [9] const */
6875 			BTF_VOLATILE_ENC(8),						/* [10] volatile */
6876 			BTF_RESTRICT_ENC(8),						/* [11] restrict */
6877 			BTF_FUNC_PROTO_ENC(1, 2),					/* [12] func_proto */
6878 				BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
6879 				BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 18),
6880 			BTF_FUNC_ENC(NAME_TBD, 12),					/* [13] func */
6881 			BTF_TYPE_FLOAT_ENC(NAME_TBD, 2),				/* [14] float */
6882 			BTF_DECL_TAG_ENC(NAME_TBD, 13, -1),				/* [15] decl_tag */
6883 			BTF_DECL_TAG_ENC(NAME_TBD, 13, 1),				/* [16] decl_tag */
6884 			BTF_DECL_TAG_ENC(NAME_TBD, 7, -1),				/* [17] decl_tag */
6885 			BTF_TYPE_TAG_ENC(NAME_TBD, 8),					/* [18] type_tag */
6886 			BTF_END_RAW,
6887 		},
6888 		BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q\0R"),
6889 	},
6890 	.expect = {
6891 		.raw_types = {
6892 			BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 8),		/* [1] int */
6893 			BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), 4),	/* [2] enum */
6894 				BTF_ENUM_ENC(NAME_TBD, 0),
6895 				BTF_ENUM_ENC(NAME_TBD, 1),
6896 			BTF_FWD_ENC(NAME_TBD, 1 /* union kind_flag */),			/* [3] fwd */
6897 			BTF_TYPE_ARRAY_ENC(2, 1, 7),					/* [4] array */
6898 			BTF_STRUCT_ENC(NAME_TBD, 1, 4),					/* [5] struct */
6899 				BTF_MEMBER_ENC(NAME_TBD, 1, 0),
6900 			BTF_UNION_ENC(NAME_TBD, 1, 4),					/* [6] union */
6901 				BTF_MEMBER_ENC(NAME_TBD, 1, 0),
6902 			BTF_TYPEDEF_ENC(NAME_TBD, 1),					/* [7] typedef */
6903 			BTF_PTR_ENC(0),							/* [8] ptr */
6904 			BTF_CONST_ENC(8),						/* [9] const */
6905 			BTF_VOLATILE_ENC(8),						/* [10] volatile */
6906 			BTF_RESTRICT_ENC(8),						/* [11] restrict */
6907 			BTF_FUNC_PROTO_ENC(1, 2),					/* [12] func_proto */
6908 				BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
6909 				BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 18),
6910 			BTF_FUNC_ENC(NAME_TBD, 12),					/* [13] func */
6911 			BTF_TYPE_FLOAT_ENC(NAME_TBD, 2),				/* [14] float */
6912 			BTF_DECL_TAG_ENC(NAME_TBD, 13, -1),				/* [15] decl_tag */
6913 			BTF_DECL_TAG_ENC(NAME_TBD, 13, 1),				/* [16] decl_tag */
6914 			BTF_DECL_TAG_ENC(NAME_TBD, 7, -1),				/* [17] decl_tag */
6915 			BTF_TYPE_TAG_ENC(NAME_TBD, 8),					/* [18] type_tag */
6916 			BTF_END_RAW,
6917 		},
6918 		BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q\0R"),
6919 	},
6920 },
6921 {
6922 	.descr = "dedup: no int/float duplicates",
6923 	.input = {
6924 		.raw_types = {
6925 			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 8),
6926 			/* different name */
6927 			BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 32, 8),
6928 			/* different encoding */
6929 			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_CHAR, 0, 32, 8),
6930 			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_BOOL, 0, 32, 8),
6931 			/* different bit offset */
6932 			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 8, 32, 8),
6933 			/* different bit size */
6934 			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 27, 8),
6935 			/* different byte size */
6936 			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),
6937 			/* all allowed sizes */
6938 			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 2),
6939 			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 4),
6940 			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 8),
6941 			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 12),
6942 			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 16),
6943 			BTF_END_RAW,
6944 		},
6945 		BTF_STR_SEC("\0int\0some other int\0float"),
6946 	},
6947 	.expect = {
6948 		.raw_types = {
6949 			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 8),
6950 			/* different name */
6951 			BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 32, 8),
6952 			/* different encoding */
6953 			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_CHAR, 0, 32, 8),
6954 			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_BOOL, 0, 32, 8),
6955 			/* different bit offset */
6956 			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 8, 32, 8),
6957 			/* different bit size */
6958 			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 27, 8),
6959 			/* different byte size */
6960 			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),
6961 			/* all allowed sizes */
6962 			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 2),
6963 			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 4),
6964 			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 8),
6965 			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 12),
6966 			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 16),
6967 			BTF_END_RAW,
6968 		},
6969 		BTF_STR_SEC("\0int\0some other int\0float"),
6970 	},
6971 },
6972 {
6973 	.descr = "dedup: enum fwd resolution",
6974 	.input = {
6975 		.raw_types = {
6976 			/* [1] fwd enum 'e1' before full enum */
6977 			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 0), 4),
6978 			/* [2] full enum 'e1' after fwd */
6979 			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
6980 				BTF_ENUM_ENC(NAME_NTH(2), 123),
6981 			/* [3] full enum 'e2' before fwd */
6982 			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
6983 				BTF_ENUM_ENC(NAME_NTH(4), 456),
6984 			/* [4] fwd enum 'e2' after full enum */
6985 			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 0), 4),
6986 			/* [5] incompatible fwd enum with different size */
6987 			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 0), 1),
6988 			/* [6] incompatible full enum with different value */
6989 			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
6990 				BTF_ENUM_ENC(NAME_NTH(2), 321),
6991 			BTF_END_RAW,
6992 		},
6993 		BTF_STR_SEC("\0e1\0e1_val\0e2\0e2_val"),
6994 	},
6995 	.expect = {
6996 		.raw_types = {
6997 			/* [1] full enum 'e1' */
6998 			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
6999 				BTF_ENUM_ENC(NAME_NTH(2), 123),
7000 			/* [2] full enum 'e2' */
7001 			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7002 				BTF_ENUM_ENC(NAME_NTH(4), 456),
7003 			/* [3] incompatible fwd enum with different size */
7004 			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 0), 1),
7005 			/* [4] incompatible full enum with different value */
7006 			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7007 				BTF_ENUM_ENC(NAME_NTH(2), 321),
7008 			BTF_END_RAW,
7009 		},
7010 		BTF_STR_SEC("\0e1\0e1_val\0e2\0e2_val"),
7011 	},
7012 },
7013 {
7014 	.descr = "dedup: datasec and vars pass-through",
7015 	.input = {
7016 		.raw_types = {
7017 			/* int */
7018 			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7019 			/* static int t */
7020 			BTF_VAR_ENC(NAME_NTH(2), 1, 0),			/* [2] */
7021 			/* .bss section */				/* [3] */
7022 			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
7023 			BTF_VAR_SECINFO_ENC(2, 0, 4),
7024 			/* int, referenced from [5] */
7025 			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [4] */
7026 			/* another static int t */
7027 			BTF_VAR_ENC(NAME_NTH(2), 4, 0),			/* [5] */
7028 			/* another .bss section */			/* [6] */
7029 			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
7030 			BTF_VAR_SECINFO_ENC(5, 0, 4),
7031 			BTF_END_RAW,
7032 		},
7033 		BTF_STR_SEC("\0.bss\0t"),
7034 	},
7035 	.expect = {
7036 		.raw_types = {
7037 			/* int */
7038 			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7039 			/* static int t */
7040 			BTF_VAR_ENC(NAME_NTH(2), 1, 0),			/* [2] */
7041 			/* .bss section */				/* [3] */
7042 			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
7043 			BTF_VAR_SECINFO_ENC(2, 0, 4),
7044 			/* another static int t */
7045 			BTF_VAR_ENC(NAME_NTH(2), 1, 0),			/* [4] */
7046 			/* another .bss section */			/* [5] */
7047 			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
7048 			BTF_VAR_SECINFO_ENC(4, 0, 4),
7049 			BTF_END_RAW,
7050 		},
7051 		BTF_STR_SEC("\0.bss\0t"),
7052 	},
7053 	.opts = {
7054 		.force_collisions = true
7055 	},
7056 },
7057 {
7058 	.descr = "dedup: func/func_arg/var tags",
7059 	.input = {
7060 		.raw_types = {
7061 			/* int */
7062 			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7063 			/* static int t */
7064 			BTF_VAR_ENC(NAME_NTH(1), 1, 0),			/* [2] */
7065 			/* void f(int a1, int a2) */
7066 			BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
7067 				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(2), 1),
7068 				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(3), 1),
7069 			BTF_FUNC_ENC(NAME_NTH(4), 2),			/* [4] */
7070 			/* tag -> t */
7071 			BTF_DECL_TAG_ENC(NAME_NTH(5), 2, -1),		/* [5] */
7072 			BTF_DECL_TAG_ENC(NAME_NTH(5), 2, -1),		/* [6] */
7073 			/* tag -> func */
7074 			BTF_DECL_TAG_ENC(NAME_NTH(5), 4, -1),		/* [7] */
7075 			BTF_DECL_TAG_ENC(NAME_NTH(5), 4, -1),		/* [8] */
7076 			/* tag -> func arg a1 */
7077 			BTF_DECL_TAG_ENC(NAME_NTH(5), 4, 1),		/* [9] */
7078 			BTF_DECL_TAG_ENC(NAME_NTH(5), 4, 1),		/* [10] */
7079 			BTF_END_RAW,
7080 		},
7081 		BTF_STR_SEC("\0t\0a1\0a2\0f\0tag"),
7082 	},
7083 	.expect = {
7084 		.raw_types = {
7085 			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7086 			BTF_VAR_ENC(NAME_NTH(1), 1, 0),			/* [2] */
7087 			BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
7088 				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(2), 1),
7089 				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(3), 1),
7090 			BTF_FUNC_ENC(NAME_NTH(4), 2),			/* [4] */
7091 			BTF_DECL_TAG_ENC(NAME_NTH(5), 2, -1),		/* [5] */
7092 			BTF_DECL_TAG_ENC(NAME_NTH(5), 4, -1),		/* [6] */
7093 			BTF_DECL_TAG_ENC(NAME_NTH(5), 4, 1),		/* [7] */
7094 			BTF_END_RAW,
7095 		},
7096 		BTF_STR_SEC("\0t\0a1\0a2\0f\0tag"),
7097 	},
7098 },
7099 {
7100 	.descr = "dedup: func/func_param tags",
7101 	.input = {
7102 		.raw_types = {
7103 			/* int */
7104 			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7105 			/* void f(int a1, int a2) */
7106 			BTF_FUNC_PROTO_ENC(0, 2),			/* [2] */
7107 				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(1), 1),
7108 				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(2), 1),
7109 			BTF_FUNC_ENC(NAME_NTH(3), 2),			/* [3] */
7110 			/* void f(int a1, int a2) */
7111 			BTF_FUNC_PROTO_ENC(0, 2),			/* [4] */
7112 				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(1), 1),
7113 				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(2), 1),
7114 			BTF_FUNC_ENC(NAME_NTH(3), 4),			/* [5] */
7115 			/* tag -> f: tag1, tag2 */
7116 			BTF_DECL_TAG_ENC(NAME_NTH(4), 3, -1),		/* [6] */
7117 			BTF_DECL_TAG_ENC(NAME_NTH(5), 3, -1),		/* [7] */
7118 			/* tag -> f/a2: tag1, tag2 */
7119 			BTF_DECL_TAG_ENC(NAME_NTH(4), 3, 1),		/* [8] */
7120 			BTF_DECL_TAG_ENC(NAME_NTH(5), 3, 1),		/* [9] */
7121 			/* tag -> f: tag1, tag3 */
7122 			BTF_DECL_TAG_ENC(NAME_NTH(4), 5, -1),		/* [10] */
7123 			BTF_DECL_TAG_ENC(NAME_NTH(6), 5, -1),		/* [11] */
7124 			/* tag -> f/a2: tag1, tag3 */
7125 			BTF_DECL_TAG_ENC(NAME_NTH(4), 5, 1),		/* [12] */
7126 			BTF_DECL_TAG_ENC(NAME_NTH(6), 5, 1),		/* [13] */
7127 			BTF_END_RAW,
7128 		},
7129 		BTF_STR_SEC("\0a1\0a2\0f\0tag1\0tag2\0tag3"),
7130 	},
7131 	.expect = {
7132 		.raw_types = {
7133 			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7134 			BTF_FUNC_PROTO_ENC(0, 2),			/* [2] */
7135 				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(1), 1),
7136 				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(2), 1),
7137 			BTF_FUNC_ENC(NAME_NTH(3), 2),			/* [3] */
7138 			BTF_DECL_TAG_ENC(NAME_NTH(4), 3, -1),		/* [4] */
7139 			BTF_DECL_TAG_ENC(NAME_NTH(5), 3, -1),		/* [5] */
7140 			BTF_DECL_TAG_ENC(NAME_NTH(6), 3, -1),		/* [6] */
7141 			BTF_DECL_TAG_ENC(NAME_NTH(4), 3, 1),		/* [7] */
7142 			BTF_DECL_TAG_ENC(NAME_NTH(5), 3, 1),		/* [8] */
7143 			BTF_DECL_TAG_ENC(NAME_NTH(6), 3, 1),		/* [9] */
7144 			BTF_END_RAW,
7145 		},
7146 		BTF_STR_SEC("\0a1\0a2\0f\0tag1\0tag2\0tag3"),
7147 	},
7148 },
7149 {
7150 	.descr = "dedup: struct/struct_member tags",
7151 	.input = {
7152 		.raw_types = {
7153 			/* int */
7154 			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7155 			BTF_STRUCT_ENC(NAME_NTH(1), 2, 8),		/* [2] */
7156 				BTF_MEMBER_ENC(NAME_NTH(2), 1, 0),
7157 				BTF_MEMBER_ENC(NAME_NTH(3), 1, 32),
7158 			BTF_STRUCT_ENC(NAME_NTH(1), 2, 8),		/* [3] */
7159 				BTF_MEMBER_ENC(NAME_NTH(2), 1, 0),
7160 				BTF_MEMBER_ENC(NAME_NTH(3), 1, 32),
7161 			/* tag -> t: tag1, tag2 */
7162 			BTF_DECL_TAG_ENC(NAME_NTH(4), 2, -1),		/* [4] */
7163 			BTF_DECL_TAG_ENC(NAME_NTH(5), 2, -1),		/* [5] */
7164 			/* tag -> t/m2: tag1, tag2 */
7165 			BTF_DECL_TAG_ENC(NAME_NTH(4), 2, 1),		/* [6] */
7166 			BTF_DECL_TAG_ENC(NAME_NTH(5), 2, 1),		/* [7] */
7167 			/* tag -> t: tag1, tag3 */
7168 			BTF_DECL_TAG_ENC(NAME_NTH(4), 3, -1),		/* [8] */
7169 			BTF_DECL_TAG_ENC(NAME_NTH(6), 3, -1),		/* [9] */
7170 			/* tag -> t/m2: tag1, tag3 */
7171 			BTF_DECL_TAG_ENC(NAME_NTH(4), 3, 1),		/* [10] */
7172 			BTF_DECL_TAG_ENC(NAME_NTH(6), 3, 1),		/* [11] */
7173 			BTF_END_RAW,
7174 		},
7175 		BTF_STR_SEC("\0t\0m1\0m2\0tag1\0tag2\0tag3"),
7176 	},
7177 	.expect = {
7178 		.raw_types = {
7179 			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7180 			BTF_STRUCT_ENC(NAME_NTH(1), 2, 8),		/* [2] */
7181 				BTF_MEMBER_ENC(NAME_NTH(2), 1, 0),
7182 				BTF_MEMBER_ENC(NAME_NTH(3), 1, 32),
7183 			BTF_DECL_TAG_ENC(NAME_NTH(4), 2, -1),		/* [3] */
7184 			BTF_DECL_TAG_ENC(NAME_NTH(5), 2, -1),		/* [4] */
7185 			BTF_DECL_TAG_ENC(NAME_NTH(6), 2, -1),		/* [5] */
7186 			BTF_DECL_TAG_ENC(NAME_NTH(4), 2, 1),		/* [6] */
7187 			BTF_DECL_TAG_ENC(NAME_NTH(5), 2, 1),		/* [7] */
7188 			BTF_DECL_TAG_ENC(NAME_NTH(6), 2, 1),		/* [8] */
7189 			BTF_END_RAW,
7190 		},
7191 		BTF_STR_SEC("\0t\0m1\0m2\0tag1\0tag2\0tag3"),
7192 	},
7193 },
7194 {
7195 	.descr = "dedup: typedef tags",
7196 	.input = {
7197 		.raw_types = {
7198 			/* int */
7199 			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7200 			BTF_TYPEDEF_ENC(NAME_NTH(1), 1),		/* [2] */
7201 			BTF_TYPEDEF_ENC(NAME_NTH(1), 1),		/* [3] */
7202 			/* tag -> t: tag1, tag2 */
7203 			BTF_DECL_TAG_ENC(NAME_NTH(2), 2, -1),		/* [4] */
7204 			BTF_DECL_TAG_ENC(NAME_NTH(3), 2, -1),		/* [5] */
7205 			/* tag -> t: tag1, tag3 */
7206 			BTF_DECL_TAG_ENC(NAME_NTH(2), 3, -1),		/* [6] */
7207 			BTF_DECL_TAG_ENC(NAME_NTH(4), 3, -1),		/* [7] */
7208 			BTF_END_RAW,
7209 		},
7210 		BTF_STR_SEC("\0t\0tag1\0tag2\0tag3"),
7211 	},
7212 	.expect = {
7213 		.raw_types = {
7214 			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7215 			BTF_TYPEDEF_ENC(NAME_NTH(1), 1),		/* [2] */
7216 			BTF_DECL_TAG_ENC(NAME_NTH(2), 2, -1),		/* [3] */
7217 			BTF_DECL_TAG_ENC(NAME_NTH(3), 2, -1),		/* [4] */
7218 			BTF_DECL_TAG_ENC(NAME_NTH(4), 2, -1),		/* [5] */
7219 			BTF_END_RAW,
7220 		},
7221 		BTF_STR_SEC("\0t\0tag1\0tag2\0tag3"),
7222 	},
7223 },
7224 {
7225 	.descr = "dedup: btf_type_tag #1",
7226 	.input = {
7227 		.raw_types = {
7228 			/* ptr -> tag2 -> tag1 -> int */
7229 			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7230 			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7231 			BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),		/* [3] */
7232 			BTF_PTR_ENC(3),					/* [4] */
7233 			/* ptr -> tag2 -> tag1 -> int */
7234 			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [5] */
7235 			BTF_TYPE_TAG_ENC(NAME_NTH(2), 5),		/* [6] */
7236 			BTF_PTR_ENC(6),					/* [7] */
7237 			/* ptr -> tag1 -> int */
7238 			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [8] */
7239 			BTF_PTR_ENC(8),					/* [9] */
7240 			BTF_END_RAW,
7241 		},
7242 		BTF_STR_SEC("\0tag1\0tag2"),
7243 	},
7244 	.expect = {
7245 		.raw_types = {
7246 			/* ptr -> tag2 -> tag1 -> int */
7247 			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7248 			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7249 			BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),		/* [3] */
7250 			BTF_PTR_ENC(3),					/* [4] */
7251 			/* ptr -> tag1 -> int */
7252 			BTF_PTR_ENC(2),					/* [5] */
7253 			BTF_END_RAW,
7254 		},
7255 		BTF_STR_SEC("\0tag1\0tag2"),
7256 	},
7257 },
7258 {
7259 	.descr = "dedup: btf_type_tag #2",
7260 	.input = {
7261 		.raw_types = {
7262 			/* ptr -> tag2 -> tag1 -> int */
7263 			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7264 			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7265 			BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),		/* [3] */
7266 			BTF_PTR_ENC(3),					/* [4] */
7267 			/* ptr -> tag2 -> int */
7268 			BTF_TYPE_TAG_ENC(NAME_NTH(2), 1),		/* [5] */
7269 			BTF_PTR_ENC(5),					/* [6] */
7270 			BTF_END_RAW,
7271 		},
7272 		BTF_STR_SEC("\0tag1\0tag2"),
7273 	},
7274 	.expect = {
7275 		.raw_types = {
7276 			/* ptr -> tag2 -> tag1 -> int */
7277 			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7278 			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7279 			BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),		/* [3] */
7280 			BTF_PTR_ENC(3),					/* [4] */
7281 			/* ptr -> tag2 -> int */
7282 			BTF_TYPE_TAG_ENC(NAME_NTH(2), 1),		/* [5] */
7283 			BTF_PTR_ENC(5),					/* [6] */
7284 			BTF_END_RAW,
7285 		},
7286 		BTF_STR_SEC("\0tag1\0tag2"),
7287 	},
7288 },
7289 {
7290 	.descr = "dedup: btf_type_tag #3",
7291 	.input = {
7292 		.raw_types = {
7293 			/* ptr -> tag2 -> tag1 -> int */
7294 			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7295 			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7296 			BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),		/* [3] */
7297 			BTF_PTR_ENC(3),					/* [4] */
7298 			/* ptr -> tag1 -> tag2 -> int */
7299 			BTF_TYPE_TAG_ENC(NAME_NTH(2), 1),		/* [5] */
7300 			BTF_TYPE_TAG_ENC(NAME_NTH(1), 5),		/* [6] */
7301 			BTF_PTR_ENC(6),					/* [7] */
7302 			BTF_END_RAW,
7303 		},
7304 		BTF_STR_SEC("\0tag1\0tag2"),
7305 	},
7306 	.expect = {
7307 		.raw_types = {
7308 			/* ptr -> tag2 -> tag1 -> int */
7309 			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7310 			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7311 			BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),		/* [3] */
7312 			BTF_PTR_ENC(3),					/* [4] */
7313 			/* ptr -> tag1 -> tag2 -> int */
7314 			BTF_TYPE_TAG_ENC(NAME_NTH(2), 1),		/* [5] */
7315 			BTF_TYPE_TAG_ENC(NAME_NTH(1), 5),		/* [6] */
7316 			BTF_PTR_ENC(6),					/* [7] */
7317 			BTF_END_RAW,
7318 		},
7319 		BTF_STR_SEC("\0tag1\0tag2"),
7320 	},
7321 },
7322 {
7323 	.descr = "dedup: btf_type_tag #4",
7324 	.input = {
7325 		.raw_types = {
7326 			/* ptr -> tag1 -> int */
7327 			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7328 			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7329 			BTF_PTR_ENC(2),					/* [3] */
7330 			/* ptr -> tag1 -> long */
7331 			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 64, 8),	/* [4] */
7332 			BTF_TYPE_TAG_ENC(NAME_NTH(1), 4),		/* [5] */
7333 			BTF_PTR_ENC(5),					/* [6] */
7334 			BTF_END_RAW,
7335 		},
7336 		BTF_STR_SEC("\0tag1"),
7337 	},
7338 	.expect = {
7339 		.raw_types = {
7340 			/* ptr -> tag1 -> int */
7341 			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7342 			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7343 			BTF_PTR_ENC(2),					/* [3] */
7344 			/* ptr -> tag1 -> long */
7345 			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 64, 8),	/* [4] */
7346 			BTF_TYPE_TAG_ENC(NAME_NTH(1), 4),		/* [5] */
7347 			BTF_PTR_ENC(5),					/* [6] */
7348 			BTF_END_RAW,
7349 		},
7350 		BTF_STR_SEC("\0tag1"),
7351 	},
7352 },
7353 {
7354 	.descr = "dedup: btf_type_tag #5, struct",
7355 	.input = {
7356 		.raw_types = {
7357 			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),				/* [1] */
7358 			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),					/* [2] */
7359 			BTF_TYPE_ENC(NAME_NTH(2), BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 1), 4),	/* [3] */
7360 			BTF_MEMBER_ENC(NAME_NTH(3), 2, BTF_MEMBER_OFFSET(0, 0)),
7361 			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),					/* [4] */
7362 			BTF_TYPE_ENC(NAME_NTH(2), BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 1), 4),	/* [5] */
7363 			BTF_MEMBER_ENC(NAME_NTH(3), 4, BTF_MEMBER_OFFSET(0, 0)),
7364 			BTF_END_RAW,
7365 		},
7366 		BTF_STR_SEC("\0tag1\0t\0m"),
7367 	},
7368 	.expect = {
7369 		.raw_types = {
7370 			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),				/* [1] */
7371 			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),					/* [2] */
7372 			BTF_TYPE_ENC(NAME_NTH(2), BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 1), 4),	/* [3] */
7373 			BTF_MEMBER_ENC(NAME_NTH(3), 2, BTF_MEMBER_OFFSET(0, 0)),
7374 			BTF_END_RAW,
7375 		},
7376 		BTF_STR_SEC("\0tag1\0t\0m"),
7377 	},
7378 },
7379 
7380 };
7381 
7382 static int btf_type_size(const struct btf_type *t)
7383 {
7384 	int base_size = sizeof(struct btf_type);
7385 	__u16 vlen = BTF_INFO_VLEN(t->info);
7386 	__u16 kind = BTF_INFO_KIND(t->info);
7387 
7388 	switch (kind) {
7389 	case BTF_KIND_FWD:
7390 	case BTF_KIND_CONST:
7391 	case BTF_KIND_VOLATILE:
7392 	case BTF_KIND_RESTRICT:
7393 	case BTF_KIND_PTR:
7394 	case BTF_KIND_TYPEDEF:
7395 	case BTF_KIND_FUNC:
7396 	case BTF_KIND_FLOAT:
7397 	case BTF_KIND_TYPE_TAG:
7398 		return base_size;
7399 	case BTF_KIND_INT:
7400 		return base_size + sizeof(__u32);
7401 	case BTF_KIND_ENUM:
7402 		return base_size + vlen * sizeof(struct btf_enum);
7403 	case BTF_KIND_ARRAY:
7404 		return base_size + sizeof(struct btf_array);
7405 	case BTF_KIND_STRUCT:
7406 	case BTF_KIND_UNION:
7407 		return base_size + vlen * sizeof(struct btf_member);
7408 	case BTF_KIND_FUNC_PROTO:
7409 		return base_size + vlen * sizeof(struct btf_param);
7410 	case BTF_KIND_VAR:
7411 		return base_size + sizeof(struct btf_var);
7412 	case BTF_KIND_DATASEC:
7413 		return base_size + vlen * sizeof(struct btf_var_secinfo);
7414 	case BTF_KIND_DECL_TAG:
7415 		return base_size + sizeof(struct btf_decl_tag);
7416 	default:
7417 		fprintf(stderr, "Unsupported BTF_KIND:%u\n", kind);
7418 		return -EINVAL;
7419 	}
7420 }
7421 
7422 static void dump_btf_strings(const char *strs, __u32 len)
7423 {
7424 	const char *cur = strs;
7425 	int i = 0;
7426 
7427 	while (cur < strs + len) {
7428 		fprintf(stderr, "string #%d: '%s'\n", i, cur);
7429 		cur += strlen(cur) + 1;
7430 		i++;
7431 	}
7432 }
7433 
7434 static void do_test_dedup(unsigned int test_num)
7435 {
7436 	struct btf_dedup_test *test = &dedup_tests[test_num - 1];
7437 	__u32 test_nr_types, expect_nr_types, test_btf_size, expect_btf_size;
7438 	const struct btf_header *test_hdr, *expect_hdr;
7439 	struct btf *test_btf = NULL, *expect_btf = NULL;
7440 	const void *test_btf_data, *expect_btf_data;
7441 	const char *ret_test_next_str, *ret_expect_next_str;
7442 	const char *test_strs, *expect_strs;
7443 	const char *test_str_cur;
7444 	const char *expect_str_cur, *expect_str_end;
7445 	unsigned int raw_btf_size;
7446 	void *raw_btf;
7447 	int err = 0, i;
7448 
7449 	if (!test__start_subtest(test->descr))
7450 		return;
7451 
7452 	raw_btf = btf_raw_create(&hdr_tmpl, test->input.raw_types,
7453 				 test->input.str_sec, test->input.str_sec_size,
7454 				 &raw_btf_size, &ret_test_next_str);
7455 	if (!raw_btf)
7456 		return;
7457 
7458 	test_btf = btf__new((__u8 *)raw_btf, raw_btf_size);
7459 	err = libbpf_get_error(test_btf);
7460 	free(raw_btf);
7461 	if (CHECK(err, "invalid test_btf errno:%d", err)) {
7462 		err = -1;
7463 		goto done;
7464 	}
7465 
7466 	raw_btf = btf_raw_create(&hdr_tmpl, test->expect.raw_types,
7467 				 test->expect.str_sec,
7468 				 test->expect.str_sec_size,
7469 				 &raw_btf_size, &ret_expect_next_str);
7470 	if (!raw_btf)
7471 		return;
7472 	expect_btf = btf__new((__u8 *)raw_btf, raw_btf_size);
7473 	err = libbpf_get_error(expect_btf);
7474 	free(raw_btf);
7475 	if (CHECK(err, "invalid expect_btf errno:%d", err)) {
7476 		err = -1;
7477 		goto done;
7478 	}
7479 
7480 	test->opts.sz = sizeof(test->opts);
7481 	err = btf__dedup(test_btf, &test->opts);
7482 	if (CHECK(err, "btf_dedup failed errno:%d", err)) {
7483 		err = -1;
7484 		goto done;
7485 	}
7486 
7487 	test_btf_data = btf__raw_data(test_btf, &test_btf_size);
7488 	expect_btf_data = btf__raw_data(expect_btf, &expect_btf_size);
7489 	if (CHECK(test_btf_size != expect_btf_size,
7490 		  "test_btf_size:%u != expect_btf_size:%u",
7491 		  test_btf_size, expect_btf_size)) {
7492 		err = -1;
7493 		goto done;
7494 	}
7495 
7496 	test_hdr = test_btf_data;
7497 	test_strs = test_btf_data + sizeof(*test_hdr) + test_hdr->str_off;
7498 	expect_hdr = expect_btf_data;
7499 	expect_strs = expect_btf_data + sizeof(*test_hdr) + expect_hdr->str_off;
7500 	if (CHECK(test_hdr->str_len != expect_hdr->str_len,
7501 		  "test_hdr->str_len:%u != expect_hdr->str_len:%u",
7502 		  test_hdr->str_len, expect_hdr->str_len)) {
7503 		fprintf(stderr, "\ntest strings:\n");
7504 		dump_btf_strings(test_strs, test_hdr->str_len);
7505 		fprintf(stderr, "\nexpected strings:\n");
7506 		dump_btf_strings(expect_strs, expect_hdr->str_len);
7507 		err = -1;
7508 		goto done;
7509 	}
7510 
7511 	expect_str_cur = expect_strs;
7512 	expect_str_end = expect_strs + expect_hdr->str_len;
7513 	while (expect_str_cur < expect_str_end) {
7514 		size_t test_len, expect_len;
7515 		int off;
7516 
7517 		off = btf__find_str(test_btf, expect_str_cur);
7518 		if (CHECK(off < 0, "exp str '%s' not found: %d\n", expect_str_cur, off)) {
7519 			err = -1;
7520 			goto done;
7521 		}
7522 		test_str_cur = btf__str_by_offset(test_btf, off);
7523 
7524 		test_len = strlen(test_str_cur);
7525 		expect_len = strlen(expect_str_cur);
7526 		if (CHECK(test_len != expect_len,
7527 			  "test_len:%zu != expect_len:%zu "
7528 			  "(test_str:%s, expect_str:%s)",
7529 			  test_len, expect_len, test_str_cur, expect_str_cur)) {
7530 			err = -1;
7531 			goto done;
7532 		}
7533 		if (CHECK(strcmp(test_str_cur, expect_str_cur),
7534 			  "test_str:%s != expect_str:%s",
7535 			  test_str_cur, expect_str_cur)) {
7536 			err = -1;
7537 			goto done;
7538 		}
7539 		expect_str_cur += expect_len + 1;
7540 	}
7541 
7542 	test_nr_types = btf__type_cnt(test_btf);
7543 	expect_nr_types = btf__type_cnt(expect_btf);
7544 	if (CHECK(test_nr_types != expect_nr_types,
7545 		  "test_nr_types:%u != expect_nr_types:%u",
7546 		  test_nr_types, expect_nr_types)) {
7547 		err = -1;
7548 		goto done;
7549 	}
7550 
7551 	for (i = 1; i < test_nr_types; i++) {
7552 		const struct btf_type *test_type, *expect_type;
7553 		int test_size, expect_size;
7554 
7555 		test_type = btf__type_by_id(test_btf, i);
7556 		expect_type = btf__type_by_id(expect_btf, i);
7557 		test_size = btf_type_size(test_type);
7558 		expect_size = btf_type_size(expect_type);
7559 
7560 		if (CHECK(test_size != expect_size,
7561 			  "type #%d: test_size:%d != expect_size:%u",
7562 			  i, test_size, expect_size)) {
7563 			err = -1;
7564 			goto done;
7565 		}
7566 		if (CHECK(btf_kind(test_type) != btf_kind(expect_type),
7567 			  "type %d kind: exp %d != got %u\n",
7568 			  i, btf_kind(expect_type), btf_kind(test_type))) {
7569 			err = -1;
7570 			goto done;
7571 		}
7572 		if (CHECK(test_type->info != expect_type->info,
7573 			  "type %d info: exp %d != got %u\n",
7574 			  i, expect_type->info, test_type->info)) {
7575 			err = -1;
7576 			goto done;
7577 		}
7578 		if (CHECK(test_type->size != expect_type->size,
7579 			  "type %d size/type: exp %d != got %u\n",
7580 			  i, expect_type->size, test_type->size)) {
7581 			err = -1;
7582 			goto done;
7583 		}
7584 	}
7585 
7586 done:
7587 	btf__free(test_btf);
7588 	btf__free(expect_btf);
7589 }
7590 
7591 void test_btf(void)
7592 {
7593 	int i;
7594 
7595 	always_log = env.verbosity > VERBOSE_NONE;
7596 
7597 	for (i = 1; i <= ARRAY_SIZE(raw_tests); i++)
7598 		do_test_raw(i);
7599 	for (i = 1; i <= ARRAY_SIZE(get_info_tests); i++)
7600 		do_test_get_info(i);
7601 	for (i = 1; i <= ARRAY_SIZE(file_tests); i++)
7602 		do_test_file(i);
7603 	for (i = 1; i <= ARRAY_SIZE(info_raw_tests); i++)
7604 		do_test_info_raw(i);
7605 	for (i = 1; i <= ARRAY_SIZE(dedup_tests); i++)
7606 		do_test_dedup(i);
7607 	test_pprint();
7608 }
7609