Linux Audio

Check our new training course

Loading...
v6.13.7
   1/* SPDX-License-Identifier: GPL-2.0 */
   2/* Copyright (c) 2018 Facebook */
   3
   4#include <linux/bpf.h>
   5#include <linux/btf.h>
   6#include <linux/err.h>
   7#include <linux/kernel.h>
   8#include <linux/filter.h>
   9#include <linux/unistd.h>
  10#include <bpf/bpf.h>
  11#include <libelf.h>
  12#include <gelf.h>
  13#include <string.h>
  14#include <stdlib.h>
  15#include <stdio.h>
  16#include <stdarg.h>
  17#include <unistd.h>
  18#include <fcntl.h>
  19#include <errno.h>
  20#include <assert.h>
  21#include <bpf/libbpf.h>
  22#include <bpf/btf.h>
  23
  24#include "bpf_util.h"
  25#include "../test_btf.h"
  26#include "test_progs.h"
  27
  28#define MAX_INSNS	512
  29#define MAX_SUBPROGS	16
  30
  31static int duration = 0;
  32static bool always_log;
  33
  34#undef CHECK
  35#define CHECK(condition, format...) _CHECK(condition, "check", duration, format)
  36
  37#define NAME_TBD 0xdeadb33f
  38
  39#define NAME_NTH(N) (0xfffe0000 | N)
  40#define IS_NAME_NTH(X) ((X & 0xffff0000) == 0xfffe0000)
  41#define GET_NAME_NTH_IDX(X) (X & 0x0000ffff)
  42
  43#define MAX_NR_RAW_U32 1024
  44#define BTF_LOG_BUF_SIZE 65535
  45
  46static char btf_log_buf[BTF_LOG_BUF_SIZE];
  47
  48static struct btf_header hdr_tmpl = {
  49	.magic = BTF_MAGIC,
  50	.version = BTF_VERSION,
  51	.hdr_len = sizeof(struct btf_header),
  52};
  53
  54/* several different mapv kinds(types) supported by pprint */
  55enum pprint_mapv_kind_t {
  56	PPRINT_MAPV_KIND_BASIC = 0,
  57	PPRINT_MAPV_KIND_INT128,
  58};
  59
  60struct btf_raw_test {
  61	const char *descr;
  62	const char *str_sec;
  63	const char *map_name;
  64	const char *err_str;
  65	__u32 raw_types[MAX_NR_RAW_U32];
  66	__u32 str_sec_size;
  67	enum bpf_map_type map_type;
  68	__u32 key_size;
  69	__u32 value_size;
  70	__u32 key_type_id;
  71	__u32 value_type_id;
  72	__u32 max_entries;
  73	bool btf_load_err;
  74	bool map_create_err;
  75	bool ordered_map;
  76	bool lossless_map;
  77	bool percpu_map;
  78	int hdr_len_delta;
  79	int type_off_delta;
  80	int str_off_delta;
  81	int str_len_delta;
  82	enum pprint_mapv_kind_t mapv_kind;
  83};
  84
  85#define BTF_STR_SEC(str) \
  86	.str_sec = str, .str_sec_size = sizeof(str)
  87
  88static struct btf_raw_test raw_tests[] = {
  89/* enum E {
  90 *     E0,
  91 *     E1,
  92 * };
  93 *
  94 * struct A {
  95 *	unsigned long long m;
  96 *	int n;
  97 *	char o;
  98 *	[3 bytes hole]
  99 *	int p[8];
 100 *	int q[4][8];
 101 *	enum E r;
 102 * };
 103 */
 104{
 105	.descr = "struct test #1",
 106	.raw_types = {
 107		/* int */
 108		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 109		/* unsigned long long */
 110		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
 111		/* char */
 112		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
 113		/* int[8] */
 114		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
 115		/* struct A { */				/* [5] */
 116		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 6), 180),
 117		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
 118		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
 119		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
 120		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
 121		BTF_MEMBER_ENC(NAME_TBD, 6, 384),/* int q[4][8]		*/
 122		BTF_MEMBER_ENC(NAME_TBD, 7, 1408), /* enum E r		*/
 123		/* } */
 124		/* int[4][8] */
 125		BTF_TYPE_ARRAY_ENC(4, 1, 4),			/* [6] */
 126		/* enum E */					/* [7] */
 127		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), sizeof(int)),
 128		BTF_ENUM_ENC(NAME_TBD, 0),
 129		BTF_ENUM_ENC(NAME_TBD, 1),
 130		BTF_END_RAW,
 131	},
 132	.str_sec = "\0A\0m\0n\0o\0p\0q\0r\0E\0E0\0E1",
 133	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0q\0r\0E\0E0\0E1"),
 134	.map_type = BPF_MAP_TYPE_ARRAY,
 135	.map_name = "struct_test1_map",
 136	.key_size = sizeof(int),
 137	.value_size = 180,
 138	.key_type_id = 1,
 139	.value_type_id = 5,
 140	.max_entries = 4,
 141},
 142
 143/* typedef struct b Struct_B;
 144 *
 145 * struct A {
 146 *     int m;
 147 *     struct b n[4];
 148 *     const Struct_B o[4];
 149 * };
 150 *
 151 * struct B {
 152 *     int m;
 153 *     int n;
 154 * };
 155 */
 156{
 157	.descr = "struct test #2",
 158	.raw_types = {
 159		/* int */					/* [1] */
 160		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
 161		/* struct b [4] */				/* [2] */
 162		BTF_TYPE_ARRAY_ENC(4, 1, 4),
 163
 164		/* struct A { */				/* [3] */
 165		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 3), 68),
 166		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int m;		*/
 167		BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* struct B n[4]	*/
 168		BTF_MEMBER_ENC(NAME_TBD, 8, 288),/* const Struct_B o[4];*/
 169		/* } */
 170
 171		/* struct B { */				/* [4] */
 172		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
 173		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int m; */
 174		BTF_MEMBER_ENC(NAME_TBD, 1, 32),/* int n; */
 175		/* } */
 176
 177		/* const int */					/* [5] */
 178		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 1),
 179		/* typedef struct b Struct_B */	/* [6] */
 180		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), 4),
 181		/* const Struct_B */				/* [7] */
 182		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 6),
 183		/* const Struct_B [4] */			/* [8] */
 184		BTF_TYPE_ARRAY_ENC(7, 1, 4),
 185		BTF_END_RAW,
 186	},
 187	.str_sec = "\0A\0m\0n\0o\0B\0m\0n\0Struct_B",
 188	.str_sec_size = sizeof("\0A\0m\0n\0o\0B\0m\0n\0Struct_B"),
 189	.map_type = BPF_MAP_TYPE_ARRAY,
 190	.map_name = "struct_test2_map",
 191	.key_size = sizeof(int),
 192	.value_size = 68,
 193	.key_type_id = 1,
 194	.value_type_id = 3,
 195	.max_entries = 4,
 196},
 197{
 198	.descr = "struct test #3 Invalid member offset",
 199	.raw_types = {
 200		/* int */					/* [1] */
 201		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
 202		/* int64 */					/* [2] */
 203		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 64, 8),
 204
 205		/* struct A { */				/* [3] */
 206		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 16),
 207		BTF_MEMBER_ENC(NAME_TBD, 1, 64),	/* int m;		*/
 208		BTF_MEMBER_ENC(NAME_TBD, 2, 0),		/* int64 n; */
 209		/* } */
 210		BTF_END_RAW,
 211	},
 212	.str_sec = "\0A\0m\0n\0",
 213	.str_sec_size = sizeof("\0A\0m\0n\0"),
 214	.map_type = BPF_MAP_TYPE_ARRAY,
 215	.map_name = "struct_test3_map",
 216	.key_size = sizeof(int),
 217	.value_size = 16,
 218	.key_type_id = 1,
 219	.value_type_id = 3,
 220	.max_entries = 4,
 221	.btf_load_err = true,
 222	.err_str = "Invalid member bits_offset",
 223},
 224/*
 225 * struct A {
 226 *	unsigned long long m;
 227 *	int n;
 228 *	char o;
 229 *	[3 bytes hole]
 230 *	int p[8];
 231 * };
 232 */
 233{
 234	.descr = "global data test #1",
 235	.raw_types = {
 236		/* int */
 237		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 238		/* unsigned long long */
 239		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
 240		/* char */
 241		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
 242		/* int[8] */
 243		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
 244		/* struct A { */				/* [5] */
 245		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
 246		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
 247		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
 248		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
 249		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
 250		/* } */
 251		BTF_END_RAW,
 252	},
 253	.str_sec = "\0A\0m\0n\0o\0p",
 254	.str_sec_size = sizeof("\0A\0m\0n\0o\0p"),
 255	.map_type = BPF_MAP_TYPE_ARRAY,
 256	.map_name = "struct_test1_map",
 257	.key_size = sizeof(int),
 258	.value_size = 48,
 259	.key_type_id = 1,
 260	.value_type_id = 5,
 261	.max_entries = 4,
 262},
 263/*
 264 * struct A {
 265 *	unsigned long long m;
 266 *	int n;
 267 *	char o;
 268 *	[3 bytes hole]
 269 *	int p[8];
 270 * };
 271 * static struct A t; <- in .bss
 272 */
 273{
 274	.descr = "global data test #2",
 275	.raw_types = {
 276		/* int */
 277		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 278		/* unsigned long long */
 279		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
 280		/* char */
 281		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
 282		/* int[8] */
 283		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
 284		/* struct A { */				/* [5] */
 285		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
 286		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
 287		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
 288		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
 289		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
 290		/* } */
 291		/* static struct A t */
 292		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
 293		/* .bss section */				/* [7] */
 294		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 48),
 295		BTF_VAR_SECINFO_ENC(6, 0, 48),
 296		BTF_END_RAW,
 297	},
 298	.str_sec = "\0A\0m\0n\0o\0p\0t\0.bss",
 299	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0.bss"),
 300	.map_type = BPF_MAP_TYPE_ARRAY,
 301	.map_name = ".bss",
 302	.key_size = sizeof(int),
 303	.value_size = 48,
 304	.key_type_id = 0,
 305	.value_type_id = 7,
 306	.max_entries = 1,
 307},
 308{
 309	.descr = "global data test #3",
 310	.raw_types = {
 311		/* int */
 312		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 313		/* static int t */
 314		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
 315		/* .bss section */				/* [3] */
 316		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
 317		BTF_VAR_SECINFO_ENC(2, 0, 4),
 318		BTF_END_RAW,
 319	},
 320	.str_sec = "\0t\0.bss",
 321	.str_sec_size = sizeof("\0t\0.bss"),
 322	.map_type = BPF_MAP_TYPE_ARRAY,
 323	.map_name = ".bss",
 324	.key_size = sizeof(int),
 325	.value_size = 4,
 326	.key_type_id = 0,
 327	.value_type_id = 3,
 328	.max_entries = 1,
 329},
 330{
 331	.descr = "global data test #4, unsupported linkage",
 332	.raw_types = {
 333		/* int */
 334		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 335		/* static int t */
 336		BTF_VAR_ENC(NAME_TBD, 1, 2),			/* [2] */
 337		/* .bss section */				/* [3] */
 338		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
 339		BTF_VAR_SECINFO_ENC(2, 0, 4),
 340		BTF_END_RAW,
 341	},
 342	.str_sec = "\0t\0.bss",
 343	.str_sec_size = sizeof("\0t\0.bss"),
 344	.map_type = BPF_MAP_TYPE_ARRAY,
 345	.map_name = ".bss",
 346	.key_size = sizeof(int),
 347	.value_size = 4,
 348	.key_type_id = 0,
 349	.value_type_id = 3,
 350	.max_entries = 1,
 351	.btf_load_err = true,
 352	.err_str = "Linkage not supported",
 353},
 354{
 355	.descr = "global data test #5, invalid var type",
 356	.raw_types = {
 357		/* static void t */
 358		BTF_VAR_ENC(NAME_TBD, 0, 0),			/* [1] */
 359		/* .bss section */				/* [2] */
 360		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
 361		BTF_VAR_SECINFO_ENC(1, 0, 4),
 362		BTF_END_RAW,
 363	},
 364	.str_sec = "\0t\0.bss",
 365	.str_sec_size = sizeof("\0t\0.bss"),
 366	.map_type = BPF_MAP_TYPE_ARRAY,
 367	.map_name = ".bss",
 368	.key_size = sizeof(int),
 369	.value_size = 4,
 370	.key_type_id = 0,
 371	.value_type_id = 2,
 372	.max_entries = 1,
 373	.btf_load_err = true,
 374	.err_str = "Invalid type_id",
 375},
 376{
 377	.descr = "global data test #6, invalid var type (fwd type)",
 378	.raw_types = {
 379		/* union A */
 380		BTF_TYPE_ENC(NAME_TBD,
 381			     BTF_INFO_ENC(BTF_KIND_FWD, 1, 0), 0), /* [1] */
 382		/* static union A t */
 383		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
 384		/* .bss section */				/* [3] */
 385		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
 386		BTF_VAR_SECINFO_ENC(2, 0, 4),
 387		BTF_END_RAW,
 388	},
 389	.str_sec = "\0A\0t\0.bss",
 390	.str_sec_size = sizeof("\0A\0t\0.bss"),
 391	.map_type = BPF_MAP_TYPE_ARRAY,
 392	.map_name = ".bss",
 393	.key_size = sizeof(int),
 394	.value_size = 4,
 395	.key_type_id = 0,
 396	.value_type_id = 2,
 397	.max_entries = 1,
 398	.btf_load_err = true,
 399	.err_str = "Invalid type",
 400},
 401{
 402	.descr = "global data test #7, invalid var type (fwd type)",
 403	.raw_types = {
 404		/* union A */
 405		BTF_TYPE_ENC(NAME_TBD,
 406			     BTF_INFO_ENC(BTF_KIND_FWD, 1, 0), 0), /* [1] */
 407		/* static union A t */
 408		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
 409		/* .bss section */				/* [3] */
 410		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
 411		BTF_VAR_SECINFO_ENC(1, 0, 4),
 412		BTF_END_RAW,
 413	},
 414	.str_sec = "\0A\0t\0.bss",
 415	.str_sec_size = sizeof("\0A\0t\0.bss"),
 416	.map_type = BPF_MAP_TYPE_ARRAY,
 417	.map_name = ".bss",
 418	.key_size = sizeof(int),
 419	.value_size = 4,
 420	.key_type_id = 0,
 421	.value_type_id = 2,
 422	.max_entries = 1,
 423	.btf_load_err = true,
 424	.err_str = "Invalid type",
 425},
 426{
 427	.descr = "global data test #8, invalid var size",
 428	.raw_types = {
 429		/* int */
 430		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 431		/* unsigned long long */
 432		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
 433		/* char */
 434		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
 435		/* int[8] */
 436		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
 437		/* struct A { */				/* [5] */
 438		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
 439		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
 440		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
 441		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
 442		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
 443		/* } */
 444		/* static struct A t */
 445		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
 446		/* .bss section */				/* [7] */
 447		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 48),
 448		BTF_VAR_SECINFO_ENC(6, 0, 47),
 449		BTF_END_RAW,
 450	},
 451	.str_sec = "\0A\0m\0n\0o\0p\0t\0.bss",
 452	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0.bss"),
 453	.map_type = BPF_MAP_TYPE_ARRAY,
 454	.map_name = ".bss",
 455	.key_size = sizeof(int),
 456	.value_size = 48,
 457	.key_type_id = 0,
 458	.value_type_id = 7,
 459	.max_entries = 1,
 460	.btf_load_err = true,
 461	.err_str = "Invalid size",
 462},
 463{
 464	.descr = "global data test #9, invalid var size",
 465	.raw_types = {
 466		/* int */
 467		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 468		/* unsigned long long */
 469		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
 470		/* char */
 471		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
 472		/* int[8] */
 473		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
 474		/* struct A { */				/* [5] */
 475		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
 476		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
 477		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
 478		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
 479		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
 480		/* } */
 481		/* static struct A t */
 482		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
 483		/* .bss section */				/* [7] */
 484		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 46),
 485		BTF_VAR_SECINFO_ENC(6, 0, 48),
 486		BTF_END_RAW,
 487	},
 488	.str_sec = "\0A\0m\0n\0o\0p\0t\0.bss",
 489	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0.bss"),
 490	.map_type = BPF_MAP_TYPE_ARRAY,
 491	.map_name = ".bss",
 492	.key_size = sizeof(int),
 493	.value_size = 48,
 494	.key_type_id = 0,
 495	.value_type_id = 7,
 496	.max_entries = 1,
 497	.btf_load_err = true,
 498	.err_str = "Invalid size",
 499},
 500{
 501	.descr = "global data test #10, invalid var size",
 502	.raw_types = {
 503		/* int */
 504		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 505		/* unsigned long long */
 506		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
 507		/* char */
 508		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
 509		/* int[8] */
 510		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
 511		/* struct A { */				/* [5] */
 512		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
 513		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
 514		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
 515		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
 516		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
 517		/* } */
 518		/* static struct A t */
 519		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
 520		/* .bss section */				/* [7] */
 521		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 46),
 522		BTF_VAR_SECINFO_ENC(6, 0, 46),
 523		BTF_END_RAW,
 524	},
 525	.str_sec = "\0A\0m\0n\0o\0p\0t\0.bss",
 526	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0.bss"),
 527	.map_type = BPF_MAP_TYPE_ARRAY,
 528	.map_name = ".bss",
 529	.key_size = sizeof(int),
 530	.value_size = 48,
 531	.key_type_id = 0,
 532	.value_type_id = 7,
 533	.max_entries = 1,
 534	.btf_load_err = true,
 535	.err_str = "Invalid size",
 536},
 537{
 538	.descr = "global data test #11, multiple section members",
 539	.raw_types = {
 540		/* int */
 541		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 542		/* unsigned long long */
 543		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
 544		/* char */
 545		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
 546		/* int[8] */
 547		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
 548		/* struct A { */				/* [5] */
 549		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
 550		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
 551		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
 552		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
 553		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
 554		/* } */
 555		/* static struct A t */
 556		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
 557		/* static int u */
 558		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [7] */
 559		/* .bss section */				/* [8] */
 560		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 2), 62),
 561		BTF_VAR_SECINFO_ENC(6, 10, 48),
 562		BTF_VAR_SECINFO_ENC(7, 58, 4),
 563		BTF_END_RAW,
 564	},
 565	.str_sec = "\0A\0m\0n\0o\0p\0t\0u\0.bss",
 566	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0u\0.bss"),
 567	.map_type = BPF_MAP_TYPE_ARRAY,
 568	.map_name = ".bss",
 569	.key_size = sizeof(int),
 570	.value_size = 62,
 571	.key_type_id = 0,
 572	.value_type_id = 8,
 573	.max_entries = 1,
 574},
 575{
 576	.descr = "global data test #12, invalid offset",
 577	.raw_types = {
 578		/* int */
 579		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 580		/* unsigned long long */
 581		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
 582		/* char */
 583		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
 584		/* int[8] */
 585		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
 586		/* struct A { */				/* [5] */
 587		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
 588		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
 589		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
 590		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
 591		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
 592		/* } */
 593		/* static struct A t */
 594		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
 595		/* static int u */
 596		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [7] */
 597		/* .bss section */				/* [8] */
 598		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 2), 62),
 599		BTF_VAR_SECINFO_ENC(6, 10, 48),
 600		BTF_VAR_SECINFO_ENC(7, 60, 4),
 601		BTF_END_RAW,
 602	},
 603	.str_sec = "\0A\0m\0n\0o\0p\0t\0u\0.bss",
 604	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0u\0.bss"),
 605	.map_type = BPF_MAP_TYPE_ARRAY,
 606	.map_name = ".bss",
 607	.key_size = sizeof(int),
 608	.value_size = 62,
 609	.key_type_id = 0,
 610	.value_type_id = 8,
 611	.max_entries = 1,
 612	.btf_load_err = true,
 613	.err_str = "Invalid offset+size",
 614},
 615{
 616	.descr = "global data test #13, invalid offset",
 617	.raw_types = {
 618		/* int */
 619		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 620		/* unsigned long long */
 621		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
 622		/* char */
 623		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
 624		/* int[8] */
 625		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
 626		/* struct A { */				/* [5] */
 627		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
 628		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
 629		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
 630		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
 631		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
 632		/* } */
 633		/* static struct A t */
 634		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
 635		/* static int u */
 636		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [7] */
 637		/* .bss section */				/* [8] */
 638		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 2), 62),
 639		BTF_VAR_SECINFO_ENC(6, 10, 48),
 640		BTF_VAR_SECINFO_ENC(7, 12, 4),
 641		BTF_END_RAW,
 642	},
 643	.str_sec = "\0A\0m\0n\0o\0p\0t\0u\0.bss",
 644	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0u\0.bss"),
 645	.map_type = BPF_MAP_TYPE_ARRAY,
 646	.map_name = ".bss",
 647	.key_size = sizeof(int),
 648	.value_size = 62,
 649	.key_type_id = 0,
 650	.value_type_id = 8,
 651	.max_entries = 1,
 652	.btf_load_err = true,
 653	.err_str = "Invalid offset",
 654},
 655{
 656	.descr = "global data test #14, invalid offset",
 657	.raw_types = {
 658		/* int */
 659		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 660		/* unsigned long long */
 661		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
 662		/* char */
 663		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
 664		/* int[8] */
 665		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
 666		/* struct A { */				/* [5] */
 667		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
 668		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
 669		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
 670		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
 671		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
 672		/* } */
 673		/* static struct A t */
 674		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
 675		/* static int u */
 676		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [7] */
 677		/* .bss section */				/* [8] */
 678		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 2), 62),
 679		BTF_VAR_SECINFO_ENC(7, 58, 4),
 680		BTF_VAR_SECINFO_ENC(6, 10, 48),
 681		BTF_END_RAW,
 682	},
 683	.str_sec = "\0A\0m\0n\0o\0p\0t\0u\0.bss",
 684	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0u\0.bss"),
 685	.map_type = BPF_MAP_TYPE_ARRAY,
 686	.map_name = ".bss",
 687	.key_size = sizeof(int),
 688	.value_size = 62,
 689	.key_type_id = 0,
 690	.value_type_id = 8,
 691	.max_entries = 1,
 692	.btf_load_err = true,
 693	.err_str = "Invalid offset",
 694},
 695{
 696	.descr = "global data test #15, not var kind",
 697	.raw_types = {
 698		/* int */
 699		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 700		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
 701		/* .bss section */				/* [3] */
 702		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
 703		BTF_VAR_SECINFO_ENC(1, 0, 4),
 704		BTF_END_RAW,
 705	},
 706	.str_sec = "\0A\0t\0.bss",
 707	.str_sec_size = sizeof("\0A\0t\0.bss"),
 708	.map_type = BPF_MAP_TYPE_ARRAY,
 709	.map_name = ".bss",
 710	.key_size = sizeof(int),
 711	.value_size = 4,
 712	.key_type_id = 0,
 713	.value_type_id = 3,
 714	.max_entries = 1,
 715	.btf_load_err = true,
 716	.err_str = "Not a VAR kind member",
 717},
 718{
 719	.descr = "global data test #16, invalid var referencing sec",
 720	.raw_types = {
 721		/* int */
 722		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 723		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [2] */
 724		BTF_VAR_ENC(NAME_TBD, 2, 0),			/* [3] */
 725		/* a section */					/* [4] */
 726		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
 727		BTF_VAR_SECINFO_ENC(3, 0, 4),
 728		/* a section */					/* [5] */
 729		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
 730		BTF_VAR_SECINFO_ENC(6, 0, 4),
 731		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [6] */
 732		BTF_END_RAW,
 733	},
 734	.str_sec = "\0A\0t\0s\0a\0a",
 735	.str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
 736	.map_type = BPF_MAP_TYPE_ARRAY,
 737	.map_name = ".bss",
 738	.key_size = sizeof(int),
 739	.value_size = 4,
 740	.key_type_id = 0,
 741	.value_type_id = 4,
 742	.max_entries = 1,
 743	.btf_load_err = true,
 744	.err_str = "Invalid type_id",
 745},
 746{
 747	.descr = "global data test #17, invalid var referencing var",
 748	.raw_types = {
 749		/* int */
 750		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 751		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
 752		BTF_VAR_ENC(NAME_TBD, 2, 0),			/* [3] */
 753		/* a section */					/* [4] */
 754		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
 755		BTF_VAR_SECINFO_ENC(3, 0, 4),
 756		BTF_END_RAW,
 757	},
 758	.str_sec = "\0A\0t\0s\0a\0a",
 759	.str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
 760	.map_type = BPF_MAP_TYPE_ARRAY,
 761	.map_name = ".bss",
 762	.key_size = sizeof(int),
 763	.value_size = 4,
 764	.key_type_id = 0,
 765	.value_type_id = 4,
 766	.max_entries = 1,
 767	.btf_load_err = true,
 768	.err_str = "Invalid type_id",
 769},
 770{
 771	.descr = "global data test #18, invalid var loop",
 772	.raw_types = {
 773		/* int */
 774		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 775		BTF_VAR_ENC(NAME_TBD, 2, 0),			/* [2] */
 776		/* .bss section */				/* [3] */
 777		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
 778		BTF_VAR_SECINFO_ENC(2, 0, 4),
 779		BTF_END_RAW,
 780	},
 781	.str_sec = "\0A\0t\0aaa",
 782	.str_sec_size = sizeof("\0A\0t\0aaa"),
 783	.map_type = BPF_MAP_TYPE_ARRAY,
 784	.map_name = ".bss",
 785	.key_size = sizeof(int),
 786	.value_size = 4,
 787	.key_type_id = 0,
 788	.value_type_id = 4,
 789	.max_entries = 1,
 790	.btf_load_err = true,
 791	.err_str = "Invalid type_id",
 792},
 793{
 794	.descr = "global data test #19, invalid var referencing var",
 795	.raw_types = {
 796		/* int */
 797		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 798		BTF_VAR_ENC(NAME_TBD, 3, 0),			/* [2] */
 799		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [3] */
 800		BTF_END_RAW,
 801	},
 802	.str_sec = "\0A\0t\0s\0a\0a",
 803	.str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
 804	.map_type = BPF_MAP_TYPE_ARRAY,
 805	.map_name = ".bss",
 806	.key_size = sizeof(int),
 807	.value_size = 4,
 808	.key_type_id = 0,
 809	.value_type_id = 4,
 810	.max_entries = 1,
 811	.btf_load_err = true,
 812	.err_str = "Invalid type_id",
 813},
 814{
 815	.descr = "global data test #20, invalid ptr referencing var",
 816	.raw_types = {
 817		/* int */
 818		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 819		/* PTR type_id=3	*/			/* [2] */
 820		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 3),
 821		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [3] */
 822		BTF_END_RAW,
 823	},
 824	.str_sec = "\0A\0t\0s\0a\0a",
 825	.str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
 826	.map_type = BPF_MAP_TYPE_ARRAY,
 827	.map_name = ".bss",
 828	.key_size = sizeof(int),
 829	.value_size = 4,
 830	.key_type_id = 0,
 831	.value_type_id = 4,
 832	.max_entries = 1,
 833	.btf_load_err = true,
 834	.err_str = "Invalid type_id",
 835},
 836{
 837	.descr = "global data test #21, var included in struct",
 838	.raw_types = {
 839		/* int */
 840		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 841		/* struct A { */				/* [2] */
 842		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 2),
 843		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int m; */
 844		BTF_MEMBER_ENC(NAME_TBD, 3, 32),/* VAR type_id=3; */
 845		/* } */
 846		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [3] */
 847		BTF_END_RAW,
 848	},
 849	.str_sec = "\0A\0t\0s\0a\0a",
 850	.str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
 851	.map_type = BPF_MAP_TYPE_ARRAY,
 852	.map_name = ".bss",
 853	.key_size = sizeof(int),
 854	.value_size = 4,
 855	.key_type_id = 0,
 856	.value_type_id = 4,
 857	.max_entries = 1,
 858	.btf_load_err = true,
 859	.err_str = "Invalid member",
 860},
 861{
 862	.descr = "global data test #22, array of var",
 863	.raw_types = {
 864		/* int */
 865		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 866		BTF_TYPE_ARRAY_ENC(3, 1, 4),			/* [2] */
 867		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [3] */
 868		BTF_END_RAW,
 869	},
 870	.str_sec = "\0A\0t\0s\0a\0a",
 871	.str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
 872	.map_type = BPF_MAP_TYPE_ARRAY,
 873	.map_name = ".bss",
 874	.key_size = sizeof(int),
 875	.value_size = 4,
 876	.key_type_id = 0,
 877	.value_type_id = 4,
 878	.max_entries = 1,
 879	.btf_load_err = true,
 880	.err_str = "Invalid elem",
 881},
 882{
 883	.descr = "var after datasec, ptr followed by modifier",
 884	.raw_types = {
 885		/* .bss section */				/* [1] */
 886		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 2),
 887			sizeof(void*)+4),
 888		BTF_VAR_SECINFO_ENC(4, 0, sizeof(void*)),
 889		BTF_VAR_SECINFO_ENC(6, sizeof(void*), 4),
 890		/* int */					/* [2] */
 891		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
 892		/* int* */					/* [3] */
 893		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 2),
 894		BTF_VAR_ENC(NAME_TBD, 3, 0),			/* [4] */
 895		/* const int */					/* [5] */
 896		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 2),
 897		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
 898		BTF_END_RAW,
 899	},
 900	.str_sec = "\0a\0b\0c\0",
 901	.str_sec_size = sizeof("\0a\0b\0c\0"),
 902	.map_type = BPF_MAP_TYPE_ARRAY,
 903	.map_name = ".bss",
 904	.key_size = sizeof(int),
 905	.value_size = sizeof(void*)+4,
 906	.key_type_id = 0,
 907	.value_type_id = 1,
 908	.max_entries = 1,
 909},
 910/* Test member exceeds the size of struct.
 911 *
 912 * struct A {
 913 *     int m;
 914 *     int n;
 915 * };
 916 */
 917{
 918	.descr = "size check test #1",
 919	.raw_types = {
 920		/* int */					/* [1] */
 921		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
 922		/* struct A { */				/* [2] */
 923		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 2 -  1),
 924		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int m; */
 925		BTF_MEMBER_ENC(NAME_TBD, 1, 32),/* int n; */
 926		/* } */
 927		BTF_END_RAW,
 928	},
 929	.str_sec = "\0A\0m\0n",
 930	.str_sec_size = sizeof("\0A\0m\0n"),
 931	.map_type = BPF_MAP_TYPE_ARRAY,
 932	.map_name = "size_check1_map",
 933	.key_size = sizeof(int),
 934	.value_size = 1,
 935	.key_type_id = 1,
 936	.value_type_id = 2,
 937	.max_entries = 4,
 938	.btf_load_err = true,
 939	.err_str = "Member exceeds struct_size",
 940},
 941
 942/* Test member exceeds the size of struct
 943 *
 944 * struct A {
 945 *     int m;
 946 *     int n[2];
 947 * };
 948 */
 949{
 950	.descr = "size check test #2",
 951	.raw_types = {
 952		/* int */					/* [1] */
 953		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
 954		/* int[2] */					/* [2] */
 955		BTF_TYPE_ARRAY_ENC(1, 1, 2),
 956		/* struct A { */				/* [3] */
 957		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 3 - 1),
 958		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int m; */
 959		BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* int n[2]; */
 960		/* } */
 961		BTF_END_RAW,
 962	},
 963	.str_sec = "\0A\0m\0n",
 964	.str_sec_size = sizeof("\0A\0m\0n"),
 965	.map_type = BPF_MAP_TYPE_ARRAY,
 966	.map_name = "size_check2_map",
 967	.key_size = sizeof(int),
 968	.value_size = 1,
 969	.key_type_id = 1,
 970	.value_type_id = 3,
 971	.max_entries = 4,
 972	.btf_load_err = true,
 973	.err_str = "Member exceeds struct_size",
 974},
 975
 976/* Test member exceeds the size of struct
 977 *
 978 * struct A {
 979 *     int m;
 980 *     void *n;
 981 * };
 982 */
 983{
 984	.descr = "size check test #3",
 985	.raw_types = {
 986		/* int */					/* [1] */
 987		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
 988		/* void* */					/* [2] */
 989		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
 990		/* struct A { */				/* [3] */
 991		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) + sizeof(void *) - 1),
 992		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int m; */
 993		BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* void *n; */
 994		/* } */
 995		BTF_END_RAW,
 996	},
 997	.str_sec = "\0A\0m\0n",
 998	.str_sec_size = sizeof("\0A\0m\0n"),
 999	.map_type = BPF_MAP_TYPE_ARRAY,
1000	.map_name = "size_check3_map",
1001	.key_size = sizeof(int),
1002	.value_size = 1,
1003	.key_type_id = 1,
1004	.value_type_id = 3,
1005	.max_entries = 4,
1006	.btf_load_err = true,
1007	.err_str = "Member exceeds struct_size",
1008},
1009
1010/* Test member exceeds the size of struct
1011 *
1012 * enum E {
1013 *     E0,
1014 *     E1,
1015 * };
1016 *
1017 * struct A {
1018 *     int m;
1019 *     enum E n;
1020 * };
1021 */
1022{
1023	.descr = "size check test #4",
1024	.raw_types = {
1025		/* int */			/* [1] */
1026		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
1027		/* enum E { */			/* [2] */
1028		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), sizeof(int)),
1029		BTF_ENUM_ENC(NAME_TBD, 0),
1030		BTF_ENUM_ENC(NAME_TBD, 1),
1031		/* } */
1032		/* struct A { */		/* [3] */
1033		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 2 - 1),
1034		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int m; */
1035		BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* enum E n; */
1036		/* } */
1037		BTF_END_RAW,
1038	},
1039	.str_sec = "\0E\0E0\0E1\0A\0m\0n",
1040	.str_sec_size = sizeof("\0E\0E0\0E1\0A\0m\0n"),
1041	.map_type = BPF_MAP_TYPE_ARRAY,
1042	.map_name = "size_check4_map",
1043	.key_size = sizeof(int),
1044	.value_size = 1,
1045	.key_type_id = 1,
1046	.value_type_id = 3,
1047	.max_entries = 4,
1048	.btf_load_err = true,
1049	.err_str = "Member exceeds struct_size",
1050},
1051
1052/* Test member unexceeds the size of struct
1053 *
1054 * enum E {
1055 *     E0,
1056 *     E1,
1057 * };
1058 *
1059 * struct A {
1060 *     char m;
1061 *     enum E __attribute__((packed)) n;
1062 * };
1063 */
1064{
1065	.descr = "size check test #5",
1066	.raw_types = {
1067		/* int */			/* [1] */
1068		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
1069		/* char */			/* [2] */
1070		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),
1071		/* enum E { */			/* [3] */
1072		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), 1),
1073		BTF_ENUM_ENC(NAME_TBD, 0),
1074		BTF_ENUM_ENC(NAME_TBD, 1),
1075		/* } */
1076		/* struct A { */		/* [4] */
1077		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 2),
1078		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* char m; */
1079		BTF_MEMBER_ENC(NAME_TBD, 3, 8),/* enum E __attribute__((packed)) n; */
1080		/* } */
1081		BTF_END_RAW,
1082	},
1083	.str_sec = "\0E\0E0\0E1\0A\0m\0n",
1084	.str_sec_size = sizeof("\0E\0E0\0E1\0A\0m\0n"),
1085	.map_type = BPF_MAP_TYPE_ARRAY,
1086	.map_name = "size_check5_map",
1087	.key_size = sizeof(int),
1088	.value_size = 2,
1089	.key_type_id = 1,
1090	.value_type_id = 4,
1091	.max_entries = 4,
1092},
1093
1094/* typedef const void * const_void_ptr;
1095 * struct A {
1096 *	const_void_ptr m;
1097 * };
1098 */
1099{
1100	.descr = "void test #1",
1101	.raw_types = {
1102		/* int */		/* [1] */
1103		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1104		/* const void */	/* [2] */
1105		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1106		/* const void* */	/* [3] */
1107		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 2),
1108		/* typedef const void * const_void_ptr */
1109		BTF_TYPEDEF_ENC(NAME_TBD, 3),	/* [4] */
1110		/* struct A { */	/* [5] */
1111		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
1112		/* const_void_ptr m; */
1113		BTF_MEMBER_ENC(NAME_TBD, 4, 0),
1114		/* } */
1115		BTF_END_RAW,
1116	},
1117	.str_sec = "\0const_void_ptr\0A\0m",
1118	.str_sec_size = sizeof("\0const_void_ptr\0A\0m"),
1119	.map_type = BPF_MAP_TYPE_ARRAY,
1120	.map_name = "void_test1_map",
1121	.key_size = sizeof(int),
1122	.value_size = sizeof(void *),
1123	.key_type_id = 1,
1124	.value_type_id = 4,
1125	.max_entries = 4,
1126},
1127
1128/* struct A {
1129 *     const void m;
1130 * };
1131 */
1132{
1133	.descr = "void test #2",
1134	.raw_types = {
1135		/* int */		/* [1] */
1136		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1137		/* const void */	/* [2] */
1138		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1139		/* struct A { */	/* [3] */
1140		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 8),
1141		/* const void m; */
1142		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
1143		/* } */
1144		BTF_END_RAW,
1145	},
1146	.str_sec = "\0A\0m",
1147	.str_sec_size = sizeof("\0A\0m"),
1148	.map_type = BPF_MAP_TYPE_ARRAY,
1149	.map_name = "void_test2_map",
1150	.key_size = sizeof(int),
1151	.value_size = sizeof(void *),
1152	.key_type_id = 1,
1153	.value_type_id = 3,
1154	.max_entries = 4,
1155	.btf_load_err = true,
1156	.err_str = "Invalid member",
1157},
1158
1159/* typedef const void * const_void_ptr;
1160 * const_void_ptr[4]
1161 */
1162{
1163	.descr = "void test #3",
1164	.raw_types = {
1165		/* int */		/* [1] */
1166		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1167		/* const void */	/* [2] */
1168		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1169		/* const void* */	/* [3] */
1170		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 2),
1171		/* typedef const void * const_void_ptr */
1172		BTF_TYPEDEF_ENC(NAME_TBD, 3),	/* [4] */
1173		/* const_void_ptr[4] */
1174		BTF_TYPE_ARRAY_ENC(4, 1, 4),	/* [5] */
1175		BTF_END_RAW,
1176	},
1177	.str_sec = "\0const_void_ptr",
1178	.str_sec_size = sizeof("\0const_void_ptr"),
1179	.map_type = BPF_MAP_TYPE_ARRAY,
1180	.map_name = "void_test3_map",
1181	.key_size = sizeof(int),
1182	.value_size = sizeof(void *) * 4,
1183	.key_type_id = 1,
1184	.value_type_id = 5,
1185	.max_entries = 4,
1186},
1187
1188/* const void[4]  */
1189{
1190	.descr = "void test #4",
1191	.raw_types = {
1192		/* int */		/* [1] */
1193		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1194		/* const void */	/* [2] */
1195		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1196		/* const void[4] */	/* [3] */
1197		BTF_TYPE_ARRAY_ENC(2, 1, 4),
1198		BTF_END_RAW,
1199	},
1200	.str_sec = "\0A\0m",
1201	.str_sec_size = sizeof("\0A\0m"),
1202	.map_type = BPF_MAP_TYPE_ARRAY,
1203	.map_name = "void_test4_map",
1204	.key_size = sizeof(int),
1205	.value_size = sizeof(void *) * 4,
1206	.key_type_id = 1,
1207	.value_type_id = 3,
1208	.max_entries = 4,
1209	.btf_load_err = true,
1210	.err_str = "Invalid elem",
1211},
1212
1213/* Array_A  <------------------+
1214 *     elem_type == Array_B    |
1215 *                    |        |
1216 *                    |        |
1217 * Array_B  <-------- +        |
1218 *      elem_type == Array A --+
1219 */
1220{
1221	.descr = "loop test #1",
1222	.raw_types = {
1223		/* int */			/* [1] */
1224		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1225		/* Array_A */			/* [2] */
1226		BTF_TYPE_ARRAY_ENC(3, 1, 8),
1227		/* Array_B */			/* [3] */
1228		BTF_TYPE_ARRAY_ENC(2, 1, 8),
1229		BTF_END_RAW,
1230	},
1231	.str_sec = "",
1232	.str_sec_size = sizeof(""),
1233	.map_type = BPF_MAP_TYPE_ARRAY,
1234	.map_name = "loop_test1_map",
1235	.key_size = sizeof(int),
1236	.value_size = sizeof(sizeof(int) * 8),
1237	.key_type_id = 1,
1238	.value_type_id = 2,
1239	.max_entries = 4,
1240	.btf_load_err = true,
1241	.err_str = "Loop detected",
1242},
1243
1244/* typedef is _before_ the BTF type of Array_A and Array_B
1245 *
1246 * typedef Array_B int_array;
1247 *
1248 * Array_A  <------------------+
1249 *     elem_type == int_array  |
1250 *                    |        |
1251 *                    |        |
1252 * Array_B  <-------- +        |
1253 *      elem_type == Array_A --+
1254 */
1255{
1256	.descr = "loop test #2",
1257	.raw_types = {
1258		/* int */
1259		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
1260		/* typedef Array_B int_array */
1261		BTF_TYPEDEF_ENC(1, 4),				/* [2] */
1262		/* Array_A */
1263		BTF_TYPE_ARRAY_ENC(2, 1, 8),			/* [3] */
1264		/* Array_B */
1265		BTF_TYPE_ARRAY_ENC(3, 1, 8),			/* [4] */
1266		BTF_END_RAW,
1267	},
1268	.str_sec = "\0int_array\0",
1269	.str_sec_size = sizeof("\0int_array"),
1270	.map_type = BPF_MAP_TYPE_ARRAY,
1271	.map_name = "loop_test2_map",
1272	.key_size = sizeof(int),
1273	.value_size = sizeof(sizeof(int) * 8),
1274	.key_type_id = 1,
1275	.value_type_id = 2,
1276	.max_entries = 4,
1277	.btf_load_err = true,
1278	.err_str = "Loop detected",
1279},
1280
1281/* Array_A  <------------------+
1282 *     elem_type == Array_B    |
1283 *                    |        |
1284 *                    |        |
1285 * Array_B  <-------- +        |
1286 *      elem_type == Array_A --+
1287 */
1288{
1289	.descr = "loop test #3",
1290	.raw_types = {
1291		/* int */				/* [1] */
1292		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1293		/* Array_A */				/* [2] */
1294		BTF_TYPE_ARRAY_ENC(3, 1, 8),
1295		/* Array_B */				/* [3] */
1296		BTF_TYPE_ARRAY_ENC(2, 1, 8),
1297		BTF_END_RAW,
1298	},
1299	.str_sec = "",
1300	.str_sec_size = sizeof(""),
1301	.map_type = BPF_MAP_TYPE_ARRAY,
1302	.map_name = "loop_test3_map",
1303	.key_size = sizeof(int),
1304	.value_size = sizeof(sizeof(int) * 8),
1305	.key_type_id = 1,
1306	.value_type_id = 2,
1307	.max_entries = 4,
1308	.btf_load_err = true,
1309	.err_str = "Loop detected",
1310},
1311
1312/* typedef is _between_ the BTF type of Array_A and Array_B
1313 *
1314 * typedef Array_B int_array;
1315 *
1316 * Array_A  <------------------+
1317 *     elem_type == int_array  |
1318 *                    |        |
1319 *                    |        |
1320 * Array_B  <-------- +        |
1321 *      elem_type == Array_A --+
1322 */
1323{
1324	.descr = "loop test #4",
1325	.raw_types = {
1326		/* int */				/* [1] */
1327		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1328		/* Array_A */				/* [2] */
1329		BTF_TYPE_ARRAY_ENC(3, 1, 8),
1330		/* typedef Array_B int_array */		/* [3] */
1331		BTF_TYPEDEF_ENC(NAME_TBD, 4),
1332		/* Array_B */				/* [4] */
1333		BTF_TYPE_ARRAY_ENC(2, 1, 8),
1334		BTF_END_RAW,
1335	},
1336	.str_sec = "\0int_array\0",
1337	.str_sec_size = sizeof("\0int_array"),
1338	.map_type = BPF_MAP_TYPE_ARRAY,
1339	.map_name = "loop_test4_map",
1340	.key_size = sizeof(int),
1341	.value_size = sizeof(sizeof(int) * 8),
1342	.key_type_id = 1,
1343	.value_type_id = 2,
1344	.max_entries = 4,
1345	.btf_load_err = true,
1346	.err_str = "Loop detected",
1347},
1348
1349/* typedef struct B Struct_B
1350 *
1351 * struct A {
1352 *     int x;
1353 *     Struct_B y;
1354 * };
1355 *
1356 * struct B {
1357 *     int x;
1358 *     struct A y;
1359 * };
1360 */
1361{
1362	.descr = "loop test #5",
1363	.raw_types = {
1364		/* int */
1365		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
1366		/* struct A */					/* [2] */
1367		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
1368		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int x;	*/
1369		BTF_MEMBER_ENC(NAME_TBD, 3, 32),/* Struct_B y;	*/
1370		/* typedef struct B Struct_B */
1371		BTF_TYPEDEF_ENC(NAME_TBD, 4),			/* [3] */
1372		/* struct B */					/* [4] */
1373		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
1374		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int x;	*/
1375		BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* struct A y;	*/
1376		BTF_END_RAW,
1377	},
1378	.str_sec = "\0A\0x\0y\0Struct_B\0B\0x\0y",
1379	.str_sec_size = sizeof("\0A\0x\0y\0Struct_B\0B\0x\0y"),
1380	.map_type = BPF_MAP_TYPE_ARRAY,
1381	.map_name = "loop_test5_map",
1382	.key_size = sizeof(int),
1383	.value_size = 8,
1384	.key_type_id = 1,
1385	.value_type_id = 2,
1386	.max_entries = 4,
1387	.btf_load_err = true,
1388	.err_str = "Loop detected",
1389},
1390
1391/* struct A {
1392 *     int x;
1393 *     struct A array_a[4];
1394 * };
1395 */
1396{
1397	.descr = "loop test #6",
1398	.raw_types = {
1399		/* int */
1400		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
1401		BTF_TYPE_ARRAY_ENC(3, 1, 4),			/* [2] */
1402		/* struct A */					/* [3] */
1403		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
1404		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int x;		*/
1405		BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* struct A array_a[4];	*/
1406		BTF_END_RAW,
1407	},
1408	.str_sec = "\0A\0x\0y",
1409	.str_sec_size = sizeof("\0A\0x\0y"),
1410	.map_type = BPF_MAP_TYPE_ARRAY,
1411	.map_name = "loop_test6_map",
1412	.key_size = sizeof(int),
1413	.value_size = 8,
1414	.key_type_id = 1,
1415	.value_type_id = 2,
1416	.max_entries = 4,
1417	.btf_load_err = true,
1418	.err_str = "Loop detected",
1419},
1420
1421{
1422	.descr = "loop test #7",
1423	.raw_types = {
1424		/* int */				/* [1] */
1425		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1426		/* struct A { */			/* [2] */
1427		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
1428		/*     const void *m;	*/
1429		BTF_MEMBER_ENC(NAME_TBD, 3, 0),
1430		/* CONST type_id=3	*/		/* [3] */
1431		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 4),
1432		/* PTR type_id=2	*/		/* [4] */
1433		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 3),
1434		BTF_END_RAW,
1435	},
1436	.str_sec = "\0A\0m",
1437	.str_sec_size = sizeof("\0A\0m"),
1438	.map_type = BPF_MAP_TYPE_ARRAY,
1439	.map_name = "loop_test7_map",
1440	.key_size = sizeof(int),
1441	.value_size = sizeof(void *),
1442	.key_type_id = 1,
1443	.value_type_id = 2,
1444	.max_entries = 4,
1445	.btf_load_err = true,
1446	.err_str = "Loop detected",
1447},
1448
1449{
1450	.descr = "loop test #8",
1451	.raw_types = {
1452		/* int */				/* [1] */
1453		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1454		/* struct A { */			/* [2] */
1455		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
1456		/*     const void *m;	*/
1457		BTF_MEMBER_ENC(NAME_TBD, 4, 0),
1458		/* struct B { */			/* [3] */
1459		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
1460		/*     const void *n;	*/
1461		BTF_MEMBER_ENC(NAME_TBD, 6, 0),
1462		/* CONST type_id=5	*/		/* [4] */
1463		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 5),
1464		/* PTR type_id=6	*/		/* [5] */
1465		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 6),
1466		/* CONST type_id=7	*/		/* [6] */
1467		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 7),
1468		/* PTR type_id=4	*/		/* [7] */
1469		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 4),
1470		BTF_END_RAW,
1471	},
1472	.str_sec = "\0A\0m\0B\0n",
1473	.str_sec_size = sizeof("\0A\0m\0B\0n"),
1474	.map_type = BPF_MAP_TYPE_ARRAY,
1475	.map_name = "loop_test8_map",
1476	.key_size = sizeof(int),
1477	.value_size = sizeof(void *),
1478	.key_type_id = 1,
1479	.value_type_id = 2,
1480	.max_entries = 4,
1481	.btf_load_err = true,
1482	.err_str = "Loop detected",
1483},
1484
1485{
1486	.descr = "string section does not end with null",
1487	.raw_types = {
1488		/* int */				/* [1] */
1489		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1490		BTF_END_RAW,
1491	},
1492	.str_sec = "\0int",
1493	.str_sec_size = sizeof("\0int") - 1,
1494	.map_type = BPF_MAP_TYPE_ARRAY,
1495	.map_name = "hdr_test_map",
1496	.key_size = sizeof(int),
1497	.value_size = sizeof(int),
1498	.key_type_id = 1,
1499	.value_type_id = 1,
1500	.max_entries = 4,
1501	.btf_load_err = true,
1502	.err_str = "Invalid string section",
1503},
1504
1505{
1506	.descr = "empty string section",
1507	.raw_types = {
1508		/* int */				/* [1] */
1509		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1510		BTF_END_RAW,
1511	},
1512	.str_sec = "",
1513	.str_sec_size = 0,
1514	.map_type = BPF_MAP_TYPE_ARRAY,
1515	.map_name = "hdr_test_map",
1516	.key_size = sizeof(int),
1517	.value_size = sizeof(int),
1518	.key_type_id = 1,
1519	.value_type_id = 1,
1520	.max_entries = 4,
1521	.btf_load_err = true,
1522	.err_str = "Invalid string section",
1523},
1524
1525{
1526	.descr = "empty type section",
1527	.raw_types = {
1528		BTF_END_RAW,
1529	},
1530	.str_sec = "\0int",
1531	.str_sec_size = sizeof("\0int"),
1532	.map_type = BPF_MAP_TYPE_ARRAY,
1533	.map_name = "hdr_test_map",
1534	.key_size = sizeof(int),
1535	.value_size = sizeof(int),
1536	.key_type_id = 1,
1537	.value_type_id = 1,
1538	.max_entries = 4,
1539	.btf_load_err = true,
1540	.err_str = "No type found",
1541},
1542
1543{
1544	.descr = "btf_header test. Longer hdr_len",
1545	.raw_types = {
1546		/* int */				/* [1] */
1547		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1548		BTF_END_RAW,
1549	},
1550	.str_sec = "\0int",
1551	.str_sec_size = sizeof("\0int"),
1552	.map_type = BPF_MAP_TYPE_ARRAY,
1553	.map_name = "hdr_test_map",
1554	.key_size = sizeof(int),
1555	.value_size = sizeof(int),
1556	.key_type_id = 1,
1557	.value_type_id = 1,
1558	.max_entries = 4,
1559	.btf_load_err = true,
1560	.hdr_len_delta = 4,
1561	.err_str = "Unsupported btf_header",
1562},
1563
1564{
1565	.descr = "btf_header test. Gap between hdr and type",
1566	.raw_types = {
1567		/* int */				/* [1] */
1568		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1569		BTF_END_RAW,
1570	},
1571	.str_sec = "\0int",
1572	.str_sec_size = sizeof("\0int"),
1573	.map_type = BPF_MAP_TYPE_ARRAY,
1574	.map_name = "hdr_test_map",
1575	.key_size = sizeof(int),
1576	.value_size = sizeof(int),
1577	.key_type_id = 1,
1578	.value_type_id = 1,
1579	.max_entries = 4,
1580	.btf_load_err = true,
1581	.type_off_delta = 4,
1582	.err_str = "Unsupported section found",
1583},
1584
1585{
1586	.descr = "btf_header test. Gap between type and str",
1587	.raw_types = {
1588		/* int */				/* [1] */
1589		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1590		BTF_END_RAW,
1591	},
1592	.str_sec = "\0int",
1593	.str_sec_size = sizeof("\0int"),
1594	.map_type = BPF_MAP_TYPE_ARRAY,
1595	.map_name = "hdr_test_map",
1596	.key_size = sizeof(int),
1597	.value_size = sizeof(int),
1598	.key_type_id = 1,
1599	.value_type_id = 1,
1600	.max_entries = 4,
1601	.btf_load_err = true,
1602	.str_off_delta = 4,
1603	.err_str = "Unsupported section found",
1604},
1605
1606{
1607	.descr = "btf_header test. Overlap between type and str",
1608	.raw_types = {
1609		/* int */				/* [1] */
1610		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1611		BTF_END_RAW,
1612	},
1613	.str_sec = "\0int",
1614	.str_sec_size = sizeof("\0int"),
1615	.map_type = BPF_MAP_TYPE_ARRAY,
1616	.map_name = "hdr_test_map",
1617	.key_size = sizeof(int),
1618	.value_size = sizeof(int),
1619	.key_type_id = 1,
1620	.value_type_id = 1,
1621	.max_entries = 4,
1622	.btf_load_err = true,
1623	.str_off_delta = -4,
1624	.err_str = "Section overlap found",
1625},
1626
1627{
1628	.descr = "btf_header test. Larger BTF size",
1629	.raw_types = {
1630		/* int */				/* [1] */
1631		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1632		BTF_END_RAW,
1633	},
1634	.str_sec = "\0int",
1635	.str_sec_size = sizeof("\0int"),
1636	.map_type = BPF_MAP_TYPE_ARRAY,
1637	.map_name = "hdr_test_map",
1638	.key_size = sizeof(int),
1639	.value_size = sizeof(int),
1640	.key_type_id = 1,
1641	.value_type_id = 1,
1642	.max_entries = 4,
1643	.btf_load_err = true,
1644	.str_len_delta = -4,
1645	.err_str = "Unsupported section found",
1646},
1647
1648{
1649	.descr = "btf_header test. Smaller BTF size",
1650	.raw_types = {
1651		/* int */				/* [1] */
1652		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1653		BTF_END_RAW,
1654	},
1655	.str_sec = "\0int",
1656	.str_sec_size = sizeof("\0int"),
1657	.map_type = BPF_MAP_TYPE_ARRAY,
1658	.map_name = "hdr_test_map",
1659	.key_size = sizeof(int),
1660	.value_size = sizeof(int),
1661	.key_type_id = 1,
1662	.value_type_id = 1,
1663	.max_entries = 4,
1664	.btf_load_err = true,
1665	.str_len_delta = 4,
1666	.err_str = "Total section length too long",
1667},
1668
1669{
1670	.descr = "array test. index_type/elem_type \"int\"",
1671	.raw_types = {
1672		/* int */				/* [1] */
1673		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1674		/* int[16] */				/* [2] */
1675		BTF_TYPE_ARRAY_ENC(1, 1, 16),
1676		BTF_END_RAW,
1677	},
1678	.str_sec = "",
1679	.str_sec_size = sizeof(""),
1680	.map_type = BPF_MAP_TYPE_ARRAY,
1681	.map_name = "array_test_map",
1682	.key_size = sizeof(int),
1683	.value_size = sizeof(int),
1684	.key_type_id = 1,
1685	.value_type_id = 1,
1686	.max_entries = 4,
1687},
1688
1689{
1690	.descr = "array test. index_type/elem_type \"const int\"",
1691	.raw_types = {
1692		/* int */				/* [1] */
1693		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1694		/* int[16] */				/* [2] */
1695		BTF_TYPE_ARRAY_ENC(3, 3, 16),
1696		/* CONST type_id=1 */			/* [3] */
1697		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 1),
1698		BTF_END_RAW,
1699	},
1700	.str_sec = "",
1701	.str_sec_size = sizeof(""),
1702	.map_type = BPF_MAP_TYPE_ARRAY,
1703	.map_name = "array_test_map",
1704	.key_size = sizeof(int),
1705	.value_size = sizeof(int),
1706	.key_type_id = 1,
1707	.value_type_id = 1,
1708	.max_entries = 4,
1709},
1710
1711{
1712	.descr = "array test. index_type \"const int:31\"",
1713	.raw_types = {
1714		/* int */				/* [1] */
1715		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1716		/* int:31 */				/* [2] */
1717		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 31, 4),
1718		/* int[16] */				/* [3] */
1719		BTF_TYPE_ARRAY_ENC(1, 4, 16),
1720		/* CONST type_id=2 */			/* [4] */
1721		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 2),
1722		BTF_END_RAW,
1723	},
1724	.str_sec = "",
1725	.str_sec_size = sizeof(""),
1726	.map_type = BPF_MAP_TYPE_ARRAY,
1727	.map_name = "array_test_map",
1728	.key_size = sizeof(int),
1729	.value_size = sizeof(int),
1730	.key_type_id = 1,
1731	.value_type_id = 1,
1732	.max_entries = 4,
1733	.btf_load_err = true,
1734	.err_str = "Invalid index",
1735},
1736
1737{
1738	.descr = "array test. elem_type \"const int:31\"",
1739	.raw_types = {
1740		/* int */				/* [1] */
1741		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1742		/* int:31 */				/* [2] */
1743		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 31, 4),
1744		/* int[16] */				/* [3] */
1745		BTF_TYPE_ARRAY_ENC(4, 1, 16),
1746		/* CONST type_id=2 */			/* [4] */
1747		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 2),
1748		BTF_END_RAW,
1749	},
1750	.str_sec = "",
1751	.str_sec_size = sizeof(""),
1752	.map_type = BPF_MAP_TYPE_ARRAY,
1753	.map_name = "array_test_map",
1754	.key_size = sizeof(int),
1755	.value_size = sizeof(int),
1756	.key_type_id = 1,
1757	.value_type_id = 1,
1758	.max_entries = 4,
1759	.btf_load_err = true,
1760	.err_str = "Invalid array of int",
1761},
1762
1763{
1764	.descr = "array test. index_type \"void\"",
1765	.raw_types = {
1766		/* int */				/* [1] */
1767		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1768		/* int[16] */				/* [2] */
1769		BTF_TYPE_ARRAY_ENC(1, 0, 16),
1770		BTF_END_RAW,
1771	},
1772	.str_sec = "",
1773	.str_sec_size = sizeof(""),
1774	.map_type = BPF_MAP_TYPE_ARRAY,
1775	.map_name = "array_test_map",
1776	.key_size = sizeof(int),
1777	.value_size = sizeof(int),
1778	.key_type_id = 1,
1779	.value_type_id = 1,
1780	.max_entries = 4,
1781	.btf_load_err = true,
1782	.err_str = "Invalid index",
1783},
1784
1785{
1786	.descr = "array test. index_type \"const void\"",
1787	.raw_types = {
1788		/* int */				/* [1] */
1789		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1790		/* int[16] */				/* [2] */
1791		BTF_TYPE_ARRAY_ENC(1, 3, 16),
1792		/* CONST type_id=0 (void) */		/* [3] */
1793		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1794		BTF_END_RAW,
1795	},
1796	.str_sec = "",
1797	.str_sec_size = sizeof(""),
1798	.map_type = BPF_MAP_TYPE_ARRAY,
1799	.map_name = "array_test_map",
1800	.key_size = sizeof(int),
1801	.value_size = sizeof(int),
1802	.key_type_id = 1,
1803	.value_type_id = 1,
1804	.max_entries = 4,
1805	.btf_load_err = true,
1806	.err_str = "Invalid index",
1807},
1808
1809{
1810	.descr = "array test. elem_type \"const void\"",
1811	.raw_types = {
1812		/* int */				/* [1] */
1813		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1814		/* int[16] */				/* [2] */
1815		BTF_TYPE_ARRAY_ENC(3, 1, 16),
1816		/* CONST type_id=0 (void) */		/* [3] */
1817		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1818		BTF_END_RAW,
1819	},
1820	.str_sec = "",
1821	.str_sec_size = sizeof(""),
1822	.map_type = BPF_MAP_TYPE_ARRAY,
1823	.map_name = "array_test_map",
1824	.key_size = sizeof(int),
1825	.value_size = sizeof(int),
1826	.key_type_id = 1,
1827	.value_type_id = 1,
1828	.max_entries = 4,
1829	.btf_load_err = true,
1830	.err_str = "Invalid elem",
1831},
1832
1833{
1834	.descr = "array test. elem_type \"const void *\"",
1835	.raw_types = {
1836		/* int */				/* [1] */
1837		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1838		/* const void *[16] */			/* [2] */
1839		BTF_TYPE_ARRAY_ENC(3, 1, 16),
1840		/* CONST type_id=4 */			/* [3] */
1841		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 4),
1842		/* void* */				/* [4] */
1843		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
1844		BTF_END_RAW,
1845	},
1846	.str_sec = "",
1847	.str_sec_size = sizeof(""),
1848	.map_type = BPF_MAP_TYPE_ARRAY,
1849	.map_name = "array_test_map",
1850	.key_size = sizeof(int),
1851	.value_size = sizeof(int),
1852	.key_type_id = 1,
1853	.value_type_id = 1,
1854	.max_entries = 4,
1855},
1856
1857{
1858	.descr = "array test. index_type \"const void *\"",
1859	.raw_types = {
1860		/* int */				/* [1] */
1861		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1862		/* const void *[16] */			/* [2] */
1863		BTF_TYPE_ARRAY_ENC(3, 3, 16),
1864		/* CONST type_id=4 */			/* [3] */
1865		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 4),
1866		/* void* */				/* [4] */
1867		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
1868		BTF_END_RAW,
1869	},
1870	.str_sec = "",
1871	.str_sec_size = sizeof(""),
1872	.map_type = BPF_MAP_TYPE_ARRAY,
1873	.map_name = "array_test_map",
1874	.key_size = sizeof(int),
1875	.value_size = sizeof(int),
1876	.key_type_id = 1,
1877	.value_type_id = 1,
1878	.max_entries = 4,
1879	.btf_load_err = true,
1880	.err_str = "Invalid index",
1881},
1882
1883{
1884	.descr = "array test. t->size != 0\"",
1885	.raw_types = {
1886		/* int */				/* [1] */
1887		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1888		/* int[16] */				/* [2] */
1889		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ARRAY, 0, 0), 1),
1890		BTF_ARRAY_ENC(1, 1, 16),
1891		BTF_END_RAW,
1892	},
1893	.str_sec = "",
1894	.str_sec_size = sizeof(""),
1895	.map_type = BPF_MAP_TYPE_ARRAY,
1896	.map_name = "array_test_map",
1897	.key_size = sizeof(int),
1898	.value_size = sizeof(int),
1899	.key_type_id = 1,
1900	.value_type_id = 1,
1901	.max_entries = 4,
1902	.btf_load_err = true,
1903	.err_str = "size != 0",
1904},
1905
1906{
1907	.descr = "int test. invalid int_data",
1908	.raw_types = {
1909		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), 4),
1910		0x10000000,
1911		BTF_END_RAW,
1912	},
1913	.str_sec = "",
1914	.str_sec_size = sizeof(""),
1915	.map_type = BPF_MAP_TYPE_ARRAY,
1916	.map_name = "array_test_map",
1917	.key_size = sizeof(int),
1918	.value_size = sizeof(int),
1919	.key_type_id = 1,
1920	.value_type_id = 1,
1921	.max_entries = 4,
1922	.btf_load_err = true,
1923	.err_str = "Invalid int_data",
1924},
1925
1926{
1927	.descr = "invalid BTF_INFO",
1928	.raw_types = {
1929		/* int */				/* [1] */
1930		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1931		BTF_TYPE_ENC(0, 0x20000000, 4),
1932		BTF_END_RAW,
1933	},
1934	.str_sec = "",
1935	.str_sec_size = sizeof(""),
1936	.map_type = BPF_MAP_TYPE_ARRAY,
1937	.map_name = "array_test_map",
1938	.key_size = sizeof(int),
1939	.value_size = sizeof(int),
1940	.key_type_id = 1,
1941	.value_type_id = 1,
1942	.max_entries = 4,
1943	.btf_load_err = true,
1944	.err_str = "Invalid btf_info",
1945},
1946
1947{
1948	.descr = "fwd test. t->type != 0\"",
1949	.raw_types = {
1950		/* int */				/* [1] */
1951		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1952		/* fwd type */				/* [2] */
1953		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FWD, 0, 0), 1),
1954		BTF_END_RAW,
1955	},
1956	.str_sec = "",
1957	.str_sec_size = sizeof(""),
1958	.map_type = BPF_MAP_TYPE_ARRAY,
1959	.map_name = "fwd_test_map",
1960	.key_size = sizeof(int),
1961	.value_size = sizeof(int),
1962	.key_type_id = 1,
1963	.value_type_id = 1,
1964	.max_entries = 4,
1965	.btf_load_err = true,
1966	.err_str = "type != 0",
1967},
1968
1969{
1970	.descr = "typedef (invalid name, name_off = 0)",
1971	.raw_types = {
1972		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
1973		BTF_TYPEDEF_ENC(0, 1),				/* [2] */
1974		BTF_END_RAW,
1975	},
1976	.str_sec = "\0__int",
1977	.str_sec_size = sizeof("\0__int"),
1978	.map_type = BPF_MAP_TYPE_ARRAY,
1979	.map_name = "typedef_check_btf",
1980	.key_size = sizeof(int),
1981	.value_size = sizeof(int),
1982	.key_type_id = 1,
1983	.value_type_id = 1,
1984	.max_entries = 4,
1985	.btf_load_err = true,
1986	.err_str = "Invalid name",
1987},
1988
1989{
1990	.descr = "typedef (invalid name, invalid identifier)",
1991	.raw_types = {
1992		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
1993		BTF_TYPEDEF_ENC(NAME_TBD, 1),			/* [2] */
1994		BTF_END_RAW,
1995	},
1996	.str_sec = "\0__!int",
1997	.str_sec_size = sizeof("\0__!int"),
1998	.map_type = BPF_MAP_TYPE_ARRAY,
1999	.map_name = "typedef_check_btf",
2000	.key_size = sizeof(int),
2001	.value_size = sizeof(int),
2002	.key_type_id = 1,
2003	.value_type_id = 1,
2004	.max_entries = 4,
2005	.btf_load_err = true,
2006	.err_str = "Invalid name",
2007},
2008
2009{
2010	.descr = "ptr type (invalid name, name_off <> 0)",
2011	.raw_types = {
2012		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2013		BTF_TYPE_ENC(NAME_TBD,
2014			     BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 1),	/* [2] */
2015		BTF_END_RAW,
2016	},
2017	.str_sec = "\0__int",
2018	.str_sec_size = sizeof("\0__int"),
2019	.map_type = BPF_MAP_TYPE_ARRAY,
2020	.map_name = "ptr_type_check_btf",
2021	.key_size = sizeof(int),
2022	.value_size = sizeof(int),
2023	.key_type_id = 1,
2024	.value_type_id = 1,
2025	.max_entries = 4,
2026	.btf_load_err = true,
2027	.err_str = "Invalid name",
2028},
2029
2030{
2031	.descr = "volatile type (invalid name, name_off <> 0)",
2032	.raw_types = {
2033		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2034		BTF_TYPE_ENC(NAME_TBD,
2035			     BTF_INFO_ENC(BTF_KIND_VOLATILE, 0, 0), 1),	/* [2] */
2036		BTF_END_RAW,
2037	},
2038	.str_sec = "\0__int",
2039	.str_sec_size = sizeof("\0__int"),
2040	.map_type = BPF_MAP_TYPE_ARRAY,
2041	.map_name = "volatile_type_check_btf",
2042	.key_size = sizeof(int),
2043	.value_size = sizeof(int),
2044	.key_type_id = 1,
2045	.value_type_id = 1,
2046	.max_entries = 4,
2047	.btf_load_err = true,
2048	.err_str = "Invalid name",
2049},
2050
2051{
2052	.descr = "const type (invalid name, name_off <> 0)",
2053	.raw_types = {
2054		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2055		BTF_TYPE_ENC(NAME_TBD,
2056			     BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 1),	/* [2] */
2057		BTF_END_RAW,
2058	},
2059	.str_sec = "\0__int",
2060	.str_sec_size = sizeof("\0__int"),
2061	.map_type = BPF_MAP_TYPE_ARRAY,
2062	.map_name = "const_type_check_btf",
2063	.key_size = sizeof(int),
2064	.value_size = sizeof(int),
2065	.key_type_id = 1,
2066	.value_type_id = 1,
2067	.max_entries = 4,
2068	.btf_load_err = true,
2069	.err_str = "Invalid name",
2070},
2071
2072{
2073	.descr = "restrict type (invalid name, name_off <> 0)",
2074	.raw_types = {
2075		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2076		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 1),	/* [2] */
2077		BTF_TYPE_ENC(NAME_TBD,
2078			     BTF_INFO_ENC(BTF_KIND_RESTRICT, 0, 0), 2),	/* [3] */
2079		BTF_END_RAW,
2080	},
2081	.str_sec = "\0__int",
2082	.str_sec_size = sizeof("\0__int"),
2083	.map_type = BPF_MAP_TYPE_ARRAY,
2084	.map_name = "restrict_type_check_btf",
2085	.key_size = sizeof(int),
2086	.value_size = sizeof(int),
2087	.key_type_id = 1,
2088	.value_type_id = 1,
2089	.max_entries = 4,
2090	.btf_load_err = true,
2091	.err_str = "Invalid name",
2092},
2093
2094{
2095	.descr = "fwd type (invalid name, name_off = 0)",
2096	.raw_types = {
2097		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2098		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FWD, 0, 0), 0),	/* [2] */
2099		BTF_END_RAW,
2100	},
2101	.str_sec = "\0__skb",
2102	.str_sec_size = sizeof("\0__skb"),
2103	.map_type = BPF_MAP_TYPE_ARRAY,
2104	.map_name = "fwd_type_check_btf",
2105	.key_size = sizeof(int),
2106	.value_size = sizeof(int),
2107	.key_type_id = 1,
2108	.value_type_id = 1,
2109	.max_entries = 4,
2110	.btf_load_err = true,
2111	.err_str = "Invalid name",
2112},
2113
2114{
2115	.descr = "fwd type (invalid name, invalid identifier)",
2116	.raw_types = {
2117		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2118		BTF_TYPE_ENC(NAME_TBD,
2119			     BTF_INFO_ENC(BTF_KIND_FWD, 0, 0), 0),	/* [2] */
2120		BTF_END_RAW,
2121	},
2122	.str_sec = "\0__!skb",
2123	.str_sec_size = sizeof("\0__!skb"),
2124	.map_type = BPF_MAP_TYPE_ARRAY,
2125	.map_name = "fwd_type_check_btf",
2126	.key_size = sizeof(int),
2127	.value_size = sizeof(int),
2128	.key_type_id = 1,
2129	.value_type_id = 1,
2130	.max_entries = 4,
2131	.btf_load_err = true,
2132	.err_str = "Invalid name",
2133},
2134
2135{
2136	.descr = "array type (invalid name, name_off <> 0)",
2137	.raw_types = {
2138		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2139		BTF_TYPE_ENC(NAME_TBD,
2140			     BTF_INFO_ENC(BTF_KIND_ARRAY, 0, 0), 0),	/* [2] */
2141		BTF_ARRAY_ENC(1, 1, 4),
2142		BTF_END_RAW,
2143	},
2144	.str_sec = "\0__skb",
2145	.str_sec_size = sizeof("\0__skb"),
2146	.map_type = BPF_MAP_TYPE_ARRAY,
2147	.map_name = "array_type_check_btf",
2148	.key_size = sizeof(int),
2149	.value_size = sizeof(int),
2150	.key_type_id = 1,
2151	.value_type_id = 1,
2152	.max_entries = 4,
2153	.btf_load_err = true,
2154	.err_str = "Invalid name",
2155},
2156
2157{
2158	.descr = "struct type (name_off = 0)",
2159	.raw_types = {
2160		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2161		BTF_TYPE_ENC(0,
2162			     BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4),	/* [2] */
2163		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
2164		BTF_END_RAW,
2165	},
2166	.str_sec = "\0A",
2167	.str_sec_size = sizeof("\0A"),
2168	.map_type = BPF_MAP_TYPE_ARRAY,
2169	.map_name = "struct_type_check_btf",
2170	.key_size = sizeof(int),
2171	.value_size = sizeof(int),
2172	.key_type_id = 1,
2173	.value_type_id = 1,
2174	.max_entries = 4,
2175},
2176
2177{
2178	.descr = "struct type (invalid name, invalid identifier)",
2179	.raw_types = {
2180		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2181		BTF_TYPE_ENC(NAME_TBD,
2182			     BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4),	/* [2] */
2183		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
2184		BTF_END_RAW,
2185	},
2186	.str_sec = "\0A!\0B",
2187	.str_sec_size = sizeof("\0A!\0B"),
2188	.map_type = BPF_MAP_TYPE_ARRAY,
2189	.map_name = "struct_type_check_btf",
2190	.key_size = sizeof(int),
2191	.value_size = sizeof(int),
2192	.key_type_id = 1,
2193	.value_type_id = 1,
2194	.max_entries = 4,
2195	.btf_load_err = true,
2196	.err_str = "Invalid name",
2197},
2198
2199{
2200	.descr = "struct member (name_off = 0)",
2201	.raw_types = {
2202		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2203		BTF_TYPE_ENC(0,
2204			     BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4),	/* [2] */
2205		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
2206		BTF_END_RAW,
2207	},
2208	.str_sec = "\0A",
2209	.str_sec_size = sizeof("\0A"),
2210	.map_type = BPF_MAP_TYPE_ARRAY,
2211	.map_name = "struct_type_check_btf",
2212	.key_size = sizeof(int),
2213	.value_size = sizeof(int),
2214	.key_type_id = 1,
2215	.value_type_id = 1,
2216	.max_entries = 4,
2217},
2218
2219{
2220	.descr = "struct member (invalid name, invalid identifier)",
2221	.raw_types = {
2222		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2223		BTF_TYPE_ENC(NAME_TBD,
2224			     BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4),	/* [2] */
2225		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
2226		BTF_END_RAW,
2227	},
2228	.str_sec = "\0A\0B*",
2229	.str_sec_size = sizeof("\0A\0B*"),
2230	.map_type = BPF_MAP_TYPE_ARRAY,
2231	.map_name = "struct_type_check_btf",
2232	.key_size = sizeof(int),
2233	.value_size = sizeof(int),
2234	.key_type_id = 1,
2235	.value_type_id = 1,
2236	.max_entries = 4,
2237	.btf_load_err = true,
2238	.err_str = "Invalid name",
2239},
2240
2241{
2242	.descr = "enum type (name_off = 0)",
2243	.raw_types = {
2244		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2245		BTF_TYPE_ENC(0,
2246			     BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1),
2247			     sizeof(int)),				/* [2] */
2248		BTF_ENUM_ENC(NAME_TBD, 0),
2249		BTF_END_RAW,
2250	},
2251	.str_sec = "\0A\0B",
2252	.str_sec_size = sizeof("\0A\0B"),
2253	.map_type = BPF_MAP_TYPE_ARRAY,
2254	.map_name = "enum_type_check_btf",
2255	.key_size = sizeof(int),
2256	.value_size = sizeof(int),
2257	.key_type_id = 1,
2258	.value_type_id = 1,
2259	.max_entries = 4,
2260},
2261
2262{
2263	.descr = "enum type (invalid name, invalid identifier)",
2264	.raw_types = {
2265		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2266		BTF_TYPE_ENC(NAME_TBD,
2267			     BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1),
2268			     sizeof(int)),				/* [2] */
2269		BTF_ENUM_ENC(NAME_TBD, 0),
2270		BTF_END_RAW,
2271	},
2272	.str_sec = "\0A!\0B",
2273	.str_sec_size = sizeof("\0A!\0B"),
2274	.map_type = BPF_MAP_TYPE_ARRAY,
2275	.map_name = "enum_type_check_btf",
2276	.key_size = sizeof(int),
2277	.value_size = sizeof(int),
2278	.key_type_id = 1,
2279	.value_type_id = 1,
2280	.max_entries = 4,
2281	.btf_load_err = true,
2282	.err_str = "Invalid name",
2283},
2284
2285{
2286	.descr = "enum member (invalid name, name_off = 0)",
2287	.raw_types = {
2288		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2289		BTF_TYPE_ENC(0,
2290			     BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1),
2291			     sizeof(int)),				/* [2] */
2292		BTF_ENUM_ENC(0, 0),
2293		BTF_END_RAW,
2294	},
2295	.str_sec = "",
2296	.str_sec_size = sizeof(""),
2297	.map_type = BPF_MAP_TYPE_ARRAY,
2298	.map_name = "enum_type_check_btf",
2299	.key_size = sizeof(int),
2300	.value_size = sizeof(int),
2301	.key_type_id = 1,
2302	.value_type_id = 1,
2303	.max_entries = 4,
2304	.btf_load_err = true,
2305	.err_str = "Invalid name",
2306},
2307
2308{
2309	.descr = "enum member (invalid name, invalid identifier)",
2310	.raw_types = {
2311		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2312		BTF_TYPE_ENC(0,
2313			     BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1),
2314			     sizeof(int)),				/* [2] */
2315		BTF_ENUM_ENC(NAME_TBD, 0),
2316		BTF_END_RAW,
2317	},
2318	.str_sec = "\0A!",
2319	.str_sec_size = sizeof("\0A!"),
2320	.map_type = BPF_MAP_TYPE_ARRAY,
2321	.map_name = "enum_type_check_btf",
2322	.key_size = sizeof(int),
2323	.value_size = sizeof(int),
2324	.key_type_id = 1,
2325	.value_type_id = 1,
2326	.max_entries = 4,
2327	.btf_load_err = true,
2328	.err_str = "Invalid name",
2329},
2330{
2331	.descr = "arraymap invalid btf key (a bit field)",
2332	.raw_types = {
2333		/* int */				/* [1] */
2334		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
2335		/* 32 bit int with 32 bit offset */	/* [2] */
2336		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 32, 32, 8),
2337		BTF_END_RAW,
2338	},
2339	.str_sec = "",
2340	.str_sec_size = sizeof(""),
2341	.map_type = BPF_MAP_TYPE_ARRAY,
2342	.map_name = "array_map_check_btf",
2343	.key_size = sizeof(int),
2344	.value_size = sizeof(int),
2345	.key_type_id = 2,
2346	.value_type_id = 1,
2347	.max_entries = 4,
2348	.map_create_err = true,
2349},
2350
2351{
2352	.descr = "arraymap invalid btf key (!= 32 bits)",
2353	.raw_types = {
2354		/* int */				/* [1] */
2355		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
2356		/* 16 bit int with 0 bit offset */	/* [2] */
2357		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 16, 2),
2358		BTF_END_RAW,
2359	},
2360	.str_sec = "",
2361	.str_sec_size = sizeof(""),
2362	.map_type = BPF_MAP_TYPE_ARRAY,
2363	.map_name = "array_map_check_btf",
2364	.key_size = sizeof(int),
2365	.value_size = sizeof(int),
2366	.key_type_id = 2,
2367	.value_type_id = 1,
2368	.max_entries = 4,
2369	.map_create_err = true,
2370},
2371
2372{
2373	.descr = "arraymap invalid btf value (too small)",
2374	.raw_types = {
2375		/* int */				/* [1] */
2376		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
2377		BTF_END_RAW,
2378	},
2379	.str_sec = "",
2380	.str_sec_size = sizeof(""),
2381	.map_type = BPF_MAP_TYPE_ARRAY,
2382	.map_name = "array_map_check_btf",
2383	.key_size = sizeof(int),
2384	/* btf_value_size < map->value_size */
2385	.value_size = sizeof(__u64),
2386	.key_type_id = 1,
2387	.value_type_id = 1,
2388	.max_entries = 4,
2389	.map_create_err = true,
2390},
2391
2392{
2393	.descr = "arraymap invalid btf value (too big)",
2394	.raw_types = {
2395		/* int */				/* [1] */
2396		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
2397		BTF_END_RAW,
2398	},
2399	.str_sec = "",
2400	.str_sec_size = sizeof(""),
2401	.map_type = BPF_MAP_TYPE_ARRAY,
2402	.map_name = "array_map_check_btf",
2403	.key_size = sizeof(int),
2404	/* btf_value_size > map->value_size */
2405	.value_size = sizeof(__u16),
2406	.key_type_id = 1,
2407	.value_type_id = 1,
2408	.max_entries = 4,
2409	.map_create_err = true,
2410},
2411
2412{
2413	.descr = "func proto (int (*)(int, unsigned int))",
2414	.raw_types = {
2415		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2416		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2417		/* int (*)(int, unsigned int) */
2418		BTF_FUNC_PROTO_ENC(1, 2),			/* [3] */
2419			BTF_FUNC_PROTO_ARG_ENC(0, 1),
2420			BTF_FUNC_PROTO_ARG_ENC(0, 2),
2421		BTF_END_RAW,
2422	},
2423	.str_sec = "",
2424	.str_sec_size = sizeof(""),
2425	.map_type = BPF_MAP_TYPE_ARRAY,
2426	.map_name = "func_proto_type_check_btf",
2427	.key_size = sizeof(int),
2428	.value_size = sizeof(int),
2429	.key_type_id = 1,
2430	.value_type_id = 1,
2431	.max_entries = 4,
2432},
2433
2434{
2435	.descr = "func proto (vararg)",
2436	.raw_types = {
2437		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2438		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2439		/* void (*)(int, unsigned int, ...) */
2440		BTF_FUNC_PROTO_ENC(0, 3),			/* [3] */
2441			BTF_FUNC_PROTO_ARG_ENC(0, 1),
2442			BTF_FUNC_PROTO_ARG_ENC(0, 2),
2443			BTF_FUNC_PROTO_ARG_ENC(0, 0),
2444		BTF_END_RAW,
2445	},
2446	.str_sec = "",
2447	.str_sec_size = sizeof(""),
2448	.map_type = BPF_MAP_TYPE_ARRAY,
2449	.map_name = "func_proto_type_check_btf",
2450	.key_size = sizeof(int),
2451	.value_size = sizeof(int),
2452	.key_type_id = 1,
2453	.value_type_id = 1,
2454	.max_entries = 4,
2455},
2456
2457{
2458	.descr = "func proto (vararg with name)",
2459	.raw_types = {
2460		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2461		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2462		/* void (*)(int a, unsigned int b, ... c) */
2463		BTF_FUNC_PROTO_ENC(0, 3),			/* [3] */
2464			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2465			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2466			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 0),
2467		BTF_END_RAW,
2468	},
2469	.str_sec = "\0a\0b\0c",
2470	.str_sec_size = sizeof("\0a\0b\0c"),
2471	.map_type = BPF_MAP_TYPE_ARRAY,
2472	.map_name = "func_proto_type_check_btf",
2473	.key_size = sizeof(int),
2474	.value_size = sizeof(int),
2475	.key_type_id = 1,
2476	.value_type_id = 1,
2477	.max_entries = 4,
2478	.btf_load_err = true,
2479	.err_str = "Invalid arg#3",
2480},
2481
2482{
2483	.descr = "func proto (arg after vararg)",
2484	.raw_types = {
2485		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2486		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2487		/* void (*)(int a, ..., unsigned int b) */
2488		BTF_FUNC_PROTO_ENC(0, 3),			/* [3] */
2489			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2490			BTF_FUNC_PROTO_ARG_ENC(0, 0),
2491			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2492		BTF_END_RAW,
2493	},
2494	.str_sec = "\0a\0b",
2495	.str_sec_size = sizeof("\0a\0b"),
2496	.map_type = BPF_MAP_TYPE_ARRAY,
2497	.map_name = "func_proto_type_check_btf",
2498	.key_size = sizeof(int),
2499	.value_size = sizeof(int),
2500	.key_type_id = 1,
2501	.value_type_id = 1,
2502	.max_entries = 4,
2503	.btf_load_err = true,
2504	.err_str = "Invalid arg#2",
2505},
2506
2507{
2508	.descr = "func proto (CONST=>TYPEDEF=>PTR=>FUNC_PROTO)",
2509	.raw_types = {
2510		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2511		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2512		/* typedef void (*func_ptr)(int, unsigned int) */
2513		BTF_TYPEDEF_ENC(NAME_TBD, 5),			/* [3] */
2514		/* const func_ptr */
2515		BTF_CONST_ENC(3),				/* [4] */
2516		BTF_PTR_ENC(6),					/* [5] */
2517		BTF_FUNC_PROTO_ENC(0, 2),			/* [6] */
2518			BTF_FUNC_PROTO_ARG_ENC(0, 1),
2519			BTF_FUNC_PROTO_ARG_ENC(0, 2),
2520		BTF_END_RAW,
2521	},
2522	.str_sec = "\0func_ptr",
2523	.str_sec_size = sizeof("\0func_ptr"),
2524	.map_type = BPF_MAP_TYPE_ARRAY,
2525	.map_name = "func_proto_type_check_btf",
2526	.key_size = sizeof(int),
2527	.value_size = sizeof(int),
2528	.key_type_id = 1,
2529	.value_type_id = 1,
2530	.max_entries = 4,
2531},
2532
2533{
2534	.descr = "func proto (TYPEDEF=>FUNC_PROTO)",
2535	.raw_types = {
2536		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2537		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2538		BTF_TYPEDEF_ENC(NAME_TBD, 4),			/* [3] */
2539		BTF_FUNC_PROTO_ENC(0, 2),			/* [4] */
2540			BTF_FUNC_PROTO_ARG_ENC(0, 1),
2541			BTF_FUNC_PROTO_ARG_ENC(0, 2),
2542		BTF_END_RAW,
2543	},
2544	.str_sec = "\0func_typedef",
2545	.str_sec_size = sizeof("\0func_typedef"),
2546	.map_type = BPF_MAP_TYPE_ARRAY,
2547	.map_name = "func_proto_type_check_btf",
2548	.key_size = sizeof(int),
2549	.value_size = sizeof(int),
2550	.key_type_id = 1,
2551	.value_type_id = 1,
2552	.max_entries = 4,
2553},
2554
2555{
2556	.descr = "func proto (btf_resolve(arg))",
2557	.raw_types = {
2558		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2559		/* void (*)(const void *) */
2560		BTF_FUNC_PROTO_ENC(0, 1),			/* [2] */
2561			BTF_FUNC_PROTO_ARG_ENC(0, 3),
2562		BTF_CONST_ENC(4),				/* [3] */
2563		BTF_PTR_ENC(0),					/* [4] */
2564		BTF_END_RAW,
2565	},
2566	.str_sec = "",
2567	.str_sec_size = sizeof(""),
2568	.map_type = BPF_MAP_TYPE_ARRAY,
2569	.map_name = "func_proto_type_check_btf",
2570	.key_size = sizeof(int),
2571	.value_size = sizeof(int),
2572	.key_type_id = 1,
2573	.value_type_id = 1,
2574	.max_entries = 4,
2575},
2576
2577{
2578	.descr = "func proto (Not all arg has name)",
2579	.raw_types = {
2580		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2581		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2582		/* void (*)(int, unsigned int b) */
2583		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2584			BTF_FUNC_PROTO_ARG_ENC(0, 1),
2585			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2586		BTF_END_RAW,
2587	},
2588	.str_sec = "\0b",
2589	.str_sec_size = sizeof("\0b"),
2590	.map_type = BPF_MAP_TYPE_ARRAY,
2591	.map_name = "func_proto_type_check_btf",
2592	.key_size = sizeof(int),
2593	.value_size = sizeof(int),
2594	.key_type_id = 1,
2595	.value_type_id = 1,
2596	.max_entries = 4,
2597},
2598
2599{
2600	.descr = "func proto (Bad arg name_off)",
2601	.raw_types = {
2602		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2603		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2604		/* void (*)(int a, unsigned int <bad_name_off>) */
2605		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2606			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2607			BTF_FUNC_PROTO_ARG_ENC(0x0fffffff, 2),
2608		BTF_END_RAW,
2609	},
2610	.str_sec = "\0a",
2611	.str_sec_size = sizeof("\0a"),
2612	.map_type = BPF_MAP_TYPE_ARRAY,
2613	.map_name = "func_proto_type_check_btf",
2614	.key_size = sizeof(int),
2615	.value_size = sizeof(int),
2616	.key_type_id = 1,
2617	.value_type_id = 1,
2618	.max_entries = 4,
2619	.btf_load_err = true,
2620	.err_str = "Invalid arg#2",
2621},
2622
2623{
2624	.descr = "func proto (Bad arg name)",
2625	.raw_types = {
2626		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2627		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2628		/* void (*)(int a, unsigned int !!!) */
2629		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2630			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2631			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2632		BTF_END_RAW,
2633	},
2634	.str_sec = "\0a\0!!!",
2635	.str_sec_size = sizeof("\0a\0!!!"),
2636	.map_type = BPF_MAP_TYPE_ARRAY,
2637	.map_name = "func_proto_type_check_btf",
2638	.key_size = sizeof(int),
2639	.value_size = sizeof(int),
2640	.key_type_id = 1,
2641	.value_type_id = 1,
2642	.max_entries = 4,
2643	.btf_load_err = true,
2644	.err_str = "Invalid arg#2",
2645},
2646
2647{
2648	.descr = "func proto (Invalid return type)",
2649	.raw_types = {
2650		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2651		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2652		/* <bad_ret_type> (*)(int, unsigned int) */
2653		BTF_FUNC_PROTO_ENC(100, 2),			/* [3] */
2654			BTF_FUNC_PROTO_ARG_ENC(0, 1),
2655			BTF_FUNC_PROTO_ARG_ENC(0, 2),
2656		BTF_END_RAW,
2657	},
2658	.str_sec = "",
2659	.str_sec_size = sizeof(""),
2660	.map_type = BPF_MAP_TYPE_ARRAY,
2661	.map_name = "func_proto_type_check_btf",
2662	.key_size = sizeof(int),
2663	.value_size = sizeof(int),
2664	.key_type_id = 1,
2665	.value_type_id = 1,
2666	.max_entries = 4,
2667	.btf_load_err = true,
2668	.err_str = "Invalid return type",
2669},
2670
2671{
2672	.descr = "func proto (with func name)",
2673	.raw_types = {
2674		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2675		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2676		/* void func_proto(int, unsigned int) */
2677		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 2), 0),	/* [3] */
2678			BTF_FUNC_PROTO_ARG_ENC(0, 1),
2679			BTF_FUNC_PROTO_ARG_ENC(0, 2),
2680		BTF_END_RAW,
2681	},
2682	.str_sec = "\0func_proto",
2683	.str_sec_size = sizeof("\0func_proto"),
2684	.map_type = BPF_MAP_TYPE_ARRAY,
2685	.map_name = "func_proto_type_check_btf",
2686	.key_size = sizeof(int),
2687	.value_size = sizeof(int),
2688	.key_type_id = 1,
2689	.value_type_id = 1,
2690	.max_entries = 4,
2691	.btf_load_err = true,
2692	.err_str = "Invalid name",
2693},
2694
2695{
2696	.descr = "func proto (const void arg)",
2697	.raw_types = {
2698		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2699		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2700		/* void (*)(const void) */
2701		BTF_FUNC_PROTO_ENC(0, 1),			/* [3] */
2702			BTF_FUNC_PROTO_ARG_ENC(0, 4),
2703		BTF_CONST_ENC(0),				/* [4] */
2704		BTF_END_RAW,
2705	},
2706	.str_sec = "",
2707	.str_sec_size = sizeof(""),
2708	.map_type = BPF_MAP_TYPE_ARRAY,
2709	.map_name = "func_proto_type_check_btf",
2710	.key_size = sizeof(int),
2711	.value_size = sizeof(int),
2712	.key_type_id = 1,
2713	.value_type_id = 1,
2714	.max_entries = 4,
2715	.btf_load_err = true,
2716	.err_str = "Invalid arg#1",
2717},
2718
2719{
2720	.descr = "func (void func(int a, unsigned int b))",
2721	.raw_types = {
2722		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2723		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2724		/* void (*)(int a, unsigned int b) */
2725		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2726			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2727			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2728		/* void func(int a, unsigned int b) */
2729		BTF_FUNC_ENC(NAME_TBD, 3),			/* [4] */
2730		BTF_END_RAW,
2731	},
2732	.str_sec = "\0a\0b\0func",
2733	.str_sec_size = sizeof("\0a\0b\0func"),
2734	.map_type = BPF_MAP_TYPE_ARRAY,
2735	.map_name = "func_type_check_btf",
2736	.key_size = sizeof(int),
2737	.value_size = sizeof(int),
2738	.key_type_id = 1,
2739	.value_type_id = 1,
2740	.max_entries = 4,
2741},
2742
2743{
2744	.descr = "func (No func name)",
2745	.raw_types = {
2746		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2747		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2748		/* void (*)(int a, unsigned int b) */
2749		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2750			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2751			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2752		/* void <no_name>(int a, unsigned int b) */
2753		BTF_FUNC_ENC(0, 3),				/* [4] */
2754		BTF_END_RAW,
2755	},
2756	.str_sec = "\0a\0b",
2757	.str_sec_size = sizeof("\0a\0b"),
2758	.map_type = BPF_MAP_TYPE_ARRAY,
2759	.map_name = "func_type_check_btf",
2760	.key_size = sizeof(int),
2761	.value_size = sizeof(int),
2762	.key_type_id = 1,
2763	.value_type_id = 1,
2764	.max_entries = 4,
2765	.btf_load_err = true,
2766	.err_str = "Invalid name",
2767},
2768
2769{
2770	.descr = "func (Invalid func name)",
2771	.raw_types = {
2772		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2773		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2774		/* void (*)(int a, unsigned int b) */
2775		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2776			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2777			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2778		/* void !!!(int a, unsigned int b) */
2779		BTF_FUNC_ENC(NAME_TBD, 3),			/* [4] */
2780		BTF_END_RAW,
2781	},
2782	.str_sec = "\0a\0b\0!!!",
2783	.str_sec_size = sizeof("\0a\0b\0!!!"),
2784	.map_type = BPF_MAP_TYPE_ARRAY,
2785	.map_name = "func_type_check_btf",
2786	.key_size = sizeof(int),
2787	.value_size = sizeof(int),
2788	.key_type_id = 1,
2789	.value_type_id = 1,
2790	.max_entries = 4,
2791	.btf_load_err = true,
2792	.err_str = "Invalid name",
2793},
2794
2795{
2796	.descr = "func (Some arg has no name)",
2797	.raw_types = {
2798		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2799		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2800		/* void (*)(int a, unsigned int) */
2801		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2802			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2803			BTF_FUNC_PROTO_ARG_ENC(0, 2),
2804		/* void func(int a, unsigned int) */
2805		BTF_FUNC_ENC(NAME_TBD, 3),			/* [4] */
2806		BTF_END_RAW,
2807	},
2808	.str_sec = "\0a\0func",
2809	.str_sec_size = sizeof("\0a\0func"),
2810	.map_type = BPF_MAP_TYPE_ARRAY,
2811	.map_name = "func_type_check_btf",
2812	.key_size = sizeof(int),
2813	.value_size = sizeof(int),
2814	.key_type_id = 1,
2815	.value_type_id = 1,
2816	.max_entries = 4,
2817	.btf_load_err = true,
2818	.err_str = "Invalid arg#2",
2819},
2820
2821{
2822	.descr = "func (Non zero vlen)",
2823	.raw_types = {
2824		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2825		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2826		/* void (*)(int a, unsigned int b) */
2827		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2828			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2829			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2830		/* void func(int a, unsigned int b) */
2831		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 2), 3), 	/* [4] */
2832		BTF_END_RAW,
2833	},
2834	.str_sec = "\0a\0b\0func",
2835	.str_sec_size = sizeof("\0a\0b\0func"),
2836	.map_type = BPF_MAP_TYPE_ARRAY,
2837	.map_name = "func_type_check_btf",
2838	.key_size = sizeof(int),
2839	.value_size = sizeof(int),
2840	.key_type_id = 1,
2841	.value_type_id = 1,
2842	.max_entries = 4,
2843	.btf_load_err = true,
2844	.err_str = "Invalid func linkage",
2845},
2846
2847{
2848	.descr = "func (Not referring to FUNC_PROTO)",
2849	.raw_types = {
2850		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2851		BTF_FUNC_ENC(NAME_TBD, 1),			/* [2] */
2852		BTF_END_RAW,
2853	},
2854	.str_sec = "\0func",
2855	.str_sec_size = sizeof("\0func"),
2856	.map_type = BPF_MAP_TYPE_ARRAY,
2857	.map_name = "func_type_check_btf",
2858	.key_size = sizeof(int),
2859	.value_size = sizeof(int),
2860	.key_type_id = 1,
2861	.value_type_id = 1,
2862	.max_entries = 4,
2863	.btf_load_err = true,
2864	.err_str = "Invalid type_id",
2865},
2866
2867{
2868	.descr = "invalid int kind_flag",
2869	.raw_types = {
2870		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2871		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_INT, 1, 0), 4),	/* [2] */
2872		BTF_INT_ENC(0, 0, 32),
2873		BTF_END_RAW,
2874	},
2875	BTF_STR_SEC(""),
2876	.map_type = BPF_MAP_TYPE_ARRAY,
2877	.map_name = "int_type_check_btf",
2878	.key_size = sizeof(int),
2879	.value_size = sizeof(int),
2880	.key_type_id = 1,
2881	.value_type_id = 1,
2882	.max_entries = 4,
2883	.btf_load_err = true,
2884	.err_str = "Invalid btf_info kind_flag",
2885},
2886
2887{
2888	.descr = "invalid ptr kind_flag",
2889	.raw_types = {
2890		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2891		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 1, 0), 1),	/* [2] */
2892		BTF_END_RAW,
2893	},
2894	BTF_STR_SEC(""),
2895	.map_type = BPF_MAP_TYPE_ARRAY,
2896	.map_name = "ptr_type_check_btf",
2897	.key_size = sizeof(int),
2898	.value_size = sizeof(int),
2899	.key_type_id = 1,
2900	.value_type_id = 1,
2901	.max_entries = 4,
2902	.btf_load_err = true,
2903	.err_str = "Invalid btf_info kind_flag",
2904},
2905
2906{
2907	.descr = "invalid array kind_flag",
2908	.raw_types = {
2909		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2910		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ARRAY, 1, 0), 0),	/* [2] */
2911		BTF_ARRAY_ENC(1, 1, 1),
2912		BTF_END_RAW,
2913	},
2914	BTF_STR_SEC(""),
2915	.map_type = BPF_MAP_TYPE_ARRAY,
2916	.map_name = "array_type_check_btf",
2917	.key_size = sizeof(int),
2918	.value_size = sizeof(int),
2919	.key_type_id = 1,
2920	.value_type_id = 1,
2921	.max_entries = 4,
2922	.btf_load_err = true,
2923	.err_str = "Invalid btf_info kind_flag",
2924},
2925
2926{
2927	.descr = "valid fwd kind_flag",
2928	.raw_types = {
2929		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2930		BTF_TYPE_ENC(NAME_TBD,
2931			     BTF_INFO_ENC(BTF_KIND_FWD, 1, 0), 0),	/* [2] */
2932		BTF_END_RAW,
2933	},
2934	BTF_STR_SEC("\0A"),
2935	.map_type = BPF_MAP_TYPE_ARRAY,
2936	.map_name = "fwd_type_check_btf",
2937	.key_size = sizeof(int),
2938	.value_size = sizeof(int),
2939	.key_type_id = 1,
2940	.value_type_id = 1,
2941	.max_entries = 4,
2942},
2943
2944{
2945	.descr = "invalid typedef kind_flag",
2946	.raw_types = {
2947		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2948		BTF_TYPE_ENC(NAME_TBD,
2949			     BTF_INFO_ENC(BTF_KIND_TYPEDEF, 1, 0), 1),	/* [2] */
2950		BTF_END_RAW,
2951	},
2952	BTF_STR_SEC("\0A"),
2953	.map_type = BPF_MAP_TYPE_ARRAY,
2954	.map_name = "typedef_type_check_btf",
2955	.key_size = sizeof(int),
2956	.value_size = sizeof(int),
2957	.key_type_id = 1,
2958	.value_type_id = 1,
2959	.max_entries = 4,
2960	.btf_load_err = true,
2961	.err_str = "Invalid btf_info kind_flag",
2962},
2963
2964{
2965	.descr = "invalid volatile kind_flag",
2966	.raw_types = {
2967		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
2968		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_VOLATILE, 1, 0), 1),	/* [2] */
2969		BTF_END_RAW,
2970	},
2971	BTF_STR_SEC(""),
2972	.map_type = BPF_MAP_TYPE_ARRAY,
2973	.map_name = "volatile_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 const 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_CONST, 1, 0), 1),	/* [2] */
2988		BTF_END_RAW,
2989	},
2990	BTF_STR_SEC(""),
2991	.map_type = BPF_MAP_TYPE_ARRAY,
2992	.map_name = "const_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 restrict 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_RESTRICT, 1, 0), 1),	/* [2] */
3007		BTF_END_RAW,
3008	},
3009	BTF_STR_SEC(""),
3010	.map_type = BPF_MAP_TYPE_ARRAY,
3011	.map_name = "restrict_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 func 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_FUNC_PROTO, 0, 0), 0),	/* [2] */
3026		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FUNC, 1, 0), 2),	/* [3] */
3027		BTF_END_RAW,
3028	},
3029	BTF_STR_SEC("\0A"),
3030	.map_type = BPF_MAP_TYPE_ARRAY,
3031	.map_name = "func_type_check_btf",
3032	.key_size = sizeof(int),
3033	.value_size = sizeof(int),
3034	.key_type_id = 1,
3035	.value_type_id = 1,
3036	.max_entries = 4,
3037	.btf_load_err = true,
3038	.err_str = "Invalid btf_info kind_flag",
3039},
3040
3041{
3042	.descr = "invalid func_proto kind_flag",
3043	.raw_types = {
3044		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3045		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 1, 0), 0),	/* [2] */
3046		BTF_END_RAW,
3047	},
3048	BTF_STR_SEC(""),
3049	.map_type = BPF_MAP_TYPE_ARRAY,
3050	.map_name = "func_proto_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 = "valid struct, kind_flag, bitfield_size = 0",
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_STRUCT, 1, 2), 8),	/* [2] */
3065		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(0, 0)),
3066		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(0, 32)),
3067		BTF_END_RAW,
3068	},
3069	BTF_STR_SEC("\0A\0B"),
3070	.map_type = BPF_MAP_TYPE_ARRAY,
3071	.map_name = "struct_type_check_btf",
3072	.key_size = sizeof(int),
3073	.value_size = sizeof(int),
3074	.key_type_id = 1,
3075	.value_type_id = 1,
3076	.max_entries = 4,
3077},
3078
3079{
3080	.descr = "valid struct, kind_flag, int member, 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), 4),	/* [2] */
3084		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(4, 0)),
3085		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(4, 4)),
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 union, 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_UNION, 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, 0)),
3105		BTF_END_RAW,
3106	},
3107	BTF_STR_SEC("\0A\0B"),
3108	.map_type = BPF_MAP_TYPE_ARRAY,
3109	.map_name = "union_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 struct, kind_flag, enum 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_ENUM, 0, 1), 4),	/* [2] */
3122		BTF_ENUM_ENC(NAME_TBD, 0),
3123		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 4),/* [3] */
3124		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(4, 0)),
3125		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(4, 4)),
3126		BTF_END_RAW,
3127	},
3128	BTF_STR_SEC("\0A\0B\0C"),
3129	.map_type = BPF_MAP_TYPE_ARRAY,
3130	.map_name = "struct_type_check_btf",
3131	.key_size = sizeof(int),
3132	.value_size = sizeof(int),
3133	.key_type_id = 1,
3134	.value_type_id = 1,
3135	.max_entries = 4,
3136},
3137
3138{
3139	.descr = "valid union, kind_flag, enum member, bitfield_size != 0",
3140	.raw_types = {
3141		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
3142		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),	/* [2] */
3143		BTF_ENUM_ENC(NAME_TBD, 0),
3144		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 1, 2), 4),	/* [3] */
3145		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(4, 0)),
3146		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(4, 0)),
3147		BTF_END_RAW,
3148	},
3149	BTF_STR_SEC("\0A\0B\0C"),
3150	.map_type = BPF_MAP_TYPE_ARRAY,
3151	.map_name = "union_type_check_btf",
3152	.key_size = sizeof(int),
3153	.value_size = sizeof(int),
3154	.key_type_id = 1,
3155	.value_type_id = 1,
3156	.max_entries = 4,
3157},
3158
3159{
3160	.descr = "valid struct, kind_flag, typedef member, bitfield_size != 0",
3161	.raw_types = {
3162		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
3163		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),	/* [2] */
3164		BTF_ENUM_ENC(NAME_TBD, 0),
3165		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 4),/* [3] */
3166		BTF_MEMBER_ENC(NAME_TBD, 4, BTF_MEMBER_OFFSET(4, 0)),
3167		BTF_MEMBER_ENC(NAME_TBD, 5, BTF_MEMBER_OFFSET(4, 4)),
3168		BTF_TYPEDEF_ENC(NAME_TBD, 1),				/* [4] */
3169		BTF_TYPEDEF_ENC(NAME_TBD, 2),				/* [5] */
3170		BTF_END_RAW,
3171	},
3172	BTF_STR_SEC("\0A\0B\0C\0D\0E"),
3173	.map_type = BPF_MAP_TYPE_ARRAY,
3174	.map_name = "struct_type_check_btf",
3175	.key_size = sizeof(int),
3176	.value_size = sizeof(int),
3177	.key_type_id = 1,
3178	.value_type_id = 1,
3179	.max_entries = 4,
3180},
3181
3182{
3183	.descr = "valid union, kind_flag, typedef member, bitfield_size != 0",
3184	.raw_types = {
3185		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
3186		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),	/* [2] */
3187		BTF_ENUM_ENC(NAME_TBD, 0),
3188		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 1, 2), 4),	/* [3] */
3189		BTF_MEMBER_ENC(NAME_TBD, 4, BTF_MEMBER_OFFSET(4, 0)),
3190		BTF_MEMBER_ENC(NAME_TBD, 5, BTF_MEMBER_OFFSET(4, 0)),
3191		BTF_TYPEDEF_ENC(NAME_TBD, 1),				/* [4] */
3192		BTF_TYPEDEF_ENC(NAME_TBD, 2),				/* [5] */
3193		BTF_END_RAW,
3194	},
3195	BTF_STR_SEC("\0A\0B\0C\0D\0E"),
3196	.map_type = BPF_MAP_TYPE_ARRAY,
3197	.map_name = "union_type_check_btf",
3198	.key_size = sizeof(int),
3199	.value_size = sizeof(int),
3200	.key_type_id = 1,
3201	.value_type_id = 1,
3202	.max_entries = 4,
3203},
3204
3205{
3206	.descr = "invalid struct, kind_flag, bitfield_size greater than struct size",
3207	.raw_types = {
3208		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3209		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 4),	/* [2] */
3210		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(20, 0)),
3211		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(20, 20)),
3212		BTF_END_RAW,
3213	},
3214	BTF_STR_SEC("\0A\0B"),
3215	.map_type = BPF_MAP_TYPE_ARRAY,
3216	.map_name = "struct_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	.btf_load_err = true,
3223	.err_str = "Member exceeds struct_size",
3224},
3225
3226{
3227	.descr = "invalid struct, kind_flag, bitfield base_type int not regular",
3228	.raw_types = {
3229		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3230		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 20, 4),			/* [2] */
3231		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 4),	/* [3] */
3232		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(20, 0)),
3233		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(20, 20)),
3234		BTF_END_RAW,
3235	},
3236	BTF_STR_SEC("\0A\0B"),
3237	.map_type = BPF_MAP_TYPE_ARRAY,
3238	.map_name = "struct_type_check_btf",
3239	.key_size = sizeof(int),
3240	.value_size = sizeof(int),
3241	.key_type_id = 1,
3242	.value_type_id = 1,
3243	.max_entries = 4,
3244	.btf_load_err = true,
3245	.err_str = "Invalid member base type",
3246},
3247
3248{
3249	.descr = "invalid struct, kind_flag, base_type int not regular",
3250	.raw_types = {
3251		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3252		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 12, 4),			/* [2] */
3253		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 4),	/* [3] */
3254		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(8, 0)),
3255		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(8, 8)),
3256		BTF_END_RAW,
3257	},
3258	BTF_STR_SEC("\0A\0B"),
3259	.map_type = BPF_MAP_TYPE_ARRAY,
3260	.map_name = "struct_type_check_btf",
3261	.key_size = sizeof(int),
3262	.value_size = sizeof(int),
3263	.key_type_id = 1,
3264	.value_type_id = 1,
3265	.max_entries = 4,
3266	.btf_load_err = true,
3267	.err_str = "Invalid member base type",
3268},
3269
3270{
3271	.descr = "invalid union, kind_flag, bitfield_size greater than struct size",
3272	.raw_types = {
3273		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
3274		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 1, 2), 2),	/* [2] */
3275		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(8, 0)),
3276		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(20, 0)),
3277		BTF_END_RAW,
3278	},
3279	BTF_STR_SEC("\0A\0B"),
3280	.map_type = BPF_MAP_TYPE_ARRAY,
3281	.map_name = "union_type_check_btf",
3282	.key_size = sizeof(int),
3283	.value_size = sizeof(int),
3284	.key_type_id = 1,
3285	.value_type_id = 1,
3286	.max_entries = 4,
3287	.btf_load_err = true,
3288	.err_str = "Member exceeds struct_size",
3289},
3290
3291{
3292	.descr = "invalid struct, kind_flag, int member, bitfield_size = 0, wrong byte alignment",
3293	.raw_types = {
3294		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3295		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [2] */
3296		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 12),	/* [3] */
3297		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 0)),
3298		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 36)),
3299		BTF_END_RAW,
3300	},
3301	BTF_STR_SEC("\0A\0B"),
3302	.map_type = BPF_MAP_TYPE_ARRAY,
3303	.map_name = "struct_type_check_btf",
3304	.key_size = sizeof(int),
3305	.value_size = sizeof(int),
3306	.key_type_id = 1,
3307	.value_type_id = 1,
3308	.max_entries = 4,
3309	.btf_load_err = true,
3310	.err_str = "Invalid member offset",
3311},
3312
3313{
3314	.descr = "invalid struct, kind_flag, enum member, bitfield_size = 0, wrong byte alignment",
3315	.raw_types = {
3316		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3317		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [2] */
3318		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),	/* [2] */
3319		BTF_ENUM_ENC(NAME_TBD, 0),
3320		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 12),	/* [3] */
3321		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 0)),
3322		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 36)),
3323		BTF_END_RAW,
3324	},
3325	BTF_STR_SEC("\0A\0B\0C"),
3326	.map_type = BPF_MAP_TYPE_ARRAY,
3327	.map_name = "struct_type_check_btf",
3328	.key_size = sizeof(int),
3329	.value_size = sizeof(int),
3330	.key_type_id = 1,
3331	.value_type_id = 1,
3332	.max_entries = 4,
3333	.btf_load_err = true,
3334	.err_str = "Invalid member offset",
3335},
3336
3337{
3338	.descr = "128-bit int",
3339	.raw_types = {
3340		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3341		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 128, 16),		/* [2] */
3342		BTF_END_RAW,
3343	},
3344	BTF_STR_SEC("\0A"),
3345	.map_type = BPF_MAP_TYPE_ARRAY,
3346	.map_name = "int_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},
3353
3354{
3355	.descr = "struct, 128-bit int member",
3356	.raw_types = {
3357		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3358		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 128, 16),		/* [2] */
3359		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 16),	/* [3] */
3360		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3361		BTF_END_RAW,
3362	},
3363	BTF_STR_SEC("\0A"),
3364	.map_type = BPF_MAP_TYPE_ARRAY,
3365	.map_name = "struct_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, 120-bit int member bitfield",
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, 120, 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, kind_flag, 128-bit int member",
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, 128, 16),		/* [2] */
3397		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 1), 16),	/* [3] */
3398		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 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, 120-bit int member bitfield",
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(120, 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 * typedef int arr_t[16];
3431 * struct s {
3432 *	arr_t *a;
3433 * };
3434 */
3435{
3436	.descr = "struct->ptr->typedef->array->int size resolution",
3437	.raw_types = {
3438		BTF_STRUCT_ENC(NAME_TBD, 1, 8),			/* [1] */
3439		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3440		BTF_PTR_ENC(3),					/* [2] */
3441		BTF_TYPEDEF_ENC(NAME_TBD, 4),			/* [3] */
3442		BTF_TYPE_ARRAY_ENC(5, 5, 16),			/* [4] */
3443		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [5] */
3444		BTF_END_RAW,
3445	},
3446	BTF_STR_SEC("\0s\0a\0arr_t"),
3447	.map_type = BPF_MAP_TYPE_ARRAY,
3448	.map_name = "ptr_mod_chain_size_resolve_map",
3449	.key_size = sizeof(int),
3450	.value_size = sizeof(int) * 16,
3451	.key_type_id = 5 /* int */,
3452	.value_type_id = 3 /* arr_t */,
3453	.max_entries = 4,
3454},
3455/*
3456 * typedef int arr_t[16][8][4];
3457 * struct s {
3458 *	arr_t *a;
3459 * };
3460 */
3461{
3462	.descr = "struct->ptr->typedef->multi-array->int size resolution",
3463	.raw_types = {
3464		BTF_STRUCT_ENC(NAME_TBD, 1, 8),			/* [1] */
3465		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3466		BTF_PTR_ENC(3),					/* [2] */
3467		BTF_TYPEDEF_ENC(NAME_TBD, 4),			/* [3] */
3468		BTF_TYPE_ARRAY_ENC(5, 7, 16),			/* [4] */
3469		BTF_TYPE_ARRAY_ENC(6, 7, 8),			/* [5] */
3470		BTF_TYPE_ARRAY_ENC(7, 7, 4),			/* [6] */
3471		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [7] */
3472		BTF_END_RAW,
3473	},
3474	BTF_STR_SEC("\0s\0a\0arr_t"),
3475	.map_type = BPF_MAP_TYPE_ARRAY,
3476	.map_name = "multi_arr_size_resolve_map",
3477	.key_size = sizeof(int),
3478	.value_size = sizeof(int) * 16 * 8 * 4,
3479	.key_type_id = 7 /* int */,
3480	.value_type_id = 3 /* arr_t */,
3481	.max_entries = 4,
3482},
3483/*
3484 * typedef int int_t;
3485 * typedef int_t arr3_t[4];
3486 * typedef arr3_t arr2_t[8];
3487 * typedef arr2_t arr1_t[16];
3488 * struct s {
3489 *	arr1_t *a;
3490 * };
3491 */
3492{
3493	.descr = "typedef/multi-arr mix size resolution",
3494	.raw_types = {
3495		BTF_STRUCT_ENC(NAME_TBD, 1, 8),			/* [1] */
3496		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3497		BTF_PTR_ENC(3),					/* [2] */
3498		BTF_TYPEDEF_ENC(NAME_TBD, 4),			/* [3] */
3499		BTF_TYPE_ARRAY_ENC(5, 10, 16),			/* [4] */
3500		BTF_TYPEDEF_ENC(NAME_TBD, 6),			/* [5] */
3501		BTF_TYPE_ARRAY_ENC(7, 10, 8),			/* [6] */
3502		BTF_TYPEDEF_ENC(NAME_TBD, 8),			/* [7] */
3503		BTF_TYPE_ARRAY_ENC(9, 10, 4),			/* [8] */
3504		BTF_TYPEDEF_ENC(NAME_TBD, 10),			/* [9] */
3505		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [10] */
3506		BTF_END_RAW,
3507	},
3508	BTF_STR_SEC("\0s\0a\0arr1_t\0arr2_t\0arr3_t\0int_t"),
3509	.map_type = BPF_MAP_TYPE_ARRAY,
3510	.map_name = "typedef_arra_mix_size_resolve_map",
3511	.key_size = sizeof(int),
3512	.value_size = sizeof(int) * 16 * 8 * 4,
3513	.key_type_id = 10 /* int */,
3514	.value_type_id = 3 /* arr_t */,
3515	.max_entries = 4,
3516},
3517/*
3518 * elf .rodata section size 4 and btf .rodata section vlen 0.
3519 */
3520{
3521	.descr = "datasec: vlen == 0",
3522	.raw_types = {
3523		/* int */
3524		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3525		/* .rodata section */
3526		BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 0), 4),
3527								 /* [2] */
3528		BTF_END_RAW,
3529	},
3530	BTF_STR_SEC("\0.rodata"),
3531	.map_type = BPF_MAP_TYPE_ARRAY,
3532	.key_size = sizeof(int),
3533	.value_size = sizeof(int),
3534	.key_type_id = 1,
3535	.value_type_id = 1,
3536	.max_entries = 1,
3537},
3538{
3539	.descr = "datasec: name '?.foo bar:buz' is ok",
3540	.raw_types = {
3541		/* int */
3542		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
3543		/* VAR x */                                     /* [2] */
3544		BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
3545		BTF_VAR_STATIC,
3546		/* DATASEC ?.data */                            /* [3] */
3547		BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
3548		BTF_VAR_SECINFO_ENC(2, 0, 4),
3549		BTF_END_RAW,
3550	},
3551	BTF_STR_SEC("\0x\0?.foo bar:buz"),
3552},
3553{
3554	.descr = "datasec: name with non-printable first char not is ok",
3555	.raw_types = {
3556		/* int */
3557		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
3558		/* VAR x */                                     /* [2] */
3559		BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
3560		BTF_VAR_STATIC,
3561		/* DATASEC ?.data */                            /* [3] */
3562		BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
3563		BTF_VAR_SECINFO_ENC(2, 0, 4),
3564		BTF_END_RAW,
3565	},
3566	BTF_STR_SEC("\0x\0\7foo"),
3567	.err_str = "Invalid name",
3568	.btf_load_err = true,
3569},
3570{
3571	.descr = "datasec: name '\\0' is not ok",
3572	.raw_types = {
3573		/* int */
3574		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
3575		/* VAR x */                                     /* [2] */
3576		BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
3577		BTF_VAR_STATIC,
3578		/* DATASEC \0 */                                /* [3] */
3579		BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
3580		BTF_VAR_SECINFO_ENC(2, 0, 4),
3581		BTF_END_RAW,
3582	},
3583	BTF_STR_SEC("\0x\0"),
3584	.err_str = "Invalid name",
3585	.btf_load_err = true,
3586},
3587{
3588	.descr = "type name '?foo' is not ok",
3589	.raw_types = {
3590		/* union ?foo; */
3591		BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_FWD, 1, 0), 0), /* [1] */
3592		BTF_END_RAW,
3593	},
3594	BTF_STR_SEC("\0?foo"),
3595	.err_str = "Invalid name",
3596	.btf_load_err = true,
3597},
3598
3599{
3600	.descr = "float test #1, well-formed",
3601	.raw_types = {
3602		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
3603								/* [1] */
3604		BTF_TYPE_FLOAT_ENC(NAME_TBD, 2),		/* [2] */
3605		BTF_TYPE_FLOAT_ENC(NAME_TBD, 4),		/* [3] */
3606		BTF_TYPE_FLOAT_ENC(NAME_TBD, 8),		/* [4] */
3607		BTF_TYPE_FLOAT_ENC(NAME_TBD, 12),		/* [5] */
3608		BTF_TYPE_FLOAT_ENC(NAME_TBD, 16),		/* [6] */
3609		BTF_STRUCT_ENC(NAME_TBD, 5, 48),		/* [7] */
3610		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3611		BTF_MEMBER_ENC(NAME_TBD, 3, 32),
3612		BTF_MEMBER_ENC(NAME_TBD, 4, 64),
3613		BTF_MEMBER_ENC(NAME_TBD, 5, 128),
3614		BTF_MEMBER_ENC(NAME_TBD, 6, 256),
3615		BTF_END_RAW,
3616	},
3617	BTF_STR_SEC("\0int\0_Float16\0float\0double\0_Float80\0long_double"
3618		    "\0floats\0a\0b\0c\0d\0e"),
3619	.map_type = BPF_MAP_TYPE_ARRAY,
3620	.map_name = "float_type_check_btf",
3621	.key_size = sizeof(int),
3622	.value_size = 48,
3623	.key_type_id = 1,
3624	.value_type_id = 7,
3625	.max_entries = 1,
3626},
3627{
3628	.descr = "float test #2, invalid vlen",
3629	.raw_types = {
3630		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
3631								/* [1] */
3632		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 1), 4),
3633								/* [2] */
3634		BTF_END_RAW,
3635	},
3636	BTF_STR_SEC("\0int\0float"),
3637	.map_type = BPF_MAP_TYPE_ARRAY,
3638	.map_name = "float_type_check_btf",
3639	.key_size = sizeof(int),
3640	.value_size = 4,
3641	.key_type_id = 1,
3642	.value_type_id = 2,
3643	.max_entries = 1,
3644	.btf_load_err = true,
3645	.err_str = "vlen != 0",
3646},
3647{
3648	.descr = "float test #3, invalid kind_flag",
3649	.raw_types = {
3650		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
3651								/* [1] */
3652		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FLOAT, 1, 0), 4),
3653								/* [2] */
3654		BTF_END_RAW,
3655	},
3656	BTF_STR_SEC("\0int\0float"),
3657	.map_type = BPF_MAP_TYPE_ARRAY,
3658	.map_name = "float_type_check_btf",
3659	.key_size = sizeof(int),
3660	.value_size = 4,
3661	.key_type_id = 1,
3662	.value_type_id = 2,
3663	.max_entries = 1,
3664	.btf_load_err = true,
3665	.err_str = "Invalid btf_info kind_flag",
3666},
3667{
3668	.descr = "float test #4, member does not fit",
3669	.raw_types = {
3670		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
3671								/* [1] */
3672		BTF_TYPE_FLOAT_ENC(NAME_TBD, 4),		/* [2] */
3673		BTF_STRUCT_ENC(NAME_TBD, 1, 2),			/* [3] */
3674		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3675		BTF_END_RAW,
3676	},
3677	BTF_STR_SEC("\0int\0float\0floats\0x"),
3678	.map_type = BPF_MAP_TYPE_ARRAY,
3679	.map_name = "float_type_check_btf",
3680	.key_size = sizeof(int),
3681	.value_size = 4,
3682	.key_type_id = 1,
3683	.value_type_id = 3,
3684	.max_entries = 1,
3685	.btf_load_err = true,
3686	.err_str = "Member exceeds struct_size",
3687},
3688{
3689	.descr = "float test #5, member is not properly aligned",
3690	.raw_types = {
3691		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
3692								/* [1] */
3693		BTF_TYPE_FLOAT_ENC(NAME_TBD, 4),		/* [2] */
3694		BTF_STRUCT_ENC(NAME_TBD, 1, 8),			/* [3] */
3695		BTF_MEMBER_ENC(NAME_TBD, 2, 8),
3696		BTF_END_RAW,
3697	},
3698	BTF_STR_SEC("\0int\0float\0floats\0x"),
3699	.map_type = BPF_MAP_TYPE_ARRAY,
3700	.map_name = "float_type_check_btf",
3701	.key_size = sizeof(int),
3702	.value_size = 4,
3703	.key_type_id = 1,
3704	.value_type_id = 3,
3705	.max_entries = 1,
3706	.btf_load_err = true,
3707	.err_str = "Member is not properly aligned",
3708},
3709{
3710	.descr = "float test #6, invalid size",
3711	.raw_types = {
3712		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
3713								/* [1] */
3714		BTF_TYPE_FLOAT_ENC(NAME_TBD, 6),		/* [2] */
3715		BTF_END_RAW,
3716	},
3717	BTF_STR_SEC("\0int\0float"),
3718	.map_type = BPF_MAP_TYPE_ARRAY,
3719	.map_name = "float_type_check_btf",
3720	.key_size = sizeof(int),
3721	.value_size = 6,
3722	.key_type_id = 1,
3723	.value_type_id = 2,
3724	.max_entries = 1,
3725	.btf_load_err = true,
3726	.err_str = "Invalid type_size",
3727},
3728
3729{
3730	.descr = "decl_tag test #1, struct/member, well-formed",
3731	.raw_types = {
3732		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3733		BTF_STRUCT_ENC(0, 2, 8),			/* [2] */
3734		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
3735		BTF_MEMBER_ENC(NAME_TBD, 1, 32),
3736		BTF_DECL_TAG_ENC(NAME_TBD, 2, -1),
3737		BTF_DECL_TAG_ENC(NAME_TBD, 2, 0),
3738		BTF_DECL_TAG_ENC(NAME_TBD, 2, 1),
3739		BTF_END_RAW,
3740	},
3741	BTF_STR_SEC("\0m1\0m2\0tag1\0tag2\0tag3"),
3742	.map_type = BPF_MAP_TYPE_ARRAY,
3743	.map_name = "tag_type_check_btf",
3744	.key_size = sizeof(int),
3745	.value_size = 8,
3746	.key_type_id = 1,
3747	.value_type_id = 2,
3748	.max_entries = 1,
3749},
3750{
3751	.descr = "decl_tag test #2, union/member, well-formed",
3752	.raw_types = {
3753		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3754		BTF_UNION_ENC(NAME_TBD, 2, 4),			/* [2] */
3755		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
3756		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
3757		BTF_DECL_TAG_ENC(NAME_TBD, 2, -1),
3758		BTF_DECL_TAG_ENC(NAME_TBD, 2, 0),
3759		BTF_DECL_TAG_ENC(NAME_TBD, 2, 1),
3760		BTF_END_RAW,
3761	},
3762	BTF_STR_SEC("\0t\0m1\0m2\0tag1\0tag2\0tag3"),
3763	.map_type = BPF_MAP_TYPE_ARRAY,
3764	.map_name = "tag_type_check_btf",
3765	.key_size = sizeof(int),
3766	.value_size = 4,
3767	.key_type_id = 1,
3768	.value_type_id = 2,
3769	.max_entries = 1,
3770},
3771{
3772	.descr = "decl_tag test #3, variable, well-formed",
3773	.raw_types = {
3774		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3775		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
3776		BTF_VAR_ENC(NAME_TBD, 1, 1),			/* [3] */
3777		BTF_DECL_TAG_ENC(NAME_TBD, 2, -1),
3778		BTF_DECL_TAG_ENC(NAME_TBD, 3, -1),
3779		BTF_END_RAW,
3780	},
3781	BTF_STR_SEC("\0local\0global\0tag1\0tag2"),
3782	.map_type = BPF_MAP_TYPE_ARRAY,
3783	.map_name = "tag_type_check_btf",
3784	.key_size = sizeof(int),
3785	.value_size = 4,
3786	.key_type_id = 1,
3787	.value_type_id = 1,
3788	.max_entries = 1,
3789},
3790{
3791	.descr = "decl_tag test #4, func/parameter, well-formed",
3792	.raw_types = {
3793		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3794		BTF_FUNC_PROTO_ENC(0, 2),			/* [2] */
3795			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3796			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3797		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
3798		BTF_DECL_TAG_ENC(NAME_TBD, 3, -1),
3799		BTF_DECL_TAG_ENC(NAME_TBD, 3, 0),
3800		BTF_DECL_TAG_ENC(NAME_TBD, 3, 1),
3801		BTF_END_RAW,
3802	},
3803	BTF_STR_SEC("\0arg1\0arg2\0f\0tag1\0tag2\0tag3"),
3804	.map_type = BPF_MAP_TYPE_ARRAY,
3805	.map_name = "tag_type_check_btf",
3806	.key_size = sizeof(int),
3807	.value_size = 4,
3808	.key_type_id = 1,
3809	.value_type_id = 1,
3810	.max_entries = 1,
3811},
3812{
3813	.descr = "decl_tag test #5, invalid value",
3814	.raw_types = {
3815		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3816		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
3817		BTF_DECL_TAG_ENC(0, 2, -1),
3818		BTF_END_RAW,
3819	},
3820	BTF_STR_SEC("\0local\0tag"),
3821	.map_type = BPF_MAP_TYPE_ARRAY,
3822	.map_name = "tag_type_check_btf",
3823	.key_size = sizeof(int),
3824	.value_size = 4,
3825	.key_type_id = 1,
3826	.value_type_id = 1,
3827	.max_entries = 1,
3828	.btf_load_err = true,
3829	.err_str = "Invalid value",
3830},
3831{
3832	.descr = "decl_tag test #6, invalid target type",
3833	.raw_types = {
3834		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3835		BTF_DECL_TAG_ENC(NAME_TBD, 1, -1),
3836		BTF_END_RAW,
3837	},
3838	BTF_STR_SEC("\0tag1"),
3839	.map_type = BPF_MAP_TYPE_ARRAY,
3840	.map_name = "tag_type_check_btf",
3841	.key_size = sizeof(int),
3842	.value_size = 4,
3843	.key_type_id = 1,
3844	.value_type_id = 1,
3845	.max_entries = 1,
3846	.btf_load_err = true,
3847	.err_str = "Invalid type",
3848},
3849{
3850	.descr = "decl_tag test #7, invalid vlen",
3851	.raw_types = {
3852		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3853		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
3854		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DECL_TAG, 0, 1), 2), (0),
3855		BTF_END_RAW,
3856	},
3857	BTF_STR_SEC("\0local\0tag1"),
3858	.map_type = BPF_MAP_TYPE_ARRAY,
3859	.map_name = "tag_type_check_btf",
3860	.key_size = sizeof(int),
3861	.value_size = 4,
3862	.key_type_id = 1,
3863	.value_type_id = 1,
3864	.max_entries = 1,
3865	.btf_load_err = true,
3866	.err_str = "vlen != 0",
3867},
3868{
3869	.descr = "decl_tag test #8, invalid kflag",
3870	.raw_types = {
3871		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3872		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
3873		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DECL_TAG, 1, 0), 2), (-1),
3874		BTF_END_RAW,
3875	},
3876	BTF_STR_SEC("\0local\0tag1"),
3877	.map_type = BPF_MAP_TYPE_ARRAY,
3878	.map_name = "tag_type_check_btf",
3879	.key_size = sizeof(int),
3880	.value_size = 4,
3881	.key_type_id = 1,
3882	.value_type_id = 1,
3883	.max_entries = 1,
3884	.btf_load_err = true,
3885	.err_str = "Invalid btf_info kind_flag",
3886},
3887{
3888	.descr = "decl_tag test #9, var, invalid component_idx",
3889	.raw_types = {
3890		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3891		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
3892		BTF_DECL_TAG_ENC(NAME_TBD, 2, 0),
3893		BTF_END_RAW,
3894	},
3895	BTF_STR_SEC("\0local\0tag"),
3896	.map_type = BPF_MAP_TYPE_ARRAY,
3897	.map_name = "tag_type_check_btf",
3898	.key_size = sizeof(int),
3899	.value_size = 4,
3900	.key_type_id = 1,
3901	.value_type_id = 1,
3902	.max_entries = 1,
3903	.btf_load_err = true,
3904	.err_str = "Invalid component_idx",
3905},
3906{
3907	.descr = "decl_tag test #10, struct member, invalid component_idx",
3908	.raw_types = {
3909		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3910		BTF_STRUCT_ENC(0, 2, 8),			/* [2] */
3911		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
3912		BTF_MEMBER_ENC(NAME_TBD, 1, 32),
3913		BTF_DECL_TAG_ENC(NAME_TBD, 2, 2),
3914		BTF_END_RAW,
3915	},
3916	BTF_STR_SEC("\0m1\0m2\0tag"),
3917	.map_type = BPF_MAP_TYPE_ARRAY,
3918	.map_name = "tag_type_check_btf",
3919	.key_size = sizeof(int),
3920	.value_size = 8,
3921	.key_type_id = 1,
3922	.value_type_id = 2,
3923	.max_entries = 1,
3924	.btf_load_err = true,
3925	.err_str = "Invalid component_idx",
3926},
3927{
3928	.descr = "decl_tag test #11, func parameter, invalid component_idx",
3929	.raw_types = {
3930		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3931		BTF_FUNC_PROTO_ENC(0, 2),			/* [2] */
3932			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3933			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3934		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
3935		BTF_DECL_TAG_ENC(NAME_TBD, 3, 2),
3936		BTF_END_RAW,
3937	},
3938	BTF_STR_SEC("\0arg1\0arg2\0f\0tag"),
3939	.map_type = BPF_MAP_TYPE_ARRAY,
3940	.map_name = "tag_type_check_btf",
3941	.key_size = sizeof(int),
3942	.value_size = 4,
3943	.key_type_id = 1,
3944	.value_type_id = 1,
3945	.max_entries = 1,
3946	.btf_load_err = true,
3947	.err_str = "Invalid component_idx",
3948},
3949{
3950	.descr = "decl_tag test #12, < -1 component_idx",
3951	.raw_types = {
3952		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3953		BTF_FUNC_PROTO_ENC(0, 2),			/* [2] */
3954			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3955			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3956		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
3957		BTF_DECL_TAG_ENC(NAME_TBD, 3, -2),
3958		BTF_END_RAW,
3959	},
3960	BTF_STR_SEC("\0arg1\0arg2\0f\0tag"),
3961	.map_type = BPF_MAP_TYPE_ARRAY,
3962	.map_name = "tag_type_check_btf",
3963	.key_size = sizeof(int),
3964	.value_size = 4,
3965	.key_type_id = 1,
3966	.value_type_id = 1,
3967	.max_entries = 1,
3968	.btf_load_err = true,
3969	.err_str = "Invalid component_idx",
3970},
3971{
3972	.descr = "decl_tag test #13, typedef, well-formed",
3973	.raw_types = {
3974		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3975		BTF_TYPEDEF_ENC(NAME_TBD, 1),			/* [2] */
3976		BTF_DECL_TAG_ENC(NAME_TBD, 2, -1),
3977		BTF_END_RAW,
3978	},
3979	BTF_STR_SEC("\0t\0tag"),
3980	.map_type = BPF_MAP_TYPE_ARRAY,
3981	.map_name = "tag_type_check_btf",
3982	.key_size = sizeof(int),
3983	.value_size = 4,
3984	.key_type_id = 1,
3985	.value_type_id = 1,
3986	.max_entries = 1,
3987},
3988{
3989	.descr = "decl_tag test #14, typedef, invalid component_idx",
3990	.raw_types = {
3991		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3992		BTF_TYPEDEF_ENC(NAME_TBD, 1),			/* [2] */
3993		BTF_DECL_TAG_ENC(NAME_TBD, 2, 0),
3994		BTF_END_RAW,
3995	},
3996	BTF_STR_SEC("\0local\0tag"),
3997	.map_type = BPF_MAP_TYPE_ARRAY,
3998	.map_name = "tag_type_check_btf",
3999	.key_size = sizeof(int),
4000	.value_size = 4,
4001	.key_type_id = 1,
4002	.value_type_id = 1,
4003	.max_entries = 1,
4004	.btf_load_err = true,
4005	.err_str = "Invalid component_idx",
4006},
4007{
4008	.descr = "decl_tag test #15, func, invalid func proto",
4009	.raw_types = {
4010		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
4011		BTF_DECL_TAG_ENC(NAME_TBD, 3, 0),		/* [2] */
4012		BTF_FUNC_ENC(NAME_TBD, 8),			/* [3] */
4013		BTF_END_RAW,
4014	},
4015	BTF_STR_SEC("\0tag\0func"),
4016	.map_type = BPF_MAP_TYPE_ARRAY,
4017	.map_name = "tag_type_check_btf",
4018	.key_size = sizeof(int),
4019	.value_size = 4,
4020	.key_type_id = 1,
4021	.value_type_id = 1,
4022	.max_entries = 1,
4023	.btf_load_err = true,
4024	.err_str = "Invalid type_id",
4025},
4026{
4027	.descr = "decl_tag test #16, func proto, return type",
4028	.raw_types = {
4029		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),				/* [1] */
4030		BTF_VAR_ENC(NAME_TBD, 1, 0),						/* [2] */
4031		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DECL_TAG, 0, 0), 2), (-1),	/* [3] */
4032		BTF_FUNC_PROTO_ENC(3, 0),						/* [4] */
4033		BTF_END_RAW,
4034	},
4035	BTF_STR_SEC("\0local\0tag1"),
4036	.btf_load_err = true,
4037	.err_str = "Invalid return type",
4038},
4039{
4040	.descr = "decl_tag test #17, func proto, argument",
4041	.raw_types = {
4042		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DECL_TAG, 0, 0), 4), (-1),	/* [1] */
4043		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0), /* [2] */
4044		BTF_FUNC_PROTO_ENC(0, 1),			/* [3] */
4045			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
4046		BTF_VAR_ENC(NAME_TBD, 2, 0),			/* [4] */
4047		BTF_END_RAW,
4048	},
4049	BTF_STR_SEC("\0local\0tag1\0var"),
4050	.btf_load_err = true,
4051	.err_str = "Invalid arg#1",
4052},
4053{
4054	.descr = "decl_tag test #18, decl_tag as the map key type",
4055	.raw_types = {
4056		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
4057		BTF_STRUCT_ENC(0, 2, 8),			/* [2] */
4058		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
4059		BTF_MEMBER_ENC(NAME_TBD, 1, 32),
4060		BTF_DECL_TAG_ENC(NAME_TBD, 2, -1),		/* [3] */
4061		BTF_END_RAW,
4062	},
4063	BTF_STR_SEC("\0m1\0m2\0tag"),
4064	.map_type = BPF_MAP_TYPE_HASH,
4065	.map_name = "tag_type_check_btf",
4066	.key_size = 8,
4067	.value_size = 4,
4068	.key_type_id = 3,
4069	.value_type_id = 1,
4070	.max_entries = 1,
4071	.map_create_err = true,
4072},
4073{
4074	.descr = "decl_tag test #19, decl_tag as the map value type",
4075	.raw_types = {
4076		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
4077		BTF_STRUCT_ENC(0, 2, 8),			/* [2] */
4078		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
4079		BTF_MEMBER_ENC(NAME_TBD, 1, 32),
4080		BTF_DECL_TAG_ENC(NAME_TBD, 2, -1),		/* [3] */
4081		BTF_END_RAW,
4082	},
4083	BTF_STR_SEC("\0m1\0m2\0tag"),
4084	.map_type = BPF_MAP_TYPE_HASH,
4085	.map_name = "tag_type_check_btf",
4086	.key_size = 4,
4087	.value_size = 8,
4088	.key_type_id = 1,
4089	.value_type_id = 3,
4090	.max_entries = 1,
4091	.map_create_err = true,
4092},
4093{
4094	.descr = "type_tag test #1",
4095	.raw_types = {
4096		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
4097		BTF_TYPE_TAG_ENC(NAME_TBD, 1),			/* [2] */
4098		BTF_PTR_ENC(2),					/* [3] */
4099		BTF_END_RAW,
4100	},
4101	BTF_STR_SEC("\0tag"),
4102	.map_type = BPF_MAP_TYPE_ARRAY,
4103	.map_name = "tag_type_check_btf",
4104	.key_size = sizeof(int),
4105	.value_size = 4,
4106	.key_type_id = 1,
4107	.value_type_id = 1,
4108	.max_entries = 1,
4109},
4110{
4111	.descr = "type_tag test #2, type tag order",
4112	.raw_types = {
4113		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
4114		BTF_CONST_ENC(3),				/* [2] */
4115		BTF_TYPE_TAG_ENC(NAME_TBD, 1),			/* [3] */
4116		BTF_END_RAW,
4117	},
4118	BTF_STR_SEC("\0tag"),
4119	.map_type = BPF_MAP_TYPE_ARRAY,
4120	.map_name = "tag_type_check_btf",
4121	.key_size = sizeof(int),
4122	.value_size = 4,
4123	.key_type_id = 1,
4124	.value_type_id = 1,
4125	.max_entries = 1,
4126	.btf_load_err = true,
4127	.err_str = "Type tags don't precede modifiers",
4128},
4129{
4130	.descr = "type_tag test #3, type tag order",
4131	.raw_types = {
4132		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
4133		BTF_TYPE_TAG_ENC(NAME_TBD, 3),			/* [2] */
4134		BTF_CONST_ENC(4),				/* [3] */
4135		BTF_TYPE_TAG_ENC(NAME_TBD, 1),			/* [4] */
4136		BTF_END_RAW,
4137	},
4138	BTF_STR_SEC("\0tag\0tag"),
4139	.map_type = BPF_MAP_TYPE_ARRAY,
4140	.map_name = "tag_type_check_btf",
4141	.key_size = sizeof(int),
4142	.value_size = 4,
4143	.key_type_id = 1,
4144	.value_type_id = 1,
4145	.max_entries = 1,
4146	.btf_load_err = true,
4147	.err_str = "Type tags don't precede modifiers",
4148},
4149{
4150	.descr = "type_tag test #4, type tag order",
4151	.raw_types = {
4152		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
4153		BTF_TYPEDEF_ENC(NAME_TBD, 3),			/* [2] */
4154		BTF_CONST_ENC(4),				/* [3] */
4155		BTF_TYPE_TAG_ENC(NAME_TBD, 1),			/* [4] */
4156		BTF_END_RAW,
4157	},
4158	BTF_STR_SEC("\0tag\0tag"),
4159	.map_type = BPF_MAP_TYPE_ARRAY,
4160	.map_name = "tag_type_check_btf",
4161	.key_size = sizeof(int),
4162	.value_size = 4,
4163	.key_type_id = 1,
4164	.value_type_id = 1,
4165	.max_entries = 1,
4166	.btf_load_err = true,
4167	.err_str = "Type tags don't precede modifiers",
4168},
4169{
4170	.descr = "type_tag test #5, type tag order",
4171	.raw_types = {
4172		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
4173		BTF_TYPE_TAG_ENC(NAME_TBD, 3),			/* [2] */
4174		BTF_CONST_ENC(1),				/* [3] */
4175		BTF_TYPE_TAG_ENC(NAME_TBD, 2),			/* [4] */
4176		BTF_END_RAW,
4177	},
4178	BTF_STR_SEC("\0tag\0tag"),
4179	.map_type = BPF_MAP_TYPE_ARRAY,
4180	.map_name = "tag_type_check_btf",
4181	.key_size = sizeof(int),
4182	.value_size = 4,
4183	.key_type_id = 1,
4184	.value_type_id = 1,
4185	.max_entries = 1,
4186},
4187{
4188	.descr = "type_tag test #6, type tag order",
4189	.raw_types = {
4190		BTF_PTR_ENC(2),					/* [1] */
4191		BTF_TYPE_TAG_ENC(NAME_TBD, 3),			/* [2] */
4192		BTF_CONST_ENC(4),				/* [3] */
4193		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [4] */
4194		BTF_PTR_ENC(6),					/* [5] */
4195		BTF_CONST_ENC(2),				/* [6] */
4196		BTF_END_RAW,
4197	},
4198	BTF_STR_SEC("\0tag"),
4199	.map_type = BPF_MAP_TYPE_ARRAY,
4200	.map_name = "tag_type_check_btf",
4201	.key_size = sizeof(int),
4202	.value_size = 4,
4203	.key_type_id = 1,
4204	.value_type_id = 1,
4205	.max_entries = 1,
4206	.btf_load_err = true,
4207	.err_str = "Type tags don't precede modifiers",
4208},
4209{
4210	.descr = "enum64 test #1, unsigned, size 8",
4211	.raw_types = {
4212		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
4213		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 2), 8),	/* [2] */
4214		BTF_ENUM64_ENC(NAME_TBD, 0, 0),
4215		BTF_ENUM64_ENC(NAME_TBD, 1, 1),
4216		BTF_END_RAW,
4217	},
4218	BTF_STR_SEC("\0a\0b\0c"),
4219	.map_type = BPF_MAP_TYPE_ARRAY,
4220	.map_name = "tag_type_check_btf",
4221	.key_size = sizeof(int),
4222	.value_size = 8,
4223	.key_type_id = 1,
4224	.value_type_id = 2,
4225	.max_entries = 1,
4226},
4227{
4228	.descr = "enum64 test #2, signed, size 4",
4229	.raw_types = {
4230		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
4231		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM64, 1, 2), 4),	/* [2] */
4232		BTF_ENUM64_ENC(NAME_TBD, -1, 0),
4233		BTF_ENUM64_ENC(NAME_TBD, 1, 0),
4234		BTF_END_RAW,
4235	},
4236	BTF_STR_SEC("\0a\0b\0c"),
4237	.map_type = BPF_MAP_TYPE_ARRAY,
4238	.map_name = "tag_type_check_btf",
4239	.key_size = sizeof(int),
4240	.value_size = 4,
4241	.key_type_id = 1,
4242	.value_type_id = 2,
4243	.max_entries = 1,
4244},
4245
4246}; /* struct btf_raw_test raw_tests[] */
4247
4248static const char *get_next_str(const char *start, const char *end)
4249{
4250	return start < end - 1 ? start + 1 : NULL;
4251}
4252
4253static int get_raw_sec_size(const __u32 *raw_types)
4254{
4255	int i;
4256
4257	for (i = MAX_NR_RAW_U32 - 1;
4258	     i >= 0 && raw_types[i] != BTF_END_RAW;
4259	     i--)
4260		;
4261
4262	return i < 0 ? i : i * sizeof(raw_types[0]);
4263}
4264
4265static void *btf_raw_create(const struct btf_header *hdr,
4266			    const __u32 *raw_types,
4267			    const char *str,
4268			    unsigned int str_sec_size,
4269			    unsigned int *btf_size,
4270			    const char **ret_next_str)
4271{
4272	const char *next_str = str, *end_str = str + str_sec_size;
4273	const char **strs_idx = NULL, **tmp_strs_idx;
4274	int strs_cap = 0, strs_cnt = 0, next_str_idx = 0;
4275	unsigned int size_needed, offset;
4276	struct btf_header *ret_hdr;
4277	int i, type_sec_size, err = 0;
4278	uint32_t *ret_types;
4279	void *raw_btf = NULL;
4280
4281	type_sec_size = get_raw_sec_size(raw_types);
4282	if (CHECK(type_sec_size < 0, "Cannot get nr_raw_types"))
4283		return NULL;
4284
4285	size_needed = sizeof(*hdr) + type_sec_size + str_sec_size;
4286	raw_btf = malloc(size_needed);
4287	if (CHECK(!raw_btf, "Cannot allocate memory for raw_btf"))
4288		return NULL;
4289
4290	/* Copy header */
4291	memcpy(raw_btf, hdr, sizeof(*hdr));
4292	offset = sizeof(*hdr);
4293
4294	/* Index strings */
4295	while ((next_str = get_next_str(next_str, end_str))) {
4296		if (strs_cnt == strs_cap) {
4297			strs_cap += max(16, strs_cap / 2);
4298			tmp_strs_idx = realloc(strs_idx,
4299					       sizeof(*strs_idx) * strs_cap);
4300			if (CHECK(!tmp_strs_idx,
4301				  "Cannot allocate memory for strs_idx")) {
4302				err = -1;
4303				goto done;
4304			}
4305			strs_idx = tmp_strs_idx;
4306		}
4307		strs_idx[strs_cnt++] = next_str;
4308		next_str += strlen(next_str);
4309	}
4310
4311	/* Copy type section */
4312	ret_types = raw_btf + offset;
4313	for (i = 0; i < type_sec_size / sizeof(raw_types[0]); i++) {
4314		if (raw_types[i] == NAME_TBD) {
4315			if (CHECK(next_str_idx == strs_cnt,
4316				  "Error in getting next_str #%d",
4317				  next_str_idx)) {
4318				err = -1;
4319				goto done;
4320			}
4321			ret_types[i] = strs_idx[next_str_idx++] - str;
4322		} else if (IS_NAME_NTH(raw_types[i])) {
4323			int idx = GET_NAME_NTH_IDX(raw_types[i]);
4324
4325			if (CHECK(idx <= 0 || idx > strs_cnt,
4326				  "Error getting string #%d, strs_cnt:%d",
4327				  idx, strs_cnt)) {
4328				err = -1;
4329				goto done;
4330			}
4331			ret_types[i] = strs_idx[idx-1] - str;
4332		} else {
4333			ret_types[i] = raw_types[i];
4334		}
4335	}
4336	offset += type_sec_size;
4337
4338	/* Copy string section */
4339	memcpy(raw_btf + offset, str, str_sec_size);
4340
4341	ret_hdr = (struct btf_header *)raw_btf;
4342	ret_hdr->type_len = type_sec_size;
4343	ret_hdr->str_off = type_sec_size;
4344	ret_hdr->str_len = str_sec_size;
4345
4346	*btf_size = size_needed;
4347	if (ret_next_str)
4348		*ret_next_str =
4349			next_str_idx < strs_cnt ? strs_idx[next_str_idx] : NULL;
4350
4351done:
4352	free(strs_idx);
4353	if (err) {
4354		free(raw_btf);
4355		return NULL;
4356	}
4357	return raw_btf;
4358}
4359
4360static int load_raw_btf(const void *raw_data, size_t raw_size)
4361{
4362	LIBBPF_OPTS(bpf_btf_load_opts, opts);
4363	int btf_fd;
4364
4365	if (always_log) {
4366		opts.log_buf = btf_log_buf,
4367		opts.log_size = BTF_LOG_BUF_SIZE,
4368		opts.log_level = 1;
4369	}
4370
4371	btf_fd = bpf_btf_load(raw_data, raw_size, &opts);
4372	if (btf_fd < 0 && !always_log) {
4373		opts.log_buf = btf_log_buf,
4374		opts.log_size = BTF_LOG_BUF_SIZE,
4375		opts.log_level = 1;
4376		btf_fd = bpf_btf_load(raw_data, raw_size, &opts);
4377	}
4378
4379	return btf_fd;
4380}
4381
4382static void do_test_raw(unsigned int test_num)
4383{
4384	struct btf_raw_test *test = &raw_tests[test_num - 1];
4385	LIBBPF_OPTS(bpf_map_create_opts, opts);
4386	int map_fd = -1, btf_fd = -1;
4387	unsigned int raw_btf_size;
4388	struct btf_header *hdr;
4389	void *raw_btf;
4390	int err;
4391
4392	if (!test__start_subtest(test->descr))
4393		return;
4394
4395	raw_btf = btf_raw_create(&hdr_tmpl,
4396				 test->raw_types,
4397				 test->str_sec,
4398				 test->str_sec_size,
4399				 &raw_btf_size, NULL);
4400	if (!raw_btf)
4401		return;
4402
4403	hdr = raw_btf;
4404
4405	hdr->hdr_len = (int)hdr->hdr_len + test->hdr_len_delta;
4406	hdr->type_off = (int)hdr->type_off + test->type_off_delta;
4407	hdr->str_off = (int)hdr->str_off + test->str_off_delta;
4408	hdr->str_len = (int)hdr->str_len + test->str_len_delta;
4409
4410	*btf_log_buf = '\0';
4411	btf_fd = load_raw_btf(raw_btf, raw_btf_size);
4412	free(raw_btf);
4413
4414	err = ((btf_fd < 0) != test->btf_load_err);
4415	if (CHECK(err, "btf_fd:%d test->btf_load_err:%u",
4416		  btf_fd, test->btf_load_err) ||
4417	    CHECK(test->err_str && !strstr(btf_log_buf, test->err_str),
4418		  "expected err_str:%s\n", test->err_str)) {
4419		err = -1;
4420		goto done;
4421	}
4422
4423	if (err || btf_fd < 0)
4424		goto done;
4425
4426	if (!test->map_type)
4427		goto done;
4428
4429	opts.btf_fd = btf_fd;
4430	opts.btf_key_type_id = test->key_type_id;
4431	opts.btf_value_type_id = test->value_type_id;
4432	map_fd = bpf_map_create(test->map_type, test->map_name,
4433				test->key_size, test->value_size, test->max_entries, &opts);
4434
4435	err = ((map_fd < 0) != test->map_create_err);
4436	CHECK(err, "map_fd:%d test->map_create_err:%u",
4437	      map_fd, test->map_create_err);
4438
4439done:
4440	if (*btf_log_buf && (err || always_log))
4441		fprintf(stderr, "\n%s", btf_log_buf);
4442	if (btf_fd >= 0)
4443		close(btf_fd);
4444	if (map_fd >= 0)
4445		close(map_fd);
4446}
4447
4448struct btf_get_info_test {
4449	const char *descr;
4450	const char *str_sec;
4451	__u32 raw_types[MAX_NR_RAW_U32];
4452	__u32 str_sec_size;
4453	int btf_size_delta;
4454	int (*special_test)(unsigned int test_num);
4455};
4456
4457static int test_big_btf_info(unsigned int test_num);
4458static int test_btf_id(unsigned int test_num);
4459
4460const struct btf_get_info_test get_info_tests[] = {
4461{
4462	.descr = "== raw_btf_size+1",
4463	.raw_types = {
4464		/* int */				/* [1] */
4465		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
4466		BTF_END_RAW,
4467	},
4468	.str_sec = "",
4469	.str_sec_size = sizeof(""),
4470	.btf_size_delta = 1,
4471},
4472{
4473	.descr = "== raw_btf_size-3",
4474	.raw_types = {
4475		/* int */				/* [1] */
4476		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
4477		BTF_END_RAW,
4478	},
4479	.str_sec = "",
4480	.str_sec_size = sizeof(""),
4481	.btf_size_delta = -3,
4482},
4483{
4484	.descr = "Large bpf_btf_info",
4485	.raw_types = {
4486		/* int */				/* [1] */
4487		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
4488		BTF_END_RAW,
4489	},
4490	.str_sec = "",
4491	.str_sec_size = sizeof(""),
4492	.special_test = test_big_btf_info,
4493},
4494{
4495	.descr = "BTF ID",
4496	.raw_types = {
4497		/* int */				/* [1] */
4498		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
4499		/* unsigned int */			/* [2] */
4500		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),
4501		BTF_END_RAW,
4502	},
4503	.str_sec = "",
4504	.str_sec_size = sizeof(""),
4505	.special_test = test_btf_id,
4506},
4507};
4508
4509static int test_big_btf_info(unsigned int test_num)
4510{
4511	const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
4512	uint8_t *raw_btf = NULL, *user_btf = NULL;
4513	unsigned int raw_btf_size;
4514	struct {
4515		struct bpf_btf_info info;
4516		uint64_t garbage;
4517	} info_garbage;
4518	struct bpf_btf_info *info;
4519	int btf_fd = -1, err;
4520	uint32_t info_len;
4521
4522	raw_btf = btf_raw_create(&hdr_tmpl,
4523				 test->raw_types,
4524				 test->str_sec,
4525				 test->str_sec_size,
4526				 &raw_btf_size, NULL);
4527
4528	if (!raw_btf)
4529		return -1;
4530
4531	*btf_log_buf = '\0';
4532
4533	user_btf = malloc(raw_btf_size);
4534	if (CHECK(!user_btf, "!user_btf")) {
4535		err = -1;
4536		goto done;
4537	}
4538
4539	btf_fd = load_raw_btf(raw_btf, raw_btf_size);
4540	if (CHECK(btf_fd < 0, "errno:%d", errno)) {
4541		err = -1;
4542		goto done;
4543	}
4544
4545	/*
4546	 * GET_INFO should error out if the userspace info
4547	 * has non zero tailing bytes.
4548	 */
4549	info = &info_garbage.info;
4550	memset(info, 0, sizeof(*info));
4551	info_garbage.garbage = 0xdeadbeef;
4552	info_len = sizeof(info_garbage);
4553	info->btf = ptr_to_u64(user_btf);
4554	info->btf_size = raw_btf_size;
4555
4556	err = bpf_btf_get_info_by_fd(btf_fd, info, &info_len);
4557	if (CHECK(!err, "!err")) {
4558		err = -1;
4559		goto done;
4560	}
4561
4562	/*
4563	 * GET_INFO should succeed even info_len is larger than
4564	 * the kernel supported as long as tailing bytes are zero.
4565	 * The kernel supported info len should also be returned
4566	 * to userspace.
4567	 */
4568	info_garbage.garbage = 0;
4569	err = bpf_btf_get_info_by_fd(btf_fd, info, &info_len);
4570	if (CHECK(err || info_len != sizeof(*info),
4571		  "err:%d errno:%d info_len:%u sizeof(*info):%zu",
4572		  err, errno, info_len, sizeof(*info))) {
4573		err = -1;
4574		goto done;
4575	}
4576
4577	fprintf(stderr, "OK");
4578
4579done:
4580	if (*btf_log_buf && (err || always_log))
4581		fprintf(stderr, "\n%s", btf_log_buf);
4582
4583	free(raw_btf);
4584	free(user_btf);
4585
4586	if (btf_fd >= 0)
4587		close(btf_fd);
4588
4589	return err;
4590}
4591
4592static int test_btf_id(unsigned int test_num)
4593{
4594	const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
4595	LIBBPF_OPTS(bpf_map_create_opts, opts);
4596	uint8_t *raw_btf = NULL, *user_btf[2] = {};
4597	int btf_fd[2] = {-1, -1}, map_fd = -1;
4598	struct bpf_map_info map_info = {};
4599	struct bpf_btf_info info[2] = {};
4600	unsigned int raw_btf_size;
4601	uint32_t info_len;
4602	int err, i, ret;
4603
4604	raw_btf = btf_raw_create(&hdr_tmpl,
4605				 test->raw_types,
4606				 test->str_sec,
4607				 test->str_sec_size,
4608				 &raw_btf_size, NULL);
4609
4610	if (!raw_btf)
4611		return -1;
4612
4613	*btf_log_buf = '\0';
4614
4615	for (i = 0; i < 2; i++) {
4616		user_btf[i] = malloc(raw_btf_size);
4617		if (CHECK(!user_btf[i], "!user_btf[%d]", i)) {
4618			err = -1;
4619			goto done;
4620		}
4621		info[i].btf = ptr_to_u64(user_btf[i]);
4622		info[i].btf_size = raw_btf_size;
4623	}
4624
4625	btf_fd[0] = load_raw_btf(raw_btf, raw_btf_size);
4626	if (CHECK(btf_fd[0] < 0, "errno:%d", errno)) {
4627		err = -1;
4628		goto done;
4629	}
4630
4631	/* Test BPF_OBJ_GET_INFO_BY_ID on btf_id */
4632	info_len = sizeof(info[0]);
4633	err = bpf_btf_get_info_by_fd(btf_fd[0], &info[0], &info_len);
4634	if (CHECK(err, "errno:%d", errno)) {
4635		err = -1;
4636		goto done;
4637	}
4638
4639	btf_fd[1] = bpf_btf_get_fd_by_id(info[0].id);
4640	if (CHECK(btf_fd[1] < 0, "errno:%d", errno)) {
4641		err = -1;
4642		goto done;
4643	}
4644
4645	ret = 0;
4646	err = bpf_btf_get_info_by_fd(btf_fd[1], &info[1], &info_len);
4647	if (CHECK(err || info[0].id != info[1].id ||
4648		  info[0].btf_size != info[1].btf_size ||
4649		  (ret = memcmp(user_btf[0], user_btf[1], info[0].btf_size)),
4650		  "err:%d errno:%d id0:%u id1:%u btf_size0:%u btf_size1:%u memcmp:%d",
4651		  err, errno, info[0].id, info[1].id,
4652		  info[0].btf_size, info[1].btf_size, ret)) {
4653		err = -1;
4654		goto done;
4655	}
4656
4657	/* Test btf members in struct bpf_map_info */
4658	opts.btf_fd = btf_fd[0];
4659	opts.btf_key_type_id = 1;
4660	opts.btf_value_type_id = 2;
4661	map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "test_btf_id",
4662				sizeof(int), sizeof(int), 4, &opts);
4663	if (CHECK(map_fd < 0, "errno:%d", errno)) {
4664		err = -1;
4665		goto done;
4666	}
4667
4668	info_len = sizeof(map_info);
4669	err = bpf_map_get_info_by_fd(map_fd, &map_info, &info_len);
4670	if (CHECK(err || map_info.btf_id != info[0].id ||
4671		  map_info.btf_key_type_id != 1 || map_info.btf_value_type_id != 2,
4672		  "err:%d errno:%d info.id:%u btf_id:%u btf_key_type_id:%u btf_value_type_id:%u",
4673		  err, errno, info[0].id, map_info.btf_id, map_info.btf_key_type_id,
4674		  map_info.btf_value_type_id)) {
4675		err = -1;
4676		goto done;
4677	}
4678
4679	for (i = 0; i < 2; i++) {
4680		close(btf_fd[i]);
4681		btf_fd[i] = -1;
4682	}
4683
4684	/* Test BTF ID is removed from the kernel */
4685	btf_fd[0] = bpf_btf_get_fd_by_id(map_info.btf_id);
4686	if (CHECK(btf_fd[0] < 0, "errno:%d", errno)) {
4687		err = -1;
4688		goto done;
4689	}
4690	close(btf_fd[0]);
4691	btf_fd[0] = -1;
4692
4693	/* The map holds the last ref to BTF and its btf_id */
4694	close(map_fd);
4695	map_fd = -1;
 
 
 
 
 
4696
4697	fprintf(stderr, "OK");
4698
4699done:
4700	if (*btf_log_buf && (err || always_log))
4701		fprintf(stderr, "\n%s", btf_log_buf);
4702
4703	free(raw_btf);
4704	if (map_fd >= 0)
4705		close(map_fd);
4706	for (i = 0; i < 2; i++) {
4707		free(user_btf[i]);
4708		if (btf_fd[i] >= 0)
4709			close(btf_fd[i]);
4710	}
4711
4712	return err;
4713}
4714
4715static void do_test_get_info(unsigned int test_num)
4716{
4717	const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
4718	unsigned int raw_btf_size, user_btf_size, expected_nbytes;
4719	uint8_t *raw_btf = NULL, *user_btf = NULL;
4720	struct bpf_btf_info info = {};
4721	int btf_fd = -1, err, ret;
4722	uint32_t info_len;
4723
4724	if (!test__start_subtest(test->descr))
4725		return;
4726
4727	if (test->special_test) {
4728		err = test->special_test(test_num);
4729		if (CHECK(err, "failed: %d\n", err))
4730			return;
4731	}
4732
4733	raw_btf = btf_raw_create(&hdr_tmpl,
4734				 test->raw_types,
4735				 test->str_sec,
4736				 test->str_sec_size,
4737				 &raw_btf_size, NULL);
4738
4739	if (!raw_btf)
4740		return;
4741
4742	*btf_log_buf = '\0';
4743
4744	user_btf = malloc(raw_btf_size);
4745	if (CHECK(!user_btf, "!user_btf")) {
4746		err = -1;
4747		goto done;
4748	}
4749
4750	btf_fd = load_raw_btf(raw_btf, raw_btf_size);
4751	if (CHECK(btf_fd <= 0, "errno:%d", errno)) {
4752		err = -1;
4753		goto done;
4754	}
4755
4756	user_btf_size = (int)raw_btf_size + test->btf_size_delta;
4757	expected_nbytes = min(raw_btf_size, user_btf_size);
4758	if (raw_btf_size > expected_nbytes)
4759		memset(user_btf + expected_nbytes, 0xff,
4760		       raw_btf_size - expected_nbytes);
4761
4762	info_len = sizeof(info);
4763	info.btf = ptr_to_u64(user_btf);
4764	info.btf_size = user_btf_size;
4765
4766	ret = 0;
4767	err = bpf_btf_get_info_by_fd(btf_fd, &info, &info_len);
4768	if (CHECK(err || !info.id || info_len != sizeof(info) ||
4769		  info.btf_size != raw_btf_size ||
4770		  (ret = memcmp(raw_btf, user_btf, expected_nbytes)),
4771		  "err:%d errno:%d info.id:%u info_len:%u sizeof(info):%zu raw_btf_size:%u info.btf_size:%u expected_nbytes:%u memcmp:%d",
4772		  err, errno, info.id, info_len, sizeof(info),
4773		  raw_btf_size, info.btf_size, expected_nbytes, ret)) {
4774		err = -1;
4775		goto done;
4776	}
4777
4778	while (expected_nbytes < raw_btf_size) {
4779		fprintf(stderr, "%u...", expected_nbytes);
4780		if (CHECK(user_btf[expected_nbytes++] != 0xff,
4781			  "user_btf[%u]:%x != 0xff", expected_nbytes - 1,
4782			  user_btf[expected_nbytes - 1])) {
4783			err = -1;
4784			goto done;
4785		}
4786	}
4787
4788	fprintf(stderr, "OK");
4789
4790done:
4791	if (*btf_log_buf && (err || always_log))
4792		fprintf(stderr, "\n%s", btf_log_buf);
4793
4794	free(raw_btf);
4795	free(user_btf);
4796
4797	if (btf_fd >= 0)
4798		close(btf_fd);
4799}
4800
4801struct btf_file_test {
4802	const char *file;
4803	bool btf_kv_notfound;
4804};
4805
4806static struct btf_file_test file_tests[] = {
4807	{ .file = "test_btf_newkv.bpf.o", },
4808	{ .file = "test_btf_nokv.bpf.o", .btf_kv_notfound = true, },
4809};
4810
4811static void do_test_file(unsigned int test_num)
4812{
4813	const struct btf_file_test *test = &file_tests[test_num - 1];
4814	const char *expected_fnames[] = {"_dummy_tracepoint",
4815					 "test_long_fname_1",
4816					 "test_long_fname_2"};
4817	struct btf_ext *btf_ext = NULL;
4818	struct bpf_prog_info info = {};
4819	struct bpf_object *obj = NULL;
4820	struct bpf_func_info *finfo;
4821	struct bpf_program *prog;
4822	__u32 info_len, rec_size;
4823	bool has_btf_ext = false;
4824	struct btf *btf = NULL;
4825	void *func_info = NULL;
4826	struct bpf_map *map;
4827	int i, err, prog_fd;
4828
4829	if (!test__start_subtest(test->file))
4830		return;
4831
4832	btf = btf__parse_elf(test->file, &btf_ext);
4833	err = libbpf_get_error(btf);
4834	if (err) {
4835		if (err == -ENOENT) {
4836			printf("%s:SKIP: No ELF %s found", __func__, BTF_ELF_SEC);
4837			test__skip();
4838			return;
4839		}
4840		return;
4841	}
4842	btf__free(btf);
4843
4844	has_btf_ext = btf_ext != NULL;
4845	btf_ext__free(btf_ext);
4846
4847	/* temporary disable LIBBPF_STRICT_MAP_DEFINITIONS to test legacy maps */
4848	libbpf_set_strict_mode(LIBBPF_STRICT_ALL & ~LIBBPF_STRICT_MAP_DEFINITIONS);
4849	obj = bpf_object__open(test->file);
4850	err = libbpf_get_error(obj);
4851	if (CHECK(err, "obj: %d", err))
4852		return;
4853
4854	prog = bpf_object__next_program(obj, NULL);
4855	if (CHECK(!prog, "Cannot find bpf_prog")) {
4856		err = -1;
4857		goto done;
4858	}
4859
4860	bpf_program__set_type(prog, BPF_PROG_TYPE_TRACEPOINT);
4861	err = bpf_object__load(obj);
4862	if (CHECK(err < 0, "bpf_object__load: %d", err))
4863		goto done;
4864	prog_fd = bpf_program__fd(prog);
4865
4866	map = bpf_object__find_map_by_name(obj, "btf_map");
4867	if (CHECK(!map, "btf_map not found")) {
4868		err = -1;
4869		goto done;
4870	}
4871
4872	err = (bpf_map__btf_key_type_id(map) == 0 || bpf_map__btf_value_type_id(map) == 0)
4873		!= test->btf_kv_notfound;
4874	if (CHECK(err, "btf_key_type_id:%u btf_value_type_id:%u test->btf_kv_notfound:%u",
4875		  bpf_map__btf_key_type_id(map), bpf_map__btf_value_type_id(map),
4876		  test->btf_kv_notfound))
4877		goto done;
4878
4879	if (!has_btf_ext)
4880		goto skip;
4881
4882	/* get necessary program info */
4883	info_len = sizeof(struct bpf_prog_info);
4884	err = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len);
4885
4886	if (CHECK(err < 0, "invalid get info (1st) errno:%d", errno)) {
4887		fprintf(stderr, "%s\n", btf_log_buf);
4888		err = -1;
4889		goto done;
4890	}
4891	if (CHECK(info.nr_func_info != 3,
4892		  "incorrect info.nr_func_info (1st) %d",
4893		  info.nr_func_info)) {
4894		err = -1;
4895		goto done;
4896	}
4897	rec_size = info.func_info_rec_size;
4898	if (CHECK(rec_size != sizeof(struct bpf_func_info),
4899		  "incorrect info.func_info_rec_size (1st) %d\n", rec_size)) {
4900		err = -1;
4901		goto done;
4902	}
4903
4904	func_info = malloc(info.nr_func_info * rec_size);
4905	if (CHECK(!func_info, "out of memory")) {
4906		err = -1;
4907		goto done;
4908	}
4909
4910	/* reset info to only retrieve func_info related data */
4911	memset(&info, 0, sizeof(info));
4912	info.nr_func_info = 3;
4913	info.func_info_rec_size = rec_size;
4914	info.func_info = ptr_to_u64(func_info);
4915
4916	err = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len);
4917
4918	if (CHECK(err < 0, "invalid get info (2nd) errno:%d", errno)) {
4919		fprintf(stderr, "%s\n", btf_log_buf);
4920		err = -1;
4921		goto done;
4922	}
4923	if (CHECK(info.nr_func_info != 3,
4924		  "incorrect info.nr_func_info (2nd) %d",
4925		  info.nr_func_info)) {
4926		err = -1;
4927		goto done;
4928	}
4929	if (CHECK(info.func_info_rec_size != rec_size,
4930		  "incorrect info.func_info_rec_size (2nd) %d",
4931		  info.func_info_rec_size)) {
4932		err = -1;
4933		goto done;
4934	}
4935
4936	btf = btf__load_from_kernel_by_id(info.btf_id);
4937	err = libbpf_get_error(btf);
4938	if (CHECK(err, "cannot get btf from kernel, err: %d", err))
4939		goto done;
4940
4941	/* check three functions */
4942	finfo = func_info;
4943	for (i = 0; i < 3; i++) {
4944		const struct btf_type *t;
4945		const char *fname;
4946
4947		t = btf__type_by_id(btf, finfo->type_id);
4948		if (CHECK(!t, "btf__type_by_id failure: id %u",
4949			  finfo->type_id)) {
4950			err = -1;
4951			goto done;
4952		}
4953
4954		fname = btf__name_by_offset(btf, t->name_off);
4955		err = strcmp(fname, expected_fnames[i]);
4956		/* for the second and third functions in .text section,
4957		 * the compiler may order them either way.
4958		 */
4959		if (i && err)
4960			err = strcmp(fname, expected_fnames[3 - i]);
4961		if (CHECK(err, "incorrect fname %s", fname ? : "")) {
4962			err = -1;
4963			goto done;
4964		}
4965
4966		finfo = (void *)finfo + rec_size;
4967	}
4968
4969skip:
4970	fprintf(stderr, "OK");
4971
4972done:
4973	libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
4974
4975	btf__free(btf);
4976	free(func_info);
4977	bpf_object__close(obj);
4978}
4979
4980const char *pprint_enum_str[] = {
4981	"ENUM_ZERO",
4982	"ENUM_ONE",
4983	"ENUM_TWO",
4984	"ENUM_THREE",
4985};
4986
4987struct pprint_mapv {
4988	uint32_t ui32;
4989	uint16_t ui16;
4990	/* 2 bytes hole */
4991	int32_t si32;
4992	uint32_t unused_bits2a:2,
4993		bits28:28,
4994		unused_bits2b:2;
4995	union {
4996		uint64_t ui64;
4997		uint8_t ui8a[8];
4998	};
4999	enum {
5000		ENUM_ZERO,
5001		ENUM_ONE,
5002		ENUM_TWO,
5003		ENUM_THREE,
5004	} aenum;
5005	uint32_t ui32b;
5006	uint32_t bits2c:2;
5007	uint8_t si8_4[2][2];
5008};
5009
5010#ifdef __SIZEOF_INT128__
5011struct pprint_mapv_int128 {
5012	__int128 si128a;
5013	__int128 si128b;
5014	unsigned __int128 bits3:3;
5015	unsigned __int128 bits80:80;
5016	unsigned __int128 ui128;
5017};
5018#endif
5019
5020static struct btf_raw_test pprint_test_template[] = {
5021{
5022	.raw_types = {
5023		/* unsigned char */			/* [1] */
5024		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 8, 1),
5025		/* unsigned short */			/* [2] */
5026		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 16, 2),
5027		/* unsigned int */			/* [3] */
5028		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),
5029		/* int */				/* [4] */
5030		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
5031		/* unsigned long long */		/* [5] */
5032		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 64, 8),
5033		/* 2 bits */				/* [6] */
5034		BTF_TYPE_INT_ENC(0, 0, 0, 2, 2),
5035		/* 28 bits */				/* [7] */
5036		BTF_TYPE_INT_ENC(0, 0, 0, 28, 4),
5037		/* uint8_t[8] */			/* [8] */
5038		BTF_TYPE_ARRAY_ENC(9, 1, 8),
5039		/* typedef unsigned char uint8_t */	/* [9] */
5040		BTF_TYPEDEF_ENC(NAME_TBD, 1),
5041		/* typedef unsigned short uint16_t */	/* [10] */
5042		BTF_TYPEDEF_ENC(NAME_TBD, 2),
5043		/* typedef unsigned int uint32_t */	/* [11] */
5044		BTF_TYPEDEF_ENC(NAME_TBD, 3),
5045		/* typedef int int32_t */		/* [12] */
5046		BTF_TYPEDEF_ENC(NAME_TBD, 4),
5047		/* typedef unsigned long long uint64_t *//* [13] */
5048		BTF_TYPEDEF_ENC(NAME_TBD, 5),
5049		/* union (anon) */			/* [14] */
5050		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 0, 2), 8),
5051		BTF_MEMBER_ENC(NAME_TBD, 13, 0),/* uint64_t ui64; */
5052		BTF_MEMBER_ENC(NAME_TBD, 8, 0),	/* uint8_t ui8a[8]; */
5053		/* enum (anon) */			/* [15] */
5054		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 4), 4),
5055		BTF_ENUM_ENC(NAME_TBD, 0),
5056		BTF_ENUM_ENC(NAME_TBD, 1),
5057		BTF_ENUM_ENC(NAME_TBD, 2),
5058		BTF_ENUM_ENC(NAME_TBD, 3),
5059		/* struct pprint_mapv */		/* [16] */
5060		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 11), 40),
5061		BTF_MEMBER_ENC(NAME_TBD, 11, 0),	/* uint32_t ui32 */
5062		BTF_MEMBER_ENC(NAME_TBD, 10, 32),	/* uint16_t ui16 */
5063		BTF_MEMBER_ENC(NAME_TBD, 12, 64),	/* int32_t si32 */
5064		BTF_MEMBER_ENC(NAME_TBD, 6, 96),	/* unused_bits2a */
5065		BTF_MEMBER_ENC(NAME_TBD, 7, 98),	/* bits28 */
5066		BTF_MEMBER_ENC(NAME_TBD, 6, 126),	/* unused_bits2b */
5067		BTF_MEMBER_ENC(0, 14, 128),		/* union (anon) */
5068		BTF_MEMBER_ENC(NAME_TBD, 15, 192),	/* aenum */
5069		BTF_MEMBER_ENC(NAME_TBD, 11, 224),	/* uint32_t ui32b */
5070		BTF_MEMBER_ENC(NAME_TBD, 6, 256),	/* bits2c */
5071		BTF_MEMBER_ENC(NAME_TBD, 17, 264),	/* si8_4 */
5072		BTF_TYPE_ARRAY_ENC(18, 1, 2),		/* [17] */
5073		BTF_TYPE_ARRAY_ENC(1, 1, 2),		/* [18] */
5074		BTF_END_RAW,
5075	},
5076	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"),
5077	.key_size = sizeof(unsigned int),
5078	.value_size = sizeof(struct pprint_mapv),
5079	.key_type_id = 3,	/* unsigned int */
5080	.value_type_id = 16,	/* struct pprint_mapv */
5081	.max_entries = 128,
5082},
5083
5084{
5085	/* this type will have the same type as the
5086	 * first .raw_types definition, but struct type will
5087	 * be encoded with kind_flag set.
5088	 */
5089	.raw_types = {
5090		/* unsigned char */			/* [1] */
5091		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 8, 1),
5092		/* unsigned short */			/* [2] */
5093		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 16, 2),
5094		/* unsigned int */			/* [3] */
5095		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),
5096		/* int */				/* [4] */
5097		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
5098		/* unsigned long long */		/* [5] */
5099		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 64, 8),
5100		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),	/* [6] */
5101		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),	/* [7] */
5102		/* uint8_t[8] */			/* [8] */
5103		BTF_TYPE_ARRAY_ENC(9, 1, 8),
5104		/* typedef unsigned char uint8_t */	/* [9] */
5105		BTF_TYPEDEF_ENC(NAME_TBD, 1),
5106		/* typedef unsigned short uint16_t */	/* [10] */
5107		BTF_TYPEDEF_ENC(NAME_TBD, 2),
5108		/* typedef unsigned int uint32_t */	/* [11] */
5109		BTF_TYPEDEF_ENC(NAME_TBD, 3),
5110		/* typedef int int32_t */		/* [12] */
5111		BTF_TYPEDEF_ENC(NAME_TBD, 4),
5112		/* typedef unsigned long long uint64_t *//* [13] */
5113		BTF_TYPEDEF_ENC(NAME_TBD, 5),
5114		/* union (anon) */			/* [14] */
5115		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 0, 2), 8),
5116		BTF_MEMBER_ENC(NAME_TBD, 13, 0),/* uint64_t ui64; */
5117		BTF_MEMBER_ENC(NAME_TBD, 8, 0),	/* uint8_t ui8a[8]; */
5118		/* enum (anon) */			/* [15] */
5119		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 4), 4),
5120		BTF_ENUM_ENC(NAME_TBD, 0),
5121		BTF_ENUM_ENC(NAME_TBD, 1),
5122		BTF_ENUM_ENC(NAME_TBD, 2),
5123		BTF_ENUM_ENC(NAME_TBD, 3),
5124		/* struct pprint_mapv */		/* [16] */
5125		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 11), 40),
5126		BTF_MEMBER_ENC(NAME_TBD, 11, BTF_MEMBER_OFFSET(0, 0)),	/* uint32_t ui32 */
5127		BTF_MEMBER_ENC(NAME_TBD, 10, BTF_MEMBER_OFFSET(0, 32)),	/* uint16_t ui16 */
5128		BTF_MEMBER_ENC(NAME_TBD, 12, BTF_MEMBER_OFFSET(0, 64)),	/* int32_t si32 */
5129		BTF_MEMBER_ENC(NAME_TBD, 6, BTF_MEMBER_OFFSET(2, 96)),	/* unused_bits2a */
5130		BTF_MEMBER_ENC(NAME_TBD, 7, BTF_MEMBER_OFFSET(28, 98)),	/* bits28 */
5131		BTF_MEMBER_ENC(NAME_TBD, 6, BTF_MEMBER_OFFSET(2, 126)),	/* unused_bits2b */
5132		BTF_MEMBER_ENC(0, 14, BTF_MEMBER_OFFSET(0, 128)),	/* union (anon) */
5133		BTF_MEMBER_ENC(NAME_TBD, 15, BTF_MEMBER_OFFSET(0, 192)),	/* aenum */
5134		BTF_MEMBER_ENC(NAME_TBD, 11, BTF_MEMBER_OFFSET(0, 224)),	/* uint32_t ui32b */
5135		BTF_MEMBER_ENC(NAME_TBD, 6, BTF_MEMBER_OFFSET(2, 256)),	/* bits2c */
5136		BTF_MEMBER_ENC(NAME_TBD, 17, 264),	/* si8_4 */
5137		BTF_TYPE_ARRAY_ENC(18, 1, 2),		/* [17] */
5138		BTF_TYPE_ARRAY_ENC(1, 1, 2),		/* [18] */
5139		BTF_END_RAW,
5140	},
5141	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"),
5142	.key_size = sizeof(unsigned int),
5143	.value_size = sizeof(struct pprint_mapv),
5144	.key_type_id = 3,	/* unsigned int */
5145	.value_type_id = 16,	/* struct pprint_mapv */
5146	.max_entries = 128,
5147},
5148
5149{
5150	/* this type will have the same layout as the
5151	 * first .raw_types definition. The struct type will
5152	 * be encoded with kind_flag set, bitfield members
5153	 * are added typedef/const/volatile, and bitfield members
5154	 * will have both int and enum types.
5155	 */
5156	.raw_types = {
5157		/* unsigned char */			/* [1] */
5158		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 8, 1),
5159		/* unsigned short */			/* [2] */
5160		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 16, 2),
5161		/* unsigned int */			/* [3] */
5162		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),
5163		/* int */				/* [4] */
5164		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
5165		/* unsigned long long */		/* [5] */
5166		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 64, 8),
5167		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),	/* [6] */
5168		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),	/* [7] */
5169		/* uint8_t[8] */			/* [8] */
5170		BTF_TYPE_ARRAY_ENC(9, 1, 8),
5171		/* typedef unsigned char uint8_t */	/* [9] */
5172		BTF_TYPEDEF_ENC(NAME_TBD, 1),
5173		/* typedef unsigned short uint16_t */	/* [10] */
5174		BTF_TYPEDEF_ENC(NAME_TBD, 2),
5175		/* typedef unsigned int uint32_t */	/* [11] */
5176		BTF_TYPEDEF_ENC(NAME_TBD, 3),
5177		/* typedef int int32_t */		/* [12] */
5178		BTF_TYPEDEF_ENC(NAME_TBD, 4),
5179		/* typedef unsigned long long uint64_t *//* [13] */
5180		BTF_TYPEDEF_ENC(NAME_TBD, 5),
5181		/* union (anon) */			/* [14] */
5182		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 0, 2), 8),
5183		BTF_MEMBER_ENC(NAME_TBD, 13, 0),/* uint64_t ui64; */
5184		BTF_MEMBER_ENC(NAME_TBD, 8, 0),	/* uint8_t ui8a[8]; */
5185		/* enum (anon) */			/* [15] */
5186		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 4), 4),
5187		BTF_ENUM_ENC(NAME_TBD, 0),
5188		BTF_ENUM_ENC(NAME_TBD, 1),
5189		BTF_ENUM_ENC(NAME_TBD, 2),
5190		BTF_ENUM_ENC(NAME_TBD, 3),
5191		/* struct pprint_mapv */		/* [16] */
5192		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 11), 40),
5193		BTF_MEMBER_ENC(NAME_TBD, 11, BTF_MEMBER_OFFSET(0, 0)),	/* uint32_t ui32 */
5194		BTF_MEMBER_ENC(NAME_TBD, 10, BTF_MEMBER_OFFSET(0, 32)),	/* uint16_t ui16 */
5195		BTF_MEMBER_ENC(NAME_TBD, 12, BTF_MEMBER_OFFSET(0, 64)),	/* int32_t si32 */
5196		BTF_MEMBER_ENC(NAME_TBD, 17, BTF_MEMBER_OFFSET(2, 96)),	/* unused_bits2a */
5197		BTF_MEMBER_ENC(NAME_TBD, 7, BTF_MEMBER_OFFSET(28, 98)),	/* bits28 */
5198		BTF_MEMBER_ENC(NAME_TBD, 19, BTF_MEMBER_OFFSET(2, 126)),/* unused_bits2b */
5199		BTF_MEMBER_ENC(0, 14, BTF_MEMBER_OFFSET(0, 128)),	/* union (anon) */
5200		BTF_MEMBER_ENC(NAME_TBD, 15, BTF_MEMBER_OFFSET(0, 192)),	/* aenum */
5201		BTF_MEMBER_ENC(NAME_TBD, 11, BTF_MEMBER_OFFSET(0, 224)),	/* uint32_t ui32b */
5202		BTF_MEMBER_ENC(NAME_TBD, 17, BTF_MEMBER_OFFSET(2, 256)),	/* bits2c */
5203		BTF_MEMBER_ENC(NAME_TBD, 20, BTF_MEMBER_OFFSET(0, 264)),	/* si8_4 */
5204		/* typedef unsigned int ___int */	/* [17] */
5205		BTF_TYPEDEF_ENC(NAME_TBD, 18),
5206		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_VOLATILE, 0, 0), 6),	/* [18] */
5207		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 15),	/* [19] */
5208		BTF_TYPE_ARRAY_ENC(21, 1, 2),					/* [20] */
5209		BTF_TYPE_ARRAY_ENC(1, 1, 2),					/* [21] */
5210		BTF_END_RAW,
5211	},
5212	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"),
5213	.key_size = sizeof(unsigned int),
5214	.value_size = sizeof(struct pprint_mapv),
5215	.key_type_id = 3,	/* unsigned int */
5216	.value_type_id = 16,	/* struct pprint_mapv */
5217	.max_entries = 128,
5218},
5219
5220#ifdef __SIZEOF_INT128__
5221{
5222	/* test int128 */
5223	.raw_types = {
5224		/* unsigned int */				/* [1] */
5225		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),
5226		/* __int128 */					/* [2] */
5227		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 128, 16),
5228		/* unsigned __int128 */				/* [3] */
5229		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 128, 16),
5230		/* struct pprint_mapv_int128 */			/* [4] */
5231		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 5), 64),
5232		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 0)),		/* si128a */
5233		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 128)),		/* si128b */
5234		BTF_MEMBER_ENC(NAME_TBD, 3, BTF_MEMBER_OFFSET(3, 256)),		/* bits3 */
5235		BTF_MEMBER_ENC(NAME_TBD, 3, BTF_MEMBER_OFFSET(80, 259)),	/* bits80 */
5236		BTF_MEMBER_ENC(NAME_TBD, 3, BTF_MEMBER_OFFSET(0, 384)),		/* ui128 */
5237		BTF_END_RAW,
5238	},
5239	BTF_STR_SEC("\0unsigned int\0__int128\0unsigned __int128\0pprint_mapv_int128\0si128a\0si128b\0bits3\0bits80\0ui128"),
5240	.key_size = sizeof(unsigned int),
5241	.value_size = sizeof(struct pprint_mapv_int128),
5242	.key_type_id = 1,
5243	.value_type_id = 4,
5244	.max_entries = 128,
5245	.mapv_kind = PPRINT_MAPV_KIND_INT128,
5246},
5247#endif
5248
5249};
5250
5251static struct btf_pprint_test_meta {
5252	const char *descr;
5253	enum bpf_map_type map_type;
5254	const char *map_name;
5255	bool ordered_map;
5256	bool lossless_map;
5257	bool percpu_map;
5258} pprint_tests_meta[] = {
5259{
5260	.descr = "BTF pretty print array",
5261	.map_type = BPF_MAP_TYPE_ARRAY,
5262	.map_name = "pprint_test_array",
5263	.ordered_map = true,
5264	.lossless_map = true,
5265	.percpu_map = false,
5266},
5267
5268{
5269	.descr = "BTF pretty print hash",
5270	.map_type = BPF_MAP_TYPE_HASH,
5271	.map_name = "pprint_test_hash",
5272	.ordered_map = false,
5273	.lossless_map = true,
5274	.percpu_map = false,
5275},
5276
5277{
5278	.descr = "BTF pretty print lru hash",
5279	.map_type = BPF_MAP_TYPE_LRU_HASH,
5280	.map_name = "pprint_test_lru_hash",
5281	.ordered_map = false,
5282	.lossless_map = false,
5283	.percpu_map = false,
5284},
5285
5286{
5287	.descr = "BTF pretty print percpu array",
5288	.map_type = BPF_MAP_TYPE_PERCPU_ARRAY,
5289	.map_name = "pprint_test_percpu_array",
5290	.ordered_map = true,
5291	.lossless_map = true,
5292	.percpu_map = true,
5293},
5294
5295{
5296	.descr = "BTF pretty print percpu hash",
5297	.map_type = BPF_MAP_TYPE_PERCPU_HASH,
5298	.map_name = "pprint_test_percpu_hash",
5299	.ordered_map = false,
5300	.lossless_map = true,
5301	.percpu_map = true,
5302},
5303
5304{
5305	.descr = "BTF pretty print lru percpu hash",
5306	.map_type = BPF_MAP_TYPE_LRU_PERCPU_HASH,
5307	.map_name = "pprint_test_lru_percpu_hash",
5308	.ordered_map = false,
5309	.lossless_map = false,
5310	.percpu_map = true,
5311},
5312
5313};
5314
5315static size_t get_pprint_mapv_size(enum pprint_mapv_kind_t mapv_kind)
5316{
5317	if (mapv_kind == PPRINT_MAPV_KIND_BASIC)
5318		return sizeof(struct pprint_mapv);
5319
5320#ifdef __SIZEOF_INT128__
5321	if (mapv_kind == PPRINT_MAPV_KIND_INT128)
5322		return sizeof(struct pprint_mapv_int128);
5323#endif
5324
5325	assert(0);
5326	return 0;
5327}
5328
5329static void set_pprint_mapv(enum pprint_mapv_kind_t mapv_kind,
5330			    void *mapv, uint32_t i,
5331			    int num_cpus, int rounded_value_size)
5332{
5333	int cpu;
5334
5335	if (mapv_kind == PPRINT_MAPV_KIND_BASIC) {
5336		struct pprint_mapv *v = mapv;
5337
5338		for (cpu = 0; cpu < num_cpus; cpu++) {
5339			v->ui32 = i + cpu;
5340			v->si32 = -i;
5341			v->unused_bits2a = 3;
5342			v->bits28 = i;
5343			v->unused_bits2b = 3;
5344			v->ui64 = i;
5345			v->aenum = i & 0x03;
5346			v->ui32b = 4;
5347			v->bits2c = 1;
5348			v->si8_4[0][0] = (cpu + i) & 0xff;
5349			v->si8_4[0][1] = (cpu + i + 1) & 0xff;
5350			v->si8_4[1][0] = (cpu + i + 2) & 0xff;
5351			v->si8_4[1][1] = (cpu + i + 3) & 0xff;
5352			v = (void *)v + rounded_value_size;
5353		}
5354	}
5355
5356#ifdef __SIZEOF_INT128__
5357	if (mapv_kind == PPRINT_MAPV_KIND_INT128) {
5358		struct pprint_mapv_int128 *v = mapv;
5359
5360		for (cpu = 0; cpu < num_cpus; cpu++) {
5361			v->si128a = i;
5362			v->si128b = -i;
5363			v->bits3 = i & 0x07;
5364			v->bits80 = (((unsigned __int128)1) << 64) + i;
5365			v->ui128 = (((unsigned __int128)2) << 64) + i;
5366			v = (void *)v + rounded_value_size;
5367		}
5368	}
5369#endif
5370}
5371
5372ssize_t get_pprint_expected_line(enum pprint_mapv_kind_t mapv_kind,
5373				 char *expected_line, ssize_t line_size,
5374				 bool percpu_map, unsigned int next_key,
5375				 int cpu, void *mapv)
5376{
5377	ssize_t nexpected_line = -1;
5378
5379	if (mapv_kind == PPRINT_MAPV_KIND_BASIC) {
5380		struct pprint_mapv *v = mapv;
5381
5382		nexpected_line = snprintf(expected_line, line_size,
5383					  "%s%u: {%u,0,%d,0x%x,0x%x,0x%x,"
5384					  "{%llu|[%u,%u,%u,%u,%u,%u,%u,%u]},%s,"
5385					  "%u,0x%x,[[%d,%d],[%d,%d]]}\n",
5386					  percpu_map ? "\tcpu" : "",
5387					  percpu_map ? cpu : next_key,
5388					  v->ui32, v->si32,
5389					  v->unused_bits2a,
5390					  v->bits28,
5391					  v->unused_bits2b,
5392					  (__u64)v->ui64,
5393					  v->ui8a[0], v->ui8a[1],
5394					  v->ui8a[2], v->ui8a[3],
5395					  v->ui8a[4], v->ui8a[5],
5396					  v->ui8a[6], v->ui8a[7],
5397					  pprint_enum_str[v->aenum],
5398					  v->ui32b,
5399					  v->bits2c,
5400					  v->si8_4[0][0], v->si8_4[0][1],
5401					  v->si8_4[1][0], v->si8_4[1][1]);
5402	}
5403
5404#ifdef __SIZEOF_INT128__
5405	if (mapv_kind == PPRINT_MAPV_KIND_INT128) {
5406		struct pprint_mapv_int128 *v = mapv;
5407
5408		nexpected_line = snprintf(expected_line, line_size,
5409					  "%s%u: {0x%lx,0x%lx,0x%lx,"
5410					  "0x%lx%016lx,0x%lx%016lx}\n",
5411					  percpu_map ? "\tcpu" : "",
5412					  percpu_map ? cpu : next_key,
5413					  (uint64_t)v->si128a,
5414					  (uint64_t)v->si128b,
5415					  (uint64_t)v->bits3,
5416					  (uint64_t)(v->bits80 >> 64),
5417					  (uint64_t)v->bits80,
5418					  (uint64_t)(v->ui128 >> 64),
5419					  (uint64_t)v->ui128);
5420	}
5421#endif
5422
5423	return nexpected_line;
5424}
5425
5426static int check_line(const char *expected_line, int nexpected_line,
5427		      int expected_line_len, const char *line)
5428{
5429	if (CHECK(nexpected_line == expected_line_len,
5430		  "expected_line is too long"))
5431		return -1;
5432
5433	if (strcmp(expected_line, line)) {
5434		fprintf(stderr, "unexpected pprint output\n");
5435		fprintf(stderr, "expected: %s", expected_line);
5436		fprintf(stderr, "    read: %s", line);
5437		return -1;
5438	}
5439
5440	return 0;
5441}
5442
5443
5444static void do_test_pprint(int test_num)
5445{
5446	const struct btf_raw_test *test = &pprint_test_template[test_num];
5447	enum pprint_mapv_kind_t mapv_kind = test->mapv_kind;
5448	LIBBPF_OPTS(bpf_map_create_opts, opts);
5449	bool ordered_map, lossless_map, percpu_map;
5450	int err, ret, num_cpus, rounded_value_size;
5451	unsigned int key, nr_read_elems;
5452	int map_fd = -1, btf_fd = -1;
5453	unsigned int raw_btf_size;
5454	char expected_line[255];
5455	FILE *pin_file = NULL;
5456	char pin_path[255];
5457	size_t line_len = 0;
5458	char *line = NULL;
5459	void *mapv = NULL;
5460	uint8_t *raw_btf;
5461	ssize_t nread;
5462
5463	if (!test__start_subtest(test->descr))
5464		return;
5465
5466	raw_btf = btf_raw_create(&hdr_tmpl, test->raw_types,
5467				 test->str_sec, test->str_sec_size,
5468				 &raw_btf_size, NULL);
5469
5470	if (!raw_btf)
5471		return;
5472
5473	*btf_log_buf = '\0';
5474	btf_fd = load_raw_btf(raw_btf, raw_btf_size);
5475	free(raw_btf);
5476
5477	if (CHECK(btf_fd < 0, "errno:%d\n", errno)) {
5478		err = -1;
5479		goto done;
5480	}
5481
5482	opts.btf_fd = btf_fd;
5483	opts.btf_key_type_id = test->key_type_id;
5484	opts.btf_value_type_id = test->value_type_id;
5485	map_fd = bpf_map_create(test->map_type, test->map_name,
5486				test->key_size, test->value_size, test->max_entries, &opts);
5487	if (CHECK(map_fd < 0, "errno:%d", errno)) {
5488		err = -1;
5489		goto done;
5490	}
5491
5492	ret = snprintf(pin_path, sizeof(pin_path), "%s/%s",
5493		       "/sys/fs/bpf", test->map_name);
5494
5495	if (CHECK(ret >= sizeof(pin_path), "pin_path %s/%s is too long",
5496		  "/sys/fs/bpf", test->map_name)) {
5497		err = -1;
5498		goto done;
5499	}
5500
5501	err = bpf_obj_pin(map_fd, pin_path);
5502	if (CHECK(err, "bpf_obj_pin(%s): errno:%d.", pin_path, errno))
5503		goto done;
5504
5505	percpu_map = test->percpu_map;
5506	num_cpus = percpu_map ? bpf_num_possible_cpus() : 1;
5507	rounded_value_size = round_up(get_pprint_mapv_size(mapv_kind), 8);
5508	mapv = calloc(num_cpus, rounded_value_size);
5509	if (CHECK(!mapv, "mapv allocation failure")) {
5510		err = -1;
5511		goto done;
5512	}
5513
5514	for (key = 0; key < test->max_entries; key++) {
5515		set_pprint_mapv(mapv_kind, mapv, key, num_cpus, rounded_value_size);
5516		bpf_map_update_elem(map_fd, &key, mapv, 0);
5517	}
5518
5519	pin_file = fopen(pin_path, "r");
5520	if (CHECK(!pin_file, "fopen(%s): errno:%d", pin_path, errno)) {
5521		err = -1;
5522		goto done;
5523	}
5524
5525	/* Skip lines start with '#' */
5526	while ((nread = getline(&line, &line_len, pin_file)) > 0 &&
5527	       *line == '#')
5528		;
5529
5530	if (CHECK(nread <= 0, "Unexpected EOF")) {
5531		err = -1;
5532		goto done;
5533	}
5534
5535	nr_read_elems = 0;
5536	ordered_map = test->ordered_map;
5537	lossless_map = test->lossless_map;
5538	do {
5539		ssize_t nexpected_line;
5540		unsigned int next_key;
5541		void *cmapv;
5542		int cpu;
5543
5544		next_key = ordered_map ? nr_read_elems : atoi(line);
5545		set_pprint_mapv(mapv_kind, mapv, next_key, num_cpus, rounded_value_size);
5546		cmapv = mapv;
5547
5548		for (cpu = 0; cpu < num_cpus; cpu++) {
5549			if (percpu_map) {
5550				/* for percpu map, the format looks like:
5551				 * <key>: {
5552				 *	cpu0: <value_on_cpu0>
5553				 *	cpu1: <value_on_cpu1>
5554				 *	...
5555				 *	cpun: <value_on_cpun>
5556				 * }
5557				 *
5558				 * let us verify the line containing the key here.
5559				 */
5560				if (cpu == 0) {
5561					nexpected_line = snprintf(expected_line,
5562								  sizeof(expected_line),
5563								  "%u: {\n",
5564								  next_key);
5565
5566					err = check_line(expected_line, nexpected_line,
5567							 sizeof(expected_line), line);
5568					if (err < 0)
5569						goto done;
5570				}
5571
5572				/* read value@cpu */
5573				nread = getline(&line, &line_len, pin_file);
5574				if (nread < 0)
5575					break;
5576			}
5577
5578			nexpected_line = get_pprint_expected_line(mapv_kind, expected_line,
5579								  sizeof(expected_line),
5580								  percpu_map, next_key,
5581								  cpu, cmapv);
5582			err = check_line(expected_line, nexpected_line,
5583					 sizeof(expected_line), line);
5584			if (err < 0)
5585				goto done;
5586
5587			cmapv = cmapv + rounded_value_size;
5588		}
5589
5590		if (percpu_map) {
5591			/* skip the last bracket for the percpu map */
5592			nread = getline(&line, &line_len, pin_file);
5593			if (nread < 0)
5594				break;
5595		}
5596
5597		nread = getline(&line, &line_len, pin_file);
5598	} while (++nr_read_elems < test->max_entries && nread > 0);
5599
5600	if (lossless_map &&
5601	    CHECK(nr_read_elems < test->max_entries,
5602		  "Unexpected EOF. nr_read_elems:%u test->max_entries:%u",
5603		  nr_read_elems, test->max_entries)) {
5604		err = -1;
5605		goto done;
5606	}
5607
5608	if (CHECK(nread > 0, "Unexpected extra pprint output: %s", line)) {
5609		err = -1;
5610		goto done;
5611	}
5612
5613	err = 0;
5614
5615done:
5616	if (mapv)
5617		free(mapv);
5618	if (!err)
5619		fprintf(stderr, "OK");
5620	if (*btf_log_buf && (err || always_log))
5621		fprintf(stderr, "\n%s", btf_log_buf);
5622	if (btf_fd >= 0)
5623		close(btf_fd);
5624	if (map_fd >= 0)
5625		close(map_fd);
5626	if (pin_file)
5627		fclose(pin_file);
5628	unlink(pin_path);
5629	free(line);
5630}
5631
5632static void test_pprint(void)
5633{
5634	unsigned int i;
5635
5636	/* test various maps with the first test template */
5637	for (i = 0; i < ARRAY_SIZE(pprint_tests_meta); i++) {
5638		pprint_test_template[0].descr = pprint_tests_meta[i].descr;
5639		pprint_test_template[0].map_type = pprint_tests_meta[i].map_type;
5640		pprint_test_template[0].map_name = pprint_tests_meta[i].map_name;
5641		pprint_test_template[0].ordered_map = pprint_tests_meta[i].ordered_map;
5642		pprint_test_template[0].lossless_map = pprint_tests_meta[i].lossless_map;
5643		pprint_test_template[0].percpu_map = pprint_tests_meta[i].percpu_map;
5644
5645		do_test_pprint(0);
5646	}
5647
5648	/* test rest test templates with the first map */
5649	for (i = 1; i < ARRAY_SIZE(pprint_test_template); i++) {
5650		pprint_test_template[i].descr = pprint_tests_meta[0].descr;
5651		pprint_test_template[i].map_type = pprint_tests_meta[0].map_type;
5652		pprint_test_template[i].map_name = pprint_tests_meta[0].map_name;
5653		pprint_test_template[i].ordered_map = pprint_tests_meta[0].ordered_map;
5654		pprint_test_template[i].lossless_map = pprint_tests_meta[0].lossless_map;
5655		pprint_test_template[i].percpu_map = pprint_tests_meta[0].percpu_map;
5656		do_test_pprint(i);
5657	}
5658}
5659
5660#define BPF_LINE_INFO_ENC(insn_off, file_off, line_off, line_num, line_col) \
5661	(insn_off), (file_off), (line_off), ((line_num) << 10 | ((line_col) & 0x3ff))
5662
5663static struct prog_info_raw_test {
5664	const char *descr;
5665	const char *str_sec;
5666	const char *err_str;
5667	__u32 raw_types[MAX_NR_RAW_U32];
5668	__u32 str_sec_size;
5669	struct bpf_insn insns[MAX_INSNS];
5670	__u32 prog_type;
5671	__u32 func_info[MAX_SUBPROGS][2];
5672	__u32 func_info_rec_size;
5673	__u32 func_info_cnt;
5674	__u32 line_info[MAX_NR_RAW_U32];
5675	__u32 line_info_rec_size;
5676	__u32 nr_jited_ksyms;
5677	bool expected_prog_load_failure;
5678	__u32 dead_code_cnt;
5679	__u32 dead_code_mask;
5680	__u32 dead_func_cnt;
5681	__u32 dead_func_mask;
5682} info_raw_tests[] = {
5683{
5684	.descr = "func_type (main func + one sub)",
5685	.raw_types = {
5686		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5687		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),	/* [2] */
5688		BTF_FUNC_PROTO_ENC(1, 2),			/* [3] */
5689			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5690			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5691		BTF_FUNC_PROTO_ENC(1, 2),			/* [4] */
5692			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5693			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5694		BTF_FUNC_ENC(NAME_TBD, 3),			/* [5] */
5695		BTF_FUNC_ENC(NAME_TBD, 4),			/* [6] */
5696		BTF_END_RAW,
5697	},
5698	.str_sec = "\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB",
5699	.str_sec_size = sizeof("\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB"),
5700	.insns = {
5701		BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
5702		BPF_MOV64_IMM(BPF_REG_0, 1),
5703		BPF_EXIT_INSN(),
5704		BPF_MOV64_IMM(BPF_REG_0, 2),
5705		BPF_EXIT_INSN(),
5706	},
5707	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5708	.func_info = { {0, 5}, {3, 6} },
5709	.func_info_rec_size = 8,
5710	.func_info_cnt = 2,
5711	.line_info = { BTF_END_RAW },
5712},
5713
5714{
5715	.descr = "func_type (Incorrect func_info_rec_size)",
5716	.raw_types = {
5717		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5718		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),	/* [2] */
5719		BTF_FUNC_PROTO_ENC(1, 2),			/* [3] */
5720			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5721			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5722		BTF_FUNC_PROTO_ENC(1, 2),			/* [4] */
5723			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5724			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5725		BTF_FUNC_ENC(NAME_TBD, 3),			/* [5] */
5726		BTF_FUNC_ENC(NAME_TBD, 4),			/* [6] */
5727		BTF_END_RAW,
5728	},
5729	.str_sec = "\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB",
5730	.str_sec_size = sizeof("\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB"),
5731	.insns = {
5732		BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
5733		BPF_MOV64_IMM(BPF_REG_0, 1),
5734		BPF_EXIT_INSN(),
5735		BPF_MOV64_IMM(BPF_REG_0, 2),
5736		BPF_EXIT_INSN(),
5737	},
5738	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5739	.func_info = { {0, 5}, {3, 6} },
5740	.func_info_rec_size = 4,
5741	.func_info_cnt = 2,
5742	.line_info = { BTF_END_RAW },
5743	.expected_prog_load_failure = true,
5744},
5745
5746{
5747	.descr = "func_type (Incorrect func_info_cnt)",
5748	.raw_types = {
5749		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5750		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),	/* [2] */
5751		BTF_FUNC_PROTO_ENC(1, 2),			/* [3] */
5752			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5753			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5754		BTF_FUNC_PROTO_ENC(1, 2),			/* [4] */
5755			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5756			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5757		BTF_FUNC_ENC(NAME_TBD, 3),			/* [5] */
5758		BTF_FUNC_ENC(NAME_TBD, 4),			/* [6] */
5759		BTF_END_RAW,
5760	},
5761	.str_sec = "\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB",
5762	.str_sec_size = sizeof("\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB"),
5763	.insns = {
5764		BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
5765		BPF_MOV64_IMM(BPF_REG_0, 1),
5766		BPF_EXIT_INSN(),
5767		BPF_MOV64_IMM(BPF_REG_0, 2),
5768		BPF_EXIT_INSN(),
5769	},
5770	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5771	.func_info = { {0, 5}, {3, 6} },
5772	.func_info_rec_size = 8,
5773	.func_info_cnt = 1,
5774	.line_info = { BTF_END_RAW },
5775	.expected_prog_load_failure = true,
5776},
5777
5778{
5779	.descr = "func_type (Incorrect bpf_func_info.insn_off)",
5780	.raw_types = {
5781		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5782		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),	/* [2] */
5783		BTF_FUNC_PROTO_ENC(1, 2),			/* [3] */
5784			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5785			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5786		BTF_FUNC_PROTO_ENC(1, 2),			/* [4] */
5787			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5788			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5789		BTF_FUNC_ENC(NAME_TBD, 3),			/* [5] */
5790		BTF_FUNC_ENC(NAME_TBD, 4),			/* [6] */
5791		BTF_END_RAW,
5792	},
5793	.str_sec = "\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB",
5794	.str_sec_size = sizeof("\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB"),
5795	.insns = {
5796		BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
5797		BPF_MOV64_IMM(BPF_REG_0, 1),
5798		BPF_EXIT_INSN(),
5799		BPF_MOV64_IMM(BPF_REG_0, 2),
5800		BPF_EXIT_INSN(),
5801	},
5802	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5803	.func_info = { {0, 5}, {2, 6} },
5804	.func_info_rec_size = 8,
5805	.func_info_cnt = 2,
5806	.line_info = { BTF_END_RAW },
5807	.expected_prog_load_failure = true,
5808},
5809
5810{
5811	.descr = "line_info (No subprog)",
5812	.raw_types = {
5813		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5814		BTF_END_RAW,
5815	},
5816	BTF_STR_SEC("\0int\0int a=1;\0int b=2;\0return a + b;\0return a + b;"),
5817	.insns = {
5818		BPF_MOV64_IMM(BPF_REG_0, 1),
5819		BPF_MOV64_IMM(BPF_REG_1, 2),
5820		BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
5821		BPF_EXIT_INSN(),
5822	},
5823	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5824	.func_info_cnt = 0,
5825	.line_info = {
5826		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5827		BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 9),
5828		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8),
5829		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 7),
5830		BTF_END_RAW,
5831	},
5832	.line_info_rec_size = sizeof(struct bpf_line_info),
5833	.nr_jited_ksyms = 1,
5834},
5835
5836{
5837	.descr = "line_info (No subprog. insn_off >= prog->len)",
5838	.raw_types = {
5839		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5840		BTF_END_RAW,
5841	},
5842	BTF_STR_SEC("\0int\0int a=1;\0int b=2;\0return a + b;\0return a + b;"),
5843	.insns = {
5844		BPF_MOV64_IMM(BPF_REG_0, 1),
5845		BPF_MOV64_IMM(BPF_REG_1, 2),
5846		BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
5847		BPF_EXIT_INSN(),
5848	},
5849	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5850	.func_info_cnt = 0,
5851	.line_info = {
5852		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5853		BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 9),
5854		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8),
5855		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 7),
5856		BPF_LINE_INFO_ENC(4, 0, 0, 5, 6),
5857		BTF_END_RAW,
5858	},
5859	.line_info_rec_size = sizeof(struct bpf_line_info),
5860	.nr_jited_ksyms = 1,
5861	.err_str = "line_info[4].insn_off",
5862	.expected_prog_load_failure = true,
5863},
5864
5865{
5866	.descr = "line_info (Zero bpf insn code)",
5867	.raw_types = {
5868		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5869		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 64, 8),	/* [2] */
5870		BTF_TYPEDEF_ENC(NAME_TBD, 2),			/* [3] */
5871		BTF_END_RAW,
5872	},
5873	BTF_STR_SEC("\0int\0unsigned long\0u64\0u64 a=1;\0return a;"),
5874	.insns = {
5875		BPF_LD_IMM64(BPF_REG_0, 1),
5876		BPF_EXIT_INSN(),
5877	},
5878	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5879	.func_info_cnt = 0,
5880	.line_info = {
5881		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5882		BPF_LINE_INFO_ENC(1, 0, 0, 2, 9),
5883		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8),
5884		BTF_END_RAW,
5885	},
5886	.line_info_rec_size = sizeof(struct bpf_line_info),
5887	.nr_jited_ksyms = 1,
5888	.err_str = "Invalid insn code at line_info[1]",
5889	.expected_prog_load_failure = true,
5890},
5891
5892{
5893	.descr = "line_info (No subprog. zero tailing line_info",
5894	.raw_types = {
5895		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5896		BTF_END_RAW,
5897	},
5898	BTF_STR_SEC("\0int\0int a=1;\0int b=2;\0return a + b;\0return a + b;"),
5899	.insns = {
5900		BPF_MOV64_IMM(BPF_REG_0, 1),
5901		BPF_MOV64_IMM(BPF_REG_1, 2),
5902		BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
5903		BPF_EXIT_INSN(),
5904	},
5905	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5906	.func_info_cnt = 0,
5907	.line_info = {
5908		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10), 0,
5909		BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 9), 0,
5910		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8), 0,
5911		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 7), 0,
5912		BTF_END_RAW,
5913	},
5914	.line_info_rec_size = sizeof(struct bpf_line_info) + sizeof(__u32),
5915	.nr_jited_ksyms = 1,
5916},
5917
5918{
5919	.descr = "line_info (No subprog. nonzero tailing line_info)",
5920	.raw_types = {
5921		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5922		BTF_END_RAW,
5923	},
5924	BTF_STR_SEC("\0int\0int a=1;\0int b=2;\0return a + b;\0return a + b;"),
5925	.insns = {
5926		BPF_MOV64_IMM(BPF_REG_0, 1),
5927		BPF_MOV64_IMM(BPF_REG_1, 2),
5928		BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
5929		BPF_EXIT_INSN(),
5930	},
5931	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5932	.func_info_cnt = 0,
5933	.line_info = {
5934		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10), 0,
5935		BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 9), 0,
5936		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8), 0,
5937		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 7), 1,
5938		BTF_END_RAW,
5939	},
5940	.line_info_rec_size = sizeof(struct bpf_line_info) + sizeof(__u32),
5941	.nr_jited_ksyms = 1,
5942	.err_str = "nonzero tailing record in line_info",
5943	.expected_prog_load_failure = true,
5944},
5945
5946{
5947	.descr = "line_info (subprog)",
5948	.raw_types = {
5949		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5950		BTF_END_RAW,
5951	},
5952	BTF_STR_SEC("\0int\0int a=1+1;\0return func(a);\0b+=1;\0return b;"),
5953	.insns = {
5954		BPF_MOV64_IMM(BPF_REG_2, 1),
5955		BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
5956		BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
5957		BPF_CALL_REL(1),
5958		BPF_EXIT_INSN(),
5959		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
5960		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
5961		BPF_EXIT_INSN(),
5962	},
5963	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5964	.func_info_cnt = 0,
5965	.line_info = {
5966		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5967		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 2, 9),
5968		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 3, 8),
5969		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 4, 7),
5970		BTF_END_RAW,
5971	},
5972	.line_info_rec_size = sizeof(struct bpf_line_info),
5973	.nr_jited_ksyms = 2,
5974},
5975
5976{
5977	.descr = "line_info (subprog + func_info)",
5978	.raw_types = {
5979		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5980		BTF_FUNC_PROTO_ENC(1, 1),			/* [2] */
5981			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5982		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
5983		BTF_FUNC_ENC(NAME_TBD, 2),			/* [4] */
5984		BTF_END_RAW,
5985	},
5986	BTF_STR_SEC("\0int\0x\0sub\0main\0int a=1+1;\0return func(a);\0b+=1;\0return b;"),
5987	.insns = {
5988		BPF_MOV64_IMM(BPF_REG_2, 1),
5989		BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
5990		BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
5991		BPF_CALL_REL(1),
5992		BPF_EXIT_INSN(),
5993		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
5994		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
5995		BPF_EXIT_INSN(),
5996	},
5997	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5998	.func_info_cnt = 2,
5999	.func_info_rec_size = 8,
6000	.func_info = { {0, 4}, {5, 3} },
6001	.line_info = {
6002		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
6003		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 2, 9),
6004		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 3, 8),
6005		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 4, 7),
6006		BTF_END_RAW,
6007	},
6008	.line_info_rec_size = sizeof(struct bpf_line_info),
6009	.nr_jited_ksyms = 2,
6010},
6011
6012{
6013	.descr = "line_info (subprog. missing 1st func line info)",
6014	.raw_types = {
6015		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6016		BTF_END_RAW,
6017	},
6018	BTF_STR_SEC("\0int\0int a=1+1;\0return func(a);\0b+=1;\0return b;"),
6019	.insns = {
6020		BPF_MOV64_IMM(BPF_REG_2, 1),
6021		BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
6022		BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
6023		BPF_CALL_REL(1),
6024		BPF_EXIT_INSN(),
6025		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
6026		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
6027		BPF_EXIT_INSN(),
6028	},
6029	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
6030	.func_info_cnt = 0,
6031	.line_info = {
6032		BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 1, 10),
6033		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 2, 9),
6034		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 3, 8),
6035		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 4, 7),
6036		BTF_END_RAW,
6037	},
6038	.line_info_rec_size = sizeof(struct bpf_line_info),
6039	.nr_jited_ksyms = 2,
6040	.err_str = "missing bpf_line_info for func#0",
6041	.expected_prog_load_failure = true,
6042},
6043
6044{
6045	.descr = "line_info (subprog. missing 2nd func line info)",
6046	.raw_types = {
6047		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6048		BTF_END_RAW,
6049	},
6050	BTF_STR_SEC("\0int\0int a=1+1;\0return func(a);\0b+=1;\0return b;"),
6051	.insns = {
6052		BPF_MOV64_IMM(BPF_REG_2, 1),
6053		BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
6054		BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
6055		BPF_CALL_REL(1),
6056		BPF_EXIT_INSN(),
6057		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
6058		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
6059		BPF_EXIT_INSN(),
6060	},
6061	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
6062	.func_info_cnt = 0,
6063	.line_info = {
6064		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
6065		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 2, 9),
6066		BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 3, 8),
6067		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 4, 7),
6068		BTF_END_RAW,
6069	},
6070	.line_info_rec_size = sizeof(struct bpf_line_info),
6071	.nr_jited_ksyms = 2,
6072	.err_str = "missing bpf_line_info for func#1",
6073	.expected_prog_load_failure = true,
6074},
6075
6076{
6077	.descr = "line_info (subprog. unordered insn offset)",
6078	.raw_types = {
6079		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6080		BTF_END_RAW,
6081	},
6082	BTF_STR_SEC("\0int\0int a=1+1;\0return func(a);\0b+=1;\0return b;"),
6083	.insns = {
6084		BPF_MOV64_IMM(BPF_REG_2, 1),
6085		BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
6086		BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
6087		BPF_CALL_REL(1),
6088		BPF_EXIT_INSN(),
6089		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
6090		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
6091		BPF_EXIT_INSN(),
6092	},
6093	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
6094	.func_info_cnt = 0,
6095	.line_info = {
6096		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
6097		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 2, 9),
6098		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8),
6099		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 4, 7),
6100		BTF_END_RAW,
6101	},
6102	.line_info_rec_size = sizeof(struct bpf_line_info),
6103	.nr_jited_ksyms = 2,
6104	.err_str = "Invalid line_info[2].insn_off",
6105	.expected_prog_load_failure = true,
6106},
6107
6108{
6109	.descr = "line_info (dead start)",
6110	.raw_types = {
6111		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6112		BTF_END_RAW,
6113	},
6114	BTF_STR_SEC("\0int\0/* dead jmp */\0int a=1;\0int b=2;\0return a + b;\0return a + b;"),
6115	.insns = {
6116		BPF_JMP_IMM(BPF_JA, 0, 0, 0),
6117		BPF_MOV64_IMM(BPF_REG_0, 1),
6118		BPF_MOV64_IMM(BPF_REG_1, 2),
6119		BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
6120		BPF_EXIT_INSN(),
6121	},
6122	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
6123	.func_info_cnt = 0,
6124	.line_info = {
6125		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
6126		BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 9),
6127		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8),
6128		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 7),
6129		BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 5, 6),
6130		BTF_END_RAW,
6131	},
6132	.line_info_rec_size = sizeof(struct bpf_line_info),
6133	.nr_jited_ksyms = 1,
6134	.dead_code_cnt = 1,
6135	.dead_code_mask = 0x01,
6136},
6137
6138{
6139	.descr = "line_info (dead end)",
6140	.raw_types = {
6141		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6142		BTF_END_RAW,
6143	},
6144	BTF_STR_SEC("\0int\0int a=1;\0int b=2;\0return a + b;\0/* dead jmp */\0return a + b;\0/* dead exit */"),
6145	.insns = {
6146		BPF_MOV64_IMM(BPF_REG_0, 1),
6147		BPF_MOV64_IMM(BPF_REG_1, 2),
6148		BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
6149		BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 10, 1),
6150		BPF_EXIT_INSN(),
6151		BPF_EXIT_INSN(),
6152	},
6153	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
6154	.func_info_cnt = 0,
6155	.line_info = {
6156		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 12),
6157		BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 11),
6158		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 10),
6159		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 9),
6160		BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 5, 8),
6161		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 6, 7),
6162		BTF_END_RAW,
6163	},
6164	.line_info_rec_size = sizeof(struct bpf_line_info),
6165	.nr_jited_ksyms = 1,
6166	.dead_code_cnt = 2,
6167	.dead_code_mask = 0x28,
6168},
6169
6170{
6171	.descr = "line_info (dead code + subprog + func_info)",
6172	.raw_types = {
6173		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6174		BTF_FUNC_PROTO_ENC(1, 1),			/* [2] */
6175			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
6176		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
6177		BTF_FUNC_ENC(NAME_TBD, 2),			/* [4] */
6178		BTF_END_RAW,
6179	},
6180	BTF_STR_SEC("\0int\0x\0sub\0main\0int a=1+1;\0/* dead jmp */"
6181		    "\0/* dead */\0/* dead */\0/* dead */\0/* dead */"
6182		    "\0/* dead */\0/* dead */\0/* dead */\0/* dead */"
6183		    "\0return func(a);\0b+=1;\0return b;"),
6184	.insns = {
6185		BPF_MOV64_IMM(BPF_REG_2, 1),
6186		BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
6187		BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
6188		BPF_JMP_IMM(BPF_JGE, BPF_REG_2, 0, 8),
6189		BPF_MOV64_IMM(BPF_REG_2, 1),
6190		BPF_MOV64_IMM(BPF_REG_2, 1),
6191		BPF_MOV64_IMM(BPF_REG_2, 1),
6192		BPF_MOV64_IMM(BPF_REG_2, 1),
6193		BPF_MOV64_IMM(BPF_REG_2, 1),
6194		BPF_MOV64_IMM(BPF_REG_2, 1),
6195		BPF_MOV64_IMM(BPF_REG_2, 1),
6196		BPF_MOV64_IMM(BPF_REG_2, 1),
6197		BPF_CALL_REL(1),
6198		BPF_EXIT_INSN(),
6199		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
6200		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
6201		BPF_EXIT_INSN(),
6202	},
6203	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
6204	.func_info_cnt = 2,
6205	.func_info_rec_size = 8,
6206	.func_info = { {0, 4}, {14, 3} },
6207	.line_info = {
6208		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
6209		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 1, 10),
6210		BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 1, 10),
6211		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 1, 10),
6212		BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
6213		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 1, 10),
6214		BPF_LINE_INFO_ENC(8, 0, NAME_TBD, 1, 10),
6215		BPF_LINE_INFO_ENC(9, 0, NAME_TBD, 1, 10),
6216		BPF_LINE_INFO_ENC(10, 0, NAME_TBD, 1, 10),
6217		BPF_LINE_INFO_ENC(11, 0, NAME_TBD, 2, 9),
6218		BPF_LINE_INFO_ENC(12, 0, NAME_TBD, 2, 9),
6219		BPF_LINE_INFO_ENC(14, 0, NAME_TBD, 3, 8),
6220		BPF_LINE_INFO_ENC(16, 0, NAME_TBD, 4, 7),
6221		BTF_END_RAW,
6222	},
6223	.line_info_rec_size = sizeof(struct bpf_line_info),
6224	.nr_jited_ksyms = 2,
6225	.dead_code_cnt = 9,
6226	.dead_code_mask = 0x3fe,
6227},
6228
6229{
6230	.descr = "line_info (dead subprog)",
6231	.raw_types = {
6232		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6233		BTF_FUNC_PROTO_ENC(1, 1),			/* [2] */
6234			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
6235		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
6236		BTF_FUNC_ENC(NAME_TBD, 2),			/* [4] */
6237		BTF_FUNC_ENC(NAME_TBD, 2),			/* [5] */
6238		BTF_END_RAW,
6239	},
6240	BTF_STR_SEC("\0int\0x\0dead\0main\0func\0int a=1+1;\0/* live call */"
6241		    "\0return 0;\0return 0;\0/* dead */\0/* dead */"
6242		    "\0/* dead */\0return bla + 1;\0return bla + 1;"
6243		    "\0return bla + 1;\0return func(a);\0b+=1;\0return b;"),
6244	.insns = {
6245		BPF_MOV64_IMM(BPF_REG_2, 1),
6246		BPF_JMP_IMM(BPF_JGE, BPF_REG_2, 0, 1),
6247		BPF_CALL_REL(3),
6248		BPF_CALL_REL(5),
6249		BPF_MOV64_IMM(BPF_REG_0, 0),
6250		BPF_EXIT_INSN(),
6251		BPF_MOV64_IMM(BPF_REG_0, 0),
6252		BPF_CALL_REL(1),
6253		BPF_EXIT_INSN(),
6254		BPF_MOV64_REG(BPF_REG_0, 2),
6255		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
6256		BPF_EXIT_INSN(),
6257	},
6258	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
6259	.func_info_cnt = 3,
6260	.func_info_rec_size = 8,
6261		.func_info = { {0, 4}, {6, 3}, {9, 5} },
6262	.line_info = {
6263		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
6264		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 1, 10),
6265		BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 1, 10),
6266		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 1, 10),
6267		BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
6268		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 1, 10),
6269		BPF_LINE_INFO_ENC(8, 0, NAME_TBD, 1, 10),
6270		BPF_LINE_INFO_ENC(9, 0, NAME_TBD, 1, 10),
6271		BPF_LINE_INFO_ENC(10, 0, NAME_TBD, 1, 10),
6272		BPF_LINE_INFO_ENC(11, 0, NAME_TBD, 2, 9),
6273		BTF_END_RAW,
6274	},
6275	.line_info_rec_size = sizeof(struct bpf_line_info),
6276	.nr_jited_ksyms = 2,
6277	.dead_code_cnt = 3,
6278	.dead_code_mask = 0x70,
6279	.dead_func_cnt = 1,
6280	.dead_func_mask = 0x2,
6281},
6282
6283{
6284	.descr = "line_info (dead last subprog)",
6285	.raw_types = {
6286		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6287		BTF_FUNC_PROTO_ENC(1, 1),			/* [2] */
6288			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
6289		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
6290		BTF_FUNC_ENC(NAME_TBD, 2),			/* [5] */
6291		BTF_END_RAW,
6292	},
6293	BTF_STR_SEC("\0int\0x\0dead\0main\0int a=1+1;\0/* live call */"
6294		    "\0return 0;\0/* dead */\0/* dead */"),
6295	.insns = {
6296		BPF_MOV64_IMM(BPF_REG_2, 1),
6297		BPF_JMP_IMM(BPF_JGE, BPF_REG_2, 0, 1),
6298		BPF_CALL_REL(2),
6299		BPF_MOV64_IMM(BPF_REG_0, 0),
6300		BPF_EXIT_INSN(),
6301		BPF_MOV64_IMM(BPF_REG_0, 0),
6302		BPF_EXIT_INSN(),
6303	},
6304	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
6305	.func_info_cnt = 2,
6306	.func_info_rec_size = 8,
6307		.func_info = { {0, 4}, {5, 3} },
6308	.line_info = {
6309		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
6310		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 1, 10),
6311		BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 1, 10),
6312		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 1, 10),
6313		BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
6314		BTF_END_RAW,
6315	},
6316	.line_info_rec_size = sizeof(struct bpf_line_info),
6317	.nr_jited_ksyms = 1,
6318	.dead_code_cnt = 2,
6319	.dead_code_mask = 0x18,
6320	.dead_func_cnt = 1,
6321	.dead_func_mask = 0x2,
6322},
6323
6324{
6325	.descr = "line_info (dead subprog + dead start)",
6326	.raw_types = {
6327		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6328		BTF_FUNC_PROTO_ENC(1, 1),			/* [2] */
6329			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
6330		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
6331		BTF_FUNC_ENC(NAME_TBD, 2),			/* [4] */
6332		BTF_FUNC_ENC(NAME_TBD, 2),			/* [5] */
6333		BTF_END_RAW,
6334	},
6335	BTF_STR_SEC("\0int\0x\0dead\0main\0func\0int a=1+1;\0/* dead */"
6336		    "\0return 0;\0return 0;\0return 0;"
6337		    "\0/* dead */\0/* dead */\0/* dead */\0/* dead */"
6338		    "\0return b + 1;\0return b + 1;\0return b + 1;"),
6339	.insns = {
6340		BPF_JMP_IMM(BPF_JA, 0, 0, 0),
6341		BPF_MOV64_IMM(BPF_REG_2, 1),
6342		BPF_JMP_IMM(BPF_JGE, BPF_REG_2, 0, 1),
6343		BPF_CALL_REL(3),
6344		BPF_CALL_REL(5),
6345		BPF_MOV64_IMM(BPF_REG_0, 0),
6346		BPF_EXIT_INSN(),
6347		BPF_MOV64_IMM(BPF_REG_0, 0),
6348		BPF_CALL_REL(1),
6349		BPF_EXIT_INSN(),
6350		BPF_JMP_IMM(BPF_JA, 0, 0, 0),
6351		BPF_MOV64_REG(BPF_REG_0, 2),
6352		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
6353		BPF_EXIT_INSN(),
6354	},
6355	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
6356	.func_info_cnt = 3,
6357	.func_info_rec_size = 8,
6358		.func_info = { {0, 4}, {7, 3}, {10, 5} },
6359	.line_info = {
6360		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
6361		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 1, 10),
6362		BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 1, 10),
6363		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 1, 10),
6364		BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
6365		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 1, 10),
6366		BPF_LINE_INFO_ENC(8, 0, NAME_TBD, 1, 10),
6367		BPF_LINE_INFO_ENC(9, 0, NAME_TBD, 1, 10),
6368		BPF_LINE_INFO_ENC(10, 0, NAME_TBD, 1, 10),
6369		BPF_LINE_INFO_ENC(11, 0, NAME_TBD, 2, 9),
6370		BPF_LINE_INFO_ENC(12, 0, NAME_TBD, 2, 9),
6371		BPF_LINE_INFO_ENC(13, 0, NAME_TBD, 2, 9),
6372		BTF_END_RAW,
6373	},
6374	.line_info_rec_size = sizeof(struct bpf_line_info),
6375	.nr_jited_ksyms = 2,
6376	.dead_code_cnt = 5,
6377	.dead_code_mask = 0x1e2,
6378	.dead_func_cnt = 1,
6379	.dead_func_mask = 0x2,
6380},
6381
6382{
6383	.descr = "line_info (dead subprog + dead start w/ move)",
6384	.raw_types = {
6385		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6386		BTF_FUNC_PROTO_ENC(1, 1),			/* [2] */
6387			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
6388		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
6389		BTF_FUNC_ENC(NAME_TBD, 2),			/* [4] */
6390		BTF_FUNC_ENC(NAME_TBD, 2),			/* [5] */
6391		BTF_END_RAW,
6392	},
6393	BTF_STR_SEC("\0int\0x\0dead\0main\0func\0int a=1+1;\0/* live call */"
6394		    "\0return 0;\0return 0;\0/* dead */\0/* dead */"
6395		    "\0/* dead */\0return bla + 1;\0return bla + 1;"
6396		    "\0return bla + 1;\0return func(a);\0b+=1;\0return b;"),
6397	.insns = {
6398		BPF_MOV64_IMM(BPF_REG_2, 1),
6399		BPF_JMP_IMM(BPF_JGE, BPF_REG_2, 0, 1),
6400		BPF_CALL_REL(3),
6401		BPF_CALL_REL(5),
6402		BPF_MOV64_IMM(BPF_REG_0, 0),
6403		BPF_EXIT_INSN(),
6404		BPF_MOV64_IMM(BPF_REG_0, 0),
6405		BPF_CALL_REL(1),
6406		BPF_EXIT_INSN(),
6407		BPF_JMP_IMM(BPF_JA, 0, 0, 0),
6408		BPF_MOV64_REG(BPF_REG_0, 2),
6409		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
6410		BPF_EXIT_INSN(),
6411	},
6412	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
6413	.func_info_cnt = 3,
6414	.func_info_rec_size = 8,
6415		.func_info = { {0, 4}, {6, 3}, {9, 5} },
6416	.line_info = {
6417		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
6418		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 1, 10),
6419		BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 1, 10),
6420		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 1, 10),
6421		BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
6422		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 1, 10),
6423		BPF_LINE_INFO_ENC(8, 0, NAME_TBD, 1, 10),
6424		BPF_LINE_INFO_ENC(9, 0, NAME_TBD, 1, 10),
6425		BPF_LINE_INFO_ENC(11, 0, NAME_TBD, 1, 10),
6426		BPF_LINE_INFO_ENC(12, 0, NAME_TBD, 2, 9),
6427		BTF_END_RAW,
6428	},
6429	.line_info_rec_size = sizeof(struct bpf_line_info),
6430	.nr_jited_ksyms = 2,
6431	.dead_code_cnt = 3,
6432	.dead_code_mask = 0x70,
6433	.dead_func_cnt = 1,
6434	.dead_func_mask = 0x2,
6435},
6436
6437{
6438	.descr = "line_info (dead end + subprog start w/ no linfo)",
6439	.raw_types = {
6440		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6441		BTF_FUNC_PROTO_ENC(1, 1),			/* [2] */
6442			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
6443		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
6444		BTF_FUNC_ENC(NAME_TBD, 2),			/* [4] */
6445		BTF_END_RAW,
6446	},
6447	BTF_STR_SEC("\0int\0x\0main\0func\0/* main linfo */\0/* func linfo */"),
6448	.insns = {
6449		BPF_MOV64_IMM(BPF_REG_0, 0),
6450		BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 1, 3),
6451		BPF_CALL_REL(3),
6452		BPF_MOV64_IMM(BPF_REG_0, 0),
6453		BPF_EXIT_INSN(),
6454		BPF_EXIT_INSN(),
6455		BPF_JMP_IMM(BPF_JA, 0, 0, 0),
6456		BPF_EXIT_INSN(),
6457	},
6458	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
6459	.func_info_cnt = 2,
6460	.func_info_rec_size = 8,
6461	.func_info = { {0, 3}, {6, 4}, },
6462	.line_info = {
6463		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
6464		BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
6465		BTF_END_RAW,
6466	},
6467	.line_info_rec_size = sizeof(struct bpf_line_info),
6468	.nr_jited_ksyms = 2,
6469},
6470
6471};
6472
6473static size_t probe_prog_length(const struct bpf_insn *fp)
6474{
6475	size_t len;
6476
6477	for (len = MAX_INSNS - 1; len > 0; --len)
6478		if (fp[len].code != 0 || fp[len].imm != 0)
6479			break;
6480	return len + 1;
6481}
6482
6483static __u32 *patch_name_tbd(const __u32 *raw_u32,
6484			     const char *str, __u32 str_off,
6485			     unsigned int str_sec_size,
6486			     unsigned int *ret_size)
6487{
6488	int i, raw_u32_size = get_raw_sec_size(raw_u32);
6489	const char *end_str = str + str_sec_size;
6490	const char *next_str = str + str_off;
6491	__u32 *new_u32 = NULL;
6492
6493	if (raw_u32_size == -1)
6494		return ERR_PTR(-EINVAL);
6495
6496	if (!raw_u32_size) {
6497		*ret_size = 0;
6498		return NULL;
6499	}
6500
6501	new_u32 = malloc(raw_u32_size);
6502	if (!new_u32)
6503		return ERR_PTR(-ENOMEM);
6504
6505	for (i = 0; i < raw_u32_size / sizeof(raw_u32[0]); i++) {
6506		if (raw_u32[i] == NAME_TBD) {
6507			next_str = get_next_str(next_str, end_str);
6508			if (CHECK(!next_str, "Error in getting next_str\n")) {
6509				free(new_u32);
6510				return ERR_PTR(-EINVAL);
6511			}
6512			new_u32[i] = next_str - str;
6513			next_str += strlen(next_str);
6514		} else {
6515			new_u32[i] = raw_u32[i];
6516		}
6517	}
6518
6519	*ret_size = raw_u32_size;
6520	return new_u32;
6521}
6522
6523static int test_get_finfo(const struct prog_info_raw_test *test,
6524			  int prog_fd)
6525{
6526	struct bpf_prog_info info = {};
6527	struct bpf_func_info *finfo;
6528	__u32 info_len, rec_size, i;
6529	void *func_info = NULL;
6530	__u32 nr_func_info;
6531	int err;
6532
6533	/* get necessary lens */
6534	info_len = sizeof(struct bpf_prog_info);
6535	err = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len);
6536	if (CHECK(err < 0, "invalid get info (1st) errno:%d", errno)) {
6537		fprintf(stderr, "%s\n", btf_log_buf);
6538		return -1;
6539	}
6540	nr_func_info = test->func_info_cnt - test->dead_func_cnt;
6541	if (CHECK(info.nr_func_info != nr_func_info,
6542		  "incorrect info.nr_func_info (1st) %d",
6543		  info.nr_func_info)) {
6544		return -1;
6545	}
6546
6547	rec_size = info.func_info_rec_size;
6548	if (CHECK(rec_size != sizeof(struct bpf_func_info),
6549		  "incorrect info.func_info_rec_size (1st) %d", rec_size)) {
6550		return -1;
6551	}
6552
6553	if (!info.nr_func_info)
6554		return 0;
6555
6556	func_info = malloc(info.nr_func_info * rec_size);
6557	if (CHECK(!func_info, "out of memory"))
6558		return -1;
6559
6560	/* reset info to only retrieve func_info related data */
6561	memset(&info, 0, sizeof(info));
6562	info.nr_func_info = nr_func_info;
6563	info.func_info_rec_size = rec_size;
6564	info.func_info = ptr_to_u64(func_info);
6565	err = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len);
6566	if (CHECK(err < 0, "invalid get info (2nd) errno:%d", errno)) {
6567		fprintf(stderr, "%s\n", btf_log_buf);
6568		err = -1;
6569		goto done;
6570	}
6571	if (CHECK(info.nr_func_info != nr_func_info,
6572		  "incorrect info.nr_func_info (2nd) %d",
6573		  info.nr_func_info)) {
6574		err = -1;
6575		goto done;
6576	}
6577	if (CHECK(info.func_info_rec_size != rec_size,
6578		  "incorrect info.func_info_rec_size (2nd) %d",
6579		  info.func_info_rec_size)) {
6580		err = -1;
6581		goto done;
6582	}
6583
6584	finfo = func_info;
6585	for (i = 0; i < nr_func_info; i++) {
6586		if (test->dead_func_mask & (1 << i))
6587			continue;
6588		if (CHECK(finfo->type_id != test->func_info[i][1],
6589			  "incorrect func_type %u expected %u",
6590			  finfo->type_id, test->func_info[i][1])) {
6591			err = -1;
6592			goto done;
6593		}
6594		finfo = (void *)finfo + rec_size;
6595	}
6596
6597	err = 0;
6598
6599done:
6600	free(func_info);
6601	return err;
6602}
6603
6604static int test_get_linfo(const struct prog_info_raw_test *test,
6605			  const void *patched_linfo,
6606			  __u32 cnt, int prog_fd)
6607{
6608	__u32 i, info_len, nr_jited_ksyms, nr_jited_func_lens;
6609	__u64 *jited_linfo = NULL, *jited_ksyms = NULL;
6610	__u32 rec_size, jited_rec_size, jited_cnt;
6611	struct bpf_line_info *linfo = NULL;
6612	__u32 cur_func_len, ksyms_found;
6613	struct bpf_prog_info info = {};
6614	__u32 *jited_func_lens = NULL;
6615	__u64 cur_func_ksyms;
6616	__u32 dead_insns;
6617	int err;
6618
6619	jited_cnt = cnt;
6620	rec_size = sizeof(*linfo);
6621	jited_rec_size = sizeof(*jited_linfo);
6622	if (test->nr_jited_ksyms)
6623		nr_jited_ksyms = test->nr_jited_ksyms;
6624	else
6625		nr_jited_ksyms = test->func_info_cnt - test->dead_func_cnt;
6626	nr_jited_func_lens = nr_jited_ksyms;
6627
6628	info_len = sizeof(struct bpf_prog_info);
6629	err = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len);
6630	if (CHECK(err < 0, "err:%d errno:%d", err, errno)) {
6631		err = -1;
6632		goto done;
6633	}
6634
6635	if (!info.jited_prog_len) {
6636		/* prog is not jited */
6637		jited_cnt = 0;
6638		nr_jited_ksyms = 1;
6639		nr_jited_func_lens = 1;
6640	}
6641
6642	if (CHECK(info.nr_line_info != cnt ||
6643		  info.nr_jited_line_info != jited_cnt ||
6644		  info.nr_jited_ksyms != nr_jited_ksyms ||
6645		  info.nr_jited_func_lens != nr_jited_func_lens ||
6646		  (!info.nr_line_info && info.nr_jited_line_info),
6647		  "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)",
6648		  info.nr_line_info, cnt,
6649		  info.nr_jited_line_info, jited_cnt,
6650		  info.nr_jited_ksyms, nr_jited_ksyms,
6651		  info.nr_jited_func_lens, nr_jited_func_lens)) {
6652		err = -1;
6653		goto done;
6654	}
6655
6656	if (CHECK(info.line_info_rec_size != sizeof(struct bpf_line_info) ||
6657		  info.jited_line_info_rec_size != sizeof(__u64),
6658		  "info: line_info_rec_size:%u(userspace expected:%u) jited_line_info_rec_size:%u(userspace expected:%u)",
6659		  info.line_info_rec_size, rec_size,
6660		  info.jited_line_info_rec_size, jited_rec_size)) {
6661		err = -1;
6662		goto done;
6663	}
6664
6665	if (!cnt)
6666		return 0;
6667
6668	rec_size = info.line_info_rec_size;
6669	jited_rec_size = info.jited_line_info_rec_size;
6670
6671	memset(&info, 0, sizeof(info));
6672
6673	linfo = calloc(cnt, rec_size);
6674	if (CHECK(!linfo, "!linfo")) {
6675		err = -1;
6676		goto done;
6677	}
6678	info.nr_line_info = cnt;
6679	info.line_info_rec_size = rec_size;
6680	info.line_info = ptr_to_u64(linfo);
6681
6682	if (jited_cnt) {
6683		jited_linfo = calloc(jited_cnt, jited_rec_size);
6684		jited_ksyms = calloc(nr_jited_ksyms, sizeof(*jited_ksyms));
6685		jited_func_lens = calloc(nr_jited_func_lens,
6686					 sizeof(*jited_func_lens));
6687		if (CHECK(!jited_linfo || !jited_ksyms || !jited_func_lens,
6688			  "jited_linfo:%p jited_ksyms:%p jited_func_lens:%p",
6689			  jited_linfo, jited_ksyms, jited_func_lens)) {
6690			err = -1;
6691			goto done;
6692		}
6693
6694		info.nr_jited_line_info = jited_cnt;
6695		info.jited_line_info_rec_size = jited_rec_size;
6696		info.jited_line_info = ptr_to_u64(jited_linfo);
6697		info.nr_jited_ksyms = nr_jited_ksyms;
6698		info.jited_ksyms = ptr_to_u64(jited_ksyms);
6699		info.nr_jited_func_lens = nr_jited_func_lens;
6700		info.jited_func_lens = ptr_to_u64(jited_func_lens);
6701	}
6702
6703	err = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len);
6704
6705	/*
6706	 * Only recheck the info.*line_info* fields.
6707	 * Other fields are not the concern of this test.
6708	 */
6709	if (CHECK(err < 0 ||
6710		  info.nr_line_info != cnt ||
6711		  (jited_cnt && !info.jited_line_info) ||
6712		  info.nr_jited_line_info != jited_cnt ||
6713		  info.line_info_rec_size != rec_size ||
6714		  info.jited_line_info_rec_size != jited_rec_size,
6715		  "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",
6716		  err, errno,
6717		  info.nr_line_info, cnt,
6718		  info.nr_jited_line_info, jited_cnt,
6719		  info.line_info_rec_size, rec_size,
6720		  info.jited_line_info_rec_size, jited_rec_size,
6721		  (void *)(long)info.line_info,
6722		  (void *)(long)info.jited_line_info)) {
6723		err = -1;
6724		goto done;
6725	}
6726
6727	dead_insns = 0;
6728	while (test->dead_code_mask & (1 << dead_insns))
6729		dead_insns++;
6730
6731	CHECK(linfo[0].insn_off, "linfo[0].insn_off:%u",
6732	      linfo[0].insn_off);
6733	for (i = 1; i < cnt; i++) {
6734		const struct bpf_line_info *expected_linfo;
6735
6736		while (test->dead_code_mask & (1 << (i + dead_insns)))
6737			dead_insns++;
6738
6739		expected_linfo = patched_linfo +
6740			((i + dead_insns) * test->line_info_rec_size);
6741		if (CHECK(linfo[i].insn_off <= linfo[i - 1].insn_off,
6742			  "linfo[%u].insn_off:%u <= linfo[%u].insn_off:%u",
6743			  i, linfo[i].insn_off,
6744			  i - 1, linfo[i - 1].insn_off)) {
6745			err = -1;
6746			goto done;
6747		}
6748		if (CHECK(linfo[i].file_name_off != expected_linfo->file_name_off ||
6749			  linfo[i].line_off != expected_linfo->line_off ||
6750			  linfo[i].line_col != expected_linfo->line_col,
6751			  "linfo[%u] (%u, %u, %u) != (%u, %u, %u)", i,
6752			  linfo[i].file_name_off,
6753			  linfo[i].line_off,
6754			  linfo[i].line_col,
6755			  expected_linfo->file_name_off,
6756			  expected_linfo->line_off,
6757			  expected_linfo->line_col)) {
6758			err = -1;
6759			goto done;
6760		}
6761	}
6762
6763	if (!jited_cnt) {
6764		fprintf(stderr, "not jited. skipping jited_line_info check. ");
6765		err = 0;
6766		goto done;
6767	}
6768
6769	if (CHECK(jited_linfo[0] != jited_ksyms[0],
6770		  "jited_linfo[0]:%lx != jited_ksyms[0]:%lx",
6771		  (long)(jited_linfo[0]), (long)(jited_ksyms[0]))) {
6772		err = -1;
6773		goto done;
6774	}
6775
6776	ksyms_found = 1;
6777	cur_func_len = jited_func_lens[0];
6778	cur_func_ksyms = jited_ksyms[0];
6779	for (i = 1; i < jited_cnt; i++) {
6780		if (ksyms_found < nr_jited_ksyms &&
6781		    jited_linfo[i] == jited_ksyms[ksyms_found]) {
6782			cur_func_ksyms = jited_ksyms[ksyms_found];
6783			cur_func_len = jited_ksyms[ksyms_found];
6784			ksyms_found++;
6785			continue;
6786		}
6787
6788		if (CHECK(jited_linfo[i] <= jited_linfo[i - 1],
6789			  "jited_linfo[%u]:%lx <= jited_linfo[%u]:%lx",
6790			  i, (long)jited_linfo[i],
6791			  i - 1, (long)(jited_linfo[i - 1]))) {
6792			err = -1;
6793			goto done;
6794		}
6795
6796		if (CHECK(jited_linfo[i] - cur_func_ksyms > cur_func_len,
6797			  "jited_linfo[%u]:%lx - %lx > %u",
6798			  i, (long)jited_linfo[i], (long)cur_func_ksyms,
6799			  cur_func_len)) {
6800			err = -1;
6801			goto done;
6802		}
6803	}
6804
6805	if (CHECK(ksyms_found != nr_jited_ksyms,
6806		  "ksyms_found:%u != nr_jited_ksyms:%u",
6807		  ksyms_found, nr_jited_ksyms)) {
6808		err = -1;
6809		goto done;
6810	}
6811
6812	err = 0;
6813
6814done:
6815	free(linfo);
6816	free(jited_linfo);
6817	free(jited_ksyms);
6818	free(jited_func_lens);
6819	return err;
6820}
6821
6822static void do_test_info_raw(unsigned int test_num)
6823{
6824	const struct prog_info_raw_test *test = &info_raw_tests[test_num - 1];
6825	unsigned int raw_btf_size, linfo_str_off, linfo_size = 0;
6826	int btf_fd = -1, prog_fd = -1, err = 0;
6827	void *raw_btf, *patched_linfo = NULL;
6828	const char *ret_next_str;
6829	union bpf_attr attr = {};
6830
6831	if (!test__start_subtest(test->descr))
6832		return;
6833
6834	raw_btf = btf_raw_create(&hdr_tmpl, test->raw_types,
6835				 test->str_sec, test->str_sec_size,
6836				 &raw_btf_size, &ret_next_str);
6837	if (!raw_btf)
6838		return;
6839
6840	*btf_log_buf = '\0';
6841	btf_fd = load_raw_btf(raw_btf, raw_btf_size);
6842	free(raw_btf);
6843
6844	if (CHECK(btf_fd < 0, "invalid btf_fd errno:%d", errno)) {
6845		err = -1;
6846		goto done;
6847	}
6848
6849	if (*btf_log_buf && always_log)
6850		fprintf(stderr, "\n%s", btf_log_buf);
6851	*btf_log_buf = '\0';
6852
6853	linfo_str_off = ret_next_str - test->str_sec;
6854	patched_linfo = patch_name_tbd(test->line_info,
6855				       test->str_sec, linfo_str_off,
6856				       test->str_sec_size, &linfo_size);
6857	err = libbpf_get_error(patched_linfo);
6858	if (err) {
6859		fprintf(stderr, "error in creating raw bpf_line_info");
6860		err = -1;
6861		goto done;
6862	}
6863
6864	attr.prog_type = test->prog_type;
6865	attr.insns = ptr_to_u64(test->insns);
6866	attr.insn_cnt = probe_prog_length(test->insns);
6867	attr.license = ptr_to_u64("GPL");
6868	attr.prog_btf_fd = btf_fd;
6869	attr.func_info_rec_size = test->func_info_rec_size;
6870	attr.func_info_cnt = test->func_info_cnt;
6871	attr.func_info = ptr_to_u64(test->func_info);
6872	attr.log_buf = ptr_to_u64(btf_log_buf);
6873	attr.log_size = BTF_LOG_BUF_SIZE;
6874	attr.log_level = 1;
6875	if (linfo_size) {
6876		attr.line_info_rec_size = test->line_info_rec_size;
6877		attr.line_info = ptr_to_u64(patched_linfo);
6878		attr.line_info_cnt = linfo_size / attr.line_info_rec_size;
6879	}
6880
6881	prog_fd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
6882	err = ((prog_fd < 0) != test->expected_prog_load_failure);
6883	if (CHECK(err, "prog_fd:%d expected_prog_load_failure:%u errno:%d",
6884		  prog_fd, test->expected_prog_load_failure, errno) ||
6885	    CHECK(test->err_str && !strstr(btf_log_buf, test->err_str),
6886		  "expected err_str:%s", test->err_str)) {
6887		err = -1;
6888		goto done;
6889	}
6890
6891	if (prog_fd < 0)
6892		goto done;
6893
6894	err = test_get_finfo(test, prog_fd);
6895	if (err)
6896		goto done;
6897
6898	err = test_get_linfo(test, patched_linfo,
6899			     attr.line_info_cnt - test->dead_code_cnt,
6900			     prog_fd);
6901	if (err)
6902		goto done;
6903
6904done:
6905	if (*btf_log_buf && (err || always_log))
6906		fprintf(stderr, "\n%s", btf_log_buf);
6907
6908	if (btf_fd >= 0)
6909		close(btf_fd);
6910	if (prog_fd >= 0)
6911		close(prog_fd);
6912
6913	if (!libbpf_get_error(patched_linfo))
6914		free(patched_linfo);
6915}
6916
6917struct btf_raw_data {
6918	__u32 raw_types[MAX_NR_RAW_U32];
6919	const char *str_sec;
6920	__u32 str_sec_size;
6921};
6922
6923struct btf_dedup_test {
6924	const char *descr;
6925	struct btf_raw_data input;
6926	struct btf_raw_data expect;
6927	struct btf_dedup_opts opts;
6928};
6929
6930static struct btf_dedup_test dedup_tests[] = {
6931
6932{
6933	.descr = "dedup: unused strings filtering",
6934	.input = {
6935		.raw_types = {
6936			BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 32, 4),
6937			BTF_TYPE_INT_ENC(NAME_NTH(5), BTF_INT_SIGNED, 0, 64, 8),
6938			BTF_END_RAW,
6939		},
6940		BTF_STR_SEC("\0unused\0int\0foo\0bar\0long"),
6941	},
6942	.expect = {
6943		.raw_types = {
6944			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),
6945			BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 64, 8),
6946			BTF_END_RAW,
6947		},
6948		BTF_STR_SEC("\0int\0long"),
6949	},
6950},
6951{
6952	.descr = "dedup: strings deduplication",
6953	.input = {
6954		.raw_types = {
6955			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),
6956			BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 64, 8),
6957			BTF_TYPE_INT_ENC(NAME_NTH(3), BTF_INT_SIGNED, 0, 32, 4),
6958			BTF_TYPE_INT_ENC(NAME_NTH(4), BTF_INT_SIGNED, 0, 64, 8),
6959			BTF_TYPE_INT_ENC(NAME_NTH(5), BTF_INT_SIGNED, 0, 32, 4),
6960			BTF_END_RAW,
6961		},
6962		BTF_STR_SEC("\0int\0long int\0int\0long int\0int"),
6963	},
6964	.expect = {
6965		.raw_types = {
6966			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),
6967			BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 64, 8),
6968			BTF_END_RAW,
6969		},
6970		BTF_STR_SEC("\0int\0long int"),
6971	},
6972},
6973{
6974	.descr = "dedup: struct example #1",
6975	/*
6976	 * struct s {
6977	 *	struct s *next;
6978	 *	const int *a;
6979	 *	int b[16];
6980	 *	int c;
6981	 * }
6982	 */
6983	.input = {
6984		.raw_types = {
6985			/* int */
6986			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6987			/* int[16] */
6988			BTF_TYPE_ARRAY_ENC(1, 1, 16),					/* [2] */
6989			/* struct s { */
6990			BTF_STRUCT_ENC(NAME_NTH(2), 5, 88),				/* [3] */
6991				BTF_MEMBER_ENC(NAME_NTH(3), 4, 0),	/* struct s *next;	*/
6992				BTF_MEMBER_ENC(NAME_NTH(4), 5, 64),	/* const int *a;	*/
6993				BTF_MEMBER_ENC(NAME_NTH(5), 2, 128),	/* int b[16];		*/
6994				BTF_MEMBER_ENC(NAME_NTH(6), 1, 640),	/* int c;		*/
6995				BTF_MEMBER_ENC(NAME_NTH(8), 15, 672),	/* float d;		*/
6996			/* ptr -> [3] struct s */
6997			BTF_PTR_ENC(3),							/* [4] */
6998			/* ptr -> [6] const int */
6999			BTF_PTR_ENC(6),							/* [5] */
7000			/* const -> [1] int */
7001			BTF_CONST_ENC(1),						/* [6] */
7002			/* tag -> [3] struct s */
7003			BTF_DECL_TAG_ENC(NAME_NTH(2), 3, -1),				/* [7] */
7004			/* tag -> [3] struct s, member 1 */
7005			BTF_DECL_TAG_ENC(NAME_NTH(2), 3, 1),				/* [8] */
7006
7007			/* full copy of the above */
7008			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),	/* [9] */
7009			BTF_TYPE_ARRAY_ENC(9, 9, 16),					/* [10] */
7010			BTF_STRUCT_ENC(NAME_NTH(2), 5, 88),				/* [11] */
7011				BTF_MEMBER_ENC(NAME_NTH(3), 12, 0),
7012				BTF_MEMBER_ENC(NAME_NTH(4), 13, 64),
7013				BTF_MEMBER_ENC(NAME_NTH(5), 10, 128),
7014				BTF_MEMBER_ENC(NAME_NTH(6), 9, 640),
7015				BTF_MEMBER_ENC(NAME_NTH(8), 15, 672),
7016			BTF_PTR_ENC(11),						/* [12] */
7017			BTF_PTR_ENC(14),						/* [13] */
7018			BTF_CONST_ENC(9),						/* [14] */
7019			BTF_TYPE_FLOAT_ENC(NAME_NTH(7), 4),				/* [15] */
7020			BTF_DECL_TAG_ENC(NAME_NTH(2), 11, -1),				/* [16] */
7021			BTF_DECL_TAG_ENC(NAME_NTH(2), 11, 1),				/* [17] */
7022			BTF_END_RAW,
7023		},
7024		BTF_STR_SEC("\0int\0s\0next\0a\0b\0c\0float\0d"),
7025	},
7026	.expect = {
7027		.raw_types = {
7028			/* int */
7029			BTF_TYPE_INT_ENC(NAME_NTH(5), BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7030			/* int[16] */
7031			BTF_TYPE_ARRAY_ENC(1, 1, 16),					/* [2] */
7032			/* struct s { */
7033			BTF_STRUCT_ENC(NAME_NTH(8), 5, 88),				/* [3] */
7034				BTF_MEMBER_ENC(NAME_NTH(7), 4, 0),	/* struct s *next;	*/
7035				BTF_MEMBER_ENC(NAME_NTH(1), 5, 64),	/* const int *a;	*/
7036				BTF_MEMBER_ENC(NAME_NTH(2), 2, 128),	/* int b[16];		*/
7037				BTF_MEMBER_ENC(NAME_NTH(3), 1, 640),	/* int c;		*/
7038				BTF_MEMBER_ENC(NAME_NTH(4), 9, 672),	/* float d;		*/
7039			/* ptr -> [3] struct s */
7040			BTF_PTR_ENC(3),							/* [4] */
7041			/* ptr -> [6] const int */
7042			BTF_PTR_ENC(6),							/* [5] */
7043			/* const -> [1] int */
7044			BTF_CONST_ENC(1),						/* [6] */
7045			BTF_DECL_TAG_ENC(NAME_NTH(2), 3, -1),				/* [7] */
7046			BTF_DECL_TAG_ENC(NAME_NTH(2), 3, 1),				/* [8] */
7047			BTF_TYPE_FLOAT_ENC(NAME_NTH(7), 4),				/* [9] */
7048			BTF_END_RAW,
7049		},
7050		BTF_STR_SEC("\0a\0b\0c\0d\0int\0float\0next\0s"),
7051	},
7052},
7053{
7054	.descr = "dedup: struct <-> fwd resolution w/ hash collision",
7055	/*
7056	 * // CU 1:
7057	 * struct x;
7058	 * struct s {
7059	 *	struct x *x;
7060	 * };
7061	 * // CU 2:
7062	 * struct x {};
7063	 * struct s {
7064	 *	struct x *x;
7065	 * };
7066	 */
7067	.input = {
7068		.raw_types = {
7069			/* CU 1 */
7070			BTF_FWD_ENC(NAME_TBD, 0 /* struct fwd */),	/* [1] fwd x      */
7071			BTF_PTR_ENC(1),					/* [2] ptr -> [1] */
7072			BTF_STRUCT_ENC(NAME_TBD, 1, 8),			/* [3] struct s   */
7073				BTF_MEMBER_ENC(NAME_TBD, 2, 0),
7074			/* CU 2 */
7075			BTF_STRUCT_ENC(NAME_TBD, 0, 0),			/* [4] struct x   */
7076			BTF_PTR_ENC(4),					/* [5] ptr -> [4] */
7077			BTF_STRUCT_ENC(NAME_TBD, 1, 8),			/* [6] struct s   */
7078				BTF_MEMBER_ENC(NAME_TBD, 5, 0),
7079			BTF_END_RAW,
7080		},
7081		BTF_STR_SEC("\0x\0s\0x\0x\0s\0x\0"),
7082	},
7083	.expect = {
7084		.raw_types = {
7085			BTF_PTR_ENC(3),					/* [1] ptr -> [3] */
7086			BTF_STRUCT_ENC(NAME_TBD, 1, 8),			/* [2] struct s   */
7087				BTF_MEMBER_ENC(NAME_TBD, 1, 0),
7088			BTF_STRUCT_ENC(NAME_NTH(2), 0, 0),		/* [3] struct x   */
7089			BTF_END_RAW,
7090		},
7091		BTF_STR_SEC("\0s\0x"),
7092	},
7093	.opts = {
7094		.force_collisions = true, /* force hash collisions */
7095	},
7096},
7097{
7098	.descr = "dedup: void equiv check",
7099	/*
7100	 * // CU 1:
7101	 * struct s {
7102	 *	struct {} *x;
7103	 * };
7104	 * // CU 2:
7105	 * struct s {
7106	 *	int *x;
7107	 * };
7108	 */
7109	.input = {
7110		.raw_types = {
7111			/* CU 1 */
7112			BTF_STRUCT_ENC(0, 0, 1),				/* [1] struct {}  */
7113			BTF_PTR_ENC(1),						/* [2] ptr -> [1] */
7114			BTF_STRUCT_ENC(NAME_NTH(1), 1, 8),			/* [3] struct s   */
7115				BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
7116			/* CU 2 */
7117			BTF_PTR_ENC(0),						/* [4] ptr -> void */
7118			BTF_STRUCT_ENC(NAME_NTH(1), 1, 8),			/* [5] struct s   */
7119				BTF_MEMBER_ENC(NAME_NTH(2), 4, 0),
7120			BTF_END_RAW,
7121		},
7122		BTF_STR_SEC("\0s\0x"),
7123	},
7124	.expect = {
7125		.raw_types = {
7126			/* CU 1 */
7127			BTF_STRUCT_ENC(0, 0, 1),				/* [1] struct {}  */
7128			BTF_PTR_ENC(1),						/* [2] ptr -> [1] */
7129			BTF_STRUCT_ENC(NAME_NTH(1), 1, 8),			/* [3] struct s   */
7130				BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
7131			/* CU 2 */
7132			BTF_PTR_ENC(0),						/* [4] ptr -> void */
7133			BTF_STRUCT_ENC(NAME_NTH(1), 1, 8),			/* [5] struct s   */
7134				BTF_MEMBER_ENC(NAME_NTH(2), 4, 0),
7135			BTF_END_RAW,
7136		},
7137		BTF_STR_SEC("\0s\0x"),
7138	},
7139	.opts = {
7140		.force_collisions = true, /* force hash collisions */
7141	},
7142},
7143{
7144	.descr = "dedup: all possible kinds (no duplicates)",
7145	.input = {
7146		.raw_types = {
7147			BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 8),		/* [1] int */
7148			BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), 4),	/* [2] enum */
7149				BTF_ENUM_ENC(NAME_TBD, 0),
7150				BTF_ENUM_ENC(NAME_TBD, 1),
7151			BTF_FWD_ENC(NAME_TBD, 1 /* union kind_flag */),			/* [3] fwd */
7152			BTF_TYPE_ARRAY_ENC(2, 1, 7),					/* [4] array */
7153			BTF_STRUCT_ENC(NAME_TBD, 1, 4),					/* [5] struct */
7154				BTF_MEMBER_ENC(NAME_TBD, 1, 0),
7155			BTF_UNION_ENC(NAME_TBD, 1, 4),					/* [6] union */
7156				BTF_MEMBER_ENC(NAME_TBD, 1, 0),
7157			BTF_TYPEDEF_ENC(NAME_TBD, 1),					/* [7] typedef */
7158			BTF_PTR_ENC(0),							/* [8] ptr */
7159			BTF_CONST_ENC(8),						/* [9] const */
7160			BTF_VOLATILE_ENC(8),						/* [10] volatile */
7161			BTF_RESTRICT_ENC(8),						/* [11] restrict */
7162			BTF_FUNC_PROTO_ENC(1, 2),					/* [12] func_proto */
7163				BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
7164				BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 18),
7165			BTF_FUNC_ENC(NAME_TBD, 12),					/* [13] func */
7166			BTF_TYPE_FLOAT_ENC(NAME_TBD, 2),				/* [14] float */
7167			BTF_DECL_TAG_ENC(NAME_TBD, 13, -1),				/* [15] decl_tag */
7168			BTF_DECL_TAG_ENC(NAME_TBD, 13, 1),				/* [16] decl_tag */
7169			BTF_DECL_TAG_ENC(NAME_TBD, 7, -1),				/* [17] decl_tag */
7170			BTF_TYPE_TAG_ENC(NAME_TBD, 8),					/* [18] type_tag */
7171			BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 2), 8),	/* [19] enum64 */
7172				BTF_ENUM64_ENC(NAME_TBD, 0, 0),
7173				BTF_ENUM64_ENC(NAME_TBD, 1, 1),
7174			BTF_END_RAW,
7175		},
7176		BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q\0R\0S\0T\0U"),
7177	},
7178	.expect = {
7179		.raw_types = {
7180			BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 8),		/* [1] int */
7181			BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), 4),	/* [2] enum */
7182				BTF_ENUM_ENC(NAME_TBD, 0),
7183				BTF_ENUM_ENC(NAME_TBD, 1),
7184			BTF_FWD_ENC(NAME_TBD, 1 /* union kind_flag */),			/* [3] fwd */
7185			BTF_TYPE_ARRAY_ENC(2, 1, 7),					/* [4] array */
7186			BTF_STRUCT_ENC(NAME_TBD, 1, 4),					/* [5] struct */
7187				BTF_MEMBER_ENC(NAME_TBD, 1, 0),
7188			BTF_UNION_ENC(NAME_TBD, 1, 4),					/* [6] union */
7189				BTF_MEMBER_ENC(NAME_TBD, 1, 0),
7190			BTF_TYPEDEF_ENC(NAME_TBD, 1),					/* [7] typedef */
7191			BTF_PTR_ENC(0),							/* [8] ptr */
7192			BTF_CONST_ENC(8),						/* [9] const */
7193			BTF_VOLATILE_ENC(8),						/* [10] volatile */
7194			BTF_RESTRICT_ENC(8),						/* [11] restrict */
7195			BTF_FUNC_PROTO_ENC(1, 2),					/* [12] func_proto */
7196				BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
7197				BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 18),
7198			BTF_FUNC_ENC(NAME_TBD, 12),					/* [13] func */
7199			BTF_TYPE_FLOAT_ENC(NAME_TBD, 2),				/* [14] float */
7200			BTF_DECL_TAG_ENC(NAME_TBD, 13, -1),				/* [15] decl_tag */
7201			BTF_DECL_TAG_ENC(NAME_TBD, 13, 1),				/* [16] decl_tag */
7202			BTF_DECL_TAG_ENC(NAME_TBD, 7, -1),				/* [17] decl_tag */
7203			BTF_TYPE_TAG_ENC(NAME_TBD, 8),					/* [18] type_tag */
7204			BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 2), 8),	/* [19] enum64 */
7205				BTF_ENUM64_ENC(NAME_TBD, 0, 0),
7206				BTF_ENUM64_ENC(NAME_TBD, 1, 1),
7207			BTF_END_RAW,
7208		},
7209		BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q\0R\0S\0T\0U"),
7210	},
7211},
7212{
7213	.descr = "dedup: no int/float duplicates",
7214	.input = {
7215		.raw_types = {
7216			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 8),
7217			/* different name */
7218			BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 32, 8),
7219			/* different encoding */
7220			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_CHAR, 0, 32, 8),
7221			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_BOOL, 0, 32, 8),
7222			/* different bit offset */
7223			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 8, 32, 8),
7224			/* different bit size */
7225			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 27, 8),
7226			/* different byte size */
7227			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),
7228			/* all allowed sizes */
7229			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 2),
7230			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 4),
7231			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 8),
7232			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 12),
7233			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 16),
7234			BTF_END_RAW,
7235		},
7236		BTF_STR_SEC("\0int\0some other int\0float"),
7237	},
7238	.expect = {
7239		.raw_types = {
7240			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 8),
7241			/* different name */
7242			BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 32, 8),
7243			/* different encoding */
7244			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_CHAR, 0, 32, 8),
7245			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_BOOL, 0, 32, 8),
7246			/* different bit offset */
7247			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 8, 32, 8),
7248			/* different bit size */
7249			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 27, 8),
7250			/* different byte size */
7251			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),
7252			/* all allowed sizes */
7253			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 2),
7254			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 4),
7255			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 8),
7256			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 12),
7257			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 16),
7258			BTF_END_RAW,
7259		},
7260		BTF_STR_SEC("\0int\0some other int\0float"),
7261	},
7262},
7263{
7264	.descr = "dedup: enum fwd resolution",
7265	.input = {
7266		.raw_types = {
7267			/* [1] fwd enum 'e1' before full enum */
7268			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 0), 4),
7269			/* [2] full enum 'e1' after fwd */
7270			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7271				BTF_ENUM_ENC(NAME_NTH(2), 123),
7272			/* [3] full enum 'e2' before fwd */
7273			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7274				BTF_ENUM_ENC(NAME_NTH(4), 456),
7275			/* [4] fwd enum 'e2' after full enum */
7276			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 0), 4),
7277			/* [5] fwd enum with different size, size does not matter for fwd */
7278			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 0), 1),
7279			/* [6] incompatible full enum with different value */
7280			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7281				BTF_ENUM_ENC(NAME_NTH(2), 321),
7282			BTF_END_RAW,
7283		},
7284		BTF_STR_SEC("\0e1\0e1_val\0e2\0e2_val"),
7285	},
7286	.expect = {
7287		.raw_types = {
7288			/* [1] full enum 'e1' */
7289			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7290				BTF_ENUM_ENC(NAME_NTH(2), 123),
7291			/* [2] full enum 'e2' */
7292			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7293				BTF_ENUM_ENC(NAME_NTH(4), 456),
7294			/* [3] incompatible full enum with different value */
7295			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7296				BTF_ENUM_ENC(NAME_NTH(2), 321),
7297			BTF_END_RAW,
7298		},
7299		BTF_STR_SEC("\0e1\0e1_val\0e2\0e2_val"),
7300	},
7301},
7302{
7303	.descr = "dedup: datasec and vars pass-through",
7304	.input = {
7305		.raw_types = {
7306			/* int */
7307			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7308			/* static int t */
7309			BTF_VAR_ENC(NAME_NTH(2), 1, 0),			/* [2] */
7310			/* .bss section */				/* [3] */
7311			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
7312			BTF_VAR_SECINFO_ENC(2, 0, 4),
7313			/* int, referenced from [5] */
7314			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [4] */
7315			/* another static int t */
7316			BTF_VAR_ENC(NAME_NTH(2), 4, 0),			/* [5] */
7317			/* another .bss section */			/* [6] */
7318			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
7319			BTF_VAR_SECINFO_ENC(5, 0, 4),
7320			BTF_END_RAW,
7321		},
7322		BTF_STR_SEC("\0.bss\0t"),
7323	},
7324	.expect = {
7325		.raw_types = {
7326			/* int */
7327			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7328			/* static int t */
7329			BTF_VAR_ENC(NAME_NTH(2), 1, 0),			/* [2] */
7330			/* .bss section */				/* [3] */
7331			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
7332			BTF_VAR_SECINFO_ENC(2, 0, 4),
7333			/* another static int t */
7334			BTF_VAR_ENC(NAME_NTH(2), 1, 0),			/* [4] */
7335			/* another .bss section */			/* [5] */
7336			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
7337			BTF_VAR_SECINFO_ENC(4, 0, 4),
7338			BTF_END_RAW,
7339		},
7340		BTF_STR_SEC("\0.bss\0t"),
7341	},
7342	.opts = {
7343		.force_collisions = true
7344	},
7345},
7346{
7347	.descr = "dedup: func/func_arg/var tags",
7348	.input = {
7349		.raw_types = {
7350			/* int */
7351			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7352			/* static int t */
7353			BTF_VAR_ENC(NAME_NTH(1), 1, 0),			/* [2] */
7354			/* void f(int a1, int a2) */
7355			BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
7356				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(2), 1),
7357				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(3), 1),
7358			BTF_FUNC_ENC(NAME_NTH(4), 3),			/* [4] */
7359			/* tag -> t */
7360			BTF_DECL_TAG_ENC(NAME_NTH(5), 2, -1),		/* [5] */
7361			BTF_DECL_TAG_ENC(NAME_NTH(5), 2, -1),		/* [6] */
7362			/* tag -> func */
7363			BTF_DECL_TAG_ENC(NAME_NTH(5), 4, -1),		/* [7] */
7364			BTF_DECL_TAG_ENC(NAME_NTH(5), 4, -1),		/* [8] */
7365			/* tag -> func arg a1 */
7366			BTF_DECL_TAG_ENC(NAME_NTH(5), 4, 1),		/* [9] */
7367			BTF_DECL_TAG_ENC(NAME_NTH(5), 4, 1),		/* [10] */
7368			BTF_END_RAW,
7369		},
7370		BTF_STR_SEC("\0t\0a1\0a2\0f\0tag"),
7371	},
7372	.expect = {
7373		.raw_types = {
7374			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7375			BTF_VAR_ENC(NAME_NTH(1), 1, 0),			/* [2] */
7376			BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
7377				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(2), 1),
7378				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(3), 1),
7379			BTF_FUNC_ENC(NAME_NTH(4), 3),			/* [4] */
7380			BTF_DECL_TAG_ENC(NAME_NTH(5), 2, -1),		/* [5] */
7381			BTF_DECL_TAG_ENC(NAME_NTH(5), 4, -1),		/* [6] */
7382			BTF_DECL_TAG_ENC(NAME_NTH(5), 4, 1),		/* [7] */
7383			BTF_END_RAW,
7384		},
7385		BTF_STR_SEC("\0t\0a1\0a2\0f\0tag"),
7386	},
7387},
7388{
7389	.descr = "dedup: func/func_param tags",
7390	.input = {
7391		.raw_types = {
7392			/* int */
7393			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7394			/* void f(int a1, int a2) */
7395			BTF_FUNC_PROTO_ENC(0, 2),			/* [2] */
7396				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(1), 1),
7397				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(2), 1),
7398			BTF_FUNC_ENC(NAME_NTH(3), 2),			/* [3] */
7399			/* void f(int a1, int a2) */
7400			BTF_FUNC_PROTO_ENC(0, 2),			/* [4] */
7401				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(1), 1),
7402				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(2), 1),
7403			BTF_FUNC_ENC(NAME_NTH(3), 4),			/* [5] */
7404			/* tag -> f: tag1, tag2 */
7405			BTF_DECL_TAG_ENC(NAME_NTH(4), 3, -1),		/* [6] */
7406			BTF_DECL_TAG_ENC(NAME_NTH(5), 3, -1),		/* [7] */
7407			/* tag -> f/a2: tag1, tag2 */
7408			BTF_DECL_TAG_ENC(NAME_NTH(4), 3, 1),		/* [8] */
7409			BTF_DECL_TAG_ENC(NAME_NTH(5), 3, 1),		/* [9] */
7410			/* tag -> f: tag1, tag3 */
7411			BTF_DECL_TAG_ENC(NAME_NTH(4), 5, -1),		/* [10] */
7412			BTF_DECL_TAG_ENC(NAME_NTH(6), 5, -1),		/* [11] */
7413			/* tag -> f/a2: tag1, tag3 */
7414			BTF_DECL_TAG_ENC(NAME_NTH(4), 5, 1),		/* [12] */
7415			BTF_DECL_TAG_ENC(NAME_NTH(6), 5, 1),		/* [13] */
7416			BTF_END_RAW,
7417		},
7418		BTF_STR_SEC("\0a1\0a2\0f\0tag1\0tag2\0tag3"),
7419	},
7420	.expect = {
7421		.raw_types = {
7422			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7423			BTF_FUNC_PROTO_ENC(0, 2),			/* [2] */
7424				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(1), 1),
7425				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(2), 1),
7426			BTF_FUNC_ENC(NAME_NTH(3), 2),			/* [3] */
7427			BTF_DECL_TAG_ENC(NAME_NTH(4), 3, -1),		/* [4] */
7428			BTF_DECL_TAG_ENC(NAME_NTH(5), 3, -1),		/* [5] */
7429			BTF_DECL_TAG_ENC(NAME_NTH(6), 3, -1),		/* [6] */
7430			BTF_DECL_TAG_ENC(NAME_NTH(4), 3, 1),		/* [7] */
7431			BTF_DECL_TAG_ENC(NAME_NTH(5), 3, 1),		/* [8] */
7432			BTF_DECL_TAG_ENC(NAME_NTH(6), 3, 1),		/* [9] */
7433			BTF_END_RAW,
7434		},
7435		BTF_STR_SEC("\0a1\0a2\0f\0tag1\0tag2\0tag3"),
7436	},
7437},
7438{
7439	.descr = "dedup: struct/struct_member tags",
7440	.input = {
7441		.raw_types = {
7442			/* int */
7443			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7444			BTF_STRUCT_ENC(NAME_NTH(1), 2, 8),		/* [2] */
7445				BTF_MEMBER_ENC(NAME_NTH(2), 1, 0),
7446				BTF_MEMBER_ENC(NAME_NTH(3), 1, 32),
7447			BTF_STRUCT_ENC(NAME_NTH(1), 2, 8),		/* [3] */
7448				BTF_MEMBER_ENC(NAME_NTH(2), 1, 0),
7449				BTF_MEMBER_ENC(NAME_NTH(3), 1, 32),
7450			/* tag -> t: tag1, tag2 */
7451			BTF_DECL_TAG_ENC(NAME_NTH(4), 2, -1),		/* [4] */
7452			BTF_DECL_TAG_ENC(NAME_NTH(5), 2, -1),		/* [5] */
7453			/* tag -> t/m2: tag1, tag2 */
7454			BTF_DECL_TAG_ENC(NAME_NTH(4), 2, 1),		/* [6] */
7455			BTF_DECL_TAG_ENC(NAME_NTH(5), 2, 1),		/* [7] */
7456			/* tag -> t: tag1, tag3 */
7457			BTF_DECL_TAG_ENC(NAME_NTH(4), 3, -1),		/* [8] */
7458			BTF_DECL_TAG_ENC(NAME_NTH(6), 3, -1),		/* [9] */
7459			/* tag -> t/m2: tag1, tag3 */
7460			BTF_DECL_TAG_ENC(NAME_NTH(4), 3, 1),		/* [10] */
7461			BTF_DECL_TAG_ENC(NAME_NTH(6), 3, 1),		/* [11] */
7462			BTF_END_RAW,
7463		},
7464		BTF_STR_SEC("\0t\0m1\0m2\0tag1\0tag2\0tag3"),
7465	},
7466	.expect = {
7467		.raw_types = {
7468			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7469			BTF_STRUCT_ENC(NAME_NTH(1), 2, 8),		/* [2] */
7470				BTF_MEMBER_ENC(NAME_NTH(2), 1, 0),
7471				BTF_MEMBER_ENC(NAME_NTH(3), 1, 32),
7472			BTF_DECL_TAG_ENC(NAME_NTH(4), 2, -1),		/* [3] */
7473			BTF_DECL_TAG_ENC(NAME_NTH(5), 2, -1),		/* [4] */
7474			BTF_DECL_TAG_ENC(NAME_NTH(6), 2, -1),		/* [5] */
7475			BTF_DECL_TAG_ENC(NAME_NTH(4), 2, 1),		/* [6] */
7476			BTF_DECL_TAG_ENC(NAME_NTH(5), 2, 1),		/* [7] */
7477			BTF_DECL_TAG_ENC(NAME_NTH(6), 2, 1),		/* [8] */
7478			BTF_END_RAW,
7479		},
7480		BTF_STR_SEC("\0t\0m1\0m2\0tag1\0tag2\0tag3"),
7481	},
7482},
7483{
7484	.descr = "dedup: typedef tags",
7485	.input = {
7486		.raw_types = {
7487			/* int */
7488			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7489			BTF_TYPEDEF_ENC(NAME_NTH(1), 1),		/* [2] */
7490			BTF_TYPEDEF_ENC(NAME_NTH(1), 1),		/* [3] */
7491			/* tag -> t: tag1, tag2 */
7492			BTF_DECL_TAG_ENC(NAME_NTH(2), 2, -1),		/* [4] */
7493			BTF_DECL_TAG_ENC(NAME_NTH(3), 2, -1),		/* [5] */
7494			/* tag -> t: tag1, tag3 */
7495			BTF_DECL_TAG_ENC(NAME_NTH(2), 3, -1),		/* [6] */
7496			BTF_DECL_TAG_ENC(NAME_NTH(4), 3, -1),		/* [7] */
7497			BTF_END_RAW,
7498		},
7499		BTF_STR_SEC("\0t\0tag1\0tag2\0tag3"),
7500	},
7501	.expect = {
7502		.raw_types = {
7503			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7504			BTF_TYPEDEF_ENC(NAME_NTH(1), 1),		/* [2] */
7505			BTF_DECL_TAG_ENC(NAME_NTH(2), 2, -1),		/* [3] */
7506			BTF_DECL_TAG_ENC(NAME_NTH(3), 2, -1),		/* [4] */
7507			BTF_DECL_TAG_ENC(NAME_NTH(4), 2, -1),		/* [5] */
7508			BTF_END_RAW,
7509		},
7510		BTF_STR_SEC("\0t\0tag1\0tag2\0tag3"),
7511	},
7512},
7513{
7514	.descr = "dedup: btf_type_tag #1",
7515	.input = {
7516		.raw_types = {
7517			/* ptr -> tag2 -> tag1 -> int */
7518			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7519			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7520			BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),		/* [3] */
7521			BTF_PTR_ENC(3),					/* [4] */
7522			/* ptr -> tag2 -> tag1 -> int */
7523			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [5] */
7524			BTF_TYPE_TAG_ENC(NAME_NTH(2), 5),		/* [6] */
7525			BTF_PTR_ENC(6),					/* [7] */
7526			/* ptr -> tag1 -> int */
7527			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [8] */
7528			BTF_PTR_ENC(8),					/* [9] */
7529			BTF_END_RAW,
7530		},
7531		BTF_STR_SEC("\0tag1\0tag2"),
7532	},
7533	.expect = {
7534		.raw_types = {
7535			/* ptr -> tag2 -> tag1 -> int */
7536			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7537			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7538			BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),		/* [3] */
7539			BTF_PTR_ENC(3),					/* [4] */
7540			/* ptr -> tag1 -> int */
7541			BTF_PTR_ENC(2),					/* [5] */
7542			BTF_END_RAW,
7543		},
7544		BTF_STR_SEC("\0tag1\0tag2"),
7545	},
7546},
7547{
7548	.descr = "dedup: btf_type_tag #2",
7549	.input = {
7550		.raw_types = {
7551			/* ptr -> tag2 -> tag1 -> int */
7552			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7553			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7554			BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),		/* [3] */
7555			BTF_PTR_ENC(3),					/* [4] */
7556			/* ptr -> tag2 -> int */
7557			BTF_TYPE_TAG_ENC(NAME_NTH(2), 1),		/* [5] */
7558			BTF_PTR_ENC(5),					/* [6] */
7559			BTF_END_RAW,
7560		},
7561		BTF_STR_SEC("\0tag1\0tag2"),
7562	},
7563	.expect = {
7564		.raw_types = {
7565			/* ptr -> tag2 -> tag1 -> int */
7566			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7567			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7568			BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),		/* [3] */
7569			BTF_PTR_ENC(3),					/* [4] */
7570			/* ptr -> tag2 -> int */
7571			BTF_TYPE_TAG_ENC(NAME_NTH(2), 1),		/* [5] */
7572			BTF_PTR_ENC(5),					/* [6] */
7573			BTF_END_RAW,
7574		},
7575		BTF_STR_SEC("\0tag1\0tag2"),
7576	},
7577},
7578{
7579	.descr = "dedup: btf_type_tag #3",
7580	.input = {
7581		.raw_types = {
7582			/* ptr -> tag2 -> tag1 -> int */
7583			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7584			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7585			BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),		/* [3] */
7586			BTF_PTR_ENC(3),					/* [4] */
7587			/* ptr -> tag1 -> tag2 -> int */
7588			BTF_TYPE_TAG_ENC(NAME_NTH(2), 1),		/* [5] */
7589			BTF_TYPE_TAG_ENC(NAME_NTH(1), 5),		/* [6] */
7590			BTF_PTR_ENC(6),					/* [7] */
7591			BTF_END_RAW,
7592		},
7593		BTF_STR_SEC("\0tag1\0tag2"),
7594	},
7595	.expect = {
7596		.raw_types = {
7597			/* ptr -> tag2 -> tag1 -> int */
7598			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7599			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7600			BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),		/* [3] */
7601			BTF_PTR_ENC(3),					/* [4] */
7602			/* ptr -> tag1 -> tag2 -> int */
7603			BTF_TYPE_TAG_ENC(NAME_NTH(2), 1),		/* [5] */
7604			BTF_TYPE_TAG_ENC(NAME_NTH(1), 5),		/* [6] */
7605			BTF_PTR_ENC(6),					/* [7] */
7606			BTF_END_RAW,
7607		},
7608		BTF_STR_SEC("\0tag1\0tag2"),
7609	},
7610},
7611{
7612	.descr = "dedup: btf_type_tag #4",
7613	.input = {
7614		.raw_types = {
7615			/* ptr -> tag1 -> int */
7616			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7617			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7618			BTF_PTR_ENC(2),					/* [3] */
7619			/* ptr -> tag1 -> long */
7620			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 64, 8),	/* [4] */
7621			BTF_TYPE_TAG_ENC(NAME_NTH(1), 4),		/* [5] */
7622			BTF_PTR_ENC(5),					/* [6] */
7623			BTF_END_RAW,
7624		},
7625		BTF_STR_SEC("\0tag1"),
7626	},
7627	.expect = {
7628		.raw_types = {
7629			/* ptr -> tag1 -> int */
7630			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7631			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7632			BTF_PTR_ENC(2),					/* [3] */
7633			/* ptr -> tag1 -> long */
7634			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 64, 8),	/* [4] */
7635			BTF_TYPE_TAG_ENC(NAME_NTH(1), 4),		/* [5] */
7636			BTF_PTR_ENC(5),					/* [6] */
7637			BTF_END_RAW,
7638		},
7639		BTF_STR_SEC("\0tag1"),
7640	},
7641},
7642{
7643	.descr = "dedup: btf_type_tag #5, struct",
7644	.input = {
7645		.raw_types = {
7646			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),				/* [1] */
7647			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),					/* [2] */
7648			BTF_TYPE_ENC(NAME_NTH(2), BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 1), 4),	/* [3] */
7649			BTF_MEMBER_ENC(NAME_NTH(3), 2, BTF_MEMBER_OFFSET(0, 0)),
7650			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),					/* [4] */
7651			BTF_TYPE_ENC(NAME_NTH(2), BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 1), 4),	/* [5] */
7652			BTF_MEMBER_ENC(NAME_NTH(3), 4, BTF_MEMBER_OFFSET(0, 0)),
7653			BTF_END_RAW,
7654		},
7655		BTF_STR_SEC("\0tag1\0t\0m"),
7656	},
7657	.expect = {
7658		.raw_types = {
7659			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),				/* [1] */
7660			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),					/* [2] */
7661			BTF_TYPE_ENC(NAME_NTH(2), BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 1), 4),	/* [3] */
7662			BTF_MEMBER_ENC(NAME_NTH(3), 2, BTF_MEMBER_OFFSET(0, 0)),
7663			BTF_END_RAW,
7664		},
7665		BTF_STR_SEC("\0tag1\0t\0m"),
7666	},
7667},
7668{
7669	.descr = "dedup: enum64, standalone",
7670	.input = {
7671		.raw_types = {
7672			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
7673				BTF_ENUM64_ENC(NAME_NTH(2), 1, 123),
7674			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
7675				BTF_ENUM64_ENC(NAME_NTH(2), 1, 123),
7676			BTF_END_RAW,
7677		},
7678		BTF_STR_SEC("\0e1\0e1_val"),
7679	},
7680	.expect = {
7681		.raw_types = {
7682			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
7683				BTF_ENUM64_ENC(NAME_NTH(2), 1, 123),
7684			BTF_END_RAW,
7685		},
7686		BTF_STR_SEC("\0e1\0e1_val"),
7687	},
7688},
7689{
7690	.descr = "dedup: enum64, fwd resolution",
7691	.input = {
7692		.raw_types = {
7693			/* [1] fwd enum64 'e1' before full enum */
7694			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 0), 8),
7695			/* [2] full enum64 'e1' after fwd */
7696			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
7697				BTF_ENUM64_ENC(NAME_NTH(2), 1, 123),
7698			/* [3] full enum64 'e2' before fwd */
7699			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
7700				BTF_ENUM64_ENC(NAME_NTH(4), 0, 456),
7701			/* [4] fwd enum64 'e2' after full enum */
7702			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 0), 8),
7703			/* [5] incompatible full enum64 with different value */
7704			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
7705				BTF_ENUM64_ENC(NAME_NTH(2), 0, 321),
7706			BTF_END_RAW,
7707		},
7708		BTF_STR_SEC("\0e1\0e1_val\0e2\0e2_val"),
7709	},
7710	.expect = {
7711		.raw_types = {
7712			/* [1] full enum64 'e1' */
7713			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
7714				BTF_ENUM64_ENC(NAME_NTH(2), 1, 123),
7715			/* [2] full enum64 'e2' */
7716			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
7717				BTF_ENUM64_ENC(NAME_NTH(4), 0, 456),
7718			/* [3] incompatible full enum64 with different value */
7719			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
7720				BTF_ENUM64_ENC(NAME_NTH(2), 0, 321),
7721			BTF_END_RAW,
7722		},
7723		BTF_STR_SEC("\0e1\0e1_val\0e2\0e2_val"),
7724	},
7725},
7726{
7727	.descr = "dedup: enum and enum64, no dedup",
7728	.input = {
7729		.raw_types = {
7730			/* [1] enum 'e1' */
7731			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7732				BTF_ENUM_ENC(NAME_NTH(2), 1),
7733			/* [2] enum64 'e1' */
7734			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 4),
7735				BTF_ENUM64_ENC(NAME_NTH(2), 1, 0),
7736			BTF_END_RAW,
7737		},
7738		BTF_STR_SEC("\0e1\0e1_val"),
7739	},
7740	.expect = {
7741		.raw_types = {
7742			/* [1] enum 'e1' */
7743			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7744				BTF_ENUM_ENC(NAME_NTH(2), 1),
7745			/* [2] enum64 'e1' */
7746			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 4),
7747				BTF_ENUM64_ENC(NAME_NTH(2), 1, 0),
7748			BTF_END_RAW,
7749		},
7750		BTF_STR_SEC("\0e1\0e1_val"),
7751	},
7752},
7753{
7754	.descr = "dedup: enum of different size: no dedup",
7755	.input = {
7756		.raw_types = {
7757			/* [1] enum 'e1' */
7758			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7759				BTF_ENUM_ENC(NAME_NTH(2), 1),
7760			/* [2] enum 'e1' */
7761			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 2),
7762				BTF_ENUM_ENC(NAME_NTH(2), 1),
7763			BTF_END_RAW,
7764		},
7765		BTF_STR_SEC("\0e1\0e1_val"),
7766	},
7767	.expect = {
7768		.raw_types = {
7769			/* [1] enum 'e1' */
7770			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7771				BTF_ENUM_ENC(NAME_NTH(2), 1),
7772			/* [2] enum 'e1' */
7773			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 2),
7774				BTF_ENUM_ENC(NAME_NTH(2), 1),
7775			BTF_END_RAW,
7776		},
7777		BTF_STR_SEC("\0e1\0e1_val"),
7778	},
7779},
7780{
7781	.descr = "dedup: enum fwd to enum64",
7782	.input = {
7783		.raw_types = {
7784			/* [1] enum64 'e1' */
7785			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
7786				BTF_ENUM64_ENC(NAME_NTH(2), 1, 0),
7787			/* [2] enum 'e1' fwd */
7788			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 0), 4),
7789			/* [3] typedef enum 'e1' td */
7790			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), 2),
7791			BTF_END_RAW,
7792		},
7793		BTF_STR_SEC("\0e1\0e1_val\0td"),
7794	},
7795	.expect = {
7796		.raw_types = {
7797			/* [1] enum64 'e1' */
7798			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
7799				BTF_ENUM64_ENC(NAME_NTH(2), 1, 0),
7800			/* [2] typedef enum 'e1' td */
7801			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), 1),
7802			BTF_END_RAW,
7803		},
7804		BTF_STR_SEC("\0e1\0e1_val\0td"),
7805	},
7806},
7807{
7808	.descr = "dedup: enum64 fwd to enum",
7809	.input = {
7810		.raw_types = {
7811			/* [1] enum 'e1' */
7812			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7813				BTF_ENUM_ENC(NAME_NTH(2), 1),
7814			/* [2] enum64 'e1' fwd */
7815			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 0), 8),
7816			/* [3] typedef enum 'e1' td */
7817			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), 2),
7818			BTF_END_RAW,
7819		},
7820		BTF_STR_SEC("\0e1\0e1_val\0td"),
7821	},
7822	.expect = {
7823		.raw_types = {
7824			/* [1] enum 'e1' */
7825			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7826				BTF_ENUM_ENC(NAME_NTH(2), 1),
7827			/* [2] typedef enum 'e1' td */
7828			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), 1),
7829			BTF_END_RAW,
7830		},
7831		BTF_STR_SEC("\0e1\0e1_val\0td"),
7832	},
7833},
7834{
7835	.descr = "dedup: standalone fwd declaration struct",
7836	/*
7837	 * Verify that CU1:foo and CU2:foo would be unified and that
7838	 * typedef/ptr would be updated to point to CU1:foo.
7839	 *
7840	 * // CU 1:
7841	 * struct foo { int x; };
7842	 *
7843	 * // CU 2:
7844	 * struct foo;
7845	 * typedef struct foo *foo_ptr;
7846	 */
7847	.input = {
7848		.raw_types = {
7849			/* CU 1 */
7850			BTF_STRUCT_ENC(NAME_NTH(1), 1, 4),             /* [1] */
7851			BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
7852			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */
7853			/* CU 2 */
7854			BTF_FWD_ENC(NAME_NTH(1), 0),                   /* [3] */
7855			BTF_PTR_ENC(3),                                /* [4] */
7856			BTF_TYPEDEF_ENC(NAME_NTH(3), 4),               /* [5] */
7857			BTF_END_RAW,
7858		},
7859		BTF_STR_SEC("\0foo\0x\0foo_ptr"),
7860	},
7861	.expect = {
7862		.raw_types = {
7863			BTF_STRUCT_ENC(NAME_NTH(1), 1, 4),             /* [1] */
7864			BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
7865			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */
7866			BTF_PTR_ENC(1),                                /* [3] */
7867			BTF_TYPEDEF_ENC(NAME_NTH(3), 3),               /* [4] */
7868			BTF_END_RAW,
7869		},
7870		BTF_STR_SEC("\0foo\0x\0foo_ptr"),
7871	},
7872},
7873{
7874	.descr = "dedup: standalone fwd declaration union",
7875	/*
7876	 * Verify that CU1:foo and CU2:foo would be unified and that
7877	 * typedef/ptr would be updated to point to CU1:foo.
7878	 * Same as "dedup: standalone fwd declaration struct" but for unions.
7879	 *
7880	 * // CU 1:
7881	 * union foo { int x; };
7882	 *
7883	 * // CU 2:
7884	 * union foo;
7885	 * typedef union foo *foo_ptr;
7886	 */
7887	.input = {
7888		.raw_types = {
7889			/* CU 1 */
7890			BTF_UNION_ENC(NAME_NTH(1), 1, 4),              /* [1] */
7891			BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
7892			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */
7893			/* CU 2 */
7894			BTF_FWD_ENC(NAME_TBD, 1),                      /* [3] */
7895			BTF_PTR_ENC(3),                                /* [4] */
7896			BTF_TYPEDEF_ENC(NAME_NTH(3), 4),               /* [5] */
7897			BTF_END_RAW,
7898		},
7899		BTF_STR_SEC("\0foo\0x\0foo_ptr"),
7900	},
7901	.expect = {
7902		.raw_types = {
7903			BTF_UNION_ENC(NAME_NTH(1), 1, 4),              /* [1] */
7904			BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
7905			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */
7906			BTF_PTR_ENC(1),                                /* [3] */
7907			BTF_TYPEDEF_ENC(NAME_NTH(3), 3),               /* [4] */
7908			BTF_END_RAW,
7909		},
7910		BTF_STR_SEC("\0foo\0x\0foo_ptr"),
7911	},
7912},
7913{
7914	.descr = "dedup: standalone fwd declaration wrong kind",
7915	/*
7916	 * Negative test for btf_dedup_resolve_fwds:
7917	 * - CU1:foo is a struct, C2:foo is a union, thus CU2:foo is not deduped;
7918	 * - typedef/ptr should remain unchanged as well.
7919	 *
7920	 * // CU 1:
7921	 * struct foo { int x; };
7922	 *
7923	 * // CU 2:
7924	 * union foo;
7925	 * typedef union foo *foo_ptr;
7926	 */
7927	.input = {
7928		.raw_types = {
7929			/* CU 1 */
7930			BTF_STRUCT_ENC(NAME_NTH(1), 1, 4),             /* [1] */
7931			BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
7932			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */
7933			/* CU 2 */
7934			BTF_FWD_ENC(NAME_NTH(3), 1),                   /* [3] */
7935			BTF_PTR_ENC(3),                                /* [4] */
7936			BTF_TYPEDEF_ENC(NAME_NTH(3), 4),               /* [5] */
7937			BTF_END_RAW,
7938		},
7939		BTF_STR_SEC("\0foo\0x\0foo_ptr"),
7940	},
7941	.expect = {
7942		.raw_types = {
7943			/* CU 1 */
7944			BTF_STRUCT_ENC(NAME_NTH(1), 1, 4),             /* [1] */
7945			BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
7946			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */
7947			/* CU 2 */
7948			BTF_FWD_ENC(NAME_NTH(3), 1),                   /* [3] */
7949			BTF_PTR_ENC(3),                                /* [4] */
7950			BTF_TYPEDEF_ENC(NAME_NTH(3), 4),               /* [5] */
7951			BTF_END_RAW,
7952		},
7953		BTF_STR_SEC("\0foo\0x\0foo_ptr"),
7954	},
7955},
7956{
7957	.descr = "dedup: standalone fwd declaration name conflict",
7958	/*
7959	 * Negative test for btf_dedup_resolve_fwds:
7960	 * - two candidates for CU2:foo dedup, thus it is unchanged;
7961	 * - typedef/ptr should remain unchanged as well.
7962	 *
7963	 * // CU 1:
7964	 * struct foo { int x; };
7965	 *
7966	 * // CU 2:
7967	 * struct foo;
7968	 * typedef struct foo *foo_ptr;
7969	 *
7970	 * // CU 3:
7971	 * struct foo { int x; int y; };
7972	 */
7973	.input = {
7974		.raw_types = {
7975			/* CU 1 */
7976			BTF_STRUCT_ENC(NAME_NTH(1), 1, 4),             /* [1] */
7977			BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
7978			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */
7979			/* CU 2 */
7980			BTF_FWD_ENC(NAME_NTH(1), 0),                   /* [3] */
7981			BTF_PTR_ENC(3),                                /* [4] */
7982			BTF_TYPEDEF_ENC(NAME_NTH(4), 4),               /* [5] */
7983			/* CU 3 */
7984			BTF_STRUCT_ENC(NAME_NTH(1), 2, 8),             /* [6] */
7985			BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
7986			BTF_MEMBER_ENC(NAME_NTH(3), 2, 0),
7987			BTF_END_RAW,
7988		},
7989		BTF_STR_SEC("\0foo\0x\0y\0foo_ptr"),
7990	},
7991	.expect = {
7992		.raw_types = {
7993			/* CU 1 */
7994			BTF_STRUCT_ENC(NAME_NTH(1), 1, 4),             /* [1] */
7995			BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
7996			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */
7997			/* CU 2 */
7998			BTF_FWD_ENC(NAME_NTH(1), 0),                   /* [3] */
7999			BTF_PTR_ENC(3),                                /* [4] */
8000			BTF_TYPEDEF_ENC(NAME_NTH(4), 4),               /* [5] */
8001			/* CU 3 */
8002			BTF_STRUCT_ENC(NAME_NTH(1), 2, 8),             /* [6] */
8003			BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
8004			BTF_MEMBER_ENC(NAME_NTH(3), 2, 0),
8005			BTF_END_RAW,
8006		},
8007		BTF_STR_SEC("\0foo\0x\0y\0foo_ptr"),
8008	},
8009},
8010};
8011
8012static int btf_type_size(const struct btf_type *t)
8013{
8014	int base_size = sizeof(struct btf_type);
8015	__u16 vlen = BTF_INFO_VLEN(t->info);
8016	__u16 kind = BTF_INFO_KIND(t->info);
8017
8018	switch (kind) {
8019	case BTF_KIND_FWD:
8020	case BTF_KIND_CONST:
8021	case BTF_KIND_VOLATILE:
8022	case BTF_KIND_RESTRICT:
8023	case BTF_KIND_PTR:
8024	case BTF_KIND_TYPEDEF:
8025	case BTF_KIND_FUNC:
8026	case BTF_KIND_FLOAT:
8027	case BTF_KIND_TYPE_TAG:
8028		return base_size;
8029	case BTF_KIND_INT:
8030		return base_size + sizeof(__u32);
8031	case BTF_KIND_ENUM:
8032		return base_size + vlen * sizeof(struct btf_enum);
8033	case BTF_KIND_ENUM64:
8034		return base_size + vlen * sizeof(struct btf_enum64);
8035	case BTF_KIND_ARRAY:
8036		return base_size + sizeof(struct btf_array);
8037	case BTF_KIND_STRUCT:
8038	case BTF_KIND_UNION:
8039		return base_size + vlen * sizeof(struct btf_member);
8040	case BTF_KIND_FUNC_PROTO:
8041		return base_size + vlen * sizeof(struct btf_param);
8042	case BTF_KIND_VAR:
8043		return base_size + sizeof(struct btf_var);
8044	case BTF_KIND_DATASEC:
8045		return base_size + vlen * sizeof(struct btf_var_secinfo);
8046	case BTF_KIND_DECL_TAG:
8047		return base_size + sizeof(struct btf_decl_tag);
8048	default:
8049		fprintf(stderr, "Unsupported BTF_KIND:%u\n", kind);
8050		return -EINVAL;
8051	}
8052}
8053
8054static void dump_btf_strings(const char *strs, __u32 len)
8055{
8056	const char *cur = strs;
8057	int i = 0;
8058
8059	while (cur < strs + len) {
8060		fprintf(stderr, "string #%d: '%s'\n", i, cur);
8061		cur += strlen(cur) + 1;
8062		i++;
8063	}
8064}
8065
8066static void do_test_dedup(unsigned int test_num)
8067{
8068	struct btf_dedup_test *test = &dedup_tests[test_num - 1];
8069	__u32 test_nr_types, expect_nr_types, test_btf_size, expect_btf_size;
8070	const struct btf_header *test_hdr, *expect_hdr;
8071	struct btf *test_btf = NULL, *expect_btf = NULL;
8072	const void *test_btf_data, *expect_btf_data;
8073	const char *ret_test_next_str, *ret_expect_next_str;
8074	const char *test_strs, *expect_strs;
8075	const char *test_str_cur;
8076	const char *expect_str_cur, *expect_str_end;
8077	unsigned int raw_btf_size;
8078	void *raw_btf;
8079	int err = 0, i;
8080
8081	if (!test__start_subtest(test->descr))
8082		return;
8083
8084	raw_btf = btf_raw_create(&hdr_tmpl, test->input.raw_types,
8085				 test->input.str_sec, test->input.str_sec_size,
8086				 &raw_btf_size, &ret_test_next_str);
8087	if (!raw_btf)
8088		return;
8089
8090	test_btf = btf__new((__u8 *)raw_btf, raw_btf_size);
8091	err = libbpf_get_error(test_btf);
8092	free(raw_btf);
8093	if (CHECK(err, "invalid test_btf errno:%d", err)) {
8094		err = -1;
8095		goto done;
8096	}
8097
8098	raw_btf = btf_raw_create(&hdr_tmpl, test->expect.raw_types,
8099				 test->expect.str_sec,
8100				 test->expect.str_sec_size,
8101				 &raw_btf_size, &ret_expect_next_str);
8102	if (!raw_btf)
8103		return;
8104	expect_btf = btf__new((__u8 *)raw_btf, raw_btf_size);
8105	err = libbpf_get_error(expect_btf);
8106	free(raw_btf);
8107	if (CHECK(err, "invalid expect_btf errno:%d", err)) {
8108		err = -1;
8109		goto done;
8110	}
8111
8112	test->opts.sz = sizeof(test->opts);
8113	err = btf__dedup(test_btf, &test->opts);
8114	if (CHECK(err, "btf_dedup failed errno:%d", err)) {
8115		err = -1;
8116		goto done;
8117	}
8118
8119	test_btf_data = btf__raw_data(test_btf, &test_btf_size);
8120	expect_btf_data = btf__raw_data(expect_btf, &expect_btf_size);
8121	if (CHECK(test_btf_size != expect_btf_size,
8122		  "test_btf_size:%u != expect_btf_size:%u",
8123		  test_btf_size, expect_btf_size)) {
8124		err = -1;
8125		goto done;
8126	}
8127
8128	test_hdr = test_btf_data;
8129	test_strs = test_btf_data + sizeof(*test_hdr) + test_hdr->str_off;
8130	expect_hdr = expect_btf_data;
8131	expect_strs = expect_btf_data + sizeof(*test_hdr) + expect_hdr->str_off;
8132	if (CHECK(test_hdr->str_len != expect_hdr->str_len,
8133		  "test_hdr->str_len:%u != expect_hdr->str_len:%u",
8134		  test_hdr->str_len, expect_hdr->str_len)) {
8135		fprintf(stderr, "\ntest strings:\n");
8136		dump_btf_strings(test_strs, test_hdr->str_len);
8137		fprintf(stderr, "\nexpected strings:\n");
8138		dump_btf_strings(expect_strs, expect_hdr->str_len);
8139		err = -1;
8140		goto done;
8141	}
8142
8143	expect_str_cur = expect_strs;
8144	expect_str_end = expect_strs + expect_hdr->str_len;
8145	while (expect_str_cur < expect_str_end) {
8146		size_t test_len, expect_len;
8147		int off;
8148
8149		off = btf__find_str(test_btf, expect_str_cur);
8150		if (CHECK(off < 0, "exp str '%s' not found: %d\n", expect_str_cur, off)) {
8151			err = -1;
8152			goto done;
8153		}
8154		test_str_cur = btf__str_by_offset(test_btf, off);
8155
8156		test_len = strlen(test_str_cur);
8157		expect_len = strlen(expect_str_cur);
8158		if (CHECK(test_len != expect_len,
8159			  "test_len:%zu != expect_len:%zu "
8160			  "(test_str:%s, expect_str:%s)",
8161			  test_len, expect_len, test_str_cur, expect_str_cur)) {
8162			err = -1;
8163			goto done;
8164		}
8165		if (CHECK(strcmp(test_str_cur, expect_str_cur),
8166			  "test_str:%s != expect_str:%s",
8167			  test_str_cur, expect_str_cur)) {
8168			err = -1;
8169			goto done;
8170		}
8171		expect_str_cur += expect_len + 1;
8172	}
8173
8174	test_nr_types = btf__type_cnt(test_btf);
8175	expect_nr_types = btf__type_cnt(expect_btf);
8176	if (CHECK(test_nr_types != expect_nr_types,
8177		  "test_nr_types:%u != expect_nr_types:%u",
8178		  test_nr_types, expect_nr_types)) {
8179		err = -1;
8180		goto done;
8181	}
8182
8183	for (i = 1; i < test_nr_types; i++) {
8184		const struct btf_type *test_type, *expect_type;
8185		int test_size, expect_size;
8186
8187		test_type = btf__type_by_id(test_btf, i);
8188		expect_type = btf__type_by_id(expect_btf, i);
8189		test_size = btf_type_size(test_type);
8190		expect_size = btf_type_size(expect_type);
8191
8192		if (CHECK(test_size != expect_size,
8193			  "type #%d: test_size:%d != expect_size:%u",
8194			  i, test_size, expect_size)) {
8195			err = -1;
8196			goto done;
8197		}
8198		if (CHECK(btf_kind(test_type) != btf_kind(expect_type),
8199			  "type %d kind: exp %d != got %u\n",
8200			  i, btf_kind(expect_type), btf_kind(test_type))) {
8201			err = -1;
8202			goto done;
8203		}
8204		if (CHECK(test_type->info != expect_type->info,
8205			  "type %d info: exp %d != got %u\n",
8206			  i, expect_type->info, test_type->info)) {
8207			err = -1;
8208			goto done;
8209		}
8210		if (CHECK(test_type->size != expect_type->size,
8211			  "type %d size/type: exp %d != got %u\n",
8212			  i, expect_type->size, test_type->size)) {
8213			err = -1;
8214			goto done;
8215		}
8216	}
8217
8218done:
8219	btf__free(test_btf);
8220	btf__free(expect_btf);
8221}
8222
8223void test_btf(void)
8224{
8225	int i;
8226
8227	always_log = env.verbosity > VERBOSE_NONE;
8228
8229	for (i = 1; i <= ARRAY_SIZE(raw_tests); i++)
8230		do_test_raw(i);
8231	for (i = 1; i <= ARRAY_SIZE(get_info_tests); i++)
8232		do_test_get_info(i);
8233	for (i = 1; i <= ARRAY_SIZE(file_tests); i++)
8234		do_test_file(i);
8235	for (i = 1; i <= ARRAY_SIZE(info_raw_tests); i++)
8236		do_test_info_raw(i);
8237	for (i = 1; i <= ARRAY_SIZE(dedup_tests); i++)
8238		do_test_dedup(i);
8239	test_pprint();
8240}
v6.2
   1/* SPDX-License-Identifier: GPL-2.0 */
   2/* Copyright (c) 2018 Facebook */
   3
   4#include <linux/bpf.h>
   5#include <linux/btf.h>
   6#include <linux/err.h>
   7#include <linux/kernel.h>
   8#include <linux/filter.h>
   9#include <linux/unistd.h>
  10#include <bpf/bpf.h>
  11#include <libelf.h>
  12#include <gelf.h>
  13#include <string.h>
  14#include <stdlib.h>
  15#include <stdio.h>
  16#include <stdarg.h>
  17#include <unistd.h>
  18#include <fcntl.h>
  19#include <errno.h>
  20#include <assert.h>
  21#include <bpf/libbpf.h>
  22#include <bpf/btf.h>
  23
  24#include "bpf_util.h"
  25#include "../test_btf.h"
  26#include "test_progs.h"
  27
  28#define MAX_INSNS	512
  29#define MAX_SUBPROGS	16
  30
  31static int duration = 0;
  32static bool always_log;
  33
  34#undef CHECK
  35#define CHECK(condition, format...) _CHECK(condition, "check", duration, format)
  36
  37#define NAME_TBD 0xdeadb33f
  38
  39#define NAME_NTH(N) (0xfffe0000 | N)
  40#define IS_NAME_NTH(X) ((X & 0xffff0000) == 0xfffe0000)
  41#define GET_NAME_NTH_IDX(X) (X & 0x0000ffff)
  42
  43#define MAX_NR_RAW_U32 1024
  44#define BTF_LOG_BUF_SIZE 65535
  45
  46static char btf_log_buf[BTF_LOG_BUF_SIZE];
  47
  48static struct btf_header hdr_tmpl = {
  49	.magic = BTF_MAGIC,
  50	.version = BTF_VERSION,
  51	.hdr_len = sizeof(struct btf_header),
  52};
  53
  54/* several different mapv kinds(types) supported by pprint */
  55enum pprint_mapv_kind_t {
  56	PPRINT_MAPV_KIND_BASIC = 0,
  57	PPRINT_MAPV_KIND_INT128,
  58};
  59
  60struct btf_raw_test {
  61	const char *descr;
  62	const char *str_sec;
  63	const char *map_name;
  64	const char *err_str;
  65	__u32 raw_types[MAX_NR_RAW_U32];
  66	__u32 str_sec_size;
  67	enum bpf_map_type map_type;
  68	__u32 key_size;
  69	__u32 value_size;
  70	__u32 key_type_id;
  71	__u32 value_type_id;
  72	__u32 max_entries;
  73	bool btf_load_err;
  74	bool map_create_err;
  75	bool ordered_map;
  76	bool lossless_map;
  77	bool percpu_map;
  78	int hdr_len_delta;
  79	int type_off_delta;
  80	int str_off_delta;
  81	int str_len_delta;
  82	enum pprint_mapv_kind_t mapv_kind;
  83};
  84
  85#define BTF_STR_SEC(str) \
  86	.str_sec = str, .str_sec_size = sizeof(str)
  87
  88static struct btf_raw_test raw_tests[] = {
  89/* enum E {
  90 *     E0,
  91 *     E1,
  92 * };
  93 *
  94 * struct A {
  95 *	unsigned long long m;
  96 *	int n;
  97 *	char o;
  98 *	[3 bytes hole]
  99 *	int p[8];
 100 *	int q[4][8];
 101 *	enum E r;
 102 * };
 103 */
 104{
 105	.descr = "struct test #1",
 106	.raw_types = {
 107		/* int */
 108		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 109		/* unsigned long long */
 110		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
 111		/* char */
 112		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
 113		/* int[8] */
 114		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
 115		/* struct A { */				/* [5] */
 116		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 6), 180),
 117		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
 118		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
 119		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
 120		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
 121		BTF_MEMBER_ENC(NAME_TBD, 6, 384),/* int q[4][8]		*/
 122		BTF_MEMBER_ENC(NAME_TBD, 7, 1408), /* enum E r		*/
 123		/* } */
 124		/* int[4][8] */
 125		BTF_TYPE_ARRAY_ENC(4, 1, 4),			/* [6] */
 126		/* enum E */					/* [7] */
 127		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), sizeof(int)),
 128		BTF_ENUM_ENC(NAME_TBD, 0),
 129		BTF_ENUM_ENC(NAME_TBD, 1),
 130		BTF_END_RAW,
 131	},
 132	.str_sec = "\0A\0m\0n\0o\0p\0q\0r\0E\0E0\0E1",
 133	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0q\0r\0E\0E0\0E1"),
 134	.map_type = BPF_MAP_TYPE_ARRAY,
 135	.map_name = "struct_test1_map",
 136	.key_size = sizeof(int),
 137	.value_size = 180,
 138	.key_type_id = 1,
 139	.value_type_id = 5,
 140	.max_entries = 4,
 141},
 142
 143/* typedef struct b Struct_B;
 144 *
 145 * struct A {
 146 *     int m;
 147 *     struct b n[4];
 148 *     const Struct_B o[4];
 149 * };
 150 *
 151 * struct B {
 152 *     int m;
 153 *     int n;
 154 * };
 155 */
 156{
 157	.descr = "struct test #2",
 158	.raw_types = {
 159		/* int */					/* [1] */
 160		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
 161		/* struct b [4] */				/* [2] */
 162		BTF_TYPE_ARRAY_ENC(4, 1, 4),
 163
 164		/* struct A { */				/* [3] */
 165		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 3), 68),
 166		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int m;		*/
 167		BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* struct B n[4]	*/
 168		BTF_MEMBER_ENC(NAME_TBD, 8, 288),/* const Struct_B o[4];*/
 169		/* } */
 170
 171		/* struct B { */				/* [4] */
 172		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
 173		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int m; */
 174		BTF_MEMBER_ENC(NAME_TBD, 1, 32),/* int n; */
 175		/* } */
 176
 177		/* const int */					/* [5] */
 178		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 1),
 179		/* typedef struct b Struct_B */	/* [6] */
 180		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), 4),
 181		/* const Struct_B */				/* [7] */
 182		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 6),
 183		/* const Struct_B [4] */			/* [8] */
 184		BTF_TYPE_ARRAY_ENC(7, 1, 4),
 185		BTF_END_RAW,
 186	},
 187	.str_sec = "\0A\0m\0n\0o\0B\0m\0n\0Struct_B",
 188	.str_sec_size = sizeof("\0A\0m\0n\0o\0B\0m\0n\0Struct_B"),
 189	.map_type = BPF_MAP_TYPE_ARRAY,
 190	.map_name = "struct_test2_map",
 191	.key_size = sizeof(int),
 192	.value_size = 68,
 193	.key_type_id = 1,
 194	.value_type_id = 3,
 195	.max_entries = 4,
 196},
 197{
 198	.descr = "struct test #3 Invalid member offset",
 199	.raw_types = {
 200		/* int */					/* [1] */
 201		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
 202		/* int64 */					/* [2] */
 203		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 64, 8),
 204
 205		/* struct A { */				/* [3] */
 206		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 16),
 207		BTF_MEMBER_ENC(NAME_TBD, 1, 64),	/* int m;		*/
 208		BTF_MEMBER_ENC(NAME_TBD, 2, 0),		/* int64 n; */
 209		/* } */
 210		BTF_END_RAW,
 211	},
 212	.str_sec = "\0A\0m\0n\0",
 213	.str_sec_size = sizeof("\0A\0m\0n\0"),
 214	.map_type = BPF_MAP_TYPE_ARRAY,
 215	.map_name = "struct_test3_map",
 216	.key_size = sizeof(int),
 217	.value_size = 16,
 218	.key_type_id = 1,
 219	.value_type_id = 3,
 220	.max_entries = 4,
 221	.btf_load_err = true,
 222	.err_str = "Invalid member bits_offset",
 223},
 224/*
 225 * struct A {
 226 *	unsigned long long m;
 227 *	int n;
 228 *	char o;
 229 *	[3 bytes hole]
 230 *	int p[8];
 231 * };
 232 */
 233{
 234	.descr = "global data test #1",
 235	.raw_types = {
 236		/* int */
 237		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 238		/* unsigned long long */
 239		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
 240		/* char */
 241		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
 242		/* int[8] */
 243		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
 244		/* struct A { */				/* [5] */
 245		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
 246		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
 247		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
 248		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
 249		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
 250		/* } */
 251		BTF_END_RAW,
 252	},
 253	.str_sec = "\0A\0m\0n\0o\0p",
 254	.str_sec_size = sizeof("\0A\0m\0n\0o\0p"),
 255	.map_type = BPF_MAP_TYPE_ARRAY,
 256	.map_name = "struct_test1_map",
 257	.key_size = sizeof(int),
 258	.value_size = 48,
 259	.key_type_id = 1,
 260	.value_type_id = 5,
 261	.max_entries = 4,
 262},
 263/*
 264 * struct A {
 265 *	unsigned long long m;
 266 *	int n;
 267 *	char o;
 268 *	[3 bytes hole]
 269 *	int p[8];
 270 * };
 271 * static struct A t; <- in .bss
 272 */
 273{
 274	.descr = "global data test #2",
 275	.raw_types = {
 276		/* int */
 277		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 278		/* unsigned long long */
 279		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
 280		/* char */
 281		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
 282		/* int[8] */
 283		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
 284		/* struct A { */				/* [5] */
 285		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
 286		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
 287		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
 288		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
 289		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
 290		/* } */
 291		/* static struct A t */
 292		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
 293		/* .bss section */				/* [7] */
 294		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 48),
 295		BTF_VAR_SECINFO_ENC(6, 0, 48),
 296		BTF_END_RAW,
 297	},
 298	.str_sec = "\0A\0m\0n\0o\0p\0t\0.bss",
 299	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0.bss"),
 300	.map_type = BPF_MAP_TYPE_ARRAY,
 301	.map_name = ".bss",
 302	.key_size = sizeof(int),
 303	.value_size = 48,
 304	.key_type_id = 0,
 305	.value_type_id = 7,
 306	.max_entries = 1,
 307},
 308{
 309	.descr = "global data test #3",
 310	.raw_types = {
 311		/* int */
 312		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 313		/* static int t */
 314		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
 315		/* .bss section */				/* [3] */
 316		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
 317		BTF_VAR_SECINFO_ENC(2, 0, 4),
 318		BTF_END_RAW,
 319	},
 320	.str_sec = "\0t\0.bss",
 321	.str_sec_size = sizeof("\0t\0.bss"),
 322	.map_type = BPF_MAP_TYPE_ARRAY,
 323	.map_name = ".bss",
 324	.key_size = sizeof(int),
 325	.value_size = 4,
 326	.key_type_id = 0,
 327	.value_type_id = 3,
 328	.max_entries = 1,
 329},
 330{
 331	.descr = "global data test #4, unsupported linkage",
 332	.raw_types = {
 333		/* int */
 334		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 335		/* static int t */
 336		BTF_VAR_ENC(NAME_TBD, 1, 2),			/* [2] */
 337		/* .bss section */				/* [3] */
 338		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
 339		BTF_VAR_SECINFO_ENC(2, 0, 4),
 340		BTF_END_RAW,
 341	},
 342	.str_sec = "\0t\0.bss",
 343	.str_sec_size = sizeof("\0t\0.bss"),
 344	.map_type = BPF_MAP_TYPE_ARRAY,
 345	.map_name = ".bss",
 346	.key_size = sizeof(int),
 347	.value_size = 4,
 348	.key_type_id = 0,
 349	.value_type_id = 3,
 350	.max_entries = 1,
 351	.btf_load_err = true,
 352	.err_str = "Linkage not supported",
 353},
 354{
 355	.descr = "global data test #5, invalid var type",
 356	.raw_types = {
 357		/* static void t */
 358		BTF_VAR_ENC(NAME_TBD, 0, 0),			/* [1] */
 359		/* .bss section */				/* [2] */
 360		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
 361		BTF_VAR_SECINFO_ENC(1, 0, 4),
 362		BTF_END_RAW,
 363	},
 364	.str_sec = "\0t\0.bss",
 365	.str_sec_size = sizeof("\0t\0.bss"),
 366	.map_type = BPF_MAP_TYPE_ARRAY,
 367	.map_name = ".bss",
 368	.key_size = sizeof(int),
 369	.value_size = 4,
 370	.key_type_id = 0,
 371	.value_type_id = 2,
 372	.max_entries = 1,
 373	.btf_load_err = true,
 374	.err_str = "Invalid type_id",
 375},
 376{
 377	.descr = "global data test #6, invalid var type (fwd type)",
 378	.raw_types = {
 379		/* union A */
 380		BTF_TYPE_ENC(NAME_TBD,
 381			     BTF_INFO_ENC(BTF_KIND_FWD, 1, 0), 0), /* [1] */
 382		/* static union A t */
 383		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
 384		/* .bss section */				/* [3] */
 385		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
 386		BTF_VAR_SECINFO_ENC(2, 0, 4),
 387		BTF_END_RAW,
 388	},
 389	.str_sec = "\0A\0t\0.bss",
 390	.str_sec_size = sizeof("\0A\0t\0.bss"),
 391	.map_type = BPF_MAP_TYPE_ARRAY,
 392	.map_name = ".bss",
 393	.key_size = sizeof(int),
 394	.value_size = 4,
 395	.key_type_id = 0,
 396	.value_type_id = 2,
 397	.max_entries = 1,
 398	.btf_load_err = true,
 399	.err_str = "Invalid type",
 400},
 401{
 402	.descr = "global data test #7, invalid var type (fwd type)",
 403	.raw_types = {
 404		/* union A */
 405		BTF_TYPE_ENC(NAME_TBD,
 406			     BTF_INFO_ENC(BTF_KIND_FWD, 1, 0), 0), /* [1] */
 407		/* static union A t */
 408		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
 409		/* .bss section */				/* [3] */
 410		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
 411		BTF_VAR_SECINFO_ENC(1, 0, 4),
 412		BTF_END_RAW,
 413	},
 414	.str_sec = "\0A\0t\0.bss",
 415	.str_sec_size = sizeof("\0A\0t\0.bss"),
 416	.map_type = BPF_MAP_TYPE_ARRAY,
 417	.map_name = ".bss",
 418	.key_size = sizeof(int),
 419	.value_size = 4,
 420	.key_type_id = 0,
 421	.value_type_id = 2,
 422	.max_entries = 1,
 423	.btf_load_err = true,
 424	.err_str = "Invalid type",
 425},
 426{
 427	.descr = "global data test #8, invalid var size",
 428	.raw_types = {
 429		/* int */
 430		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 431		/* unsigned long long */
 432		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
 433		/* char */
 434		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
 435		/* int[8] */
 436		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
 437		/* struct A { */				/* [5] */
 438		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
 439		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
 440		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
 441		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
 442		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
 443		/* } */
 444		/* static struct A t */
 445		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
 446		/* .bss section */				/* [7] */
 447		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 48),
 448		BTF_VAR_SECINFO_ENC(6, 0, 47),
 449		BTF_END_RAW,
 450	},
 451	.str_sec = "\0A\0m\0n\0o\0p\0t\0.bss",
 452	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0.bss"),
 453	.map_type = BPF_MAP_TYPE_ARRAY,
 454	.map_name = ".bss",
 455	.key_size = sizeof(int),
 456	.value_size = 48,
 457	.key_type_id = 0,
 458	.value_type_id = 7,
 459	.max_entries = 1,
 460	.btf_load_err = true,
 461	.err_str = "Invalid size",
 462},
 463{
 464	.descr = "global data test #9, invalid var size",
 465	.raw_types = {
 466		/* int */
 467		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 468		/* unsigned long long */
 469		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
 470		/* char */
 471		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
 472		/* int[8] */
 473		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
 474		/* struct A { */				/* [5] */
 475		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
 476		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
 477		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
 478		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
 479		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
 480		/* } */
 481		/* static struct A t */
 482		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
 483		/* .bss section */				/* [7] */
 484		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 46),
 485		BTF_VAR_SECINFO_ENC(6, 0, 48),
 486		BTF_END_RAW,
 487	},
 488	.str_sec = "\0A\0m\0n\0o\0p\0t\0.bss",
 489	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0.bss"),
 490	.map_type = BPF_MAP_TYPE_ARRAY,
 491	.map_name = ".bss",
 492	.key_size = sizeof(int),
 493	.value_size = 48,
 494	.key_type_id = 0,
 495	.value_type_id = 7,
 496	.max_entries = 1,
 497	.btf_load_err = true,
 498	.err_str = "Invalid size",
 499},
 500{
 501	.descr = "global data test #10, invalid var size",
 502	.raw_types = {
 503		/* int */
 504		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 505		/* unsigned long long */
 506		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
 507		/* char */
 508		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
 509		/* int[8] */
 510		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
 511		/* struct A { */				/* [5] */
 512		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
 513		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
 514		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
 515		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
 516		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
 517		/* } */
 518		/* static struct A t */
 519		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
 520		/* .bss section */				/* [7] */
 521		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 46),
 522		BTF_VAR_SECINFO_ENC(6, 0, 46),
 523		BTF_END_RAW,
 524	},
 525	.str_sec = "\0A\0m\0n\0o\0p\0t\0.bss",
 526	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0.bss"),
 527	.map_type = BPF_MAP_TYPE_ARRAY,
 528	.map_name = ".bss",
 529	.key_size = sizeof(int),
 530	.value_size = 48,
 531	.key_type_id = 0,
 532	.value_type_id = 7,
 533	.max_entries = 1,
 534	.btf_load_err = true,
 535	.err_str = "Invalid size",
 536},
 537{
 538	.descr = "global data test #11, multiple section members",
 539	.raw_types = {
 540		/* int */
 541		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 542		/* unsigned long long */
 543		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
 544		/* char */
 545		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
 546		/* int[8] */
 547		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
 548		/* struct A { */				/* [5] */
 549		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
 550		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
 551		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
 552		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
 553		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
 554		/* } */
 555		/* static struct A t */
 556		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
 557		/* static int u */
 558		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [7] */
 559		/* .bss section */				/* [8] */
 560		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 2), 62),
 561		BTF_VAR_SECINFO_ENC(6, 10, 48),
 562		BTF_VAR_SECINFO_ENC(7, 58, 4),
 563		BTF_END_RAW,
 564	},
 565	.str_sec = "\0A\0m\0n\0o\0p\0t\0u\0.bss",
 566	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0u\0.bss"),
 567	.map_type = BPF_MAP_TYPE_ARRAY,
 568	.map_name = ".bss",
 569	.key_size = sizeof(int),
 570	.value_size = 62,
 571	.key_type_id = 0,
 572	.value_type_id = 8,
 573	.max_entries = 1,
 574},
 575{
 576	.descr = "global data test #12, invalid offset",
 577	.raw_types = {
 578		/* int */
 579		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 580		/* unsigned long long */
 581		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
 582		/* char */
 583		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
 584		/* int[8] */
 585		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
 586		/* struct A { */				/* [5] */
 587		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
 588		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
 589		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
 590		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
 591		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
 592		/* } */
 593		/* static struct A t */
 594		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
 595		/* static int u */
 596		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [7] */
 597		/* .bss section */				/* [8] */
 598		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 2), 62),
 599		BTF_VAR_SECINFO_ENC(6, 10, 48),
 600		BTF_VAR_SECINFO_ENC(7, 60, 4),
 601		BTF_END_RAW,
 602	},
 603	.str_sec = "\0A\0m\0n\0o\0p\0t\0u\0.bss",
 604	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0u\0.bss"),
 605	.map_type = BPF_MAP_TYPE_ARRAY,
 606	.map_name = ".bss",
 607	.key_size = sizeof(int),
 608	.value_size = 62,
 609	.key_type_id = 0,
 610	.value_type_id = 8,
 611	.max_entries = 1,
 612	.btf_load_err = true,
 613	.err_str = "Invalid offset+size",
 614},
 615{
 616	.descr = "global data test #13, invalid offset",
 617	.raw_types = {
 618		/* int */
 619		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 620		/* unsigned long long */
 621		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
 622		/* char */
 623		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
 624		/* int[8] */
 625		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
 626		/* struct A { */				/* [5] */
 627		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
 628		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
 629		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
 630		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
 631		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
 632		/* } */
 633		/* static struct A t */
 634		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
 635		/* static int u */
 636		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [7] */
 637		/* .bss section */				/* [8] */
 638		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 2), 62),
 639		BTF_VAR_SECINFO_ENC(6, 10, 48),
 640		BTF_VAR_SECINFO_ENC(7, 12, 4),
 641		BTF_END_RAW,
 642	},
 643	.str_sec = "\0A\0m\0n\0o\0p\0t\0u\0.bss",
 644	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0u\0.bss"),
 645	.map_type = BPF_MAP_TYPE_ARRAY,
 646	.map_name = ".bss",
 647	.key_size = sizeof(int),
 648	.value_size = 62,
 649	.key_type_id = 0,
 650	.value_type_id = 8,
 651	.max_entries = 1,
 652	.btf_load_err = true,
 653	.err_str = "Invalid offset",
 654},
 655{
 656	.descr = "global data test #14, invalid offset",
 657	.raw_types = {
 658		/* int */
 659		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 660		/* unsigned long long */
 661		BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),		/* [2] */
 662		/* char */
 663		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),	/* [3] */
 664		/* int[8] */
 665		BTF_TYPE_ARRAY_ENC(1, 1, 8),			/* [4] */
 666		/* struct A { */				/* [5] */
 667		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 4), 48),
 668		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* unsigned long long m;*/
 669		BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n;		*/
 670		BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o;		*/
 671		BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8]		*/
 672		/* } */
 673		/* static struct A t */
 674		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [6] */
 675		/* static int u */
 676		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [7] */
 677		/* .bss section */				/* [8] */
 678		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 2), 62),
 679		BTF_VAR_SECINFO_ENC(7, 58, 4),
 680		BTF_VAR_SECINFO_ENC(6, 10, 48),
 681		BTF_END_RAW,
 682	},
 683	.str_sec = "\0A\0m\0n\0o\0p\0t\0u\0.bss",
 684	.str_sec_size = sizeof("\0A\0m\0n\0o\0p\0t\0u\0.bss"),
 685	.map_type = BPF_MAP_TYPE_ARRAY,
 686	.map_name = ".bss",
 687	.key_size = sizeof(int),
 688	.value_size = 62,
 689	.key_type_id = 0,
 690	.value_type_id = 8,
 691	.max_entries = 1,
 692	.btf_load_err = true,
 693	.err_str = "Invalid offset",
 694},
 695{
 696	.descr = "global data test #15, not var kind",
 697	.raw_types = {
 698		/* int */
 699		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 700		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
 701		/* .bss section */				/* [3] */
 702		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
 703		BTF_VAR_SECINFO_ENC(1, 0, 4),
 704		BTF_END_RAW,
 705	},
 706	.str_sec = "\0A\0t\0.bss",
 707	.str_sec_size = sizeof("\0A\0t\0.bss"),
 708	.map_type = BPF_MAP_TYPE_ARRAY,
 709	.map_name = ".bss",
 710	.key_size = sizeof(int),
 711	.value_size = 4,
 712	.key_type_id = 0,
 713	.value_type_id = 3,
 714	.max_entries = 1,
 715	.btf_load_err = true,
 716	.err_str = "Not a VAR kind member",
 717},
 718{
 719	.descr = "global data test #16, invalid var referencing sec",
 720	.raw_types = {
 721		/* int */
 722		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 723		BTF_VAR_ENC(NAME_TBD, 5, 0),			/* [2] */
 724		BTF_VAR_ENC(NAME_TBD, 2, 0),			/* [3] */
 725		/* a section */					/* [4] */
 726		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
 727		BTF_VAR_SECINFO_ENC(3, 0, 4),
 728		/* a section */					/* [5] */
 729		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
 730		BTF_VAR_SECINFO_ENC(6, 0, 4),
 731		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [6] */
 732		BTF_END_RAW,
 733	},
 734	.str_sec = "\0A\0t\0s\0a\0a",
 735	.str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
 736	.map_type = BPF_MAP_TYPE_ARRAY,
 737	.map_name = ".bss",
 738	.key_size = sizeof(int),
 739	.value_size = 4,
 740	.key_type_id = 0,
 741	.value_type_id = 4,
 742	.max_entries = 1,
 743	.btf_load_err = true,
 744	.err_str = "Invalid type_id",
 745},
 746{
 747	.descr = "global data test #17, invalid var referencing var",
 748	.raw_types = {
 749		/* int */
 750		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 751		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
 752		BTF_VAR_ENC(NAME_TBD, 2, 0),			/* [3] */
 753		/* a section */					/* [4] */
 754		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
 755		BTF_VAR_SECINFO_ENC(3, 0, 4),
 756		BTF_END_RAW,
 757	},
 758	.str_sec = "\0A\0t\0s\0a\0a",
 759	.str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
 760	.map_type = BPF_MAP_TYPE_ARRAY,
 761	.map_name = ".bss",
 762	.key_size = sizeof(int),
 763	.value_size = 4,
 764	.key_type_id = 0,
 765	.value_type_id = 4,
 766	.max_entries = 1,
 767	.btf_load_err = true,
 768	.err_str = "Invalid type_id",
 769},
 770{
 771	.descr = "global data test #18, invalid var loop",
 772	.raw_types = {
 773		/* int */
 774		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 775		BTF_VAR_ENC(NAME_TBD, 2, 0),			/* [2] */
 776		/* .bss section */				/* [3] */
 777		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
 778		BTF_VAR_SECINFO_ENC(2, 0, 4),
 779		BTF_END_RAW,
 780	},
 781	.str_sec = "\0A\0t\0aaa",
 782	.str_sec_size = sizeof("\0A\0t\0aaa"),
 783	.map_type = BPF_MAP_TYPE_ARRAY,
 784	.map_name = ".bss",
 785	.key_size = sizeof(int),
 786	.value_size = 4,
 787	.key_type_id = 0,
 788	.value_type_id = 4,
 789	.max_entries = 1,
 790	.btf_load_err = true,
 791	.err_str = "Invalid type_id",
 792},
 793{
 794	.descr = "global data test #19, invalid var referencing var",
 795	.raw_types = {
 796		/* int */
 797		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 798		BTF_VAR_ENC(NAME_TBD, 3, 0),			/* [2] */
 799		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [3] */
 800		BTF_END_RAW,
 801	},
 802	.str_sec = "\0A\0t\0s\0a\0a",
 803	.str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
 804	.map_type = BPF_MAP_TYPE_ARRAY,
 805	.map_name = ".bss",
 806	.key_size = sizeof(int),
 807	.value_size = 4,
 808	.key_type_id = 0,
 809	.value_type_id = 4,
 810	.max_entries = 1,
 811	.btf_load_err = true,
 812	.err_str = "Invalid type_id",
 813},
 814{
 815	.descr = "global data test #20, invalid ptr referencing var",
 816	.raw_types = {
 817		/* int */
 818		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 819		/* PTR type_id=3	*/			/* [2] */
 820		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 3),
 821		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [3] */
 822		BTF_END_RAW,
 823	},
 824	.str_sec = "\0A\0t\0s\0a\0a",
 825	.str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
 826	.map_type = BPF_MAP_TYPE_ARRAY,
 827	.map_name = ".bss",
 828	.key_size = sizeof(int),
 829	.value_size = 4,
 830	.key_type_id = 0,
 831	.value_type_id = 4,
 832	.max_entries = 1,
 833	.btf_load_err = true,
 834	.err_str = "Invalid type_id",
 835},
 836{
 837	.descr = "global data test #21, var included in struct",
 838	.raw_types = {
 839		/* int */
 840		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 841		/* struct A { */				/* [2] */
 842		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 2),
 843		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int m; */
 844		BTF_MEMBER_ENC(NAME_TBD, 3, 32),/* VAR type_id=3; */
 845		/* } */
 846		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [3] */
 847		BTF_END_RAW,
 848	},
 849	.str_sec = "\0A\0t\0s\0a\0a",
 850	.str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
 851	.map_type = BPF_MAP_TYPE_ARRAY,
 852	.map_name = ".bss",
 853	.key_size = sizeof(int),
 854	.value_size = 4,
 855	.key_type_id = 0,
 856	.value_type_id = 4,
 857	.max_entries = 1,
 858	.btf_load_err = true,
 859	.err_str = "Invalid member",
 860},
 861{
 862	.descr = "global data test #22, array of var",
 863	.raw_types = {
 864		/* int */
 865		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
 866		BTF_TYPE_ARRAY_ENC(3, 1, 4),			/* [2] */
 867		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [3] */
 868		BTF_END_RAW,
 869	},
 870	.str_sec = "\0A\0t\0s\0a\0a",
 871	.str_sec_size = sizeof("\0A\0t\0s\0a\0a"),
 872	.map_type = BPF_MAP_TYPE_ARRAY,
 873	.map_name = ".bss",
 874	.key_size = sizeof(int),
 875	.value_size = 4,
 876	.key_type_id = 0,
 877	.value_type_id = 4,
 878	.max_entries = 1,
 879	.btf_load_err = true,
 880	.err_str = "Invalid elem",
 881},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 882/* Test member exceeds the size of struct.
 883 *
 884 * struct A {
 885 *     int m;
 886 *     int n;
 887 * };
 888 */
 889{
 890	.descr = "size check test #1",
 891	.raw_types = {
 892		/* int */					/* [1] */
 893		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
 894		/* struct A { */				/* [2] */
 895		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 2 -  1),
 896		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int m; */
 897		BTF_MEMBER_ENC(NAME_TBD, 1, 32),/* int n; */
 898		/* } */
 899		BTF_END_RAW,
 900	},
 901	.str_sec = "\0A\0m\0n",
 902	.str_sec_size = sizeof("\0A\0m\0n"),
 903	.map_type = BPF_MAP_TYPE_ARRAY,
 904	.map_name = "size_check1_map",
 905	.key_size = sizeof(int),
 906	.value_size = 1,
 907	.key_type_id = 1,
 908	.value_type_id = 2,
 909	.max_entries = 4,
 910	.btf_load_err = true,
 911	.err_str = "Member exceeds struct_size",
 912},
 913
 914/* Test member exceeds the size of struct
 915 *
 916 * struct A {
 917 *     int m;
 918 *     int n[2];
 919 * };
 920 */
 921{
 922	.descr = "size check test #2",
 923	.raw_types = {
 924		/* int */					/* [1] */
 925		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
 926		/* int[2] */					/* [2] */
 927		BTF_TYPE_ARRAY_ENC(1, 1, 2),
 928		/* struct A { */				/* [3] */
 929		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 3 - 1),
 930		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int m; */
 931		BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* int n[2]; */
 932		/* } */
 933		BTF_END_RAW,
 934	},
 935	.str_sec = "\0A\0m\0n",
 936	.str_sec_size = sizeof("\0A\0m\0n"),
 937	.map_type = BPF_MAP_TYPE_ARRAY,
 938	.map_name = "size_check2_map",
 939	.key_size = sizeof(int),
 940	.value_size = 1,
 941	.key_type_id = 1,
 942	.value_type_id = 3,
 943	.max_entries = 4,
 944	.btf_load_err = true,
 945	.err_str = "Member exceeds struct_size",
 946},
 947
 948/* Test member exceeds the size of struct
 949 *
 950 * struct A {
 951 *     int m;
 952 *     void *n;
 953 * };
 954 */
 955{
 956	.descr = "size check test #3",
 957	.raw_types = {
 958		/* int */					/* [1] */
 959		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
 960		/* void* */					/* [2] */
 961		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
 962		/* struct A { */				/* [3] */
 963		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) + sizeof(void *) - 1),
 964		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int m; */
 965		BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* void *n; */
 966		/* } */
 967		BTF_END_RAW,
 968	},
 969	.str_sec = "\0A\0m\0n",
 970	.str_sec_size = sizeof("\0A\0m\0n"),
 971	.map_type = BPF_MAP_TYPE_ARRAY,
 972	.map_name = "size_check3_map",
 973	.key_size = sizeof(int),
 974	.value_size = 1,
 975	.key_type_id = 1,
 976	.value_type_id = 3,
 977	.max_entries = 4,
 978	.btf_load_err = true,
 979	.err_str = "Member exceeds struct_size",
 980},
 981
 982/* Test member exceeds the size of struct
 983 *
 984 * enum E {
 985 *     E0,
 986 *     E1,
 987 * };
 988 *
 989 * struct A {
 990 *     int m;
 991 *     enum E n;
 992 * };
 993 */
 994{
 995	.descr = "size check test #4",
 996	.raw_types = {
 997		/* int */			/* [1] */
 998		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
 999		/* enum E { */			/* [2] */
1000		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), sizeof(int)),
1001		BTF_ENUM_ENC(NAME_TBD, 0),
1002		BTF_ENUM_ENC(NAME_TBD, 1),
1003		/* } */
1004		/* struct A { */		/* [3] */
1005		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 2 - 1),
1006		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int m; */
1007		BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* enum E n; */
1008		/* } */
1009		BTF_END_RAW,
1010	},
1011	.str_sec = "\0E\0E0\0E1\0A\0m\0n",
1012	.str_sec_size = sizeof("\0E\0E0\0E1\0A\0m\0n"),
1013	.map_type = BPF_MAP_TYPE_ARRAY,
1014	.map_name = "size_check4_map",
1015	.key_size = sizeof(int),
1016	.value_size = 1,
1017	.key_type_id = 1,
1018	.value_type_id = 3,
1019	.max_entries = 4,
1020	.btf_load_err = true,
1021	.err_str = "Member exceeds struct_size",
1022},
1023
1024/* Test member unexceeds the size of struct
1025 *
1026 * enum E {
1027 *     E0,
1028 *     E1,
1029 * };
1030 *
1031 * struct A {
1032 *     char m;
1033 *     enum E __attribute__((packed)) n;
1034 * };
1035 */
1036{
1037	.descr = "size check test #5",
1038	.raw_types = {
1039		/* int */			/* [1] */
1040		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
1041		/* char */			/* [2] */
1042		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),
1043		/* enum E { */			/* [3] */
1044		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), 1),
1045		BTF_ENUM_ENC(NAME_TBD, 0),
1046		BTF_ENUM_ENC(NAME_TBD, 1),
1047		/* } */
1048		/* struct A { */		/* [4] */
1049		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 2),
1050		BTF_MEMBER_ENC(NAME_TBD, 2, 0),	/* char m; */
1051		BTF_MEMBER_ENC(NAME_TBD, 3, 8),/* enum E __attribute__((packed)) n; */
1052		/* } */
1053		BTF_END_RAW,
1054	},
1055	.str_sec = "\0E\0E0\0E1\0A\0m\0n",
1056	.str_sec_size = sizeof("\0E\0E0\0E1\0A\0m\0n"),
1057	.map_type = BPF_MAP_TYPE_ARRAY,
1058	.map_name = "size_check5_map",
1059	.key_size = sizeof(int),
1060	.value_size = 2,
1061	.key_type_id = 1,
1062	.value_type_id = 4,
1063	.max_entries = 4,
1064},
1065
1066/* typedef const void * const_void_ptr;
1067 * struct A {
1068 *	const_void_ptr m;
1069 * };
1070 */
1071{
1072	.descr = "void test #1",
1073	.raw_types = {
1074		/* int */		/* [1] */
1075		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1076		/* const void */	/* [2] */
1077		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1078		/* const void* */	/* [3] */
1079		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 2),
1080		/* typedef const void * const_void_ptr */
1081		BTF_TYPEDEF_ENC(NAME_TBD, 3),	/* [4] */
1082		/* struct A { */	/* [5] */
1083		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
1084		/* const_void_ptr m; */
1085		BTF_MEMBER_ENC(NAME_TBD, 4, 0),
1086		/* } */
1087		BTF_END_RAW,
1088	},
1089	.str_sec = "\0const_void_ptr\0A\0m",
1090	.str_sec_size = sizeof("\0const_void_ptr\0A\0m"),
1091	.map_type = BPF_MAP_TYPE_ARRAY,
1092	.map_name = "void_test1_map",
1093	.key_size = sizeof(int),
1094	.value_size = sizeof(void *),
1095	.key_type_id = 1,
1096	.value_type_id = 4,
1097	.max_entries = 4,
1098},
1099
1100/* struct A {
1101 *     const void m;
1102 * };
1103 */
1104{
1105	.descr = "void test #2",
1106	.raw_types = {
1107		/* int */		/* [1] */
1108		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1109		/* const void */	/* [2] */
1110		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1111		/* struct A { */	/* [3] */
1112		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 8),
1113		/* const void m; */
1114		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
1115		/* } */
1116		BTF_END_RAW,
1117	},
1118	.str_sec = "\0A\0m",
1119	.str_sec_size = sizeof("\0A\0m"),
1120	.map_type = BPF_MAP_TYPE_ARRAY,
1121	.map_name = "void_test2_map",
1122	.key_size = sizeof(int),
1123	.value_size = sizeof(void *),
1124	.key_type_id = 1,
1125	.value_type_id = 3,
1126	.max_entries = 4,
1127	.btf_load_err = true,
1128	.err_str = "Invalid member",
1129},
1130
1131/* typedef const void * const_void_ptr;
1132 * const_void_ptr[4]
1133 */
1134{
1135	.descr = "void test #3",
1136	.raw_types = {
1137		/* int */		/* [1] */
1138		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1139		/* const void */	/* [2] */
1140		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1141		/* const void* */	/* [3] */
1142		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 2),
1143		/* typedef const void * const_void_ptr */
1144		BTF_TYPEDEF_ENC(NAME_TBD, 3),	/* [4] */
1145		/* const_void_ptr[4] */
1146		BTF_TYPE_ARRAY_ENC(4, 1, 4),	/* [5] */
1147		BTF_END_RAW,
1148	},
1149	.str_sec = "\0const_void_ptr",
1150	.str_sec_size = sizeof("\0const_void_ptr"),
1151	.map_type = BPF_MAP_TYPE_ARRAY,
1152	.map_name = "void_test3_map",
1153	.key_size = sizeof(int),
1154	.value_size = sizeof(void *) * 4,
1155	.key_type_id = 1,
1156	.value_type_id = 5,
1157	.max_entries = 4,
1158},
1159
1160/* const void[4]  */
1161{
1162	.descr = "void test #4",
1163	.raw_types = {
1164		/* int */		/* [1] */
1165		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1166		/* const void */	/* [2] */
1167		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1168		/* const void[4] */	/* [3] */
1169		BTF_TYPE_ARRAY_ENC(2, 1, 4),
1170		BTF_END_RAW,
1171	},
1172	.str_sec = "\0A\0m",
1173	.str_sec_size = sizeof("\0A\0m"),
1174	.map_type = BPF_MAP_TYPE_ARRAY,
1175	.map_name = "void_test4_map",
1176	.key_size = sizeof(int),
1177	.value_size = sizeof(void *) * 4,
1178	.key_type_id = 1,
1179	.value_type_id = 3,
1180	.max_entries = 4,
1181	.btf_load_err = true,
1182	.err_str = "Invalid elem",
1183},
1184
1185/* Array_A  <------------------+
1186 *     elem_type == Array_B    |
1187 *                    |        |
1188 *                    |        |
1189 * Array_B  <-------- +        |
1190 *      elem_type == Array A --+
1191 */
1192{
1193	.descr = "loop test #1",
1194	.raw_types = {
1195		/* int */			/* [1] */
1196		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1197		/* Array_A */			/* [2] */
1198		BTF_TYPE_ARRAY_ENC(3, 1, 8),
1199		/* Array_B */			/* [3] */
1200		BTF_TYPE_ARRAY_ENC(2, 1, 8),
1201		BTF_END_RAW,
1202	},
1203	.str_sec = "",
1204	.str_sec_size = sizeof(""),
1205	.map_type = BPF_MAP_TYPE_ARRAY,
1206	.map_name = "loop_test1_map",
1207	.key_size = sizeof(int),
1208	.value_size = sizeof(sizeof(int) * 8),
1209	.key_type_id = 1,
1210	.value_type_id = 2,
1211	.max_entries = 4,
1212	.btf_load_err = true,
1213	.err_str = "Loop detected",
1214},
1215
1216/* typedef is _before_ the BTF type of Array_A and Array_B
1217 *
1218 * typedef Array_B int_array;
1219 *
1220 * Array_A  <------------------+
1221 *     elem_type == int_array  |
1222 *                    |        |
1223 *                    |        |
1224 * Array_B  <-------- +        |
1225 *      elem_type == Array_A --+
1226 */
1227{
1228	.descr = "loop test #2",
1229	.raw_types = {
1230		/* int */
1231		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
1232		/* typedef Array_B int_array */
1233		BTF_TYPEDEF_ENC(1, 4),				/* [2] */
1234		/* Array_A */
1235		BTF_TYPE_ARRAY_ENC(2, 1, 8),			/* [3] */
1236		/* Array_B */
1237		BTF_TYPE_ARRAY_ENC(3, 1, 8),			/* [4] */
1238		BTF_END_RAW,
1239	},
1240	.str_sec = "\0int_array\0",
1241	.str_sec_size = sizeof("\0int_array"),
1242	.map_type = BPF_MAP_TYPE_ARRAY,
1243	.map_name = "loop_test2_map",
1244	.key_size = sizeof(int),
1245	.value_size = sizeof(sizeof(int) * 8),
1246	.key_type_id = 1,
1247	.value_type_id = 2,
1248	.max_entries = 4,
1249	.btf_load_err = true,
1250	.err_str = "Loop detected",
1251},
1252
1253/* Array_A  <------------------+
1254 *     elem_type == Array_B    |
1255 *                    |        |
1256 *                    |        |
1257 * Array_B  <-------- +        |
1258 *      elem_type == Array_A --+
1259 */
1260{
1261	.descr = "loop test #3",
1262	.raw_types = {
1263		/* int */				/* [1] */
1264		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1265		/* Array_A */				/* [2] */
1266		BTF_TYPE_ARRAY_ENC(3, 1, 8),
1267		/* Array_B */				/* [3] */
1268		BTF_TYPE_ARRAY_ENC(2, 1, 8),
1269		BTF_END_RAW,
1270	},
1271	.str_sec = "",
1272	.str_sec_size = sizeof(""),
1273	.map_type = BPF_MAP_TYPE_ARRAY,
1274	.map_name = "loop_test3_map",
1275	.key_size = sizeof(int),
1276	.value_size = sizeof(sizeof(int) * 8),
1277	.key_type_id = 1,
1278	.value_type_id = 2,
1279	.max_entries = 4,
1280	.btf_load_err = true,
1281	.err_str = "Loop detected",
1282},
1283
1284/* typedef is _between_ the BTF type of Array_A and Array_B
1285 *
1286 * typedef Array_B int_array;
1287 *
1288 * Array_A  <------------------+
1289 *     elem_type == int_array  |
1290 *                    |        |
1291 *                    |        |
1292 * Array_B  <-------- +        |
1293 *      elem_type == Array_A --+
1294 */
1295{
1296	.descr = "loop test #4",
1297	.raw_types = {
1298		/* int */				/* [1] */
1299		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1300		/* Array_A */				/* [2] */
1301		BTF_TYPE_ARRAY_ENC(3, 1, 8),
1302		/* typedef Array_B int_array */		/* [3] */
1303		BTF_TYPEDEF_ENC(NAME_TBD, 4),
1304		/* Array_B */				/* [4] */
1305		BTF_TYPE_ARRAY_ENC(2, 1, 8),
1306		BTF_END_RAW,
1307	},
1308	.str_sec = "\0int_array\0",
1309	.str_sec_size = sizeof("\0int_array"),
1310	.map_type = BPF_MAP_TYPE_ARRAY,
1311	.map_name = "loop_test4_map",
1312	.key_size = sizeof(int),
1313	.value_size = sizeof(sizeof(int) * 8),
1314	.key_type_id = 1,
1315	.value_type_id = 2,
1316	.max_entries = 4,
1317	.btf_load_err = true,
1318	.err_str = "Loop detected",
1319},
1320
1321/* typedef struct B Struct_B
1322 *
1323 * struct A {
1324 *     int x;
1325 *     Struct_B y;
1326 * };
1327 *
1328 * struct B {
1329 *     int x;
1330 *     struct A y;
1331 * };
1332 */
1333{
1334	.descr = "loop test #5",
1335	.raw_types = {
1336		/* int */
1337		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
1338		/* struct A */					/* [2] */
1339		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
1340		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int x;	*/
1341		BTF_MEMBER_ENC(NAME_TBD, 3, 32),/* Struct_B y;	*/
1342		/* typedef struct B Struct_B */
1343		BTF_TYPEDEF_ENC(NAME_TBD, 4),			/* [3] */
1344		/* struct B */					/* [4] */
1345		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
1346		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int x;	*/
1347		BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* struct A y;	*/
1348		BTF_END_RAW,
1349	},
1350	.str_sec = "\0A\0x\0y\0Struct_B\0B\0x\0y",
1351	.str_sec_size = sizeof("\0A\0x\0y\0Struct_B\0B\0x\0y"),
1352	.map_type = BPF_MAP_TYPE_ARRAY,
1353	.map_name = "loop_test5_map",
1354	.key_size = sizeof(int),
1355	.value_size = 8,
1356	.key_type_id = 1,
1357	.value_type_id = 2,
1358	.max_entries = 4,
1359	.btf_load_err = true,
1360	.err_str = "Loop detected",
1361},
1362
1363/* struct A {
1364 *     int x;
1365 *     struct A array_a[4];
1366 * };
1367 */
1368{
1369	.descr = "loop test #6",
1370	.raw_types = {
1371		/* int */
1372		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
1373		BTF_TYPE_ARRAY_ENC(3, 1, 4),			/* [2] */
1374		/* struct A */					/* [3] */
1375		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
1376		BTF_MEMBER_ENC(NAME_TBD, 1, 0),	/* int x;		*/
1377		BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* struct A array_a[4];	*/
1378		BTF_END_RAW,
1379	},
1380	.str_sec = "\0A\0x\0y",
1381	.str_sec_size = sizeof("\0A\0x\0y"),
1382	.map_type = BPF_MAP_TYPE_ARRAY,
1383	.map_name = "loop_test6_map",
1384	.key_size = sizeof(int),
1385	.value_size = 8,
1386	.key_type_id = 1,
1387	.value_type_id = 2,
1388	.max_entries = 4,
1389	.btf_load_err = true,
1390	.err_str = "Loop detected",
1391},
1392
1393{
1394	.descr = "loop test #7",
1395	.raw_types = {
1396		/* int */				/* [1] */
1397		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1398		/* struct A { */			/* [2] */
1399		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
1400		/*     const void *m;	*/
1401		BTF_MEMBER_ENC(NAME_TBD, 3, 0),
1402		/* CONST type_id=3	*/		/* [3] */
1403		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 4),
1404		/* PTR type_id=2	*/		/* [4] */
1405		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 3),
1406		BTF_END_RAW,
1407	},
1408	.str_sec = "\0A\0m",
1409	.str_sec_size = sizeof("\0A\0m"),
1410	.map_type = BPF_MAP_TYPE_ARRAY,
1411	.map_name = "loop_test7_map",
1412	.key_size = sizeof(int),
1413	.value_size = sizeof(void *),
1414	.key_type_id = 1,
1415	.value_type_id = 2,
1416	.max_entries = 4,
1417	.btf_load_err = true,
1418	.err_str = "Loop detected",
1419},
1420
1421{
1422	.descr = "loop test #8",
1423	.raw_types = {
1424		/* int */				/* [1] */
1425		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1426		/* struct A { */			/* [2] */
1427		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
1428		/*     const void *m;	*/
1429		BTF_MEMBER_ENC(NAME_TBD, 4, 0),
1430		/* struct B { */			/* [3] */
1431		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
1432		/*     const void *n;	*/
1433		BTF_MEMBER_ENC(NAME_TBD, 6, 0),
1434		/* CONST type_id=5	*/		/* [4] */
1435		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 5),
1436		/* PTR type_id=6	*/		/* [5] */
1437		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 6),
1438		/* CONST type_id=7	*/		/* [6] */
1439		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 7),
1440		/* PTR type_id=4	*/		/* [7] */
1441		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 4),
1442		BTF_END_RAW,
1443	},
1444	.str_sec = "\0A\0m\0B\0n",
1445	.str_sec_size = sizeof("\0A\0m\0B\0n"),
1446	.map_type = BPF_MAP_TYPE_ARRAY,
1447	.map_name = "loop_test8_map",
1448	.key_size = sizeof(int),
1449	.value_size = sizeof(void *),
1450	.key_type_id = 1,
1451	.value_type_id = 2,
1452	.max_entries = 4,
1453	.btf_load_err = true,
1454	.err_str = "Loop detected",
1455},
1456
1457{
1458	.descr = "string section does not end with null",
1459	.raw_types = {
1460		/* int */				/* [1] */
1461		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1462		BTF_END_RAW,
1463	},
1464	.str_sec = "\0int",
1465	.str_sec_size = sizeof("\0int") - 1,
1466	.map_type = BPF_MAP_TYPE_ARRAY,
1467	.map_name = "hdr_test_map",
1468	.key_size = sizeof(int),
1469	.value_size = sizeof(int),
1470	.key_type_id = 1,
1471	.value_type_id = 1,
1472	.max_entries = 4,
1473	.btf_load_err = true,
1474	.err_str = "Invalid string section",
1475},
1476
1477{
1478	.descr = "empty string section",
1479	.raw_types = {
1480		/* int */				/* [1] */
1481		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1482		BTF_END_RAW,
1483	},
1484	.str_sec = "",
1485	.str_sec_size = 0,
1486	.map_type = BPF_MAP_TYPE_ARRAY,
1487	.map_name = "hdr_test_map",
1488	.key_size = sizeof(int),
1489	.value_size = sizeof(int),
1490	.key_type_id = 1,
1491	.value_type_id = 1,
1492	.max_entries = 4,
1493	.btf_load_err = true,
1494	.err_str = "Invalid string section",
1495},
1496
1497{
1498	.descr = "empty type section",
1499	.raw_types = {
1500		BTF_END_RAW,
1501	},
1502	.str_sec = "\0int",
1503	.str_sec_size = sizeof("\0int"),
1504	.map_type = BPF_MAP_TYPE_ARRAY,
1505	.map_name = "hdr_test_map",
1506	.key_size = sizeof(int),
1507	.value_size = sizeof(int),
1508	.key_type_id = 1,
1509	.value_type_id = 1,
1510	.max_entries = 4,
1511	.btf_load_err = true,
1512	.err_str = "No type found",
1513},
1514
1515{
1516	.descr = "btf_header test. Longer hdr_len",
1517	.raw_types = {
1518		/* int */				/* [1] */
1519		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1520		BTF_END_RAW,
1521	},
1522	.str_sec = "\0int",
1523	.str_sec_size = sizeof("\0int"),
1524	.map_type = BPF_MAP_TYPE_ARRAY,
1525	.map_name = "hdr_test_map",
1526	.key_size = sizeof(int),
1527	.value_size = sizeof(int),
1528	.key_type_id = 1,
1529	.value_type_id = 1,
1530	.max_entries = 4,
1531	.btf_load_err = true,
1532	.hdr_len_delta = 4,
1533	.err_str = "Unsupported btf_header",
1534},
1535
1536{
1537	.descr = "btf_header test. Gap between hdr and type",
1538	.raw_types = {
1539		/* int */				/* [1] */
1540		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1541		BTF_END_RAW,
1542	},
1543	.str_sec = "\0int",
1544	.str_sec_size = sizeof("\0int"),
1545	.map_type = BPF_MAP_TYPE_ARRAY,
1546	.map_name = "hdr_test_map",
1547	.key_size = sizeof(int),
1548	.value_size = sizeof(int),
1549	.key_type_id = 1,
1550	.value_type_id = 1,
1551	.max_entries = 4,
1552	.btf_load_err = true,
1553	.type_off_delta = 4,
1554	.err_str = "Unsupported section found",
1555},
1556
1557{
1558	.descr = "btf_header test. Gap between type and str",
1559	.raw_types = {
1560		/* int */				/* [1] */
1561		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1562		BTF_END_RAW,
1563	},
1564	.str_sec = "\0int",
1565	.str_sec_size = sizeof("\0int"),
1566	.map_type = BPF_MAP_TYPE_ARRAY,
1567	.map_name = "hdr_test_map",
1568	.key_size = sizeof(int),
1569	.value_size = sizeof(int),
1570	.key_type_id = 1,
1571	.value_type_id = 1,
1572	.max_entries = 4,
1573	.btf_load_err = true,
1574	.str_off_delta = 4,
1575	.err_str = "Unsupported section found",
1576},
1577
1578{
1579	.descr = "btf_header test. Overlap between type and str",
1580	.raw_types = {
1581		/* int */				/* [1] */
1582		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1583		BTF_END_RAW,
1584	},
1585	.str_sec = "\0int",
1586	.str_sec_size = sizeof("\0int"),
1587	.map_type = BPF_MAP_TYPE_ARRAY,
1588	.map_name = "hdr_test_map",
1589	.key_size = sizeof(int),
1590	.value_size = sizeof(int),
1591	.key_type_id = 1,
1592	.value_type_id = 1,
1593	.max_entries = 4,
1594	.btf_load_err = true,
1595	.str_off_delta = -4,
1596	.err_str = "Section overlap found",
1597},
1598
1599{
1600	.descr = "btf_header test. Larger BTF size",
1601	.raw_types = {
1602		/* int */				/* [1] */
1603		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1604		BTF_END_RAW,
1605	},
1606	.str_sec = "\0int",
1607	.str_sec_size = sizeof("\0int"),
1608	.map_type = BPF_MAP_TYPE_ARRAY,
1609	.map_name = "hdr_test_map",
1610	.key_size = sizeof(int),
1611	.value_size = sizeof(int),
1612	.key_type_id = 1,
1613	.value_type_id = 1,
1614	.max_entries = 4,
1615	.btf_load_err = true,
1616	.str_len_delta = -4,
1617	.err_str = "Unsupported section found",
1618},
1619
1620{
1621	.descr = "btf_header test. Smaller BTF size",
1622	.raw_types = {
1623		/* int */				/* [1] */
1624		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
1625		BTF_END_RAW,
1626	},
1627	.str_sec = "\0int",
1628	.str_sec_size = sizeof("\0int"),
1629	.map_type = BPF_MAP_TYPE_ARRAY,
1630	.map_name = "hdr_test_map",
1631	.key_size = sizeof(int),
1632	.value_size = sizeof(int),
1633	.key_type_id = 1,
1634	.value_type_id = 1,
1635	.max_entries = 4,
1636	.btf_load_err = true,
1637	.str_len_delta = 4,
1638	.err_str = "Total section length too long",
1639},
1640
1641{
1642	.descr = "array test. index_type/elem_type \"int\"",
1643	.raw_types = {
1644		/* int */				/* [1] */
1645		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1646		/* int[16] */				/* [2] */
1647		BTF_TYPE_ARRAY_ENC(1, 1, 16),
1648		BTF_END_RAW,
1649	},
1650	.str_sec = "",
1651	.str_sec_size = sizeof(""),
1652	.map_type = BPF_MAP_TYPE_ARRAY,
1653	.map_name = "array_test_map",
1654	.key_size = sizeof(int),
1655	.value_size = sizeof(int),
1656	.key_type_id = 1,
1657	.value_type_id = 1,
1658	.max_entries = 4,
1659},
1660
1661{
1662	.descr = "array test. index_type/elem_type \"const int\"",
1663	.raw_types = {
1664		/* int */				/* [1] */
1665		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1666		/* int[16] */				/* [2] */
1667		BTF_TYPE_ARRAY_ENC(3, 3, 16),
1668		/* CONST type_id=1 */			/* [3] */
1669		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 1),
1670		BTF_END_RAW,
1671	},
1672	.str_sec = "",
1673	.str_sec_size = sizeof(""),
1674	.map_type = BPF_MAP_TYPE_ARRAY,
1675	.map_name = "array_test_map",
1676	.key_size = sizeof(int),
1677	.value_size = sizeof(int),
1678	.key_type_id = 1,
1679	.value_type_id = 1,
1680	.max_entries = 4,
1681},
1682
1683{
1684	.descr = "array test. index_type \"const int:31\"",
1685	.raw_types = {
1686		/* int */				/* [1] */
1687		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1688		/* int:31 */				/* [2] */
1689		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 31, 4),
1690		/* int[16] */				/* [3] */
1691		BTF_TYPE_ARRAY_ENC(1, 4, 16),
1692		/* CONST type_id=2 */			/* [4] */
1693		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 2),
1694		BTF_END_RAW,
1695	},
1696	.str_sec = "",
1697	.str_sec_size = sizeof(""),
1698	.map_type = BPF_MAP_TYPE_ARRAY,
1699	.map_name = "array_test_map",
1700	.key_size = sizeof(int),
1701	.value_size = sizeof(int),
1702	.key_type_id = 1,
1703	.value_type_id = 1,
1704	.max_entries = 4,
1705	.btf_load_err = true,
1706	.err_str = "Invalid index",
1707},
1708
1709{
1710	.descr = "array test. elem_type \"const int:31\"",
1711	.raw_types = {
1712		/* int */				/* [1] */
1713		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1714		/* int:31 */				/* [2] */
1715		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 31, 4),
1716		/* int[16] */				/* [3] */
1717		BTF_TYPE_ARRAY_ENC(4, 1, 16),
1718		/* CONST type_id=2 */			/* [4] */
1719		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 2),
1720		BTF_END_RAW,
1721	},
1722	.str_sec = "",
1723	.str_sec_size = sizeof(""),
1724	.map_type = BPF_MAP_TYPE_ARRAY,
1725	.map_name = "array_test_map",
1726	.key_size = sizeof(int),
1727	.value_size = sizeof(int),
1728	.key_type_id = 1,
1729	.value_type_id = 1,
1730	.max_entries = 4,
1731	.btf_load_err = true,
1732	.err_str = "Invalid array of int",
1733},
1734
1735{
1736	.descr = "array test. index_type \"void\"",
1737	.raw_types = {
1738		/* int */				/* [1] */
1739		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1740		/* int[16] */				/* [2] */
1741		BTF_TYPE_ARRAY_ENC(1, 0, 16),
1742		BTF_END_RAW,
1743	},
1744	.str_sec = "",
1745	.str_sec_size = sizeof(""),
1746	.map_type = BPF_MAP_TYPE_ARRAY,
1747	.map_name = "array_test_map",
1748	.key_size = sizeof(int),
1749	.value_size = sizeof(int),
1750	.key_type_id = 1,
1751	.value_type_id = 1,
1752	.max_entries = 4,
1753	.btf_load_err = true,
1754	.err_str = "Invalid index",
1755},
1756
1757{
1758	.descr = "array test. index_type \"const void\"",
1759	.raw_types = {
1760		/* int */				/* [1] */
1761		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1762		/* int[16] */				/* [2] */
1763		BTF_TYPE_ARRAY_ENC(1, 3, 16),
1764		/* CONST type_id=0 (void) */		/* [3] */
1765		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1766		BTF_END_RAW,
1767	},
1768	.str_sec = "",
1769	.str_sec_size = sizeof(""),
1770	.map_type = BPF_MAP_TYPE_ARRAY,
1771	.map_name = "array_test_map",
1772	.key_size = sizeof(int),
1773	.value_size = sizeof(int),
1774	.key_type_id = 1,
1775	.value_type_id = 1,
1776	.max_entries = 4,
1777	.btf_load_err = true,
1778	.err_str = "Invalid index",
1779},
1780
1781{
1782	.descr = "array test. elem_type \"const void\"",
1783	.raw_types = {
1784		/* int */				/* [1] */
1785		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1786		/* int[16] */				/* [2] */
1787		BTF_TYPE_ARRAY_ENC(3, 1, 16),
1788		/* CONST type_id=0 (void) */		/* [3] */
1789		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
1790		BTF_END_RAW,
1791	},
1792	.str_sec = "",
1793	.str_sec_size = sizeof(""),
1794	.map_type = BPF_MAP_TYPE_ARRAY,
1795	.map_name = "array_test_map",
1796	.key_size = sizeof(int),
1797	.value_size = sizeof(int),
1798	.key_type_id = 1,
1799	.value_type_id = 1,
1800	.max_entries = 4,
1801	.btf_load_err = true,
1802	.err_str = "Invalid elem",
1803},
1804
1805{
1806	.descr = "array test. elem_type \"const void *\"",
1807	.raw_types = {
1808		/* int */				/* [1] */
1809		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1810		/* const void *[16] */			/* [2] */
1811		BTF_TYPE_ARRAY_ENC(3, 1, 16),
1812		/* CONST type_id=4 */			/* [3] */
1813		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 4),
1814		/* void* */				/* [4] */
1815		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
1816		BTF_END_RAW,
1817	},
1818	.str_sec = "",
1819	.str_sec_size = sizeof(""),
1820	.map_type = BPF_MAP_TYPE_ARRAY,
1821	.map_name = "array_test_map",
1822	.key_size = sizeof(int),
1823	.value_size = sizeof(int),
1824	.key_type_id = 1,
1825	.value_type_id = 1,
1826	.max_entries = 4,
1827},
1828
1829{
1830	.descr = "array test. index_type \"const void *\"",
1831	.raw_types = {
1832		/* int */				/* [1] */
1833		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1834		/* const void *[16] */			/* [2] */
1835		BTF_TYPE_ARRAY_ENC(3, 3, 16),
1836		/* CONST type_id=4 */			/* [3] */
1837		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 4),
1838		/* void* */				/* [4] */
1839		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
1840		BTF_END_RAW,
1841	},
1842	.str_sec = "",
1843	.str_sec_size = sizeof(""),
1844	.map_type = BPF_MAP_TYPE_ARRAY,
1845	.map_name = "array_test_map",
1846	.key_size = sizeof(int),
1847	.value_size = sizeof(int),
1848	.key_type_id = 1,
1849	.value_type_id = 1,
1850	.max_entries = 4,
1851	.btf_load_err = true,
1852	.err_str = "Invalid index",
1853},
1854
1855{
1856	.descr = "array test. t->size != 0\"",
1857	.raw_types = {
1858		/* int */				/* [1] */
1859		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1860		/* int[16] */				/* [2] */
1861		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ARRAY, 0, 0), 1),
1862		BTF_ARRAY_ENC(1, 1, 16),
1863		BTF_END_RAW,
1864	},
1865	.str_sec = "",
1866	.str_sec_size = sizeof(""),
1867	.map_type = BPF_MAP_TYPE_ARRAY,
1868	.map_name = "array_test_map",
1869	.key_size = sizeof(int),
1870	.value_size = sizeof(int),
1871	.key_type_id = 1,
1872	.value_type_id = 1,
1873	.max_entries = 4,
1874	.btf_load_err = true,
1875	.err_str = "size != 0",
1876},
1877
1878{
1879	.descr = "int test. invalid int_data",
1880	.raw_types = {
1881		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), 4),
1882		0x10000000,
1883		BTF_END_RAW,
1884	},
1885	.str_sec = "",
1886	.str_sec_size = sizeof(""),
1887	.map_type = BPF_MAP_TYPE_ARRAY,
1888	.map_name = "array_test_map",
1889	.key_size = sizeof(int),
1890	.value_size = sizeof(int),
1891	.key_type_id = 1,
1892	.value_type_id = 1,
1893	.max_entries = 4,
1894	.btf_load_err = true,
1895	.err_str = "Invalid int_data",
1896},
1897
1898{
1899	.descr = "invalid BTF_INFO",
1900	.raw_types = {
1901		/* int */				/* [1] */
1902		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1903		BTF_TYPE_ENC(0, 0x20000000, 4),
1904		BTF_END_RAW,
1905	},
1906	.str_sec = "",
1907	.str_sec_size = sizeof(""),
1908	.map_type = BPF_MAP_TYPE_ARRAY,
1909	.map_name = "array_test_map",
1910	.key_size = sizeof(int),
1911	.value_size = sizeof(int),
1912	.key_type_id = 1,
1913	.value_type_id = 1,
1914	.max_entries = 4,
1915	.btf_load_err = true,
1916	.err_str = "Invalid btf_info",
1917},
1918
1919{
1920	.descr = "fwd test. t->type != 0\"",
1921	.raw_types = {
1922		/* int */				/* [1] */
1923		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
1924		/* fwd type */				/* [2] */
1925		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FWD, 0, 0), 1),
1926		BTF_END_RAW,
1927	},
1928	.str_sec = "",
1929	.str_sec_size = sizeof(""),
1930	.map_type = BPF_MAP_TYPE_ARRAY,
1931	.map_name = "fwd_test_map",
1932	.key_size = sizeof(int),
1933	.value_size = sizeof(int),
1934	.key_type_id = 1,
1935	.value_type_id = 1,
1936	.max_entries = 4,
1937	.btf_load_err = true,
1938	.err_str = "type != 0",
1939},
1940
1941{
1942	.descr = "typedef (invalid name, name_off = 0)",
1943	.raw_types = {
1944		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
1945		BTF_TYPEDEF_ENC(0, 1),				/* [2] */
1946		BTF_END_RAW,
1947	},
1948	.str_sec = "\0__int",
1949	.str_sec_size = sizeof("\0__int"),
1950	.map_type = BPF_MAP_TYPE_ARRAY,
1951	.map_name = "typedef_check_btf",
1952	.key_size = sizeof(int),
1953	.value_size = sizeof(int),
1954	.key_type_id = 1,
1955	.value_type_id = 1,
1956	.max_entries = 4,
1957	.btf_load_err = true,
1958	.err_str = "Invalid name",
1959},
1960
1961{
1962	.descr = "typedef (invalid name, invalid identifier)",
1963	.raw_types = {
1964		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
1965		BTF_TYPEDEF_ENC(NAME_TBD, 1),			/* [2] */
1966		BTF_END_RAW,
1967	},
1968	.str_sec = "\0__!int",
1969	.str_sec_size = sizeof("\0__!int"),
1970	.map_type = BPF_MAP_TYPE_ARRAY,
1971	.map_name = "typedef_check_btf",
1972	.key_size = sizeof(int),
1973	.value_size = sizeof(int),
1974	.key_type_id = 1,
1975	.value_type_id = 1,
1976	.max_entries = 4,
1977	.btf_load_err = true,
1978	.err_str = "Invalid name",
1979},
1980
1981{
1982	.descr = "ptr type (invalid name, name_off <> 0)",
1983	.raw_types = {
1984		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
1985		BTF_TYPE_ENC(NAME_TBD,
1986			     BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 1),	/* [2] */
1987		BTF_END_RAW,
1988	},
1989	.str_sec = "\0__int",
1990	.str_sec_size = sizeof("\0__int"),
1991	.map_type = BPF_MAP_TYPE_ARRAY,
1992	.map_name = "ptr_type_check_btf",
1993	.key_size = sizeof(int),
1994	.value_size = sizeof(int),
1995	.key_type_id = 1,
1996	.value_type_id = 1,
1997	.max_entries = 4,
1998	.btf_load_err = true,
1999	.err_str = "Invalid name",
2000},
2001
2002{
2003	.descr = "volatile type (invalid name, name_off <> 0)",
2004	.raw_types = {
2005		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2006		BTF_TYPE_ENC(NAME_TBD,
2007			     BTF_INFO_ENC(BTF_KIND_VOLATILE, 0, 0), 1),	/* [2] */
2008		BTF_END_RAW,
2009	},
2010	.str_sec = "\0__int",
2011	.str_sec_size = sizeof("\0__int"),
2012	.map_type = BPF_MAP_TYPE_ARRAY,
2013	.map_name = "volatile_type_check_btf",
2014	.key_size = sizeof(int),
2015	.value_size = sizeof(int),
2016	.key_type_id = 1,
2017	.value_type_id = 1,
2018	.max_entries = 4,
2019	.btf_load_err = true,
2020	.err_str = "Invalid name",
2021},
2022
2023{
2024	.descr = "const type (invalid name, name_off <> 0)",
2025	.raw_types = {
2026		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2027		BTF_TYPE_ENC(NAME_TBD,
2028			     BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 1),	/* [2] */
2029		BTF_END_RAW,
2030	},
2031	.str_sec = "\0__int",
2032	.str_sec_size = sizeof("\0__int"),
2033	.map_type = BPF_MAP_TYPE_ARRAY,
2034	.map_name = "const_type_check_btf",
2035	.key_size = sizeof(int),
2036	.value_size = sizeof(int),
2037	.key_type_id = 1,
2038	.value_type_id = 1,
2039	.max_entries = 4,
2040	.btf_load_err = true,
2041	.err_str = "Invalid name",
2042},
2043
2044{
2045	.descr = "restrict type (invalid name, name_off <> 0)",
2046	.raw_types = {
2047		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2048		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 1),	/* [2] */
2049		BTF_TYPE_ENC(NAME_TBD,
2050			     BTF_INFO_ENC(BTF_KIND_RESTRICT, 0, 0), 2),	/* [3] */
2051		BTF_END_RAW,
2052	},
2053	.str_sec = "\0__int",
2054	.str_sec_size = sizeof("\0__int"),
2055	.map_type = BPF_MAP_TYPE_ARRAY,
2056	.map_name = "restrict_type_check_btf",
2057	.key_size = sizeof(int),
2058	.value_size = sizeof(int),
2059	.key_type_id = 1,
2060	.value_type_id = 1,
2061	.max_entries = 4,
2062	.btf_load_err = true,
2063	.err_str = "Invalid name",
2064},
2065
2066{
2067	.descr = "fwd type (invalid name, name_off = 0)",
2068	.raw_types = {
2069		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2070		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FWD, 0, 0), 0),	/* [2] */
2071		BTF_END_RAW,
2072	},
2073	.str_sec = "\0__skb",
2074	.str_sec_size = sizeof("\0__skb"),
2075	.map_type = BPF_MAP_TYPE_ARRAY,
2076	.map_name = "fwd_type_check_btf",
2077	.key_size = sizeof(int),
2078	.value_size = sizeof(int),
2079	.key_type_id = 1,
2080	.value_type_id = 1,
2081	.max_entries = 4,
2082	.btf_load_err = true,
2083	.err_str = "Invalid name",
2084},
2085
2086{
2087	.descr = "fwd type (invalid name, invalid identifier)",
2088	.raw_types = {
2089		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2090		BTF_TYPE_ENC(NAME_TBD,
2091			     BTF_INFO_ENC(BTF_KIND_FWD, 0, 0), 0),	/* [2] */
2092		BTF_END_RAW,
2093	},
2094	.str_sec = "\0__!skb",
2095	.str_sec_size = sizeof("\0__!skb"),
2096	.map_type = BPF_MAP_TYPE_ARRAY,
2097	.map_name = "fwd_type_check_btf",
2098	.key_size = sizeof(int),
2099	.value_size = sizeof(int),
2100	.key_type_id = 1,
2101	.value_type_id = 1,
2102	.max_entries = 4,
2103	.btf_load_err = true,
2104	.err_str = "Invalid name",
2105},
2106
2107{
2108	.descr = "array type (invalid name, name_off <> 0)",
2109	.raw_types = {
2110		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2111		BTF_TYPE_ENC(NAME_TBD,
2112			     BTF_INFO_ENC(BTF_KIND_ARRAY, 0, 0), 0),	/* [2] */
2113		BTF_ARRAY_ENC(1, 1, 4),
2114		BTF_END_RAW,
2115	},
2116	.str_sec = "\0__skb",
2117	.str_sec_size = sizeof("\0__skb"),
2118	.map_type = BPF_MAP_TYPE_ARRAY,
2119	.map_name = "array_type_check_btf",
2120	.key_size = sizeof(int),
2121	.value_size = sizeof(int),
2122	.key_type_id = 1,
2123	.value_type_id = 1,
2124	.max_entries = 4,
2125	.btf_load_err = true,
2126	.err_str = "Invalid name",
2127},
2128
2129{
2130	.descr = "struct type (name_off = 0)",
2131	.raw_types = {
2132		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2133		BTF_TYPE_ENC(0,
2134			     BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4),	/* [2] */
2135		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
2136		BTF_END_RAW,
2137	},
2138	.str_sec = "\0A",
2139	.str_sec_size = sizeof("\0A"),
2140	.map_type = BPF_MAP_TYPE_ARRAY,
2141	.map_name = "struct_type_check_btf",
2142	.key_size = sizeof(int),
2143	.value_size = sizeof(int),
2144	.key_type_id = 1,
2145	.value_type_id = 1,
2146	.max_entries = 4,
2147},
2148
2149{
2150	.descr = "struct type (invalid name, invalid identifier)",
2151	.raw_types = {
2152		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2153		BTF_TYPE_ENC(NAME_TBD,
2154			     BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4),	/* [2] */
2155		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
2156		BTF_END_RAW,
2157	},
2158	.str_sec = "\0A!\0B",
2159	.str_sec_size = sizeof("\0A!\0B"),
2160	.map_type = BPF_MAP_TYPE_ARRAY,
2161	.map_name = "struct_type_check_btf",
2162	.key_size = sizeof(int),
2163	.value_size = sizeof(int),
2164	.key_type_id = 1,
2165	.value_type_id = 1,
2166	.max_entries = 4,
2167	.btf_load_err = true,
2168	.err_str = "Invalid name",
2169},
2170
2171{
2172	.descr = "struct member (name_off = 0)",
2173	.raw_types = {
2174		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2175		BTF_TYPE_ENC(0,
2176			     BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4),	/* [2] */
2177		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
2178		BTF_END_RAW,
2179	},
2180	.str_sec = "\0A",
2181	.str_sec_size = sizeof("\0A"),
2182	.map_type = BPF_MAP_TYPE_ARRAY,
2183	.map_name = "struct_type_check_btf",
2184	.key_size = sizeof(int),
2185	.value_size = sizeof(int),
2186	.key_type_id = 1,
2187	.value_type_id = 1,
2188	.max_entries = 4,
2189},
2190
2191{
2192	.descr = "struct member (invalid name, invalid identifier)",
2193	.raw_types = {
2194		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2195		BTF_TYPE_ENC(NAME_TBD,
2196			     BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4),	/* [2] */
2197		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
2198		BTF_END_RAW,
2199	},
2200	.str_sec = "\0A\0B*",
2201	.str_sec_size = sizeof("\0A\0B*"),
2202	.map_type = BPF_MAP_TYPE_ARRAY,
2203	.map_name = "struct_type_check_btf",
2204	.key_size = sizeof(int),
2205	.value_size = sizeof(int),
2206	.key_type_id = 1,
2207	.value_type_id = 1,
2208	.max_entries = 4,
2209	.btf_load_err = true,
2210	.err_str = "Invalid name",
2211},
2212
2213{
2214	.descr = "enum type (name_off = 0)",
2215	.raw_types = {
2216		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2217		BTF_TYPE_ENC(0,
2218			     BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1),
2219			     sizeof(int)),				/* [2] */
2220		BTF_ENUM_ENC(NAME_TBD, 0),
2221		BTF_END_RAW,
2222	},
2223	.str_sec = "\0A\0B",
2224	.str_sec_size = sizeof("\0A\0B"),
2225	.map_type = BPF_MAP_TYPE_ARRAY,
2226	.map_name = "enum_type_check_btf",
2227	.key_size = sizeof(int),
2228	.value_size = sizeof(int),
2229	.key_type_id = 1,
2230	.value_type_id = 1,
2231	.max_entries = 4,
2232},
2233
2234{
2235	.descr = "enum type (invalid name, invalid identifier)",
2236	.raw_types = {
2237		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2238		BTF_TYPE_ENC(NAME_TBD,
2239			     BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1),
2240			     sizeof(int)),				/* [2] */
2241		BTF_ENUM_ENC(NAME_TBD, 0),
2242		BTF_END_RAW,
2243	},
2244	.str_sec = "\0A!\0B",
2245	.str_sec_size = sizeof("\0A!\0B"),
2246	.map_type = BPF_MAP_TYPE_ARRAY,
2247	.map_name = "enum_type_check_btf",
2248	.key_size = sizeof(int),
2249	.value_size = sizeof(int),
2250	.key_type_id = 1,
2251	.value_type_id = 1,
2252	.max_entries = 4,
2253	.btf_load_err = true,
2254	.err_str = "Invalid name",
2255},
2256
2257{
2258	.descr = "enum member (invalid name, name_off = 0)",
2259	.raw_types = {
2260		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2261		BTF_TYPE_ENC(0,
2262			     BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1),
2263			     sizeof(int)),				/* [2] */
2264		BTF_ENUM_ENC(0, 0),
2265		BTF_END_RAW,
2266	},
2267	.str_sec = "",
2268	.str_sec_size = sizeof(""),
2269	.map_type = BPF_MAP_TYPE_ARRAY,
2270	.map_name = "enum_type_check_btf",
2271	.key_size = sizeof(int),
2272	.value_size = sizeof(int),
2273	.key_type_id = 1,
2274	.value_type_id = 1,
2275	.max_entries = 4,
2276	.btf_load_err = true,
2277	.err_str = "Invalid name",
2278},
2279
2280{
2281	.descr = "enum member (invalid name, invalid identifier)",
2282	.raw_types = {
2283		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2284		BTF_TYPE_ENC(0,
2285			     BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1),
2286			     sizeof(int)),				/* [2] */
2287		BTF_ENUM_ENC(NAME_TBD, 0),
2288		BTF_END_RAW,
2289	},
2290	.str_sec = "\0A!",
2291	.str_sec_size = sizeof("\0A!"),
2292	.map_type = BPF_MAP_TYPE_ARRAY,
2293	.map_name = "enum_type_check_btf",
2294	.key_size = sizeof(int),
2295	.value_size = sizeof(int),
2296	.key_type_id = 1,
2297	.value_type_id = 1,
2298	.max_entries = 4,
2299	.btf_load_err = true,
2300	.err_str = "Invalid name",
2301},
2302{
2303	.descr = "arraymap invalid btf key (a bit field)",
2304	.raw_types = {
2305		/* int */				/* [1] */
2306		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
2307		/* 32 bit int with 32 bit offset */	/* [2] */
2308		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 32, 32, 8),
2309		BTF_END_RAW,
2310	},
2311	.str_sec = "",
2312	.str_sec_size = sizeof(""),
2313	.map_type = BPF_MAP_TYPE_ARRAY,
2314	.map_name = "array_map_check_btf",
2315	.key_size = sizeof(int),
2316	.value_size = sizeof(int),
2317	.key_type_id = 2,
2318	.value_type_id = 1,
2319	.max_entries = 4,
2320	.map_create_err = true,
2321},
2322
2323{
2324	.descr = "arraymap invalid btf key (!= 32 bits)",
2325	.raw_types = {
2326		/* int */				/* [1] */
2327		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
2328		/* 16 bit int with 0 bit offset */	/* [2] */
2329		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 16, 2),
2330		BTF_END_RAW,
2331	},
2332	.str_sec = "",
2333	.str_sec_size = sizeof(""),
2334	.map_type = BPF_MAP_TYPE_ARRAY,
2335	.map_name = "array_map_check_btf",
2336	.key_size = sizeof(int),
2337	.value_size = sizeof(int),
2338	.key_type_id = 2,
2339	.value_type_id = 1,
2340	.max_entries = 4,
2341	.map_create_err = true,
2342},
2343
2344{
2345	.descr = "arraymap invalid btf value (too small)",
2346	.raw_types = {
2347		/* int */				/* [1] */
2348		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
2349		BTF_END_RAW,
2350	},
2351	.str_sec = "",
2352	.str_sec_size = sizeof(""),
2353	.map_type = BPF_MAP_TYPE_ARRAY,
2354	.map_name = "array_map_check_btf",
2355	.key_size = sizeof(int),
2356	/* btf_value_size < map->value_size */
2357	.value_size = sizeof(__u64),
2358	.key_type_id = 1,
2359	.value_type_id = 1,
2360	.max_entries = 4,
2361	.map_create_err = true,
2362},
2363
2364{
2365	.descr = "arraymap invalid btf value (too big)",
2366	.raw_types = {
2367		/* int */				/* [1] */
2368		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
2369		BTF_END_RAW,
2370	},
2371	.str_sec = "",
2372	.str_sec_size = sizeof(""),
2373	.map_type = BPF_MAP_TYPE_ARRAY,
2374	.map_name = "array_map_check_btf",
2375	.key_size = sizeof(int),
2376	/* btf_value_size > map->value_size */
2377	.value_size = sizeof(__u16),
2378	.key_type_id = 1,
2379	.value_type_id = 1,
2380	.max_entries = 4,
2381	.map_create_err = true,
2382},
2383
2384{
2385	.descr = "func proto (int (*)(int, unsigned int))",
2386	.raw_types = {
2387		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
2388		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2389		/* int (*)(int, unsigned int) */
2390		BTF_FUNC_PROTO_ENC(1, 2),			/* [3] */
2391			BTF_FUNC_PROTO_ARG_ENC(0, 1),
2392			BTF_FUNC_PROTO_ARG_ENC(0, 2),
2393		BTF_END_RAW,
2394	},
2395	.str_sec = "",
2396	.str_sec_size = sizeof(""),
2397	.map_type = BPF_MAP_TYPE_ARRAY,
2398	.map_name = "func_proto_type_check_btf",
2399	.key_size = sizeof(int),
2400	.value_size = sizeof(int),
2401	.key_type_id = 1,
2402	.value_type_id = 1,
2403	.max_entries = 4,
2404},
2405
2406{
2407	.descr = "func proto (vararg)",
2408	.raw_types = {
2409		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2410		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2411		/* void (*)(int, unsigned int, ...) */
2412		BTF_FUNC_PROTO_ENC(0, 3),			/* [3] */
2413			BTF_FUNC_PROTO_ARG_ENC(0, 1),
2414			BTF_FUNC_PROTO_ARG_ENC(0, 2),
2415			BTF_FUNC_PROTO_ARG_ENC(0, 0),
2416		BTF_END_RAW,
2417	},
2418	.str_sec = "",
2419	.str_sec_size = sizeof(""),
2420	.map_type = BPF_MAP_TYPE_ARRAY,
2421	.map_name = "func_proto_type_check_btf",
2422	.key_size = sizeof(int),
2423	.value_size = sizeof(int),
2424	.key_type_id = 1,
2425	.value_type_id = 1,
2426	.max_entries = 4,
2427},
2428
2429{
2430	.descr = "func proto (vararg with name)",
2431	.raw_types = {
2432		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2433		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2434		/* void (*)(int a, unsigned int b, ... c) */
2435		BTF_FUNC_PROTO_ENC(0, 3),			/* [3] */
2436			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2437			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2438			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 0),
2439		BTF_END_RAW,
2440	},
2441	.str_sec = "\0a\0b\0c",
2442	.str_sec_size = sizeof("\0a\0b\0c"),
2443	.map_type = BPF_MAP_TYPE_ARRAY,
2444	.map_name = "func_proto_type_check_btf",
2445	.key_size = sizeof(int),
2446	.value_size = sizeof(int),
2447	.key_type_id = 1,
2448	.value_type_id = 1,
2449	.max_entries = 4,
2450	.btf_load_err = true,
2451	.err_str = "Invalid arg#3",
2452},
2453
2454{
2455	.descr = "func proto (arg after vararg)",
2456	.raw_types = {
2457		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2458		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2459		/* void (*)(int a, ..., unsigned int b) */
2460		BTF_FUNC_PROTO_ENC(0, 3),			/* [3] */
2461			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2462			BTF_FUNC_PROTO_ARG_ENC(0, 0),
2463			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2464		BTF_END_RAW,
2465	},
2466	.str_sec = "\0a\0b",
2467	.str_sec_size = sizeof("\0a\0b"),
2468	.map_type = BPF_MAP_TYPE_ARRAY,
2469	.map_name = "func_proto_type_check_btf",
2470	.key_size = sizeof(int),
2471	.value_size = sizeof(int),
2472	.key_type_id = 1,
2473	.value_type_id = 1,
2474	.max_entries = 4,
2475	.btf_load_err = true,
2476	.err_str = "Invalid arg#2",
2477},
2478
2479{
2480	.descr = "func proto (CONST=>TYPEDEF=>PTR=>FUNC_PROTO)",
2481	.raw_types = {
2482		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2483		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2484		/* typedef void (*func_ptr)(int, unsigned int) */
2485		BTF_TYPEDEF_ENC(NAME_TBD, 5),			/* [3] */
2486		/* const func_ptr */
2487		BTF_CONST_ENC(3),				/* [4] */
2488		BTF_PTR_ENC(6),					/* [5] */
2489		BTF_FUNC_PROTO_ENC(0, 2),			/* [6] */
2490			BTF_FUNC_PROTO_ARG_ENC(0, 1),
2491			BTF_FUNC_PROTO_ARG_ENC(0, 2),
2492		BTF_END_RAW,
2493	},
2494	.str_sec = "\0func_ptr",
2495	.str_sec_size = sizeof("\0func_ptr"),
2496	.map_type = BPF_MAP_TYPE_ARRAY,
2497	.map_name = "func_proto_type_check_btf",
2498	.key_size = sizeof(int),
2499	.value_size = sizeof(int),
2500	.key_type_id = 1,
2501	.value_type_id = 1,
2502	.max_entries = 4,
2503},
2504
2505{
2506	.descr = "func proto (TYPEDEF=>FUNC_PROTO)",
2507	.raw_types = {
2508		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2509		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2510		BTF_TYPEDEF_ENC(NAME_TBD, 4),			/* [3] */
2511		BTF_FUNC_PROTO_ENC(0, 2),			/* [4] */
2512			BTF_FUNC_PROTO_ARG_ENC(0, 1),
2513			BTF_FUNC_PROTO_ARG_ENC(0, 2),
2514		BTF_END_RAW,
2515	},
2516	.str_sec = "\0func_typedef",
2517	.str_sec_size = sizeof("\0func_typedef"),
2518	.map_type = BPF_MAP_TYPE_ARRAY,
2519	.map_name = "func_proto_type_check_btf",
2520	.key_size = sizeof(int),
2521	.value_size = sizeof(int),
2522	.key_type_id = 1,
2523	.value_type_id = 1,
2524	.max_entries = 4,
2525},
2526
2527{
2528	.descr = "func proto (btf_resolve(arg))",
2529	.raw_types = {
2530		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2531		/* void (*)(const void *) */
2532		BTF_FUNC_PROTO_ENC(0, 1),			/* [2] */
2533			BTF_FUNC_PROTO_ARG_ENC(0, 3),
2534		BTF_CONST_ENC(4),				/* [3] */
2535		BTF_PTR_ENC(0),					/* [4] */
2536		BTF_END_RAW,
2537	},
2538	.str_sec = "",
2539	.str_sec_size = sizeof(""),
2540	.map_type = BPF_MAP_TYPE_ARRAY,
2541	.map_name = "func_proto_type_check_btf",
2542	.key_size = sizeof(int),
2543	.value_size = sizeof(int),
2544	.key_type_id = 1,
2545	.value_type_id = 1,
2546	.max_entries = 4,
2547},
2548
2549{
2550	.descr = "func proto (Not all arg has name)",
2551	.raw_types = {
2552		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2553		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2554		/* void (*)(int, unsigned int b) */
2555		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2556			BTF_FUNC_PROTO_ARG_ENC(0, 1),
2557			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2558		BTF_END_RAW,
2559	},
2560	.str_sec = "\0b",
2561	.str_sec_size = sizeof("\0b"),
2562	.map_type = BPF_MAP_TYPE_ARRAY,
2563	.map_name = "func_proto_type_check_btf",
2564	.key_size = sizeof(int),
2565	.value_size = sizeof(int),
2566	.key_type_id = 1,
2567	.value_type_id = 1,
2568	.max_entries = 4,
2569},
2570
2571{
2572	.descr = "func proto (Bad arg name_off)",
2573	.raw_types = {
2574		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2575		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2576		/* void (*)(int a, unsigned int <bad_name_off>) */
2577		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2578			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2579			BTF_FUNC_PROTO_ARG_ENC(0x0fffffff, 2),
2580		BTF_END_RAW,
2581	},
2582	.str_sec = "\0a",
2583	.str_sec_size = sizeof("\0a"),
2584	.map_type = BPF_MAP_TYPE_ARRAY,
2585	.map_name = "func_proto_type_check_btf",
2586	.key_size = sizeof(int),
2587	.value_size = sizeof(int),
2588	.key_type_id = 1,
2589	.value_type_id = 1,
2590	.max_entries = 4,
2591	.btf_load_err = true,
2592	.err_str = "Invalid arg#2",
2593},
2594
2595{
2596	.descr = "func proto (Bad arg name)",
2597	.raw_types = {
2598		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2599		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2600		/* void (*)(int a, unsigned int !!!) */
2601		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2602			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2603			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2604		BTF_END_RAW,
2605	},
2606	.str_sec = "\0a\0!!!",
2607	.str_sec_size = sizeof("\0a\0!!!"),
2608	.map_type = BPF_MAP_TYPE_ARRAY,
2609	.map_name = "func_proto_type_check_btf",
2610	.key_size = sizeof(int),
2611	.value_size = sizeof(int),
2612	.key_type_id = 1,
2613	.value_type_id = 1,
2614	.max_entries = 4,
2615	.btf_load_err = true,
2616	.err_str = "Invalid arg#2",
2617},
2618
2619{
2620	.descr = "func proto (Invalid return type)",
2621	.raw_types = {
2622		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2623		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2624		/* <bad_ret_type> (*)(int, unsigned int) */
2625		BTF_FUNC_PROTO_ENC(100, 2),			/* [3] */
2626			BTF_FUNC_PROTO_ARG_ENC(0, 1),
2627			BTF_FUNC_PROTO_ARG_ENC(0, 2),
2628		BTF_END_RAW,
2629	},
2630	.str_sec = "",
2631	.str_sec_size = sizeof(""),
2632	.map_type = BPF_MAP_TYPE_ARRAY,
2633	.map_name = "func_proto_type_check_btf",
2634	.key_size = sizeof(int),
2635	.value_size = sizeof(int),
2636	.key_type_id = 1,
2637	.value_type_id = 1,
2638	.max_entries = 4,
2639	.btf_load_err = true,
2640	.err_str = "Invalid return type",
2641},
2642
2643{
2644	.descr = "func proto (with func name)",
2645	.raw_types = {
2646		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2647		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2648		/* void func_proto(int, unsigned int) */
2649		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 2), 0),	/* [3] */
2650			BTF_FUNC_PROTO_ARG_ENC(0, 1),
2651			BTF_FUNC_PROTO_ARG_ENC(0, 2),
2652		BTF_END_RAW,
2653	},
2654	.str_sec = "\0func_proto",
2655	.str_sec_size = sizeof("\0func_proto"),
2656	.map_type = BPF_MAP_TYPE_ARRAY,
2657	.map_name = "func_proto_type_check_btf",
2658	.key_size = sizeof(int),
2659	.value_size = sizeof(int),
2660	.key_type_id = 1,
2661	.value_type_id = 1,
2662	.max_entries = 4,
2663	.btf_load_err = true,
2664	.err_str = "Invalid name",
2665},
2666
2667{
2668	.descr = "func proto (const void arg)",
2669	.raw_types = {
2670		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2671		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2672		/* void (*)(const void) */
2673		BTF_FUNC_PROTO_ENC(0, 1),			/* [3] */
2674			BTF_FUNC_PROTO_ARG_ENC(0, 4),
2675		BTF_CONST_ENC(0),				/* [4] */
2676		BTF_END_RAW,
2677	},
2678	.str_sec = "",
2679	.str_sec_size = sizeof(""),
2680	.map_type = BPF_MAP_TYPE_ARRAY,
2681	.map_name = "func_proto_type_check_btf",
2682	.key_size = sizeof(int),
2683	.value_size = sizeof(int),
2684	.key_type_id = 1,
2685	.value_type_id = 1,
2686	.max_entries = 4,
2687	.btf_load_err = true,
2688	.err_str = "Invalid arg#1",
2689},
2690
2691{
2692	.descr = "func (void func(int a, unsigned int b))",
2693	.raw_types = {
2694		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2695		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2696		/* void (*)(int a, unsigned int b) */
2697		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2698			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2699			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2700		/* void func(int a, unsigned int b) */
2701		BTF_FUNC_ENC(NAME_TBD, 3),			/* [4] */
2702		BTF_END_RAW,
2703	},
2704	.str_sec = "\0a\0b\0func",
2705	.str_sec_size = sizeof("\0a\0b\0func"),
2706	.map_type = BPF_MAP_TYPE_ARRAY,
2707	.map_name = "func_type_check_btf",
2708	.key_size = sizeof(int),
2709	.value_size = sizeof(int),
2710	.key_type_id = 1,
2711	.value_type_id = 1,
2712	.max_entries = 4,
2713},
2714
2715{
2716	.descr = "func (No func name)",
2717	.raw_types = {
2718		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2719		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2720		/* void (*)(int a, unsigned int b) */
2721		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2722			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2723			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2724		/* void <no_name>(int a, unsigned int b) */
2725		BTF_FUNC_ENC(0, 3),				/* [4] */
2726		BTF_END_RAW,
2727	},
2728	.str_sec = "\0a\0b",
2729	.str_sec_size = sizeof("\0a\0b"),
2730	.map_type = BPF_MAP_TYPE_ARRAY,
2731	.map_name = "func_type_check_btf",
2732	.key_size = sizeof(int),
2733	.value_size = sizeof(int),
2734	.key_type_id = 1,
2735	.value_type_id = 1,
2736	.max_entries = 4,
2737	.btf_load_err = true,
2738	.err_str = "Invalid name",
2739},
2740
2741{
2742	.descr = "func (Invalid func name)",
2743	.raw_types = {
2744		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2745		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2746		/* void (*)(int a, unsigned int b) */
2747		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2748			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2749			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2750		/* void !!!(int a, unsigned int b) */
2751		BTF_FUNC_ENC(NAME_TBD, 3),			/* [4] */
2752		BTF_END_RAW,
2753	},
2754	.str_sec = "\0a\0b\0!!!",
2755	.str_sec_size = sizeof("\0a\0b\0!!!"),
2756	.map_type = BPF_MAP_TYPE_ARRAY,
2757	.map_name = "func_type_check_btf",
2758	.key_size = sizeof(int),
2759	.value_size = sizeof(int),
2760	.key_type_id = 1,
2761	.value_type_id = 1,
2762	.max_entries = 4,
2763	.btf_load_err = true,
2764	.err_str = "Invalid name",
2765},
2766
2767{
2768	.descr = "func (Some arg has no name)",
2769	.raw_types = {
2770		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2771		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2772		/* void (*)(int a, unsigned int) */
2773		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2774			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2775			BTF_FUNC_PROTO_ARG_ENC(0, 2),
2776		/* void func(int a, unsigned int) */
2777		BTF_FUNC_ENC(NAME_TBD, 3),			/* [4] */
2778		BTF_END_RAW,
2779	},
2780	.str_sec = "\0a\0func",
2781	.str_sec_size = sizeof("\0a\0func"),
2782	.map_type = BPF_MAP_TYPE_ARRAY,
2783	.map_name = "func_type_check_btf",
2784	.key_size = sizeof(int),
2785	.value_size = sizeof(int),
2786	.key_type_id = 1,
2787	.value_type_id = 1,
2788	.max_entries = 4,
2789	.btf_load_err = true,
2790	.err_str = "Invalid arg#2",
2791},
2792
2793{
2794	.descr = "func (Non zero vlen)",
2795	.raw_types = {
2796		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2797		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),		/* [2] */
2798		/* void (*)(int a, unsigned int b) */
2799		BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
2800			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
2801			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
2802		/* void func(int a, unsigned int b) */
2803		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 2), 3), 	/* [4] */
2804		BTF_END_RAW,
2805	},
2806	.str_sec = "\0a\0b\0func",
2807	.str_sec_size = sizeof("\0a\0b\0func"),
2808	.map_type = BPF_MAP_TYPE_ARRAY,
2809	.map_name = "func_type_check_btf",
2810	.key_size = sizeof(int),
2811	.value_size = sizeof(int),
2812	.key_type_id = 1,
2813	.value_type_id = 1,
2814	.max_entries = 4,
2815	.btf_load_err = true,
2816	.err_str = "Invalid func linkage",
2817},
2818
2819{
2820	.descr = "func (Not referring to FUNC_PROTO)",
2821	.raw_types = {
2822		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
2823		BTF_FUNC_ENC(NAME_TBD, 1),			/* [2] */
2824		BTF_END_RAW,
2825	},
2826	.str_sec = "\0func",
2827	.str_sec_size = sizeof("\0func"),
2828	.map_type = BPF_MAP_TYPE_ARRAY,
2829	.map_name = "func_type_check_btf",
2830	.key_size = sizeof(int),
2831	.value_size = sizeof(int),
2832	.key_type_id = 1,
2833	.value_type_id = 1,
2834	.max_entries = 4,
2835	.btf_load_err = true,
2836	.err_str = "Invalid type_id",
2837},
2838
2839{
2840	.descr = "invalid int kind_flag",
2841	.raw_types = {
2842		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2843		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_INT, 1, 0), 4),	/* [2] */
2844		BTF_INT_ENC(0, 0, 32),
2845		BTF_END_RAW,
2846	},
2847	BTF_STR_SEC(""),
2848	.map_type = BPF_MAP_TYPE_ARRAY,
2849	.map_name = "int_type_check_btf",
2850	.key_size = sizeof(int),
2851	.value_size = sizeof(int),
2852	.key_type_id = 1,
2853	.value_type_id = 1,
2854	.max_entries = 4,
2855	.btf_load_err = true,
2856	.err_str = "Invalid btf_info kind_flag",
2857},
2858
2859{
2860	.descr = "invalid ptr kind_flag",
2861	.raw_types = {
2862		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2863		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 1, 0), 1),	/* [2] */
2864		BTF_END_RAW,
2865	},
2866	BTF_STR_SEC(""),
2867	.map_type = BPF_MAP_TYPE_ARRAY,
2868	.map_name = "ptr_type_check_btf",
2869	.key_size = sizeof(int),
2870	.value_size = sizeof(int),
2871	.key_type_id = 1,
2872	.value_type_id = 1,
2873	.max_entries = 4,
2874	.btf_load_err = true,
2875	.err_str = "Invalid btf_info kind_flag",
2876},
2877
2878{
2879	.descr = "invalid array kind_flag",
2880	.raw_types = {
2881		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2882		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ARRAY, 1, 0), 0),	/* [2] */
2883		BTF_ARRAY_ENC(1, 1, 1),
2884		BTF_END_RAW,
2885	},
2886	BTF_STR_SEC(""),
2887	.map_type = BPF_MAP_TYPE_ARRAY,
2888	.map_name = "array_type_check_btf",
2889	.key_size = sizeof(int),
2890	.value_size = sizeof(int),
2891	.key_type_id = 1,
2892	.value_type_id = 1,
2893	.max_entries = 4,
2894	.btf_load_err = true,
2895	.err_str = "Invalid btf_info kind_flag",
2896},
2897
2898{
2899	.descr = "valid fwd kind_flag",
2900	.raw_types = {
2901		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2902		BTF_TYPE_ENC(NAME_TBD,
2903			     BTF_INFO_ENC(BTF_KIND_FWD, 1, 0), 0),	/* [2] */
2904		BTF_END_RAW,
2905	},
2906	BTF_STR_SEC("\0A"),
2907	.map_type = BPF_MAP_TYPE_ARRAY,
2908	.map_name = "fwd_type_check_btf",
2909	.key_size = sizeof(int),
2910	.value_size = sizeof(int),
2911	.key_type_id = 1,
2912	.value_type_id = 1,
2913	.max_entries = 4,
2914},
2915
2916{
2917	.descr = "invalid typedef kind_flag",
2918	.raw_types = {
2919		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2920		BTF_TYPE_ENC(NAME_TBD,
2921			     BTF_INFO_ENC(BTF_KIND_TYPEDEF, 1, 0), 1),	/* [2] */
2922		BTF_END_RAW,
2923	},
2924	BTF_STR_SEC("\0A"),
2925	.map_type = BPF_MAP_TYPE_ARRAY,
2926	.map_name = "typedef_type_check_btf",
2927	.key_size = sizeof(int),
2928	.value_size = sizeof(int),
2929	.key_type_id = 1,
2930	.value_type_id = 1,
2931	.max_entries = 4,
2932	.btf_load_err = true,
2933	.err_str = "Invalid btf_info kind_flag",
2934},
2935
2936{
2937	.descr = "invalid volatile kind_flag",
2938	.raw_types = {
2939		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
2940		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_VOLATILE, 1, 0), 1),	/* [2] */
2941		BTF_END_RAW,
2942	},
2943	BTF_STR_SEC(""),
2944	.map_type = BPF_MAP_TYPE_ARRAY,
2945	.map_name = "volatile_type_check_btf",
2946	.key_size = sizeof(int),
2947	.value_size = sizeof(int),
2948	.key_type_id = 1,
2949	.value_type_id = 1,
2950	.max_entries = 4,
2951	.btf_load_err = true,
2952	.err_str = "Invalid btf_info kind_flag",
2953},
2954
2955{
2956	.descr = "invalid const kind_flag",
2957	.raw_types = {
2958		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
2959		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 1, 0), 1),	/* [2] */
2960		BTF_END_RAW,
2961	},
2962	BTF_STR_SEC(""),
2963	.map_type = BPF_MAP_TYPE_ARRAY,
2964	.map_name = "const_type_check_btf",
2965	.key_size = sizeof(int),
2966	.value_size = sizeof(int),
2967	.key_type_id = 1,
2968	.value_type_id = 1,
2969	.max_entries = 4,
2970	.btf_load_err = true,
2971	.err_str = "Invalid btf_info kind_flag",
2972},
2973
2974{
2975	.descr = "invalid restrict kind_flag",
2976	.raw_types = {
2977		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
2978		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_RESTRICT, 1, 0), 1),	/* [2] */
2979		BTF_END_RAW,
2980	},
2981	BTF_STR_SEC(""),
2982	.map_type = BPF_MAP_TYPE_ARRAY,
2983	.map_name = "restrict_type_check_btf",
2984	.key_size = sizeof(int),
2985	.value_size = sizeof(int),
2986	.key_type_id = 1,
2987	.value_type_id = 1,
2988	.max_entries = 4,
2989	.btf_load_err = true,
2990	.err_str = "Invalid btf_info kind_flag",
2991},
2992
2993{
2994	.descr = "invalid func kind_flag",
2995	.raw_types = {
2996		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
2997		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 0), 0),	/* [2] */
2998		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FUNC, 1, 0), 2),	/* [3] */
2999		BTF_END_RAW,
3000	},
3001	BTF_STR_SEC("\0A"),
3002	.map_type = BPF_MAP_TYPE_ARRAY,
3003	.map_name = "func_type_check_btf",
3004	.key_size = sizeof(int),
3005	.value_size = sizeof(int),
3006	.key_type_id = 1,
3007	.value_type_id = 1,
3008	.max_entries = 4,
3009	.btf_load_err = true,
3010	.err_str = "Invalid btf_info kind_flag",
3011},
3012
3013{
3014	.descr = "invalid func_proto kind_flag",
3015	.raw_types = {
3016		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3017		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 1, 0), 0),	/* [2] */
3018		BTF_END_RAW,
3019	},
3020	BTF_STR_SEC(""),
3021	.map_type = BPF_MAP_TYPE_ARRAY,
3022	.map_name = "func_proto_type_check_btf",
3023	.key_size = sizeof(int),
3024	.value_size = sizeof(int),
3025	.key_type_id = 1,
3026	.value_type_id = 1,
3027	.max_entries = 4,
3028	.btf_load_err = true,
3029	.err_str = "Invalid btf_info kind_flag",
3030},
3031
3032{
3033	.descr = "valid struct, kind_flag, bitfield_size = 0",
3034	.raw_types = {
3035		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3036		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 8),	/* [2] */
3037		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(0, 0)),
3038		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(0, 32)),
3039		BTF_END_RAW,
3040	},
3041	BTF_STR_SEC("\0A\0B"),
3042	.map_type = BPF_MAP_TYPE_ARRAY,
3043	.map_name = "struct_type_check_btf",
3044	.key_size = sizeof(int),
3045	.value_size = sizeof(int),
3046	.key_type_id = 1,
3047	.value_type_id = 1,
3048	.max_entries = 4,
3049},
3050
3051{
3052	.descr = "valid struct, kind_flag, int member, bitfield_size != 0",
3053	.raw_types = {
3054		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3055		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 4),	/* [2] */
3056		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(4, 0)),
3057		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(4, 4)),
3058		BTF_END_RAW,
3059	},
3060	BTF_STR_SEC("\0A\0B"),
3061	.map_type = BPF_MAP_TYPE_ARRAY,
3062	.map_name = "struct_type_check_btf",
3063	.key_size = sizeof(int),
3064	.value_size = sizeof(int),
3065	.key_type_id = 1,
3066	.value_type_id = 1,
3067	.max_entries = 4,
3068},
3069
3070{
3071	.descr = "valid union, kind_flag, int member, bitfield_size != 0",
3072	.raw_types = {
3073		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
3074		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 1, 2), 4),	/* [2] */
3075		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(4, 0)),
3076		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(4, 0)),
3077		BTF_END_RAW,
3078	},
3079	BTF_STR_SEC("\0A\0B"),
3080	.map_type = BPF_MAP_TYPE_ARRAY,
3081	.map_name = "union_type_check_btf",
3082	.key_size = sizeof(int),
3083	.value_size = sizeof(int),
3084	.key_type_id = 1,
3085	.value_type_id = 1,
3086	.max_entries = 4,
3087},
3088
3089{
3090	.descr = "valid struct, kind_flag, enum member, bitfield_size != 0",
3091	.raw_types = {
3092		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
3093		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),	/* [2] */
3094		BTF_ENUM_ENC(NAME_TBD, 0),
3095		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 4),/* [3] */
3096		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(4, 0)),
3097		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(4, 4)),
3098		BTF_END_RAW,
3099	},
3100	BTF_STR_SEC("\0A\0B\0C"),
3101	.map_type = BPF_MAP_TYPE_ARRAY,
3102	.map_name = "struct_type_check_btf",
3103	.key_size = sizeof(int),
3104	.value_size = sizeof(int),
3105	.key_type_id = 1,
3106	.value_type_id = 1,
3107	.max_entries = 4,
3108},
3109
3110{
3111	.descr = "valid union, kind_flag, enum member, bitfield_size != 0",
3112	.raw_types = {
3113		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
3114		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),	/* [2] */
3115		BTF_ENUM_ENC(NAME_TBD, 0),
3116		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 1, 2), 4),	/* [3] */
3117		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(4, 0)),
3118		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(4, 0)),
3119		BTF_END_RAW,
3120	},
3121	BTF_STR_SEC("\0A\0B\0C"),
3122	.map_type = BPF_MAP_TYPE_ARRAY,
3123	.map_name = "union_type_check_btf",
3124	.key_size = sizeof(int),
3125	.value_size = sizeof(int),
3126	.key_type_id = 1,
3127	.value_type_id = 1,
3128	.max_entries = 4,
3129},
3130
3131{
3132	.descr = "valid struct, kind_flag, typedef member, bitfield_size != 0",
3133	.raw_types = {
3134		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
3135		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),	/* [2] */
3136		BTF_ENUM_ENC(NAME_TBD, 0),
3137		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 4),/* [3] */
3138		BTF_MEMBER_ENC(NAME_TBD, 4, BTF_MEMBER_OFFSET(4, 0)),
3139		BTF_MEMBER_ENC(NAME_TBD, 5, BTF_MEMBER_OFFSET(4, 4)),
3140		BTF_TYPEDEF_ENC(NAME_TBD, 1),				/* [4] */
3141		BTF_TYPEDEF_ENC(NAME_TBD, 2),				/* [5] */
3142		BTF_END_RAW,
3143	},
3144	BTF_STR_SEC("\0A\0B\0C\0D\0E"),
3145	.map_type = BPF_MAP_TYPE_ARRAY,
3146	.map_name = "struct_type_check_btf",
3147	.key_size = sizeof(int),
3148	.value_size = sizeof(int),
3149	.key_type_id = 1,
3150	.value_type_id = 1,
3151	.max_entries = 4,
3152},
3153
3154{
3155	.descr = "valid union, kind_flag, typedef member, bitfield_size != 0",
3156	.raw_types = {
3157		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
3158		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),	/* [2] */
3159		BTF_ENUM_ENC(NAME_TBD, 0),
3160		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 1, 2), 4),	/* [3] */
3161		BTF_MEMBER_ENC(NAME_TBD, 4, BTF_MEMBER_OFFSET(4, 0)),
3162		BTF_MEMBER_ENC(NAME_TBD, 5, BTF_MEMBER_OFFSET(4, 0)),
3163		BTF_TYPEDEF_ENC(NAME_TBD, 1),				/* [4] */
3164		BTF_TYPEDEF_ENC(NAME_TBD, 2),				/* [5] */
3165		BTF_END_RAW,
3166	},
3167	BTF_STR_SEC("\0A\0B\0C\0D\0E"),
3168	.map_type = BPF_MAP_TYPE_ARRAY,
3169	.map_name = "union_type_check_btf",
3170	.key_size = sizeof(int),
3171	.value_size = sizeof(int),
3172	.key_type_id = 1,
3173	.value_type_id = 1,
3174	.max_entries = 4,
3175},
3176
3177{
3178	.descr = "invalid struct, kind_flag, bitfield_size greater than struct size",
3179	.raw_types = {
3180		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3181		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 4),	/* [2] */
3182		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(20, 0)),
3183		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(20, 20)),
3184		BTF_END_RAW,
3185	},
3186	BTF_STR_SEC("\0A\0B"),
3187	.map_type = BPF_MAP_TYPE_ARRAY,
3188	.map_name = "struct_type_check_btf",
3189	.key_size = sizeof(int),
3190	.value_size = sizeof(int),
3191	.key_type_id = 1,
3192	.value_type_id = 1,
3193	.max_entries = 4,
3194	.btf_load_err = true,
3195	.err_str = "Member exceeds struct_size",
3196},
3197
3198{
3199	.descr = "invalid struct, kind_flag, bitfield base_type int not regular",
3200	.raw_types = {
3201		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3202		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 20, 4),			/* [2] */
3203		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 4),	/* [3] */
3204		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(20, 0)),
3205		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(20, 20)),
3206		BTF_END_RAW,
3207	},
3208	BTF_STR_SEC("\0A\0B"),
3209	.map_type = BPF_MAP_TYPE_ARRAY,
3210	.map_name = "struct_type_check_btf",
3211	.key_size = sizeof(int),
3212	.value_size = sizeof(int),
3213	.key_type_id = 1,
3214	.value_type_id = 1,
3215	.max_entries = 4,
3216	.btf_load_err = true,
3217	.err_str = "Invalid member base type",
3218},
3219
3220{
3221	.descr = "invalid struct, kind_flag, base_type int not regular",
3222	.raw_types = {
3223		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3224		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 12, 4),			/* [2] */
3225		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 4),	/* [3] */
3226		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(8, 0)),
3227		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(8, 8)),
3228		BTF_END_RAW,
3229	},
3230	BTF_STR_SEC("\0A\0B"),
3231	.map_type = BPF_MAP_TYPE_ARRAY,
3232	.map_name = "struct_type_check_btf",
3233	.key_size = sizeof(int),
3234	.value_size = sizeof(int),
3235	.key_type_id = 1,
3236	.value_type_id = 1,
3237	.max_entries = 4,
3238	.btf_load_err = true,
3239	.err_str = "Invalid member base type",
3240},
3241
3242{
3243	.descr = "invalid union, kind_flag, bitfield_size greater than struct size",
3244	.raw_types = {
3245		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),		/* [1] */
3246		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 1, 2), 2),	/* [2] */
3247		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(8, 0)),
3248		BTF_MEMBER_ENC(NAME_TBD, 1, BTF_MEMBER_OFFSET(20, 0)),
3249		BTF_END_RAW,
3250	},
3251	BTF_STR_SEC("\0A\0B"),
3252	.map_type = BPF_MAP_TYPE_ARRAY,
3253	.map_name = "union_type_check_btf",
3254	.key_size = sizeof(int),
3255	.value_size = sizeof(int),
3256	.key_type_id = 1,
3257	.value_type_id = 1,
3258	.max_entries = 4,
3259	.btf_load_err = true,
3260	.err_str = "Member exceeds struct_size",
3261},
3262
3263{
3264	.descr = "invalid struct, kind_flag, int member, bitfield_size = 0, wrong byte alignment",
3265	.raw_types = {
3266		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3267		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [2] */
3268		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 12),	/* [3] */
3269		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 0)),
3270		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 36)),
3271		BTF_END_RAW,
3272	},
3273	BTF_STR_SEC("\0A\0B"),
3274	.map_type = BPF_MAP_TYPE_ARRAY,
3275	.map_name = "struct_type_check_btf",
3276	.key_size = sizeof(int),
3277	.value_size = sizeof(int),
3278	.key_type_id = 1,
3279	.value_type_id = 1,
3280	.max_entries = 4,
3281	.btf_load_err = true,
3282	.err_str = "Invalid member offset",
3283},
3284
3285{
3286	.descr = "invalid struct, kind_flag, enum member, bitfield_size = 0, wrong byte alignment",
3287	.raw_types = {
3288		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3289		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [2] */
3290		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),	/* [2] */
3291		BTF_ENUM_ENC(NAME_TBD, 0),
3292		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 2), 12),	/* [3] */
3293		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 0)),
3294		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 36)),
3295		BTF_END_RAW,
3296	},
3297	BTF_STR_SEC("\0A\0B\0C"),
3298	.map_type = BPF_MAP_TYPE_ARRAY,
3299	.map_name = "struct_type_check_btf",
3300	.key_size = sizeof(int),
3301	.value_size = sizeof(int),
3302	.key_type_id = 1,
3303	.value_type_id = 1,
3304	.max_entries = 4,
3305	.btf_load_err = true,
3306	.err_str = "Invalid member offset",
3307},
3308
3309{
3310	.descr = "128-bit int",
3311	.raw_types = {
3312		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3313		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 128, 16),		/* [2] */
3314		BTF_END_RAW,
3315	},
3316	BTF_STR_SEC("\0A"),
3317	.map_type = BPF_MAP_TYPE_ARRAY,
3318	.map_name = "int_type_check_btf",
3319	.key_size = sizeof(int),
3320	.value_size = sizeof(int),
3321	.key_type_id = 1,
3322	.value_type_id = 1,
3323	.max_entries = 4,
3324},
3325
3326{
3327	.descr = "struct, 128-bit int member",
3328	.raw_types = {
3329		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3330		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 128, 16),		/* [2] */
3331		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 16),	/* [3] */
3332		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3333		BTF_END_RAW,
3334	},
3335	BTF_STR_SEC("\0A"),
3336	.map_type = BPF_MAP_TYPE_ARRAY,
3337	.map_name = "struct_type_check_btf",
3338	.key_size = sizeof(int),
3339	.value_size = sizeof(int),
3340	.key_type_id = 1,
3341	.value_type_id = 1,
3342	.max_entries = 4,
3343},
3344
3345{
3346	.descr = "struct, 120-bit int member bitfield",
3347	.raw_types = {
3348		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3349		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 120, 16),		/* [2] */
3350		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 16),	/* [3] */
3351		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3352		BTF_END_RAW,
3353	},
3354	BTF_STR_SEC("\0A"),
3355	.map_type = BPF_MAP_TYPE_ARRAY,
3356	.map_name = "struct_type_check_btf",
3357	.key_size = sizeof(int),
3358	.value_size = sizeof(int),
3359	.key_type_id = 1,
3360	.value_type_id = 1,
3361	.max_entries = 4,
3362},
3363
3364{
3365	.descr = "struct, kind_flag, 128-bit int member",
3366	.raw_types = {
3367		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3368		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 128, 16),		/* [2] */
3369		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 1), 16),	/* [3] */
3370		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 0)),
3371		BTF_END_RAW,
3372	},
3373	BTF_STR_SEC("\0A"),
3374	.map_type = BPF_MAP_TYPE_ARRAY,
3375	.map_name = "struct_type_check_btf",
3376	.key_size = sizeof(int),
3377	.value_size = sizeof(int),
3378	.key_type_id = 1,
3379	.value_type_id = 1,
3380	.max_entries = 4,
3381},
3382
3383{
3384	.descr = "struct, kind_flag, 120-bit int member bitfield",
3385	.raw_types = {
3386		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
3387		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 128, 16),		/* [2] */
3388		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 1), 16),	/* [3] */
3389		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(120, 0)),
3390		BTF_END_RAW,
3391	},
3392	BTF_STR_SEC("\0A"),
3393	.map_type = BPF_MAP_TYPE_ARRAY,
3394	.map_name = "struct_type_check_btf",
3395	.key_size = sizeof(int),
3396	.value_size = sizeof(int),
3397	.key_type_id = 1,
3398	.value_type_id = 1,
3399	.max_entries = 4,
3400},
3401/*
3402 * typedef int arr_t[16];
3403 * struct s {
3404 *	arr_t *a;
3405 * };
3406 */
3407{
3408	.descr = "struct->ptr->typedef->array->int size resolution",
3409	.raw_types = {
3410		BTF_STRUCT_ENC(NAME_TBD, 1, 8),			/* [1] */
3411		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3412		BTF_PTR_ENC(3),					/* [2] */
3413		BTF_TYPEDEF_ENC(NAME_TBD, 4),			/* [3] */
3414		BTF_TYPE_ARRAY_ENC(5, 5, 16),			/* [4] */
3415		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [5] */
3416		BTF_END_RAW,
3417	},
3418	BTF_STR_SEC("\0s\0a\0arr_t"),
3419	.map_type = BPF_MAP_TYPE_ARRAY,
3420	.map_name = "ptr_mod_chain_size_resolve_map",
3421	.key_size = sizeof(int),
3422	.value_size = sizeof(int) * 16,
3423	.key_type_id = 5 /* int */,
3424	.value_type_id = 3 /* arr_t */,
3425	.max_entries = 4,
3426},
3427/*
3428 * typedef int arr_t[16][8][4];
3429 * struct s {
3430 *	arr_t *a;
3431 * };
3432 */
3433{
3434	.descr = "struct->ptr->typedef->multi-array->int size resolution",
3435	.raw_types = {
3436		BTF_STRUCT_ENC(NAME_TBD, 1, 8),			/* [1] */
3437		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3438		BTF_PTR_ENC(3),					/* [2] */
3439		BTF_TYPEDEF_ENC(NAME_TBD, 4),			/* [3] */
3440		BTF_TYPE_ARRAY_ENC(5, 7, 16),			/* [4] */
3441		BTF_TYPE_ARRAY_ENC(6, 7, 8),			/* [5] */
3442		BTF_TYPE_ARRAY_ENC(7, 7, 4),			/* [6] */
3443		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [7] */
3444		BTF_END_RAW,
3445	},
3446	BTF_STR_SEC("\0s\0a\0arr_t"),
3447	.map_type = BPF_MAP_TYPE_ARRAY,
3448	.map_name = "multi_arr_size_resolve_map",
3449	.key_size = sizeof(int),
3450	.value_size = sizeof(int) * 16 * 8 * 4,
3451	.key_type_id = 7 /* int */,
3452	.value_type_id = 3 /* arr_t */,
3453	.max_entries = 4,
3454},
3455/*
3456 * typedef int int_t;
3457 * typedef int_t arr3_t[4];
3458 * typedef arr3_t arr2_t[8];
3459 * typedef arr2_t arr1_t[16];
3460 * struct s {
3461 *	arr1_t *a;
3462 * };
3463 */
3464{
3465	.descr = "typedef/multi-arr mix size resolution",
3466	.raw_types = {
3467		BTF_STRUCT_ENC(NAME_TBD, 1, 8),			/* [1] */
3468		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3469		BTF_PTR_ENC(3),					/* [2] */
3470		BTF_TYPEDEF_ENC(NAME_TBD, 4),			/* [3] */
3471		BTF_TYPE_ARRAY_ENC(5, 10, 16),			/* [4] */
3472		BTF_TYPEDEF_ENC(NAME_TBD, 6),			/* [5] */
3473		BTF_TYPE_ARRAY_ENC(7, 10, 8),			/* [6] */
3474		BTF_TYPEDEF_ENC(NAME_TBD, 8),			/* [7] */
3475		BTF_TYPE_ARRAY_ENC(9, 10, 4),			/* [8] */
3476		BTF_TYPEDEF_ENC(NAME_TBD, 10),			/* [9] */
3477		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [10] */
3478		BTF_END_RAW,
3479	},
3480	BTF_STR_SEC("\0s\0a\0arr1_t\0arr2_t\0arr3_t\0int_t"),
3481	.map_type = BPF_MAP_TYPE_ARRAY,
3482	.map_name = "typedef_arra_mix_size_resolve_map",
3483	.key_size = sizeof(int),
3484	.value_size = sizeof(int) * 16 * 8 * 4,
3485	.key_type_id = 10 /* int */,
3486	.value_type_id = 3 /* arr_t */,
3487	.max_entries = 4,
3488},
3489/*
3490 * elf .rodata section size 4 and btf .rodata section vlen 0.
3491 */
3492{
3493	.descr = "datasec: vlen == 0",
3494	.raw_types = {
3495		/* int */
3496		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3497		/* .rodata section */
3498		BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 0), 4),
3499								 /* [2] */
3500		BTF_END_RAW,
3501	},
3502	BTF_STR_SEC("\0.rodata"),
3503	.map_type = BPF_MAP_TYPE_ARRAY,
3504	.key_size = sizeof(int),
3505	.value_size = sizeof(int),
3506	.key_type_id = 1,
3507	.value_type_id = 1,
3508	.max_entries = 1,
3509},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3510
3511{
3512	.descr = "float test #1, well-formed",
3513	.raw_types = {
3514		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
3515								/* [1] */
3516		BTF_TYPE_FLOAT_ENC(NAME_TBD, 2),		/* [2] */
3517		BTF_TYPE_FLOAT_ENC(NAME_TBD, 4),		/* [3] */
3518		BTF_TYPE_FLOAT_ENC(NAME_TBD, 8),		/* [4] */
3519		BTF_TYPE_FLOAT_ENC(NAME_TBD, 12),		/* [5] */
3520		BTF_TYPE_FLOAT_ENC(NAME_TBD, 16),		/* [6] */
3521		BTF_STRUCT_ENC(NAME_TBD, 5, 48),		/* [7] */
3522		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3523		BTF_MEMBER_ENC(NAME_TBD, 3, 32),
3524		BTF_MEMBER_ENC(NAME_TBD, 4, 64),
3525		BTF_MEMBER_ENC(NAME_TBD, 5, 128),
3526		BTF_MEMBER_ENC(NAME_TBD, 6, 256),
3527		BTF_END_RAW,
3528	},
3529	BTF_STR_SEC("\0int\0_Float16\0float\0double\0_Float80\0long_double"
3530		    "\0floats\0a\0b\0c\0d\0e"),
3531	.map_type = BPF_MAP_TYPE_ARRAY,
3532	.map_name = "float_type_check_btf",
3533	.key_size = sizeof(int),
3534	.value_size = 48,
3535	.key_type_id = 1,
3536	.value_type_id = 7,
3537	.max_entries = 1,
3538},
3539{
3540	.descr = "float test #2, invalid vlen",
3541	.raw_types = {
3542		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
3543								/* [1] */
3544		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 1), 4),
3545								/* [2] */
3546		BTF_END_RAW,
3547	},
3548	BTF_STR_SEC("\0int\0float"),
3549	.map_type = BPF_MAP_TYPE_ARRAY,
3550	.map_name = "float_type_check_btf",
3551	.key_size = sizeof(int),
3552	.value_size = 4,
3553	.key_type_id = 1,
3554	.value_type_id = 2,
3555	.max_entries = 1,
3556	.btf_load_err = true,
3557	.err_str = "vlen != 0",
3558},
3559{
3560	.descr = "float test #3, invalid kind_flag",
3561	.raw_types = {
3562		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
3563								/* [1] */
3564		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_FLOAT, 1, 0), 4),
3565								/* [2] */
3566		BTF_END_RAW,
3567	},
3568	BTF_STR_SEC("\0int\0float"),
3569	.map_type = BPF_MAP_TYPE_ARRAY,
3570	.map_name = "float_type_check_btf",
3571	.key_size = sizeof(int),
3572	.value_size = 4,
3573	.key_type_id = 1,
3574	.value_type_id = 2,
3575	.max_entries = 1,
3576	.btf_load_err = true,
3577	.err_str = "Invalid btf_info kind_flag",
3578},
3579{
3580	.descr = "float test #4, member does not fit",
3581	.raw_types = {
3582		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
3583								/* [1] */
3584		BTF_TYPE_FLOAT_ENC(NAME_TBD, 4),		/* [2] */
3585		BTF_STRUCT_ENC(NAME_TBD, 1, 2),			/* [3] */
3586		BTF_MEMBER_ENC(NAME_TBD, 2, 0),
3587		BTF_END_RAW,
3588	},
3589	BTF_STR_SEC("\0int\0float\0floats\0x"),
3590	.map_type = BPF_MAP_TYPE_ARRAY,
3591	.map_name = "float_type_check_btf",
3592	.key_size = sizeof(int),
3593	.value_size = 4,
3594	.key_type_id = 1,
3595	.value_type_id = 3,
3596	.max_entries = 1,
3597	.btf_load_err = true,
3598	.err_str = "Member exceeds struct_size",
3599},
3600{
3601	.descr = "float test #5, member is not properly aligned",
3602	.raw_types = {
3603		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
3604								/* [1] */
3605		BTF_TYPE_FLOAT_ENC(NAME_TBD, 4),		/* [2] */
3606		BTF_STRUCT_ENC(NAME_TBD, 1, 8),			/* [3] */
3607		BTF_MEMBER_ENC(NAME_TBD, 2, 8),
3608		BTF_END_RAW,
3609	},
3610	BTF_STR_SEC("\0int\0float\0floats\0x"),
3611	.map_type = BPF_MAP_TYPE_ARRAY,
3612	.map_name = "float_type_check_btf",
3613	.key_size = sizeof(int),
3614	.value_size = 4,
3615	.key_type_id = 1,
3616	.value_type_id = 3,
3617	.max_entries = 1,
3618	.btf_load_err = true,
3619	.err_str = "Member is not properly aligned",
3620},
3621{
3622	.descr = "float test #6, invalid size",
3623	.raw_types = {
3624		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
3625								/* [1] */
3626		BTF_TYPE_FLOAT_ENC(NAME_TBD, 6),		/* [2] */
3627		BTF_END_RAW,
3628	},
3629	BTF_STR_SEC("\0int\0float"),
3630	.map_type = BPF_MAP_TYPE_ARRAY,
3631	.map_name = "float_type_check_btf",
3632	.key_size = sizeof(int),
3633	.value_size = 6,
3634	.key_type_id = 1,
3635	.value_type_id = 2,
3636	.max_entries = 1,
3637	.btf_load_err = true,
3638	.err_str = "Invalid type_size",
3639},
3640
3641{
3642	.descr = "decl_tag test #1, struct/member, well-formed",
3643	.raw_types = {
3644		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3645		BTF_STRUCT_ENC(0, 2, 8),			/* [2] */
3646		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
3647		BTF_MEMBER_ENC(NAME_TBD, 1, 32),
3648		BTF_DECL_TAG_ENC(NAME_TBD, 2, -1),
3649		BTF_DECL_TAG_ENC(NAME_TBD, 2, 0),
3650		BTF_DECL_TAG_ENC(NAME_TBD, 2, 1),
3651		BTF_END_RAW,
3652	},
3653	BTF_STR_SEC("\0m1\0m2\0tag1\0tag2\0tag3"),
3654	.map_type = BPF_MAP_TYPE_ARRAY,
3655	.map_name = "tag_type_check_btf",
3656	.key_size = sizeof(int),
3657	.value_size = 8,
3658	.key_type_id = 1,
3659	.value_type_id = 2,
3660	.max_entries = 1,
3661},
3662{
3663	.descr = "decl_tag test #2, union/member, well-formed",
3664	.raw_types = {
3665		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3666		BTF_UNION_ENC(NAME_TBD, 2, 4),			/* [2] */
3667		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
3668		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
3669		BTF_DECL_TAG_ENC(NAME_TBD, 2, -1),
3670		BTF_DECL_TAG_ENC(NAME_TBD, 2, 0),
3671		BTF_DECL_TAG_ENC(NAME_TBD, 2, 1),
3672		BTF_END_RAW,
3673	},
3674	BTF_STR_SEC("\0t\0m1\0m2\0tag1\0tag2\0tag3"),
3675	.map_type = BPF_MAP_TYPE_ARRAY,
3676	.map_name = "tag_type_check_btf",
3677	.key_size = sizeof(int),
3678	.value_size = 4,
3679	.key_type_id = 1,
3680	.value_type_id = 2,
3681	.max_entries = 1,
3682},
3683{
3684	.descr = "decl_tag test #3, variable, well-formed",
3685	.raw_types = {
3686		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3687		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
3688		BTF_VAR_ENC(NAME_TBD, 1, 1),			/* [3] */
3689		BTF_DECL_TAG_ENC(NAME_TBD, 2, -1),
3690		BTF_DECL_TAG_ENC(NAME_TBD, 3, -1),
3691		BTF_END_RAW,
3692	},
3693	BTF_STR_SEC("\0local\0global\0tag1\0tag2"),
3694	.map_type = BPF_MAP_TYPE_ARRAY,
3695	.map_name = "tag_type_check_btf",
3696	.key_size = sizeof(int),
3697	.value_size = 4,
3698	.key_type_id = 1,
3699	.value_type_id = 1,
3700	.max_entries = 1,
3701},
3702{
3703	.descr = "decl_tag test #4, func/parameter, well-formed",
3704	.raw_types = {
3705		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3706		BTF_FUNC_PROTO_ENC(0, 2),			/* [2] */
3707			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3708			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3709		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
3710		BTF_DECL_TAG_ENC(NAME_TBD, 3, -1),
3711		BTF_DECL_TAG_ENC(NAME_TBD, 3, 0),
3712		BTF_DECL_TAG_ENC(NAME_TBD, 3, 1),
3713		BTF_END_RAW,
3714	},
3715	BTF_STR_SEC("\0arg1\0arg2\0f\0tag1\0tag2\0tag3"),
3716	.map_type = BPF_MAP_TYPE_ARRAY,
3717	.map_name = "tag_type_check_btf",
3718	.key_size = sizeof(int),
3719	.value_size = 4,
3720	.key_type_id = 1,
3721	.value_type_id = 1,
3722	.max_entries = 1,
3723},
3724{
3725	.descr = "decl_tag test #5, invalid value",
3726	.raw_types = {
3727		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3728		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
3729		BTF_DECL_TAG_ENC(0, 2, -1),
3730		BTF_END_RAW,
3731	},
3732	BTF_STR_SEC("\0local\0tag"),
3733	.map_type = BPF_MAP_TYPE_ARRAY,
3734	.map_name = "tag_type_check_btf",
3735	.key_size = sizeof(int),
3736	.value_size = 4,
3737	.key_type_id = 1,
3738	.value_type_id = 1,
3739	.max_entries = 1,
3740	.btf_load_err = true,
3741	.err_str = "Invalid value",
3742},
3743{
3744	.descr = "decl_tag test #6, invalid target type",
3745	.raw_types = {
3746		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3747		BTF_DECL_TAG_ENC(NAME_TBD, 1, -1),
3748		BTF_END_RAW,
3749	},
3750	BTF_STR_SEC("\0tag1"),
3751	.map_type = BPF_MAP_TYPE_ARRAY,
3752	.map_name = "tag_type_check_btf",
3753	.key_size = sizeof(int),
3754	.value_size = 4,
3755	.key_type_id = 1,
3756	.value_type_id = 1,
3757	.max_entries = 1,
3758	.btf_load_err = true,
3759	.err_str = "Invalid type",
3760},
3761{
3762	.descr = "decl_tag test #7, invalid vlen",
3763	.raw_types = {
3764		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3765		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
3766		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DECL_TAG, 0, 1), 2), (0),
3767		BTF_END_RAW,
3768	},
3769	BTF_STR_SEC("\0local\0tag1"),
3770	.map_type = BPF_MAP_TYPE_ARRAY,
3771	.map_name = "tag_type_check_btf",
3772	.key_size = sizeof(int),
3773	.value_size = 4,
3774	.key_type_id = 1,
3775	.value_type_id = 1,
3776	.max_entries = 1,
3777	.btf_load_err = true,
3778	.err_str = "vlen != 0",
3779},
3780{
3781	.descr = "decl_tag test #8, invalid kflag",
3782	.raw_types = {
3783		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3784		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
3785		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DECL_TAG, 1, 0), 2), (-1),
3786		BTF_END_RAW,
3787	},
3788	BTF_STR_SEC("\0local\0tag1"),
3789	.map_type = BPF_MAP_TYPE_ARRAY,
3790	.map_name = "tag_type_check_btf",
3791	.key_size = sizeof(int),
3792	.value_size = 4,
3793	.key_type_id = 1,
3794	.value_type_id = 1,
3795	.max_entries = 1,
3796	.btf_load_err = true,
3797	.err_str = "Invalid btf_info kind_flag",
3798},
3799{
3800	.descr = "decl_tag test #9, var, invalid component_idx",
3801	.raw_types = {
3802		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3803		BTF_VAR_ENC(NAME_TBD, 1, 0),			/* [2] */
3804		BTF_DECL_TAG_ENC(NAME_TBD, 2, 0),
3805		BTF_END_RAW,
3806	},
3807	BTF_STR_SEC("\0local\0tag"),
3808	.map_type = BPF_MAP_TYPE_ARRAY,
3809	.map_name = "tag_type_check_btf",
3810	.key_size = sizeof(int),
3811	.value_size = 4,
3812	.key_type_id = 1,
3813	.value_type_id = 1,
3814	.max_entries = 1,
3815	.btf_load_err = true,
3816	.err_str = "Invalid component_idx",
3817},
3818{
3819	.descr = "decl_tag test #10, struct member, invalid component_idx",
3820	.raw_types = {
3821		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3822		BTF_STRUCT_ENC(0, 2, 8),			/* [2] */
3823		BTF_MEMBER_ENC(NAME_TBD, 1, 0),
3824		BTF_MEMBER_ENC(NAME_TBD, 1, 32),
3825		BTF_DECL_TAG_ENC(NAME_TBD, 2, 2),
3826		BTF_END_RAW,
3827	},
3828	BTF_STR_SEC("\0m1\0m2\0tag"),
3829	.map_type = BPF_MAP_TYPE_ARRAY,
3830	.map_name = "tag_type_check_btf",
3831	.key_size = sizeof(int),
3832	.value_size = 8,
3833	.key_type_id = 1,
3834	.value_type_id = 2,
3835	.max_entries = 1,
3836	.btf_load_err = true,
3837	.err_str = "Invalid component_idx",
3838},
3839{
3840	.descr = "decl_tag test #11, func parameter, invalid component_idx",
3841	.raw_types = {
3842		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3843		BTF_FUNC_PROTO_ENC(0, 2),			/* [2] */
3844			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3845			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3846		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
3847		BTF_DECL_TAG_ENC(NAME_TBD, 3, 2),
3848		BTF_END_RAW,
3849	},
3850	BTF_STR_SEC("\0arg1\0arg2\0f\0tag"),
3851	.map_type = BPF_MAP_TYPE_ARRAY,
3852	.map_name = "tag_type_check_btf",
3853	.key_size = sizeof(int),
3854	.value_size = 4,
3855	.key_type_id = 1,
3856	.value_type_id = 1,
3857	.max_entries = 1,
3858	.btf_load_err = true,
3859	.err_str = "Invalid component_idx",
3860},
3861{
3862	.descr = "decl_tag test #12, < -1 component_idx",
3863	.raw_types = {
3864		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3865		BTF_FUNC_PROTO_ENC(0, 2),			/* [2] */
3866			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3867			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3868		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
3869		BTF_DECL_TAG_ENC(NAME_TBD, 3, -2),
3870		BTF_END_RAW,
3871	},
3872	BTF_STR_SEC("\0arg1\0arg2\0f\0tag"),
3873	.map_type = BPF_MAP_TYPE_ARRAY,
3874	.map_name = "tag_type_check_btf",
3875	.key_size = sizeof(int),
3876	.value_size = 4,
3877	.key_type_id = 1,
3878	.value_type_id = 1,
3879	.max_entries = 1,
3880	.btf_load_err = true,
3881	.err_str = "Invalid component_idx",
3882},
3883{
3884	.descr = "decl_tag test #13, typedef, well-formed",
3885	.raw_types = {
3886		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3887		BTF_TYPEDEF_ENC(NAME_TBD, 1),			/* [2] */
3888		BTF_DECL_TAG_ENC(NAME_TBD, 2, -1),
3889		BTF_END_RAW,
3890	},
3891	BTF_STR_SEC("\0t\0tag"),
3892	.map_type = BPF_MAP_TYPE_ARRAY,
3893	.map_name = "tag_type_check_btf",
3894	.key_size = sizeof(int),
3895	.value_size = 4,
3896	.key_type_id = 1,
3897	.value_type_id = 1,
3898	.max_entries = 1,
3899},
3900{
3901	.descr = "decl_tag test #14, typedef, invalid component_idx",
3902	.raw_types = {
3903		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3904		BTF_TYPEDEF_ENC(NAME_TBD, 1),			/* [2] */
3905		BTF_DECL_TAG_ENC(NAME_TBD, 2, 0),
3906		BTF_END_RAW,
3907	},
3908	BTF_STR_SEC("\0local\0tag"),
3909	.map_type = BPF_MAP_TYPE_ARRAY,
3910	.map_name = "tag_type_check_btf",
3911	.key_size = sizeof(int),
3912	.value_size = 4,
3913	.key_type_id = 1,
3914	.value_type_id = 1,
3915	.max_entries = 1,
3916	.btf_load_err = true,
3917	.err_str = "Invalid component_idx",
3918},
3919{
3920	.descr = "decl_tag test #15, func, invalid func proto",
3921	.raw_types = {
3922		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3923		BTF_DECL_TAG_ENC(NAME_TBD, 3, 0),		/* [2] */
3924		BTF_FUNC_ENC(NAME_TBD, 8),			/* [3] */
3925		BTF_END_RAW,
3926	},
3927	BTF_STR_SEC("\0tag\0func"),
3928	.map_type = BPF_MAP_TYPE_ARRAY,
3929	.map_name = "tag_type_check_btf",
3930	.key_size = sizeof(int),
3931	.value_size = 4,
3932	.key_type_id = 1,
3933	.value_type_id = 1,
3934	.max_entries = 1,
3935	.btf_load_err = true,
3936	.err_str = "Invalid type_id",
3937},
3938{
3939	.descr = "decl_tag test #16, func proto, return type",
3940	.raw_types = {
3941		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),				/* [1] */
3942		BTF_VAR_ENC(NAME_TBD, 1, 0),						/* [2] */
3943		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DECL_TAG, 0, 0), 2), (-1),	/* [3] */
3944		BTF_FUNC_PROTO_ENC(3, 0),						/* [4] */
3945		BTF_END_RAW,
3946	},
3947	BTF_STR_SEC("\0local\0tag1"),
3948	.btf_load_err = true,
3949	.err_str = "Invalid return type",
3950},
3951{
3952	.descr = "decl_tag test #17, func proto, argument",
3953	.raw_types = {
3954		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_DECL_TAG, 0, 0), 4), (-1),	/* [1] */
3955		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0), /* [2] */
3956		BTF_FUNC_PROTO_ENC(0, 1),			/* [3] */
3957			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
3958		BTF_VAR_ENC(NAME_TBD, 2, 0),			/* [4] */
3959		BTF_END_RAW,
3960	},
3961	BTF_STR_SEC("\0local\0tag1\0var"),
3962	.btf_load_err = true,
3963	.err_str = "Invalid arg#1",
3964},
3965{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3966	.descr = "type_tag test #1",
3967	.raw_types = {
3968		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3969		BTF_TYPE_TAG_ENC(NAME_TBD, 1),			/* [2] */
3970		BTF_PTR_ENC(2),					/* [3] */
3971		BTF_END_RAW,
3972	},
3973	BTF_STR_SEC("\0tag"),
3974	.map_type = BPF_MAP_TYPE_ARRAY,
3975	.map_name = "tag_type_check_btf",
3976	.key_size = sizeof(int),
3977	.value_size = 4,
3978	.key_type_id = 1,
3979	.value_type_id = 1,
3980	.max_entries = 1,
3981},
3982{
3983	.descr = "type_tag test #2, type tag order",
3984	.raw_types = {
3985		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
3986		BTF_CONST_ENC(3),				/* [2] */
3987		BTF_TYPE_TAG_ENC(NAME_TBD, 1),			/* [3] */
3988		BTF_END_RAW,
3989	},
3990	BTF_STR_SEC("\0tag"),
3991	.map_type = BPF_MAP_TYPE_ARRAY,
3992	.map_name = "tag_type_check_btf",
3993	.key_size = sizeof(int),
3994	.value_size = 4,
3995	.key_type_id = 1,
3996	.value_type_id = 1,
3997	.max_entries = 1,
3998	.btf_load_err = true,
3999	.err_str = "Type tags don't precede modifiers",
4000},
4001{
4002	.descr = "type_tag test #3, type tag order",
4003	.raw_types = {
4004		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
4005		BTF_TYPE_TAG_ENC(NAME_TBD, 3),			/* [2] */
4006		BTF_CONST_ENC(4),				/* [3] */
4007		BTF_TYPE_TAG_ENC(NAME_TBD, 1),			/* [4] */
4008		BTF_END_RAW,
4009	},
4010	BTF_STR_SEC("\0tag\0tag"),
4011	.map_type = BPF_MAP_TYPE_ARRAY,
4012	.map_name = "tag_type_check_btf",
4013	.key_size = sizeof(int),
4014	.value_size = 4,
4015	.key_type_id = 1,
4016	.value_type_id = 1,
4017	.max_entries = 1,
4018	.btf_load_err = true,
4019	.err_str = "Type tags don't precede modifiers",
4020},
4021{
4022	.descr = "type_tag test #4, type tag order",
4023	.raw_types = {
4024		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
4025		BTF_TYPEDEF_ENC(NAME_TBD, 3),			/* [2] */
4026		BTF_CONST_ENC(4),				/* [3] */
4027		BTF_TYPE_TAG_ENC(NAME_TBD, 1),			/* [4] */
4028		BTF_END_RAW,
4029	},
4030	BTF_STR_SEC("\0tag\0tag"),
4031	.map_type = BPF_MAP_TYPE_ARRAY,
4032	.map_name = "tag_type_check_btf",
4033	.key_size = sizeof(int),
4034	.value_size = 4,
4035	.key_type_id = 1,
4036	.value_type_id = 1,
4037	.max_entries = 1,
4038	.btf_load_err = true,
4039	.err_str = "Type tags don't precede modifiers",
4040},
4041{
4042	.descr = "type_tag test #5, type tag order",
4043	.raw_types = {
4044		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
4045		BTF_TYPE_TAG_ENC(NAME_TBD, 3),			/* [2] */
4046		BTF_CONST_ENC(1),				/* [3] */
4047		BTF_TYPE_TAG_ENC(NAME_TBD, 2),			/* [4] */
4048		BTF_END_RAW,
4049	},
4050	BTF_STR_SEC("\0tag\0tag"),
4051	.map_type = BPF_MAP_TYPE_ARRAY,
4052	.map_name = "tag_type_check_btf",
4053	.key_size = sizeof(int),
4054	.value_size = 4,
4055	.key_type_id = 1,
4056	.value_type_id = 1,
4057	.max_entries = 1,
4058},
4059{
4060	.descr = "type_tag test #6, type tag order",
4061	.raw_types = {
4062		BTF_PTR_ENC(2),					/* [1] */
4063		BTF_TYPE_TAG_ENC(NAME_TBD, 3),			/* [2] */
4064		BTF_CONST_ENC(4),				/* [3] */
4065		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [4] */
4066		BTF_PTR_ENC(6),					/* [5] */
4067		BTF_CONST_ENC(2),				/* [6] */
4068		BTF_END_RAW,
4069	},
4070	BTF_STR_SEC("\0tag"),
4071	.map_type = BPF_MAP_TYPE_ARRAY,
4072	.map_name = "tag_type_check_btf",
4073	.key_size = sizeof(int),
4074	.value_size = 4,
4075	.key_type_id = 1,
4076	.value_type_id = 1,
4077	.max_entries = 1,
4078	.btf_load_err = true,
4079	.err_str = "Type tags don't precede modifiers",
4080},
4081{
4082	.descr = "enum64 test #1, unsigned, size 8",
4083	.raw_types = {
4084		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
4085		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 2), 8),	/* [2] */
4086		BTF_ENUM64_ENC(NAME_TBD, 0, 0),
4087		BTF_ENUM64_ENC(NAME_TBD, 1, 1),
4088		BTF_END_RAW,
4089	},
4090	BTF_STR_SEC("\0a\0b\0c"),
4091	.map_type = BPF_MAP_TYPE_ARRAY,
4092	.map_name = "tag_type_check_btf",
4093	.key_size = sizeof(int),
4094	.value_size = 8,
4095	.key_type_id = 1,
4096	.value_type_id = 2,
4097	.max_entries = 1,
4098},
4099{
4100	.descr = "enum64 test #2, signed, size 4",
4101	.raw_types = {
4102		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),			/* [1] */
4103		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM64, 1, 2), 4),	/* [2] */
4104		BTF_ENUM64_ENC(NAME_TBD, -1, 0),
4105		BTF_ENUM64_ENC(NAME_TBD, 1, 0),
4106		BTF_END_RAW,
4107	},
4108	BTF_STR_SEC("\0a\0b\0c"),
4109	.map_type = BPF_MAP_TYPE_ARRAY,
4110	.map_name = "tag_type_check_btf",
4111	.key_size = sizeof(int),
4112	.value_size = 4,
4113	.key_type_id = 1,
4114	.value_type_id = 2,
4115	.max_entries = 1,
4116},
4117
4118}; /* struct btf_raw_test raw_tests[] */
4119
4120static const char *get_next_str(const char *start, const char *end)
4121{
4122	return start < end - 1 ? start + 1 : NULL;
4123}
4124
4125static int get_raw_sec_size(const __u32 *raw_types)
4126{
4127	int i;
4128
4129	for (i = MAX_NR_RAW_U32 - 1;
4130	     i >= 0 && raw_types[i] != BTF_END_RAW;
4131	     i--)
4132		;
4133
4134	return i < 0 ? i : i * sizeof(raw_types[0]);
4135}
4136
4137static void *btf_raw_create(const struct btf_header *hdr,
4138			    const __u32 *raw_types,
4139			    const char *str,
4140			    unsigned int str_sec_size,
4141			    unsigned int *btf_size,
4142			    const char **ret_next_str)
4143{
4144	const char *next_str = str, *end_str = str + str_sec_size;
4145	const char **strs_idx = NULL, **tmp_strs_idx;
4146	int strs_cap = 0, strs_cnt = 0, next_str_idx = 0;
4147	unsigned int size_needed, offset;
4148	struct btf_header *ret_hdr;
4149	int i, type_sec_size, err = 0;
4150	uint32_t *ret_types;
4151	void *raw_btf = NULL;
4152
4153	type_sec_size = get_raw_sec_size(raw_types);
4154	if (CHECK(type_sec_size < 0, "Cannot get nr_raw_types"))
4155		return NULL;
4156
4157	size_needed = sizeof(*hdr) + type_sec_size + str_sec_size;
4158	raw_btf = malloc(size_needed);
4159	if (CHECK(!raw_btf, "Cannot allocate memory for raw_btf"))
4160		return NULL;
4161
4162	/* Copy header */
4163	memcpy(raw_btf, hdr, sizeof(*hdr));
4164	offset = sizeof(*hdr);
4165
4166	/* Index strings */
4167	while ((next_str = get_next_str(next_str, end_str))) {
4168		if (strs_cnt == strs_cap) {
4169			strs_cap += max(16, strs_cap / 2);
4170			tmp_strs_idx = realloc(strs_idx,
4171					       sizeof(*strs_idx) * strs_cap);
4172			if (CHECK(!tmp_strs_idx,
4173				  "Cannot allocate memory for strs_idx")) {
4174				err = -1;
4175				goto done;
4176			}
4177			strs_idx = tmp_strs_idx;
4178		}
4179		strs_idx[strs_cnt++] = next_str;
4180		next_str += strlen(next_str);
4181	}
4182
4183	/* Copy type section */
4184	ret_types = raw_btf + offset;
4185	for (i = 0; i < type_sec_size / sizeof(raw_types[0]); i++) {
4186		if (raw_types[i] == NAME_TBD) {
4187			if (CHECK(next_str_idx == strs_cnt,
4188				  "Error in getting next_str #%d",
4189				  next_str_idx)) {
4190				err = -1;
4191				goto done;
4192			}
4193			ret_types[i] = strs_idx[next_str_idx++] - str;
4194		} else if (IS_NAME_NTH(raw_types[i])) {
4195			int idx = GET_NAME_NTH_IDX(raw_types[i]);
4196
4197			if (CHECK(idx <= 0 || idx > strs_cnt,
4198				  "Error getting string #%d, strs_cnt:%d",
4199				  idx, strs_cnt)) {
4200				err = -1;
4201				goto done;
4202			}
4203			ret_types[i] = strs_idx[idx-1] - str;
4204		} else {
4205			ret_types[i] = raw_types[i];
4206		}
4207	}
4208	offset += type_sec_size;
4209
4210	/* Copy string section */
4211	memcpy(raw_btf + offset, str, str_sec_size);
4212
4213	ret_hdr = (struct btf_header *)raw_btf;
4214	ret_hdr->type_len = type_sec_size;
4215	ret_hdr->str_off = type_sec_size;
4216	ret_hdr->str_len = str_sec_size;
4217
4218	*btf_size = size_needed;
4219	if (ret_next_str)
4220		*ret_next_str =
4221			next_str_idx < strs_cnt ? strs_idx[next_str_idx] : NULL;
4222
4223done:
4224	free(strs_idx);
4225	if (err) {
4226		free(raw_btf);
4227		return NULL;
4228	}
4229	return raw_btf;
4230}
4231
4232static int load_raw_btf(const void *raw_data, size_t raw_size)
4233{
4234	LIBBPF_OPTS(bpf_btf_load_opts, opts);
4235	int btf_fd;
4236
4237	if (always_log) {
4238		opts.log_buf = btf_log_buf,
4239		opts.log_size = BTF_LOG_BUF_SIZE,
4240		opts.log_level = 1;
4241	}
4242
4243	btf_fd = bpf_btf_load(raw_data, raw_size, &opts);
4244	if (btf_fd < 0 && !always_log) {
4245		opts.log_buf = btf_log_buf,
4246		opts.log_size = BTF_LOG_BUF_SIZE,
4247		opts.log_level = 1;
4248		btf_fd = bpf_btf_load(raw_data, raw_size, &opts);
4249	}
4250
4251	return btf_fd;
4252}
4253
4254static void do_test_raw(unsigned int test_num)
4255{
4256	struct btf_raw_test *test = &raw_tests[test_num - 1];
4257	LIBBPF_OPTS(bpf_map_create_opts, opts);
4258	int map_fd = -1, btf_fd = -1;
4259	unsigned int raw_btf_size;
4260	struct btf_header *hdr;
4261	void *raw_btf;
4262	int err;
4263
4264	if (!test__start_subtest(test->descr))
4265		return;
4266
4267	raw_btf = btf_raw_create(&hdr_tmpl,
4268				 test->raw_types,
4269				 test->str_sec,
4270				 test->str_sec_size,
4271				 &raw_btf_size, NULL);
4272	if (!raw_btf)
4273		return;
4274
4275	hdr = raw_btf;
4276
4277	hdr->hdr_len = (int)hdr->hdr_len + test->hdr_len_delta;
4278	hdr->type_off = (int)hdr->type_off + test->type_off_delta;
4279	hdr->str_off = (int)hdr->str_off + test->str_off_delta;
4280	hdr->str_len = (int)hdr->str_len + test->str_len_delta;
4281
4282	*btf_log_buf = '\0';
4283	btf_fd = load_raw_btf(raw_btf, raw_btf_size);
4284	free(raw_btf);
4285
4286	err = ((btf_fd < 0) != test->btf_load_err);
4287	if (CHECK(err, "btf_fd:%d test->btf_load_err:%u",
4288		  btf_fd, test->btf_load_err) ||
4289	    CHECK(test->err_str && !strstr(btf_log_buf, test->err_str),
4290		  "expected err_str:%s\n", test->err_str)) {
4291		err = -1;
4292		goto done;
4293	}
4294
4295	if (err || btf_fd < 0)
4296		goto done;
4297
 
 
 
4298	opts.btf_fd = btf_fd;
4299	opts.btf_key_type_id = test->key_type_id;
4300	opts.btf_value_type_id = test->value_type_id;
4301	map_fd = bpf_map_create(test->map_type, test->map_name,
4302				test->key_size, test->value_size, test->max_entries, &opts);
4303
4304	err = ((map_fd < 0) != test->map_create_err);
4305	CHECK(err, "map_fd:%d test->map_create_err:%u",
4306	      map_fd, test->map_create_err);
4307
4308done:
4309	if (*btf_log_buf && (err || always_log))
4310		fprintf(stderr, "\n%s", btf_log_buf);
4311	if (btf_fd >= 0)
4312		close(btf_fd);
4313	if (map_fd >= 0)
4314		close(map_fd);
4315}
4316
4317struct btf_get_info_test {
4318	const char *descr;
4319	const char *str_sec;
4320	__u32 raw_types[MAX_NR_RAW_U32];
4321	__u32 str_sec_size;
4322	int btf_size_delta;
4323	int (*special_test)(unsigned int test_num);
4324};
4325
4326static int test_big_btf_info(unsigned int test_num);
4327static int test_btf_id(unsigned int test_num);
4328
4329const struct btf_get_info_test get_info_tests[] = {
4330{
4331	.descr = "== raw_btf_size+1",
4332	.raw_types = {
4333		/* int */				/* [1] */
4334		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
4335		BTF_END_RAW,
4336	},
4337	.str_sec = "",
4338	.str_sec_size = sizeof(""),
4339	.btf_size_delta = 1,
4340},
4341{
4342	.descr = "== raw_btf_size-3",
4343	.raw_types = {
4344		/* int */				/* [1] */
4345		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
4346		BTF_END_RAW,
4347	},
4348	.str_sec = "",
4349	.str_sec_size = sizeof(""),
4350	.btf_size_delta = -3,
4351},
4352{
4353	.descr = "Large bpf_btf_info",
4354	.raw_types = {
4355		/* int */				/* [1] */
4356		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
4357		BTF_END_RAW,
4358	},
4359	.str_sec = "",
4360	.str_sec_size = sizeof(""),
4361	.special_test = test_big_btf_info,
4362},
4363{
4364	.descr = "BTF ID",
4365	.raw_types = {
4366		/* int */				/* [1] */
4367		BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
4368		/* unsigned int */			/* [2] */
4369		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),
4370		BTF_END_RAW,
4371	},
4372	.str_sec = "",
4373	.str_sec_size = sizeof(""),
4374	.special_test = test_btf_id,
4375},
4376};
4377
4378static int test_big_btf_info(unsigned int test_num)
4379{
4380	const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
4381	uint8_t *raw_btf = NULL, *user_btf = NULL;
4382	unsigned int raw_btf_size;
4383	struct {
4384		struct bpf_btf_info info;
4385		uint64_t garbage;
4386	} info_garbage;
4387	struct bpf_btf_info *info;
4388	int btf_fd = -1, err;
4389	uint32_t info_len;
4390
4391	raw_btf = btf_raw_create(&hdr_tmpl,
4392				 test->raw_types,
4393				 test->str_sec,
4394				 test->str_sec_size,
4395				 &raw_btf_size, NULL);
4396
4397	if (!raw_btf)
4398		return -1;
4399
4400	*btf_log_buf = '\0';
4401
4402	user_btf = malloc(raw_btf_size);
4403	if (CHECK(!user_btf, "!user_btf")) {
4404		err = -1;
4405		goto done;
4406	}
4407
4408	btf_fd = load_raw_btf(raw_btf, raw_btf_size);
4409	if (CHECK(btf_fd < 0, "errno:%d", errno)) {
4410		err = -1;
4411		goto done;
4412	}
4413
4414	/*
4415	 * GET_INFO should error out if the userspace info
4416	 * has non zero tailing bytes.
4417	 */
4418	info = &info_garbage.info;
4419	memset(info, 0, sizeof(*info));
4420	info_garbage.garbage = 0xdeadbeef;
4421	info_len = sizeof(info_garbage);
4422	info->btf = ptr_to_u64(user_btf);
4423	info->btf_size = raw_btf_size;
4424
4425	err = bpf_obj_get_info_by_fd(btf_fd, info, &info_len);
4426	if (CHECK(!err, "!err")) {
4427		err = -1;
4428		goto done;
4429	}
4430
4431	/*
4432	 * GET_INFO should succeed even info_len is larger than
4433	 * the kernel supported as long as tailing bytes are zero.
4434	 * The kernel supported info len should also be returned
4435	 * to userspace.
4436	 */
4437	info_garbage.garbage = 0;
4438	err = bpf_obj_get_info_by_fd(btf_fd, info, &info_len);
4439	if (CHECK(err || info_len != sizeof(*info),
4440		  "err:%d errno:%d info_len:%u sizeof(*info):%zu",
4441		  err, errno, info_len, sizeof(*info))) {
4442		err = -1;
4443		goto done;
4444	}
4445
4446	fprintf(stderr, "OK");
4447
4448done:
4449	if (*btf_log_buf && (err || always_log))
4450		fprintf(stderr, "\n%s", btf_log_buf);
4451
4452	free(raw_btf);
4453	free(user_btf);
4454
4455	if (btf_fd >= 0)
4456		close(btf_fd);
4457
4458	return err;
4459}
4460
4461static int test_btf_id(unsigned int test_num)
4462{
4463	const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
4464	LIBBPF_OPTS(bpf_map_create_opts, opts);
4465	uint8_t *raw_btf = NULL, *user_btf[2] = {};
4466	int btf_fd[2] = {-1, -1}, map_fd = -1;
4467	struct bpf_map_info map_info = {};
4468	struct bpf_btf_info info[2] = {};
4469	unsigned int raw_btf_size;
4470	uint32_t info_len;
4471	int err, i, ret;
4472
4473	raw_btf = btf_raw_create(&hdr_tmpl,
4474				 test->raw_types,
4475				 test->str_sec,
4476				 test->str_sec_size,
4477				 &raw_btf_size, NULL);
4478
4479	if (!raw_btf)
4480		return -1;
4481
4482	*btf_log_buf = '\0';
4483
4484	for (i = 0; i < 2; i++) {
4485		user_btf[i] = malloc(raw_btf_size);
4486		if (CHECK(!user_btf[i], "!user_btf[%d]", i)) {
4487			err = -1;
4488			goto done;
4489		}
4490		info[i].btf = ptr_to_u64(user_btf[i]);
4491		info[i].btf_size = raw_btf_size;
4492	}
4493
4494	btf_fd[0] = load_raw_btf(raw_btf, raw_btf_size);
4495	if (CHECK(btf_fd[0] < 0, "errno:%d", errno)) {
4496		err = -1;
4497		goto done;
4498	}
4499
4500	/* Test BPF_OBJ_GET_INFO_BY_ID on btf_id */
4501	info_len = sizeof(info[0]);
4502	err = bpf_obj_get_info_by_fd(btf_fd[0], &info[0], &info_len);
4503	if (CHECK(err, "errno:%d", errno)) {
4504		err = -1;
4505		goto done;
4506	}
4507
4508	btf_fd[1] = bpf_btf_get_fd_by_id(info[0].id);
4509	if (CHECK(btf_fd[1] < 0, "errno:%d", errno)) {
4510		err = -1;
4511		goto done;
4512	}
4513
4514	ret = 0;
4515	err = bpf_obj_get_info_by_fd(btf_fd[1], &info[1], &info_len);
4516	if (CHECK(err || info[0].id != info[1].id ||
4517		  info[0].btf_size != info[1].btf_size ||
4518		  (ret = memcmp(user_btf[0], user_btf[1], info[0].btf_size)),
4519		  "err:%d errno:%d id0:%u id1:%u btf_size0:%u btf_size1:%u memcmp:%d",
4520		  err, errno, info[0].id, info[1].id,
4521		  info[0].btf_size, info[1].btf_size, ret)) {
4522		err = -1;
4523		goto done;
4524	}
4525
4526	/* Test btf members in struct bpf_map_info */
4527	opts.btf_fd = btf_fd[0];
4528	opts.btf_key_type_id = 1;
4529	opts.btf_value_type_id = 2;
4530	map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "test_btf_id",
4531				sizeof(int), sizeof(int), 4, &opts);
4532	if (CHECK(map_fd < 0, "errno:%d", errno)) {
4533		err = -1;
4534		goto done;
4535	}
4536
4537	info_len = sizeof(map_info);
4538	err = bpf_obj_get_info_by_fd(map_fd, &map_info, &info_len);
4539	if (CHECK(err || map_info.btf_id != info[0].id ||
4540		  map_info.btf_key_type_id != 1 || map_info.btf_value_type_id != 2,
4541		  "err:%d errno:%d info.id:%u btf_id:%u btf_key_type_id:%u btf_value_type_id:%u",
4542		  err, errno, info[0].id, map_info.btf_id, map_info.btf_key_type_id,
4543		  map_info.btf_value_type_id)) {
4544		err = -1;
4545		goto done;
4546	}
4547
4548	for (i = 0; i < 2; i++) {
4549		close(btf_fd[i]);
4550		btf_fd[i] = -1;
4551	}
4552
4553	/* Test BTF ID is removed from the kernel */
4554	btf_fd[0] = bpf_btf_get_fd_by_id(map_info.btf_id);
4555	if (CHECK(btf_fd[0] < 0, "errno:%d", errno)) {
4556		err = -1;
4557		goto done;
4558	}
4559	close(btf_fd[0]);
4560	btf_fd[0] = -1;
4561
4562	/* The map holds the last ref to BTF and its btf_id */
4563	close(map_fd);
4564	map_fd = -1;
4565	btf_fd[0] = bpf_btf_get_fd_by_id(map_info.btf_id);
4566	if (CHECK(btf_fd[0] >= 0, "BTF lingers")) {
4567		err = -1;
4568		goto done;
4569	}
4570
4571	fprintf(stderr, "OK");
4572
4573done:
4574	if (*btf_log_buf && (err || always_log))
4575		fprintf(stderr, "\n%s", btf_log_buf);
4576
4577	free(raw_btf);
4578	if (map_fd >= 0)
4579		close(map_fd);
4580	for (i = 0; i < 2; i++) {
4581		free(user_btf[i]);
4582		if (btf_fd[i] >= 0)
4583			close(btf_fd[i]);
4584	}
4585
4586	return err;
4587}
4588
4589static void do_test_get_info(unsigned int test_num)
4590{
4591	const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
4592	unsigned int raw_btf_size, user_btf_size, expected_nbytes;
4593	uint8_t *raw_btf = NULL, *user_btf = NULL;
4594	struct bpf_btf_info info = {};
4595	int btf_fd = -1, err, ret;
4596	uint32_t info_len;
4597
4598	if (!test__start_subtest(test->descr))
4599		return;
4600
4601	if (test->special_test) {
4602		err = test->special_test(test_num);
4603		if (CHECK(err, "failed: %d\n", err))
4604			return;
4605	}
4606
4607	raw_btf = btf_raw_create(&hdr_tmpl,
4608				 test->raw_types,
4609				 test->str_sec,
4610				 test->str_sec_size,
4611				 &raw_btf_size, NULL);
4612
4613	if (!raw_btf)
4614		return;
4615
4616	*btf_log_buf = '\0';
4617
4618	user_btf = malloc(raw_btf_size);
4619	if (CHECK(!user_btf, "!user_btf")) {
4620		err = -1;
4621		goto done;
4622	}
4623
4624	btf_fd = load_raw_btf(raw_btf, raw_btf_size);
4625	if (CHECK(btf_fd <= 0, "errno:%d", errno)) {
4626		err = -1;
4627		goto done;
4628	}
4629
4630	user_btf_size = (int)raw_btf_size + test->btf_size_delta;
4631	expected_nbytes = min(raw_btf_size, user_btf_size);
4632	if (raw_btf_size > expected_nbytes)
4633		memset(user_btf + expected_nbytes, 0xff,
4634		       raw_btf_size - expected_nbytes);
4635
4636	info_len = sizeof(info);
4637	info.btf = ptr_to_u64(user_btf);
4638	info.btf_size = user_btf_size;
4639
4640	ret = 0;
4641	err = bpf_obj_get_info_by_fd(btf_fd, &info, &info_len);
4642	if (CHECK(err || !info.id || info_len != sizeof(info) ||
4643		  info.btf_size != raw_btf_size ||
4644		  (ret = memcmp(raw_btf, user_btf, expected_nbytes)),
4645		  "err:%d errno:%d info.id:%u info_len:%u sizeof(info):%zu raw_btf_size:%u info.btf_size:%u expected_nbytes:%u memcmp:%d",
4646		  err, errno, info.id, info_len, sizeof(info),
4647		  raw_btf_size, info.btf_size, expected_nbytes, ret)) {
4648		err = -1;
4649		goto done;
4650	}
4651
4652	while (expected_nbytes < raw_btf_size) {
4653		fprintf(stderr, "%u...", expected_nbytes);
4654		if (CHECK(user_btf[expected_nbytes++] != 0xff,
4655			  "user_btf[%u]:%x != 0xff", expected_nbytes - 1,
4656			  user_btf[expected_nbytes - 1])) {
4657			err = -1;
4658			goto done;
4659		}
4660	}
4661
4662	fprintf(stderr, "OK");
4663
4664done:
4665	if (*btf_log_buf && (err || always_log))
4666		fprintf(stderr, "\n%s", btf_log_buf);
4667
4668	free(raw_btf);
4669	free(user_btf);
4670
4671	if (btf_fd >= 0)
4672		close(btf_fd);
4673}
4674
4675struct btf_file_test {
4676	const char *file;
4677	bool btf_kv_notfound;
4678};
4679
4680static struct btf_file_test file_tests[] = {
4681	{ .file = "test_btf_newkv.bpf.o", },
4682	{ .file = "test_btf_nokv.bpf.o", .btf_kv_notfound = true, },
4683};
4684
4685static void do_test_file(unsigned int test_num)
4686{
4687	const struct btf_file_test *test = &file_tests[test_num - 1];
4688	const char *expected_fnames[] = {"_dummy_tracepoint",
4689					 "test_long_fname_1",
4690					 "test_long_fname_2"};
4691	struct btf_ext *btf_ext = NULL;
4692	struct bpf_prog_info info = {};
4693	struct bpf_object *obj = NULL;
4694	struct bpf_func_info *finfo;
4695	struct bpf_program *prog;
4696	__u32 info_len, rec_size;
4697	bool has_btf_ext = false;
4698	struct btf *btf = NULL;
4699	void *func_info = NULL;
4700	struct bpf_map *map;
4701	int i, err, prog_fd;
4702
4703	if (!test__start_subtest(test->file))
4704		return;
4705
4706	btf = btf__parse_elf(test->file, &btf_ext);
4707	err = libbpf_get_error(btf);
4708	if (err) {
4709		if (err == -ENOENT) {
4710			printf("%s:SKIP: No ELF %s found", __func__, BTF_ELF_SEC);
4711			test__skip();
4712			return;
4713		}
4714		return;
4715	}
4716	btf__free(btf);
4717
4718	has_btf_ext = btf_ext != NULL;
4719	btf_ext__free(btf_ext);
4720
4721	/* temporary disable LIBBPF_STRICT_MAP_DEFINITIONS to test legacy maps */
4722	libbpf_set_strict_mode(LIBBPF_STRICT_ALL & ~LIBBPF_STRICT_MAP_DEFINITIONS);
4723	obj = bpf_object__open(test->file);
4724	err = libbpf_get_error(obj);
4725	if (CHECK(err, "obj: %d", err))
4726		return;
4727
4728	prog = bpf_object__next_program(obj, NULL);
4729	if (CHECK(!prog, "Cannot find bpf_prog")) {
4730		err = -1;
4731		goto done;
4732	}
4733
4734	bpf_program__set_type(prog, BPF_PROG_TYPE_TRACEPOINT);
4735	err = bpf_object__load(obj);
4736	if (CHECK(err < 0, "bpf_object__load: %d", err))
4737		goto done;
4738	prog_fd = bpf_program__fd(prog);
4739
4740	map = bpf_object__find_map_by_name(obj, "btf_map");
4741	if (CHECK(!map, "btf_map not found")) {
4742		err = -1;
4743		goto done;
4744	}
4745
4746	err = (bpf_map__btf_key_type_id(map) == 0 || bpf_map__btf_value_type_id(map) == 0)
4747		!= test->btf_kv_notfound;
4748	if (CHECK(err, "btf_key_type_id:%u btf_value_type_id:%u test->btf_kv_notfound:%u",
4749		  bpf_map__btf_key_type_id(map), bpf_map__btf_value_type_id(map),
4750		  test->btf_kv_notfound))
4751		goto done;
4752
4753	if (!has_btf_ext)
4754		goto skip;
4755
4756	/* get necessary program info */
4757	info_len = sizeof(struct bpf_prog_info);
4758	err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
4759
4760	if (CHECK(err < 0, "invalid get info (1st) errno:%d", errno)) {
4761		fprintf(stderr, "%s\n", btf_log_buf);
4762		err = -1;
4763		goto done;
4764	}
4765	if (CHECK(info.nr_func_info != 3,
4766		  "incorrect info.nr_func_info (1st) %d",
4767		  info.nr_func_info)) {
4768		err = -1;
4769		goto done;
4770	}
4771	rec_size = info.func_info_rec_size;
4772	if (CHECK(rec_size != sizeof(struct bpf_func_info),
4773		  "incorrect info.func_info_rec_size (1st) %d\n", rec_size)) {
4774		err = -1;
4775		goto done;
4776	}
4777
4778	func_info = malloc(info.nr_func_info * rec_size);
4779	if (CHECK(!func_info, "out of memory")) {
4780		err = -1;
4781		goto done;
4782	}
4783
4784	/* reset info to only retrieve func_info related data */
4785	memset(&info, 0, sizeof(info));
4786	info.nr_func_info = 3;
4787	info.func_info_rec_size = rec_size;
4788	info.func_info = ptr_to_u64(func_info);
4789
4790	err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
4791
4792	if (CHECK(err < 0, "invalid get info (2nd) errno:%d", errno)) {
4793		fprintf(stderr, "%s\n", btf_log_buf);
4794		err = -1;
4795		goto done;
4796	}
4797	if (CHECK(info.nr_func_info != 3,
4798		  "incorrect info.nr_func_info (2nd) %d",
4799		  info.nr_func_info)) {
4800		err = -1;
4801		goto done;
4802	}
4803	if (CHECK(info.func_info_rec_size != rec_size,
4804		  "incorrect info.func_info_rec_size (2nd) %d",
4805		  info.func_info_rec_size)) {
4806		err = -1;
4807		goto done;
4808	}
4809
4810	btf = btf__load_from_kernel_by_id(info.btf_id);
4811	err = libbpf_get_error(btf);
4812	if (CHECK(err, "cannot get btf from kernel, err: %d", err))
4813		goto done;
4814
4815	/* check three functions */
4816	finfo = func_info;
4817	for (i = 0; i < 3; i++) {
4818		const struct btf_type *t;
4819		const char *fname;
4820
4821		t = btf__type_by_id(btf, finfo->type_id);
4822		if (CHECK(!t, "btf__type_by_id failure: id %u",
4823			  finfo->type_id)) {
4824			err = -1;
4825			goto done;
4826		}
4827
4828		fname = btf__name_by_offset(btf, t->name_off);
4829		err = strcmp(fname, expected_fnames[i]);
4830		/* for the second and third functions in .text section,
4831		 * the compiler may order them either way.
4832		 */
4833		if (i && err)
4834			err = strcmp(fname, expected_fnames[3 - i]);
4835		if (CHECK(err, "incorrect fname %s", fname ? : "")) {
4836			err = -1;
4837			goto done;
4838		}
4839
4840		finfo = (void *)finfo + rec_size;
4841	}
4842
4843skip:
4844	fprintf(stderr, "OK");
4845
4846done:
4847	libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
4848
4849	btf__free(btf);
4850	free(func_info);
4851	bpf_object__close(obj);
4852}
4853
4854const char *pprint_enum_str[] = {
4855	"ENUM_ZERO",
4856	"ENUM_ONE",
4857	"ENUM_TWO",
4858	"ENUM_THREE",
4859};
4860
4861struct pprint_mapv {
4862	uint32_t ui32;
4863	uint16_t ui16;
4864	/* 2 bytes hole */
4865	int32_t si32;
4866	uint32_t unused_bits2a:2,
4867		bits28:28,
4868		unused_bits2b:2;
4869	union {
4870		uint64_t ui64;
4871		uint8_t ui8a[8];
4872	};
4873	enum {
4874		ENUM_ZERO,
4875		ENUM_ONE,
4876		ENUM_TWO,
4877		ENUM_THREE,
4878	} aenum;
4879	uint32_t ui32b;
4880	uint32_t bits2c:2;
4881	uint8_t si8_4[2][2];
4882};
4883
4884#ifdef __SIZEOF_INT128__
4885struct pprint_mapv_int128 {
4886	__int128 si128a;
4887	__int128 si128b;
4888	unsigned __int128 bits3:3;
4889	unsigned __int128 bits80:80;
4890	unsigned __int128 ui128;
4891};
4892#endif
4893
4894static struct btf_raw_test pprint_test_template[] = {
4895{
4896	.raw_types = {
4897		/* unsighed char */			/* [1] */
4898		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 8, 1),
4899		/* unsigned short */			/* [2] */
4900		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 16, 2),
4901		/* unsigned int */			/* [3] */
4902		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),
4903		/* int */				/* [4] */
4904		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
4905		/* unsigned long long */		/* [5] */
4906		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 64, 8),
4907		/* 2 bits */				/* [6] */
4908		BTF_TYPE_INT_ENC(0, 0, 0, 2, 2),
4909		/* 28 bits */				/* [7] */
4910		BTF_TYPE_INT_ENC(0, 0, 0, 28, 4),
4911		/* uint8_t[8] */			/* [8] */
4912		BTF_TYPE_ARRAY_ENC(9, 1, 8),
4913		/* typedef unsigned char uint8_t */	/* [9] */
4914		BTF_TYPEDEF_ENC(NAME_TBD, 1),
4915		/* typedef unsigned short uint16_t */	/* [10] */
4916		BTF_TYPEDEF_ENC(NAME_TBD, 2),
4917		/* typedef unsigned int uint32_t */	/* [11] */
4918		BTF_TYPEDEF_ENC(NAME_TBD, 3),
4919		/* typedef int int32_t */		/* [12] */
4920		BTF_TYPEDEF_ENC(NAME_TBD, 4),
4921		/* typedef unsigned long long uint64_t *//* [13] */
4922		BTF_TYPEDEF_ENC(NAME_TBD, 5),
4923		/* union (anon) */			/* [14] */
4924		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 0, 2), 8),
4925		BTF_MEMBER_ENC(NAME_TBD, 13, 0),/* uint64_t ui64; */
4926		BTF_MEMBER_ENC(NAME_TBD, 8, 0),	/* uint8_t ui8a[8]; */
4927		/* enum (anon) */			/* [15] */
4928		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 4), 4),
4929		BTF_ENUM_ENC(NAME_TBD, 0),
4930		BTF_ENUM_ENC(NAME_TBD, 1),
4931		BTF_ENUM_ENC(NAME_TBD, 2),
4932		BTF_ENUM_ENC(NAME_TBD, 3),
4933		/* struct pprint_mapv */		/* [16] */
4934		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 11), 40),
4935		BTF_MEMBER_ENC(NAME_TBD, 11, 0),	/* uint32_t ui32 */
4936		BTF_MEMBER_ENC(NAME_TBD, 10, 32),	/* uint16_t ui16 */
4937		BTF_MEMBER_ENC(NAME_TBD, 12, 64),	/* int32_t si32 */
4938		BTF_MEMBER_ENC(NAME_TBD, 6, 96),	/* unused_bits2a */
4939		BTF_MEMBER_ENC(NAME_TBD, 7, 98),	/* bits28 */
4940		BTF_MEMBER_ENC(NAME_TBD, 6, 126),	/* unused_bits2b */
4941		BTF_MEMBER_ENC(0, 14, 128),		/* union (anon) */
4942		BTF_MEMBER_ENC(NAME_TBD, 15, 192),	/* aenum */
4943		BTF_MEMBER_ENC(NAME_TBD, 11, 224),	/* uint32_t ui32b */
4944		BTF_MEMBER_ENC(NAME_TBD, 6, 256),	/* bits2c */
4945		BTF_MEMBER_ENC(NAME_TBD, 17, 264),	/* si8_4 */
4946		BTF_TYPE_ARRAY_ENC(18, 1, 2),		/* [17] */
4947		BTF_TYPE_ARRAY_ENC(1, 1, 2),		/* [18] */
4948		BTF_END_RAW,
4949	},
4950	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"),
4951	.key_size = sizeof(unsigned int),
4952	.value_size = sizeof(struct pprint_mapv),
4953	.key_type_id = 3,	/* unsigned int */
4954	.value_type_id = 16,	/* struct pprint_mapv */
4955	.max_entries = 128,
4956},
4957
4958{
4959	/* this type will have the same type as the
4960	 * first .raw_types definition, but struct type will
4961	 * be encoded with kind_flag set.
4962	 */
4963	.raw_types = {
4964		/* unsighed char */			/* [1] */
4965		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 8, 1),
4966		/* unsigned short */			/* [2] */
4967		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 16, 2),
4968		/* unsigned int */			/* [3] */
4969		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),
4970		/* int */				/* [4] */
4971		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
4972		/* unsigned long long */		/* [5] */
4973		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 64, 8),
4974		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),	/* [6] */
4975		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),	/* [7] */
4976		/* uint8_t[8] */			/* [8] */
4977		BTF_TYPE_ARRAY_ENC(9, 1, 8),
4978		/* typedef unsigned char uint8_t */	/* [9] */
4979		BTF_TYPEDEF_ENC(NAME_TBD, 1),
4980		/* typedef unsigned short uint16_t */	/* [10] */
4981		BTF_TYPEDEF_ENC(NAME_TBD, 2),
4982		/* typedef unsigned int uint32_t */	/* [11] */
4983		BTF_TYPEDEF_ENC(NAME_TBD, 3),
4984		/* typedef int int32_t */		/* [12] */
4985		BTF_TYPEDEF_ENC(NAME_TBD, 4),
4986		/* typedef unsigned long long uint64_t *//* [13] */
4987		BTF_TYPEDEF_ENC(NAME_TBD, 5),
4988		/* union (anon) */			/* [14] */
4989		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 0, 2), 8),
4990		BTF_MEMBER_ENC(NAME_TBD, 13, 0),/* uint64_t ui64; */
4991		BTF_MEMBER_ENC(NAME_TBD, 8, 0),	/* uint8_t ui8a[8]; */
4992		/* enum (anon) */			/* [15] */
4993		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 4), 4),
4994		BTF_ENUM_ENC(NAME_TBD, 0),
4995		BTF_ENUM_ENC(NAME_TBD, 1),
4996		BTF_ENUM_ENC(NAME_TBD, 2),
4997		BTF_ENUM_ENC(NAME_TBD, 3),
4998		/* struct pprint_mapv */		/* [16] */
4999		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 11), 40),
5000		BTF_MEMBER_ENC(NAME_TBD, 11, BTF_MEMBER_OFFSET(0, 0)),	/* uint32_t ui32 */
5001		BTF_MEMBER_ENC(NAME_TBD, 10, BTF_MEMBER_OFFSET(0, 32)),	/* uint16_t ui16 */
5002		BTF_MEMBER_ENC(NAME_TBD, 12, BTF_MEMBER_OFFSET(0, 64)),	/* int32_t si32 */
5003		BTF_MEMBER_ENC(NAME_TBD, 6, BTF_MEMBER_OFFSET(2, 96)),	/* unused_bits2a */
5004		BTF_MEMBER_ENC(NAME_TBD, 7, BTF_MEMBER_OFFSET(28, 98)),	/* bits28 */
5005		BTF_MEMBER_ENC(NAME_TBD, 6, BTF_MEMBER_OFFSET(2, 126)),	/* unused_bits2b */
5006		BTF_MEMBER_ENC(0, 14, BTF_MEMBER_OFFSET(0, 128)),	/* union (anon) */
5007		BTF_MEMBER_ENC(NAME_TBD, 15, BTF_MEMBER_OFFSET(0, 192)),	/* aenum */
5008		BTF_MEMBER_ENC(NAME_TBD, 11, BTF_MEMBER_OFFSET(0, 224)),	/* uint32_t ui32b */
5009		BTF_MEMBER_ENC(NAME_TBD, 6, BTF_MEMBER_OFFSET(2, 256)),	/* bits2c */
5010		BTF_MEMBER_ENC(NAME_TBD, 17, 264),	/* si8_4 */
5011		BTF_TYPE_ARRAY_ENC(18, 1, 2),		/* [17] */
5012		BTF_TYPE_ARRAY_ENC(1, 1, 2),		/* [18] */
5013		BTF_END_RAW,
5014	},
5015	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"),
5016	.key_size = sizeof(unsigned int),
5017	.value_size = sizeof(struct pprint_mapv),
5018	.key_type_id = 3,	/* unsigned int */
5019	.value_type_id = 16,	/* struct pprint_mapv */
5020	.max_entries = 128,
5021},
5022
5023{
5024	/* this type will have the same layout as the
5025	 * first .raw_types definition. The struct type will
5026	 * be encoded with kind_flag set, bitfield members
5027	 * are added typedef/const/volatile, and bitfield members
5028	 * will have both int and enum types.
5029	 */
5030	.raw_types = {
5031		/* unsighed char */			/* [1] */
5032		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 8, 1),
5033		/* unsigned short */			/* [2] */
5034		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 16, 2),
5035		/* unsigned int */			/* [3] */
5036		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),
5037		/* int */				/* [4] */
5038		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
5039		/* unsigned long long */		/* [5] */
5040		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 64, 8),
5041		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),	/* [6] */
5042		BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),	/* [7] */
5043		/* uint8_t[8] */			/* [8] */
5044		BTF_TYPE_ARRAY_ENC(9, 1, 8),
5045		/* typedef unsigned char uint8_t */	/* [9] */
5046		BTF_TYPEDEF_ENC(NAME_TBD, 1),
5047		/* typedef unsigned short uint16_t */	/* [10] */
5048		BTF_TYPEDEF_ENC(NAME_TBD, 2),
5049		/* typedef unsigned int uint32_t */	/* [11] */
5050		BTF_TYPEDEF_ENC(NAME_TBD, 3),
5051		/* typedef int int32_t */		/* [12] */
5052		BTF_TYPEDEF_ENC(NAME_TBD, 4),
5053		/* typedef unsigned long long uint64_t *//* [13] */
5054		BTF_TYPEDEF_ENC(NAME_TBD, 5),
5055		/* union (anon) */			/* [14] */
5056		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 0, 2), 8),
5057		BTF_MEMBER_ENC(NAME_TBD, 13, 0),/* uint64_t ui64; */
5058		BTF_MEMBER_ENC(NAME_TBD, 8, 0),	/* uint8_t ui8a[8]; */
5059		/* enum (anon) */			/* [15] */
5060		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 4), 4),
5061		BTF_ENUM_ENC(NAME_TBD, 0),
5062		BTF_ENUM_ENC(NAME_TBD, 1),
5063		BTF_ENUM_ENC(NAME_TBD, 2),
5064		BTF_ENUM_ENC(NAME_TBD, 3),
5065		/* struct pprint_mapv */		/* [16] */
5066		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 11), 40),
5067		BTF_MEMBER_ENC(NAME_TBD, 11, BTF_MEMBER_OFFSET(0, 0)),	/* uint32_t ui32 */
5068		BTF_MEMBER_ENC(NAME_TBD, 10, BTF_MEMBER_OFFSET(0, 32)),	/* uint16_t ui16 */
5069		BTF_MEMBER_ENC(NAME_TBD, 12, BTF_MEMBER_OFFSET(0, 64)),	/* int32_t si32 */
5070		BTF_MEMBER_ENC(NAME_TBD, 17, BTF_MEMBER_OFFSET(2, 96)),	/* unused_bits2a */
5071		BTF_MEMBER_ENC(NAME_TBD, 7, BTF_MEMBER_OFFSET(28, 98)),	/* bits28 */
5072		BTF_MEMBER_ENC(NAME_TBD, 19, BTF_MEMBER_OFFSET(2, 126)),/* unused_bits2b */
5073		BTF_MEMBER_ENC(0, 14, BTF_MEMBER_OFFSET(0, 128)),	/* union (anon) */
5074		BTF_MEMBER_ENC(NAME_TBD, 15, BTF_MEMBER_OFFSET(0, 192)),	/* aenum */
5075		BTF_MEMBER_ENC(NAME_TBD, 11, BTF_MEMBER_OFFSET(0, 224)),	/* uint32_t ui32b */
5076		BTF_MEMBER_ENC(NAME_TBD, 17, BTF_MEMBER_OFFSET(2, 256)),	/* bits2c */
5077		BTF_MEMBER_ENC(NAME_TBD, 20, BTF_MEMBER_OFFSET(0, 264)),	/* si8_4 */
5078		/* typedef unsigned int ___int */	/* [17] */
5079		BTF_TYPEDEF_ENC(NAME_TBD, 18),
5080		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_VOLATILE, 0, 0), 6),	/* [18] */
5081		BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 15),	/* [19] */
5082		BTF_TYPE_ARRAY_ENC(21, 1, 2),					/* [20] */
5083		BTF_TYPE_ARRAY_ENC(1, 1, 2),					/* [21] */
5084		BTF_END_RAW,
5085	},
5086	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"),
5087	.key_size = sizeof(unsigned int),
5088	.value_size = sizeof(struct pprint_mapv),
5089	.key_type_id = 3,	/* unsigned int */
5090	.value_type_id = 16,	/* struct pprint_mapv */
5091	.max_entries = 128,
5092},
5093
5094#ifdef __SIZEOF_INT128__
5095{
5096	/* test int128 */
5097	.raw_types = {
5098		/* unsigned int */				/* [1] */
5099		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),
5100		/* __int128 */					/* [2] */
5101		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 128, 16),
5102		/* unsigned __int128 */				/* [3] */
5103		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 128, 16),
5104		/* struct pprint_mapv_int128 */			/* [4] */
5105		BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 5), 64),
5106		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 0)),		/* si128a */
5107		BTF_MEMBER_ENC(NAME_TBD, 2, BTF_MEMBER_OFFSET(0, 128)),		/* si128b */
5108		BTF_MEMBER_ENC(NAME_TBD, 3, BTF_MEMBER_OFFSET(3, 256)),		/* bits3 */
5109		BTF_MEMBER_ENC(NAME_TBD, 3, BTF_MEMBER_OFFSET(80, 259)),	/* bits80 */
5110		BTF_MEMBER_ENC(NAME_TBD, 3, BTF_MEMBER_OFFSET(0, 384)),		/* ui128 */
5111		BTF_END_RAW,
5112	},
5113	BTF_STR_SEC("\0unsigned int\0__int128\0unsigned __int128\0pprint_mapv_int128\0si128a\0si128b\0bits3\0bits80\0ui128"),
5114	.key_size = sizeof(unsigned int),
5115	.value_size = sizeof(struct pprint_mapv_int128),
5116	.key_type_id = 1,
5117	.value_type_id = 4,
5118	.max_entries = 128,
5119	.mapv_kind = PPRINT_MAPV_KIND_INT128,
5120},
5121#endif
5122
5123};
5124
5125static struct btf_pprint_test_meta {
5126	const char *descr;
5127	enum bpf_map_type map_type;
5128	const char *map_name;
5129	bool ordered_map;
5130	bool lossless_map;
5131	bool percpu_map;
5132} pprint_tests_meta[] = {
5133{
5134	.descr = "BTF pretty print array",
5135	.map_type = BPF_MAP_TYPE_ARRAY,
5136	.map_name = "pprint_test_array",
5137	.ordered_map = true,
5138	.lossless_map = true,
5139	.percpu_map = false,
5140},
5141
5142{
5143	.descr = "BTF pretty print hash",
5144	.map_type = BPF_MAP_TYPE_HASH,
5145	.map_name = "pprint_test_hash",
5146	.ordered_map = false,
5147	.lossless_map = true,
5148	.percpu_map = false,
5149},
5150
5151{
5152	.descr = "BTF pretty print lru hash",
5153	.map_type = BPF_MAP_TYPE_LRU_HASH,
5154	.map_name = "pprint_test_lru_hash",
5155	.ordered_map = false,
5156	.lossless_map = false,
5157	.percpu_map = false,
5158},
5159
5160{
5161	.descr = "BTF pretty print percpu array",
5162	.map_type = BPF_MAP_TYPE_PERCPU_ARRAY,
5163	.map_name = "pprint_test_percpu_array",
5164	.ordered_map = true,
5165	.lossless_map = true,
5166	.percpu_map = true,
5167},
5168
5169{
5170	.descr = "BTF pretty print percpu hash",
5171	.map_type = BPF_MAP_TYPE_PERCPU_HASH,
5172	.map_name = "pprint_test_percpu_hash",
5173	.ordered_map = false,
5174	.lossless_map = true,
5175	.percpu_map = true,
5176},
5177
5178{
5179	.descr = "BTF pretty print lru percpu hash",
5180	.map_type = BPF_MAP_TYPE_LRU_PERCPU_HASH,
5181	.map_name = "pprint_test_lru_percpu_hash",
5182	.ordered_map = false,
5183	.lossless_map = false,
5184	.percpu_map = true,
5185},
5186
5187};
5188
5189static size_t get_pprint_mapv_size(enum pprint_mapv_kind_t mapv_kind)
5190{
5191	if (mapv_kind == PPRINT_MAPV_KIND_BASIC)
5192		return sizeof(struct pprint_mapv);
5193
5194#ifdef __SIZEOF_INT128__
5195	if (mapv_kind == PPRINT_MAPV_KIND_INT128)
5196		return sizeof(struct pprint_mapv_int128);
5197#endif
5198
5199	assert(0);
 
5200}
5201
5202static void set_pprint_mapv(enum pprint_mapv_kind_t mapv_kind,
5203			    void *mapv, uint32_t i,
5204			    int num_cpus, int rounded_value_size)
5205{
5206	int cpu;
5207
5208	if (mapv_kind == PPRINT_MAPV_KIND_BASIC) {
5209		struct pprint_mapv *v = mapv;
5210
5211		for (cpu = 0; cpu < num_cpus; cpu++) {
5212			v->ui32 = i + cpu;
5213			v->si32 = -i;
5214			v->unused_bits2a = 3;
5215			v->bits28 = i;
5216			v->unused_bits2b = 3;
5217			v->ui64 = i;
5218			v->aenum = i & 0x03;
5219			v->ui32b = 4;
5220			v->bits2c = 1;
5221			v->si8_4[0][0] = (cpu + i) & 0xff;
5222			v->si8_4[0][1] = (cpu + i + 1) & 0xff;
5223			v->si8_4[1][0] = (cpu + i + 2) & 0xff;
5224			v->si8_4[1][1] = (cpu + i + 3) & 0xff;
5225			v = (void *)v + rounded_value_size;
5226		}
5227	}
5228
5229#ifdef __SIZEOF_INT128__
5230	if (mapv_kind == PPRINT_MAPV_KIND_INT128) {
5231		struct pprint_mapv_int128 *v = mapv;
5232
5233		for (cpu = 0; cpu < num_cpus; cpu++) {
5234			v->si128a = i;
5235			v->si128b = -i;
5236			v->bits3 = i & 0x07;
5237			v->bits80 = (((unsigned __int128)1) << 64) + i;
5238			v->ui128 = (((unsigned __int128)2) << 64) + i;
5239			v = (void *)v + rounded_value_size;
5240		}
5241	}
5242#endif
5243}
5244
5245ssize_t get_pprint_expected_line(enum pprint_mapv_kind_t mapv_kind,
5246				 char *expected_line, ssize_t line_size,
5247				 bool percpu_map, unsigned int next_key,
5248				 int cpu, void *mapv)
5249{
5250	ssize_t nexpected_line = -1;
5251
5252	if (mapv_kind == PPRINT_MAPV_KIND_BASIC) {
5253		struct pprint_mapv *v = mapv;
5254
5255		nexpected_line = snprintf(expected_line, line_size,
5256					  "%s%u: {%u,0,%d,0x%x,0x%x,0x%x,"
5257					  "{%llu|[%u,%u,%u,%u,%u,%u,%u,%u]},%s,"
5258					  "%u,0x%x,[[%d,%d],[%d,%d]]}\n",
5259					  percpu_map ? "\tcpu" : "",
5260					  percpu_map ? cpu : next_key,
5261					  v->ui32, v->si32,
5262					  v->unused_bits2a,
5263					  v->bits28,
5264					  v->unused_bits2b,
5265					  (__u64)v->ui64,
5266					  v->ui8a[0], v->ui8a[1],
5267					  v->ui8a[2], v->ui8a[3],
5268					  v->ui8a[4], v->ui8a[5],
5269					  v->ui8a[6], v->ui8a[7],
5270					  pprint_enum_str[v->aenum],
5271					  v->ui32b,
5272					  v->bits2c,
5273					  v->si8_4[0][0], v->si8_4[0][1],
5274					  v->si8_4[1][0], v->si8_4[1][1]);
5275	}
5276
5277#ifdef __SIZEOF_INT128__
5278	if (mapv_kind == PPRINT_MAPV_KIND_INT128) {
5279		struct pprint_mapv_int128 *v = mapv;
5280
5281		nexpected_line = snprintf(expected_line, line_size,
5282					  "%s%u: {0x%lx,0x%lx,0x%lx,"
5283					  "0x%lx%016lx,0x%lx%016lx}\n",
5284					  percpu_map ? "\tcpu" : "",
5285					  percpu_map ? cpu : next_key,
5286					  (uint64_t)v->si128a,
5287					  (uint64_t)v->si128b,
5288					  (uint64_t)v->bits3,
5289					  (uint64_t)(v->bits80 >> 64),
5290					  (uint64_t)v->bits80,
5291					  (uint64_t)(v->ui128 >> 64),
5292					  (uint64_t)v->ui128);
5293	}
5294#endif
5295
5296	return nexpected_line;
5297}
5298
5299static int check_line(const char *expected_line, int nexpected_line,
5300		      int expected_line_len, const char *line)
5301{
5302	if (CHECK(nexpected_line == expected_line_len,
5303		  "expected_line is too long"))
5304		return -1;
5305
5306	if (strcmp(expected_line, line)) {
5307		fprintf(stderr, "unexpected pprint output\n");
5308		fprintf(stderr, "expected: %s", expected_line);
5309		fprintf(stderr, "    read: %s", line);
5310		return -1;
5311	}
5312
5313	return 0;
5314}
5315
5316
5317static void do_test_pprint(int test_num)
5318{
5319	const struct btf_raw_test *test = &pprint_test_template[test_num];
5320	enum pprint_mapv_kind_t mapv_kind = test->mapv_kind;
5321	LIBBPF_OPTS(bpf_map_create_opts, opts);
5322	bool ordered_map, lossless_map, percpu_map;
5323	int err, ret, num_cpus, rounded_value_size;
5324	unsigned int key, nr_read_elems;
5325	int map_fd = -1, btf_fd = -1;
5326	unsigned int raw_btf_size;
5327	char expected_line[255];
5328	FILE *pin_file = NULL;
5329	char pin_path[255];
5330	size_t line_len = 0;
5331	char *line = NULL;
5332	void *mapv = NULL;
5333	uint8_t *raw_btf;
5334	ssize_t nread;
5335
5336	if (!test__start_subtest(test->descr))
5337		return;
5338
5339	raw_btf = btf_raw_create(&hdr_tmpl, test->raw_types,
5340				 test->str_sec, test->str_sec_size,
5341				 &raw_btf_size, NULL);
5342
5343	if (!raw_btf)
5344		return;
5345
5346	*btf_log_buf = '\0';
5347	btf_fd = load_raw_btf(raw_btf, raw_btf_size);
5348	free(raw_btf);
5349
5350	if (CHECK(btf_fd < 0, "errno:%d\n", errno)) {
5351		err = -1;
5352		goto done;
5353	}
5354
5355	opts.btf_fd = btf_fd;
5356	opts.btf_key_type_id = test->key_type_id;
5357	opts.btf_value_type_id = test->value_type_id;
5358	map_fd = bpf_map_create(test->map_type, test->map_name,
5359				test->key_size, test->value_size, test->max_entries, &opts);
5360	if (CHECK(map_fd < 0, "errno:%d", errno)) {
5361		err = -1;
5362		goto done;
5363	}
5364
5365	ret = snprintf(pin_path, sizeof(pin_path), "%s/%s",
5366		       "/sys/fs/bpf", test->map_name);
5367
5368	if (CHECK(ret >= sizeof(pin_path), "pin_path %s/%s is too long",
5369		  "/sys/fs/bpf", test->map_name)) {
5370		err = -1;
5371		goto done;
5372	}
5373
5374	err = bpf_obj_pin(map_fd, pin_path);
5375	if (CHECK(err, "bpf_obj_pin(%s): errno:%d.", pin_path, errno))
5376		goto done;
5377
5378	percpu_map = test->percpu_map;
5379	num_cpus = percpu_map ? bpf_num_possible_cpus() : 1;
5380	rounded_value_size = round_up(get_pprint_mapv_size(mapv_kind), 8);
5381	mapv = calloc(num_cpus, rounded_value_size);
5382	if (CHECK(!mapv, "mapv allocation failure")) {
5383		err = -1;
5384		goto done;
5385	}
5386
5387	for (key = 0; key < test->max_entries; key++) {
5388		set_pprint_mapv(mapv_kind, mapv, key, num_cpus, rounded_value_size);
5389		bpf_map_update_elem(map_fd, &key, mapv, 0);
5390	}
5391
5392	pin_file = fopen(pin_path, "r");
5393	if (CHECK(!pin_file, "fopen(%s): errno:%d", pin_path, errno)) {
5394		err = -1;
5395		goto done;
5396	}
5397
5398	/* Skip lines start with '#' */
5399	while ((nread = getline(&line, &line_len, pin_file)) > 0 &&
5400	       *line == '#')
5401		;
5402
5403	if (CHECK(nread <= 0, "Unexpected EOF")) {
5404		err = -1;
5405		goto done;
5406	}
5407
5408	nr_read_elems = 0;
5409	ordered_map = test->ordered_map;
5410	lossless_map = test->lossless_map;
5411	do {
5412		ssize_t nexpected_line;
5413		unsigned int next_key;
5414		void *cmapv;
5415		int cpu;
5416
5417		next_key = ordered_map ? nr_read_elems : atoi(line);
5418		set_pprint_mapv(mapv_kind, mapv, next_key, num_cpus, rounded_value_size);
5419		cmapv = mapv;
5420
5421		for (cpu = 0; cpu < num_cpus; cpu++) {
5422			if (percpu_map) {
5423				/* for percpu map, the format looks like:
5424				 * <key>: {
5425				 *	cpu0: <value_on_cpu0>
5426				 *	cpu1: <value_on_cpu1>
5427				 *	...
5428				 *	cpun: <value_on_cpun>
5429				 * }
5430				 *
5431				 * let us verify the line containing the key here.
5432				 */
5433				if (cpu == 0) {
5434					nexpected_line = snprintf(expected_line,
5435								  sizeof(expected_line),
5436								  "%u: {\n",
5437								  next_key);
5438
5439					err = check_line(expected_line, nexpected_line,
5440							 sizeof(expected_line), line);
5441					if (err < 0)
5442						goto done;
5443				}
5444
5445				/* read value@cpu */
5446				nread = getline(&line, &line_len, pin_file);
5447				if (nread < 0)
5448					break;
5449			}
5450
5451			nexpected_line = get_pprint_expected_line(mapv_kind, expected_line,
5452								  sizeof(expected_line),
5453								  percpu_map, next_key,
5454								  cpu, cmapv);
5455			err = check_line(expected_line, nexpected_line,
5456					 sizeof(expected_line), line);
5457			if (err < 0)
5458				goto done;
5459
5460			cmapv = cmapv + rounded_value_size;
5461		}
5462
5463		if (percpu_map) {
5464			/* skip the last bracket for the percpu map */
5465			nread = getline(&line, &line_len, pin_file);
5466			if (nread < 0)
5467				break;
5468		}
5469
5470		nread = getline(&line, &line_len, pin_file);
5471	} while (++nr_read_elems < test->max_entries && nread > 0);
5472
5473	if (lossless_map &&
5474	    CHECK(nr_read_elems < test->max_entries,
5475		  "Unexpected EOF. nr_read_elems:%u test->max_entries:%u",
5476		  nr_read_elems, test->max_entries)) {
5477		err = -1;
5478		goto done;
5479	}
5480
5481	if (CHECK(nread > 0, "Unexpected extra pprint output: %s", line)) {
5482		err = -1;
5483		goto done;
5484	}
5485
5486	err = 0;
5487
5488done:
5489	if (mapv)
5490		free(mapv);
5491	if (!err)
5492		fprintf(stderr, "OK");
5493	if (*btf_log_buf && (err || always_log))
5494		fprintf(stderr, "\n%s", btf_log_buf);
5495	if (btf_fd >= 0)
5496		close(btf_fd);
5497	if (map_fd >= 0)
5498		close(map_fd);
5499	if (pin_file)
5500		fclose(pin_file);
5501	unlink(pin_path);
5502	free(line);
5503}
5504
5505static void test_pprint(void)
5506{
5507	unsigned int i;
5508
5509	/* test various maps with the first test template */
5510	for (i = 0; i < ARRAY_SIZE(pprint_tests_meta); i++) {
5511		pprint_test_template[0].descr = pprint_tests_meta[i].descr;
5512		pprint_test_template[0].map_type = pprint_tests_meta[i].map_type;
5513		pprint_test_template[0].map_name = pprint_tests_meta[i].map_name;
5514		pprint_test_template[0].ordered_map = pprint_tests_meta[i].ordered_map;
5515		pprint_test_template[0].lossless_map = pprint_tests_meta[i].lossless_map;
5516		pprint_test_template[0].percpu_map = pprint_tests_meta[i].percpu_map;
5517
5518		do_test_pprint(0);
5519	}
5520
5521	/* test rest test templates with the first map */
5522	for (i = 1; i < ARRAY_SIZE(pprint_test_template); i++) {
5523		pprint_test_template[i].descr = pprint_tests_meta[0].descr;
5524		pprint_test_template[i].map_type = pprint_tests_meta[0].map_type;
5525		pprint_test_template[i].map_name = pprint_tests_meta[0].map_name;
5526		pprint_test_template[i].ordered_map = pprint_tests_meta[0].ordered_map;
5527		pprint_test_template[i].lossless_map = pprint_tests_meta[0].lossless_map;
5528		pprint_test_template[i].percpu_map = pprint_tests_meta[0].percpu_map;
5529		do_test_pprint(i);
5530	}
5531}
5532
5533#define BPF_LINE_INFO_ENC(insn_off, file_off, line_off, line_num, line_col) \
5534	(insn_off), (file_off), (line_off), ((line_num) << 10 | ((line_col) & 0x3ff))
5535
5536static struct prog_info_raw_test {
5537	const char *descr;
5538	const char *str_sec;
5539	const char *err_str;
5540	__u32 raw_types[MAX_NR_RAW_U32];
5541	__u32 str_sec_size;
5542	struct bpf_insn insns[MAX_INSNS];
5543	__u32 prog_type;
5544	__u32 func_info[MAX_SUBPROGS][2];
5545	__u32 func_info_rec_size;
5546	__u32 func_info_cnt;
5547	__u32 line_info[MAX_NR_RAW_U32];
5548	__u32 line_info_rec_size;
5549	__u32 nr_jited_ksyms;
5550	bool expected_prog_load_failure;
5551	__u32 dead_code_cnt;
5552	__u32 dead_code_mask;
5553	__u32 dead_func_cnt;
5554	__u32 dead_func_mask;
5555} info_raw_tests[] = {
5556{
5557	.descr = "func_type (main func + one sub)",
5558	.raw_types = {
5559		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5560		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),	/* [2] */
5561		BTF_FUNC_PROTO_ENC(1, 2),			/* [3] */
5562			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5563			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5564		BTF_FUNC_PROTO_ENC(1, 2),			/* [4] */
5565			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5566			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5567		BTF_FUNC_ENC(NAME_TBD, 3),			/* [5] */
5568		BTF_FUNC_ENC(NAME_TBD, 4),			/* [6] */
5569		BTF_END_RAW,
5570	},
5571	.str_sec = "\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB",
5572	.str_sec_size = sizeof("\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB"),
5573	.insns = {
5574		BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
5575		BPF_MOV64_IMM(BPF_REG_0, 1),
5576		BPF_EXIT_INSN(),
5577		BPF_MOV64_IMM(BPF_REG_0, 2),
5578		BPF_EXIT_INSN(),
5579	},
5580	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5581	.func_info = { {0, 5}, {3, 6} },
5582	.func_info_rec_size = 8,
5583	.func_info_cnt = 2,
5584	.line_info = { BTF_END_RAW },
5585},
5586
5587{
5588	.descr = "func_type (Incorrect func_info_rec_size)",
5589	.raw_types = {
5590		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5591		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),	/* [2] */
5592		BTF_FUNC_PROTO_ENC(1, 2),			/* [3] */
5593			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5594			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5595		BTF_FUNC_PROTO_ENC(1, 2),			/* [4] */
5596			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5597			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5598		BTF_FUNC_ENC(NAME_TBD, 3),			/* [5] */
5599		BTF_FUNC_ENC(NAME_TBD, 4),			/* [6] */
5600		BTF_END_RAW,
5601	},
5602	.str_sec = "\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB",
5603	.str_sec_size = sizeof("\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB"),
5604	.insns = {
5605		BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
5606		BPF_MOV64_IMM(BPF_REG_0, 1),
5607		BPF_EXIT_INSN(),
5608		BPF_MOV64_IMM(BPF_REG_0, 2),
5609		BPF_EXIT_INSN(),
5610	},
5611	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5612	.func_info = { {0, 5}, {3, 6} },
5613	.func_info_rec_size = 4,
5614	.func_info_cnt = 2,
5615	.line_info = { BTF_END_RAW },
5616	.expected_prog_load_failure = true,
5617},
5618
5619{
5620	.descr = "func_type (Incorrect func_info_cnt)",
5621	.raw_types = {
5622		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5623		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),	/* [2] */
5624		BTF_FUNC_PROTO_ENC(1, 2),			/* [3] */
5625			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5626			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5627		BTF_FUNC_PROTO_ENC(1, 2),			/* [4] */
5628			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5629			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5630		BTF_FUNC_ENC(NAME_TBD, 3),			/* [5] */
5631		BTF_FUNC_ENC(NAME_TBD, 4),			/* [6] */
5632		BTF_END_RAW,
5633	},
5634	.str_sec = "\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB",
5635	.str_sec_size = sizeof("\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB"),
5636	.insns = {
5637		BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
5638		BPF_MOV64_IMM(BPF_REG_0, 1),
5639		BPF_EXIT_INSN(),
5640		BPF_MOV64_IMM(BPF_REG_0, 2),
5641		BPF_EXIT_INSN(),
5642	},
5643	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5644	.func_info = { {0, 5}, {3, 6} },
5645	.func_info_rec_size = 8,
5646	.func_info_cnt = 1,
5647	.line_info = { BTF_END_RAW },
5648	.expected_prog_load_failure = true,
5649},
5650
5651{
5652	.descr = "func_type (Incorrect bpf_func_info.insn_off)",
5653	.raw_types = {
5654		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5655		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),	/* [2] */
5656		BTF_FUNC_PROTO_ENC(1, 2),			/* [3] */
5657			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5658			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5659		BTF_FUNC_PROTO_ENC(1, 2),			/* [4] */
5660			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 2),
5661			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5662		BTF_FUNC_ENC(NAME_TBD, 3),			/* [5] */
5663		BTF_FUNC_ENC(NAME_TBD, 4),			/* [6] */
5664		BTF_END_RAW,
5665	},
5666	.str_sec = "\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB",
5667	.str_sec_size = sizeof("\0int\0unsigned int\0a\0b\0c\0d\0funcA\0funcB"),
5668	.insns = {
5669		BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 1, 0, 2),
5670		BPF_MOV64_IMM(BPF_REG_0, 1),
5671		BPF_EXIT_INSN(),
5672		BPF_MOV64_IMM(BPF_REG_0, 2),
5673		BPF_EXIT_INSN(),
5674	},
5675	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5676	.func_info = { {0, 5}, {2, 6} },
5677	.func_info_rec_size = 8,
5678	.func_info_cnt = 2,
5679	.line_info = { BTF_END_RAW },
5680	.expected_prog_load_failure = true,
5681},
5682
5683{
5684	.descr = "line_info (No subprog)",
5685	.raw_types = {
5686		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5687		BTF_END_RAW,
5688	},
5689	BTF_STR_SEC("\0int\0int a=1;\0int b=2;\0return a + b;\0return a + b;"),
5690	.insns = {
5691		BPF_MOV64_IMM(BPF_REG_0, 1),
5692		BPF_MOV64_IMM(BPF_REG_1, 2),
5693		BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
5694		BPF_EXIT_INSN(),
5695	},
5696	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5697	.func_info_cnt = 0,
5698	.line_info = {
5699		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5700		BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 9),
5701		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8),
5702		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 7),
5703		BTF_END_RAW,
5704	},
5705	.line_info_rec_size = sizeof(struct bpf_line_info),
5706	.nr_jited_ksyms = 1,
5707},
5708
5709{
5710	.descr = "line_info (No subprog. insn_off >= prog->len)",
5711	.raw_types = {
5712		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5713		BTF_END_RAW,
5714	},
5715	BTF_STR_SEC("\0int\0int a=1;\0int b=2;\0return a + b;\0return a + b;"),
5716	.insns = {
5717		BPF_MOV64_IMM(BPF_REG_0, 1),
5718		BPF_MOV64_IMM(BPF_REG_1, 2),
5719		BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
5720		BPF_EXIT_INSN(),
5721	},
5722	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5723	.func_info_cnt = 0,
5724	.line_info = {
5725		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5726		BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 9),
5727		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8),
5728		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 7),
5729		BPF_LINE_INFO_ENC(4, 0, 0, 5, 6),
5730		BTF_END_RAW,
5731	},
5732	.line_info_rec_size = sizeof(struct bpf_line_info),
5733	.nr_jited_ksyms = 1,
5734	.err_str = "line_info[4].insn_off",
5735	.expected_prog_load_failure = true,
5736},
5737
5738{
5739	.descr = "line_info (Zero bpf insn code)",
5740	.raw_types = {
5741		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5742		BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 64, 8),	/* [2] */
5743		BTF_TYPEDEF_ENC(NAME_TBD, 2),			/* [3] */
5744		BTF_END_RAW,
5745	},
5746	BTF_STR_SEC("\0int\0unsigned long\0u64\0u64 a=1;\0return a;"),
5747	.insns = {
5748		BPF_LD_IMM64(BPF_REG_0, 1),
5749		BPF_EXIT_INSN(),
5750	},
5751	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5752	.func_info_cnt = 0,
5753	.line_info = {
5754		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5755		BPF_LINE_INFO_ENC(1, 0, 0, 2, 9),
5756		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8),
5757		BTF_END_RAW,
5758	},
5759	.line_info_rec_size = sizeof(struct bpf_line_info),
5760	.nr_jited_ksyms = 1,
5761	.err_str = "Invalid insn code at line_info[1]",
5762	.expected_prog_load_failure = true,
5763},
5764
5765{
5766	.descr = "line_info (No subprog. zero tailing line_info",
5767	.raw_types = {
5768		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5769		BTF_END_RAW,
5770	},
5771	BTF_STR_SEC("\0int\0int a=1;\0int b=2;\0return a + b;\0return a + b;"),
5772	.insns = {
5773		BPF_MOV64_IMM(BPF_REG_0, 1),
5774		BPF_MOV64_IMM(BPF_REG_1, 2),
5775		BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
5776		BPF_EXIT_INSN(),
5777	},
5778	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5779	.func_info_cnt = 0,
5780	.line_info = {
5781		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10), 0,
5782		BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 9), 0,
5783		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8), 0,
5784		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 7), 0,
5785		BTF_END_RAW,
5786	},
5787	.line_info_rec_size = sizeof(struct bpf_line_info) + sizeof(__u32),
5788	.nr_jited_ksyms = 1,
5789},
5790
5791{
5792	.descr = "line_info (No subprog. nonzero tailing line_info)",
5793	.raw_types = {
5794		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5795		BTF_END_RAW,
5796	},
5797	BTF_STR_SEC("\0int\0int a=1;\0int b=2;\0return a + b;\0return a + b;"),
5798	.insns = {
5799		BPF_MOV64_IMM(BPF_REG_0, 1),
5800		BPF_MOV64_IMM(BPF_REG_1, 2),
5801		BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
5802		BPF_EXIT_INSN(),
5803	},
5804	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5805	.func_info_cnt = 0,
5806	.line_info = {
5807		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10), 0,
5808		BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 9), 0,
5809		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8), 0,
5810		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 7), 1,
5811		BTF_END_RAW,
5812	},
5813	.line_info_rec_size = sizeof(struct bpf_line_info) + sizeof(__u32),
5814	.nr_jited_ksyms = 1,
5815	.err_str = "nonzero tailing record in line_info",
5816	.expected_prog_load_failure = true,
5817},
5818
5819{
5820	.descr = "line_info (subprog)",
5821	.raw_types = {
5822		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5823		BTF_END_RAW,
5824	},
5825	BTF_STR_SEC("\0int\0int a=1+1;\0return func(a);\0b+=1;\0return b;"),
5826	.insns = {
5827		BPF_MOV64_IMM(BPF_REG_2, 1),
5828		BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
5829		BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
5830		BPF_CALL_REL(1),
5831		BPF_EXIT_INSN(),
5832		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
5833		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
5834		BPF_EXIT_INSN(),
5835	},
5836	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5837	.func_info_cnt = 0,
5838	.line_info = {
5839		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5840		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 2, 9),
5841		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 3, 8),
5842		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 4, 7),
5843		BTF_END_RAW,
5844	},
5845	.line_info_rec_size = sizeof(struct bpf_line_info),
5846	.nr_jited_ksyms = 2,
5847},
5848
5849{
5850	.descr = "line_info (subprog + func_info)",
5851	.raw_types = {
5852		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5853		BTF_FUNC_PROTO_ENC(1, 1),			/* [2] */
5854			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
5855		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
5856		BTF_FUNC_ENC(NAME_TBD, 2),			/* [4] */
5857		BTF_END_RAW,
5858	},
5859	BTF_STR_SEC("\0int\0x\0sub\0main\0int a=1+1;\0return func(a);\0b+=1;\0return b;"),
5860	.insns = {
5861		BPF_MOV64_IMM(BPF_REG_2, 1),
5862		BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
5863		BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
5864		BPF_CALL_REL(1),
5865		BPF_EXIT_INSN(),
5866		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
5867		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
5868		BPF_EXIT_INSN(),
5869	},
5870	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5871	.func_info_cnt = 2,
5872	.func_info_rec_size = 8,
5873	.func_info = { {0, 4}, {5, 3} },
5874	.line_info = {
5875		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5876		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 2, 9),
5877		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 3, 8),
5878		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 4, 7),
5879		BTF_END_RAW,
5880	},
5881	.line_info_rec_size = sizeof(struct bpf_line_info),
5882	.nr_jited_ksyms = 2,
5883},
5884
5885{
5886	.descr = "line_info (subprog. missing 1st func line info)",
5887	.raw_types = {
5888		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5889		BTF_END_RAW,
5890	},
5891	BTF_STR_SEC("\0int\0int a=1+1;\0return func(a);\0b+=1;\0return b;"),
5892	.insns = {
5893		BPF_MOV64_IMM(BPF_REG_2, 1),
5894		BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
5895		BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
5896		BPF_CALL_REL(1),
5897		BPF_EXIT_INSN(),
5898		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
5899		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
5900		BPF_EXIT_INSN(),
5901	},
5902	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5903	.func_info_cnt = 0,
5904	.line_info = {
5905		BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 1, 10),
5906		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 2, 9),
5907		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 3, 8),
5908		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 4, 7),
5909		BTF_END_RAW,
5910	},
5911	.line_info_rec_size = sizeof(struct bpf_line_info),
5912	.nr_jited_ksyms = 2,
5913	.err_str = "missing bpf_line_info for func#0",
5914	.expected_prog_load_failure = true,
5915},
5916
5917{
5918	.descr = "line_info (subprog. missing 2nd func line info)",
5919	.raw_types = {
5920		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5921		BTF_END_RAW,
5922	},
5923	BTF_STR_SEC("\0int\0int a=1+1;\0return func(a);\0b+=1;\0return b;"),
5924	.insns = {
5925		BPF_MOV64_IMM(BPF_REG_2, 1),
5926		BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
5927		BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
5928		BPF_CALL_REL(1),
5929		BPF_EXIT_INSN(),
5930		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
5931		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
5932		BPF_EXIT_INSN(),
5933	},
5934	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5935	.func_info_cnt = 0,
5936	.line_info = {
5937		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5938		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 2, 9),
5939		BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 3, 8),
5940		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 4, 7),
5941		BTF_END_RAW,
5942	},
5943	.line_info_rec_size = sizeof(struct bpf_line_info),
5944	.nr_jited_ksyms = 2,
5945	.err_str = "missing bpf_line_info for func#1",
5946	.expected_prog_load_failure = true,
5947},
5948
5949{
5950	.descr = "line_info (subprog. unordered insn offset)",
5951	.raw_types = {
5952		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5953		BTF_END_RAW,
5954	},
5955	BTF_STR_SEC("\0int\0int a=1+1;\0return func(a);\0b+=1;\0return b;"),
5956	.insns = {
5957		BPF_MOV64_IMM(BPF_REG_2, 1),
5958		BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
5959		BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
5960		BPF_CALL_REL(1),
5961		BPF_EXIT_INSN(),
5962		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
5963		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
5964		BPF_EXIT_INSN(),
5965	},
5966	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5967	.func_info_cnt = 0,
5968	.line_info = {
5969		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5970		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 2, 9),
5971		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8),
5972		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 4, 7),
5973		BTF_END_RAW,
5974	},
5975	.line_info_rec_size = sizeof(struct bpf_line_info),
5976	.nr_jited_ksyms = 2,
5977	.err_str = "Invalid line_info[2].insn_off",
5978	.expected_prog_load_failure = true,
5979},
5980
5981{
5982	.descr = "line_info (dead start)",
5983	.raw_types = {
5984		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
5985		BTF_END_RAW,
5986	},
5987	BTF_STR_SEC("\0int\0/* dead jmp */\0int a=1;\0int b=2;\0return a + b;\0return a + b;"),
5988	.insns = {
5989		BPF_JMP_IMM(BPF_JA, 0, 0, 0),
5990		BPF_MOV64_IMM(BPF_REG_0, 1),
5991		BPF_MOV64_IMM(BPF_REG_1, 2),
5992		BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
5993		BPF_EXIT_INSN(),
5994	},
5995	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
5996	.func_info_cnt = 0,
5997	.line_info = {
5998		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
5999		BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 9),
6000		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 8),
6001		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 7),
6002		BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 5, 6),
6003		BTF_END_RAW,
6004	},
6005	.line_info_rec_size = sizeof(struct bpf_line_info),
6006	.nr_jited_ksyms = 1,
6007	.dead_code_cnt = 1,
6008	.dead_code_mask = 0x01,
6009},
6010
6011{
6012	.descr = "line_info (dead end)",
6013	.raw_types = {
6014		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6015		BTF_END_RAW,
6016	},
6017	BTF_STR_SEC("\0int\0int a=1;\0int b=2;\0return a + b;\0/* dead jmp */\0return a + b;\0/* dead exit */"),
6018	.insns = {
6019		BPF_MOV64_IMM(BPF_REG_0, 1),
6020		BPF_MOV64_IMM(BPF_REG_1, 2),
6021		BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1),
6022		BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 10, 1),
6023		BPF_EXIT_INSN(),
6024		BPF_EXIT_INSN(),
6025	},
6026	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
6027	.func_info_cnt = 0,
6028	.line_info = {
6029		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 12),
6030		BPF_LINE_INFO_ENC(1, 0, NAME_TBD, 2, 11),
6031		BPF_LINE_INFO_ENC(2, 0, NAME_TBD, 3, 10),
6032		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 4, 9),
6033		BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 5, 8),
6034		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 6, 7),
6035		BTF_END_RAW,
6036	},
6037	.line_info_rec_size = sizeof(struct bpf_line_info),
6038	.nr_jited_ksyms = 1,
6039	.dead_code_cnt = 2,
6040	.dead_code_mask = 0x28,
6041},
6042
6043{
6044	.descr = "line_info (dead code + subprog + func_info)",
6045	.raw_types = {
6046		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6047		BTF_FUNC_PROTO_ENC(1, 1),			/* [2] */
6048			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
6049		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
6050		BTF_FUNC_ENC(NAME_TBD, 2),			/* [4] */
6051		BTF_END_RAW,
6052	},
6053	BTF_STR_SEC("\0int\0x\0sub\0main\0int a=1+1;\0/* dead jmp */"
6054		    "\0/* dead */\0/* dead */\0/* dead */\0/* dead */"
6055		    "\0/* dead */\0/* dead */\0/* dead */\0/* dead */"
6056		    "\0return func(a);\0b+=1;\0return b;"),
6057	.insns = {
6058		BPF_MOV64_IMM(BPF_REG_2, 1),
6059		BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, 1),
6060		BPF_MOV64_REG(BPF_REG_1, BPF_REG_2),
6061		BPF_JMP_IMM(BPF_JGE, BPF_REG_2, 0, 8),
6062		BPF_MOV64_IMM(BPF_REG_2, 1),
6063		BPF_MOV64_IMM(BPF_REG_2, 1),
6064		BPF_MOV64_IMM(BPF_REG_2, 1),
6065		BPF_MOV64_IMM(BPF_REG_2, 1),
6066		BPF_MOV64_IMM(BPF_REG_2, 1),
6067		BPF_MOV64_IMM(BPF_REG_2, 1),
6068		BPF_MOV64_IMM(BPF_REG_2, 1),
6069		BPF_MOV64_IMM(BPF_REG_2, 1),
6070		BPF_CALL_REL(1),
6071		BPF_EXIT_INSN(),
6072		BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
6073		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
6074		BPF_EXIT_INSN(),
6075	},
6076	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
6077	.func_info_cnt = 2,
6078	.func_info_rec_size = 8,
6079	.func_info = { {0, 4}, {14, 3} },
6080	.line_info = {
6081		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
6082		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 1, 10),
6083		BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 1, 10),
6084		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 1, 10),
6085		BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
6086		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 1, 10),
6087		BPF_LINE_INFO_ENC(8, 0, NAME_TBD, 1, 10),
6088		BPF_LINE_INFO_ENC(9, 0, NAME_TBD, 1, 10),
6089		BPF_LINE_INFO_ENC(10, 0, NAME_TBD, 1, 10),
6090		BPF_LINE_INFO_ENC(11, 0, NAME_TBD, 2, 9),
6091		BPF_LINE_INFO_ENC(12, 0, NAME_TBD, 2, 9),
6092		BPF_LINE_INFO_ENC(14, 0, NAME_TBD, 3, 8),
6093		BPF_LINE_INFO_ENC(16, 0, NAME_TBD, 4, 7),
6094		BTF_END_RAW,
6095	},
6096	.line_info_rec_size = sizeof(struct bpf_line_info),
6097	.nr_jited_ksyms = 2,
6098	.dead_code_cnt = 9,
6099	.dead_code_mask = 0x3fe,
6100},
6101
6102{
6103	.descr = "line_info (dead subprog)",
6104	.raw_types = {
6105		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6106		BTF_FUNC_PROTO_ENC(1, 1),			/* [2] */
6107			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
6108		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
6109		BTF_FUNC_ENC(NAME_TBD, 2),			/* [4] */
6110		BTF_FUNC_ENC(NAME_TBD, 2),			/* [5] */
6111		BTF_END_RAW,
6112	},
6113	BTF_STR_SEC("\0int\0x\0dead\0main\0func\0int a=1+1;\0/* live call */"
6114		    "\0return 0;\0return 0;\0/* dead */\0/* dead */"
6115		    "\0/* dead */\0return bla + 1;\0return bla + 1;"
6116		    "\0return bla + 1;\0return func(a);\0b+=1;\0return b;"),
6117	.insns = {
6118		BPF_MOV64_IMM(BPF_REG_2, 1),
6119		BPF_JMP_IMM(BPF_JGE, BPF_REG_2, 0, 1),
6120		BPF_CALL_REL(3),
6121		BPF_CALL_REL(5),
6122		BPF_MOV64_IMM(BPF_REG_0, 0),
6123		BPF_EXIT_INSN(),
6124		BPF_MOV64_IMM(BPF_REG_0, 0),
6125		BPF_CALL_REL(1),
6126		BPF_EXIT_INSN(),
6127		BPF_MOV64_REG(BPF_REG_0, 2),
6128		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
6129		BPF_EXIT_INSN(),
6130	},
6131	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
6132	.func_info_cnt = 3,
6133	.func_info_rec_size = 8,
6134		.func_info = { {0, 4}, {6, 3}, {9, 5} },
6135	.line_info = {
6136		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
6137		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 1, 10),
6138		BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 1, 10),
6139		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 1, 10),
6140		BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
6141		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 1, 10),
6142		BPF_LINE_INFO_ENC(8, 0, NAME_TBD, 1, 10),
6143		BPF_LINE_INFO_ENC(9, 0, NAME_TBD, 1, 10),
6144		BPF_LINE_INFO_ENC(10, 0, NAME_TBD, 1, 10),
6145		BPF_LINE_INFO_ENC(11, 0, NAME_TBD, 2, 9),
6146		BTF_END_RAW,
6147	},
6148	.line_info_rec_size = sizeof(struct bpf_line_info),
6149	.nr_jited_ksyms = 2,
6150	.dead_code_cnt = 3,
6151	.dead_code_mask = 0x70,
6152	.dead_func_cnt = 1,
6153	.dead_func_mask = 0x2,
6154},
6155
6156{
6157	.descr = "line_info (dead last subprog)",
6158	.raw_types = {
6159		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6160		BTF_FUNC_PROTO_ENC(1, 1),			/* [2] */
6161			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
6162		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
6163		BTF_FUNC_ENC(NAME_TBD, 2),			/* [5] */
6164		BTF_END_RAW,
6165	},
6166	BTF_STR_SEC("\0int\0x\0dead\0main\0int a=1+1;\0/* live call */"
6167		    "\0return 0;\0/* dead */\0/* dead */"),
6168	.insns = {
6169		BPF_MOV64_IMM(BPF_REG_2, 1),
6170		BPF_JMP_IMM(BPF_JGE, BPF_REG_2, 0, 1),
6171		BPF_CALL_REL(2),
6172		BPF_MOV64_IMM(BPF_REG_0, 0),
6173		BPF_EXIT_INSN(),
6174		BPF_MOV64_IMM(BPF_REG_0, 0),
6175		BPF_EXIT_INSN(),
6176	},
6177	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
6178	.func_info_cnt = 2,
6179	.func_info_rec_size = 8,
6180		.func_info = { {0, 4}, {5, 3} },
6181	.line_info = {
6182		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
6183		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 1, 10),
6184		BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 1, 10),
6185		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 1, 10),
6186		BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
6187		BTF_END_RAW,
6188	},
6189	.line_info_rec_size = sizeof(struct bpf_line_info),
6190	.nr_jited_ksyms = 1,
6191	.dead_code_cnt = 2,
6192	.dead_code_mask = 0x18,
6193	.dead_func_cnt = 1,
6194	.dead_func_mask = 0x2,
6195},
6196
6197{
6198	.descr = "line_info (dead subprog + dead start)",
6199	.raw_types = {
6200		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6201		BTF_FUNC_PROTO_ENC(1, 1),			/* [2] */
6202			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
6203		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
6204		BTF_FUNC_ENC(NAME_TBD, 2),			/* [4] */
6205		BTF_FUNC_ENC(NAME_TBD, 2),			/* [5] */
6206		BTF_END_RAW,
6207	},
6208	BTF_STR_SEC("\0int\0x\0dead\0main\0func\0int a=1+1;\0/* dead */"
6209		    "\0return 0;\0return 0;\0return 0;"
6210		    "\0/* dead */\0/* dead */\0/* dead */\0/* dead */"
6211		    "\0return b + 1;\0return b + 1;\0return b + 1;"),
6212	.insns = {
6213		BPF_JMP_IMM(BPF_JA, 0, 0, 0),
6214		BPF_MOV64_IMM(BPF_REG_2, 1),
6215		BPF_JMP_IMM(BPF_JGE, BPF_REG_2, 0, 1),
6216		BPF_CALL_REL(3),
6217		BPF_CALL_REL(5),
6218		BPF_MOV64_IMM(BPF_REG_0, 0),
6219		BPF_EXIT_INSN(),
6220		BPF_MOV64_IMM(BPF_REG_0, 0),
6221		BPF_CALL_REL(1),
6222		BPF_EXIT_INSN(),
6223		BPF_JMP_IMM(BPF_JA, 0, 0, 0),
6224		BPF_MOV64_REG(BPF_REG_0, 2),
6225		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
6226		BPF_EXIT_INSN(),
6227	},
6228	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
6229	.func_info_cnt = 3,
6230	.func_info_rec_size = 8,
6231		.func_info = { {0, 4}, {7, 3}, {10, 5} },
6232	.line_info = {
6233		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
6234		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 1, 10),
6235		BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 1, 10),
6236		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 1, 10),
6237		BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
6238		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 1, 10),
6239		BPF_LINE_INFO_ENC(8, 0, NAME_TBD, 1, 10),
6240		BPF_LINE_INFO_ENC(9, 0, NAME_TBD, 1, 10),
6241		BPF_LINE_INFO_ENC(10, 0, NAME_TBD, 1, 10),
6242		BPF_LINE_INFO_ENC(11, 0, NAME_TBD, 2, 9),
6243		BPF_LINE_INFO_ENC(12, 0, NAME_TBD, 2, 9),
6244		BPF_LINE_INFO_ENC(13, 0, NAME_TBD, 2, 9),
6245		BTF_END_RAW,
6246	},
6247	.line_info_rec_size = sizeof(struct bpf_line_info),
6248	.nr_jited_ksyms = 2,
6249	.dead_code_cnt = 5,
6250	.dead_code_mask = 0x1e2,
6251	.dead_func_cnt = 1,
6252	.dead_func_mask = 0x2,
6253},
6254
6255{
6256	.descr = "line_info (dead subprog + dead start w/ move)",
6257	.raw_types = {
6258		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6259		BTF_FUNC_PROTO_ENC(1, 1),			/* [2] */
6260			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
6261		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
6262		BTF_FUNC_ENC(NAME_TBD, 2),			/* [4] */
6263		BTF_FUNC_ENC(NAME_TBD, 2),			/* [5] */
6264		BTF_END_RAW,
6265	},
6266	BTF_STR_SEC("\0int\0x\0dead\0main\0func\0int a=1+1;\0/* live call */"
6267		    "\0return 0;\0return 0;\0/* dead */\0/* dead */"
6268		    "\0/* dead */\0return bla + 1;\0return bla + 1;"
6269		    "\0return bla + 1;\0return func(a);\0b+=1;\0return b;"),
6270	.insns = {
6271		BPF_MOV64_IMM(BPF_REG_2, 1),
6272		BPF_JMP_IMM(BPF_JGE, BPF_REG_2, 0, 1),
6273		BPF_CALL_REL(3),
6274		BPF_CALL_REL(5),
6275		BPF_MOV64_IMM(BPF_REG_0, 0),
6276		BPF_EXIT_INSN(),
6277		BPF_MOV64_IMM(BPF_REG_0, 0),
6278		BPF_CALL_REL(1),
6279		BPF_EXIT_INSN(),
6280		BPF_JMP_IMM(BPF_JA, 0, 0, 0),
6281		BPF_MOV64_REG(BPF_REG_0, 2),
6282		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
6283		BPF_EXIT_INSN(),
6284	},
6285	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
6286	.func_info_cnt = 3,
6287	.func_info_rec_size = 8,
6288		.func_info = { {0, 4}, {6, 3}, {9, 5} },
6289	.line_info = {
6290		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
6291		BPF_LINE_INFO_ENC(3, 0, NAME_TBD, 1, 10),
6292		BPF_LINE_INFO_ENC(4, 0, NAME_TBD, 1, 10),
6293		BPF_LINE_INFO_ENC(5, 0, NAME_TBD, 1, 10),
6294		BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
6295		BPF_LINE_INFO_ENC(7, 0, NAME_TBD, 1, 10),
6296		BPF_LINE_INFO_ENC(8, 0, NAME_TBD, 1, 10),
6297		BPF_LINE_INFO_ENC(9, 0, NAME_TBD, 1, 10),
6298		BPF_LINE_INFO_ENC(11, 0, NAME_TBD, 1, 10),
6299		BPF_LINE_INFO_ENC(12, 0, NAME_TBD, 2, 9),
6300		BTF_END_RAW,
6301	},
6302	.line_info_rec_size = sizeof(struct bpf_line_info),
6303	.nr_jited_ksyms = 2,
6304	.dead_code_cnt = 3,
6305	.dead_code_mask = 0x70,
6306	.dead_func_cnt = 1,
6307	.dead_func_mask = 0x2,
6308},
6309
6310{
6311	.descr = "line_info (dead end + subprog start w/ no linfo)",
6312	.raw_types = {
6313		BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6314		BTF_FUNC_PROTO_ENC(1, 1),			/* [2] */
6315			BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
6316		BTF_FUNC_ENC(NAME_TBD, 2),			/* [3] */
6317		BTF_FUNC_ENC(NAME_TBD, 2),			/* [4] */
6318		BTF_END_RAW,
6319	},
6320	BTF_STR_SEC("\0int\0x\0main\0func\0/* main linfo */\0/* func linfo */"),
6321	.insns = {
6322		BPF_MOV64_IMM(BPF_REG_0, 0),
6323		BPF_JMP_IMM(BPF_JGE, BPF_REG_0, 1, 3),
6324		BPF_CALL_REL(3),
6325		BPF_MOV64_IMM(BPF_REG_0, 0),
6326		BPF_EXIT_INSN(),
6327		BPF_EXIT_INSN(),
6328		BPF_JMP_IMM(BPF_JA, 0, 0, 0),
6329		BPF_EXIT_INSN(),
6330	},
6331	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
6332	.func_info_cnt = 2,
6333	.func_info_rec_size = 8,
6334	.func_info = { {0, 3}, {6, 4}, },
6335	.line_info = {
6336		BPF_LINE_INFO_ENC(0, 0, NAME_TBD, 1, 10),
6337		BPF_LINE_INFO_ENC(6, 0, NAME_TBD, 1, 10),
6338		BTF_END_RAW,
6339	},
6340	.line_info_rec_size = sizeof(struct bpf_line_info),
6341	.nr_jited_ksyms = 2,
6342},
6343
6344};
6345
6346static size_t probe_prog_length(const struct bpf_insn *fp)
6347{
6348	size_t len;
6349
6350	for (len = MAX_INSNS - 1; len > 0; --len)
6351		if (fp[len].code != 0 || fp[len].imm != 0)
6352			break;
6353	return len + 1;
6354}
6355
6356static __u32 *patch_name_tbd(const __u32 *raw_u32,
6357			     const char *str, __u32 str_off,
6358			     unsigned int str_sec_size,
6359			     unsigned int *ret_size)
6360{
6361	int i, raw_u32_size = get_raw_sec_size(raw_u32);
6362	const char *end_str = str + str_sec_size;
6363	const char *next_str = str + str_off;
6364	__u32 *new_u32 = NULL;
6365
6366	if (raw_u32_size == -1)
6367		return ERR_PTR(-EINVAL);
6368
6369	if (!raw_u32_size) {
6370		*ret_size = 0;
6371		return NULL;
6372	}
6373
6374	new_u32 = malloc(raw_u32_size);
6375	if (!new_u32)
6376		return ERR_PTR(-ENOMEM);
6377
6378	for (i = 0; i < raw_u32_size / sizeof(raw_u32[0]); i++) {
6379		if (raw_u32[i] == NAME_TBD) {
6380			next_str = get_next_str(next_str, end_str);
6381			if (CHECK(!next_str, "Error in getting next_str\n")) {
6382				free(new_u32);
6383				return ERR_PTR(-EINVAL);
6384			}
6385			new_u32[i] = next_str - str;
6386			next_str += strlen(next_str);
6387		} else {
6388			new_u32[i] = raw_u32[i];
6389		}
6390	}
6391
6392	*ret_size = raw_u32_size;
6393	return new_u32;
6394}
6395
6396static int test_get_finfo(const struct prog_info_raw_test *test,
6397			  int prog_fd)
6398{
6399	struct bpf_prog_info info = {};
6400	struct bpf_func_info *finfo;
6401	__u32 info_len, rec_size, i;
6402	void *func_info = NULL;
6403	__u32 nr_func_info;
6404	int err;
6405
6406	/* get necessary lens */
6407	info_len = sizeof(struct bpf_prog_info);
6408	err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
6409	if (CHECK(err < 0, "invalid get info (1st) errno:%d", errno)) {
6410		fprintf(stderr, "%s\n", btf_log_buf);
6411		return -1;
6412	}
6413	nr_func_info = test->func_info_cnt - test->dead_func_cnt;
6414	if (CHECK(info.nr_func_info != nr_func_info,
6415		  "incorrect info.nr_func_info (1st) %d",
6416		  info.nr_func_info)) {
6417		return -1;
6418	}
6419
6420	rec_size = info.func_info_rec_size;
6421	if (CHECK(rec_size != sizeof(struct bpf_func_info),
6422		  "incorrect info.func_info_rec_size (1st) %d", rec_size)) {
6423		return -1;
6424	}
6425
6426	if (!info.nr_func_info)
6427		return 0;
6428
6429	func_info = malloc(info.nr_func_info * rec_size);
6430	if (CHECK(!func_info, "out of memory"))
6431		return -1;
6432
6433	/* reset info to only retrieve func_info related data */
6434	memset(&info, 0, sizeof(info));
6435	info.nr_func_info = nr_func_info;
6436	info.func_info_rec_size = rec_size;
6437	info.func_info = ptr_to_u64(func_info);
6438	err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
6439	if (CHECK(err < 0, "invalid get info (2nd) errno:%d", errno)) {
6440		fprintf(stderr, "%s\n", btf_log_buf);
6441		err = -1;
6442		goto done;
6443	}
6444	if (CHECK(info.nr_func_info != nr_func_info,
6445		  "incorrect info.nr_func_info (2nd) %d",
6446		  info.nr_func_info)) {
6447		err = -1;
6448		goto done;
6449	}
6450	if (CHECK(info.func_info_rec_size != rec_size,
6451		  "incorrect info.func_info_rec_size (2nd) %d",
6452		  info.func_info_rec_size)) {
6453		err = -1;
6454		goto done;
6455	}
6456
6457	finfo = func_info;
6458	for (i = 0; i < nr_func_info; i++) {
6459		if (test->dead_func_mask & (1 << i))
6460			continue;
6461		if (CHECK(finfo->type_id != test->func_info[i][1],
6462			  "incorrect func_type %u expected %u",
6463			  finfo->type_id, test->func_info[i][1])) {
6464			err = -1;
6465			goto done;
6466		}
6467		finfo = (void *)finfo + rec_size;
6468	}
6469
6470	err = 0;
6471
6472done:
6473	free(func_info);
6474	return err;
6475}
6476
6477static int test_get_linfo(const struct prog_info_raw_test *test,
6478			  const void *patched_linfo,
6479			  __u32 cnt, int prog_fd)
6480{
6481	__u32 i, info_len, nr_jited_ksyms, nr_jited_func_lens;
6482	__u64 *jited_linfo = NULL, *jited_ksyms = NULL;
6483	__u32 rec_size, jited_rec_size, jited_cnt;
6484	struct bpf_line_info *linfo = NULL;
6485	__u32 cur_func_len, ksyms_found;
6486	struct bpf_prog_info info = {};
6487	__u32 *jited_func_lens = NULL;
6488	__u64 cur_func_ksyms;
6489	__u32 dead_insns;
6490	int err;
6491
6492	jited_cnt = cnt;
6493	rec_size = sizeof(*linfo);
6494	jited_rec_size = sizeof(*jited_linfo);
6495	if (test->nr_jited_ksyms)
6496		nr_jited_ksyms = test->nr_jited_ksyms;
6497	else
6498		nr_jited_ksyms = test->func_info_cnt - test->dead_func_cnt;
6499	nr_jited_func_lens = nr_jited_ksyms;
6500
6501	info_len = sizeof(struct bpf_prog_info);
6502	err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
6503	if (CHECK(err < 0, "err:%d errno:%d", err, errno)) {
6504		err = -1;
6505		goto done;
6506	}
6507
6508	if (!info.jited_prog_len) {
6509		/* prog is not jited */
6510		jited_cnt = 0;
6511		nr_jited_ksyms = 1;
6512		nr_jited_func_lens = 1;
6513	}
6514
6515	if (CHECK(info.nr_line_info != cnt ||
6516		  info.nr_jited_line_info != jited_cnt ||
6517		  info.nr_jited_ksyms != nr_jited_ksyms ||
6518		  info.nr_jited_func_lens != nr_jited_func_lens ||
6519		  (!info.nr_line_info && info.nr_jited_line_info),
6520		  "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)",
6521		  info.nr_line_info, cnt,
6522		  info.nr_jited_line_info, jited_cnt,
6523		  info.nr_jited_ksyms, nr_jited_ksyms,
6524		  info.nr_jited_func_lens, nr_jited_func_lens)) {
6525		err = -1;
6526		goto done;
6527	}
6528
6529	if (CHECK(info.line_info_rec_size != sizeof(struct bpf_line_info) ||
6530		  info.jited_line_info_rec_size != sizeof(__u64),
6531		  "info: line_info_rec_size:%u(userspace expected:%u) jited_line_info_rec_size:%u(userspace expected:%u)",
6532		  info.line_info_rec_size, rec_size,
6533		  info.jited_line_info_rec_size, jited_rec_size)) {
6534		err = -1;
6535		goto done;
6536	}
6537
6538	if (!cnt)
6539		return 0;
6540
6541	rec_size = info.line_info_rec_size;
6542	jited_rec_size = info.jited_line_info_rec_size;
6543
6544	memset(&info, 0, sizeof(info));
6545
6546	linfo = calloc(cnt, rec_size);
6547	if (CHECK(!linfo, "!linfo")) {
6548		err = -1;
6549		goto done;
6550	}
6551	info.nr_line_info = cnt;
6552	info.line_info_rec_size = rec_size;
6553	info.line_info = ptr_to_u64(linfo);
6554
6555	if (jited_cnt) {
6556		jited_linfo = calloc(jited_cnt, jited_rec_size);
6557		jited_ksyms = calloc(nr_jited_ksyms, sizeof(*jited_ksyms));
6558		jited_func_lens = calloc(nr_jited_func_lens,
6559					 sizeof(*jited_func_lens));
6560		if (CHECK(!jited_linfo || !jited_ksyms || !jited_func_lens,
6561			  "jited_linfo:%p jited_ksyms:%p jited_func_lens:%p",
6562			  jited_linfo, jited_ksyms, jited_func_lens)) {
6563			err = -1;
6564			goto done;
6565		}
6566
6567		info.nr_jited_line_info = jited_cnt;
6568		info.jited_line_info_rec_size = jited_rec_size;
6569		info.jited_line_info = ptr_to_u64(jited_linfo);
6570		info.nr_jited_ksyms = nr_jited_ksyms;
6571		info.jited_ksyms = ptr_to_u64(jited_ksyms);
6572		info.nr_jited_func_lens = nr_jited_func_lens;
6573		info.jited_func_lens = ptr_to_u64(jited_func_lens);
6574	}
6575
6576	err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
6577
6578	/*
6579	 * Only recheck the info.*line_info* fields.
6580	 * Other fields are not the concern of this test.
6581	 */
6582	if (CHECK(err < 0 ||
6583		  info.nr_line_info != cnt ||
6584		  (jited_cnt && !info.jited_line_info) ||
6585		  info.nr_jited_line_info != jited_cnt ||
6586		  info.line_info_rec_size != rec_size ||
6587		  info.jited_line_info_rec_size != jited_rec_size,
6588		  "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",
6589		  err, errno,
6590		  info.nr_line_info, cnt,
6591		  info.nr_jited_line_info, jited_cnt,
6592		  info.line_info_rec_size, rec_size,
6593		  info.jited_line_info_rec_size, jited_rec_size,
6594		  (void *)(long)info.line_info,
6595		  (void *)(long)info.jited_line_info)) {
6596		err = -1;
6597		goto done;
6598	}
6599
6600	dead_insns = 0;
6601	while (test->dead_code_mask & (1 << dead_insns))
6602		dead_insns++;
6603
6604	CHECK(linfo[0].insn_off, "linfo[0].insn_off:%u",
6605	      linfo[0].insn_off);
6606	for (i = 1; i < cnt; i++) {
6607		const struct bpf_line_info *expected_linfo;
6608
6609		while (test->dead_code_mask & (1 << (i + dead_insns)))
6610			dead_insns++;
6611
6612		expected_linfo = patched_linfo +
6613			((i + dead_insns) * test->line_info_rec_size);
6614		if (CHECK(linfo[i].insn_off <= linfo[i - 1].insn_off,
6615			  "linfo[%u].insn_off:%u <= linfo[%u].insn_off:%u",
6616			  i, linfo[i].insn_off,
6617			  i - 1, linfo[i - 1].insn_off)) {
6618			err = -1;
6619			goto done;
6620		}
6621		if (CHECK(linfo[i].file_name_off != expected_linfo->file_name_off ||
6622			  linfo[i].line_off != expected_linfo->line_off ||
6623			  linfo[i].line_col != expected_linfo->line_col,
6624			  "linfo[%u] (%u, %u, %u) != (%u, %u, %u)", i,
6625			  linfo[i].file_name_off,
6626			  linfo[i].line_off,
6627			  linfo[i].line_col,
6628			  expected_linfo->file_name_off,
6629			  expected_linfo->line_off,
6630			  expected_linfo->line_col)) {
6631			err = -1;
6632			goto done;
6633		}
6634	}
6635
6636	if (!jited_cnt) {
6637		fprintf(stderr, "not jited. skipping jited_line_info check. ");
6638		err = 0;
6639		goto done;
6640	}
6641
6642	if (CHECK(jited_linfo[0] != jited_ksyms[0],
6643		  "jited_linfo[0]:%lx != jited_ksyms[0]:%lx",
6644		  (long)(jited_linfo[0]), (long)(jited_ksyms[0]))) {
6645		err = -1;
6646		goto done;
6647	}
6648
6649	ksyms_found = 1;
6650	cur_func_len = jited_func_lens[0];
6651	cur_func_ksyms = jited_ksyms[0];
6652	for (i = 1; i < jited_cnt; i++) {
6653		if (ksyms_found < nr_jited_ksyms &&
6654		    jited_linfo[i] == jited_ksyms[ksyms_found]) {
6655			cur_func_ksyms = jited_ksyms[ksyms_found];
6656			cur_func_len = jited_ksyms[ksyms_found];
6657			ksyms_found++;
6658			continue;
6659		}
6660
6661		if (CHECK(jited_linfo[i] <= jited_linfo[i - 1],
6662			  "jited_linfo[%u]:%lx <= jited_linfo[%u]:%lx",
6663			  i, (long)jited_linfo[i],
6664			  i - 1, (long)(jited_linfo[i - 1]))) {
6665			err = -1;
6666			goto done;
6667		}
6668
6669		if (CHECK(jited_linfo[i] - cur_func_ksyms > cur_func_len,
6670			  "jited_linfo[%u]:%lx - %lx > %u",
6671			  i, (long)jited_linfo[i], (long)cur_func_ksyms,
6672			  cur_func_len)) {
6673			err = -1;
6674			goto done;
6675		}
6676	}
6677
6678	if (CHECK(ksyms_found != nr_jited_ksyms,
6679		  "ksyms_found:%u != nr_jited_ksyms:%u",
6680		  ksyms_found, nr_jited_ksyms)) {
6681		err = -1;
6682		goto done;
6683	}
6684
6685	err = 0;
6686
6687done:
6688	free(linfo);
6689	free(jited_linfo);
6690	free(jited_ksyms);
6691	free(jited_func_lens);
6692	return err;
6693}
6694
6695static void do_test_info_raw(unsigned int test_num)
6696{
6697	const struct prog_info_raw_test *test = &info_raw_tests[test_num - 1];
6698	unsigned int raw_btf_size, linfo_str_off, linfo_size = 0;
6699	int btf_fd = -1, prog_fd = -1, err = 0;
6700	void *raw_btf, *patched_linfo = NULL;
6701	const char *ret_next_str;
6702	union bpf_attr attr = {};
6703
6704	if (!test__start_subtest(test->descr))
6705		return;
6706
6707	raw_btf = btf_raw_create(&hdr_tmpl, test->raw_types,
6708				 test->str_sec, test->str_sec_size,
6709				 &raw_btf_size, &ret_next_str);
6710	if (!raw_btf)
6711		return;
6712
6713	*btf_log_buf = '\0';
6714	btf_fd = load_raw_btf(raw_btf, raw_btf_size);
6715	free(raw_btf);
6716
6717	if (CHECK(btf_fd < 0, "invalid btf_fd errno:%d", errno)) {
6718		err = -1;
6719		goto done;
6720	}
6721
6722	if (*btf_log_buf && always_log)
6723		fprintf(stderr, "\n%s", btf_log_buf);
6724	*btf_log_buf = '\0';
6725
6726	linfo_str_off = ret_next_str - test->str_sec;
6727	patched_linfo = patch_name_tbd(test->line_info,
6728				       test->str_sec, linfo_str_off,
6729				       test->str_sec_size, &linfo_size);
6730	err = libbpf_get_error(patched_linfo);
6731	if (err) {
6732		fprintf(stderr, "error in creating raw bpf_line_info");
6733		err = -1;
6734		goto done;
6735	}
6736
6737	attr.prog_type = test->prog_type;
6738	attr.insns = ptr_to_u64(test->insns);
6739	attr.insn_cnt = probe_prog_length(test->insns);
6740	attr.license = ptr_to_u64("GPL");
6741	attr.prog_btf_fd = btf_fd;
6742	attr.func_info_rec_size = test->func_info_rec_size;
6743	attr.func_info_cnt = test->func_info_cnt;
6744	attr.func_info = ptr_to_u64(test->func_info);
6745	attr.log_buf = ptr_to_u64(btf_log_buf);
6746	attr.log_size = BTF_LOG_BUF_SIZE;
6747	attr.log_level = 1;
6748	if (linfo_size) {
6749		attr.line_info_rec_size = test->line_info_rec_size;
6750		attr.line_info = ptr_to_u64(patched_linfo);
6751		attr.line_info_cnt = linfo_size / attr.line_info_rec_size;
6752	}
6753
6754	prog_fd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
6755	err = ((prog_fd < 0) != test->expected_prog_load_failure);
6756	if (CHECK(err, "prog_fd:%d expected_prog_load_failure:%u errno:%d",
6757		  prog_fd, test->expected_prog_load_failure, errno) ||
6758	    CHECK(test->err_str && !strstr(btf_log_buf, test->err_str),
6759		  "expected err_str:%s", test->err_str)) {
6760		err = -1;
6761		goto done;
6762	}
6763
6764	if (prog_fd < 0)
6765		goto done;
6766
6767	err = test_get_finfo(test, prog_fd);
6768	if (err)
6769		goto done;
6770
6771	err = test_get_linfo(test, patched_linfo,
6772			     attr.line_info_cnt - test->dead_code_cnt,
6773			     prog_fd);
6774	if (err)
6775		goto done;
6776
6777done:
6778	if (*btf_log_buf && (err || always_log))
6779		fprintf(stderr, "\n%s", btf_log_buf);
6780
6781	if (btf_fd >= 0)
6782		close(btf_fd);
6783	if (prog_fd >= 0)
6784		close(prog_fd);
6785
6786	if (!libbpf_get_error(patched_linfo))
6787		free(patched_linfo);
6788}
6789
6790struct btf_raw_data {
6791	__u32 raw_types[MAX_NR_RAW_U32];
6792	const char *str_sec;
6793	__u32 str_sec_size;
6794};
6795
6796struct btf_dedup_test {
6797	const char *descr;
6798	struct btf_raw_data input;
6799	struct btf_raw_data expect;
6800	struct btf_dedup_opts opts;
6801};
6802
6803static struct btf_dedup_test dedup_tests[] = {
6804
6805{
6806	.descr = "dedup: unused strings filtering",
6807	.input = {
6808		.raw_types = {
6809			BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 32, 4),
6810			BTF_TYPE_INT_ENC(NAME_NTH(5), BTF_INT_SIGNED, 0, 64, 8),
6811			BTF_END_RAW,
6812		},
6813		BTF_STR_SEC("\0unused\0int\0foo\0bar\0long"),
6814	},
6815	.expect = {
6816		.raw_types = {
6817			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),
6818			BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 64, 8),
6819			BTF_END_RAW,
6820		},
6821		BTF_STR_SEC("\0int\0long"),
6822	},
6823},
6824{
6825	.descr = "dedup: strings deduplication",
6826	.input = {
6827		.raw_types = {
6828			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),
6829			BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 64, 8),
6830			BTF_TYPE_INT_ENC(NAME_NTH(3), BTF_INT_SIGNED, 0, 32, 4),
6831			BTF_TYPE_INT_ENC(NAME_NTH(4), BTF_INT_SIGNED, 0, 64, 8),
6832			BTF_TYPE_INT_ENC(NAME_NTH(5), BTF_INT_SIGNED, 0, 32, 4),
6833			BTF_END_RAW,
6834		},
6835		BTF_STR_SEC("\0int\0long int\0int\0long int\0int"),
6836	},
6837	.expect = {
6838		.raw_types = {
6839			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),
6840			BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 64, 8),
6841			BTF_END_RAW,
6842		},
6843		BTF_STR_SEC("\0int\0long int"),
6844	},
6845},
6846{
6847	.descr = "dedup: struct example #1",
6848	/*
6849	 * struct s {
6850	 *	struct s *next;
6851	 *	const int *a;
6852	 *	int b[16];
6853	 *	int c;
6854	 * }
6855	 */
6856	.input = {
6857		.raw_types = {
6858			/* int */
6859			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6860			/* int[16] */
6861			BTF_TYPE_ARRAY_ENC(1, 1, 16),					/* [2] */
6862			/* struct s { */
6863			BTF_STRUCT_ENC(NAME_NTH(2), 5, 88),				/* [3] */
6864				BTF_MEMBER_ENC(NAME_NTH(3), 4, 0),	/* struct s *next;	*/
6865				BTF_MEMBER_ENC(NAME_NTH(4), 5, 64),	/* const int *a;	*/
6866				BTF_MEMBER_ENC(NAME_NTH(5), 2, 128),	/* int b[16];		*/
6867				BTF_MEMBER_ENC(NAME_NTH(6), 1, 640),	/* int c;		*/
6868				BTF_MEMBER_ENC(NAME_NTH(8), 15, 672),	/* float d;		*/
6869			/* ptr -> [3] struct s */
6870			BTF_PTR_ENC(3),							/* [4] */
6871			/* ptr -> [6] const int */
6872			BTF_PTR_ENC(6),							/* [5] */
6873			/* const -> [1] int */
6874			BTF_CONST_ENC(1),						/* [6] */
6875			/* tag -> [3] struct s */
6876			BTF_DECL_TAG_ENC(NAME_NTH(2), 3, -1),				/* [7] */
6877			/* tag -> [3] struct s, member 1 */
6878			BTF_DECL_TAG_ENC(NAME_NTH(2), 3, 1),				/* [8] */
6879
6880			/* full copy of the above */
6881			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),	/* [9] */
6882			BTF_TYPE_ARRAY_ENC(9, 9, 16),					/* [10] */
6883			BTF_STRUCT_ENC(NAME_NTH(2), 5, 88),				/* [11] */
6884				BTF_MEMBER_ENC(NAME_NTH(3), 12, 0),
6885				BTF_MEMBER_ENC(NAME_NTH(4), 13, 64),
6886				BTF_MEMBER_ENC(NAME_NTH(5), 10, 128),
6887				BTF_MEMBER_ENC(NAME_NTH(6), 9, 640),
6888				BTF_MEMBER_ENC(NAME_NTH(8), 15, 672),
6889			BTF_PTR_ENC(11),						/* [12] */
6890			BTF_PTR_ENC(14),						/* [13] */
6891			BTF_CONST_ENC(9),						/* [14] */
6892			BTF_TYPE_FLOAT_ENC(NAME_NTH(7), 4),				/* [15] */
6893			BTF_DECL_TAG_ENC(NAME_NTH(2), 11, -1),				/* [16] */
6894			BTF_DECL_TAG_ENC(NAME_NTH(2), 11, 1),				/* [17] */
6895			BTF_END_RAW,
6896		},
6897		BTF_STR_SEC("\0int\0s\0next\0a\0b\0c\0float\0d"),
6898	},
6899	.expect = {
6900		.raw_types = {
6901			/* int */
6902			BTF_TYPE_INT_ENC(NAME_NTH(5), BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
6903			/* int[16] */
6904			BTF_TYPE_ARRAY_ENC(1, 1, 16),					/* [2] */
6905			/* struct s { */
6906			BTF_STRUCT_ENC(NAME_NTH(8), 5, 88),				/* [3] */
6907				BTF_MEMBER_ENC(NAME_NTH(7), 4, 0),	/* struct s *next;	*/
6908				BTF_MEMBER_ENC(NAME_NTH(1), 5, 64),	/* const int *a;	*/
6909				BTF_MEMBER_ENC(NAME_NTH(2), 2, 128),	/* int b[16];		*/
6910				BTF_MEMBER_ENC(NAME_NTH(3), 1, 640),	/* int c;		*/
6911				BTF_MEMBER_ENC(NAME_NTH(4), 9, 672),	/* float d;		*/
6912			/* ptr -> [3] struct s */
6913			BTF_PTR_ENC(3),							/* [4] */
6914			/* ptr -> [6] const int */
6915			BTF_PTR_ENC(6),							/* [5] */
6916			/* const -> [1] int */
6917			BTF_CONST_ENC(1),						/* [6] */
6918			BTF_DECL_TAG_ENC(NAME_NTH(2), 3, -1),				/* [7] */
6919			BTF_DECL_TAG_ENC(NAME_NTH(2), 3, 1),				/* [8] */
6920			BTF_TYPE_FLOAT_ENC(NAME_NTH(7), 4),				/* [9] */
6921			BTF_END_RAW,
6922		},
6923		BTF_STR_SEC("\0a\0b\0c\0d\0int\0float\0next\0s"),
6924	},
6925},
6926{
6927	.descr = "dedup: struct <-> fwd resolution w/ hash collision",
6928	/*
6929	 * // CU 1:
6930	 * struct x;
6931	 * struct s {
6932	 *	struct x *x;
6933	 * };
6934	 * // CU 2:
6935	 * struct x {};
6936	 * struct s {
6937	 *	struct x *x;
6938	 * };
6939	 */
6940	.input = {
6941		.raw_types = {
6942			/* CU 1 */
6943			BTF_FWD_ENC(NAME_TBD, 0 /* struct fwd */),	/* [1] fwd x      */
6944			BTF_PTR_ENC(1),					/* [2] ptr -> [1] */
6945			BTF_STRUCT_ENC(NAME_TBD, 1, 8),			/* [3] struct s   */
6946				BTF_MEMBER_ENC(NAME_TBD, 2, 0),
6947			/* CU 2 */
6948			BTF_STRUCT_ENC(NAME_TBD, 0, 0),			/* [4] struct x   */
6949			BTF_PTR_ENC(4),					/* [5] ptr -> [4] */
6950			BTF_STRUCT_ENC(NAME_TBD, 1, 8),			/* [6] struct s   */
6951				BTF_MEMBER_ENC(NAME_TBD, 5, 0),
6952			BTF_END_RAW,
6953		},
6954		BTF_STR_SEC("\0x\0s\0x\0x\0s\0x\0"),
6955	},
6956	.expect = {
6957		.raw_types = {
6958			BTF_PTR_ENC(3),					/* [1] ptr -> [3] */
6959			BTF_STRUCT_ENC(NAME_TBD, 1, 8),			/* [2] struct s   */
6960				BTF_MEMBER_ENC(NAME_TBD, 1, 0),
6961			BTF_STRUCT_ENC(NAME_NTH(2), 0, 0),		/* [3] struct x   */
6962			BTF_END_RAW,
6963		},
6964		BTF_STR_SEC("\0s\0x"),
6965	},
6966	.opts = {
6967		.force_collisions = true, /* force hash collisions */
6968	},
6969},
6970{
6971	.descr = "dedup: void equiv check",
6972	/*
6973	 * // CU 1:
6974	 * struct s {
6975	 *	struct {} *x;
6976	 * };
6977	 * // CU 2:
6978	 * struct s {
6979	 *	int *x;
6980	 * };
6981	 */
6982	.input = {
6983		.raw_types = {
6984			/* CU 1 */
6985			BTF_STRUCT_ENC(0, 0, 1),				/* [1] struct {}  */
6986			BTF_PTR_ENC(1),						/* [2] ptr -> [1] */
6987			BTF_STRUCT_ENC(NAME_NTH(1), 1, 8),			/* [3] struct s   */
6988				BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
6989			/* CU 2 */
6990			BTF_PTR_ENC(0),						/* [4] ptr -> void */
6991			BTF_STRUCT_ENC(NAME_NTH(1), 1, 8),			/* [5] struct s   */
6992				BTF_MEMBER_ENC(NAME_NTH(2), 4, 0),
6993			BTF_END_RAW,
6994		},
6995		BTF_STR_SEC("\0s\0x"),
6996	},
6997	.expect = {
6998		.raw_types = {
6999			/* CU 1 */
7000			BTF_STRUCT_ENC(0, 0, 1),				/* [1] struct {}  */
7001			BTF_PTR_ENC(1),						/* [2] ptr -> [1] */
7002			BTF_STRUCT_ENC(NAME_NTH(1), 1, 8),			/* [3] struct s   */
7003				BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
7004			/* CU 2 */
7005			BTF_PTR_ENC(0),						/* [4] ptr -> void */
7006			BTF_STRUCT_ENC(NAME_NTH(1), 1, 8),			/* [5] struct s   */
7007				BTF_MEMBER_ENC(NAME_NTH(2), 4, 0),
7008			BTF_END_RAW,
7009		},
7010		BTF_STR_SEC("\0s\0x"),
7011	},
7012	.opts = {
7013		.force_collisions = true, /* force hash collisions */
7014	},
7015},
7016{
7017	.descr = "dedup: all possible kinds (no duplicates)",
7018	.input = {
7019		.raw_types = {
7020			BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 8),		/* [1] int */
7021			BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), 4),	/* [2] enum */
7022				BTF_ENUM_ENC(NAME_TBD, 0),
7023				BTF_ENUM_ENC(NAME_TBD, 1),
7024			BTF_FWD_ENC(NAME_TBD, 1 /* union kind_flag */),			/* [3] fwd */
7025			BTF_TYPE_ARRAY_ENC(2, 1, 7),					/* [4] array */
7026			BTF_STRUCT_ENC(NAME_TBD, 1, 4),					/* [5] struct */
7027				BTF_MEMBER_ENC(NAME_TBD, 1, 0),
7028			BTF_UNION_ENC(NAME_TBD, 1, 4),					/* [6] union */
7029				BTF_MEMBER_ENC(NAME_TBD, 1, 0),
7030			BTF_TYPEDEF_ENC(NAME_TBD, 1),					/* [7] typedef */
7031			BTF_PTR_ENC(0),							/* [8] ptr */
7032			BTF_CONST_ENC(8),						/* [9] const */
7033			BTF_VOLATILE_ENC(8),						/* [10] volatile */
7034			BTF_RESTRICT_ENC(8),						/* [11] restrict */
7035			BTF_FUNC_PROTO_ENC(1, 2),					/* [12] func_proto */
7036				BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
7037				BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 18),
7038			BTF_FUNC_ENC(NAME_TBD, 12),					/* [13] func */
7039			BTF_TYPE_FLOAT_ENC(NAME_TBD, 2),				/* [14] float */
7040			BTF_DECL_TAG_ENC(NAME_TBD, 13, -1),				/* [15] decl_tag */
7041			BTF_DECL_TAG_ENC(NAME_TBD, 13, 1),				/* [16] decl_tag */
7042			BTF_DECL_TAG_ENC(NAME_TBD, 7, -1),				/* [17] decl_tag */
7043			BTF_TYPE_TAG_ENC(NAME_TBD, 8),					/* [18] type_tag */
7044			BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 2), 8),	/* [19] enum64 */
7045				BTF_ENUM64_ENC(NAME_TBD, 0, 0),
7046				BTF_ENUM64_ENC(NAME_TBD, 1, 1),
7047			BTF_END_RAW,
7048		},
7049		BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q\0R\0S\0T\0U"),
7050	},
7051	.expect = {
7052		.raw_types = {
7053			BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 8),		/* [1] int */
7054			BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), 4),	/* [2] enum */
7055				BTF_ENUM_ENC(NAME_TBD, 0),
7056				BTF_ENUM_ENC(NAME_TBD, 1),
7057			BTF_FWD_ENC(NAME_TBD, 1 /* union kind_flag */),			/* [3] fwd */
7058			BTF_TYPE_ARRAY_ENC(2, 1, 7),					/* [4] array */
7059			BTF_STRUCT_ENC(NAME_TBD, 1, 4),					/* [5] struct */
7060				BTF_MEMBER_ENC(NAME_TBD, 1, 0),
7061			BTF_UNION_ENC(NAME_TBD, 1, 4),					/* [6] union */
7062				BTF_MEMBER_ENC(NAME_TBD, 1, 0),
7063			BTF_TYPEDEF_ENC(NAME_TBD, 1),					/* [7] typedef */
7064			BTF_PTR_ENC(0),							/* [8] ptr */
7065			BTF_CONST_ENC(8),						/* [9] const */
7066			BTF_VOLATILE_ENC(8),						/* [10] volatile */
7067			BTF_RESTRICT_ENC(8),						/* [11] restrict */
7068			BTF_FUNC_PROTO_ENC(1, 2),					/* [12] func_proto */
7069				BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
7070				BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 18),
7071			BTF_FUNC_ENC(NAME_TBD, 12),					/* [13] func */
7072			BTF_TYPE_FLOAT_ENC(NAME_TBD, 2),				/* [14] float */
7073			BTF_DECL_TAG_ENC(NAME_TBD, 13, -1),				/* [15] decl_tag */
7074			BTF_DECL_TAG_ENC(NAME_TBD, 13, 1),				/* [16] decl_tag */
7075			BTF_DECL_TAG_ENC(NAME_TBD, 7, -1),				/* [17] decl_tag */
7076			BTF_TYPE_TAG_ENC(NAME_TBD, 8),					/* [18] type_tag */
7077			BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 2), 8),	/* [19] enum64 */
7078				BTF_ENUM64_ENC(NAME_TBD, 0, 0),
7079				BTF_ENUM64_ENC(NAME_TBD, 1, 1),
7080			BTF_END_RAW,
7081		},
7082		BTF_STR_SEC("\0A\0B\0C\0D\0E\0F\0G\0H\0I\0J\0K\0L\0M\0N\0O\0P\0Q\0R\0S\0T\0U"),
7083	},
7084},
7085{
7086	.descr = "dedup: no int/float duplicates",
7087	.input = {
7088		.raw_types = {
7089			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 8),
7090			/* different name */
7091			BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 32, 8),
7092			/* different encoding */
7093			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_CHAR, 0, 32, 8),
7094			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_BOOL, 0, 32, 8),
7095			/* different bit offset */
7096			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 8, 32, 8),
7097			/* different bit size */
7098			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 27, 8),
7099			/* different byte size */
7100			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),
7101			/* all allowed sizes */
7102			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 2),
7103			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 4),
7104			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 8),
7105			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 12),
7106			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 16),
7107			BTF_END_RAW,
7108		},
7109		BTF_STR_SEC("\0int\0some other int\0float"),
7110	},
7111	.expect = {
7112		.raw_types = {
7113			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 8),
7114			/* different name */
7115			BTF_TYPE_INT_ENC(NAME_NTH(2), BTF_INT_SIGNED, 0, 32, 8),
7116			/* different encoding */
7117			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_CHAR, 0, 32, 8),
7118			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_BOOL, 0, 32, 8),
7119			/* different bit offset */
7120			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 8, 32, 8),
7121			/* different bit size */
7122			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 27, 8),
7123			/* different byte size */
7124			BTF_TYPE_INT_ENC(NAME_NTH(1), BTF_INT_SIGNED, 0, 32, 4),
7125			/* all allowed sizes */
7126			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 2),
7127			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 4),
7128			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 8),
7129			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 12),
7130			BTF_TYPE_FLOAT_ENC(NAME_NTH(3), 16),
7131			BTF_END_RAW,
7132		},
7133		BTF_STR_SEC("\0int\0some other int\0float"),
7134	},
7135},
7136{
7137	.descr = "dedup: enum fwd resolution",
7138	.input = {
7139		.raw_types = {
7140			/* [1] fwd enum 'e1' before full enum */
7141			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 0), 4),
7142			/* [2] full enum 'e1' after fwd */
7143			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7144				BTF_ENUM_ENC(NAME_NTH(2), 123),
7145			/* [3] full enum 'e2' before fwd */
7146			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7147				BTF_ENUM_ENC(NAME_NTH(4), 456),
7148			/* [4] fwd enum 'e2' after full enum */
7149			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 0), 4),
7150			/* [5] fwd enum with different size, size does not matter for fwd */
7151			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 0), 1),
7152			/* [6] incompatible full enum with different value */
7153			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7154				BTF_ENUM_ENC(NAME_NTH(2), 321),
7155			BTF_END_RAW,
7156		},
7157		BTF_STR_SEC("\0e1\0e1_val\0e2\0e2_val"),
7158	},
7159	.expect = {
7160		.raw_types = {
7161			/* [1] full enum 'e1' */
7162			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7163				BTF_ENUM_ENC(NAME_NTH(2), 123),
7164			/* [2] full enum 'e2' */
7165			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7166				BTF_ENUM_ENC(NAME_NTH(4), 456),
7167			/* [3] incompatible full enum with different value */
7168			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7169				BTF_ENUM_ENC(NAME_NTH(2), 321),
7170			BTF_END_RAW,
7171		},
7172		BTF_STR_SEC("\0e1\0e1_val\0e2\0e2_val"),
7173	},
7174},
7175{
7176	.descr = "dedup: datasec and vars pass-through",
7177	.input = {
7178		.raw_types = {
7179			/* int */
7180			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7181			/* static int t */
7182			BTF_VAR_ENC(NAME_NTH(2), 1, 0),			/* [2] */
7183			/* .bss section */				/* [3] */
7184			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
7185			BTF_VAR_SECINFO_ENC(2, 0, 4),
7186			/* int, referenced from [5] */
7187			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [4] */
7188			/* another static int t */
7189			BTF_VAR_ENC(NAME_NTH(2), 4, 0),			/* [5] */
7190			/* another .bss section */			/* [6] */
7191			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
7192			BTF_VAR_SECINFO_ENC(5, 0, 4),
7193			BTF_END_RAW,
7194		},
7195		BTF_STR_SEC("\0.bss\0t"),
7196	},
7197	.expect = {
7198		.raw_types = {
7199			/* int */
7200			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7201			/* static int t */
7202			BTF_VAR_ENC(NAME_NTH(2), 1, 0),			/* [2] */
7203			/* .bss section */				/* [3] */
7204			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
7205			BTF_VAR_SECINFO_ENC(2, 0, 4),
7206			/* another static int t */
7207			BTF_VAR_ENC(NAME_NTH(2), 1, 0),			/* [4] */
7208			/* another .bss section */			/* [5] */
7209			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
7210			BTF_VAR_SECINFO_ENC(4, 0, 4),
7211			BTF_END_RAW,
7212		},
7213		BTF_STR_SEC("\0.bss\0t"),
7214	},
7215	.opts = {
7216		.force_collisions = true
7217	},
7218},
7219{
7220	.descr = "dedup: func/func_arg/var tags",
7221	.input = {
7222		.raw_types = {
7223			/* int */
7224			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7225			/* static int t */
7226			BTF_VAR_ENC(NAME_NTH(1), 1, 0),			/* [2] */
7227			/* void f(int a1, int a2) */
7228			BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
7229				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(2), 1),
7230				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(3), 1),
7231			BTF_FUNC_ENC(NAME_NTH(4), 2),			/* [4] */
7232			/* tag -> t */
7233			BTF_DECL_TAG_ENC(NAME_NTH(5), 2, -1),		/* [5] */
7234			BTF_DECL_TAG_ENC(NAME_NTH(5), 2, -1),		/* [6] */
7235			/* tag -> func */
7236			BTF_DECL_TAG_ENC(NAME_NTH(5), 4, -1),		/* [7] */
7237			BTF_DECL_TAG_ENC(NAME_NTH(5), 4, -1),		/* [8] */
7238			/* tag -> func arg a1 */
7239			BTF_DECL_TAG_ENC(NAME_NTH(5), 4, 1),		/* [9] */
7240			BTF_DECL_TAG_ENC(NAME_NTH(5), 4, 1),		/* [10] */
7241			BTF_END_RAW,
7242		},
7243		BTF_STR_SEC("\0t\0a1\0a2\0f\0tag"),
7244	},
7245	.expect = {
7246		.raw_types = {
7247			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7248			BTF_VAR_ENC(NAME_NTH(1), 1, 0),			/* [2] */
7249			BTF_FUNC_PROTO_ENC(0, 2),			/* [3] */
7250				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(2), 1),
7251				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(3), 1),
7252			BTF_FUNC_ENC(NAME_NTH(4), 2),			/* [4] */
7253			BTF_DECL_TAG_ENC(NAME_NTH(5), 2, -1),		/* [5] */
7254			BTF_DECL_TAG_ENC(NAME_NTH(5), 4, -1),		/* [6] */
7255			BTF_DECL_TAG_ENC(NAME_NTH(5), 4, 1),		/* [7] */
7256			BTF_END_RAW,
7257		},
7258		BTF_STR_SEC("\0t\0a1\0a2\0f\0tag"),
7259	},
7260},
7261{
7262	.descr = "dedup: func/func_param tags",
7263	.input = {
7264		.raw_types = {
7265			/* int */
7266			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7267			/* void f(int a1, int a2) */
7268			BTF_FUNC_PROTO_ENC(0, 2),			/* [2] */
7269				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(1), 1),
7270				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(2), 1),
7271			BTF_FUNC_ENC(NAME_NTH(3), 2),			/* [3] */
7272			/* void f(int a1, int a2) */
7273			BTF_FUNC_PROTO_ENC(0, 2),			/* [4] */
7274				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(1), 1),
7275				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(2), 1),
7276			BTF_FUNC_ENC(NAME_NTH(3), 4),			/* [5] */
7277			/* tag -> f: tag1, tag2 */
7278			BTF_DECL_TAG_ENC(NAME_NTH(4), 3, -1),		/* [6] */
7279			BTF_DECL_TAG_ENC(NAME_NTH(5), 3, -1),		/* [7] */
7280			/* tag -> f/a2: tag1, tag2 */
7281			BTF_DECL_TAG_ENC(NAME_NTH(4), 3, 1),		/* [8] */
7282			BTF_DECL_TAG_ENC(NAME_NTH(5), 3, 1),		/* [9] */
7283			/* tag -> f: tag1, tag3 */
7284			BTF_DECL_TAG_ENC(NAME_NTH(4), 5, -1),		/* [10] */
7285			BTF_DECL_TAG_ENC(NAME_NTH(6), 5, -1),		/* [11] */
7286			/* tag -> f/a2: tag1, tag3 */
7287			BTF_DECL_TAG_ENC(NAME_NTH(4), 5, 1),		/* [12] */
7288			BTF_DECL_TAG_ENC(NAME_NTH(6), 5, 1),		/* [13] */
7289			BTF_END_RAW,
7290		},
7291		BTF_STR_SEC("\0a1\0a2\0f\0tag1\0tag2\0tag3"),
7292	},
7293	.expect = {
7294		.raw_types = {
7295			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7296			BTF_FUNC_PROTO_ENC(0, 2),			/* [2] */
7297				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(1), 1),
7298				BTF_FUNC_PROTO_ARG_ENC(NAME_NTH(2), 1),
7299			BTF_FUNC_ENC(NAME_NTH(3), 2),			/* [3] */
7300			BTF_DECL_TAG_ENC(NAME_NTH(4), 3, -1),		/* [4] */
7301			BTF_DECL_TAG_ENC(NAME_NTH(5), 3, -1),		/* [5] */
7302			BTF_DECL_TAG_ENC(NAME_NTH(6), 3, -1),		/* [6] */
7303			BTF_DECL_TAG_ENC(NAME_NTH(4), 3, 1),		/* [7] */
7304			BTF_DECL_TAG_ENC(NAME_NTH(5), 3, 1),		/* [8] */
7305			BTF_DECL_TAG_ENC(NAME_NTH(6), 3, 1),		/* [9] */
7306			BTF_END_RAW,
7307		},
7308		BTF_STR_SEC("\0a1\0a2\0f\0tag1\0tag2\0tag3"),
7309	},
7310},
7311{
7312	.descr = "dedup: struct/struct_member tags",
7313	.input = {
7314		.raw_types = {
7315			/* int */
7316			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7317			BTF_STRUCT_ENC(NAME_NTH(1), 2, 8),		/* [2] */
7318				BTF_MEMBER_ENC(NAME_NTH(2), 1, 0),
7319				BTF_MEMBER_ENC(NAME_NTH(3), 1, 32),
7320			BTF_STRUCT_ENC(NAME_NTH(1), 2, 8),		/* [3] */
7321				BTF_MEMBER_ENC(NAME_NTH(2), 1, 0),
7322				BTF_MEMBER_ENC(NAME_NTH(3), 1, 32),
7323			/* tag -> t: tag1, tag2 */
7324			BTF_DECL_TAG_ENC(NAME_NTH(4), 2, -1),		/* [4] */
7325			BTF_DECL_TAG_ENC(NAME_NTH(5), 2, -1),		/* [5] */
7326			/* tag -> t/m2: tag1, tag2 */
7327			BTF_DECL_TAG_ENC(NAME_NTH(4), 2, 1),		/* [6] */
7328			BTF_DECL_TAG_ENC(NAME_NTH(5), 2, 1),		/* [7] */
7329			/* tag -> t: tag1, tag3 */
7330			BTF_DECL_TAG_ENC(NAME_NTH(4), 3, -1),		/* [8] */
7331			BTF_DECL_TAG_ENC(NAME_NTH(6), 3, -1),		/* [9] */
7332			/* tag -> t/m2: tag1, tag3 */
7333			BTF_DECL_TAG_ENC(NAME_NTH(4), 3, 1),		/* [10] */
7334			BTF_DECL_TAG_ENC(NAME_NTH(6), 3, 1),		/* [11] */
7335			BTF_END_RAW,
7336		},
7337		BTF_STR_SEC("\0t\0m1\0m2\0tag1\0tag2\0tag3"),
7338	},
7339	.expect = {
7340		.raw_types = {
7341			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7342			BTF_STRUCT_ENC(NAME_NTH(1), 2, 8),		/* [2] */
7343				BTF_MEMBER_ENC(NAME_NTH(2), 1, 0),
7344				BTF_MEMBER_ENC(NAME_NTH(3), 1, 32),
7345			BTF_DECL_TAG_ENC(NAME_NTH(4), 2, -1),		/* [3] */
7346			BTF_DECL_TAG_ENC(NAME_NTH(5), 2, -1),		/* [4] */
7347			BTF_DECL_TAG_ENC(NAME_NTH(6), 2, -1),		/* [5] */
7348			BTF_DECL_TAG_ENC(NAME_NTH(4), 2, 1),		/* [6] */
7349			BTF_DECL_TAG_ENC(NAME_NTH(5), 2, 1),		/* [7] */
7350			BTF_DECL_TAG_ENC(NAME_NTH(6), 2, 1),		/* [8] */
7351			BTF_END_RAW,
7352		},
7353		BTF_STR_SEC("\0t\0m1\0m2\0tag1\0tag2\0tag3"),
7354	},
7355},
7356{
7357	.descr = "dedup: typedef tags",
7358	.input = {
7359		.raw_types = {
7360			/* int */
7361			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7362			BTF_TYPEDEF_ENC(NAME_NTH(1), 1),		/* [2] */
7363			BTF_TYPEDEF_ENC(NAME_NTH(1), 1),		/* [3] */
7364			/* tag -> t: tag1, tag2 */
7365			BTF_DECL_TAG_ENC(NAME_NTH(2), 2, -1),		/* [4] */
7366			BTF_DECL_TAG_ENC(NAME_NTH(3), 2, -1),		/* [5] */
7367			/* tag -> t: tag1, tag3 */
7368			BTF_DECL_TAG_ENC(NAME_NTH(2), 3, -1),		/* [6] */
7369			BTF_DECL_TAG_ENC(NAME_NTH(4), 3, -1),		/* [7] */
7370			BTF_END_RAW,
7371		},
7372		BTF_STR_SEC("\0t\0tag1\0tag2\0tag3"),
7373	},
7374	.expect = {
7375		.raw_types = {
7376			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7377			BTF_TYPEDEF_ENC(NAME_NTH(1), 1),		/* [2] */
7378			BTF_DECL_TAG_ENC(NAME_NTH(2), 2, -1),		/* [3] */
7379			BTF_DECL_TAG_ENC(NAME_NTH(3), 2, -1),		/* [4] */
7380			BTF_DECL_TAG_ENC(NAME_NTH(4), 2, -1),		/* [5] */
7381			BTF_END_RAW,
7382		},
7383		BTF_STR_SEC("\0t\0tag1\0tag2\0tag3"),
7384	},
7385},
7386{
7387	.descr = "dedup: btf_type_tag #1",
7388	.input = {
7389		.raw_types = {
7390			/* ptr -> tag2 -> tag1 -> int */
7391			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7392			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7393			BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),		/* [3] */
7394			BTF_PTR_ENC(3),					/* [4] */
7395			/* ptr -> tag2 -> tag1 -> int */
7396			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [5] */
7397			BTF_TYPE_TAG_ENC(NAME_NTH(2), 5),		/* [6] */
7398			BTF_PTR_ENC(6),					/* [7] */
7399			/* ptr -> tag1 -> int */
7400			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [8] */
7401			BTF_PTR_ENC(8),					/* [9] */
7402			BTF_END_RAW,
7403		},
7404		BTF_STR_SEC("\0tag1\0tag2"),
7405	},
7406	.expect = {
7407		.raw_types = {
7408			/* ptr -> tag2 -> tag1 -> int */
7409			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7410			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7411			BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),		/* [3] */
7412			BTF_PTR_ENC(3),					/* [4] */
7413			/* ptr -> tag1 -> int */
7414			BTF_PTR_ENC(2),					/* [5] */
7415			BTF_END_RAW,
7416		},
7417		BTF_STR_SEC("\0tag1\0tag2"),
7418	},
7419},
7420{
7421	.descr = "dedup: btf_type_tag #2",
7422	.input = {
7423		.raw_types = {
7424			/* ptr -> tag2 -> tag1 -> int */
7425			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7426			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7427			BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),		/* [3] */
7428			BTF_PTR_ENC(3),					/* [4] */
7429			/* ptr -> tag2 -> int */
7430			BTF_TYPE_TAG_ENC(NAME_NTH(2), 1),		/* [5] */
7431			BTF_PTR_ENC(5),					/* [6] */
7432			BTF_END_RAW,
7433		},
7434		BTF_STR_SEC("\0tag1\0tag2"),
7435	},
7436	.expect = {
7437		.raw_types = {
7438			/* ptr -> tag2 -> tag1 -> int */
7439			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7440			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7441			BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),		/* [3] */
7442			BTF_PTR_ENC(3),					/* [4] */
7443			/* ptr -> tag2 -> int */
7444			BTF_TYPE_TAG_ENC(NAME_NTH(2), 1),		/* [5] */
7445			BTF_PTR_ENC(5),					/* [6] */
7446			BTF_END_RAW,
7447		},
7448		BTF_STR_SEC("\0tag1\0tag2"),
7449	},
7450},
7451{
7452	.descr = "dedup: btf_type_tag #3",
7453	.input = {
7454		.raw_types = {
7455			/* ptr -> tag2 -> tag1 -> int */
7456			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7457			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7458			BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),		/* [3] */
7459			BTF_PTR_ENC(3),					/* [4] */
7460			/* ptr -> tag1 -> tag2 -> int */
7461			BTF_TYPE_TAG_ENC(NAME_NTH(2), 1),		/* [5] */
7462			BTF_TYPE_TAG_ENC(NAME_NTH(1), 5),		/* [6] */
7463			BTF_PTR_ENC(6),					/* [7] */
7464			BTF_END_RAW,
7465		},
7466		BTF_STR_SEC("\0tag1\0tag2"),
7467	},
7468	.expect = {
7469		.raw_types = {
7470			/* ptr -> tag2 -> tag1 -> int */
7471			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7472			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7473			BTF_TYPE_TAG_ENC(NAME_NTH(2), 2),		/* [3] */
7474			BTF_PTR_ENC(3),					/* [4] */
7475			/* ptr -> tag1 -> tag2 -> int */
7476			BTF_TYPE_TAG_ENC(NAME_NTH(2), 1),		/* [5] */
7477			BTF_TYPE_TAG_ENC(NAME_NTH(1), 5),		/* [6] */
7478			BTF_PTR_ENC(6),					/* [7] */
7479			BTF_END_RAW,
7480		},
7481		BTF_STR_SEC("\0tag1\0tag2"),
7482	},
7483},
7484{
7485	.descr = "dedup: btf_type_tag #4",
7486	.input = {
7487		.raw_types = {
7488			/* ptr -> tag1 -> int */
7489			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7490			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7491			BTF_PTR_ENC(2),					/* [3] */
7492			/* ptr -> tag1 -> long */
7493			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 64, 8),	/* [4] */
7494			BTF_TYPE_TAG_ENC(NAME_NTH(1), 4),		/* [5] */
7495			BTF_PTR_ENC(5),					/* [6] */
7496			BTF_END_RAW,
7497		},
7498		BTF_STR_SEC("\0tag1"),
7499	},
7500	.expect = {
7501		.raw_types = {
7502			/* ptr -> tag1 -> int */
7503			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),	/* [1] */
7504			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),		/* [2] */
7505			BTF_PTR_ENC(2),					/* [3] */
7506			/* ptr -> tag1 -> long */
7507			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 64, 8),	/* [4] */
7508			BTF_TYPE_TAG_ENC(NAME_NTH(1), 4),		/* [5] */
7509			BTF_PTR_ENC(5),					/* [6] */
7510			BTF_END_RAW,
7511		},
7512		BTF_STR_SEC("\0tag1"),
7513	},
7514},
7515{
7516	.descr = "dedup: btf_type_tag #5, struct",
7517	.input = {
7518		.raw_types = {
7519			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),				/* [1] */
7520			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),					/* [2] */
7521			BTF_TYPE_ENC(NAME_NTH(2), BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 1), 4),	/* [3] */
7522			BTF_MEMBER_ENC(NAME_NTH(3), 2, BTF_MEMBER_OFFSET(0, 0)),
7523			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),					/* [4] */
7524			BTF_TYPE_ENC(NAME_NTH(2), BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 1), 4),	/* [5] */
7525			BTF_MEMBER_ENC(NAME_NTH(3), 4, BTF_MEMBER_OFFSET(0, 0)),
7526			BTF_END_RAW,
7527		},
7528		BTF_STR_SEC("\0tag1\0t\0m"),
7529	},
7530	.expect = {
7531		.raw_types = {
7532			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),				/* [1] */
7533			BTF_TYPE_TAG_ENC(NAME_NTH(1), 1),					/* [2] */
7534			BTF_TYPE_ENC(NAME_NTH(2), BTF_INFO_ENC(BTF_KIND_STRUCT, 1, 1), 4),	/* [3] */
7535			BTF_MEMBER_ENC(NAME_NTH(3), 2, BTF_MEMBER_OFFSET(0, 0)),
7536			BTF_END_RAW,
7537		},
7538		BTF_STR_SEC("\0tag1\0t\0m"),
7539	},
7540},
7541{
7542	.descr = "dedup: enum64, standalone",
7543	.input = {
7544		.raw_types = {
7545			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
7546				BTF_ENUM64_ENC(NAME_NTH(2), 1, 123),
7547			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
7548				BTF_ENUM64_ENC(NAME_NTH(2), 1, 123),
7549			BTF_END_RAW,
7550		},
7551		BTF_STR_SEC("\0e1\0e1_val"),
7552	},
7553	.expect = {
7554		.raw_types = {
7555			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
7556				BTF_ENUM64_ENC(NAME_NTH(2), 1, 123),
7557			BTF_END_RAW,
7558		},
7559		BTF_STR_SEC("\0e1\0e1_val"),
7560	},
7561},
7562{
7563	.descr = "dedup: enum64, fwd resolution",
7564	.input = {
7565		.raw_types = {
7566			/* [1] fwd enum64 'e1' before full enum */
7567			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 0), 8),
7568			/* [2] full enum64 'e1' after fwd */
7569			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
7570				BTF_ENUM64_ENC(NAME_NTH(2), 1, 123),
7571			/* [3] full enum64 'e2' before fwd */
7572			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
7573				BTF_ENUM64_ENC(NAME_NTH(4), 0, 456),
7574			/* [4] fwd enum64 'e2' after full enum */
7575			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 0), 8),
7576			/* [5] incompatible full enum64 with different value */
7577			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
7578				BTF_ENUM64_ENC(NAME_NTH(2), 0, 321),
7579			BTF_END_RAW,
7580		},
7581		BTF_STR_SEC("\0e1\0e1_val\0e2\0e2_val"),
7582	},
7583	.expect = {
7584		.raw_types = {
7585			/* [1] full enum64 'e1' */
7586			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
7587				BTF_ENUM64_ENC(NAME_NTH(2), 1, 123),
7588			/* [2] full enum64 'e2' */
7589			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
7590				BTF_ENUM64_ENC(NAME_NTH(4), 0, 456),
7591			/* [3] incompatible full enum64 with different value */
7592			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
7593				BTF_ENUM64_ENC(NAME_NTH(2), 0, 321),
7594			BTF_END_RAW,
7595		},
7596		BTF_STR_SEC("\0e1\0e1_val\0e2\0e2_val"),
7597	},
7598},
7599{
7600	.descr = "dedup: enum and enum64, no dedup",
7601	.input = {
7602		.raw_types = {
7603			/* [1] enum 'e1' */
7604			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7605				BTF_ENUM_ENC(NAME_NTH(2), 1),
7606			/* [2] enum64 'e1' */
7607			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 4),
7608				BTF_ENUM64_ENC(NAME_NTH(2), 1, 0),
7609			BTF_END_RAW,
7610		},
7611		BTF_STR_SEC("\0e1\0e1_val"),
7612	},
7613	.expect = {
7614		.raw_types = {
7615			/* [1] enum 'e1' */
7616			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7617				BTF_ENUM_ENC(NAME_NTH(2), 1),
7618			/* [2] enum64 'e1' */
7619			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 4),
7620				BTF_ENUM64_ENC(NAME_NTH(2), 1, 0),
7621			BTF_END_RAW,
7622		},
7623		BTF_STR_SEC("\0e1\0e1_val"),
7624	},
7625},
7626{
7627	.descr = "dedup: enum of different size: no dedup",
7628	.input = {
7629		.raw_types = {
7630			/* [1] enum 'e1' */
7631			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7632				BTF_ENUM_ENC(NAME_NTH(2), 1),
7633			/* [2] enum 'e1' */
7634			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 2),
7635				BTF_ENUM_ENC(NAME_NTH(2), 1),
7636			BTF_END_RAW,
7637		},
7638		BTF_STR_SEC("\0e1\0e1_val"),
7639	},
7640	.expect = {
7641		.raw_types = {
7642			/* [1] enum 'e1' */
7643			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7644				BTF_ENUM_ENC(NAME_NTH(2), 1),
7645			/* [2] enum 'e1' */
7646			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 2),
7647				BTF_ENUM_ENC(NAME_NTH(2), 1),
7648			BTF_END_RAW,
7649		},
7650		BTF_STR_SEC("\0e1\0e1_val"),
7651	},
7652},
7653{
7654	.descr = "dedup: enum fwd to enum64",
7655	.input = {
7656		.raw_types = {
7657			/* [1] enum64 'e1' */
7658			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
7659				BTF_ENUM64_ENC(NAME_NTH(2), 1, 0),
7660			/* [2] enum 'e1' fwd */
7661			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 0), 4),
7662			/* [3] typedef enum 'e1' td */
7663			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), 2),
7664			BTF_END_RAW,
7665		},
7666		BTF_STR_SEC("\0e1\0e1_val\0td"),
7667	},
7668	.expect = {
7669		.raw_types = {
7670			/* [1] enum64 'e1' */
7671			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 1), 8),
7672				BTF_ENUM64_ENC(NAME_NTH(2), 1, 0),
7673			/* [2] typedef enum 'e1' td */
7674			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), 1),
7675			BTF_END_RAW,
7676		},
7677		BTF_STR_SEC("\0e1\0e1_val\0td"),
7678	},
7679},
7680{
7681	.descr = "dedup: enum64 fwd to enum",
7682	.input = {
7683		.raw_types = {
7684			/* [1] enum 'e1' */
7685			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7686				BTF_ENUM_ENC(NAME_NTH(2), 1),
7687			/* [2] enum64 'e1' fwd */
7688			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 0), 8),
7689			/* [3] typedef enum 'e1' td */
7690			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), 2),
7691			BTF_END_RAW,
7692		},
7693		BTF_STR_SEC("\0e1\0e1_val\0td"),
7694	},
7695	.expect = {
7696		.raw_types = {
7697			/* [1] enum 'e1' */
7698			BTF_TYPE_ENC(NAME_NTH(1), BTF_INFO_ENC(BTF_KIND_ENUM, 0, 1), 4),
7699				BTF_ENUM_ENC(NAME_NTH(2), 1),
7700			/* [2] typedef enum 'e1' td */
7701			BTF_TYPE_ENC(NAME_NTH(3), BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), 1),
7702			BTF_END_RAW,
7703		},
7704		BTF_STR_SEC("\0e1\0e1_val\0td"),
7705	},
7706},
7707{
7708	.descr = "dedup: standalone fwd declaration struct",
7709	/*
7710	 * Verify that CU1:foo and CU2:foo would be unified and that
7711	 * typedef/ptr would be updated to point to CU1:foo.
7712	 *
7713	 * // CU 1:
7714	 * struct foo { int x; };
7715	 *
7716	 * // CU 2:
7717	 * struct foo;
7718	 * typedef struct foo *foo_ptr;
7719	 */
7720	.input = {
7721		.raw_types = {
7722			/* CU 1 */
7723			BTF_STRUCT_ENC(NAME_NTH(1), 1, 4),             /* [1] */
7724			BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
7725			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */
7726			/* CU 2 */
7727			BTF_FWD_ENC(NAME_NTH(1), 0),                   /* [3] */
7728			BTF_PTR_ENC(3),                                /* [4] */
7729			BTF_TYPEDEF_ENC(NAME_NTH(3), 4),               /* [5] */
7730			BTF_END_RAW,
7731		},
7732		BTF_STR_SEC("\0foo\0x\0foo_ptr"),
7733	},
7734	.expect = {
7735		.raw_types = {
7736			BTF_STRUCT_ENC(NAME_NTH(1), 1, 4),             /* [1] */
7737			BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
7738			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */
7739			BTF_PTR_ENC(1),                                /* [3] */
7740			BTF_TYPEDEF_ENC(NAME_NTH(3), 3),               /* [4] */
7741			BTF_END_RAW,
7742		},
7743		BTF_STR_SEC("\0foo\0x\0foo_ptr"),
7744	},
7745},
7746{
7747	.descr = "dedup: standalone fwd declaration union",
7748	/*
7749	 * Verify that CU1:foo and CU2:foo would be unified and that
7750	 * typedef/ptr would be updated to point to CU1:foo.
7751	 * Same as "dedup: standalone fwd declaration struct" but for unions.
7752	 *
7753	 * // CU 1:
7754	 * union foo { int x; };
7755	 *
7756	 * // CU 2:
7757	 * union foo;
7758	 * typedef union foo *foo_ptr;
7759	 */
7760	.input = {
7761		.raw_types = {
7762			/* CU 1 */
7763			BTF_UNION_ENC(NAME_NTH(1), 1, 4),              /* [1] */
7764			BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
7765			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */
7766			/* CU 2 */
7767			BTF_FWD_ENC(NAME_TBD, 1),                      /* [3] */
7768			BTF_PTR_ENC(3),                                /* [4] */
7769			BTF_TYPEDEF_ENC(NAME_NTH(3), 4),               /* [5] */
7770			BTF_END_RAW,
7771		},
7772		BTF_STR_SEC("\0foo\0x\0foo_ptr"),
7773	},
7774	.expect = {
7775		.raw_types = {
7776			BTF_UNION_ENC(NAME_NTH(1), 1, 4),              /* [1] */
7777			BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
7778			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */
7779			BTF_PTR_ENC(1),                                /* [3] */
7780			BTF_TYPEDEF_ENC(NAME_NTH(3), 3),               /* [4] */
7781			BTF_END_RAW,
7782		},
7783		BTF_STR_SEC("\0foo\0x\0foo_ptr"),
7784	},
7785},
7786{
7787	.descr = "dedup: standalone fwd declaration wrong kind",
7788	/*
7789	 * Negative test for btf_dedup_resolve_fwds:
7790	 * - CU1:foo is a struct, C2:foo is a union, thus CU2:foo is not deduped;
7791	 * - typedef/ptr should remain unchanged as well.
7792	 *
7793	 * // CU 1:
7794	 * struct foo { int x; };
7795	 *
7796	 * // CU 2:
7797	 * union foo;
7798	 * typedef union foo *foo_ptr;
7799	 */
7800	.input = {
7801		.raw_types = {
7802			/* CU 1 */
7803			BTF_STRUCT_ENC(NAME_NTH(1), 1, 4),             /* [1] */
7804			BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
7805			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */
7806			/* CU 2 */
7807			BTF_FWD_ENC(NAME_NTH(3), 1),                   /* [3] */
7808			BTF_PTR_ENC(3),                                /* [4] */
7809			BTF_TYPEDEF_ENC(NAME_NTH(3), 4),               /* [5] */
7810			BTF_END_RAW,
7811		},
7812		BTF_STR_SEC("\0foo\0x\0foo_ptr"),
7813	},
7814	.expect = {
7815		.raw_types = {
7816			/* CU 1 */
7817			BTF_STRUCT_ENC(NAME_NTH(1), 1, 4),             /* [1] */
7818			BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
7819			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */
7820			/* CU 2 */
7821			BTF_FWD_ENC(NAME_NTH(3), 1),                   /* [3] */
7822			BTF_PTR_ENC(3),                                /* [4] */
7823			BTF_TYPEDEF_ENC(NAME_NTH(3), 4),               /* [5] */
7824			BTF_END_RAW,
7825		},
7826		BTF_STR_SEC("\0foo\0x\0foo_ptr"),
7827	},
7828},
7829{
7830	.descr = "dedup: standalone fwd declaration name conflict",
7831	/*
7832	 * Negative test for btf_dedup_resolve_fwds:
7833	 * - two candidates for CU2:foo dedup, thus it is unchanged;
7834	 * - typedef/ptr should remain unchanged as well.
7835	 *
7836	 * // CU 1:
7837	 * struct foo { int x; };
7838	 *
7839	 * // CU 2:
7840	 * struct foo;
7841	 * typedef struct foo *foo_ptr;
7842	 *
7843	 * // CU 3:
7844	 * struct foo { int x; int y; };
7845	 */
7846	.input = {
7847		.raw_types = {
7848			/* CU 1 */
7849			BTF_STRUCT_ENC(NAME_NTH(1), 1, 4),             /* [1] */
7850			BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
7851			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */
7852			/* CU 2 */
7853			BTF_FWD_ENC(NAME_NTH(1), 0),                   /* [3] */
7854			BTF_PTR_ENC(3),                                /* [4] */
7855			BTF_TYPEDEF_ENC(NAME_NTH(4), 4),               /* [5] */
7856			/* CU 3 */
7857			BTF_STRUCT_ENC(NAME_NTH(1), 2, 8),             /* [6] */
7858			BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
7859			BTF_MEMBER_ENC(NAME_NTH(3), 2, 0),
7860			BTF_END_RAW,
7861		},
7862		BTF_STR_SEC("\0foo\0x\0y\0foo_ptr"),
7863	},
7864	.expect = {
7865		.raw_types = {
7866			/* CU 1 */
7867			BTF_STRUCT_ENC(NAME_NTH(1), 1, 4),             /* [1] */
7868			BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
7869			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [2] */
7870			/* CU 2 */
7871			BTF_FWD_ENC(NAME_NTH(1), 0),                   /* [3] */
7872			BTF_PTR_ENC(3),                                /* [4] */
7873			BTF_TYPEDEF_ENC(NAME_NTH(4), 4),               /* [5] */
7874			/* CU 3 */
7875			BTF_STRUCT_ENC(NAME_NTH(1), 2, 8),             /* [6] */
7876			BTF_MEMBER_ENC(NAME_NTH(2), 2, 0),
7877			BTF_MEMBER_ENC(NAME_NTH(3), 2, 0),
7878			BTF_END_RAW,
7879		},
7880		BTF_STR_SEC("\0foo\0x\0y\0foo_ptr"),
7881	},
7882},
7883};
7884
7885static int btf_type_size(const struct btf_type *t)
7886{
7887	int base_size = sizeof(struct btf_type);
7888	__u16 vlen = BTF_INFO_VLEN(t->info);
7889	__u16 kind = BTF_INFO_KIND(t->info);
7890
7891	switch (kind) {
7892	case BTF_KIND_FWD:
7893	case BTF_KIND_CONST:
7894	case BTF_KIND_VOLATILE:
7895	case BTF_KIND_RESTRICT:
7896	case BTF_KIND_PTR:
7897	case BTF_KIND_TYPEDEF:
7898	case BTF_KIND_FUNC:
7899	case BTF_KIND_FLOAT:
7900	case BTF_KIND_TYPE_TAG:
7901		return base_size;
7902	case BTF_KIND_INT:
7903		return base_size + sizeof(__u32);
7904	case BTF_KIND_ENUM:
7905		return base_size + vlen * sizeof(struct btf_enum);
7906	case BTF_KIND_ENUM64:
7907		return base_size + vlen * sizeof(struct btf_enum64);
7908	case BTF_KIND_ARRAY:
7909		return base_size + sizeof(struct btf_array);
7910	case BTF_KIND_STRUCT:
7911	case BTF_KIND_UNION:
7912		return base_size + vlen * sizeof(struct btf_member);
7913	case BTF_KIND_FUNC_PROTO:
7914		return base_size + vlen * sizeof(struct btf_param);
7915	case BTF_KIND_VAR:
7916		return base_size + sizeof(struct btf_var);
7917	case BTF_KIND_DATASEC:
7918		return base_size + vlen * sizeof(struct btf_var_secinfo);
7919	case BTF_KIND_DECL_TAG:
7920		return base_size + sizeof(struct btf_decl_tag);
7921	default:
7922		fprintf(stderr, "Unsupported BTF_KIND:%u\n", kind);
7923		return -EINVAL;
7924	}
7925}
7926
7927static void dump_btf_strings(const char *strs, __u32 len)
7928{
7929	const char *cur = strs;
7930	int i = 0;
7931
7932	while (cur < strs + len) {
7933		fprintf(stderr, "string #%d: '%s'\n", i, cur);
7934		cur += strlen(cur) + 1;
7935		i++;
7936	}
7937}
7938
7939static void do_test_dedup(unsigned int test_num)
7940{
7941	struct btf_dedup_test *test = &dedup_tests[test_num - 1];
7942	__u32 test_nr_types, expect_nr_types, test_btf_size, expect_btf_size;
7943	const struct btf_header *test_hdr, *expect_hdr;
7944	struct btf *test_btf = NULL, *expect_btf = NULL;
7945	const void *test_btf_data, *expect_btf_data;
7946	const char *ret_test_next_str, *ret_expect_next_str;
7947	const char *test_strs, *expect_strs;
7948	const char *test_str_cur;
7949	const char *expect_str_cur, *expect_str_end;
7950	unsigned int raw_btf_size;
7951	void *raw_btf;
7952	int err = 0, i;
7953
7954	if (!test__start_subtest(test->descr))
7955		return;
7956
7957	raw_btf = btf_raw_create(&hdr_tmpl, test->input.raw_types,
7958				 test->input.str_sec, test->input.str_sec_size,
7959				 &raw_btf_size, &ret_test_next_str);
7960	if (!raw_btf)
7961		return;
7962
7963	test_btf = btf__new((__u8 *)raw_btf, raw_btf_size);
7964	err = libbpf_get_error(test_btf);
7965	free(raw_btf);
7966	if (CHECK(err, "invalid test_btf errno:%d", err)) {
7967		err = -1;
7968		goto done;
7969	}
7970
7971	raw_btf = btf_raw_create(&hdr_tmpl, test->expect.raw_types,
7972				 test->expect.str_sec,
7973				 test->expect.str_sec_size,
7974				 &raw_btf_size, &ret_expect_next_str);
7975	if (!raw_btf)
7976		return;
7977	expect_btf = btf__new((__u8 *)raw_btf, raw_btf_size);
7978	err = libbpf_get_error(expect_btf);
7979	free(raw_btf);
7980	if (CHECK(err, "invalid expect_btf errno:%d", err)) {
7981		err = -1;
7982		goto done;
7983	}
7984
7985	test->opts.sz = sizeof(test->opts);
7986	err = btf__dedup(test_btf, &test->opts);
7987	if (CHECK(err, "btf_dedup failed errno:%d", err)) {
7988		err = -1;
7989		goto done;
7990	}
7991
7992	test_btf_data = btf__raw_data(test_btf, &test_btf_size);
7993	expect_btf_data = btf__raw_data(expect_btf, &expect_btf_size);
7994	if (CHECK(test_btf_size != expect_btf_size,
7995		  "test_btf_size:%u != expect_btf_size:%u",
7996		  test_btf_size, expect_btf_size)) {
7997		err = -1;
7998		goto done;
7999	}
8000
8001	test_hdr = test_btf_data;
8002	test_strs = test_btf_data + sizeof(*test_hdr) + test_hdr->str_off;
8003	expect_hdr = expect_btf_data;
8004	expect_strs = expect_btf_data + sizeof(*test_hdr) + expect_hdr->str_off;
8005	if (CHECK(test_hdr->str_len != expect_hdr->str_len,
8006		  "test_hdr->str_len:%u != expect_hdr->str_len:%u",
8007		  test_hdr->str_len, expect_hdr->str_len)) {
8008		fprintf(stderr, "\ntest strings:\n");
8009		dump_btf_strings(test_strs, test_hdr->str_len);
8010		fprintf(stderr, "\nexpected strings:\n");
8011		dump_btf_strings(expect_strs, expect_hdr->str_len);
8012		err = -1;
8013		goto done;
8014	}
8015
8016	expect_str_cur = expect_strs;
8017	expect_str_end = expect_strs + expect_hdr->str_len;
8018	while (expect_str_cur < expect_str_end) {
8019		size_t test_len, expect_len;
8020		int off;
8021
8022		off = btf__find_str(test_btf, expect_str_cur);
8023		if (CHECK(off < 0, "exp str '%s' not found: %d\n", expect_str_cur, off)) {
8024			err = -1;
8025			goto done;
8026		}
8027		test_str_cur = btf__str_by_offset(test_btf, off);
8028
8029		test_len = strlen(test_str_cur);
8030		expect_len = strlen(expect_str_cur);
8031		if (CHECK(test_len != expect_len,
8032			  "test_len:%zu != expect_len:%zu "
8033			  "(test_str:%s, expect_str:%s)",
8034			  test_len, expect_len, test_str_cur, expect_str_cur)) {
8035			err = -1;
8036			goto done;
8037		}
8038		if (CHECK(strcmp(test_str_cur, expect_str_cur),
8039			  "test_str:%s != expect_str:%s",
8040			  test_str_cur, expect_str_cur)) {
8041			err = -1;
8042			goto done;
8043		}
8044		expect_str_cur += expect_len + 1;
8045	}
8046
8047	test_nr_types = btf__type_cnt(test_btf);
8048	expect_nr_types = btf__type_cnt(expect_btf);
8049	if (CHECK(test_nr_types != expect_nr_types,
8050		  "test_nr_types:%u != expect_nr_types:%u",
8051		  test_nr_types, expect_nr_types)) {
8052		err = -1;
8053		goto done;
8054	}
8055
8056	for (i = 1; i < test_nr_types; i++) {
8057		const struct btf_type *test_type, *expect_type;
8058		int test_size, expect_size;
8059
8060		test_type = btf__type_by_id(test_btf, i);
8061		expect_type = btf__type_by_id(expect_btf, i);
8062		test_size = btf_type_size(test_type);
8063		expect_size = btf_type_size(expect_type);
8064
8065		if (CHECK(test_size != expect_size,
8066			  "type #%d: test_size:%d != expect_size:%u",
8067			  i, test_size, expect_size)) {
8068			err = -1;
8069			goto done;
8070		}
8071		if (CHECK(btf_kind(test_type) != btf_kind(expect_type),
8072			  "type %d kind: exp %d != got %u\n",
8073			  i, btf_kind(expect_type), btf_kind(test_type))) {
8074			err = -1;
8075			goto done;
8076		}
8077		if (CHECK(test_type->info != expect_type->info,
8078			  "type %d info: exp %d != got %u\n",
8079			  i, expect_type->info, test_type->info)) {
8080			err = -1;
8081			goto done;
8082		}
8083		if (CHECK(test_type->size != expect_type->size,
8084			  "type %d size/type: exp %d != got %u\n",
8085			  i, expect_type->size, test_type->size)) {
8086			err = -1;
8087			goto done;
8088		}
8089	}
8090
8091done:
8092	btf__free(test_btf);
8093	btf__free(expect_btf);
8094}
8095
8096void test_btf(void)
8097{
8098	int i;
8099
8100	always_log = env.verbosity > VERBOSE_NONE;
8101
8102	for (i = 1; i <= ARRAY_SIZE(raw_tests); i++)
8103		do_test_raw(i);
8104	for (i = 1; i <= ARRAY_SIZE(get_info_tests); i++)
8105		do_test_get_info(i);
8106	for (i = 1; i <= ARRAY_SIZE(file_tests); i++)
8107		do_test_file(i);
8108	for (i = 1; i <= ARRAY_SIZE(info_raw_tests); i++)
8109		do_test_info_raw(i);
8110	for (i = 1; i <= ARRAY_SIZE(dedup_tests); i++)
8111		do_test_dedup(i);
8112	test_pprint();
8113}