1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */ 3 #include <linux/capability.h> 4 #include <linux/err.h> 5 #include <stdlib.h> 6 #include <test_progs.h> 7 #include <bpf/btf.h> 8 9 #include "autoconf_helper.h" 10 #include "disasm_helpers.h" 11 #include "unpriv_helpers.h" 12 #include "cap_helpers.h" 13 #include "jit_disasm_helpers.h" 14 15 static inline const char *str_has_pfx(const char *str, const char *pfx) 16 { 17 size_t len = strlen(pfx); 18 19 return strncmp(str, pfx, len) == 0 ? str + len : NULL; 20 } 21 22 #define TEST_LOADER_LOG_BUF_SZ 2097152 23 24 25 /* Warning: duplicated in bpf_misc.h */ 26 #define POINTER_VALUE 0xbadcafe 27 #define TEST_DATA_LEN 64 28 29 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 30 #define EFFICIENT_UNALIGNED_ACCESS 1 31 #else 32 #define EFFICIENT_UNALIGNED_ACCESS 0 33 #endif 34 35 static int sysctl_unpriv_disabled = -1; 36 37 enum mode { 38 PRIV = 1, 39 UNPRIV = 2 40 }; 41 42 enum load_mode { 43 JITED = 1 << 0, 44 NO_JITED = 1 << 1, 45 }; 46 47 struct test_subspec { 48 char *name; 49 char *description; 50 bool expect_failure; 51 struct expected_msgs expect_msgs; 52 struct expected_msgs expect_xlated; 53 struct expected_msgs jited; 54 struct expected_msgs stderr; 55 struct expected_msgs stdout; 56 int retval; 57 bool execute; 58 __u64 caps; 59 }; 60 61 struct test_spec { 62 const char *prog_name; 63 struct test_subspec priv; 64 struct test_subspec unpriv; 65 const char *btf_custom_path; 66 const char *btf_custom_func_path; 67 int log_level; 68 int prog_flags; 69 int mode_mask; 70 int arch_mask; 71 int load_mask; 72 int linear_sz; 73 bool auxiliary; 74 bool valid; 75 }; 76 77 static int tester_init(struct test_loader *tester) 78 { 79 if (!tester->log_buf) { 80 tester->log_buf_sz = TEST_LOADER_LOG_BUF_SZ; 81 tester->log_buf = calloc(tester->log_buf_sz, 1); 82 if (!ASSERT_OK_PTR(tester->log_buf, "tester_log_buf")) 83 return -ENOMEM; 84 } 85 86 return 0; 87 } 88 89 void test_loader_fini(struct test_loader *tester) 90 { 91 if (!tester) 92 return; 93 94 free(tester->log_buf); 95 } 96 97 void free_msgs(struct expected_msgs *msgs) 98 { 99 int i; 100 101 for (i = 0; i < msgs->cnt; i++) 102 if (msgs->patterns[i].is_regex) 103 regfree(&msgs->patterns[i].regex); 104 free(msgs->patterns); 105 msgs->patterns = NULL; 106 msgs->cnt = 0; 107 } 108 109 static void free_test_spec(struct test_spec *spec) 110 { 111 /* Deallocate expect_msgs arrays. */ 112 free_msgs(&spec->priv.expect_msgs); 113 free_msgs(&spec->unpriv.expect_msgs); 114 free_msgs(&spec->priv.expect_xlated); 115 free_msgs(&spec->unpriv.expect_xlated); 116 free_msgs(&spec->priv.jited); 117 free_msgs(&spec->unpriv.jited); 118 free_msgs(&spec->unpriv.stderr); 119 free_msgs(&spec->priv.stderr); 120 free_msgs(&spec->unpriv.stdout); 121 free_msgs(&spec->priv.stdout); 122 123 free(spec->priv.name); 124 free(spec->priv.description); 125 free(spec->unpriv.name); 126 free(spec->unpriv.description); 127 spec->priv.name = NULL; 128 spec->priv.description = NULL; 129 spec->unpriv.name = NULL; 130 spec->unpriv.description = NULL; 131 } 132 133 /* Compiles regular expression matching pattern. 134 * Pattern has a special syntax: 135 * 136 * pattern := (<verbatim text> | regex)* 137 * regex := "{{" <posix extended regular expression> "}}" 138 * 139 * In other words, pattern is a verbatim text with inclusion 140 * of regular expressions enclosed in "{{" "}}" pairs. 141 * For example, pattern "foo{{[0-9]+}}" matches strings like 142 * "foo0", "foo007", etc. 143 */ 144 static int compile_regex(const char *pattern, regex_t *regex) 145 { 146 char err_buf[256], buf[256] = {}, *ptr, *buf_end; 147 const char *original_pattern = pattern, *next; 148 bool in_regex = false; 149 int err; 150 151 buf_end = buf + sizeof(buf); 152 ptr = buf; 153 while (*pattern && ptr < buf_end - 2) { 154 if (!in_regex && (next = str_has_pfx(pattern, "{{"))) { 155 in_regex = true; 156 pattern = next; 157 continue; 158 } 159 if (in_regex && (next = str_has_pfx(pattern, "}}"))) { 160 in_regex = false; 161 pattern = next; 162 continue; 163 } 164 if (in_regex) { 165 *ptr++ = *pattern++; 166 continue; 167 } 168 /* list of characters that need escaping for extended posix regex */ 169 if (strchr(".[]\\()*+?{}|^$", *pattern)) { 170 *ptr++ = '\\'; 171 *ptr++ = *pattern++; 172 continue; 173 } 174 *ptr++ = *pattern++; 175 } 176 if (*pattern) { 177 PRINT_FAIL("Regexp too long: '%s'\n", original_pattern); 178 return -EINVAL; 179 } 180 if (in_regex) { 181 PRINT_FAIL("Regexp has open '{{' but no closing '}}': '%s'\n", original_pattern); 182 return -EINVAL; 183 } 184 err = regcomp(regex, buf, REG_EXTENDED | REG_NEWLINE); 185 if (err != 0) { 186 regerror(err, regex, err_buf, sizeof(err_buf)); 187 PRINT_FAIL("Regexp compilation error in '%s': '%s'\n", buf, err_buf); 188 return -EINVAL; 189 } 190 return 0; 191 } 192 193 static int __push_msg(const char *pattern, bool on_next_line, bool negative, 194 struct expected_msgs *msgs) 195 { 196 struct expect_msg *msg; 197 void *tmp; 198 int err; 199 200 tmp = realloc(msgs->patterns, 201 (1 + msgs->cnt) * sizeof(struct expect_msg)); 202 if (!tmp) { 203 ASSERT_FAIL("failed to realloc memory for messages\n"); 204 return -ENOMEM; 205 } 206 msgs->patterns = tmp; 207 msg = &msgs->patterns[msgs->cnt]; 208 msg->on_next_line = on_next_line; 209 msg->substr = pattern; 210 msg->negative = negative; 211 msg->is_regex = false; 212 if (strstr(pattern, "{{")) { 213 err = compile_regex(pattern, &msg->regex); 214 if (err) 215 return err; 216 msg->is_regex = true; 217 } 218 msgs->cnt += 1; 219 return 0; 220 } 221 222 static int clone_msgs(struct expected_msgs *from, struct expected_msgs *to) 223 { 224 struct expect_msg *msg; 225 int i, err; 226 227 for (i = 0; i < from->cnt; i++) { 228 msg = &from->patterns[i]; 229 err = __push_msg(msg->substr, msg->on_next_line, msg->negative, to); 230 if (err) 231 return err; 232 } 233 return 0; 234 } 235 236 static int push_msg(const char *substr, bool negative, struct expected_msgs *msgs) 237 { 238 return __push_msg(substr, false, negative, msgs); 239 } 240 241 static int push_disasm_msg(const char *regex_str, bool *on_next_line, struct expected_msgs *msgs) 242 { 243 int err; 244 245 if (strcmp(regex_str, "...") == 0) { 246 *on_next_line = false; 247 return 0; 248 } 249 err = __push_msg(regex_str, *on_next_line, false, msgs); 250 if (err) 251 return err; 252 *on_next_line = true; 253 return 0; 254 } 255 256 static int parse_int(const char *str, int *val, const char *name) 257 { 258 char *end; 259 long tmp; 260 261 errno = 0; 262 if (str_has_pfx(str, "0x")) 263 tmp = strtol(str + 2, &end, 16); 264 else 265 tmp = strtol(str, &end, 10); 266 if (errno || end[0] != '\0') { 267 PRINT_FAIL("failed to parse %s from '%s'\n", name, str); 268 return -EINVAL; 269 } 270 *val = tmp; 271 return 0; 272 } 273 274 static int parse_caps(const char *str, __u64 *val, const char *name) 275 { 276 int cap_flag = 0; 277 char *token = NULL, *saveptr = NULL; 278 279 char *str_cpy = strdup(str); 280 if (str_cpy == NULL) { 281 PRINT_FAIL("Memory allocation failed\n"); 282 return -EINVAL; 283 } 284 285 token = strtok_r(str_cpy, "|", &saveptr); 286 while (token != NULL) { 287 errno = 0; 288 if (!strncmp("CAP_", token, sizeof("CAP_") - 1)) { 289 PRINT_FAIL("define %s constant in bpf_misc.h, failed to parse caps\n", token); 290 return -EINVAL; 291 } 292 cap_flag = strtol(token, NULL, 10); 293 if (!cap_flag || errno) { 294 PRINT_FAIL("failed to parse caps %s\n", name); 295 return -EINVAL; 296 } 297 *val |= (1ULL << cap_flag); 298 token = strtok_r(NULL, "|", &saveptr); 299 } 300 301 free(str_cpy); 302 return 0; 303 } 304 305 static int parse_retval(const char *str, int *val, const char *name) 306 { 307 /* 308 * INT_MIN is defined as (-INT_MAX -1), i.e. it doesn't expand to a 309 * single int and cannot be parsed with strtol, so we handle it 310 * separately here. In addition, it expands to different expressions in 311 * different compilers so we use a prefixed _INT_MIN instead. 312 */ 313 if (strcmp(str, "_INT_MIN") == 0) { 314 *val = INT_MIN; 315 return 0; 316 } 317 318 return parse_int(str, val, name); 319 } 320 321 static void update_flags(int *flags, int flag, bool clear) 322 { 323 if (clear) 324 *flags &= ~flag; 325 else 326 *flags |= flag; 327 } 328 329 static const char *skip_decl_tag_pfx(const char *s) 330 { 331 int n = 0; 332 333 if (sscanf(s, "comment:%*d:%n", &n) < 0 || !n) 334 return NULL; 335 return s + n; 336 } 337 338 static int compare_decl_tags(const void *a, const void *b) 339 { 340 return strverscmp(*(const char **)a, *(const char **)b); 341 } 342 343 /* 344 * Compilers don't guarantee order in which BTF attributes would be generated, 345 * while order is important for test tags like __msg. 346 * Each test tag has the following prefix: "comment:" __COUNTER__, 347 * when sorted using strverscmp this gives same order as in the original C code. 348 */ 349 static const char **collect_decl_tags(struct btf *btf, int id, int *cnt) 350 { 351 const char **tmp, **tags = NULL; 352 const struct btf_type *t; 353 int i; 354 355 *cnt = 0; 356 for (i = 1; i < btf__type_cnt(btf); i++) { 357 t = btf__type_by_id(btf, i); 358 if (!btf_is_decl_tag(t) || t->type != id || btf_decl_tag(t)->component_idx != -1) 359 continue; 360 tmp = realloc(tags, (*cnt + 1) * sizeof(*tags)); 361 if (!tmp) { 362 free(tags); 363 return ERR_PTR(-ENOMEM); 364 } 365 tags = tmp; 366 tags[(*cnt)++] = btf__str_by_offset(btf, t->name_off); 367 } 368 369 if (*cnt) 370 qsort(tags, *cnt, sizeof(*tags), compare_decl_tags); 371 return tags; 372 } 373 374 enum arch { 375 ARCH_UNKNOWN = 0x1, 376 ARCH_X86_64 = 0x2, 377 ARCH_ARM64 = 0x4, 378 ARCH_RISCV64 = 0x8, 379 ARCH_S390X = 0x10, 380 }; 381 382 static int get_current_arch(void) 383 { 384 #if defined(__x86_64__) 385 return ARCH_X86_64; 386 #elif defined(__aarch64__) 387 return ARCH_ARM64; 388 #elif defined(__riscv) && __riscv_xlen == 64 389 return ARCH_RISCV64; 390 #elif defined(__s390x__) 391 return ARCH_S390X; 392 #endif 393 return ARCH_UNKNOWN; 394 } 395 396 /* Uses btf_decl_tag attributes to describe the expected test 397 * behavior, see bpf_misc.h for detailed description of each attribute 398 * and attribute combinations. 399 */ 400 static int parse_test_spec(struct test_loader *tester, 401 struct bpf_object *obj, 402 struct bpf_program *prog, 403 struct test_spec *spec) 404 { 405 const char *description = NULL; 406 bool has_unpriv_result = false; 407 bool has_unpriv_retval = false; 408 bool unpriv_xlated_on_next_line = true; 409 bool xlated_on_next_line = true; 410 bool unpriv_jit_on_next_line; 411 bool jit_on_next_line; 412 bool stderr_on_next_line = true; 413 bool unpriv_stderr_on_next_line = true; 414 bool stdout_on_next_line = true; 415 bool unpriv_stdout_on_next_line = true; 416 bool collect_jit = false; 417 const char **tags = NULL; 418 int func_id, i, nr_tags; 419 int err = 0; 420 u32 arch_mask = 0; 421 u32 load_mask = 0; 422 struct btf *btf; 423 enum arch arch; 424 425 memset(spec, 0, sizeof(*spec)); 426 427 spec->prog_name = bpf_program__name(prog); 428 spec->prog_flags = testing_prog_flags(); 429 430 btf = bpf_object__btf(obj); 431 if (!btf) { 432 ASSERT_FAIL("BPF object has no BTF"); 433 return -EINVAL; 434 } 435 436 func_id = btf__find_by_name_kind(btf, spec->prog_name, BTF_KIND_FUNC); 437 if (func_id < 0) { 438 ASSERT_FAIL("failed to find FUNC BTF type for '%s'", spec->prog_name); 439 return -EINVAL; 440 } 441 442 tags = collect_decl_tags(btf, func_id, &nr_tags); 443 if (IS_ERR(tags)) 444 return PTR_ERR(tags); 445 446 for (i = 0; i < nr_tags; i++) { 447 const char *s, *val, *msg; 448 bool clear; 449 int flags; 450 451 s = skip_decl_tag_pfx(tags[i]); 452 if (!s) 453 continue; 454 if ((val = str_has_pfx(s, "test_description="))) { 455 description = val; 456 } else if (strcmp(s, "test_expect_failure") == 0) { 457 spec->priv.expect_failure = true; 458 spec->mode_mask |= PRIV; 459 } else if (strcmp(s, "test_expect_success") == 0) { 460 spec->priv.expect_failure = false; 461 spec->mode_mask |= PRIV; 462 } else if (strcmp(s, "test_expect_failure_unpriv") == 0) { 463 spec->unpriv.expect_failure = true; 464 spec->mode_mask |= UNPRIV; 465 has_unpriv_result = true; 466 } else if (strcmp(s, "test_expect_success_unpriv") == 0) { 467 spec->unpriv.expect_failure = false; 468 spec->mode_mask |= UNPRIV; 469 has_unpriv_result = true; 470 } else if (strcmp(s, "test_auxiliary") == 0) { 471 spec->auxiliary = true; 472 spec->mode_mask |= PRIV; 473 } else if (strcmp(s, "test_auxiliary_unpriv") == 0) { 474 spec->auxiliary = true; 475 spec->mode_mask |= UNPRIV; 476 } else if ((msg = str_has_pfx(s, "test_expect_msg="))) { 477 err = push_msg(msg, false, &spec->priv.expect_msgs); 478 if (err) 479 goto cleanup; 480 spec->mode_mask |= PRIV; 481 } else if ((msg = str_has_pfx(s, "test_expect_not_msg="))) { 482 err = push_msg(msg, true, &spec->priv.expect_msgs); 483 if (err) 484 goto cleanup; 485 spec->mode_mask |= PRIV; 486 } else if ((msg = str_has_pfx(s, "test_expect_msg_unpriv="))) { 487 err = push_msg(msg, false, &spec->unpriv.expect_msgs); 488 if (err) 489 goto cleanup; 490 spec->mode_mask |= UNPRIV; 491 } else if ((msg = str_has_pfx(s, "test_expect_not_msg_unpriv="))) { 492 err = push_msg(msg, true, &spec->unpriv.expect_msgs); 493 if (err) 494 goto cleanup; 495 spec->mode_mask |= UNPRIV; 496 } else if ((msg = str_has_pfx(s, "test_jited="))) { 497 if (arch_mask == 0) { 498 PRINT_FAIL("__jited used before __arch_*"); 499 goto cleanup; 500 } 501 if (collect_jit) { 502 err = push_disasm_msg(msg, &jit_on_next_line, 503 &spec->priv.jited); 504 if (err) 505 goto cleanup; 506 spec->mode_mask |= PRIV; 507 } 508 } else if ((msg = str_has_pfx(s, "test_jited_unpriv="))) { 509 if (arch_mask == 0) { 510 PRINT_FAIL("__unpriv_jited used before __arch_*"); 511 goto cleanup; 512 } 513 if (collect_jit) { 514 err = push_disasm_msg(msg, &unpriv_jit_on_next_line, 515 &spec->unpriv.jited); 516 if (err) 517 goto cleanup; 518 spec->mode_mask |= UNPRIV; 519 } 520 } else if ((msg = str_has_pfx(s, "test_expect_xlated="))) { 521 err = push_disasm_msg(msg, &xlated_on_next_line, 522 &spec->priv.expect_xlated); 523 if (err) 524 goto cleanup; 525 spec->mode_mask |= PRIV; 526 } else if ((msg = str_has_pfx(s, "test_expect_xlated_unpriv="))) { 527 err = push_disasm_msg(msg, &unpriv_xlated_on_next_line, 528 &spec->unpriv.expect_xlated); 529 if (err) 530 goto cleanup; 531 spec->mode_mask |= UNPRIV; 532 } else if ((val = str_has_pfx(s, "test_retval="))) { 533 err = parse_retval(val, &spec->priv.retval, "__retval"); 534 if (err) 535 goto cleanup; 536 spec->priv.execute = true; 537 spec->mode_mask |= PRIV; 538 } else if ((val = str_has_pfx(s, "test_retval_unpriv="))) { 539 err = parse_retval(val, &spec->unpriv.retval, "__retval_unpriv"); 540 if (err) 541 goto cleanup; 542 spec->mode_mask |= UNPRIV; 543 spec->unpriv.execute = true; 544 has_unpriv_retval = true; 545 } else if ((val = str_has_pfx(s, "test_log_level="))) { 546 err = parse_int(val, &spec->log_level, "test log level"); 547 if (err) 548 goto cleanup; 549 } else if ((val = str_has_pfx(s, "test_prog_flags="))) { 550 clear = val[0] == '!'; 551 if (clear) 552 val++; 553 554 if (strcmp(val, "BPF_F_STRICT_ALIGNMENT") == 0) { 555 update_flags(&spec->prog_flags, BPF_F_STRICT_ALIGNMENT, clear); 556 } else if (strcmp(val, "BPF_F_ANY_ALIGNMENT") == 0) { 557 update_flags(&spec->prog_flags, BPF_F_ANY_ALIGNMENT, clear); 558 } else if (strcmp(val, "BPF_F_TEST_RND_HI32") == 0) { 559 update_flags(&spec->prog_flags, BPF_F_TEST_RND_HI32, clear); 560 } else if (strcmp(val, "BPF_F_TEST_STATE_FREQ") == 0) { 561 update_flags(&spec->prog_flags, BPF_F_TEST_STATE_FREQ, clear); 562 } else if (strcmp(val, "BPF_F_SLEEPABLE") == 0) { 563 update_flags(&spec->prog_flags, BPF_F_SLEEPABLE, clear); 564 } else if (strcmp(val, "BPF_F_XDP_HAS_FRAGS") == 0) { 565 update_flags(&spec->prog_flags, BPF_F_XDP_HAS_FRAGS, clear); 566 } else if (strcmp(val, "BPF_F_TEST_REG_INVARIANTS") == 0) { 567 update_flags(&spec->prog_flags, BPF_F_TEST_REG_INVARIANTS, clear); 568 } else /* assume numeric value */ { 569 err = parse_int(val, &flags, "test prog flags"); 570 if (err) 571 goto cleanup; 572 update_flags(&spec->prog_flags, flags, clear); 573 } 574 } else if ((val = str_has_pfx(s, "test_arch="))) { 575 if (strcmp(val, "X86_64") == 0) { 576 arch = ARCH_X86_64; 577 } else if (strcmp(val, "ARM64") == 0) { 578 arch = ARCH_ARM64; 579 } else if (strcmp(val, "RISCV64") == 0) { 580 arch = ARCH_RISCV64; 581 } else if (strcmp(val, "s390x") == 0) { 582 arch = ARCH_S390X; 583 } else { 584 PRINT_FAIL("bad arch spec: '%s'\n", val); 585 err = -EINVAL; 586 goto cleanup; 587 } 588 arch_mask |= arch; 589 collect_jit = get_current_arch() == arch; 590 unpriv_jit_on_next_line = true; 591 jit_on_next_line = true; 592 } else if ((val = str_has_pfx(s, "test_btf_path="))) { 593 spec->btf_custom_path = val; 594 } else if ((val = str_has_pfx(s, "test_btf_func_path="))) { 595 spec->btf_custom_func_path = val; 596 } else if ((val = str_has_pfx(s, "test_caps_unpriv="))) { 597 err = parse_caps(val, &spec->unpriv.caps, "test caps"); 598 if (err) 599 goto cleanup; 600 spec->mode_mask |= UNPRIV; 601 } else if ((val = str_has_pfx(s, "load_mode="))) { 602 if (strcmp(val, "jited") == 0) { 603 load_mask = JITED; 604 } else if (strcmp(val, "no_jited") == 0) { 605 load_mask = NO_JITED; 606 } else { 607 PRINT_FAIL("bad load spec: '%s'", val); 608 err = -EINVAL; 609 goto cleanup; 610 } 611 } else if ((msg = str_has_pfx(s, "test_expect_stderr="))) { 612 err = push_disasm_msg(msg, &stderr_on_next_line, 613 &spec->priv.stderr); 614 if (err) 615 goto cleanup; 616 } else if ((msg = str_has_pfx(s, "test_expect_stderr_unpriv="))) { 617 err = push_disasm_msg(msg, &unpriv_stderr_on_next_line, 618 &spec->unpriv.stderr); 619 if (err) 620 goto cleanup; 621 } else if ((msg = str_has_pfx(s, "test_expect_stdout="))) { 622 err = push_disasm_msg(msg, &stdout_on_next_line, 623 &spec->priv.stdout); 624 if (err) 625 goto cleanup; 626 } else if ((msg = str_has_pfx(s, "test_expect_stdout_unpriv="))) { 627 err = push_disasm_msg(msg, &unpriv_stdout_on_next_line, 628 &spec->unpriv.stdout); 629 if (err) 630 goto cleanup; 631 } else if ((val = str_has_pfx(s, "test_linear_size="))) { 632 switch (bpf_program__type(prog)) { 633 case BPF_PROG_TYPE_SCHED_ACT: 634 case BPF_PROG_TYPE_SCHED_CLS: 635 case BPF_PROG_TYPE_CGROUP_SKB: 636 err = parse_int(val, &spec->linear_sz, "test linear size"); 637 if (err) 638 goto cleanup; 639 break; 640 default: 641 PRINT_FAIL("__linear_size for unsupported program type"); 642 err = -EINVAL; 643 goto cleanup; 644 } 645 } 646 } 647 648 spec->arch_mask = arch_mask ?: -1; 649 spec->load_mask = load_mask ?: (JITED | NO_JITED); 650 651 if (spec->mode_mask == 0) 652 spec->mode_mask = PRIV; 653 654 if (spec->mode_mask & PRIV) { 655 spec->priv.name = strdup(spec->prog_name); 656 if (!spec->priv.name) { 657 PRINT_FAIL("failed to allocate memory for priv.name\n"); 658 err = -ENOMEM; 659 goto cleanup; 660 } 661 662 if (description) { 663 spec->priv.description = strdup(description); 664 if (!spec->priv.description) { 665 PRINT_FAIL("failed to allocate memory for priv.description\n"); 666 err = -ENOMEM; 667 goto cleanup; 668 } 669 } 670 } 671 672 if (spec->mode_mask & UNPRIV) { 673 int name_len = strlen(spec->prog_name); 674 const char *suffix = " @unpriv"; 675 int suffix_len = strlen(suffix); 676 char *name; 677 678 name = malloc(name_len + suffix_len + 1); 679 if (!name) { 680 PRINT_FAIL("failed to allocate memory for unpriv.name\n"); 681 err = -ENOMEM; 682 goto cleanup; 683 } 684 685 strcpy(name, spec->prog_name); 686 strcpy(&name[name_len], suffix); 687 spec->unpriv.name = name; 688 689 if (description) { 690 int descr_len = strlen(description); 691 char *descr; 692 693 descr = malloc(descr_len + suffix_len + 1); 694 if (!descr) { 695 PRINT_FAIL("failed to allocate memory for unpriv.description\n"); 696 err = -ENOMEM; 697 goto cleanup; 698 } 699 700 strcpy(descr, description); 701 strcpy(&descr[descr_len], suffix); 702 spec->unpriv.description = descr; 703 } 704 } 705 706 if (spec->mode_mask & (PRIV | UNPRIV)) { 707 if (!has_unpriv_result) 708 spec->unpriv.expect_failure = spec->priv.expect_failure; 709 710 if (!has_unpriv_retval) { 711 spec->unpriv.retval = spec->priv.retval; 712 spec->unpriv.execute = spec->priv.execute; 713 } 714 715 if (spec->unpriv.expect_msgs.cnt == 0) 716 clone_msgs(&spec->priv.expect_msgs, &spec->unpriv.expect_msgs); 717 if (spec->unpriv.expect_xlated.cnt == 0) 718 clone_msgs(&spec->priv.expect_xlated, &spec->unpriv.expect_xlated); 719 if (spec->unpriv.jited.cnt == 0) 720 clone_msgs(&spec->priv.jited, &spec->unpriv.jited); 721 if (spec->unpriv.stderr.cnt == 0) 722 clone_msgs(&spec->priv.stderr, &spec->unpriv.stderr); 723 if (spec->unpriv.stdout.cnt == 0) 724 clone_msgs(&spec->priv.stdout, &spec->unpriv.stdout); 725 } 726 727 spec->valid = true; 728 729 free(tags); 730 return 0; 731 732 cleanup: 733 free(tags); 734 free_test_spec(spec); 735 return err; 736 } 737 738 static void prepare_case(struct test_loader *tester, 739 struct test_spec *spec, 740 struct bpf_object *obj, 741 struct bpf_program *prog) 742 { 743 int min_log_level = 0, prog_flags; 744 745 if (env.verbosity > VERBOSE_NONE) 746 min_log_level = 1; 747 if (env.verbosity > VERBOSE_VERY) 748 min_log_level = 2; 749 750 bpf_program__set_log_buf(prog, tester->log_buf, tester->log_buf_sz); 751 752 /* Make sure we set at least minimal log level, unless test requires 753 * even higher level already. Make sure to preserve independent log 754 * level 4 (verifier stats), though. 755 */ 756 if ((spec->log_level & 3) < min_log_level) 757 bpf_program__set_log_level(prog, (spec->log_level & 4) | min_log_level); 758 else 759 bpf_program__set_log_level(prog, spec->log_level); 760 761 prog_flags = bpf_program__flags(prog); 762 bpf_program__set_flags(prog, prog_flags | spec->prog_flags); 763 764 tester->log_buf[0] = '\0'; 765 } 766 767 static void emit_verifier_log(const char *log_buf, bool force) 768 { 769 if (!force && env.verbosity == VERBOSE_NONE) 770 return; 771 fprintf(stdout, "VERIFIER LOG:\n=============\n%s=============\n", log_buf); 772 } 773 774 static void emit_xlated(const char *xlated, bool force) 775 { 776 if (!force && env.verbosity == VERBOSE_NONE) 777 return; 778 fprintf(stdout, "XLATED:\n=============\n%s=============\n", xlated); 779 } 780 781 static void emit_jited(const char *jited, bool force) 782 { 783 if (!force && env.verbosity == VERBOSE_NONE) 784 return; 785 fprintf(stdout, "JITED:\n=============\n%s=============\n", jited); 786 } 787 788 static void emit_stderr(const char *stderr, bool force) 789 { 790 if (!force && env.verbosity == VERBOSE_NONE) 791 return; 792 fprintf(stdout, "STDERR:\n=============\n%s=============\n", stderr); 793 } 794 795 static void verify_stderr(int prog_fd, struct expected_msgs *msgs) 796 { 797 LIBBPF_OPTS(bpf_prog_stream_read_opts, ropts); 798 char *buf; 799 int ret; 800 801 if (!msgs->cnt) 802 return; 803 804 buf = malloc(TEST_LOADER_LOG_BUF_SZ); 805 if (!ASSERT_OK_PTR(buf, "malloc")) 806 return; 807 808 ret = bpf_prog_stream_read(prog_fd, 2, buf, TEST_LOADER_LOG_BUF_SZ - 1, 809 &ropts); 810 if (ret > 0) { 811 buf[ret] = '\0'; 812 emit_stderr(buf, false); 813 validate_msgs(buf, msgs, emit_stderr); 814 } else { 815 ASSERT_GT(ret, 0, "stderr stream read"); 816 } 817 818 free(buf); 819 } 820 821 void verify_test_stderr(struct bpf_object *obj, struct bpf_program *prog) 822 { 823 struct test_spec spec = {}; 824 825 if (parse_test_spec(NULL, obj, prog, &spec)) 826 return; 827 828 verify_stderr(bpf_program__fd(prog), &spec.priv.stderr); 829 free_test_spec(&spec); 830 } 831 832 static void emit_stdout(const char *bpf_stdout, bool force) 833 { 834 if (!force && env.verbosity == VERBOSE_NONE) 835 return; 836 fprintf(stdout, "STDOUT:\n=============\n%s=============\n", bpf_stdout); 837 } 838 839 static const char *match_msg(struct expect_msg *msg, const char **log) 840 { 841 const char *match = NULL; 842 regmatch_t reg_match[1]; 843 int err; 844 845 if (!msg->is_regex) { 846 match = strstr(*log, msg->substr); 847 if (match) 848 *log = match + strlen(msg->substr); 849 } else { 850 err = regexec(&msg->regex, *log, 1, reg_match, 0); 851 if (err == 0) { 852 match = *log + reg_match[0].rm_so; 853 *log += reg_match[0].rm_eo; 854 } 855 } 856 return match; 857 } 858 859 static int count_lines(const char *start, const char *end) 860 { 861 const char *tmp; 862 int n = 0; 863 864 for (tmp = start; tmp < end; ++tmp) 865 if (*tmp == '\n') 866 n++; 867 return n; 868 } 869 870 struct match { 871 const char *start; 872 const char *end; 873 int line; 874 }; 875 876 /* 877 * Positive messages are matched sequentially, each next message 878 * is looked for starting from the end of a previous matched one. 879 */ 880 static void match_positive_msgs(const char *log, struct expected_msgs *msgs, struct match *matches) 881 { 882 const char *prev_match; 883 int i, line; 884 885 prev_match = log; 886 line = 0; 887 for (i = 0; i < msgs->cnt; i++) { 888 struct expect_msg *msg = &msgs->patterns[i]; 889 const char *match = NULL; 890 891 if (msg->negative) 892 continue; 893 894 match = match_msg(msg, &log); 895 if (match) { 896 line += count_lines(prev_match, match); 897 matches[i].start = match; 898 matches[i].end = log; 899 matches[i].line = line; 900 prev_match = match; 901 } 902 } 903 } 904 905 /* 906 * Each negative messages N located between positive messages P1 and P2 907 * is matched in the span P1.end .. P2.start. Consequently, negative messages 908 * are unordered within the span. 909 */ 910 static void match_negative_msgs(const char *log, struct expected_msgs *msgs, struct match *matches) 911 { 912 const char *start = log, *end, *next, *match; 913 const char *log_end = log + strlen(log); 914 int i, j, next_positive; 915 916 for (i = 0; i < msgs->cnt; i++) { 917 struct expect_msg *msg = &msgs->patterns[i]; 918 919 /* positive message bumps span start */ 920 if (!msg->negative) { 921 start = matches[i].end ?: start; 922 continue; 923 } 924 925 /* count stride of negative patterns and adjust span end */ 926 end = log_end; 927 for (next_positive = i + 1; next_positive < msgs->cnt; next_positive++) { 928 if (!msgs->patterns[next_positive].negative) { 929 end = matches[next_positive].start; 930 break; 931 } 932 } 933 934 /* try matching negative messages within identified span */ 935 for (j = i; j < next_positive; j++) { 936 next = start; 937 match = match_msg(msg, &next); 938 if (match && next <= end) { 939 matches[j].start = match; 940 matches[j].end = next; 941 } 942 } 943 944 /* -1 to account for i++ */ 945 i = next_positive - 1; 946 } 947 } 948 949 void validate_msgs(const char *log_buf, struct expected_msgs *msgs, 950 void (*emit_fn)(const char *buf, bool force)) 951 { 952 struct match matches[msgs->cnt]; 953 struct match *prev_match = NULL; 954 int i, j; 955 956 memset(matches, 0, sizeof(*matches) * msgs->cnt); 957 match_positive_msgs(log_buf, msgs, matches); 958 match_negative_msgs(log_buf, msgs, matches); 959 960 for (i = 0; i < msgs->cnt; i++) { 961 struct expect_msg *msg = &msgs->patterns[i]; 962 struct match *match = &matches[i]; 963 const char *pat_status; 964 bool unexpected; 965 bool wrong_line; 966 bool no_match; 967 968 no_match = !msg->negative && !match->start; 969 wrong_line = !msg->negative && 970 msg->on_next_line && 971 prev_match && prev_match->line + 1 != match->line; 972 unexpected = msg->negative && match->start; 973 if (no_match || wrong_line || unexpected) { 974 PRINT_FAIL("expect_msg\n"); 975 if (env.verbosity == VERBOSE_NONE) 976 emit_fn(log_buf, true /*force*/); 977 for (j = 0; j <= i; j++) { 978 msg = &msgs->patterns[j]; 979 if (j < i) 980 pat_status = "MATCHED "; 981 else if (wrong_line) 982 pat_status = "WRONG LINE"; 983 else if (no_match) 984 pat_status = "EXPECTED "; 985 else 986 pat_status = "UNEXPECTED"; 987 msg = &msgs->patterns[j]; 988 fprintf(stderr, "%s %s: '%s'\n", 989 pat_status, 990 msg->is_regex ? " REGEX" : "SUBSTR", 991 msg->substr); 992 } 993 if (wrong_line) { 994 fprintf(stderr, 995 "expecting match at line %d, actual match is at line %d\n", 996 prev_match->line + 1, match->line); 997 } 998 break; 999 } 1000 1001 if (!msg->negative) 1002 prev_match = match; 1003 } 1004 } 1005 1006 struct cap_state { 1007 __u64 old_caps; 1008 bool initialized; 1009 }; 1010 1011 static int drop_capabilities(struct cap_state *caps) 1012 { 1013 const __u64 caps_to_drop = (1ULL << CAP_SYS_ADMIN | 1ULL << CAP_NET_ADMIN | 1014 1ULL << CAP_PERFMON | 1ULL << CAP_BPF); 1015 int err; 1016 1017 err = cap_disable_effective(caps_to_drop, &caps->old_caps); 1018 if (err) { 1019 PRINT_FAIL("failed to drop capabilities: %i, %s\n", err, strerror(-err)); 1020 return err; 1021 } 1022 1023 caps->initialized = true; 1024 return 0; 1025 } 1026 1027 static int restore_capabilities(struct cap_state *caps) 1028 { 1029 int err; 1030 1031 if (!caps->initialized) 1032 return 0; 1033 1034 err = cap_enable_effective(caps->old_caps, NULL); 1035 if (err) 1036 PRINT_FAIL("failed to restore capabilities: %i, %s\n", err, strerror(-err)); 1037 caps->initialized = false; 1038 return err; 1039 } 1040 1041 static bool can_execute_unpriv(struct test_loader *tester, struct test_spec *spec) 1042 { 1043 if (sysctl_unpriv_disabled < 0) 1044 sysctl_unpriv_disabled = get_unpriv_disabled() ? 1 : 0; 1045 if (sysctl_unpriv_disabled) 1046 return false; 1047 if ((spec->prog_flags & BPF_F_ANY_ALIGNMENT) && !EFFICIENT_UNALIGNED_ACCESS) 1048 return false; 1049 return true; 1050 } 1051 1052 static bool is_unpriv_capable_map(struct bpf_map *map) 1053 { 1054 enum bpf_map_type type; 1055 __u32 flags; 1056 1057 type = bpf_map__type(map); 1058 1059 switch (type) { 1060 case BPF_MAP_TYPE_HASH: 1061 case BPF_MAP_TYPE_PERCPU_HASH: 1062 case BPF_MAP_TYPE_HASH_OF_MAPS: 1063 flags = bpf_map__map_flags(map); 1064 return !(flags & BPF_F_ZERO_SEED); 1065 case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: 1066 case BPF_MAP_TYPE_ARRAY: 1067 case BPF_MAP_TYPE_RINGBUF: 1068 case BPF_MAP_TYPE_PROG_ARRAY: 1069 case BPF_MAP_TYPE_CGROUP_ARRAY: 1070 case BPF_MAP_TYPE_PERCPU_ARRAY: 1071 case BPF_MAP_TYPE_USER_RINGBUF: 1072 case BPF_MAP_TYPE_ARRAY_OF_MAPS: 1073 case BPF_MAP_TYPE_CGROUP_STORAGE: 1074 case BPF_MAP_TYPE_PERF_EVENT_ARRAY: 1075 return true; 1076 default: 1077 return false; 1078 } 1079 } 1080 1081 static int do_prog_test_run(int fd_prog, int *retval, bool empty_opts, int linear_sz) 1082 { 1083 __u8 tmp_out[TEST_DATA_LEN << 2] = {}; 1084 __u8 tmp_in[TEST_DATA_LEN] = {}; 1085 struct __sk_buff ctx = {}; 1086 int err, saved_errno; 1087 LIBBPF_OPTS(bpf_test_run_opts, topts, 1088 .data_in = tmp_in, 1089 .data_size_in = sizeof(tmp_in), 1090 .data_out = tmp_out, 1091 .data_size_out = sizeof(tmp_out), 1092 .repeat = 1, 1093 ); 1094 1095 if (linear_sz) { 1096 ctx.data_end = linear_sz; 1097 topts.ctx_in = &ctx; 1098 topts.ctx_size_in = sizeof(ctx); 1099 } 1100 1101 if (empty_opts) { 1102 memset(&topts, 0, sizeof(struct bpf_test_run_opts)); 1103 topts.sz = sizeof(struct bpf_test_run_opts); 1104 } 1105 err = bpf_prog_test_run_opts(fd_prog, &topts); 1106 saved_errno = errno; 1107 1108 if (err) { 1109 PRINT_FAIL("FAIL: Unexpected bpf_prog_test_run error: %d (%s) ", 1110 saved_errno, strerror(saved_errno)); 1111 return err; 1112 } 1113 1114 ASSERT_OK(0, "bpf_prog_test_run"); 1115 *retval = topts.retval; 1116 1117 return 0; 1118 } 1119 1120 static bool should_do_test_run(struct test_spec *spec, struct test_subspec *subspec) 1121 { 1122 if (!subspec->execute) 1123 return false; 1124 1125 if (subspec->expect_failure) 1126 return false; 1127 1128 if ((spec->prog_flags & BPF_F_ANY_ALIGNMENT) && !EFFICIENT_UNALIGNED_ACCESS) { 1129 if (env.verbosity != VERBOSE_NONE) 1130 printf("alignment prevents execution\n"); 1131 return false; 1132 } 1133 1134 return true; 1135 } 1136 1137 /* Get a disassembly of BPF program after verifier applies all rewrites */ 1138 static int get_xlated_program_text(int prog_fd, char *text, size_t text_sz) 1139 { 1140 struct bpf_insn *insn_start = NULL, *insn, *insn_end; 1141 __u32 insns_cnt = 0, i; 1142 char buf[64]; 1143 FILE *out = NULL; 1144 int err; 1145 1146 err = get_xlated_program(prog_fd, &insn_start, &insns_cnt); 1147 if (!ASSERT_OK(err, "get_xlated_program")) 1148 goto out; 1149 out = fmemopen(text, text_sz, "w"); 1150 if (!ASSERT_OK_PTR(out, "open_memstream")) 1151 goto out; 1152 insn_end = insn_start + insns_cnt; 1153 insn = insn_start; 1154 while (insn < insn_end) { 1155 i = insn - insn_start; 1156 insn = disasm_insn(insn, buf, sizeof(buf)); 1157 fprintf(out, "%d: %s\n", i, buf); 1158 } 1159 fflush(out); 1160 1161 out: 1162 free(insn_start); 1163 if (out) 1164 fclose(out); 1165 return err; 1166 } 1167 1168 /* Read the bpf stream corresponding to the stream_id */ 1169 static int get_stream(int stream_id, int prog_fd, char *text, size_t text_sz) 1170 { 1171 LIBBPF_OPTS(bpf_prog_stream_read_opts, ropts); 1172 int ret; 1173 1174 ret = bpf_prog_stream_read(prog_fd, stream_id, text, text_sz, &ropts); 1175 ASSERT_GT(ret, 0, "stream read"); 1176 text[ret] = '\0'; 1177 1178 return ret; 1179 } 1180 1181 /* 1182 * Fix up the program's BTF using BTF from a separate file. 1183 * 1184 * For __naked subprogs, clang drops parameter names from BTF. Find FUNC 1185 * entries with anonymous parameters and replace their FUNC_PROTO with the 1186 * properly-named version from the custom file. 1187 */ 1188 static int fixup_btf_from_path(struct bpf_object *obj, const char *path) 1189 { 1190 struct btf *prog_btf, *custom_btf; 1191 __u32 i, j, cnt, custom_cnt; 1192 int err = 0; 1193 1194 prog_btf = bpf_object__btf(obj); 1195 if (!prog_btf) 1196 return 0; 1197 1198 custom_btf = btf__parse(path, NULL); 1199 if (!ASSERT_OK_PTR(custom_btf, "parse_custom_btf")) 1200 return -EINVAL; 1201 1202 cnt = btf__type_cnt(prog_btf); 1203 custom_cnt = btf__type_cnt(custom_btf); 1204 1205 /* Fix up FUNC entries with anonymous params. 1206 * Save all data from prog_btf BEFORE calling btf__add_*, 1207 * since those calls may reallocate the BTF data buffer 1208 * and invalidate any pointers obtained from btf__type_by_id. 1209 */ 1210 for (i = 1; i < cnt; i++) { 1211 const struct btf_type *t = btf__type_by_id(prog_btf, i); 1212 const struct btf_type *fp, *custom_t, *custom_fp; 1213 const struct btf_param *params, *custom_params; 1214 __u32 ret_type_id, vlen; 1215 __u32 *prog_param_types = NULL; 1216 const char *name; 1217 int new_proto_id; 1218 1219 if (!btf_is_func(t)) 1220 continue; 1221 1222 fp = btf__type_by_id(prog_btf, t->type); 1223 if (!fp || !btf_is_func_proto(fp) || btf_vlen(fp) == 0) 1224 continue; 1225 1226 /* Check if any param is anonymous */ 1227 params = btf_params(fp); 1228 if (params[0].name_off != 0) 1229 continue; 1230 1231 /* Find matching FUNC by name in custom BTF */ 1232 name = btf__name_by_offset(prog_btf, t->name_off); 1233 if (!name) 1234 continue; 1235 1236 for (j = 1; j < custom_cnt; j++) { 1237 const char *cname; 1238 1239 custom_t = btf__type_by_id(custom_btf, j); 1240 if (!btf_is_func(custom_t)) 1241 continue; 1242 cname = btf__name_by_offset(custom_btf, custom_t->name_off); 1243 if (cname && strcmp(name, cname) == 0) 1244 break; 1245 } 1246 if (j >= custom_cnt) 1247 continue; 1248 1249 custom_fp = btf__type_by_id(custom_btf, custom_t->type); 1250 if (!custom_fp || !btf_is_func_proto(custom_fp)) 1251 continue; 1252 1253 vlen = btf_vlen(fp); 1254 if (vlen != btf_vlen(custom_fp)) 1255 continue; 1256 1257 /* Save data before btf__add_* calls invalidate pointers */ 1258 ret_type_id = fp->type; 1259 prog_param_types = malloc(vlen * sizeof(*prog_param_types)); 1260 if (!prog_param_types) { 1261 err = -ENOMEM; 1262 break; 1263 } 1264 for (j = 0; j < vlen; j++) 1265 prog_param_types[j] = params[j].type; 1266 1267 /* Add a new FUNC_PROTO: param names from custom, types from prog */ 1268 new_proto_id = btf__add_func_proto(prog_btf, ret_type_id); 1269 if (new_proto_id < 0) { 1270 err = new_proto_id; 1271 free(prog_param_types); 1272 break; 1273 } 1274 1275 custom_params = btf_params(custom_fp); 1276 for (j = 0; j < vlen; j++) { 1277 const char *pname; 1278 1279 pname = btf__name_by_offset(custom_btf, custom_params[j].name_off); 1280 err = btf__add_func_param(prog_btf, pname ?: "", prog_param_types[j]); 1281 if (err) 1282 break; 1283 } 1284 free(prog_param_types); 1285 if (err) 1286 break; 1287 1288 /* Update the FUNC to point to the new FUNC_PROTO (re-fetch 1289 * since btf__add_* may have reallocated the data buffer). 1290 */ 1291 ((struct btf_type *)btf__type_by_id(prog_btf, i))->type = new_proto_id; 1292 } 1293 1294 btf__free(custom_btf); 1295 return err; 1296 } 1297 1298 /* this function is forced noinline and has short generic name to look better 1299 * in test_progs output (in case of a failure) 1300 */ 1301 static noinline 1302 void run_subtest(struct test_loader *tester, 1303 struct bpf_object_open_opts *open_opts, 1304 const void *obj_bytes, 1305 size_t obj_byte_cnt, 1306 struct test_spec *specs, 1307 struct test_spec *spec, 1308 bool unpriv) 1309 { 1310 struct test_subspec *subspec = unpriv ? &spec->unpriv : &spec->priv; 1311 int current_runtime = is_jit_enabled() ? JITED : NO_JITED; 1312 struct bpf_program *tprog = NULL, *tprog_iter; 1313 struct bpf_link *link, *links[32] = {}; 1314 struct test_spec *spec_iter; 1315 struct cap_state caps = {}; 1316 struct bpf_object *tobj; 1317 struct bpf_map *map; 1318 int retval, err, i; 1319 int links_cnt = 0; 1320 bool should_load; 1321 1322 if (!test__start_subtest_with_desc(subspec->name, subspec->description)) 1323 return; 1324 1325 if ((get_current_arch() & spec->arch_mask) == 0) { 1326 test__skip(); 1327 return; 1328 } 1329 1330 if ((current_runtime & spec->load_mask) == 0) { 1331 test__skip(); 1332 return; 1333 } 1334 1335 if (unpriv) { 1336 if (!can_execute_unpriv(tester, spec)) { 1337 test__skip(); 1338 test__end_subtest(); 1339 return; 1340 } 1341 if (drop_capabilities(&caps)) { 1342 test__end_subtest(); 1343 return; 1344 } 1345 if (subspec->caps) { 1346 err = cap_enable_effective(subspec->caps, NULL); 1347 if (err) { 1348 PRINT_FAIL("failed to set capabilities: %i, %s\n", err, strerror(-err)); 1349 goto subtest_cleanup; 1350 } 1351 } 1352 } 1353 1354 /* Implicitly reset to NULL if next test case doesn't specify. 1355 * btf_custom_func_path also serves as btf_custom_path for kfunc resolution. 1356 */ 1357 open_opts->btf_custom_path = spec->btf_custom_path; 1358 if (!open_opts->btf_custom_path) 1359 open_opts->btf_custom_path = spec->btf_custom_func_path; 1360 1361 tobj = bpf_object__open_mem(obj_bytes, obj_byte_cnt, open_opts); 1362 if (!ASSERT_OK_PTR(tobj, "obj_open_mem")) /* shouldn't happen */ 1363 goto subtest_cleanup; 1364 1365 /* Fix up __naked subprog BTF using a separate file with named params */ 1366 if (spec->btf_custom_func_path) { 1367 err = fixup_btf_from_path(tobj, spec->btf_custom_func_path); 1368 if (err) { 1369 PRINT_FAIL("failed to fixup BTF from %s: %d\n", 1370 spec->btf_custom_func_path, err); 1371 goto tobj_cleanup; 1372 } 1373 } 1374 1375 i = 0; 1376 bpf_object__for_each_program(tprog_iter, tobj) { 1377 spec_iter = &specs[i++]; 1378 should_load = false; 1379 1380 if (spec_iter->valid) { 1381 if (strcmp(bpf_program__name(tprog_iter), spec->prog_name) == 0) { 1382 tprog = tprog_iter; 1383 should_load = true; 1384 } 1385 1386 if (spec_iter->auxiliary && 1387 spec_iter->mode_mask & (unpriv ? UNPRIV : PRIV)) 1388 should_load = true; 1389 } 1390 1391 bpf_program__set_autoload(tprog_iter, should_load); 1392 } 1393 1394 prepare_case(tester, spec, tobj, tprog); 1395 1396 /* By default bpf_object__load() automatically creates all 1397 * maps declared in the skeleton. Some map types are only 1398 * allowed in priv mode. Disable autoload for such maps in 1399 * unpriv mode. 1400 */ 1401 bpf_object__for_each_map(map, tobj) 1402 bpf_map__set_autocreate(map, !unpriv || is_unpriv_capable_map(map)); 1403 1404 err = bpf_object__load(tobj); 1405 if (subspec->expect_failure) { 1406 if (!ASSERT_ERR(err, "unexpected_load_success")) { 1407 emit_verifier_log(tester->log_buf, false /*force*/); 1408 goto tobj_cleanup; 1409 } 1410 } else { 1411 if (!ASSERT_OK(err, "unexpected_load_failure")) { 1412 emit_verifier_log(tester->log_buf, true /*force*/); 1413 goto tobj_cleanup; 1414 } 1415 } 1416 emit_verifier_log(tester->log_buf, false /*force*/); 1417 validate_msgs(tester->log_buf, &subspec->expect_msgs, emit_verifier_log); 1418 1419 /* Restore capabilities because the kernel will silently ignore requests 1420 * for program info (such as xlated program text) if we are not 1421 * bpf-capable. Also, for some reason test_verifier executes programs 1422 * with all capabilities restored. Do the same here. 1423 */ 1424 if (restore_capabilities(&caps)) 1425 goto tobj_cleanup; 1426 1427 if (subspec->expect_xlated.cnt) { 1428 err = get_xlated_program_text(bpf_program__fd(tprog), 1429 tester->log_buf, tester->log_buf_sz); 1430 if (err) 1431 goto tobj_cleanup; 1432 emit_xlated(tester->log_buf, false /*force*/); 1433 validate_msgs(tester->log_buf, &subspec->expect_xlated, emit_xlated); 1434 } 1435 1436 if (subspec->jited.cnt) { 1437 err = get_jited_program_text(bpf_program__fd(tprog), 1438 tester->log_buf, tester->log_buf_sz); 1439 if (err == -EOPNOTSUPP) { 1440 printf("%s:SKIP: jited programs disassembly is not supported,\n", __func__); 1441 printf("%s:SKIP: tests are built w/o LLVM development libs\n", __func__); 1442 test__skip(); 1443 goto tobj_cleanup; 1444 } 1445 if (!ASSERT_EQ(err, 0, "get_jited_program_text")) 1446 goto tobj_cleanup; 1447 emit_jited(tester->log_buf, false /*force*/); 1448 validate_msgs(tester->log_buf, &subspec->jited, emit_jited); 1449 } 1450 1451 if (should_do_test_run(spec, subspec)) { 1452 /* Do bpf_map__attach_struct_ops() for each struct_ops map. 1453 * This should trigger bpf_struct_ops->reg callback on kernel side. 1454 */ 1455 bpf_object__for_each_map(map, tobj) { 1456 if (!bpf_map__autocreate(map) || 1457 bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS) 1458 continue; 1459 if (links_cnt >= ARRAY_SIZE(links)) { 1460 PRINT_FAIL("too many struct_ops maps"); 1461 goto tobj_cleanup; 1462 } 1463 link = bpf_map__attach_struct_ops(map); 1464 if (!link) { 1465 PRINT_FAIL("bpf_map__attach_struct_ops failed for map %s: err=%d\n", 1466 bpf_map__name(map), -errno); 1467 goto tobj_cleanup; 1468 } 1469 links[links_cnt++] = link; 1470 } 1471 1472 if (tester->pre_execution_cb) { 1473 err = tester->pre_execution_cb(tobj); 1474 if (err) { 1475 PRINT_FAIL("pre_execution_cb failed: %d\n", err); 1476 goto tobj_cleanup; 1477 } 1478 } 1479 1480 err = do_prog_test_run(bpf_program__fd(tprog), &retval, 1481 bpf_program__type(tprog) == BPF_PROG_TYPE_SYSCALL ? true : false, 1482 spec->linear_sz); 1483 if (!err && retval != subspec->retval && subspec->retval != POINTER_VALUE) { 1484 PRINT_FAIL("Unexpected retval: %d != %d\n", retval, subspec->retval); 1485 goto tobj_cleanup; 1486 } 1487 1488 verify_stderr(bpf_program__fd(tprog), &subspec->stderr); 1489 1490 if (subspec->stdout.cnt) { 1491 err = get_stream(1, bpf_program__fd(tprog), 1492 tester->log_buf, tester->log_buf_sz); 1493 if (err <= 0) { 1494 PRINT_FAIL("Unexpected retval from get_stream(): %d, errno = %d\n", 1495 err, errno); 1496 goto tobj_cleanup; 1497 } 1498 emit_stdout(tester->log_buf, false /*force*/); 1499 validate_msgs(tester->log_buf, &subspec->stdout, emit_stdout); 1500 } 1501 1502 /* redo bpf_map__attach_struct_ops for each test */ 1503 while (links_cnt > 0) 1504 bpf_link__destroy(links[--links_cnt]); 1505 } 1506 1507 tobj_cleanup: 1508 while (links_cnt > 0) 1509 bpf_link__destroy(links[--links_cnt]); 1510 bpf_object__close(tobj); 1511 subtest_cleanup: 1512 test__end_subtest(); 1513 restore_capabilities(&caps); 1514 } 1515 1516 static void process_subtest(struct test_loader *tester, 1517 const char *skel_name, 1518 skel_elf_bytes_fn elf_bytes_factory) 1519 { 1520 LIBBPF_OPTS(bpf_object_open_opts, open_opts, .object_name = skel_name); 1521 struct test_spec *specs = NULL; 1522 struct bpf_object *obj = NULL; 1523 struct bpf_program *prog; 1524 const void *obj_bytes; 1525 int err, i, nr_progs; 1526 size_t obj_byte_cnt; 1527 1528 if (tester_init(tester) < 0) 1529 return; /* failed to initialize tester */ 1530 1531 obj_bytes = elf_bytes_factory(&obj_byte_cnt); 1532 obj = bpf_object__open_mem(obj_bytes, obj_byte_cnt, &open_opts); 1533 if (!ASSERT_OK_PTR(obj, "obj_open_mem")) 1534 return; 1535 1536 nr_progs = 0; 1537 bpf_object__for_each_program(prog, obj) 1538 ++nr_progs; 1539 1540 specs = calloc(nr_progs, sizeof(struct test_spec)); 1541 if (!ASSERT_OK_PTR(specs, "specs_alloc")) 1542 return; 1543 1544 i = 0; 1545 bpf_object__for_each_program(prog, obj) { 1546 /* ignore tests for which we can't derive test specification */ 1547 err = parse_test_spec(tester, obj, prog, &specs[i++]); 1548 if (err) 1549 PRINT_FAIL("Can't parse test spec for program '%s'\n", 1550 bpf_program__name(prog)); 1551 } 1552 1553 i = 0; 1554 bpf_object__for_each_program(prog, obj) { 1555 struct test_spec *spec = &specs[i++]; 1556 1557 if (!spec->valid || spec->auxiliary) 1558 continue; 1559 1560 if (spec->mode_mask & PRIV) 1561 run_subtest(tester, &open_opts, obj_bytes, obj_byte_cnt, 1562 specs, spec, false); 1563 if (spec->mode_mask & UNPRIV) 1564 run_subtest(tester, &open_opts, obj_bytes, obj_byte_cnt, 1565 specs, spec, true); 1566 1567 } 1568 1569 for (i = 0; i < nr_progs; ++i) 1570 free_test_spec(&specs[i]); 1571 free(specs); 1572 bpf_object__close(obj); 1573 } 1574 1575 void test_loader__run_subtests(struct test_loader *tester, 1576 const char *skel_name, 1577 skel_elf_bytes_fn elf_bytes_factory) 1578 { 1579 /* see comment in run_subtest() for why we do this function nesting */ 1580 process_subtest(tester, skel_name, elf_bytes_factory); 1581 } 1582