Re: [PATCH] Implement -fsanitize=null + new sanopt pass

2013-11-29 Thread H.J. Lu
On Mon, Nov 18, 2013 at 6:44 AM, Marek Polacek pola...@redhat.com wrote:
 On Mon, Nov 18, 2013 at 02:51:41PM +0100, Jakub Jelinek wrote:
 On Wed, Nov 13, 2013 at 12:13:48AM +0100, Marek Polacek wrote:
  --- gcc/config/bootstrap-ubsan.mk.mp2013-11-12 13:46:13.345182065 
  +0100
  +++ gcc/config/bootstrap-ubsan.mk   2013-11-12 13:46:49.812314016 +0100
  @@ -2,6 +2,6 @@
 
   STAGE2_CFLAGS += -fsanitize=undefined
   STAGE3_CFLAGS += -fsanitize=undefined
  -POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread \
  +POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread 
  -ldl \

 Hopefully with my pending patch you can remove the -lpthread -ldl again, but
 ok for now.


You shouldn't use -ldl directly.  Not all OSes have libdl.  You
should extract the libdl check from gcc/configure.ac and
set LIBDL instead by changing gcc/Makefile.in

PLUGINLIBS = @pluginlibs@

to

LIBDL = @libdl@
PLUGINLIBS = @pluginlibs@ $(LIBD)

Then you can use

POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread $(LIBDL) \


-- 
H.J.


Re: [PATCH] Implement -fsanitize=null + new sanopt pass

2013-11-29 Thread H.J. Lu
On Fri, Nov 29, 2013 at 11:22 AM, H.J. Lu hjl.to...@gmail.com wrote:
 On Mon, Nov 18, 2013 at 6:44 AM, Marek Polacek pola...@redhat.com wrote:
 On Mon, Nov 18, 2013 at 02:51:41PM +0100, Jakub Jelinek wrote:
 On Wed, Nov 13, 2013 at 12:13:48AM +0100, Marek Polacek wrote:
  --- gcc/config/bootstrap-ubsan.mk.mp2013-11-12 13:46:13.345182065 
  +0100
  +++ gcc/config/bootstrap-ubsan.mk   2013-11-12 13:46:49.812314016 +0100
  @@ -2,6 +2,6 @@
 
   STAGE2_CFLAGS += -fsanitize=undefined
   STAGE3_CFLAGS += -fsanitize=undefined
  -POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread \
  +POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread 
  -ldl \

 Hopefully with my pending patch you can remove the -lpthread -ldl again, but
 ok for now.


 You shouldn't use -ldl directly.  Not all OSes have libdl.  You
 should extract the libdl check from gcc/configure.ac and
 set LIBDL instead by changing gcc/Makefile.in

 PLUGINLIBS = @pluginlibs@

 to

 LIBDL = @libdl@
 PLUGINLIBS = @pluginlibs@ $(LIBD)

 Then you can use

 POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread 
 $(LIBDL) \


Something like this.   Only tested with normal build.

-- 
H.J.
---
diff --git a/config/bootstrap-ubsan.mk b/config/bootstrap-ubsan.mk
index 0cd8b17..c298cd1 100644
--- a/config/bootstrap-ubsan.mk
+++ b/config/bootstrap-ubsan.mk
@@ -2,6 +2,7 @@

 STAGE2_CFLAGS += -fsanitize=undefined
 STAGE3_CFLAGS += -fsanitize=undefined
-POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread -ldl \
+POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread \
+  $(LIBDL) \
   -B$$r/prev-$(TARGET_SUBDIR)/libsanitizer/ubsan/ \
   -B$$r/prev-$(TARGET_SUBDIR)/libsanitizer/ubsan/.libs
diff --git a/gcc/Makefile.in b/gcc/Makefile.in
index 4d683a0..cb64241 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -340,12 +340,15 @@ CLOOGINC = @CLOOGINC@
 # Set to 'yes' if the LTO front end is enabled.
 enable_lto = @enable_lto@

+# Library for dlopen
+LIBDL=@libdl@
+
 # Compiler and flags needed for plugin support
 PLUGINCC = @CXX@
 PLUGINCFLAGS = @CXXFLAGS@

 # Libs and linker options needed for plugin support
-PLUGINLIBS = @pluginlibs@
+PLUGINLIBS = @pluginlibs@ $(LIBDL)

 enable_plugin = @enable_plugin@

