Skip to content

Commit c766570

Browse files
davem330Alexei Starovoitov
authored andcommitted
bpf: Adjust F_NEEDS_EFFICIENT_UNALIGNED_ACCESS handling in test_verifier.c
Make it set the flag argument to bpf_verify_program() which will relax the alignment restrictions. Now all such test cases will go properly through the verifier even on inefficient unaligned access architectures. On inefficient unaligned access architectures do not try to run such programs, instead mark the test case as passing but annotate the result similarly to how it is done now in the presence of this flag. So, we get complete full coverage for all REJECT test cases, and at least verifier level coverage for ACCEPT test cases. Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent e9ee9ef commit c766570

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

tools/testing/selftests/bpf/test_verifier.c

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14257,13 +14257,14 @@ static int set_admin(bool admin)
1425714257
static void do_test_single(struct bpf_test *test, bool unpriv,
1425814258
int *passes, int *errors)
1425914259
{
14260-
int fd_prog, expected_ret, reject_from_alignment;
14260+
int fd_prog, expected_ret, alignment_prevented_execution;
1426114261
int prog_len, prog_type = test->prog_type;
1426214262
struct bpf_insn *prog = test->insns;
1426314263
int map_fds[MAX_NR_MAPS];
1426414264
const char *expected_err;
1426514265
uint32_t expected_val;
1426614266
uint32_t retval;
14267+
__u32 pflags;
1426714268
int i, err;
1426814269

1426914270
for (i = 0; i < MAX_NR_MAPS; i++)
@@ -14274,9 +14275,12 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
1427414275
do_test_fixup(test, prog_type, prog, map_fds);
1427514276
prog_len = probe_filter_length(prog);
1427614277

14277-
fd_prog = bpf_verify_program(prog_type, prog, prog_len,
14278-
test->flags & F_LOAD_WITH_STRICT_ALIGNMENT ?
14279-
BPF_F_STRICT_ALIGNMENT : 0,
14278+
pflags = 0;
14279+
if (test->flags & F_LOAD_WITH_STRICT_ALIGNMENT)
14280+
pflags |= BPF_F_STRICT_ALIGNMENT;
14281+
if (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS)
14282+
pflags |= BPF_F_ANY_ALIGNMENT;
14283+
fd_prog = bpf_verify_program(prog_type, prog, prog_len, pflags,
1428014284
"GPL", 0, bpf_vlog, sizeof(bpf_vlog), 1);
1428114285

1428214286
expected_ret = unpriv && test->result_unpriv != UNDEF ?
@@ -14286,28 +14290,27 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
1428614290
expected_val = unpriv && test->retval_unpriv ?
1428714291
test->retval_unpriv : test->retval;
1428814292

14289-
reject_from_alignment = fd_prog < 0 &&
14290-
(test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS) &&
14291-
strstr(bpf_vlog, "misaligned");
14292-
#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
14293-
if (reject_from_alignment) {
14294-
printf("FAIL\nFailed due to alignment despite having efficient unaligned access: '%s'!\n",
14295-
strerror(errno));
14296-
goto fail_log;
14297-
}
14298-
#endif
14293+
alignment_prevented_execution = 0;
14294+
1429914295
if (expected_ret == ACCEPT) {
14300-
if (fd_prog < 0 && !reject_from_alignment) {
14296+
if (fd_prog < 0) {
1430114297
printf("FAIL\nFailed to load prog '%s'!\n",
1430214298
strerror(errno));
1430314299
goto fail_log;
1430414300
}
14301+
#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
14302+
if (fd_prog >= 0 &&
14303+
(test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS)) {
14304+
alignment_prevented_execution = 1;
14305+
goto test_ok;
14306+
}
14307+
#endif
1430514308
} else {
1430614309
if (fd_prog >= 0) {
1430714310
printf("FAIL\nUnexpected success to load!\n");
1430814311
goto fail_log;
1430914312
}
14310-
if (!strstr(bpf_vlog, expected_err) && !reject_from_alignment) {
14313+
if (!strstr(bpf_vlog, expected_err)) {
1431114314
printf("FAIL\nUnexpected error message!\n\tEXP: %s\n\tRES: %s\n",
1431214315
expected_err, bpf_vlog);
1431314316
goto fail_log;
@@ -14335,9 +14338,12 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
1433514338
goto fail_log;
1433614339
}
1433714340
}
14341+
#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
14342+
test_ok:
14343+
#endif
1433814344
(*passes)++;
14339-
printf("OK%s\n", reject_from_alignment ?
14340-
" (NOTE: reject due to unknown alignment)" : "");
14345+
printf("OK%s\n", alignment_prevented_execution ?
14346+
" (NOTE: not executed due to unknown alignment)" : "");
1434114347
close_fds:
1434214348
close(fd_prog);
1434314349
for (i = 0; i < MAX_NR_MAPS; i++)

0 commit comments

Comments
 (0)