• Eduard Zingerman's avatar
    selftests/bpf: specify expected instructions in test_verifier tests · 933ff531
    Eduard Zingerman authored
    Allows to specify expected and unexpected instruction sequences in
    test_verifier test cases. The instructions are requested from kernel
    after BPF program loading, thus allowing to check some of the
    transformations applied by BPF verifier.
    
    - `expected_insn` field specifies a sequence of instructions expected
      to be found in the program;
    - `unexpected_insn` field specifies a sequence of instructions that
      are not expected to be found in the program;
    - `INSN_OFF_MASK` and `INSN_IMM_MASK` values could be used to mask
      `off` and `imm` fields.
    - `SKIP_INSNS` could be used to specify that some instructions in the
      (un)expected pattern are not important (behavior similar to usage of
      `\t` in `errstr` field).
    
    The intended usage is as follows:
    
      {
    	"inline simple bpf_loop call",
    	.insns = {
    	/* main */
    	BPF_ALU64_IMM(BPF_MOV, BPF_REG_1, 1),
    	BPF_RAW_INSN(BPF_LD | BPF_IMM | BPF_DW, BPF_REG_2,
    			BPF_PSEUDO_FUNC, 0, 6),
        ...
    	BPF_EXIT_INSN(),
    	/* callback */
    	BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 1),
    	BPF_EXIT_INSN(),
    	},
    	.expected_insns = {
    	BPF_ALU64_IMM(BPF_MOV, BPF_REG_1, 1),
    	SKIP_INSNS(),
    	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_CALL, 8, 1)
    	},
    	.unexpected_insns = {
    	BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0,
    			INSN_OFF_MASK, INSN_IMM_MASK),
    	},
    	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
    	.result = ACCEPT,
    	.runs = 0,
      },
    
    Here it is expected that move of 1 to register 1 would remain in place
    and helper function call instruction would be replaced by a relative
    call instruction.
    Signed-off-by: default avatarEduard Zingerman <eddyz87@gmail.com>
    Acked-by: default avatarSong Liu <songliubraving@fb.com>
    Link: https://lore.kernel.org/r/20220620235344.569325-2-eddyz87@gmail.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    933ff531
test_verifier.c 43.5 KB