diff --git a/gcc/configure.ac b/gcc/configure.ac
index 91a22d5..80cd248 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -5404,14 +5404,6 @@ if test x$enable_plugin = xyes; then
 AC_MSG_RESULT([unable to check])
   fi

-  # Check -ldl
-  saved_LIBS=$LIBS
-  AC_SEARCH_LIBS([dlopen], [dl])
-  if test x$ac_cv_search_dlopen = x-ldl; then
-pluginlibs=$pluginlibs -ldl
-  fi
-  LIBS=$saved_LIBS
-
   # Check that we can build shared objects with -fPIC -shared
   saved_LDFLAGS=$LDFLAGS
   saved_CFLAGS=$CFLAGS
@@ -5454,6 +5446,16 @@ if test x$enable_plugin = xyes; then
   AC_DEFINE(ENABLE_PLUGIN, 1, [Define to enable plugin support.])
 fi

+# Check -ldl
+libdl=
+saved_LIBS=$LIBS
+AC_SEARCH_LIBS([dlopen], [dl])
+if test x$ac_cv_search_dlopen = x-ldl; then
+  libdl=-ldl
+fi
+LIBS=$saved_LIBS
+AC_SUBST(libdl)
+

 # Enable --enable-host-shared
 AC_ARG_ENABLE(host-shared,


Re: [PATCH] Implement -fsanitize=null + new sanopt pass

2013-11-29 Thread Jakub Jelinek
On Fri, Nov 29, 2013 at 11:22:00AM -0800, H.J. Lu wrote:
 On Mon, Nov 18, 2013 at 6:44 AM, Marek Polacek pola...@redhat.com wrote:
  On Mon, Nov 18, 2013 at 02:51:41PM +0100, Jakub Jelinek wrote:
  On Wed, Nov 13, 2013 at 12:13:48AM +0100, Marek Polacek wrote:
   --- gcc/config/bootstrap-ubsan.mk.mp2013-11-12 
   13:46:13.345182065 +0100
   +++ gcc/config/bootstrap-ubsan.mk   2013-11-12 13:46:49.812314016 +0100
   @@ -2,6 +2,6 @@
  
STAGE2_CFLAGS += -fsanitize=undefined
STAGE3_CFLAGS += -fsanitize=undefined
   -POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread \
   +POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread 
   -ldl \
 
  Hopefully with my pending patch you can remove the -lpthread -ldl again, 
  but
  ok for now.
 
 
 You shouldn't use -ldl directly.  Not all OSes have libdl.  You
 should extract the libdl check from gcc/configure.ac and
 set LIBDL instead by changing gcc/Makefile.in

-static-libubsan should add all the libraries needed of libubsan.a by now,
so -lpthread -ldl should be just removed from POSTSTAGE1_LDFLAGS.

Jakub


Re: [PATCH] Implement -fsanitize=null + new sanopt pass

2013-11-29 Thread Marek Polacek
On Fri, Nov 29, 2013 at 08:32:34PM +0100, Jakub Jelinek wrote:
 On Fri, Nov 29, 2013 at 11:22:00AM -0800, H.J. Lu wrote:
  On Mon, Nov 18, 2013 at 6:44 AM, Marek Polacek pola...@redhat.com wrote:
   On Mon, Nov 18, 2013 at 02:51:41PM +0100, Jakub Jelinek wrote:
   On Wed, Nov 13, 2013 at 12:13:48AM +0100, Marek Polacek wrote:
--- gcc/config/bootstrap-ubsan.mk.mp2013-11-12 
13:46:13.345182065 +0100
+++ gcc/config/bootstrap-ubsan.mk   2013-11-12 13:46:49.812314016 +0100
@@ -2,6 +2,6 @@
   
 STAGE2_CFLAGS += -fsanitize=undefined
 STAGE3_CFLAGS += -fsanitize=undefined
-POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread 
\
+POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread 
-ldl \
  
   Hopefully with my pending patch you can remove the -lpthread -ldl again, 
   but
   ok for now.
  
  
  You shouldn't use -ldl directly.  Not all OSes have libdl.  You
  should extract the libdl check from gcc/configure.ac and
  set LIBDL instead by changing gcc/Makefile.in
 
 -static-libubsan should add all the libraries needed of libubsan.a by now,
 so -lpthread -ldl should be just removed from POSTSTAGE1_LDFLAGS.

So ok to install this one?

2013-11-29  Marek Polacek  pola...@redhat.com

* bootstrap-ubsan.mk (POSTSTAGE1_LDFLAGS): Remove -lpthread -ldl.

--- gcc/bootstrap-ubsan.mk.mp3  2013-11-29 20:50:04.788238860 +0100
+++ gcc/bootstrap-ubsan.mk  2013-11-29 20:50:25.870322185 +0100
@@ -2,6 +2,6 @@
 
 STAGE2_CFLAGS += -fsanitize=undefined
 STAGE3_CFLAGS += -fsanitize=undefined
-POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread -ldl \
+POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan \
  -B$$r/prev-$(TARGET_SUBDIR)/libsanitizer/ubsan/ \
  -B$$r/prev-$(TARGET_SUBDIR)/libsanitizer/ubsan/.libs

Marek


Re: [PATCH] Implement -fsanitize=null + new sanopt pass

2013-11-29 Thread Jakub Jelinek
On Fri, Nov 29, 2013 at 08:55:26PM +0100, Marek Polacek wrote:
 2013-11-29  Marek Polacek  pola...@redhat.com
 
   * bootstrap-ubsan.mk (POSTSTAGE1_LDFLAGS): Remove -lpthread -ldl.
 
 --- gcc/bootstrap-ubsan.mk.mp32013-11-29 20:50:04.788238860 +0100
 +++ gcc/bootstrap-ubsan.mk2013-11-29 20:50:25.870322185 +0100
 @@ -2,6 +2,6 @@
  
  STAGE2_CFLAGS += -fsanitize=undefined
  STAGE3_CFLAGS += -fsanitize=undefined
 -POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread -ldl \
 +POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan \
 -B$$r/prev-$(TARGET_SUBDIR)/libsanitizer/ubsan/ \
 -B$$r/prev-$(TARGET_SUBDIR)/libsanitizer/ubsan/.libs

Please add -B$$r/prev-$(TARGET_SUBDIR)/libsanitizer/ too, so that
it is able to find libsanitizer.spec.  Ok with that change.

Jakub


Re: [PATCH] Implement -fsanitize=null + new sanopt pass

2013-11-29 Thread Marek Polacek
On Fri, Nov 29, 2013 at 08:57:23PM +0100, Jakub Jelinek wrote:
 On Fri, Nov 29, 2013 at 08:55:26PM +0100, Marek Polacek wrote:
  2013-11-29  Marek Polacek  pola...@redhat.com
  
  * bootstrap-ubsan.mk (POSTSTAGE1_LDFLAGS): Remove -lpthread -ldl.
  
  --- gcc/bootstrap-ubsan.mk.mp3  2013-11-29 20:50:04.788238860 +0100
  +++ gcc/bootstrap-ubsan.mk  2013-11-29 20:50:25.870322185 +0100
  @@ -2,6 +2,6 @@
   
   STAGE2_CFLAGS += -fsanitize=undefined
   STAGE3_CFLAGS += -fsanitize=undefined
  -POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread -ldl 
  \
  +POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan \
-B$$r/prev-$(TARGET_SUBDIR)/libsanitizer/ubsan/ \
-B$$r/prev-$(TARGET_SUBDIR)/libsanitizer/ubsan/.libs
 
 Please add -B$$r/prev-$(TARGET_SUBDIR)/libsanitizer/ too, so that
 it is able to find libsanitizer.spec.  Ok with that change.

Thanks, will apply the following then.

2013-11-29  Marek Polacek  pola...@redhat.com

* bootstrap-ubsan.mk (POSTSTAGE1_LDFLAGS): Remove -lpthread -ldl.
Add -B$$r/prev-$(TARGET_SUBDIR)/libsanitizer/.

--- gcc/bootstrap-ubsan.mk.mp3  2013-11-29 20:50:04.788238860 +0100
+++ gcc/bootstrap-ubsan.mk  2013-11-29 20:58:23.322131822 +0100
@@ -2,6 +2,7 @@
 
 STAGE2_CFLAGS += -fsanitize=undefined
 STAGE3_CFLAGS += -fsanitize=undefined
-POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread -ldl \
+POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan \
+ -B$$r/prev-$(TARGET_SUBDIR)/libsanitizer/ \
  -B$$r/prev-$(TARGET_SUBDIR)/libsanitizer/ubsan/ \
  -B$$r/prev-$(TARGET_SUBDIR)/libsanitizer/ubsan/.libs

Marek


Re: [PATCH] Implement -fsanitize=null + new sanopt pass

2013-11-29 Thread H.J. Lu
On Fri, Nov 29, 2013 at 12:02 PM, Marek Polacek pola...@redhat.com wrote:
 On Fri, Nov 29, 2013 at 08:57:23PM +0100, Jakub Jelinek wrote:
 On Fri, Nov 29, 2013 at 08:55:26PM +0100, Marek Polacek wrote:
  2013-11-29  Marek Polacek  pola...@redhat.com
 
  * bootstrap-ubsan.mk (POSTSTAGE1_LDFLAGS): Remove -lpthread -ldl.
 
  --- gcc/bootstrap-ubsan.mk.mp3  2013-11-29 20:50:04.788238860 +0100
  +++ gcc/bootstrap-ubsan.mk  2013-11-29 20:50:25.870322185 +0100
  @@ -2,6 +2,6 @@
 
   STAGE2_CFLAGS += -fsanitize=undefined
   STAGE3_CFLAGS += -fsanitize=undefined
  -POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread 
  -ldl \
  +POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan \
-B$$r/prev-$(TARGET_SUBDIR)/libsanitizer/ubsan/ \
-B$$r/prev-$(TARGET_SUBDIR)/libsanitizer/ubsan/.libs

 Please add -B$$r/prev-$(TARGET_SUBDIR)/libsanitizer/ too, so that
 it is able to find libsanitizer.spec.  Ok with that change.

 Thanks, will apply the following then.

 2013-11-29  Marek Polacek  pola...@redhat.com

 * bootstrap-ubsan.mk (POSTSTAGE1_LDFLAGS): Remove -lpthread -ldl.
 Add -B$$r/prev-$(TARGET_SUBDIR)/libsanitizer/.

 --- gcc/bootstrap-ubsan.mk.mp3  2013-11-29 20:50:04.788238860 +0100
 +++ gcc/bootstrap-ubsan.mk  2013-11-29 20:58:23.322131822 +0100
 @@ -2,6 +2,7 @@

  STAGE2_CFLAGS += -fsanitize=undefined
  STAGE3_CFLAGS += -fsanitize=undefined
 -POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread -ldl \
 +POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan \
 + -B$$r/prev-$(TARGET_SUBDIR)/libsanitizer/ \
   -B$$r/prev-$(TARGET_SUBDIR)/libsanitizer/ubsan/ \
   -B$$r/prev-$(TARGET_SUBDIR)/libsanitizer/ubsan/.libs

 Marek

I pushed it to binutils-gdb.

-- 
H.J.


Re: [PATCH] Implement -fsanitize=null + new sanopt pass

2013-11-18 Thread Jakub Jelinek
On Wed, Nov 13, 2013 at 12:13:48AM +0100, Marek Polacek wrote:
 --- gcc/config/bootstrap-ubsan.mk.mp  2013-11-12 13:46:13.345182065 +0100
 +++ gcc/config/bootstrap-ubsan.mk 2013-11-12 13:46:49.812314016 +0100
 @@ -2,6 +2,6 @@
  
  STAGE2_CFLAGS += -fsanitize=undefined
  STAGE3_CFLAGS += -fsanitize=undefined
 -POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread \
 +POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread -ldl \

Hopefully with my pending patch you can remove the -lpthread -ldl again, but
ok for now.
 +  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (gsi))
 + {
 +   gimple stmt = gsi_stmt (gsi);
 +
 +   if (gimple_code (stmt) != GIMPLE_CALL)

if (is_gimple_call (stmt))

Ok with those changes.

Jakub


Re: [PATCH] Implement -fsanitize=null + new sanopt pass

2013-11-18 Thread Jakub Jelinek
On Mon, Nov 18, 2013 at 02:51:41PM +0100, Jakub Jelinek wrote:
 On Wed, Nov 13, 2013 at 12:13:48AM +0100, Marek Polacek wrote:
  --- gcc/config/bootstrap-ubsan.mk.mp2013-11-12 13:46:13.345182065 
  +0100
  +++ gcc/config/bootstrap-ubsan.mk   2013-11-12 13:46:49.812314016 +0100
  @@ -2,6 +2,6 @@
   
   STAGE2_CFLAGS += -fsanitize=undefined
   STAGE3_CFLAGS += -fsanitize=undefined
  -POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread \
  +POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread -ldl 
  \
 
 Hopefully with my pending patch you can remove the -lpthread -ldl again, but
 ok for now.
  +  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (gsi))
  +   {
  + gimple stmt = gsi_stmt (gsi);
  +
  + if (gimple_code (stmt) != GIMPLE_CALL)
 
 if (is_gimple_call (stmt))
 
 Ok with those changes.

Oh, one more thing, please update gcc/doc/, the -fsanitize= description is
far from up to date there.

Jakub


Re: [PATCH] Implement -fsanitize=null + new sanopt pass

2013-11-18 Thread Marek Polacek
On Mon, Nov 18, 2013 at 02:51:41PM +0100, Jakub Jelinek wrote:
 On Wed, Nov 13, 2013 at 12:13:48AM +0100, Marek Polacek wrote:
  --- gcc/config/bootstrap-ubsan.mk.mp2013-11-12 13:46:13.345182065 
  +0100
  +++ gcc/config/bootstrap-ubsan.mk   2013-11-12 13:46:49.812314016 +0100
  @@ -2,6 +2,6 @@
   
   STAGE2_CFLAGS += -fsanitize=undefined
   STAGE3_CFLAGS += -fsanitize=undefined
  -POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread \
  +POSTSTAGE1_LDFLAGS += -fsanitize=undefined -static-libubsan -lpthread -ldl 
  \
 
 Hopefully with my pending patch you can remove the -lpthread -ldl again, but
 ok for now.

Cool.

  +  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (gsi))
  +   {
  + gimple stmt = gsi_stmt (gsi);
  +
  + if (gimple_code (stmt) != GIMPLE_CALL)
 
 if (is_gimple_call (stmt))

Fixed.
 
 Ok with those changes.

Thanks.  Also I'll have to add some headers after the gimple.h reorg,
but that is an obvious change.

Marek


Re: [PATCH] Implement -fsanitize=null + new sanopt pass

2013-11-18 Thread Marek Polacek
On Mon, Nov 18, 2013 at 02:52:34PM +0100, Jakub Jelinek wrote:
 Oh, one more thing, please update gcc/doc/, the -fsanitize= description is
 far from up to date there.

Ok, the following (incremental) hopefully improves the docs.  Joseph, would
you mind having a look at this?  Thanks,

2013-11-18  Marek Polacek  pola...@redhat.com

* doc/invoke.texi: Extend -fsanitize=undefined documentation.

--- gcc/doc/invoke.texi.mp3 2013-11-18 15:57:47.104103101 +0100
+++ gcc/doc/invoke.texi 2013-11-18 17:08:51.305594441 +0100
@@ -5260,9 +5260,45 @@ data race bugs.
 See @uref{http://code.google.com/p/data-race-test/wiki/ThreadSanitizer} for 
more details.
 
 @item -fsanitize=undefined
-Enable UndefinedBehaviorSanitizer, a fast undefined behavior detector
+Enable UndefinedBehaviorSanitizer, a fast undefined behavior detector.
 Various computations will be instrumented to detect undefined behavior
-at runtime, e.g.@: division by zero or various overflows.
+at runtime.  Current suboptions are:
+
+@itemize @bullet
+
+@item @option{-fsanitize=shift}
+
+This option enables checking that the result of a shift operation is
+not undefined.  Note that what exactly is considered undefined differs
+slightly between C and C++, as well as between ANSI C and C99, etc.
+
+@item @option{-fsanitize=integer-divide-by-zero}
+
+Detect integer division by zero as well as @code{INT_MIN / -1} division.
+Note that the latter is only made undefined from C99 onwards.
+
+@item @option{-fsanitize=unreachable}
+
+With this option, the compiler will turn the @code{__builtin_unreachable}
+call into a diagnostics message call instead.  When reaching the
+@code{__builtin_unreachable} call, the behavior is undefined.
+
+@item @option{-fsanitize=vla-bound}
+
+This option instructs the compiler to check that the size of a variable
+length array is positive.  This option does not have any effect in
+@option{-std=c++1y} mode, as the standard requires the exception be thrown
+instead.
+
+@item @option{-fsanitize=null}
+
+This option enables pointer checking.  Particularly, the application
+built with this option turned on will issue an error message when it
+tries to dereference a NULL pointer, or if a reference (possibly an
+rvalue reference) is bound to a NULL pointer.
+
+@end itemize
+
 While @option{-ftrapv} causes traps for signed overflows to be emitted,
 @option{-fsanitize=undefined} gives a diagnostic message.
 This currently works only for the C family of languages.

Marek


Re: [PATCH] Implement -fsanitize=null + new sanopt pass

2013-11-18 Thread Joseph S. Myers
On Mon, 18 Nov 2013, Marek Polacek wrote:

 +@item @option{-fsanitize=shift}
 +
 +This option enables checking that the result of a shift operation is
 +not undefined.  Note that what exactly is considered undefined differs
 +slightly between C and C++, as well as between ANSI C and C99, etc.

We generally refer to ISO C90, not ANSI C.

 +Detect integer division by zero as well as @code{INT_MIN / -1} division.
 +Note that the latter is only made undefined from C99 onwards.

INT_MIN / -1 is unambiguously undefined in C90 - it's a signed arithmetic 
overflow (result not within the range of its type).  It's INT_MIN % -1 
where there's more ambiguity, but I consider the wording changes in C11 as 
a defect correction that should be applied back to C90.  (A comment on 
what the semantics should be, not on whether the documentation accurately 
reflects the code.)

-- 
Joseph S. Myers
jos...@codesourcery.com


Re: [PATCH] Implement -fsanitize=null + new sanopt pass

2013-11-18 Thread Marek Polacek
On Mon, Nov 18, 2013 at 04:58:36PM +, Joseph S. Myers wrote:
 On Mon, 18 Nov 2013, Marek Polacek wrote:
 
  +@item @option{-fsanitize=shift}
  +
  +This option enables checking that the result of a shift operation is
  +not undefined.  Note that what exactly is considered undefined differs
  +slightly between C and C++, as well as between ANSI C and C99, etc.
 
 We generally refer to ISO C90, not ANSI C.

Fixed.
 
  +Detect integer division by zero as well as @code{INT_MIN / -1} division.
  +Note that the latter is only made undefined from C99 onwards.
 
 INT_MIN / -1 is unambiguously undefined in C90 - it's a signed arithmetic 
 overflow (result not within the range of its type).  It's INT_MIN % -1 
 where there's more ambiguity, but I consider the wording changes in C11 as 
 a defect correction that should be applied back to C90.  (A comment on 
 what the semantics should be, not on whether the documentation accurately 
 reflects the code.)

I removed that sentence to not confuse readers.  (We issue runtime
error for INT_MIN % -1 for all c90, c99, c11 modes.)  Thanks.

Ok now?

2013-11-18  Marek Polacek  pola...@redhat.com

* doc/invoke.texi: Extend -fsanitize=undefined documentation.

--- gcc/doc/invoke.texi.mp3 2013-11-18 15:57:47.104103101 +0100
+++ gcc/doc/invoke.texi 2013-11-18 18:55:00.178009402 +0100
@@ -5260,9 +5260,44 @@ data race bugs.
 See @uref{http://code.google.com/p/data-race-test/wiki/ThreadSanitizer} for 
more details.
 
 @item -fsanitize=undefined
-Enable UndefinedBehaviorSanitizer, a fast undefined behavior detector
+Enable UndefinedBehaviorSanitizer, a fast undefined behavior detector.
 Various computations will be instrumented to detect undefined behavior
-at runtime, e.g.@: division by zero or various overflows.
+at runtime.  Current suboptions are:
+
+@itemize @bullet
+
+@item @option{-fsanitize=shift}
+
+This option enables checking that the result of a shift operation is
+not undefined.  Note that what exactly is considered undefined differs
+slightly between C and C++, as well as between ISO C90 and C99, etc.
+
+@item @option{-fsanitize=integer-divide-by-zero}
+
+Detect integer division by zero as well as @code{INT_MIN / -1} division.
+
+@item @option{-fsanitize=unreachable}
+
+With this option, the compiler will turn the @code{__builtin_unreachable}
+call into a diagnostics message call instead.  When reaching the
+@code{__builtin_unreachable} call, the behavior is undefined.
+
+@item @option{-fsanitize=vla-bound}
+
+This option instructs the compiler to check that the size of a variable
+length array is positive.  This option does not have any effect in
+@option{-std=c++1y} mode, as the standard requires the exception be thrown
+instead.
+
+@item @option{-fsanitize=null}
+
+This option enables pointer checking.  Particularly, the application
+built with this option turned on will issue an error message when it
+tries to dereference a NULL pointer, or if a reference (possibly an
+rvalue reference) is bound to a NULL pointer.
+
+@end itemize
+
 While @option{-ftrapv} causes traps for signed overflows to be emitted,
 @option{-fsanitize=undefined} gives a diagnostic message.
 This currently works only for the C family of languages.

Marek


Re: [PATCH] Implement -fsanitize=null + new sanopt pass

2013-11-18 Thread Joseph S. Myers
On Mon, 18 Nov 2013, Marek Polacek wrote:

 2013-11-18  Marek Polacek  pola...@redhat.com
 
   * doc/invoke.texi: Extend -fsanitize=undefined documentation.

OK.

-- 
Joseph S. Myers
jos...@codesourcery.com


Re: [PATCH] Implement -fsanitize=null + new sanopt pass

2013-11-13 Thread Marek Polacek
On Wed, Nov 13, 2013 at 06:45:06AM +0100, Markus Trippelsdorf wrote:
 On 2013.11.13 at 00:13 +0100, Marek Polacek wrote:
  2) bootstrap-ubsan almost passes, but the bootstrap fails when building
 all-fixincludes.  The problem here is that libiberty.a is built
 with -fsanitize=undefined, but fixincludes, when linking,
 don't link libubsan in.  My attemps to tweak
 FIXINC_CFLAGS/LDFLAGS/BOOT_LDFLAGS and whatnot weren't successfull.
 
 I'm using the following patch locally as a part to enable
 slim-lto-bootstrap. Maybe it helps in your case, too?

Unfortunately, doesn't seem to help :(.  Thanks anyway.

Marek


Re: [PATCH] Implement -fsanitize=null + new sanopt pass

2013-11-12 Thread Markus Trippelsdorf
On 2013.11.13 at 00:13 +0100, Marek Polacek wrote:
 2) bootstrap-ubsan almost passes, but the bootstrap fails when building
all-fixincludes.  The problem here is that libiberty.a is built
with -fsanitize=undefined, but fixincludes, when linking,
don't link libubsan in.  My attemps to tweak
FIXINC_CFLAGS/LDFLAGS/BOOT_LDFLAGS and whatnot weren't successfull.

I'm using the following patch locally as a part to enable
slim-lto-bootstrap. Maybe it helps in your case, too?

diff --git a/Makefile.in b/Makefile.in
index f9e8e0d5cb79..5db913fa0b8d 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -2930,6 +2930,7 @@ configure-build-fixincludes:
test ! -f $(BUILD_SUBDIR)/fixincludes/Makefile || exit 0; \
$(SHELL) $(srcdir)/mkinstalldirs $(BUILD_SUBDIR)/fixincludes ; \
$(BUILD_EXPORTS)  \
+   CFLAGS=$(STAGE_CFLAGS); export CFLAGS; \
echo Configuring in $(BUILD_SUBDIR)/fixincludes; \
cd $(BUILD_SUBDIR)/fixincludes || exit 1; \
case $(srcdir) in \
@@ -2965,6 +2966,7 @@ all-build-fixincludes: configure-build-fixincludes
$(BUILD_EXPORTS)  \
(cd $(BUILD_SUBDIR)/fixincludes  \
  $(MAKE) $(BASE_FLAGS_TO_PASS) $(EXTRA_BUILD_FLAGS)   \
+   CFLAGS=$(STAGE_CFLAGS) \
$(TARGET-build-fixincludes))
 @endif build-fixincludes
 
@@ -7813,6 +7815,7 @@ configure-fixincludes:
test ! -f $(HOST_SUBDIR)/fixincludes/Makefile || exit 0; \
$(SHELL) $(srcdir)/mkinstalldirs $(HOST_SUBDIR)/fixincludes ; \
$(HOST_EXPORTS)  \
+   CFLAGS=$(STAGE_CFLAGS); export CFLAGS; \
echo Configuring in $(HOST_SUBDIR)/fixincludes; \
cd $(HOST_SUBDIR)/fixincludes || exit 1; \
case $(srcdir) in \
@@ -7847,6 +7850,7 @@ all-fixincludes: configure-fixincludes
$(HOST_EXPORTS)  \
(cd $(HOST_SUBDIR)/fixincludes  \
  $(MAKE) $(BASE_FLAGS_TO_PASS) $(EXTRA_HOST_FLAGS) 
$(STAGE1_FLAGS_TO_PASS)  \
+   CFLAGS=$(STAGE_CFLAGS) \
$(TARGET-fixincludes))
 @endif fixincludes
 

-- 
Markus