Skip to content

Commit 7386898

Browse files
jemarchAlexei Starovoitov
authored andcommitted
bpf: disable strict aliasing in test_global_func9.c
The BPF selftest test_global_func9.c performs type punning and breaks srict-aliasing rules. In particular, given: int global_func9(struct __sk_buff *skb) { int result = 0; [...] { const struct C c = {.x = skb->len, .y = skb->family }; result |= foo((const struct S *)&c); } } When building with strict-aliasing enabled (the default) the initialization of `c' gets optimized away in its entirely: [... no initialization of `c' ...] r1 = r10 r1 += -40 call foo w0 |= w6 Since GCC knows that `foo' accesses s->x, we get a "maybe uninitialized" warning. On the other hand, when strict-aliasing is disabled GCC only optimizes away the store to `.y': r1 = *(u32 *) (r6+0) *(u32 *) (r10+-40) = r1 ; This is .x = skb->len in `c' r1 = r10 r1 += -40 call foo w0 |= w6 In this case the warning is not emitted, because s-> is initialized. This patch disables strict aliasing in this test when building with GCC. clang seems to not optimize this particular code even when strict aliasing is enabled. Tested in bpf-next master. Signed-off-by: Jose E. Marchesi <[email protected]> Cc: [email protected] Cc: [email protected] Cc: Yonghong Song <[email protected]> Cc: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent a3c1c95 commit 7386898

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

tools/testing/selftests/bpf/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ progs/syscall.c-CFLAGS := -fno-strict-aliasing
5353
progs/test_pkt_md_access.c-CFLAGS := -fno-strict-aliasing
5454
progs/test_sk_lookup.c-CFLAGS := -fno-strict-aliasing
5555
progs/timer_crash.c-CFLAGS := -fno-strict-aliasing
56+
progs/test_global_func9.c-CFLAGS := -fno-strict-aliasing
5657

5758
ifneq ($(LLVM),)
5859
# Silence some warnings when compiled with clang

0 commit comments

Comments
 (0)