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