diff mbox

Add __int128 test to ubsan

Message ID 20131217171701.GW11710@redhat.com
State New
Headers show

Commit Message

Marek Polacek Dec. 17, 2013, 5:17 p.m. UTC
Regtested on x86_64-linux with -m32/-m64.

Ok?

2013-12-17  Marek Polacek  <polacek@redhat.com>

testsuite/
	* c-c++-common/ubsan/overflow-int128.c: New test.


	Marek

Comments

Jakub Jelinek Dec. 17, 2013, 5:50 p.m. UTC | #1
On Tue, Dec 17, 2013 at 06:17:01PM +0100, Marek Polacek wrote:
> Regtested on x86_64-linux with -m32/-m64.
> 
> Ok?
> 
> 2013-12-17  Marek Polacek  <polacek@redhat.com>
> 
> testsuite/
> 	* c-c++-common/ubsan/overflow-int128.c: New test.
> 
> --- gcc/testsuite/c-c++-common/ubsan/overflow-int128.c.mp	2013-12-17 16:54:28.123468111 +0100
> +++ gcc/testsuite/c-c++-common/ubsan/overflow-int128.c	2013-12-17 18:07:19.539221035 +0100
> @@ -0,0 +1,48 @@
> +/* { dg-do run } */
> +/* { dg-require-effective-target int128 } */
> +/* { dg-options "-fsanitize=signed-integer-overflow -Wno-overflow" } */
> +/* { dg-skip-if "" { *-*-* } { "-flto" } { "" } } */
> +
> +/* 2^127 - 1 */
> +#define INT128_MAX (((__int128) 1 << ((__SIZEOF_INT128__ * __CHAR_BIT__) - 1)) - 1)

Isn't this undefined behavior in C?  I mean, shouldn't you
shift up (unsigned __int128) 1 and only cast to (__int128) at the end?

	Jakub
diff mbox

Patch

--- gcc/testsuite/c-c++-common/ubsan/overflow-int128.c.mp	2013-12-17 16:54:28.123468111 +0100
+++ gcc/testsuite/c-c++-common/ubsan/overflow-int128.c	2013-12-17 18:07:19.539221035 +0100
@@ -0,0 +1,48 @@ 
+/* { dg-do run } */
+/* { dg-require-effective-target int128 } */
+/* { dg-options "-fsanitize=signed-integer-overflow -Wno-overflow" } */
+/* { dg-skip-if "" { *-*-* } { "-flto" } { "" } } */
+
+/* 2^127 - 1 */
+#define INT128_MAX (((__int128) 1 << ((__SIZEOF_INT128__ * __CHAR_BIT__) - 1)) - 1)
+#define INT128_MIN (-INT128_MAX - 1)
+
+int
+main (void)
+{
+  volatile __int128 i = INT128_MAX;
+  volatile __int128 j = 1;
+  volatile __int128 k = i + j;
+  k = j + i;
+  i++;
+  j = INT128_MAX - 100;
+  j += (1 << 10);
+
+  j = INT128_MIN;
+  i = -1;
+  k = i + j;
+  k = j + i;
+  j--;
+  j = INT128_MIN + 100;
+  j += -(1 << 10);
+
+  i = INT128_MAX;
+  j = 2;
+  k = i * j;
+
+  i = INT128_MIN;
+  i = -i;
+
+  return 0;
+}
+
+/* { dg-output "signed integer overflow: 0x7fffffffffffffffffffffffffffffff \\+ 1 cannot be represented in type '__int128'(\n|\r\n|\r)" } */
+/* { dg-output "\[^\n\r]*signed integer overflow: 1 \\+ 0x7fffffffffffffffffffffffffffffff cannot be represented in type '__int128'(\n|\r\n|\r)" } */
+/* { dg-output "\[^\n\r]*signed integer overflow: 0x7fffffffffffffffffffffffffffffff \\+ 1 cannot be represented in type '__int128'(\n|\r\n|\r)" } */
+/* { dg-output "\[^\n\r]*signed integer overflow: 0x7fffffffffffffffffffffffffffff9b \\+ 1024 cannot be represented in type '__int128'(\n|\r\n|\r)" } */
+/* { dg-output "\[^\n\r]*signed integer overflow: -1 \\+ 0x80000000000000000000000000000000 cannot be represented in type '__int128'(\n|\r\n|\r)" } */
+/* { dg-output "\[^\n\r]*signed integer overflow: 0x80000000000000000000000000000000 \\+ -1 cannot be represented in type '__int128'(\n|\r\n|\r)" } */
+/* { dg-output "\[^\n\r]*signed integer overflow: 0x80000000000000000000000000000000 \\+ -1 cannot be represented in type '__int128'(\n|\r\n|\r)" } */
+/* { dg-output "\[^\n\r]*signed integer overflow: 0x80000000000000000000000000000064 \\+ -1024 cannot be represented in type '__int128'(\n|\r\n|\r)" } */
+/* { dg-output "\[^\n\r]*signed integer overflow: 0x7fffffffffffffffffffffffffffffff \\* 2 cannot be represented in type '__int128'(\n|\r\n|\r)" } */
+/* { dg-output "\[^\n\r]*negation of 0x80000000000000000000000000000000 cannot be represented in type '__int128'; cast to an unsigned type to negate this value to itself(\n|\r\n|\r)" } */