From: Andrew Burgess <aburgess@redhat.com>
To: Lancelot SIX <lsix@lancelotsix.com>, Pedro Alves <pedro@palves.net>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH 4/5] sim/erc32: avoid dereferencing type-punned pointer warnings
Date: Thu, 13 Oct 2022 11:35:01 +0100 [thread overview]
Message-ID: <87czawau5m.fsf@redhat.com> (raw)
In-Reply-To: <20221012170215.imifj66p6dndtf6p@octopus>
Lancelot SIX <lsix@lancelotsix.com> writes:
> 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.
>
Pedro, Lancelot, thanks for taking the time to give really useful
feedback.
In the end I went with the memcpy approach. I ran a few tests with GCC,
Clang, and ICC, and in each case the code generated at -O0 was either
identical, or pretty much identical when using memcpy vs using a union.
When switching to -O2 the code was identical in all cases I checked.
Thoughts?
Thanks,
Andrew
---
commit d04acbda1f2a191193772fc9416cf5b29f0702ce
Author: Andrew Burgess <aburgess@redhat.com>
Date: Wed Oct 12 11:45:53 2022 +0100
sim/erc32: avoid dereferencing type-punned pointer warnings
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 makes use of memcpy when performing the type-punning,
which resolves the above warnings.
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.
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..26d48c0e46e 100644
--- a/sim/erc32/exec.c
+++ b/sim/erc32/exec.c
@@ -1345,7 +1345,7 @@ dispatch_instruction(struct pstate *sregs)
if (mexc) {
sregs->trap = TRAP_DEXC;
} else {
- sregs->fs[rd] = *((float32 *) & data);
+ memcpy (&sregs->fs[rd], &data, sizeof (sregs->fs[rd]));
}
break;
case LDDF:
@@ -1373,11 +1373,12 @@ dispatch_instruction(struct pstate *sregs)
} else {
rd &= 0x1E;
sregs->flrd = rd;
- sregs->fs[rd] = *((float32 *) & ddata[0]);
+ memcpy (&sregs->fs[rd], &ddata[0], sizeof (sregs->fs[rd]));
#ifdef STAT
sregs->nload++; /* Double load counts twice */
#endif
- sregs->fs[rd + 1] = *((float32 *) & ddata[1]);
+ memcpy (&sregs->fs[rd + 1], &ddata[1],
+ sizeof (sregs->fs[rd + 1]));
sregs->ltime = ebase.simtime + sregs->icnt + FLSTHOLD +
sregs->hold + sregs->fhold;
}
next prev parent reply other threads:[~2022-10-13 10:35 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-12 12:38 [PATCH 0/5] Silence some build warnings in various simulators Andrew Burgess
2022-10-12 12:38 ` [PATCH 1/5] sim/cgen: mask uninitialized variable warning in cgen-run.c Andrew Burgess
2022-10-23 12:30 ` Mike Frysinger
2022-10-24 15:57 ` Andrew Burgess
2022-10-24 15:59 ` Mike Frysinger
2022-10-27 15:53 ` Andrew Burgess
2022-10-12 12:38 ` [PATCH 2/5] sim/ppc: fix warnings related to printf format strings Andrew Burgess
2022-10-12 12:46 ` Tsukasa OI
2022-10-12 13:50 ` Andrew Burgess
2022-10-23 12:20 ` Mike Frysinger
2022-10-24 15:41 ` Andrew Burgess
2022-10-12 12:38 ` [PATCH 3/5] sim/ppc: mark device_error function as ATTRIBUTE_NORETURN Andrew Burgess
2022-10-12 12:38 ` [PATCH 4/5] sim/erc32: avoid dereferencing type-punned pointer warnings Andrew Burgess
2022-10-12 14:11 ` Pedro Alves
2022-10-12 17:02 ` Lancelot SIX
2022-10-13 10:35 ` Andrew Burgess [this message]
2022-10-13 10:49 ` Pedro Alves
2022-10-23 12:34 ` Mike Frysinger
2022-10-24 15:42 ` Andrew Burgess
2022-10-12 12:38 ` [PATCH 5/5] sim/iq2000: silence pointer-sign warnings Andrew Burgess
2022-10-23 12:32 ` Mike Frysinger
2022-10-24 15:45 ` Andrew Burgess
2022-10-26 8:51 ` Mike Frysinger
2022-10-14 17:50 ` [PATCH 0/5] Silence some build warnings in various simulators Tom Tromey
2022-10-19 13:34 ` Andrew Burgess
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87czawau5m.fsf@redhat.com \
--to=aburgess@redhat.com \
--cc=gdb-patches@sourceware.org \
--cc=lsix@lancelotsix.com \
--cc=pedro@palves.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).