| 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. Avoid the use of red zones by avoiding |
| 7 |
leaf functions. */ |
| 8 |
|
| 9 |
const char test_string[] = "\x55\xde\xad\xbe\xef\xc0\x1d\xca\xfe\x55\xaa"; |
| 10 |
|
| 11 |
/* Pad before and after the string on the stack, so that it's not overwritten by |
| 12 |
regular stack use. */ |
| 13 |
#define PAD 7 |
| 14 |
|
| 15 |
static inline __attribute__ ((__always_inline__, __strub__ ("callable"))) |
| 16 |
char * |
| 17 |
leak_string (void) |
| 18 |
{ |
| 19 |
/* We use this variable to avoid any stack red zone. Stack scrubbing covers |
| 20 |
it, but __builtin_stack_address, that we take as a reference, doesn't, so |
| 21 |
if e.g. callable() were to store the string in the red zone, we wouldn't |
| 22 |
find it because it would be outside the range we searched. */ |
| 23 |
typedef void __attribute__ ((__strub__ ("callable"))) callable_t (char *); |
| 24 |
callable_t *f = 0; |
| 25 |
|
| 26 |
char s[2 * PAD + 1][sizeof (test_string)]; |
| 27 |
__builtin_strcpy (s[PAD], test_string); |
| 28 |
asm ("" : "+m" (s), "+r" (f)); |
| 29 |
|
| 30 |
if (__builtin_expect (!f, 1)) |
| 31 |
return (char *) __builtin_stack_address (); |
| 32 |
|
| 33 |
f (s[PAD]); |
| 34 |
return 0; |
| 35 |
} |
| 36 |
|
| 37 |
static inline __attribute__ ((__always_inline__)) |
| 38 |
int |
| 39 |
look_for_string (char *e) |
| 40 |
{ |
| 41 |
char *p = (char *) __builtin_stack_address (); |
| 42 |
|
| 43 |
if (p == e) |
| 44 |
__builtin_abort (); |
| 45 |
|
| 46 |
if (p > e) |
| 47 |
{ |
| 48 |
char *q = p; |
| 49 |
p = e; |
| 50 |
e = q; |
| 51 |
} |
| 52 |
|
| 53 |
for (char *re = e - sizeof (test_string); p < re; p++) |
| 54 |
for (int i = 0; p[i] == test_string[i]; i++) |
| 55 |
if (i == sizeof (test_string) - 1) |
| 56 |
return i; |
| 57 |
|
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
static __attribute__ ((__noinline__, __noclone__)) |
| 62 |
char * |
| 63 |
callable () |
| 64 |
{ |
| 65 |
return leak_string (); |
| 66 |
} |
| 67 |
|
| 68 |
static __attribute__ ((__strub__ ("at-calls"))) |
| 69 |
char * |
| 70 |
at_calls () |
| 71 |
{ |
| 72 |
return leak_string (); |
| 73 |
} |
| 74 |
|
| 75 |
static __attribute__ ((__strub__ ("internal"))) |
| 76 |
char * |
| 77 |
internal () |
| 78 |
{ |
| 79 |
return leak_string (); |
| 80 |
} |
| 81 |
|
| 82 |
int main () |
| 83 |
{ |
| 84 |
/* Since these test check stack contents above the top of the stack, an |
| 85 |
unexpected asynchronous signal or interrupt might overwrite the bits we |
| 86 |
expect to find and cause spurious fails. Tolerate one such overall |
| 87 |
spurious fail by retrying. */ |
| 88 |
int i = 1; |
| 89 |
while (!look_for_string (callable ())) |
| 90 |
if (!i--) __builtin_abort (); |
| 91 |
while (look_for_string (at_calls ())) |
| 92 |
if (!i--) __builtin_abort (); |
| 93 |
while (look_for_string (internal ())) |
| 94 |
if (!i--) __builtin_abort (); |
| 95 |
__builtin_exit (0); |
| 96 |
} |