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 : 2295529 : get_location (const gimple *stmt)
79 : : {
80 : 2037060 : 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 : 1236581 : get_callee_fndecl (const gimple *stmt)
101 : : {
102 : 632 : return gimple_call_fndecl (stmt);
103 : : }
104 : :
105 : : static inline unsigned
106 : 35017066 : call_nargs (const gimple *stmt)
107 : : {
108 : 2353300 : 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 : 13877127 : call_arg (const gimple *stmt, unsigned argno)
120 : : {
121 : 1371478 : 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 : 703157 : 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 : 703157 : c_strlen_data lendata = { };
275 : 703157 : tree len = c_strlen (exp, 1, &lendata);
276 : 703157 : 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 : 696510 : 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 : 696510 : tree nonstr = unterminated_array (src, &size, &exact);
325 : 696510 : 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 : 399320 : check_nul_terminated_array (gimple *stmt, tree src, tree bound /* = NULL_TREE */)
363 : : {
364 : 399320 : return check_nul_terminated_array<gimple *>(stmt, src, bound);
365 : : }
366 : :
367 : : bool
368 : 284776 : check_nul_terminated_array (tree expr, tree src, tree bound /* = NULL_TREE */)
369 : : {
370 : 284776 : 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 : 7397143 : maybe_warn_nonstring_arg (tree fndecl, GimpleOrTree exp)
380 : : {
381 : 7397143 : if (!fndecl || !fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
382 : : return false;
383 : :
384 : 2365443 : if (!warn_stringop_overread
385 : 2365443 : || warning_suppressed_p (exp, OPT_Wstringop_overread))
386 : 11787 : return false;
387 : :
388 : : /* Avoid clearly invalid calls (more checking done below). */
389 : 2353656 : unsigned nargs = call_nargs (exp);
390 : 2353656 : if (!nargs)
391 : : return false;
392 : :
393 : : /* The bound argument to a bounded string function like strncpy. */
394 : 2036720 : 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 : 2036720 : 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 : 2036720 : int fncode = DECL_FUNCTION_CODE (fndecl);
407 : 2036720 : 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 : 392359 : for (unsigned argno = 0;
419 : 783796 : argno < MIN (nargs, 2)
420 : 783796 : && !(maxlen && TREE_CODE (maxlen) == INTEGER_CST); argno++)
421 : : {
422 : 392359 : tree arg = call_arg (exp, argno);
423 : 392359 : if (!get_attr_nonstring_decl (arg))
424 : : {
425 : 391419 : 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 : 391419 : lendata.maxbound = arg;
430 : 391419 : get_range_strlen (arg, &lendata, /* eltsize = */ 1);
431 : 391419 : 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 : 397424 : if (nargs > 2)
441 : 11342 : 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 : 2036720 : tree bndrng[2] = { NULL_TREE, NULL_TREE };
474 : 2036720 : if (bound)
475 : : {
476 : 13534 : STRIP_NOPS (bound);
477 : 13534 : get_size_range (bound, bndrng);
478 : : }
479 : :
480 : 2036720 : location_t loc = get_location (exp);
481 : :
482 : 2036720 : if (bndrng[0])
483 : : {
484 : : /* Diagnose excessive bound prior to the adjustment below and
485 : : regardless of attribute nonstring. */
486 : 13514 : tree maxobjsize = max_object_size ();
487 : 13514 : 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 : 2036589 : if (maxlen && !integer_all_onesp (maxlen))
509 : : {
510 : : /* Add one for the nul. */
511 : 6950 : maxlen = const_binop (PLUS_EXPR, TREE_TYPE (maxlen), maxlen,
512 : : size_one_node);
513 : :
514 : 6950 : 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 : 4592 : bndrng[0] = maxlen;
519 : 4592 : bndrng[1] = maxlen;
520 : 4592 : bound = void_type_node;
521 : : }
522 : 2358 : 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 : 2349 : if (tree_int_cst_lt (maxlen, bndrng[0]))
527 : 200 : bndrng[0] = maxlen;
528 : 2149 : else if (tree_int_cst_lt (maxlen, bndrng[1]))
529 : 527 : bndrng[1] = maxlen;
530 : : }
531 : : }
532 : :
533 : 2036589 : 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 : 2036589 : function_args_iter_init (&it, TREE_TYPE (fndecl));
540 : :
541 : 6565722 : 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 : 6562768 : if (argno >= nargs)
546 : : break;
547 : :
548 : 4689775 : tree argtype = function_args_iter_cond (&it);
549 : 4689775 : if (!argtype)
550 : : break;
551 : :
552 : 4526179 : if (TREE_CODE (argtype) != POINTER_TYPE)
553 : 4523225 : continue;
554 : :
555 : 2777311 : argtype = TREE_TYPE (argtype);
556 : :
557 : 4564290 : if (TREE_CODE (argtype) != INTEGER_TYPE
558 : 2777311 : || !TYPE_READONLY (argtype))
559 : 1786979 : continue;
560 : :
561 : 990332 : argtype = TYPE_MAIN_VARIANT (argtype);
562 : 990332 : if (argtype != char_type_node)
563 : 11399 : continue;
564 : :
565 : 978933 : tree callarg = call_arg (exp, argno);
566 : 978933 : if (TREE_CODE (callarg) == ADDR_EXPR)
567 : 542301 : callarg = TREE_OPERAND (callarg, 0);
568 : :
569 : : /* See if the destination is declared with attribute "nonstring". */
570 : 978933 : tree decl = get_attr_nonstring_decl (callarg);
571 : 978933 : if (!decl)
572 : 975979 : 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 : 2036589 : if (any_arg_warned)
684 : 897 : suppress_warning (exp, OPT_Wstringop_overread);
685 : :
686 : : return any_arg_warned;
687 : : }
688 : :
689 : : bool
690 : 513810 : maybe_warn_nonstring_arg (tree fndecl, gimple *stmt)
691 : : {
692 : 513810 : 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 : 1891 : 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 : 1891 : bool warned = false;
935 : :
936 : 1891 : 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 : 1753 : 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 : 579 : if (read)
1073 : : {
1074 : 576 : if (tree_int_cst_equal (range[0], range[1]))
1075 : 248 : warned = (func
1076 : 494 : ? 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 : 250 : : 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 : 576 : if (warned)
1139 : 400 : suppress_warning (exp, OPT_Wstringop_overread);
1140 : :
1141 : 576 : 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 : 1442 : 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 : 1534145 : get_size_range (range_query *query, tree bound, gimple *stmt, tree range[2],
1208 : : int flags, const offset_int bndrng[2])
1209 : : {
1210 : 1534145 : if (bound)
1211 : 664430 : get_size_range (query, bound, stmt, range, flags);
1212 : :
1213 : 1534145 : if (!bndrng || (bndrng[0] == 0 && bndrng[1] == HOST_WIDE_INT_M1U))
1214 : 1015841 : return;
1215 : :
1216 : 518304 : if (range[0] && TREE_CODE (range[0]) == INTEGER_CST)
1217 : : {
1218 : 518268 : offset_int r[] =
1219 : 518268 : { wi::to_offset (range[0]), wi::to_offset (range[1]) };
1220 : 518268 : if (r[0] < bndrng[0])
1221 : 0 : range[0] = wide_int_to_tree (sizetype, bndrng[0]);
1222 : 518268 : if (bndrng[1] < r[1])
1223 : 3 : range[1] = wide_int_to_tree (sizetype, bndrng[1]);
1224 : 518268 : }
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 : 834000 : 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 : 834000 : 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 : 834000 : 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 : 834000 : 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 : 834000 : bool at_least_one = false;
1289 : 834000 : 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 : 513023 : if (POINTER_TYPE_P (TREE_TYPE (srcstr)))
1294 : : {
1295 : 405963 : 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 : 405404 : c_strlen_data lendata = { };
1306 : 405404 : get_range_strlen (srcstr, &lendata, /* eltsize = */ 1);
1307 : 405404 : range[0] = lendata.minlen;
1308 : 405404 : range[1] = lendata.maxbound ? lendata.maxbound : lendata.maxlen;
1309 : 405404 : if (range[0]
1310 : 405404 : && TREE_CODE (range[0]) == INTEGER_CST
1311 : 405399 : && TREE_CODE (range[1]) == INTEGER_CST
1312 : 405399 : && (!maxread || TREE_CODE (maxread) == INTEGER_CST))
1313 : : {
1314 : 106494 : if (maxread && tree_int_cst_le (maxread, range[0]))
1315 : 532 : range[0] = range[1] = maxread;
1316 : : else
1317 : 394126 : range[0] = fold_build2 (PLUS_EXPR, size_type_node,
1318 : : range[0], size_one_node);
1319 : :
1320 : 394658 : if (maxread && tree_int_cst_le (maxread, range[1]))
1321 : 84677 : range[1] = maxread;
1322 : 309981 : else if (!integer_all_onesp (range[1]))
1323 : 162996 : range[1] = fold_build2 (PLUS_EXPR, size_type_node,
1324 : : range[1], size_one_node);
1325 : :
1326 : 394658 : slen = range[0];
1327 : : }
1328 : : else
1329 : : {
1330 : 10746 : at_least_one = true;
1331 : 10746 : slen = size_one_node;
1332 : : }
1333 : : }
1334 : : else
1335 : : slen = srcstr;
1336 : : }
1337 : :
1338 : 833441 : if (!dstwrite && !maxread)
1339 : : {
1340 : : /* When the only available piece of data is the object size
1341 : : there is nothing to do. */
1342 : 288407 : 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 : 288001 : if (!range[0])
1348 : 79 : dstwrite = slen;
1349 : : }
1350 : :
1351 : 833035 : if (!dstsize)
1352 : 400617 : dstsize = maxobjsize;
1353 : :
1354 : : /* Set RANGE to that of DSTWRITE if non-null, bounded by PAD->DST_BNDRNG
1355 : : if valid. */
1356 : 833035 : gimple *stmt = pad ? pad->stmt : nullptr;
1357 : 2496639 : 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 : 833035 : integer_zerop (dstsize) ? SR_ALLOW_ZERO : 0,
1362 : : pad ? pad->dst_bndrng : NULL);
1363 : :
1364 : 833035 : 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 : 833035 : 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 : 833035 : if (range[0]
1374 : 831875 : && TREE_CODE (range[0]) == INTEGER_CST
1375 : 1664906 : && 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 : 832907 : 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 : 832907 : if (range[0] || !exactwrite || integer_all_onesp (dstwrite))
1390 : : {
1391 : 832907 : if (range[0]
1392 : 831747 : && TREE_CODE (range[0]) == INTEGER_CST
1393 : 1664650 : && ((tree_fits_uhwi_p (dstsize)
1394 : 831426 : && tree_int_cst_lt (dstsize, range[0]))
1395 : 830135 : || (dstwrite
1396 : 427574 : && tree_fits_uhwi_p (dstwrite)
1397 : 350921 : && 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 : 920 : suppress_warning (exp, OPT_Wstringop_overflow_);
1441 : 920 : if (pad)
1442 : 837 : 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 : 831299 : 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 : 118285 : get_size_range (rvals, maxread, stmt, range, 0,
1458 : : pad ? pad->src_bndrng : NULL);
1459 : :
1460 : 118285 : location_t loc = get_location (exp);
1461 : 118285 : tree size = dstsize;
1462 : 118285 : if (pad && pad->mode == access_read_only)
1463 : 114670 : size = wide_int_to_tree (sizetype, pad->src.size_remaining ());
1464 : :
1465 : 118285 : if (range[0] && maxread && tree_fits_uhwi_p (size))
1466 : : {
1467 : 118241 : 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 : 118154 : if (size != maxobjsize && tree_int_cst_lt (size, range[0]))
1475 : : {
1476 : 107 : 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 : 117980 : maybe_warn_nonstring_arg (func, exp);
1485 : : }
1486 : :
1487 : : /* Check for reading past the end of SRC. */
1488 : 1661988 : bool overread = (slen
1489 : 830994 : && slen == srcstr
1490 : 106449 : && dstwrite
1491 : 105630 : && range[0]
1492 : 105630 : && TREE_CODE (slen) == INTEGER_CST
1493 : 936624 : && 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 : 830994 : if (!overread
1497 : 830994 : && pad
1498 : 829148 : && pad->src.sizrng[1] >= 0
1499 : 592941 : && pad->src.offrng[0] >= 0
1500 : 1660142 : && (pad->src.offrng[1] < 0
1501 : 580287 : || 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 : 580287 : 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 : 580287 : overread = pad->src.sizrng[1] - pad->src.offrng[0] < pad->src_bndrng[0];
1509 : 580287 : range[0] = wide_int_to_tree (sizetype, pad->src_bndrng[0]);
1510 : 580287 : slen = size_zero_node;
1511 : : }
1512 : :
1513 : 830994 : if (overread)
1514 : : {
1515 : 663 : const opt_code opt = OPT_Wstringop_overread;
1516 : 663 : if (warning_suppressed_p (exp, opt)
1517 : 596 : || (srcstr && warning_suppressed_p (srcstr, opt))
1518 : 1231 : || (pad && pad->src.ref
1519 : 568 : && warning_suppressed_p (pad->src.ref, opt)))
1520 : 95 : return false;
1521 : :
1522 : 568 : location_t loc = get_location (exp);
1523 : 568 : const bool read
1524 : 568 : = mode == access_read_only || mode == access_read_write;
1525 : 568 : const bool maybe = pad && pad->dst.parmarray;
1526 : 568 : auto_diagnostic_group d;
1527 : 568 : 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 : 568 : }
1536 : :
1537 : : return true;
1538 : : }
1539 : :
1540 : : static bool
1541 : 825293 : 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 : 44302 : fndecl_alloc_p (tree fndecl, bool all_alloc)
1566 : : {
1567 : 44302 : if (!fndecl)
1568 : : return false;
1569 : :
1570 : : /* A call to operator new isn't recognized as one to a built-in. */
1571 : 44265 : if (DECL_IS_OPERATOR_NEW_P (fndecl))
1572 : : return true;
1573 : :
1574 : 32656 : if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
1575 : : {
1576 : 28323 : 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 : 4333 : tree attrs = DECL_ATTRIBUTES (fndecl);
1599 : 4333 : if (!attrs)
1600 : : return false;
1601 : :
1602 : 103 : for (tree allocs = attrs;
1603 : 1687 : (allocs = lookup_attribute ("malloc", allocs));
1604 : 103 : allocs = TREE_CHAIN (allocs))
1605 : : {
1606 : 875 : tree args = TREE_VALUE (allocs);
1607 : 875 : if (!args)
1608 : 103 : continue;
1609 : :
1610 : 772 : 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 : 44302 : gimple_call_alloc_p (gimple *stmt, bool all_alloc = false)
1622 : : {
1623 : 44302 : 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 : 2362 : new_delete_mismatch_p (const demangle_component &newc,
1632 : : const demangle_component &delc)
1633 : : {
1634 : 3646 : if (newc.type != delc.type)
1635 : : return true;
1636 : :
1637 : 3534 : switch (newc.type)
1638 : : {
1639 : 988 : case DEMANGLE_COMPONENT_NAME:
1640 : 988 : {
1641 : 988 : int len = newc.u.s_name.len;
1642 : 988 : const char *news = newc.u.s_name.s;
1643 : 988 : const char *dels = delc.u.s_name.s;
1644 : 988 : if (len != delc.u.s_name.len || memcmp (news, dels, len))
1645 : : return true;
1646 : :
1647 : 973 : if (news[len] == 'n')
1648 : : {
1649 : 354 : if (news[len + 1] == 'a')
1650 : 114 : return dels[len] != 'd' || dels[len + 1] != 'a';
1651 : 258 : if (news[len + 1] == 'w')
1652 : 270 : 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 : 1708 : default:
1724 : 1708 : break;
1725 : : }
1726 : :
1727 : 1708 : if (!newc.u.s_binary.left != !delc.u.s_binary.left)
1728 : : return true;
1729 : :
1730 : 1708 : if (!newc.u.s_binary.left)
1731 : : return false;
1732 : :
1733 : 1682 : if (new_delete_mismatch_p (*newc.u.s_binary.left, *delc.u.s_binary.left)
1734 : 1682 : || !newc.u.s_binary.right != !delc.u.s_binary.right)
1735 : : return true;
1736 : :
1737 : 1571 : 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 : 11580 : new_delete_mismatch_p (tree new_decl, tree delete_decl)
1748 : : {
1749 : 11580 : tree new_name = DECL_ASSEMBLER_NAME (new_decl);
1750 : 11580 : 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 : 11580 : bool certain;
1757 : 11580 : 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 : 732 : 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 : 686 : const char *new_str = IDENTIFIER_POINTER (new_name);
1767 : 686 : const char *del_str = IDENTIFIER_POINTER (delete_name);
1768 : :
1769 : 686 : void *np = NULL, *dp = NULL;
1770 : 686 : demangle_component *ndc = cplus_demangle_v3_components (new_str, 0, &np);
1771 : 686 : 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 : 2046 : auto strip_dc_template = [] (demangle_component* dc)
1780 : : {
1781 : 680 : if (dc->type == DEMANGLE_COMPONENT_TEMPLATE)
1782 : 45 : dc = dc->u.s_binary.left;
1783 : 1360 : return dc;
1784 : : };
1785 : :
1786 : 686 : bool mismatch = (ndc && ddc
1787 : 1366 : && new_delete_mismatch_p (*strip_dc_template (ndc),
1788 : 680 : *strip_dc_template (ddc)));
1789 : 686 : free (np);
1790 : 686 : free (dp);
1791 : 686 : 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 : 43154 : 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 : 43154 : enum class alloc_kind_t { none, builtin, user }
1804 : 43154 : alloc_dealloc_kind = alloc_kind_t::none;
1805 : :
1806 : 43154 : if (DECL_IS_OPERATOR_NEW_P (alloc_decl))
1807 : : {
1808 : 11609 : 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 : 11580 : return !new_delete_mismatch_p (alloc_decl, dealloc_decl);
1812 : :
1813 : : /* Return false for deallocation functions that are known not
1814 : : to match. */
1815 : 29 : 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 : 31545 : else if (fndecl_built_in_p (alloc_decl, BUILT_IN_NORMAL))
1822 : : {
1823 : 30761 : switch (DECL_FUNCTION_CODE (alloc_decl))
1824 : : {
1825 : : case BUILT_IN_ALLOCA:
1826 : : case BUILT_IN_ALLOCA_WITH_ALIGN:
1827 : : return false;
1828 : :
1829 : 1195 : case BUILT_IN_GOMP_ALLOC:
1830 : 1195 : case BUILT_IN_GOMP_REALLOC:
1831 : 1195 : if (DECL_IS_OPERATOR_DELETE_P (dealloc_decl))
1832 : : return false;
1833 : :
1834 : 1195 : 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 : 29566 : case BUILT_IN_ALIGNED_ALLOC:
1842 : 29566 : case BUILT_IN_CALLOC:
1843 : 29566 : case BUILT_IN_MALLOC:
1844 : 29566 : case BUILT_IN_REALLOC:
1845 : 29566 : case BUILT_IN_STRDUP:
1846 : 29566 : case BUILT_IN_STRNDUP:
1847 : 29566 : if (DECL_IS_OPERATOR_DELETE_P (dealloc_decl))
1848 : : return false;
1849 : :
1850 : 29491 : 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 : 824 : alloc_kind_t realloc_kind = alloc_kind_t::none;
1864 : :
1865 : 824 : if (fndecl_built_in_p (dealloc_decl, BUILT_IN_NORMAL))
1866 : : {
1867 : 130 : built_in_function dealloc_code = DECL_FUNCTION_CODE (dealloc_decl);
1868 : 130 : if (dealloc_code == BUILT_IN_REALLOC
1869 : 130 : || dealloc_code == BUILT_IN_GOMP_REALLOC)
1870 : 27 : realloc_kind = alloc_kind_t::builtin;
1871 : :
1872 : 130 : for (tree amats = DECL_ATTRIBUTES (alloc_decl);
1873 : 225 : (amats = lookup_attribute ("malloc", amats));
1874 : 95 : amats = TREE_CHAIN (amats))
1875 : : {
1876 : 163 : tree args = TREE_VALUE (amats);
1877 : 163 : if (!args)
1878 : 0 : continue;
1879 : :
1880 : 163 : tree fndecl = TREE_VALUE (args);
1881 : 163 : if (!fndecl || !DECL_P (fndecl))
1882 : 0 : continue;
1883 : :
1884 : 163 : if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
1885 : 163 : && dealloc_code == DECL_FUNCTION_CODE (fndecl))
1886 : : return true;
1887 : : }
1888 : : }
1889 : :
1890 : 756 : const bool alloc_builtin = fndecl_built_in_p (alloc_decl, BUILT_IN_NORMAL);
1891 : 756 : 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 : 756 : for (tree ddats = DECL_ATTRIBUTES (dealloc_decl);
1899 : 2592 : (ddats = lookup_attribute ("*dealloc", ddats));
1900 : 1836 : ddats = TREE_CHAIN (ddats))
1901 : : {
1902 : 2424 : tree args = TREE_VALUE (ddats);
1903 : 2424 : if (!args)
1904 : 0 : continue;
1905 : :
1906 : 2424 : tree alloc = TREE_VALUE (args);
1907 : 2424 : if (!alloc)
1908 : 0 : continue;
1909 : :
1910 : 2424 : if (alloc == DECL_NAME (dealloc_decl))
1911 : 44 : realloc_kind = alloc_kind_t::user;
1912 : :
1913 : 2424 : 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 : 2424 : 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 : 115 : 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 : 83 : 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 : 43154 : matching_alloc_calls_p (gimple *alloc, tree dealloc_decl)
2013 : : {
2014 : 43154 : tree alloc_decl = gimple_call_fndecl (alloc);
2015 : 43154 : if (!alloc_decl)
2016 : : return true;
2017 : :
2018 : 43154 : 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 : 159053 : warn_dealloc_offset (location_t loc, gimple *call, const access_ref &aref)
2028 : : {
2029 : 159053 : if (aref.deref || aref.offrng[0] <= 0 || aref.offrng[1] <= 0)
2030 : 158978 : 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 : 844242 : pass_waccess::pass_waccess (gcc::context *ctxt)
2205 : : : gimple_opt_pass (pass_data_waccess, ctxt),
2206 : 844242 : m_ptr_qry (NULL),
2207 : 844242 : m_clobbers (),
2208 : 844242 : m_bb_uids_set (),
2209 : 844242 : m_func (),
2210 : 844242 : m_check_dangling_p (),
2211 : 844242 : m_early_checks_p ()
2212 : : {
2213 : 844242 : }
2214 : :
2215 : : /* Return a copy of the pass with RUN_NUMBER one greater than THIS. */
2216 : :
2217 : : opt_pass*
2218 : 562828 : pass_waccess::clone ()
2219 : : {
2220 : 562828 : return new pass_waccess (m_ctxt);
2221 : : }
2222 : :
2223 : : /* Release pointer_query cache. */
2224 : :
2225 : 1517586 : pass_waccess::~pass_waccess ()
2226 : : {
2227 : 758793 : m_ptr_qry.flush_cache ();
2228 : 1517586 : }
2229 : :
2230 : : void
2231 : 844242 : pass_waccess::set_pass_param (unsigned int n, bool early)
2232 : : {
2233 : 844242 : gcc_assert (n == 0);
2234 : :
2235 : 844242 : m_early_checks_p = early;
2236 : 844242 : }
2237 : :
2238 : : /* Return true when any checks performed by the pass are enabled. */
2239 : :
2240 : : bool
2241 : 5434856 : pass_waccess::gate (function *)
2242 : : {
2243 : 5434856 : return (warn_free_nonheap_object
2244 : 117 : || warn_mismatched_alloc
2245 : 5434973 : || 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 : 51805 : alloc_max_size (void)
2254 : : {
2255 : 51805 : HOST_WIDE_INT limit = warn_alloc_size_limit;
2256 : 51805 : if (limit == HOST_WIDE_INT_MAX)
2257 : 51409 : limit = tree_to_shwi (TYPE_MAX_VALUE (ptrdiff_type_node));
2258 : :
2259 : 51805 : 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 : 51805 : 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 : 51805 : 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 : 51805 : tree maxobjsize = alloc_max_size ();
2279 : :
2280 : 51805 : location_t loc = get_location (stmt);
2281 : :
2282 : 51805 : tree fn = gimple_call_fndecl (stmt);
2283 : 51805 : tree fntype = fn ? TREE_TYPE (fn) : gimple_call_fntype (stmt);
2284 : 51805 : bool warned = false;
2285 : :
2286 : : /* Validate each argument individually. */
2287 : 104875 : for (unsigned i = 0; i != 2 && args[i]; ++i)
2288 : : {
2289 : 53070 : if (TREE_CODE (args[i]) == INTEGER_CST)
2290 : : {
2291 : 33033 : argrange[i][0] = args[i];
2292 : 33033 : argrange[i][1] = args[i];
2293 : :
2294 : 33033 : 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 : 33009 : 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 : 956 : if (fn && fndecl_built_in_p (fn, BUILT_IN_ALLOCA)
2310 : 978 : ? IDENTIFIER_LENGTH (DECL_NAME (fn)) != 6
2311 : 489 : : !lookup_attribute ("returns_nonnull",
2312 : 489 : TYPE_ATTRIBUTES (fntype)))
2313 : 482 : warned = warning_at (loc, OPT_Walloc_zero,
2314 : : "argument %i value is zero",
2315 : 482 : idx[i] + 1);
2316 : : }
2317 : 32520 : 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 : 126 : if (i == 0
2324 : 105 : && fn
2325 : 87 : && !args[1]
2326 : 79 : && lang_GNU_CXX ()
2327 : 21 : && DECL_IS_OPERATOR_NEW_P (fn)
2328 : 126 : && integer_all_onesp (args[i]))
2329 : 21 : continue;
2330 : :
2331 : 84 : warned = warning_at (loc, OPT_Walloc_size_larger_than_,
2332 : : "argument %i value %qE exceeds "
2333 : : "maximum object size %E",
2334 : 84 : idx[i] + 1, args[i], maxobjsize);
2335 : : }
2336 : : }
2337 : 20037 : else if (TREE_CODE (args[i]) == SSA_NAME
2338 : 20037 : && 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 : 20024 : if (tree_int_cst_lt (argrange[i][0], integer_zero_node)
2343 : 20024 : && 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 : 19993 : else if (tree_int_cst_lt (maxobjsize, argrange[i][0]))
2351 : : {
2352 : 33 : warned = warning_at (loc, OPT_Walloc_size_larger_than_,
2353 : : "argument %i range [%E, %E] exceeds "
2354 : : "maximum object size %E",
2355 : 33 : idx[i] + 1,
2356 : : argrange[i][0], argrange[i][1],
2357 : : maxobjsize);
2358 : : }
2359 : : }
2360 : : }
2361 : :
2362 : 51805 : 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 : 51572 : if (!warned && tree_fits_uhwi_p (argrange[0][0])
2368 : 51539 : && argrange[1][0] && tree_fits_uhwi_p (argrange[1][0])
2369 : 1165 : && !integer_onep (argrange[0][0])
2370 : 52620 : && !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 : 441 : unsigned szprec = TYPE_PRECISION (size_type_node);
2375 : 441 : wide_int x = wi::to_wide (argrange[0][0], szprec);
2376 : 441 : wide_int y = wi::to_wide (argrange[1][0], szprec);
2377 : :
2378 : 441 : wi::overflow_type vflow;
2379 : 441 : wide_int prod = wi::umul (x, y, &vflow);
2380 : :
2381 : 441 : 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 : 437 : 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 : 441 : 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 : 441 : }
2407 : :
2408 : 51795 : if (warned && fn)
2409 : : {
2410 : 214 : location_t fnloc = DECL_SOURCE_LOCATION (fn);
2411 : :
2412 : 214 : if (DECL_IS_UNDECLARED_BUILTIN (fn))
2413 : 64 : inform (loc,
2414 : : "in a call to built-in allocation function %qD", fn);
2415 : : else
2416 : 150 : 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 : 41427 : pass_waccess::check_alloca (gcall *stmt)
2425 : : {
2426 : 41427 : if (m_early_checks_p)
2427 : : return;
2428 : :
2429 : 14496 : if ((warn_vla_limit >= HOST_WIDE_INT_MAX
2430 : 14469 : && warn_alloc_size_limit < warn_vla_limit)
2431 : 14486 : || (warn_alloca_limit >= HOST_WIDE_INT_MAX
2432 : 14423 : && 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 : 4278009 : pass_waccess::check_alloc_size_call (gcall *stmt)
2449 : : {
2450 : 4278009 : if (m_early_checks_p)
2451 : 4226214 : return;
2452 : :
2453 : 1387779 : if (gimple_call_num_args (stmt) < 1)
2454 : : /* Avoid invalid calls to functions without a prototype. */
2455 : : return;
2456 : :
2457 : 1150028 : tree fndecl = gimple_call_fndecl (stmt);
2458 : 1150028 : if (fndecl && gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
2459 : : {
2460 : : /* Alloca is handled separately. */
2461 : 571631 : 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 : 1140753 : tree fntype = gimple_call_fntype (stmt);
2473 : 1140753 : tree fntypeattrs = TYPE_ATTRIBUTES (fntype);
2474 : :
2475 : 1140753 : tree alloc_size = lookup_attribute ("alloc_size", fntypeattrs);
2476 : 1140753 : 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 : 51800 : int idx[2] = { -1, -1 };
2484 : 51800 : tree alloc_args[] = { NULL_TREE, NULL_TREE };
2485 : 51800 : unsigned nargs = gimple_call_num_args (stmt);
2486 : :
2487 : 51800 : tree args = TREE_VALUE (alloc_size);
2488 : 51800 : idx[0] = TREE_INT_CST_LOW (TREE_VALUE (args)) - 1;
2489 : : /* Avoid invalid calls to functions without a prototype. */
2490 : 51800 : if ((unsigned) idx[0] >= nargs)
2491 : : return;
2492 : 51795 : alloc_args[0] = call_arg (stmt, idx[0]);
2493 : 51795 : if (TREE_CHAIN (args))
2494 : : {
2495 : 1265 : idx[1] = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (args))) - 1;
2496 : 1265 : if ((unsigned) idx[1] >= nargs)
2497 : : return;
2498 : 1265 : alloc_args[1] = call_arg (stmt, idx[1]);
2499 : : }
2500 : :
2501 : 51795 : 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 : 2438 : pass_waccess::check_strcat (gcall *stmt)
2508 : : {
2509 : 2438 : if (m_early_checks_p)
2510 : 1716 : return;
2511 : :
2512 : 722 : if (!warn_stringop_overflow && !warn_stringop_overread)
2513 : : return;
2514 : :
2515 : 722 : tree dest = call_arg (stmt, 0);
2516 : 722 : 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 : 722 : access_data data (m_ptr_qry.rvals, stmt, access_read_write, NULL_TREE,
2523 : 722 : true, NULL_TREE, true);
2524 : 722 : const int ost = warn_stringop_overflow ? warn_stringop_overflow - 1 : 1;
2525 : 722 : compute_objsize (src, stmt, ost, &data.src, &m_ptr_qry);
2526 : 722 : tree destsize = compute_objsize (dest, stmt, ost, &data.dst, &m_ptr_qry);
2527 : :
2528 : 722 : 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 : : : 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 : 10233 : pass_waccess::check_stxcpy (gcall *stmt)
2608 : : {
2609 : 10233 : if (m_early_checks_p)
2610 : 7407 : return;
2611 : :
2612 : 2968 : tree dst = call_arg (stmt, 0);
2613 : 2968 : tree src = call_arg (stmt, 1);
2614 : :
2615 : 2968 : tree size;
2616 : 2968 : bool exact;
2617 : 2968 : 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 : 2826 : if (warn_stringop_overflow)
2626 : : {
2627 : 2621 : access_data data (m_ptr_qry.rvals, stmt, access_read_write, NULL_TREE,
2628 : 2621 : true, NULL_TREE, true);
2629 : 2621 : const int ost = warn_stringop_overflow ? warn_stringop_overflow - 1 : 1;
2630 : 2621 : compute_objsize (src, stmt, ost, &data.src, &m_ptr_qry);
2631 : 2621 : tree dstsize = compute_objsize (dst, stmt, ost, &data.dst, &m_ptr_qry);
2632 : 2621 : 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 : 2826 : tree fndecl = get_callee_fndecl (stmt);
2641 : 2826 : 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 : 8553 : pass_waccess::check_stxncpy (gcall *stmt)
2649 : : {
2650 : 8553 : if (m_early_checks_p || !warn_stringop_overflow)
2651 : 5970 : return;
2652 : :
2653 : 2583 : tree dst = call_arg (stmt, 0);
2654 : 2583 : tree src = call_arg (stmt, 1);
2655 : : /* The number of bytes to write (not the maximum). */
2656 : 2583 : tree len = call_arg (stmt, 2);
2657 : :
2658 : 2583 : access_data data (m_ptr_qry.rvals, stmt, access_read_write, len, true, len,
2659 : 2583 : true);
2660 : 2583 : const int ost = warn_stringop_overflow ? warn_stringop_overflow - 1 : 1;
2661 : 2583 : compute_objsize (src, stmt, ost, &data.src, &m_ptr_qry);
2662 : 2583 : tree dstsize = compute_objsize (dst, stmt, ost, &data.dst, &m_ptr_qry);
2663 : :
2664 : 2583 : 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 : 8455 : pass_waccess::check_strncmp (gcall *stmt)
2673 : : {
2674 : 8455 : if (m_early_checks_p || !warn_stringop_overread)
2675 : 6364 : return;
2676 : :
2677 : 2834 : tree arg1 = call_arg (stmt, 0);
2678 : 2834 : tree arg2 = call_arg (stmt, 1);
2679 : 2834 : tree bound = call_arg (stmt, 2);
2680 : :
2681 : : /* First check each argument separately, considering the bound. */
2682 : 2834 : if (!check_nul_terminated_array (stmt, arg1, bound)
2683 : 2834 : || !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 : 2828 : c_strlen_data lendata1{ }, lendata2{ };
2695 : 2828 : tree len1 = c_strlen (arg1, 1, &lendata1);
2696 : 2828 : tree len2 = c_strlen (arg2, 1, &lendata2);
2697 : :
2698 : 2828 : if (len1 && TREE_CODE (len1) != INTEGER_CST)
2699 : 2561 : len1 = NULL_TREE;
2700 : 2828 : if (len2 && TREE_CODE (len2) != INTEGER_CST)
2701 : 1556 : len2 = NULL_TREE;
2702 : :
2703 : 2828 : 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 : 2711 : if (maybe_warn_nonstring_arg (get_callee_fndecl (stmt), stmt))
2713 : : return;
2714 : :
2715 : 2538 : access_data adata1 (m_ptr_qry.rvals, stmt, access_read_only, NULL_TREE, false,
2716 : 2538 : bound, true);
2717 : 2538 : access_data adata2 (m_ptr_qry.rvals, stmt, access_read_only, NULL_TREE, false,
2718 : 2538 : 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 : 2538 : tree bndrng[2] = { NULL_TREE, NULL_TREE };
2723 : 2538 : get_size_range (m_ptr_qry.rvals, bound, stmt, bndrng, 0, adata1.src_bndrng);
2724 : 2538 : if (!bndrng[0] || integer_zerop (bndrng[0]))
2725 : 447 : return;
2726 : :
2727 : 2091 : if (len1 && tree_int_cst_lt (len1, bndrng[0]))
2728 : 20 : bndrng[0] = len1;
2729 : 2091 : 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 : 2091 : if (!compute_objsize (arg1, stmt, 1, &adata1.src, &m_ptr_qry)
2735 : 2091 : || !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 : 2091 : offset_int rem1 = adata1.src.size_remaining ();
2741 : 2091 : 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 : 4179 : if (rem1 == 0 || (rem1 < rem2 && lendata1.decl))
2748 : 5 : rem2 = rem1;
2749 : 4171 : 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 : 2091 : access_data *pad = len1 ? &adata2 : &adata1;
2755 : 2091 : offset_int maxrem = wi::max (rem1, rem2, UNSIGNED);
2756 : 2089 : if (lendata1.decl || lendata2.decl
2757 : 4178 : || 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 : 851352 : pass_waccess::check_memop_access (gimple *stmt, tree dest, tree src, tree size)
2778 : : {
2779 : 851352 : if (m_early_checks_p)
2780 : 512269 : 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 : 339083 : access_data data (m_ptr_qry.rvals, stmt, access_read_write);
2787 : 339083 : tree srcsize
2788 : 339083 : = src ? compute_objsize (src, stmt, 0, &data.src, &m_ptr_qry) : NULL_TREE;
2789 : 339083 : tree dstsize = compute_objsize (dest, stmt, 0, &data.dst, &m_ptr_qry);
2790 : :
2791 : 339083 : 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 : 1405277 : pass_waccess::check_read_access (gimple *stmt, tree src,
2800 : : tree bound /* = NULL_TREE */,
2801 : : int ost /* = 1 */)
2802 : : {
2803 : 1405277 : if (m_early_checks_p || !warn_stringop_overread)
2804 : 1005957 : return;
2805 : :
2806 : 399320 : if (bound && !useless_type_conversion_p (size_type_node, TREE_TYPE (bound)))
2807 : 0 : bound = fold_convert (size_type_node, bound);
2808 : :
2809 : 399320 : tree fndecl = get_callee_fndecl (stmt);
2810 : 399320 : maybe_warn_nonstring_arg (fndecl, stmt);
2811 : :
2812 : 399320 : access_data data (m_ptr_qry.rvals, stmt, access_read_only, NULL_TREE,
2813 : 399320 : false, bound, true);
2814 : 399320 : compute_objsize (src, stmt, ost, &data.src, &m_ptr_qry);
2815 : 399320 : 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 : 352022 : memmodel_to_uhwi (tree ord, gimple *stmt, unsigned HOST_WIDE_INT *cstval)
2826 : : {
2827 : 352022 : unsigned HOST_WIDE_INT val;
2828 : :
2829 : 352022 : if (TREE_CODE (ord) == INTEGER_CST)
2830 : : {
2831 : 348764 : if (!tree_fits_uhwi_p (ord))
2832 : : return false;
2833 : 348756 : 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 : 3258 : int_range_max rng (TREE_TYPE (ord));
2840 : 6516 : if (!get_range_query (cfun)->range_of_expr (rng, ord, stmt)
2841 : 3258 : || !rng.singleton_p (&ord))
2842 : 2805 : 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 : 3258 : }
2850 : :
2851 : 349209 : if (targetm.memmodel_check)
2852 : : /* This might warn for an invalid VAL but return a conservatively
2853 : : valid result. */
2854 : 349209 : 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 : 349209 : *cstval = val;
2868 : :
2869 : 349209 : 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 : 324787 : pass_waccess::maybe_warn_memmodel (gimple *stmt, tree ord_sucs,
2923 : : tree ord_fail, const unsigned char *valid)
2924 : : {
2925 : 324787 : unsigned HOST_WIDE_INT sucs, fail = 0;
2926 : 324787 : if (!memmodel_to_uhwi (ord_sucs, stmt, &sucs)
2927 : 324787 : || (ord_fail && !memmodel_to_uhwi (ord_fail, stmt, &fail)))
2928 : 2813 : return false;
2929 : :
2930 : 321974 : bool is_valid = false;
2931 : 321974 : if (valid)
2932 : 701147 : for (unsigned i = 0; valid[i] != UCHAR_MAX; ++i)
2933 : : {
2934 : 701065 : memmodel model = memory_models[valid[i]].modval;
2935 : 701065 : if (memmodel_base (sucs) == model)
2936 : : {
2937 : : is_valid = true;
2938 : : break;
2939 : : }
2940 : : }
2941 : : else
2942 : : is_valid = true;
2943 : :
2944 : 321974 : tree fndecl = gimple_call_fndecl (stmt);
2945 : 321974 : location_t loc = gimple_location (stmt);
2946 : 321974 : loc = expansion_point_location_if_in_system_header (loc);
2947 : :
2948 : 321974 : 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 : 321892 : if (!ord_fail)
2977 : : return false;
2978 : :
2979 : 27223 : 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 : 27163 : 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 : 324848 : pass_waccess::check_atomic_memmodel (gimple *stmt, tree ord_sucs,
3039 : : tree ord_fail, const unsigned char *valid)
3040 : : {
3041 : 324848 : if (warning_suppressed_p (stmt, OPT_Winvalid_memory_model))
3042 : : return;
3043 : :
3044 : 324787 : 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 : 3775230 : pass_waccess::check_atomic_builtin (gcall *stmt)
3054 : : {
3055 : 3775230 : tree callee = gimple_call_fndecl (stmt);
3056 : 3775230 : 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 : 3775230 : unsigned bytes = 0, arg2 = UINT_MAX;
3062 : 3775230 : unsigned sucs_arg = UINT_MAX, fail_arg = UINT_MAX;
3063 : : /* Points to the array of indices of valid memory models. */
3064 : 3775230 : const unsigned char *pvalid_models = NULL;
3065 : :
3066 : 3775230 : 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 : 93801 : case BUILTIN_ACCESS_SIZE_FNSPEC (1);
3126 : 5632 : break;
3127 : 23882 : case BUILTIN_ACCESS_SIZE_FNSPEC (2);
3128 : 1898 : break;
3129 : 123658 : case BUILTIN_ACCESS_SIZE_FNSPEC (4);
3130 : 6803 : break;
3131 : 97836 : case BUILTIN_ACCESS_SIZE_FNSPEC (8);
3132 : 8691 : break;
3133 : 21357 : case BUILTIN_ACCESS_SIZE_FNSPEC (16);
3134 : 4693 : break;
3135 : :
3136 : 160 : case BUILT_IN_ATOMIC_CLEAR:
3137 : 160 : sucs_arg = 1;
3138 : 160 : pvalid_models = flag_clr_models;
3139 : 160 : break;
3140 : :
3141 : : default:
3142 : : return false;
3143 : : }
3144 : :
3145 : 345781 : unsigned nargs = gimple_call_num_args (stmt);
3146 : 345781 : if (sucs_arg < nargs)
3147 : : {
3148 : 324848 : tree ord_sucs = gimple_call_arg (stmt, sucs_arg);
3149 : 324848 : tree ord_fail = NULL_TREE;
3150 : 324848 : if (fail_arg < nargs)
3151 : 27717 : ord_fail = gimple_call_arg (stmt, fail_arg);
3152 : 324848 : check_atomic_memmodel (stmt, ord_sucs, ord_fail, pvalid_models);
3153 : : }
3154 : :
3155 : 345781 : if (!bytes)
3156 : : return true;
3157 : :
3158 : 345621 : tree size = build_int_cstu (sizetype, bytes);
3159 : 345621 : tree dst = gimple_call_arg (stmt, 0);
3160 : 345621 : check_memop_access (stmt, dst, NULL_TREE, size);
3161 : :
3162 : 345621 : if (arg2 != UINT_MAX)
3163 : : {
3164 : 27717 : tree dst = gimple_call_arg (stmt, arg2);
3165 : 27717 : 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 : 5246883 : pass_waccess::check_builtin (gcall *stmt)
3176 : : {
3177 : 5246883 : tree callee = gimple_call_fndecl (stmt);
3178 : 5246883 : if (!callee)
3179 : : return false;
3180 : :
3181 : 5246883 : switch (DECL_FUNCTION_CODE (callee))
3182 : : {
3183 : 41427 : case BUILT_IN_ALLOCA:
3184 : 41427 : case BUILT_IN_ALLOCA_WITH_ALIGN:
3185 : 41427 : case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX:
3186 : 41427 : check_alloca (stmt);
3187 : 41427 : 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 : 177494 : case BUILT_IN_FREE:
3199 : 177494 : case BUILT_IN_REALLOC:
3200 : 177494 : if (!m_early_checks_p)
3201 : : {
3202 : 62648 : tree arg = call_arg (stmt, 0);
3203 : 62648 : if (TREE_CODE (arg) == SSA_NAME)
3204 : 62564 : check_pointer_uses (stmt, arg);
3205 : : }
3206 : : return true;
3207 : :
3208 : 13111 : case BUILT_IN_GETTEXT:
3209 : 13111 : case BUILT_IN_PUTS:
3210 : 13111 : case BUILT_IN_PUTS_UNLOCKED:
3211 : 13111 : case BUILT_IN_STRDUP:
3212 : 13111 : check_read_access (stmt, call_arg (stmt, 0));
3213 : 13111 : return true;
3214 : :
3215 : 51067 : case BUILT_IN_INDEX:
3216 : 51067 : case BUILT_IN_RINDEX:
3217 : 51067 : case BUILT_IN_STRCHR:
3218 : 51067 : case BUILT_IN_STRRCHR:
3219 : 51067 : case BUILT_IN_STRLEN:
3220 : 51067 : check_read_access (stmt, call_arg (stmt, 0));
3221 : 51067 : 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 : 2438 : case BUILT_IN_STRCAT:
3238 : 2438 : check_strcat (stmt);
3239 : 2438 : return true;
3240 : :
3241 : 2450 : case BUILT_IN_STRNCAT:
3242 : 2450 : check_strncat (stmt);
3243 : 2450 : return true;
3244 : :
3245 : 10233 : case BUILT_IN_STPCPY:
3246 : 10233 : case BUILT_IN_STRCPY:
3247 : 10233 : check_stxcpy (stmt);
3248 : 10233 : return true;
3249 : :
3250 : 8553 : case BUILT_IN_STPNCPY:
3251 : 8553 : case BUILT_IN_STRNCPY:
3252 : 8553 : check_stxncpy (stmt);
3253 : 8553 : return true;
3254 : :
3255 : 388025 : case BUILT_IN_STRCASECMP:
3256 : 388025 : case BUILT_IN_STRCMP:
3257 : 388025 : case BUILT_IN_STRPBRK:
3258 : 388025 : case BUILT_IN_STRSPN:
3259 : 388025 : case BUILT_IN_STRCSPN:
3260 : 388025 : case BUILT_IN_STRSTR:
3261 : 388025 : check_read_access (stmt, call_arg (stmt, 0));
3262 : 388025 : check_read_access (stmt, call_arg (stmt, 1));
3263 : 388025 : return true;
3264 : :
3265 : 8455 : case BUILT_IN_STRNCASECMP:
3266 : 8455 : case BUILT_IN_STRNCMP:
3267 : 8455 : check_strncmp (stmt);
3268 : 8455 : return true;
3269 : :
3270 : 274663 : case BUILT_IN_MEMCMP:
3271 : 274663 : {
3272 : 274663 : tree a1 = call_arg (stmt, 0);
3273 : 274663 : tree a2 = call_arg (stmt, 1);
3274 : 274663 : tree len = call_arg (stmt, 2);
3275 : 274663 : check_read_access (stmt, a1, len, 0);
3276 : 274663 : check_read_access (stmt, a2, len, 0);
3277 : 274663 : return true;
3278 : : }
3279 : :
3280 : 272464 : case BUILT_IN_MEMCPY:
3281 : 272464 : case BUILT_IN_MEMPCPY:
3282 : 272464 : case BUILT_IN_MEMMOVE:
3283 : 272464 : {
3284 : 272464 : tree dst = call_arg (stmt, 0);
3285 : 272464 : tree src = call_arg (stmt, 1);
3286 : 272464 : tree len = call_arg (stmt, 2);
3287 : 272464 : check_memop_access (stmt, dst, src, len);
3288 : 272464 : return true;
3289 : : }
3290 : :
3291 : 7869 : case BUILT_IN_MEMCHR:
3292 : 7869 : {
3293 : 7869 : tree src = call_arg (stmt, 0);
3294 : 7869 : tree len = call_arg (stmt, 2);
3295 : 7869 : check_read_access (stmt, src, len, 0);
3296 : 7869 : return true;
3297 : : }
3298 : :
3299 : 205550 : case BUILT_IN_MEMSET:
3300 : 205550 : {
3301 : 205550 : tree dst = call_arg (stmt, 0);
3302 : 205550 : tree len = call_arg (stmt, 2);
3303 : 205550 : check_memop_access (stmt, dst, NULL_TREE, len);
3304 : 205550 : return true;
3305 : : }
3306 : :
3307 : 3775230 : default:
3308 : 3775230 : 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 : 86925 : fntype_argno_type (tree fntype, unsigned argno)
3321 : : {
3322 : 86925 : if (!prototype_p (fntype))
3323 : : return NULL_TREE;
3324 : :
3325 : 86921 : tree argtype;
3326 : 86921 : function_args_iterator it;
3327 : 173630 : FOREACH_FUNCTION_ARGS (fntype, argtype, it)
3328 : 173630 : 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 : 4278009 : pass_waccess::maybe_check_access_sizes (rdwr_map *rwm, tree fndecl, tree fntype,
3356 : : gimple *stmt)
3357 : : {
3358 : 4278009 : if (warning_suppressed_p (stmt, OPT_Wnonnull)
3359 : 4278009 : || warning_suppressed_p (stmt, OPT_Wstringop_overflow_))
3360 : 22292 : return;
3361 : :
3362 : 4255721 : 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 : 4255721 : 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 : 4255721 : char attrstr[80];
3372 : 4255721 : attrstr[0] = 0;
3373 : :
3374 : 4434089 : for (rdwr_map::iterator it = rwm->begin (); it != rwm->end (); ++it)
3375 : : {
3376 : 89188 : 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 : 89188 : const int ptridx = access.second.ptrarg;
3384 : 89188 : const int sizidx = access.second.sizarg;
3385 : :
3386 : 89188 : gcc_assert (ptridx != -1);
3387 : 89188 : 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 : 89188 : if (!access.second.ptr)
3393 : 2294 : continue;
3394 : :
3395 : 86925 : tree ptrtype = fntype_argno_type (fntype, ptridx);
3396 : 86925 : 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 : 86921 : tree argtype = TREE_TYPE (ptrtype);
3403 : :
3404 : : /* The size of the access by the call in elements. */
3405 : 86921 : tree access_nelts;
3406 : 86921 : 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 : 84658 : if (access.second.minsize
3414 : 84658 : && access.second.minsize != HOST_WIDE_INT_M1U)
3415 : 71781 : access_nelts = build_int_cstu (sizetype, access.second.minsize);
3416 : 12877 : 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 : 386 : access_nelts = size_zero_node;
3420 : : else
3421 : 12491 : access_nelts = size_one_node;
3422 : : }
3423 : : else
3424 : 2263 : 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 : 86921 : if (access_nelts
3430 : 86921 : && INTEGRAL_TYPE_P (TREE_TYPE (access_nelts))
3431 : 173842 : && (TYPE_PRECISION (TREE_TYPE (access_nelts))
3432 : 86921 : > 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 : 86921 : char sizstr[80];
3437 : 86921 : tree sizrng[2] = { size_zero_node, build_all_ones_cst (sizetype) };
3438 : 86921 : if (get_size_range (m_ptr_qry.rvals, access_nelts, stmt, sizrng, 1))
3439 : : {
3440 : 86921 : char *s0 = print_generic_expr_to_str (sizrng[0]);
3441 : 86921 : if (tree_int_cst_equal (sizrng[0], sizrng[1]))
3442 : : {
3443 : 86300 : gcc_checking_assert (strlen (s0) < sizeof sizstr);
3444 : 86300 : strcpy (sizstr, s0);
3445 : : }
3446 : : else
3447 : : {
3448 : 621 : char *s1 = print_generic_expr_to_str (sizrng[1]);
3449 : 621 : gcc_checking_assert (strlen (s0) + strlen (s1)
3450 : : < sizeof sizstr - 4);
3451 : 621 : sprintf (sizstr, "[%.37s, %.37s]", s0, s1);
3452 : 621 : free (s1);
3453 : : }
3454 : 86921 : free (s0);
3455 : : }
3456 : : else
3457 : 0 : *sizstr = '\0';
3458 : :
3459 : : /* Set if a warning has been issued for the current argument. */
3460 : 86921 : opt_code arg_warned = no_warning;
3461 : 86921 : location_t loc = get_location (stmt);
3462 : 86921 : tree ptr = access.second.ptr;
3463 : 86921 : if (*sizstr
3464 : 86921 : && tree_int_cst_sgn (sizrng[0]) < 0
3465 : 87116 : && 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 : 86898 : tree access_size = NULL_TREE;
3498 : 86898 : if (tree_int_cst_sgn (sizrng[0]) >= 0)
3499 : : {
3500 : 86726 : 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 : 82838 : if (tree argsize = TYPE_SIZE_UNIT (argtype))
3506 : 82838 : if (TREE_CODE (argsize) == INTEGER_CST)
3507 : : {
3508 : 82559 : const int prec = TYPE_PRECISION (sizetype);
3509 : 82559 : wide_int minsize = wi::to_wide (sizrng[0], prec);
3510 : 82559 : minsize *= wi::to_wide (argsize, prec);
3511 : 82559 : access_size = wide_int_to_tree (sizetype, minsize);
3512 : 82559 : }
3513 : : }
3514 : : else
3515 : : access_size = access_nelts;
3516 : : }
3517 : :
3518 : 86898 : 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 : 86890 : access_data data (m_ptr_qry.rvals, stmt, access.second.mode,
3546 : 86890 : NULL_TREE, false, NULL_TREE, false);
3547 : 85589 : access_ref* const pobj = (access.second.mode == access_write_only
3548 : 86890 : ? &data.dst : &data.src);
3549 : 86890 : tree objsize = compute_objsize (ptr, stmt, 1, pobj, &m_ptr_qry);
3550 : :
3551 : : /* The size of the destination or source object. */
3552 : 86890 : tree dstsize = NULL_TREE, srcsize = NULL_TREE;
3553 : 86890 : 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 : 1836 : srcsize = objsize;
3560 : 1836 : 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 : 1836 : 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 : 86890 : suppress_warning (stmt, OPT_Wstringop_overflow_, false);
3577 : 86890 : access_mode mode = data.mode;
3578 : 86890 : if (mode == access_deferred)
3579 : 83177 : mode = TYPE_READONLY (argtype) ? access_read_only : access_read_write;
3580 : 86890 : check_access (stmt, access_size, /*maxread=*/ NULL_TREE, srcsize,
3581 : : dstsize, mode, &data, m_ptr_qry.rvals);
3582 : :
3583 : 86890 : if (warning_suppressed_p (stmt, OPT_Wstringop_overflow_))
3584 : : opt_warned = OPT_Wstringop_overflow_;
3585 : 86683 : if (opt_warned != no_warning)
3586 : : {
3587 : 322 : if (access.second.internal_p)
3588 : : {
3589 : 73 : 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 : 4255717 : 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 : 4255572 : 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 : 4255721 : }
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 : 23034268 : pass_waccess::check_call_access (gcall *stmt)
3633 : : {
3634 : 23034268 : tree fntype = gimple_call_fntype (stmt);
3635 : 22372146 : if (!fntype)
3636 : : return false;
3637 : :
3638 : 22372146 : tree fntypeattrs = TYPE_ATTRIBUTES (fntype);
3639 : 22372146 : if (!fntypeattrs)
3640 : : return false;
3641 : :
3642 : : /* Map of attribute access specifications for function arguments. */
3643 : 4278009 : rdwr_map rdwr_idx;
3644 : 4278009 : init_attr_rdwr_indices (&rdwr_idx, fntypeattrs);
3645 : :
3646 : 4278009 : unsigned nargs = call_nargs (stmt);
3647 : 13203878 : for (unsigned i = 0; i != nargs; ++i)
3648 : : {
3649 : 8925869 : tree arg = call_arg (stmt, i);
3650 : :
3651 : : /* Save the actual argument that corresponds to the access attribute
3652 : : operand for later processing. */
3653 : 8925869 : if (attr_access *access = rdwr_idx.get (i))
3654 : : {
3655 : 90016 : if (POINTER_TYPE_P (TREE_TYPE (arg)))
3656 : : {
3657 : 87656 : access->ptr = arg;
3658 : : /* A nonnull ACCESS->SIZE contains VLA bounds. */
3659 : : }
3660 : : else
3661 : : {
3662 : 2360 : access->size = arg;
3663 : 2360 : gcc_assert (access->ptr == NULL_TREE);
3664 : : }
3665 : : }
3666 : : }
3667 : :
3668 : : /* Check attribute access arguments. */
3669 : 4278009 : tree fndecl = gimple_call_fndecl (stmt);
3670 : 4278009 : maybe_check_access_sizes (&rdwr_idx, fndecl, fntype, stmt);
3671 : :
3672 : 4278009 : check_alloc_size_call (stmt);
3673 : 4278009 : return true;
3674 : 4278009 : }
3675 : :
3676 : : /* Check arguments in a call STMT for attribute nonstring. */
3677 : :
3678 : : static void
3679 : 6874272 : check_nonstring_args (gcall *stmt)
3680 : : {
3681 : 6874272 : tree fndecl = gimple_call_fndecl (stmt);
3682 : :
3683 : : /* Detect passing non-string arguments to functions expecting
3684 : : nul-terminated strings. */
3685 : 6874272 : maybe_warn_nonstring_arg (fndecl, stmt);
3686 : 6874272 : }
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 : 6874272 : pass_waccess::maybe_check_dealloc_call (gcall *call)
3695 : : {
3696 : 6874272 : tree fndecl = gimple_call_fndecl (call);
3697 : 6874272 : if (!fndecl)
3698 : 6671980 : return;
3699 : :
3700 : 6494951 : unsigned argno = fndecl_dealloc_argno (fndecl);
3701 : 6494951 : if ((unsigned) call_nargs (call) <= argno)
3702 : : return;
3703 : :
3704 : 202970 : tree ptr = gimple_call_arg (call, argno);
3705 : 202970 : if (integer_zerop (ptr))
3706 : : return;
3707 : :
3708 : 202870 : access_ref aref;
3709 : 202870 : if (!compute_objsize (ptr, call, 0, &aref, &m_ptr_qry))
3710 : : return;
3711 : :
3712 : 202870 : tree ref = aref.ref;
3713 : 202870 : if (integer_zerop (ref))
3714 : : return;
3715 : :
3716 : 202766 : tree dealloc_decl = fndecl;
3717 : 202766 : location_t loc = gimple_location (call);
3718 : :
3719 : 202766 : if (DECL_P (ref) || EXPR_P (ref))
3720 : : {
3721 : : /* Diagnose freeing a declared object. */
3722 : 115321 : 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 : 115181 : if (aref.sizrng[0] != aref.sizrng[1]
3738 : 115181 : && warn_dealloc_offset (loc, call, aref))
3739 : : return;
3740 : : }
3741 : 87445 : 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 : 87407 : 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 : 87407 : gimple *def_stmt = SSA_NAME_DEF_STMT (ref);
3765 : 87407 : if (!def_stmt)
3766 : : return;
3767 : :
3768 : 87407 : if (is_gimple_call (def_stmt))
3769 : : {
3770 : 44302 : auto_diagnostic_group d;
3771 : 44302 : bool warned = false;
3772 : 44302 : if (gimple_call_alloc_p (def_stmt))
3773 : : {
3774 : 40684 : if (matching_alloc_calls_p (def_stmt, dealloc_decl))
3775 : : {
3776 : 40268 : if (warn_dealloc_offset (loc, call, aref))
3777 : : return;
3778 : : }
3779 : : else
3780 : : {
3781 : 416 : tree alloc_decl = gimple_call_fndecl (def_stmt);
3782 : 416 : const opt_code opt =
3783 : 416 : (DECL_IS_OPERATOR_NEW_P (alloc_decl)
3784 : 172 : || DECL_IS_OPERATOR_DELETE_P (dealloc_decl)
3785 : 416 : ? OPT_Wmismatched_new_delete
3786 : : : OPT_Wmismatched_dealloc);
3787 : 416 : warned = warning_at (loc, opt,
3788 : : "%qD called on pointer returned "
3789 : : "from a mismatched allocation "
3790 : : "function", dealloc_decl);
3791 : : }
3792 : : }
3793 : 3618 : else if (gimple_call_builtin_p (def_stmt, BUILT_IN_ALLOCA)
3794 : 3618 : || gimple_call_builtin_p (def_stmt,
3795 : : BUILT_IN_ALLOCA_WITH_ALIGN))
3796 : 20 : warned = warning_at (loc, OPT_Wfree_nonheap_object,
3797 : : "%qD called on pointer to "
3798 : : "an unallocated object",
3799 : : dealloc_decl);
3800 : 3598 : else if (warn_dealloc_offset (loc, call, aref))
3801 : : return;
3802 : :
3803 : 436 : if (warned)
3804 : : {
3805 : 227 : tree fndecl = gimple_call_fndecl (def_stmt);
3806 : 227 : inform (gimple_location (def_stmt),
3807 : : "returned from %qD", fndecl);
3808 : 227 : return;
3809 : : }
3810 : 44302 : }
3811 : 43105 : else if (gimple_nop_p (def_stmt))
3812 : : {
3813 : 13956 : ref = SSA_NAME_VAR (ref);
3814 : : /* Diagnose freeing a pointer that includes a positive offset. */
3815 : 13956 : if (TREE_CODE (ref) == PARM_DECL
3816 : 13942 : && !aref.deref
3817 : 13942 : && aref.sizrng[0] != aref.sizrng[1]
3818 : 13956 : && aref.offrng[0] > 0 && aref.offrng[1] > 0
3819 : 13962 : && warn_dealloc_offset (loc, call, aref))
3820 : 6 : return;
3821 : : }
3822 : : }
3823 : : }
3824 : :
3825 : : /* Return true if either USE_STMT's basic block (that of a pointer's use)
3826 : : is dominated by INVAL_STMT's (that of a pointer's invalidating statement,
3827 : : which is either a clobber or a deallocation call), or if they're in
3828 : : the same block, USE_STMT follows INVAL_STMT. */
3829 : :
3830 : : bool
3831 : 5618255 : pass_waccess::use_after_inval_p (gimple *inval_stmt, gimple *use_stmt,
3832 : : bool last_block /* = false */)
3833 : : {
3834 : 5618255 : tree clobvar =
3835 : 5618255 : gimple_clobber_p (inval_stmt) ? gimple_assign_lhs (inval_stmt) : NULL_TREE;
3836 : :
3837 : 5618255 : basic_block inval_bb = gimple_bb (inval_stmt);
3838 : 5618255 : basic_block use_bb = gimple_bb (use_stmt);
3839 : :
3840 : 5618255 : if (!inval_bb || !use_bb)
3841 : : return false;
3842 : :
3843 : 5617675 : if (inval_bb != use_bb)
3844 : : {
3845 : 4904017 : if (dominated_by_p (CDI_DOMINATORS, use_bb, inval_bb))
3846 : : return true;
3847 : :
3848 : 4902733 : if (!clobvar || !last_block)
3849 : : return false;
3850 : :
3851 : : /* Proceed only when looking for uses of dangling pointers. */
3852 : 3429701 : auto gsi = gsi_for_stmt (use_stmt);
3853 : :
3854 : : /* A use statement in the last basic block in a function or one that
3855 : : falls through to it is after any other prior clobber of the used
3856 : : variable unless it's followed by a clobber of the same variable. */
3857 : 3429701 : basic_block bb = use_bb;
3858 : 3429701 : while (bb != inval_bb
3859 : 4192010 : && single_succ_p (bb)
3860 : 5822160 : && !(single_succ_edge (bb)->flags
3861 : 1346424 : & (EDGE_EH | EDGE_ABNORMAL | EDGE_DFS_BACK)))
3862 : : {
3863 : 11170042 : for (; !gsi_end_p (gsi); gsi_next_nondebug (&gsi))
3864 : : {
3865 : 10124007 : gimple *stmt = gsi_stmt (gsi);
3866 : 10124007 : if (gimple_clobber_p (stmt))
3867 : : {
3868 : 448357 : if (clobvar == gimple_assign_lhs (stmt))
3869 : : /* The use is followed by a clobber. */
3870 : : return false;
3871 : : }
3872 : : }
3873 : :
3874 : 1046035 : bb = single_succ (bb);
3875 : 2092070 : gsi = gsi_start_bb (bb);
3876 : : }
3877 : :
3878 : : /* The use is one of a dangling pointer if a clobber of the variable
3879 : : [the pointer points to] has not been found before the function exit
3880 : : point. */
3881 : 3413061 : return bb == EXIT_BLOCK_PTR_FOR_FN (cfun);
3882 : : }
3883 : :
3884 : 713658 : if (bitmap_set_bit (m_bb_uids_set, inval_bb->index))
3885 : : /* The first time this basic block is visited assign increasing ids
3886 : : to consecutive statements in it. Use the ids to determine which
3887 : : precedes which. This avoids the linear traversal on subsequent
3888 : : visits to the same block. */
3889 : 374081 : renumber_gimple_stmt_uids_in_block (m_func, inval_bb);
3890 : :
3891 : 713658 : return gimple_uid (inval_stmt) < gimple_uid (use_stmt);
3892 : : }
3893 : :
3894 : : /* Issue a warning for the USE_STMT of pointer or reference REF rendered
3895 : : invalid by INVAL_STMT. REF may be null when it's been optimized away.
3896 : : When nonnull, INVAL_STMT is the deallocation function that rendered
3897 : : the pointer or reference dangling. Otherwise, VAR is the auto variable
3898 : : (including an unnamed temporary such as a compound literal) whose
3899 : : lifetime's rended it dangling. MAYBE is true to issue the "maybe"
3900 : : kind of warning. EQUALITY is true when the pointer is used in
3901 : : an equality expression. */
3902 : :
3903 : : void
3904 : 18603 : pass_waccess::warn_invalid_pointer (tree ref, gimple *use_stmt,
3905 : : gimple *inval_stmt, tree var,
3906 : : bool maybe, bool equality /* = false */)
3907 : : {
3908 : : /* Avoid printing the unhelpful "<unknown>" in the diagnostics. */
3909 : 18603 : if (ref && TREE_CODE (ref) == SSA_NAME)
3910 : : {
3911 : 18576 : tree var = SSA_NAME_VAR (ref);
3912 : 17912 : if (!var)
3913 : : ref = NULL_TREE;
3914 : : /* Don't warn for cases like when a cdtor returns 'this' on ARM. */
3915 : 17912 : else if (warning_suppressed_p (var, OPT_Wuse_after_free))
3916 : : return;
3917 : 17909 : else if (DECL_ARTIFICIAL (var))
3918 : 700 : ref = NULL_TREE;
3919 : : }
3920 : :
3921 : 18600 : location_t use_loc = gimple_location (use_stmt);
3922 : 18600 : if (use_loc == UNKNOWN_LOCATION)
3923 : : {
3924 : 0 : use_loc = m_func->function_end_locus;
3925 : 0 : if (!ref)
3926 : : /* Avoid issuing a warning with no context other than
3927 : : the function. That would make it difficult to debug
3928 : : in any but very simple cases. */
3929 : : return;
3930 : : }
3931 : :
3932 : 18600 : if (is_gimple_call (inval_stmt))
3933 : : {
3934 : 18409 : if (!m_early_checks_p
3935 : 6460 : || (equality && warn_use_after_free < 3)
3936 : 5879 : || (maybe && warn_use_after_free < 2)
3937 : 24108 : || warning_suppressed_p (use_stmt, OPT_Wuse_after_free))
3938 : 12767 : return;
3939 : :
3940 : 5642 : const tree inval_decl = gimple_call_fndecl (inval_stmt);
3941 : :
3942 : 5642 : auto_diagnostic_group d;
3943 : 11152 : if ((ref && warning_at (use_loc, OPT_Wuse_after_free,
3944 : : (maybe
3945 : : ? G_("pointer %qE may be used after %qD")
3946 : : : G_("pointer %qE used after %qD")),
3947 : : ref, inval_decl))
3948 : 10928 : || (!ref && warning_at (use_loc, OPT_Wuse_after_free,
3949 : : (maybe
3950 : : ? G_("pointer may be used after %qD")
3951 : : : G_("pointer used after %qD")),
3952 : : inval_decl)))
3953 : : {
3954 : 359 : location_t loc = gimple_location (inval_stmt);
3955 : 359 : inform (loc, "call to %qD here", inval_decl);
3956 : 359 : suppress_warning (use_stmt, OPT_Wuse_after_free);
3957 : : }
3958 : 5642 : return;
3959 : 5642 : }
3960 : :
3961 : 191 : if (equality
3962 : 185 : || (maybe && warn_dangling_pointer < 2)
3963 : 371 : || warning_suppressed_p (use_stmt, OPT_Wdangling_pointer_))
3964 : 66 : return;
3965 : :
3966 : 125 : if (DECL_NAME (var))
3967 : : {
3968 : 111 : auto_diagnostic_group d;
3969 : 111 : if ((ref
3970 : 174 : && warning_at (use_loc, OPT_Wdangling_pointer_,
3971 : : (maybe
3972 : : ? G_("dangling pointer %qE to %qD may be used")
3973 : : : G_("using dangling pointer %qE to %qD")),
3974 : : ref, var))
3975 : 111 : || (!ref
3976 : 22 : && warning_at (use_loc, OPT_Wdangling_pointer_,
3977 : : (maybe
3978 : : ? G_("dangling pointer to %qD may be used")
3979 : : : G_("using a dangling pointer to %qD")),
3980 : : var)))
3981 : 98 : inform (DECL_SOURCE_LOCATION (var),
3982 : : "%qD declared here", var);
3983 : 111 : suppress_warning (use_stmt, OPT_Wdangling_pointer_);
3984 : 111 : return;
3985 : 111 : }
3986 : :
3987 : 14 : if ((ref
3988 : 14 : && warning_at (use_loc, OPT_Wdangling_pointer_,
3989 : : (maybe
3990 : : ? G_("dangling pointer %qE to an unnamed temporary "
3991 : : "may be used")
3992 : : : G_("using dangling pointer %qE to an unnamed "
3993 : : "temporary")),
3994 : : ref))
3995 : 14 : || (!ref
3996 : 12 : && warning_at (use_loc, OPT_Wdangling_pointer_,
3997 : : (maybe
3998 : : ? G_("dangling pointer to an unnamed temporary "
3999 : : "may be used")
4000 : : : G_("using a dangling pointer to an unnamed "
4001 : : "temporary")))))
4002 : : {
4003 : 14 : inform (DECL_SOURCE_LOCATION (var),
4004 : : "unnamed temporary defined here");
4005 : 14 : suppress_warning (use_stmt, OPT_Wdangling_pointer_);
4006 : : }
4007 : : }
4008 : :
4009 : : /* If STMT is a call to either the standard realloc or to a user-defined
4010 : : reallocation function returns its LHS and set *PTR to the reallocated
4011 : : pointer. Otherwise return null. */
4012 : :
4013 : : static tree
4014 : 747578 : get_realloc_lhs (gimple *stmt, tree *ptr)
4015 : : {
4016 : 747578 : if (gimple_call_builtin_p (stmt, BUILT_IN_REALLOC))
4017 : : {
4018 : 23222 : *ptr = gimple_call_arg (stmt, 0);
4019 : 23222 : return gimple_call_lhs (stmt);
4020 : : }
4021 : :
4022 : 1266246 : gcall *call = dyn_cast<gcall *>(stmt);
4023 : 542030 : if (!call)
4024 : : return NULL_TREE;
4025 : :
4026 : 542030 : tree fnattr = NULL_TREE;
4027 : 542030 : tree fndecl = gimple_call_fndecl (call);
4028 : 542030 : if (fndecl)
4029 : 542030 : fnattr = DECL_ATTRIBUTES (fndecl);
4030 : : else
4031 : : {
4032 : 0 : tree fntype = gimple_call_fntype (stmt);
4033 : 0 : if (!fntype)
4034 : : return NULL_TREE;
4035 : 0 : fnattr = TYPE_ATTRIBUTES (fntype);
4036 : : }
4037 : :
4038 : 542030 : if (!fnattr)
4039 : : return NULL_TREE;
4040 : :
4041 : 533864 : for (tree ats = fnattr; (ats = lookup_attribute ("*dealloc", ats));
4042 : 16289 : ats = TREE_CHAIN (ats))
4043 : : {
4044 : 16429 : tree args = TREE_VALUE (ats);
4045 : 16429 : if (!args)
4046 : 0 : continue;
4047 : :
4048 : 16429 : tree alloc = TREE_VALUE (args);
4049 : 16429 : if (!alloc)
4050 : 0 : continue;
4051 : :
4052 : 16429 : if (alloc == DECL_NAME (fndecl))
4053 : : {
4054 : 140 : unsigned argno = 0;
4055 : 140 : if (tree index = TREE_CHAIN (args))
4056 : 82 : argno = TREE_INT_CST_LOW (TREE_VALUE (index)) - 1;
4057 : 140 : *ptr = gimple_call_arg (stmt, argno);
4058 : 140 : return gimple_call_lhs (stmt);
4059 : : }
4060 : : }
4061 : :
4062 : : return NULL_TREE;
4063 : : }
4064 : :
4065 : : /* Warn if STMT is a call to a deallocation function that's not a match
4066 : : for the REALLOC_STMT call. Return true if warned. */
4067 : :
4068 : : static bool
4069 : 93038 : maybe_warn_mismatched_realloc (tree ptr, gimple *realloc_stmt, gimple *stmt)
4070 : : {
4071 : 93038 : if (!is_gimple_call (stmt))
4072 : : return false;
4073 : :
4074 : 40309 : tree fndecl = gimple_call_fndecl (stmt);
4075 : 40309 : if (!fndecl)
4076 : : return false;
4077 : :
4078 : 40304 : unsigned argno = fndecl_dealloc_argno (fndecl);
4079 : 40304 : if (call_nargs (stmt) <= argno)
4080 : : return false;
4081 : :
4082 : 2470 : if (matching_alloc_calls_p (realloc_stmt, fndecl))
4083 : : return false;
4084 : :
4085 : : /* Avoid printing the unhelpful "<unknown>" in the diagnostics. */
4086 : 43 : if (ptr && TREE_CODE (ptr) == SSA_NAME
4087 : 86 : && (!SSA_NAME_VAR (ptr) || DECL_ARTIFICIAL (SSA_NAME_VAR (ptr))))
4088 : : ptr = NULL_TREE;
4089 : :
4090 : 43 : location_t loc = gimple_location (stmt);
4091 : 43 : tree realloc_decl = gimple_call_fndecl (realloc_stmt);
4092 : 43 : tree dealloc_decl = gimple_call_fndecl (stmt);
4093 : 43 : if (ptr && !warning_at (loc, OPT_Wmismatched_dealloc,
4094 : : "%qD called on pointer %qE passed to mismatched "
4095 : : "allocation function %qD",
4096 : : dealloc_decl, ptr, realloc_decl))
4097 : 4 : return false;
4098 : 39 : if (!ptr && !warning_at (loc, OPT_Wmismatched_dealloc,
4099 : : "%qD called on a pointer passed to mismatched "
4100 : : "reallocation function %qD",
4101 : : dealloc_decl, realloc_decl))
4102 : 0 : return false;
4103 : :
4104 : 39 : inform (gimple_location (realloc_stmt),
4105 : : "call to %qD", realloc_decl);
4106 : 39 : return true;
4107 : : }
4108 : :
4109 : : /* Return true if P and Q point to the same object, and false if they
4110 : : either don't or their relationship cannot be determined. */
4111 : :
4112 : : static bool
4113 : 92999 : pointers_related_p (gimple *stmt, tree p, tree q, pointer_query &qry,
4114 : : auto_bitmap &visited)
4115 : : {
4116 : 92999 : if (!ptr_derefs_may_alias_p (p, q))
4117 : : return false;
4118 : :
4119 : : /* TODO: Work harder to rule out relatedness. */
4120 : 92999 : access_ref pref, qref;
4121 : 92999 : if (!qry.get_ref (p, stmt, &pref, 0)
4122 : 92999 : || !qry.get_ref (q, stmt, &qref, 0))
4123 : : /* GET_REF() only rarely fails. When it does, it's likely because
4124 : : it involves a self-referential PHI. Return a conservative result. */
4125 : 0 : return false;
4126 : :
4127 : 92999 : if (pref.ref == qref.ref)
4128 : : return true;
4129 : :
4130 : : /* If either pointer is a PHI, iterate over all its operands and
4131 : : return true if they're all related to the other pointer. */
4132 : 108 : tree ptr = q;
4133 : 108 : unsigned version;
4134 : 108 : gphi *phi = pref.phi ();
4135 : 108 : if (phi)
4136 : 108 : version = SSA_NAME_VERSION (pref.ref);
4137 : : else
4138 : : {
4139 : 0 : phi = qref.phi ();
4140 : 0 : if (!phi)
4141 : : return false;
4142 : :
4143 : 0 : ptr = p;
4144 : 0 : version = SSA_NAME_VERSION (qref.ref);
4145 : : }
4146 : :
4147 : 108 : if (!bitmap_set_bit (visited, version))
4148 : : return true;
4149 : :
4150 : 60 : unsigned nargs = gimple_phi_num_args (phi);
4151 : 180 : for (unsigned i = 0; i != nargs; ++i)
4152 : : {
4153 : 120 : tree arg = gimple_phi_arg_def (phi, i);
4154 : 120 : if (!pointers_related_p (stmt, arg, ptr, qry, visited))
4155 : : return false;
4156 : : }
4157 : :
4158 : : return true;
4159 : : }
4160 : :
4161 : : /* Convenience wrapper for the above. */
4162 : :
4163 : : static bool
4164 : 92879 : pointers_related_p (gimple *stmt, tree p, tree q, pointer_query &qry)
4165 : : {
4166 : 92879 : auto_bitmap visited;
4167 : 92879 : return pointers_related_p (stmt, p, q, qry, visited);
4168 : 92879 : }
4169 : :
4170 : : /* For a STMT either a call to a deallocation function or a clobber, warn
4171 : : for uses of the pointer PTR it was called with (including its copies
4172 : : or others derived from it by pointer arithmetic). If STMT is a clobber,
4173 : : VAR is the decl of the clobbered variable. When MAYBE is true use
4174 : : a "maybe" form of diagnostic. */
4175 : :
4176 : : void
4177 : 747578 : pass_waccess::check_pointer_uses (gimple *stmt, tree ptr,
4178 : : tree var /* = NULL_TREE */,
4179 : : bool maybe /* = false */)
4180 : : {
4181 : 747578 : gcc_assert (TREE_CODE (ptr) == SSA_NAME);
4182 : :
4183 : 747578 : const bool check_dangling = !is_gimple_call (stmt);
4184 : 747578 : basic_block stmt_bb = gimple_bb (stmt);
4185 : :
4186 : : /* If STMT is a reallocation function set to the reallocated pointer
4187 : : and the LHS of the call, respectively. */
4188 : 747578 : tree realloc_ptr = NULL_TREE;
4189 : 747578 : tree realloc_lhs = get_realloc_lhs (stmt, &realloc_ptr);
4190 : :
4191 : 747578 : auto_bitmap visited;
4192 : :
4193 : 747578 : auto_vec<tree, 8> pointers;
4194 : 747578 : pointers.quick_push (ptr);
4195 : 747578 : hash_map<tree, int> *phi_map = nullptr;
4196 : :
4197 : : /* Starting with PTR, iterate over POINTERS added by the loop, and
4198 : : either warn for their uses in basic blocks dominated by the STMT
4199 : : or in statements that follow it in the same basic block, or add
4200 : : them to POINTERS if they point into the same object as PTR (i.e.,
4201 : : are obtained by pointer arithmetic on PTR). */
4202 : 3392442 : for (unsigned i = 0; i != pointers.length (); ++i)
4203 : : {
4204 : 948643 : tree ptr = pointers[i];
4205 : 948643 : if (!bitmap_set_bit (visited, SSA_NAME_VERSION (ptr)))
4206 : : /* Avoid revisiting the same pointer. */
4207 : 43 : continue;
4208 : :
4209 : 948600 : use_operand_p use_p;
4210 : 948600 : imm_use_iterator iter;
4211 : 4778708 : FOR_EACH_IMM_USE_FAST (use_p, iter, ptr)
4212 : : {
4213 : 3830108 : gimple *use_stmt = USE_STMT (use_p);
4214 : 3830108 : if (use_stmt == stmt || is_gimple_debug (use_stmt))
4215 : 1753487 : continue;
4216 : :
4217 : : /* A clobber isn't a use. */
4218 : 2076621 : if (gimple_clobber_p (use_stmt))
4219 : 96052 : continue;
4220 : :
4221 : 1980569 : if (realloc_lhs)
4222 : : {
4223 : : /* Check to see if USE_STMT is a mismatched deallocation
4224 : : call for the pointer passed to realloc. That's a bug
4225 : : regardless of the pointer's value and so warn. */
4226 : 93038 : if (maybe_warn_mismatched_realloc (*use_p->use, stmt, use_stmt))
4227 : 159 : continue;
4228 : :
4229 : : /* Pointers passed to realloc that are used in basic blocks
4230 : : where the realloc call is known to have failed are valid.
4231 : : Ignore pointers that nothing is known about. Those could
4232 : : have escaped along with their nullness. */
4233 : 92999 : prange vr;
4234 : 92999 : if (m_ptr_qry.rvals->range_of_expr (vr, realloc_lhs, use_stmt))
4235 : : {
4236 : 92999 : if (vr.zero_p ())
4237 : 120 : continue;
4238 : :
4239 : 92879 : if (!pointers_related_p (stmt, ptr, realloc_ptr, m_ptr_qry))
4240 : 0 : continue;
4241 : : }
4242 : 92999 : }
4243 : :
4244 : 1980619 : if (check_dangling
4245 : 1980410 : && gimple_code (use_stmt) == GIMPLE_RETURN)
4246 : : /* Avoid interfering with -Wreturn-local-addr (which runs only
4247 : : with optimization enabled so it won't diagnose cases that
4248 : : would be caught here when optimization is disabled). */
4249 : 209 : continue;
4250 : :
4251 : 1980201 : bool equality = false;
4252 : 1980201 : if (is_gimple_assign (use_stmt))
4253 : : {
4254 : 1154197 : tree_code code = gimple_assign_rhs_code (use_stmt);
4255 : 1154197 : equality = code == EQ_EXPR || code == NE_EXPR;
4256 : : }
4257 : 826004 : else if (gcond *cond = dyn_cast<gcond *>(use_stmt))
4258 : : {
4259 : 328479 : tree_code code = gimple_cond_code (cond);
4260 : 328479 : equality = code == EQ_EXPR || code == NE_EXPR;
4261 : : }
4262 : 497525 : else if (gphi *phi = dyn_cast <gphi *> (use_stmt))
4263 : : {
4264 : : /* Only add a PHI result to POINTERS if all its
4265 : : operands are related to PTR, otherwise continue. The
4266 : : PHI result is related once we've reached all arguments
4267 : : through this iteration. That also means any invariant
4268 : : argument will make the PHI not related. For arguments
4269 : : flowing over natural loop backedges we are optimistic
4270 : : (and diagnose the first iteration). */
4271 : 119391 : tree lhs = gimple_phi_result (phi);
4272 : 119391 : if (!phi_map)
4273 : 49215 : phi_map = new hash_map<tree, int>;
4274 : 119391 : bool existed_p;
4275 : 119391 : int &related = phi_map->get_or_insert (lhs, &existed_p);
4276 : 119391 : if (!existed_p)
4277 : : {
4278 : 68592 : related = gimple_phi_num_args (phi) - 1;
4279 : 277985 : for (unsigned j = 0; j < gimple_phi_num_args (phi); ++j)
4280 : : {
4281 : 209393 : if ((unsigned) phi_arg_index_from_use (use_p) == j)
4282 : 68592 : continue;
4283 : 140801 : tree arg = gimple_phi_arg_def (phi, j);
4284 : 140801 : edge e = gimple_phi_arg_edge (phi, j);
4285 : 140801 : basic_block arg_bb;
4286 : 140801 : if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest)
4287 : : /* Make sure we are not forward visiting a
4288 : : backedge argument. */
4289 : 140801 : && (TREE_CODE (arg) != SSA_NAME
4290 : 19924 : || (!SSA_NAME_IS_DEFAULT_DEF (arg)
4291 : 19924 : && ((arg_bb
4292 : 19924 : = gimple_bb (SSA_NAME_DEF_STMT (arg)))
4293 : 19924 : != e->dest)
4294 : 16301 : && !dominated_by_p (CDI_DOMINATORS,
4295 : : e->dest, arg_bb))))
4296 : 16302 : related--;
4297 : : }
4298 : : }
4299 : : else
4300 : 50799 : related--;
4301 : :
4302 : 119391 : if (related == 0)
4303 : 27508 : pointers.safe_push (lhs);
4304 : 119391 : continue;
4305 : 119391 : }
4306 : :
4307 : : /* Warn if USE_STMT is dominated by the deallocation STMT.
4308 : : Otherwise, add the pointer to POINTERS so that the uses
4309 : : of any other pointers derived from it can be checked. */
4310 : 1860810 : if (use_after_inval_p (stmt, use_stmt, check_dangling))
4311 : : {
4312 : 18576 : basic_block use_bb = gimple_bb (use_stmt);
4313 : 18576 : bool this_maybe
4314 : : = (maybe
4315 : 18576 : || !dominated_by_p (CDI_POST_DOMINATORS, stmt_bb, use_bb));
4316 : 18576 : warn_invalid_pointer (*use_p->use, use_stmt, stmt, var,
4317 : : this_maybe, equality);
4318 : 18576 : continue;
4319 : 18576 : }
4320 : :
4321 : 1842234 : if (is_gimple_assign (use_stmt))
4322 : : {
4323 : 1152348 : tree lhs = gimple_assign_lhs (use_stmt);
4324 : 1152348 : if (TREE_CODE (lhs) == SSA_NAME)
4325 : : {
4326 : 737992 : tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
4327 : 737992 : if (rhs_code == POINTER_PLUS_EXPR || rhs_code == SSA_NAME)
4328 : 172179 : pointers.safe_push (lhs);
4329 : : }
4330 : 1152348 : continue;
4331 : 1152348 : }
4332 : :
4333 : 4519994 : if (gcall *call = dyn_cast <gcall *>(use_stmt))
4334 : : {
4335 : 360697 : if (gimple_call_return_arg (call) == ptr)
4336 : 39376 : if (tree lhs = gimple_call_lhs (call))
4337 : 1378 : if (TREE_CODE (lhs) == SSA_NAME)
4338 : 1378 : pointers.safe_push (lhs);
4339 : 360697 : continue;
4340 : 360697 : }
4341 : : }
4342 : : }
4343 : :
4344 : 747578 : if (phi_map)
4345 : 49215 : delete phi_map;
4346 : 747578 : }
4347 : :
4348 : : /* Check call STMT for invalid accesses. */
4349 : :
4350 : : void
4351 : 23049099 : pass_waccess::check_call (gcall *stmt)
4352 : : {
4353 : : /* Skip special calls generated by the compiler. */
4354 : 23049099 : if (gimple_call_from_thunk_p (stmt))
4355 : : return;
4356 : :
4357 : : /* .ASAN_MARK doesn't access any vars, only modifies shadow memory. */
4358 : 23045338 : if (gimple_call_internal_p (stmt)
4359 : 23045338 : && gimple_call_internal_fn (stmt) == IFN_ASAN_MARK)
4360 : : return;
4361 : :
4362 : 23034268 : if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
4363 : 5246883 : check_builtin (stmt);
4364 : :
4365 : 23034268 : if (tree callee = gimple_call_fndecl (stmt))
4366 : : {
4367 : : /* Check for uses of the pointer passed to either a standard
4368 : : or a user-defined deallocation function. */
4369 : 21850502 : unsigned argno = fndecl_dealloc_argno (callee);
4370 : 21850502 : if (argno < (unsigned) call_nargs (stmt))
4371 : : {
4372 : 503180 : tree arg = call_arg (stmt, argno);
4373 : 503180 : if (TREE_CODE (arg) == SSA_NAME)
4374 : 502688 : check_pointer_uses (stmt, arg);
4375 : : }
4376 : : }
4377 : :
4378 : 23034268 : check_call_access (stmt);
4379 : 23034268 : check_call_dangling (stmt);
4380 : :
4381 : 23034268 : if (m_early_checks_p)
4382 : : return;
4383 : :
4384 : 6874272 : maybe_check_dealloc_call (stmt);
4385 : 6874272 : check_nonstring_args (stmt);
4386 : : }
4387 : :
4388 : : /* Check non-call STMT for invalid accesses. */
4389 : :
4390 : : void
4391 : 133547748 : pass_waccess::check_stmt (gimple *stmt)
4392 : : {
4393 : 133547748 : if (m_check_dangling_p
4394 : 133547748 : && gimple_clobber_p (stmt, CLOBBER_STORAGE_END))
4395 : : {
4396 : : /* Ignore clobber statements in blocks with exceptional edges. */
4397 : 3524812 : basic_block bb = gimple_bb (stmt);
4398 : 3524812 : edge e = EDGE_PRED (bb, 0);
4399 : 3524812 : if (e->flags & EDGE_EH)
4400 : : return;
4401 : :
4402 : 2726412 : tree var = gimple_assign_lhs (stmt);
4403 : 2726412 : m_clobbers.put (var, stmt);
4404 : 2726412 : return;
4405 : : }
4406 : :
4407 : 130022936 : if (is_gimple_assign (stmt))
4408 : : {
4409 : : /* Clobbered unnamed temporaries such as compound literals can be
4410 : : revived. Check for an assignment to one and remove it from
4411 : : M_CLOBBERS. */
4412 : 96791618 : tree lhs = gimple_assign_lhs (stmt);
4413 : 120494906 : while (handled_component_p (lhs))
4414 : 23703288 : lhs = TREE_OPERAND (lhs, 0);
4415 : :
4416 : 96791618 : if (auto_var_p (lhs))
4417 : 15500019 : m_clobbers.remove (lhs);
4418 : 96791618 : return;
4419 : : }
4420 : :
4421 : 33231318 : if (greturn *ret = dyn_cast <greturn *> (stmt))
4422 : : {
4423 : 5345524 : if (optimize && flag_isolate_erroneous_paths_dereference)
4424 : : /* Avoid interfering with -Wreturn-local-addr (which runs only
4425 : : with optimization enabled). */
4426 : 5345523 : return;
4427 : :
4428 : 1097001 : tree arg = gimple_return_retval (ret);
4429 : 1097001 : if (!arg || TREE_CODE (arg) != ADDR_EXPR)
4430 : : return;
4431 : :
4432 : 313 : arg = TREE_OPERAND (arg, 0);
4433 : 421 : while (handled_component_p (arg))
4434 : 108 : arg = TREE_OPERAND (arg, 0);
4435 : :
4436 : 313 : if (!auto_var_p (arg))
4437 : : return;
4438 : :
4439 : 3 : gimple **pclobber = m_clobbers.get (arg);
4440 : 3 : if (!pclobber)
4441 : : return;
4442 : :
4443 : 1 : if (!use_after_inval_p (*pclobber, stmt))
4444 : : return;
4445 : :
4446 : 1 : warn_invalid_pointer (NULL_TREE, stmt, *pclobber, arg, false);
4447 : : }
4448 : : }
4449 : :
4450 : : /* Check basic block BB for invalid accesses. */
4451 : :
4452 : : void
4453 : 44553590 : pass_waccess::check_block (basic_block bb)
4454 : : {
4455 : : /* Iterate over statements, looking for function calls. */
4456 : 245704027 : for (auto si = gsi_start_bb (bb); !gsi_end_p (si);
4457 : 156596847 : gsi_next_nondebug (&si))
4458 : : {
4459 : 156596847 : gimple *stmt = gsi_stmt (si);
4460 : 156596847 : if (gcall *call = dyn_cast <gcall *> (stmt))
4461 : 23049099 : check_call (call);
4462 : : else
4463 : 133547748 : check_stmt (stmt);
4464 : : }
4465 : 44553590 : }
4466 : :
4467 : : /* Return the argument that the call STMT to a built-in function returns
4468 : : (including with an offset) or null if it doesn't. */
4469 : :
4470 : : tree
4471 : 2302195 : pass_waccess::gimple_call_return_arg (gcall *call)
4472 : : {
4473 : : /* Check for attribute fn spec to see if the function returns one
4474 : : of its arguments. */
4475 : 2302195 : attr_fnspec fnspec = gimple_call_fnspec (call);
4476 : 2302195 : unsigned int argno;
4477 : 2302195 : if (!fnspec.returns_arg (&argno))
4478 : : {
4479 : 2230451 : if (gimple_call_num_args (call) < 1)
4480 : : return NULL_TREE;
4481 : :
4482 : 2161942 : if (!gimple_call_builtin_p (call, BUILT_IN_NORMAL))
4483 : : return NULL_TREE;
4484 : :
4485 : 286211 : tree fndecl = gimple_call_fndecl (call);
4486 : 286211 : switch (DECL_FUNCTION_CODE (fndecl))
4487 : : {
4488 : : case BUILT_IN_MEMPCPY:
4489 : : case BUILT_IN_MEMPCPY_CHK:
4490 : : case BUILT_IN_MEMCHR:
4491 : : case BUILT_IN_STRCHR:
4492 : : case BUILT_IN_STRRCHR:
4493 : : case BUILT_IN_STRSTR:
4494 : : case BUILT_IN_STPCPY:
4495 : : case BUILT_IN_STPCPY_CHK:
4496 : : case BUILT_IN_STPNCPY:
4497 : : case BUILT_IN_STPNCPY_CHK:
4498 : : argno = 0;
4499 : : break;
4500 : :
4501 : : default:
4502 : : return NULL_TREE;
4503 : : }
4504 : : }
4505 : :
4506 : 85921 : if (gimple_call_num_args (call) <= argno)
4507 : : return NULL_TREE;
4508 : :
4509 : 85921 : return gimple_call_arg (call, argno);
4510 : : }
4511 : :
4512 : : /* Check for and diagnose all uses of the dangling pointer VAR to the auto
4513 : : object DECL whose lifetime has ended. OBJREF is true when VAR denotes
4514 : : an access to a DECL that may have been clobbered. */
4515 : :
4516 : : void
4517 : 14806361 : pass_waccess::check_dangling_uses (tree var, tree decl, bool maybe /* = false */,
4518 : : bool objref /* = false */)
4519 : : {
4520 : 14806361 : if (!decl || !auto_var_p (decl))
4521 : 6555374 : return;
4522 : :
4523 : 8250987 : gimple **pclob = m_clobbers.get (decl);
4524 : 8250987 : if (!pclob)
4525 : : return;
4526 : :
4527 : 3742379 : if (!objref)
4528 : : {
4529 : 182326 : check_pointer_uses (*pclob, var, decl, maybe);
4530 : 182326 : return;
4531 : : }
4532 : :
4533 : 3560053 : gimple *use_stmt = SSA_NAME_DEF_STMT (var);
4534 : 3560053 : if (!use_after_inval_p (*pclob, use_stmt, true))
4535 : : return;
4536 : :
4537 : 0 : basic_block use_bb = gimple_bb (use_stmt);
4538 : 0 : basic_block clob_bb = gimple_bb (*pclob);
4539 : 0 : maybe = maybe || !dominated_by_p (CDI_POST_DOMINATORS, clob_bb, use_bb);
4540 : 0 : warn_invalid_pointer (var, use_stmt, *pclob, decl, maybe, false);
4541 : : }
4542 : :
4543 : : /* Diagnose stores in BB and (recursively) its predecessors of the addresses
4544 : : of local variables into nonlocal pointers that are left dangling after
4545 : : the function returns. Returns true when we can continue walking
4546 : : the CFG to predecessors. */
4547 : :
4548 : : bool
4549 : 15828916 : pass_waccess::check_dangling_stores (basic_block bb,
4550 : : hash_set<tree> &stores)
4551 : : {
4552 : : /* Iterate backwards over the statements looking for a store of
4553 : : the address of a local variable into a nonlocal pointer. */
4554 : 53110960 : for (auto gsi = gsi_last_nondebug_bb (bb); ; gsi_prev_nondebug (&gsi))
4555 : : {
4556 : 53110960 : gimple *stmt = gsi_stmt (gsi);
4557 : 53110960 : if (!stmt)
4558 : : break;
4559 : :
4560 : 41178304 : if (warning_suppressed_p (stmt, OPT_Wdangling_pointer_))
4561 : 37281456 : continue;
4562 : :
4563 : 41123402 : if (is_gimple_call (stmt)
4564 : 41123402 : && !(gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE)))
4565 : : /* Avoid looking before nonconst, nonpure calls since those might
4566 : : use the escaped locals. */
4567 : 3896260 : return false;
4568 : :
4569 : 37378040 : if (!is_gimple_assign (stmt) || gimple_clobber_p (stmt)
4570 : 61721547 : || !gimple_store_p (stmt))
4571 : 32102949 : continue;
4572 : :
4573 : 5275091 : access_ref lhs_ref;
4574 : 5275091 : tree lhs = gimple_assign_lhs (stmt);
4575 : 5275091 : if (!m_ptr_qry.get_ref (lhs, stmt, &lhs_ref, 0))
4576 : 9341 : continue;
4577 : :
4578 : 5265750 : if (TREE_CODE (lhs_ref.ref) == MEM_REF)
4579 : : {
4580 : 23057 : lhs_ref.ref = TREE_OPERAND (lhs_ref.ref, 0);
4581 : 23057 : ++lhs_ref.deref;
4582 : : }
4583 : 5265750 : if (TREE_CODE (lhs_ref.ref) == ADDR_EXPR)
4584 : : {
4585 : 9959 : lhs_ref.ref = TREE_OPERAND (lhs_ref.ref, 0);
4586 : 9959 : --lhs_ref.deref;
4587 : : }
4588 : 5265750 : if (TREE_CODE (lhs_ref.ref) == SSA_NAME)
4589 : : {
4590 : 1085009 : gimple *def_stmt = SSA_NAME_DEF_STMT (lhs_ref.ref);
4591 : 1085009 : if (!gimple_nop_p (def_stmt))
4592 : : /* Avoid looking at or before stores into unknown objects. */
4593 : : return false;
4594 : :
4595 : 934111 : lhs_ref.ref = SSA_NAME_VAR (lhs_ref.ref);
4596 : : }
4597 : :
4598 : 5114852 : if (TREE_CODE (lhs_ref.ref) == PARM_DECL
4599 : 5114852 : && (lhs_ref.deref - DECL_BY_REFERENCE (lhs_ref.ref)) > 0)
4600 : : /* Assignment through a (real) pointer/reference parameter. */;
4601 : 7450375 : else if (VAR_P (lhs_ref.ref)
4602 : 4242344 : && !auto_var_p (lhs_ref.ref))
4603 : : /* Assignment to/through a non-local variable. */;
4604 : : else
4605 : : /* Something else, don't warn. */
4606 : 3208031 : continue;
4607 : :
4608 : 1906821 : if (stores.add (lhs_ref.ref))
4609 : 1295960 : continue;
4610 : :
4611 : : /* FIXME: Handle stores of alloca() and VLA. */
4612 : 610861 : access_ref rhs_ref;
4613 : 610861 : tree rhs = gimple_assign_rhs1 (stmt);
4614 : 610861 : if (!m_ptr_qry.get_ref (rhs, stmt, &rhs_ref, 0)
4615 : 610861 : || rhs_ref.deref != -1)
4616 : 583249 : continue;
4617 : :
4618 : 27612 : if (!auto_var_p (rhs_ref.ref))
4619 : 27024 : continue;
4620 : :
4621 : 588 : auto_diagnostic_group d;
4622 : 588 : location_t loc = gimple_location (stmt);
4623 : 588 : if (warning_at (loc, OPT_Wdangling_pointer_,
4624 : : "storing the address of local variable %qD in %qE",
4625 : : rhs_ref.ref, lhs))
4626 : : {
4627 : 45 : suppress_warning (stmt, OPT_Wdangling_pointer_);
4628 : :
4629 : 45 : location_t loc = DECL_SOURCE_LOCATION (rhs_ref.ref);
4630 : 45 : inform (loc, "%qD declared here", rhs_ref.ref);
4631 : :
4632 : 45 : loc = DECL_SOURCE_LOCATION (lhs_ref.ref);
4633 : 45 : inform (loc, "%qD declared here", lhs_ref.ref);
4634 : : }
4635 : 37282632 : }
4636 : :
4637 : 11932656 : return true;
4638 : : }
4639 : :
4640 : : /* Diagnose stores of the addresses of local variables into nonlocal
4641 : : pointers that are left dangling after the function returns. */
4642 : :
4643 : : void
4644 : 3968946 : pass_waccess::check_dangling_stores ()
4645 : : {
4646 : 3968946 : if (EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (m_func)->preds) == 0)
4647 : 58201 : return;
4648 : :
4649 : 3910745 : auto_bitmap bbs;
4650 : 3910745 : hash_set<tree> stores;
4651 : 3910745 : auto_vec<edge_iterator, 8> worklist (n_basic_blocks_for_fn (cfun) + 1);
4652 : 3910745 : worklist.quick_push (ei_start (EXIT_BLOCK_PTR_FOR_FN (m_func)->preds));
4653 : 34430853 : do
4654 : : {
4655 : 34430853 : edge_iterator ei = worklist.last ();
4656 : 34430853 : basic_block src = ei_edge (ei)->src;
4657 : 34430853 : if (bitmap_set_bit (bbs, src->index))
4658 : : {
4659 : 15828916 : if (check_dangling_stores (src, stores)
4660 : 15828916 : && EDGE_COUNT (src->preds) > 0)
4661 : 10355307 : worklist.quick_push (ei_start (src->preds));
4662 : : }
4663 : : else
4664 : : {
4665 : 18601937 : if (ei_one_before_end_p (ei))
4666 : 14266052 : worklist.pop ();
4667 : : else
4668 : 4335885 : ei_next (&worklist.last ());
4669 : : }
4670 : : }
4671 : 68861706 : while (!worklist.is_empty ());
4672 : 3910745 : }
4673 : :
4674 : : /* Check for and diagnose uses of dangling pointers to auto objects
4675 : : whose lifetime has ended. */
4676 : :
4677 : : void
4678 : 3968946 : pass_waccess::check_dangling_uses ()
4679 : : {
4680 : 3968946 : tree var;
4681 : 3968946 : unsigned i;
4682 : 115532151 : FOR_EACH_SSA_NAME (i, var, m_func)
4683 : : {
4684 : : /* For each SSA_NAME pointer VAR find the object it points to.
4685 : : If the object is a clobbered local variable, check to see
4686 : : if any of VAR's uses (or those of other pointers derived
4687 : : from VAR) happens after the clobber. If so, warn. */
4688 : :
4689 : 106976858 : gimple *def_stmt = SSA_NAME_DEF_STMT (var);
4690 : 106976858 : if (is_gimple_assign (def_stmt))
4691 : : {
4692 : 68323172 : tree rhs = gimple_assign_rhs1 (def_stmt);
4693 : 68323172 : if (TREE_CODE (rhs) == ADDR_EXPR)
4694 : : {
4695 : 4662282 : if (!POINTER_TYPE_P (TREE_TYPE (var)))
4696 : 2049525 : continue;
4697 : 2612757 : check_dangling_uses (var, TREE_OPERAND (rhs, 0));
4698 : : }
4699 : : else
4700 : : {
4701 : : /* For other expressions, check the base DECL to see
4702 : : if it's been clobbered, most likely as a result of
4703 : : inlining a reference to it. */
4704 : 63660890 : tree decl = get_base_address (rhs);
4705 : 63660890 : if (DECL_P (decl))
4706 : 11945939 : check_dangling_uses (var, decl, false, true);
4707 : : }
4708 : : }
4709 : 38653686 : else if (POINTER_TYPE_P (TREE_TYPE (var)))
4710 : : {
4711 : 6411040 : if (gcall *call = dyn_cast<gcall *>(def_stmt))
4712 : : {
4713 : 1941498 : if (tree arg = gimple_call_return_arg (call))
4714 : : {
4715 : 28926 : access_ref aref;
4716 : 28926 : if (m_ptr_qry.get_ref (arg, call, &aref, 0)
4717 : 28926 : && aref.deref < 0)
4718 : 9358 : check_dangling_uses (var, aref.ref);
4719 : : }
4720 : : }
4721 : 112179126 : else if (gphi *phi = dyn_cast <gphi *>(def_stmt))
4722 : : {
4723 : 615921 : unsigned nargs = gimple_phi_num_args (phi);
4724 : 2092534 : for (unsigned i = 0; i != nargs; ++i)
4725 : : {
4726 : 1476613 : access_ref aref;
4727 : 1476613 : tree arg = gimple_phi_arg_def (phi, i);
4728 : 1476613 : if (m_ptr_qry.get_ref (arg, phi, &aref, 0)
4729 : 1476613 : && aref.deref < 0)
4730 : 238307 : check_dangling_uses (var, aref.ref, true);
4731 : : }
4732 : : }
4733 : : }
4734 : : }
4735 : 3968946 : }
4736 : :
4737 : : /* Check CALL arguments for dangling pointers (those that have been
4738 : : clobbered) and warn if found. */
4739 : :
4740 : : void
4741 : 23034268 : pass_waccess::check_call_dangling (gcall *call)
4742 : : {
4743 : 23034268 : unsigned nargs = gimple_call_num_args (call);
4744 : 67949286 : for (unsigned i = 0; i != nargs; ++i)
4745 : : {
4746 : 44915018 : tree arg = gimple_call_arg (call, i);
4747 : 44915018 : if (TREE_CODE (arg) != ADDR_EXPR)
4748 : 44914992 : continue;
4749 : :
4750 : 12678832 : arg = TREE_OPERAND (arg, 0);
4751 : 12678832 : if (!DECL_P (arg))
4752 : 4278607 : continue;
4753 : :
4754 : 8400225 : gimple **pclobber = m_clobbers.get (arg);
4755 : 8400225 : if (!pclobber)
4756 : 8202834 : continue;
4757 : :
4758 : 197391 : if (!use_after_inval_p (*pclobber, call))
4759 : 197365 : continue;
4760 : :
4761 : 26 : warn_invalid_pointer (NULL_TREE, call, *pclobber, arg, false);
4762 : : }
4763 : 23034268 : }
4764 : :
4765 : : /* Check function FUN for invalid accesses. */
4766 : :
4767 : : unsigned
4768 : 5434540 : pass_waccess::execute (function *fun)
4769 : : {
4770 : 5434540 : auto_urlify_attributes sentinel;
4771 : :
4772 : 5434540 : calculate_dominance_info (CDI_DOMINATORS);
4773 : 5434540 : calculate_dominance_info (CDI_POST_DOMINATORS);
4774 : :
4775 : : /* Set or clear EDGE_DFS_BACK bits on back edges. */
4776 : 5434540 : mark_dfs_back_edges (fun);
4777 : :
4778 : : /* Create a new ranger instance and associate it with FUN. */
4779 : 5434540 : m_ptr_qry.rvals = enable_ranger (fun);
4780 : 5434540 : m_func = fun;
4781 : :
4782 : : /* Check for dangling pointers in the earliest run of the pass.
4783 : : The latest point -Wdangling-pointer should run is just before
4784 : : loop unrolling which introduces uses after clobbers. Most cases
4785 : : can be detected without optimization; cases where the address of
4786 : : the local variable is passed to and then returned from a user-
4787 : : defined function before its lifetime ends and the returned pointer
4788 : : becomes dangling depend on inlining. */
4789 : 5434540 : m_check_dangling_p = m_early_checks_p;
4790 : :
4791 : 5434540 : auto_bitmap bb_uids_set (&bitmap_default_obstack);
4792 : 5434540 : m_bb_uids_set = bb_uids_set;
4793 : :
4794 : 5434540 : set_gimple_stmt_max_uid (m_func, 0);
4795 : :
4796 : 5434540 : basic_block bb;
4797 : 49988130 : FOR_EACH_BB_FN (bb, fun)
4798 : 44553590 : check_block (bb);
4799 : :
4800 : 5434540 : if (m_check_dangling_p)
4801 : : {
4802 : 3968946 : check_dangling_uses ();
4803 : 3968946 : check_dangling_stores ();
4804 : : }
4805 : :
4806 : 5434540 : if (dump_file)
4807 : 149 : m_ptr_qry.dump (dump_file, (dump_flags & TDF_DETAILS) != 0);
4808 : :
4809 : 5434540 : m_ptr_qry.flush_cache ();
4810 : :
4811 : : /* Release the ranger instance and replace it with a global ranger.
4812 : : Also reset the pointer since calling disable_ranger() deletes it. */
4813 : 5434540 : disable_ranger (fun);
4814 : 5434540 : m_ptr_qry.rvals = NULL;
4815 : :
4816 : 5434540 : m_clobbers.empty ();
4817 : 5434540 : m_bb_uids_set = NULL;
4818 : :
4819 : 5434540 : free_dominance_info (CDI_POST_DOMINATORS);
4820 : 5434540 : free_dominance_info (CDI_DOMINATORS);
4821 : 5434540 : return 0;
4822 : 5434540 : }
4823 : :
4824 : : } // namespace
4825 : :
4826 : : /* Return a new instance of the pass. */
4827 : :
4828 : : gimple_opt_pass *
4829 : 281414 : make_pass_warn_access (gcc::context *ctxt)
4830 : : {
4831 : 281414 : return new pass_waccess (ctxt);
4832 : : }
|