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