tech-toolchain archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Compiler bug or violation of strict aliasing rule?



For aarch64eb, GCC 9 and 10 compile this "if" block of
tests/net/net/t_tcp.c:

https://nxr.netbsd.org/xref/src/tests/net/net/t_tcp.c#141

   141          if (sfamily == AF_INET6 && cfamily == AF_INET) {
   142                  ss = bs;
   143                  sin6 = (void *)&ss;
   144                  sin = (void *)&bs;
   145                  addrlen = sizeof(*sin);
   146  #ifdef BSD4_4
   147                  sin->sin_len = sizeof(*sin);
   148  #endif
   149                  sin->sin_family = AF_INET;
   150                  sin->sin_port = sin6->sin6_port;
   151                  sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
   152          }

into a code, where fetch for "sin6->sin6_port" is preceding to
structure copy "ss = bs".

This is apparently not what the author of this code intended.

However, I'm not sure whether this is a compiler bug, or a kind of
violation of strict aliasing rule.

Note that the emitted code is "fixed" if (1) -fno-strict-aliasing
option is added, or (2) the attached patch is applied.

Thoughts?

Thanks,
rin
----
Index: t_tcp.c
===================================================================
RCS file: /home/netbsd/src/tests/net/net/t_tcp.c,v
retrieving revision 1.11
diff -p -u -r1.11 t_tcp.c
--- t_tcp.c	26 Oct 2019 23:08:27 -0000	1.11
+++ t_tcp.c	4 Nov 2021 09:35:30 -0000
@@ -139,15 +139,14 @@ accept_test(sa_family_t sfamily, sa_fami
 		FAIL("socket");
if (sfamily == AF_INET6 && cfamily == AF_INET) {
-		ss = bs;
-		sin6 = (void *)&ss;
+		in_port_t port = ((struct sockaddr_in6 *)&bs)->sin6_port;
 		sin = (void *)&bs;
 		addrlen = sizeof(*sin);
 #ifdef BSD4_4
 		sin->sin_len = sizeof(*sin);
 #endif
 		sin->sin_family = AF_INET;
-		sin->sin_port = sin6->sin6_port;
+		sin->sin_port = port;
 		sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
 	}


Home | Main Index | Thread Index | Old Index