diff mbox

Make some ubsan tests more robust

Message ID 20140324093746.GP6523@redhat.com
State New
Headers show

Commit Message

Marek Polacek March 24, 2014, 9:37 a.m. UTC
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  <polacek@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.


	Marek

Comments

Jakub Jelinek March 24, 2014, 9:43 a.m. UTC | #1
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  <polacek@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
diff mbox

Patch

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 "-fsanitize=signed-integer-overflow" } */
 
+#include <stdio.h>
+
 __attribute__((noinline, noclone)) long long
 mul (long long x, long long y)
 {
@@ -29,10 +31,16 @@  long long tab[] = {
 int
 main ()
 {
+  fputs ("UBSAN TEST START\n", stderr);
+
   unsigned int i;
   for (i = 0; i < sizeof (tab) / sizeof (long long); i += 3)
     if (mul (tab[i], tab[i + 1]) != tab[i + 2]
         || mul (tab[i + 1], tab[i]) != tab[i + 2])
       __builtin_abort ();
+
+  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-negate-2.c gcc/testsuite/c-c++-common/ubsan/overflow-negate-2.c
index 9a711c2..6a4f288 100644
--- gcc/testsuite/c-c++-common/ubsan/overflow-negate-2.c
+++ gcc/testsuite/c-c++-common/ubsan/overflow-negate-2.c
@@ -1,6 +1,8 @@ 
 /* { dg-do run } */
 /* { dg-options "-fsanitize=signed-integer-overflow -Wno-unused-variable" } */
 
+#include <stdio.h>
+
 #define SCHAR_MIN (-__SCHAR_MAX__ - 1)
 #define SHRT_MIN (-__SHRT_MAX__ - 1)
 #define INT_MIN (-__INT_MAX__ - 1)
@@ -12,6 +14,8 @@ 
 int
 main (void)
 {
+  fputs ("UBSAN TEST START\n", stderr);
+
   volatile signed char c = -SCHAR_MIN;
   CHECK (c, -128);
 
@@ -34,5 +38,8 @@  main (void)
   lli = -(unsigned long long) lli;
   CHECK (lli, -0x8000000000000000L);
 
+  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-sub-1.c gcc/testsuite/c-c++-common/ubsan/overflow-sub-1.c
index 2dcf16d..15f0445 100644
--- gcc/testsuite/c-c++-common/ubsan/overflow-sub-1.c
+++ gcc/testsuite/c-c++-common/ubsan/overflow-sub-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 SCHAR_MIN (-__SCHAR_MAX__ - 1)
 #define SHRT_MAX __SHRT_MAX__
@@ -18,6 +20,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 i = -1;
@@ -58,5 +62,8 @@  main (void)
   check (d, 32767);
 #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/pr59503.c gcc/testsuite/c-c++-common/ubsan/pr59503.c
index 12fb4c2..1637f01 100644
--- gcc/testsuite/c-c++-common/ubsan/pr59503.c
+++ gcc/testsuite/c-c++-common/ubsan/pr59503.c
@@ -1,13 +1,21 @@ 
 /* { dg-do run } */
 /* { dg-options "-fsanitize=signed-integer-overflow" } */
 
+#include <stdio.h>
+
 int
 main (void)
 {
+  fputs ("UBSAN TEST START\n", stderr);
+
   long long int a = 14;
   long int b = 9;
   asm volatile ("" : "+r" (a), "+r" (b));
   if ((a - b) != 5)
     __builtin_abort ();
+
+  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/pr60613-1.c gcc/testsuite/c-c++-common/ubsan/pr60613-1.c
index 2161e0b..6794532 100644
--- gcc/testsuite/c-c++-common/ubsan/pr60613-1.c
+++ gcc/testsuite/c-c++-common/ubsan/pr60613-1.c
@@ -2,6 +2,8 @@ 
 /* { dg-do run } */
 /* { dg-options "-fsanitize=undefined" } */
 
+#include <stdio.h>
+
 long long y;
 
 __attribute__((noinline, noclone)) long long
@@ -24,10 +26,16 @@  bar (long long x)
 int
 main ()
 {
+  fputs ("UBSAN TEST START\n", stderr);
+
   y = 1;
   if (foo (8 - 2040) != 8 - 1)
     __builtin_abort ();
   if (bar (1) != 8 - 1)
     __builtin_abort ();
+
+  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/save-expr-1.c gcc/testsuite/c-c++-common/ubsan/save-expr-1.c
index 24532e8..19b1eb0 100644
--- gcc/testsuite/c-c++-common/ubsan/save-expr-1.c
+++ gcc/testsuite/c-c++-common/ubsan/save-expr-1.c
@@ -1,11 +1,19 @@ 
 /* { dg-do compile } */
 /* { dg-options "-fsanitize=shift -Wall -Werror -O" } */
 
+#include <stdio.h>
+
 static int x;
 int
 main (void)
 {
+  fputs ("UBSAN TEST START\n", stderr);
+
   int o = 1;
   int y = x << o;
+
+  fputs ("UBSAN TEST END\n", stderr);
   return y;
 }
+
+/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */
diff --git gcc/testsuite/c-c++-common/ubsan/shift-3.c gcc/testsuite/c-c++-common/ubsan/shift-3.c
index c639d17..65ee5d8 100644
--- gcc/testsuite/c-c++-common/ubsan/shift-3.c
+++ gcc/testsuite/c-c++-common/ubsan/shift-3.c
@@ -1,11 +1,19 @@ 
 /* { dg-do run } */
 /* { dg-options "-fsanitize=shift -w" } */
 
+#include <stdio.h>
+
 int
 main (void)
 {
+  fputs ("UBSAN TEST START\n", stderr);
+
   unsigned int a = 1;
   a <<= 31;
   a <<= 1;
+
+  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/shift-6.c gcc/testsuite/c-c++-common/ubsan/shift-6.c
index a0e2e20..d88a429 100644
--- gcc/testsuite/c-c++-common/ubsan/shift-6.c
+++ gcc/testsuite/c-c++-common/ubsan/shift-6.c
@@ -2,10 +2,14 @@ 
 /* { dg-do run { target int32plus } } */
 /* { dg-options "-fsanitize=shift -w" } */
 
+#include <stdio.h>
+
 int x = 7;
 int
 main (void)
 {
+  fputs ("UBSAN TEST START\n", stderr);
+
   /* All of the following should pass.  */
   int A[128 >> 5] = {};
   int B[128 << 5] = {};
@@ -26,5 +30,9 @@  main (void)
     case 128 >> (4 + 1):
       return 1;
     }
+
+  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/undefined-1.c gcc/testsuite/c-c++-common/ubsan/undefined-1.c
index 2458fcb..d1b9ce7 100644
--- gcc/testsuite/c-c++-common/ubsan/undefined-1.c
+++ gcc/testsuite/c-c++-common/ubsan/undefined-1.c
@@ -1,6 +1,8 @@ 
 /* { dg-do run } */
 /* { dg-options "-fsanitize=undefined" } */
 
+#include <stdio.h>
+
 int
 foo (int x, int y)
 {
@@ -19,7 +21,13 @@  bar (int x, int y)
 int
 main (void)
 {
+  fputs ("UBSAN TEST START\n", stderr);
+
   foo (3, 2);
   bar (12, 42);
+
+  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/vla-2.c gcc/testsuite/c-c++-common/ubsan/vla-2.c
index c62ced9..a11e4e7 100644
--- gcc/testsuite/c-c++-common/ubsan/vla-2.c
+++ gcc/testsuite/c-c++-common/ubsan/vla-2.c
@@ -1,9 +1,13 @@ 
 /* { dg-do run } */
 /* { dg-options "-fsanitize=vla-bound -Wall -Wno-unused-variable" } */
 
+#include <stdio.h>
+
 int
 main (void)
 {
+  fputs ("UBSAN TEST START\n", stderr);
+
   const int t = 0;
   struct s {
     int x;
@@ -11,5 +15,8 @@  main (void)
     int g[t];
   };
 
+  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/vla-3.c gcc/testsuite/c-c++-common/ubsan/vla-3.c
index 5e10af0..7772857 100644
--- gcc/testsuite/c-c++-common/ubsan/vla-3.c
+++ gcc/testsuite/c-c++-common/ubsan/vla-3.c
@@ -1,6 +1,8 @@ 
 /* { dg-do run } */
 /* { dg-options "-fsanitize=vla-bound" } */
 
+#include <stdio.h>
+
 /* Don't instrument the arrays here.  */
 int
 foo (int n, int a[])
@@ -11,6 +13,13 @@  foo (int n, int a[])
 int
 main (void)
 {
+  fputs ("UBSAN TEST START\n", stderr);
+
   int a[6] = { };
-  return foo (3, a);
+  int ret = foo (3, a);
+
+  fputs ("UBSAN TEST END\n", stderr);
+  return ret;
 }
+
+/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */
diff --git gcc/testsuite/c-c++-common/ubsan/vla-4.c gcc/testsuite/c-c++-common/ubsan/vla-4.c
index 3f7dd1d..8a7bbac 100644
--- gcc/testsuite/c-c++-common/ubsan/vla-4.c
+++ gcc/testsuite/c-c++-common/ubsan/vla-4.c
@@ -1,13 +1,21 @@ 
 /* { dg-do run } */
 /* { dg-options "-fsanitize=vla-bound" } */
 
+#include <stdio.h>
+
 int
 main (void)
 {
+  fputs ("UBSAN TEST START\n", stderr);
+
   int x = 1;
   /* Check that the size of an array is evaluated only once.  */
   int a[++x];
   if (x != 2)
     __builtin_abort ();
+
+  fputs ("UBSAN TEST END\n", stderr);
   return 0;
 }
+
+/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */
diff --git gcc/testsuite/g++.dg/ubsan/cxx11-shift-1.C gcc/testsuite/g++.dg/ubsan/cxx11-shift-1.C
index 3e16892..f2b2cbd 100644
--- gcc/testsuite/g++.dg/ubsan/cxx11-shift-1.C
+++ gcc/testsuite/g++.dg/ubsan/cxx11-shift-1.C
@@ -1,9 +1,18 @@ 
 /* { dg-do run { target c++11 } } */
 /* { dg-options "-fsanitize=shift -w" } */
 
+#include <stdio.h>
+
 int
 main (void)
 {
+  fputs ("UBSAN TEST START\n", stderr);
+
   int a = 1;
   a <<= 31;
+
+  fputs ("UBSAN TEST END\n", stderr);
+  return 0;
 }
+
+/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */
diff --git gcc/testsuite/g++.dg/ubsan/return-2.C gcc/testsuite/g++.dg/ubsan/return-2.C
index c7380f03..546f178 100644
--- gcc/testsuite/g++.dg/ubsan/return-2.C
+++ gcc/testsuite/g++.dg/ubsan/return-2.C
@@ -1,6 +1,8 @@ 
 // { dg-do run }
 // { dg-options "-fsanitize=return" }
 
+#include <stdio.h>
+
 struct S { S (); ~S (); };
 
 S::S () {}
@@ -20,6 +22,12 @@  foo (int x)
 int
 main ()
 {
+  fputs ("UBSAN TEST START\n", stderr);
+
   foo (1);
   foo (14);
+
+  fputs ("UBSAN TEST END\n", stderr);
 }
+
+/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */