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="
40 /* Warning: duplicated in bpf_misc.h */
41 #define POINTER_VALUE 0xcafe4all
42 #define TEST_DATA_LEN 64
44 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
45 #define EFFICIENT_UNALIGNED_ACCESS 1
47 #define EFFICIENT_UNALIGNED_ACCESS 0
50 static int sysctl_unpriv_disabled
= -1;
58 const char *substr
; /* substring match */
64 struct expected_msgs
{
65 struct expect_msg
*patterns
;
72 struct expected_msgs expect_msgs
;
73 struct expected_msgs expect_xlated
;
74 struct expected_msgs jited
;
80 const char *prog_name
;
81 struct test_subspec priv
;
82 struct test_subspec unpriv
;
83 const char *btf_custom_path
;
92 static int tester_init(struct test_loader
*tester
)
94 if (!tester
->log_buf
) {
95 tester
->log_buf_sz
= TEST_LOADER_LOG_BUF_SZ
;
96 tester
->log_buf
= calloc(tester
->log_buf_sz
, 1);
97 if (!ASSERT_OK_PTR(tester
->log_buf
, "tester_log_buf"))
104 void test_loader_fini(struct test_loader
*tester
)
109 free(tester
->log_buf
);
112 static void free_msgs(struct expected_msgs
*msgs
)
116 for (i
= 0; i
< msgs
->cnt
; i
++)
117 if (msgs
->patterns
[i
].is_regex
)
118 regfree(&msgs
->patterns
[i
].regex
);
119 free(msgs
->patterns
);
120 msgs
->patterns
= NULL
;
124 static void free_test_spec(struct test_spec
*spec
)
126 /* Deallocate expect_msgs arrays. */
127 free_msgs(&spec
->priv
.expect_msgs
);
128 free_msgs(&spec
->unpriv
.expect_msgs
);
129 free_msgs(&spec
->priv
.expect_xlated
);
130 free_msgs(&spec
->unpriv
.expect_xlated
);
131 free_msgs(&spec
->priv
.jited
);
132 free_msgs(&spec
->unpriv
.jited
);
134 free(spec
->priv
.name
);
135 free(spec
->unpriv
.name
);
136 spec
->priv
.name
= NULL
;
137 spec
->unpriv
.name
= NULL
;
140 /* Compiles regular expression matching pattern.
141 * Pattern has a special syntax:
143 * pattern := (<verbatim text> | regex)*
144 * regex := "{{" <posix extended regular expression> "}}"
146 * In other words, pattern is a verbatim text with inclusion
147 * of regular expressions enclosed in "{{" "}}" pairs.
148 * For example, pattern "foo{{[0-9]+}}" matches strings like
149 * "foo0", "foo007", etc.
151 static int compile_regex(const char *pattern
, regex_t
*regex
)
153 char err_buf
[256], buf
[256] = {}, *ptr
, *buf_end
;
154 const char *original_pattern
= pattern
;
155 bool in_regex
= false;
158 buf_end
= buf
+ sizeof(buf
);
160 while (*pattern
&& ptr
< buf_end
- 2) {
161 if (!in_regex
&& str_has_pfx(pattern
, "{{")) {
166 if (in_regex
&& str_has_pfx(pattern
, "}}")) {
175 /* list of characters that need escaping for extended posix regex */
176 if (strchr(".[]\\()*+?{}|^$", *pattern
)) {
184 PRINT_FAIL("Regexp too long: '%s'\n", original_pattern
);
188 PRINT_FAIL("Regexp has open '{{' but no closing '}}': '%s'\n", original_pattern
);
191 err
= regcomp(regex
, buf
, REG_EXTENDED
| REG_NEWLINE
);
193 regerror(err
, regex
, err_buf
, sizeof(err_buf
));
194 PRINT_FAIL("Regexp compilation error in '%s': '%s'\n", buf
, err_buf
);
200 static int __push_msg(const char *pattern
, bool on_next_line
, struct expected_msgs
*msgs
)
202 struct expect_msg
*msg
;
206 tmp
= realloc(msgs
->patterns
,
207 (1 + msgs
->cnt
) * sizeof(struct expect_msg
));
209 ASSERT_FAIL("failed to realloc memory for messages\n");
212 msgs
->patterns
= tmp
;
213 msg
= &msgs
->patterns
[msgs
->cnt
];
214 msg
->on_next_line
= on_next_line
;
215 msg
->substr
= pattern
;
216 msg
->is_regex
= false;
217 if (strstr(pattern
, "{{")) {
218 err
= compile_regex(pattern
, &msg
->regex
);
221 msg
->is_regex
= true;
227 static int clone_msgs(struct expected_msgs
*from
, struct expected_msgs
*to
)
229 struct expect_msg
*msg
;
232 for (i
= 0; i
< from
->cnt
; i
++) {
233 msg
= &from
->patterns
[i
];
234 err
= __push_msg(msg
->substr
, msg
->on_next_line
, to
);
241 static int push_msg(const char *substr
, struct expected_msgs
*msgs
)
243 return __push_msg(substr
, false, msgs
);
246 static int push_disasm_msg(const char *regex_str
, bool *on_next_line
, struct expected_msgs
*msgs
)
250 if (strcmp(regex_str
, "...") == 0) {
251 *on_next_line
= false;
254 err
= __push_msg(regex_str
, *on_next_line
, msgs
);
257 *on_next_line
= true;
261 static int parse_int(const char *str
, int *val
, const char *name
)
267 if (str_has_pfx(str
, "0x"))
268 tmp
= strtol(str
+ 2, &end
, 16);
270 tmp
= strtol(str
, &end
, 10);
271 if (errno
|| end
[0] != '\0') {
272 PRINT_FAIL("failed to parse %s from '%s'\n", name
, str
);
279 static int parse_retval(const char *str
, int *val
, const char *name
)
285 { "INT_MIN" , INT_MIN
},
286 { "POINTER_VALUE", POINTER_VALUE
},
287 { "TEST_DATA_LEN", TEST_DATA_LEN
},
291 for (i
= 0; i
< ARRAY_SIZE(named_values
); ++i
) {
292 if (strcmp(str
, named_values
[i
].name
) != 0)
294 *val
= named_values
[i
].val
;
298 return parse_int(str
, val
, name
);
301 static void update_flags(int *flags
, int flag
, bool clear
)
309 /* Matches a string of form '<pfx>[^=]=.*' and returns it's suffix.
310 * Used to parse btf_decl_tag values.
311 * Such values require unique prefix because compiler does not add
312 * same __attribute__((btf_decl_tag(...))) twice.
313 * Test suite uses two-component tags for such cases:
315 * <pfx> __COUNTER__ '='
317 * For example, two consecutive __msg tags '__msg("foo") __msg("foo")'
318 * would be encoded as:
320 * [18] DECL_TAG 'comment:test_expect_msg=0=foo' type_id=15 component_idx=-1
321 * [19] DECL_TAG 'comment:test_expect_msg=1=foo' type_id=15 component_idx=-1
323 * And the purpose of this function is to extract 'foo' from the above.
325 static const char *skip_dynamic_pfx(const char *s
, const char *pfx
)
329 if (strncmp(s
, pfx
, strlen(pfx
)) != 0)
331 msg
= s
+ strlen(pfx
);
332 msg
= strchr(msg
, '=');
345 static int get_current_arch(void)
347 #if defined(__x86_64__)
349 #elif defined(__aarch64__)
351 #elif defined(__riscv) && __riscv_xlen == 64
357 /* Uses btf_decl_tag attributes to describe the expected test
358 * behavior, see bpf_misc.h for detailed description of each attribute
359 * and attribute combinations.
361 static int parse_test_spec(struct test_loader
*tester
,
362 struct bpf_object
*obj
,
363 struct bpf_program
*prog
,
364 struct test_spec
*spec
)
366 const char *description
= NULL
;
367 bool has_unpriv_result
= false;
368 bool has_unpriv_retval
= false;
369 bool unpriv_xlated_on_next_line
= true;
370 bool xlated_on_next_line
= true;
371 bool unpriv_jit_on_next_line
;
372 bool jit_on_next_line
;
373 bool collect_jit
= false;
374 int func_id
, i
, err
= 0;
379 memset(spec
, 0, sizeof(*spec
));
381 spec
->prog_name
= bpf_program__name(prog
);
382 spec
->prog_flags
= testing_prog_flags();
384 btf
= bpf_object__btf(obj
);
386 ASSERT_FAIL("BPF object has no BTF");
390 func_id
= btf__find_by_name_kind(btf
, spec
->prog_name
, BTF_KIND_FUNC
);
392 ASSERT_FAIL("failed to find FUNC BTF type for '%s'", spec
->prog_name
);
396 for (i
= 1; i
< btf__type_cnt(btf
); i
++) {
397 const char *s
, *val
, *msg
;
398 const struct btf_type
*t
;
402 t
= btf__type_by_id(btf
, i
);
403 if (!btf_is_decl_tag(t
))
406 if (t
->type
!= func_id
|| btf_decl_tag(t
)->component_idx
!= -1)
409 s
= btf__str_by_offset(btf
, t
->name_off
);
410 if (str_has_pfx(s
, TEST_TAG_DESCRIPTION_PFX
)) {
411 description
= s
+ sizeof(TEST_TAG_DESCRIPTION_PFX
) - 1;
412 } else if (strcmp(s
, TEST_TAG_EXPECT_FAILURE
) == 0) {
413 spec
->priv
.expect_failure
= true;
414 spec
->mode_mask
|= PRIV
;
415 } else if (strcmp(s
, TEST_TAG_EXPECT_SUCCESS
) == 0) {
416 spec
->priv
.expect_failure
= false;
417 spec
->mode_mask
|= PRIV
;
418 } else if (strcmp(s
, TEST_TAG_EXPECT_FAILURE_UNPRIV
) == 0) {
419 spec
->unpriv
.expect_failure
= true;
420 spec
->mode_mask
|= UNPRIV
;
421 has_unpriv_result
= true;
422 } else if (strcmp(s
, TEST_TAG_EXPECT_SUCCESS_UNPRIV
) == 0) {
423 spec
->unpriv
.expect_failure
= false;
424 spec
->mode_mask
|= UNPRIV
;
425 has_unpriv_result
= true;
426 } else if (strcmp(s
, TEST_TAG_AUXILIARY
) == 0) {
427 spec
->auxiliary
= true;
428 spec
->mode_mask
|= PRIV
;
429 } else if (strcmp(s
, TEST_TAG_AUXILIARY_UNPRIV
) == 0) {
430 spec
->auxiliary
= true;
431 spec
->mode_mask
|= UNPRIV
;
432 } else if ((msg
= skip_dynamic_pfx(s
, TEST_TAG_EXPECT_MSG_PFX
))) {
433 err
= push_msg(msg
, &spec
->priv
.expect_msgs
);
436 spec
->mode_mask
|= PRIV
;
437 } else if ((msg
= skip_dynamic_pfx(s
, TEST_TAG_EXPECT_MSG_PFX_UNPRIV
))) {
438 err
= push_msg(msg
, &spec
->unpriv
.expect_msgs
);
441 spec
->mode_mask
|= UNPRIV
;
442 } else if ((msg
= skip_dynamic_pfx(s
, TEST_TAG_JITED_PFX
))) {
443 if (arch_mask
== 0) {
444 PRINT_FAIL("__jited used before __arch_*");
448 err
= push_disasm_msg(msg
, &jit_on_next_line
,
452 spec
->mode_mask
|= PRIV
;
454 } else if ((msg
= skip_dynamic_pfx(s
, TEST_TAG_JITED_PFX_UNPRIV
))) {
455 if (arch_mask
== 0) {
456 PRINT_FAIL("__unpriv_jited used before __arch_*");
460 err
= push_disasm_msg(msg
, &unpriv_jit_on_next_line
,
461 &spec
->unpriv
.jited
);
464 spec
->mode_mask
|= UNPRIV
;
466 } else if ((msg
= skip_dynamic_pfx(s
, TEST_TAG_EXPECT_XLATED_PFX
))) {
467 err
= push_disasm_msg(msg
, &xlated_on_next_line
,
468 &spec
->priv
.expect_xlated
);
471 spec
->mode_mask
|= PRIV
;
472 } else if ((msg
= skip_dynamic_pfx(s
, TEST_TAG_EXPECT_XLATED_PFX_UNPRIV
))) {
473 err
= push_disasm_msg(msg
, &unpriv_xlated_on_next_line
,
474 &spec
->unpriv
.expect_xlated
);
477 spec
->mode_mask
|= UNPRIV
;
478 } else if (str_has_pfx(s
, TEST_TAG_RETVAL_PFX
)) {
479 val
= s
+ sizeof(TEST_TAG_RETVAL_PFX
) - 1;
480 err
= parse_retval(val
, &spec
->priv
.retval
, "__retval");
483 spec
->priv
.execute
= true;
484 spec
->mode_mask
|= PRIV
;
485 } else if (str_has_pfx(s
, TEST_TAG_RETVAL_PFX_UNPRIV
)) {
486 val
= s
+ sizeof(TEST_TAG_RETVAL_PFX_UNPRIV
) - 1;
487 err
= parse_retval(val
, &spec
->unpriv
.retval
, "__retval_unpriv");
490 spec
->mode_mask
|= UNPRIV
;
491 spec
->unpriv
.execute
= true;
492 has_unpriv_retval
= true;
493 } else if (str_has_pfx(s
, TEST_TAG_LOG_LEVEL_PFX
)) {
494 val
= s
+ sizeof(TEST_TAG_LOG_LEVEL_PFX
) - 1;
495 err
= parse_int(val
, &spec
->log_level
, "test log level");
498 } else if (str_has_pfx(s
, TEST_TAG_PROG_FLAGS_PFX
)) {
499 val
= s
+ sizeof(TEST_TAG_PROG_FLAGS_PFX
) - 1;
501 clear
= val
[0] == '!';
505 if (strcmp(val
, "BPF_F_STRICT_ALIGNMENT") == 0) {
506 update_flags(&spec
->prog_flags
, BPF_F_STRICT_ALIGNMENT
, clear
);
507 } else if (strcmp(val
, "BPF_F_ANY_ALIGNMENT") == 0) {
508 update_flags(&spec
->prog_flags
, BPF_F_ANY_ALIGNMENT
, clear
);
509 } else if (strcmp(val
, "BPF_F_TEST_RND_HI32") == 0) {
510 update_flags(&spec
->prog_flags
, BPF_F_TEST_RND_HI32
, clear
);
511 } else if (strcmp(val
, "BPF_F_TEST_STATE_FREQ") == 0) {
512 update_flags(&spec
->prog_flags
, BPF_F_TEST_STATE_FREQ
, clear
);
513 } else if (strcmp(val
, "BPF_F_SLEEPABLE") == 0) {
514 update_flags(&spec
->prog_flags
, BPF_F_SLEEPABLE
, clear
);
515 } else if (strcmp(val
, "BPF_F_XDP_HAS_FRAGS") == 0) {
516 update_flags(&spec
->prog_flags
, BPF_F_XDP_HAS_FRAGS
, clear
);
517 } else if (strcmp(val
, "BPF_F_TEST_REG_INVARIANTS") == 0) {
518 update_flags(&spec
->prog_flags
, BPF_F_TEST_REG_INVARIANTS
, clear
);
519 } else /* assume numeric value */ {
520 err
= parse_int(val
, &flags
, "test prog flags");
523 update_flags(&spec
->prog_flags
, flags
, clear
);
525 } else if (str_has_pfx(s
, TEST_TAG_ARCH
)) {
526 val
= s
+ sizeof(TEST_TAG_ARCH
) - 1;
527 if (strcmp(val
, "X86_64") == 0) {
529 } else if (strcmp(val
, "ARM64") == 0) {
531 } else if (strcmp(val
, "RISCV64") == 0) {
534 PRINT_FAIL("bad arch spec: '%s'", val
);
539 collect_jit
= get_current_arch() == arch
;
540 unpriv_jit_on_next_line
= true;
541 jit_on_next_line
= true;
542 } else if (str_has_pfx(s
, TEST_BTF_PATH
)) {
543 spec
->btf_custom_path
= s
+ sizeof(TEST_BTF_PATH
) - 1;
547 spec
->arch_mask
= arch_mask
?: -1;
549 if (spec
->mode_mask
== 0)
550 spec
->mode_mask
= PRIV
;
553 description
= spec
->prog_name
;
555 if (spec
->mode_mask
& PRIV
) {
556 spec
->priv
.name
= strdup(description
);
557 if (!spec
->priv
.name
) {
558 PRINT_FAIL("failed to allocate memory for priv.name\n");
564 if (spec
->mode_mask
& UNPRIV
) {
565 int descr_len
= strlen(description
);
566 const char *suffix
= " @unpriv";
569 name
= malloc(descr_len
+ strlen(suffix
) + 1);
571 PRINT_FAIL("failed to allocate memory for unpriv.name\n");
576 strcpy(name
, description
);
577 strcpy(&name
[descr_len
], suffix
);
578 spec
->unpriv
.name
= name
;
581 if (spec
->mode_mask
& (PRIV
| UNPRIV
)) {
582 if (!has_unpriv_result
)
583 spec
->unpriv
.expect_failure
= spec
->priv
.expect_failure
;
585 if (!has_unpriv_retval
) {
586 spec
->unpriv
.retval
= spec
->priv
.retval
;
587 spec
->unpriv
.execute
= spec
->priv
.execute
;
590 if (spec
->unpriv
.expect_msgs
.cnt
== 0)
591 clone_msgs(&spec
->priv
.expect_msgs
, &spec
->unpriv
.expect_msgs
);
592 if (spec
->unpriv
.expect_xlated
.cnt
== 0)
593 clone_msgs(&spec
->priv
.expect_xlated
, &spec
->unpriv
.expect_xlated
);
594 if (spec
->unpriv
.jited
.cnt
== 0)
595 clone_msgs(&spec
->priv
.jited
, &spec
->unpriv
.jited
);
603 free_test_spec(spec
);
607 static void prepare_case(struct test_loader
*tester
,
608 struct test_spec
*spec
,
609 struct bpf_object
*obj
,
610 struct bpf_program
*prog
)
612 int min_log_level
= 0, prog_flags
;
614 if (env
.verbosity
> VERBOSE_NONE
)
616 if (env
.verbosity
> VERBOSE_VERY
)
619 bpf_program__set_log_buf(prog
, tester
->log_buf
, tester
->log_buf_sz
);
621 /* Make sure we set at least minimal log level, unless test requires
622 * even higher level already. Make sure to preserve independent log
623 * level 4 (verifier stats), though.
625 if ((spec
->log_level
& 3) < min_log_level
)
626 bpf_program__set_log_level(prog
, (spec
->log_level
& 4) | min_log_level
);
628 bpf_program__set_log_level(prog
, spec
->log_level
);
630 prog_flags
= bpf_program__flags(prog
);
631 bpf_program__set_flags(prog
, prog_flags
| spec
->prog_flags
);
633 tester
->log_buf
[0] = '\0';
636 static void emit_verifier_log(const char *log_buf
, bool force
)
638 if (!force
&& env
.verbosity
== VERBOSE_NONE
)
640 fprintf(stdout
, "VERIFIER LOG:\n=============\n%s=============\n", log_buf
);
643 static void emit_xlated(const char *xlated
, bool force
)
645 if (!force
&& env
.verbosity
== VERBOSE_NONE
)
647 fprintf(stdout
, "XLATED:\n=============\n%s=============\n", xlated
);
650 static void emit_jited(const char *jited
, bool force
)
652 if (!force
&& env
.verbosity
== VERBOSE_NONE
)
654 fprintf(stdout
, "JITED:\n=============\n%s=============\n", jited
);
657 static void validate_msgs(char *log_buf
, struct expected_msgs
*msgs
,
658 void (*emit_fn
)(const char *buf
, bool force
))
660 const char *log
= log_buf
, *prev_match
;
661 regmatch_t reg_match
[1];
666 prev_match_line
= -1;
669 for (i
= 0; i
< msgs
->cnt
; i
++) {
670 struct expect_msg
*msg
= &msgs
->patterns
[i
];
671 const char *match
= NULL
, *pat_status
;
672 bool wrong_line
= false;
674 if (!msg
->is_regex
) {
675 match
= strstr(log
, msg
->substr
);
677 log
= match
+ strlen(msg
->substr
);
679 err
= regexec(&msg
->regex
, log
, 1, reg_match
, 0);
681 match
= log
+ reg_match
[0].rm_so
;
682 log
+= reg_match
[0].rm_eo
;
687 for (; prev_match
< match
; ++prev_match
)
688 if (*prev_match
== '\n')
690 wrong_line
= msg
->on_next_line
&& prev_match_line
>= 0 &&
691 prev_match_line
+ 1 != match_line
;
694 if (!match
|| wrong_line
) {
695 PRINT_FAIL("expect_msg\n");
696 if (env
.verbosity
== VERBOSE_NONE
)
697 emit_fn(log_buf
, true /*force*/);
698 for (j
= 0; j
<= i
; j
++) {
699 msg
= &msgs
->patterns
[j
];
701 pat_status
= "MATCHED ";
703 pat_status
= "WRONG LINE";
705 pat_status
= "EXPECTED ";
706 msg
= &msgs
->patterns
[j
];
707 fprintf(stderr
, "%s %s: '%s'\n",
709 msg
->is_regex
? " REGEX" : "SUBSTR",
714 "expecting match at line %d, actual match is at line %d\n",
715 prev_match_line
+ 1, match_line
);
720 prev_match_line
= match_line
;
729 static int drop_capabilities(struct cap_state
*caps
)
731 const __u64 caps_to_drop
= (1ULL << CAP_SYS_ADMIN
| 1ULL << CAP_NET_ADMIN
|
732 1ULL << CAP_PERFMON
| 1ULL << CAP_BPF
);
735 err
= cap_disable_effective(caps_to_drop
, &caps
->old_caps
);
737 PRINT_FAIL("failed to drop capabilities: %i, %s\n", err
, strerror(err
));
741 caps
->initialized
= true;
745 static int restore_capabilities(struct cap_state
*caps
)
749 if (!caps
->initialized
)
752 err
= cap_enable_effective(caps
->old_caps
, NULL
);
754 PRINT_FAIL("failed to restore capabilities: %i, %s\n", err
, strerror(err
));
755 caps
->initialized
= false;
759 static bool can_execute_unpriv(struct test_loader
*tester
, struct test_spec
*spec
)
761 if (sysctl_unpriv_disabled
< 0)
762 sysctl_unpriv_disabled
= get_unpriv_disabled() ? 1 : 0;
763 if (sysctl_unpriv_disabled
)
765 if ((spec
->prog_flags
& BPF_F_ANY_ALIGNMENT
) && !EFFICIENT_UNALIGNED_ACCESS
)
770 static bool is_unpriv_capable_map(struct bpf_map
*map
)
772 enum bpf_map_type type
;
775 type
= bpf_map__type(map
);
778 case BPF_MAP_TYPE_HASH
:
779 case BPF_MAP_TYPE_PERCPU_HASH
:
780 case BPF_MAP_TYPE_HASH_OF_MAPS
:
781 flags
= bpf_map__map_flags(map
);
782 return !(flags
& BPF_F_ZERO_SEED
);
783 case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE
:
784 case BPF_MAP_TYPE_ARRAY
:
785 case BPF_MAP_TYPE_RINGBUF
:
786 case BPF_MAP_TYPE_PROG_ARRAY
:
787 case BPF_MAP_TYPE_CGROUP_ARRAY
:
788 case BPF_MAP_TYPE_PERCPU_ARRAY
:
789 case BPF_MAP_TYPE_USER_RINGBUF
:
790 case BPF_MAP_TYPE_ARRAY_OF_MAPS
:
791 case BPF_MAP_TYPE_CGROUP_STORAGE
:
792 case BPF_MAP_TYPE_PERF_EVENT_ARRAY
:
799 static int do_prog_test_run(int fd_prog
, int *retval
, bool empty_opts
)
801 __u8 tmp_out
[TEST_DATA_LEN
<< 2] = {};
802 __u8 tmp_in
[TEST_DATA_LEN
] = {};
803 int err
, saved_errno
;
804 LIBBPF_OPTS(bpf_test_run_opts
, topts
,
806 .data_size_in
= sizeof(tmp_in
),
808 .data_size_out
= sizeof(tmp_out
),
813 memset(&topts
, 0, sizeof(struct bpf_test_run_opts
));
814 topts
.sz
= sizeof(struct bpf_test_run_opts
);
816 err
= bpf_prog_test_run_opts(fd_prog
, &topts
);
820 PRINT_FAIL("FAIL: Unexpected bpf_prog_test_run error: %d (%s) ",
821 saved_errno
, strerror(saved_errno
));
825 ASSERT_OK(0, "bpf_prog_test_run");
826 *retval
= topts
.retval
;
831 static bool should_do_test_run(struct test_spec
*spec
, struct test_subspec
*subspec
)
833 if (!subspec
->execute
)
836 if (subspec
->expect_failure
)
839 if ((spec
->prog_flags
& BPF_F_ANY_ALIGNMENT
) && !EFFICIENT_UNALIGNED_ACCESS
) {
840 if (env
.verbosity
!= VERBOSE_NONE
)
841 printf("alignment prevents execution\n");
848 /* Get a disassembly of BPF program after verifier applies all rewrites */
849 static int get_xlated_program_text(int prog_fd
, char *text
, size_t text_sz
)
851 struct bpf_insn
*insn_start
= NULL
, *insn
, *insn_end
;
852 __u32 insns_cnt
= 0, i
;
857 err
= get_xlated_program(prog_fd
, &insn_start
, &insns_cnt
);
858 if (!ASSERT_OK(err
, "get_xlated_program"))
860 out
= fmemopen(text
, text_sz
, "w");
861 if (!ASSERT_OK_PTR(out
, "open_memstream"))
863 insn_end
= insn_start
+ insns_cnt
;
865 while (insn
< insn_end
) {
866 i
= insn
- insn_start
;
867 insn
= disasm_insn(insn
, buf
, sizeof(buf
));
868 fprintf(out
, "%d: %s\n", i
, buf
);
879 /* this function is forced noinline and has short generic name to look better
880 * in test_progs output (in case of a failure)
883 void run_subtest(struct test_loader
*tester
,
884 struct bpf_object_open_opts
*open_opts
,
885 const void *obj_bytes
,
887 struct test_spec
*specs
,
888 struct test_spec
*spec
,
891 struct test_subspec
*subspec
= unpriv
? &spec
->unpriv
: &spec
->priv
;
892 struct bpf_program
*tprog
= NULL
, *tprog_iter
;
893 struct bpf_link
*link
, *links
[32] = {};
894 struct test_spec
*spec_iter
;
895 struct cap_state caps
= {};
896 struct bpf_object
*tobj
;
902 if (!test__start_subtest(subspec
->name
))
905 if ((get_current_arch() & spec
->arch_mask
) == 0) {
911 if (!can_execute_unpriv(tester
, spec
)) {
916 if (drop_capabilities(&caps
)) {
922 /* Implicitly reset to NULL if next test case doesn't specify */
923 open_opts
->btf_custom_path
= spec
->btf_custom_path
;
925 tobj
= bpf_object__open_mem(obj_bytes
, obj_byte_cnt
, open_opts
);
926 if (!ASSERT_OK_PTR(tobj
, "obj_open_mem")) /* shouldn't happen */
927 goto subtest_cleanup
;
930 bpf_object__for_each_program(tprog_iter
, tobj
) {
931 spec_iter
= &specs
[i
++];
934 if (spec_iter
->valid
) {
935 if (strcmp(bpf_program__name(tprog_iter
), spec
->prog_name
) == 0) {
940 if (spec_iter
->auxiliary
&&
941 spec_iter
->mode_mask
& (unpriv
? UNPRIV
: PRIV
))
945 bpf_program__set_autoload(tprog_iter
, should_load
);
948 prepare_case(tester
, spec
, tobj
, tprog
);
950 /* By default bpf_object__load() automatically creates all
951 * maps declared in the skeleton. Some map types are only
952 * allowed in priv mode. Disable autoload for such maps in
955 bpf_object__for_each_map(map
, tobj
)
956 bpf_map__set_autocreate(map
, !unpriv
|| is_unpriv_capable_map(map
));
958 err
= bpf_object__load(tobj
);
959 if (subspec
->expect_failure
) {
960 if (!ASSERT_ERR(err
, "unexpected_load_success")) {
961 emit_verifier_log(tester
->log_buf
, false /*force*/);
965 if (!ASSERT_OK(err
, "unexpected_load_failure")) {
966 emit_verifier_log(tester
->log_buf
, true /*force*/);
970 emit_verifier_log(tester
->log_buf
, false /*force*/);
971 validate_msgs(tester
->log_buf
, &subspec
->expect_msgs
, emit_verifier_log
);
973 if (subspec
->expect_xlated
.cnt
) {
974 err
= get_xlated_program_text(bpf_program__fd(tprog
),
975 tester
->log_buf
, tester
->log_buf_sz
);
978 emit_xlated(tester
->log_buf
, false /*force*/);
979 validate_msgs(tester
->log_buf
, &subspec
->expect_xlated
, emit_xlated
);
982 if (subspec
->jited
.cnt
) {
983 err
= get_jited_program_text(bpf_program__fd(tprog
),
984 tester
->log_buf
, tester
->log_buf_sz
);
985 if (err
== -EOPNOTSUPP
) {
986 printf("%s:SKIP: jited programs disassembly is not supported,\n", __func__
);
987 printf("%s:SKIP: tests are built w/o LLVM development libs\n", __func__
);
991 if (!ASSERT_EQ(err
, 0, "get_jited_program_text"))
993 emit_jited(tester
->log_buf
, false /*force*/);
994 validate_msgs(tester
->log_buf
, &subspec
->jited
, emit_jited
);
997 if (should_do_test_run(spec
, subspec
)) {
998 /* For some reason test_verifier executes programs
999 * with all capabilities restored. Do the same here.
1001 if (restore_capabilities(&caps
))
1004 /* Do bpf_map__attach_struct_ops() for each struct_ops map.
1005 * This should trigger bpf_struct_ops->reg callback on kernel side.
1007 bpf_object__for_each_map(map
, tobj
) {
1008 if (!bpf_map__autocreate(map
) ||
1009 bpf_map__type(map
) != BPF_MAP_TYPE_STRUCT_OPS
)
1011 if (links_cnt
>= ARRAY_SIZE(links
)) {
1012 PRINT_FAIL("too many struct_ops maps");
1015 link
= bpf_map__attach_struct_ops(map
);
1017 PRINT_FAIL("bpf_map__attach_struct_ops failed for map %s: err=%d\n",
1018 bpf_map__name(map
), err
);
1021 links
[links_cnt
++] = link
;
1024 if (tester
->pre_execution_cb
) {
1025 err
= tester
->pre_execution_cb(tobj
);
1027 PRINT_FAIL("pre_execution_cb failed: %d\n", err
);
1032 do_prog_test_run(bpf_program__fd(tprog
), &retval
,
1033 bpf_program__type(tprog
) == BPF_PROG_TYPE_SYSCALL
? true : false);
1034 if (retval
!= subspec
->retval
&& subspec
->retval
!= POINTER_VALUE
) {
1035 PRINT_FAIL("Unexpected retval: %d != %d\n", retval
, subspec
->retval
);
1038 /* redo bpf_map__attach_struct_ops for each test */
1039 while (links_cnt
> 0)
1040 bpf_link__destroy(links
[--links_cnt
]);
1044 while (links_cnt
> 0)
1045 bpf_link__destroy(links
[--links_cnt
]);
1046 bpf_object__close(tobj
);
1048 test__end_subtest();
1049 restore_capabilities(&caps
);
1052 static void process_subtest(struct test_loader
*tester
,
1053 const char *skel_name
,
1054 skel_elf_bytes_fn elf_bytes_factory
)
1056 LIBBPF_OPTS(bpf_object_open_opts
, open_opts
, .object_name
= skel_name
);
1057 struct test_spec
*specs
= NULL
;
1058 struct bpf_object
*obj
= NULL
;
1059 struct bpf_program
*prog
;
1060 const void *obj_bytes
;
1061 int err
, i
, nr_progs
;
1062 size_t obj_byte_cnt
;
1064 if (tester_init(tester
) < 0)
1065 return; /* failed to initialize tester */
1067 obj_bytes
= elf_bytes_factory(&obj_byte_cnt
);
1068 obj
= bpf_object__open_mem(obj_bytes
, obj_byte_cnt
, &open_opts
);
1069 if (!ASSERT_OK_PTR(obj
, "obj_open_mem"))
1073 bpf_object__for_each_program(prog
, obj
)
1076 specs
= calloc(nr_progs
, sizeof(struct test_spec
));
1077 if (!ASSERT_OK_PTR(specs
, "specs_alloc"))
1081 bpf_object__for_each_program(prog
, obj
) {
1082 /* ignore tests for which we can't derive test specification */
1083 err
= parse_test_spec(tester
, obj
, prog
, &specs
[i
++]);
1085 PRINT_FAIL("Can't parse test spec for program '%s'\n",
1086 bpf_program__name(prog
));
1090 bpf_object__for_each_program(prog
, obj
) {
1091 struct test_spec
*spec
= &specs
[i
++];
1093 if (!spec
->valid
|| spec
->auxiliary
)
1096 if (spec
->mode_mask
& PRIV
)
1097 run_subtest(tester
, &open_opts
, obj_bytes
, obj_byte_cnt
,
1098 specs
, spec
, false);
1099 if (spec
->mode_mask
& UNPRIV
)
1100 run_subtest(tester
, &open_opts
, obj_bytes
, obj_byte_cnt
,
1105 for (i
= 0; i
< nr_progs
; ++i
)
1106 free_test_spec(&specs
[i
]);
1108 bpf_object__close(obj
);
1111 void test_loader__run_subtests(struct test_loader
*tester
,
1112 const char *skel_name
,
1113 skel_elf_bytes_fn elf_bytes_factory
)
1115 /* see comment in run_subtest() for why we do this function nesting */
1116 process_subtest(tester
, skel_name
, elf_bytes_factory
);