1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
3 #include <linux/capability.h>
6 #include <test_progs.h>
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"
15 #define str_has_pfx(str, pfx) \
16 (strncmp(str, pfx, __builtin_constant_p(pfx) ? sizeof(pfx) - 1 : strlen(pfx)) == 0)
18 #define TEST_LOADER_LOG_BUF_SZ 2097152
20 #define TEST_TAG_EXPECT_FAILURE "comment:test_expect_failure"
21 #define TEST_TAG_EXPECT_SUCCESS "comment:test_expect_success"
22 #define TEST_TAG_EXPECT_MSG_PFX "comment:test_expect_msg="
23 #define TEST_TAG_EXPECT_XLATED_PFX "comment:test_expect_xlated="
24 #define TEST_TAG_EXPECT_FAILURE_UNPRIV "comment:test_expect_failure_unpriv"
25 #define TEST_TAG_EXPECT_SUCCESS_UNPRIV "comment:test_expect_success_unpriv"
26 #define TEST_TAG_EXPECT_MSG_PFX_UNPRIV "comment:test_expect_msg_unpriv="
27 #define TEST_TAG_EXPECT_XLATED_PFX_UNPRIV "comment:test_expect_xlated_unpriv="
28 #define TEST_TAG_LOG_LEVEL_PFX "comment:test_log_level="
29 #define TEST_TAG_PROG_FLAGS_PFX "comment:test_prog_flags="
30 #define TEST_TAG_DESCRIPTION_PFX "comment:test_description="
31 #define TEST_TAG_RETVAL_PFX "comment:test_retval="
32 #define TEST_TAG_RETVAL_PFX_UNPRIV "comment:test_retval_unpriv="
33 #define TEST_TAG_AUXILIARY "comment:test_auxiliary"
34 #define TEST_TAG_AUXILIARY_UNPRIV "comment:test_auxiliary_unpriv"
35 #define TEST_BTF_PATH "comment:test_btf_path="
36 #define TEST_TAG_ARCH "comment:test_arch="
37 #define TEST_TAG_JITED_PFX "comment:test_jited="
38 #define TEST_TAG_JITED_PFX_UNPRIV "comment:test_jited_unpriv="
39 #define TEST_TAG_CAPS_UNPRIV "comment:test_caps_unpriv="
41 /* Warning: duplicated in bpf_misc.h */
42 #define POINTER_VALUE 0xcafe4all
43 #define TEST_DATA_LEN 64
45 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
46 #define EFFICIENT_UNALIGNED_ACCESS 1
48 #define EFFICIENT_UNALIGNED_ACCESS 0
51 static int sysctl_unpriv_disabled
= -1;
59 const char *substr
; /* substring match */
65 struct expected_msgs
{
66 struct expect_msg
*patterns
;
73 struct expected_msgs expect_msgs
;
74 struct expected_msgs expect_xlated
;
75 struct expected_msgs jited
;
82 const char *prog_name
;
83 struct test_subspec priv
;
84 struct test_subspec unpriv
;
85 const char *btf_custom_path
;
94 static int tester_init(struct test_loader
*tester
)
96 if (!tester
->log_buf
) {
97 tester
->log_buf_sz
= TEST_LOADER_LOG_BUF_SZ
;
98 tester
->log_buf
= calloc(tester
->log_buf_sz
, 1);
99 if (!ASSERT_OK_PTR(tester
->log_buf
, "tester_log_buf"))
106 void test_loader_fini(struct test_loader
*tester
)
111 free(tester
->log_buf
);
114 static void free_msgs(struct expected_msgs
*msgs
)
118 for (i
= 0; i
< msgs
->cnt
; i
++)
119 if (msgs
->patterns
[i
].is_regex
)
120 regfree(&msgs
->patterns
[i
].regex
);
121 free(msgs
->patterns
);
122 msgs
->patterns
= NULL
;
126 static void free_test_spec(struct test_spec
*spec
)
128 /* Deallocate expect_msgs arrays. */
129 free_msgs(&spec
->priv
.expect_msgs
);
130 free_msgs(&spec
->unpriv
.expect_msgs
);
131 free_msgs(&spec
->priv
.expect_xlated
);
132 free_msgs(&spec
->unpriv
.expect_xlated
);
133 free_msgs(&spec
->priv
.jited
);
134 free_msgs(&spec
->unpriv
.jited
);
136 free(spec
->priv
.name
);
137 free(spec
->unpriv
.name
);
138 spec
->priv
.name
= NULL
;
139 spec
->unpriv
.name
= NULL
;
142 /* Compiles regular expression matching pattern.
143 * Pattern has a special syntax:
145 * pattern := (<verbatim text> | regex)*
146 * regex := "{{" <posix extended regular expression> "}}"
148 * In other words, pattern is a verbatim text with inclusion
149 * of regular expressions enclosed in "{{" "}}" pairs.
150 * For example, pattern "foo{{[0-9]+}}" matches strings like
151 * "foo0", "foo007", etc.
153 static int compile_regex(const char *pattern
, regex_t
*regex
)
155 char err_buf
[256], buf
[256] = {}, *ptr
, *buf_end
;
156 const char *original_pattern
= pattern
;
157 bool in_regex
= false;
160 buf_end
= buf
+ sizeof(buf
);
162 while (*pattern
&& ptr
< buf_end
- 2) {
163 if (!in_regex
&& str_has_pfx(pattern
, "{{")) {
168 if (in_regex
&& str_has_pfx(pattern
, "}}")) {
177 /* list of characters that need escaping for extended posix regex */
178 if (strchr(".[]\\()*+?{}|^$", *pattern
)) {
186 PRINT_FAIL("Regexp too long: '%s'\n", original_pattern
);
190 PRINT_FAIL("Regexp has open '{{' but no closing '}}': '%s'\n", original_pattern
);
193 err
= regcomp(regex
, buf
, REG_EXTENDED
| REG_NEWLINE
);
195 regerror(err
, regex
, err_buf
, sizeof(err_buf
));
196 PRINT_FAIL("Regexp compilation error in '%s': '%s'\n", buf
, err_buf
);
202 static int __push_msg(const char *pattern
, bool on_next_line
, struct expected_msgs
*msgs
)
204 struct expect_msg
*msg
;
208 tmp
= realloc(msgs
->patterns
,
209 (1 + msgs
->cnt
) * sizeof(struct expect_msg
));
211 ASSERT_FAIL("failed to realloc memory for messages\n");
214 msgs
->patterns
= tmp
;
215 msg
= &msgs
->patterns
[msgs
->cnt
];
216 msg
->on_next_line
= on_next_line
;
217 msg
->substr
= pattern
;
218 msg
->is_regex
= false;
219 if (strstr(pattern
, "{{")) {
220 err
= compile_regex(pattern
, &msg
->regex
);
223 msg
->is_regex
= true;
229 static int clone_msgs(struct expected_msgs
*from
, struct expected_msgs
*to
)
231 struct expect_msg
*msg
;
234 for (i
= 0; i
< from
->cnt
; i
++) {
235 msg
= &from
->patterns
[i
];
236 err
= __push_msg(msg
->substr
, msg
->on_next_line
, to
);
243 static int push_msg(const char *substr
, struct expected_msgs
*msgs
)
245 return __push_msg(substr
, false, msgs
);
248 static int push_disasm_msg(const char *regex_str
, bool *on_next_line
, struct expected_msgs
*msgs
)
252 if (strcmp(regex_str
, "...") == 0) {
253 *on_next_line
= false;
256 err
= __push_msg(regex_str
, *on_next_line
, msgs
);
259 *on_next_line
= true;
263 static int parse_int(const char *str
, int *val
, const char *name
)
269 if (str_has_pfx(str
, "0x"))
270 tmp
= strtol(str
+ 2, &end
, 16);
272 tmp
= strtol(str
, &end
, 10);
273 if (errno
|| end
[0] != '\0') {
274 PRINT_FAIL("failed to parse %s from '%s'\n", name
, str
);
281 static int parse_caps(const char *str
, __u64
*val
, const char *name
)
284 char *token
= NULL
, *saveptr
= NULL
;
286 char *str_cpy
= strdup(str
);
287 if (str_cpy
== NULL
) {
288 PRINT_FAIL("Memory allocation failed\n");
292 token
= strtok_r(str_cpy
, "|", &saveptr
);
293 while (token
!= NULL
) {
295 if (!strncmp("CAP_", token
, sizeof("CAP_") - 1)) {
296 PRINT_FAIL("define %s constant in bpf_misc.h, failed to parse caps\n", token
);
299 cap_flag
= strtol(token
, NULL
, 10);
300 if (!cap_flag
|| errno
) {
301 PRINT_FAIL("failed to parse caps %s\n", name
);
304 *val
|= (1ULL << cap_flag
);
305 token
= strtok_r(NULL
, "|", &saveptr
);
312 static int parse_retval(const char *str
, int *val
, const char *name
)
318 { "INT_MIN" , INT_MIN
},
319 { "POINTER_VALUE", POINTER_VALUE
},
320 { "TEST_DATA_LEN", TEST_DATA_LEN
},
324 for (i
= 0; i
< ARRAY_SIZE(named_values
); ++i
) {
325 if (strcmp(str
, named_values
[i
].name
) != 0)
327 *val
= named_values
[i
].val
;
331 return parse_int(str
, val
, name
);
334 static void update_flags(int *flags
, int flag
, bool clear
)
342 /* Matches a string of form '<pfx>[^=]=.*' and returns it's suffix.
343 * Used to parse btf_decl_tag values.
344 * Such values require unique prefix because compiler does not add
345 * same __attribute__((btf_decl_tag(...))) twice.
346 * Test suite uses two-component tags for such cases:
348 * <pfx> __COUNTER__ '='
350 * For example, two consecutive __msg tags '__msg("foo") __msg("foo")'
351 * would be encoded as:
353 * [18] DECL_TAG 'comment:test_expect_msg=0=foo' type_id=15 component_idx=-1
354 * [19] DECL_TAG 'comment:test_expect_msg=1=foo' type_id=15 component_idx=-1
356 * And the purpose of this function is to extract 'foo' from the above.
358 static const char *skip_dynamic_pfx(const char *s
, const char *pfx
)
362 if (strncmp(s
, pfx
, strlen(pfx
)) != 0)
364 msg
= s
+ strlen(pfx
);
365 msg
= strchr(msg
, '=');
378 static int get_current_arch(void)
380 #if defined(__x86_64__)
382 #elif defined(__aarch64__)
384 #elif defined(__riscv) && __riscv_xlen == 64
390 /* Uses btf_decl_tag attributes to describe the expected test
391 * behavior, see bpf_misc.h for detailed description of each attribute
392 * and attribute combinations.
394 static int parse_test_spec(struct test_loader
*tester
,
395 struct bpf_object
*obj
,
396 struct bpf_program
*prog
,
397 struct test_spec
*spec
)
399 const char *description
= NULL
;
400 bool has_unpriv_result
= false;
401 bool has_unpriv_retval
= false;
402 bool unpriv_xlated_on_next_line
= true;
403 bool xlated_on_next_line
= true;
404 bool unpriv_jit_on_next_line
;
405 bool jit_on_next_line
;
406 bool collect_jit
= false;
407 int func_id
, i
, err
= 0;
412 memset(spec
, 0, sizeof(*spec
));
414 spec
->prog_name
= bpf_program__name(prog
);
415 spec
->prog_flags
= testing_prog_flags();
417 btf
= bpf_object__btf(obj
);
419 ASSERT_FAIL("BPF object has no BTF");
423 func_id
= btf__find_by_name_kind(btf
, spec
->prog_name
, BTF_KIND_FUNC
);
425 ASSERT_FAIL("failed to find FUNC BTF type for '%s'", spec
->prog_name
);
429 for (i
= 1; i
< btf__type_cnt(btf
); i
++) {
430 const char *s
, *val
, *msg
;
431 const struct btf_type
*t
;
435 t
= btf__type_by_id(btf
, i
);
436 if (!btf_is_decl_tag(t
))
439 if (t
->type
!= func_id
|| btf_decl_tag(t
)->component_idx
!= -1)
442 s
= btf__str_by_offset(btf
, t
->name_off
);
443 if (str_has_pfx(s
, TEST_TAG_DESCRIPTION_PFX
)) {
444 description
= s
+ sizeof(TEST_TAG_DESCRIPTION_PFX
) - 1;
445 } else if (strcmp(s
, TEST_TAG_EXPECT_FAILURE
) == 0) {
446 spec
->priv
.expect_failure
= true;
447 spec
->mode_mask
|= PRIV
;
448 } else if (strcmp(s
, TEST_TAG_EXPECT_SUCCESS
) == 0) {
449 spec
->priv
.expect_failure
= false;
450 spec
->mode_mask
|= PRIV
;
451 } else if (strcmp(s
, TEST_TAG_EXPECT_FAILURE_UNPRIV
) == 0) {
452 spec
->unpriv
.expect_failure
= true;
453 spec
->mode_mask
|= UNPRIV
;
454 has_unpriv_result
= true;
455 } else if (strcmp(s
, TEST_TAG_EXPECT_SUCCESS_UNPRIV
) == 0) {
456 spec
->unpriv
.expect_failure
= false;
457 spec
->mode_mask
|= UNPRIV
;
458 has_unpriv_result
= true;
459 } else if (strcmp(s
, TEST_TAG_AUXILIARY
) == 0) {
460 spec
->auxiliary
= true;
461 spec
->mode_mask
|= PRIV
;
462 } else if (strcmp(s
, TEST_TAG_AUXILIARY_UNPRIV
) == 0) {
463 spec
->auxiliary
= true;
464 spec
->mode_mask
|= UNPRIV
;
465 } else if ((msg
= skip_dynamic_pfx(s
, TEST_TAG_EXPECT_MSG_PFX
))) {
466 err
= push_msg(msg
, &spec
->priv
.expect_msgs
);
469 spec
->mode_mask
|= PRIV
;
470 } else if ((msg
= skip_dynamic_pfx(s
, TEST_TAG_EXPECT_MSG_PFX_UNPRIV
))) {
471 err
= push_msg(msg
, &spec
->unpriv
.expect_msgs
);
474 spec
->mode_mask
|= UNPRIV
;
475 } else if ((msg
= skip_dynamic_pfx(s
, TEST_TAG_JITED_PFX
))) {
476 if (arch_mask
== 0) {
477 PRINT_FAIL("__jited used before __arch_*");
481 err
= push_disasm_msg(msg
, &jit_on_next_line
,
485 spec
->mode_mask
|= PRIV
;
487 } else if ((msg
= skip_dynamic_pfx(s
, TEST_TAG_JITED_PFX_UNPRIV
))) {
488 if (arch_mask
== 0) {
489 PRINT_FAIL("__unpriv_jited used before __arch_*");
493 err
= push_disasm_msg(msg
, &unpriv_jit_on_next_line
,
494 &spec
->unpriv
.jited
);
497 spec
->mode_mask
|= UNPRIV
;
499 } else if ((msg
= skip_dynamic_pfx(s
, TEST_TAG_EXPECT_XLATED_PFX
))) {
500 err
= push_disasm_msg(msg
, &xlated_on_next_line
,
501 &spec
->priv
.expect_xlated
);
504 spec
->mode_mask
|= PRIV
;
505 } else if ((msg
= skip_dynamic_pfx(s
, TEST_TAG_EXPECT_XLATED_PFX_UNPRIV
))) {
506 err
= push_disasm_msg(msg
, &unpriv_xlated_on_next_line
,
507 &spec
->unpriv
.expect_xlated
);
510 spec
->mode_mask
|= UNPRIV
;
511 } else if (str_has_pfx(s
, TEST_TAG_RETVAL_PFX
)) {
512 val
= s
+ sizeof(TEST_TAG_RETVAL_PFX
) - 1;
513 err
= parse_retval(val
, &spec
->priv
.retval
, "__retval");
516 spec
->priv
.execute
= true;
517 spec
->mode_mask
|= PRIV
;
518 } else if (str_has_pfx(s
, TEST_TAG_RETVAL_PFX_UNPRIV
)) {
519 val
= s
+ sizeof(TEST_TAG_RETVAL_PFX_UNPRIV
) - 1;
520 err
= parse_retval(val
, &spec
->unpriv
.retval
, "__retval_unpriv");
523 spec
->mode_mask
|= UNPRIV
;
524 spec
->unpriv
.execute
= true;
525 has_unpriv_retval
= true;
526 } else if (str_has_pfx(s
, TEST_TAG_LOG_LEVEL_PFX
)) {
527 val
= s
+ sizeof(TEST_TAG_LOG_LEVEL_PFX
) - 1;
528 err
= parse_int(val
, &spec
->log_level
, "test log level");
531 } else if (str_has_pfx(s
, TEST_TAG_PROG_FLAGS_PFX
)) {
532 val
= s
+ sizeof(TEST_TAG_PROG_FLAGS_PFX
) - 1;
534 clear
= val
[0] == '!';
538 if (strcmp(val
, "BPF_F_STRICT_ALIGNMENT") == 0) {
539 update_flags(&spec
->prog_flags
, BPF_F_STRICT_ALIGNMENT
, clear
);
540 } else if (strcmp(val
, "BPF_F_ANY_ALIGNMENT") == 0) {
541 update_flags(&spec
->prog_flags
, BPF_F_ANY_ALIGNMENT
, clear
);
542 } else if (strcmp(val
, "BPF_F_TEST_RND_HI32") == 0) {
543 update_flags(&spec
->prog_flags
, BPF_F_TEST_RND_HI32
, clear
);
544 } else if (strcmp(val
, "BPF_F_TEST_STATE_FREQ") == 0) {
545 update_flags(&spec
->prog_flags
, BPF_F_TEST_STATE_FREQ
, clear
);
546 } else if (strcmp(val
, "BPF_F_SLEEPABLE") == 0) {
547 update_flags(&spec
->prog_flags
, BPF_F_SLEEPABLE
, clear
);
548 } else if (strcmp(val
, "BPF_F_XDP_HAS_FRAGS") == 0) {
549 update_flags(&spec
->prog_flags
, BPF_F_XDP_HAS_FRAGS
, clear
);
550 } else if (strcmp(val
, "BPF_F_TEST_REG_INVARIANTS") == 0) {
551 update_flags(&spec
->prog_flags
, BPF_F_TEST_REG_INVARIANTS
, clear
);
552 } else /* assume numeric value */ {
553 err
= parse_int(val
, &flags
, "test prog flags");
556 update_flags(&spec
->prog_flags
, flags
, clear
);
558 } else if (str_has_pfx(s
, TEST_TAG_ARCH
)) {
559 val
= s
+ sizeof(TEST_TAG_ARCH
) - 1;
560 if (strcmp(val
, "X86_64") == 0) {
562 } else if (strcmp(val
, "ARM64") == 0) {
564 } else if (strcmp(val
, "RISCV64") == 0) {
567 PRINT_FAIL("bad arch spec: '%s'", val
);
572 collect_jit
= get_current_arch() == arch
;
573 unpriv_jit_on_next_line
= true;
574 jit_on_next_line
= true;
575 } else if (str_has_pfx(s
, TEST_BTF_PATH
)) {
576 spec
->btf_custom_path
= s
+ sizeof(TEST_BTF_PATH
) - 1;
577 } else if (str_has_pfx(s
, TEST_TAG_CAPS_UNPRIV
)) {
578 val
= s
+ sizeof(TEST_TAG_CAPS_UNPRIV
) - 1;
579 err
= parse_caps(val
, &spec
->unpriv
.caps
, "test caps");
582 spec
->mode_mask
|= UNPRIV
;
586 spec
->arch_mask
= arch_mask
?: -1;
588 if (spec
->mode_mask
== 0)
589 spec
->mode_mask
= PRIV
;
592 description
= spec
->prog_name
;
594 if (spec
->mode_mask
& PRIV
) {
595 spec
->priv
.name
= strdup(description
);
596 if (!spec
->priv
.name
) {
597 PRINT_FAIL("failed to allocate memory for priv.name\n");
603 if (spec
->mode_mask
& UNPRIV
) {
604 int descr_len
= strlen(description
);
605 const char *suffix
= " @unpriv";
608 name
= malloc(descr_len
+ strlen(suffix
) + 1);
610 PRINT_FAIL("failed to allocate memory for unpriv.name\n");
615 strcpy(name
, description
);
616 strcpy(&name
[descr_len
], suffix
);
617 spec
->unpriv
.name
= name
;
620 if (spec
->mode_mask
& (PRIV
| UNPRIV
)) {
621 if (!has_unpriv_result
)
622 spec
->unpriv
.expect_failure
= spec
->priv
.expect_failure
;
624 if (!has_unpriv_retval
) {
625 spec
->unpriv
.retval
= spec
->priv
.retval
;
626 spec
->unpriv
.execute
= spec
->priv
.execute
;
629 if (spec
->unpriv
.expect_msgs
.cnt
== 0)
630 clone_msgs(&spec
->priv
.expect_msgs
, &spec
->unpriv
.expect_msgs
);
631 if (spec
->unpriv
.expect_xlated
.cnt
== 0)
632 clone_msgs(&spec
->priv
.expect_xlated
, &spec
->unpriv
.expect_xlated
);
633 if (spec
->unpriv
.jited
.cnt
== 0)
634 clone_msgs(&spec
->priv
.jited
, &spec
->unpriv
.jited
);
642 free_test_spec(spec
);
646 static void prepare_case(struct test_loader
*tester
,
647 struct test_spec
*spec
,
648 struct bpf_object
*obj
,
649 struct bpf_program
*prog
)
651 int min_log_level
= 0, prog_flags
;
653 if (env
.verbosity
> VERBOSE_NONE
)
655 if (env
.verbosity
> VERBOSE_VERY
)
658 bpf_program__set_log_buf(prog
, tester
->log_buf
, tester
->log_buf_sz
);
660 /* Make sure we set at least minimal log level, unless test requires
661 * even higher level already. Make sure to preserve independent log
662 * level 4 (verifier stats), though.
664 if ((spec
->log_level
& 3) < min_log_level
)
665 bpf_program__set_log_level(prog
, (spec
->log_level
& 4) | min_log_level
);
667 bpf_program__set_log_level(prog
, spec
->log_level
);
669 prog_flags
= bpf_program__flags(prog
);
670 bpf_program__set_flags(prog
, prog_flags
| spec
->prog_flags
);
672 tester
->log_buf
[0] = '\0';
675 static void emit_verifier_log(const char *log_buf
, bool force
)
677 if (!force
&& env
.verbosity
== VERBOSE_NONE
)
679 fprintf(stdout
, "VERIFIER LOG:\n=============\n%s=============\n", log_buf
);
682 static void emit_xlated(const char *xlated
, bool force
)
684 if (!force
&& env
.verbosity
== VERBOSE_NONE
)
686 fprintf(stdout
, "XLATED:\n=============\n%s=============\n", xlated
);
689 static void emit_jited(const char *jited
, bool force
)
691 if (!force
&& env
.verbosity
== VERBOSE_NONE
)
693 fprintf(stdout
, "JITED:\n=============\n%s=============\n", jited
);
696 static void validate_msgs(char *log_buf
, struct expected_msgs
*msgs
,
697 void (*emit_fn
)(const char *buf
, bool force
))
699 const char *log
= log_buf
, *prev_match
;
700 regmatch_t reg_match
[1];
705 prev_match_line
= -1;
708 for (i
= 0; i
< msgs
->cnt
; i
++) {
709 struct expect_msg
*msg
= &msgs
->patterns
[i
];
710 const char *match
= NULL
, *pat_status
;
711 bool wrong_line
= false;
713 if (!msg
->is_regex
) {
714 match
= strstr(log
, msg
->substr
);
716 log
= match
+ strlen(msg
->substr
);
718 err
= regexec(&msg
->regex
, log
, 1, reg_match
, 0);
720 match
= log
+ reg_match
[0].rm_so
;
721 log
+= reg_match
[0].rm_eo
;
726 for (; prev_match
< match
; ++prev_match
)
727 if (*prev_match
== '\n')
729 wrong_line
= msg
->on_next_line
&& prev_match_line
>= 0 &&
730 prev_match_line
+ 1 != match_line
;
733 if (!match
|| wrong_line
) {
734 PRINT_FAIL("expect_msg\n");
735 if (env
.verbosity
== VERBOSE_NONE
)
736 emit_fn(log_buf
, true /*force*/);
737 for (j
= 0; j
<= i
; j
++) {
738 msg
= &msgs
->patterns
[j
];
740 pat_status
= "MATCHED ";
742 pat_status
= "WRONG LINE";
744 pat_status
= "EXPECTED ";
745 msg
= &msgs
->patterns
[j
];
746 fprintf(stderr
, "%s %s: '%s'\n",
748 msg
->is_regex
? " REGEX" : "SUBSTR",
753 "expecting match at line %d, actual match is at line %d\n",
754 prev_match_line
+ 1, match_line
);
759 prev_match_line
= match_line
;
768 static int drop_capabilities(struct cap_state
*caps
)
770 const __u64 caps_to_drop
= (1ULL << CAP_SYS_ADMIN
| 1ULL << CAP_NET_ADMIN
|
771 1ULL << CAP_PERFMON
| 1ULL << CAP_BPF
);
774 err
= cap_disable_effective(caps_to_drop
, &caps
->old_caps
);
776 PRINT_FAIL("failed to drop capabilities: %i, %s\n", err
, strerror(err
));
780 caps
->initialized
= true;
784 static int restore_capabilities(struct cap_state
*caps
)
788 if (!caps
->initialized
)
791 err
= cap_enable_effective(caps
->old_caps
, NULL
);
793 PRINT_FAIL("failed to restore capabilities: %i, %s\n", err
, strerror(err
));
794 caps
->initialized
= false;
798 static bool can_execute_unpriv(struct test_loader
*tester
, struct test_spec
*spec
)
800 if (sysctl_unpriv_disabled
< 0)
801 sysctl_unpriv_disabled
= get_unpriv_disabled() ? 1 : 0;
802 if (sysctl_unpriv_disabled
)
804 if ((spec
->prog_flags
& BPF_F_ANY_ALIGNMENT
) && !EFFICIENT_UNALIGNED_ACCESS
)
809 static bool is_unpriv_capable_map(struct bpf_map
*map
)
811 enum bpf_map_type type
;
814 type
= bpf_map__type(map
);
817 case BPF_MAP_TYPE_HASH
:
818 case BPF_MAP_TYPE_PERCPU_HASH
:
819 case BPF_MAP_TYPE_HASH_OF_MAPS
:
820 flags
= bpf_map__map_flags(map
);
821 return !(flags
& BPF_F_ZERO_SEED
);
822 case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE
:
823 case BPF_MAP_TYPE_ARRAY
:
824 case BPF_MAP_TYPE_RINGBUF
:
825 case BPF_MAP_TYPE_PROG_ARRAY
:
826 case BPF_MAP_TYPE_CGROUP_ARRAY
:
827 case BPF_MAP_TYPE_PERCPU_ARRAY
:
828 case BPF_MAP_TYPE_USER_RINGBUF
:
829 case BPF_MAP_TYPE_ARRAY_OF_MAPS
:
830 case BPF_MAP_TYPE_CGROUP_STORAGE
:
831 case BPF_MAP_TYPE_PERF_EVENT_ARRAY
:
838 static int do_prog_test_run(int fd_prog
, int *retval
, bool empty_opts
)
840 __u8 tmp_out
[TEST_DATA_LEN
<< 2] = {};
841 __u8 tmp_in
[TEST_DATA_LEN
] = {};
842 int err
, saved_errno
;
843 LIBBPF_OPTS(bpf_test_run_opts
, topts
,
845 .data_size_in
= sizeof(tmp_in
),
847 .data_size_out
= sizeof(tmp_out
),
852 memset(&topts
, 0, sizeof(struct bpf_test_run_opts
));
853 topts
.sz
= sizeof(struct bpf_test_run_opts
);
855 err
= bpf_prog_test_run_opts(fd_prog
, &topts
);
859 PRINT_FAIL("FAIL: Unexpected bpf_prog_test_run error: %d (%s) ",
860 saved_errno
, strerror(saved_errno
));
864 ASSERT_OK(0, "bpf_prog_test_run");
865 *retval
= topts
.retval
;
870 static bool should_do_test_run(struct test_spec
*spec
, struct test_subspec
*subspec
)
872 if (!subspec
->execute
)
875 if (subspec
->expect_failure
)
878 if ((spec
->prog_flags
& BPF_F_ANY_ALIGNMENT
) && !EFFICIENT_UNALIGNED_ACCESS
) {
879 if (env
.verbosity
!= VERBOSE_NONE
)
880 printf("alignment prevents execution\n");
887 /* Get a disassembly of BPF program after verifier applies all rewrites */
888 static int get_xlated_program_text(int prog_fd
, char *text
, size_t text_sz
)
890 struct bpf_insn
*insn_start
= NULL
, *insn
, *insn_end
;
891 __u32 insns_cnt
= 0, i
;
896 err
= get_xlated_program(prog_fd
, &insn_start
, &insns_cnt
);
897 if (!ASSERT_OK(err
, "get_xlated_program"))
899 out
= fmemopen(text
, text_sz
, "w");
900 if (!ASSERT_OK_PTR(out
, "open_memstream"))
902 insn_end
= insn_start
+ insns_cnt
;
904 while (insn
< insn_end
) {
905 i
= insn
- insn_start
;
906 insn
= disasm_insn(insn
, buf
, sizeof(buf
));
907 fprintf(out
, "%d: %s\n", i
, buf
);
918 /* this function is forced noinline and has short generic name to look better
919 * in test_progs output (in case of a failure)
922 void run_subtest(struct test_loader
*tester
,
923 struct bpf_object_open_opts
*open_opts
,
924 const void *obj_bytes
,
926 struct test_spec
*specs
,
927 struct test_spec
*spec
,
930 struct test_subspec
*subspec
= unpriv
? &spec
->unpriv
: &spec
->priv
;
931 struct bpf_program
*tprog
= NULL
, *tprog_iter
;
932 struct bpf_link
*link
, *links
[32] = {};
933 struct test_spec
*spec_iter
;
934 struct cap_state caps
= {};
935 struct bpf_object
*tobj
;
941 if (!test__start_subtest(subspec
->name
))
944 if ((get_current_arch() & spec
->arch_mask
) == 0) {
950 if (!can_execute_unpriv(tester
, spec
)) {
955 if (drop_capabilities(&caps
)) {
960 err
= cap_enable_effective(subspec
->caps
, NULL
);
962 PRINT_FAIL("failed to set capabilities: %i, %s\n", err
, strerror(err
));
963 goto subtest_cleanup
;
968 /* Implicitly reset to NULL if next test case doesn't specify */
969 open_opts
->btf_custom_path
= spec
->btf_custom_path
;
971 tobj
= bpf_object__open_mem(obj_bytes
, obj_byte_cnt
, open_opts
);
972 if (!ASSERT_OK_PTR(tobj
, "obj_open_mem")) /* shouldn't happen */
973 goto subtest_cleanup
;
976 bpf_object__for_each_program(tprog_iter
, tobj
) {
977 spec_iter
= &specs
[i
++];
980 if (spec_iter
->valid
) {
981 if (strcmp(bpf_program__name(tprog_iter
), spec
->prog_name
) == 0) {
986 if (spec_iter
->auxiliary
&&
987 spec_iter
->mode_mask
& (unpriv
? UNPRIV
: PRIV
))
991 bpf_program__set_autoload(tprog_iter
, should_load
);
994 prepare_case(tester
, spec
, tobj
, tprog
);
996 /* By default bpf_object__load() automatically creates all
997 * maps declared in the skeleton. Some map types are only
998 * allowed in priv mode. Disable autoload for such maps in
1001 bpf_object__for_each_map(map
, tobj
)
1002 bpf_map__set_autocreate(map
, !unpriv
|| is_unpriv_capable_map(map
));
1004 err
= bpf_object__load(tobj
);
1005 if (subspec
->expect_failure
) {
1006 if (!ASSERT_ERR(err
, "unexpected_load_success")) {
1007 emit_verifier_log(tester
->log_buf
, false /*force*/);
1011 if (!ASSERT_OK(err
, "unexpected_load_failure")) {
1012 emit_verifier_log(tester
->log_buf
, true /*force*/);
1016 emit_verifier_log(tester
->log_buf
, false /*force*/);
1017 validate_msgs(tester
->log_buf
, &subspec
->expect_msgs
, emit_verifier_log
);
1019 if (subspec
->expect_xlated
.cnt
) {
1020 err
= get_xlated_program_text(bpf_program__fd(tprog
),
1021 tester
->log_buf
, tester
->log_buf_sz
);
1024 emit_xlated(tester
->log_buf
, false /*force*/);
1025 validate_msgs(tester
->log_buf
, &subspec
->expect_xlated
, emit_xlated
);
1028 if (subspec
->jited
.cnt
) {
1029 err
= get_jited_program_text(bpf_program__fd(tprog
),
1030 tester
->log_buf
, tester
->log_buf_sz
);
1031 if (err
== -EOPNOTSUPP
) {
1032 printf("%s:SKIP: jited programs disassembly is not supported,\n", __func__
);
1033 printf("%s:SKIP: tests are built w/o LLVM development libs\n", __func__
);
1037 if (!ASSERT_EQ(err
, 0, "get_jited_program_text"))
1039 emit_jited(tester
->log_buf
, false /*force*/);
1040 validate_msgs(tester
->log_buf
, &subspec
->jited
, emit_jited
);
1043 if (should_do_test_run(spec
, subspec
)) {
1044 /* For some reason test_verifier executes programs
1045 * with all capabilities restored. Do the same here.
1047 if (restore_capabilities(&caps
))
1050 /* Do bpf_map__attach_struct_ops() for each struct_ops map.
1051 * This should trigger bpf_struct_ops->reg callback on kernel side.
1053 bpf_object__for_each_map(map
, tobj
) {
1054 if (!bpf_map__autocreate(map
) ||
1055 bpf_map__type(map
) != BPF_MAP_TYPE_STRUCT_OPS
)
1057 if (links_cnt
>= ARRAY_SIZE(links
)) {
1058 PRINT_FAIL("too many struct_ops maps");
1061 link
= bpf_map__attach_struct_ops(map
);
1063 PRINT_FAIL("bpf_map__attach_struct_ops failed for map %s: err=%d\n",
1064 bpf_map__name(map
), err
);
1067 links
[links_cnt
++] = link
;
1070 if (tester
->pre_execution_cb
) {
1071 err
= tester
->pre_execution_cb(tobj
);
1073 PRINT_FAIL("pre_execution_cb failed: %d\n", err
);
1078 do_prog_test_run(bpf_program__fd(tprog
), &retval
,
1079 bpf_program__type(tprog
) == BPF_PROG_TYPE_SYSCALL
? true : false);
1080 if (retval
!= subspec
->retval
&& subspec
->retval
!= POINTER_VALUE
) {
1081 PRINT_FAIL("Unexpected retval: %d != %d\n", retval
, subspec
->retval
);
1084 /* redo bpf_map__attach_struct_ops for each test */
1085 while (links_cnt
> 0)
1086 bpf_link__destroy(links
[--links_cnt
]);
1090 while (links_cnt
> 0)
1091 bpf_link__destroy(links
[--links_cnt
]);
1092 bpf_object__close(tobj
);
1094 test__end_subtest();
1095 restore_capabilities(&caps
);
1098 static void process_subtest(struct test_loader
*tester
,
1099 const char *skel_name
,
1100 skel_elf_bytes_fn elf_bytes_factory
)
1102 LIBBPF_OPTS(bpf_object_open_opts
, open_opts
, .object_name
= skel_name
);
1103 struct test_spec
*specs
= NULL
;
1104 struct bpf_object
*obj
= NULL
;
1105 struct bpf_program
*prog
;
1106 const void *obj_bytes
;
1107 int err
, i
, nr_progs
;
1108 size_t obj_byte_cnt
;
1110 if (tester_init(tester
) < 0)
1111 return; /* failed to initialize tester */
1113 obj_bytes
= elf_bytes_factory(&obj_byte_cnt
);
1114 obj
= bpf_object__open_mem(obj_bytes
, obj_byte_cnt
, &open_opts
);
1115 if (!ASSERT_OK_PTR(obj
, "obj_open_mem"))
1119 bpf_object__for_each_program(prog
, obj
)
1122 specs
= calloc(nr_progs
, sizeof(struct test_spec
));
1123 if (!ASSERT_OK_PTR(specs
, "specs_alloc"))
1127 bpf_object__for_each_program(prog
, obj
) {
1128 /* ignore tests for which we can't derive test specification */
1129 err
= parse_test_spec(tester
, obj
, prog
, &specs
[i
++]);
1131 PRINT_FAIL("Can't parse test spec for program '%s'\n",
1132 bpf_program__name(prog
));
1136 bpf_object__for_each_program(prog
, obj
) {
1137 struct test_spec
*spec
= &specs
[i
++];
1139 if (!spec
->valid
|| spec
->auxiliary
)
1142 if (spec
->mode_mask
& PRIV
)
1143 run_subtest(tester
, &open_opts
, obj_bytes
, obj_byte_cnt
,
1144 specs
, spec
, false);
1145 if (spec
->mode_mask
& UNPRIV
)
1146 run_subtest(tester
, &open_opts
, obj_bytes
, obj_byte_cnt
,
1151 for (i
= 0; i
< nr_progs
; ++i
)
1152 free_test_spec(&specs
[i
]);
1154 bpf_object__close(obj
);
1157 void test_loader__run_subtests(struct test_loader
*tester
,
1158 const char *skel_name
,
1159 skel_elf_bytes_fn elf_bytes_factory
)
1161 /* see comment in run_subtest() for why we do this function nesting */
1162 process_subtest(tester
, skel_name
, elf_bytes_factory
);