[PATCH 1/3] bpf: fix signed shift overflows in ARM JIT
Morten Brørup
mb at smartsharesystems.com
Tue Nov 11 08:53:09 CET 2025
> From: Jerin Jacob [mailto:jerinjacobk at gmail.com]
> Sent: Tuesday, 11 November 2025 07.25
>
> On Mon, Nov 10, 2025 at 9:01 PM Marat Khalili
> <marat.khalili at huawei.com> wrote:
> >
> > Left shifts of integer literals and bool values overwriting the sign
> bit
> > were used multiple times in bpf_jit_arm64.c. E.g.:
> >
> > insn = (!!is64) << 31;
> >
> > where is64 has type bool (double bang is a no-op here). The operand
> of
> > left shift was promoted to type int, which when 32-bit wide cannot
> > represent the result. Similarly literal integers have int type by
> > default. Sanitizer produced the following diagnostic during runtime
> > (for various lines):
> >
> > lib/bpf/bpf_jit_arm64.c:241:18: runtime error: left shift of 1 by
> 31
> > places cannot be represented in type 'int'
>
> Wonder why none of the tests in app/test/test_bpf.c able to catch
> this? The generated ARM opcode looks OK (otherwise tests wont pass).
> Could you check what is missing in the app/test/test_bpf.c?
>
> Also SHIFT_VAR32 needs goto common code.
>
>
> >
> > To fix the issue use RTE_BIT32 and similar macros instead.
> >
> > Signed-off-by: Marat Khalili <marat.khalili at huawei.com>
> > ---
> > lib/bpf/bpf_jit_arm64.c | 188 +++++++++++++++++++++++---------------
> --
> > 1 file changed, 107 insertions(+), 81 deletions(-)
> >
> > diff --git a/lib/bpf/bpf_jit_arm64.c b/lib/bpf/bpf_jit_arm64.c
> > index 96b8cd2e03..5f43db0170 100644
> > --- a/lib/bpf/bpf_jit_arm64.c
> > +++ b/lib/bpf/bpf_jit_arm64.c
> > @@ -28,7 +28,33 @@
> > #define A64_ZR 31
> >
> > #define check_imm(n, val) (((val) >= 0) ? !!((val) >> (n)) :
> !!((~val) >> (n)))
> > -#define mask_imm(n, val) ((val) & ((1 << (n)) - 1))
> > +#define mask_imm(n, val) ((val) & (RTE_BIT32(n) - 1))
> > +
> > +/**
> > + * Get the uint32_t shifted value.
> > + *
> > + * Works similarly to RTE_SHIFT_VAL32 but accepts non-literal
> arguments.
> > + * Performs identically to RTE_SHIFT_VAL32 with literal arguments.
> > + *
> > + * @param val
> > + * The value to be shifted, can be non-literal.
> > + * @param nr
> > + * The shift number in range of 0 to (32 - width of val).
> > + */
> > +#define SHIFT_VAR32(val, nr) ((uint32_t)(val) << (nr))
> > +
> > +/**
> > + * Get the uint64_t shifted value.
> > + *
> > + * Works similarly to RTE_SHIFT_VAL64 but accepts non-literal
> arguments.
> > + * Performs identically to RTE_SHIFT_VAL64 with literal arguments.
> > + *
> > + * @param val
> > + * The value to be shifted, can be non-literal.
> > + * @param nr
> > + * The shift number in range of 0 to (64 - width of val).
> > + */
> > +#define SHIFT_VAR64(val, nr) ((uint64_t)(val) << (nr))
Better replace the RTE_SHIFT_VAL32/VAL64 macros with these, to support both literal and non-literal arguments.
More information about the dev
mailing list