Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf: arm64: Silence "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'):

Silence these warnings by casting imm to u32 first.

Reported-by: Breno Leitao <leitao@debian.org>
Signed-off-by: Song Liu <song@kernel.org>
Tested-by: Breno Leitao <leitao@debian.org>
Link: https://lore.kernel.org/r/20250218080240.2431257-1-song@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Song Liu and committed by
Alexei Starovoitov
23986082 201b62cc

+3 -3
+3 -3
arch/arm64/net/bpf_jit_comp.c
··· 272 272 { 273 273 if (is_addsub_imm(imm)) { 274 274 emit(A64_ADD_I(is64, dst, src, imm), ctx); 275 - } else if (is_addsub_imm(-imm)) { 275 + } else if (is_addsub_imm(-(u32)imm)) { 276 276 emit(A64_SUB_I(is64, dst, src, -imm), ctx); 277 277 } else { 278 278 emit_a64_mov_i(is64, tmp, imm, ctx); ··· 1159 1159 case BPF_ALU64 | BPF_SUB | BPF_K: 1160 1160 if (is_addsub_imm(imm)) { 1161 1161 emit(A64_SUB_I(is64, dst, dst, imm), ctx); 1162 - } else if (is_addsub_imm(-imm)) { 1162 + } else if (is_addsub_imm(-(u32)imm)) { 1163 1163 emit(A64_ADD_I(is64, dst, dst, -imm), ctx); 1164 1164 } else { 1165 1165 emit_a64_mov_i(is64, tmp, imm, ctx); ··· 1330 1330 case BPF_JMP32 | BPF_JSLE | BPF_K: 1331 1331 if (is_addsub_imm(imm)) { 1332 1332 emit(A64_CMP_I(is64, dst, imm), ctx); 1333 - } else if (is_addsub_imm(-imm)) { 1333 + } else if (is_addsub_imm(-(u32)imm)) { 1334 1334 emit(A64_CMN_I(is64, dst, -imm), ctx); 1335 1335 } else { 1336 1336 emit_a64_mov_i(is64, tmp, imm, ctx);