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