[PATCH] Make some ubsan tests more robust

2014-03-24 Thread Marek Polacek
dg-output as it stands can't be used in a negate form, that is we can't
check that no output was produced.  This is needed for ubsan: there
are testcases that when run should issue no output at all.  We can,
however, use this really ugly hack, where we output some fake stuff
at the beginning and at the end of main() and check that nothing was
issued in between.  And it's even uglier because I need to pull in
stdio.h because of stderr...

Fortunately in 5.0 this will not be needed, because I'll implement
the -fno-sanitize-recover option (then any runtime error causes the
program to abort).

Ran ubsan testsuite.  Ok for trunk?

2014-03-24  Marek Polacek  pola...@redhat.com

testsuite/
* c-c++-common/ubsan/div-by-zero-4.c: Don't include limits.h.  Define
INT_MIN.
* c-c++-common/ubsan/overflow-1.c: Check for unwanted output.
* c-c++-common/ubsan/overflow-add-1.c: Likewise.
* c-c++-common/ubsan/overflow-mul-1.c: Likewise.
* c-c++-common/ubsan/overflow-mul-3.c: Likewise.
* c-c++-common/ubsan/overflow-negate-2.c: Likewise.
* c-c++-common/ubsan/overflow-sub-1.c: Likewise.
* c-c++-common/ubsan/pr59503.c: Likewise.
* c-c++-common/ubsan/pr60613-1.c: Likewise.
* c-c++-common/ubsan/save-expr-1.c: Likewise.
* c-c++-common/ubsan/shift-3.c: Likewise.
* c-c++-common/ubsan/shift-6.c: Likewise.
* c-c++-common/ubsan/undefined-1.c: Likewise.
* c-c++-common/ubsan/vla-2.c: Likewise.
* c-c++-common/ubsan/vla-3.c: Likewise.
* c-c++-common/ubsan/vla-4.c: Likewise.
* g++.dg/ubsan/cxx11-shift-1.C: Likewise.
* g++.dg/ubsan/return-2.C: Likewise.

diff --git gcc/testsuite/c-c++-common/ubsan/div-by-zero-4.c 
gcc/testsuite/c-c++-common/ubsan/div-by-zero-4.c
index 295f624..02162e1 100644
--- gcc/testsuite/c-c++-common/ubsan/div-by-zero-4.c
+++ gcc/testsuite/c-c++-common/ubsan/div-by-zero-4.c
@@ -1,7 +1,7 @@
 /* { dg-do run } */
 /* { dg-options -fsanitize=integer-divide-by-zero -Wno-overflow } */
 
-#include limits.h
+#define INT_MIN (-__INT_MAX__ - 1)
 
 int
 main (void)
diff --git gcc/testsuite/c-c++-common/ubsan/overflow-1.c 
gcc/testsuite/c-c++-common/ubsan/overflow-1.c
index 8165463..6819955 100644
--- gcc/testsuite/c-c++-common/ubsan/overflow-1.c
+++ gcc/testsuite/c-c++-common/ubsan/overflow-1.c
@@ -1,6 +1,8 @@
 /* { dg-do run } */
 /* { dg-options -fsanitize=signed-integer-overflow } */
 
+#include stdio.h
+
 #ifndef ASM1
 # define ASM1(a) /* Nothing */
 #endif
@@ -51,6 +53,8 @@
 int
 main (void)
 {
+  fputs (UBSAN TEST START\n, stderr);
+
   CHECK (FN1 (char, char, +), 23);
   CHECK (FN1 (char, char, -), 5);
   CHECK (FN1 (char, char, *), 126);
@@ -258,5 +262,8 @@ main (void)
   CHECK (FN5 (long long int), -77);
   CHECK (FN5 (unsigned long long int), -77);
 
+  fputs (UBSAN TEST END\n, stderr);
   return 0;
 }
+
+/* { dg-output UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END } */
diff --git gcc/testsuite/c-c++-common/ubsan/overflow-add-1.c 
gcc/testsuite/c-c++-common/ubsan/overflow-add-1.c
index 3f4790b..fd6c6d3 100644
--- gcc/testsuite/c-c++-common/ubsan/overflow-add-1.c
+++ gcc/testsuite/c-c++-common/ubsan/overflow-add-1.c
@@ -1,6 +1,8 @@
 /* { dg-do run } */
 /* { dg-options -fsanitize=signed-integer-overflow -Wno-unused-variable } */
 
+#include stdio.h
+
 #define SCHAR_MAX __SCHAR_MAX__
 #define SHRT_MAX __SHRT_MAX__
 #define INT_MAX __INT_MAX__
