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 6 the removal of code. 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" } */ 11 12 #ifndef LINE 13 # define LINE 0 14 #endif 15 16 #define INT_MAX __INT_MAX__ 17 #define INT_MIN (-INT_MAX - 1) 18 19 #define LONG_MAX __LONG_MAX__ 20 #define LONG_MIN (-LONG_MAX - 1) 21 22 char *buf; 23 char buf8k [8192]; 24 25 #define concat(a, b) a ## b 26 #define CAT(a, b) concat (a, b) 27 28 /* Calls to this function must not be eliminated. */ 29 void must_not_eliminate (void); 30 31 #define EQL(expect, size, fmt, ...) \ 32 void __attribute__ ((noinline, noclone)) \ 33 CAT (test_on_line_, __LINE__)(void) \ 34 { \ 35 if (!LINE || LINE == __LINE__) \ 36 { \ 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 (); \ 41 } \ 42 } 43 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) \ 50 { \ 51 if (!LINE || LINE == __LINE__) \ 52 { \ 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 (); \ 57 } \ 58 } 59 60 typedef __SIZE_TYPE__ size_t; 61 62 volatile int i; 63 volatile unsigned u; 64 volatile long li; 65 volatile unsigned long lu; 66 volatile size_t sz; 67 volatile char *str; 68 69 volatile double d; 70 volatile long double ld; 71 72 /* Verify that overflowing the destination object disables the return 73 value optimization. */ 74 EQL (0, 0, "%c", ' '); 75 EQL (0, 0, "%c", i) 76 EQL (0, 0, "%-s", ""); 77 78 EQL (1, 1, "%c", 'x'); 79 EQL (1, 1, "%-s", "x"); 80 81 EQL (1, 2, "%c", 'x'); 82 83 EQL (4, 4, "%4c", 'x'); 84 85 /* Verify that exceeding the environmental limit of 4095 bytes for 86 a single conversion specification disables the return value 87 folding. */ 88 EQL ( 4096, sizeof buf8k, "%4096c", 'x'); 89 90 EQL (INT_MAX, -1, "%*c", INT_MAX, 'x'); 91 92 EQL ( 4096, sizeof buf8k, "%4096.4094f", 1.0); 93 EQL ( 4096, sizeof buf8k, "%.4094f", 1.0); 94 EQL ( 4097, sizeof buf8k, "%.4095f", 1.0); 95 96 enum { imax2 = (INT_MAX / 2) * 2 }; 97 EQL (imax2, -1, "%*c%*c", INT_MAX / 2, 'x', INT_MAX / 2, 'y'); 98 99 /* Verify that range information for calls that overflow the destination 100 isn't available. 101 102 +-- lower bound of the tested range 103 | +-- upper bound of the tested range 104 | | +-- size of destination buffer 105 | | | +-- format string 106 | | | | +-- argument(s) 107 | | | | | 108 V V V V V */ 109 RNG (0, 0, 0, "%hhi", i) 110 RNG (0, 0, 1, "%hhi", i) 111 RNG (0, 1, 1, "%hhi", i) 112 RNG (0, 0, 2, "%hhi", i) 113 RNG (0, 1, 2, "%hhi", i) 114 RNG (0, 2, 2, "%hhi", i) 115 RNG (0, 0, 3, "%hhi", i) 116 RNG (0, 1, 3, "%hhi", i) 117 RNG (0, 2, 3, "%hhi", i) 118 RNG (0, 3, 3, "%hhi", i) 119 RNG (0, 0, 4, "%hhi", i) 120 RNG (0, 1, 4, "%hhi", i) 121 RNG (0, 2, 4, "%hhi", i) 122 RNG (0, 3, 4, "%hhi", i) 123 RNG (0, 4, 4, "%hhi", i) 124 125 RNG (0, 0, 0, "%hhu", i) 126 RNG (0, 0, 1, "%hhu", i) 127 RNG (0, 1, 1, "%hhu", i) 128 RNG (0, 0, 2, "%hhu", i) 129 RNG (0, 1, 2, "%hhu", i) 130 RNG (0, 2, 2, "%hhu", i) 131 RNG (0, 0, 3, "%hhu", i) 132 RNG (0, 1, 3, "%hhu", i) 133 RNG (0, 2, 3, "%hhu", i) 134 RNG (0, 3, 3, "%hhu", i) 135 136 RNG (0, 0, 0, "%i", i) 137 138 RNG (0, 0, 1, "%i", i) 139 RNG (0, 1, 1, "%i", i) 140 141 RNG (0, 0, 2, "%i", i) 142 RNG (0, 1, 2, "%i", i) 143 RNG (0, 2, 2, "%i", i) 144 145 RNG (0, 0, 3, "%i", i) 146 RNG (0, 1, 3, "%i", i) 147 RNG (0, 2, 3, "%i", i) 148 RNG (0, 3, 3, "%i", i) 149 150 RNG (0, 0, 4, "%i", i) 151 RNG (0, 1, 4, "%i", i) 152 RNG (0, 2, 4, "%i", i) 153 RNG (0, 3, 4, "%i", i) 154 RNG (0, 4, 4, "%i", i) 155 156 RNG (0, 0, 5, "%i", i) 157 RNG (0, 1, 5, "%i", i) 158 RNG (0, 2, 5, "%i", i) 159 RNG (0, 3, 5, "%i", i) 160 RNG (0, 4, 5, "%i", i) 161 RNG (0, 5, 5, "%i", i) 162 163 RNG (0, 0, 6, "%i", i) 164 RNG (0, 1, 6, "%i", i) 165 RNG (0, 2, 6, "%i", i) 166 RNG (0, 3, 6, "%i", i) 167 RNG (0, 4, 6, "%i", i) 168 RNG (0, 5, 6, "%i", i) 169 RNG (0, 6, 6, "%i", i) 170 171 RNG (0, 0, 7, "%i", i) 172 RNG (0, 1, 7, "%i", i) 173 RNG (0, 2, 7, "%i", i) 174 RNG (0, 3, 7, "%i", i) 175 RNG (0, 4, 7, "%i", i) 176 RNG (0, 5, 7, "%i", i) 177 RNG (0, 6, 7, "%i", i) 178 179 RNG (4, 4, 32, "%i", i) 180 RNG (4, 4, 32, "%u", u) 181 RNG (4, 4, 32, "%li", li) 182 RNG (4, 4, 32, "%lu", lu) 183 RNG (4, 4, 32, "%zu", sz) 184 185 /* Exercise bug 78586. */ 186 RNG (4, 4, 32, "%lu", (unsigned long)i) 187 RNG (4, 4, 32, "%lu", (unsigned long)u) 188 RNG (4, 4, 32, "%lu", (unsigned long)li) 189 RNG (4, 4, 32, "%lu", (unsigned long)lu) 190 RNG (4, 4, 32, "%lu", (unsigned long)sz) 191 192 193 #if __SIZEOF_INT__ == 4 194 195 /* A 32-bit int takes up at most 11 bytes (-2147483648) not including 196 the terminating nul. */ 197 RNG (0, 7, 7, "%i", i) 198 199 RNG (0, 0, 8, "%i", i) 200 RNG (0, 1, 8, "%i", i) 201 RNG (0, 2, 8, "%i", i) 202 RNG (0, 3, 8, "%i", i) 203 RNG (0, 4, 8, "%i", i) 204 RNG (0, 5, 8, "%i", i) 205 RNG (0, 6, 8, "%i", i) 206 RNG (0, 7, 8, "%i", i) 207 RNG (0, 8, 8, "%i", i) 208 209 RNG (0, 0, 9, "%i", i) 210 RNG (0, 1, 9, "%i", i) 211 RNG (0, 2, 9, "%i", i) 212 RNG (0, 3, 9, "%i", i) 213 RNG (0, 4, 9, "%i", i) 214 RNG (0, 5, 9, "%i", i) 215 RNG (0, 6, 9, "%i", i) 216 RNG (0, 7, 9, "%i", i) 217 RNG (0, 8, 9, "%i", i) 218 RNG (0, 9, 9, "%i", i) 219 220 RNG (0, 0, 10, "%i", i) 221 RNG (0, 1, 10, "%i", i) 222 RNG (0, 2, 10, "%i", i) 223 RNG (0, 3, 10, "%i", i) 224 RNG (0, 4, 10, "%i", i) 225 RNG (0, 5, 10, "%i", i) 226 RNG (0, 6, 10, "%i", i) 227 RNG (0, 7, 10, "%i", i) 228 RNG (0, 8, 10, "%i", i) 229 RNG (0, 9, 10, "%i", i) 230 RNG (0, 10, 10, "%i", i) 231 232 #endif 233 234 /* Verify that the output of a "%a" directive with no precision is not 235 considered constant or within a known range (the number of digits 236 after the decimal point is unspecified in this case). The hardcoded 237 ranges correspond to Glibc values. */ 238 RNG (6, 6, 7, "%a", 0.0) /* Glibc output: "0x0p+0" */ 239 RNG (6, 6, 7, "%a", d) 240 RNG (6, 6, 7, "%.4096a", d) 241 242 RNG (6, 6, 7, "%La", 0.0L) /* Glibc output: "0x0p+0" */ 243 RNG (6, 6, 7, "%La", ld) 244 RNG (6, 6, 7, "%.4096La", ld) 245 246 /* Verify that the pound flag with unknown precision prevents the %g 247 directive from trimming trailing zeros as it otherwise does. As 248 a consequence, the result must be assumed to be as large as 249 precision. */ 250 RNG (1, 315, 316, "%#.*g", i, d); 251 RNG (1, 4095, 4096, "%#.*g", i, d); 252 RNG (1, 4095, 4096, "%#.*g", i, 0.0); 253 254 /* Verify that the result of formatting an unknown string isn't optimized 255 into a non-negative range. The string could be longer that 4,095 bytes, 256 resulting in the formatting function having undefined behavior (and 257 returning a negative value as Glibc can for some directives). */ 258 RNG (0, INT_MAX, -1, "%-s", str); 259 260 /* Verify the result of a conditional expression involving a string 261 literal and an unknown string isn't optimized. */ 262 RNG (0, 1, 4, "%-s", i ? str : "123"); 263 RNG (0, 1, 4, "%-s", i ? "123" : str); 264 265 /* Verfy that the output involving wide strings is not optimized 266 (the output is actually bounded by a function of MB_LEN_MAX 267 which should be at least 6 to accommodate UTF-8 but this isn't 268 implemented yet). */ 269 RNG (0, 5, 7, "%ls", L"1"); 270 RNG (0, 6, 8, "%s%ls", "1", L"2"); 271 272 /* Verify that no call to abort has been eliminated and that each call 273 is at the beginning of a basic block (and thus the result of a branch). 274 This latter test tries to verify that the test preceding the call to 275 the must_not_eliminate() function has not been eliminated either. 276 277 The expected output looks something like this: 278 279 <bb 2>: 280 result_3 = __builtin_sprintf (&MEM[(void *)&buf8k + 8192B], "%c", 32); 281 if (result_3 != 0) 282 goto <bb 3>; [50.0%] [count: INV] 283 else 284 goto <bb 4>; [50.0%] [count: INV] 285 286 <bb 3>[50.0%] [count: INV]: 287 must_not_eliminate (); 288 289 */ 290 291 /* Only conditional calls to must_not_eliminate must be made (with 292 any probability): 293 { dg-final { scan-tree-dump-times "> \\\[local count: \[0-9INV\]*\\\]:\n *must_not_eliminate" 127 "optimized" { target { ilp32 || lp64 } } } } 294 { dg-final { scan-tree-dump-times "> \\\[local count: \[0-9INV\]*\\\]:\n *must_not_eliminate" 96 "optimized" { target { { ! ilp32 } && { ! lp64 } } } } } 295 No unconditional calls to abort should be made: 296 { dg-final { scan-tree-dump-not ";\n *must_not_eliminate" "optimized" } } */ 297