BPF verifier log improvements
Subsystems (4)
Andrii Nakryiko
maintainercover BPF verifier log improvements
This patch set moves a big chunk of verifier log related code from gigantic verifier.c file into more focused kernel/bpf/log.c. This is not essential to the rest of functionality in this patch set, so I can undo it, but it felt like it's good to start chipping away from 20K+ verifier.c whenever we can.
The main purpose of the patch set, though, is in improving verifier log further.
Patches #3-#4 start printing out register state even if that register is spilled into stack slot. Previously we'd get only spilled register type, but no additional information, like SCALAR_VALUE's ranges. Super limiting during debugging. For cases of register spills smaller than 8 bytes, we also print out STACK_MISC/STACK_ZERO/STACK_INVALID markers. This, among other things, will make it easier to write tests for these mixed spill/misc cases.
Patch #5 prints map name for PTR_TO_MAP_VALUE/PTR_TO_MAP_KEY/CONST_PTR_TO_MAP registers. In big production BPF programs, it's important to map assembly to actual map, and it's often non-trivial. Having map name helps.
Patch #6 just removes visual noise in form of ubiquitous imm=0 and off=0. They are default values, omit them.
Patch #7 is probably the most controversial, but it reworks how verifier log prints numbers. For small valued integers we use decimals, but for large ones we switch to hexadecimal. From personal experience this is a much more useful convention. We can tune what consitutes "small value", for now it's 16-bit range.
Patch #8 prints frame number for PTR_TO_CTX registers, if that frame is different from the "current" one. This removes ambiguity and confusion, especially in complicated cases with multiple subprogs passing around pointers.
Andrii Nakryiko (8):
bpf: move verbose_linfo() into kernel/bpf/log.c
bpf: move verifier state printing code to kernel/bpf/log.c
bpf: extract register state printing
bpf: print spilled register state in stack slot
bpf: emit map name in register state if applicable and available
bpf: omit default off=0 and imm=0 in register state log
bpf: smarter verifier log number printing logic
bpf: emit frameno for PTR_TO_STACK regs if it differs from current one
include/linux/bpf_verifier.h | 76 +++ kernel/bpf/log.c | 476 ++++++++++++++++++ kernel/bpf/verifier.c | 460 ----------------- .../testing/selftests/bpf/prog_tests/align.c | 42 +- .../selftests/bpf/prog_tests/log_buf.c | 4 +- .../selftests/bpf/prog_tests/spin_lock.c | 14 +- .../selftests/bpf/progs/exceptions_assert.c | 40 +- 7 files changed, 602 insertions(+), 510 deletions(-)
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH 1/8 bpf: move verbose_linfo() into kernel/bpf/log.c
verifier.c is huge. Let's try to move out parts that are logging-related into log.c, as we previously did with bpf_log() and other related stuff. This patch moves line info verbose output routines: it's pretty self-contained and isolated code, so there is no problem with this.
--- include/linux/bpf_verifier.h | 4 +++ kernel/bpf/log.c | 59 ++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 57 ---------------------------------- 3 files changed, 63 insertions(+), 57 deletions(-)
include/linux/bpf_verifier.h +4 -0
@@ -679,6 +679,10 @@ int bpf_vlog_init(struct bpf_verifier_log *log, u32 log_level,void bpf_vlog_reset (struct bpf_verifier_log *log,u64 new_pos);int bpf_vlog_finalize (struct bpf_verifier_log *log,u32 *log_size_actual); +__printf(3, 4) void verbose_linfo(struct bpf_verifier_env *env, +u32 insn_off, +const char *prefix_fmt, ...); +static inline struct bpf_func_state *cur_func (struct bpf_verifier_env *env) {struct bpf_verifier_state *cur = env->cur_state;
kernel/bpf/log.c +59 -0
68 lines not on this page · raw message
kernel/bpf/verifier.c +0 -57
69 lines not on this page · raw message
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH 2/8 bpf: move verifier state printing code to kernel/bpf/log.c
In reply to Andrii NakryikoMove a good chunk of code from verifier.c to log.c: verifier state verbose printing logic. This is an important and very much logging/debugging oriented code. It fits the overlall log.c's focus on verifier logging, and moving it allows to keep growing it without unnecessarily adding to verifier.c code that otherwise contains a core verification logic.
There are not many shared dependencies between this code and the rest of verifier.c code, except a few single-line helpers for various register type checks and a bit of state "scratching" helpers. We move all such trivial helpers into include/bpf/bpf_verifier.h as static inlines.
No functional changes in this patch.
--- include/linux/bpf_verifier.h | 72 +++++++ kernel/bpf/log.c | 342 +++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 403 ----------------------------------- 3 files changed, 414 insertions(+), 403 deletions(-)
include/linux/bpf_verifier.h +72 -0
@@ -782,4 +782,76 @@ static inline bool bpf_type_has_unsafe_modifiers(u32 type)return type_flag (type) & ~BPF_REG_TRUSTED_MODIFIERS ; } +static inline bool type_is_ptr_alloc_obj (u32 type) +{ +return base_type (type) ==PTR_TO_BTF_ID &&type_flag (type) &MEM_ALLOC ; +} + +static inline bool type_is_non_owning_ref (u32 type) +{ +return type_is_ptr_alloc_obj (type) &&type_flag (type) &NON_OWN_REF ; +} + +static inline bool type_is_pkt_pointer (enum bpf_reg_type type) +{ + type =base_type (type); +return type ==PTR_TO_PACKET || + type ==PTR_TO_PACKET_META ; +} + +static inline bool type_is_sk_pointer (enum bpf_reg_type type) +{ +return type ==PTR_TO_SOCKET || + type ==PTR_TO_SOCK_COMMON || + type ==PTR_TO_TCP_SOCK || + type ==PTR_TO_XDP_SOCK ; +} + +static inline void mark_reg_scratched (struct bpf_verifier_env *env,u32 regno) +{ + env->scratched_regs |=1U << regno; +} + +static inline void mark_stack_slot_scratched (struct bpf_verifier_env *env,u32 spi) +{ + env->scratched_stack_slots |=1ULL << spi; +} + +static inline bool reg_scratched (const struct bpf_verifier_env *env,u32 regno) +{ +return (env->scratched_regs >> regno) &1 ; +} + +static inline bool stack_slot_scratched (const struct bpf_verifier_env *env,u64 regno) +{ +return (env->scratched_stack_slots >> regno) &1 ; +} + +static inline bool verifier_state_scratched (const struct bpf_verifier_env *env) +{ +return env->scratched_regs || env->scratched_stack_slots; +} + +static inline void mark_verifier_state_clean (struct bpf_verifier_env *env) +{ + env->scratched_regs =0U ; + env->scratched_stack_slots =0ULL ; +} + +/* Used for printing the entire verifier state. */ +static inline void mark_verifier_state_scratched (struct bpf_verifier_env *env) +{ + env->scratched_regs = ~0U ; + env->scratched_stack_slots = ~0ULL ; +} + +const char *reg_type_str (struct bpf_verifier_env *env,enum bpf_reg_type type); +const char *dynptr_type_str (enum bpf_dynptr_type type); +const char *iter_type_str (const struct btf *btf,u32 btf_id); +const char *iter_state_str (enum bpf_iter_state state); + +void print_verifier_state (struct bpf_verifier_env *env, +const struct bpf_func_state *state,bool print_all); +void print_insn_state (struct bpf_verifier_env *env,const struct bpf_func_state *state); +#endif /* _LINUX_BPF_VERIFIER_H */
kernel/bpf/log.c +342 -0
345 lines not on this page · raw message
kernel/bpf/verifier.c +0 -403
433 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH bpf-next 2/8] bpf: move verifier state printing code to kernel/bpf/log.c
Quoted 26 lines · Andrii Nakryiko
Move a good chunk of code from
verifier.ctolog.c: verifier state verbose printing logic. This is an important and very much logging/debugging oriented code. It fits the overlall log.c's focus on verifier logging, and moving it allows to keep growing it without unnecessarily adding toverifier.ccode that otherwise contains a core verification logic.There are not many shared dependencies between this code and the rest of
verifier.ccode, except a few single-line helpers for various register type checks and a bit of state "scratching" helpers. We move all such trivial helpers intoinclude/bpf/bpf_verifier.has static inlines.No functional changes in this patch.
Signed-off-by Andrii Nakryiko <[email protected]>--- include/linux/bpf_verifier.h | 72 +++++++ kernel/bpf/log.c | 342 +++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 403 ----------------------------------- 3 files changed, 414 insertions(+), 403 deletions(-)include/linux/bpf_verifier.h +0
-0
include/linux/bpf_verifier.h static inline bool bpf_type_has_unsafe_modifiers(u32 type) · line 783
Quoted 2 lines
include/linux/bpf_verifier.h +0
-0@@ -782,4 +782,76 @@ static inline bool bpf_type_has_unsafe_modifiers(u32 type)return type_flag (type) & ~BPF_REG_TRUSTED_MODIFIERS ; }
Does it make sense to have a new bpf_log.h and move these in there? We can then include it from verifier.c only. Looks like bpf_verifier.h is included in a bunch of places and those symbols don't have a prefix and might (potentially) clash with something else in the future.
Or is not a super clear cut?
Re: [PATCH bpf-next 2/8] bpf: move verifier state printing code to kernel/bpf/log.c
Quoted 36 lines · Stanislav Fomichev
Quoted 26 lines · Andrii Nakryiko
Move a good chunk of code from
verifier.ctolog.c: verifier state verbose printing logic. This is an important and very much logging/debugging oriented code. It fits the overlall log.c's focus on verifier logging, and moving it allows to keep growing it without unnecessarily adding toverifier.ccode that otherwise contains a core verification logic.There are not many shared dependencies between this code and the rest of
verifier.ccode, except a few single-line helpers for various register type checks and a bit of state "scratching" helpers. We move all such trivial helpers intoinclude/bpf/bpf_verifier.has static inlines.No functional changes in this patch.
Signed-off-by Andrii Nakryiko <[email protected]>--- include/linux/bpf_verifier.h | 72 +++++++ kernel/bpf/log.c | 342 +++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 403 ----------------------------------- 3 files changed, 414 insertions(+), 403 deletions(-)include/linux/bpf_verifier.h +0
-0include/linux/bpf_verifier.h static inline bool bpf_type_has_unsafe_modifiers(u32 type) · line 783
Quoted 2 lines
include/linux/bpf_verifier.h +0
-0@@ -782,4 +782,76 @@ static inline bool bpf_type_has_unsafe_modifiers(u32 type)return type_flag (type) & ~BPF_REG_TRUSTED_MODIFIERS ; }Does it make sense to have a new
bpf_log.hand move these in there? We can then include it fromverifier.conly. Looks likebpf_verifier.his included in a bunch of places and those symbols don't have a prefix and might (potentially) clash with something else in the future.
Quoted 1 line
Or is not a super clear cut?
bpf_verifier.h should be used in very BPF-specific portions of code, so I think this shouldn't be a big problem. Doesn't feel justified to add a new small header for 4-5 functions, but I don't feel very strongly about this.
Andrii Nakryiko
maintainerPATCH 3/8 bpf: extract register state printing
In reply to Andrii NakryikoExtract printing register state representation logic into a separate helper, as we are going to reuse it for spilled register state printing in the next patch. This also nicely reduces code nestedness.
No functional changes.
--- kernel/bpf/log.c | 120 +++++++++++++++++++++++++---------------------- 1 file changed, 63 insertions(+), 57 deletions(-)
kernel/bpf/log.c +63 -57
@@ -553,6 +553,67 @@ static void print_scalar_ranges(struct bpf_verifier_env *env, } } +static void print_reg_state (struct bpf_verifier_env *env,const struct bpf_reg_state *reg) +{ +enum bpf_reg_type t; +const char *sep ="" ; + + t = reg->type; +if (t ==SCALAR_VALUE && reg->precise) +verbose (env,"P" ); +if ((t ==SCALAR_VALUE || t ==PTR_TO_STACK ) && +tnum_is_const (reg->var_off)) { +/* reg->off should be 0 for SCALAR_VALUE */ +verbose (env,"%s" , t ==SCALAR_VALUE ?"" :reg_type_str (env, t)); +verbose (env,"%lld" , reg->var_off.value + reg->off); +return ; + } +/* +* _a stands for append, was shortened to avoid multiline statements below. +* This macro is used to output a comma separated list of attributes. +*/ +#define verbose_a (fmt, ...) ({ verbose(env, "%s" fmt, sep, __VA_ARGS__); sep = ","; }) + +verbose (env,"%s" ,reg_type_str (env, t)); +if (base_type (t) ==PTR_TO_BTF_ID ) +verbose (env,"%s" ,btf_type_name (reg->btf, reg->btf_id)); +verbose (env,"(" ); +if (reg->id) +verbose_a ("id=%d" , reg->id); +if (reg->ref_obj_id) +verbose_a ("ref_obj_id=%d" , reg->ref_obj_id); +if (type_is_non_owning_ref (reg->type)) +verbose_a ("%s" ,"non_own_ref" ); +if (t !=SCALAR_VALUE ) +verbose_a ("off=%d" , reg->off); +if (type_is_pkt_pointer (t)) +verbose_a ("r=%d" , reg->range); +else if (base_type (t) ==CONST_PTR_TO_MAP || +base_type (t) ==PTR_TO_MAP_KEY || +base_type (t) ==PTR_TO_MAP_VALUE ) +verbose_a ("ks=%d,vs=%d" , + reg->map_ptr->key_size, + reg->map_ptr->value_size); +if (tnum_is_const (reg->var_off)) { +/* Typically an immediate SCALAR_VALUE, but +* could be a pointer whose offset is too big +* for reg->off +*/ +verbose_a ("imm=%llx" , reg->var_off.value); + }else { +print_scalar_ranges (env, reg, &sep); +if (!tnum_is_unknown (reg->var_off)) { +char tn_buf[48 ]; + +tnum_strn (tn_buf,sizeof (tn_buf), reg->var_off); +verbose_a ("var_off=%s" , tn_buf); + } + } +verbose (env,")" ); + +#undef verbose_a +} +void print_verifier_state (struct bpf_verifier_env *env,const struct bpf_func_state *state,bool print_all) {
@@ -564,69 +625,14 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_func_stverbose (env," frame%d:" , state->frameno);for (i =0 ; i <MAX_BPF_REG ; i++) { reg = &state->regs[i]; - t = reg->type; -if (t ==NOT_INIT ) +if (reg->type ==NOT_INIT )continue ;if (!print_all && !reg_scratched (env, i))continue ;verbose (env," R%d" , i);print_liveness (env, reg->live);verbose (env,"=" ); -if (t ==SCALAR_VALUE && reg->precise) -verbose (env,"P" ); -if ((t ==SCALAR_VALUE || t ==PTR_TO_STACK ) && -tnum_is_const (reg->var_off)) { -/* reg->off should be 0 for SCALAR_VALUE */ -verbose (env,"%s" , t ==SCALAR_VALUE ?"" :reg_type_str (env, t)); -verbose (env,"%lld" , reg->var_off.value + reg->off); - }else { -const char *sep ="" ; - -verbose (env,"%s" ,reg_type_str (env, t)); -if (base_type (t) ==PTR_TO_BTF_ID ) -verbose (env,"%s" ,btf_type_name (reg->btf, reg->btf_id)); -verbose (env,"(" ); -/* -* _a stands for append, was shortened to avoid multiline statements below. -* This macro is used to output a comma separated list of attributes. -*/ -#define verbose_a (fmt, ...) ({ verbose(env, "%s" fmt, sep, __VA_ARGS__); sep = ","; }) - -if (reg->id) -verbose_a ("id=%d" , reg->id); -if (reg->ref_obj_id) -verbose_a ("ref_obj_id=%d" , reg->ref_obj_id); -if (type_is_non_owning_ref (reg->type)) -verbose_a ("%s" ,"non_own_ref" ); -if (t !=SCALAR_VALUE ) -verbose_a ("off=%d" , reg->off); -if (type_is_pkt_pointer (t)) -verbose_a ("r=%d" , reg->range); -else if (base_type (t) ==CONST_PTR_TO_MAP || -base_type (t) ==PTR_TO_MAP_KEY || -base_type (t) ==PTR_TO_MAP_VALUE ) -verbose_a ("ks=%d,vs=%d" , - reg->map_ptr->key_size, - reg->map_ptr->value_size); -if (tnum_is_const (reg->var_off)) { -/* Typically an immediate SCALAR_VALUE, but -* could be a pointer whose offset is too big -* for reg->off -*/ -verbose_a ("imm=%llx" , reg->var_off.value); - }else { -print_scalar_ranges (env, reg, &sep); -if (!tnum_is_unknown (reg->var_off)) { -char tn_buf[48 ]; - -tnum_strn (tn_buf,sizeof (tn_buf), reg->var_off); -verbose_a ("var_off=%s" , tn_buf); - } - } -#undef verbose_a - -verbose (env,")" ); - } +print_reg_state (env, reg); }for (i =0 ; i < state->allocated_stack /BPF_REG_SIZE ; i++) {char types_buf[BPF_REG_SIZE +1 ];
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH 4/8 bpf: print spilled register state in stack slot
In reply to Andrii NakryikoPrint the same register state representation when printing stack state, as we do for normal registers. Note that if stack slot contains subregister spill (1, 2, or 4 byte long), we'll still emit "m0?" mask for those bytes that are not part of spilled register.
While means we can get something like fp-8=0000scalar() for a 4-byte spill with other 4 bytes still being STACK_ZERO.
Some example before and after, taken from the log of pyperf_subprogs.bpf.o:
49: (7b) *(u64 *)(r10 -256) = r1 ; frame1: R1_w=ctx(off=0,imm=0) R10=fp0 fp-256_w=ctx
49: (7b) *(u64 *)(r10 -256) = r1 ; frame1: R1_w=ctx(off=0,imm=0) R10=fp0 fp-256_w=ctx(off=0,imm=0)
150: (7b) *(u64 *)(r10 -264) = r0 ; frame1: R0_w=map_value_or_null(id=6,off=0,ks=192,vs=4,imm=0) R10=fp0 fp-264_w=map_value_or_null
150: (7b) *(u64 *)(r10 -264) = r0 ; frame1: R0_w=map_value_or_null(id=6,off=0,ks=192,vs=4,imm=0) R10=fp0 fp-264_w=map_value_or_null(id=6,off=0,ks=192,vs=4,imm=0)
5192: (61) r1 = *(u32 *)(r10 -272) ; frame1: R1_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=15,var_off=(0x0; 0xf)) R10=fp0 fp-272=
5192: (61) r1 = *(u32 *)(r10 -272) ; frame1: R1_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=15,var_off=(0x0; 0xf)) R10=fp0 fp-272=????scalar(smin=smin32=0,smax=umax=smax32=umax32=15,var_off=(0x0; 0xf))
While at it, do a few other simple clean ups:
- skip slot if it's not scratched before detecting whether it's valid;
- move taking spilled_reg pointer outside of switch (only DYNPTR has
to adjust that to get to the "main" slot);
- don't recalculate types_buf second time for MISC/ZERO/default case.
--- kernel/bpf/log.c | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-)
kernel/bpf/log.c +17 -19
@@ -618,7 +618,6 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_func_st bool print_all) {const struct bpf_reg_state *reg; -enum bpf_reg_type t;int i;if (state->frameno)
@@ -637,32 +636,38 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_func_stfor (i =0 ; i < state->allocated_stack /BPF_REG_SIZE ; i++) {char types_buf[BPF_REG_SIZE +1 ];bool valid = false; +u8 slot_type;int j; +if (!print_all && !stack_slot_scratched (env, i)) +continue ; +for (j =0 ; j <BPF_REG_SIZE ; j++) { -if (state->stack[i].slot_type[j] !=STACK_INVALID ) + slot_type = state->stack[i].slot_type[j]; +if (slot_type !=STACK_INVALID ) valid = true; - types_buf[j] = slot_type_char[state->stack[i].slot_type[j]]; + types_buf[j] = slot_type_char[slot_type]; } types_buf[BPF_REG_SIZE ] =0 ;if (!valid)continue ; -if (!print_all && !stack_slot_scratched (env, i)) -continue ; + + reg = &state->stack[i].spilled_ptr;switch (state->stack[i].slot_type[BPF_REG_SIZE -1 ]) {case STACK_SPILL : - reg = &state->stack[i].spilled_ptr; - t = reg->type; +/* print MISC/ZERO/INVALID slots above subreg spill */ +for (j =0 ; j <BPF_REG_SIZE ; j++) +if (state->stack[i].slot_type[j] ==STACK_SPILL ) +break ; + types_buf[j] ='\0' ;verbose (env," fp%d" , (-i -1 ) *BPF_REG_SIZE );print_liveness (env, reg->live); -verbose (env,"=%s" , t ==SCALAR_VALUE ?"" :reg_type_str (env, t)); -if (t ==SCALAR_VALUE && reg->precise) -verbose (env,"P" ); -if (t ==SCALAR_VALUE &&tnum_is_const (reg->var_off)) -verbose (env,"%lld" , reg->var_off.value + reg->off); +verbose (env,"=%s" , types_buf); +print_reg_state (env, reg);break ;case STACK_DYNPTR : +/* skip to main dynptr slot */ i +=BPF_DYNPTR_NR_SLOTS -1 ; reg = &state->stack[i].spilled_ptr;
@@ -674,7 +679,6 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_func_stbreak ;case STACK_ITER :/* only main slot has ref_obj_id set; skip others */ - reg = &state->stack[i].spilled_ptr;if (!reg->ref_obj_id)continue ;
@@ -688,12 +692,6 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_func_stcase STACK_MISC :case STACK_ZERO :default : - reg = &state->stack[i].spilled_ptr; - -for (j =0 ; j <BPF_REG_SIZE ; j++) - types_buf[j] = slot_type_char[state->stack[i].slot_type[j]]; - types_buf[BPF_REG_SIZE ] =0 ; -verbose (env," fp%d" , (-i -1 ) *BPF_REG_SIZE );print_liveness (env, reg->live);verbose (env,"=%s" , types_buf);
Signature · 1 line
2.34.1
Re: [PATCH bpf-next 4/8] bpf: print spilled register state in stack slot
Quoted 20 lines · Andrii Nakryiko
Print the same register state representation when printing stack state, as we do for normal registers. Note that if stack slot contains subregister spill (1, 2, or 4 byte long), we'll still emit "m0?" mask for those bytes that are not part of spilled register.
While means we can get something like fp-8=0000scalar() for a 4-byte spill with other 4 bytes still being
STACK_ZERO.Some example before and after, taken from the log of pyperf_subprogs.bpf.o:
49: (7b) *(u64 *)(r10 -256) = r1 ; frame1: R1_w=ctx(off=0,imm=0) R10=fp0 fp-256_w=ctx
49: (7b) *(u64 *)(r10 -256) = r1 ; frame1: R1_w=ctx(off=0,imm=0) R10=fp0 fp-256_w=ctx(off=0,imm=0)150: (7b) *(u64 *)(r10 -264) = r0 ; frame1: R0_w=map_value_or_null(id=6,off=0,ks=192,vs=4,imm=0) R10=fp0 fp-264_w=map_value_or_null
150: (7b) *(u64 *)(r10 -264) = r0 ; frame1: R0_w=map_value_or_null(id=6,off=0,ks=192,vs=4,imm=0) R10=fp0 fp-264_w=map_value_or_null(id=6,off=0,ks=192,vs=4,imm=0)5192: (61) r1 = *(u32 *)(r10 -272) ; frame1: R1_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=15,var_off=(0x0;
0xf)) R10=fp0 fp-272=
5192: (61) r1 = *(u32 *)(r10 -272) ; frame1: R1_w=scalar(smin=smin32=0,smax=umax=smax32=umax32=15,var_off=(0x0;0xf)) R10=fp0 fp-272=????scalar(smin=smin32=0,smax=umax=smax32=umax32=15,var_off=(0x0;0xf))
Quoted 7 lines
While at it, do a few other simple clean ups:
- skip slot if it's not scratched before detecting whether it's valid;
- move takingspilled_regpointer outside of switch (only DYNPTR hasto adjust that to get to the "main" slot);- don't recalculate
types_bufsecond time for MISC/ZERO/default case.Signed-off-by Andrii Nakryiko <[email protected]>
This is great!
Andrii Nakryiko
maintainerPATCH 5/8 bpf: emit map name in register state if applicable and available
In reply to Andrii NakryikoIn complicated real-world applications, whenever debugging some verification error through verifier log, it often would be very useful to see map name for PTR_TO_MAP_VALUE register. Usually this needs to be inferred from key/value sizes and maybe trying to guess C code location, but it's not always clear.
Given verifier has the name, and it's never too long, let's just emit it for ptr_to_map_key, ptr_to_map_value, and const_ptr_to_map registers. We reshuffle the order a bit, so that map name, key size, and value size appear before offset and immediate values, which seems like a more logical order.
Current output:
R1_w=map_ptr(map=array_map,ks=4,vs=8,off=0,imm=0)
But we'll get rid of useless off=0 and imm=0 parts in the next patch.
--- kernel/bpf/log.c | 24 ++++++++++++++----- .../selftests/bpf/prog_tests/spin_lock.c | 10 ++++---- 2 files changed, 23 insertions(+), 11 deletions(-)
kernel/bpf/log.c +18 -6
@@ -553,6 +553,17 @@ static void print_scalar_ranges(struct bpf_verifier_env *env, } } +static bool type_is_map_ptr (enum bpf_reg_type t) { +switch (base_type (t)) { +case CONST_PTR_TO_MAP : +case PTR_TO_MAP_KEY : +case PTR_TO_MAP_VALUE : +return true; +default : +return false; + } +} +static void print_reg_state (struct bpf_verifier_env *env,const struct bpf_reg_state *reg) {enum bpf_reg_type t;
@@ -584,16 +595,17 @@ static void print_reg_state(struct bpf_verifier_env *env, const struct bpf_reg_sverbose_a ("ref_obj_id=%d" , reg->ref_obj_id);if (type_is_non_owning_ref (reg->type))verbose_a ("%s" ,"non_own_ref" ); +if (type_is_map_ptr (t)) { +if (reg->map_ptr->name[0 ]) +verbose_a ("map=%s" , reg->map_ptr->name); +verbose_a ("ks=%d,vs=%d" , + reg->map_ptr->key_size, + reg->map_ptr->value_size); + }if (t !=SCALAR_VALUE )verbose_a ("off=%d" , reg->off);if (type_is_pkt_pointer (t))verbose_a ("r=%d" , reg->range); -else if (base_type (t) ==CONST_PTR_TO_MAP || -base_type (t) ==PTR_TO_MAP_KEY || -base_type (t) ==PTR_TO_MAP_VALUE ) -verbose_a ("ks=%d,vs=%d" , - reg->map_ptr->key_size, - reg->map_ptr->value_size);if (tnum_is_const (reg->var_off)) {/* Typically an immediate SCALAR_VALUE, but * could be a pointer whose offset is too big
tools/testing/selftests/bpf/prog_tests/spin_lock.c +5 -5
@@ -17,18 +17,18 @@ static struct {"R1_w=ptr_foo(id=2,ref_obj_id=2,off=0,imm=0) refs=2\n6: (85) call bpf_this_cpu_ptr#154\n" "R1 type=ptr_ expected=percpu_ptr_" }, { "lock_id_global_zero", -"; R1_w=map_value(off=0,ks=4,vs=4,imm=0)\n2: (85) call bpf_this_cpu_ptr#154\n" +"; R1_w=map_value(map=.data.A,ks=4,vs=4,off=0,imm=0)\n2: (85) call bpf_this_cpu_ptr#154\n" "R1 type=map_value expected=percpu_ptr_" }, { "lock_id_mapval_preserve","[0-9]\\+: (bf) r1 = r0 ;" -" R0_w=map_value(id=1,off=0,ks=4,vs=8,imm=0)" -" R1_w=map_value(id=1,off=0,ks=4,vs=8,imm=0)\n" +" R0_w=map_value(id=1,map=array_map,ks=4,vs=8,off=0,imm=0)" +" R1_w=map_value(id=1,map=array_map,ks=4,vs=8,off=0,imm=0)\n" "[0-9]\\+: (85) call bpf_this_cpu_ptr#154\n" "R1 type=map_value expected=percpu_ptr_" }, { "lock_id_innermapval_preserve","[0-9]\\+: (bf) r1 = r0 ;" -" R0=map_value(id=2,off=0,ks=4,vs=8,imm=0)" -" R1_w=map_value(id=2,off=0,ks=4,vs=8,imm=0)\n" +" R0=map_value(id=2,ks=4,vs=8,off=0,imm=0)" +" R1_w=map_value(id=2,ks=4,vs=8,off=0,imm=0)\n" "[0-9]\\+: (85) call bpf_this_cpu_ptr#154\n" "R1 type=map_value expected=percpu_ptr_" }, { "lock_id_mismatch_kptr_kptr", "bpf_spin_unlock of different lock" },
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH 6/8 bpf: omit default off=0 and imm=0 in register state log
In reply to Andrii NakryikoSimplify BPF verifierl log furthre by omitting default (and frequently irrelevant) off=0 and imm=0 parts for non-SCALAR_VALUE registers. As can be seen from fixed tests, this is often a visual noise for PTR_TO_CTX register and even for PTR_TO_PACKET registers.
Omitting default values follows the rest of register state logic: we omit default values to keep verifier log succinct and to highlight interesting state that deviates from default one. E.g., we do the same for var_off, when it's unknown, which gives no additional information.
--- kernel/bpf/log.c | 10 ++--- .../testing/selftests/bpf/prog_tests/align.c | 42 +++++++++---------- .../selftests/bpf/prog_tests/log_buf.c | 4 +- .../selftests/bpf/prog_tests/spin_lock.c | 14 +++---- .../selftests/bpf/progs/exceptions_assert.c | 10 ++--- 5 files changed, 39 insertions(+), 41 deletions(-)
kernel/bpf/log.c +4 -6
@@ -602,16 +602,14 @@ static void print_reg_state(struct bpf_verifier_env *env, const struct bpf_reg_s reg->map_ptr->key_size, reg->map_ptr->value_size); } -if (t !=SCALAR_VALUE ) +if (t !=SCALAR_VALUE && reg->off)verbose_a ("off=%d" , reg->off);if (type_is_pkt_pointer (t))verbose_a ("r=%d" , reg->range);if (tnum_is_const (reg->var_off)) { -/* Typically an immediate SCALAR_VALUE, but -* could be a pointer whose offset is too big -* for reg->off -*/ -verbose_a ("imm=%llx" , reg->var_off.value); +/* a pointer register with fixed offset */ +if (reg->var_off.value) +verbose_a ("imm=%llx" , reg->var_off.value); }else {print_scalar_ranges (env, reg, &sep);if (!tnum_is_unknown (reg->var_off)) {
tools/testing/selftests/bpf/prog_tests/align.c +21 -21
129 lines not on this page · raw message
tools/testing/selftests/bpf/prog_tests/log_buf.c +2 -2
@@ -78,7 +78,7 @@ static void obj_load_log_buf(void)ASSERT_OK_PTR (strstr (libbpf_log_buf,"prog 'bad_prog': BPF program load failed" ),"libbpf_log_not_empty" );ASSERT_OK_PTR (strstr (obj_log_buf,"DATASEC license" ),"obj_log_not_empty" ); -ASSERT_OK_PTR (strstr (good_log_buf,"0: R1=ctx(off=0,imm=0) R10=fp0" ), +ASSERT_OK_PTR (strstr (good_log_buf,"0: R1=ctx() R10=fp0" ),"good_log_verbose" );ASSERT_OK_PTR (strstr (bad_log_buf,"invalid access to map value, value_size=16 off=16000 size=4" ),"bad_log_not_empty" );
@@ -175,7 +175,7 @@ static void bpf_prog_load_log_buf(void) opts.log_level =2 ; fd =bpf_prog_load (BPF_PROG_TYPE_SOCKET_FILTER ,"good_prog" ,"GPL" , good_prog_insns, good_prog_insn_cnt, &opts); -ASSERT_OK_PTR (strstr (log_buf,"0: R1=ctx(off=0,imm=0) R10=fp0" ),"good_log_2" ); +ASSERT_OK_PTR (strstr (log_buf,"0: R1=ctx() R10=fp0" ),"good_log_2" );ASSERT_GE (fd,0 ,"good_fd2" );if (fd >=0 )close (fd);
tools/testing/selftests/bpf/prog_tests/spin_lock.c +7 -7
@@ -13,22 +13,22 @@ static struct {const char *err_msg; } spin_lock_fail_tests[] = { {"lock_id_kptr_preserve" , -"5: (bf) r1 = r0 ; R0_w=ptr_foo(id=2,ref_obj_id=2,off=0,imm=0) " -"R1_w=ptr_foo(id=2,ref_obj_id=2,off=0,imm=0) refs=2\n6: (85) call bpf_this_cpu_ptr#154\n" +"5: (bf) r1 = r0 ; R0_w=ptr_foo(id=2,ref_obj_id=2) " +"R1_w=ptr_foo(id=2,ref_obj_id=2) refs=2\n6: (85) call bpf_this_cpu_ptr#154\n" "R1 type=ptr_ expected=percpu_ptr_" }, { "lock_id_global_zero", -"; R1_w=map_value(map=.data.A,ks=4,vs=4,off=0,imm=0)\n2: (85) call bpf_this_cpu_ptr#154\n" +"; R1_w=map_value(map=.data.A,ks=4,vs=4)\n2: (85) call bpf_this_cpu_ptr#154\n" "R1 type=map_value expected=percpu_ptr_" }, { "lock_id_mapval_preserve","[0-9]\\+: (bf) r1 = r0 ;" -" R0_w=map_value(id=1,map=array_map,ks=4,vs=8,off=0,imm=0)" -" R1_w=map_value(id=1,map=array_map,ks=4,vs=8,off=0,imm=0)\n" +" R0_w=map_value(id=1,map=array_map,ks=4,vs=8)" +" R1_w=map_value(id=1,map=array_map,ks=4,vs=8)\n" "[0-9]\\+: (85) call bpf_this_cpu_ptr#154\n" "R1 type=map_value expected=percpu_ptr_" }, { "lock_id_innermapval_preserve","[0-9]\\+: (bf) r1 = r0 ;" -" R0=map_value(id=2,ks=4,vs=8,off=0,imm=0)" -" R1_w=map_value(id=2,ks=4,vs=8,off=0,imm=0)\n" +" R0=map_value(id=2,ks=4,vs=8)" +" R1_w=map_value(id=2,ks=4,vs=8)\n" "[0-9]\\+: (85) call bpf_this_cpu_ptr#154\n" "R1 type=map_value expected=percpu_ptr_" }, { "lock_id_mismatch_kptr_kptr", "bpf_spin_unlock of different lock" },
tools/testing/selftests/bpf/progs/exceptions_assert.c +5 -5
@@ -59,7 +59,7 @@ check_assert(s64, ge, neg, INT_MIN); SEC("?tc") __log_level(2) __failure -__msg(": R0=0 R1=ctx(off=0,imm=0) R2=scalar(smin=smin32=-2147483646,smax=smax32=2147483645) R10=fp0") +__msg(": R0=0 R1=ctx() R2=scalar(smin=smin32=-2147483646,smax=smax32=2147483645) R10=fp0")int check_assert_range_s64 (struct __sk_buff *ctx) {struct bpf_sock *sk = ctx->sk;
@@ -75,7 +75,7 @@ int check_assert_range_s64(struct __sk_buff *ctx) SEC("?tc") __log_level(2) __failure -__msg(": R1=ctx(off=0,imm=0) R2=scalar(smin=umin=smin32=umin32=4096,smax=umax=smax32=umax32=8192,var_off=(0x0; 0x3fff))") +__msg(": R1=ctx() R2=scalar(smin=umin=smin32=umin32=4096,smax=umax=smax32=umax32=8192,var_off=(0x0; 0x3fff))")int check_assert_range_u64 (struct __sk_buff *ctx) {u64 num = ctx->len;
@@ -86,7 +86,7 @@ int check_assert_range_u64(struct __sk_buff *ctx) SEC("?tc") __log_level(2) __failure -__msg(": R0=0 R1=ctx(off=0,imm=0) R2=4096 R10=fp0") +__msg(": R0=0 R1=ctx() R2=4096 R10=fp0")int check_assert_single_range_s64 (struct __sk_buff *ctx) {struct bpf_sock *sk = ctx->sk;
@@ -103,7 +103,7 @@ int check_assert_single_range_s64(struct __sk_buff *ctx) SEC("?tc") __log_level(2) __failure -__msg(": R1=ctx(off=0,imm=0) R2=4096 R10=fp0") +__msg(": R1=ctx() R2=4096 R10=fp0")int check_assert_single_range_u64 (struct __sk_buff *ctx) {u64 num = ctx->len;
@@ -114,7 +114,7 @@ int check_assert_single_range_u64(struct __sk_buff *ctx) SEC("?tc") __log_level(2) __failure -__msg(": R1=pkt(off=64,r=64,imm=0) R2=pkt_end(off=0,imm=0) R6=pkt(off=0,r=64,imm=0) R10=fp0") +__msg(": R1=pkt(off=64,r=64) R2=pkt_end() R6=pkt(r=64) R10=fp0")int check_assert_generic (struct __sk_buff *ctx) {u8 *data_end = (void *)(long )ctx->data_end;
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH 7/8 bpf: smarter verifier log number printing logic
In reply to Andrii NakryikoInstead of always printing numbers as either decimals (and in some cases, like for "imm=%llx", in hexadecimals), decide the form based on actual values. For numbers in a reasonably small range (currently, [0, U16_MAX] for unsigned values, and [S16_MIN, S16_MAX] for signed ones), emit them as decimals. In all other cases, even for signed values, emit them in hexadecimals.
For large values hex form is often times way more useful: it's easier to see an exact difference between 0xffffffff80000000 and 0xffffffff7fffffff, than between 18446744071562067966 and 18446744071562067967, as one particular example.
Small values representing small pointer offsets or application constants, on the other hand, are way more useful to be represented in decimal notation.
--- kernel/bpf/log.c | 67 +++++++++++++++++-- .../selftests/bpf/progs/exceptions_assert.c | 32 ++++----- 2 files changed, 76 insertions(+), 23 deletions(-)
kernel/bpf/log.c +60 -7
@@ -509,10 +509,52 @@ static void print_liveness(struct bpf_verifier_env *env,verbose (env,"D" ); } +#define UNUM_MAX_DECIMAL U16_MAX +#define SNUM_MAX_DECIMAL S16_MAX +#define SNUM_MIN_DECIMAL S16_MIN + +static bool is_unum_decimal (u64 num) +{ +return num <=UNUM_MAX_DECIMAL ; +} + +static bool is_snum_decimal (s64 num) +{ +return num >=SNUM_MIN_DECIMAL && num <=SNUM_MAX_DECIMAL ; +} + +static void verbose_unum (struct bpf_verifier_env *env,u64 num) +{ +if (is_unum_decimal (num)) +verbose (env,"%llu" , num); +else +verbose (env,"%#llx" , num); +} + +static void verbose_snum (struct bpf_verifier_env *env,s64 num) +{ +if (is_snum_decimal (num)) +verbose (env,"%lld" , num); +else +verbose (env,"%#llx" , num); +} +static void print_scalar_ranges (struct bpf_verifier_env *env,const struct bpf_reg_state *reg,const char **sep) { +/* For signed ranges, we want to unify 64-bit and 32-bit values in the +* output as much as possible, but there is a bit of a complication. +* If we choose to print values as decimals, this is natural to do, +* because negative 64-bit and 32-bit values >= -S32_MIN have the same +* representation due to sign extension. But if we choose to print +* them in hex format (see is_snum_decimal()), then sign extension is +* misleading. +* E.g., smin=-2 and smin32=-2 are exactly the same in decimal, but in +* hex they will be smin=0xfffffffffffffffe and smin32=0xfffffffe, two +* very different numbers. +* So we avoid sign extension if we choose to print values in hex. +*/ struct {const char *name;u64 val;
@@ -522,8 +564,14 @@ static void print_scalar_ranges(struct bpf_verifier_env *env, {"smax", reg->smax_value, reg->smax_value == S64_MAX}, {"umin", reg->umin_value, reg->umin_value == 0}, {"umax", reg->umax_value, reg->umax_value == U64_MAX}, - {"smin32", (s64)reg->s32_min_value, reg->s32_min_value == S32_MIN}, - {"smax32", (s64)reg->s32_max_value, reg->s32_max_value == S32_MAX}, + {"smin32", +is_snum_decimal ((s64 )reg->s32_min_value) + ? (s64 )reg->s32_min_value + : (u32)reg->s32_min_value, reg->s32_min_value == S32_MIN}, + {"smax32", +is_snum_decimal ((s64 )reg->s32_max_value) + ? (s64 )reg->s32_max_value + : (u32)reg->s32_max_value, reg->s32_max_value == S32_MAX}, {"umin32", reg->u32_min_value, reg->u32_min_value == 0}, {"umax32", reg->u32_max_value, reg->u32_max_value == U32_MAX}, }, *m1, *m2, *mend = &minmaxs[ARRAY_SIZE(minmaxs)];
@@ -549,7 +597,10 @@ static void print_scalar_ranges(struct bpf_verifier_env *env,verbose (env,"%s=" , m2->name); } -verbose (env, m1->name[0 ] =='s' ?"%lld" :"%llu" , m1->val); +if (m1->name[0 ] =='s' ) +verbose_snum (env, m1->val); +else +verbose_unum (env, m1->val); } }
@@ -576,14 +627,14 @@ static void print_reg_state(struct bpf_verifier_env *env, const struct bpf_reg_s tnum_is_const(reg->var_off)) {/* reg->off should be 0 for SCALAR_VALUE */ verbose (env,"%s" , t ==SCALAR_VALUE ?"" :reg_type_str (env, t)); -verbose (env,"%lld" , reg->var_off.value + reg->off); +verbose_snum (env, reg->var_off.value + reg->off);return ; }/* * _a stands for append, was shortened to avoid multiline statements below. * This macro is used to output a comma separated list of attributes. */ -#define verbose_a (fmt, ...) ({ verbose(env, "%s" fmt, sep, __VA_ARGS__); sep = ","; }) +#define verbose_a (fmt, ...) ({ verbose(env, "%s" fmt, sep, ##__VA_ARGS__); sep = ","; })verbose (env,"%s" ,reg_type_str (env, t));if (base_type (t) ==PTR_TO_BTF_ID )
@@ -608,8 +659,10 @@ static void print_reg_state(struct bpf_verifier_env *env, const struct bpf_reg_sverbose_a ("r=%d" , reg->range);if (tnum_is_const (reg->var_off)) {/* a pointer register with fixed offset */ -if (reg->var_off.value) -verbose_a ("imm=%llx" , reg->var_off.value); +if (reg->var_off.value) { +verbose_a ("imm=" ); +verbose_snum (env, reg->var_off.value); + } }else {print_scalar_ranges (env, reg, &sep);if (!tnum_is_unknown (reg->var_off)) {
tools/testing/selftests/bpf/progs/exceptions_assert.c +16 -16
@@ -18,48 +18,48 @@return *(u64 *)num; \ } -__msg(": R0_w=-2147483648 R10=fp0") +__msg(": R0_w=0xffffffff80000000 R10=fp0")check_assert (s64, eq, int_min,INT_MIN ); -__msg(": R0_w=2147483647 R10=fp0") +__msg(": R0_w=0x7fffffff R10=fp0")check_assert (s64, eq, int_max,INT_MAX ); __msg(": R0_w=0 R10=fp0")check_assert (s64, eq, zero,0 ); -__msg(": R0_w=-9223372036854775808 R1_w=-9223372036854775808 R10=fp0") +__msg(": R0_w=0x8000000000000000 R1_w=0x8000000000000000 R10=fp0")check_assert (s64, eq, llong_min,LLONG_MIN ); -__msg(": R0_w=9223372036854775807 R1_w=9223372036854775807 R10=fp0") +__msg(": R0_w=0x7fffffffffffffff R1_w=0x7fffffffffffffff R10=fp0")check_assert (s64, eq, llong_max,LLONG_MAX ); -__msg(": R0_w=scalar(smax=2147483646) R10=fp0") +__msg(": R0_w=scalar(smax=0x7ffffffe) R10=fp0")check_assert (s64, lt, pos,INT_MAX ); -__msg(": R0_w=scalar(smax=-1,umin=9223372036854775808,var_off=(0x8000000000000000; 0x7fffffffffffffff))") +__msg(": R0_w=scalar(smax=-1,umin=0x8000000000000000,var_off=(0x8000000000000000; 0x7fffffffffffffff))")check_assert (s64, lt, zero,0 ); -__msg(": R0_w=scalar(smax=-2147483649,umin=9223372036854775808,umax=18446744071562067967,var_off=(0x8000000000000000; 0x7fffffffffffffff))") +__msg(": R0_w=scalar(smax=0xffffffff7fffffff,umin=0x8000000000000000,umax=0xffffffff7fffffff,var_off=(0x8000000000000000; 0x7fffffffffffffff))")check_assert (s64, lt, neg,INT_MIN ); -__msg(": R0_w=scalar(smax=2147483647) R10=fp0") +__msg(": R0_w=scalar(smax=0x7fffffff) R10=fp0")check_assert (s64, le, pos,INT_MAX ); __msg(": R0_w=scalar(smax=0) R10=fp0")check_assert (s64, le, zero,0 ); -__msg(": R0_w=scalar(smax=-2147483648,umin=9223372036854775808,umax=18446744071562067968,var_off=(0x8000000000000000; 0x7fffffffffffffff))") +__msg(": R0_w=scalar(smax=0xffffffff80000000,umin=0x8000000000000000,umax=0xffffffff80000000,var_off=(0x8000000000000000; 0x7fffffffffffffff))")check_assert (s64, le, neg,INT_MIN ); -__msg(": R0_w=scalar(smin=umin=2147483648,umax=9223372036854775807,var_off=(0x0; 0x7fffffffffffffff))") +__msg(": R0_w=scalar(smin=umin=0x80000000,umax=0x7fffffffffffffff,var_off=(0x0; 0x7fffffffffffffff))")check_assert (s64, gt, pos,INT_MAX ); -__msg(": R0_w=scalar(smin=umin=1,umax=9223372036854775807,var_off=(0x0; 0x7fffffffffffffff))") +__msg(": R0_w=scalar(smin=umin=1,umax=0x7fffffffffffffff,var_off=(0x0; 0x7fffffffffffffff))")check_assert (s64, gt, zero,0 ); -__msg(": R0_w=scalar(smin=-2147483647) R10=fp0") +__msg(": R0_w=scalar(smin=0xffffffff80000001) R10=fp0")check_assert (s64, gt, neg,INT_MIN ); -__msg(": R0_w=scalar(smin=umin=2147483647,umax=9223372036854775807,var_off=(0x0; 0x7fffffffffffffff))") +__msg(": R0_w=scalar(smin=umin=0x7fffffff,umax=0x7fffffffffffffff,var_off=(0x0; 0x7fffffffffffffff))")check_assert (s64, ge, pos,INT_MAX ); -__msg(": R0_w=scalar(smin=0,umax=9223372036854775807,var_off=(0x0; 0x7fffffffffffffff)) R10=fp0") +__msg(": R0_w=scalar(smin=0,umax=0x7fffffffffffffff,var_off=(0x0; 0x7fffffffffffffff)) R10=fp0")check_assert (s64, ge, zero,0 ); -__msg(": R0_w=scalar(smin=-2147483648) R10=fp0") +__msg(": R0_w=scalar(smin=0xffffffff80000000) R10=fp0")check_assert (s64, ge, neg,INT_MIN ); SEC("?tc") __log_level(2) __failure -__msg(": R0=0 R1=ctx() R2=scalar(smin=smin32=-2147483646,smax=smax32=2147483645) R10=fp0") +__msg(": R0=0 R1=ctx() R2=scalar(smin=0xffffffff80000002,smax=smax32=0x7ffffffd,smin32=0x80000002) R10=fp0") int check_assert_range_s64(struct __sk_buff *ctx) {struct bpf_sock *sk = ctx->sk;
Signature · 1 line
2.34.1
Re: [PATCH bpf-next 7/8] bpf: smarter verifier log number printing logic
Quoted 12 lines · Andrii Nakryiko
Instead of always printing numbers as either decimals (and in some cases, like for "imm=%llx", in hexadecimals), decide the form based on actual values. For numbers in a reasonably small range (currently, [0,
U16_MAX] for unsigned values, and [S16_MIN,S16_MAX] for signed ones), emit them as decimals. In all other cases, even for signed values, emit them in hexadecimals.For large values hex form is often times way more useful: it's easier to see an exact difference between
0xffffffff80000000and0xffffffff7fffffff, than between 18446744071562067966 and 18446744071562067967, as one particular example.
Quoted 5 lines
Small values representing small pointer offsets or application constants, on the other hand, are way more useful to be represented in decimal notation.
Signed-off-by Andrii Nakryiko <[email protected]>
[...]
Quoted 18 lines
kernel/bpf/log.c +2
-2@@ -576,14 +627,14 @@ static void print_reg_state(struct bpf_verifier_env *env, const struct bpf_reg_s tnum_is_const(reg->var_off)) {/* reg->off should be 0 for SCALAR_VALUE */ verbose (env,"%s" , t ==SCALAR_VALUE ?"" :reg_type_str (env, t)); -verbose (env,"%lld" , reg->var_off.value + reg->off); +verbose_snum (env, reg->var_off.value + reg->off);return ; }/* * _a stands for append, was shortened to avoid multiline statements below. * This macro is used to output a comma separated list of attributes. */ -#define verbose_a (fmt, ...) ({ verbose(env, "%s" fmt, sep, __VA_ARGS__); sep = ","; }) +#define verbose_a (fmt, ...) ({ verbose(env, "%s" fmt, sep, ##__VA_ARGS__); sep = ","; })verbose (env,"%s" ,reg_type_str (env, t));if (base_type (t) ==PTR_TO_BTF_ID )
kernel/bpf/log.c static void print_reg_state(struct bpf_verifier_env *env, const struct bpf_reg_s · line 665
Quoted 9 lines
kernel/bpf/log.c +4
-2@@ -608,8 +659,10 @@ static void print_reg_state(struct bpf_verifier_env *env, const struct bpf_reg_sverbose_a ("r=%d" , reg->range);if (tnum_is_const (reg->var_off)) {/* a pointer register with fixed offset */ -if (reg->var_off.value) -verbose_a ("imm=%llx" , reg->var_off.value); +if (reg->var_off.value) { +verbose_a ("imm=" ); +verbose_snum (env, reg->var_off.value); + }
Maybe use same verbose_{u,s}num() for reg->off and reg->range in this function?
Re: [PATCH bpf-next 7/8] bpf: smarter verifier log number printing logic
Quoted 38 lines · Eduard Zingerman
Quoted 12 lines · Andrii Nakryiko
Instead of always printing numbers as either decimals (and in some cases, like for "imm=%llx", in hexadecimals), decide the form based on actual values. For numbers in a reasonably small range (currently, [0,
U16_MAX] for unsigned values, and [S16_MIN,S16_MAX] for signed ones), emit them as decimals. In all other cases, even for signed values, emit them in hexadecimals.For large values hex form is often times way more useful: it's easier to see an exact difference between
0xffffffff80000000and0xffffffff7fffffff, than between 18446744071562067966 and 18446744071562067967, as one particular example.Quoted 5 lines
Small values representing small pointer offsets or application constants, on the other hand, are way more useful to be represented in decimal notation.
Signed-off-by Andrii Nakryiko <[email protected]>Acked-by Eduard Zingerman <[email protected]>[...]
Quoted 15 lines
kernel/bpf/log.c +2
-2@@ -576,14 +627,14 @@ static void print_reg_state(struct bpf_verifier_env *env, const struct bpf_reg_s tnum_is_const(reg->var_off)) {/* reg->off should be 0 for SCALAR_VALUE */ verbose (env,"%s" , t ==SCALAR_VALUE ?"" :reg_type_str (env, t)); -verbose (env,"%lld" , reg->var_off.value + reg->off); +verbose_snum (env, reg->var_off.value + reg->off);return ; }/* * _a stands for append, was shortened to avoid multiline statements below. * This macro is used to output a comma separated list of attributes. */ -#define verbose_a (fmt, ...) ({ verbose(env, "%s" fmt, sep, __VA_ARGS__); sep = ","; }) +#define verbose_a (fmt, ...) ({ verbose(env, "%s" fmt, sep, ##__VA_ARGS__); sep = ","; })
Quoted 15 lines
kernel/bpf/log.c static void print_reg_state(struct bpf_verifier_env *env, const struct bpf_reg_s · line 665
Quoted 12 lines
kernel/bpf/log.c +4
-2verbose (env,"%s" ,reg_type_str (env, t));if (base_type (t) ==PTR_TO_BTF_ID )@@ -608,8 +659,10 @@ static void print_reg_state(struct bpf_verifier_env *env, const struct bpf_reg_sverbose_a ("r=%d" , reg->range);if (tnum_is_const (reg->var_off)) {/* a pointer register with fixed offset */ -if (reg->var_off.value) -verbose_a ("imm=%llx" , reg->var_off.value); +if (reg->var_off.value) { +verbose_a ("imm=" ); +verbose_snum (env, reg->var_off.value); + }Maybe use same verbose_{u,s}num() for
reg->offandreg->rangein this function?
sure, will do!
Andrii Nakryiko
maintainerPATCH 8/8 bpf: emit frameno for PTR_TO_STACK regs if it differs from current one
In reply to Andrii NakryikoIt's possible to pass a pointer to parent's stack to child subprogs. In such case verifier state output is ambiguous not showing whether register container a pointer to "current" stack, belonging to current subprog (frame), or it's actually a pointer to one of parent frames.
So emit this information if frame number differs between the state which register is part of. E.g., if current state is in frame 2 and it has a register pointing to stack in grand parent state (frame #0), we'll see something like 'R1=fp[0]-16', while "local stack pointer" will be just 'R2=fp-16'.
--- kernel/bpf/log.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
kernel/bpf/log.c +14 -6
@@ -615,7 +615,9 @@ static bool type_is_map_ptr(enum bpf_reg_type t) { } } -static void print_reg_state (struct bpf_verifier_env *env,const struct bpf_reg_state *reg) +static void print_reg_state (struct bpf_verifier_env *env, +const struct bpf_func_state *state, +const struct bpf_reg_state *reg) {enum bpf_reg_type t;const char *sep ="" ;
@@ -623,10 +625,8 @@ static void print_reg_state(struct bpf_verifier_env *env, const struct bpf_reg_s t = reg->type;if (t ==SCALAR_VALUE && reg->precise)verbose (env,"P" ); -if ((t ==SCALAR_VALUE || t ==PTR_TO_STACK ) && -tnum_is_const (reg->var_off)) { +if (t ==SCALAR_VALUE &&tnum_is_const (reg->var_off)) {/* reg->off should be 0 for SCALAR_VALUE */ -verbose (env,"%s" , t ==SCALAR_VALUE ?"" :reg_type_str (env, t));verbose_snum (env, reg->var_off.value + reg->off);return ; }
@@ -637,6 +637,14 @@ static void print_reg_state(struct bpf_verifier_env *env, const struct bpf_reg_s#define verbose_a (fmt, ...) ({ verbose(env, "%s" fmt, sep, ##__VA_ARGS__); sep = ","; })verbose (env,"%s" ,reg_type_str (env, t)); +if (t ==PTR_TO_STACK ) { +if (state->frameno != reg->frameno) +verbose (env,"[%d]" , reg->frameno); +if (tnum_is_const (reg->var_off)) { +verbose_snum (env, reg->var_off.value + reg->off); +return ; + } + }if (base_type (t) ==PTR_TO_BTF_ID )verbose (env,"%s" ,btf_type_name (reg->btf, reg->btf_id));verbose (env,"(" );
@@ -694,7 +702,7 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_func_stverbose (env," R%d" , i);print_liveness (env, reg->live);verbose (env,"=" ); -print_reg_state (env, reg); +print_reg_state (env, state, reg); }for (i =0 ; i < state->allocated_stack /BPF_REG_SIZE ; i++) {char types_buf[BPF_REG_SIZE +1 ];
@@ -727,7 +735,7 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_func_stverbose (env," fp%d" , (-i -1 ) *BPF_REG_SIZE );print_liveness (env, reg->live);verbose (env,"=%s" , types_buf); -print_reg_state (env, reg); +print_reg_state (env, state, reg);break ;case STACK_DYNPTR :/* skip to main dynptr slot */
Signature · 1 line
2.34.1
Quoted 18 lines · Andrii Nakryiko
This patch set moves a big chunk of verifier log related code from gigantic
verifier.cfile into more focusedkernel/bpf/log.c. This is not essential to the rest of functionality in this patch set, so I can undo it, but it felt like it's good to start chipping away from 20K+verifier.cwhenever we can.The main purpose of the patch set, though, is in improving verifier log further.
Patches #3-#4 start printing out register state even if that register is spilled into stack slot. Previously we'd get only spilled register type, but no additional information, like SCALAR_VALUE's ranges. Super limiting during debugging. For cases of register spills smaller than 8 bytes, we also print out STACK_MISC/STACK_ZERO/STACK_INVALID markers. This, among other things, will make it easier to write tests for these mixed spill/misc cases.
Patch #5 prints map name for PTR_TO_MAP_VALUE/PTR_TO_MAP_KEY/CONST_PTR_TO_MAP registers. In big production BPF programs, it's important to map assembly to actual map, and it's often non-trivial. Having map name helps.
[..]
Quoted 2 lines
Patch #6 just removes visual noise in form of ubiquitous imm=0 and off=0. They are default values, omit them.
If you end up with another respin for some reason:
s/verifierl/verifier/
s/furthre/futher/
(in the commit description)
Quoted 5 lines
Patch #7 is probably the most controversial, but it reworks how verifier log prints numbers. For small valued integers we use decimals, but for large ones we switch to hexadecimal. From personal experience this is a much more useful convention. We can tune what consitutes "small value", for now it's 16-bit range.
Not sure why not always print in hex, but no strong preference here.
Quoted 5 lines
Patch #8 prints frame number for
PTR_TO_CTXregisters, if that frame is different from the "current" one. This removes ambiguity and confusion, especially in complicated cases with multiple subprogs passing around pointers.
Quoted 9 lines
Andrii Nakryiko (8):
bpf: moveverbose_linfo()intokernel/bpf/log.c
bpf: move verifier state printing code tokernel/bpf/log.c
bpf: extract register state printing
bpf: print spilled register state in stack slot
bpf: emit map name in register state if applicable and available
bpf: omit default off=0 and imm=0 in register state log
bpf: smarter verifier log number printing logic
bpf: emit frameno forPTR_TO_STACKregs if it differs from current one
Quoted 23 lines · Stanislav Fomichev
Quoted 18 lines · Andrii Nakryiko
This patch set moves a big chunk of verifier log related code from gigantic
verifier.cfile into more focusedkernel/bpf/log.c. This is not essential to the rest of functionality in this patch set, so I can undo it, but it felt like it's good to start chipping away from 20K+verifier.cwhenever we can.The main purpose of the patch set, though, is in improving verifier log further.
Patches #3-#4 start printing out register state even if that register is spilled into stack slot. Previously we'd get only spilled register type, but no additional information, like SCALAR_VALUE's ranges. Super limiting during debugging. For cases of register spills smaller than 8 bytes, we also print out STACK_MISC/STACK_ZERO/STACK_INVALID markers. This, among other things, will make it easier to write tests for these mixed spill/misc cases.
Patch #5 prints map name for PTR_TO_MAP_VALUE/PTR_TO_MAP_KEY/CONST_PTR_TO_MAP registers. In big production BPF programs, it's important to map assembly to actual map, and it's often non-trivial. Having map name helps.
[..]
Quoted 8 lines
Quoted 2 lines
Patch #6 just removes visual noise in form of ubiquitous imm=0 and off=0. They are default values, omit them.
If you end up with another respin for some reason:
s/verifierl/verifier/
s/furthre/futher/(in the commit description)
thanks, fixed it up locally, but will wait for the other feedback
Quoted 8 lines
Quoted 6 lines
Patch #7 is probably the most controversial, but it reworks how verifier log prints numbers. For small valued integers we use decimals, but for large ones we switch to hexadecimal. From personal experience this is a much more useful convention. We can tune what consitutes "small value", for now it's 16-bit range.
Not sure why not always print in hex, but no strong preference here.
for small values decimal is usually much more sane. E.g., if you have some index into int arr[100];, seeing that register's range is [0, 396] is much more convenient than [0x0, 0x18c], IMO. So I don't think we want to abandon decimals completely.
Quoted 16 lines
Quoted 6 lines
Patch #8 prints frame number for
PTR_TO_CTXregisters, if that frame is different from the "current" one. This removes ambiguity and confusion, especially in complicated cases with multiple subprogs passing around pointers.Quoted 9 lines
Andrii Nakryiko (8):
bpf: moveverbose_linfo()intokernel/bpf/log.c
bpf: move verifier state printing code tokernel/bpf/log.c
bpf: extract register state printing
bpf: print spilled register state in stack slot
bpf: emit map name in register state if applicable and available
bpf: omit default off=0 and imm=0 in register state log
bpf: smarter verifier log number printing logic
bpf: emit frameno forPTR_TO_STACKregs if it differs from current one
Quoted 1 line
thanks!
On Fri, 2023-11-10 at 08:10 -0800, Andrii Nakryiko wrote:
[...]
Quoted 5 lines
Patch #8 prints frame number for
PTR_TO_CTXregisters, if that frame is different from the "current" one. This removes ambiguity and confusion, especially in complicated cases with multiple subprogs passing around pointers.
Quoted 9 lines
Andrii Nakryiko (8):
bpf: moveverbose_linfo()intokernel/bpf/log.c
bpf: move verifier state printing code tokernel/bpf/log.c
bpf: extract register state printing
bpf: print spilled register state in stack slot
bpf: emit map name in register state if applicable and available
bpf: omit default off=0 and imm=0 in register state log
bpf: smarter verifier log number printing logic
bpf: emit frameno forPTR_TO_STACKregs if it differs from current one
This should be very handy, at-least for verifier development.