Message ID | b36f2b9339266fdef2e34970f932782925744ba4.1665578246.git.aburgess@redhat.com |
---|---|
State | New |
Headers |
Return-Path: <gdb-patches-bounces+patchwork=sourceware.org@sourceware.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9A1903898391 for <patchwork@sourceware.org>; Wed, 12 Oct 2022 12:39:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9A1903898391 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1665578381; bh=TT5hpEy6b9bzQJvWDdSFFqeHrV3E8WFMYWJ/LnOcUd8=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=SRfUzlddriXF6sgeTcaGmGTKA8kfyMl033jvUstjLruaiyBU1t7NU01vav1yO5lv6 HaHRlS4Qc58+nYkFzfRQ1/ABBopxAnO/GGawSkIHMNm6ws5heHxEpZ/49xmvZOOYn4 b5Xv7c32Hb8L0+7jTmFPuIXa1D5hJt4rwGhSmJsI= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 26465385116B for <gdb-patches@sourceware.org>; Wed, 12 Oct 2022 12:38:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 26465385116B Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-634-CZahK0IBOIWE9li_PgCXog-1; Wed, 12 Oct 2022 08:38:47 -0400 X-MC-Unique: CZahK0IBOIWE9li_PgCXog-1 Received: by mail-wm1-f71.google.com with SMTP id v191-20020a1cacc8000000b003bdf7b78dccso9946909wme.3 for <gdb-patches@sourceware.org>; Wed, 12 Oct 2022 05:38:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TT5hpEy6b9bzQJvWDdSFFqeHrV3E8WFMYWJ/LnOcUd8=; b=Cx3garYV+3qSMpmIABppXAw+B+i+8layzJebLr7D2W66XbZxStQtBipfrGnjTSPgd0 hU1DQZZoeLZojRzpOkOe8teIlt8c2bi3R/Rqkgp0CKCJTv93u4vJe7es4G6isfCYp5Gl KQ5VtvvmIpSujyEMgNp0uwCZlZoq6nW17ZFJC7r3RMdBTgE6qAxE2tbZlDnVegELxnC0 pf3FF9u+fAur2vpdUxwbN5C6yMHtPEtLgCJVYbcZMtJ2sDRqhOp46xLTUAStcSS89miC 3mRm85VysfaD44tWEqPepzTB7RxBuG+43EVnB4LbK/E+mobXL+hWSXz5Ynpw3WeOVE6f fKEQ== X-Gm-Message-State: ACrzQf1jBRyZIOvr6HdywP3SmrEVeHfwTg7A450bLy25fvROf5p6Acz7 zd1WbS563I7MSxnHANqYvQuFZ4NJJ+ByqTz/1RuykGPGze8Mg+VdWv6Sxviu+7FFKNTHhP6lERH tUkVX386be8Q5fA9f4ClhJVk4keyPSV1JTsK5zHOLwgnTkgs/DsWRohUDAKbjr/RAw00FCZy3EQ == X-Received: by 2002:a7b:c050:0:b0:3b4:fae1:6bce with SMTP id u16-20020a7bc050000000b003b4fae16bcemr2530338wmc.131.1665578326348; Wed, 12 Oct 2022 05:38:46 -0700 (PDT) X-Google-Smtp-Source: AMsMyM45ilqL7wpJnuiryU4V7RvlSrziFvltbCEbpTI7dSSwszW4muzrVebXTqGyjppDCs5gcQ9ZvQ== X-Received: by 2002:a7b:c050:0:b0:3b4:fae1:6bce with SMTP id u16-20020a7bc050000000b003b4fae16bcemr2530326wmc.131.1665578325982; Wed, 12 Oct 2022 05:38:45 -0700 (PDT) Received: from localhost (52.72.115.87.dyn.plus.net. [87.115.72.52]) by smtp.gmail.com with ESMTPSA id j19-20020a05600c1c1300b003c6c1686b10sm1899117wms.7.2022.10.12.05.38.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Oct 2022 05:38:45 -0700 (PDT) To: gdb-patches@sourceware.org Subject: [PATCH 4/5] sim/erc32: avoid dereferencing type-punned pointer warnings Date: Wed, 12 Oct 2022 13:38:34 +0100 Message-Id: <b36f2b9339266fdef2e34970f932782925744ba4.1665578246.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <cover.1665578246.git.aburgess@redhat.com> References: <cover.1665578246.git.aburgess@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list <gdb-patches.sourceware.org> List-Unsubscribe: <https://sourceware.org/mailman/options/gdb-patches>, <mailto:gdb-patches-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/gdb-patches/> List-Post: <mailto:gdb-patches@sourceware.org> List-Help: <mailto:gdb-patches-request@sourceware.org?subject=help> List-Subscribe: <https://sourceware.org/mailman/listinfo/gdb-patches>, <mailto:gdb-patches-request@sourceware.org?subject=subscribe> From: Andrew Burgess via Gdb-patches <gdb-patches@sourceware.org> Reply-To: Andrew Burgess <aburgess@redhat.com> Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" <gdb-patches-bounces+patchwork=sourceware.org@sourceware.org> |
Series |
Silence some build warnings in various simulators
|
|
Commit Message
Andrew Burgess
Oct. 12, 2022, 12:38 p.m. UTC
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. With this change, I now see no warnings when compiling exec.c, which means that the line in Makefile.in that disables -Werror can be removed. There should be no change in behaviour after this commit. --- sim/erc32/Makefile.in | 3 --- sim/erc32/exec.c | 12 +++++++++--- 2 files changed, 9 insertions(+), 6 deletions(-)
Comments
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 Pedro Alves
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
On 12 Oct 2022 13:38, 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. this is kind of a poor fix. if we can't arrange for the pointers to be the right type, at least a memcpy would be better. -mike
Mike Frysinger <vapier@gentoo.org> writes: > On 12 Oct 2022 13:38, 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. > > this is kind of a poor fix. if we can't arrange for the pointers to be > the right type, at least a memcpy would be better. This was already discussed in this thread: https://sourceware.org/pipermail/gdb-patches/2022-October/192604.html The version that got pushed: https://sourceware.org/pipermail/gdb-patches/2022-October/192647.html does make use of memcpy. Thanks, Andrew
diff --git a/sim/erc32/Makefile.in b/sim/erc32/Makefile.in index 786ae1dcc7b..41830aab726 100644 --- a/sim/erc32/Makefile.in +++ b/sim/erc32/Makefile.in @@ -32,9 +32,6 @@ SIM_EXTRA_CLEAN = clean-sis # behaviour of UART interrupt routines ... SIM_EXTRA_CFLAGS += -DFAST_UART -I$(srcroot) -# Some modules don't build cleanly yet. -exec.o: SIM_WERROR_CFLAGS = - ## COMMON_POST_CONFIG_FRAG # `sis' doesn't need interf.o. diff --git a/sim/erc32/exec.c b/sim/erc32/exec.c index ef93692e7a2..af9ad9ea9ab 100644 --- a/sim/erc32/exec.c +++ b/sim/erc32/exec.c @@ -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); } break; case LDDF: @@ -1371,13 +1372,18 @@ dispatch_instruction(struct pstate *sregs) if (mexc) { sregs->trap = TRAP_DEXC; } else { + char *ptr; + rd &= 0x1E; sregs->flrd = rd; - sregs->fs[rd] = *((float32 *) & ddata[0]); + + ptr = (char *) &ddata[0]; + sregs->fs[rd] = *((float32 *) ptr); #ifdef STAT sregs->nload++; /* Double load counts twice */ #endif - sregs->fs[rd + 1] = *((float32 *) & ddata[1]); + ptr = (char *) &ddata[1]; + sregs->fs[rd + 1] = *((float32 *) ptr); sregs->ltime = ebase.simtime + sregs->icnt + FLSTHOLD + sregs->hold + sregs->fhold; }