Skip to contentSkip to conversation

bpf: arm64: Silent "UBSAN: negation-overflow" warning

PATCH Applied Song Liu 4 participants 5 messages Feb 18, 2025 – Feb 23, 2025

Subsystems (3)
lore
Share & tools
lore

Song Liu

author reviewer
#0 lore raw

PATCH 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.

Fixes fd868f148189 ("bpf, arm64: Optimize ADD,SUB,JMP BPF_K using arm64 add/sub immediates")
---
 arch/arm64/net/bpf_jit_comp.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/net/bpf_jit_comp.c +3 -3
index 8446848edddb..7409c8acbde3 100644
@@ -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
reviewer
#1 lore raw
Quoted 8 lines · Song Liu

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.

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
---
  arch/arm64/net/bpf_jit_comp.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/net/bpf_jit_comp.c +3 -3
index 8446848edddb..7409c8acbde3 100644
@@ -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);
author reviewer
#2 lore raw
Quoted 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:28

negation 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
---
  arch/arm64/net/bpf_jit_comp.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/net/bpf_jit_comp.c +3 -3
index 8446848edddb..7409c8acbde3 100644
@@ -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);
Tested-by on v1 1/1
#3 lore raw
In reply to Song Liu
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:28

negation 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")
#4 lore raw
In reply to Song Liu
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:28

negation 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