1 /* Test to verify that the return value of calls to __builtin_sprintf
2 is not folded if the call isn't fully specified, even if it would
3 otherwise produce a known number of bytes on output, and that if
4 the return value is in a known range the range is not made
5 available to subsequent passes and doesn't affect branching and
7 The test is compiled with warnings disabled to make sure the absence
8 of optimizations does not depend on the presence of warnings. */
9 /* { dg-do compile } */
10 /* { dg-options "-O2 -fprintf-return-value -fdump-tree-optimized -w" } */
16 #define INT_MAX __INT_MAX__
17 #define INT_MIN (-INT_MAX - 1)
19 #define LONG_MAX __LONG_MAX__
20 #define LONG_MIN (-LONG_MAX - 1)
25 #define concat(a, b) a ## b
26 #define CAT(a, b) concat (a, b)
28 /* Calls to this function must not be eliminated. */
29 void must_not_eliminate (void);
31 #define EQL(expect, size, fmt, ...) \
32 void __attribute__ ((noinline, noclone)) \
33 CAT (test_on_line_, __LINE__)(void) \
35 if (!LINE || LINE == __LINE__) \
37 char *dst = size < 0 ? buf : buf8k + sizeof buf8k - size; \
38 int result = __builtin_sprintf (dst, fmt, __VA_ARGS__); \
39 if (result != expect) \
40 must_not_eliminate (); \
44 /* Verify that the return value or range or return values from the call
45 to the formatted function is not treated as a constant or made available
46 to subsequent optimization passes. */
47 #define RNG(min, max, size, fmt, ...) \
48 void __attribute__ ((noinline, noclone)) \
49 CAT (test_on_line_, __LINE__)(void) \
51 if (!LINE || LINE == __LINE__) \
53 char *dst = size < 0 ? buf : buf8k + sizeof buf8k - size; \
54 int result = __builtin_sprintf (dst, fmt, __VA_ARGS__); \
55 if (result < min || max < result) \
56 must_not_eliminate (); \
60 typedef __SIZE_TYPE__
size_t;
65 volatile unsigned long lu
;
70 volatile long double ld
;
72 /* Verify that overflowing the destination object disables the return
73 value optimization. */
74 EQL (0, 0, "%c", ' ');
76 EQL (0, 0, "%-s", "");
78 EQL (1, 1, "%c", 'x');
79 EQL (1, 1, "%-s", "x");
81 /* Size of character constant must be larger than 2 for this to overflow. */
82 #if __SIZEOF_INT__ > 2
83 EQL (1, 2, "%c", 'x');
86 EQL (4, 4, "%4c", 'x');
88 /* Verify that exceeding the environmental limit of 4095 bytes for
89 a single conversion specification disables the return value
91 EQL ( 4096, sizeof buf8k
, "%4096c", 'x');
93 EQL (INT_MAX
, -1, "%*c", INT_MAX
, 'x');
95 EQL ( 4096, sizeof buf8k
, "%4096.4094f", 1.0);
96 EQL ( 4096, sizeof buf8k
, "%.4094f", 1.0);
97 EQL ( 4097, sizeof buf8k
, "%.4095f", 1.0);
99 enum { imax2
= (INT_MAX
/ 2) * 2 };
100 EQL (imax2
, -1, "%*c%*c", INT_MAX
/ 2, 'x', INT_MAX
/ 2, 'y');
102 /* Verify that range information for calls that overflow the destination
105 +-- lower bound of the tested range
106 | +-- upper bound of the tested range
107 | | +-- size of destination buffer
108 | | | +-- format string
109 | | | | +-- argument(s)
112 RNG (0, 0, 0, "%hhi", i
)
113 RNG (0, 0, 1, "%hhi", i
)
114 RNG (0, 1, 1, "%hhi", i
)
115 RNG (0, 0, 2, "%hhi", i
)
116 RNG (0, 1, 2, "%hhi", i
)
117 RNG (0, 2, 2, "%hhi", i
)
118 RNG (0, 0, 3, "%hhi", i
)
119 RNG (0, 1, 3, "%hhi", i
)
120 RNG (0, 2, 3, "%hhi", i
)
121 RNG (0, 3, 3, "%hhi", i
)
122 RNG (0, 0, 4, "%hhi", i
)
123 RNG (0, 1, 4, "%hhi", i
)
124 RNG (0, 2, 4, "%hhi", i
)
125 RNG (0, 3, 4, "%hhi", i
)
126 RNG (0, 4, 4, "%hhi", i
)
128 RNG (0, 0, 0, "%hhu", i
)
129 RNG (0, 0, 1, "%hhu", i
)
130 RNG (0, 1, 1, "%hhu", i
)
131 RNG (0, 0, 2, "%hhu", i
)
132 RNG (0, 1, 2, "%hhu", i
)
133 RNG (0, 2, 2, "%hhu", i
)
134 RNG (0, 0, 3, "%hhu", i
)
135 RNG (0, 1, 3, "%hhu", i
)
136 RNG (0, 2, 3, "%hhu", i
)
137 RNG (0, 3, 3, "%hhu", i
)
139 RNG (0, 0, 0, "%i", i
)
141 RNG (0, 0, 1, "%i", i
)
142 RNG (0, 1, 1, "%i", i
)
144 RNG (0, 0, 2, "%i", i
)
145 RNG (0, 1, 2, "%i", i
)
146 RNG (0, 2, 2, "%i", i
)
148 RNG (0, 0, 3, "%i", i
)
149 RNG (0, 1, 3, "%i", i
)
150 RNG (0, 2, 3, "%i", i
)
151 RNG (0, 3, 3, "%i", i
)
153 RNG (0, 0, 4, "%i", i
)
154 RNG (0, 1, 4, "%i", i
)
155 RNG (0, 2, 4, "%i", i
)
156 RNG (0, 3, 4, "%i", i
)
157 RNG (0, 4, 4, "%i", i
)
159 RNG (0, 0, 5, "%i", i
)
160 RNG (0, 1, 5, "%i", i
)
161 RNG (0, 2, 5, "%i", i
)
162 RNG (0, 3, 5, "%i", i
)
163 RNG (0, 4, 5, "%i", i
)
164 RNG (0, 5, 5, "%i", i
)
166 RNG (0, 0, 6, "%i", i
)
167 RNG (0, 1, 6, "%i", i
)
168 RNG (0, 2, 6, "%i", i
)
169 RNG (0, 3, 6, "%i", i
)
170 RNG (0, 4, 6, "%i", i
)
171 RNG (0, 5, 6, "%i", i
)
172 RNG (0, 6, 6, "%i", i
)
174 /* If int is 16bit then it will always fit in 7 char characters with this
176 #if __SIZEOF_INT__ > 2
177 RNG (0, 0, 7, "%i", i
)
179 RNG (0, 1, 7, "%i", i
)
180 RNG (0, 2, 7, "%i", i
)
181 RNG (0, 3, 7, "%i", i
)
182 RNG (0, 4, 7, "%i", i
)
183 RNG (0, 5, 7, "%i", i
)
184 RNG (0, 6, 7, "%i", i
)
186 RNG (4, 4, 32, "%i", i
)
187 RNG (4, 4, 32, "%u", u
)
188 RNG (4, 4, 32, "%li", li
)
189 RNG (4, 4, 32, "%lu", lu
)
190 RNG (4, 4, 32, "%zu", sz
)
192 /* Exercise bug 78586. */
193 RNG (4, 4, 32, "%lu", (unsigned long)i
)
194 RNG (4, 4, 32, "%lu", (unsigned long)u
)
195 RNG (4, 4, 32, "%lu", (unsigned long)li
)
196 RNG (4, 4, 32, "%lu", (unsigned long)lu
)
197 RNG (4, 4, 32, "%lu", (unsigned long)sz
)
200 #if __SIZEOF_INT__ == 4
202 /* A 32-bit int takes up at most 11 bytes (-2147483648) not including
203 the terminating nul. */
204 RNG (0, 7, 7, "%i", i
)
206 RNG (0, 0, 8, "%i", i
)
207 RNG (0, 1, 8, "%i", i
)
208 RNG (0, 2, 8, "%i", i
)
209 RNG (0, 3, 8, "%i", i
)
210 RNG (0, 4, 8, "%i", i
)
211 RNG (0, 5, 8, "%i", i
)
212 RNG (0, 6, 8, "%i", i
)
213 RNG (0, 7, 8, "%i", i
)
214 RNG (0, 8, 8, "%i", i
)
216 RNG (0, 0, 9, "%i", i
)
217 RNG (0, 1, 9, "%i", i
)
218 RNG (0, 2, 9, "%i", i
)
219 RNG (0, 3, 9, "%i", i
)
220 RNG (0, 4, 9, "%i", i
)
221 RNG (0, 5, 9, "%i", i
)
222 RNG (0, 6, 9, "%i", i
)
223 RNG (0, 7, 9, "%i", i
)
224 RNG (0, 8, 9, "%i", i
)
225 RNG (0, 9, 9, "%i", i
)
227 RNG (0, 0, 10, "%i", i
)
228 RNG (0, 1, 10, "%i", i
)
229 RNG (0, 2, 10, "%i", i
)
230 RNG (0, 3, 10, "%i", i
)
231 RNG (0, 4, 10, "%i", i
)
232 RNG (0, 5, 10, "%i", i
)
233 RNG (0, 6, 10, "%i", i
)
234 RNG (0, 7, 10, "%i", i
)
235 RNG (0, 8, 10, "%i", i
)
236 RNG (0, 9, 10, "%i", i
)
237 RNG (0, 10, 10, "%i", i
)
241 /* Verify that the output of a "%a" directive with no precision is not
242 considered constant or within a known range (the number of digits
243 after the decimal point is unspecified in this case). The hardcoded
244 ranges correspond to Glibc values. */
245 RNG (6, 6, 7, "%a", 0.0) /* Glibc output: "0x0p+0" */
246 RNG (6, 6, 7, "%a", d
)
247 RNG (6, 6, 7, "%.4096a", d
)
249 RNG (6, 6, 7, "%La", 0.0L) /* Glibc output: "0x0p+0" */
250 RNG (6, 6, 7, "%La", ld
)
251 RNG (6, 6, 7, "%.4096La", ld
)
253 /* Verify that the pound flag with unknown precision prevents the %g
254 directive from trimming trailing zeros as it otherwise does. As
255 a consequence, the result must be assumed to be as large as
257 RNG (1, 315, 316, "%#.*g", i
, d
);
258 RNG (1, 4095, 4096, "%#.*g", i
, d
);
259 RNG (1, 4095, 4096, "%#.*g", i
, 0.0);
261 /* Verify that the result of formatting an unknown string isn't optimized
262 into a non-negative range. The string could be longer that 4,095 bytes,
263 resulting in the formatting function having undefined behavior (and
264 returning a negative value as Glibc can for some directives). */
265 RNG (0, INT_MAX
, -1, "%-s", str
);
267 /* Verify the result of a conditional expression involving a string
268 literal and an unknown string isn't optimized. */
269 RNG (0, 1, 4, "%-s", i
? str
: "123");
270 RNG (0, 1, 4, "%-s", i
? "123" : str
);
272 /* Verfy that the output involving wide strings is not optimized
273 (the output is actually bounded by a function of MB_LEN_MAX
274 which should be at least 6 to accommodate UTF-8 but this isn't
276 RNG (0, 5, 7, "%ls", L
"1");
277 RNG (0, 6, 8, "%s%ls", "1", L
"2");
279 /* Verify that no call to abort has been eliminated and that each call
280 is at the beginning of a basic block (and thus the result of a branch).
281 This latter test tries to verify that the test preceding the call to
282 the must_not_eliminate() function has not been eliminated either.
284 The expected output looks something like this:
287 result_3 = __builtin_sprintf (&MEM[(void *)&buf8k + 8192B], "%c", 32);
289 goto <bb 3>; [50.0%] [count: INV]
291 goto <bb 4>; [50.0%] [count: INV]
293 <bb 3>[50.0%] [count: INV]:
294 must_not_eliminate ();
298 /* Only conditional calls to must_not_eliminate must be made (with
300 { dg-final { scan-tree-dump-times "> \\\[local count: \[0-9INV\]*\\\]:\n *must_not_eliminate" 127 "optimized" { target { ilp32 || lp64 } } } }
301 { dg-final { scan-tree-dump-times "> \\\[local count: \[0-9INV\]*\\\]:\n *must_not_eliminate" 94 "optimized" { target { ! { ilp32 || lp64 } } } } }
302 No unconditional calls to abort should be made:
303 { dg-final { scan-tree-dump-not ";\n *must_not_eliminate" "optimized" } } */