public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [RFC PATCH] Do not sanitize left shifts for -fwrapv
@ 2015-11-17 13:33 Paolo Bonzini
  2015-11-18 12:51 ` Marek Polacek
  0 siblings, 1 reply; 2+ messages in thread
From: Paolo Bonzini @ 2015-11-17 13:33 UTC (permalink / raw)
  To: gcc-patches, joseph, mpolacek

Left shifts into the sign bit is a kind of overflow, and the
standard chooses to treat left shifts of negative values the
same way.

However, the -fwrapv option modifies the language to one where
integers are defined as two's complement---which also defines
entirely the behavior of shifts.  Disable sanitization of left
shifts when -fwrapv is in effect.

This needs test cases of course, but I wanted to be sure in advance
whether this is an acceptable change and whether it is considered
a bug (thus acceptable for stage 3).  The same change was proposed
for LLVM at https://llvm.org/bugs/show_bug.cgi?id=25552.

Paolo

* c-family/c-ubsan.c (ubsan_instrument_shift): Disable sanitization
of left shifts for wrapping signed types as well.


Index: c-family/c-ubsan.c
===================================================================
--- c-family/c-ubsan.c	(revision 227511)
+++ c-family/c-ubsan.c	(working copy)
@@ -150,7 +150,7 @@
      (unsigned) x >> (uprecm1 - y)
      if non-zero, is undefined.  */
   if (code == LSHIFT_EXPR
-      && !TYPE_UNSIGNED (type0)
+      && !TYPE_OVERFLOW_WRAPS (type0)
       && flag_isoc99)
     {
       tree x = fold_build2 (MINUS_EXPR, op1_utype, uprecm1,
@@ -165,7 +165,7 @@
      x < 0 || ((unsigned) x >> (uprecm1 - y))
      if > 1, is undefined.  */
   if (code == LSHIFT_EXPR
-      && !TYPE_UNSIGNED (type0)
+      && !TYPE_OVERFLOW_WRAPS (type0)
       && (cxx_dialect >= cxx11))
     {
       tree x = fold_build2 (MINUS_EXPR, op1_utype, uprecm1,

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [RFC PATCH] Do not sanitize left shifts for -fwrapv
  2015-11-17 13:33 [RFC PATCH] Do not sanitize left shifts for -fwrapv Paolo Bonzini
@ 2015-11-18 12:51 ` Marek Polacek
  0 siblings, 0 replies; 2+ messages in thread
From: Marek Polacek @ 2015-11-18 12:51 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: gcc-patches, joseph

On Tue, Nov 17, 2015 at 02:32:50PM +0100, Paolo Bonzini wrote:
> Left shifts into the sign bit is a kind of overflow, and the
> standard chooses to treat left shifts of negative values the
> same way.
> 
> However, the -fwrapv option modifies the language to one where
> integers are defined as two's complement---which also defines
> entirely the behavior of shifts.  Disable sanitization of left
> shifts when -fwrapv is in effect.
> 
> This needs test cases of course, but I wanted to be sure in advance
> whether this is an acceptable change and whether it is considered
> a bug (thus acceptable for stage 3).  The same change was proposed
> for LLVM at https://llvm.org/bugs/show_bug.cgi?id=25552.
> 
> Paolo
> 
> * c-family/c-ubsan.c (ubsan_instrument_shift): Disable sanitization
> of left shifts for wrapping signed types as well.
> 
> 
> Index: c-family/c-ubsan.c
> ===================================================================
> --- c-family/c-ubsan.c	(revision 227511)
> +++ c-family/c-ubsan.c	(working copy)
> @@ -150,7 +150,7 @@
>       (unsigned) x >> (uprecm1 - y)
>       if non-zero, is undefined.  */
>    if (code == LSHIFT_EXPR
> -      && !TYPE_UNSIGNED (type0)
> +      && !TYPE_OVERFLOW_WRAPS (type0)
>        && flag_isoc99)
>      {
>        tree x = fold_build2 (MINUS_EXPR, op1_utype, uprecm1,
> @@ -165,7 +165,7 @@
>       x < 0 || ((unsigned) x >> (uprecm1 - y))
>       if > 1, is undefined.  */
>    if (code == LSHIFT_EXPR
> -      && !TYPE_UNSIGNED (type0)
> +      && !TYPE_OVERFLOW_WRAPS (type0)
>        && (cxx_dialect >= cxx11))
>      {
>        tree x = fold_build2 (MINUS_EXPR, op1_utype, uprecm1,

I think this would be ok provided you add some testcases (unless I'm missing
something).  Note that this suppresses instrumenting not only left-shifting
into the sign bit, but also shift overflows, so e.g. 10 << 30.

And I think this might be viewed on as a bug, thus should be ok even at this
stage if you open a PR.

	Marek

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-11-18 12:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-17 13:33 [RFC PATCH] Do not sanitize left shifts for -fwrapv Paolo Bonzini
2015-11-18 12:51 ` Marek Polacek

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).