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