@@ -16,6 +18,8 @@ check (int i, int j)
 int
 main (void)
 {
+  fputs (UBSAN TEST START\n, stderr);
+
 #if __INT_MAX__ == 2147483647
   /* Here, nothing should fail.  */
   volatile int j = INT_MAX;
@@ -56,5 +60,8 @@ main (void)
   check (d, -32768);
 #endif
 
+  fputs (UBSAN TEST END\n, stderr);
   return 0;
 }
+
+/* { dg-output UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END } */
diff --git gcc/testsuite/c-c++-common/ubsan/overflow-mul-1.c 
gcc/testsuite/c-c++-common/ubsan/overflow-mul-1.c
index bb355a2..afb1a25 100644
--- gcc/testsuite/c-c++-common/ubsan/overflow-mul-1.c
+++ gcc/testsuite/c-c++-common/ubsan/overflow-mul-1.c
@@ -1,6 +1,8 @@
 /* { dg-do run } */
 /* { dg-options -fsanitize=signed-integer-overflow -Wno-unused-variable } */
 
+#include stdio.h
+
 #define SCHAR_MAX __SCHAR_MAX__
 #define SHRT_MAX __SHRT_MAX__
 #define INT_MAX __INT_MAX__
@@ -16,6 +18,8 @@ check (int i, int j)
 int
 main (void)
 {
+  fputs (UBSAN TEST START\n, stderr);
+
   /* Test integer promotion.  */
 #if __SCHAR_MAX__ == 127
   volatile signed char a = -2;
@@ -42,5 +46,8 @@ main (void)
   check (o, INT_MIN);
 #endif
 
+  fputs (UBSAN TEST END\n, stderr);
   return 0;
 }
+
+/* { dg-output UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END } */
diff --git gcc/testsuite/c-c++-common/ubsan/overflow-mul-3.c 
gcc/testsuite/c-c++-common/ubsan/overflow-mul-3.c
index 49332dc..037609b 100644
--- gcc/testsuite/c-c++-common/ubsan/overflow-mul-3.c
+++ gcc/testsuite/c-c++-common/ubsan/overflow-mul-3.c
@@ -1,6 +1,8 @@
 /* { dg-do run } */
 /* { dg-options 

Re: [PATCH] Make some ubsan tests more robust

2014-03-24 Thread Jakub Jelinek
On Mon, Mar 24, 2014 at 10:37:46AM +0100, Marek Polacek wrote:
 dg-output as it stands can't be used in a negate form, that is we can't
 check that no output was produced.  This is needed for ubsan: there
 are testcases that when run should issue no output at all.  We can,
 however, use this really ugly hack, where we output some fake stuff
 at the beginning and at the end of main() and check that nothing was
 issued in between.  And it's even uglier because I need to pull in
 stdio.h because of stderr...
 
 Fortunately in 5.0 this will not be needed, because I'll implement
 the -fno-sanitize-recover option (then any runtime error causes the
 program to abort).
 
 Ran ubsan testsuite.  Ok for trunk?
 
 2014-03-24  Marek Polacek  pola...@redhat.com
 
 testsuite/
   * c-c++-common/ubsan/div-by-zero-4.c: Don't include limits.h.  Define
   INT_MIN.
   * c-c++-common/ubsan/overflow-1.c: Check for unwanted output.
   * c-c++-common/ubsan/overflow-add-1.c: Likewise.
   * c-c++-common/ubsan/overflow-mul-1.c: Likewise.
   * c-c++-common/ubsan/overflow-mul-3.c: Likewise.
   * c-c++-common/ubsan/overflow-negate-2.c: Likewise.
   * c-c++-common/ubsan/overflow-sub-1.c: Likewise.
   * c-c++-common/ubsan/pr59503.c: Likewise.
   * c-c++-common/ubsan/pr60613-1.c: Likewise.
   * c-c++-common/ubsan/save-expr-1.c: Likewise.
   * c-c++-common/ubsan/shift-3.c: Likewise.
   * c-c++-common/ubsan/shift-6.c: Likewise.
   * c-c++-common/ubsan/undefined-1.c: Likewise.
   * c-c++-common/ubsan/vla-2.c: Likewise.
   * c-c++-common/ubsan/vla-3.c: Likewise.
   * c-c++-common/ubsan/vla-4.c: Likewise.
   * g++.dg/ubsan/cxx11-shift-1.C: Likewise.
   * g++.dg/ubsan/return-2.C: Likewise.

Ok, thanks.

Jakub