Debian Codegraph @

gcc/testsuite/gcc.c-torture/execute/return-addr.c

raw
1 /* Test to verify that a function that returns either the address
2 of a local variable or a non-local via a MAX_EXPR or MIN_EXPR
3 doesn't return null when the result of the expression is
4 the latter. */
5
6 #define NOIPA __attribute__ ((noclone, noinline, noipa))
7
8 #define A(expr) \
9 ((expr) \
10 ? (void)0 \
11 : (__builtin_printf ("assertion failed on line %i: %s\n", \
12 __LINE__, #expr), \
13 __builtin_abort ()))
14
15
16 typedef __UINTPTR_TYPE__ uintptr_t;
17
18 /* Return a bigger value than P. The address still points (just
19 past) the local variable pointed to by P so the caller does
20 return the address of a local variable but that's hidden from
21 GCC by the attribute and the point of the test is to verify
22 that the address in the return statement in the caller isn't
23 replaced by null when GCC cannot prove the address doesn't
24 reference a non-local variable. */
25
26 NOIPA char* get_max_2 (char *p)
27 {
28 return p + 1;
29 }
30
31 NOIPA char* get_max_3 (char *p, char *q)
32 {
33 return p < q ? q + 1 : p + 1;
34 }
35
36 /* Analogous to the above. The expressions are undefined because
37 they form an address prior to the beginning of the object but
38 it's hidden from GCC by the attributes. */
39
40 NOIPA char* get_min_2 (char *p)
41 {
42 return p - 1;
43 }
44
45 NOIPA char* get_min_3 (char *p, char *q)
46 {
47 return p < q ? p - 1 : q - 1;
48 }
49
50
51 NOIPA void* test_max_2 (void)
52 {
53 char c;
54
55 char *p = get_max_2 (&c);
56
57 void *q = p > &c ? p : &c; /* MAX_EXPR */
58 return q;
59 }
60
61 NOIPA void* test_max_3 (void)
62 {
63 char c;
64 char d;
65
66 char *p = get_max_3 (&c, &d);
67
68 void *q = p < &c ? &c < &d ? &d : &c : p;
69 return q;
70 }
71
72 NOIPA void* test_min_2 (void)
73 {
74 char c;
75
76 char *p = get_min_2 (&c);
77
78 void *q = p < &c ? p : &c; /* MIN_EXPR" */
79 return q;
80 }
81
82 NOIPA void* test_min_3 (void)
83 {
84 char c;
85 char d;
86
87 char *p = get_min_3 (&c, &d);
88
89 void *q = p > &c ? &c > &d ? &d : &c : p;
90 return q;
91 }
92
93 NOIPA void* test_min_3_phi (int i)
94 {
95 char a, b;
96
97 char *p0 = &a;
98 char *p1 = &b;
99 char *p2 = get_min_3 (&a, &b);
100 char *p3 = get_min_3 (&a, &b);
101
102 char *p4 = p2 < p0 ? p2 : p0;
103 char *p5 = p3 < p1 ? p3 : p1;
104
105 __builtin_printf ("%p %p %p %p\n", p2, p3, p4, p5);
106
107 if (i == 1)
108 return p4;
109 else
110 return p5;
111 }
112
113 int main ()
114 {
115 A (0 != test_max_2 ());
116 A (0 != test_max_3 ());
117
118 A (0 != test_min_2 ());
119 A (0 != test_min_3 ());
120
121 A (0 != test_min_3_phi (0));
122 }