bpf: arm64: Silent "UBSAN: negation-overflow" warning
Subsystems (3)
Song Liu
reviewerPATCH 1/1 bpf: arm64: Silent "UBSAN: negation-overflow" warning
With UBSAN, test_bpf.ko triggers warnings like:
UBSAN: negation-overflow in arch/arm64/net/bpf_jit_comp.c:1333:28
negation of -2147483648 cannot be represented in type 's32' (aka 'int'):
Silent these warnings by casting imm to u32 first.
--- arch/arm64/net/bpf_jit_comp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/net/bpf_jit_comp.c +3 -3
@@ -272,7 +272,7 @@ static inline void emit_a64_add_i(const bool is64, const int dst, const int src, {if (is_addsub_imm (imm)) {emit (A64_ADD_I (is64, dst, src, imm), ctx); - }else if (is_addsub_imm (-imm)) { + }else if (is_addsub_imm (-(u32 )imm)) {emit (A64_SUB_I (is64, dst, src, -imm), ctx); }else {emit_a64_mov_i (is64, tmp, imm, ctx);
@@ -1159,7 +1159,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,case BPF_ALU64 |BPF_SUB |BPF_K :if (is_addsub_imm (imm)) {emit (A64_SUB_I (is64, dst, dst, imm), ctx); - }else if (is_addsub_imm (-imm)) { + }else if (is_addsub_imm (-(u32 )imm)) {emit (A64_ADD_I (is64, dst, dst, -imm), ctx); }else {emit_a64_mov_i (is64, tmp, imm, ctx);
@@ -1330,7 +1330,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,case BPF_JMP32 |BPF_JSLE |BPF_K :if (is_addsub_imm (imm)) {emit (A64_CMP_I (is64, dst, imm), ctx); - }else if (is_addsub_imm (-imm)) { + }else if (is_addsub_imm (-(u32 )imm)) {emit (A64_CMN_I (is64, dst, -imm), ctx); }else {emit_a64_mov_i (is64, tmp, imm, ctx);
Signature · 1 line
2.43.5
Xu Kuohai
reviewerQuoted 8 lines · Song Liu
With UBSAN, test_bpf.ko triggers warnings like:
UBSAN: negation-overflow in arch/arm64/net/bpf_jit_comp.c:1333:28negation of -2147483648 cannot be represented in type 's32' (aka 'int'):
Silent these warnings by casting imm to u32 first.
Fixes fd868f148189 ("bpf, arm64: Optimize ADD,SUB,JMP BPF_K using arm64 add/sub immediates")
I doubt this is a bug fix since I checked the build result and found nothing changed.
Quoted 37 lines
Reported-by Breno Leitao <[email protected]>Signed-off-by Song Liu <[email protected]>--- arch/arm64/net/bpf_jit_comp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)arch/arm64/net/bpf_jit_comp.c +3
-3@@ -272,7 +272,7 @@ static inline void emit_a64_add_i(const bool is64, const int dst, const int src, {if (is_addsub_imm (imm)) {emit (A64_ADD_I (is64, dst, src, imm), ctx); - }else if (is_addsub_imm (-imm)) { + }else if (is_addsub_imm (-(u32 )imm)) {emit (A64_SUB_I (is64, dst, src, -imm), ctx); }else {emit_a64_mov_i (is64, tmp, imm, ctx);@@ -1159,7 +1159,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,case BPF_ALU64 |BPF_SUB |BPF_K :if (is_addsub_imm (imm)) {emit (A64_SUB_I (is64, dst, dst, imm), ctx); - }else if (is_addsub_imm (-imm)) { + }else if (is_addsub_imm (-(u32 )imm)) {emit (A64_ADD_I (is64, dst, dst, -imm), ctx); }else {emit_a64_mov_i (is64, tmp, imm, ctx);@@ -1330,7 +1330,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,case BPF_JMP32 |BPF_JSLE |BPF_K :if (is_addsub_imm (imm)) {emit (A64_CMP_I (is64, dst, imm), ctx); - }else if (is_addsub_imm (-imm)) { + }else if (is_addsub_imm (-(u32 )imm)) {emit (A64_CMN_I (is64, dst, -imm), ctx); }else {emit_a64_mov_i (is64, tmp, imm, ctx);
Song Liu
reviewerQuoted 4 lines · Xu Kuohai
Quoted 2 lines · Song Liu
With UBSAN, test_bpf.ko triggers warnings like:
Quoted 8 lines
Quoted 6 lines
UBSAN: negation-overflow in arch/arm64/net/bpf_jit_comp.c:1333:28negation of -2147483648 cannot be represented in type 's32' (aka 'int'):
Silent these warnings by casting imm to u32 first.
Fixes fd868f148189 ("bpf, arm64: Optimize ADD,SUB,JMP BPF_K using arm64 add/sub immediates")I doubt this is a bug fix since I checked the build result and found nothing changed.
This is just to silence the UBSAN. We can remove the Fixes tag.
Thanks,
Song
Quoted 38 lines
Quoted 37 lines
Reported-by Breno Leitao <[email protected]>Signed-off-by Song Liu <[email protected]>--- arch/arm64/net/bpf_jit_comp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)arch/arm64/net/bpf_jit_comp.c +3
-3@@ -272,7 +272,7 @@ static inline void emit_a64_add_i(const bool is64, const int dst, const int src, {if (is_addsub_imm (imm)) {emit (A64_ADD_I (is64, dst, src, imm), ctx); - }else if (is_addsub_imm (-imm)) { + }else if (is_addsub_imm (-(u32 )imm)) {emit (A64_SUB_I (is64, dst, src, -imm), ctx); }else {emit_a64_mov_i (is64, tmp, imm, ctx);@@ -1159,7 +1159,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,case BPF_ALU64 |BPF_SUB |BPF_K :if (is_addsub_imm (imm)) {emit (A64_SUB_I (is64, dst, dst, imm), ctx); - }else if (is_addsub_imm (-imm)) { + }else if (is_addsub_imm (-(u32 )imm)) {emit (A64_ADD_I (is64, dst, dst, -imm), ctx); }else {emit_a64_mov_i (is64, tmp, imm, ctx);@@ -1330,7 +1330,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,case BPF_JMP32 |BPF_JSLE |BPF_K :if (is_addsub_imm (imm)) {emit (A64_CMP_I (is64, dst, imm), ctx); - }else if (is_addsub_imm (-imm)) { + }else if (is_addsub_imm (-(u32 )imm)) {emit (A64_CMN_I (is64, dst, -imm), ctx); }else {emit_a64_mov_i (is64, tmp, imm, ctx);
Quoted 10 lines · Song Liu
With UBSAN, test_bpf.ko triggers warnings like:
UBSAN: negation-overflow in arch/arm64/net/bpf_jit_comp.c:1333:28negation of -2147483648 cannot be represented in type 's32' (aka 'int'):
Silent these warnings by casting imm to u32 first.
Fixes fd868f148189 ("bpf, arm64: Optimize ADD,SUB,JMP BPF_K using arm64 add/sub immediates")Reported-by Breno Leitao <[email protected]>Signed-off-by Song Liu <[email protected]>
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 8 lines · On Tue, 18 Feb 2025 00:02:40 -0800 you wrote:
With UBSAN, test_bpf.ko triggers warnings like:
UBSAN: negation-overflow in arch/arm64/net/bpf_jit_comp.c:1333:28negation of -2147483648 cannot be represented in type 's32' (aka 'int'):
Silent these warnings by casting imm to u32 first.
[...]
Here is the summary with links:
- [bpf-next] bpf: arm64: Silent "UBSAN: negation-overflow" warning
https://git.kernel.org/bpf/bpf-next/c/239860828f86
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html