PR lto/83954
[official-gcc.git] / gcc / gimple-ssa-warn-restrict.c
blob5b3b96913132a7b8e796674e62456a973366798a
1 /* Pass to detect and issue warnings for violations of the restrict
2 qualifier.
3 Copyright (C) 2017-2018 Free Software Foundation, Inc.
4 Contributed by Martin Sebor <msebor@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "domwalk.h"
29 #include "tree-pass.h"
30 #include "builtins.h"
31 #include "ssa.h"
32 #include "gimple-pretty-print.h"
33 #include "gimple-ssa-warn-restrict.h"
34 #include "diagnostic-core.h"
35 #include "fold-const.h"
36 #include "gimple-iterator.h"
37 #include "tree-dfa.h"
38 #include "tree-ssa.h"
39 #include "params.h"
40 #include "tree-cfg.h"
41 #include "tree-object-size.h"
42 #include "calls.h"
43 #include "cfgloop.h"
44 #include "intl.h"
46 namespace {
48 const pass_data pass_data_wrestrict = {
49 GIMPLE_PASS,
50 "wrestrict",
51 OPTGROUP_NONE,
52 TV_NONE,
53 PROP_cfg, /* Properties_required. */
54 0, /* properties_provided. */
55 0, /* properties_destroyed. */
56 0, /* properties_start */
57 0, /* properties_finish */
60 /* Pass to detect violations of strict aliasing requirements in calls
61 to built-in string and raw memory functions. */
62 class pass_wrestrict : public gimple_opt_pass
64 public:
65 pass_wrestrict (gcc::context *ctxt)
66 : gimple_opt_pass (pass_data_wrestrict, ctxt)
67 { }
69 opt_pass *clone () { return new pass_wrestrict (m_ctxt); }
71 virtual bool gate (function *);
72 virtual unsigned int execute (function *);
75 bool
76 pass_wrestrict::gate (function *fun ATTRIBUTE_UNUSED)
78 return warn_array_bounds != 0 || warn_restrict != 0;
81 /* Class to walk the basic blocks of a function in dominator order. */
82 class wrestrict_dom_walker : public dom_walker
84 public:
85 wrestrict_dom_walker () : dom_walker (CDI_DOMINATORS) {}
87 edge before_dom_children (basic_block) FINAL OVERRIDE;
88 bool handle_gimple_call (gimple_stmt_iterator *);
90 private:
91 void check_call (gcall *);
94 edge
95 wrestrict_dom_walker::before_dom_children (basic_block bb)
97 /* Iterate over statements, looking for function calls. */
98 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
99 gsi_next (&si))
101 gimple *stmt = gsi_stmt (si);
102 if (!is_gimple_call (stmt))
103 continue;
105 if (gcall *call = as_a <gcall *> (stmt))
106 check_call (call);
109 return NULL;
112 /* Execute the pass for function FUN, walking in dominator order. */
114 unsigned
115 pass_wrestrict::execute (function *fun)
117 calculate_dominance_info (CDI_DOMINATORS);
119 wrestrict_dom_walker walker;
120 walker.walk (ENTRY_BLOCK_PTR_FOR_FN (fun));
122 return 0;
125 /* Description of a memory reference by a built-in function. This
126 is similar to ao_ref but made especially suitable for -Wrestrict
127 and not for optimization. */
128 struct builtin_memref
130 /* The original pointer argument to the built-in function. */
131 tree ptr;
132 /* The referenced subobject or NULL if not available, and the base
133 object of the memory reference or NULL. */
134 tree ref;
135 tree base;
137 /* The size of the BASE object, PTRDIFF_MAX if indeterminate,
138 and negative until (possibly lazily) initialized. */
139 offset_int basesize;
141 /* The non-negative offset of the referenced subobject. Used to avoid
142 warnings for (apparently) possibly but not definitively overlapping
143 accesses to member arrays. Negative when unknown/invalid. */
144 offset_int refoff;
146 /* The offset range relative to the base. */
147 offset_int offrange[2];
148 /* The size range of the access to this reference. */
149 offset_int sizrange[2];
151 /* True for "bounded" string functions like strncat, and strncpy
152 and their variants that specify either an exact or upper bound
153 on the size of the accesses they perform. For strncat both
154 the source and destination references are bounded. For strncpy
155 only the destination reference is. */
156 bool strbounded_p;
158 builtin_memref (tree, tree);
160 tree offset_out_of_bounds (int, offset_int[2]) const;
163 /* Description of a memory access by a raw memory or string built-in
164 function involving a pair of builtin_memref's. */
165 class builtin_access
167 public:
168 /* Destination and source memory reference. */
169 builtin_memref* const dstref;
170 builtin_memref* const srcref;
171 /* The size range of the access. It's the greater of the accesses
172 to the two references. */
173 HOST_WIDE_INT sizrange[2];
175 /* The minimum and maximum offset of an overlap of the access
176 (if it does, in fact, overlap), and the size of the overlap. */
177 HOST_WIDE_INT ovloff[2];
178 HOST_WIDE_INT ovlsiz[2];
180 /* True to consider valid only accesses to the smallest subobject
181 and false for raw memory functions. */
182 bool strict () const
184 return detect_overlap != &builtin_access::generic_overlap;
187 builtin_access (gcall *, builtin_memref &, builtin_memref &);
189 /* Entry point to determine overlap. */
190 bool overlap ();
192 private:
193 /* Implementation functions used to determine overlap. */
194 bool generic_overlap ();
195 bool strcat_overlap ();
196 bool strcpy_overlap ();
198 bool no_overlap ()
200 return false;
203 offset_int overlap_size (const offset_int [2], const offset_int[2],
204 offset_int [2]);
206 private:
207 /* Temporaries used to compute the final result. */
208 offset_int dstoff[2];
209 offset_int srcoff[2];
210 offset_int dstsiz[2];
211 offset_int srcsiz[2];
213 /* Pointer to a member function to call to determine overlap. */
214 bool (builtin_access::*detect_overlap) ();
217 /* Initialize a memory reference representation from a pointer EXPR and
218 a size SIZE in bytes. If SIZE is NULL_TREE then the size is assumed
219 to be unknown. */
221 builtin_memref::builtin_memref (tree expr, tree size)
222 : ptr (expr),
223 ref (),
224 base (),
225 basesize (-1),
226 refoff (HOST_WIDE_INT_MIN),
227 offrange (),
228 sizrange (),
229 strbounded_p ()
231 /* Unfortunately, wide_int default ctor is a no-op so array members
232 of the type must be set individually. */
233 offrange[0] = offrange[1] = 0;
234 sizrange[0] = sizrange[1] = 0;
236 const offset_int maxobjsize = tree_to_shwi (max_object_size ());
238 if (TREE_CODE (expr) == SSA_NAME)
240 /* Try to tease the offset out of the pointer. */
241 gimple *stmt = SSA_NAME_DEF_STMT (expr);
242 if (gimple_assign_single_p (stmt)
243 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
244 expr = gimple_assign_rhs1 (stmt);
245 else if (is_gimple_assign (stmt))
247 tree_code code = gimple_assign_rhs_code (stmt);
248 if (code == NOP_EXPR)
250 tree rhs = gimple_assign_rhs1 (stmt);
251 if (POINTER_TYPE_P (TREE_TYPE (rhs)))
252 expr = gimple_assign_rhs1 (stmt);
254 else if (code == POINTER_PLUS_EXPR)
256 expr = gimple_assign_rhs1 (stmt);
258 tree offset = gimple_assign_rhs2 (stmt);
259 if (TREE_CODE (offset) == INTEGER_CST)
261 offset_int off = int_cst_value (offset);
262 offrange[0] = off;
263 offrange[1] = off;
265 if (TREE_CODE (expr) == SSA_NAME)
267 gimple *stmt = SSA_NAME_DEF_STMT (expr);
268 if (gimple_assign_single_p (stmt)
269 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
270 expr = gimple_assign_rhs1 (stmt);
273 else if (TREE_CODE (offset) == SSA_NAME)
275 wide_int min, max;
276 value_range_type rng = get_range_info (offset, &min, &max);
277 if (rng == VR_RANGE)
279 offrange[0] = offset_int::from (min, SIGNED);
280 offrange[1] = offset_int::from (max, SIGNED);
282 else if (rng == VR_ANTI_RANGE)
284 offrange[0] = offset_int::from (max + 1, SIGNED);
285 offrange[1] = offset_int::from (min - 1, SIGNED);
287 else
289 gimple *stmt = SSA_NAME_DEF_STMT (offset);
290 tree type;
291 if (is_gimple_assign (stmt)
292 && gimple_assign_rhs_code (stmt) == NOP_EXPR
293 && (type = TREE_TYPE (gimple_assign_rhs1 (stmt)))
294 && INTEGRAL_TYPE_P (type))
296 /* Use the bounds of the type of the NOP_EXPR operand
297 even if it's signed. The result doesn't trigger
298 warnings but makes their output more readable. */
299 offrange[0] = wi::to_offset (TYPE_MIN_VALUE (type));
300 offrange[1] = wi::to_offset (TYPE_MAX_VALUE (type));
302 else
303 offrange[1] = maxobjsize;
306 else
307 offrange[1] = maxobjsize;
312 if (TREE_CODE (expr) == ADDR_EXPR)
314 poly_int64 off;
315 tree op = TREE_OPERAND (expr, 0);
317 /* Determine the base object or pointer of the reference
318 and its constant offset from the beginning of the base. */
319 base = get_addr_base_and_unit_offset (op, &off);
321 HOST_WIDE_INT const_off;
322 if (base && off.is_constant (&const_off))
324 offrange[0] += const_off;
325 offrange[1] += const_off;
327 /* Stash the reference for offset validation. */
328 ref = op;
330 /* Also stash the constant offset for offset validation. */
331 if (TREE_CODE (op) == COMPONENT_REF)
332 refoff = const_off;
334 else
336 size = NULL_TREE;
337 base = get_base_address (TREE_OPERAND (expr, 0));
341 if (!base)
342 base = build2 (MEM_REF, char_type_node, expr, null_pointer_node);
344 if (TREE_CODE (base) == MEM_REF)
346 offset_int off;
347 if (mem_ref_offset (base).is_constant (&off))
349 refoff += off;
350 offrange[0] += off;
351 offrange[1] += off;
353 else
354 size = NULL_TREE;
355 base = TREE_OPERAND (base, 0);
358 if (TREE_CODE (base) == SSA_NAME)
359 if (gimple *stmt = SSA_NAME_DEF_STMT (base))
361 enum gimple_code code = gimple_code (stmt);
362 if (code == GIMPLE_ASSIGN)
363 if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
365 base = gimple_assign_rhs1 (stmt);
367 tree offset = gimple_assign_rhs2 (stmt);
368 if (TREE_CODE (offset) == INTEGER_CST)
370 offset_int off = int_cst_value (offset);
371 refoff += off;
372 offrange[0] += off;
373 offrange[1] += off;
378 if (size)
380 tree range[2];
381 /* Determine the size range, allowing for the result to be [0, 0]
382 for SIZE in the anti-range ~[0, N] where N >= PTRDIFF_MAX. */
383 get_size_range (size, range, true);
384 sizrange[0] = wi::to_offset (range[0]);
385 sizrange[1] = wi::to_offset (range[1]);
386 /* get_size_range returns SIZE_MAX for the maximum size.
387 Constrain it to the real maximum of PTRDIFF_MAX. */
388 if (sizrange[1] > maxobjsize)
389 sizrange[1] = maxobjsize;
391 else
392 sizrange[1] = maxobjsize;
395 /* Return error_mark_node if the signed offset exceeds the bounds
396 of the address space (PTRDIFF_MAX). Otherwise, return either
397 BASE or REF when the offset exceeds the bounds of the BASE or
398 REF object, and set OOBOFF to the past-the-end offset formed
399 by the reference, including its size. When STRICT is non-zero
400 use REF size, when available, otherwise use BASE size. When
401 STRICT is greater than 1, use the size of the last array member
402 as the bound, otherwise treat such a member as a flexible array
403 member. Return NULL when the offset is in bounds. */
405 tree
406 builtin_memref::offset_out_of_bounds (int strict, offset_int ooboff[2]) const
408 const offset_int maxobjsize = tree_to_shwi (max_object_size ());
410 /* A temporary, possibly adjusted, copy of the offset range. */
411 offset_int offrng[2] = { offrange[0], offrange[1] };
413 if (DECL_P (base) && TREE_CODE (TREE_TYPE (base)) == ARRAY_TYPE)
415 /* Check for offset in an anti-range with a negative lower bound.
416 For such a range, consider only the non-negative subrange. */
417 if (offrng[1] < offrng[0] && offrng[1] < 0)
418 offrng[1] = maxobjsize;
421 /* Conservative offset of the last byte of the referenced object. */
422 offset_int endoff;
424 /* The bounds need not be ordered. Set HIB to use as the index
425 of the larger of the bounds and LOB as the opposite. */
426 bool hib = wi::les_p (offrng[0], offrng[1]);
427 bool lob = !hib;
429 if (basesize < 0)
431 endoff = offrng[lob] + sizrange[0];
433 /* For a reference through a pointer to an object of unknown size
434 all initial offsets are considered valid, positive as well as
435 negative, since the pointer itself can point past the beginning
436 of the object. However, the sum of the lower bound of the offset
437 and that of the size must be less than or equal than PTRDIFF_MAX. */
438 if (endoff > maxobjsize)
439 return error_mark_node;
441 return NULL_TREE;
444 /* A reference to an object of known size must be within the bounds
445 of the base object. */
446 if (offrng[hib] < 0 || offrng[lob] > basesize)
447 return base;
449 /* The extent of the reference must also be within the bounds of
450 the base object (if known) or the maximum object size otherwise. */
451 endoff = wi::smax (offrng[lob], 0) + sizrange[0];
452 if (endoff > maxobjsize)
453 return error_mark_node;
455 offset_int size = basesize;
456 tree obj = base;
458 if (strict
459 && DECL_P (obj)
460 && ref
461 && refoff >= 0
462 && TREE_CODE (ref) == COMPONENT_REF
463 && (strict > 1
464 || !array_at_struct_end_p (ref)))
466 /* If the reference is to a member subobject, the offset must
467 be within the bounds of the subobject. */
468 tree field = TREE_OPERAND (ref, 1);
469 tree type = TREE_TYPE (field);
470 if (tree sz = TYPE_SIZE_UNIT (type))
471 if (TREE_CODE (sz) == INTEGER_CST)
473 size = refoff + wi::to_offset (sz);
474 obj = ref;
478 if (endoff <= size)
479 return NULL_TREE;
481 /* Set the out-of-bounds offset range to be one greater than
482 that delimited by the reference including its size. */
483 ooboff[lob] = size + 1;
485 if (endoff > ooboff[lob])
486 ooboff[hib] = endoff;
487 else
488 ooboff[hib] = wi::smax (offrng[lob], 0) + sizrange[1];
490 return obj;
493 /* Create an association between the memory references DST and SRC
494 for access by a call EXPR to a memory or string built-in funtion. */
496 builtin_access::builtin_access (gcall *call, builtin_memref &dst,
497 builtin_memref &src)
498 : dstref (&dst), srcref (&src), sizrange (), ovloff (), ovlsiz (),
499 dstoff (), srcoff (), dstsiz (), srcsiz ()
501 /* Zero out since the offset_int ctors invoked above are no-op. */
502 dstoff[0] = dstoff[1] = 0;
503 srcoff[0] = srcoff[1] = 0;
504 dstsiz[0] = dstsiz[1] = 0;
505 srcsiz[0] = srcsiz[1] = 0;
507 /* Object Size Type to use to determine the size of the destination
508 and source objects. Overridden below for raw memory functions. */
509 int ostype = 1;
511 /* True when the size of one reference depends on the offset of
512 itself or the other. */
513 bool depends_p = true;
515 /* True when the size of the destination reference DSTREF has been
516 determined from SRCREF and so needs to be adjusted by the latter's
517 offset. Only meaningful for bounded string functions like strncpy. */
518 bool dstadjust_p = false;
520 /* The size argument number (depends on the built-in). */
521 unsigned sizeargno = 2;
522 if (gimple_call_with_bounds_p (call))
523 sizeargno += 2;
525 tree func = gimple_call_fndecl (call);
526 switch (DECL_FUNCTION_CODE (func))
528 case BUILT_IN_MEMCPY:
529 case BUILT_IN_MEMCPY_CHK:
530 case BUILT_IN_MEMCPY_CHKP:
531 case BUILT_IN_MEMCPY_CHK_CHKP:
532 case BUILT_IN_MEMPCPY:
533 case BUILT_IN_MEMPCPY_CHK:
534 case BUILT_IN_MEMPCPY_CHKP:
535 case BUILT_IN_MEMPCPY_CHK_CHKP:
536 ostype = 0;
537 depends_p = false;
538 detect_overlap = &builtin_access::generic_overlap;
539 break;
541 case BUILT_IN_MEMMOVE:
542 case BUILT_IN_MEMMOVE_CHK:
543 case BUILT_IN_MEMMOVE_CHKP:
544 case BUILT_IN_MEMMOVE_CHK_CHKP:
545 /* For memmove there is never any overlap to check for. */
546 ostype = 0;
547 depends_p = false;
548 detect_overlap = &builtin_access::no_overlap;
549 break;
551 case BUILT_IN_STPNCPY:
552 case BUILT_IN_STPNCPY_CHK:
553 case BUILT_IN_STRNCPY:
554 case BUILT_IN_STRNCPY_CHK:
555 dstref->strbounded_p = true;
556 detect_overlap = &builtin_access::strcpy_overlap;
557 break;
559 case BUILT_IN_STPCPY:
560 case BUILT_IN_STPCPY_CHK:
561 case BUILT_IN_STPCPY_CHKP:
562 case BUILT_IN_STPCPY_CHK_CHKP:
563 case BUILT_IN_STRCPY:
564 case BUILT_IN_STRCPY_CHK:
565 case BUILT_IN_STRCPY_CHKP:
566 case BUILT_IN_STRCPY_CHK_CHKP:
567 detect_overlap = &builtin_access::strcpy_overlap;
568 break;
570 case BUILT_IN_STRCAT:
571 case BUILT_IN_STRCAT_CHK:
572 case BUILT_IN_STRCAT_CHKP:
573 case BUILT_IN_STRCAT_CHK_CHKP:
574 detect_overlap = &builtin_access::strcat_overlap;
575 break;
577 case BUILT_IN_STRNCAT:
578 case BUILT_IN_STRNCAT_CHK:
579 dstref->strbounded_p = true;
580 srcref->strbounded_p = true;
581 detect_overlap = &builtin_access::strcat_overlap;
582 break;
584 default:
585 /* Handle other string functions here whose access may need
586 to be validated for in-bounds offsets and non-overlapping
587 copies. (Not all _chkp functions have BUILT_IN_XXX_CHKP
588 macros so they need to be handled here.) */
589 return;
592 const offset_int maxobjsize = tree_to_shwi (max_object_size ());
594 /* Try to determine the size of the base object. compute_objsize
595 expects a pointer so create one if BASE is a non-pointer object. */
596 tree addr;
597 if (dst.basesize < 0)
599 addr = dst.base;
600 if (!POINTER_TYPE_P (TREE_TYPE (addr)))
601 addr = build1 (ADDR_EXPR, (TREE_TYPE (addr)), addr);
603 if (tree dstsize = compute_objsize (addr, ostype))
604 dst.basesize = wi::to_offset (dstsize);
605 else if (POINTER_TYPE_P (TREE_TYPE (addr)))
606 dst.basesize = HOST_WIDE_INT_MIN;
607 else
608 dst.basesize = maxobjsize;
611 if (src.basesize < 0)
613 addr = src.base;
614 if (!POINTER_TYPE_P (TREE_TYPE (addr)))
615 addr = build1 (ADDR_EXPR, (TREE_TYPE (addr)), addr);
617 if (tree srcsize = compute_objsize (addr, ostype))
618 src.basesize = wi::to_offset (srcsize);
619 else if (POINTER_TYPE_P (TREE_TYPE (addr)))
620 src.basesize = HOST_WIDE_INT_MIN;
621 else
622 src.basesize = maxobjsize;
625 /* If there is no dependency between the references or the base
626 objects of the two references aren't the same there's nothing
627 else to do. */
628 if (depends_p && dstref->base != srcref->base)
629 return;
631 /* ...otherwise, make adjustments for references to the same object
632 by string built-in functions to reflect the constraints imposed
633 by the function. */
635 /* For bounded string functions determine the range of the bound
636 on the access. For others, the range stays unbounded. */
637 offset_int bounds[2] = { maxobjsize, maxobjsize };
638 if (dstref->strbounded_p)
640 tree size = gimple_call_arg (call, sizeargno);
641 tree range[2];
642 if (get_size_range (size, range, true))
644 bounds[0] = wi::to_offset (range[0]);
645 bounds[1] = wi::to_offset (range[1]);
648 /* If both references' size ranges are indeterminate use the last
649 (size) argument from the function call as a substitute. This
650 may only be necessary for strncpy (but not for memcpy where
651 the size range would have been already determined this way). */
652 if (dstref->sizrange[0] == 0 && dstref->sizrange[1] == maxobjsize
653 && srcref->sizrange[0] == 0 && srcref->sizrange[1] == maxobjsize)
655 dstref->sizrange[0] = bounds[0];
656 dstref->sizrange[1] = bounds[1];
660 /* The size range of one reference involving the same base object
661 can be determined from the size range of the other reference.
662 This makes it possible to compute accurate offsets for warnings
663 involving functions like strcpy where the length of just one of
664 the two arguments is known (determined by tree-ssa-strlen). */
665 if (dstref->sizrange[0] == 0 && dstref->sizrange[1] == maxobjsize)
667 /* When the destination size is unknown set it to the size of
668 the source. */
669 dstref->sizrange[0] = srcref->sizrange[0];
670 dstref->sizrange[1] = srcref->sizrange[1];
672 else if (srcref->sizrange[0] == 0 && srcref->sizrange[1] == maxobjsize)
674 /* When the source size is unknown set it to the size of
675 the destination. */
676 srcref->sizrange[0] = dstref->sizrange[0];
677 srcref->sizrange[1] = dstref->sizrange[1];
679 if (depends_p)
681 if (dstref->strbounded_p)
683 /* Read access by strncpy is bounded. */
684 if (bounds[0] < srcref->sizrange[0])
685 srcref->sizrange[0] = bounds[0];
686 if (bounds[1] < srcref->sizrange[1])
687 srcref->sizrange[1] = bounds[1];
690 /* For string functions, adjust the size range of the source
691 reference by the inverse boundaries of the offset (because
692 the higher the offset into the string the shorter its
693 length). */
694 if (srcref->offrange[1] >= 0
695 && srcref->offrange[1] < srcref->sizrange[0])
696 srcref->sizrange[0] -= srcref->offrange[1];
697 else
698 srcref->sizrange[0] = 0;
700 if (srcref->offrange[0] > 0)
702 if (srcref->offrange[0] < srcref->sizrange[1])
703 srcref->sizrange[1] -= srcref->offrange[0];
704 else
705 srcref->sizrange[1] = 0;
708 dstadjust_p = true;
712 if (detect_overlap == &builtin_access::generic_overlap)
714 if (dstref->strbounded_p)
716 dstref->sizrange[0] = bounds[0];
717 dstref->sizrange[1] = bounds[1];
719 if (dstref->sizrange[0] < srcref->sizrange[0])
720 srcref->sizrange[0] = dstref->sizrange[0];
722 if (dstref->sizrange[1] < srcref->sizrange[1])
723 srcref->sizrange[1] = dstref->sizrange[1];
726 else if (detect_overlap == &builtin_access::strcpy_overlap)
728 if (!dstref->strbounded_p)
730 /* For strcpy, adjust the destination size range to match that
731 of the source computed above. */
732 if (depends_p && dstadjust_p)
734 dstref->sizrange[0] = srcref->sizrange[0];
735 dstref->sizrange[1] = srcref->sizrange[1];
740 if (dstref->strbounded_p)
742 /* For strncpy, adjust the destination size range to match that
743 of the source computed above. */
744 dstref->sizrange[0] = bounds[0];
745 dstref->sizrange[1] = bounds[1];
747 if (bounds[0] < srcref->sizrange[0])
748 srcref->sizrange[0] = bounds[0];
750 if (bounds[1] < srcref->sizrange[1])
751 srcref->sizrange[1] = bounds[1];
755 offset_int
756 builtin_access::overlap_size (const offset_int a[2], const offset_int b[2],
757 offset_int *off)
759 const offset_int *p = a;
760 const offset_int *q = b;
762 /* Point P at the bigger of the two ranges and Q at the smaller. */
763 if (wi::lts_p (a[1] - a[0], b[1] - b[0]))
765 p = b;
766 q = a;
769 if (p[0] < q[0])
771 if (p[1] < q[0])
772 return 0;
774 *off = q[0];
775 return wi::smin (p[1], q[1]) - q[0];
778 if (q[1] < p[0])
779 return 0;
781 off[0] = p[0];
782 return q[1] - p[0];
785 /* Return true if the bounded mempry (memcpy amd similar) or string function
786 access (strncpy and similar) ACS overlaps. */
788 bool
789 builtin_access::generic_overlap ()
791 builtin_access &acs = *this;
792 const builtin_memref *dstref = acs.dstref;
793 const builtin_memref *srcref = acs.srcref;
795 gcc_assert (dstref->base == srcref->base);
797 const offset_int maxobjsize = tree_to_shwi (max_object_size ());
799 offset_int maxsize = dstref->basesize < 0 ? maxobjsize : dstref->basesize;
800 gcc_assert (maxsize <= maxobjsize);
802 /* Adjust the larger bounds of the offsets (which may be the first
803 element if the lower bound is larger than the upper bound) to
804 make them valid for the smallest access (if possible) but no smaller
805 than the smaller bounds. */
806 gcc_assert (wi::les_p (acs.dstoff[0], acs.dstoff[1]));
808 if (maxsize < acs.dstoff[1] + acs.dstsiz[0])
809 acs.dstoff[1] = maxsize - acs.dstsiz[0];
810 if (acs.dstoff[1] < acs.dstoff[0])
811 acs.dstoff[1] = acs.dstoff[0];
813 gcc_assert (wi::les_p (acs.srcoff[0], acs.srcoff[1]));
815 if (maxsize < acs.srcoff[1] + acs.srcsiz[0])
816 acs.srcoff[1] = maxsize - acs.srcsiz[0];
817 if (acs.srcoff[1] < acs.srcoff[0])
818 acs.srcoff[1] = acs.srcoff[0];
820 /* Determine the minimum and maximum space for the access given
821 the offsets. */
822 offset_int space[2];
823 space[0] = wi::abs (acs.dstoff[0] - acs.srcoff[0]);
824 space[1] = space[0];
826 offset_int d = wi::abs (acs.dstoff[0] - acs.srcoff[1]);
827 if (acs.srcsiz[0] > 0)
829 if (d < space[0])
830 space[0] = d;
832 if (space[1] < d)
833 space[1] = d;
835 else
836 space[1] = acs.dstsiz[1];
838 d = wi::abs (acs.dstoff[1] - acs.srcoff[0]);
839 if (d < space[0])
840 space[0] = d;
842 if (space[1] < d)
843 space[1] = d;
845 /* Treat raw memory functions both of whose references are bounded
846 as special and permit uncertain overlaps to go undetected. For
847 all kinds of constant offset and constant size accesses, if
848 overlap isn't certain it is not possible. */
849 bool overlap_possible = space[0] < acs.dstsiz[1];
850 if (!overlap_possible)
851 return false;
853 bool overlap_certain = space[1] < acs.dstsiz[0];
855 /* True when the size of one reference depends on the offset of
856 the other. */
857 bool depends_p = detect_overlap != &builtin_access::generic_overlap;
859 if (!overlap_certain
860 && !dstref->strbounded_p
861 && !depends_p)
862 return false;
864 /* True for stpcpy and strcpy. */
865 bool stxcpy_p = (!dstref->strbounded_p
866 && detect_overlap == &builtin_access::strcpy_overlap);
868 if (dstref->refoff >= 0
869 && srcref->refoff >= 0
870 && dstref->refoff != srcref->refoff
871 && (stxcpy_p || dstref->strbounded_p || srcref->strbounded_p))
872 return false;
874 offset_int siz[2] = { maxobjsize + 1, 0 };
876 ovloff[0] = HOST_WIDE_INT_MAX;
877 ovloff[1] = HOST_WIDE_INT_MIN;
879 /* Adjustment to the lower bound of the offset of the overlap to
880 account for a subset of unbounded string calls where the size
881 of the destination string depends on the length of the source
882 which in turn depends on the offset into it. */
883 bool sub1;
885 if (stxcpy_p)
887 sub1 = acs.dstoff[0] <= acs.srcoff[0];
889 /* Iterate over the extreme locations (on the horizontal axis formed
890 by their offsets) and sizes of two regions and find their smallest
891 and largest overlap and the corresponding offsets. */
892 for (unsigned i = 0; i != 2; ++i)
894 const offset_int a[2] = {
895 acs.dstoff[i], acs.dstoff[i] + acs.dstsiz[!i]
898 const offset_int b[2] = {
899 acs.srcoff[i], acs.srcoff[i] + acs.srcsiz[!i]
902 offset_int off;
903 offset_int sz = overlap_size (a, b, &off);
904 if (sz < siz[0])
905 siz[0] = sz;
907 if (siz[1] <= sz)
908 siz[1] = sz;
910 if (sz != 0)
912 if (wi::lts_p (off, ovloff[0]))
913 ovloff[0] = off.to_shwi ();
914 if (wi::lts_p (ovloff[1], off))
915 ovloff[1] = off.to_shwi ();
919 else
921 sub1 = !depends_p;
923 /* Iterate over the extreme locations (on the horizontal axis
924 formed by their offsets) and sizes of two regions and find
925 their smallest and largest overlap and the corresponding
926 offsets. */
928 for (unsigned io = 0; io != 2; ++io)
929 for (unsigned is = 0; is != 2; ++is)
931 const offset_int a[2] = {
932 acs.dstoff[io], acs.dstoff[io] + acs.dstsiz[is]
935 for (unsigned jo = 0; jo != 2; ++jo)
936 for (unsigned js = 0; js != 2; ++js)
938 if (depends_p)
940 /* For st{p,r}ncpy the size of the source sequence
941 depends on the offset into it. */
942 if (js)
943 break;
944 js = !jo;
947 const offset_int b[2] = {
948 acs.srcoff[jo], acs.srcoff[jo] + acs.srcsiz[js]
951 offset_int off;
952 offset_int sz = overlap_size (a, b, &off);
953 if (sz < siz[0])
954 siz[0] = sz;
956 if (siz[1] <= sz)
957 siz[1] = sz;
959 if (sz != 0)
961 if (wi::lts_p (off, ovloff[0]))
962 ovloff[0] = off.to_shwi ();
963 if (wi::lts_p (ovloff[1], off))
964 ovloff[1] = off.to_shwi ();
970 ovlsiz[0] = siz[0].to_shwi ();
971 ovlsiz[1] = siz[1].to_shwi ();
973 if (ovlsiz[0] == 0 && ovlsiz[1] > 1)
974 ovloff[0] = ovloff[1] + ovlsiz[1] - 1 - sub1;
976 return true;
979 /* Return true if the strcat-like access overlaps. */
981 bool
982 builtin_access::strcat_overlap ()
984 builtin_access &acs = *this;
985 const builtin_memref *dstref = acs.dstref;
986 const builtin_memref *srcref = acs.srcref;
988 gcc_assert (dstref->base == srcref->base);
990 const offset_int maxobjsize = tree_to_shwi (max_object_size ());
992 gcc_assert (dstref->base && dstref->base == srcref->base);
994 /* Adjust for strcat-like accesses. */
996 /* As a special case for strcat, set the DSTREF offsets to the length
997 of the source string since the function starts writing at the first
998 nul, and set the size to 1 for the length of the nul. */
999 acs.dstoff[0] += acs.dstsiz[0];
1000 acs.dstoff[1] += acs.dstsiz[1];
1002 bool strfunc_unknown_args = acs.dstsiz[0] == 0 && acs.dstsiz[1] != 0;
1004 /* The lower bound is zero when the size is unknown because then
1005 overlap is not certain. */
1006 acs.dstsiz[0] = strfunc_unknown_args ? 0 : 1;
1007 acs.dstsiz[1] = 1;
1009 offset_int maxsize = dstref->basesize < 0 ? maxobjsize : dstref->basesize;
1010 gcc_assert (maxsize <= maxobjsize);
1012 /* For references to the same base object, determine if there's a pair
1013 of valid offsets into the two references such that access between
1014 them doesn't overlap. Adjust both upper bounds to be valid for
1015 the smaller size (i.e., at most MAXSIZE - SIZE). */
1017 if (maxsize < acs.dstoff[1] + acs.dstsiz[0])
1018 acs.dstoff[1] = maxsize - acs.dstsiz[0];
1020 if (maxsize < acs.srcoff[1] + acs.srcsiz[0])
1021 acs.srcoff[1] = maxsize - acs.srcsiz[0];
1023 /* Check to see if there's enough space for both accesses without
1024 overlap. Determine the optimistic (maximum) amount of available
1025 space. */
1026 offset_int space;
1027 if (acs.dstoff[0] <= acs.srcoff[0])
1029 if (acs.dstoff[1] < acs.srcoff[1])
1030 space = acs.srcoff[1] + acs.srcsiz[0] - acs.dstoff[0];
1031 else
1032 space = acs.dstoff[1] + acs.dstsiz[0] - acs.srcoff[0];
1034 else
1035 space = acs.dstoff[1] + acs.dstsiz[0] - acs.srcoff[0];
1037 /* Overlap is certain if the distance between the farthest offsets
1038 of the opposite accesses is less than the sum of the lower bounds
1039 of the sizes of the two accesses. */
1040 bool overlap_certain = space < acs.dstsiz[0] + acs.srcsiz[0];
1042 /* For a constant-offset, constant size access, consider the largest
1043 distance between the offset bounds and the lower bound of the access
1044 size. If the overlap isn't certain return success. */
1045 if (!overlap_certain
1046 && acs.dstoff[0] == acs.dstoff[1]
1047 && acs.srcoff[0] == acs.srcoff[1]
1048 && acs.dstsiz[0] == acs.dstsiz[1]
1049 && acs.srcsiz[0] == acs.srcsiz[1])
1050 return false;
1052 /* Overlap is not certain but may be possible. */
1054 offset_int access_min = acs.dstsiz[0] + acs.srcsiz[0];
1056 /* Determine the conservative (minimum) amount of space. */
1057 space = wi::abs (acs.dstoff[0] - acs.srcoff[0]);
1058 offset_int d = wi::abs (acs.dstoff[0] - acs.srcoff[1]);
1059 if (d < space)
1060 space = d;
1061 d = wi::abs (acs.dstoff[1] - acs.srcoff[0]);
1062 if (d < space)
1063 space = d;
1065 /* For a strict test (used for strcpy and similar with unknown or
1066 variable bounds or sizes), consider the smallest distance between
1067 the offset bounds and either the upper bound of the access size
1068 if known, or the lower bound otherwise. */
1069 if (access_min <= space && (access_min != 0 || !strfunc_unknown_args))
1070 return false;
1072 /* When strcat overlap is certain it is always a single byte:
1073 the terminatinn NUL, regardless of offsets and sizes. When
1074 overlap is only possible its range is [0, 1]. */
1075 acs.ovlsiz[0] = dstref->sizrange[0] == dstref->sizrange[1] ? 1 : 0;
1076 acs.ovlsiz[1] = 1;
1077 acs.ovloff[0] = (dstref->sizrange[0] + dstref->offrange[0]).to_shwi ();
1078 acs.ovloff[1] = (dstref->sizrange[1] + dstref->offrange[1]).to_shwi ();
1080 acs.sizrange[0] = wi::smax (acs.dstsiz[0], srcref->sizrange[0]).to_shwi ();
1081 acs.sizrange[1] = wi::smax (acs.dstsiz[1], srcref->sizrange[1]).to_shwi ();
1082 return true;
1085 /* Return true if the strcpy-like access overlaps. */
1087 bool
1088 builtin_access::strcpy_overlap ()
1090 return generic_overlap ();
1094 /* Return true if DSTREF and SRCREF describe accesses that either overlap
1095 one another or that, in order not to overlap, would imply that the size
1096 of the referenced object(s) exceeds the maximum size of an object. Set
1097 Otherwise, if DSTREF and SRCREF do not definitely overlap (even though
1098 they may overlap in a way that's not apparent from the available data),
1099 return false. */
1101 bool
1102 builtin_access::overlap ()
1104 builtin_access &acs = *this;
1106 const offset_int maxobjsize = tree_to_shwi (max_object_size ());
1108 acs.sizrange[0] = wi::smax (dstref->sizrange[0],
1109 srcref->sizrange[0]).to_shwi ();
1110 acs.sizrange[1] = wi::smax (dstref->sizrange[1],
1111 srcref->sizrange[1]).to_shwi ();
1113 /* Check to see if the two references refer to regions that are
1114 too large not to overlap in the address space (whose maximum
1115 size is PTRDIFF_MAX). */
1116 offset_int size = dstref->sizrange[0] + srcref->sizrange[0];
1117 if (maxobjsize < size)
1119 acs.ovloff[0] = (maxobjsize - dstref->sizrange[0]).to_shwi ();
1120 acs.ovlsiz[0] = (size - maxobjsize).to_shwi ();
1121 return true;
1124 /* If both base objects aren't known return the maximum possible
1125 offset that would make them not overlap. */
1126 if (!dstref->base || !srcref->base)
1127 return false;
1129 /* Set the access offsets. */
1130 acs.dstoff[0] = dstref->offrange[0];
1131 acs.dstoff[1] = dstref->offrange[1];
1133 /* If the base object is an array adjust the bounds of the offset
1134 to be non-negative and within the bounds of the array if possible. */
1135 if (dstref->base
1136 && TREE_CODE (TREE_TYPE (dstref->base)) == ARRAY_TYPE)
1138 if (acs.dstoff[0] < 0 && acs.dstoff[1] >= 0)
1139 acs.dstoff[0] = 0;
1141 if (acs.dstoff[1] < acs.dstoff[0])
1143 if (tree size = TYPE_SIZE_UNIT (TREE_TYPE (dstref->base)))
1144 acs.dstoff[1] = wi::umin (acs.dstoff[1], wi::to_offset (size));
1145 else
1146 acs.dstoff[1] = wi::umin (acs.dstoff[1], maxobjsize);
1150 acs.srcoff[0] = srcref->offrange[0];
1151 acs.srcoff[1] = srcref->offrange[1];
1153 if (srcref->base
1154 && TREE_CODE (TREE_TYPE (srcref->base)) == ARRAY_TYPE)
1156 if (acs.srcoff[0] < 0 && acs.srcoff[1] >= 0)
1157 acs.srcoff[0] = 0;
1159 if (tree size = TYPE_SIZE_UNIT (TREE_TYPE (srcref->base)))
1160 acs.srcoff[1] = wi::umin (acs.srcoff[1], wi::to_offset (size));
1161 else if (acs.srcoff[1] < acs.srcoff[0])
1162 acs.srcoff[1] = wi::umin (acs.srcoff[1], maxobjsize);
1165 /* When the upper bound of the offset is less than the lower bound
1166 the former is the result of a negative offset being represented
1167 as a large positive value or vice versa. The resulting range is
1168 a union of two subranges: [MIN, UB] and [LB, MAX]. Since such
1169 a union is not representable using the current data structure
1170 replace it with the full range of offsets. */
1171 if (acs.dstoff[1] < acs.dstoff[0])
1173 acs.dstoff[0] = -maxobjsize - 1;
1174 acs.dstoff[1] = maxobjsize;
1177 /* Validate the offset and size of each reference on its own first.
1178 This is independent of whether or not the base objects are the
1179 same. Normally, this would have already been detected and
1180 diagnosed by -Warray-bounds, unless it has been disabled. */
1181 offset_int maxoff = acs.dstoff[0] + dstref->sizrange[0];
1182 if (maxobjsize < maxoff)
1184 acs.ovlsiz[0] = (maxoff - maxobjsize).to_shwi ();
1185 acs.ovloff[0] = acs.dstoff[0].to_shwi () - acs.ovlsiz[0];
1186 return true;
1189 /* Repeat the same as above but for the source offsets. */
1190 if (acs.srcoff[1] < acs.srcoff[0])
1192 acs.srcoff[0] = -maxobjsize - 1;
1193 acs.srcoff[1] = maxobjsize;
1196 maxoff = acs.srcoff[0] + srcref->sizrange[0];
1197 if (maxobjsize < maxoff)
1199 acs.ovlsiz[0] = (maxoff - maxobjsize).to_shwi ();
1200 acs.ovlsiz[1] = (acs.srcoff[0] + srcref->sizrange[1]
1201 - maxobjsize).to_shwi ();
1202 acs.ovloff[0] = acs.srcoff[0].to_shwi () - acs.ovlsiz[0];
1203 return true;
1206 if (dstref->base != srcref->base)
1207 return false;
1209 acs.dstsiz[0] = dstref->sizrange[0];
1210 acs.dstsiz[1] = dstref->sizrange[1];
1212 acs.srcsiz[0] = srcref->sizrange[0];
1213 acs.srcsiz[1] = srcref->sizrange[1];
1215 /* Call the appropriate function to determine the overlap. */
1216 if ((this->*detect_overlap) ())
1218 sizrange[0] = wi::smax (acs.dstsiz[0], srcref->sizrange[0]).to_shwi ();
1219 sizrange[1] = wi::smax (acs.dstsiz[1], srcref->sizrange[1]).to_shwi ();
1220 return true;
1223 return false;
1226 /* Attempt to detect and diagnose an overlapping copy in a call expression
1227 EXPR involving an an access ACS to a built-in memory or string function.
1228 Return true when one has been detected, false otherwise. */
1230 static bool
1231 maybe_diag_overlap (location_t loc, gcall *call, builtin_access &acs)
1233 if (!acs.overlap ())
1234 return false;
1236 /* For convenience. */
1237 const builtin_memref &dstref = *acs.dstref;
1238 const builtin_memref &srcref = *acs.srcref;
1240 /* Determine the range of offsets and sizes of the overlap if it
1241 exists and issue diagnostics. */
1242 HOST_WIDE_INT *ovloff = acs.ovloff;
1243 HOST_WIDE_INT *ovlsiz = acs.ovlsiz;
1244 HOST_WIDE_INT *sizrange = acs.sizrange;
1246 tree func = gimple_call_fndecl (call);
1248 /* To avoid a combinatorial explosion of diagnostics format the offsets
1249 or their ranges as strings and use them in the warning calls below. */
1250 char offstr[3][64];
1252 if (dstref.offrange[0] == dstref.offrange[1]
1253 || dstref.offrange[1] > HOST_WIDE_INT_MAX)
1254 sprintf (offstr[0], HOST_WIDE_INT_PRINT_DEC,
1255 dstref.offrange[0].to_shwi ());
1256 else
1257 sprintf (offstr[0],
1258 "[" HOST_WIDE_INT_PRINT_DEC ", " HOST_WIDE_INT_PRINT_DEC "]",
1259 dstref.offrange[0].to_shwi (),
1260 dstref.offrange[1].to_shwi ());
1262 if (srcref.offrange[0] == srcref.offrange[1]
1263 || srcref.offrange[1] > HOST_WIDE_INT_MAX)
1264 sprintf (offstr[1],
1265 HOST_WIDE_INT_PRINT_DEC,
1266 srcref.offrange[0].to_shwi ());
1267 else
1268 sprintf (offstr[1],
1269 "[" HOST_WIDE_INT_PRINT_DEC ", " HOST_WIDE_INT_PRINT_DEC "]",
1270 srcref.offrange[0].to_shwi (),
1271 srcref.offrange[1].to_shwi ());
1273 if (ovloff[0] == ovloff[1] || !ovloff[1])
1274 sprintf (offstr[2], HOST_WIDE_INT_PRINT_DEC, ovloff[0]);
1275 else
1276 sprintf (offstr[2],
1277 "[" HOST_WIDE_INT_PRINT_DEC ", " HOST_WIDE_INT_PRINT_DEC "]",
1278 ovloff[0], ovloff[1]);
1280 const offset_int maxobjsize = tree_to_shwi (max_object_size ());
1281 bool must_overlap = ovlsiz[0] > 0;
1283 if (ovlsiz[1] == 0)
1284 ovlsiz[1] = ovlsiz[0];
1286 if (must_overlap)
1288 /* Issue definitive "overlaps" diagnostic in this block. */
1290 if (sizrange[0] == sizrange[1])
1292 if (ovlsiz[0] == ovlsiz[1])
1293 warning_at (loc, OPT_Wrestrict,
1294 sizrange[0] == 1
1295 ? (ovlsiz[0] == 1
1296 ? G_("%G%qD accessing %wu byte at offsets %s "
1297 "and %s overlaps %wu byte at offset %s")
1298 : G_("%G%qD accessing %wu byte at offsets %s "
1299 "and %s overlaps %wu bytes at offset "
1300 "%s"))
1301 : (ovlsiz[0] == 1
1302 ? G_("%G%qD accessing %wu bytes at offsets %s "
1303 "and %s overlaps %wu byte at offset %s")
1304 : G_("%G%qD accessing %wu bytes at offsets %s "
1305 "and %s overlaps %wu bytes at offset "
1306 "%s")),
1307 call, func, sizrange[0],
1308 offstr[0], offstr[1], ovlsiz[0], offstr[2]);
1309 else if (ovlsiz[1] >= 0 && ovlsiz[1] < maxobjsize.to_shwi ())
1310 warning_at (loc, OPT_Wrestrict,
1311 sizrange[0] == 1
1312 ? G_("%G%qD accessing %wu byte at offsets %s "
1313 "and %s overlaps between %wu and %wu bytes "
1314 "at offset %s")
1315 : G_("%G%qD accessing %wu bytes at offsets %s "
1316 "and %s overlaps between %wu and %wu bytes "
1317 "at offset %s"),
1318 call, func, sizrange[0],
1319 offstr[0], offstr[1], ovlsiz[0], ovlsiz[1],
1320 offstr[2]);
1321 else
1322 warning_at (loc, OPT_Wrestrict,
1323 sizrange[0] == 1
1324 ? G_("%G%qD accessing %wu byte at offsets %s and "
1325 "%s overlaps %wu or more bytes at offset %s")
1326 : G_("%G%qD accessing %wu bytes at offsets %s and "
1327 "%s overlaps %wu or more bytes at offset %s"),
1328 call, func, sizrange[0],
1329 offstr[0], offstr[1], ovlsiz[0], offstr[2]);
1330 return true;
1333 if (sizrange[1] >= 0 && sizrange[1] < maxobjsize.to_shwi ())
1335 if (ovlsiz[0] == ovlsiz[1])
1336 warning_at (loc, OPT_Wrestrict,
1337 ovlsiz[0] == 1
1338 ? G_("%G%qD accessing between %wu and %wu bytes "
1339 "at offsets %s and %s overlaps %wu byte at "
1340 "offset %s")
1341 : G_("%G%qD accessing between %wu and %wu bytes "
1342 "at offsets %s and %s overlaps %wu bytes "
1343 "at offset %s"),
1344 call, func, sizrange[0], sizrange[1],
1345 offstr[0], offstr[1], ovlsiz[0], offstr[2]);
1346 else if (ovlsiz[1] >= 0 && ovlsiz[1] < maxobjsize.to_shwi ())
1347 warning_at (loc, OPT_Wrestrict,
1348 "%G%qD accessing between %wu and %wu bytes at "
1349 "offsets %s and %s overlaps between %wu and %wu "
1350 "bytes at offset %s",
1351 call, func, sizrange[0], sizrange[1],
1352 offstr[0], offstr[1], ovlsiz[0], ovlsiz[1],
1353 offstr[2]);
1354 else
1355 warning_at (loc, OPT_Wrestrict,
1356 "%G%qD accessing between %wu and %wu bytes at "
1357 "offsets %s and %s overlaps %wu or more bytes "
1358 "at offset %s",
1359 call, func, sizrange[0], sizrange[1],
1360 offstr[0], offstr[1], ovlsiz[0], offstr[2]);
1361 return true;
1364 if (ovlsiz[0] != ovlsiz[1])
1365 ovlsiz[1] = maxobjsize.to_shwi ();
1367 if (ovlsiz[0] == ovlsiz[1])
1368 warning_at (loc, OPT_Wrestrict,
1369 ovlsiz[0] == 1
1370 ? G_("%G%qD accessing %wu or more bytes at offsets "
1371 "%s and %s overlaps %wu byte at offset %s")
1372 : G_("%G%qD accessing %wu or more bytes at offsets "
1373 "%s and %s overlaps %wu bytes at offset %s"),
1374 call, func, sizrange[0], offstr[0], offstr[1],
1375 ovlsiz[0], offstr[2]);
1376 else if (ovlsiz[1] >= 0 && ovlsiz[1] < maxobjsize.to_shwi ())
1377 warning_at (loc, OPT_Wrestrict,
1378 "%G%qD accessing %wu or more bytes at offsets %s "
1379 "and %s overlaps between %wu and %wu bytes "
1380 "at offset %s",
1381 call, func, sizrange[0], offstr[0], offstr[1],
1382 ovlsiz[0], ovlsiz[1], offstr[2]);
1383 else
1384 warning_at (loc, OPT_Wrestrict,
1385 "%G%qD accessing %wu or more bytes at offsets %s "
1386 "and %s overlaps %wu or more bytes at offset %s",
1387 call, func, sizrange[0], offstr[0], offstr[1],
1388 ovlsiz[0], offstr[2]);
1389 return true;
1392 /* Use more concise wording when one of the offsets is unbounded
1393 to avoid confusing the user with large and mostly meaningless
1394 numbers. */
1395 bool open_range = ((dstref.offrange[0] == -maxobjsize - 1
1396 && dstref.offrange[1] == maxobjsize)
1397 || (srcref.offrange[0] == -maxobjsize - 1
1398 && srcref.offrange[1] == maxobjsize));
1400 if (sizrange[0] == sizrange[1] || sizrange[1] == 1)
1402 if (ovlsiz[1] == 1)
1404 if (open_range)
1405 warning_at (loc, OPT_Wrestrict,
1406 sizrange[1] == 1
1407 ? G_("%G%qD accessing %wu byte may overlap "
1408 "%wu byte")
1409 : G_("%G%qD accessing %wu bytes may overlap "
1410 "%wu byte"),
1411 call, func, sizrange[1], ovlsiz[1]);
1412 else
1413 warning_at (loc, OPT_Wrestrict,
1414 sizrange[1] == 1
1415 ? G_("%G%qD accessing %wu byte at offsets %s "
1416 "and %s may overlap %wu byte at offset %s")
1417 : G_("%G%qD accessing %wu bytes at offsets %s "
1418 "and %s may overlap %wu byte at offset %s"),
1419 call, func, sizrange[1], offstr[0], offstr[1],
1420 ovlsiz[1], offstr[2]);
1421 return true;
1424 if (open_range)
1425 warning_at (loc, OPT_Wrestrict,
1426 sizrange[1] == 1
1427 ? G_("%G%qD accessing %wu byte may overlap "
1428 "up to %wu bytes")
1429 : G_("%G%qD accessing %wu bytes may overlap "
1430 "up to %wu bytes"),
1431 call, func, sizrange[1], ovlsiz[1]);
1432 else
1433 warning_at (loc, OPT_Wrestrict,
1434 sizrange[1] == 1
1435 ? G_("%G%qD accessing %wu byte at offsets %s and "
1436 "%s may overlap up to %wu bytes at offset %s")
1437 : G_("%G%qD accessing %wu bytes at offsets %s and "
1438 "%s may overlap up to %wu bytes at offset %s"),
1439 call, func, sizrange[1], offstr[0], offstr[1],
1440 ovlsiz[1], offstr[2]);
1441 return true;
1444 if (sizrange[1] >= 0 && sizrange[1] < maxobjsize.to_shwi ())
1446 if (open_range)
1447 warning_at (loc, OPT_Wrestrict,
1448 ovlsiz[1] == 1
1449 ? G_("%G%qD accessing between %wu and %wu bytes "
1450 "may overlap %wu byte")
1451 : G_("%G%qD accessing between %wu and %wu bytes "
1452 "may overlap up to %wu bytes"),
1453 call, func, sizrange[0], sizrange[1], ovlsiz[1]);
1454 else
1455 warning_at (loc, OPT_Wrestrict,
1456 ovlsiz[1] == 1
1457 ? G_("%G%qD accessing between %wu and %wu bytes "
1458 "at offsets %s and %s may overlap %wu byte "
1459 "at offset %s")
1460 : G_("%G%qD accessing between %wu and %wu bytes "
1461 "at offsets %s and %s may overlap up to %wu "
1462 "bytes at offset %s"),
1463 call, func, sizrange[0], sizrange[1],
1464 offstr[0], offstr[1], ovlsiz[1], offstr[2]);
1465 return true;
1468 warning_at (loc, OPT_Wrestrict,
1469 ovlsiz[1] == 1
1470 ? G_("%G%qD accessing %wu or more bytes at offsets %s "
1471 "and %s may overlap %wu byte at offset %s")
1472 : G_("%G%qD accessing %wu or more bytes at offsets %s "
1473 "and %s may overlap up to %wu bytes at offset %s"),
1474 call, func, sizrange[0], offstr[0], offstr[1],
1475 ovlsiz[1], offstr[2]);
1477 return true;
1480 /* Validate REF offsets in an EXPRession passed as an argument to a CALL
1481 to a built-in function FUNC to make sure they are within the bounds
1482 of the referenced object if its size is known, or PTRDIFF_MAX otherwise.
1483 Both initial values of the offsets and their final value computed by
1484 the function by incrementing the initial value by the size are
1485 validated. Return true if the offsets are not valid and a diagnostic
1486 has been issued. */
1488 static bool
1489 maybe_diag_offset_bounds (location_t loc, gcall *call, tree func, int strict,
1490 tree expr, const builtin_memref &ref)
1492 if (!warn_array_bounds)
1493 return false;
1495 offset_int ooboff[] = { ref.offrange[0], ref.offrange[1] };
1496 tree oobref = ref.offset_out_of_bounds (strict, ooboff);
1497 if (!oobref)
1498 return false;
1500 if (EXPR_HAS_LOCATION (expr))
1501 loc = EXPR_LOCATION (expr);
1503 loc = expansion_point_location_if_in_system_header (loc);
1505 tree type;
1507 char rangestr[2][64];
1508 if (ooboff[0] == ooboff[1]
1509 || (ooboff[0] != ref.offrange[0]
1510 && ooboff[0].to_shwi () >= ooboff[1].to_shwi ()))
1511 sprintf (rangestr[0], "%lli", (long long) ooboff[0].to_shwi ());
1512 else
1513 sprintf (rangestr[0], "[%lli, %lli]",
1514 (long long) ooboff[0].to_shwi (),
1515 (long long) ooboff[1].to_shwi ());
1517 if (oobref == error_mark_node)
1519 if (ref.sizrange[0] == ref.sizrange[1])
1520 sprintf (rangestr[1], "%lli", (long long) ref.sizrange[0].to_shwi ());
1521 else
1522 sprintf (rangestr[1], "[%lli, %lli]",
1523 (long long) ref.sizrange[0].to_shwi (),
1524 (long long) ref.sizrange[1].to_shwi ());
1526 if (DECL_P (ref.base)
1527 && TREE_CODE (type = TREE_TYPE (ref.base)) == ARRAY_TYPE)
1529 if (warning_at (loc, OPT_Warray_bounds,
1530 "%G%qD pointer overflow between offset %s "
1531 "and size %s accessing array %qD with type %qT",
1532 call, func, rangestr[0], rangestr[1], ref.base, type))
1533 inform (DECL_SOURCE_LOCATION (ref.base),
1534 "array %qD declared here", ref.base);
1535 else
1536 warning_at (loc, OPT_Warray_bounds,
1537 "%G%qD pointer overflow between offset %s "
1538 "and size %s",
1539 call, func, rangestr[0], rangestr[1]);
1541 else
1542 warning_at (loc, OPT_Warray_bounds,
1543 "%G%qD pointer overflow between offset %s "
1544 "and size %s",
1545 call, func, rangestr[0], rangestr[1]);
1547 else if (oobref == ref.base)
1549 const offset_int maxobjsize = tree_to_shwi (max_object_size ());
1551 /* True when the offset formed by an access to the reference
1552 is out of bounds, rather than the initial offset wich is
1553 in bounds. This implies access past the end. */
1554 bool form = ooboff[0] != ref.offrange[0];
1556 if (DECL_P (ref.base))
1558 if ((ref.basesize < maxobjsize
1559 && warning_at (loc, OPT_Warray_bounds,
1560 form
1561 ? G_("%G%qD forming offset %s is out of "
1562 "the bounds [0, %wu] of object %qD with "
1563 "type %qT")
1564 : G_("%G%qD offset %s is out of the bounds "
1565 "[0, %wu] of object %qD with type %qT"),
1566 call, func, rangestr[0], ref.basesize.to_uhwi (),
1567 ref.base, TREE_TYPE (ref.base)))
1568 || warning_at (loc, OPT_Warray_bounds,
1569 form
1570 ? G_("%G%qD forming offset %s is out of "
1571 "the bounds of object %qD with type %qT")
1572 : G_("%G%qD offset %s is out of the bounds "
1573 "of object %qD with type %qT"),
1574 call, func, rangestr[0],
1575 ref.base, TREE_TYPE (ref.base)))
1576 inform (DECL_SOURCE_LOCATION (ref.base),
1577 "%qD declared here", ref.base);
1579 else if (ref.basesize < maxobjsize)
1580 warning_at (loc, OPT_Warray_bounds,
1581 form
1582 ? G_("%G%qD forming offset %s is out of the bounds "
1583 "[0, %wu]")
1584 : G_("%G%qD offset %s is out of the bounds [0, %wu]"),
1585 call, func, rangestr[0], ref.basesize.to_uhwi ());
1586 else
1587 warning_at (loc, OPT_Warray_bounds,
1588 form
1589 ? G_("%G%qD forming offset %s is out of bounds")
1590 : G_("%G%qD offset %s is out of bounds"),
1591 call, func, rangestr[0]);
1593 else if (TREE_CODE (ref.ref) == MEM_REF)
1595 tree type = TREE_TYPE (TREE_OPERAND (ref.ref, 0));
1596 if (POINTER_TYPE_P (type))
1597 type = TREE_TYPE (type);
1598 type = TYPE_MAIN_VARIANT (type);
1600 warning_at (loc, OPT_Warray_bounds,
1601 "%G%qD offset %s from the object at %qE is out "
1602 "of the bounds of %qT",
1603 call, func, rangestr[0], ref.base, type);
1605 else
1607 type = TYPE_MAIN_VARIANT (TREE_TYPE (ref.ref));
1609 warning_at (loc, OPT_Warray_bounds,
1610 "%G%qD offset %s from the object at %qE is out "
1611 "of the bounds of referenced subobject %qD with type %qT "
1612 "at offset %wu",
1613 call, func, rangestr[0], ref.base, TREE_OPERAND (ref.ref, 1),
1614 type, ref.refoff.to_uhwi ());
1617 return true;
1620 /* Check a CALL statement for restrict-violations and issue warnings
1621 if/when appropriate. */
1623 void
1624 wrestrict_dom_walker::check_call (gcall *call)
1626 /* Avoid checking the call if it has already been diagnosed for
1627 some reason. */
1628 if (gimple_no_warning_p (call))
1629 return;
1631 tree func = gimple_call_fndecl (call);
1632 if (!func || DECL_BUILT_IN_CLASS (func) != BUILT_IN_NORMAL)
1633 return;
1635 bool with_bounds = gimple_call_with_bounds_p (call);
1637 /* Argument number to extract from the call (depends on the built-in
1638 and its kind). */
1639 unsigned dst_idx = -1;
1640 unsigned src_idx = -1;
1641 unsigned bnd_idx = -1;
1643 /* Is this CALL to a string function (as opposed to one to a raw
1644 memory function). */
1645 bool strfun = true;
1647 switch (DECL_FUNCTION_CODE (func))
1649 case BUILT_IN_MEMCPY:
1650 case BUILT_IN_MEMCPY_CHK:
1651 case BUILT_IN_MEMCPY_CHKP:
1652 case BUILT_IN_MEMCPY_CHK_CHKP:
1653 case BUILT_IN_MEMPCPY:
1654 case BUILT_IN_MEMPCPY_CHK:
1655 case BUILT_IN_MEMPCPY_CHKP:
1656 case BUILT_IN_MEMPCPY_CHK_CHKP:
1657 case BUILT_IN_MEMMOVE:
1658 case BUILT_IN_MEMMOVE_CHK:
1659 case BUILT_IN_MEMMOVE_CHKP:
1660 case BUILT_IN_MEMMOVE_CHK_CHKP:
1661 strfun = false;
1662 /* Fall through. */
1664 case BUILT_IN_STPNCPY:
1665 case BUILT_IN_STPNCPY_CHK:
1666 case BUILT_IN_STRNCAT:
1667 case BUILT_IN_STRNCAT_CHK:
1668 case BUILT_IN_STRNCPY:
1669 case BUILT_IN_STRNCPY_CHK:
1670 dst_idx = 0;
1671 src_idx = 1 + with_bounds;
1672 bnd_idx = 2 + 2 * with_bounds;
1673 break;
1675 case BUILT_IN_STPCPY:
1676 case BUILT_IN_STPCPY_CHK:
1677 case BUILT_IN_STPCPY_CHKP:
1678 case BUILT_IN_STPCPY_CHK_CHKP:
1679 case BUILT_IN_STRCPY:
1680 case BUILT_IN_STRCPY_CHK:
1681 case BUILT_IN_STRCPY_CHKP:
1682 case BUILT_IN_STRCPY_CHK_CHKP:
1683 case BUILT_IN_STRCAT:
1684 case BUILT_IN_STRCAT_CHK:
1685 case BUILT_IN_STRCAT_CHKP:
1686 case BUILT_IN_STRCAT_CHK_CHKP:
1687 dst_idx = 0;
1688 src_idx = 1 + with_bounds;
1689 break;
1691 default:
1692 /* Handle other string functions here whose access may need
1693 to be validated for in-bounds offsets and non-overlapping
1694 copies. (Not all _chkp functions have BUILT_IN_XXX_CHKP
1695 macros so they need to be handled here.) */
1696 return;
1699 unsigned nargs = gimple_call_num_args (call);
1701 tree dst = dst_idx < nargs ? gimple_call_arg (call, dst_idx) : NULL_TREE;
1702 tree src = src_idx < nargs ? gimple_call_arg (call, src_idx) : NULL_TREE;
1703 tree dstwr = bnd_idx < nargs ? gimple_call_arg (call, bnd_idx) : NULL_TREE;
1705 /* For string functions with an unspecified or unknown bound,
1706 assume the size of the access is one. */
1707 if (!dstwr && strfun)
1708 dstwr = size_one_node;
1710 /* DST and SRC can be null for a call with an insufficient number
1711 of arguments to a built-in function declared without a protype. */
1712 if (!dst || !src)
1713 return;
1715 /* DST, SRC, or DSTWR can also have the wrong type in a call to
1716 a function declared without a prototype. Avoid checking such
1717 invalid calls. */
1718 if (TREE_CODE (TREE_TYPE (dst)) != POINTER_TYPE
1719 || TREE_CODE (TREE_TYPE (src)) != POINTER_TYPE
1720 || (dstwr && !INTEGRAL_TYPE_P (TREE_TYPE (dstwr))))
1721 return;
1723 if (check_bounds_or_overlap (call, dst, src, dstwr, NULL_TREE))
1724 return;
1726 /* Avoid diagnosing the call again. */
1727 gimple_set_no_warning (call, true);
1730 } /* anonymous namespace */
1732 /* Attempt to detect and diagnose invalid offset bounds and (except for
1733 memmove) overlapping copy in a call expression EXPR from SRC to DST
1734 and DSTSIZE and SRCSIZE bytes, respectively. Both DSTSIZE and
1735 SRCSIZE may be NULL. Return false when one or the other has been
1736 detected and diagnosed, true otherwise. */
1738 bool
1739 check_bounds_or_overlap (gcall *call, tree dst, tree src, tree dstsize,
1740 tree srcsize, bool bounds_only /* = false */)
1742 location_t loc = gimple_location (call);
1744 if (tree block = gimple_block (call))
1745 if (location_t *pbloc = block_nonartificial_location (block))
1746 loc = *pbloc;
1748 loc = expansion_point_location_if_in_system_header (loc);
1750 tree func = gimple_call_fndecl (call);
1752 builtin_memref dstref (dst, dstsize);
1753 builtin_memref srcref (src, srcsize);
1755 builtin_access acs (call, dstref, srcref);
1757 /* Set STRICT to the value of the -Warray-bounds=N argument for
1758 string functions or when N > 1. */
1759 int strict = (acs.strict () || warn_array_bounds > 1 ? warn_array_bounds : 0);
1761 /* Validate offsets first to make sure they are within the bounds
1762 of the destination object if its size is known, or PTRDIFF_MAX
1763 otherwise. */
1764 if (maybe_diag_offset_bounds (loc, call, func, strict, dst, dstref)
1765 || maybe_diag_offset_bounds (loc, call, func, strict, src, srcref))
1767 gimple_set_no_warning (call, true);
1768 return false;
1771 bool check_overlap
1772 = (warn_restrict
1773 && (bounds_only
1774 || (DECL_FUNCTION_CODE (func) != BUILT_IN_MEMMOVE
1775 && DECL_FUNCTION_CODE (func) != BUILT_IN_MEMMOVE_CHK)));
1777 if (!check_overlap)
1778 return true;
1780 if (operand_equal_p (dst, src, 0))
1782 warning_at (loc, OPT_Wrestrict,
1783 "%G%qD source argument is the same as destination",
1784 call, func);
1785 gimple_set_no_warning (call, true);
1786 return false;
1789 /* Return false when overlap has been detected. */
1790 if (maybe_diag_overlap (loc, call, acs))
1792 gimple_set_no_warning (call, true);
1793 return false;
1796 return true;
1799 gimple_opt_pass *
1800 make_pass_warn_restrict (gcc::context *ctxt)
1802 return new pass_wrestrict (ctxt);