提交 fb8d251e 编写于 作者: A Alexei Starovoitov 提交者: Daniel Borkmann

bpf: extend is_branch_taken to registers

This patch extends is_branch_taken() logic from JMP+K instructions
to JMP+X instructions.
Conditional branches are often done when src and dst registers
contain known scalars. In such case the verifier can follow
the branch that is going to be taken when program executes.
That speeds up the verification and is essential feature to support
bounded loops.
Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
Acked-by: NAndrii Nakryiko <andriin@fb.com>
Signed-off-by: NDaniel Borkmann <daniel@iogearbox.net>
上级 fc559a70
...@@ -5266,9 +5266,10 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env, ...@@ -5266,9 +5266,10 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
struct bpf_verifier_state *this_branch = env->cur_state; struct bpf_verifier_state *this_branch = env->cur_state;
struct bpf_verifier_state *other_branch; struct bpf_verifier_state *other_branch;
struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs;
struct bpf_reg_state *dst_reg, *other_branch_regs; struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL;
u8 opcode = BPF_OP(insn->code); u8 opcode = BPF_OP(insn->code);
bool is_jmp32; bool is_jmp32;
int pred = -1;
int err; int err;
/* Only conditional jumps are expected to reach here. */ /* Only conditional jumps are expected to reach here. */
...@@ -5293,6 +5294,7 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env, ...@@ -5293,6 +5294,7 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
insn->src_reg); insn->src_reg);
return -EACCES; return -EACCES;
} }
src_reg = &regs[insn->src_reg];
} else { } else {
if (insn->src_reg != BPF_REG_0) { if (insn->src_reg != BPF_REG_0) {
verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
...@@ -5308,20 +5310,22 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env, ...@@ -5308,20 +5310,22 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
dst_reg = &regs[insn->dst_reg]; dst_reg = &regs[insn->dst_reg];
is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32;
if (BPF_SRC(insn->code) == BPF_K) { if (BPF_SRC(insn->code) == BPF_K)
int pred = is_branch_taken(dst_reg, insn->imm, opcode, pred = is_branch_taken(dst_reg, insn->imm,
is_jmp32); opcode, is_jmp32);
else if (src_reg->type == SCALAR_VALUE &&
if (pred == 1) { tnum_is_const(src_reg->var_off))
/* only follow the goto, ignore fall-through */ pred = is_branch_taken(dst_reg, src_reg->var_off.value,
*insn_idx += insn->off; opcode, is_jmp32);
return 0; if (pred == 1) {
} else if (pred == 0) { /* only follow the goto, ignore fall-through */
/* only follow fall-through branch, since *insn_idx += insn->off;
* that's where the program will go return 0;
*/ } else if (pred == 0) {
return 0; /* only follow fall-through branch, since
} * that's where the program will go
*/
return 0;
} }
other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册