Debian Codegraph @

gcc/testsuite/gcc.c-torture/execute/pr17377.c

raw
1 /* PR target/17377
2 Bug in code emitted by "return" pattern on CRIS: missing pop of
3 forced return address on stack. */
4 /* { dg-require-effective-target return_address } */
5 void abort (void);
6 void exit (int);
7
8 int calls = 0;
9
10 void *f (int) __attribute__ ((__noinline__));
11 void *
12 f (int i)
13 {
14 /* The code does a little brittle song and dance to trig the "return"
15 pattern instead of the function epilogue. This must still be a
16 leaf function for the bug to be exposed. */
17
18 if (calls++ == 0)
19 return __builtin_return_address (0);
20
21 switch (i)
22 {
23 case 1:
24 return f;
25 case 0:
26 return __builtin_return_address (0);
27 }
28 return 0;
29 }
30
31 int x;
32
33 void *y (int i) __attribute__ ((__noinline__,__noclone__));
34 void *
35 y (int i)
36 {
37 x = 0;
38
39 /* This must not be a sibling call: the return address must appear
40 constant for different calls to this function. Postincrementing x
41 catches otherwise unidentified multiple returns (e.g. through the
42 return-address register and then this epilogue popping the address
43 stored on stack in "f"). */
44 return (char *) f (i) + x++;
45 }
46
47 int
48 main (void)
49 {
50 void *v = y (4);
51 if (y (1) != f
52 /* Can't reasonably check the validity of the return address
53 above, but it's not that important: the test-case will probably
54 crash on the first call to f with the bug present, or it will
55 run wild including returning early (in y or here), so we also
56 try and check the number of calls. */
57 || y (0) != v
58 || y (3) != 0
59 || y (-1) != 0
60 || calls != 5)
61 abort ();
62 exit (0);
63 }