* [PATCH bpf-next] bpf: avoid gcc overflow warning in test_xdp_vlan.c
@ 2024-05-08 19:35 David Faust
2024-05-13 0:20 ` patchwork-bot+netdevbpf
0 siblings, 1 reply; 2+ messages in thread
From: David Faust @ 2024-05-08 19:35 UTC (permalink / raw)
To: bpf; +Cc: jose.marchesi, cupertino.miranda, eddyz87, yonghong.song
This patch fixes an integer overflow warning raised by GCC in
xdp_prognum1 of progs/test_xdp_vlan.c:
GCC-BPF [test_maps] test_xdp_vlan.bpf.o
progs/test_xdp_vlan.c: In function 'xdp_prognum1':
progs/test_xdp_vlan.c:163:25: error: integer overflow in expression
'(short int)(((__builtin_constant_p((int)vlan_hdr->h_vlan_TCI)) != 0
? (int)(short unsigned int)((short int)((int)vlan_hdr->h_vlan_TCI
<< 8 >> 8) << 8 | (short int)((int)vlan_hdr->h_vlan_TCI << 0 >> 8
<< 0)) & 61440 : (int)__builtin_bswap16(vlan_hdr->h_vlan_TCI)
& 61440) << 8 >> 8) << 8' of type 'short int' results in '0' [-Werror=overflow]
163 | bpf_htons((bpf_ntohs(vlan_hdr->h_vlan_TCI) & 0xf000)
| ^~~~~~~~~
The problem lies with the expansion of the bpf_htons macro and the
expression passed into it. The bpf_htons macro (and similarly the
bpf_ntohs macro) expand to a ternary operation using either
__builtin_bswap16 or ___bpf_swab16 to swap the bytes, depending on
whether the expression is constant.
For an expression, with 'value' as a u16, like:
bpf_htons (value & 0xf000)
The entire (value & 0xf000) is 'x' in the expansion of ___bpf_swab16
and we get as one part of the expanded swab16:
((__u16)(value & 0xf000) << 8 >> 8 << 8
This will always evaluate to 0, which is intentional since this
subexpression deals with the byte guaranteed to be 0 by the mask.
However, GCC warns because the precise reason this always evaluates to 0
is an overflow. Specifically, the plain 0xf000 in the expression is a
signed 32-bit integer, which causes 'value' to also be promoted to a
signed 32-bit integer, and the combination of the 8-bit left shift and
down-cast back to __u16 results in a signed overflow (really a 'warning:
overflow in conversion from int to __u16' which is propegated up through
the rest of the expression leading to the ultimate overflow warning
above), which is a valid warning despite being the intended result of
this code.
Clang does not warn on this case, likely because it performs constant
folding later in the compilation process relative to GCC. It seems that
by the time clang does constant folding for this expression, the side of
the ternary with this overflow has already been discarded.
Fortunately, this warning is easily silenced by simply making the 0xf000
mask explicitly unsigned. This has no impact on the result.
Signed-off-by: David Faust <david.faust@oracle.com>
Cc: jose.marchesi@oracle.com
Cc: cupertino.miranda@oracle.com
Cc: Eduard Zingerman <eddyz87@gmail.com>
Cc: Yonghong Song <yonghong.song@linux.dev>
---
tools/testing/selftests/bpf/progs/test_xdp_vlan.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/testing/selftests/bpf/progs/test_xdp_vlan.c b/tools/testing/selftests/bpf/progs/test_xdp_vlan.c
index f3ec8086482d..a7588302268d 100644
--- a/tools/testing/selftests/bpf/progs/test_xdp_vlan.c
+++ b/tools/testing/selftests/bpf/progs/test_xdp_vlan.c
@@ -160,7 +160,7 @@ int xdp_prognum1(struct xdp_md *ctx)
/* Modifying VLAN, preserve top 4 bits */
vlan_hdr->h_vlan_TCI =
- bpf_htons((bpf_ntohs(vlan_hdr->h_vlan_TCI) & 0xf000)
+ bpf_htons((bpf_ntohs(vlan_hdr->h_vlan_TCI) & 0xf000U)
| TO_VLAN);
}
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH bpf-next] bpf: avoid gcc overflow warning in test_xdp_vlan.c
2024-05-08 19:35 [PATCH bpf-next] bpf: avoid gcc overflow warning in test_xdp_vlan.c David Faust
@ 2024-05-13 0:20 ` patchwork-bot+netdevbpf
0 siblings, 0 replies; 2+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-05-13 0:20 UTC (permalink / raw)
To: David Faust; +Cc: bpf, jose.marchesi, cupertino.miranda, eddyz87, yonghong.song
Hello:
This patch was applied to bpf/bpf-next.git (master)
by Alexei Starovoitov <ast@kernel.org>:
On Wed, 8 May 2024 12:35:12 -0700 you wrote:
> This patch fixes an integer overflow warning raised by GCC in
> xdp_prognum1 of progs/test_xdp_vlan.c:
>
> GCC-BPF [test_maps] test_xdp_vlan.bpf.o
> progs/test_xdp_vlan.c: In function 'xdp_prognum1':
> progs/test_xdp_vlan.c:163:25: error: integer overflow in expression
> '(short int)(((__builtin_constant_p((int)vlan_hdr->h_vlan_TCI)) != 0
> ? (int)(short unsigned int)((short int)((int)vlan_hdr->h_vlan_TCI
> << 8 >> 8) << 8 | (short int)((int)vlan_hdr->h_vlan_TCI << 0 >> 8
> << 0)) & 61440 : (int)__builtin_bswap16(vlan_hdr->h_vlan_TCI)
> & 61440) << 8 >> 8) << 8' of type 'short int' results in '0' [-Werror=overflow]
> 163 | bpf_htons((bpf_ntohs(vlan_hdr->h_vlan_TCI) & 0xf000)
> | ^~~~~~~~~
>
> [...]
Here is the summary with links:
- [bpf-next] bpf: avoid gcc overflow warning in test_xdp_vlan.c
https://git.kernel.org/bpf/bpf-next/c/792a04bed41c
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-05-13 0:20 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-08 19:35 [PATCH bpf-next] bpf: avoid gcc overflow warning in test_xdp_vlan.c David Faust
2024-05-13 0:20 ` patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox