Debian Codegraph @

gcc/testsuite/c-c++-common/torture/strub-run4.c

raw
1 /* { dg-do run } */
2 /* { dg-options "-fstrub=all" } */
3 /* { dg-require-effective-target strub } */
4
5 /* Check that multi-level, multi-inlined functions still get cleaned up as
6 expected, without overwriting temporary stack allocations while they should
7 still be available. */
8
9 #ifndef ATTR_STRUB_AT_CALLS
10 # define ATTR_STRUB_AT_CALLS /* Defined in strub-run4d.c. */
11 #endif
12
13 const char test_string[] = "\x55\xde\xad\xbe\xef\xc0\x1d\xca\xfe\x55\xaa";
14
15 static inline __attribute__ ((__always_inline__))
16 char *
17 leak_string (void)
18 {
19 int __attribute__ ((__strub__)) len = 512;
20 asm ("" : "+r" (len));
21 char s[len];
22 __builtin_strcpy (s, test_string);
23 __builtin_strcpy (s + len - sizeof (test_string), test_string);
24 asm ("" : "+m" (s));
25 return (char *) __builtin_stack_address ();
26 }
27
28 static inline __attribute__ ((__always_inline__))
29 int
30 look_for_string (char *e)
31 {
32 char *p = (char *) __builtin_stack_address ();
33
34 if (p == e)
35 __builtin_abort ();
36
37 if (p > e)
38 {
39 char *q = p;
40 p = e;
41 e = q;
42 }
43
44 for (char *re = e - sizeof (test_string); p < re; p++)
45 for (int i = 0; p[i] == test_string[i]; i++)
46 if (i == sizeof (test_string) - 1)
47 return i;
48
49 return 0;
50 }
51
52 static inline ATTR_STRUB_AT_CALLS
53 char *
54 innermost ()
55 {
56 int __attribute__ ((__strub__)) len = 512;
57 asm ("" : "+r" (len));
58 char s[len];
59 __builtin_strcpy (s, test_string);
60 __builtin_strcpy (s + len - sizeof (test_string), test_string);
61 asm ("" : "+m" (s));
62 char *ret = leak_string ();
63 if (__builtin_strcmp (s, test_string) != 0)
64 __builtin_abort ();
65 if (__builtin_strcmp (s + len - sizeof (test_string), test_string) != 0)
66 __builtin_abort ();
67 return ret;
68 }
69
70 static inline ATTR_STRUB_AT_CALLS
71 char *
72 intermediate ()
73 {
74 int __attribute__ ((__strub__)) len = 512;
75 asm ("" : "+r" (len));
76 char s[len];
77 __builtin_strcpy (s, test_string);
78 __builtin_strcpy (s + len - sizeof (test_string), test_string);
79 asm ("" : "+m" (s));
80 char *ret = innermost ();
81 if (__builtin_strcmp (s, test_string) != 0)
82 __builtin_abort ();
83 if (__builtin_strcmp (s + len - sizeof (test_string), test_string) != 0)
84 __builtin_abort ();
85 return ret;
86 }
87
88 static inline __attribute__ ((__strub__ ("internal")))
89 char *
90 internal ()
91 {
92 return intermediate ();
93 }
94
95 int __attribute__ ((__strub__ ("disabled")))
96 main ()
97 {
98 /* Since these test check stack contents above the top of the stack, an
99 unexpected asynchronous signal or interrupt might overwrite the bits we
100 expect to find and cause spurious fails. Tolerate one such overall
101 spurious fail by retrying. */
102 int i = 1;
103 while (look_for_string (internal ()))
104 if (!i--) __builtin_abort ();
105 __builtin_exit (0);
106 }