Skip to content

Commit

Permalink
bpf: simplify tnum output if a fully known constant
Browse files Browse the repository at this point in the history
Emit tnum representation as just a constant if all bits are known.
Use decimal-vs-hex logic to determine exact format of emitted
constant value, just like it's done for register range values.
For that move tnum_strn() to kernel/bpf/log.c to reuse decimal-vs-hex
determination logic and constants.

Acked-by: Shung-Hsi Yu <shung-hsi.yu@suse.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20231202175705.885270-12-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
  • Loading branch information
Andrii Nakryiko authored and Alexei Starovoitov committed Dec 2, 2023
1 parent 5c19e1d commit 81eff2e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
13 changes: 13 additions & 0 deletions kernel/bpf/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,19 @@ static void verbose_snum(struct bpf_verifier_env *env, s64 num)
verbose(env, "%#llx", num);
}

int tnum_strn(char *str, size_t size, struct tnum a)
{
/* print as a constant, if tnum is fully known */
if (a.mask == 0) {
if (is_unum_decimal(a.value))
return snprintf(str, size, "%llu", a.value);
else
return snprintf(str, size, "%#llx", a.value);
}
return snprintf(str, size, "(%#llx; %#llx)", a.value, a.mask);
}
EXPORT_SYMBOL_GPL(tnum_strn);

static void print_scalar_ranges(struct bpf_verifier_env *env,
const struct bpf_reg_state *reg,
const char **sep)
Expand Down
6 changes: 0 additions & 6 deletions kernel/bpf/tnum.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,6 @@ bool tnum_in(struct tnum a, struct tnum b)
return a.value == b.value;
}

int tnum_strn(char *str, size_t size, struct tnum a)
{
return snprintf(str, size, "(%#llx; %#llx)", a.value, a.mask);
}
EXPORT_SYMBOL_GPL(tnum_strn);

int tnum_sbin(char *str, size_t size, struct tnum a)
{
size_t n;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ l0_%=: r0 = 0; \

SEC("tc")
__description("direct packet access: test17 (pruning, alignment)")
__failure __msg("misaligned packet access off 2+(0x0; 0x0)+15+-4 size 4")
__failure __msg("misaligned packet access off 2+0+15+-4 size 4")
__flag(BPF_F_STRICT_ALIGNMENT)
__naked void packet_access_test17_pruning_alignment(void)
{
Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/progs/verifier_int_ptr.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ __naked void ptr_to_long_half_uninitialized(void)

SEC("cgroup/sysctl")
__description("ARG_PTR_TO_LONG misaligned")
__failure __msg("misaligned stack access off (0x0; 0x0)+-20+0 size 8")
__failure __msg("misaligned stack access off 0+-20+0 size 8")
__naked void arg_ptr_to_long_misaligned(void)
{
asm volatile (" \
Expand Down
4 changes: 2 additions & 2 deletions tools/testing/selftests/bpf/progs/verifier_stack_ptr.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ __naked void ptr_to_stack_store_load(void)

SEC("socket")
__description("PTR_TO_STACK store/load - bad alignment on off")
__failure __msg("misaligned stack access off (0x0; 0x0)+-8+2 size 8")
__failure __msg("misaligned stack access off 0+-8+2 size 8")
__failure_unpriv
__naked void load_bad_alignment_on_off(void)
{
Expand All @@ -53,7 +53,7 @@ __naked void load_bad_alignment_on_off(void)

SEC("socket")
__description("PTR_TO_STACK store/load - bad alignment on reg")
__failure __msg("misaligned stack access off (0x0; 0x0)+-10+8 size 8")
__failure __msg("misaligned stack access off 0+-10+8 size 8")
__failure_unpriv
__naked void load_bad_alignment_on_reg(void)
{
Expand Down

0 comments on commit 81eff2e

Please sign in to comment.