| 1 |
/* { dg-do run } */ |
| 2 |
/* { dg-options "-fstrub=strict" } */ |
| 3 |
/* { dg-require-effective-target strub } */ |
| 4 |
|
| 5 |
/* Check that a non-strub function leaves a string behind in the stack, and that |
| 6 |
equivalent strub functions don't. */ |
| 7 |
|
| 8 |
const char test_string[] = "\x55\xde\xad\xbe\xef\xc0\x1d\xca\xfe\x55\xaa"; |
| 9 |
|
| 10 |
static inline __attribute__ ((__always_inline__, __strub__ ("callable"))) |
| 11 |
char * |
| 12 |
leak_string (void) |
| 13 |
{ |
| 14 |
int len = sizeof (test_string); |
| 15 |
char *s = (char *) __builtin_alloca (len); |
| 16 |
__builtin_strcpy (s, test_string); |
| 17 |
asm ("" : "+m" (s)); |
| 18 |
return (char *) __builtin_stack_address (); |
| 19 |
} |
| 20 |
|
| 21 |
static inline __attribute__ ((__always_inline__)) |
| 22 |
int |
| 23 |
look_for_string (char *e) |
| 24 |
{ |
| 25 |
char *p = (char *) __builtin_stack_address (); |
| 26 |
|
| 27 |
if (p == e) |
| 28 |
__builtin_abort (); |
| 29 |
|
| 30 |
if (p > e) |
| 31 |
{ |
| 32 |
char *q = p; |
| 33 |
p = e; |
| 34 |
e = q; |
| 35 |
} |
| 36 |
|
| 37 |
for (char *re = e - sizeof (test_string); p < re; p++) |
| 38 |
for (int i = 0; p[i] == test_string[i]; i++) |
| 39 |
if (i == sizeof (test_string) - 1) |
| 40 |
return i; |
| 41 |
|
| 42 |
return 0; |
| 43 |
} |
| 44 |
|
| 45 |
static __attribute__ ((__noinline__, __noclone__)) |
| 46 |
char * |
| 47 |
callable () |
| 48 |
{ |
| 49 |
return leak_string (); |
| 50 |
} |
| 51 |
|
| 52 |
static __attribute__ ((__strub__ ("at-calls"))) |
| 53 |
char * |
| 54 |
at_calls () |
| 55 |
{ |
| 56 |
return leak_string (); |
| 57 |
} |
| 58 |
|
| 59 |
static __attribute__ ((__strub__ ("internal"))) |
| 60 |
char * |
| 61 |
internal () |
| 62 |
{ |
| 63 |
return leak_string (); |
| 64 |
} |
| 65 |
|
| 66 |
int main () |
| 67 |
{ |
| 68 |
/* Since these test check stack contents above the top of the stack, an |
| 69 |
unexpected asynchronous signal or interrupt might overwrite the bits we |
| 70 |
expect to find and cause spurious fails. Tolerate one such overall |
| 71 |
spurious fail by retrying. */ |
| 72 |
int i = 1; |
| 73 |
while (!look_for_string (callable ())) |
| 74 |
if (!i--) __builtin_abort (); |
| 75 |
while (look_for_string (at_calls ())) |
| 76 |
if (!i--) __builtin_abort (); |
| 77 |
while (look_for_string (internal ())) |
| 78 |
if (!i--) __builtin_abort (); |
| 79 |
__builtin_exit (0); |
| 80 |
} |