Skip to content

Commit 81eff2e

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
bpf: simplify tnum output if a fully known constant
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 <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 5c19e1d commit 81eff2e

File tree

5 files changed

+17
-10
lines changed

5 files changed

+17
-10
lines changed

kernel/bpf/log.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,19 @@ static void verbose_snum(struct bpf_verifier_env *env, s64 num)
539539
verbose(env, "%#llx", num);
540540
}
541541

542+
int tnum_strn(char *str, size_t size, struct tnum a)
543+
{
544+
/* print as a constant, if tnum is fully known */
545+
if (a.mask == 0) {
546+
if (is_unum_decimal(a.value))
547+
return snprintf(str, size, "%llu", a.value);
548+
else
549+
return snprintf(str, size, "%#llx", a.value);
550+
}
551+
return snprintf(str, size, "(%#llx; %#llx)", a.value, a.mask);
552+
}
553+
EXPORT_SYMBOL_GPL(tnum_strn);
554+
542555
static void print_scalar_ranges(struct bpf_verifier_env *env,
543556
const struct bpf_reg_state *reg,
544557
const char **sep)

kernel/bpf/tnum.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,6 @@ bool tnum_in(struct tnum a, struct tnum b)
172172
return a.value == b.value;
173173
}
174174

175-
int tnum_strn(char *str, size_t size, struct tnum a)
176-
{
177-
return snprintf(str, size, "(%#llx; %#llx)", a.value, a.mask);
178-
}
179-
EXPORT_SYMBOL_GPL(tnum_strn);
180-
181175
int tnum_sbin(char *str, size_t size, struct tnum a)
182176
{
183177
size_t n;

tools/testing/selftests/bpf/progs/verifier_direct_packet_access.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ l0_%=: r0 = 0; \
411411

412412
SEC("tc")
413413
__description("direct packet access: test17 (pruning, alignment)")
414-
__failure __msg("misaligned packet access off 2+(0x0; 0x0)+15+-4 size 4")
414+
__failure __msg("misaligned packet access off 2+0+15+-4 size 4")
415415
__flag(BPF_F_STRICT_ALIGNMENT)
416416
__naked void packet_access_test17_pruning_alignment(void)
417417
{

tools/testing/selftests/bpf/progs/verifier_int_ptr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ __naked void ptr_to_long_half_uninitialized(void)
6767

6868
SEC("cgroup/sysctl")
6969
__description("ARG_PTR_TO_LONG misaligned")
70-
__failure __msg("misaligned stack access off (0x0; 0x0)+-20+0 size 8")
70+
__failure __msg("misaligned stack access off 0+-20+0 size 8")
7171
__naked void arg_ptr_to_long_misaligned(void)
7272
{
7373
asm volatile (" \

tools/testing/selftests/bpf/progs/verifier_stack_ptr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ __naked void ptr_to_stack_store_load(void)
3737

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

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

0 commit comments

Comments
 (0)