BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next] bpf: disable strict aliasing in test_global_func9.c
@ 2024-05-11 21:22 Jose E. Marchesi
  2024-05-13  0:40 ` patchwork-bot+netdevbpf
  0 siblings, 1 reply; 2+ messages in thread
From: Jose E. Marchesi @ 2024-05-11 21:22 UTC (permalink / raw)
  To: bpf
  Cc: Jose E . Marchesi, david.faust, cupertino.miranda, Yonghong Song,
	Eduard Zingerman

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>
---
 tools/testing/selftests/bpf/Makefile | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index 135023a357b3..979838c5a495 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -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
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH bpf-next] bpf: disable strict aliasing in test_global_func9.c
  2024-05-11 21:22 [PATCH bpf-next] bpf: disable strict aliasing in test_global_func9.c Jose E. Marchesi
@ 2024-05-13  0:40 ` patchwork-bot+netdevbpf
  0 siblings, 0 replies; 2+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-05-13  0:40 UTC (permalink / raw)
  To: Jose E. Marchesi
  Cc: bpf, david.faust, cupertino.miranda, yonghong.song, eddyz87

Hello:

This patch was applied to bpf/bpf-next.git (master)
by Alexei Starovoitov <ast@kernel.org>:

On Sat, 11 May 2024 23:22:13 +0200 you wrote:
> 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;
> 
> [...]

Here is the summary with links:
  - [bpf-next] bpf: disable strict aliasing in test_global_func9.c
    https://git.kernel.org/bpf/bpf-next/c/73868988c90d

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:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-11 21:22 [PATCH bpf-next] bpf: disable strict aliasing in test_global_func9.c Jose E. Marchesi
2024-05-13  0:40 ` 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