Branch data Line data Source code
1 : : /* Pass to detect and issue warnings for invalid accesses, including
2 : : invalid or mismatched allocation/deallocation calls.
3 : :
4 : : Copyright (C) 2020-2025 Free Software Foundation, Inc.
5 : : Contributed by Martin Sebor <msebor@redhat.com>.
6 : :
7 : : This file is part of GCC.
8 : :
9 : : GCC is free software; you can redistribute it and/or modify it under
10 : : the terms of the GNU General Public License as published by the Free
11 : : Software Foundation; either version 3, or (at your option) any later
12 : : version.
13 : :
14 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 : : for more details.
18 : :
19 : : You should have received a copy of the GNU General Public License
20 : : along with GCC; see the file COPYING3. If not see
21 : : <http://www.gnu.org/licenses/>. */
22 : :
23 : : #define INCLUDE_STRING
24 : : #include "config.h"
25 : : #include "system.h"
26 : : #include "coretypes.h"
27 : : #include "backend.h"
28 : : #include "tree.h"
29 : : #include "gimple.h"
30 : : #include "tree-pass.h"
31 : : #include "builtins.h"
32 : : #include "diagnostic.h"
33 : : #include "ssa.h"
34 : : #include "gimple-pretty-print.h"
35 : : #include "gimple-ssa-warn-access.h"
36 : : #include "gimple-ssa-warn-restrict.h"
37 : : #include "diagnostic-core.h"
38 : : #include "fold-const.h"
39 : : #include "gimple-iterator.h"
40 : : #include "gimple-fold.h"
41 : : #include "langhooks.h"
42 : : #include "memmodel.h"
43 : : #include "target.h"
44 : : #include "tree-dfa.h"
45 : : #include "tree-ssa.h"
46 : : #include "tree-cfg.h"
47 : : #include "tree-object-size.h"
48 : : #include "tree-ssa-strlen.h"
49 : : #include "calls.h"
50 : : #include "cfganal.h"
51 : : #include "intl.h"
52 : : #include "gimple-range.h"
53 : : #include "stringpool.h"
54 : : #include "attribs.h"
55 : : #include "demangle.h"
56 : : #include "attr-fnspec.h"
57 : : #include "pointer-query.h"
58 : : #include "pretty-print-markup.h"
59 : : #include "gcc-urlifier.h"
60 : :
61 : : /* Return true if tree node X has an associated location. */
62 : :
63 : : static inline location_t
64 : 117 : has_location (const_tree x)
65 : : {
66 : 117 : if (DECL_P (x))
67 : 92 : return DECL_SOURCE_LOCATION (x) != UNKNOWN_LOCATION;
68 : :
69 : 25 : if (EXPR_P (x))
70 : 0 : return EXPR_HAS_LOCATION (x);
71 : :
72 : : return false;
73 : : }
74 : :
75 : : /* Return the associated location of STMT. */
76 : :
77 : : static inline location_t
78 : 2194347 : get_location (const gimple *stmt)
79 : : {
80 : 1938714 : return gimple_location (stmt);
81 : : }
82 : :
83 : : /* Return the associated location of tree node X. */
84 : :
85 : : static inline location_t
86 : 2041 : get_location (tree x)
87 : : {
88 : 2041 : if (DECL_P (x))
89 : 981 : return DECL_SOURCE_LOCATION (x);
90 : :
91 : 1060 : if (EXPR_P (x))
92 : 1060 : return EXPR_LOCATION (x);
93 : :
94 : : return UNKNOWN_LOCATION;
95 : : }
96 : :
97 : : /* Overload of the nascent tree function for GIMPLE STMT. */
98 : :
99 : : static inline tree
100 : 1221026 : get_callee_fndecl (const gimple *stmt)
101 : : {
102 : 632 : return gimple_call_fndecl (stmt);
103 : : }
104 : :
105 : : static inline unsigned
106 : 33051687 : call_nargs (const gimple *stmt)
107 : : {
108 : 2248594 : return gimple_call_num_args (stmt);
109 : : }
110 : :
111 : : static inline unsigned
112 : 356 : call_nargs (const_tree expr)
113 : : {
114 : 356 : return call_expr_nargs (expr);
115 : : }
116 : :
117 : :
118 : : static inline tree
119 : 13034743 : call_arg (const gimple *stmt, unsigned argno)
120 : : {
121 : 1351532 : return gimple_call_arg (stmt, argno);
122 : : }
123 : :
124 : : static inline tree
125 : 302 : call_arg (tree expr, unsigned argno)
126 : : {
127 : 302 : return CALL_EXPR_ARG (expr, argno);
128 : : }
129 : :
130 : : /* For a call EXPR at LOC to a function FNAME that expects a string
131 : : in the argument ARG, issue a diagnostic due to it being a called
132 : : with an argument that is a character array with no terminating
133 : : NUL. SIZE is the EXACT size of the array, and BNDRNG the number
134 : : of characters in which the NUL is expected. Either EXPR or FNAME
135 : : may be null but noth both. SIZE may be null when BNDRNG is null. */
136 : :
137 : : template <class GimpleOrTree>
138 : : static void
139 : 8030 : warn_string_no_nul (location_t loc, GimpleOrTree expr, const char *fname,
140 : : tree arg, tree decl, tree size, bool exact,
141 : : const wide_int bndrng[2] /* = NULL */)
142 : : {
143 : 8030 : const opt_code opt = OPT_Wstringop_overread;
144 : 1399 : if ((expr && warning_suppressed_p (expr, opt))
145 : 8843 : || warning_suppressed_p (arg, opt))
146 : 1139 : return;
147 : :
148 : 6891 : loc = expansion_point_location_if_in_system_header (loc);
149 : : bool warned;
150 : :
151 : : /* Format the bound range as a string to keep the number of messages
152 : : from exploding. */
153 : : char bndstr[80];
154 : 6891 : *bndstr = 0;
155 : 6891 : if (bndrng)
156 : : {
157 : 144 : if (bndrng[0] == bndrng[1])
158 : 132 : sprintf (bndstr, "%llu", (unsigned long long) bndrng[0].to_uhwi ());
159 : : else
160 : 12 : sprintf (bndstr, "[%llu, %llu]",
161 : 12 : (unsigned long long) bndrng[0].to_uhwi (),
162 : 12 : (unsigned long long) bndrng[1].to_uhwi ());
163 : : }
164 : :
165 : 6891 : auto_diagnostic_group d;
166 : :
167 : 6891 : const tree maxobjsize = max_object_size ();
168 : 6891 : const wide_int maxsiz = wi::to_wide (maxobjsize);
169 : 6891 : if (expr)
170 : : {
171 : 774 : tree func = get_callee_fndecl (expr);
172 : 774 : if (bndrng)
173 : : {
174 : 144 : if (wi::ltu_p (maxsiz, bndrng[0]))
175 : 0 : warned = warning_at (loc, opt,
176 : : "%qD specified bound %s exceeds "
177 : : "maximum object size %E",
178 : : func, bndstr, maxobjsize);
179 : : else
180 : : {
181 : 144 : bool maybe = wi::to_wide (size) == bndrng[0];
182 : 191 : warned = warning_at (loc, opt,
183 : : exact
184 : : ? G_("%qD specified bound %s exceeds "
185 : : "the size %E of unterminated array")
186 : : : (maybe
187 : 47 : ? G_("%qD specified bound %s may "
188 : : "exceed the size of at most %E "
189 : : "of unterminated array")
190 : : : G_("%qD specified bound %s exceeds "
191 : : "the size of at most %E "
192 : : "of unterminated array")),
193 : : func, bndstr, size);
194 : : }
195 : : }
196 : : else
197 : 630 : warned = warning_at (loc, opt,
198 : : "%qD argument missing terminating nul",
199 : : func);
200 : : }
201 : : else
202 : : {
203 : 6117 : if (bndrng)
204 : : {
205 : 0 : if (wi::ltu_p (maxsiz, bndrng[0]))
206 : 0 : warned = warning_at (loc, opt,
207 : : "%qs specified bound %s exceeds "
208 : : "maximum object size %E",
209 : : fname, bndstr, maxobjsize);
210 : : else
211 : : {
212 : 0 : bool maybe = wi::to_wide (size) == bndrng[0];
213 : 0 : warned = warning_at (loc, opt,
214 : : exact
215 : : ? G_("%qs specified bound %s exceeds "
216 : : "the size %E of unterminated array")
217 : : : (maybe
218 : 0 : ? G_("%qs specified bound %s may "
219 : : "exceed the size of at most %E "
220 : : "of unterminated array")
221 : : : G_("%qs specified bound %s exceeds "
222 : : "the size of at most %E "
223 : : "of unterminated array")),
224 : : fname, bndstr, size);
225 : : }
226 : : }
227 : : else
228 : 6117 : warned = warning_at (loc, opt,
229 : : "%qs argument missing terminating nul",
230 : : fname);
231 : : }
232 : :
233 : 6891 : if (warned)
234 : : {
235 : 521 : inform (get_location (decl),
236 : : "referenced argument declared here");
237 : 521 : suppress_warning (arg, opt);
238 : 521 : if (expr)
239 : 441 : suppress_warning (expr, opt);
240 : : }
241 : 6891 : }
242 : :
243 : : void
244 : 828 : warn_string_no_nul (location_t loc, gimple *stmt, const char *fname,
245 : : tree arg, tree decl, tree size /* = NULL_TREE */,
246 : : bool exact /* = false */,
247 : : const wide_int bndrng[2] /* = NULL */)
248 : : {
249 : 828 : return warn_string_no_nul<gimple *> (loc, stmt, fname,
250 : 828 : arg, decl, size, exact, bndrng);
251 : : }
252 : :
253 : : void
254 : 7173 : warn_string_no_nul (location_t loc, tree expr, const char *fname,
255 : : tree arg, tree decl, tree size /* = NULL_TREE */,
256 : : bool exact /* = false */,
257 : : const wide_int bndrng[2] /* = NULL */)
258 : : {
259 : 7173 : return warn_string_no_nul<tree> (loc, expr, fname,
260 : 7173 : arg, decl, size, exact, bndrng);
261 : : }
262 : :
263 : : /* If EXP refers to an unterminated constant character array return
264 : : the declaration of the object of which the array is a member or
265 : : element and if SIZE is not null, set *SIZE to the size of
266 : : the unterminated array and set *EXACT if the size is exact or
267 : : clear it otherwise. Otherwise return null. */
268 : :
269 : : tree
270 : 703712 : unterminated_array (tree exp, tree *size /* = NULL */, bool *exact /* = NULL */)
271 : : {
272 : : /* C_STRLEN will return NULL and set DECL in the info
273 : : structure if EXP references a unterminated array. */
274 : 703712 : c_strlen_data lendata = { };
275 : 703712 : tree len = c_strlen (exp, 1, &lendata);
276 : 703712 : if (len || !lendata.minlen || !lendata.decl)
277 : : return NULL_TREE;
278 : :
279 : 1860 : if (!size)
280 : : return lendata.decl;
281 : :
282 : 1860 : len = lendata.minlen;
283 : 1860 : if (lendata.off)
284 : : {
285 : : /* Constant offsets are already accounted for in LENDATA.MINLEN,
286 : : but not in a SSA_NAME + CST expression. */
287 : 1860 : if (TREE_CODE (lendata.off) == INTEGER_CST)
288 : 1464 : *exact = true;
289 : 396 : else if (TREE_CODE (lendata.off) == PLUS_EXPR
290 : 396 : && TREE_CODE (TREE_OPERAND (lendata.off, 1)) == INTEGER_CST)
291 : : {
292 : : /* Subtract the offset from the size of the array. */
293 : 121 : *exact = false;
294 : 121 : tree temp = TREE_OPERAND (lendata.off, 1);
295 : 121 : temp = fold_convert (ssizetype, temp);
296 : 121 : len = fold_build2 (MINUS_EXPR, ssizetype, len, temp);
297 : : }
298 : : else
299 : 275 : *exact = false;
300 : : }
301 : : else
302 : 0 : *exact = true;
303 : :
304 : 1860 : *size = len;
305 : 1860 : return lendata.decl;
306 : : }
307 : :
308 : : /* For a call EXPR (which may be null) that expects a string argument
309 : : SRC as an argument, returns false if SRC is a character array with
310 : : no terminating NUL. When nonnull, BOUND is the number of characters
311 : : in which to expect the terminating NUL. When EXPR is nonnull also
312 : : issues a warning. */
313 : :
314 : : template <class GimpleOrTree>
315 : : static bool
316 : 696292 : check_nul_terminated_array (GimpleOrTree expr, tree src, tree bound)
317 : : {
318 : : /* The constant size of the array SRC points to. The actual size
319 : : may be less of EXACT is true, but not more. */
320 : : tree size;
321 : : /* True if SRC involves a non-constant offset into the array. */
322 : : bool exact;
323 : : /* The unterminated constant array SRC points to. */
324 : 696292 : tree nonstr = unterminated_array (src, &size, &exact);
325 : 696292 : if (!nonstr)
326 : : return true;
327 : :
328 : : /* NONSTR refers to the non-nul terminated constant array and SIZE
329 : : is the constant size of the array in bytes. EXACT is true when
330 : : SIZE is exact. */
331 : :
332 : 5115 : wide_int bndrng[2];
333 : 1023 : if (bound)
334 : : {
335 : 357 : int_range_max r (TREE_TYPE (bound));
336 : :
337 : 714 : get_range_query (cfun)->range_of_expr (r, bound);
338 : :
339 : 357 : if (r.undefined_p () || r.varying_p ())
340 : : return true;
341 : :
342 : 356 : bndrng[0] = r.lower_bound ();
343 : 356 : bndrng[1] = r.upper_bound ();
344 : :
345 : 356 : if (exact)
346 : : {
347 : 206 : if (wi::leu_p (bndrng[0], wi::to_wide (size)))
348 : : return true;
349 : : }
350 : 150 : else if (wi::lt_p (bndrng[0], wi::to_wide (size), UNSIGNED))
351 : : return true;
352 : 357 : }
353 : :
354 : 813 : if (expr)
355 : 1077 : warn_string_no_nul (get_location (expr), expr, NULL, src, nonstr,
356 : : size, exact, bound ? bndrng : NULL);
357 : :
358 : : return false;
359 : 3069 : }
360 : :
361 : : bool
362 : 398652 : check_nul_terminated_array (gimple *stmt, tree src, tree bound /* = NULL_TREE */)
363 : : {
364 : 398652 : return check_nul_terminated_array<gimple *>(stmt, src, bound);
365 : : }
366 : :
367 : : bool
368 : 284987 : check_nul_terminated_array (tree expr, tree src, tree bound /* = NULL_TREE */)
369 : : {
370 : 284987 : return check_nul_terminated_array<tree>(expr, src, bound);
371 : : }
372 : :
373 : : /* Warn about passing a non-string array/pointer to a built-in function
374 : : that expects a nul-terminated string argument. Returns true if
375 : : a warning has been issued.*/
376 : :
377 : : template <class GimpleOrTree>
378 : : static bool
379 : 7096141 : maybe_warn_nonstring_arg (tree fndecl, GimpleOrTree exp)
380 : : {
381 : 7096141 : if (!fndecl || !fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
382 : : return false;
383 : :
384 : 2260725 : if (!warn_stringop_overread
385 : 2260725 : || warning_suppressed_p (exp, OPT_Wstringop_overread))
386 : 11775 : return false;
387 : :
388 : : /* Avoid clearly invalid calls (more checking done below). */
389 : 2248950 : unsigned nargs = call_nargs (exp);
390 : 2248950 : if (!nargs)
391 : : return false;
392 : :
393 : : /* The bound argument to a bounded string function like strncpy. */
394 : 1938376 : tree bound = NULL_TREE;
395 : :
396 : : /* The longest known or possible string argument to one of the comparison
397 : : functions. If the length is less than the bound it is used instead.
398 : : Since the length is only used for warning and not for code generation
399 : : disable strict mode in the calls to get_range_strlen below. */
400 : 1938376 : tree maxlen = NULL_TREE;
401 : :
402 : : /* It's safe to call "bounded" string functions with a non-string
403 : : argument since the functions provide an explicit bound for this
404 : : purpose. The exception is strncat where the bound may refer to
405 : : either the destination or the source. */
406 : 1938376 : int fncode = DECL_FUNCTION_CODE (fndecl);
407 : 1938376 : switch (fncode)
408 : : {
409 : : case BUILT_IN_STRCMP:
410 : : case BUILT_IN_STRNCMP:
411 : : case BUILT_IN_STRNCASECMP:
412 : : {
413 : : /* For these, if one argument refers to one or more of a set
414 : : of string constants or arrays of known size, determine
415 : : the range of their known or possible lengths and use it
416 : : conservatively as the bound for the unbounded function,
417 : : and to adjust the range of the bound of the bounded ones. */
418 : 392517 : for (unsigned argno = 0;
419 : 784112 : argno < MIN (nargs, 2)
420 : 784112 : && !(maxlen && TREE_CODE (maxlen) == INTEGER_CST); argno++)
421 : : {
422 : 392517 : tree arg = call_arg (exp, argno);
423 : 392517 : if (!get_attr_nonstring_decl (arg))
424 : : {
425 : 391577 : c_strlen_data lendata = { };
426 : : /* Set MAXBOUND to an arbitrary non-null non-integer
427 : : node as a request to have it set to the length of
428 : : the longest string in a PHI. */
429 : 391577 : lendata.maxbound = arg;
430 : 391577 : get_range_strlen (arg, &lendata, /* eltsize = */ 1);
431 : 391577 : maxlen = lendata.maxbound;
432 : : }
433 : : }
434 : : }
435 : : /* Fall through. */
436 : :
437 : : case BUILT_IN_STRNCAT:
438 : : case BUILT_IN_STPNCPY:
439 : : case BUILT_IN_STRNCPY:
440 : 397444 : if (nargs > 2)
441 : 11170 : bound = call_arg (exp, 2);
442 : : break;
443 : :
444 : 722 : case BUILT_IN_STRNDUP:
445 : 722 : if (nargs < 2)
446 : : return false;
447 : 722 : bound = call_arg (exp, 1);
448 : 722 : break;
449 : :
450 : 1471 : case BUILT_IN_STRNLEN:
451 : : {
452 : 1471 : tree arg = call_arg (exp, 0);
453 : 1471 : if (!get_attr_nonstring_decl (arg))
454 : : {
455 : 1333 : c_strlen_data lendata = { };
456 : : /* Set MAXBOUND to an arbitrary non-null non-integer
457 : : node as a request to have it set to the length of
458 : : the longest string in a PHI. */
459 : 1333 : lendata.maxbound = arg;
460 : 1333 : get_range_strlen (arg, &lendata, /* eltsize = */ 1);
461 : 1333 : maxlen = lendata.maxbound;
462 : : }
463 : 1471 : if (nargs > 1)
464 : 1470 : bound = call_arg (exp, 1);
465 : : break;
466 : : }
467 : :
468 : : default:
469 : : break;
470 : : }
471 : :
472 : : /* Determine the range of the bound argument (if specified). */
473 : 1938376 : tree bndrng[2] = { NULL_TREE, NULL_TREE };
474 : 1938376 : if (bound)
475 : : {
476 : 13362 : STRIP_NOPS (bound);
477 : 13362 : get_size_range (bound, bndrng);
478 : : }
479 : :
480 : 1938376 : location_t loc = get_location (exp);
481 : :
482 : 1938376 : if (bndrng[0])
483 : : {
484 : : /* Diagnose excessive bound prior to the adjustment below and
485 : : regardless of attribute nonstring. */
486 : 13342 : tree maxobjsize = max_object_size ();
487 : 13342 : if (tree_int_cst_lt (maxobjsize, bndrng[0]))
488 : : {
489 : 131 : bool warned = false;
490 : 131 : if (tree_int_cst_equal (bndrng[0], bndrng[1]))
491 : 114 : warned = warning_at (loc, OPT_Wstringop_overread,
492 : : "%qD specified bound %E "
493 : : "exceeds maximum object size %E",
494 : : fndecl, bndrng[0], maxobjsize);
495 : : else
496 : 17 : warned = warning_at (loc, OPT_Wstringop_overread,
497 : : "%qD specified bound [%E, %E] "
498 : : "exceeds maximum object size %E",
499 : : fndecl, bndrng[0], bndrng[1],
500 : : maxobjsize);
501 : 131 : if (warned)
502 : 45 : suppress_warning (exp, OPT_Wstringop_overread);
503 : :
504 : 131 : return warned;
505 : : }
506 : : }
507 : :
508 : 1938245 : if (maxlen && !integer_all_onesp (maxlen))
509 : : {
510 : : /* Add one for the nul. */
511 : 7183 : maxlen = const_binop (PLUS_EXPR, TREE_TYPE (maxlen), maxlen,
512 : : size_one_node);
513 : :
514 : 7183 : if (!bndrng[0])
515 : : {
516 : : /* Conservatively use the upper bound of the lengths for
517 : : both the lower and the upper bound of the operation. */
518 : 4829 : bndrng[0] = maxlen;
519 : 4829 : bndrng[1] = maxlen;
520 : 4829 : bound = void_type_node;
521 : : }
522 : 2354 : else if (maxlen)
523 : : {
524 : : /* Replace the bound on the operation with the upper bound
525 : : of the length of the string if the latter is smaller. */
526 : 2345 : if (tree_int_cst_lt (maxlen, bndrng[0]))
527 : 200 : bndrng[0] = maxlen;
528 : 2145 : else if (tree_int_cst_lt (maxlen, bndrng[1]))
529 : 523 : bndrng[1] = maxlen;
530 : : }
531 : : }
532 : :
533 : 1938245 : bool any_arg_warned = false;
534 : : /* Iterate over the built-in function's formal arguments and check
535 : : each const char* against the actual argument. If the actual
536 : : argument is declared attribute non-string issue a warning unless
537 : : the argument's maximum length is bounded. */
538 : : function_args_iterator it;
539 : 1938245 : function_args_iter_init (&it, TREE_TYPE (fndecl));
540 : :
541 : 6277494 : for (unsigned argno = 0; ; ++argno, function_args_iter_next (&it))
542 : : {
543 : : /* Avoid iterating past the declared argument in a call
544 : : to function declared without a prototype. */
545 : 6274540 : if (argno >= nargs)
546 : : break;
547 : :
548 : 4478586 : tree argtype = function_args_iter_cond (&it);
549 : 4478586 : if (!argtype)
550 : : break;
551 : :
552 : 4336295 : if (TREE_CODE (argtype) != POINTER_TYPE)
553 : 4333341 : continue;
554 : :
555 : 2674118 : argtype = TREE_TYPE (argtype);
556 : :
557 : 4377811 : if (TREE_CODE (argtype) != INTEGER_TYPE
558 : 2674118 : || !TYPE_READONLY (argtype))
559 : 1703693 : continue;
560 : :
561 : 970425 : argtype = TYPE_MAIN_VARIANT (argtype);
562 : 970425 : if (argtype != char_type_node)
563 : 11596 : continue;
564 : :
565 : 958829 : tree callarg = call_arg (exp, argno);
566 : 958829 : if (TREE_CODE (callarg) == ADDR_EXPR)
567 : 523249 : callarg = TREE_OPERAND (callarg, 0);
568 : :
569 : : /* See if the destination is declared with attribute "nonstring". */
570 : 958829 : tree decl = get_attr_nonstring_decl (callarg);
571 : 958829 : if (!decl)
572 : 955875 : continue;
573 : :
574 : : /* The maximum number of array elements accessed. */
575 : 2954 : offset_int wibnd = 0;
576 : :
577 : 2954 : if (argno && fncode == BUILT_IN_STRNCAT)
578 : : {
579 : : /* See if the bound in strncat is derived from the length
580 : : of the strlen of the destination (as it's expected to be).
581 : : If so, reset BOUND and FNCODE to trigger a warning. */
582 : 488 : tree dstarg = call_arg (exp, 0);
583 : 488 : if (is_strlen_related_p (dstarg, bound))
584 : : {
585 : : /* The bound applies to the destination, not to the source,
586 : : so reset these to trigger a warning without mentioning
587 : : the bound. */
588 : : bound = NULL;
589 : : fncode = 0;
590 : : }
591 : 480 : else if (bndrng[1])
592 : : /* Use the upper bound of the range for strncat. */
593 : 480 : wibnd = wi::to_offset (bndrng[1]);
594 : : }
595 : 2466 : else if (bndrng[0])
596 : : /* Use the lower bound of the range for functions other than
597 : : strncat. */
598 : 2010 : wibnd = wi::to_offset (bndrng[0]);
599 : :
600 : : /* Determine the size of the argument array if it is one. */
601 : 2954 : offset_int asize = wibnd;
602 : 2954 : bool known_size = false;
603 : 2954 : tree type = TREE_TYPE (decl);
604 : :
605 : 2954 : while (TREE_CODE (type) == ARRAY_TYPE
606 : 4146 : && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
607 : 1192 : type = TREE_TYPE (type);
608 : :
609 : : /* Determine the array size. For arrays of unknown bound and
610 : : pointers reset BOUND to trigger the appropriate warning. */
611 : 2954 : if (TREE_CODE (type) == ARRAY_TYPE)
612 : : {
613 : 2490 : if (tree arrbnd = TYPE_DOMAIN (type))
614 : : {
615 : 2428 : if ((arrbnd = TYPE_MAX_VALUE (arrbnd))
616 : 2428 : && TREE_CODE (arrbnd) == INTEGER_CST)
617 : : {
618 : 2426 : asize = wi::to_offset (arrbnd) + 1;
619 : 2426 : known_size = true;
620 : : }
621 : : }
622 : 62 : else if (bound == void_type_node)
623 : 2954 : bound = NULL_TREE;
624 : : }
625 : 464 : else if (bound == void_type_node)
626 : 2954 : bound = NULL_TREE;
627 : :
628 : : /* In a call to strncat with a bound in a range whose lower but
629 : : not upper bound is less than the array size, reset ASIZE to
630 : : be the same as the bound and the other variable to trigger
631 : : the appropriate warning below. */
632 : : if (fncode == BUILT_IN_STRNCAT
633 : 480 : && bndrng[0] != bndrng[1]
634 : 3114 : && wi::ltu_p (wi::to_offset (bndrng[0]), asize)
635 : 3146 : && (!known_size
636 : 184 : || wi::ltu_p (asize, wibnd)))
637 : : {
638 : 32 : asize = wibnd;
639 : 32 : bound = NULL_TREE;
640 : 32 : fncode = 0;
641 : : }
642 : :
643 : 2954 : bool warned = false;
644 : :
645 : 2954 : auto_diagnostic_group d;
646 : 2954 : if (wi::ltu_p (asize, wibnd))
647 : : {
648 : 369 : if (bndrng[0] == bndrng[1])
649 : 312 : warned = warning_at (loc, OPT_Wstringop_overread,
650 : : "%qD argument %i declared attribute "
651 : : "%<nonstring%> is smaller than the specified "
652 : : "bound %wu",
653 : : fndecl, argno + 1, wibnd.to_uhwi ());
654 : 57 : else if (wi::ltu_p (asize, wi::to_offset (bndrng[0])))
655 : 41 : warned = warning_at (loc, OPT_Wstringop_overread,
656 : : "%qD argument %i declared attribute "
657 : : "%<nonstring%> is smaller than "
658 : : "the specified bound [%E, %E]",
659 : : fndecl, argno + 1, bndrng[0], bndrng[1]);
660 : : else
661 : 16 : warned = warning_at (loc, OPT_Wstringop_overread,
662 : : "%qD argument %i declared attribute "
663 : : "%<nonstring%> may be smaller than "
664 : : "the specified bound [%E, %E]",
665 : : fndecl, argno + 1, bndrng[0], bndrng[1]);
666 : : }
667 : 2585 : else if (fncode == BUILT_IN_STRNCAT)
668 : : ; /* Avoid warning for calls to strncat() when the bound
669 : : is equal to the size of the non-string argument. */
670 : 2177 : else if (!bound)
671 : 528 : warned = warning_at (loc, OPT_Wstringop_overread,
672 : : "%qD argument %i declared attribute %<nonstring%>",
673 : : fndecl, argno + 1);
674 : :
675 : 897 : if (warned)
676 : : {
677 : 897 : inform (DECL_SOURCE_LOCATION (decl),
678 : : "argument %qD declared here", decl);
679 : 897 : any_arg_warned = true;
680 : : }
681 : : }
682 : :
683 : 1938245 : if (any_arg_warned)
684 : 897 : suppress_warning (exp, OPT_Wstringop_overread);
685 : :
686 : : return any_arg_warned;
687 : : }
688 : :
689 : : bool
690 : 512370 : maybe_warn_nonstring_arg (tree fndecl, gimple *stmt)
691 : : {
692 : 512370 : return maybe_warn_nonstring_arg<gimple *>(fndecl, stmt);
693 : : }
694 : :
695 : :
696 : : bool
697 : 356 : maybe_warn_nonstring_arg (tree fndecl, tree expr)
698 : : {
699 : 356 : return maybe_warn_nonstring_arg<tree>(fndecl, expr);
700 : : }
701 : :
702 : : /* Issue a warning OPT for a bounded call EXP with a bound in RANGE
703 : : accessing an object with SIZE. */
704 : :
705 : : template <class GimpleOrTree>
706 : : static bool
707 : 454 : maybe_warn_for_bound (opt_code opt, location_t loc, GimpleOrTree exp, tree func,
708 : : tree bndrng[2], tree size, const access_data *pad)
709 : : {
710 : 454 : if (!bndrng[0] || warning_suppressed_p (exp, opt))
711 : 90 : return false;
712 : :
713 : 364 : tree maxobjsize = max_object_size ();
714 : :
715 : 364 : bool warned = false;
716 : :
717 : 364 : if (opt == OPT_Wstringop_overread)
718 : : {
719 : 162 : bool maybe = pad && pad->src.phi ();
720 : : if (maybe)
721 : : {
722 : : /* Issue a "maybe" warning only if the PHI refers to objects
723 : : at least one of which has more space remaining than the bound.
724 : : Otherwise, if the bound is greater, use the definitive form. */
725 : 2 : offset_int remmax = pad->src.size_remaining ();
726 : 2 : if (remmax < wi::to_offset (bndrng[0]))
727 : 2 : maybe = false;
728 : : }
729 : :
730 : 162 : auto_diagnostic_group d;
731 : 162 : if (tree_int_cst_lt (maxobjsize, bndrng[0]))
732 : : {
733 : 75 : if (bndrng[0] == bndrng[1])
734 : 73 : warned = (func
735 : 146 : ? warning_at (loc, opt,
736 : : (maybe
737 : : ? G_("%qD specified bound %E may "
738 : : "exceed maximum object size %E")
739 : : : G_("%qD specified bound %E "
740 : : "exceeds maximum object size %E")),
741 : : func, bndrng[0], maxobjsize)
742 : 73 : : warning_at (loc, opt,
743 : : (maybe
744 : : ? G_("specified bound %E may "
745 : : "exceed maximum object size %E")
746 : : : G_("specified bound %E "
747 : : "exceeds maximum object size %E")),
748 : : bndrng[0], maxobjsize));
749 : : else
750 : 2 : warned = (func
751 : 4 : ? warning_at (loc, opt,
752 : : (maybe
753 : : ? G_("%qD specified bound [%E, %E] may "
754 : : "exceed maximum object size %E")
755 : : : G_("%qD specified bound [%E, %E] "
756 : : "exceeds maximum object size %E")),
757 : : func,
758 : : bndrng[0], bndrng[1], maxobjsize)
759 : 2 : : warning_at (loc, opt,
760 : : (maybe
761 : : ? G_("specified bound [%E, %E] may "
762 : : "exceed maximum object size %E")
763 : : : G_("specified bound [%E, %E] "
764 : : "exceeds maximum object size %E")),
765 : : bndrng[0], bndrng[1], maxobjsize));
766 : : }
767 : 87 : else if (!size || tree_int_cst_le (bndrng[0], size))
768 : 4 : return false;
769 : 83 : else if (tree_int_cst_equal (bndrng[0], bndrng[1]))
770 : 73 : warned = (func
771 : 146 : ? warning_at (loc, opt,
772 : : (maybe
773 : : ? G_("%qD specified bound %E may exceed "
774 : : "source size %E")
775 : : : G_("%qD specified bound %E exceeds "
776 : : "source size %E")),
777 : : func, bndrng[0], size)
778 : 73 : : warning_at (loc, opt,
779 : : (maybe
780 : : ? G_("specified bound %E may exceed "
781 : : "source size %E")
782 : : : G_("specified bound %E exceeds "
783 : : "source size %E")),
784 : : bndrng[0], size));
785 : : else
786 : 10 : warned = (func
787 : 20 : ? warning_at (loc, opt,
788 : : (maybe
789 : : ? G_("%qD specified bound [%E, %E] may "
790 : : "exceed source size %E")
791 : : : G_("%qD specified bound [%E, %E] exceeds "
792 : : "source size %E")),
793 : : func, bndrng[0], bndrng[1], size)
794 : 10 : : warning_at (loc, opt,
795 : : (maybe
796 : : ? G_("specified bound [%E, %E] may exceed "
797 : : "source size %E")
798 : : : G_("specified bound [%E, %E] exceeds "
799 : : "source size %E")),
800 : : bndrng[0], bndrng[1], size));
801 : 158 : if (warned)
802 : : {
803 : 77 : if (pad && pad->src.ref
804 : 150 : && has_location (pad->src.ref))
805 : 49 : inform (get_location (pad->src.ref),
806 : : "source object allocated here");
807 : 78 : suppress_warning (exp, opt);
808 : : }
809 : :
810 : 158 : return warned;
811 : 162 : }
812 : :
813 : 202 : bool maybe = pad && pad->dst.phi ();
814 : : if (maybe)
815 : : {
816 : : /* Issue a "maybe" warning only if the PHI refers to objects
817 : : at least one of which has more space remaining than the bound.
818 : : Otherwise, if the bound is greater, use the definitive form. */
819 : 5 : offset_int remmax = pad->dst.size_remaining ();
820 : 5 : if (remmax < wi::to_offset (bndrng[0]))
821 : 5 : maybe = false;
822 : : }
823 : 202 : if (tree_int_cst_lt (maxobjsize, bndrng[0]))
824 : : {
825 : 91 : if (bndrng[0] == bndrng[1])
826 : 79 : warned = (func
827 : 158 : ? warning_at (loc, opt,
828 : : (maybe
829 : : ? G_("%qD specified size %E may "
830 : : "exceed maximum object size %E")
831 : : : G_("%qD specified size %E "
832 : : "exceeds maximum object size %E")),
833 : : func, bndrng[0], maxobjsize)
834 : 79 : : warning_at (loc, opt,
835 : : (maybe
836 : : ? G_("specified size %E may exceed "
837 : : "maximum object size %E")
838 : : : G_("specified size %E exceeds "
839 : : "maximum object size %E")),
840 : : bndrng[0], maxobjsize));
841 : : else
842 : 12 : warned = (func
843 : 24 : ? warning_at (loc, opt,
844 : : (maybe
845 : : ? G_("%qD specified size between %E and %E "
846 : : "may exceed maximum object size %E")
847 : : : G_("%qD specified size between %E and %E "
848 : : "exceeds maximum object size %E")),
849 : : func, bndrng[0], bndrng[1], maxobjsize)
850 : 12 : : warning_at (loc, opt,
851 : : (maybe
852 : : ? G_("specified size between %E and %E "
853 : : "may exceed maximum object size %E")
854 : : : G_("specified size between %E and %E "
855 : : "exceeds maximum object size %E")),
856 : : bndrng[0], bndrng[1], maxobjsize));
857 : : }
858 : 111 : else if (!size || tree_int_cst_le (bndrng[0], size))
859 : 0 : return false;
860 : 111 : else if (tree_int_cst_equal (bndrng[0], bndrng[1]))
861 : 102 : warned = (func
862 : 204 : ? warning_at (loc, opt,
863 : : (maybe
864 : : ? G_("%qD specified bound %E may exceed "
865 : : "destination size %E")
866 : : : G_("%qD specified bound %E exceeds "
867 : : "destination size %E")),
868 : : func, bndrng[0], size)
869 : 102 : : warning_at (loc, opt,
870 : : (maybe
871 : : ? G_("specified bound %E may exceed "
872 : : "destination size %E")
873 : : : G_("specified bound %E exceeds "
874 : : "destination size %E")),
875 : : bndrng[0], size));
876 : : else
877 : 9 : warned = (func
878 : 18 : ? warning_at (loc, opt,
879 : : (maybe
880 : : ? G_("%qD specified bound [%E, %E] may exceed "
881 : : "destination size %E")
882 : : : G_("%qD specified bound [%E, %E] exceeds "
883 : : "destination size %E")),
884 : : func, bndrng[0], bndrng[1], size)
885 : 9 : : warning_at (loc, opt,
886 : : (maybe
887 : : ? G_("specified bound [%E, %E] exceeds "
888 : : "destination size %E")
889 : : : G_("specified bound [%E, %E] exceeds "
890 : : "destination size %E")),
891 : : bndrng[0], bndrng[1], size));
892 : :
893 : 202 : if (warned)
894 : : {
895 : 48 : if (pad && pad->dst.ref
896 : 97 : && has_location (pad->dst.ref))
897 : 43 : inform (get_location (pad->dst.ref),
898 : : "destination object allocated here");
899 : 52 : suppress_warning (exp, opt);
900 : : }
901 : :
902 : : return warned;
903 : : }
904 : :
905 : : bool
906 : 267 : maybe_warn_for_bound (opt_code opt, location_t loc, gimple *stmt, tree func,
907 : : tree bndrng[2], tree size,
908 : : const access_data *pad /* = NULL */)
909 : : {
910 : 267 : return maybe_warn_for_bound<gimple *> (opt, loc, stmt, func, bndrng, size,
911 : 267 : pad);
912 : : }
913 : :
914 : : bool
915 : 73 : maybe_warn_for_bound (opt_code opt, location_t loc, tree expr, tree func,
916 : : tree bndrng[2], tree size,
917 : : const access_data *pad /* = NULL */)
918 : : {
919 : 73 : return maybe_warn_for_bound<tree> (opt, loc, expr, func, bndrng, size, pad);
920 : : }
921 : :
922 : : /* For an expression EXP issue an access warning controlled by option OPT
923 : : with access to a region SIZE bytes in size in the RANGE of sizes.
924 : : WRITE is true for a write access, READ for a read access, neither for
925 : : call that may or may not perform an access but for which the range
926 : : is expected to valid.
927 : : Returns true when a warning has been issued. */
928 : :
929 : : template <class GimpleOrTree>
930 : : static bool
931 : 1889 : warn_for_access (location_t loc, tree func, GimpleOrTree exp, int opt,
932 : : tree range[2], tree size, bool write, bool read, bool maybe)
933 : : {
934 : 1889 : bool warned = false;
935 : :
936 : 1889 : if (write && read)
937 : : {
938 : 138 : if (tree_int_cst_equal (range[0], range[1]))
939 : 138 : warned = (func
940 : 264 : ? warning_n (loc, opt, tree_to_uhwi (range[0]),
941 : : (maybe
942 : : ? G_("%qD may access %E byte in a region "
943 : : "of size %E")
944 : : : G_("%qD accessing %E byte in a region "
945 : : "of size %E")),
946 : : (maybe
947 : : ? G_ ("%qD may access %E bytes in a region "
948 : : "of size %E")
949 : : : G_ ("%qD accessing %E bytes in a region "
950 : : "of size %E")),
951 : : func, range[0], size)
952 : 150 : : warning_n (loc, opt, tree_to_uhwi (range[0]),
953 : : (maybe
954 : : ? G_("may access %E byte in a region "
955 : : "of size %E")
956 : : : G_("accessing %E byte in a region "
957 : : "of size %E")),
958 : : (maybe
959 : : ? G_("may access %E bytes in a region "
960 : : "of size %E")
961 : : : G_("accessing %E bytes in a region "
962 : : "of size %E")),
963 : : range[0], size));
964 : 0 : else if (tree_int_cst_sign_bit (range[1]))
965 : : {
966 : : /* Avoid printing the upper bound if it's invalid. */
967 : 0 : warned = (func
968 : 0 : ? warning_at (loc, opt,
969 : : (maybe
970 : : ? G_("%qD may access %E or more bytes "
971 : : "in a region of size %E")
972 : : : G_("%qD accessing %E or more bytes "
973 : : "in a region of size %E")),
974 : : func, range[0], size)
975 : 0 : : warning_at (loc, opt,
976 : : (maybe
977 : : ? G_("may access %E or more bytes "
978 : : "in a region of size %E")
979 : : : G_("accessing %E or more bytes "
980 : : "in a region of size %E")),
981 : : range[0], size));
982 : : }
983 : : else
984 : 0 : warned = (func
985 : 0 : ? warning_at (loc, opt,
986 : : (maybe
987 : : ? G_("%qD may access between %E and %E "
988 : : "bytes in a region of size %E")
989 : : : G_("%qD accessing between %E and %E "
990 : : "bytes in a region of size %E")),
991 : : func, range[0], range[1], size)
992 : 0 : : warning_at (loc, opt,
993 : : (maybe
994 : : ? G_("may access between %E and %E bytes "
995 : : "in a region of size %E")
996 : : : G_("accessing between %E and %E bytes "
997 : : "in a region of size %E")),
998 : : range[0], range[1], size));
999 : 138 : return warned;
1000 : : }
1001 : :
1002 : 1751 : if (write)
1003 : : {
1004 : 1174 : if (tree_int_cst_equal (range[0], range[1]))
1005 : 1061 : warned = (func
1006 : 2120 : ? warning_n (loc, opt, tree_to_uhwi (range[0]),
1007 : : (maybe
1008 : : ? G_("%qD may write %E byte into a region "
1009 : : "of size %E")
1010 : : : G_("%qD writing %E byte into a region "
1011 : : "of size %E overflows the destination")),
1012 : : (maybe
1013 : : ? G_("%qD may write %E bytes into a region "
1014 : : "of size %E")
1015 : : : G_("%qD writing %E bytes into a region "
1016 : : "of size %E overflows the destination")),
1017 : : func, range[0], size)
1018 : 1063 : : warning_n (loc, opt, tree_to_uhwi (range[0]),
1019 : : (maybe
1020 : : ? G_("may write %E byte into a region "
1021 : : "of size %E")
1022 : : : G_("writing %E byte into a region "
1023 : : "of size %E overflows the destination")),
1024 : : (maybe
1025 : : ? G_("may write %E bytes into a region "
1026 : : "of size %E")
1027 : : : G_("writing %E bytes into a region "
1028 : : "of size %E overflows the destination")),
1029 : : range[0], size));
1030 : 113 : else if (tree_int_cst_sign_bit (range[1]))
1031 : : {
1032 : : /* Avoid printing the upper bound if it's invalid. */
1033 : 51 : warned = (func
1034 : 102 : ? warning_at (loc, opt,
1035 : : (maybe
1036 : : ? G_("%qD may write %E or more bytes "
1037 : : "into a region of size %E")
1038 : : : G_("%qD writing %E or more bytes "
1039 : : "into a region of size %E overflows "
1040 : : "the destination")),
1041 : : func, range[0], size)
1042 : 51 : : warning_at (loc, opt,
1043 : : (maybe
1044 : : ? G_("may write %E or more bytes into "
1045 : : "a region of size %E")
1046 : : : G_("writing %E or more bytes into "
1047 : : "a region of size %E overflows "
1048 : : "the destination")),
1049 : : range[0], size));
1050 : : }
1051 : : else
1052 : 62 : warned = (func
1053 : 124 : ? warning_at (loc, opt,
1054 : : (maybe
1055 : : ? G_("%qD may write between %E and %E bytes "
1056 : : "into a region of size %E")
1057 : : : G_("%qD writing between %E and %E bytes "
1058 : : "into a region of size %E overflows "
1059 : : "the destination")),
1060 : : func, range[0], range[1], size)
1061 : 62 : : warning_at (loc, opt,
1062 : : (maybe
1063 : : ? G_("may write between %E and %E bytes "
1064 : : "into a region of size %E")
1065 : : : G_("writing between %E and %E bytes "
1066 : : "into a region of size %E overflows "
1067 : : "the destination")),
1068 : : range[0], range[1], size));
1069 : 1174 : return warned;
1070 : : }
1071 : :
1072 : 577 : if (read)
1073 : : {
1074 : 574 : if (tree_int_cst_equal (range[0], range[1]))
1075 : 246 : warned = (func
1076 : 490 : ? warning_n (loc, OPT_Wstringop_overread,
1077 : : tree_to_uhwi (range[0]),
1078 : : (maybe
1079 : : ? G_("%qD may read %E byte from a region "
1080 : : "of size %E")
1081 : : : G_("%qD reading %E byte from a region "
1082 : : "of size %E")),
1083 : : (maybe
1084 : : ? G_("%qD may read %E bytes from a region "
1085 : : "of size %E")
1086 : : : G_("%qD reading %E bytes from a region "
1087 : : "of size %E")),
1088 : : func, range[0], size)
1089 : 248 : : warning_n (loc, OPT_Wstringop_overread,
1090 : : tree_to_uhwi (range[0]),
1091 : : (maybe
1092 : : ? G_("may read %E byte from a region "
1093 : : "of size %E")
1094 : : : G_("reading %E byte from a region "
1095 : : "of size %E")),
1096 : : (maybe
1097 : : ? G_("may read %E bytes from a region "
1098 : : "of size %E")
1099 : : : G_("reading %E bytes from a region "
1100 : : "of size %E")),
1101 : : range[0], size));
1102 : 328 : else if (tree_int_cst_sign_bit (range[1]))
1103 : : {
1104 : : /* Avoid printing the upper bound if it's invalid. */
1105 : 305 : warned = (func
1106 : 610 : ? warning_at (loc, OPT_Wstringop_overread,
1107 : : (maybe
1108 : : ? G_("%qD may read %E or more bytes "
1109 : : "from a region of size %E")
1110 : : : G_("%qD reading %E or more bytes "
1111 : : "from a region of size %E")),
1112 : : func, range[0], size)
1113 : 305 : : warning_at (loc, OPT_Wstringop_overread,
1114 : : (maybe
1115 : : ? G_("may read %E or more bytes "
1116 : : "from a region of size %E")
1117 : : : G_("reading %E or more bytes "
1118 : : "from a region of size %E")),
1119 : : range[0], size));
1120 : : }
1121 : : else
1122 : 23 : warned = (func
1123 : 46 : ? warning_at (loc, OPT_Wstringop_overread,
1124 : : (maybe
1125 : : ? G_("%qD may read between %E and %E bytes "
1126 : : "from a region of size %E")
1127 : : : G_("%qD reading between %E and %E bytes "
1128 : : "from a region of size %E")),
1129 : : func, range[0], range[1], size)
1130 : 23 : : warning_at (loc, opt,
1131 : : (maybe
1132 : : ? G_("may read between %E and %E bytes "
1133 : : "from a region of size %E")
1134 : : : G_("reading between %E and %E bytes "
1135 : : "from a region of size %E")),
1136 : : range[0], range[1], size));
1137 : :
1138 : 574 : if (warned)
1139 : 400 : suppress_warning (exp, OPT_Wstringop_overread);
1140 : :
1141 : 574 : return warned;
1142 : : }
1143 : :
1144 : 3 : if (tree_int_cst_equal (range[0], range[1])
1145 : 3 : || tree_int_cst_sign_bit (range[1]))
1146 : 3 : warned = (func
1147 : 3 : ? warning_n (loc, OPT_Wstringop_overread,
1148 : : tree_to_uhwi (range[0]),
1149 : : "%qD expecting %E byte in a region of size %E",
1150 : : "%qD expecting %E bytes in a region of size %E",
1151 : : func, range[0], size)
1152 : 3 : : warning_n (loc, OPT_Wstringop_overread,
1153 : : tree_to_uhwi (range[0]),
1154 : : "expecting %E byte in a region of size %E",
1155 : : "expecting %E bytes in a region of size %E",
1156 : : range[0], size));
1157 : 0 : else if (tree_int_cst_sign_bit (range[1]))
1158 : : {
1159 : : /* Avoid printing the upper bound if it's invalid. */
1160 : 0 : warned = (func
1161 : 0 : ? warning_at (loc, OPT_Wstringop_overread,
1162 : : "%qD expecting %E or more bytes in a region "
1163 : : "of size %E",
1164 : : func, range[0], size)
1165 : 0 : : warning_at (loc, OPT_Wstringop_overread,
1166 : : "expecting %E or more bytes in a region "
1167 : : "of size %E",
1168 : : range[0], size));
1169 : : }
1170 : : else
1171 : 0 : warned = (func
1172 : 0 : ? warning_at (loc, OPT_Wstringop_overread,
1173 : : "%qD expecting between %E and %E bytes in "
1174 : : "a region of size %E",
1175 : : func, range[0], range[1], size)
1176 : 0 : : warning_at (loc, OPT_Wstringop_overread,
1177 : : "expecting between %E and %E bytes in "
1178 : : "a region of size %E",
1179 : : range[0], range[1], size));
1180 : :
1181 : 3 : if (warned)
1182 : 3 : suppress_warning (exp, OPT_Wstringop_overread);
1183 : :
1184 : : return warned;
1185 : : }
1186 : :
1187 : : static bool
1188 : 1440 : warn_for_access (location_t loc, tree func, gimple *stmt, int opt,
1189 : : tree range[2], tree size, bool write, bool read, bool maybe)
1190 : : {
1191 : 0 : return warn_for_access<gimple *>(loc, func, stmt, opt, range, size,
1192 : 0 : write, read, maybe);
1193 : : }
1194 : :
1195 : : static bool
1196 : 269 : warn_for_access (location_t loc, tree func, tree expr, int opt,
1197 : : tree range[2], tree size, bool write, bool read, bool maybe)
1198 : : {
1199 : 0 : return warn_for_access<tree>(loc, func, expr, opt, range, size,
1200 : 0 : write, read, maybe);
1201 : : }
1202 : :
1203 : : /* Helper to set RANGE to the range of BOUND if it's nonnull, bounded
1204 : : by BNDRNG if nonnull and valid. */
1205 : :
1206 : : static void
1207 : 1509363 : get_size_range (range_query *query, tree bound, gimple *stmt, tree range[2],
1208 : : int flags, const offset_int bndrng[2])
1209 : : {
1210 : 1509363 : if (bound)
1211 : 648320 : get_size_range (query, bound, stmt, range, flags);
1212 : :
1213 : 1509363 : if (!bndrng || (bndrng[0] == 0 && bndrng[1] == HOST_WIDE_INT_M1U))
1214 : 991718 : return;
1215 : :
1216 : 517645 : if (range[0] && TREE_CODE (range[0]) == INTEGER_CST)
1217 : : {
1218 : 517609 : offset_int r[] =
1219 : 517609 : { wi::to_offset (range[0]), wi::to_offset (range[1]) };
1220 : 517609 : if (r[0] < bndrng[0])
1221 : 0 : range[0] = wide_int_to_tree (sizetype, bndrng[0]);
1222 : 517609 : if (bndrng[1] < r[1])
1223 : 19 : range[1] = wide_int_to_tree (sizetype, bndrng[1]);
1224 : 517609 : }
1225 : : else
1226 : : {
1227 : 36 : range[0] = wide_int_to_tree (sizetype, bndrng[0]);
1228 : 36 : range[1] = wide_int_to_tree (sizetype, bndrng[1]);
1229 : : }
1230 : : }
1231 : :
1232 : : /* Try to verify that the sizes and lengths of the arguments to a string
1233 : : manipulation function given by EXP are within valid bounds and that
1234 : : the operation does not lead to buffer overflow or read past the end.
1235 : : Arguments other than EXP may be null. When non-null, the arguments
1236 : : have the following meaning:
1237 : : DST is the destination of a copy call or NULL otherwise.
1238 : : SRC is the source of a copy call or NULL otherwise.
1239 : : DSTWRITE is the number of bytes written into the destination obtained
1240 : : from the user-supplied size argument to the function (such as in
1241 : : memcpy(DST, SRCs, DSTWRITE) or strncpy(DST, DRC, DSTWRITE).
1242 : : MAXREAD is the user-supplied bound on the length of the source sequence
1243 : : (such as in strncat(d, s, N). It specifies the upper limit on the number
1244 : : of bytes to write. If NULL, it's taken to be the same as DSTWRITE.
1245 : : SRCSTR is the source string (such as in strcpy(DST, SRC)) when the
1246 : : expression EXP is a string function call (as opposed to a memory call
1247 : : like memcpy). As an exception, SRCSTR can also be an integer denoting
1248 : : the precomputed size of the source string or object (for functions like
1249 : : memcpy).
1250 : : DSTSIZE is the size of the destination object.
1251 : :
1252 : : When DSTWRITE is null LEN is checked to verify that it doesn't exceed
1253 : : SIZE_MAX.
1254 : :
1255 : : WRITE is true for write accesses, READ is true for reads. Both are
1256 : : false for simple size checks in calls to functions that neither read
1257 : : from nor write to the region.
1258 : :
1259 : : When nonnull, PAD points to a more detailed description of the access.
1260 : :
1261 : : If the call is successfully verified as safe return true, otherwise
1262 : : return false. */
1263 : :
1264 : : template <class GimpleOrTree>
1265 : : static bool
1266 : 818884 : check_access (GimpleOrTree exp, tree dstwrite,
1267 : : tree maxread, tree srcstr, tree dstsize,
1268 : : access_mode mode, const access_data *pad,
1269 : : range_query *rvals)
1270 : : {
1271 : : /* The size of the largest object is half the address space, or
1272 : : PTRDIFF_MAX. (This is way too permissive.) */
1273 : 818884 : tree maxobjsize = max_object_size ();
1274 : :
1275 : : /* Either an approximate/minimum the length of the source string for
1276 : : string functions or the size of the source object for raw memory
1277 : : functions. */
1278 : 818884 : tree slen = NULL_TREE;
1279 : :
1280 : : /* The range of the access in bytes; first set to the write access
1281 : : for functions that write and then read for those that also (or
1282 : : just) read. */
1283 : 818884 : tree range[2] = { NULL_TREE, NULL_TREE };
1284 : :
1285 : : /* Set to true when the exact number of bytes written by a string
1286 : : function like strcpy is not known and the only thing that is
1287 : : known is that it must be at least one (for the terminating nul). */
1288 : 818884 : bool at_least_one = false;
1289 : 818884 : if (srcstr)
1290 : : {
1291 : : /* SRCSTR is normally a pointer to string but as a special case
1292 : : it can be an integer denoting the length of a string. */
1293 : 499033 : if (POINTER_TYPE_P (TREE_TYPE (srcstr)))
1294 : : {
1295 : 405568 : if (!check_nul_terminated_array (exp, srcstr, maxread))
1296 : : /* Return if the array is not nul-terminated and a warning
1297 : : has been issued. */
1298 : 559 : return false;
1299 : :
1300 : : /* Try to determine the range of lengths the source string
1301 : : refers to. If it can be determined and is less than
1302 : : the upper bound given by MAXREAD add one to it for
1303 : : the terminating nul. Otherwise, set it to one for
1304 : : the same reason, or to MAXREAD as appropriate. */
1305 : 405009 : c_strlen_data lendata = { };
1306 : 405009 : get_range_strlen (srcstr, &lendata, /* eltsize = */ 1);
1307 : 405009 : range[0] = lendata.minlen;
1308 : 405009 : range[1] = lendata.maxbound ? lendata.maxbound : lendata.maxlen;
1309 : 405009 : if (range[0]
1310 : 405009 : && TREE_CODE (range[0]) == INTEGER_CST
1311 : 405004 : && TREE_CODE (range[1]) == INTEGER_CST
1312 : 405004 : && (!maxread || TREE_CODE (maxread) == INTEGER_CST))
1313 : : {
1314 : 106589 : if (maxread && tree_int_cst_le (maxread, range[0]))
1315 : 516 : range[0] = range[1] = maxread;
1316 : : else
1317 : 394641 : range[0] = fold_build2 (PLUS_EXPR, size_type_node,
1318 : : range[0], size_one_node);
1319 : :
1320 : 395157 : if (maxread && tree_int_cst_le (maxread, range[1]))
1321 : 84663 : range[1] = maxread;
1322 : 310494 : else if (!integer_all_onesp (range[1]))
1323 : 164030 : range[1] = fold_build2 (PLUS_EXPR, size_type_node,
1324 : : range[1], size_one_node);
1325 : :
1326 : 395157 : slen = range[0];
1327 : : }
1328 : : else
1329 : : {
1330 : 9852 : at_least_one = true;
1331 : 9852 : slen = size_one_node;
1332 : : }
1333 : : }
1334 : : else
1335 : : slen = srcstr;
1336 : : }
1337 : :
1338 : 818325 : if (!dstwrite && !maxread)
1339 : : {
1340 : : /* When the only available piece of data is the object size
1341 : : there is nothing to do. */
1342 : 288823 : if (!slen)
1343 : : return true;
1344 : :
1345 : : /* Otherwise, when the length of the source sequence is known
1346 : : (as with strlen), set DSTWRITE to it. */
1347 : 288405 : if (!range[0])
1348 : 79 : dstwrite = slen;
1349 : : }
1350 : :
1351 : 817907 : if (!dstsize)
1352 : 399909 : dstsize = maxobjsize;
1353 : :
1354 : : /* Set RANGE to that of DSTWRITE if non-null, bounded by PAD->DST_BNDRNG
1355 : : if valid. */
1356 : 817907 : gimple *stmt = pad ? pad->stmt : nullptr;
1357 : 2451255 : get_size_range (rvals, dstwrite, stmt, range,
1358 : : /* If the destination has known zero size prefer a zero
1359 : : size range to avoid false positives if that's a
1360 : : possibility. */
1361 : 817907 : integer_zerop (dstsize) ? SR_ALLOW_ZERO : 0,
1362 : : pad ? pad->dst_bndrng : NULL);
1363 : :
1364 : 817907 : tree func = get_callee_fndecl (exp);
1365 : : /* Read vs write access by built-ins can be determined from the const
1366 : : qualifiers on the pointer argument. In the absence of attribute
1367 : : access, non-const qualified pointer arguments to user-defined
1368 : : functions are assumed to both read and write the objects. */
1369 : 817907 : const bool builtin = func ? fndecl_built_in_p (func) : false;
1370 : :
1371 : : /* First check the number of bytes to be written against the maximum
1372 : : object size. */
1373 : 817907 : if (range[0]
1374 : 816747 : && TREE_CODE (range[0]) == INTEGER_CST
1375 : 1634650 : && tree_int_cst_lt (maxobjsize, range[0]))
1376 : : {
1377 : 128 : location_t loc = get_location (exp);
1378 : 128 : maybe_warn_for_bound (OPT_Wstringop_overflow_, loc, exp, func, range,
1379 : : NULL_TREE, pad);
1380 : 128 : return false;
1381 : : }
1382 : :
1383 : : /* The number of bytes to write is "exact" if DSTWRITE is non-null,
1384 : : constant, and in range of unsigned HOST_WIDE_INT. */
1385 : 817779 : bool exactwrite = dstwrite && tree_fits_uhwi_p (dstwrite);
1386 : :
1387 : : /* Next check the number of bytes to be written against the destination
1388 : : object size. */
1389 : 817779 : if (range[0] || !exactwrite || integer_all_onesp (dstwrite))
1390 : : {
1391 : 817779 : if (range[0]
1392 : 816619 : && TREE_CODE (range[0]) == INTEGER_CST
1393 : 1634394 : && ((tree_fits_uhwi_p (dstsize)
1394 : 816298 : && tree_int_cst_lt (dstsize, range[0]))
1395 : 815007 : || (dstwrite
1396 : 412814 : && tree_fits_uhwi_p (dstwrite)
1397 : 345346 : && tree_int_cst_lt (dstwrite, range[0]))))
1398 : : {
1399 : 1608 : const opt_code opt = OPT_Wstringop_overflow_;
1400 : 1608 : if (warning_suppressed_p (exp, opt)
1401 : 1608 : || (pad && pad->dst.ref
1402 : 905 : && warning_suppressed_p (pad->dst.ref, opt)))
1403 : 285 : return false;
1404 : :
1405 : 1323 : auto_diagnostic_group d;
1406 : 1323 : location_t loc = get_location (exp);
1407 : 1323 : bool warned = false;
1408 : 1323 : if (dstwrite == slen && at_least_one)
1409 : : {
1410 : : /* This is a call to strcpy with a destination of 0 size
1411 : : and a source of unknown length. The call will write
1412 : : at least one byte past the end of the destination. */
1413 : 0 : warned = (func
1414 : 0 : ? warning_at (loc, opt,
1415 : : "%qD writing %E or more bytes into "
1416 : : "a region of size %E overflows "
1417 : : "the destination",
1418 : : func, range[0], dstsize)
1419 : 0 : : warning_at (loc, opt,
1420 : : "writing %E or more bytes into "
1421 : : "a region of size %E overflows "
1422 : : "the destination",
1423 : : range[0], dstsize));
1424 : : }
1425 : : else
1426 : : {
1427 : 1323 : const bool read
1428 : 1323 : = mode == access_read_only || mode == access_read_write;
1429 : 1323 : const bool write
1430 : 1323 : = mode == access_write_only || mode == access_read_write;
1431 : 1323 : const bool maybe = pad && pad->dst.parmarray;
1432 : 1323 : warned = warn_for_access (loc, func, exp,
1433 : : OPT_Wstringop_overflow_,
1434 : : range, dstsize,
1435 : 1323 : write, read && !builtin, maybe);
1436 : : }
1437 : :
1438 : 1323 : if (warned)
1439 : : {
1440 : 921 : suppress_warning (exp, OPT_Wstringop_overflow_);
1441 : 921 : if (pad)
1442 : 838 : pad->dst.inform_access (pad->mode);
1443 : : }
1444 : :
1445 : : /* Return error when an overflow has been detected. */
1446 : : return false;
1447 : 1323 : }
1448 : : }
1449 : :
1450 : : /* Check the maximum length of the source sequence against the size
1451 : : of the destination object if known, or against the maximum size
1452 : : of an object. */
1453 : 816171 : if (maxread)
1454 : : {
1455 : : /* Set RANGE to that of MAXREAD, bounded by PAD->SRC_BNDRNG if
1456 : : PAD is nonnull and BNDRNG is valid. */
1457 : 117486 : get_size_range (rvals, maxread, stmt, range, 0,
1458 : : pad ? pad->src_bndrng : NULL);
1459 : :
1460 : 117486 : location_t loc = get_location (exp);
1461 : 117486 : tree size = dstsize;
1462 : 117486 : if (pad && pad->mode == access_read_only)
1463 : 113898 : size = wide_int_to_tree (sizetype, pad->src.size_remaining ());
1464 : :
1465 : 117486 : if (range[0] && maxread && tree_fits_uhwi_p (size))
1466 : : {
1467 : 117442 : if (tree_int_cst_lt (maxobjsize, range[0]))
1468 : : {
1469 : 87 : maybe_warn_for_bound (OPT_Wstringop_overread, loc, exp, func,
1470 : : range, size, pad);
1471 : 87 : return false;
1472 : : }
1473 : :
1474 : 117355 : if (size != maxobjsize && tree_int_cst_lt (size, range[0]))
1475 : : {
1476 : 436 : opt_code opt = (dstwrite || mode != access_read_only
1477 : 218 : ? OPT_Wstringop_overflow_
1478 : : : OPT_Wstringop_overread);
1479 : 218 : maybe_warn_for_bound (opt, loc, exp, func, range, size, pad);
1480 : 218 : return false;
1481 : : }
1482 : : }
1483 : :
1484 : 117181 : maybe_warn_nonstring_arg (func, exp);
1485 : : }
1486 : :
1487 : : /* Check for reading past the end of SRC. */
1488 : 1631732 : bool overread = (slen
1489 : 815866 : && slen == srcstr
1490 : 92854 : && dstwrite
1491 : 92035 : && range[0]
1492 : 92035 : && TREE_CODE (slen) == INTEGER_CST
1493 : 907901 : && tree_int_cst_lt (slen, range[0]));
1494 : : /* If none is determined try to get a better answer based on the details
1495 : : in PAD. */
1496 : 815866 : if (!overread
1497 : 815866 : && pad
1498 : 814022 : && pad->src.sizrng[1] >= 0
1499 : 581599 : && pad->src.offrng[0] >= 0
1500 : 1629888 : && (pad->src.offrng[1] < 0
1501 : 571449 : || pad->src.offrng[0] <= pad->src.offrng[1]))
1502 : : {
1503 : : /* Set RANGE to that of MAXREAD, bounded by PAD->SRC_BNDRNG if
1504 : : PAD is nonnull and BNDRNG is valid. */
1505 : 571449 : get_size_range (rvals, maxread, stmt, range, 0,
1506 : : pad ? pad->src_bndrng : NULL);
1507 : : /* Set OVERREAD for reads starting just past the end of an object. */
1508 : 571449 : overread = pad->src.sizrng[1] - pad->src.offrng[0] < pad->src_bndrng[0];
1509 : 571449 : range[0] = wide_int_to_tree (sizetype, pad->src_bndrng[0]);
1510 : 571449 : slen = size_zero_node;
1511 : : }
1512 : :
1513 : 815866 : if (overread)
1514 : : {
1515 : 661 : const opt_code opt = OPT_Wstringop_overread;
1516 : 661 : if (warning_suppressed_p (exp, opt)
1517 : 594 : || (srcstr && warning_suppressed_p (srcstr, opt))
1518 : 1227 : || (pad && pad->src.ref
1519 : 566 : && warning_suppressed_p (pad->src.ref, opt)))
1520 : 95 : return false;
1521 : :
1522 : 566 : location_t loc = get_location (exp);
1523 : 566 : const bool read
1524 : 566 : = mode == access_read_only || mode == access_read_write;
1525 : 566 : const bool maybe = pad && pad->dst.parmarray;
1526 : 566 : auto_diagnostic_group d;
1527 : 566 : if (warn_for_access (loc, func, exp, opt, range, slen, false, read,
1528 : : maybe))
1529 : : {
1530 : 392 : suppress_warning (exp, opt);
1531 : 392 : if (pad)
1532 : 392 : pad->src.inform_access (access_read_only);
1533 : : }
1534 : : return false;
1535 : 566 : }
1536 : :
1537 : : return true;
1538 : : }
1539 : :
1540 : : static bool
1541 : 809904 : check_access (gimple *stmt, tree dstwrite,
1542 : : tree maxread, tree srcstr, tree dstsize,
1543 : : access_mode mode, const access_data *pad,
1544 : : range_query *rvals)
1545 : : {
1546 : 0 : return check_access<gimple *> (stmt, dstwrite, maxread, srcstr, dstsize,
1547 : 0 : mode, pad, rvals);
1548 : : }
1549 : :
1550 : : bool
1551 : 1981 : check_access (tree expr, tree dstwrite,
1552 : : tree maxread, tree srcstr, tree dstsize,
1553 : : access_mode mode, const access_data *pad /* = NULL */)
1554 : : {
1555 : 1981 : return check_access<tree> (expr, dstwrite, maxread, srcstr, dstsize,
1556 : 1981 : mode, pad, nullptr);
1557 : : }
1558 : :
1559 : : /* Return true if STMT is a call to an allocation function. Unless
1560 : : ALL_ALLOC is set, consider only functions that return dynamically
1561 : : allocated objects. Otherwise return true even for all forms of
1562 : : alloca (including VLA). */
1563 : :
1564 : : static bool
1565 : 40805 : fndecl_alloc_p (tree fndecl, bool all_alloc)
1566 : : {
1567 : 40805 : if (!fndecl)
1568 : : return false;
1569 : :
1570 : : /* A call to operator new isn't recognized as one to a built-in. */
1571 : 40779 : if (DECL_IS_OPERATOR_NEW_P (fndecl))
1572 : : return true;
1573 : :
1574 : 31767 : if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
1575 : : {
1576 : 27665 : switch (DECL_FUNCTION_CODE (fndecl))
1577 : : {
1578 : : case BUILT_IN_ALLOCA:
1579 : : case BUILT_IN_ALLOCA_WITH_ALIGN:
1580 : : return all_alloc;
1581 : : case BUILT_IN_ALIGNED_ALLOC:
1582 : : case BUILT_IN_CALLOC:
1583 : : case BUILT_IN_GOMP_ALLOC:
1584 : : case BUILT_IN_GOMP_REALLOC:
1585 : : case BUILT_IN_MALLOC:
1586 : : case BUILT_IN_REALLOC:
1587 : : case BUILT_IN_STRDUP:
1588 : : case BUILT_IN_STRNDUP:
1589 : : return true;
1590 : : default:
1591 : : break;
1592 : : }
1593 : : }
1594 : :
1595 : : /* A function is considered an allocation function if it's declared
1596 : : with attribute malloc with an argument naming its associated
1597 : : deallocation function. */
1598 : 4102 : tree attrs = DECL_ATTRIBUTES (fndecl);
1599 : 4102 : if (!attrs)
1600 : : return false;
1601 : :
1602 : 103 : for (tree allocs = attrs;
1603 : 1644 : (allocs = lookup_attribute ("malloc", allocs));
1604 : 103 : allocs = TREE_CHAIN (allocs))
1605 : : {
1606 : 837 : tree args = TREE_VALUE (allocs);
1607 : 837 : if (!args)
1608 : 103 : continue;
1609 : :
1610 : 734 : if (TREE_VALUE (args))
1611 : : return true;
1612 : : }
1613 : :
1614 : : return false;
1615 : : }
1616 : :
1617 : : /* Return true if STMT is a call to an allocation function. A wrapper
1618 : : around fndecl_alloc_p. */
1619 : :
1620 : : static bool
1621 : 40805 : gimple_call_alloc_p (gimple *stmt, bool all_alloc = false)
1622 : : {
1623 : 40805 : return fndecl_alloc_p (gimple_call_fndecl (stmt), all_alloc);
1624 : : }
1625 : :
1626 : : /* Return true if DELC doesn't refer to an operator delete that's
1627 : : suitable to call with a pointer returned from the operator new
1628 : : described by NEWC. */
1629 : :
1630 : : static bool
1631 : 2255 : new_delete_mismatch_p (const demangle_component &newc,
1632 : : const demangle_component &delc)
1633 : : {
1634 : 3499 : if (newc.type != delc.type)
1635 : : return true;
1636 : :
1637 : 3434 : switch (newc.type)
1638 : : {
1639 : 948 : case DEMANGLE_COMPONENT_NAME:
1640 : 948 : {
1641 : 948 : int len = newc.u.s_name.len;
1642 : 948 : const char *news = newc.u.s_name.s;
1643 : 948 : const char *dels = delc.u.s_name.s;
1644 : 948 : if (len != delc.u.s_name.len || memcmp (news, dels, len))
1645 : : return true;
1646 : :
1647 : 933 : if (news[len] == 'n')
1648 : : {
1649 : 334 : if (news[len + 1] == 'a')
1650 : 114 : return dels[len] != 'd' || dels[len + 1] != 'a';
1651 : 238 : if (news[len + 1] == 'w')
1652 : 250 : return dels[len] != 'd' || dels[len + 1] != 'l';
1653 : : }
1654 : : return false;
1655 : : }
1656 : :
1657 : : case DEMANGLE_COMPONENT_OPERATOR:
1658 : : /* Operator mismatches are handled above. */
1659 : : return false;
1660 : :
1661 : 0 : case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
1662 : 0 : if (newc.u.s_extended_operator.args != delc.u.s_extended_operator.args)
1663 : : return true;
1664 : 0 : return new_delete_mismatch_p (*newc.u.s_extended_operator.name,
1665 : 0 : *delc.u.s_extended_operator.name);
1666 : :
1667 : 0 : case DEMANGLE_COMPONENT_FIXED_TYPE:
1668 : 0 : if (newc.u.s_fixed.accum != delc.u.s_fixed.accum
1669 : 0 : || newc.u.s_fixed.sat != delc.u.s_fixed.sat)
1670 : : return true;
1671 : 0 : return new_delete_mismatch_p (*newc.u.s_fixed.length,
1672 : 0 : *delc.u.s_fixed.length);
1673 : :
1674 : 0 : case DEMANGLE_COMPONENT_CTOR:
1675 : 0 : if (newc.u.s_ctor.kind != delc.u.s_ctor.kind)
1676 : : return true;
1677 : 0 : return new_delete_mismatch_p (*newc.u.s_ctor.name,
1678 : 0 : *delc.u.s_ctor.name);
1679 : :
1680 : 0 : case DEMANGLE_COMPONENT_DTOR:
1681 : 0 : if (newc.u.s_dtor.kind != delc.u.s_dtor.kind)
1682 : : return true;
1683 : 0 : return new_delete_mismatch_p (*newc.u.s_dtor.name,
1684 : 0 : *delc.u.s_dtor.name);
1685 : :
1686 : 321 : case DEMANGLE_COMPONENT_BUILTIN_TYPE:
1687 : 321 : {
1688 : : /* The demangler API provides no better way to compare built-in
1689 : : types except to by comparing their demangled names. */
1690 : 321 : size_t nsz, dsz;
1691 : 321 : demangle_component *pnc = const_cast<demangle_component *>(&newc);
1692 : 321 : demangle_component *pdc = const_cast<demangle_component *>(&delc);
1693 : 321 : char *nts = cplus_demangle_print (0, pnc, 16, &nsz);
1694 : 321 : char *dts = cplus_demangle_print (0, pdc, 16, &dsz);
1695 : 321 : if (!nts != !dts)
1696 : : return true;
1697 : 321 : bool mismatch = strcmp (nts, dts);
1698 : 321 : free (nts);
1699 : 321 : free (dts);
1700 : 321 : return mismatch;
1701 : : }
1702 : :
1703 : 0 : case DEMANGLE_COMPONENT_SUB_STD:
1704 : 0 : if (newc.u.s_string.len != delc.u.s_string.len)
1705 : : return true;
1706 : 0 : return memcmp (newc.u.s_string.string, delc.u.s_string.string,
1707 : 0 : newc.u.s_string.len);
1708 : :
1709 : 6 : case DEMANGLE_COMPONENT_FUNCTION_PARAM:
1710 : 6 : case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
1711 : 6 : case DEMANGLE_COMPONENT_UNNAMED_TYPE:
1712 : 6 : return newc.u.s_number.number != delc.u.s_number.number;
1713 : :
1714 : 0 : case DEMANGLE_COMPONENT_CHARACTER:
1715 : 0 : return newc.u.s_character.character != delc.u.s_character.character;
1716 : :
1717 : 0 : case DEMANGLE_COMPONENT_DEFAULT_ARG:
1718 : 0 : case DEMANGLE_COMPONENT_LAMBDA:
1719 : 0 : if (newc.u.s_unary_num.num != delc.u.s_unary_num.num)
1720 : : return true;
1721 : 0 : return new_delete_mismatch_p (*newc.u.s_unary_num.sub,
1722 : 0 : *delc.u.s_unary_num.sub);
1723 : 1668 : default:
1724 : 1668 : break;
1725 : : }
1726 : :
1727 : 1668 : if (!newc.u.s_binary.left != !delc.u.s_binary.left)
1728 : : return true;
1729 : :
1730 : 1668 : if (!newc.u.s_binary.left)
1731 : : return false;
1732 : :
1733 : 1642 : if (new_delete_mismatch_p (*newc.u.s_binary.left, *delc.u.s_binary.left)
1734 : 1642 : || !newc.u.s_binary.right != !delc.u.s_binary.right)
1735 : : return true;
1736 : :
1737 : 1531 : if (newc.u.s_binary.right)
1738 : : return new_delete_mismatch_p (*newc.u.s_binary.right,
1739 : : *delc.u.s_binary.right);
1740 : : return false;
1741 : : }
1742 : :
1743 : : /* Return true if DELETE_DECL is an operator delete that's not suitable
1744 : : to call with a pointer returned from NEW_DECL. */
1745 : :
1746 : : static bool
1747 : 8990 : new_delete_mismatch_p (tree new_decl, tree delete_decl)
1748 : : {
1749 : 8990 : tree new_name = DECL_ASSEMBLER_NAME (new_decl);
1750 : 8990 : tree delete_name = DECL_ASSEMBLER_NAME (delete_decl);
1751 : :
1752 : : /* valid_new_delete_pair_p() returns a conservative result (currently
1753 : : it only handles global operators). A true result is reliable but
1754 : : a false result doesn't necessarily mean the operators don't match
1755 : : unless CERTAIN is set. */
1756 : 8990 : bool certain;
1757 : 8990 : if (valid_new_delete_pair_p (new_name, delete_name, &certain))
1758 : : return false;
1759 : : /* CERTAIN is set when the negative result is certain. */
1760 : 665 : if (certain)
1761 : : return true;
1762 : :
1763 : : /* For anything not handled by valid_new_delete_pair_p() such as member
1764 : : operators compare the individual demangled components of the mangled
1765 : : name. */
1766 : 619 : const char *new_str = IDENTIFIER_POINTER (new_name);
1767 : 619 : const char *del_str = IDENTIFIER_POINTER (delete_name);
1768 : :
1769 : 619 : void *np = NULL, *dp = NULL;
1770 : 619 : demangle_component *ndc = cplus_demangle_v3_components (new_str, 0, &np);
1771 : 619 : demangle_component *ddc = cplus_demangle_v3_components (del_str, 0, &dp);
1772 : :
1773 : : /* Sometimes, notably quite often with coroutines, 'operator new' is
1774 : : templated. However, template arguments can't change whether a given
1775 : : new/delete is a singleton or array one, nor what it is a member of, so
1776 : : the template arguments can be safely ignored for the purposes of checking
1777 : : for mismatches. */
1778 : :
1779 : 1845 : auto strip_dc_template = [] (demangle_component* dc)
1780 : : {
1781 : 613 : if (dc->type == DEMANGLE_COMPONENT_TEMPLATE)
1782 : 39 : dc = dc->u.s_binary.left;
1783 : 1226 : return dc;
1784 : : };
1785 : :
1786 : 619 : bool mismatch = (ndc && ddc
1787 : 1232 : && new_delete_mismatch_p (*strip_dc_template (ndc),
1788 : 613 : *strip_dc_template (ddc)));
1789 : 619 : free (np);
1790 : 619 : free (dp);
1791 : 619 : return mismatch;
1792 : : }
1793 : :
1794 : : /* ALLOC_DECL and DEALLOC_DECL are pair of allocation and deallocation
1795 : : functions. Return true if the latter is suitable to deallocate objects
1796 : : allocated by calls to the former. */
1797 : :
1798 : : static bool
1799 : 39901 : matching_alloc_calls_p (tree alloc_decl, tree dealloc_decl)
1800 : : {
1801 : : /* Set to alloc_kind_t::builtin if ALLOC_DECL is associated with
1802 : : a built-in deallocator. */
1803 : 39901 : enum class alloc_kind_t { none, builtin, user }
1804 : 39901 : alloc_dealloc_kind = alloc_kind_t::none;
1805 : :
1806 : 39901 : if (DECL_IS_OPERATOR_NEW_P (alloc_decl))
1807 : : {
1808 : 9012 : if (DECL_IS_OPERATOR_DELETE_P (dealloc_decl))
1809 : : /* Return true iff both functions are of the same array or
1810 : : singleton form and false otherwise. */
1811 : 8990 : return !new_delete_mismatch_p (alloc_decl, dealloc_decl);
1812 : :
1813 : : /* Return false for deallocation functions that are known not
1814 : : to match. */
1815 : 22 : if (fndecl_built_in_p (dealloc_decl, BUILT_IN_FREE, BUILT_IN_REALLOC))
1816 : : return false;
1817 : : /* Otherwise proceed below to check the deallocation function's
1818 : : "*dealloc" attributes to look for one that mentions this operator
1819 : : new. */
1820 : : }
1821 : 30889 : else if (fndecl_built_in_p (alloc_decl, BUILT_IN_NORMAL))
1822 : : {
1823 : 30143 : switch (DECL_FUNCTION_CODE (alloc_decl))
1824 : : {
1825 : : case BUILT_IN_ALLOCA:
1826 : : case BUILT_IN_ALLOCA_WITH_ALIGN:
1827 : : return false;
1828 : :
1829 : 1192 : case BUILT_IN_GOMP_ALLOC:
1830 : 1192 : case BUILT_IN_GOMP_REALLOC:
1831 : 1192 : if (DECL_IS_OPERATOR_DELETE_P (dealloc_decl))
1832 : : return false;
1833 : :
1834 : 1192 : if (fndecl_built_in_p (dealloc_decl, BUILT_IN_GOMP_FREE,
1835 : : BUILT_IN_GOMP_REALLOC))
1836 : : return true;
1837 : :
1838 : : alloc_dealloc_kind = alloc_kind_t::builtin;
1839 : : break;
1840 : :
1841 : 28951 : case BUILT_IN_ALIGNED_ALLOC:
1842 : 28951 : case BUILT_IN_CALLOC:
1843 : 28951 : case BUILT_IN_MALLOC:
1844 : 28951 : case BUILT_IN_REALLOC:
1845 : 28951 : case BUILT_IN_STRDUP:
1846 : 28951 : case BUILT_IN_STRNDUP:
1847 : 28951 : if (DECL_IS_OPERATOR_DELETE_P (dealloc_decl))
1848 : : return false;
1849 : :
1850 : 28878 : if (fndecl_built_in_p (dealloc_decl, BUILT_IN_FREE,
1851 : : BUILT_IN_REALLOC))
1852 : : return true;
1853 : :
1854 : : alloc_dealloc_kind = alloc_kind_t::builtin;
1855 : : break;
1856 : :
1857 : : default:
1858 : : break;
1859 : : }
1860 : : }
1861 : :
1862 : : /* Set if DEALLOC_DECL both allocates and deallocates. */
1863 : 786 : alloc_kind_t realloc_kind = alloc_kind_t::none;
1864 : :
1865 : 786 : if (fndecl_built_in_p (dealloc_decl, BUILT_IN_NORMAL))
1866 : : {
1867 : 128 : built_in_function dealloc_code = DECL_FUNCTION_CODE (dealloc_decl);
1868 : 128 : if (dealloc_code == BUILT_IN_REALLOC
1869 : 128 : || dealloc_code == BUILT_IN_GOMP_REALLOC)
1870 : 27 : realloc_kind = alloc_kind_t::builtin;
1871 : :
1872 : 128 : for (tree amats = DECL_ATTRIBUTES (alloc_decl);
1873 : 223 : (amats = lookup_attribute ("malloc", amats));
1874 : 95 : amats = TREE_CHAIN (amats))
1875 : : {
1876 : 161 : tree args = TREE_VALUE (amats);
1877 : 161 : if (!args)
1878 : 0 : continue;
1879 : :
1880 : 161 : tree fndecl = TREE_VALUE (args);
1881 : 161 : if (!fndecl || !DECL_P (fndecl))
1882 : 0 : continue;
1883 : :
1884 : 161 : if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
1885 : 161 : && dealloc_code == DECL_FUNCTION_CODE (fndecl))
1886 : : return true;
1887 : : }
1888 : : }
1889 : :
1890 : 720 : const bool alloc_builtin = fndecl_built_in_p (alloc_decl, BUILT_IN_NORMAL);
1891 : 720 : alloc_kind_t realloc_dealloc_kind = alloc_kind_t::none;
1892 : :
1893 : : /* If DEALLOC_DECL has an internal "*dealloc" attribute scan the list
1894 : : of its associated allocation functions for ALLOC_DECL.
1895 : : If the corresponding ALLOC_DECL is found they're a matching pair,
1896 : : otherwise they're not.
1897 : : With DDATS set to the Deallocator's *Dealloc ATtributes... */
1898 : 720 : for (tree ddats = DECL_ATTRIBUTES (dealloc_decl);
1899 : 2466 : (ddats = lookup_attribute ("*dealloc", ddats));
1900 : 1746 : ddats = TREE_CHAIN (ddats))
1901 : : {
1902 : 2298 : tree args = TREE_VALUE (ddats);
1903 : 2298 : if (!args)
1904 : 0 : continue;
1905 : :
1906 : 2298 : tree alloc = TREE_VALUE (args);
1907 : 2298 : if (!alloc)
1908 : 0 : continue;
1909 : :
1910 : 2298 : if (alloc == DECL_NAME (dealloc_decl))
1911 : 44 : realloc_kind = alloc_kind_t::user;
1912 : :
1913 : 2298 : if (DECL_P (alloc))
1914 : : {
1915 : 0 : gcc_checking_assert (fndecl_built_in_p (alloc, BUILT_IN_NORMAL));
1916 : :
1917 : 0 : switch (DECL_FUNCTION_CODE (alloc))
1918 : : {
1919 : 0 : case BUILT_IN_ALIGNED_ALLOC:
1920 : 0 : case BUILT_IN_CALLOC:
1921 : 0 : case BUILT_IN_GOMP_ALLOC:
1922 : 0 : case BUILT_IN_GOMP_REALLOC:
1923 : 0 : case BUILT_IN_MALLOC:
1924 : 0 : case BUILT_IN_REALLOC:
1925 : 0 : case BUILT_IN_STRDUP:
1926 : 0 : case BUILT_IN_STRNDUP:
1927 : 0 : realloc_dealloc_kind = alloc_kind_t::builtin;
1928 : 0 : break;
1929 : : default:
1930 : : break;
1931 : : }
1932 : :
1933 : 0 : if (!alloc_builtin)
1934 : 0 : continue;
1935 : :
1936 : 0 : if (DECL_FUNCTION_CODE (alloc) != DECL_FUNCTION_CODE (alloc_decl))
1937 : 0 : continue;
1938 : :
1939 : : return true;
1940 : : }
1941 : :
1942 : 2298 : if (alloc == DECL_NAME (alloc_decl))
1943 : : return true;
1944 : : }
1945 : :
1946 : 168 : if (realloc_kind == alloc_kind_t::none)
1947 : : return false;
1948 : :
1949 : 110 : hash_set<tree> common_deallocs;
1950 : : /* Special handling for deallocators. Iterate over both the allocator's
1951 : : and the reallocator's associated deallocator functions looking for
1952 : : the first one in common. If one is found, the de/reallocator is
1953 : : a match for the allocator even though the latter isn't directly
1954 : : associated with the former. This simplifies declarations in system
1955 : : headers.
1956 : : With AMATS set to the Allocator's Malloc ATtributes,
1957 : : and RMATS set to Reallocator's Malloc ATtributes... */
1958 : 55 : for (tree amats = DECL_ATTRIBUTES (alloc_decl);
1959 : 115 : (amats = lookup_attribute ("malloc", amats));
1960 : 60 : amats = amats ? TREE_CHAIN (amats) : NULL_TREE)
1961 : 68 : if (tree args = amats ? TREE_VALUE (amats) : NULL_TREE)
1962 : 58 : if (tree adealloc = TREE_VALUE (args))
1963 : : {
1964 : 58 : if (DECL_P (adealloc)
1965 : 58 : && fndecl_built_in_p (adealloc, BUILT_IN_NORMAL))
1966 : : {
1967 : 12 : built_in_function fncode = DECL_FUNCTION_CODE (adealloc);
1968 : 12 : if (fncode == BUILT_IN_FREE || fncode == BUILT_IN_REALLOC)
1969 : : {
1970 : 12 : if (realloc_kind == alloc_kind_t::builtin)
1971 : 8 : return true;
1972 : : alloc_dealloc_kind = alloc_kind_t::builtin;
1973 : : }
1974 : 4 : continue;
1975 : 4 : }
1976 : :
1977 : 46 : common_deallocs.add (adealloc);
1978 : : }
1979 : 47 : for (tree rmats = DECL_ATTRIBUTES (dealloc_decl);
1980 : 83 : (rmats = lookup_attribute ("malloc", rmats));
1981 : 36 : rmats = rmats ? TREE_CHAIN (rmats) : NULL_TREE)
1982 : 56 : if (tree args = rmats ? TREE_VALUE (rmats) : NULL_TREE)
1983 : 56 : if (tree ddealloc = TREE_VALUE (args))
1984 : : {
1985 : 56 : if (DECL_P (ddealloc)
1986 : 56 : && fndecl_built_in_p (ddealloc, BUILT_IN_NORMAL))
1987 : : {
1988 : 16 : built_in_function fncode = DECL_FUNCTION_CODE (ddealloc);
1989 : 16 : if (fncode == BUILT_IN_FREE || fncode == BUILT_IN_REALLOC)
1990 : : {
1991 : 16 : if (alloc_dealloc_kind == alloc_kind_t::builtin)
1992 : 20 : return true;
1993 : : realloc_dealloc_kind = alloc_kind_t::builtin;
1994 : : }
1995 : 1 : continue;
1996 : 1 : }
1997 : :
1998 : 40 : if (common_deallocs.contains (ddealloc))
1999 : : return true;
2000 : : }
2001 : :
2002 : : /* Succeed only if ALLOC_DECL and the reallocator DEALLOC_DECL share
2003 : : a built-in deallocator. */
2004 : 27 : return (alloc_dealloc_kind == alloc_kind_t::builtin
2005 : 27 : && realloc_dealloc_kind == alloc_kind_t::builtin);
2006 : : }
2007 : :
2008 : : /* Return true if DEALLOC_DECL is a function suitable to deallocate
2009 : : objects allocated by the ALLOC call. */
2010 : :
2011 : : static bool
2012 : 39901 : matching_alloc_calls_p (gimple *alloc, tree dealloc_decl)
2013 : : {
2014 : 39901 : tree alloc_decl = gimple_call_fndecl (alloc);
2015 : 39901 : if (!alloc_decl)
2016 : : return true;
2017 : :
2018 : 39901 : return matching_alloc_calls_p (alloc_decl, dealloc_decl);
2019 : : }
2020 : :
2021 : : /* Diagnose a call EXP to deallocate a pointer referenced by AREF if it
2022 : : includes a nonzero offset. Such a pointer cannot refer to the beginning
2023 : : of an allocated object. A negative offset may refer to it only if
2024 : : the target pointer is unknown. */
2025 : :
2026 : : static bool
2027 : 139373 : warn_dealloc_offset (location_t loc, gimple *call, const access_ref &aref)
2028 : : {
2029 : 139373 : if (aref.deref || aref.offrng[0] <= 0 || aref.offrng[1] <= 0)
2030 : 139298 : return false;
2031 : :
2032 : 75 : tree dealloc_decl = gimple_call_fndecl (call);
2033 : 75 : if (!dealloc_decl)
2034 : : return false;
2035 : :
2036 : 75 : if (DECL_IS_OPERATOR_DELETE_P (dealloc_decl)
2037 : 75 : && !DECL_IS_REPLACEABLE_OPERATOR (dealloc_decl))
2038 : : {
2039 : : /* A call to a user-defined operator delete with a pointer plus offset
2040 : : may be valid if it's returned from an unknown function (i.e., one
2041 : : that's not operator new). */
2042 : 6 : if (TREE_CODE (aref.ref) == SSA_NAME)
2043 : : {
2044 : 6 : gimple *def_stmt = SSA_NAME_DEF_STMT (aref.ref);
2045 : 6 : if (is_gimple_call (def_stmt))
2046 : : {
2047 : 6 : tree alloc_decl = gimple_call_fndecl (def_stmt);
2048 : 9 : if (!alloc_decl || !DECL_IS_OPERATOR_NEW_P (alloc_decl))
2049 : : return false;
2050 : : }
2051 : : }
2052 : : }
2053 : :
2054 : 69 : char offstr[80];
2055 : 69 : offstr[0] = '\0';
2056 : 69 : if (wi::fits_shwi_p (aref.offrng[0]))
2057 : : {
2058 : 69 : if (aref.offrng[0] == aref.offrng[1]
2059 : 69 : || !wi::fits_shwi_p (aref.offrng[1]))
2060 : 55 : sprintf (offstr, " %lli",
2061 : 55 : (long long)aref.offrng[0].to_shwi ());
2062 : : else
2063 : 14 : sprintf (offstr, " [%lli, %lli]",
2064 : 14 : (long long)aref.offrng[0].to_shwi (),
2065 : 14 : (long long)aref.offrng[1].to_shwi ());
2066 : : }
2067 : :
2068 : 69 : auto_diagnostic_group d;
2069 : 69 : if (!warning_at (loc, OPT_Wfree_nonheap_object,
2070 : : "%qD called on pointer %qE with nonzero offset%s",
2071 : 69 : dealloc_decl, aref.ref, offstr))
2072 : : return false;
2073 : :
2074 : 69 : if (DECL_P (aref.ref))
2075 : 8 : inform (get_location (aref.ref), "declared here");
2076 : 61 : else if (TREE_CODE (aref.ref) == SSA_NAME)
2077 : : {
2078 : 59 : gimple *def_stmt = SSA_NAME_DEF_STMT (aref.ref);
2079 : 59 : if (is_gimple_call (def_stmt))
2080 : : {
2081 : 53 : location_t def_loc = get_location (def_stmt);
2082 : 53 : tree alloc_decl = gimple_call_fndecl (def_stmt);
2083 : 53 : if (alloc_decl)
2084 : 53 : inform (def_loc,
2085 : : "returned from %qD", alloc_decl);
2086 : 0 : else if (tree alloc_fntype = gimple_call_fntype (def_stmt))
2087 : 0 : inform (def_loc,
2088 : : "returned from %qT", alloc_fntype);
2089 : : else
2090 : 0 : inform (def_loc, "obtained here");
2091 : : }
2092 : : }
2093 : :
2094 : : return true;
2095 : 69 : }
2096 : :
2097 : : namespace {
2098 : :
2099 : : const pass_data pass_data_waccess = {
2100 : : GIMPLE_PASS,
2101 : : "waccess",
2102 : : OPTGROUP_NONE,
2103 : : TV_WARN_ACCESS, /* timer variable */
2104 : : PROP_cfg, /* properties_required */
2105 : : 0, /* properties_provided */
2106 : : 0, /* properties_destroyed */
2107 : : 0, /* properties_start */
2108 : : 0, /* properties_finish */
2109 : : };
2110 : :
2111 : : /* Pass to detect invalid accesses. */
2112 : : class pass_waccess : public gimple_opt_pass
2113 : : {
2114 : : public:
2115 : : pass_waccess (gcc::context *);
2116 : :
2117 : : ~pass_waccess ();
2118 : :
2119 : : opt_pass *clone () final override;
2120 : :
2121 : : bool gate (function *) final override;
2122 : :
2123 : : void set_pass_param (unsigned, bool) final override;
2124 : :
2125 : : unsigned int execute (function *) final override;
2126 : :
2127 : : private:
2128 : : /* Not copyable or assignable. */
2129 : : pass_waccess (pass_waccess &) = delete;
2130 : : void operator= (pass_waccess &) = delete;
2131 : :
2132 : : /* Check a call to an atomic built-in function. */
2133 : : bool check_atomic_builtin (gcall *);
2134 : :
2135 : : /* Check a call to a built-in function. */
2136 : : bool check_builtin (gcall *);
2137 : :
2138 : : /* Check a call to an ordinary function for invalid accesses. */
2139 : : bool check_call_access (gcall *);
2140 : :
2141 : : /* Check a non-call statement. */
2142 : : void check_stmt (gimple *);
2143 : :
2144 : : /* Check statements in a basic block. */
2145 : : void check_block (basic_block);
2146 : :
2147 : : /* Check a call to a function. */
2148 : : void check_call (gcall *);
2149 : :
2150 : : /* Check a call to the named built-in function. */
2151 : : void check_alloca (gcall *);
2152 : : void check_alloc_size_call (gcall *);
2153 : : void check_strcat (gcall *);
2154 : : void check_strncat (gcall *);
2155 : : void check_stxcpy (gcall *);
2156 : : void check_stxncpy (gcall *);
2157 : : void check_strncmp (gcall *);
2158 : : void check_memop_access (gimple *, tree, tree, tree);
2159 : : void check_read_access (gimple *, tree, tree = NULL_TREE, int = 1);
2160 : :
2161 : : void maybe_check_dealloc_call (gcall *);
2162 : : void maybe_check_access_sizes (rdwr_map *, tree, tree, gimple *);
2163 : : bool maybe_warn_memmodel (gimple *, tree, tree, const unsigned char *);
2164 : : void check_atomic_memmodel (gimple *, tree, tree, const unsigned char *);
2165 : :
2166 : : /* Check for uses of indeterminate pointers. */
2167 : : void check_pointer_uses (gimple *, tree, tree = NULL_TREE, bool = false);
2168 : :
2169 : : /* Return the argument that a call returns. */
2170 : : tree gimple_call_return_arg (gcall *);
2171 : :
2172 : : /* Check a call for uses of a dangling pointer arguments. */
2173 : : void check_call_dangling (gcall *);
2174 : :
2175 : : /* Check uses of a dangling pointer or those derived from it. */
2176 : : void check_dangling_uses (tree, tree, bool = false, bool = false);
2177 : : void check_dangling_uses ();
2178 : : void check_dangling_stores ();
2179 : : bool check_dangling_stores (basic_block, hash_set<tree> &);
2180 : :
2181 : : void warn_invalid_pointer (tree, gimple *, gimple *, tree, bool, bool = false);
2182 : :
2183 : : /* Return true if use follows an invalidating statement. */
2184 : : bool use_after_inval_p (gimple *, gimple *, bool = false);
2185 : :
2186 : : /* A pointer_query object to store information about pointers and
2187 : : their targets in. */
2188 : : pointer_query m_ptr_qry;
2189 : : /* Mapping from DECLs and their clobber statements in the function. */
2190 : : hash_map<tree, gimple *> m_clobbers;
2191 : : /* A bit is set for each basic block whose statements have been assigned
2192 : : valid UIDs. */
2193 : : bitmap m_bb_uids_set;
2194 : : /* The current function. */
2195 : : function *m_func;
2196 : : /* True to run checks for uses of dangling pointers. */
2197 : : bool m_check_dangling_p;
2198 : : /* True to run checks early on in the optimization pipeline. */
2199 : : bool m_early_checks_p;
2200 : : };
2201 : :
2202 : : /* Construct the pass. */
2203 : :
2204 : 848598 : pass_waccess::pass_waccess (gcc::context *ctxt)
2205 : : : gimple_opt_pass (pass_data_waccess, ctxt),
2206 : 848598 : m_ptr_qry (NULL),
2207 : 848598 : m_clobbers (),
2208 : 848598 : m_bb_uids_set (),
2209 : 848598 : m_func (),
2210 : 848598 : m_check_dangling_p (),
2211 : 848598 : m_early_checks_p ()
2212 : : {
2213 : 848598 : }
2214 : :
2215 : : /* Return a copy of the pass with RUN_NUMBER one greater than THIS. */
2216 : :
2217 : : opt_pass*
2218 : 565732 : pass_waccess::clone ()
2219 : : {
2220 : 565732 : return new pass_waccess (m_ctxt);
2221 : : }
2222 : :
2223 : : /* Release pointer_query cache. */
2224 : :
2225 : 1530126 : pass_waccess::~pass_waccess ()
2226 : : {
2227 : 765063 : m_ptr_qry.flush_cache ();
2228 : 1530126 : }
2229 : :
2230 : : void
2231 : 848598 : pass_waccess::set_pass_param (unsigned int n, bool early)
2232 : : {
2233 : 848598 : gcc_assert (n == 0);
2234 : :
2235 : 848598 : m_early_checks_p = early;
2236 : 848598 : }
2237 : :
2238 : : /* Return true when any checks performed by the pass are enabled. */
2239 : :
2240 : : bool
2241 : 5125552 : pass_waccess::gate (function *)
2242 : : {
2243 : 5125552 : return (warn_free_nonheap_object
2244 : : || warn_mismatched_alloc
2245 : 5125552 : || warn_mismatched_new_delete);
2246 : : }
2247 : :
2248 : : /* Initialize ALLOC_OBJECT_SIZE_LIMIT based on the -Walloc-size-larger-than=
2249 : : setting if the option is specified, or to the maximum object size if it
2250 : : is not. Return the initialized value. */
2251 : :
2252 : : static tree
2253 : 47326 : alloc_max_size (void)
2254 : : {
2255 : 47326 : HOST_WIDE_INT limit = warn_alloc_size_limit;
2256 : 47326 : if (limit == HOST_WIDE_INT_MAX)
2257 : 46930 : limit = tree_to_shwi (TYPE_MAX_VALUE (ptrdiff_type_node));
2258 : :
2259 : 47326 : return build_int_cst (size_type_node, limit);
2260 : : }
2261 : :
2262 : : /* Diagnose a call EXP to function FN decorated with attribute alloc_size
2263 : : whose argument numbers given by IDX with values given by ARGS exceed
2264 : : the maximum object size or cause an unsigned overflow (wrapping) when
2265 : : multiplied. FN is null when EXP is a call via a function pointer.
2266 : : When ARGS[0] is null the function does nothing. ARGS[1] may be null
2267 : : for functions like malloc, and non-null for those like calloc that
2268 : : are decorated with a two-argument attribute alloc_size. */
2269 : :
2270 : : void
2271 : 47326 : maybe_warn_alloc_args_overflow (gimple *stmt, const tree args[2],
2272 : : const int idx[2])
2273 : : {
2274 : : /* The range each of the (up to) two arguments is known to be in. */
2275 : 47326 : tree argrange[2][2] = { { NULL_TREE, NULL_TREE }, { NULL_TREE, NULL_TREE } };
2276 : :
2277 : : /* Maximum object size set by -Walloc-size-larger-than= or SIZE_MAX / 2. */
2278 : 47326 : tree maxobjsize = alloc_max_size ();
2279 : :
2280 : 47326 : location_t loc = get_location (stmt);
2281 : :
2282 : 47326 : tree fn = gimple_call_fndecl (stmt);
2283 : 47326 : tree fntype = fn ? TREE_TYPE (fn) : gimple_call_fntype (stmt);
2284 : 47326 : bool warned = false;
2285 : :
2286 : : /* Validate each argument individually. */
2287 : 95842 : for (unsigned i = 0; i != 2 && args[i]; ++i)
2288 : : {
2289 : 48516 : if (TREE_CODE (args[i]) == INTEGER_CST)
2290 : : {
2291 : 30996 : argrange[i][0] = args[i];
2292 : 30996 : argrange[i][1] = args[i];
2293 : :
2294 : 30996 : if (tree_int_cst_lt (args[i], integer_zero_node))
2295 : : {
2296 : 24 : warned = warning_at (loc, OPT_Walloc_size_larger_than_,
2297 : : "argument %i value %qE is negative",
2298 : 24 : idx[i] + 1, args[i]);
2299 : : }
2300 : 30972 : else if (integer_zerop (args[i]))
2301 : : {
2302 : : /* Avoid issuing -Walloc-zero for allocation functions other
2303 : : than __builtin_alloca that are declared with attribute
2304 : : returns_nonnull because there's no portability risk. This
2305 : : avoids warning for such calls to libiberty's xmalloc and
2306 : : friends.
2307 : : Also avoid issuing the warning for calls to function named
2308 : : "alloca". */
2309 : 942 : if (fn && fndecl_built_in_p (fn, BUILT_IN_ALLOCA)
2310 : 964 : ? IDENTIFIER_LENGTH (DECL_NAME (fn)) != 6
2311 : 482 : : !lookup_attribute ("returns_nonnull",
2312 : 482 : TYPE_ATTRIBUTES (fntype)))
2313 : 475 : warned = warning_at (loc, OPT_Walloc_zero,
2314 : : "argument %i value is zero",
2315 : 475 : idx[i] + 1);
2316 : : }
2317 : 30490 : else if (tree_int_cst_lt (maxobjsize, args[i]))
2318 : : {
2319 : : /* G++ emits calls to ::operator new[](SIZE_MAX) in C++98
2320 : : mode and with -fno-exceptions as a way to indicate array
2321 : : size overflow. There's no good way to detect C++98 here
2322 : : so avoid diagnosing these calls for all C++ modes. */
2323 : 116 : if (i == 0
2324 : 99 : && fn
2325 : 82 : && !args[1]
2326 : 75 : && lang_GNU_CXX ()
2327 : 17 : && DECL_IS_OPERATOR_NEW_P (fn)
2328 : 116 : && integer_all_onesp (args[i]))
2329 : 17 : continue;
2330 : :
2331 : 82 : warned = warning_at (loc, OPT_Walloc_size_larger_than_,
2332 : : "argument %i value %qE exceeds "
2333 : : "maximum object size %E",
2334 : 82 : idx[i] + 1, args[i], maxobjsize);
2335 : : }
2336 : : }
2337 : 17520 : else if (TREE_CODE (args[i]) == SSA_NAME
2338 : 17520 : && get_size_range (args[i], argrange[i]))
2339 : : {
2340 : : /* Verify that the argument's range is not negative (including
2341 : : upper bound of zero). */
2342 : 17507 : if (tree_int_cst_lt (argrange[i][0], integer_zero_node)
2343 : 17507 : && tree_int_cst_le (argrange[i][1], integer_zero_node))
2344 : : {
2345 : 31 : warned = warning_at (loc, OPT_Walloc_size_larger_than_,
2346 : : "argument %i range [%E, %E] is negative",
2347 : 31 : idx[i] + 1,
2348 : : argrange[i][0], argrange[i][1]);
2349 : : }
2350 : 17476 : else if (tree_int_cst_lt (maxobjsize, argrange[i][0]))
2351 : : {
2352 : 31 : warned = warning_at (loc, OPT_Walloc_size_larger_than_,
2353 : : "argument %i range [%E, %E] exceeds "
2354 : : "maximum object size %E",
2355 : 31 : idx[i] + 1,
2356 : : argrange[i][0], argrange[i][1],
2357 : : maxobjsize);
2358 : : }
2359 : : }
2360 : : }
2361 : :
2362 : 47326 : if (!argrange[0][0])
2363 : 10 : return;
2364 : :
2365 : : /* For a two-argument alloc_size, validate the product of the two
2366 : : arguments if both of their values or ranges are known. */
2367 : 47095 : if (!warned && tree_fits_uhwi_p (argrange[0][0])
2368 : 47040 : && argrange[1][0] && tree_fits_uhwi_p (argrange[1][0])
2369 : 1090 : && !integer_onep (argrange[0][0])
2370 : 48095 : && !integer_onep (argrange[1][0]))
2371 : : {
2372 : : /* Check for overflow in the product of a function decorated with
2373 : : attribute alloc_size (X, Y). */
2374 : 403 : unsigned szprec = TYPE_PRECISION (size_type_node);
2375 : 403 : wide_int x = wi::to_wide (argrange[0][0], szprec);
2376 : 403 : wide_int y = wi::to_wide (argrange[1][0], szprec);
2377 : :
2378 : 403 : wi::overflow_type vflow;
2379 : 403 : wide_int prod = wi::umul (x, y, &vflow);
2380 : :
2381 : 403 : if (vflow)
2382 : 4 : warned = warning_at (loc, OPT_Walloc_size_larger_than_,
2383 : : "product %<%E * %E%> of arguments %i and %i "
2384 : : "exceeds %<SIZE_MAX%>",
2385 : : argrange[0][0], argrange[1][0],
2386 : 4 : idx[0] + 1, idx[1] + 1);
2387 : 399 : else if (wi::ltu_p (wi::to_wide (maxobjsize, szprec), prod))
2388 : 14 : warned = warning_at (loc, OPT_Walloc_size_larger_than_,
2389 : : "product %<%E * %E%> of arguments %i and %i "
2390 : : "exceeds maximum object size %E",
2391 : : argrange[0][0], argrange[1][0],
2392 : 14 : idx[0] + 1, idx[1] + 1,
2393 : : maxobjsize);
2394 : :
2395 : 403 : if (warned)
2396 : : {
2397 : : /* Print the full range of each of the two arguments to make
2398 : : it clear when it is, in fact, in a range and not constant. */
2399 : 16 : if (argrange[0][0] != argrange [0][1])
2400 : 1 : inform (loc, "argument %i in the range [%E, %E]",
2401 : 1 : idx[0] + 1, argrange[0][0], argrange[0][1]);
2402 : 16 : if (argrange[1][0] != argrange [1][1])
2403 : 1 : inform (loc, "argument %i in the range [%E, %E]",
2404 : 1 : idx[1] + 1, argrange[1][0], argrange[1][1]);
2405 : : }
2406 : 403 : }
2407 : :
2408 : 47316 : if (warned && fn)
2409 : : {
2410 : 212 : location_t fnloc = DECL_SOURCE_LOCATION (fn);
2411 : :
2412 : 212 : if (DECL_IS_UNDECLARED_BUILTIN (fn))
2413 : 64 : inform (loc,
2414 : : "in a call to built-in allocation function %qD", fn);
2415 : : else
2416 : 148 : inform (fnloc,
2417 : : "in a call to allocation function %qD declared here", fn);
2418 : : }
2419 : : }
2420 : :
2421 : : /* Check a call to an alloca function for an excessive size. */
2422 : :
2423 : : void
2424 : 41046 : pass_waccess::check_alloca (gcall *stmt)
2425 : : {
2426 : 41046 : if (m_early_checks_p)
2427 : : return;
2428 : :
2429 : 14397 : if ((warn_vla_limit >= HOST_WIDE_INT_MAX
2430 : 14370 : && warn_alloc_size_limit < warn_vla_limit)
2431 : 14387 : || (warn_alloca_limit >= HOST_WIDE_INT_MAX
2432 : 14324 : && warn_alloc_size_limit < warn_alloca_limit))
2433 : : {
2434 : : /* -Walloca-larger-than and -Wvla-larger-than settings of less
2435 : : than HWI_MAX override the more general -Walloc-size-larger-than
2436 : : so unless either of the former options is smaller than the last
2437 : : one (which would imply that the call was already checked), check
2438 : : the alloca arguments for overflow. */
2439 : 10 : const tree alloc_args[] = { call_arg (stmt, 0), NULL_TREE };
2440 : 10 : const int idx[] = { 0, -1 };
2441 : 10 : maybe_warn_alloc_args_overflow (stmt, alloc_args, idx);
2442 : : }
2443 : : }
2444 : :
2445 : : /* Check a call to an allocation function for an excessive size. */
2446 : :
2447 : : void
2448 : 4049100 : pass_waccess::check_alloc_size_call (gcall *stmt)
2449 : : {
2450 : 4049100 : if (m_early_checks_p)
2451 : 4001784 : return;
2452 : :
2453 : 1313466 : if (gimple_call_num_args (stmt) < 1)
2454 : : /* Avoid invalid calls to functions without a prototype. */
2455 : : return;
2456 : :
2457 : 1078986 : tree fndecl = gimple_call_fndecl (stmt);
2458 : 1078986 : if (fndecl && gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
2459 : : {
2460 : : /* Alloca is handled separately. */
2461 : 531482 : switch (DECL_FUNCTION_CODE (fndecl))
2462 : : {
2463 : : case BUILT_IN_ALLOCA:
2464 : : case BUILT_IN_ALLOCA_WITH_ALIGN:
2465 : : case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX:
2466 : : return;
2467 : : default:
2468 : : break;
2469 : : }
2470 : : }
2471 : :
2472 : 1069770 : tree fntype = gimple_call_fntype (stmt);
2473 : 1069770 : tree fntypeattrs = TYPE_ATTRIBUTES (fntype);
2474 : :
2475 : 1069770 : tree alloc_size = lookup_attribute ("alloc_size", fntypeattrs);
2476 : 1069770 : if (!alloc_size)
2477 : : return;
2478 : :
2479 : : /* Extract attribute alloc_size from the type of the called expression
2480 : : (which could be a function or a function pointer) and if set, store
2481 : : the indices of the corresponding arguments in ALLOC_IDX, and then
2482 : : the actual argument(s) at those indices in ALLOC_ARGS. */
2483 : 47321 : int idx[2] = { -1, -1 };
2484 : 47321 : tree alloc_args[] = { NULL_TREE, NULL_TREE };
2485 : 47321 : unsigned nargs = gimple_call_num_args (stmt);
2486 : :
2487 : 47321 : tree args = TREE_VALUE (alloc_size);
2488 : 47321 : idx[0] = TREE_INT_CST_LOW (TREE_VALUE (args)) - 1;
2489 : : /* Avoid invalid calls to functions without a prototype. */
2490 : 47321 : if ((unsigned) idx[0] >= nargs)
2491 : : return;
2492 : 47316 : alloc_args[0] = call_arg (stmt, idx[0]);
2493 : 47316 : if (TREE_CHAIN (args))
2494 : : {
2495 : 1190 : idx[1] = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (args))) - 1;
2496 : 1190 : if ((unsigned) idx[1] >= nargs)
2497 : : return;
2498 : 1190 : alloc_args[1] = call_arg (stmt, idx[1]);
2499 : : }
2500 : :
2501 : 47316 : maybe_warn_alloc_args_overflow (stmt, alloc_args, idx);
2502 : : }
2503 : :
2504 : : /* Check a call STMT to strcat() for overflow and warn if it does. */
2505 : :
2506 : : void
2507 : 2522 : pass_waccess::check_strcat (gcall *stmt)
2508 : : {
2509 : 2522 : if (m_early_checks_p)
2510 : 1758 : return;
2511 : :
2512 : 764 : if (!warn_stringop_overflow && !warn_stringop_overread)
2513 : : return;
2514 : :
2515 : 764 : tree dest = call_arg (stmt, 0);
2516 : 764 : tree src = call_arg (stmt, 1);
2517 : :
2518 : : /* There is no way here to determine the length of the string in
2519 : : the destination to which the SRC string is being appended so
2520 : : just diagnose cases when the source string is longer than
2521 : : the destination object. */
2522 : 764 : access_data data (m_ptr_qry.rvals, stmt, access_read_write, NULL_TREE,
2523 : 764 : true, NULL_TREE, true);
2524 : 764 : const int ost = warn_stringop_overflow ? warn_stringop_overflow - 1 : 1;
2525 : 764 : compute_objsize (src, stmt, ost, &data.src, &m_ptr_qry);
2526 : 764 : tree destsize = compute_objsize (dest, stmt, ost, &data.dst, &m_ptr_qry);
2527 : :
2528 : 764 : check_access (stmt, /*dstwrite=*/NULL_TREE, /*maxread=*/NULL_TREE,
2529 : : src, destsize, data.mode, &data, m_ptr_qry.rvals);
2530 : : }
2531 : :
2532 : : /* Check a call STMT to strcat() for overflow and warn if it does. */
2533 : :
2534 : : void
2535 : 2450 : pass_waccess::check_strncat (gcall *stmt)
2536 : : {
2537 : 2450 : if (m_early_checks_p)
2538 : 1650 : return;
2539 : :
2540 : 842 : if (!warn_stringop_overflow && !warn_stringop_overread)
2541 : : return;
2542 : :
2543 : 823 : tree dest = call_arg (stmt, 0);
2544 : 823 : tree src = call_arg (stmt, 1);
2545 : : /* The upper bound on the number of bytes to write. */
2546 : 823 : tree maxread = call_arg (stmt, 2);
2547 : :
2548 : : /* Detect unterminated source (only). */
2549 : 823 : if (!check_nul_terminated_array (stmt, src, maxread))
2550 : : return;
2551 : :
2552 : : /* The length of the source sequence. */
2553 : 817 : tree slen = c_strlen (src, 1);
2554 : :
2555 : : /* Try to determine the range of lengths that the source expression
2556 : : refers to. Since the lengths are only used for warning and not
2557 : : for code generation disable strict mode below. */
2558 : 817 : tree maxlen = slen;
2559 : 817 : if (!maxlen)
2560 : : {
2561 : 710 : c_strlen_data lendata = { };
2562 : 710 : get_range_strlen (src, &lendata, /* eltsize = */ 1);
2563 : 710 : maxlen = lendata.maxbound;
2564 : : }
2565 : :
2566 : 817 : access_data data (m_ptr_qry.rvals, stmt, access_read_write);
2567 : : /* Try to verify that the destination is big enough for the shortest
2568 : : string. First try to determine the size of the destination object
2569 : : into which the source is being copied. */
2570 : 817 : const int ost = warn_stringop_overflow - 1;
2571 : 817 : tree destsize = compute_objsize (dest, stmt, ost, &data.dst, &m_ptr_qry);
2572 : :
2573 : : /* Add one for the terminating nul. */
2574 : 817 : tree srclen = (maxlen
2575 : 817 : ? fold_build2 (PLUS_EXPR, size_type_node, maxlen,
2576 : : size_one_node)
2577 : 817 : : NULL_TREE);
2578 : :
2579 : : /* The strncat function copies at most MAXREAD bytes and always appends
2580 : : the terminating nul so the specified upper bound should never be equal
2581 : : to (or greater than) the size of the destination. */
2582 : 324 : if (tree_fits_uhwi_p (maxread) && tree_fits_uhwi_p (destsize)
2583 : 1141 : && tree_int_cst_equal (destsize, maxread))
2584 : : {
2585 : 17 : location_t loc = get_location (stmt);
2586 : 17 : warning_at (loc, OPT_Wstringop_overflow_,
2587 : : "%qD specified bound %E equals destination size",
2588 : : get_callee_fndecl (stmt), maxread);
2589 : :
2590 : 17 : return;
2591 : : }
2592 : :
2593 : 800 : if (!srclen
2594 : 800 : || (maxread && tree_fits_uhwi_p (maxread)
2595 : 237 : && tree_fits_uhwi_p (srclen)
2596 : 237 : && tree_int_cst_lt (maxread, srclen)))
2597 : : srclen = maxread;
2598 : :
2599 : 800 : check_access (stmt, /*dstwrite=*/NULL_TREE, maxread, srclen,
2600 : : destsize, data.mode, &data, m_ptr_qry.rvals);
2601 : : }
2602 : :
2603 : : /* Check a call STMT to stpcpy() or strcpy() for overflow and warn
2604 : : if it does. */
2605 : :
2606 : : void
2607 : 10572 : pass_waccess::check_stxcpy (gcall *stmt)
2608 : : {
2609 : 10572 : if (m_early_checks_p)
2610 : 7488 : return;
2611 : :
2612 : 3226 : tree dst = call_arg (stmt, 0);
2613 : 3226 : tree src = call_arg (stmt, 1);
2614 : :
2615 : 3226 : tree size;
2616 : 3226 : bool exact;
2617 : 3226 : if (tree nonstr = unterminated_array (src, &size, &exact))
2618 : : {
2619 : : /* NONSTR refers to the non-nul terminated constant array. */
2620 : 142 : warn_string_no_nul (get_location (stmt), stmt, NULL, src, nonstr,
2621 : : size, exact);
2622 : 142 : return;
2623 : : }
2624 : :
2625 : 3084 : if (warn_stringop_overflow)
2626 : : {
2627 : 2879 : access_data data (m_ptr_qry.rvals, stmt, access_read_write, NULL_TREE,
2628 : 2879 : true, NULL_TREE, true);
2629 : 2879 : const int ost = warn_stringop_overflow ? warn_stringop_overflow - 1 : 1;
2630 : 2879 : compute_objsize (src, stmt, ost, &data.src, &m_ptr_qry);
2631 : 2879 : tree dstsize = compute_objsize (dst, stmt, ost, &data.dst, &m_ptr_qry);
2632 : 2879 : check_access (stmt, /*dstwrite=*/ NULL_TREE,
2633 : : /*maxread=*/ NULL_TREE, /*srcstr=*/ src,
2634 : : dstsize, data.mode, &data, m_ptr_qry.rvals);
2635 : : }
2636 : :
2637 : : /* Check to see if the argument was declared attribute nonstring
2638 : : and if so, issue a warning since at this point it's not known
2639 : : to be nul-terminated. */
2640 : 3084 : tree fndecl = get_callee_fndecl (stmt);
2641 : 3084 : maybe_warn_nonstring_arg (fndecl, stmt);
2642 : : }
2643 : :
2644 : : /* Check a call STMT to stpncpy() or strncpy() for overflow and warn
2645 : : if it does. */
2646 : :
2647 : : void
2648 : 8500 : pass_waccess::check_stxncpy (gcall *stmt)
2649 : : {
2650 : 8500 : if (m_early_checks_p || !warn_stringop_overflow)
2651 : 5944 : return;
2652 : :
2653 : 2556 : tree dst = call_arg (stmt, 0);
2654 : 2556 : tree src = call_arg (stmt, 1);
2655 : : /* The number of bytes to write (not the maximum). */
2656 : 2556 : tree len = call_arg (stmt, 2);
2657 : :
2658 : 2556 : access_data data (m_ptr_qry.rvals, stmt, access_read_write, len, true, len,
2659 : 2556 : true);
2660 : 2556 : const int ost = warn_stringop_overflow ? warn_stringop_overflow - 1 : 1;
2661 : 2556 : compute_objsize (src, stmt, ost, &data.src, &m_ptr_qry);
2662 : 2556 : tree dstsize = compute_objsize (dst, stmt, ost, &data.dst, &m_ptr_qry);
2663 : :
2664 : 2556 : check_access (stmt, /*dstwrite=*/len, /*maxread=*/len, src, dstsize,
2665 : : data.mode, &data, m_ptr_qry.rvals);
2666 : : }
2667 : :
2668 : : /* Check a call STMT to stpncpy() or strncpy() for overflow and warn
2669 : : if it does. */
2670 : :
2671 : : void
2672 : 8425 : pass_waccess::check_strncmp (gcall *stmt)
2673 : : {
2674 : 8425 : if (m_early_checks_p || !warn_stringop_overread)
2675 : 6346 : return;
2676 : :
2677 : 2817 : tree arg1 = call_arg (stmt, 0);
2678 : 2817 : tree arg2 = call_arg (stmt, 1);
2679 : 2817 : tree bound = call_arg (stmt, 2);
2680 : :
2681 : : /* First check each argument separately, considering the bound. */
2682 : 2817 : if (!check_nul_terminated_array (stmt, arg1, bound)
2683 : 2817 : || !check_nul_terminated_array (stmt, arg2, bound))
2684 : 6 : return;
2685 : :
2686 : : /* A strncmp read from each argument is constrained not just by
2687 : : the bound but also by the length of the shorter string. Specifying
2688 : : a bound that's larger than the size of either array makes no sense
2689 : : and is likely a bug. When the length of neither of the two strings
2690 : : is known but the sizes of both of the arrays they are stored in is,
2691 : : issue a warning if the bound is larger than the size of
2692 : : the larger of the two arrays. */
2693 : :
2694 : 2811 : c_strlen_data lendata1{ }, lendata2{ };
2695 : 2811 : tree len1 = c_strlen (arg1, 1, &lendata1);
2696 : 2811 : tree len2 = c_strlen (arg2, 1, &lendata2);
2697 : :
2698 : 2811 : if (len1 && TREE_CODE (len1) != INTEGER_CST)
2699 : 2546 : len1 = NULL_TREE;
2700 : 2811 : if (len2 && TREE_CODE (len2) != INTEGER_CST)
2701 : 1551 : len2 = NULL_TREE;
2702 : :
2703 : 2811 : if (len1 && len2)
2704 : : /* If the length of both arguments was computed they must both be
2705 : : nul-terminated and no further checking is necessary regardless
2706 : : of the bound. */
2707 : : return;
2708 : :
2709 : : /* Check to see if the argument was declared with attribute nonstring
2710 : : and if so, issue a warning since at this point it's not known to be
2711 : : nul-terminated. */
2712 : 2694 : if (maybe_warn_nonstring_arg (get_callee_fndecl (stmt), stmt))
2713 : : return;
2714 : :
2715 : 2521 : access_data adata1 (m_ptr_qry.rvals, stmt, access_read_only, NULL_TREE, false,
2716 : 2521 : bound, true);
2717 : 2521 : access_data adata2 (m_ptr_qry.rvals, stmt, access_read_only, NULL_TREE, false,
2718 : 2521 : bound, true);
2719 : :
2720 : : /* Determine the range of the bound first and bail if it fails; it's
2721 : : cheaper than computing the size of the objects. */
2722 : 2521 : tree bndrng[2] = { NULL_TREE, NULL_TREE };
2723 : 2521 : get_size_range (m_ptr_qry.rvals, bound, stmt, bndrng, 0, adata1.src_bndrng);
2724 : 2521 : if (!bndrng[0] || integer_zerop (bndrng[0]))
2725 : 442 : return;
2726 : :
2727 : 2079 : if (len1 && tree_int_cst_lt (len1, bndrng[0]))
2728 : 20 : bndrng[0] = len1;
2729 : 2079 : if (len2 && tree_int_cst_lt (len2, bndrng[0]))
2730 : 54 : bndrng[0] = len2;
2731 : :
2732 : : /* compute_objsize almost never fails (and ultimately should never
2733 : : fail). Don't bother to handle the rare case when it does. */
2734 : 2079 : if (!compute_objsize (arg1, stmt, 1, &adata1.src, &m_ptr_qry)
2735 : 2079 : || !compute_objsize (arg2, stmt, 1, &adata2.src, &m_ptr_qry))
2736 : 0 : return;
2737 : :
2738 : : /* Compute the size of the remaining space in each array after
2739 : : subtracting any offset into it. */
2740 : 2079 : offset_int rem1 = adata1.src.size_remaining ();
2741 : 2079 : offset_int rem2 = adata2.src.size_remaining ();
2742 : :
2743 : : /* Cap REM1 and REM2 at the other if the other's argument is known
2744 : : to be an unterminated array, either because there's no space
2745 : : left in it after adding its offset or because it's constant and
2746 : : has no nul. */
2747 : 4155 : if (rem1 == 0 || (rem1 < rem2 && lendata1.decl))
2748 : 5 : rem2 = rem1;
2749 : 4147 : else if (rem2 == 0 || (rem2 < rem1 && lendata2.decl))
2750 : 3 : rem1 = rem2;
2751 : :
2752 : : /* Point PAD at the array to reference in the note if a warning
2753 : : is issued. */
2754 : 2079 : access_data *pad = len1 ? &adata2 : &adata1;
2755 : 2079 : offset_int maxrem = wi::max (rem1, rem2, UNSIGNED);
2756 : 2077 : if (lendata1.decl || lendata2.decl
2757 : 4154 : || maxrem < wi::to_offset (bndrng[0]))
2758 : : {
2759 : : /* Warn when either argument isn't nul-terminated or the maximum
2760 : : remaining space in the two arrays is less than the bound. */
2761 : 21 : tree func = get_callee_fndecl (stmt);
2762 : 21 : location_t loc = gimple_location (stmt);
2763 : 42 : maybe_warn_for_bound (OPT_Wstringop_overread, loc, stmt, func,
2764 : 42 : bndrng, wide_int_to_tree (sizetype, maxrem),
2765 : : pad);
2766 : : }
2767 : : }
2768 : :
2769 : : /* Determine and check the sizes of the source and the destination
2770 : : of calls to __builtin_{bzero,memcpy,mempcpy,memset} calls. STMT is
2771 : : the call statement, DEST is the destination argument, SRC is the source
2772 : : argument or null, and SIZE is the number of bytes being accessed. Use
2773 : : Object Size type-0 regardless of the OPT_Wstringop_overflow_ setting.
2774 : : Return true on success (no overflow or invalid sizes), false otherwise. */
2775 : :
2776 : : void
2777 : 815117 : pass_waccess::check_memop_access (gimple *stmt, tree dest, tree src, tree size)
2778 : : {
2779 : 815117 : if (m_early_checks_p)
2780 : 493197 : return;
2781 : :
2782 : : /* For functions like memset and memcpy that operate on raw memory
2783 : : try to determine the size of the largest source and destination
2784 : : object using type-0 Object Size regardless of the object size
2785 : : type specified by the option. */
2786 : 321920 : access_data data (m_ptr_qry.rvals, stmt, access_read_write);
2787 : 321920 : tree srcsize
2788 : 321920 : = src ? compute_objsize (src, stmt, 0, &data.src, &m_ptr_qry) : NULL_TREE;
2789 : 321920 : tree dstsize = compute_objsize (dest, stmt, 0, &data.dst, &m_ptr_qry);
2790 : :
2791 : 321920 : check_access (stmt, size, /*maxread=*/NULL_TREE, srcsize, dstsize,
2792 : : data.mode, &data, m_ptr_qry.rvals);
2793 : : }
2794 : :
2795 : : /* A convenience wrapper for check_access to check access by a read-only
2796 : : function like puts or strcmp. */
2797 : :
2798 : : void
2799 : 1397942 : pass_waccess::check_read_access (gimple *stmt, tree src,
2800 : : tree bound /* = NULL_TREE */,
2801 : : int ost /* = 1 */)
2802 : : {
2803 : 1397942 : if (m_early_checks_p || !warn_stringop_overread)
2804 : 999290 : return;
2805 : :
2806 : 398652 : if (bound && !useless_type_conversion_p (size_type_node, TREE_TYPE (bound)))
2807 : 0 : bound = fold_convert (size_type_node, bound);
2808 : :
2809 : 398652 : tree fndecl = get_callee_fndecl (stmt);
2810 : 398652 : maybe_warn_nonstring_arg (fndecl, stmt);
2811 : :
2812 : 398652 : access_data data (m_ptr_qry.rvals, stmt, access_read_only, NULL_TREE,
2813 : 398652 : false, bound, true);
2814 : 398652 : compute_objsize (src, stmt, ost, &data.src, &m_ptr_qry);
2815 : 398652 : check_access (stmt, /*dstwrite=*/ NULL_TREE, /*maxread=*/ bound,
2816 : : /*srcstr=*/ src, /*dstsize=*/ NULL_TREE, data.mode,
2817 : : &data, m_ptr_qry.rvals);
2818 : : }
2819 : :
2820 : : /* Return true if memory model ORD is constant in the context of STMT and
2821 : : set *CSTVAL to the constant value. Otherwise return false. Warn for
2822 : : invalid ORD. */
2823 : :
2824 : : bool
2825 : 348767 : memmodel_to_uhwi (tree ord, gimple *stmt, unsigned HOST_WIDE_INT *cstval)
2826 : : {
2827 : 348767 : unsigned HOST_WIDE_INT val;
2828 : :
2829 : 348767 : if (TREE_CODE (ord) == INTEGER_CST)
2830 : : {
2831 : 345572 : if (!tree_fits_uhwi_p (ord))
2832 : : return false;
2833 : 345564 : val = tree_to_uhwi (ord);
2834 : : }
2835 : : else
2836 : : {
2837 : : /* Use the range query to determine constant values in the absence
2838 : : of constant propagation (such as at -O0). */
2839 : 3195 : int_range_max rng (TREE_TYPE (ord));
2840 : 6390 : if (!get_range_query (cfun)->range_of_expr (rng, ord, stmt)
2841 : 3195 : || !rng.singleton_p (&ord))
2842 : 2742 : return false;
2843 : :
2844 : 453 : wide_int lob = rng.lower_bound ();
2845 : 453 : if (!wi::fits_uhwi_p (lob))
2846 : 0 : return false;
2847 : :
2848 : 453 : val = lob.to_shwi ();
2849 : 3195 : }
2850 : :
2851 : 346017 : if (targetm.memmodel_check)
2852 : : /* This might warn for an invalid VAL but return a conservatively
2853 : : valid result. */
2854 : 346017 : val = targetm.memmodel_check (val);
2855 : 0 : else if (val & ~MEMMODEL_MASK)
2856 : : {
2857 : 0 : tree fndecl = gimple_call_fndecl (stmt);
2858 : 0 : location_t loc = gimple_location (stmt);
2859 : 0 : loc = expansion_point_location_if_in_system_header (loc);
2860 : :
2861 : 0 : warning_at (loc, OPT_Winvalid_memory_model,
2862 : : "unknown architecture specifier in memory model "
2863 : : "%wi for %qD", val, fndecl);
2864 : 0 : return false;
2865 : : }
2866 : :
2867 : 346017 : *cstval = val;
2868 : :
2869 : 346017 : return true;
2870 : : }
2871 : :
2872 : : /* Valid memory model for each set of atomic built-in functions. */
2873 : :
2874 : : struct memmodel_pair
2875 : : {
2876 : : memmodel modval;
2877 : : const char* modname;
2878 : :
2879 : : #define MEMMODEL_PAIR(val, str) \
2880 : : { MEMMODEL_ ## val, "memory_order_" str }
2881 : : };
2882 : :
2883 : : /* Valid memory models in the order of increasing strength. */
2884 : :
2885 : : static const memmodel_pair memory_models[] =
2886 : : { MEMMODEL_PAIR (RELAXED, "relaxed"),
2887 : : MEMMODEL_PAIR (SEQ_CST, "seq_cst"),
2888 : : MEMMODEL_PAIR (ACQUIRE, "acquire"),
2889 : : MEMMODEL_PAIR (CONSUME, "consume"),
2890 : : MEMMODEL_PAIR (RELEASE, "release"),
2891 : : MEMMODEL_PAIR (ACQ_REL, "acq_rel")
2892 : : };
2893 : :
2894 : : /* Return the name of the memory model VAL. */
2895 : :
2896 : : static const char*
2897 : 230 : memmodel_name (unsigned HOST_WIDE_INT val)
2898 : : {
2899 : 230 : val = memmodel_base (val);
2900 : :
2901 : 988 : for (unsigned i = 0; i != ARRAY_SIZE (memory_models); ++i)
2902 : : {
2903 : 971 : if (val == memory_models[i].modval)
2904 : 213 : return memory_models[i].modname;
2905 : : }
2906 : : return NULL;
2907 : : }
2908 : :
2909 : : /* Indices of valid MEMORY_MODELS above for corresponding atomic operations. */
2910 : : static const unsigned char load_models[] = { 0, 1, 2, 3, UCHAR_MAX };
2911 : : static const unsigned char store_models[] = { 0, 1, 4, UCHAR_MAX };
2912 : : static const unsigned char xchg_models[] = { 0, 1, 3, 4, 5, UCHAR_MAX };
2913 : : static const unsigned char flag_clr_models[] = { 0, 1, 4, UCHAR_MAX };
2914 : : static const unsigned char all_models[] = { 0, 1, 2, 3, 4, 5, UCHAR_MAX };
2915 : :
2916 : : /* Check the success memory model argument ORD_SUCS to the call STMT to
2917 : : an atomic function and warn if it's invalid. If nonnull, also check
2918 : : the failure memory model ORD_FAIL and warn if it's invalid. Return
2919 : : true if a warning has been issued. */
2920 : :
2921 : : bool
2922 : 321546 : pass_waccess::maybe_warn_memmodel (gimple *stmt, tree ord_sucs,
2923 : : tree ord_fail, const unsigned char *valid)
2924 : : {
2925 : 321546 : unsigned HOST_WIDE_INT sucs, fail = 0;
2926 : 321546 : if (!memmodel_to_uhwi (ord_sucs, stmt, &sucs)
2927 : 321546 : || (ord_fail && !memmodel_to_uhwi (ord_fail, stmt, &fail)))
2928 : 2750 : return false;
2929 : :
2930 : 318796 : bool is_valid = false;
2931 : 318796 : if (valid)
2932 : 690534 : for (unsigned i = 0; valid[i] != UCHAR_MAX; ++i)
2933 : : {
2934 : 690452 : memmodel model = memory_models[valid[i]].modval;
2935 : 690452 : if (memmodel_base (sucs) == model)
2936 : : {
2937 : : is_valid = true;
2938 : : break;
2939 : : }
2940 : : }
2941 : : else
2942 : : is_valid = true;
2943 : :
2944 : 318796 : tree fndecl = gimple_call_fndecl (stmt);
2945 : 318796 : location_t loc = gimple_location (stmt);
2946 : 318796 : loc = expansion_point_location_if_in_system_header (loc);
2947 : :
2948 : 318796 : if (!is_valid)
2949 : : {
2950 : 82 : bool warned = false;
2951 : 82 : auto_diagnostic_group d;
2952 : 82 : if (const char *modname = memmodel_name (sucs))
2953 : 65 : warned = warning_at (loc, OPT_Winvalid_memory_model,
2954 : : "invalid memory model %qs for %qD",
2955 : : modname, fndecl);
2956 : : else
2957 : 17 : warned = warning_at (loc, OPT_Winvalid_memory_model,
2958 : : "invalid memory model %wi for %qD",
2959 : : sucs, fndecl);
2960 : :
2961 : 82 : if (!warned)
2962 : : return false;
2963 : :
2964 : : /* Print a note with the valid memory models. */
2965 : 82 : auto_vec<const char *> strings;
2966 : 401 : for (unsigned i = 0; valid[i] != UCHAR_MAX; ++i)
2967 : : {
2968 : 319 : const char *modname = memory_models[valid[i]].modname;
2969 : 319 : strings.safe_push (modname);
2970 : : }
2971 : 82 : pp_markup::comma_separated_quoted_strings e (strings);
2972 : 82 : inform (loc, "valid models are %e", &e);
2973 : 82 : return true;
2974 : 82 : }
2975 : :
2976 : 318714 : if (!ord_fail)
2977 : : return false;
2978 : :
2979 : 27209 : if (fail == MEMMODEL_RELEASE || fail == MEMMODEL_ACQ_REL)
2980 : 60 : if (const char *failname = memmodel_name (fail))
2981 : : {
2982 : : /* If both memory model arguments are valid but their combination
2983 : : is not, use their names in the warning. */
2984 : 60 : auto_diagnostic_group d;
2985 : 60 : if (!warning_at (loc, OPT_Winvalid_memory_model,
2986 : : "invalid failure memory model %qs for %qD",
2987 : : failname, fndecl))
2988 : : return false;
2989 : :
2990 : 60 : inform (loc,
2991 : : "valid failure models are %qs, %qs, %qs, %qs",
2992 : : "memory_order_relaxed", "memory_order_seq_cst",
2993 : : "memory_order_acquire", "memory_order_consume");
2994 : 60 : return true;
2995 : 60 : }
2996 : :
2997 : 27149 : if (memmodel_base (fail) <= memmodel_base (sucs))
2998 : : return false;
2999 : :
3000 : 44 : if (const char *sucsname = memmodel_name (sucs))
3001 : 44 : if (const char *failname = memmodel_name (fail))
3002 : : {
3003 : : /* If both memory model arguments are valid but their combination
3004 : : is not, use their names in the warning. */
3005 : 44 : auto_diagnostic_group d;
3006 : 44 : if (!warning_at (loc, OPT_Winvalid_memory_model,
3007 : : "failure memory model %qs cannot be stronger "
3008 : : "than success memory model %qs for %qD",
3009 : : failname, sucsname, fndecl))
3010 : : return false;
3011 : :
3012 : : /* Print a note with the valid failure memory models which are
3013 : : those with a value less than or equal to the success mode. */
3014 : 44 : auto_vec<const char *> strings;
3015 : 88 : for (unsigned i = 0;
3016 : 88 : memory_models[i].modval <= memmodel_base (sucs); ++i)
3017 : : {
3018 : 44 : const char *modname = memory_models[valid[i]].modname;
3019 : 44 : strings.safe_push (modname);
3020 : : }
3021 : 44 : pp_markup::comma_separated_quoted_strings e (strings);
3022 : :
3023 : 44 : inform (loc, "valid models are %e", &e);
3024 : 44 : return true;
3025 : 44 : }
3026 : :
3027 : : /* If either memory model argument value is invalid use the numerical
3028 : : value of both in the message. */
3029 : 0 : return warning_at (loc, OPT_Winvalid_memory_model,
3030 : : "failure memory model %wi cannot be stronger "
3031 : : "than success memory model %wi for %qD",
3032 : : fail, sucs, fndecl);
3033 : : }
3034 : :
3035 : : /* Wrapper for the above. */
3036 : :
3037 : : void
3038 : 321607 : pass_waccess::check_atomic_memmodel (gimple *stmt, tree ord_sucs,
3039 : : tree ord_fail, const unsigned char *valid)
3040 : : {
3041 : 321607 : if (warning_suppressed_p (stmt, OPT_Winvalid_memory_model))
3042 : : return;
3043 : :
3044 : 321546 : if (!maybe_warn_memmodel (stmt, ord_sucs, ord_fail, valid))
3045 : : return;
3046 : :
3047 : 186 : suppress_warning (stmt, OPT_Winvalid_memory_model);
3048 : : }
3049 : :
3050 : : /* Check a call STMT to an atomic or sync built-in. */
3051 : :
3052 : : bool
3053 : 3491819 : pass_waccess::check_atomic_builtin (gcall *stmt)
3054 : : {
3055 : 3491819 : tree callee = gimple_call_fndecl (stmt);
3056 : 3491819 : if (!callee)
3057 : : return false;
3058 : :
3059 : : /* The size in bytes of the access by the function, and the number
3060 : : of the second argument to check (if any). */
3061 : 3491819 : unsigned bytes = 0, arg2 = UINT_MAX;
3062 : 3491819 : unsigned sucs_arg = UINT_MAX, fail_arg = UINT_MAX;
3063 : : /* Points to the array of indices of valid memory models. */
3064 : 3491819 : const unsigned char *pvalid_models = NULL;
3065 : :
3066 : 3491819 : switch (DECL_FUNCTION_CODE (callee))
3067 : : {
3068 : : #define BUILTIN_ACCESS_SIZE_FNSPEC(N) \
3069 : : BUILT_IN_SYNC_FETCH_AND_ADD_ ## N: \
3070 : : case BUILT_IN_SYNC_FETCH_AND_SUB_ ## N: \
3071 : : case BUILT_IN_SYNC_FETCH_AND_OR_ ## N: \
3072 : : case BUILT_IN_SYNC_FETCH_AND_AND_ ## N: \
3073 : : case BUILT_IN_SYNC_FETCH_AND_XOR_ ## N: \
3074 : : case BUILT_IN_SYNC_FETCH_AND_NAND_ ## N: \
3075 : : case BUILT_IN_SYNC_ADD_AND_FETCH_ ## N: \
3076 : : case BUILT_IN_SYNC_SUB_AND_FETCH_ ## N: \
3077 : : case BUILT_IN_SYNC_OR_AND_FETCH_ ## N: \
3078 : : case BUILT_IN_SYNC_AND_AND_FETCH_ ## N: \
3079 : : case BUILT_IN_SYNC_XOR_AND_FETCH_ ## N: \
3080 : : case BUILT_IN_SYNC_NAND_AND_FETCH_ ## N: \
3081 : : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_ ## N: \
3082 : : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_ ## N: \
3083 : : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_ ## N: \
3084 : : case BUILT_IN_SYNC_LOCK_RELEASE_ ## N: \
3085 : : bytes = N; \
3086 : : break; \
3087 : : case BUILT_IN_ATOMIC_LOAD_ ## N: \
3088 : : pvalid_models = load_models; \
3089 : : sucs_arg = 1; \
3090 : : /* FALLTHROUGH */ \
3091 : : case BUILT_IN_ATOMIC_STORE_ ## N: \
3092 : : if (!pvalid_models) \
3093 : : pvalid_models = store_models; \
3094 : : /* FALLTHROUGH */ \
3095 : : case BUILT_IN_ATOMIC_ADD_FETCH_ ## N: \
3096 : : case BUILT_IN_ATOMIC_SUB_FETCH_ ## N: \
3097 : : case BUILT_IN_ATOMIC_AND_FETCH_ ## N: \
3098 : : case BUILT_IN_ATOMIC_NAND_FETCH_ ## N: \
3099 : : case BUILT_IN_ATOMIC_XOR_FETCH_ ## N: \
3100 : : case BUILT_IN_ATOMIC_OR_FETCH_ ## N: \
3101 : : case BUILT_IN_ATOMIC_FETCH_ADD_ ## N: \
3102 : : case BUILT_IN_ATOMIC_FETCH_SUB_ ## N: \
3103 : : case BUILT_IN_ATOMIC_FETCH_AND_ ## N: \
3104 : : case BUILT_IN_ATOMIC_FETCH_NAND_ ## N: \
3105 : : case BUILT_IN_ATOMIC_FETCH_OR_ ## N: \
3106 : : case BUILT_IN_ATOMIC_FETCH_XOR_ ## N: \
3107 : : bytes = N; \
3108 : : if (sucs_arg == UINT_MAX) \
3109 : : sucs_arg = 2; \
3110 : : if (!pvalid_models) \
3111 : : pvalid_models = all_models; \
3112 : : break; \
3113 : : case BUILT_IN_ATOMIC_EXCHANGE_ ## N: \
3114 : : bytes = N; \
3115 : : sucs_arg = 3; \
3116 : : pvalid_models = xchg_models; \
3117 : : break; \
3118 : : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_ ## N: \
3119 : : bytes = N; \
3120 : : sucs_arg = 4; \
3121 : : fail_arg = 5; \
3122 : : pvalid_models = all_models; \
3123 : : arg2 = 1
3124 : :
3125 : 90876 : case BUILTIN_ACCESS_SIZE_FNSPEC (1);
3126 : 5631 : break;
3127 : 23882 : case BUILTIN_ACCESS_SIZE_FNSPEC (2);
3128 : 1900 : break;
3129 : 123567 : case BUILTIN_ACCESS_SIZE_FNSPEC (4);
3130 : 6801 : break;
3131 : 97286 : case BUILTIN_ACCESS_SIZE_FNSPEC (8);
3132 : 8660 : break;
3133 : 21357 : case BUILTIN_ACCESS_SIZE_FNSPEC (16);
3134 : 4693 : break;
3135 : :
3136 : 158 : case BUILT_IN_ATOMIC_CLEAR:
3137 : 158 : sucs_arg = 1;
3138 : 158 : pvalid_models = flag_clr_models;
3139 : 158 : break;
3140 : :
3141 : : default:
3142 : : return false;
3143 : : }
3144 : :
3145 : 342411 : unsigned nargs = gimple_call_num_args (stmt);
3146 : 342411 : if (sucs_arg < nargs)
3147 : : {
3148 : 321607 : tree ord_sucs = gimple_call_arg (stmt, sucs_arg);
3149 : 321607 : tree ord_fail = NULL_TREE;
3150 : 321607 : if (fail_arg < nargs)
3151 : 27685 : ord_fail = gimple_call_arg (stmt, fail_arg);
3152 : 321607 : check_atomic_memmodel (stmt, ord_sucs, ord_fail, pvalid_models);
3153 : : }
3154 : :
3155 : 342411 : if (!bytes)
3156 : : return true;
3157 : :
3158 : 342253 : tree size = build_int_cstu (sizetype, bytes);
3159 : 342253 : tree dst = gimple_call_arg (stmt, 0);
3160 : 342253 : check_memop_access (stmt, dst, NULL_TREE, size);
3161 : :
3162 : 342253 : if (arg2 != UINT_MAX)
3163 : : {
3164 : 27685 : tree dst = gimple_call_arg (stmt, arg2);
3165 : 27685 : check_memop_access (stmt, dst, NULL_TREE, size);
3166 : : }
3167 : :
3168 : : return true;
3169 : : }
3170 : :
3171 : : /* Check call STMT to a built-in function for invalid accesses. Return
3172 : : true if a call has been handled. */
3173 : :
3174 : : bool
3175 : 4920337 : pass_waccess::check_builtin (gcall *stmt)
3176 : : {
3177 : 4920337 : tree callee = gimple_call_fndecl (stmt);
3178 : 4920337 : if (!callee)
3179 : : return false;
3180 : :
3181 : 4920337 : switch (DECL_FUNCTION_CODE (callee))
3182 : : {
3183 : 41046 : case BUILT_IN_ALLOCA:
3184 : 41046 : case BUILT_IN_ALLOCA_WITH_ALIGN:
3185 : 41046 : case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX:
3186 : 41046 : check_alloca (stmt);
3187 : 41046 : return true;
3188 : :
3189 : 73 : case BUILT_IN_EXECL:
3190 : 73 : case BUILT_IN_EXECLE:
3191 : 73 : case BUILT_IN_EXECLP:
3192 : 73 : case BUILT_IN_EXECV:
3193 : 73 : case BUILT_IN_EXECVE:
3194 : 73 : case BUILT_IN_EXECVP:
3195 : 73 : check_read_access (stmt, call_arg (stmt, 0));
3196 : 73 : return true;
3197 : :
3198 : 171835 : case BUILT_IN_FREE:
3199 : 171835 : case BUILT_IN_REALLOC:
3200 : 171835 : if (!m_early_checks_p)
3201 : : {
3202 : 60597 : tree arg = call_arg (stmt, 0);
3203 : 60597 : if (TREE_CODE (arg) == SSA_NAME)
3204 : 60513 : check_pointer_uses (stmt, arg);
3205 : : }
3206 : : return true;
3207 : :
3208 : 15225 : case BUILT_IN_GETTEXT:
3209 : 15225 : case BUILT_IN_PUTS:
3210 : 15225 : case BUILT_IN_PUTS_UNLOCKED:
3211 : 15225 : case BUILT_IN_STRDUP:
3212 : 15225 : check_read_access (stmt, call_arg (stmt, 0));
3213 : 15225 : return true;
3214 : :
3215 : 48676 : case BUILT_IN_INDEX:
3216 : 48676 : case BUILT_IN_RINDEX:
3217 : 48676 : case BUILT_IN_STRCHR:
3218 : 48676 : case BUILT_IN_STRRCHR:
3219 : 48676 : case BUILT_IN_STRLEN:
3220 : 48676 : check_read_access (stmt, call_arg (stmt, 0));
3221 : 48676 : return true;
3222 : :
3223 : 4305 : case BUILT_IN_FPUTS:
3224 : 4305 : case BUILT_IN_FPUTS_UNLOCKED:
3225 : 4305 : check_read_access (stmt, call_arg (stmt, 0));
3226 : 4305 : return true;
3227 : :
3228 : 3476 : case BUILT_IN_STRNDUP:
3229 : 3476 : case BUILT_IN_STRNLEN:
3230 : 3476 : {
3231 : 3476 : tree str = call_arg (stmt, 0);
3232 : 3476 : tree len = call_arg (stmt, 1);
3233 : 3476 : check_read_access (stmt, str, len);
3234 : 3476 : return true;
3235 : : }
3236 : :
3237 : 2522 : case BUILT_IN_STRCAT:
3238 : 2522 : check_strcat (stmt);
3239 : 2522 : return true;
3240 : :
3241 : 2450 : case BUILT_IN_STRNCAT:
3242 : 2450 : check_strncat (stmt);
3243 : 2450 : return true;
3244 : :
3245 : 10572 : case BUILT_IN_STPCPY:
3246 : 10572 : case BUILT_IN_STRCPY:
3247 : 10572 : check_stxcpy (stmt);
3248 : 10572 : return true;
3249 : :
3250 : 8500 : case BUILT_IN_STPNCPY:
3251 : 8500 : case BUILT_IN_STRNCPY:
3252 : 8500 : check_stxncpy (stmt);
3253 : 8500 : return true;
3254 : :
3255 : 388067 : case BUILT_IN_STRCASECMP:
3256 : 388067 : case BUILT_IN_STRCMP:
3257 : 388067 : case BUILT_IN_STRPBRK:
3258 : 388067 : case BUILT_IN_STRSPN:
3259 : 388067 : case BUILT_IN_STRCSPN:
3260 : 388067 : case BUILT_IN_STRSTR:
3261 : 388067 : check_read_access (stmt, call_arg (stmt, 0));
3262 : 388067 : check_read_access (stmt, call_arg (stmt, 1));
3263 : 388067 : return true;
3264 : :
3265 : 8425 : case BUILT_IN_STRNCASECMP:
3266 : 8425 : case BUILT_IN_STRNCMP:
3267 : 8425 : check_strncmp (stmt);
3268 : 8425 : return true;
3269 : :
3270 : 271886 : case BUILT_IN_MEMCMP:
3271 : 271886 : {
3272 : 271886 : tree a1 = call_arg (stmt, 0);
3273 : 271886 : tree a2 = call_arg (stmt, 1);
3274 : 271886 : tree len = call_arg (stmt, 2);
3275 : 271886 : check_read_access (stmt, a1, len, 0);
3276 : 271886 : check_read_access (stmt, a2, len, 0);
3277 : 271886 : return true;
3278 : : }
3279 : :
3280 : 243735 : case BUILT_IN_MEMCPY:
3281 : 243735 : case BUILT_IN_MEMPCPY:
3282 : 243735 : case BUILT_IN_MEMMOVE:
3283 : 243735 : {
3284 : 243735 : tree dst = call_arg (stmt, 0);
3285 : 243735 : tree src = call_arg (stmt, 1);
3286 : 243735 : tree len = call_arg (stmt, 2);
3287 : 243735 : check_memop_access (stmt, dst, src, len);
3288 : 243735 : return true;
3289 : : }
3290 : :
3291 : 6281 : case BUILT_IN_MEMCHR:
3292 : 6281 : {
3293 : 6281 : tree src = call_arg (stmt, 0);
3294 : 6281 : tree len = call_arg (stmt, 2);
3295 : 6281 : check_read_access (stmt, src, len, 0);
3296 : 6281 : return true;
3297 : : }
3298 : :
3299 : 201444 : case BUILT_IN_MEMSET:
3300 : 201444 : {
3301 : 201444 : tree dst = call_arg (stmt, 0);
3302 : 201444 : tree len = call_arg (stmt, 2);
3303 : 201444 : check_memop_access (stmt, dst, NULL_TREE, len);
3304 : 201444 : return true;
3305 : : }
3306 : :
3307 : 3491819 : default:
3308 : 3491819 : if (check_atomic_builtin (stmt))
3309 : : return true;
3310 : : break;
3311 : : }
3312 : :
3313 : : return false;
3314 : : }
3315 : :
3316 : : /* Returns the type of the argument ARGNO to function with type FNTYPE
3317 : : or null when the type cannot be determined or no such argument exists. */
3318 : :
3319 : : static tree
3320 : 89367 : fntype_argno_type (tree fntype, unsigned argno)
3321 : : {
3322 : 89367 : if (!prototype_p (fntype))
3323 : : return NULL_TREE;
3324 : :
3325 : 89363 : tree argtype;
3326 : 89363 : function_args_iterator it;
3327 : 175984 : FOREACH_FUNCTION_ARGS (fntype, argtype, it)
3328 : 175984 : if (argno-- == 0)
3329 : : return argtype;
3330 : :
3331 : : return NULL_TREE;
3332 : : }
3333 : :
3334 : : /* Helper to append the "human readable" attribute access specification
3335 : : described by ACCESS to the array ATTRSTR with size STRSIZE. Used in
3336 : : diagnostics. */
3337 : :
3338 : : static inline void
3339 : 280 : append_attrname (const std::pair<int, attr_access> &access,
3340 : : char *attrstr, size_t strsize)
3341 : : {
3342 : 280 : if (access.second.internal_p)
3343 : : return;
3344 : :
3345 : 262 : tree str = access.second.to_external_string ();
3346 : 262 : gcc_assert (strsize >= (size_t) TREE_STRING_LENGTH (str));
3347 : 262 : strcpy (attrstr, TREE_STRING_POINTER (str));
3348 : : }
3349 : :
3350 : : /* Iterate over attribute access read-only, read-write, and write-only
3351 : : arguments and diagnose past-the-end accesses and related problems
3352 : : in the function call EXP. */
3353 : :
3354 : : void
3355 : 4049100 : pass_waccess::maybe_check_access_sizes (rdwr_map *rwm, tree fndecl, tree fntype,
3356 : : gimple *stmt)
3357 : : {
3358 : 4049100 : if (warning_suppressed_p (stmt, OPT_Wnonnull)
3359 : 4049100 : || warning_suppressed_p (stmt, OPT_Wstringop_overflow_))
3360 : 21735 : return;
3361 : :
3362 : 4027369 : auto_diagnostic_group adg;
3363 : :
3364 : : /* Set if a warning has been issued for any argument (used to decide
3365 : : whether to emit an informational note at the end). */
3366 : 4027369 : opt_code opt_warned = no_warning;
3367 : :
3368 : : /* A string describing the attributes that the warnings issued by this
3369 : : function apply to. Used to print one informational note per function
3370 : : call, rather than one per warning. That reduces clutter. */
3371 : 4027369 : char attrstr[80];
3372 : 4027369 : attrstr[0] = 0;
3373 : :
3374 : 4210229 : for (rdwr_map::iterator it = rwm->begin (); it != rwm->end (); ++it)
3375 : : {
3376 : 91434 : std::pair<int, attr_access> access = *it;
3377 : :
3378 : : /* Get the function call arguments corresponding to the attribute's
3379 : : positional arguments. When both arguments have been specified
3380 : : there will be two entries in *RWM, one for each. They are
3381 : : cross-referenced by their respective argument numbers in
3382 : : ACCESS.PTRARG and ACCESS.SIZARG. */
3383 : 91434 : const int ptridx = access.second.ptrarg;
3384 : 91434 : const int sizidx = access.second.sizarg;
3385 : :
3386 : 91434 : gcc_assert (ptridx != -1);
3387 : 91434 : gcc_assert (access.first == ptridx || access.first == sizidx);
3388 : :
3389 : : /* The pointer is set to null for the entry corresponding to
3390 : : the size argument. Skip it. It's handled when the entry
3391 : : corresponding to the pointer argument comes up. */
3392 : 91434 : if (!access.second.ptr)
3393 : 2098 : continue;
3394 : :
3395 : 89367 : tree ptrtype = fntype_argno_type (fntype, ptridx);
3396 : 89367 : if (!ptrtype)
3397 : : /* A function with a prototype was redeclared without one and
3398 : : the prototype has been lost. See pr102759. Avoid dealing
3399 : : with this pathological case. */
3400 : 4 : return;
3401 : :
3402 : 89363 : tree argtype = TREE_TYPE (ptrtype);
3403 : :
3404 : : /* The size of the access by the call in elements. */
3405 : 89363 : tree access_nelts;
3406 : 89363 : if (sizidx == -1)
3407 : : {
3408 : : /* If only the pointer attribute operand was specified and
3409 : : not size, set SIZE to the greater of MINSIZE or size of
3410 : : one element of the pointed to type to detect smaller
3411 : : objects (null pointers are diagnosed in this case only
3412 : : if the pointer is also declared with attribute nonnull. */
3413 : 87296 : if (access.second.minsize
3414 : 87296 : && access.second.minsize != HOST_WIDE_INT_M1U)
3415 : 73918 : access_nelts = build_int_cstu (sizetype, access.second.minsize);
3416 : 13378 : else if (VOID_TYPE_P (argtype) && access.second.mode == access_none)
3417 : : /* Treat access mode none on a void* argument as expecting
3418 : : as little as zero bytes. */
3419 : 364 : access_nelts = size_zero_node;
3420 : : else
3421 : 13014 : access_nelts = size_one_node;
3422 : : }
3423 : : else
3424 : 2067 : access_nelts = rwm->get (sizidx)->size;
3425 : :
3426 : : /* If access_nelts is e.g. a PARM_DECL with larger precision than
3427 : : sizetype, such as __int128 or _BitInt(34123) parameters,
3428 : : cast it to sizetype. */
3429 : 89363 : if (access_nelts
3430 : 89363 : && INTEGRAL_TYPE_P (TREE_TYPE (access_nelts))
3431 : 178726 : && (TYPE_PRECISION (TREE_TYPE (access_nelts))
3432 : 89363 : > TYPE_PRECISION (sizetype)))
3433 : 2 : access_nelts = fold_convert (sizetype, access_nelts);
3434 : :
3435 : : /* Format the value or range to avoid an explosion of messages. */
3436 : 89363 : char sizstr[80];
3437 : 89363 : tree sizrng[2] = { size_zero_node, build_all_ones_cst (sizetype) };
3438 : 89363 : if (get_size_range (m_ptr_qry.rvals, access_nelts, stmt, sizrng, 1))
3439 : : {
3440 : 89363 : char *s0 = print_generic_expr_to_str (sizrng[0]);
3441 : 89363 : if (tree_int_cst_equal (sizrng[0], sizrng[1]))
3442 : : {
3443 : 88782 : gcc_checking_assert (strlen (s0) < sizeof sizstr);
3444 : 88782 : strcpy (sizstr, s0);
3445 : : }
3446 : : else
3447 : : {
3448 : 581 : char *s1 = print_generic_expr_to_str (sizrng[1]);
3449 : 581 : gcc_checking_assert (strlen (s0) + strlen (s1)
3450 : : < sizeof sizstr - 4);
3451 : 581 : sprintf (sizstr, "[%.37s, %.37s]", s0, s1);
3452 : 581 : free (s1);
3453 : : }
3454 : 89363 : free (s0);
3455 : : }
3456 : : else
3457 : 0 : *sizstr = '\0';
3458 : :
3459 : : /* Set if a warning has been issued for the current argument. */
3460 : 89363 : opt_code arg_warned = no_warning;
3461 : 89363 : location_t loc = get_location (stmt);
3462 : 89363 : tree ptr = access.second.ptr;
3463 : 89363 : if (*sizstr
3464 : 89363 : && tree_int_cst_sgn (sizrng[0]) < 0
3465 : 89558 : && tree_int_cst_sgn (sizrng[1]) < 0)
3466 : : {
3467 : : /* Warn about negative sizes. */
3468 : 67 : if (access.second.internal_p)
3469 : : {
3470 : 18 : const std::string argtypestr
3471 : 18 : = access.second.array_as_string (ptrtype);
3472 : :
3473 : 18 : if (warning_at (loc, OPT_Wstringop_overflow_,
3474 : : "bound argument %i value %s is "
3475 : : "negative for a variable length array "
3476 : : "argument %i of type %s",
3477 : : sizidx + 1, sizstr,
3478 : : ptridx + 1, argtypestr.c_str ()))
3479 : 18 : arg_warned = OPT_Wstringop_overflow_;
3480 : 18 : }
3481 : 49 : else if (warning_at (loc, OPT_Wstringop_overflow_,
3482 : : "argument %i value %s is negative",
3483 : : sizidx + 1, sizstr))
3484 : : arg_warned = OPT_Wstringop_overflow_;
3485 : :
3486 : 18 : if (arg_warned != no_warning)
3487 : : {
3488 : 23 : append_attrname (access, attrstr, sizeof attrstr);
3489 : : /* Remember a warning has been issued and avoid warning
3490 : : again below for the same attribute. */
3491 : 23 : opt_warned = arg_warned;
3492 : 23 : continue;
3493 : : }
3494 : : }
3495 : :
3496 : : /* The size of the access by the call in bytes. */
3497 : 89340 : tree access_size = NULL_TREE;
3498 : 89340 : if (tree_int_cst_sgn (sizrng[0]) >= 0)
3499 : : {
3500 : 89168 : if (COMPLETE_TYPE_P (argtype))
3501 : : {
3502 : : /* Multiply ACCESS_SIZE by the size of the type the pointer
3503 : : argument points to. If it's incomplete the size is used
3504 : : as is. */
3505 : 85492 : if (tree argsize = TYPE_SIZE_UNIT (argtype))
3506 : 85492 : if (TREE_CODE (argsize) == INTEGER_CST)
3507 : : {
3508 : 85201 : const int prec = TYPE_PRECISION (sizetype);
3509 : 85201 : wide_int minsize = wi::to_wide (sizrng[0], prec);
3510 : 85201 : minsize *= wi::to_wide (argsize, prec);
3511 : 85201 : access_size = wide_int_to_tree (sizetype, minsize);
3512 : 85201 : }
3513 : : }
3514 : : else
3515 : : access_size = access_nelts;
3516 : : }
3517 : :
3518 : 89340 : if (integer_zerop (ptr))
3519 : : {
3520 : 1939 : if (!access.second.internal_p
3521 : 1939 : && sizidx >= 0 && tree_int_cst_sgn (sizrng[0]) > 0)
3522 : : {
3523 : : /* Warn about null pointers with positive sizes. This is
3524 : : different from also declaring the pointer argument with
3525 : : attribute nonnull when the function accepts null pointers
3526 : : only when the corresponding size is zero. */
3527 : 27 : if (warning_at (loc, OPT_Wnonnull,
3528 : : "argument %i is null but "
3529 : : "the corresponding size argument "
3530 : : "%i value is %s",
3531 : : ptridx + 1, sizidx + 1, sizstr))
3532 : 8 : arg_warned = OPT_Wnonnull;
3533 : : }
3534 : :
3535 : 16 : if (arg_warned != no_warning)
3536 : : {
3537 : 8 : append_attrname (access, attrstr, sizeof attrstr);
3538 : : /* Remember a warning has been issued and avoid warning
3539 : : again below for the same attribute. */
3540 : 8 : opt_warned = OPT_Wnonnull;
3541 : 8 : continue;
3542 : : }
3543 : : }
3544 : :
3545 : 89332 : access_data data (m_ptr_qry.rvals, stmt, access.second.mode,
3546 : 89332 : NULL_TREE, false, NULL_TREE, false);
3547 : 178664 : access_ref* const pobj = (access.second.mode == access_write_only
3548 : 89332 : ? &data.dst : &data.src);
3549 : 89332 : tree objsize = compute_objsize (ptr, stmt, 1, pobj, &m_ptr_qry);
3550 : :
3551 : : /* The size of the destination or source object. */
3552 : 89332 : tree dstsize = NULL_TREE, srcsize = NULL_TREE;
3553 : 89332 : if (access.second.mode == access_read_only
3554 : : || access.second.mode == access_none)
3555 : : {
3556 : : /* For a read-only argument there is no destination. For
3557 : : no access, set the source as well and differentiate via
3558 : : the access flag below. */
3559 : 1796 : srcsize = objsize;
3560 : 1796 : if (access.second.mode == access_read_only
3561 : : || access.second.mode == access_none)
3562 : : {
3563 : : /* For a read-only attribute there is no destination so
3564 : : clear OBJSIZE. This emits "reading N bytes" kind of
3565 : : diagnostics instead of the "writing N bytes" kind,
3566 : : unless MODE is none. */
3567 : 1796 : objsize = NULL_TREE;
3568 : : }
3569 : : }
3570 : : else
3571 : : dstsize = objsize;
3572 : :
3573 : : /* Clear the no-warning bit in case it was set by check_access
3574 : : in a prior iteration so that accesses via different arguments
3575 : : are diagnosed. */
3576 : 89332 : suppress_warning (stmt, OPT_Wstringop_overflow_, false);
3577 : 89332 : access_mode mode = data.mode;
3578 : 89332 : if (mode == access_deferred)
3579 : 85835 : mode = TYPE_READONLY (argtype) ? access_read_only : access_read_write;
3580 : 89332 : check_access (stmt, access_size, /*maxread=*/ NULL_TREE, srcsize,
3581 : : dstsize, mode, &data, m_ptr_qry.rvals);
3582 : :
3583 : 89332 : if (warning_suppressed_p (stmt, OPT_Wstringop_overflow_))
3584 : : opt_warned = OPT_Wstringop_overflow_;
3585 : 89125 : if (opt_warned != no_warning)
3586 : : {
3587 : 322 : if (access.second.internal_p)
3588 : : {
3589 : 146 : unsigned HOST_WIDE_INT nelts =
3590 : 73 : access_nelts ? access.second.minsize : HOST_WIDE_INT_M1U;
3591 : 73 : tree arrtype = build_printable_array_type (argtype, nelts);
3592 : 73 : inform (loc, "referencing argument %u of type %qT",
3593 : : ptridx + 1, arrtype);
3594 : : }
3595 : : else
3596 : : /* If check_access issued a warning above, append the relevant
3597 : : attribute to the string. */
3598 : 249 : append_attrname (access, attrstr, sizeof attrstr);
3599 : : }
3600 : : }
3601 : :
3602 : 4027365 : if (*attrstr)
3603 : : {
3604 : 145 : if (fndecl)
3605 : 129 : inform (get_location (fndecl),
3606 : : "in a call to function %qD declared with attribute %qs",
3607 : : fndecl, attrstr);
3608 : : else
3609 : 16 : inform (get_location (stmt),
3610 : : "in a call with type %qT and attribute %qs",
3611 : : fntype, attrstr);
3612 : : }
3613 : 4027220 : else if (opt_warned != no_warning)
3614 : : {
3615 : 91 : if (fndecl)
3616 : 91 : inform (get_location (fndecl),
3617 : : "in a call to function %qD", fndecl);
3618 : : else
3619 : 0 : inform (get_location (stmt),
3620 : : "in a call with type %qT", fntype);
3621 : : }
3622 : :
3623 : : /* Set the bit in case it was cleared and not set above. */
3624 : 236 : if (opt_warned != no_warning)
3625 : 236 : suppress_warning (stmt, opt_warned);
3626 : 4027369 : }
3627 : :
3628 : : /* Check call STMT to an ordinary (non-built-in) function for invalid
3629 : : accesses. Return true if a call has been handled. */
3630 : :
3631 : : bool
3632 : 21654835 : pass_waccess::check_call_access (gcall *stmt)
3633 : : {
3634 : 21654835 : tree fntype = gimple_call_fntype (stmt);
3635 : 21000666 : if (!fntype)
3636 : : return false;
3637 : :
3638 : 21000666 : tree fntypeattrs = TYPE_ATTRIBUTES (fntype);
3639 : 21000666 : if (!fntypeattrs)
3640 : : return false;
3641 : :
3642 : : /* Map of attribute access specifications for function arguments. */
3643 : 4049100 : rdwr_map rdwr_idx;
3644 : 4049100 : init_attr_rdwr_indices (&rdwr_idx, fntypeattrs);
3645 : :
3646 : 4049100 : unsigned nargs = call_nargs (stmt);
3647 : 12322673 : for (unsigned i = 0; i != nargs; ++i)
3648 : : {
3649 : 8273573 : tree arg = call_arg (stmt, i);
3650 : :
3651 : : /* Save the actual argument that corresponds to the access attribute
3652 : : operand for later processing. */
3653 : 8273573 : if (attr_access *access = rdwr_idx.get (i))
3654 : : {
3655 : 92262 : if (POINTER_TYPE_P (TREE_TYPE (arg)))
3656 : : {
3657 : 90098 : access->ptr = arg;
3658 : : /* A nonnull ACCESS->SIZE contains VLA bounds. */
3659 : : }
3660 : : else
3661 : : {
3662 : 2164 : access->size = arg;
3663 : 2164 : gcc_assert (access->ptr == NULL_TREE);
3664 : : }
3665 : : }
3666 : : }
3667 : :
3668 : : /* Check attribute access arguments. */
3669 : 4049100 : tree fndecl = gimple_call_fndecl (stmt);
3670 : 4049100 : maybe_check_access_sizes (&rdwr_idx, fndecl, fntype, stmt);
3671 : :
3672 : 4049100 : check_alloc_size_call (stmt);
3673 : 4049100 : return true;
3674 : 4049100 : }
3675 : :
3676 : : /* Check arguments in a call STMT for attribute nonstring. */
3677 : :
3678 : : static void
3679 : 6574496 : check_nonstring_args (gcall *stmt)
3680 : : {
3681 : 6574496 : tree fndecl = gimple_call_fndecl (stmt);
3682 : :
3683 : : /* Detect passing non-string arguments to functions expecting
3684 : : nul-terminated strings. */
3685 : 6574496 : maybe_warn_nonstring_arg (fndecl, stmt);
3686 : 6574496 : }
3687 : :
3688 : : /* Issue a warning if a deallocation function such as free, realloc,
3689 : : or C++ operator delete is called with an argument not returned by
3690 : : a matching allocation function such as malloc or the corresponding
3691 : : form of C++ operator new. */
3692 : :
3693 : : void
3694 : 6574496 : pass_waccess::maybe_check_dealloc_call (gcall *call)
3695 : : {
3696 : 6574496 : tree fndecl = gimple_call_fndecl (call);
3697 : 6574496 : if (!fndecl)
3698 : 6396736 : return;
3699 : :
3700 : 6208893 : unsigned argno = fndecl_dealloc_argno (fndecl);
3701 : 6208893 : if ((unsigned) call_nargs (call) <= argno)
3702 : : return;
3703 : :
3704 : 178429 : tree ptr = gimple_call_arg (call, argno);
3705 : 178429 : if (integer_zerop (ptr))
3706 : : return;
3707 : :
3708 : 178337 : access_ref aref;
3709 : 178337 : if (!compute_objsize (ptr, call, 0, &aref, &m_ptr_qry))
3710 : : return;
3711 : :
3712 : 178337 : tree ref = aref.ref;
3713 : 178337 : if (integer_zerop (ref))
3714 : : return;
3715 : :
3716 : 178234 : tree dealloc_decl = fndecl;
3717 : 178234 : location_t loc = gimple_location (call);
3718 : :
3719 : 178234 : if (DECL_P (ref) || EXPR_P (ref))
3720 : : {
3721 : : /* Diagnose freeing a declared object. */
3722 : 99082 : if (aref.ref_declared ())
3723 : : {
3724 : 152 : auto_diagnostic_group d;
3725 : 152 : if (warning_at (loc, OPT_Wfree_nonheap_object,
3726 : : "%qD called on unallocated object %qD",
3727 : : dealloc_decl, ref))
3728 : : {
3729 : 140 : inform (get_location (ref), "declared here");
3730 : 140 : return;
3731 : : }
3732 : 152 : }
3733 : :
3734 : : /* Diagnose freeing a pointer that includes a positive offset.
3735 : : Such a pointer cannot refer to the beginning of an allocated
3736 : : object. A negative offset may refer to it. */
3737 : 98942 : if (aref.sizrng[0] != aref.sizrng[1]
3738 : 98942 : && warn_dealloc_offset (loc, call, aref))
3739 : : return;
3740 : : }
3741 : 79152 : else if (CONSTANT_CLASS_P (ref))
3742 : : {
3743 : 38 : auto_diagnostic_group d;
3744 : 38 : if (warning_at (loc, OPT_Wfree_nonheap_object,
3745 : : "%qD called on a pointer to an unallocated "
3746 : : "object %qE", dealloc_decl, ref))
3747 : : {
3748 : 38 : if (TREE_CODE (ptr) == SSA_NAME)
3749 : : {
3750 : 12 : gimple *def_stmt = SSA_NAME_DEF_STMT (ptr);
3751 : 12 : if (is_gimple_assign (def_stmt))
3752 : : {
3753 : 12 : location_t loc = gimple_location (def_stmt);
3754 : 12 : inform (loc, "assigned here");
3755 : : }
3756 : : }
3757 : 38 : return;
3758 : : }
3759 : 38 : }
3760 : 79114 : else if (TREE_CODE (ref) == SSA_NAME)
3761 : : {
3762 : : /* Also warn if the pointer argument refers to the result
3763 : : of an allocation call like alloca or VLA. */
3764 : 79114 : gimple *def_stmt = SSA_NAME_DEF_STMT (ref);
3765 : 79114 : if (!def_stmt)
3766 : : return;
3767 : :
3768 : 79114 : if (is_gimple_call (def_stmt))
3769 : : {
3770 : 40805 : bool warned = false;
3771 : 40805 : if (gimple_call_alloc_p (def_stmt))
3772 : : {
3773 : 37391 : if (matching_alloc_calls_p (def_stmt, dealloc_decl))
3774 : : {
3775 : 37031 : if (warn_dealloc_offset (loc, call, aref))
3776 : : return;
3777 : : }
3778 : : else
3779 : : {
3780 : 360 : tree alloc_decl = gimple_call_fndecl (def_stmt);
3781 : 360 : const opt_code opt =
3782 : 360 : (DECL_IS_OPERATOR_NEW_P (alloc_decl)
3783 : 170 : || DECL_IS_OPERATOR_DELETE_P (dealloc_decl)
3784 : 360 : ? OPT_Wmismatched_new_delete
3785 : 360 : : OPT_Wmismatched_dealloc);
3786 : 360 : warned = warning_at (loc, opt,
3787 : : "%qD called on pointer returned "
3788 : : "from a mismatched allocation "
3789 : : "function", dealloc_decl);
3790 : : }
3791 : : }
3792 : 3414 : else if (gimple_call_builtin_p (def_stmt, BUILT_IN_ALLOCA)
3793 : 3414 : || gimple_call_builtin_p (def_stmt,
3794 : : BUILT_IN_ALLOCA_WITH_ALIGN))
3795 : 20 : warned = warning_at (loc, OPT_Wfree_nonheap_object,
3796 : : "%qD called on pointer to "
3797 : : "an unallocated object",
3798 : : dealloc_decl);
3799 : 3394 : else if (warn_dealloc_offset (loc, call, aref))
3800 : : return;
3801 : :
3802 : 380 : if (warned)
3803 : : {
3804 : 227 : tree fndecl = gimple_call_fndecl (def_stmt);
3805 : 227 : inform (gimple_location (def_stmt),
3806 : : "returned from %qD", fndecl);
3807 : 227 : return;
3808 : : }
3809 : : }
3810 : 38309 : else if (gimple_nop_p (def_stmt))
3811 : : {
3812 : 13207 : ref = SSA_NAME_VAR (ref);
3813 : : /* Diagnose freeing a pointer that includes a positive offset. */
3814 : 13207 : if (TREE_CODE (ref) == PARM_DECL
3815 : 13193 : && !aref.deref
3816 : 13193 : && aref.sizrng[0] != aref.sizrng[1]
3817 : 13207 : && aref.offrng[0] > 0 && aref.offrng[1] > 0
3818 : 13213 : && warn_dealloc_offset (loc, call, aref))
3819 : 6 : return;
3820 : : }
3821 : : }
3822 : : }
3823 : :
3824 : : /* Return true if either USE_STMT's basic block (that of a pointer's use)
3825 : : is dominated by INVAL_STMT's (that of a pointer's invalidating statement,
3826 : : which is either a clobber or a deallocation call), or if they're in
3827 : : the same block, USE_STMT follows INVAL_STMT. */
3828 : :
3829 : : bool
3830 : 5023020 : pass_waccess::use_after_inval_p (gimple *inval_stmt, gimple *use_stmt,
3831 : : bool last_block /* = false */)
3832 : : {
3833 : 5023020 : tree clobvar =
3834 : 5023020 : gimple_clobber_p (inval_stmt) ? gimple_assign_lhs (inval_stmt) : NULL_TREE;
3835 : :
3836 : 5023020 : basic_block inval_bb = gimple_bb (inval_stmt);
3837 : 5023020 : basic_block use_bb = gimple_bb (use_stmt);
3838 : :
3839 : 5023020 : if (!inval_bb || !use_bb)
3840 : : return false;
3841 : :
3842 : 5022599 : if (inval_bb != use_bb)
3843 : : {
3844 : 4390979 : if (dominated_by_p (CDI_DOMINATORS, use_bb, inval_bb))
3845 : : return true;
3846 : :
3847 : 4389695 : if (!clobvar || !last_block)
3848 : : return false;
3849 : :
3850 : : /* Proceed only when looking for uses of dangling pointers. */
3851 : 3124066 : auto gsi = gsi_for_stmt (use_stmt);
3852 : :
3853 : : /* A use statement in the last basic block in a function or one that
3854 : : falls through to it is after any other prior clobber of the used
3855 : : variable unless it's followed by a clobber of the same variable. */
3856 : 3124066 : basic_block bb = use_bb;
3857 : 3124066 : while (bb != inval_bb
3858 : 3783181 : && single_succ_p (bb)
3859 : 5282070 : && !(single_succ_edge (bb)->flags
3860 : 1216524 : & (EDGE_EH | EDGE_ABNORMAL | EDGE_DFS_BACK)))
3861 : : {
3862 : 10411110 : for (; !gsi_end_p (gsi); gsi_next_nondebug (&gsi))
3863 : : {
3864 : 9469630 : gimple *stmt = gsi_stmt (gsi);
3865 : 9469630 : if (gimple_clobber_p (stmt))
3866 : : {
3867 : 399431 : if (clobvar == gimple_assign_lhs (stmt))
3868 : : /* The use is followed by a clobber. */
3869 : : return false;
3870 : : }
3871 : : }
3872 : :
3873 : 941480 : bb = single_succ (bb);
3874 : 1882960 : gsi = gsi_start_bb (bb);
3875 : : }
3876 : :
3877 : : /* The use is one of a dangling pointer if a clobber of the variable
3878 : : [the pointer points to] has not been found before the function exit
3879 : : point. */
3880 : 3108560 : return bb == EXIT_BLOCK_PTR_FOR_FN (cfun);
3881 : : }
3882 : :
3883 : 631620 : if (bitmap_set_bit (m_bb_uids_set, inval_bb->index))
3884 : : /* The first time this basic block is visited assign increasing ids
3885 : : to consecutive statements in it. Use the ids to determine which
3886 : : precedes which. This avoids the linear traversal on subsequent
3887 : : visits to the same block. */
3888 : 317015 : renumber_gimple_stmt_uids_in_block (m_func, inval_bb);
3889 : :
3890 : 631620 : return gimple_uid (inval_stmt) < gimple_uid (use_stmt);
3891 : : }
3892 : :
3893 : : /* Issue a warning for the USE_STMT of pointer or reference REF rendered
3894 : : invalid by INVAL_STMT. REF may be null when it's been optimized away.
3895 : : When nonnull, INVAL_STMT is the deallocation function that rendered
3896 : : the pointer or reference dangling. Otherwise, VAR is the auto variable
3897 : : (including an unnamed temporary such as a compound literal) whose
3898 : : lifetime's rended it dangling. MAYBE is true to issue the "maybe"
3899 : : kind of warning. EQUALITY is true when the pointer is used in
3900 : : an equality expression. */
3901 : :
3902 : : void
3903 : 18180 : pass_waccess::warn_invalid_pointer (tree ref, gimple *use_stmt,
3904 : : gimple *inval_stmt, tree var,
3905 : : bool maybe, bool equality /* = false */)
3906 : : {
3907 : : /* Avoid printing the unhelpful "<unknown>" in the diagnostics. */
3908 : 18180 : if (ref && TREE_CODE (ref) == SSA_NAME)
3909 : : {
3910 : 18153 : tree var = SSA_NAME_VAR (ref);
3911 : 17913 : if (!var)
3912 : : ref = NULL_TREE;
3913 : : /* Don't warn for cases like when a cdtor returns 'this' on ARM. */
3914 : 17913 : else if (warning_suppressed_p (var, OPT_Wuse_after_free))
3915 : : return;
3916 : 17910 : else if (DECL_ARTIFICIAL (var))
3917 : 276 : ref = NULL_TREE;
3918 : : }
3919 : :
3920 : 18177 : location_t use_loc = gimple_location (use_stmt);
3921 : 18177 : if (use_loc == UNKNOWN_LOCATION)
3922 : : {
3923 : 0 : use_loc = m_func->function_end_locus;
3924 : 0 : if (!ref)
3925 : : /* Avoid issuing a warning with no context other than
3926 : : the function. That would make it difficult to debug
3927 : : in any but very simple cases. */
3928 : : return;
3929 : : }
3930 : :
3931 : 18177 : if (is_gimple_call (inval_stmt))
3932 : : {
3933 : 17982 : if (!m_early_checks_p
3934 : 6461 : || (equality && warn_use_after_free < 3)
3935 : 5883 : || (maybe && warn_use_after_free < 2)
3936 : 23685 : || warning_suppressed_p (use_stmt, OPT_Wuse_after_free))
3937 : 12336 : return;
3938 : :
3939 : 5646 : const tree inval_decl = gimple_call_fndecl (inval_stmt);
3940 : :
3941 : 5646 : auto_diagnostic_group d;
3942 : 11150 : if ((ref && warning_at (use_loc, OPT_Wuse_after_free,
3943 : : (maybe
3944 : : ? G_("pointer %qE may be used after %qD")
3945 : : : G_("pointer %qE used after %qD")),
3946 : : ref, inval_decl))
3947 : 10936 : || (!ref && warning_at (use_loc, OPT_Wuse_after_free,
3948 : : (maybe
3949 : : ? G_("pointer may be used after %qD")
3950 : : : G_("pointer used after %qD")),
3951 : : inval_decl)))
3952 : : {
3953 : 359 : location_t loc = gimple_location (inval_stmt);
3954 : 359 : inform (loc, "call to %qD here", inval_decl);
3955 : 359 : suppress_warning (use_stmt, OPT_Wuse_after_free);
3956 : : }
3957 : 5646 : return;
3958 : 5646 : }
3959 : :
3960 : 195 : if (equality
3961 : 189 : || (maybe && warn_dangling_pointer < 2)
3962 : 379 : || warning_suppressed_p (use_stmt, OPT_Wdangling_pointer_))
3963 : 70 : return;
3964 : :
3965 : 125 : if (DECL_NAME (var))
3966 : : {
3967 : 111 : auto_diagnostic_group d;
3968 : 111 : if ((ref
3969 : 174 : && warning_at (use_loc, OPT_Wdangling_pointer_,
3970 : : (maybe
3971 : : ? G_("dangling pointer %qE to %qD may be used")
3972 : : : G_("using dangling pointer %qE to %qD")),
3973 : : ref, var))
3974 : 111 : || (!ref
3975 : 22 : && warning_at (use_loc, OPT_Wdangling_pointer_,
3976 : : (maybe
3977 : : ? G_("dangling pointer to %qD may be used")
3978 : : : G_("using a dangling pointer to %qD")),
3979 : : var)))
3980 : 98 : inform (DECL_SOURCE_LOCATION (var),
3981 : : "%qD declared here", var);
3982 : 111 : suppress_warning (use_stmt, OPT_Wdangling_pointer_);
3983 : 111 : return;
3984 : 111 : }
3985 : :
3986 : 14 : if ((ref
3987 : 14 : && warning_at (use_loc, OPT_Wdangling_pointer_,
3988 : : (maybe
3989 : : ? G_("dangling pointer %qE to an unnamed temporary "
3990 : : "may be used")
3991 : : : G_("using dangling pointer %qE to an unnamed "
3992 : : "temporary")),
3993 : : ref))
3994 : 14 : || (!ref
3995 : 12 : && warning_at (use_loc, OPT_Wdangling_pointer_,
3996 : : (maybe
3997 : : ? G_("dangling pointer to an unnamed temporary "
3998 : : "may be used")
3999 : : : G_("using a dangling pointer to an unnamed "
4000 : : "temporary")))))
4001 : : {
4002 : 14 : inform (DECL_SOURCE_LOCATION (var),
4003 : : "unnamed temporary defined here");
4004 : 14 : suppress_warning (use_stmt, OPT_Wdangling_pointer_);
4005 : : }
4006 : : }
4007 : :
4008 : : /* If STMT is a call to either the standard realloc or to a user-defined
4009 : : reallocation function returns its LHS and set *PTR to the reallocated
4010 : : pointer. Otherwise return null. */
4011 : :
4012 : : static tree
4013 : 663836 : get_realloc_lhs (gimple *stmt, tree *ptr)
4014 : : {
4015 : 663836 : if (gimple_call_builtin_p (stmt, BUILT_IN_REALLOC))
4016 : : {
4017 : 22724 : *ptr = gimple_call_arg (stmt, 0);
4018 : 22724 : return gimple_call_lhs (stmt);
4019 : : }
4020 : :
4021 : 1123723 : gcall *call = dyn_cast<gcall *>(stmt);
4022 : 482751 : if (!call)
4023 : : return NULL_TREE;
4024 : :
4025 : 482751 : tree fnattr = NULL_TREE;
4026 : 482751 : tree fndecl = gimple_call_fndecl (call);
4027 : 482751 : if (fndecl)
4028 : 482751 : fnattr = DECL_ATTRIBUTES (fndecl);
4029 : : else
4030 : : {
4031 : 0 : tree fntype = gimple_call_fntype (stmt);
4032 : 0 : if (!fntype)
4033 : : return NULL_TREE;
4034 : 0 : fnattr = TYPE_ATTRIBUTES (fntype);
4035 : : }
4036 : :
4037 : 482751 : if (!fnattr)
4038 : : return NULL_TREE;
4039 : :
4040 : 476544 : for (tree ats = fnattr; (ats = lookup_attribute ("*dealloc", ats));
4041 : 14413 : ats = TREE_CHAIN (ats))
4042 : : {
4043 : 14553 : tree args = TREE_VALUE (ats);
4044 : 14553 : if (!args)
4045 : 0 : continue;
4046 : :
4047 : 14553 : tree alloc = TREE_VALUE (args);
4048 : 14553 : if (!alloc)
4049 : 0 : continue;
4050 : :
4051 : 14553 : if (alloc == DECL_NAME (fndecl))
4052 : : {
4053 : 140 : unsigned argno = 0;
4054 : 140 : if (tree index = TREE_CHAIN (args))
4055 : 82 : argno = TREE_INT_CST_LOW (TREE_VALUE (index)) - 1;
4056 : 140 : *ptr = gimple_call_arg (stmt, argno);
4057 : 140 : return gimple_call_lhs (stmt);
4058 : : }
4059 : : }
4060 : :
4061 : : return NULL_TREE;
4062 : : }
4063 : :
4064 : : /* Warn if STMT is a call to a deallocation function that's not a match
4065 : : for the REALLOC_STMT call. Return true if warned. */
4066 : :
4067 : : static bool
4068 : 94336 : maybe_warn_mismatched_realloc (tree ptr, gimple *realloc_stmt, gimple *stmt)
4069 : : {
4070 : 94336 : if (!is_gimple_call (stmt))
4071 : : return false;
4072 : :
4073 : 40227 : tree fndecl = gimple_call_fndecl (stmt);
4074 : 40227 : if (!fndecl)
4075 : : return false;
4076 : :
4077 : 40222 : unsigned argno = fndecl_dealloc_argno (fndecl);
4078 : 40222 : if (call_nargs (stmt) <= argno)
4079 : : return false;
4080 : :
4081 : 2510 : if (matching_alloc_calls_p (realloc_stmt, fndecl))
4082 : : return false;
4083 : :
4084 : : /* Avoid printing the unhelpful "<unknown>" in the diagnostics. */
4085 : 43 : if (ptr && TREE_CODE (ptr) == SSA_NAME
4086 : 86 : && (!SSA_NAME_VAR (ptr) || DECL_ARTIFICIAL (SSA_NAME_VAR (ptr))))
4087 : : ptr = NULL_TREE;
4088 : :
4089 : 43 : location_t loc = gimple_location (stmt);
4090 : 43 : tree realloc_decl = gimple_call_fndecl (realloc_stmt);
4091 : 43 : tree dealloc_decl = gimple_call_fndecl (stmt);
4092 : 43 : if (ptr && !warning_at (loc, OPT_Wmismatched_dealloc,
4093 : : "%qD called on pointer %qE passed to mismatched "
4094 : : "allocation function %qD",
4095 : : dealloc_decl, ptr, realloc_decl))
4096 : 4 : return false;
4097 : 39 : if (!ptr && !warning_at (loc, OPT_Wmismatched_dealloc,
4098 : : "%qD called on a pointer passed to mismatched "
4099 : : "reallocation function %qD",
4100 : : dealloc_decl, realloc_decl))
4101 : 0 : return false;
4102 : :
4103 : 39 : inform (gimple_location (realloc_stmt),
4104 : : "call to %qD", realloc_decl);
4105 : 39 : return true;
4106 : : }
4107 : :
4108 : : /* Return true if P and Q point to the same object, and false if they
4109 : : either don't or their relationship cannot be determined. */
4110 : :
4111 : : static bool
4112 : 94273 : pointers_related_p (gimple *stmt, tree p, tree q, pointer_query &qry,
4113 : : auto_bitmap &visited)
4114 : : {
4115 : 94273 : if (!ptr_derefs_may_alias_p (p, q))
4116 : : return false;
4117 : :
4118 : : /* TODO: Work harder to rule out relatedness. */
4119 : 94273 : access_ref pref, qref;
4120 : 94273 : if (!qry.get_ref (p, stmt, &pref, 0)
4121 : 94273 : || !qry.get_ref (q, stmt, &qref, 0))
4122 : : /* GET_REF() only rarely fails. When it does, it's likely because
4123 : : it involves a self-referential PHI. Return a conservative result. */
4124 : 0 : return false;
4125 : :
4126 : 94273 : if (pref.ref == qref.ref)
4127 : : return true;
4128 : :
4129 : : /* If either pointer is a PHI, iterate over all its operands and
4130 : : return true if they're all related to the other pointer. */
4131 : 96 : tree ptr = q;
4132 : 96 : unsigned version;
4133 : 96 : gphi *phi = pref.phi ();
4134 : 96 : if (phi)
4135 : 96 : version = SSA_NAME_VERSION (pref.ref);
4136 : : else
4137 : : {
4138 : 0 : phi = qref.phi ();
4139 : 0 : if (!phi)
4140 : : return false;
4141 : :
4142 : 0 : ptr = p;
4143 : 0 : version = SSA_NAME_VERSION (qref.ref);
4144 : : }
4145 : :
4146 : 96 : if (!bitmap_set_bit (visited, version))
4147 : : return true;
4148 : :
4149 : 48 : unsigned nargs = gimple_phi_num_args (phi);
4150 : 144 : for (unsigned i = 0; i != nargs; ++i)
4151 : : {
4152 : 96 : tree arg = gimple_phi_arg_def (phi, i);
4153 : 96 : if (!pointers_related_p (stmt, arg, ptr, qry, visited))
4154 : : return false;
4155 : : }
4156 : :
4157 : : return true;
4158 : : }
4159 : :
4160 : : /* Convenience wrapper for the above. */
4161 : :
4162 : : static bool
4163 : 94177 : pointers_related_p (gimple *stmt, tree p, tree q, pointer_query &qry)
4164 : : {
4165 : 94177 : auto_bitmap visited;
4166 : 94177 : return pointers_related_p (stmt, p, q, qry, visited);
4167 : 94177 : }
4168 : :
4169 : : /* For a STMT either a call to a deallocation function or a clobber, warn
4170 : : for uses of the pointer PTR it was called with (including its copies
4171 : : or others derived from it by pointer arithmetic). If STMT is a clobber,
4172 : : VAR is the decl of the clobbered variable. When MAYBE is true use
4173 : : a "maybe" form of diagnostic. */
4174 : :
4175 : : void
4176 : 663836 : pass_waccess::check_pointer_uses (gimple *stmt, tree ptr,
4177 : : tree var /* = NULL_TREE */,
4178 : : bool maybe /* = false */)
4179 : : {
4180 : 663836 : gcc_assert (TREE_CODE (ptr) == SSA_NAME);
4181 : :
4182 : 663836 : const bool check_dangling = !is_gimple_call (stmt);
4183 : 663836 : basic_block stmt_bb = gimple_bb (stmt);
4184 : :
4185 : : /* If STMT is a reallocation function set to the reallocated pointer
4186 : : and the LHS of the call, respectively. */
4187 : 663836 : tree realloc_ptr = NULL_TREE;
4188 : 663836 : tree realloc_lhs = get_realloc_lhs (stmt, &realloc_ptr);
4189 : :
4190 : 663836 : auto_bitmap visited;
4191 : :
4192 : 663836 : auto_vec<tree, 8> pointers;
4193 : 663836 : pointers.quick_push (ptr);
4194 : 663836 : hash_map<tree, int> *phi_map = nullptr;
4195 : :
4196 : : /* Starting with PTR, iterate over POINTERS added by the loop, and
4197 : : either warn for their uses in basic blocks dominated by the STMT
4198 : : or in statements that follow it in the same basic block, or add
4199 : : them to POINTERS if they point into the same object as PTR (i.e.,
4200 : : are obtained by pointer arithmetic on PTR). */
4201 : 3008246 : for (unsigned i = 0; i != pointers.length (); ++i)
4202 : : {
4203 : 840287 : tree ptr = pointers[i];
4204 : 840287 : if (!bitmap_set_bit (visited, SSA_NAME_VERSION (ptr)))
4205 : : /* Avoid revisiting the same pointer. */
4206 : 43 : continue;
4207 : :
4208 : 840244 : use_operand_p use_p;
4209 : 840244 : imm_use_iterator iter;
4210 : 4104051 : FOR_EACH_IMM_USE_FAST (use_p, iter, ptr)
4211 : : {
4212 : 3263807 : gimple *use_stmt = USE_STMT (use_p);
4213 : 3263807 : if (use_stmt == stmt || is_gimple_debug (use_stmt))
4214 : 1483404 : continue;
4215 : :
4216 : : /* A clobber isn't a use. */
4217 : 1780403 : if (gimple_clobber_p (use_stmt))
4218 : 79151 : continue;
4219 : :
4220 : 1701252 : if (realloc_lhs)
4221 : : {
4222 : : /* Check to see if USE_STMT is a mismatched deallocation
4223 : : call for the pointer passed to realloc. That's a bug
4224 : : regardless of the pointer's value and so warn. */
4225 : 94336 : if (maybe_warn_mismatched_realloc (*use_p->use, stmt, use_stmt))
4226 : 159 : continue;
4227 : :
4228 : : /* Pointers passed to realloc that are used in basic blocks
4229 : : where the realloc call is known to have failed are valid.
4230 : : Ignore pointers that nothing is known about. Those could
4231 : : have escaped along with their nullness. */
4232 : 94297 : prange vr;
4233 : 94297 : if (m_ptr_qry.rvals->range_of_expr (vr, realloc_lhs, use_stmt))
4234 : : {
4235 : 94297 : if (vr.zero_p ())
4236 : 120 : continue;
4237 : :
4238 : 94177 : if (!pointers_related_p (stmt, ptr, realloc_ptr, m_ptr_qry))
4239 : 0 : continue;
4240 : : }
4241 : 94297 : }
4242 : :
4243 : 1701334 : if (check_dangling
4244 : 1701093 : && gimple_code (use_stmt) == GIMPLE_RETURN)
4245 : : /* Avoid interfering with -Wreturn-local-addr (which runs only
4246 : : with optimization enabled so it won't diagnose cases that
4247 : : would be caught here when optimization is disabled). */
4248 : 241 : continue;
4249 : :
4250 : 1700852 : bool equality = false;
4251 : 1700852 : if (is_gimple_assign (use_stmt))
4252 : : {
4253 : 972383 : tree_code code = gimple_assign_rhs_code (use_stmt);
4254 : 972383 : equality = code == EQ_EXPR || code == NE_EXPR;
4255 : : }
4256 : 728469 : else if (gcond *cond = dyn_cast<gcond *>(use_stmt))
4257 : : {
4258 : 287654 : tree_code code = gimple_cond_code (cond);
4259 : 287654 : equality = code == EQ_EXPR || code == NE_EXPR;
4260 : : }
4261 : 440815 : else if (gphi *phi = dyn_cast <gphi *> (use_stmt))
4262 : : {
4263 : : /* Only add a PHI result to POINTERS if all its
4264 : : operands are related to PTR, otherwise continue. The
4265 : : PHI result is related once we've reached all arguments
4266 : : through this iteration. That also means any invariant
4267 : : argument will make the PHI not related. For arguments
4268 : : flowing over natural loop backedges we are optimistic
4269 : : (and diagnose the first iteration). */
4270 : 93866 : tree lhs = gimple_phi_result (phi);
4271 : 93866 : if (!phi_map)
4272 : 41701 : phi_map = new hash_map<tree, int>;
4273 : 93866 : bool existed_p;
4274 : 93866 : int &related = phi_map->get_or_insert (lhs, &existed_p);
4275 : 93866 : if (!existed_p)
4276 : : {
4277 : 56912 : related = gimple_phi_num_args (phi) - 1;
4278 : 217726 : for (unsigned j = 0; j < gimple_phi_num_args (phi); ++j)
4279 : : {
4280 : 160814 : if ((unsigned) phi_arg_index_from_use (use_p) == j)
4281 : 56912 : continue;
4282 : 103902 : tree arg = gimple_phi_arg_def (phi, j);
4283 : 103902 : edge e = gimple_phi_arg_edge (phi, j);
4284 : 103902 : basic_block arg_bb;
4285 : 103902 : if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest)
4286 : : /* Make sure we are not forward visiting a
4287 : : backedge argument. */
4288 : 103902 : && (TREE_CODE (arg) != SSA_NAME
4289 : 16763 : || (!SSA_NAME_IS_DEFAULT_DEF (arg)
4290 : 16763 : && ((arg_bb
4291 : 16763 : = gimple_bb (SSA_NAME_DEF_STMT (arg)))
4292 : 16763 : != e->dest)
4293 : 13582 : && !dominated_by_p (CDI_DOMINATORS,
4294 : : e->dest, arg_bb))))
4295 : 13582 : related--;
4296 : : }
4297 : : }
4298 : : else
4299 : 36954 : related--;
4300 : :
4301 : 93866 : if (related == 0)
4302 : 22002 : pointers.safe_push (lhs);
4303 : 93866 : continue;
4304 : 93866 : }
4305 : :
4306 : : /* Warn if USE_STMT is dominated by the deallocation STMT.
4307 : : Otherwise, add the pointer to POINTERS so that the uses
4308 : : of any other pointers derived from it can be checked. */
4309 : 1606986 : if (use_after_inval_p (stmt, use_stmt, check_dangling))
4310 : : {
4311 : 18153 : basic_block use_bb = gimple_bb (use_stmt);
4312 : 18153 : bool this_maybe
4313 : : = (maybe
4314 : 18153 : || !dominated_by_p (CDI_POST_DOMINATORS, stmt_bb, use_bb));
4315 : 18153 : warn_invalid_pointer (*use_p->use, use_stmt, stmt, var,
4316 : : this_maybe, equality);
4317 : 18153 : continue;
4318 : 18153 : }
4319 : :
4320 : 1588833 : if (is_gimple_assign (use_stmt))
4321 : : {
4322 : 970930 : tree lhs = gimple_assign_lhs (use_stmt);
4323 : 970930 : if (TREE_CODE (lhs) == SSA_NAME)
4324 : : {
4325 : 638686 : tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
4326 : 638686 : if (rhs_code == POINTER_PLUS_EXPR || rhs_code == SSA_NAME)
4327 : 153051 : pointers.safe_push (lhs);
4328 : : }
4329 : 970930 : continue;
4330 : 970930 : }
4331 : :
4332 : 3881710 : if (gcall *call = dyn_cast <gcall *>(use_stmt))
4333 : : {
4334 : 329612 : if (gimple_call_return_arg (call) == ptr)
4335 : 36626 : if (tree lhs = gimple_call_lhs (call))
4336 : 1398 : if (TREE_CODE (lhs) == SSA_NAME)
4337 : 1398 : pointers.safe_push (lhs);
4338 : 329612 : continue;
4339 : 329612 : }
4340 : : }
4341 : : }
4342 : :
4343 : 663836 : if (phi_map)
4344 : 41701 : delete phi_map;
4345 : 663836 : }
4346 : :
4347 : : /* Check call STMT for invalid accesses. */
4348 : :
4349 : : void
4350 : 21668350 : pass_waccess::check_call (gcall *stmt)
4351 : : {
4352 : : /* Skip special calls generated by the compiler. */
4353 : 21668350 : if (gimple_call_from_thunk_p (stmt))
4354 : : return;
4355 : :
4356 : : /* .ASAN_MARK doesn't access any vars, only modifies shadow memory. */
4357 : 21664660 : if (gimple_call_internal_p (stmt)
4358 : 21664660 : && gimple_call_internal_fn (stmt) == IFN_ASAN_MARK)
4359 : : return;
4360 : :
4361 : 21654835 : if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
4362 : 4920337 : check_builtin (stmt);
4363 : :
4364 : 21654835 : if (tree callee = gimple_call_fndecl (stmt))
4365 : : {
4366 : : /* Check for uses of the pointer passed to either a standard
4367 : : or a user-defined deallocation function. */
4368 : 20504878 : unsigned argno = fndecl_dealloc_argno (callee);
4369 : 20504878 : if (argno < (unsigned) call_nargs (stmt))
4370 : : {
4371 : 445446 : tree arg = call_arg (stmt, argno);
4372 : 445446 : if (TREE_CODE (arg) == SSA_NAME)
4373 : 444962 : check_pointer_uses (stmt, arg);
4374 : : }
4375 : : }
4376 : :
4377 : 21654835 : check_call_access (stmt);
4378 : 21654835 : check_call_dangling (stmt);
4379 : :
4380 : 21654835 : if (m_early_checks_p)
4381 : : return;
4382 : :
4383 : 6574496 : maybe_check_dealloc_call (stmt);
4384 : 6574496 : check_nonstring_args (stmt);
4385 : : }
4386 : :
4387 : : /* Check non-call STMT for invalid accesses. */
4388 : :
4389 : : void
4390 : 126027643 : pass_waccess::check_stmt (gimple *stmt)
4391 : : {
4392 : 126027643 : if (m_check_dangling_p
4393 : 126027643 : && gimple_clobber_p (stmt, CLOBBER_STORAGE_END))
4394 : : {
4395 : : /* Ignore clobber statements in blocks with exceptional edges. */
4396 : 3070932 : basic_block bb = gimple_bb (stmt);
4397 : 3070932 : edge e = EDGE_PRED (bb, 0);
4398 : 3070932 : if (e->flags & EDGE_EH)
4399 : : return;
4400 : :
4401 : 2414722 : tree var = gimple_assign_lhs (stmt);
4402 : 2414722 : m_clobbers.put (var, stmt);
4403 : 2414722 : return;
4404 : : }
4405 : :
4406 : 122956711 : if (is_gimple_assign (stmt))
4407 : : {
4408 : : /* Clobbered unnamed temporaries such as compound literals can be
4409 : : revived. Check for an assignment to one and remove it from
4410 : : M_CLOBBERS. */
4411 : 92059782 : tree lhs = gimple_assign_lhs (stmt);
4412 : 115286567 : while (handled_component_p (lhs))
4413 : 23226785 : lhs = TREE_OPERAND (lhs, 0);
4414 : :
4415 : 92059782 : if (auto_var_p (lhs))
4416 : 15079304 : m_clobbers.remove (lhs);
4417 : 92059782 : return;
4418 : : }
4419 : :
4420 : 30896929 : if (greturn *ret = dyn_cast <greturn *> (stmt))
4421 : : {
4422 : 5043048 : if (optimize && flag_isolate_erroneous_paths_dereference)
4423 : : /* Avoid interfering with -Wreturn-local-addr (which runs only
4424 : : with optimization enabled). */
4425 : 5043047 : return;
4426 : :
4427 : 1094889 : tree arg = gimple_return_retval (ret);
4428 : 1094889 : if (!arg || TREE_CODE (arg) != ADDR_EXPR)
4429 : : return;
4430 : :
4431 : 311 : arg = TREE_OPERAND (arg, 0);
4432 : 419 : while (handled_component_p (arg))
4433 : 108 : arg = TREE_OPERAND (arg, 0);
4434 : :
4435 : 311 : if (!auto_var_p (arg))
4436 : : return;
4437 : :
4438 : 3 : gimple **pclobber = m_clobbers.get (arg);
4439 : 3 : if (!pclobber)
4440 : : return;
4441 : :
4442 : 1 : if (!use_after_inval_p (*pclobber, stmt))
4443 : : return;
4444 : :
4445 : 1 : warn_invalid_pointer (NULL_TREE, stmt, *pclobber, arg, false);
4446 : : }
4447 : : }
4448 : :
4449 : : /* Check basic block BB for invalid accesses. */
4450 : :
4451 : : void
4452 : 41802301 : pass_waccess::check_block (basic_block bb)
4453 : : {
4454 : : /* Iterate over statements, looking for function calls. */
4455 : 231300595 : for (auto si = gsi_start_bb (bb); !gsi_end_p (si);
4456 : 147695993 : gsi_next_nondebug (&si))
4457 : : {
4458 : 147695993 : gimple *stmt = gsi_stmt (si);
4459 : 147695993 : if (gcall *call = dyn_cast <gcall *> (stmt))
4460 : 21668350 : check_call (call);
4461 : : else
4462 : 126027643 : check_stmt (stmt);
4463 : : }
4464 : 41802301 : }
4465 : :
4466 : : /* Return the argument that the call STMT to a built-in function returns
4467 : : (including with an offset) or null if it doesn't. */
4468 : :
4469 : : tree
4470 : 2122117 : pass_waccess::gimple_call_return_arg (gcall *call)
4471 : : {
4472 : : /* Check for attribute fn spec to see if the function returns one
4473 : : of its arguments. */
4474 : 2122117 : attr_fnspec fnspec = gimple_call_fnspec (call);
4475 : 2122117 : unsigned int argno;
4476 : 2122117 : if (!fnspec.returns_arg (&argno))
4477 : : {
4478 : 2055076 : if (gimple_call_num_args (call) < 1)
4479 : : return NULL_TREE;
4480 : :
4481 : 1981426 : if (!gimple_call_builtin_p (call, BUILT_IN_NORMAL))
4482 : : return NULL_TREE;
4483 : :
4484 : 273579 : tree fndecl = gimple_call_fndecl (call);
4485 : 273579 : switch (DECL_FUNCTION_CODE (fndecl))
4486 : : {
4487 : : case BUILT_IN_MEMPCPY:
4488 : : case BUILT_IN_MEMPCPY_CHK:
4489 : : case BUILT_IN_MEMCHR:
4490 : : case BUILT_IN_STRCHR:
4491 : : case BUILT_IN_STRRCHR:
4492 : : case BUILT_IN_STRSTR:
4493 : : case BUILT_IN_STPCPY:
4494 : : case BUILT_IN_STPCPY_CHK:
4495 : : case BUILT_IN_STPNCPY:
4496 : : case BUILT_IN_STPNCPY_CHK:
4497 : : argno = 0;
4498 : : break;
4499 : :
4500 : : default:
4501 : : return NULL_TREE;
4502 : : }
4503 : : }
4504 : :
4505 : 80415 : if (gimple_call_num_args (call) <= argno)
4506 : : return NULL_TREE;
4507 : :
4508 : 80415 : return gimple_call_arg (call, argno);
4509 : : }
4510 : :
4511 : : /* Check for and diagnose all uses of the dangling pointer VAR to the auto
4512 : : object DECL whose lifetime has ended. OBJREF is true when VAR denotes
4513 : : an access to a DECL that may have been clobbered. */
4514 : :
4515 : : void
4516 : 14086786 : pass_waccess::check_dangling_uses (tree var, tree decl, bool maybe /* = false */,
4517 : : bool objref /* = false */)
4518 : : {
4519 : 14086786 : if (!decl || !auto_var_p (decl))
4520 : 6247841 : return;
4521 : :
4522 : 7838945 : gimple **pclob = m_clobbers.get (decl);
4523 : 7838945 : if (!pclob)
4524 : : return;
4525 : :
4526 : 3402149 : if (!objref)
4527 : : {
4528 : 158361 : check_pointer_uses (*pclob, var, decl, maybe);
4529 : 158361 : return;
4530 : : }
4531 : :
4532 : 3243788 : gimple *use_stmt = SSA_NAME_DEF_STMT (var);
4533 : 3243788 : if (!use_after_inval_p (*pclob, use_stmt, true))
4534 : : return;
4535 : :
4536 : 0 : basic_block use_bb = gimple_bb (use_stmt);
4537 : 0 : basic_block clob_bb = gimple_bb (*pclob);
4538 : 0 : maybe = maybe || !dominated_by_p (CDI_POST_DOMINATORS, clob_bb, use_bb);
4539 : 0 : warn_invalid_pointer (var, use_stmt, *pclob, decl, maybe, false);
4540 : : }
4541 : :
4542 : : /* Diagnose stores in BB and (recursively) its predecessors of the addresses
4543 : : of local variables into nonlocal pointers that are left dangling after
4544 : : the function returns. Returns true when we can continue walking
4545 : : the CFG to predecessors. */
4546 : :
4547 : : bool
4548 : 14858569 : pass_waccess::check_dangling_stores (basic_block bb,
4549 : : hash_set<tree> &stores)
4550 : : {
4551 : : /* Iterate backwards over the statements looking for a store of
4552 : : the address of a local variable into a nonlocal pointer. */
4553 : 50112576 : for (auto gsi = gsi_last_nondebug_bb (bb); ; gsi_prev_nondebug (&gsi))
4554 : : {
4555 : 50112576 : gimple *stmt = gsi_stmt (gsi);
4556 : 50112576 : if (!stmt)
4557 : : break;
4558 : :
4559 : 38874987 : if (warning_suppressed_p (stmt, OPT_Wdangling_pointer_))
4560 : 35253440 : continue;
4561 : :
4562 : 38816672 : if (is_gimple_call (stmt)
4563 : 38816672 : && !(gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE)))
4564 : : /* Avoid looking before nonconst, nonpure calls since those might
4565 : : use the escaped locals. */
4566 : 3620980 : return false;
4567 : :
4568 : 35341094 : if (!is_gimple_assign (stmt) || gimple_clobber_p (stmt)
4569 : 58511145 : || !gimple_store_p (stmt))
4570 : 30356030 : continue;
4571 : :
4572 : 4985064 : access_ref lhs_ref;
4573 : 4985064 : tree lhs = gimple_assign_lhs (stmt);
4574 : 4985064 : if (!m_ptr_qry.get_ref (lhs, stmt, &lhs_ref, 0))
4575 : 9260 : continue;
4576 : :
4577 : 4975804 : if (TREE_CODE (lhs_ref.ref) == MEM_REF)
4578 : : {
4579 : 18253 : lhs_ref.ref = TREE_OPERAND (lhs_ref.ref, 0);
4580 : 18253 : ++lhs_ref.deref;
4581 : : }
4582 : 4975804 : if (TREE_CODE (lhs_ref.ref) == ADDR_EXPR)
4583 : : {
4584 : 6017 : lhs_ref.ref = TREE_OPERAND (lhs_ref.ref, 0);
4585 : 6017 : --lhs_ref.deref;
4586 : : }
4587 : 4975804 : if (TREE_CODE (lhs_ref.ref) == SSA_NAME)
4588 : : {
4589 : 1014749 : gimple *def_stmt = SSA_NAME_DEF_STMT (lhs_ref.ref);
4590 : 1014749 : if (!gimple_nop_p (def_stmt))
4591 : : /* Avoid looking at or before stores into unknown objects. */
4592 : : return false;
4593 : :
4594 : 869347 : lhs_ref.ref = SSA_NAME_VAR (lhs_ref.ref);
4595 : : }
4596 : :
4597 : 4830402 : if (TREE_CODE (lhs_ref.ref) == PARM_DECL
4598 : 4830402 : && (lhs_ref.deref - DECL_BY_REFERENCE (lhs_ref.ref)) > 0)
4599 : : /* Assignment through a (real) pointer/reference parameter. */;
4600 : 7022691 : else if (VAR_P (lhs_ref.ref)
4601 : 4022838 : && !auto_var_p (lhs_ref.ref))
4602 : : /* Assignment to/through a non-local variable. */;
4603 : : else
4604 : : /* Something else, don't warn. */
4605 : 2999853 : continue;
4606 : :
4607 : 1830549 : if (stores.add (lhs_ref.ref))
4608 : 1260715 : continue;
4609 : :
4610 : : /* FIXME: Handle stores of alloca() and VLA. */
4611 : 569834 : access_ref rhs_ref;
4612 : 569834 : tree rhs = gimple_assign_rhs1 (stmt);
4613 : 569834 : if (!m_ptr_qry.get_ref (rhs, stmt, &rhs_ref, 0)
4614 : 569834 : || rhs_ref.deref != -1)
4615 : 543785 : continue;
4616 : :
4617 : 26049 : if (!auto_var_p (rhs_ref.ref))
4618 : 25482 : continue;
4619 : :
4620 : 567 : auto_diagnostic_group d;
4621 : 567 : location_t loc = gimple_location (stmt);
4622 : 567 : if (warning_at (loc, OPT_Wdangling_pointer_,
4623 : : "storing the address of local variable %qD in %qE",
4624 : : rhs_ref.ref, lhs))
4625 : : {
4626 : 45 : suppress_warning (stmt, OPT_Wdangling_pointer_);
4627 : :
4628 : 45 : location_t loc = DECL_SOURCE_LOCATION (rhs_ref.ref);
4629 : 45 : inform (loc, "%qD declared here", rhs_ref.ref);
4630 : :
4631 : 45 : loc = DECL_SOURCE_LOCATION (lhs_ref.ref);
4632 : 45 : inform (loc, "%qD declared here", lhs_ref.ref);
4633 : : }
4634 : 35254574 : }
4635 : :
4636 : 11237589 : return true;
4637 : : }
4638 : :
4639 : : /* Diagnose stores of the addresses of local variables into nonlocal
4640 : : pointers that are left dangling after the function returns. */
4641 : :
4642 : : void
4643 : 3692939 : pass_waccess::check_dangling_stores ()
4644 : : {
4645 : 3692939 : if (EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (m_func)->preds) == 0)
4646 : 54534 : return;
4647 : :
4648 : 3638405 : auto_bitmap bbs;
4649 : 3638405 : hash_set<tree> stores;
4650 : 3638405 : auto_vec<edge_iterator, 8> worklist (n_basic_blocks_for_fn (cfun) + 1);
4651 : 3638405 : worklist.quick_push (ei_start (EXIT_BLOCK_PTR_FOR_FN (m_func)->preds));
4652 : 32310486 : do
4653 : : {
4654 : 32310486 : edge_iterator ei = worklist.last ();
4655 : 32310486 : basic_block src = ei_edge (ei)->src;
4656 : 32310486 : if (bitmap_set_bit (bbs, src->index))
4657 : : {
4658 : 14858569 : if (check_dangling_stores (src, stores)
4659 : 14858569 : && EDGE_COUNT (src->preds) > 0)
4660 : 9748338 : worklist.quick_push (ei_start (src->preds));
4661 : : }
4662 : : else
4663 : : {
4664 : 17451917 : if (ei_one_before_end_p (ei))
4665 : 13386743 : worklist.pop ();
4666 : : else
4667 : 4065174 : ei_next (&worklist.last ());
4668 : : }
4669 : : }
4670 : 64620972 : while (!worklist.is_empty ());
4671 : 3638405 : }
4672 : :
4673 : : /* Check for and diagnose uses of dangling pointers to auto objects
4674 : : whose lifetime has ended. */
4675 : :
4676 : : void
4677 : 3692939 : pass_waccess::check_dangling_uses ()
4678 : : {
4679 : 3692939 : tree var;
4680 : 3692939 : unsigned i;
4681 : 108453824 : FOR_EACH_SSA_NAME (i, var, m_func)
4682 : : {
4683 : : /* For each SSA_NAME pointer VAR find the object it points to.
4684 : : If the object is a clobbered local variable, check to see
4685 : : if any of VAR's uses (or those of other pointers derived
4686 : : from VAR) happens after the clobber. If so, warn. */
4687 : :
4688 : 100914120 : gimple *def_stmt = SSA_NAME_DEF_STMT (var);
4689 : 100914120 : if (is_gimple_assign (def_stmt))
4690 : : {
4691 : 64752929 : tree rhs = gimple_assign_rhs1 (def_stmt);
4692 : 64752929 : if (TREE_CODE (rhs) == ADDR_EXPR)
4693 : : {
4694 : 4320428 : if (!POINTER_TYPE_P (TREE_TYPE (var)))
4695 : 1928849 : continue;
4696 : 2391579 : check_dangling_uses (var, TREE_OPERAND (rhs, 0));
4697 : : }
4698 : : else
4699 : : {
4700 : : /* For other expressions, check the base DECL to see
4701 : : if it's been clobbered, most likely as a result of
4702 : : inlining a reference to it. */
4703 : 60432501 : tree decl = get_base_address (rhs);
4704 : 60432501 : if (DECL_P (decl))
4705 : 11483329 : check_dangling_uses (var, decl, false, true);
4706 : : }
4707 : : }
4708 : 36161191 : else if (POINTER_TYPE_P (TREE_TYPE (var)))
4709 : : {
4710 : 5868423 : if (gcall *call = dyn_cast<gcall *>(def_stmt))
4711 : : {
4712 : 1792505 : if (tree arg = gimple_call_return_arg (call))
4713 : : {
4714 : 27575 : access_ref aref;
4715 : 27575 : if (m_ptr_qry.get_ref (arg, call, &aref, 0)
4716 : 27575 : && aref.deref < 0)
4717 : 9349 : check_dangling_uses (var, aref.ref);
4718 : : }
4719 : : }
4720 : 105325669 : else if (gphi *phi = dyn_cast <gphi *>(def_stmt))
4721 : : {
4722 : 564784 : unsigned nargs = gimple_phi_num_args (phi);
4723 : 1932294 : for (unsigned i = 0; i != nargs; ++i)
4724 : : {
4725 : 1367510 : access_ref aref;
4726 : 1367510 : tree arg = gimple_phi_arg_def (phi, i);
4727 : 1367510 : if (m_ptr_qry.get_ref (arg, phi, &aref, 0)
4728 : 1367510 : && aref.deref < 0)
4729 : 202529 : check_dangling_uses (var, aref.ref, true);
4730 : : }
4731 : : }
4732 : : }
4733 : : }
4734 : 3692939 : }
4735 : :
4736 : : /* Check CALL arguments for dangling pointers (those that have been
4737 : : clobbered) and warn if found. */
4738 : :
4739 : : void
4740 : 21654835 : pass_waccess::check_call_dangling (gcall *call)
4741 : : {
4742 : 21654835 : unsigned nargs = gimple_call_num_args (call);
4743 : 64125398 : for (unsigned i = 0; i != nargs; ++i)
4744 : : {
4745 : 42470563 : tree arg = gimple_call_arg (call, i);
4746 : 42470563 : if (TREE_CODE (arg) != ADDR_EXPR)
4747 : 42470537 : continue;
4748 : :
4749 : 11798572 : arg = TREE_OPERAND (arg, 0);
4750 : 11798572 : if (!DECL_P (arg))
4751 : 3895445 : continue;
4752 : :
4753 : 7903127 : gimple **pclobber = m_clobbers.get (arg);
4754 : 7903127 : if (!pclobber)
4755 : 7730882 : continue;
4756 : :
4757 : 172245 : if (!use_after_inval_p (*pclobber, call))
4758 : 172219 : continue;
4759 : :
4760 : 26 : warn_invalid_pointer (NULL_TREE, call, *pclobber, arg, false);
4761 : : }
4762 : 21654835 : }
4763 : :
4764 : : /* Check function FUN for invalid accesses. */
4765 : :
4766 : : unsigned
4767 : 5125240 : pass_waccess::execute (function *fun)
4768 : : {
4769 : 5125240 : auto_urlify_attributes sentinel;
4770 : :
4771 : 5125240 : calculate_dominance_info (CDI_DOMINATORS);
4772 : 5125240 : calculate_dominance_info (CDI_POST_DOMINATORS);
4773 : :
4774 : : /* Set or clear EDGE_DFS_BACK bits on back edges. */
4775 : 5125240 : mark_dfs_back_edges (fun);
4776 : :
4777 : : /* Create a new ranger instance and associate it with FUN. */
4778 : 5125240 : m_ptr_qry.rvals = enable_ranger (fun);
4779 : 5125240 : m_func = fun;
4780 : :
4781 : : /* Check for dangling pointers in the earliest run of the pass.
4782 : : The latest point -Wdangling-pointer should run is just before
4783 : : loop unrolling which introduces uses after clobbers. Most cases
4784 : : can be detected without optimization; cases where the address of
4785 : : the local variable is passed to and then returned from a user-
4786 : : defined function before its lifetime ends and the returned pointer
4787 : : becomes dangling depend on inlining. */
4788 : 5125240 : m_check_dangling_p = m_early_checks_p;
4789 : :
4790 : 5125240 : auto_bitmap bb_uids_set (&bitmap_default_obstack);
4791 : 5125240 : m_bb_uids_set = bb_uids_set;
4792 : :
4793 : 5125240 : set_gimple_stmt_max_uid (m_func, 0);
4794 : :
4795 : 5125240 : basic_block bb;
4796 : 46927541 : FOR_EACH_BB_FN (bb, fun)
4797 : 41802301 : check_block (bb);
4798 : :
4799 : 5125240 : if (m_check_dangling_p)
4800 : : {
4801 : 3692939 : check_dangling_uses ();
4802 : 3692939 : check_dangling_stores ();
4803 : : }
4804 : :
4805 : 5125240 : if (dump_file)
4806 : 149 : m_ptr_qry.dump (dump_file, (dump_flags & TDF_DETAILS) != 0);
4807 : :
4808 : 5125240 : m_ptr_qry.flush_cache ();
4809 : :
4810 : : /* Release the ranger instance and replace it with a global ranger.
4811 : : Also reset the pointer since calling disable_ranger() deletes it. */
4812 : 5125240 : disable_ranger (fun);
4813 : 5125240 : m_ptr_qry.rvals = NULL;
4814 : :
4815 : 5125240 : m_clobbers.empty ();
4816 : 5125240 : m_bb_uids_set = NULL;
4817 : :
4818 : 5125240 : free_dominance_info (CDI_POST_DOMINATORS);
4819 : 5125240 : free_dominance_info (CDI_DOMINATORS);
4820 : 5125240 : return 0;
4821 : 5125240 : }
4822 : :
4823 : : } // namespace
4824 : :
4825 : : /* Return a new instance of the pass. */
4826 : :
4827 : : gimple_opt_pass *
4828 : 282866 : make_pass_warn_access (gcc::context *ctxt)
4829 : : {
4830 : 282866 : return new pass_waccess (ctxt);
4831 : : }
|