Preserving locations for variable-uses and constants (PR c++/43486)
[official-gcc.git] / gcc / gimple-ssa-warn-restrict.c
blob6979403a4f492791d6f61cec2cc784841e40c7b2
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 oper = 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 (oper, &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 = oper;
330 /* Also stash the constant offset for offset validation. */
331 tree_code code = TREE_CODE (oper);
332 if (code == COMPONENT_REF)
334 tree field = TREE_OPERAND (ref, 1);
335 tree fldoff = DECL_FIELD_OFFSET (field);
336 if (TREE_CODE (fldoff) == INTEGER_CST)
337 refoff = const_off + wi::to_offset (fldoff);
340 else
342 size = NULL_TREE;
343 base = get_base_address (TREE_OPERAND (expr, 0));
347 if (!base)
348 base = build2 (MEM_REF, char_type_node, expr, null_pointer_node);
350 if (TREE_CODE (base) == MEM_REF)
352 offset_int off;
353 if (mem_ref_offset (base).is_constant (&off))
355 refoff += off;
356 offrange[0] += off;
357 offrange[1] += off;
359 else
360 size = NULL_TREE;
361 base = TREE_OPERAND (base, 0);
364 if (TREE_CODE (base) == SSA_NAME)
365 if (gimple *stmt = SSA_NAME_DEF_STMT (base))
367 enum gimple_code code = gimple_code (stmt);
368 if (code == GIMPLE_ASSIGN)
369 if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
371 base = gimple_assign_rhs1 (stmt);
373 tree offset = gimple_assign_rhs2 (stmt);
374 if (TREE_CODE (offset) == INTEGER_CST)
376 offset_int off = int_cst_value (offset);
377 refoff += off;
378 offrange[0] += off;
379 offrange[1] += off;
383 if (TREE_CODE (base) == SSA_NAME && SSA_NAME_VAR (base))
384 base = SSA_NAME_VAR (base);
387 if (size)
389 tree range[2];
390 /* Determine the size range, allowing for the result to be [0, 0]
391 for SIZE in the anti-range ~[0, N] where N >= PTRDIFF_MAX. */
392 get_size_range (size, range, true);
393 sizrange[0] = wi::to_offset (range[0]);
394 sizrange[1] = wi::to_offset (range[1]);
395 /* get_size_range returns SIZE_MAX for the maximum size.
396 Constrain it to the real maximum of PTRDIFF_MAX. */
397 if (sizrange[1] > maxobjsize)
398 sizrange[1] = maxobjsize;
400 else
401 sizrange[1] = maxobjsize;
404 /* Return error_mark_node if the signed offset exceeds the bounds
405 of the address space (PTRDIFF_MAX). Otherwise, return either
406 BASE or REF when the offset exceeds the bounds of the BASE or
407 REF object, and set OOBOFF to the past-the-end offset formed
408 by the reference, including its size. When STRICT is non-zero
409 use REF size, when available, otherwise use BASE size. When
410 STRICT is greater than 1, use the size of the last array member
411 as the bound, otherwise treat such a member as a flexible array
412 member. Return NULL when the offset is in bounds. */
414 tree
415 builtin_memref::offset_out_of_bounds (int strict, offset_int ooboff[2]) const
417 const offset_int maxobjsize = tree_to_shwi (max_object_size ());
419 /* A temporary, possibly adjusted, copy of the offset range. */
420 offset_int offrng[2] = { offrange[0], offrange[1] };
422 if (DECL_P (base) && TREE_CODE (TREE_TYPE (base)) == ARRAY_TYPE)
424 /* Check for offset in an anti-range with a negative lower bound.
425 For such a range, consider only the non-negative subrange. */
426 if (offrng[1] < offrng[0] && offrng[1] < 0)
427 offrng[1] = maxobjsize;
430 /* Conservative offset of the last byte of the referenced object. */
431 offset_int endoff;
433 /* The bounds need not be ordered. Set HIB to use as the index
434 of the larger of the bounds and LOB as the opposite. */
435 bool hib = wi::les_p (offrng[0], offrng[1]);
436 bool lob = !hib;
438 if (basesize < 0)
440 endoff = offrng[lob] + sizrange[0];
442 /* For a reference through a pointer to an object of unknown size
443 all initial offsets are considered valid, positive as well as
444 negative, since the pointer itself can point past the beginning
445 of the object. However, the sum of the lower bound of the offset
446 and that of the size must be less than or equal than PTRDIFF_MAX. */
447 if (endoff > maxobjsize)
448 return error_mark_node;
450 return NULL_TREE;
453 /* A reference to an object of known size must be within the bounds
454 of the base object. */
455 if (offrng[hib] < 0 || offrng[lob] > basesize)
456 return base;
458 /* The extent of the reference must also be within the bounds of
459 the base object (if known) or the maximum object size otherwise. */
460 endoff = wi::smax (offrng[lob], 0) + sizrange[0];
461 if (endoff > maxobjsize)
462 return error_mark_node;
464 offset_int size = basesize;
465 tree obj = base;
467 if (strict
468 && DECL_P (obj)
469 && ref
470 && refoff >= 0
471 && TREE_CODE (ref) == COMPONENT_REF
472 && (strict > 1
473 || !array_at_struct_end_p (ref)))
475 /* If the reference is to a member subobject, the offset must
476 be within the bounds of the subobject. */
477 tree field = TREE_OPERAND (ref, 1);
478 tree type = TREE_TYPE (field);
479 if (tree sz = TYPE_SIZE_UNIT (type))
480 if (TREE_CODE (sz) == INTEGER_CST)
482 size = refoff + wi::to_offset (sz);
483 obj = ref;
487 if (endoff <= size)
488 return NULL_TREE;
490 /* Set the out-of-bounds offset range to be one greater than
491 that delimited by the reference including its size. */
492 ooboff[lob] = size + 1;
494 if (endoff > ooboff[lob])
495 ooboff[hib] = endoff;
496 else
497 ooboff[hib] = wi::smax (offrng[lob], 0) + sizrange[1];
499 return obj;
502 /* Create an association between the memory references DST and SRC
503 for access by a call EXPR to a memory or string built-in funtion. */
505 builtin_access::builtin_access (gcall *call, builtin_memref &dst,
506 builtin_memref &src)
507 : dstref (&dst), srcref (&src), sizrange (), ovloff (), ovlsiz (),
508 dstoff (), srcoff (), dstsiz (), srcsiz ()
510 /* Zero out since the offset_int ctors invoked above are no-op. */
511 dstoff[0] = dstoff[1] = 0;
512 srcoff[0] = srcoff[1] = 0;
513 dstsiz[0] = dstsiz[1] = 0;
514 srcsiz[0] = srcsiz[1] = 0;
516 /* Object Size Type to use to determine the size of the destination
517 and source objects. Overridden below for raw memory functions. */
518 int ostype = 1;
520 /* True when the size of one reference depends on the offset of
521 itself or the other. */
522 bool depends_p = true;
524 /* True when the size of the destination reference DSTREF has been
525 determined from SRCREF and so needs to be adjusted by the latter's
526 offset. Only meaningful for bounded string functions like strncpy. */
527 bool dstadjust_p = false;
529 /* The size argument number (depends on the built-in). */
530 unsigned sizeargno = 2;
531 if (gimple_call_with_bounds_p (call))
532 sizeargno += 2;
534 tree func = gimple_call_fndecl (call);
535 switch (DECL_FUNCTION_CODE (func))
537 case BUILT_IN_MEMCPY:
538 case BUILT_IN_MEMCPY_CHK:
539 case BUILT_IN_MEMCPY_CHKP:
540 case BUILT_IN_MEMCPY_CHK_CHKP:
541 case BUILT_IN_MEMPCPY:
542 case BUILT_IN_MEMPCPY_CHK:
543 case BUILT_IN_MEMPCPY_CHKP:
544 case BUILT_IN_MEMPCPY_CHK_CHKP:
545 ostype = 0;
546 depends_p = false;
547 detect_overlap = &builtin_access::generic_overlap;
548 break;
550 case BUILT_IN_MEMMOVE:
551 case BUILT_IN_MEMMOVE_CHK:
552 case BUILT_IN_MEMMOVE_CHKP:
553 case BUILT_IN_MEMMOVE_CHK_CHKP:
554 /* For memmove there is never any overlap to check for. */
555 ostype = 0;
556 depends_p = false;
557 detect_overlap = &builtin_access::no_overlap;
558 break;
560 case BUILT_IN_STPNCPY:
561 case BUILT_IN_STPNCPY_CHK:
562 case BUILT_IN_STRNCPY:
563 case BUILT_IN_STRNCPY_CHK:
564 dstref->strbounded_p = true;
565 detect_overlap = &builtin_access::strcpy_overlap;
566 break;
568 case BUILT_IN_STPCPY:
569 case BUILT_IN_STPCPY_CHK:
570 case BUILT_IN_STPCPY_CHKP:
571 case BUILT_IN_STPCPY_CHK_CHKP:
572 case BUILT_IN_STRCPY:
573 case BUILT_IN_STRCPY_CHK:
574 case BUILT_IN_STRCPY_CHKP:
575 case BUILT_IN_STRCPY_CHK_CHKP:
576 detect_overlap = &builtin_access::strcpy_overlap;
577 break;
579 case BUILT_IN_STRCAT:
580 case BUILT_IN_STRCAT_CHK:
581 case BUILT_IN_STRCAT_CHKP:
582 case BUILT_IN_STRCAT_CHK_CHKP:
583 detect_overlap = &builtin_access::strcat_overlap;
584 break;
586 case BUILT_IN_STRNCAT:
587 case BUILT_IN_STRNCAT_CHK:
588 dstref->strbounded_p = true;
589 srcref->strbounded_p = true;
590 detect_overlap = &builtin_access::strcat_overlap;
591 break;
593 default:
594 /* Handle other string functions here whose access may need
595 to be validated for in-bounds offsets and non-overlapping
596 copies. (Not all _chkp functions have BUILT_IN_XXX_CHKP
597 macros so they need to be handled here.) */
598 return;
601 const offset_int maxobjsize = tree_to_shwi (max_object_size ());
603 /* Try to determine the size of the base object. compute_objsize
604 expects a pointer so create one if BASE is a non-pointer object. */
605 tree addr;
606 if (dst.basesize < 0)
608 addr = dst.base;
609 if (!POINTER_TYPE_P (TREE_TYPE (addr)))
610 addr = build1 (ADDR_EXPR, (TREE_TYPE (addr)), addr);
612 if (tree dstsize = compute_objsize (addr, ostype))
613 dst.basesize = wi::to_offset (dstsize);
614 else if (POINTER_TYPE_P (TREE_TYPE (addr)))
615 dst.basesize = HOST_WIDE_INT_MIN;
616 else
617 dst.basesize = maxobjsize;
620 if (src.basesize < 0)
622 addr = src.base;
623 if (!POINTER_TYPE_P (TREE_TYPE (addr)))
624 addr = build1 (ADDR_EXPR, (TREE_TYPE (addr)), addr);
626 if (tree srcsize = compute_objsize (addr, ostype))
627 src.basesize = wi::to_offset (srcsize);
628 else if (POINTER_TYPE_P (TREE_TYPE (addr)))
629 src.basesize = HOST_WIDE_INT_MIN;
630 else
631 src.basesize = maxobjsize;
634 /* If there is no dependency between the references or the base
635 objects of the two references aren't the same there's nothing
636 else to do. */
637 if (depends_p && dstref->base != srcref->base)
638 return;
640 /* ...otherwise, make adjustments for references to the same object
641 by string built-in functions to reflect the constraints imposed
642 by the function. */
644 /* For bounded string functions determine the range of the bound
645 on the access. For others, the range stays unbounded. */
646 offset_int bounds[2] = { maxobjsize, maxobjsize };
647 if (dstref->strbounded_p)
649 tree size = gimple_call_arg (call, sizeargno);
650 tree range[2];
651 if (get_size_range (size, range, true))
653 bounds[0] = wi::to_offset (range[0]);
654 bounds[1] = wi::to_offset (range[1]);
657 /* If both references' size ranges are indeterminate use the last
658 (size) argument from the function call as a substitute. This
659 may only be necessary for strncpy (but not for memcpy where
660 the size range would have been already determined this way). */
661 if (dstref->sizrange[0] == 0 && dstref->sizrange[1] == maxobjsize
662 && srcref->sizrange[0] == 0 && srcref->sizrange[1] == maxobjsize)
664 dstref->sizrange[0] = bounds[0];
665 dstref->sizrange[1] = bounds[1];
669 /* The size range of one reference involving the same base object
670 can be determined from the size range of the other reference.
671 This makes it possible to compute accurate offsets for warnings
672 involving functions like strcpy where the length of just one of
673 the two arguments is known (determined by tree-ssa-strlen). */
674 if (dstref->sizrange[0] == 0 && dstref->sizrange[1] == maxobjsize)
676 /* When the destination size is unknown set it to the size of
677 the source. */
678 dstref->sizrange[0] = srcref->sizrange[0];
679 dstref->sizrange[1] = srcref->sizrange[1];
681 else if (srcref->sizrange[0] == 0 && srcref->sizrange[1] == maxobjsize)
683 /* When the source size is unknown set it to the size of
684 the destination. */
685 srcref->sizrange[0] = dstref->sizrange[0];
686 srcref->sizrange[1] = dstref->sizrange[1];
688 if (depends_p)
690 if (dstref->strbounded_p)
692 /* Read access by strncpy is bounded. */
693 if (bounds[0] < srcref->sizrange[0])
694 srcref->sizrange[0] = bounds[0];
695 if (bounds[1] < srcref->sizrange[1])
696 srcref->sizrange[1] = bounds[1];
699 /* For string functions, adjust the size range of the source
700 reference by the inverse boundaries of the offset (because
701 the higher the offset into the string the shorter its
702 length). */
703 if (srcref->offrange[1] >= 0
704 && srcref->offrange[1] < srcref->sizrange[0])
705 srcref->sizrange[0] -= srcref->offrange[1];
706 else
707 srcref->sizrange[0] = 0;
709 if (srcref->offrange[0] > 0)
711 if (srcref->offrange[0] < srcref->sizrange[1])
712 srcref->sizrange[1] -= srcref->offrange[0];
713 else
714 srcref->sizrange[1] = 0;
717 dstadjust_p = true;
721 if (detect_overlap == &builtin_access::generic_overlap)
723 if (dstref->strbounded_p)
725 dstref->sizrange[0] = bounds[0];
726 dstref->sizrange[1] = bounds[1];
728 if (dstref->sizrange[0] < srcref->sizrange[0])
729 srcref->sizrange[0] = dstref->sizrange[0];
731 if (dstref->sizrange[1] < srcref->sizrange[1])
732 srcref->sizrange[1] = dstref->sizrange[1];
735 else if (detect_overlap == &builtin_access::strcpy_overlap)
737 if (!dstref->strbounded_p)
739 /* For strcpy, adjust the destination size range to match that
740 of the source computed above. */
741 if (depends_p && dstadjust_p)
743 dstref->sizrange[0] = srcref->sizrange[0];
744 dstref->sizrange[1] = srcref->sizrange[1];
749 if (dstref->strbounded_p)
751 /* For strncpy, adjust the destination size range to match that
752 of the source computed above. */
753 dstref->sizrange[0] = bounds[0];
754 dstref->sizrange[1] = bounds[1];
756 if (bounds[0] < srcref->sizrange[0])
757 srcref->sizrange[0] = bounds[0];
759 if (bounds[1] < srcref->sizrange[1])
760 srcref->sizrange[1] = bounds[1];
764 offset_int
765 builtin_access::overlap_size (const offset_int a[2], const offset_int b[2],
766 offset_int *off)
768 const offset_int *p = a;
769 const offset_int *q = b;
771 /* Point P at the bigger of the two ranges and Q at the smaller. */
772 if (wi::lts_p (a[1] - a[0], b[1] - b[0]))
774 p = b;
775 q = a;
778 if (p[0] < q[0])
780 if (p[1] < q[0])
781 return 0;
783 *off = q[0];
784 return wi::smin (p[1], q[1]) - q[0];
787 if (q[1] < p[0])
788 return 0;
790 off[0] = p[0];
791 return q[1] - p[0];
794 /* Return true if the bounded mempry (memcpy amd similar) or string function
795 access (strncpy and similar) ACS overlaps. */
797 bool
798 builtin_access::generic_overlap ()
800 builtin_access &acs = *this;
801 const builtin_memref *dstref = acs.dstref;
802 const builtin_memref *srcref = acs.srcref;
804 gcc_assert (dstref->base == srcref->base);
806 const offset_int maxobjsize = tree_to_shwi (max_object_size ());
808 offset_int maxsize = dstref->basesize < 0 ? maxobjsize : dstref->basesize;
809 gcc_assert (maxsize <= maxobjsize);
811 /* Adjust the larger bounds of the offsets (which may be the first
812 element if the lower bound is larger than the upper bound) to
813 make them valid for the smallest access (if possible) but no smaller
814 than the smaller bounds. */
815 gcc_assert (wi::les_p (acs.dstoff[0], acs.dstoff[1]));
817 if (maxsize < acs.dstoff[1] + acs.dstsiz[0])
818 acs.dstoff[1] = maxsize - acs.dstsiz[0];
819 if (acs.dstoff[1] < acs.dstoff[0])
820 acs.dstoff[1] = acs.dstoff[0];
822 gcc_assert (wi::les_p (acs.srcoff[0], acs.srcoff[1]));
824 if (maxsize < acs.srcoff[1] + acs.srcsiz[0])
825 acs.srcoff[1] = maxsize - acs.srcsiz[0];
826 if (acs.srcoff[1] < acs.srcoff[0])
827 acs.srcoff[1] = acs.srcoff[0];
829 /* Determine the minimum and maximum space for the access given
830 the offsets. */
831 offset_int space[2];
832 space[0] = wi::abs (acs.dstoff[0] - acs.srcoff[0]);
833 space[1] = space[0];
835 offset_int d = wi::abs (acs.dstoff[0] - acs.srcoff[1]);
836 if (acs.srcsiz[0] > 0)
838 if (d < space[0])
839 space[0] = d;
841 if (space[1] < d)
842 space[1] = d;
844 else
845 space[1] = acs.dstsiz[1];
847 d = wi::abs (acs.dstoff[1] - acs.srcoff[0]);
848 if (d < space[0])
849 space[0] = d;
851 if (space[1] < d)
852 space[1] = d;
854 /* Treat raw memory functions both of whose references are bounded
855 as special and permit uncertain overlaps to go undetected. For
856 all kinds of constant offset and constant size accesses, if
857 overlap isn't certain it is not possible. */
858 bool overlap_possible = space[0] < acs.dstsiz[1];
859 if (!overlap_possible)
860 return false;
862 bool overlap_certain = space[1] < acs.dstsiz[0];
864 /* True when the size of one reference depends on the offset of
865 the other. */
866 bool depends_p = detect_overlap != &builtin_access::generic_overlap;
868 if (!overlap_certain
869 && !dstref->strbounded_p
870 && !depends_p)
871 return false;
873 /* True for stpcpy and strcpy. */
874 bool stxcpy_p = (!dstref->strbounded_p
875 && detect_overlap == &builtin_access::strcpy_overlap);
877 if (dstref->refoff >= 0
878 && srcref->refoff >= 0
879 && dstref->refoff != srcref->refoff
880 && (stxcpy_p || dstref->strbounded_p || srcref->strbounded_p))
881 return false;
883 offset_int siz[2] = { maxobjsize + 1, 0 };
885 ovloff[0] = HOST_WIDE_INT_MAX;
886 ovloff[1] = HOST_WIDE_INT_MIN;
888 /* Adjustment to the lower bound of the offset of the overlap to
889 account for a subset of unbounded string calls where the size
890 of the destination string depends on the length of the source
891 which in turn depends on the offset into it. */
892 bool sub1;
894 if (stxcpy_p)
896 sub1 = acs.dstoff[0] <= acs.srcoff[0];
898 /* Iterate over the extreme locations (on the horizontal axis formed
899 by their offsets) and sizes of two regions and find their smallest
900 and largest overlap and the corresponding offsets. */
901 for (unsigned i = 0; i != 2; ++i)
903 const offset_int a[2] = {
904 acs.dstoff[i], acs.dstoff[i] + acs.dstsiz[!i]
907 const offset_int b[2] = {
908 acs.srcoff[i], acs.srcoff[i] + acs.srcsiz[!i]
911 offset_int off;
912 offset_int sz = overlap_size (a, b, &off);
913 if (sz < siz[0])
914 siz[0] = sz;
916 if (siz[1] <= sz)
917 siz[1] = sz;
919 if (sz != 0)
921 if (wi::lts_p (off, ovloff[0]))
922 ovloff[0] = off.to_shwi ();
923 if (wi::lts_p (ovloff[1], off))
924 ovloff[1] = off.to_shwi ();
928 else
930 sub1 = !depends_p;
932 /* Iterate over the extreme locations (on the horizontal axis
933 formed by their offsets) and sizes of two regions and find
934 their smallest and largest overlap and the corresponding
935 offsets. */
937 for (unsigned io = 0; io != 2; ++io)
938 for (unsigned is = 0; is != 2; ++is)
940 const offset_int a[2] = {
941 acs.dstoff[io], acs.dstoff[io] + acs.dstsiz[is]
944 for (unsigned jo = 0; jo != 2; ++jo)
945 for (unsigned js = 0; js != 2; ++js)
947 if (depends_p)
949 /* For st{p,r}ncpy the size of the source sequence
950 depends on the offset into it. */
951 if (js)
952 break;
953 js = !jo;
956 const offset_int b[2] = {
957 acs.srcoff[jo], acs.srcoff[jo] + acs.srcsiz[js]
960 offset_int off;
961 offset_int sz = overlap_size (a, b, &off);
962 if (sz < siz[0])
963 siz[0] = sz;
965 if (siz[1] <= sz)
966 siz[1] = sz;
968 if (sz != 0)
970 if (wi::lts_p (off, ovloff[0]))
971 ovloff[0] = off.to_shwi ();
972 if (wi::lts_p (ovloff[1], off))
973 ovloff[1] = off.to_shwi ();
979 ovlsiz[0] = siz[0].to_shwi ();
980 ovlsiz[1] = siz[1].to_shwi ();
982 if (ovlsiz[0] == 0 && ovlsiz[1] > 1)
983 ovloff[0] = ovloff[1] + ovlsiz[1] - 1 - sub1;
985 return true;
988 /* Return true if the strcat-like access overlaps. */
990 bool
991 builtin_access::strcat_overlap ()
993 builtin_access &acs = *this;
994 const builtin_memref *dstref = acs.dstref;
995 const builtin_memref *srcref = acs.srcref;
997 gcc_assert (dstref->base == srcref->base);
999 const offset_int maxobjsize = tree_to_shwi (max_object_size ());
1001 gcc_assert (dstref->base && dstref->base == srcref->base);
1003 /* Adjust for strcat-like accesses. */
1005 /* As a special case for strcat, set the DSTREF offsets to the length
1006 of the source string since the function starts writing at the first
1007 nul, and set the size to 1 for the length of the nul. */
1008 acs.dstoff[0] += acs.dstsiz[0];
1009 acs.dstoff[1] += acs.dstsiz[1];
1011 bool strfunc_unknown_args = acs.dstsiz[0] == 0 && acs.dstsiz[1] != 0;
1013 /* The lower bound is zero when the size is unknown because then
1014 overlap is not certain. */
1015 acs.dstsiz[0] = strfunc_unknown_args ? 0 : 1;
1016 acs.dstsiz[1] = 1;
1018 offset_int maxsize = dstref->basesize < 0 ? maxobjsize : dstref->basesize;
1019 gcc_assert (maxsize <= maxobjsize);
1021 /* For references to the same base object, determine if there's a pair
1022 of valid offsets into the two references such that access between
1023 them doesn't overlap. Adjust both upper bounds to be valid for
1024 the smaller size (i.e., at most MAXSIZE - SIZE). */
1026 if (maxsize < acs.dstoff[1] + acs.dstsiz[0])
1027 acs.dstoff[1] = maxsize - acs.dstsiz[0];
1029 if (maxsize < acs.srcoff[1] + acs.srcsiz[0])
1030 acs.srcoff[1] = maxsize - acs.srcsiz[0];
1032 /* Check to see if there's enough space for both accesses without
1033 overlap. Determine the optimistic (maximum) amount of available
1034 space. */
1035 offset_int space;
1036 if (acs.dstoff[0] <= acs.srcoff[0])
1038 if (acs.dstoff[1] < acs.srcoff[1])
1039 space = acs.srcoff[1] + acs.srcsiz[0] - acs.dstoff[0];
1040 else
1041 space = acs.dstoff[1] + acs.dstsiz[0] - acs.srcoff[0];
1043 else
1044 space = acs.dstoff[1] + acs.dstsiz[0] - acs.srcoff[0];
1046 /* Overlap is certain if the distance between the farthest offsets
1047 of the opposite accesses is less than the sum of the lower bounds
1048 of the sizes of the two accesses. */
1049 bool overlap_certain = space < acs.dstsiz[0] + acs.srcsiz[0];
1051 /* For a constant-offset, constant size access, consider the largest
1052 distance between the offset bounds and the lower bound of the access
1053 size. If the overlap isn't certain return success. */
1054 if (!overlap_certain
1055 && acs.dstoff[0] == acs.dstoff[1]
1056 && acs.srcoff[0] == acs.srcoff[1]
1057 && acs.dstsiz[0] == acs.dstsiz[1]
1058 && acs.srcsiz[0] == acs.srcsiz[1])
1059 return false;
1061 /* Overlap is not certain but may be possible. */
1063 offset_int access_min = acs.dstsiz[0] + acs.srcsiz[0];
1065 /* Determine the conservative (minimum) amount of space. */
1066 space = wi::abs (acs.dstoff[0] - acs.srcoff[0]);
1067 offset_int d = wi::abs (acs.dstoff[0] - acs.srcoff[1]);
1068 if (d < space)
1069 space = d;
1070 d = wi::abs (acs.dstoff[1] - acs.srcoff[0]);
1071 if (d < space)
1072 space = d;
1074 /* For a strict test (used for strcpy and similar with unknown or
1075 variable bounds or sizes), consider the smallest distance between
1076 the offset bounds and either the upper bound of the access size
1077 if known, or the lower bound otherwise. */
1078 if (access_min <= space && (access_min != 0 || !strfunc_unknown_args))
1079 return false;
1081 /* When strcat overlap is certain it is always a single byte:
1082 the terminatinn NUL, regardless of offsets and sizes. When
1083 overlap is only possible its range is [0, 1]. */
1084 acs.ovlsiz[0] = dstref->sizrange[0] == dstref->sizrange[1] ? 1 : 0;
1085 acs.ovlsiz[1] = 1;
1086 acs.ovloff[0] = (dstref->sizrange[0] + dstref->offrange[0]).to_shwi ();
1087 acs.ovloff[1] = (dstref->sizrange[1] + dstref->offrange[1]).to_shwi ();
1089 acs.sizrange[0] = wi::smax (acs.dstsiz[0], srcref->sizrange[0]).to_shwi ();
1090 acs.sizrange[1] = wi::smax (acs.dstsiz[1], srcref->sizrange[1]).to_shwi ();
1091 return true;
1094 /* Return true if the strcpy-like access overlaps. */
1096 bool
1097 builtin_access::strcpy_overlap ()
1099 return generic_overlap ();
1103 /* Return true if DSTREF and SRCREF describe accesses that either overlap
1104 one another or that, in order not to overlap, would imply that the size
1105 of the referenced object(s) exceeds the maximum size of an object. Set
1106 Otherwise, if DSTREF and SRCREF do not definitely overlap (even though
1107 they may overlap in a way that's not apparent from the available data),
1108 return false. */
1110 bool
1111 builtin_access::overlap ()
1113 builtin_access &acs = *this;
1115 const offset_int maxobjsize = tree_to_shwi (max_object_size ());
1117 acs.sizrange[0] = wi::smax (dstref->sizrange[0],
1118 srcref->sizrange[0]).to_shwi ();
1119 acs.sizrange[1] = wi::smax (dstref->sizrange[1],
1120 srcref->sizrange[1]).to_shwi ();
1122 /* Check to see if the two references refer to regions that are
1123 too large not to overlap in the address space (whose maximum
1124 size is PTRDIFF_MAX). */
1125 offset_int size = dstref->sizrange[0] + srcref->sizrange[0];
1126 if (maxobjsize < size)
1128 acs.ovloff[0] = (maxobjsize - dstref->sizrange[0]).to_shwi ();
1129 acs.ovlsiz[0] = (size - maxobjsize).to_shwi ();
1130 return true;
1133 /* If both base objects aren't known return the maximum possible
1134 offset that would make them not overlap. */
1135 if (!dstref->base || !srcref->base)
1136 return false;
1138 /* Set the access offsets. */
1139 acs.dstoff[0] = dstref->offrange[0];
1140 acs.dstoff[1] = dstref->offrange[1];
1142 /* If the base object is an array adjust the bounds of the offset
1143 to be non-negative and within the bounds of the array if possible. */
1144 if (dstref->base
1145 && TREE_CODE (TREE_TYPE (dstref->base)) == ARRAY_TYPE)
1147 if (acs.dstoff[0] < 0 && acs.dstoff[1] >= 0)
1148 acs.dstoff[0] = 0;
1150 if (acs.dstoff[1] < acs.dstoff[0])
1152 if (tree size = TYPE_SIZE_UNIT (TREE_TYPE (dstref->base)))
1153 acs.dstoff[1] = wi::umin (acs.dstoff[1], wi::to_offset (size));
1154 else
1155 acs.dstoff[1] = wi::umin (acs.dstoff[1], maxobjsize);
1159 acs.srcoff[0] = srcref->offrange[0];
1160 acs.srcoff[1] = srcref->offrange[1];
1162 if (srcref->base
1163 && TREE_CODE (TREE_TYPE (srcref->base)) == ARRAY_TYPE)
1165 if (acs.srcoff[0] < 0 && acs.srcoff[1] >= 0)
1166 acs.srcoff[0] = 0;
1168 if (tree size = TYPE_SIZE_UNIT (TREE_TYPE (srcref->base)))
1169 acs.srcoff[1] = wi::umin (acs.srcoff[1], wi::to_offset (size));
1170 else if (acs.srcoff[1] < acs.srcoff[0])
1171 acs.srcoff[1] = wi::umin (acs.srcoff[1], maxobjsize);
1174 /* When the upper bound of the offset is less than the lower bound
1175 the former is the result of a negative offset being represented
1176 as a large positive value or vice versa. The resulting range is
1177 a union of two subranges: [MIN, UB] and [LB, MAX]. Since such
1178 a union is not representable using the current data structure
1179 replace it with the full range of offsets. */
1180 if (acs.dstoff[1] < acs.dstoff[0])
1182 acs.dstoff[0] = -maxobjsize - 1;
1183 acs.dstoff[1] = maxobjsize;
1186 /* Validate the offset and size of each reference on its own first.
1187 This is independent of whether or not the base objects are the
1188 same. Normally, this would have already been detected and
1189 diagnosed by -Warray-bounds, unless it has been disabled. */
1190 offset_int maxoff = acs.dstoff[0] + dstref->sizrange[0];
1191 if (maxobjsize < maxoff)
1193 acs.ovlsiz[0] = (maxoff - maxobjsize).to_shwi ();
1194 acs.ovloff[0] = acs.dstoff[0].to_shwi () - acs.ovlsiz[0];
1195 return true;
1198 /* Repeat the same as above but for the source offsets. */
1199 if (acs.srcoff[1] < acs.srcoff[0])
1201 acs.srcoff[0] = -maxobjsize - 1;
1202 acs.srcoff[1] = maxobjsize;
1205 maxoff = acs.srcoff[0] + srcref->sizrange[0];
1206 if (maxobjsize < maxoff)
1208 acs.ovlsiz[0] = (maxoff - maxobjsize).to_shwi ();
1209 acs.ovlsiz[1] = (acs.srcoff[0] + srcref->sizrange[1]
1210 - maxobjsize).to_shwi ();
1211 acs.ovloff[0] = acs.srcoff[0].to_shwi () - acs.ovlsiz[0];
1212 return true;
1215 if (dstref->base != srcref->base)
1216 return false;
1218 acs.dstsiz[0] = dstref->sizrange[0];
1219 acs.dstsiz[1] = dstref->sizrange[1];
1221 acs.srcsiz[0] = srcref->sizrange[0];
1222 acs.srcsiz[1] = srcref->sizrange[1];
1224 /* Call the appropriate function to determine the overlap. */
1225 if ((this->*detect_overlap) ())
1227 sizrange[0] = wi::smax (acs.dstsiz[0], srcref->sizrange[0]).to_shwi ();
1228 sizrange[1] = wi::smax (acs.dstsiz[1], srcref->sizrange[1]).to_shwi ();
1229 return true;
1232 return false;
1235 /* Attempt to detect and diagnose an overlapping copy in a call expression
1236 EXPR involving an an access ACS to a built-in memory or string function.
1237 Return true when one has been detected, false otherwise. */
1239 static bool
1240 maybe_diag_overlap (location_t loc, gcall *call, builtin_access &acs)
1242 if (!acs.overlap ())
1243 return false;
1245 /* For convenience. */
1246 const builtin_memref &dstref = *acs.dstref;
1247 const builtin_memref &srcref = *acs.srcref;
1249 /* Determine the range of offsets and sizes of the overlap if it
1250 exists and issue diagnostics. */
1251 HOST_WIDE_INT *ovloff = acs.ovloff;
1252 HOST_WIDE_INT *ovlsiz = acs.ovlsiz;
1253 HOST_WIDE_INT *sizrange = acs.sizrange;
1255 tree func = gimple_call_fndecl (call);
1257 /* To avoid a combinatorial explosion of diagnostics format the offsets
1258 or their ranges as strings and use them in the warning calls below. */
1259 char offstr[3][64];
1261 if (dstref.offrange[0] == dstref.offrange[1]
1262 || dstref.offrange[1] > HOST_WIDE_INT_MAX)
1263 sprintf (offstr[0], HOST_WIDE_INT_PRINT_DEC,
1264 dstref.offrange[0].to_shwi ());
1265 else
1266 sprintf (offstr[0],
1267 "[" HOST_WIDE_INT_PRINT_DEC ", " HOST_WIDE_INT_PRINT_DEC "]",
1268 dstref.offrange[0].to_shwi (),
1269 dstref.offrange[1].to_shwi ());
1271 if (srcref.offrange[0] == srcref.offrange[1]
1272 || srcref.offrange[1] > HOST_WIDE_INT_MAX)
1273 sprintf (offstr[1],
1274 HOST_WIDE_INT_PRINT_DEC,
1275 srcref.offrange[0].to_shwi ());
1276 else
1277 sprintf (offstr[1],
1278 "[" HOST_WIDE_INT_PRINT_DEC ", " HOST_WIDE_INT_PRINT_DEC "]",
1279 srcref.offrange[0].to_shwi (),
1280 srcref.offrange[1].to_shwi ());
1282 if (ovloff[0] == ovloff[1] || !ovloff[1])
1283 sprintf (offstr[2], HOST_WIDE_INT_PRINT_DEC, ovloff[0]);
1284 else
1285 sprintf (offstr[2],
1286 "[" HOST_WIDE_INT_PRINT_DEC ", " HOST_WIDE_INT_PRINT_DEC "]",
1287 ovloff[0], ovloff[1]);
1289 const offset_int maxobjsize = tree_to_shwi (max_object_size ());
1290 bool must_overlap = ovlsiz[0] > 0;
1292 if (ovlsiz[1] == 0)
1293 ovlsiz[1] = ovlsiz[0];
1295 if (must_overlap)
1297 /* Issue definitive "overlaps" diagnostic in this block. */
1299 if (sizrange[0] == sizrange[1])
1301 if (ovlsiz[0] == ovlsiz[1])
1302 warning_at (loc, OPT_Wrestrict,
1303 sizrange[0] == 1
1304 ? (ovlsiz[0] == 1
1305 ? G_("%G%qD accessing %wu byte at offsets %s "
1306 "and %s overlaps %wu byte at offset %s")
1307 : G_("%G%qD accessing %wu byte at offsets %s "
1308 "and %s overlaps %wu bytes at offset "
1309 "%s"))
1310 : (ovlsiz[0] == 1
1311 ? G_("%G%qD accessing %wu bytes at offsets %s "
1312 "and %s overlaps %wu byte at offset %s")
1313 : G_("%G%qD accessing %wu bytes at offsets %s "
1314 "and %s overlaps %wu bytes at offset "
1315 "%s")),
1316 call, func, sizrange[0],
1317 offstr[0], offstr[1], ovlsiz[0], offstr[2]);
1318 else if (ovlsiz[1] >= 0 && ovlsiz[1] < maxobjsize.to_shwi ())
1319 warning_at (loc, OPT_Wrestrict,
1320 sizrange[0] == 1
1321 ? G_("%G%qD accessing %wu byte at offsets %s "
1322 "and %s overlaps between %wu and %wu bytes "
1323 "at offset %s")
1324 : G_("%G%qD accessing %wu bytes at offsets %s "
1325 "and %s overlaps between %wu and %wu bytes "
1326 "at offset %s"),
1327 call, func, sizrange[0],
1328 offstr[0], offstr[1], ovlsiz[0], ovlsiz[1],
1329 offstr[2]);
1330 else
1331 warning_at (loc, OPT_Wrestrict,
1332 sizrange[0] == 1
1333 ? G_("%G%qD accessing %wu byte at offsets %s and "
1334 "%s overlaps %wu or more bytes at offset %s")
1335 : G_("%G%qD accessing %wu bytes at offsets %s and "
1336 "%s overlaps %wu or more bytes at offset %s"),
1337 call, func, sizrange[0],
1338 offstr[0], offstr[1], ovlsiz[0], offstr[2]);
1339 return true;
1342 if (sizrange[1] >= 0 && sizrange[1] < maxobjsize.to_shwi ())
1344 if (ovlsiz[0] == ovlsiz[1])
1345 warning_at (loc, OPT_Wrestrict,
1346 ovlsiz[0] == 1
1347 ? G_("%G%qD accessing between %wu and %wu bytes "
1348 "at offsets %s and %s overlaps %wu byte at "
1349 "offset %s")
1350 : G_("%G%qD accessing between %wu and %wu bytes "
1351 "at offsets %s and %s overlaps %wu bytes "
1352 "at offset %s"),
1353 call, func, sizrange[0], sizrange[1],
1354 offstr[0], offstr[1], ovlsiz[0], offstr[2]);
1355 else if (ovlsiz[1] >= 0 && ovlsiz[1] < maxobjsize.to_shwi ())
1356 warning_at (loc, OPT_Wrestrict,
1357 "%G%qD accessing between %wu and %wu bytes at "
1358 "offsets %s and %s overlaps between %wu and %wu "
1359 "bytes at offset %s",
1360 call, func, sizrange[0], sizrange[1],
1361 offstr[0], offstr[1], ovlsiz[0], ovlsiz[1],
1362 offstr[2]);
1363 else
1364 warning_at (loc, OPT_Wrestrict,
1365 "%G%qD accessing between %wu and %wu bytes at "
1366 "offsets %s and %s overlaps %wu or more bytes "
1367 "at offset %s",
1368 call, func, sizrange[0], sizrange[1],
1369 offstr[0], offstr[1], ovlsiz[0], offstr[2]);
1370 return true;
1373 if (ovlsiz[0] != ovlsiz[1])
1374 ovlsiz[1] = maxobjsize.to_shwi ();
1376 if (ovlsiz[0] == ovlsiz[1])
1377 warning_at (loc, OPT_Wrestrict,
1378 ovlsiz[0] == 1
1379 ? G_("%G%qD accessing %wu or more bytes at offsets "
1380 "%s and %s overlaps %wu byte at offset %s")
1381 : G_("%G%qD accessing %wu or more bytes at offsets "
1382 "%s and %s overlaps %wu bytes at offset %s"),
1383 call, func, sizrange[0], offstr[0], offstr[1],
1384 ovlsiz[0], offstr[2]);
1385 else if (ovlsiz[1] >= 0 && ovlsiz[1] < maxobjsize.to_shwi ())
1386 warning_at (loc, OPT_Wrestrict,
1387 "%G%qD accessing %wu or more bytes at offsets %s "
1388 "and %s overlaps between %wu and %wu bytes "
1389 "at offset %s",
1390 call, func, sizrange[0], offstr[0], offstr[1],
1391 ovlsiz[0], ovlsiz[1], offstr[2]);
1392 else
1393 warning_at (loc, OPT_Wrestrict,
1394 "%G%qD accessing %wu or more bytes at offsets %s "
1395 "and %s overlaps %wu or more bytes at offset %s",
1396 call, func, sizrange[0], offstr[0], offstr[1],
1397 ovlsiz[0], offstr[2]);
1398 return true;
1401 /* Use more concise wording when one of the offsets is unbounded
1402 to avoid confusing the user with large and mostly meaningless
1403 numbers. */
1404 bool open_range = ((dstref.offrange[0] == -maxobjsize - 1
1405 && dstref.offrange[1] == maxobjsize)
1406 || (srcref.offrange[0] == -maxobjsize - 1
1407 && srcref.offrange[1] == maxobjsize));
1409 if (sizrange[0] == sizrange[1] || sizrange[1] == 1)
1411 if (ovlsiz[1] == 1)
1413 if (open_range)
1414 warning_at (loc, OPT_Wrestrict,
1415 sizrange[1] == 1
1416 ? G_("%G%qD accessing %wu byte may overlap "
1417 "%wu byte")
1418 : G_("%G%qD accessing %wu bytes may overlap "
1419 "%wu byte"),
1420 call, func, sizrange[1], ovlsiz[1]);
1421 else
1422 warning_at (loc, OPT_Wrestrict,
1423 sizrange[1] == 1
1424 ? G_("%G%qD accessing %wu byte at offsets %s "
1425 "and %s may overlap %wu byte at offset %s")
1426 : G_("%G%qD accessing %wu bytes at offsets %s "
1427 "and %s may overlap %wu byte at offset %s"),
1428 call, func, sizrange[1], offstr[0], offstr[1],
1429 ovlsiz[1], offstr[2]);
1430 return true;
1433 if (open_range)
1434 warning_at (loc, OPT_Wrestrict,
1435 sizrange[1] == 1
1436 ? G_("%G%qD accessing %wu byte may overlap "
1437 "up to %wu bytes")
1438 : G_("%G%qD accessing %wu bytes may overlap "
1439 "up to %wu bytes"),
1440 call, func, sizrange[1], ovlsiz[1]);
1441 else
1442 warning_at (loc, OPT_Wrestrict,
1443 sizrange[1] == 1
1444 ? G_("%G%qD accessing %wu byte at offsets %s and "
1445 "%s may overlap up to %wu bytes at offset %s")
1446 : G_("%G%qD accessing %wu bytes at offsets %s and "
1447 "%s may overlap up to %wu bytes at offset %s"),
1448 call, func, sizrange[1], offstr[0], offstr[1],
1449 ovlsiz[1], offstr[2]);
1450 return true;
1453 if (sizrange[1] >= 0 && sizrange[1] < maxobjsize.to_shwi ())
1455 if (open_range)
1456 warning_at (loc, OPT_Wrestrict,
1457 ovlsiz[1] == 1
1458 ? G_("%G%qD accessing between %wu and %wu bytes "
1459 "may overlap %wu byte")
1460 : G_("%G%qD accessing between %wu and %wu bytes "
1461 "may overlap up to %wu bytes"),
1462 call, func, sizrange[0], sizrange[1], ovlsiz[1]);
1463 else
1464 warning_at (loc, OPT_Wrestrict,
1465 ovlsiz[1] == 1
1466 ? G_("%G%qD accessing between %wu and %wu bytes "
1467 "at offsets %s and %s may overlap %wu byte "
1468 "at offset %s")
1469 : G_("%G%qD accessing between %wu and %wu bytes "
1470 "at offsets %s and %s may overlap up to %wu "
1471 "bytes at offset %s"),
1472 call, func, sizrange[0], sizrange[1],
1473 offstr[0], offstr[1], ovlsiz[1], offstr[2]);
1474 return true;
1477 warning_at (loc, OPT_Wrestrict,
1478 ovlsiz[1] == 1
1479 ? G_("%G%qD accessing %wu or more bytes at offsets %s "
1480 "and %s may overlap %wu byte at offset %s")
1481 : G_("%G%qD accessing %wu or more bytes at offsets %s "
1482 "and %s may overlap up to %wu bytes at offset %s"),
1483 call, func, sizrange[0], offstr[0], offstr[1],
1484 ovlsiz[1], offstr[2]);
1486 return true;
1489 /* Validate REF offsets in an EXPRession passed as an argument to a CALL
1490 to a built-in function FUNC to make sure they are within the bounds
1491 of the referenced object if its size is known, or PTRDIFF_MAX otherwise.
1492 Both initial values of the offsets and their final value computed by
1493 the function by incrementing the initial value by the size are
1494 validated. Return true if the offsets are not valid and a diagnostic
1495 has been issued. */
1497 static bool
1498 maybe_diag_offset_bounds (location_t loc, gcall *call, tree func, int strict,
1499 tree expr, const builtin_memref &ref)
1501 if (!warn_array_bounds)
1502 return false;
1504 offset_int ooboff[] = { ref.offrange[0], ref.offrange[1] };
1505 tree oobref = ref.offset_out_of_bounds (strict, ooboff);
1506 if (!oobref)
1507 return false;
1509 if (EXPR_HAS_LOCATION (expr))
1510 loc = EXPR_LOCATION (expr);
1512 loc = expansion_point_location_if_in_system_header (loc);
1514 tree type;
1516 char rangestr[2][64];
1517 if (ooboff[0] == ooboff[1]
1518 || (ooboff[0] != ref.offrange[0]
1519 && ooboff[0].to_shwi () >= ooboff[1].to_shwi ()))
1520 sprintf (rangestr[0], "%lli", (long long) ooboff[0].to_shwi ());
1521 else
1522 sprintf (rangestr[0], "[%lli, %lli]",
1523 (long long) ooboff[0].to_shwi (),
1524 (long long) ooboff[1].to_shwi ());
1526 if (oobref == error_mark_node)
1528 if (ref.sizrange[0] == ref.sizrange[1])
1529 sprintf (rangestr[1], "%lli", (long long) ref.sizrange[0].to_shwi ());
1530 else
1531 sprintf (rangestr[1], "[%lli, %lli]",
1532 (long long) ref.sizrange[0].to_shwi (),
1533 (long long) ref.sizrange[1].to_shwi ());
1535 if (DECL_P (ref.base)
1536 && TREE_CODE (type = TREE_TYPE (ref.base)) == ARRAY_TYPE)
1538 if (warning_at (loc, OPT_Warray_bounds,
1539 "%G%qD pointer overflow between offset %s "
1540 "and size %s accessing array %qD with type %qT",
1541 call, func, rangestr[0], rangestr[1], ref.base, type))
1542 inform (DECL_SOURCE_LOCATION (ref.base),
1543 "array %qD declared here", ref.base);
1544 else
1545 warning_at (loc, OPT_Warray_bounds,
1546 "%G%qD pointer overflow between offset %s "
1547 "and size %s",
1548 call, func, rangestr[0], rangestr[1]);
1550 else
1551 warning_at (loc, OPT_Warray_bounds,
1552 "%G%qD pointer overflow between offset %s "
1553 "and size %s",
1554 call, func, rangestr[0], rangestr[1]);
1556 else if (oobref == ref.base)
1558 const offset_int maxobjsize = tree_to_shwi (max_object_size ());
1560 /* True when the offset formed by an access to the reference
1561 is out of bounds, rather than the initial offset wich is
1562 in bounds. This implies access past the end. */
1563 bool form = ooboff[0] != ref.offrange[0];
1565 if (DECL_P (ref.base))
1567 if ((ref.basesize < maxobjsize
1568 && warning_at (loc, OPT_Warray_bounds,
1569 form
1570 ? G_("%G%qD forming offset %s is out of "
1571 "the bounds [0, %wu] of object %qD with "
1572 "type %qT")
1573 : G_("%G%qD offset %s is out of the bounds "
1574 "[0, %wu] of object %qD with type %qT"),
1575 call, func, rangestr[0], ref.basesize.to_uhwi (),
1576 ref.base, TREE_TYPE (ref.base)))
1577 || warning_at (loc, OPT_Warray_bounds,
1578 form
1579 ? G_("%G%qD forming offset %s is out of "
1580 "the bounds of object %qD with type %qT")
1581 : G_("%G%qD offset %s is out of the bounds "
1582 "of object %qD with type %qT"),
1583 call, func, rangestr[0],
1584 ref.base, TREE_TYPE (ref.base)))
1585 inform (DECL_SOURCE_LOCATION (ref.base),
1586 "%qD declared here", ref.base);
1588 else if (ref.basesize < maxobjsize)
1589 warning_at (loc, OPT_Warray_bounds,
1590 form
1591 ? G_("%G%qD forming offset %s is out of the bounds "
1592 "[0, %wu]")
1593 : G_("%G%qD offset %s is out of the bounds [0, %wu]"),
1594 call, func, rangestr[0], ref.basesize.to_uhwi ());
1595 else
1596 warning_at (loc, OPT_Warray_bounds,
1597 form
1598 ? G_("%G%qD forming offset %s is out of bounds")
1599 : G_("%G%qD offset %s is out of bounds"),
1600 call, func, rangestr[0]);
1602 else if (TREE_CODE (ref.ref) == MEM_REF)
1604 tree type = TREE_TYPE (TREE_OPERAND (ref.ref, 0));
1605 if (POINTER_TYPE_P (type))
1606 type = TREE_TYPE (type);
1607 type = TYPE_MAIN_VARIANT (type);
1609 warning_at (loc, OPT_Warray_bounds,
1610 "%G%qD offset %s from the object at %qE is out "
1611 "of the bounds of %qT",
1612 call, func, rangestr[0], ref.base, type);
1614 else
1616 type = TYPE_MAIN_VARIANT (TREE_TYPE (ref.ref));
1618 warning_at (loc, OPT_Warray_bounds,
1619 "%G%qD offset %s from the object at %qE is out "
1620 "of the bounds of referenced subobject %qD with type %qT "
1621 "at offset %wu",
1622 call, func, rangestr[0], ref.base, TREE_OPERAND (ref.ref, 1),
1623 type, ref.refoff.to_uhwi ());
1626 return true;
1629 /* Check a CALL statement for restrict-violations and issue warnings
1630 if/when appropriate. */
1632 void
1633 wrestrict_dom_walker::check_call (gcall *call)
1635 /* Avoid checking the call if it has already been diagnosed for
1636 some reason. */
1637 if (gimple_no_warning_p (call))
1638 return;
1640 tree func = gimple_call_fndecl (call);
1641 if (!func || DECL_BUILT_IN_CLASS (func) != BUILT_IN_NORMAL)
1642 return;
1644 bool with_bounds = gimple_call_with_bounds_p (call);
1646 /* Argument number to extract from the call (depends on the built-in
1647 and its kind). */
1648 unsigned dst_idx = -1;
1649 unsigned src_idx = -1;
1650 unsigned bnd_idx = -1;
1652 /* Is this CALL to a string function (as opposed to one to a raw
1653 memory function). */
1654 bool strfun = true;
1656 switch (DECL_FUNCTION_CODE (func))
1658 case BUILT_IN_MEMCPY:
1659 case BUILT_IN_MEMCPY_CHK:
1660 case BUILT_IN_MEMCPY_CHKP:
1661 case BUILT_IN_MEMCPY_CHK_CHKP:
1662 case BUILT_IN_MEMPCPY:
1663 case BUILT_IN_MEMPCPY_CHK:
1664 case BUILT_IN_MEMPCPY_CHKP:
1665 case BUILT_IN_MEMPCPY_CHK_CHKP:
1666 case BUILT_IN_MEMMOVE:
1667 case BUILT_IN_MEMMOVE_CHK:
1668 case BUILT_IN_MEMMOVE_CHKP:
1669 case BUILT_IN_MEMMOVE_CHK_CHKP:
1670 strfun = false;
1671 /* Fall through. */
1673 case BUILT_IN_STPNCPY:
1674 case BUILT_IN_STPNCPY_CHK:
1675 case BUILT_IN_STRNCAT:
1676 case BUILT_IN_STRNCAT_CHK:
1677 case BUILT_IN_STRNCPY:
1678 case BUILT_IN_STRNCPY_CHK:
1679 dst_idx = 0;
1680 src_idx = 1 + with_bounds;
1681 bnd_idx = 2 + 2 * with_bounds;
1682 break;
1684 case BUILT_IN_STPCPY:
1685 case BUILT_IN_STPCPY_CHK:
1686 case BUILT_IN_STPCPY_CHKP:
1687 case BUILT_IN_STPCPY_CHK_CHKP:
1688 case BUILT_IN_STRCPY:
1689 case BUILT_IN_STRCPY_CHK:
1690 case BUILT_IN_STRCPY_CHKP:
1691 case BUILT_IN_STRCPY_CHK_CHKP:
1692 case BUILT_IN_STRCAT:
1693 case BUILT_IN_STRCAT_CHK:
1694 case BUILT_IN_STRCAT_CHKP:
1695 case BUILT_IN_STRCAT_CHK_CHKP:
1696 dst_idx = 0;
1697 src_idx = 1 + with_bounds;
1698 break;
1700 default:
1701 /* Handle other string functions here whose access may need
1702 to be validated for in-bounds offsets and non-overlapping
1703 copies. (Not all _chkp functions have BUILT_IN_XXX_CHKP
1704 macros so they need to be handled here.) */
1705 return;
1708 unsigned nargs = gimple_call_num_args (call);
1710 tree dst = dst_idx < nargs ? gimple_call_arg (call, dst_idx) : NULL_TREE;
1711 tree src = src_idx < nargs ? gimple_call_arg (call, src_idx) : NULL_TREE;
1712 tree dstwr = bnd_idx < nargs ? gimple_call_arg (call, bnd_idx) : NULL_TREE;
1714 /* For string functions with an unspecified or unknown bound,
1715 assume the size of the access is one. */
1716 if (!dstwr && strfun)
1717 dstwr = size_one_node;
1719 /* DST and SRC can be null for a call with an insufficient number
1720 of arguments to a built-in function declared without a protype. */
1721 if (!dst || !src)
1722 return;
1724 /* DST, SRC, or DSTWR can also have the wrong type in a call to
1725 a function declared without a prototype. Avoid checking such
1726 invalid calls. */
1727 if (TREE_CODE (TREE_TYPE (dst)) != POINTER_TYPE
1728 || TREE_CODE (TREE_TYPE (src)) != POINTER_TYPE
1729 || (dstwr && !INTEGRAL_TYPE_P (TREE_TYPE (dstwr))))
1730 return;
1732 if (check_bounds_or_overlap (call, dst, src, dstwr, NULL_TREE))
1733 return;
1735 /* Avoid diagnosing the call again. */
1736 gimple_set_no_warning (call, true);
1739 } /* anonymous namespace */
1741 /* Attempt to detect and diagnose invalid offset bounds and (except for
1742 memmove) overlapping copy in a call expression EXPR from SRC to DST
1743 and DSTSIZE and SRCSIZE bytes, respectively. Both DSTSIZE and
1744 SRCSIZE may be NULL. Return false when one or the other has been
1745 detected and diagnosed, true otherwise. */
1747 bool
1748 check_bounds_or_overlap (gcall *call, tree dst, tree src, tree dstsize,
1749 tree srcsize, bool bounds_only /* = false */)
1751 location_t loc = gimple_location (call);
1753 if (tree block = gimple_block (call))
1754 if (location_t *pbloc = block_nonartificial_location (block))
1755 loc = *pbloc;
1757 loc = expansion_point_location_if_in_system_header (loc);
1759 tree func = gimple_call_fndecl (call);
1761 builtin_memref dstref (dst, dstsize);
1762 builtin_memref srcref (src, srcsize);
1764 builtin_access acs (call, dstref, srcref);
1766 /* Set STRICT to the value of the -Warray-bounds=N argument for
1767 string functions or when N > 1. */
1768 int strict = (acs.strict () || warn_array_bounds > 1 ? warn_array_bounds : 0);
1770 /* Validate offsets first to make sure they are within the bounds
1771 of the destination object if its size is known, or PTRDIFF_MAX
1772 otherwise. */
1773 if (maybe_diag_offset_bounds (loc, call, func, strict, dst, dstref)
1774 || maybe_diag_offset_bounds (loc, call, func, strict, src, srcref))
1776 gimple_set_no_warning (call, true);
1777 return false;
1780 bool check_overlap
1781 = (warn_restrict
1782 && (bounds_only
1783 || (DECL_FUNCTION_CODE (func) != BUILT_IN_MEMMOVE
1784 && DECL_FUNCTION_CODE (func) != BUILT_IN_MEMMOVE_CHK)));
1786 if (!check_overlap)
1787 return true;
1789 if (operand_equal_p (dst, src, 0))
1791 warning_at (loc, OPT_Wrestrict,
1792 "%G%qD source argument is the same as destination",
1793 call, func);
1794 gimple_set_no_warning (call, true);
1795 return false;
1798 /* Return false when overlap has been detected. */
1799 if (maybe_diag_overlap (loc, call, acs))
1801 gimple_set_no_warning (call, true);
1802 return false;
1805 return true;
1808 gimple_opt_pass *
1809 make_pass_warn_restrict (gcc::context *ctxt)
1811 return new pass_wrestrict (ctxt);