From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 3jlrJzbzRmPTNAsAWB0awg (envelope-from ) for ; Wed, 12 Oct 2022 13:02:46 -0400 Received: by simark.ca (Postfix, from userid 112) id 8FE091E112; Wed, 12 Oct 2022 13:02:46 -0400 (EDT) Authentication-Results: simark.ca; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=uviUA5ID; dkim-atps=neutral X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RDNS_DYNAMIC,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 81D621E0D3 for ; Wed, 12 Oct 2022 13:02:45 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E20BC3857366 for ; Wed, 12 Oct 2022 17:02:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E20BC3857366 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1665594164; bh=IQb0KmmuyPiIvWpLSDDS4Nd8ab1OjtYznQHxYk9oHqM=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=uviUA5IDTolWbv4682UoPeQMzDpBsEoe/92xUwROO7ix1t/i4kt/CMk+WvWP3Tm8J nquFM6BtO3CuLaK/zb518vdUNiasRB9k4T//K92RWCINOLY4iRT4XYsw2+to6cLqQ1 Q8sfwNVJLhdQFQ52Lvvygqy0lzxe0N1HBzktNxJk= Received: from lndn.lancelotsix.com (vps-42846194.vps.ovh.net [IPv6:2001:41d0:801:2000::2400]) by sourceware.org (Postfix) with ESMTPS id 9799E38582BF for ; Wed, 12 Oct 2022 17:02:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9799E38582BF Received: from octopus (unknown [IPv6:2a02:390:9086:0:a150:3:4de6:2e3b]) by lndn.lancelotsix.com (Postfix) with ESMTPSA id 5D0C980EB8; Wed, 12 Oct 2022 17:02:22 +0000 (UTC) Date: Wed, 12 Oct 2022 18:02:15 +0100 To: Pedro Alves Subject: Re: [PATCH 4/5] sim/erc32: avoid dereferencing type-punned pointer warnings Message-ID: <20221012170215.imifj66p6dndtf6p@octopus> References: <170dc056-7e74-6c15-7131-31943c17be3a@palves.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <170dc056-7e74-6c15-7131-31943c17be3a@palves.net> X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.5.11 (lndn.lancelotsix.com [0.0.0.0]); Wed, 12 Oct 2022 17:02:22 +0000 (UTC) X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Lancelot SIX via Gdb-patches Reply-To: Lancelot SIX Cc: gdb-patches@sourceware.org Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" On Wed, Oct 12, 2022 at 03:11:27PM +0100, Pedro Alves wrote: > On 2022-10-12 1:38 p.m., Andrew Burgess via Gdb-patches wrote: > > When building the erc32 simulator I get a few warnings like this: > > > > /tmp/build/sim/../../src/sim/erc32/exec.c:1377:21: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] > > 1377 | sregs->fs[rd] = *((float32 *) & ddata[0]); > > | ~^~~~~~~~~~~~~~~~~~~~~~~ > > > > The type of '& ddata[0]' will be 'uint32_t *', which is what triggers > > the warning. > > > > This commit uses an intermediate pointer of type 'char *' when > > performing the type-punning, which is well-defined behaviour, and will > > silence the above warning. > > I'm afraid that's not correct. That's still undefined behavior, it's just silencing > the warning. The end result is still aliasing float32 and uint32_t objects, and risks > generating bogus code. The char escape hatch only works if you access the object > representation via a character type. Here, the patch is still accessing the object > representation of a uint32_t object via a floa32 type. > > Here's an old article explaining strict aliasing (just one that I found via a quick google): > > https://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html > > This scenario is the "CASTING TO CHAR*" one in that article. > > > @@ -1345,7 +1345,8 @@ dispatch_instruction(struct pstate *sregs) > > if (mexc) { > > sregs->trap = TRAP_DEXC; > > } else { > > - sregs->fs[rd] = *((float32 *) & data); > > + char *ptr = (char *) &data; > > + sregs->fs[rd] = *((float32 *) ptr); > > The best IMHO is to do the type punning via a union, like e.g.: > > union { float32 f; uint32_t i; } u; > u.i = data; > sregs->fs[rd] = u.f; > > See here in the C11 standard: > > https://port70.net/~nsz/c/c11/n1570.html#note95 > > and also the documentation of -fstrict-aliasing at: > > https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html > Hi, Another well defined (at least to my knowledge) solution to this problem is memcpy. You could do something like: memcpy (&sregt->fs[rd], ddata, sizeof (float32)); I tend to find this more straightforward than the type punning version, but I would be happy with either. Best, Lancelot. > Pedro Alves