bpf: disable strict aliasing in test_global_func9.c
authorJose E. Marchesi <jose.marchesi@oracle.com>
Sat, 11 May 2024 21:22:13 +0000 (23:22 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Mon, 13 May 2024 00:30:15 +0000 (17:30 -0700)
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 <jose.marchesi@oracle.com>
Cc: david.faust@oracle.com
Cc: cupertino.miranda@oracle.com
Cc: Yonghong Song <yonghong.song@linux.dev>
Cc: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20240511212213.23418-1-jose.marchesi@oracle.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/Makefile

index ed381b0197fe4a36aaa2554e7097d29155b9e000..e0b3887b3d2df139fb2d18c161db2a04e94ca45d 100644 (file)
@@ -53,6 +53,7 @@ progs/syscall.c-CFLAGS := -fno-strict-aliasing
 progs/test_pkt_md_access.c-CFLAGS := -fno-strict-aliasing
 progs/test_sk_lookup.c-CFLAGS := -fno-strict-aliasing
 progs/timer_crash.c-CFLAGS := -fno-strict-aliasing
+progs/test_global_func9.c-CFLAGS := -fno-strict-aliasing
 
 ifneq ($(LLVM),)
 # Silence some warnings when compiled with clang