diff mbox series

[rs6000] Executable tests for -msafe-indirect-jumps

Message ID c549b468-78a1-b5e8-e59d-6f85680f575c@linux.vnet.ibm.com
State New
Headers show
Series [rs6000] Executable tests for -msafe-indirect-jumps | expand

Commit Message

Bill Schmidt Jan. 14, 2018, 5:34 p.m. UTC
Hi,

It was pointed out off-list that I should add some executable tests for
the new -msafe-indirect-jumps implementation.  This patch adds three
such tests to demonstrate correct behavior.

Tested on powerpc64-linux-gnu and powerpc64le-linux-gnu.  Are these tests
okay for trunk after the other patch is approved?

Thanks,
Bill


2018-01-14  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>

	* gcc.target/powerpc/safe-indirect-jump-4.c: New file.
	* gcc.target/powerpc/safe-indirect-jump-5.c: New file.
	* gcc.target/powerpc/safe-indirect-jump-6.c: New file.

Comments

Segher Boessenkool Jan. 15, 2018, 5:05 p.m. UTC | #1
Hi!

On Sun, Jan 14, 2018 at 11:34:06AM -0600, Bill Schmidt wrote:
> It was pointed out off-list that I should add some executable tests for
> the new -msafe-indirect-jumps implementation.  This patch adds three
> such tests to demonstrate correct behavior.
> 
> Tested on powerpc64-linux-gnu and powerpc64le-linux-gnu.  Are these tests
> okay for trunk after the other patch is approved?

These look fine, so sure.  One nit:

> --- gcc/testsuite/gcc.target/powerpc/safe-indirect-jump-4.c	(nonexistent)
> +++ gcc/testsuite/gcc.target/powerpc/safe-indirect-jump-4.c	(working copy)
> @@ -0,0 +1,25 @@
> +/* { dg-do run { target { powerpc64le-*-* } } } */
> +/* { dg-additional-options "-msafe-indirect-jumps" } */

You could as well run all these tests on powerpc*-*-* as far as I see?
Or does that -m error if there is no "safe" implementation for the current
target?


Segher
Bill Schmidt Jan. 15, 2018, 5:09 p.m. UTC | #2
On Jan 15, 2018, at 11:05 AM, Segher Boessenkool <segher@kernel.crashing.org> wrote:
> 
> Hi!
> 
> On Sun, Jan 14, 2018 at 11:34:06AM -0600, Bill Schmidt wrote:
>> It was pointed out off-list that I should add some executable tests for
>> the new -msafe-indirect-jumps implementation.  This patch adds three
>> such tests to demonstrate correct behavior.
>> 
>> Tested on powerpc64-linux-gnu and powerpc64le-linux-gnu.  Are these tests
>> okay for trunk after the other patch is approved?
> 
> These look fine, so sure.  One nit:
> 
>> --- gcc/testsuite/gcc.target/powerpc/safe-indirect-jump-4.c	(nonexistent)
>> +++ gcc/testsuite/gcc.target/powerpc/safe-indirect-jump-4.c	(working copy)
>> @@ -0,0 +1,25 @@
>> +/* { dg-do run { target { powerpc64le-*-* } } } */
>> +/* { dg-additional-options "-msafe-indirect-jumps" } */
> 
> You could as well run all these tests on powerpc*-*-* as far as I see?
> Or does that -m error if there is no "safe" implementation for the current
> target?

Ah, yes, certainly.  The compile-only tests can't, but the execution ones can.
I'll fix that in the next revision.  Thanks!

Bill
> 
> 
> Segher
>
diff mbox series

Patch

Index: gcc/testsuite/gcc.target/powerpc/safe-indirect-jump-4.c
===================================================================
--- gcc/testsuite/gcc.target/powerpc/safe-indirect-jump-4.c	(nonexistent)
+++ gcc/testsuite/gcc.target/powerpc/safe-indirect-jump-4.c	(working copy)
@@ -0,0 +1,25 @@ 
+/* { dg-do run { target { powerpc64le-*-* } } } */
+/* { dg-additional-options "-msafe-indirect-jumps" } */
+
+/* Test for deliberate misprediction of indirect calls for ELFv2.  */
+
+int (*f)();
+
+int __attribute__((noinline)) bar ()
+{
+  return (*f) ();
+}
+
+int g ()
+{
+  return 26;
+}
+
+int main ()
+{
+  f = &g;
+  if (bar () != 26)
+    __builtin_abort ();
+
+  return 0;
+}
Index: gcc/testsuite/gcc.target/powerpc/safe-indirect-jump-5.c
===================================================================
--- gcc/testsuite/gcc.target/powerpc/safe-indirect-jump-5.c	(nonexistent)
+++ gcc/testsuite/gcc.target/powerpc/safe-indirect-jump-5.c	(working copy)
@@ -0,0 +1,55 @@ 
+/* { dg-do run { target { powerpc*-*-* } } } */
+/* { dg-additional-options "-msafe-indirect-jumps -Wno-pedantic" } */
+
+/* Test for deliberate misprediction of computed goto.  */
+
+int __attribute__((noinline)) bar (int i)
+{
+  return 1960 + i;
+}
+
+int __attribute__((noinline)) baz (int i)
+{
+  return i * i;
+}
+
+int __attribute__((noinline)) spaz (int i)
+{
+  return i + 1;
+}
+
+int foo (int x)
+{
+  static void *labptr[] = { &&lab0, &&lab1, &&lab2 };
+
+  if (x < 0 || x > 2)
+    return -1;
+
+  goto *labptr[x];
+
+ lab0:
+  return bar (x);
+
+ lab1:
+  return baz (x) + 1;
+
+ lab2:
+  return spaz (x) / 2;
+}
+
+int main ()
+{
+  if (foo (0) != 1960)
+    __builtin_abort ();
+
+  if (foo (1) != 2)
+    __builtin_abort ();
+
+  if (foo (2) != 1)
+    __builtin_abort ();
+
+  if (foo (3) != -1)
+    __builtin_abort ();
+
+  return 0;
+}
Index: gcc/testsuite/gcc.target/powerpc/safe-indirect-jump-6.c
===================================================================
--- gcc/testsuite/gcc.target/powerpc/safe-indirect-jump-6.c	(nonexistent)
+++ gcc/testsuite/gcc.target/powerpc/safe-indirect-jump-6.c	(working copy)
@@ -0,0 +1,80 @@ 
+/* { dg-do run { target { powerpc*-*-* } } } */
+/* { dg-additional-options "-msafe-indirect-jumps" } */
+
+/* Test for deliberate misprediction of jump tables.  */
+
+void __attribute__((noinline)) bar ()
+{
+}
+
+int foo (int x)
+{
+  int a;
+  
+  switch (x)
+    {
+    default:
+      a = -1;
+      break;
+    case 0:
+      a = x * x + 3;
+      break;
+    case 1:
+      a = x + 1;
+      break;
+    case 2:
+      a = x + x;
+      break;
+    case 3:
+      a = x << 3;
+      break;
+    case 4:
+      a = x >> 1;
+      break;
+    case 5:
+      a = x;
+      break;
+    case 6:
+      a = 0;
+      break;
+    case 7:
+      a = x * x + x;
+      break;
+    }
+
+  bar();
+
+  return a;
+}
+
+int main ()
+{
+  if (foo (0) != 3)
+    __builtin_abort ();
+  
+  if (foo (1) != 2)
+    __builtin_abort ();
+  
+  if (foo (2) != 4)
+    __builtin_abort ();
+  
+  if (foo (3) != 24)
+    __builtin_abort ();
+  
+  if (foo (4) != 2)
+    __builtin_abort ();
+  
+  if (foo (5) != 5)
+    __builtin_abort ();
+  
+  if (foo (6) != 0)
+    __builtin_abort ();
+  
+  if (foo (7) != 56)
+    __builtin_abort ();
+  
+  if (foo (8) != -1)
+    __builtin_abort ();
+  
+  return 0;
+}