Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: verifier/uninit.c converted to inline assembly

Test verifier/uninit.c automatically converted to use inline assembly.

Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20230325025524.144043-37-eddyz87@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Eduard Zingerman and committed by
Alexei Starovoitov
ab839a58 edff37b2

+63 -39
+2
tools/testing/selftests/bpf/prog_tests/verifier.c
··· 33 33 #include "verifier_ringbuf.skel.h" 34 34 #include "verifier_spill_fill.skel.h" 35 35 #include "verifier_stack_ptr.skel.h" 36 + #include "verifier_uninit.skel.h" 36 37 37 38 __maybe_unused 38 39 static void run_tests_aux(const char *skel_name, skel_elf_bytes_fn elf_bytes_factory) ··· 89 88 void test_verifier_ringbuf(void) { RUN(verifier_ringbuf); } 90 89 void test_verifier_spill_fill(void) { RUN(verifier_spill_fill); } 91 90 void test_verifier_stack_ptr(void) { RUN(verifier_stack_ptr); } 91 + void test_verifier_uninit(void) { RUN(verifier_uninit); }
+61
tools/testing/selftests/bpf/progs/verifier_uninit.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Converted from tools/testing/selftests/bpf/verifier/uninit.c */ 3 + 4 + #include <linux/bpf.h> 5 + #include <bpf/bpf_helpers.h> 6 + #include "../../../include/linux/filter.h" 7 + #include "bpf_misc.h" 8 + 9 + SEC("socket") 10 + __description("read uninitialized register") 11 + __failure __msg("R2 !read_ok") 12 + __failure_unpriv 13 + __naked void read_uninitialized_register(void) 14 + { 15 + asm volatile (" \ 16 + r0 = r2; \ 17 + exit; \ 18 + " ::: __clobber_all); 19 + } 20 + 21 + SEC("socket") 22 + __description("read invalid register") 23 + __failure __msg("R15 is invalid") 24 + __failure_unpriv 25 + __naked void read_invalid_register(void) 26 + { 27 + asm volatile (" \ 28 + .8byte %[mov64_reg]; \ 29 + exit; \ 30 + " : 31 + : __imm_insn(mov64_reg, BPF_MOV64_REG(BPF_REG_0, -1)) 32 + : __clobber_all); 33 + } 34 + 35 + SEC("socket") 36 + __description("program doesn't init R0 before exit") 37 + __failure __msg("R0 !read_ok") 38 + __failure_unpriv 39 + __naked void t_init_r0_before_exit(void) 40 + { 41 + asm volatile (" \ 42 + r2 = r1; \ 43 + exit; \ 44 + " ::: __clobber_all); 45 + } 46 + 47 + SEC("socket") 48 + __description("program doesn't init R0 before exit in all branches") 49 + __failure __msg("R0 !read_ok") 50 + __msg_unpriv("R1 pointer comparison") 51 + __naked void before_exit_in_all_branches(void) 52 + { 53 + asm volatile (" \ 54 + if r1 >= 0 goto l0_%=; \ 55 + r0 = 1; \ 56 + r0 += 2; \ 57 + l0_%=: exit; \ 58 + " ::: __clobber_all); 59 + } 60 + 61 + char _license[] SEC("license") = "GPL";
-39
tools/testing/selftests/bpf/verifier/uninit.c
··· 1 - { 2 - "read uninitialized register", 3 - .insns = { 4 - BPF_MOV64_REG(BPF_REG_0, BPF_REG_2), 5 - BPF_EXIT_INSN(), 6 - }, 7 - .errstr = "R2 !read_ok", 8 - .result = REJECT, 9 - }, 10 - { 11 - "read invalid register", 12 - .insns = { 13 - BPF_MOV64_REG(BPF_REG_0, -1), 14 - BPF_EXIT_INSN(), 15 - }, 16 - .errstr = "R15 is invalid", 17 - .result = REJECT, 18 - }, 19 - { 20 - "program doesn't init R0 before exit", 21 - .insns = { 22 - BPF_ALU64_REG(BPF_MOV, BPF_REG_2, BPF_REG_1), 23 - BPF_EXIT_INSN(), 24 - }, 25 - .errstr = "R0 !read_ok", 26 - .result = REJECT, 27 - }, 28 - { 29 - "program doesn't init R0 before exit in all branches", 30 - .insns = { 31 - BPF_JMP_IMM(BPF_JGE, BPF_REG_1, 0, 2), 32 - BPF_MOV64_IMM(BPF_REG_0, 1), 33 - BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 2), 34 - BPF_EXIT_INSN(), 35 - }, 36 - .errstr = "R0 !read_ok", 37 - .errstr_unpriv = "R1 pointer comparison", 38 - .result = REJECT, 39 - },