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