From: Yonghong Song <yonghong.song@linux.dev>
To: "Jose E. Marchesi" <jose.marchesi@oracle.com>, bpf@vger.kernel.org
Cc: Eduard Zingerman <eddyz87@gmail.com>,
david.faust@oracle.com, cupertino.miranda@oracle.com,
Yonghong Song <yhs@meta.com>
Subject: Re: BPF selftests and strict aliasing
Date: Sat, 27 Jan 2024 18:05:15 -0800 [thread overview]
Message-ID: <b1906297-d784-479b-b2f3-07ab84ae99c1@linux.dev> (raw)
In-Reply-To: <87plxmsg37.fsf@oracle.com>
On 1/27/24 11:59 AM, Jose E. Marchesi wrote:
> Hello.
> The following BPF selftests perform type-punning:
>
> progs/bind4_prog.c
> 136 | user_ip4 |= ((volatile __u16 *)&ctx->user_ip4)[0] << 0;
>
> progs/bind6_prog.c
> 149 | user_ip6 |= ((volatile __u16 *)&ctx->user_ip6[i])[0] << 0;
>
> progs/dynptr_fail.c
> 549 | val = *(int *)&ptr;
>
> progs/linked_list_fail.c
> 318 | return *(int *)&f->head;
> 329 | *(int *)&f->head = 0;
> 338 | f = bpf_obj_new(typeof(*f));
> 341 | return *(int *)&f->node2;
> 349 | f = bpf_obj_new(typeof(*f));
> 352 | *(int *)&f->node2 = 0;
>
> progs/map_kptr_fail.c
> 34 | *(u32 *)&v->unref_ptr = 0;
>
> progs/syscall.c
> 172 | attr->map_id = ((struct bpf_map *)&outer_array_map)->id;
>
> progs/test_pkt_md_access.c
> 13 | TYPE tmp = *(volatile TYPE *)&skb->FIELD; \
>
> progs/test_sk_lookup.c
> 31 | (((__u16 *)&(value))[LSE_INDEX((index), sizeof(value) / 2)])
> 427 | val_u32 = *(__u32 *)&ctx->remote_port;
>
> progs/timer_crash.c
> 38 | *(void **)&value = (void *)0xdeadcaf3;
>
> This results in GCC warnings with -Wall but violating strict aliasing
> may also result in the compiler incorrectly optimizing something.
>
> There are some alternatives to deal with this:
>
> a) To rewrite the tests to conform to strict aliasing rules.
>
> b) To build these tests using -fno-strict-aliasing to make sure the
> compiler will not rely on strict aliasing while optimizing.
>
> c) To add pragmas to these test files to avoid the warning:
> _Pragma("GCC diagnostic ignored \"-Wstrict-aliasing\"")
>
> I think b) is probably the best way to go, because it will avoid the
> warnings, will void potential problems with optimizations triggered by
> strict aliasing, and will not require to rewrite the tests.
I tried with latest clang with -fstrict-aliasing:
[~/work/bpf-next/tools/testing/selftests/bpf (master)]$ cat run.sh
clang -g -Wall -Werror -D__TARGET_ARCH_x86 -mlittle-endian -I/home/yhs/work/bpf-next/tools/testing/selftests/bpf/tools/include \
-I/home/yhs/work/bpf-next/tools/testing/selftests/bpf -I/home/yhs/work/bpf-next/tools/include/uapi
-I/home/yhs/work/bpf-next/tools/testing/selftests/usr/include
-idirafter /home/yhs/work/llvm-project/llvm/build.19/install/lib/clang/19/include
-idirafter /usr/local/include -idirafter /usr/include -Wno-compare-distinct-pointer-types
-DENABLE_ATOMICS_TESTS -O2 -fstrict-aliasing --target=bpf -c progs/bind4_prog.c -mcpu=v3
-o /home/yhs/work/bpf-next/tools/testing/selftests/bpf/bind4_prog.bpf.o
[~/work/bpf-next/tools/testing/selftests/bpf (master)]$ ./run.sh
[~/work/bpf-next/tools/testing/selftests/bpf (master)]$
I does not have compilation failure. I am wondering why -fstrict-aliasing won't have warning in clang side
but have warning in gcc side.
Your suggestion 'b' seems okay or we could even add -fno-strict-aliasing into common compilation flags,
but I would like to understand more about -fstrict-aliasing difference between gcc and clang.
>
> Provided [1] gets applied, I can prepare a patch that adds the following
> to selftests/bpf/Makefile:
>
> progs/bin4_prog.c-CFLAGS := -fno-strict-aliasing
> progs/bind6_prog.c-CFLAGS := -fno-strict-aliasing
> progs/dynptr_fail.cw-CFLAGS := -fno-strict-aliasing
> progs/linked_list_fail.c-CFLAGS := -fno-strict-aliasing
> progs/map_kptr_fail.c-CFLAGS := -fno-strict-aliasing
> 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
>
> [1] https://lore.kernel.org/bpf/20240127100702.21549-1-jose.marchesi@oracle.com/T/#u
>
next prev parent reply other threads:[~2024-01-28 2:05 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-27 19:59 BPF selftests and strict aliasing Jose E. Marchesi
2024-01-28 2:05 ` Yonghong Song [this message]
2024-01-28 12:25 ` Jose E. Marchesi
2024-01-29 5:33 ` Yonghong Song
2024-01-29 16:15 ` Eduard Zingerman
2024-01-29 17:05 ` Jose E. Marchesi
2024-01-29 18:16 ` Yonghong Song
2024-01-29 18:43 ` Jose E. Marchesi
2024-01-29 21:48 ` Yonghong Song
2024-01-29 21:59 ` Jose E. Marchesi
2024-01-29 18:12 ` Yonghong Song
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b1906297-d784-479b-b2f3-07ab84ae99c1@linux.dev \
--to=yonghong.song@linux.dev \
--cc=bpf@vger.kernel.org \
--cc=cupertino.miranda@oracle.com \
--cc=david.faust@oracle.com \
--cc=eddyz87@gmail.com \
--cc=jose.marchesi@oracle.com \
--cc=yhs@meta.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.