Commit f3709f69 authored by Joe Stringer's avatar Joe Stringer Committed by Daniel Borkmann

bpf: Add iterator for spilled registers

Add this iterator for spilled registers, it concentrates the details of
how to get the current frame's spilled registers into a single macro
while clarifying the intention of the code which is calling the macro.
Signed-off-by: default avatarJoe Stringer <joe@wand.net.nz>
Acked-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent 940656fb
...@@ -131,6 +131,17 @@ struct bpf_verifier_state { ...@@ -131,6 +131,17 @@ struct bpf_verifier_state {
u32 curframe; u32 curframe;
}; };
#define bpf_get_spilled_reg(slot, frame) \
(((slot < frame->allocated_stack / BPF_REG_SIZE) && \
(frame->stack[slot].slot_type[0] == STACK_SPILL)) \
? &frame->stack[slot].spilled_ptr : NULL)
/* Iterate over 'frame', setting 'reg' to either NULL or a spilled register. */
#define bpf_for_each_spilled_reg(iter, frame, reg) \
for (iter = 0, reg = bpf_get_spilled_reg(iter, frame); \
iter < frame->allocated_stack / BPF_REG_SIZE; \
iter++, reg = bpf_get_spilled_reg(iter, frame))
/* linked list of verifier states used to prune search */ /* linked list of verifier states used to prune search */
struct bpf_verifier_state_list { struct bpf_verifier_state_list {
struct bpf_verifier_state state; struct bpf_verifier_state state;
......
...@@ -2252,10 +2252,9 @@ static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, ...@@ -2252,10 +2252,9 @@ static void __clear_all_pkt_pointers(struct bpf_verifier_env *env,
if (reg_is_pkt_pointer_any(&regs[i])) if (reg_is_pkt_pointer_any(&regs[i]))
mark_reg_unknown(env, regs, i); mark_reg_unknown(env, regs, i);
for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { bpf_for_each_spilled_reg(i, state, reg) {
if (state->stack[i].slot_type[0] != STACK_SPILL) if (!reg)
continue; continue;
reg = &state->stack[i].spilled_ptr;
if (reg_is_pkt_pointer_any(reg)) if (reg_is_pkt_pointer_any(reg))
__mark_reg_unknown(reg); __mark_reg_unknown(reg);
} }
...@@ -3395,10 +3394,9 @@ static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, ...@@ -3395,10 +3394,9 @@ static void find_good_pkt_pointers(struct bpf_verifier_state *vstate,
for (j = 0; j <= vstate->curframe; j++) { for (j = 0; j <= vstate->curframe; j++) {
state = vstate->frame[j]; state = vstate->frame[j];
for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { bpf_for_each_spilled_reg(i, state, reg) {
if (state->stack[i].slot_type[0] != STACK_SPILL) if (!reg)
continue; continue;
reg = &state->stack[i].spilled_ptr;
if (reg->type == type && reg->id == dst_reg->id) if (reg->type == type && reg->id == dst_reg->id)
reg->range = max(reg->range, new_range); reg->range = max(reg->range, new_range);
} }
...@@ -3643,7 +3641,7 @@ static void mark_map_regs(struct bpf_verifier_state *vstate, u32 regno, ...@@ -3643,7 +3641,7 @@ static void mark_map_regs(struct bpf_verifier_state *vstate, u32 regno,
bool is_null) bool is_null)
{ {
struct bpf_func_state *state = vstate->frame[vstate->curframe]; struct bpf_func_state *state = vstate->frame[vstate->curframe];
struct bpf_reg_state *regs = state->regs; struct bpf_reg_state *reg, *regs = state->regs;
u32 id = regs[regno].id; u32 id = regs[regno].id;
int i, j; int i, j;
...@@ -3652,8 +3650,8 @@ static void mark_map_regs(struct bpf_verifier_state *vstate, u32 regno, ...@@ -3652,8 +3650,8 @@ static void mark_map_regs(struct bpf_verifier_state *vstate, u32 regno,
for (j = 0; j <= vstate->curframe; j++) { for (j = 0; j <= vstate->curframe; j++) {
state = vstate->frame[j]; state = vstate->frame[j];
for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { bpf_for_each_spilled_reg(i, state, reg) {
if (state->stack[i].slot_type[0] != STACK_SPILL) if (!reg)
continue; continue;
mark_map_reg(&state->stack[i].spilled_ptr, 0, id, is_null); mark_map_reg(&state->stack[i].spilled_ptr, 0, id, is_null);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment