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 <wschm...@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. 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; +}