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