From: Paolo Bonzini <pbonzini@redhat.com>
To: Peter Maydell <peter.maydell@linaro.org>
Cc: QEMU Developers <qemu-devel@nongnu.org>
Subject: Re: [Qemu-devel] [PULL 0/9] Misc patches for QEMU 2.5-rc2 (2015-11-25)
Date: Thu, 26 Nov 2015 14:04:08 +0100 [thread overview]
Message-ID: <56570348.3050409@redhat.com> (raw)
In-Reply-To: <CAFEAcA8krmW+JCGZ-WfXT0cVDgK0m+qpqTf14XHm27dUpb7aWA@mail.gmail.com>
On 26/11/2015 12:28, Peter Maydell wrote:
>> But we are relying on them, and thus we should document them. Witness
>> the number of patches fixing so called "undefined" behavior. And those
>> patches are _dangerous_.
>
> Until and unless the compiler guarantees us the semantics that
> we want, then silently hoping we get something we're not getting
> is even more dangerous, and avoiding the UB is better than
> just crossing our fingers and hoping.
>
>> I can certainly remove the "as documented by the GCC manual" part and
>> the -fwrapv setting, but silencing -Wshift-negative-value and
>> documenting what we rely on should go in.
>
> I don't see much point in documenting what we rely on
> if we can't rely on it and need to stop relying on it.
I'm having a hard, hard time believing that we can't rely on it. And if
we can rely on it, we don't need to stop relying on it.
To sum up:
- GCC promises that signed shift of << is implementation-defined except
for overflow in constant expressions, and defines it as operating on bit
values. This was approved. For GCC, -fwrapv does not even apply except
again for constant expressions.
- signed shift of negative values in constant expressions _are_ covered
by GCC's promise. The implementation-defined behavior of signed <<
gives a meaning to all signed shifts, and all that the C standard
requires is "Each constant expression shall evaluate to a constant that
is in the range of representable values for its type" (6.6p4).
Therefore we should at least disable -Wshift-negative-value, because it
doesn't buy us anything.
- regarding overflow, in addition to the weird -Wpedantic warning, GCC 6
adds a new -Wshift-overflow flag which is enabled by default in C99 and
C11 modes, and which only applies to constant expressions. So the
remaining case where the compiler may change its behavior on overflow,
i.e. constant expressions, will thus be caught by our usage of -Werror
(because -Wshift-overflow is enabled by default). So, independent of
the limited scope of GCC's promise, with GCC 6 we will never have
constant expressions that overflow because of left shifts.
- if a compiler actually treated signed << overflow undefined behavior,
a possible fix would be to use C89 mode (-std=gnu89), since signed << is
unspecified there rather than undefined. With C89, GCC's promise is
complete. We do use C89 with GCC <= 4.9 anyway, it makes sense to be
homogeneous across all supported compilers.
Now, -fwrapv was really included in my patch only to silence ubsan in
the future. I think it should, and I will work on patches to fix that.
However, an advantage of -std=gnu89 is that it silences ubsan _now_ at
least for GCC. So let's just drop -fwrapv and use -std=gnu89 instead.
This buys us time, and in the meanwhile I'll gett -fwrapv complete in GCC.
If this is okay, I'll remove the patch, respin the pull request, and
post a new configure change to use -std=gnu89.
Yes, we haven't heard anything from clang developers. But clang does
not even document implementation-defined behavior
(https://llvm.org/bugs/show_bug.cgi?id=11272). The bug says:
> The clang documentation should specify how clang behaves in cases
> specified to be implementation-defined in the relevant standards.
> Perhaps simply saying that our behavior is the same as GCC's would suffice?
This is about implementation-defined rather than undefined behavior, but
I think it's enough to not care about clang developer's silence.
Paolo
next prev parent reply other threads:[~2015-11-26 13:04 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-11-25 17:19 [Qemu-devel] [PULL 0/9] Misc patches for QEMU 2.5-rc2 (2015-11-25) Paolo Bonzini
2015-11-25 17:19 ` [Qemu-devel] [PULL 1/9] MAINTAINERS: Update TCG CPU cores section Paolo Bonzini
2015-11-25 17:19 ` [Qemu-devel] [PULL 2/9] QEMU does not care about left shifts of signed negative values Paolo Bonzini
2015-11-25 17:44 ` Peter Maydell
2015-11-25 17:50 ` Paolo Bonzini
2015-11-25 19:18 ` Peter Maydell
2015-11-25 19:30 ` Paolo Bonzini
2015-11-25 19:54 ` Peter Maydell
2015-11-25 21:05 ` Paolo Bonzini
2015-11-25 21:22 ` Peter Maydell
2015-11-25 17:19 ` [Qemu-devel] [PULL 3/9] call bdrv_drain_all() even if the vm is stopped Paolo Bonzini
2015-11-25 17:19 ` [Qemu-devel] [PULL 4/9] Revert "exec: silence hugetlbfs warning under qtest" Paolo Bonzini
2015-11-25 17:19 ` [Qemu-devel] [PULL 5/9] exec: remove warning about mempath and hugetlbfs Paolo Bonzini
2015-11-25 17:19 ` [Qemu-devel] [PULL 6/9] target-sparc: fix 32-bit truncation in fpackfix Paolo Bonzini
2015-11-25 17:19 ` [Qemu-devel] [PULL 7/9] target-i386: kvm: Abort if MCE bank count is not supported by host Paolo Bonzini
2015-11-25 17:19 ` [Qemu-devel] [PULL 8/9] target-i386: kvm: Use env->mcg_cap when setting up MCE Paolo Bonzini
2015-11-25 17:19 ` [Qemu-devel] [PULL 9/9] target-i386: kvm: Print warning when clearing mcg_cap bits Paolo Bonzini
2015-11-26 9:46 ` [Qemu-devel] [PULL 0/9] Misc patches for QEMU 2.5-rc2 (2015-11-25) Peter Maydell
2015-11-26 10:40 ` Paolo Bonzini
2015-11-26 10:56 ` Peter Maydell
2015-11-26 11:23 ` Paolo Bonzini
2015-11-26 11:28 ` Peter Maydell
2015-11-26 12:15 ` Markus Armbruster
2015-11-26 12:19 ` Peter Maydell
2015-11-26 13:07 ` Paolo Bonzini
2015-11-26 13:04 ` Paolo Bonzini [this message]
2015-11-26 15:01 ` Peter Maydell
2015-11-26 15:40 ` Paolo Bonzini
2015-11-26 15:55 ` Peter Maydell
2015-11-26 16:06 ` Paolo Bonzini
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=56570348.3050409@redhat.com \
--to=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
/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 NNTP newsgroup(s).