* [PATCH bpf-next] selftests/bpf: fix -Wstrict-aliasing in test_sockopt_sk.c
@ 2019-06-28 1:12 Stanislav Fomichev
2019-06-28 20:25 ` Song Liu
2019-06-28 23:33 ` Daniel Borkmann
0 siblings, 2 replies; 3+ messages in thread
From: Stanislav Fomichev @ 2019-06-28 1:12 UTC (permalink / raw)
To: netdev, bpf; +Cc: davem, ast, daniel, Stanislav Fomichev
Let's use union with u8[4] and u32 members for sockopt buffer,
that should fix any possible aliasing issues.
test_sockopt_sk.c: In function ‘getsetsockopt’:
test_sockopt_sk.c:115:2: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
if (*(__u32 *)buf != 0x55AA*2) {
^~
test_sockopt_sk.c:116:3: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
log_err("Unexpected getsockopt(SO_SNDBUF) 0x%x != 0x55AA*2",
^~~~~~~
Fixes: 8a027dc0d8f5 ("selftests/bpf: add sockopt test that exercises sk helpers")
Reported-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Stanislav Fomichev <sdf@google.com>
---
tools/testing/selftests/bpf/test_sockopt_sk.c | 51 +++++++++----------
1 file changed, 24 insertions(+), 27 deletions(-)
diff --git a/tools/testing/selftests/bpf/test_sockopt_sk.c b/tools/testing/selftests/bpf/test_sockopt_sk.c
index 12e79ed075ce..036b652e5ca9 100644
--- a/tools/testing/selftests/bpf/test_sockopt_sk.c
+++ b/tools/testing/selftests/bpf/test_sockopt_sk.c
@@ -22,7 +22,10 @@
static int getsetsockopt(void)
{
int fd, err;
- char buf[4] = {};
+ union {
+ char u8[4];
+ __u32 u32;
+ } buf = {};
socklen_t optlen;
fd = socket(AF_INET, SOCK_STREAM, 0);
@@ -33,31 +36,31 @@ static int getsetsockopt(void)
/* IP_TOS - BPF bypass */
- buf[0] = 0x08;
- err = setsockopt(fd, SOL_IP, IP_TOS, buf, 1);
+ buf.u8[0] = 0x08;
+ err = setsockopt(fd, SOL_IP, IP_TOS, &buf, 1);
if (err) {
log_err("Failed to call setsockopt(IP_TOS)");
goto err;
}
- buf[0] = 0x00;
+ buf.u8[0] = 0x00;
optlen = 1;
- err = getsockopt(fd, SOL_IP, IP_TOS, buf, &optlen);
+ err = getsockopt(fd, SOL_IP, IP_TOS, &buf, &optlen);
if (err) {
log_err("Failed to call getsockopt(IP_TOS)");
goto err;
}
- if (buf[0] != 0x08) {
+ if (buf.u8[0] != 0x08) {
log_err("Unexpected getsockopt(IP_TOS) buf[0] 0x%02x != 0x08",
- buf[0]);
+ buf.u8[0]);
goto err;
}
/* IP_TTL - EPERM */
- buf[0] = 1;
- err = setsockopt(fd, SOL_IP, IP_TTL, buf, 1);
+ buf.u8[0] = 1;
+ err = setsockopt(fd, SOL_IP, IP_TTL, &buf, 1);
if (!err || errno != EPERM) {
log_err("Unexpected success from setsockopt(IP_TTL)");
goto err;
@@ -65,16 +68,16 @@ static int getsetsockopt(void)
/* SOL_CUSTOM - handled by BPF */
- buf[0] = 0x01;
- err = setsockopt(fd, SOL_CUSTOM, 0, buf, 1);
+ buf.u8[0] = 0x01;
+ err = setsockopt(fd, SOL_CUSTOM, 0, &buf, 1);
if (err) {
log_err("Failed to call setsockopt");
goto err;
}
- buf[0] = 0x00;
+ buf.u32 = 0x00;
optlen = 4;
- err = getsockopt(fd, SOL_CUSTOM, 0, buf, &optlen);
+ err = getsockopt(fd, SOL_CUSTOM, 0, &buf, &optlen);
if (err) {
log_err("Failed to call getsockopt");
goto err;
@@ -84,37 +87,31 @@ static int getsetsockopt(void)
log_err("Unexpected optlen %d != 1", optlen);
goto err;
}
- if (buf[0] != 0x01) {
- log_err("Unexpected buf[0] 0x%02x != 0x01", buf[0]);
+ if (buf.u8[0] != 0x01) {
+ log_err("Unexpected buf[0] 0x%02x != 0x01", buf.u8[0]);
goto err;
}
/* SO_SNDBUF is overwritten */
- buf[0] = 0x01;
- buf[1] = 0x01;
- buf[2] = 0x01;
- buf[3] = 0x01;
- err = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, buf, 4);
+ buf.u32 = 0x01010101;
+ err = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &buf, 4);
if (err) {
log_err("Failed to call setsockopt(SO_SNDBUF)");
goto err;
}
- buf[0] = 0x00;
- buf[1] = 0x00;
- buf[2] = 0x00;
- buf[3] = 0x00;
+ buf.u32 = 0x00;
optlen = 4;
- err = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, buf, &optlen);
+ err = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &buf, &optlen);
if (err) {
log_err("Failed to call getsockopt(SO_SNDBUF)");
goto err;
}
- if (*(__u32 *)buf != 0x55AA*2) {
+ if (buf.u32 != 0x55AA*2) {
log_err("Unexpected getsockopt(SO_SNDBUF) 0x%x != 0x55AA*2",
- *(__u32 *)buf);
+ buf.u32);
goto err;
}
--
2.22.0.410.gd8fdbe21b5-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH bpf-next] selftests/bpf: fix -Wstrict-aliasing in test_sockopt_sk.c
2019-06-28 1:12 [PATCH bpf-next] selftests/bpf: fix -Wstrict-aliasing in test_sockopt_sk.c Stanislav Fomichev
@ 2019-06-28 20:25 ` Song Liu
2019-06-28 23:33 ` Daniel Borkmann
1 sibling, 0 replies; 3+ messages in thread
From: Song Liu @ 2019-06-28 20:25 UTC (permalink / raw)
To: Stanislav Fomichev
Cc: Networking, bpf, David S . Miller, Alexei Starovoitov, Daniel Borkmann
On Thu, Jun 27, 2019 at 6:14 PM Stanislav Fomichev <sdf@google.com> wrote:
>
> Let's use union with u8[4] and u32 members for sockopt buffer,
> that should fix any possible aliasing issues.
>
> test_sockopt_sk.c: In function ‘getsetsockopt’:
> test_sockopt_sk.c:115:2: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
> if (*(__u32 *)buf != 0x55AA*2) {
> ^~
> test_sockopt_sk.c:116:3: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
> log_err("Unexpected getsockopt(SO_SNDBUF) 0x%x != 0x55AA*2",
> ^~~~~~~
>
> Fixes: 8a027dc0d8f5 ("selftests/bpf: add sockopt test that exercises sk helpers")
> Reported-by: Alexei Starovoitov <ast@kernel.org>
> Signed-off-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Song Liu <songliubraving@fb.com>
> ---
> tools/testing/selftests/bpf/test_sockopt_sk.c | 51 +++++++++----------
> 1 file changed, 24 insertions(+), 27 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/test_sockopt_sk.c b/tools/testing/selftests/bpf/test_sockopt_sk.c
> index 12e79ed075ce..036b652e5ca9 100644
> --- a/tools/testing/selftests/bpf/test_sockopt_sk.c
> +++ b/tools/testing/selftests/bpf/test_sockopt_sk.c
> @@ -22,7 +22,10 @@
> static int getsetsockopt(void)
> {
> int fd, err;
> - char buf[4] = {};
> + union {
> + char u8[4];
> + __u32 u32;
> + } buf = {};
> socklen_t optlen;
>
> fd = socket(AF_INET, SOCK_STREAM, 0);
> @@ -33,31 +36,31 @@ static int getsetsockopt(void)
>
> /* IP_TOS - BPF bypass */
>
> - buf[0] = 0x08;
> - err = setsockopt(fd, SOL_IP, IP_TOS, buf, 1);
> + buf.u8[0] = 0x08;
> + err = setsockopt(fd, SOL_IP, IP_TOS, &buf, 1);
> if (err) {
> log_err("Failed to call setsockopt(IP_TOS)");
> goto err;
> }
>
> - buf[0] = 0x00;
> + buf.u8[0] = 0x00;
> optlen = 1;
> - err = getsockopt(fd, SOL_IP, IP_TOS, buf, &optlen);
> + err = getsockopt(fd, SOL_IP, IP_TOS, &buf, &optlen);
> if (err) {
> log_err("Failed to call getsockopt(IP_TOS)");
> goto err;
> }
>
> - if (buf[0] != 0x08) {
> + if (buf.u8[0] != 0x08) {
> log_err("Unexpected getsockopt(IP_TOS) buf[0] 0x%02x != 0x08",
> - buf[0]);
> + buf.u8[0]);
> goto err;
> }
>
> /* IP_TTL - EPERM */
>
> - buf[0] = 1;
> - err = setsockopt(fd, SOL_IP, IP_TTL, buf, 1);
> + buf.u8[0] = 1;
> + err = setsockopt(fd, SOL_IP, IP_TTL, &buf, 1);
> if (!err || errno != EPERM) {
> log_err("Unexpected success from setsockopt(IP_TTL)");
> goto err;
> @@ -65,16 +68,16 @@ static int getsetsockopt(void)
>
> /* SOL_CUSTOM - handled by BPF */
>
> - buf[0] = 0x01;
> - err = setsockopt(fd, SOL_CUSTOM, 0, buf, 1);
> + buf.u8[0] = 0x01;
> + err = setsockopt(fd, SOL_CUSTOM, 0, &buf, 1);
> if (err) {
> log_err("Failed to call setsockopt");
> goto err;
> }
>
> - buf[0] = 0x00;
> + buf.u32 = 0x00;
> optlen = 4;
> - err = getsockopt(fd, SOL_CUSTOM, 0, buf, &optlen);
> + err = getsockopt(fd, SOL_CUSTOM, 0, &buf, &optlen);
> if (err) {
> log_err("Failed to call getsockopt");
> goto err;
> @@ -84,37 +87,31 @@ static int getsetsockopt(void)
> log_err("Unexpected optlen %d != 1", optlen);
> goto err;
> }
> - if (buf[0] != 0x01) {
> - log_err("Unexpected buf[0] 0x%02x != 0x01", buf[0]);
> + if (buf.u8[0] != 0x01) {
> + log_err("Unexpected buf[0] 0x%02x != 0x01", buf.u8[0]);
> goto err;
> }
>
> /* SO_SNDBUF is overwritten */
>
> - buf[0] = 0x01;
> - buf[1] = 0x01;
> - buf[2] = 0x01;
> - buf[3] = 0x01;
> - err = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, buf, 4);
> + buf.u32 = 0x01010101;
> + err = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &buf, 4);
> if (err) {
> log_err("Failed to call setsockopt(SO_SNDBUF)");
> goto err;
> }
>
> - buf[0] = 0x00;
> - buf[1] = 0x00;
> - buf[2] = 0x00;
> - buf[3] = 0x00;
> + buf.u32 = 0x00;
> optlen = 4;
> - err = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, buf, &optlen);
> + err = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &buf, &optlen);
> if (err) {
> log_err("Failed to call getsockopt(SO_SNDBUF)");
> goto err;
> }
>
> - if (*(__u32 *)buf != 0x55AA*2) {
> + if (buf.u32 != 0x55AA*2) {
> log_err("Unexpected getsockopt(SO_SNDBUF) 0x%x != 0x55AA*2",
> - *(__u32 *)buf);
> + buf.u32);
> goto err;
> }
>
> --
> 2.22.0.410.gd8fdbe21b5-goog
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH bpf-next] selftests/bpf: fix -Wstrict-aliasing in test_sockopt_sk.c
2019-06-28 1:12 [PATCH bpf-next] selftests/bpf: fix -Wstrict-aliasing in test_sockopt_sk.c Stanislav Fomichev
2019-06-28 20:25 ` Song Liu
@ 2019-06-28 23:33 ` Daniel Borkmann
1 sibling, 0 replies; 3+ messages in thread
From: Daniel Borkmann @ 2019-06-28 23:33 UTC (permalink / raw)
To: Stanislav Fomichev, netdev, bpf; +Cc: davem, ast
On 06/28/2019 03:12 AM, Stanislav Fomichev wrote:
> Let's use union with u8[4] and u32 members for sockopt buffer,
> that should fix any possible aliasing issues.
>
> test_sockopt_sk.c: In function ‘getsetsockopt’:
> test_sockopt_sk.c:115:2: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
> if (*(__u32 *)buf != 0x55AA*2) {
> ^~
> test_sockopt_sk.c:116:3: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
> log_err("Unexpected getsockopt(SO_SNDBUF) 0x%x != 0x55AA*2",
> ^~~~~~~
>
> Fixes: 8a027dc0d8f5 ("selftests/bpf: add sockopt test that exercises sk helpers")
> Reported-by: Alexei Starovoitov <ast@kernel.org>
> Signed-off-by: Stanislav Fomichev <sdf@google.com>
Applied, thanks!
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2019-06-28 23:33 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-28 1:12 [PATCH bpf-next] selftests/bpf: fix -Wstrict-aliasing in test_sockopt_sk.c Stanislav Fomichev
2019-06-28 20:25 ` Song Liu
2019-06-28 23:33 ` Daniel Borkmann
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.