From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 204565A0271 for ; Sun, 7 Jan 2024 06:36:11 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1704605765; bh=r8vpFQgVdDXpDM9T5w3CqyZ7DW5UJ/snZZeuihAkdBE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=WkNmgrDqoT5+J8+PzfoDkbSXD3HMnEZBo3wz6VJI9N5fnhCCCMcWPkzvo2D7hfqLJ tvW8DhS70WgPJVf6f4eMGpXmbZTvufxWgDALSp4iNkGN2ATuWIRW4c+tb5xJYIdojN 8tH1zxSykI13JDhODQC1yfZFpxflVOhTGW6N198zEUToB2GKq9NFvVIB+ivau2KVmx kWV/Tq6xaCpP+KiLe2Dsnmx/a+40P9bEIdZBMqozhqqsYzQU8NkbzfNpbKq9OAw6rQ 5GV6VlL3TObALS9tbBXOOBd+boXcsl/rrLy9Nx/uOn9MVdinq2Rsr9X19ESUAog8Pr vHTAJGhuuAEAg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4T75X51dPmz4wd5; Sun, 7 Jan 2024 16:36:05 +1100 (AEDT) Date: Sun, 7 Jan 2024 16:35:56 +1100 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH 1/8] tcp: Fix address type for tcp_sock_init_af() Message-ID: References: <20231207143140.1851378-1-david@gibson.dropbear.id.au> <20231207143140.1851378-2-david@gibson.dropbear.id.au> <20231227212506.75eb41c7@elisabeth> <20231228111119.48afa9a5@elisabeth> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="Stl2BS5hP6iUtO17" Content-Disposition: inline In-Reply-To: <20231228111119.48afa9a5@elisabeth> Message-ID-Hash: YTEMCISNCBXLRIYKTWPGE4HYFRHRVUWB X-Message-ID-Hash: YTEMCISNCBXLRIYKTWPGE4HYFRHRVUWB X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --Stl2BS5hP6iUtO17 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Dec 28, 2023 at 11:11:19AM +0100, Stefano Brivio wrote: > On Thu, 28 Dec 2023 13:42:25 +1100 > David Gibson wrote: >=20 > > On Wed, Dec 27, 2023 at 09:25:06PM +0100, Stefano Brivio wrote: > > > On Fri, 8 Dec 2023 01:31:33 +1100 > > > David Gibson wrote: > > > =20 > > > > This takes a struct in_addr * (i.e. an IPv4 address), although it's > > > > explicitly supposed to handle IPv6 as well. Both its caller and so= ck_l4() > > > > which it calls use a void * for the address, which can be either an= in_addr > > > > or an in6_addr. > > > >=20 > > > > We get away with this, because we don't do anything with the pointe= r other > > > > than transfer it from the caller to sock_l4(), but it's misleading.= And > > > > quite possibly technically UB, because C is like that. > > > >=20 > > > > Signed-off-by: David Gibson > > > > --- > > > > tcp.c | 2 +- > > > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > >=20 > > > > diff --git a/tcp.c b/tcp.c > > > > index f506cfd..bda95b2 100644 > > > > --- a/tcp.c > > > > +++ b/tcp.c > > > > @@ -2905,7 +2905,7 @@ void tcp_sock_handler(struct ctx *c, union ep= oll_ref ref, uint32_t events) > > > > * Return: fd for the new listening socket, negative error code on= failure > > > > */ > > > > static int tcp_sock_init_af(const struct ctx *c, int af, in_port_t= port, > > > > - const struct in_addr *addr, const char *ifname) > > > > + const void *addr, const char *ifname) =20 > > >=20 > > > This is obviously correct. > > >=20 > > > However, after a lot of thinking: (gcc) optimisations based on > > > Type-Based Alias Analysis, which we don't disable on this path, could, > > > now, happily defer filling 'addr' with inet_pton() in conf_ports() to= a > > > point *after* the tcp_sock_init() call. =20 > >=20 > > Hrm... possibly. The fact that the addr variable in conf_ports() is a > > char array, not a struct in*_addr might save us. >=20 > Hmm, look at the commit message for a48c5c2abf8a ("treewide: Disable > gcc strict aliasing rules as needed, drop workarounds"): that didn't help > with the checksum functions, because yes, at some point I had char *, but > then I used those as different types. >=20 > I guess struct in_addr / struct in6_addr as we have in sock_l4() might be > equivalent to that. >=20 > > I think replacing it > > with a union of an in_addr and in6_addr would also be ok. >=20 > That should work, yes, and that's what I originally wanted to suggest, > before remembering about union inany_addr... but that doesn't fit, see > below. >=20 > > > Without this patch, at least 32 bits must be updated before the call.= =20 > >=20 > > I'm not sure that's correct. If the compiler is allowed to assume > > that a char[] and a void * aren't aliased (even if they clearly are), > > then I'd expect it to also be allowed to assume that a char[] and a > > struct in_addr * aren't aliased. >=20 > Ouch, right, they aren't (again... sarcastically speaking). >=20 > > > It might sound like a joke because... it actually is. But look at what > > > we had to do for the functions in checksum.c. We pass const void *buf, > > > and anything that buf points to can be updated (with TBAA) after the > > > call. > > >=20 > > > I don't see any conceptual difference between this case and those > > > functions. > > >=20 > > > Anyway, that won't reasonably happen here, and in any case this would > > > have been broken for IPv6, so I'll go ahead and apply this. > > >=20 > > > But, eventually, I think we should switch all these usages to union > > > inany_addr *. =20 > >=20 > > So, we may be able to use union inany_addr in some places, but that's > > not the same thing as this: inany_addr carries IPv4 addresses as > > mapped IPv6 addresses, it's not switched on a separate af parameter. >=20 > I really meant *a pointer* to union inany_addr, that is: >=20 > > We could, of course, define a new type as a simple union of in_addr > > and in6_addr. >=20 > ...abusing it instead of using a separate union. On the other hand, > given where 'a4' is in there, it's not necessarily the same for > (strict) aliasing considerations. >=20 > Is "union in10_addr" fashionable enough? We could use A [16], but it's > inconvenient to type, and difficult to pronounce. Uh, I haven't heard of either of those. --=20 David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson --Stl2BS5hP6iUtO17 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmWaODwACgkQzQJF27ox 2GdAiBAAmaHg9e8YZLb4o31229aickOlg13lV0j+dmAlOByFowpDY/l+i3Ih+f1G SaZZGn2cZYNOoEaLCFg5YdJMtV9hnvcv3O54ZUk5VikNyPgybIR5sYCeRQGHwAwP nw0BTLXJNP8vA3NHKEbduW2qCo+tTQLPSfQIKCjqcME1hdJCojpx5yYVxbMPNrIa x2crdeY5PzOC74axhDDZzz4f8DEYItyIr9WBKAJshEfCjAXCZ1bV2yN/NE2OBRhG cJeZJeEwHsR4nQYdyemTlCtuzZBOLQnFjkXWHwe7xWWVM3pGryvD/ryZRDsf3bwQ lQz1DGsU4RjmxmNp8zWIau2UNpTTtqex6NQfNhuSSTW0x8IYWu7SGy0C2Y3U3dVW lq0zuLQeNfx/WvT09pdNnBXzT4vichDZBnK19IHbvj9KupR721HBnrjo0/aGF8CI tBxFfhqDzV+zPKUpPaTC1r3qXvw0YgE4W67AC6bIwv0ipdlbBv9lQR5TK+9Ukz4O FuQ8TF9cyJ3xhSfoXENZG4KSLCvG/SNV3V4ydnJJjEhoQkimjFIRW2gnpQaLd/mj tc4+X6kE6zWowSVOPKqZ2wUs6ffb3P0JxNpfMstRRL6T07zpsRQ59cdeE156CKv/ mKOtIlVZm7BlZSAP0OpCDrdyA4YPBYBYfpMXYOpK+cbgmMgIYoA= =Kb45 -----END PGP SIGNATURE----- --Stl2BS5hP6iUtO17--