aix: Support libsupc++ as a FAT library
[official-gcc.git] / gcc / gimple-ssa-warn-restrict.c
blob512fc138528e97107844eccf39e885c15d45ce1c
1 /* Pass to detect and issue warnings for violations of the restrict
2 qualifier.
3 Copyright (C) 2017-2020 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 "tree-cfg.h"
40 #include "tree-object-size.h"
41 #include "calls.h"
42 #include "cfgloop.h"
43 #include "intl.h"
45 namespace {
47 const pass_data pass_data_wrestrict = {
48 GIMPLE_PASS,
49 "wrestrict",
50 OPTGROUP_NONE,
51 TV_NONE,
52 PROP_cfg, /* Properties_required. */
53 0, /* properties_provided. */
54 0, /* properties_destroyed. */
55 0, /* properties_start */
56 0, /* properties_finish */
59 /* Pass to detect violations of strict aliasing requirements in calls
60 to built-in string and raw memory functions. */
61 class pass_wrestrict : public gimple_opt_pass
63 public:
64 pass_wrestrict (gcc::context *ctxt)
65 : gimple_opt_pass (pass_data_wrestrict, ctxt)
66 { }
68 opt_pass *clone () { return new pass_wrestrict (m_ctxt); }
70 virtual bool gate (function *);
71 virtual unsigned int execute (function *);
74 bool
75 pass_wrestrict::gate (function *fun ATTRIBUTE_UNUSED)
77 return warn_array_bounds || warn_restrict || warn_stringop_overflow;
80 /* Class to walk the basic blocks of a function in dominator order. */
81 class wrestrict_dom_walker : public dom_walker
83 public:
84 wrestrict_dom_walker () : dom_walker (CDI_DOMINATORS) {}
86 edge before_dom_children (basic_block) FINAL OVERRIDE;
87 bool handle_gimple_call (gimple_stmt_iterator *);
89 private:
90 void check_call (gimple *);
93 edge
94 wrestrict_dom_walker::before_dom_children (basic_block bb)
96 /* Iterate over statements, looking for function calls. */
97 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
98 gsi_next (&si))
100 gimple *stmt = gsi_stmt (si);
101 if (!is_gimple_call (stmt))
102 continue;
104 check_call (stmt);
107 return NULL;
110 /* Execute the pass for function FUN, walking in dominator order. */
112 unsigned
113 pass_wrestrict::execute (function *fun)
115 calculate_dominance_info (CDI_DOMINATORS);
117 wrestrict_dom_walker walker;
118 walker.walk (ENTRY_BLOCK_PTR_FOR_FN (fun));
120 return 0;
123 /* Description of a memory reference by a built-in function. This
124 is similar to ao_ref but made especially suitable for -Wrestrict
125 and not for optimization. */
126 class builtin_memref
128 public:
129 /* The original pointer argument to the built-in function. */
130 tree ptr;
131 /* The referenced subobject or NULL if not available, and the base
132 object of the memory reference or NULL. */
133 tree ref;
134 tree base;
136 /* The size of the BASE object, PTRDIFF_MAX if indeterminate,
137 and negative until (possibly lazily) initialized. */
138 offset_int basesize;
139 /* Same for the subobject. */
140 offset_int refsize;
142 /* The non-negative offset of the referenced subobject. Used to avoid
143 warnings for (apparently) possibly but not definitively overlapping
144 accesses to member arrays. Negative when unknown/invalid. */
145 offset_int refoff;
147 /* The offset range relative to the base. */
148 offset_int offrange[2];
149 /* The size range of the access to this reference. */
150 offset_int sizrange[2];
152 /* Cached result of get_max_objsize(). */
153 const offset_int maxobjsize;
155 /* True for "bounded" string functions like strncat, and strncpy
156 and their variants that specify either an exact or upper bound
157 on the size of the accesses they perform. For strncat both
158 the source and destination references are bounded. For strncpy
159 only the destination reference is. */
160 bool strbounded_p;
162 builtin_memref (tree, tree);
164 tree offset_out_of_bounds (int, offset_int[3]) const;
166 private:
168 /* Ctor helper to set or extend OFFRANGE based on argument. */
169 void extend_offset_range (tree);
171 /* Ctor helper to determine BASE and OFFRANGE from argument. */
172 void set_base_and_offset (tree);
175 /* Description of a memory access by a raw memory or string built-in
176 function involving a pair of builtin_memref's. */
177 class builtin_access
179 public:
180 /* Destination and source memory reference. */
181 builtin_memref* const dstref;
182 builtin_memref* const srcref;
183 /* The size range of the access. It's the greater of the accesses
184 to the two references. */
185 HOST_WIDE_INT sizrange[2];
187 /* The minimum and maximum offset of an overlap of the access
188 (if it does, in fact, overlap), and the size of the overlap. */
189 HOST_WIDE_INT ovloff[2];
190 HOST_WIDE_INT ovlsiz[2];
192 /* True to consider valid only accesses to the smallest subobject
193 and false for raw memory functions. */
194 bool strict () const
196 return (detect_overlap != &builtin_access::generic_overlap
197 && detect_overlap != &builtin_access::no_overlap);
200 builtin_access (gimple *, builtin_memref &, builtin_memref &);
202 /* Entry point to determine overlap. */
203 bool overlap ();
205 offset_int write_off (tree) const;
207 void dump (FILE *) const;
209 private:
210 /* Implementation functions used to determine overlap. */
211 bool generic_overlap ();
212 bool strcat_overlap ();
213 bool strcpy_overlap ();
215 bool no_overlap ()
217 return false;
220 offset_int overlap_size (const offset_int [2], const offset_int[2],
221 offset_int [2]);
223 private:
224 /* Temporaries used to compute the final result. */
225 offset_int dstoff[2];
226 offset_int srcoff[2];
227 offset_int dstsiz[2];
228 offset_int srcsiz[2];
230 /* Pointer to a member function to call to determine overlap. */
231 bool (builtin_access::*detect_overlap) ();
234 /* Initialize a memory reference representation from a pointer EXPR and
235 a size SIZE in bytes. If SIZE is NULL_TREE then the size is assumed
236 to be unknown. */
238 builtin_memref::builtin_memref (tree expr, tree size)
239 : ptr (expr),
240 ref (),
241 base (),
242 basesize (-1),
243 refsize (-1),
244 refoff (HOST_WIDE_INT_MIN),
245 offrange (),
246 sizrange (),
247 maxobjsize (tree_to_shwi (max_object_size ())),
248 strbounded_p ()
250 /* Unfortunately, wide_int default ctor is a no-op so array members
251 of the type must be set individually. */
252 offrange[0] = offrange[1] = 0;
253 sizrange[0] = sizrange[1] = 0;
255 if (!expr)
256 return;
258 /* Find the BASE object or pointer referenced by EXPR and set
259 the offset range OFFRANGE in the process. */
260 set_base_and_offset (expr);
262 if (size)
264 tree range[2];
265 /* Determine the size range, allowing for the result to be [0, 0]
266 for SIZE in the anti-range ~[0, N] where N >= PTRDIFF_MAX. */
267 get_size_range (size, range, true);
268 sizrange[0] = wi::to_offset (range[0]);
269 sizrange[1] = wi::to_offset (range[1]);
270 /* get_size_range returns SIZE_MAX for the maximum size.
271 Constrain it to the real maximum of PTRDIFF_MAX. */
272 if (sizrange[0] <= maxobjsize && sizrange[1] > maxobjsize)
273 sizrange[1] = maxobjsize;
275 else
276 sizrange[1] = maxobjsize;
278 if (!DECL_P (base))
279 return;
281 /* If the offset could be in the range of the referenced object
282 constrain its bounds so neither exceeds those of the object. */
283 if (offrange[0] < 0 && offrange[1] > 0)
284 offrange[0] = 0;
286 offset_int maxoff = maxobjsize;
287 tree basetype = TREE_TYPE (base);
288 if (TREE_CODE (basetype) == ARRAY_TYPE)
290 if (ref && array_at_struct_end_p (ref))
291 ; /* Use the maximum possible offset for last member arrays. */
292 else if (tree basesize = TYPE_SIZE_UNIT (basetype))
293 if (TREE_CODE (basesize) == INTEGER_CST)
294 /* Size could be non-constant for a variable-length type such
295 as a struct with a VLA member (a GCC extension). */
296 maxoff = wi::to_offset (basesize);
299 if (offrange[0] >= 0)
301 if (offrange[1] < 0)
302 offrange[1] = offrange[0] <= maxoff ? maxoff : maxobjsize;
303 else if (offrange[0] <= maxoff && offrange[1] > maxoff)
304 offrange[1] = maxoff;
308 /* Based on the initial length of the destination STARTLEN, returns
309 the offset of the first write access from the beginning of
310 the destination. Nonzero only for strcat-type of calls. */
312 offset_int builtin_access::write_off (tree startlen) const
314 if (detect_overlap != &builtin_access::strcat_overlap
315 || !startlen || TREE_CODE (startlen) != INTEGER_CST)
316 return 0;
318 return wi::to_offset (startlen);
321 /* Ctor helper to set or extend OFFRANGE based on the OFFSET argument.
322 Pointer offsets are represented as unsigned sizetype but must be
323 treated as signed. */
325 void
326 builtin_memref::extend_offset_range (tree offset)
328 if (TREE_CODE (offset) == INTEGER_CST)
330 offset_int off = int_cst_value (offset);
331 if (off != 0)
333 offrange[0] += off;
334 offrange[1] += off;
336 return;
339 if (TREE_CODE (offset) == SSA_NAME)
341 /* A pointer offset is represented as sizetype but treated
342 as signed. */
343 wide_int min, max;
344 value_range_kind rng = get_range_info (offset, &min, &max);
345 if (rng == VR_ANTI_RANGE && wi::lts_p (max, min))
347 /* Convert an anti-range whose upper bound is less than
348 its lower bound to a signed range. */
349 offrange[0] += offset_int::from (max + 1, SIGNED);
350 offrange[1] += offset_int::from (min - 1, SIGNED);
351 return;
354 if (rng == VR_RANGE
355 && (DECL_P (base) || wi::lts_p (min, max)))
357 /* Preserve the bounds of the range for an offset into
358 a known object (it may be adjusted later relative to
359 a constant offset from its beginning). Otherwise use
360 the bounds only when they are ascending when treated
361 as signed. */
362 offrange[0] += offset_int::from (min, SIGNED);
363 offrange[1] += offset_int::from (max, SIGNED);
364 return;
367 /* Handle an anti-range the same as no range at all. */
368 gimple *stmt = SSA_NAME_DEF_STMT (offset);
369 tree type;
370 if (is_gimple_assign (stmt)
371 && (type = TREE_TYPE (gimple_assign_rhs1 (stmt)))
372 && INTEGRAL_TYPE_P (type))
374 tree_code code = gimple_assign_rhs_code (stmt);
375 if (code == NOP_EXPR)
377 /* Use the bounds of the type of the NOP_EXPR operand
378 even if it's signed. The result doesn't trigger
379 warnings but makes their output more readable. */
380 offrange[0] += wi::to_offset (TYPE_MIN_VALUE (type));
381 offrange[1] += wi::to_offset (TYPE_MAX_VALUE (type));
382 return;
387 const offset_int maxoff = tree_to_shwi (max_object_size ()) >> 1;
388 const offset_int minoff = -maxoff - 1;
390 offrange[0] += minoff;
391 offrange[1] += maxoff;
394 /* Determines the base object or pointer of the reference EXPR
395 and the offset range from the beginning of the base. */
397 void
398 builtin_memref::set_base_and_offset (tree expr)
400 tree offset = NULL_TREE;
402 if (TREE_CODE (expr) == SSA_NAME)
404 /* Try to tease the offset out of the pointer. */
405 gimple *stmt = SSA_NAME_DEF_STMT (expr);
406 if (!base
407 && gimple_assign_single_p (stmt)
408 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
409 expr = gimple_assign_rhs1 (stmt);
410 else if (is_gimple_assign (stmt))
412 tree_code code = gimple_assign_rhs_code (stmt);
413 if (code == NOP_EXPR)
415 tree rhs = gimple_assign_rhs1 (stmt);
416 if (POINTER_TYPE_P (TREE_TYPE (rhs)))
417 expr = gimple_assign_rhs1 (stmt);
418 else
420 base = expr;
421 return;
424 else if (code == POINTER_PLUS_EXPR)
426 expr = gimple_assign_rhs1 (stmt);
427 offset = gimple_assign_rhs2 (stmt);
429 else
431 base = expr;
432 return;
435 else
437 /* FIXME: Handle PHI nodes in case like:
438 _12 = &MEM[(void *)&a + 2B] + _10;
440 <bb> [local count: 1073741824]:
441 # prephitmp_13 = PHI <_12, &MEM[(void *)&a + 2B]>
442 memcpy (prephitmp_13, p_7(D), 6); */
443 base = expr;
444 return;
448 if (TREE_CODE (expr) == ADDR_EXPR)
449 expr = TREE_OPERAND (expr, 0);
451 /* Stash the reference for offset validation. */
452 ref = expr;
454 poly_int64 bitsize, bitpos;
455 tree var_off;
456 machine_mode mode;
457 int sign, reverse, vol;
459 /* Determine the base object or pointer of the reference and
460 the constant bit offset from the beginning of the base.
461 If the offset has a non-constant component, it will be in
462 VAR_OFF. MODE, SIGN, REVERSE, and VOL are write only and
463 unused here. */
464 base = get_inner_reference (expr, &bitsize, &bitpos, &var_off,
465 &mode, &sign, &reverse, &vol);
467 /* get_inner_reference is not expected to return null. */
468 gcc_assert (base != NULL);
470 if (offset)
471 extend_offset_range (offset);
473 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
475 /* Convert the poly_int64 offset to offset_int. The offset
476 should be constant but be prepared for it not to be just in
477 case. */
478 offset_int cstoff;
479 if (bytepos.is_constant (&cstoff))
481 offrange[0] += cstoff;
482 offrange[1] += cstoff;
484 /* Besides the reference saved above, also stash the offset
485 for validation. */
486 if (TREE_CODE (expr) == COMPONENT_REF)
487 refoff = cstoff;
489 else
490 offrange[1] += maxobjsize;
492 if (var_off)
494 if (TREE_CODE (var_off) == INTEGER_CST)
496 cstoff = wi::to_offset (var_off);
497 offrange[0] += cstoff;
498 offrange[1] += cstoff;
500 else
501 offrange[1] += maxobjsize;
504 if (TREE_CODE (base) == MEM_REF)
506 tree memrefoff = fold_convert (ptrdiff_type_node, TREE_OPERAND (base, 1));
507 extend_offset_range (memrefoff);
508 base = TREE_OPERAND (base, 0);
510 if (refoff != HOST_WIDE_INT_MIN
511 && TREE_CODE (expr) == COMPONENT_REF)
513 /* Bump up the offset of the referenced subobject to reflect
514 the offset to the enclosing object. For example, so that
516 struct S { char a, b[3]; } s[2];
517 strcpy (s[1].b, "1234");
518 REFOFF is set to s[1].b - (char*)s. */
519 offset_int off = tree_to_shwi (memrefoff);
520 refoff += off;
523 if (!integer_zerop (memrefoff))
524 /* A non-zero offset into an array of struct with flexible array
525 members implies that the array is empty because there is no
526 way to initialize such a member when it belongs to an array.
527 This must be some sort of a bug. */
528 refsize = 0;
531 if (TREE_CODE (ref) == COMPONENT_REF)
532 if (tree size = component_ref_size (ref))
533 if (TREE_CODE (size) == INTEGER_CST)
534 refsize = wi::to_offset (size);
536 if (TREE_CODE (base) == SSA_NAME)
537 set_base_and_offset (base);
540 /* Return error_mark_node if the signed offset exceeds the bounds
541 of the address space (PTRDIFF_MAX). Otherwise, return either BASE
542 or REF when the offset exceeds the bounds of the BASE or REF object,
543 and set OOBOFF to the past-the-end offset formed by the reference,
544 including its size. OOBOFF is initially setto the range of offsets,
545 and OOBOFF[2] to the offset of the first write access (nonzero for
546 the strcat family). When STRICT is nonzero use REF size, when
547 available, otherwise use BASE size. When STRICT is greater than 1,
548 use the size of the last array member as the bound, otherwise treat
549 such a member as a flexible array member. Return NULL when the offset
550 is in bounds. */
552 tree
553 builtin_memref::offset_out_of_bounds (int strict, offset_int ooboff[3]) const
555 if (!ptr)
556 return NULL_TREE;
558 /* The offset of the first write access or zero. */
559 offset_int wroff = ooboff[2];
561 /* A temporary, possibly adjusted, copy of the offset range. */
562 offset_int offrng[2] = { ooboff[0], ooboff[1] };
564 if (DECL_P (base) && TREE_CODE (TREE_TYPE (base)) == ARRAY_TYPE)
566 /* Check for offset in an anti-range with a negative lower bound.
567 For such a range, consider only the non-negative subrange. */
568 if (offrng[1] < offrng[0] && offrng[1] < 0)
569 offrng[1] = maxobjsize;
572 /* Conservative offset of the last byte of the referenced object. */
573 offset_int endoff;
575 /* The bounds need not be ordered. Set HIB to use as the index
576 of the larger of the bounds and LOB as the opposite. */
577 bool hib = wi::les_p (offrng[0], offrng[1]);
578 bool lob = !hib;
580 /* Set to the size remaining in the object after subtracting
581 REFOFF. It may become negative as a result of negative indices
582 into the enclosing object, such as in:
583 extern struct S { char a[4], b[3], c[1]; } *p;
584 strcpy (p[-3].b, "123"); */
585 offset_int size = basesize;
586 tree obj = base;
588 const bool decl_p = DECL_P (obj);
590 if (basesize < 0)
592 endoff = offrng[lob] + (sizrange[0] - wroff);
594 /* For a reference through a pointer to an object of unknown size
595 all initial offsets are considered valid, positive as well as
596 negative, since the pointer itself can point past the beginning
597 of the object. However, the sum of the lower bound of the offset
598 and that of the size must be less than or equal than PTRDIFF_MAX. */
599 if (endoff > maxobjsize)
600 return error_mark_node;
602 /* When the referenced subobject is known, the end offset must be
603 within its bounds. Otherwise there is nothing to do. */
604 if (strict
605 && !decl_p
606 && ref
607 && refsize >= 0
608 && TREE_CODE (ref) == COMPONENT_REF)
610 /* If REFOFF is negative, SIZE will become negative here. */
611 size = refoff + refsize;
612 obj = ref;
614 else
615 return NULL_TREE;
618 /* A reference to an object of known size must be within the bounds
619 of either the base object or the subobject (see above for when
620 a subobject can be used). */
621 if ((decl_p && offrng[hib] < 0) || offrng[lob] > size)
622 return obj;
624 /* The extent of the reference must also be within the bounds of
625 the base object (if known) or the subobject or the maximum object
626 size otherwise. */
627 endoff = offrng[lob] + sizrange[0];
628 if (endoff > maxobjsize)
629 return error_mark_node;
631 if (strict
632 && decl_p
633 && ref
634 && refsize >= 0
635 && TREE_CODE (ref) == COMPONENT_REF)
637 /* If the reference is to a member subobject of a declared object,
638 the offset must be within the bounds of the subobject. */
639 size = refoff + refsize;
640 obj = ref;
643 if (endoff <= size)
644 return NULL_TREE;
646 /* Set the out-of-bounds offset range to be one greater than
647 that delimited by the reference including its size. */
648 ooboff[lob] = size;
650 if (endoff > ooboff[lob])
651 ooboff[hib] = endoff - 1;
652 else
653 ooboff[hib] = offrng[lob] + sizrange[1];
655 return obj;
658 /* Create an association between the memory references DST and SRC
659 for access by a call EXPR to a memory or string built-in funtion. */
661 builtin_access::builtin_access (gimple *call, builtin_memref &dst,
662 builtin_memref &src)
663 : dstref (&dst), srcref (&src), sizrange (), ovloff (), ovlsiz (),
664 dstoff (), srcoff (), dstsiz (), srcsiz ()
666 dstoff[0] = dst.offrange[0];
667 dstoff[1] = dst.offrange[1];
669 /* Zero out since the offset_int ctors invoked above are no-op. */
670 srcoff[0] = srcoff[1] = 0;
671 dstsiz[0] = dstsiz[1] = 0;
672 srcsiz[0] = srcsiz[1] = 0;
674 /* Object Size Type to use to determine the size of the destination
675 and source objects. Overridden below for raw memory functions. */
676 int ostype = 1;
678 /* True when the size of one reference depends on the offset of
679 itself or the other. */
680 bool depends_p = true;
682 /* True when the size of the destination reference DSTREF has been
683 determined from SRCREF and so needs to be adjusted by the latter's
684 offset. Only meaningful for bounded string functions like strncpy. */
685 bool dstadjust_p = false;
687 /* The size argument number (depends on the built-in). */
688 unsigned sizeargno = 2;
690 tree func = gimple_call_fndecl (call);
691 switch (DECL_FUNCTION_CODE (func))
693 case BUILT_IN_MEMCPY:
694 case BUILT_IN_MEMCPY_CHK:
695 case BUILT_IN_MEMPCPY:
696 case BUILT_IN_MEMPCPY_CHK:
697 ostype = 0;
698 depends_p = false;
699 detect_overlap = &builtin_access::generic_overlap;
700 break;
702 case BUILT_IN_MEMMOVE:
703 case BUILT_IN_MEMMOVE_CHK:
704 /* For memmove there is never any overlap to check for. */
705 ostype = 0;
706 depends_p = false;
707 detect_overlap = &builtin_access::no_overlap;
708 break;
710 case BUILT_IN_MEMSET:
711 case BUILT_IN_MEMSET_CHK:
712 /* For memset there is never any overlap to check for. */
713 ostype = 0;
714 depends_p = false;
715 detect_overlap = &builtin_access::no_overlap;
716 break;
718 case BUILT_IN_STPNCPY:
719 case BUILT_IN_STPNCPY_CHK:
720 case BUILT_IN_STRNCPY:
721 case BUILT_IN_STRNCPY_CHK:
722 dstref->strbounded_p = true;
723 detect_overlap = &builtin_access::strcpy_overlap;
724 break;
726 case BUILT_IN_STPCPY:
727 case BUILT_IN_STPCPY_CHK:
728 case BUILT_IN_STRCPY:
729 case BUILT_IN_STRCPY_CHK:
730 detect_overlap = &builtin_access::strcpy_overlap;
731 break;
733 case BUILT_IN_STRCAT:
734 case BUILT_IN_STRCAT_CHK:
735 detect_overlap = &builtin_access::strcat_overlap;
736 break;
738 case BUILT_IN_STRNCAT:
739 case BUILT_IN_STRNCAT_CHK:
740 dstref->strbounded_p = true;
741 srcref->strbounded_p = true;
742 detect_overlap = &builtin_access::strcat_overlap;
743 break;
745 default:
746 /* Handle other string functions here whose access may need
747 to be validated for in-bounds offsets and non-overlapping
748 copies. */
749 return;
752 const offset_int maxobjsize = dst.maxobjsize;
754 /* Try to determine the size of the base object. compute_objsize
755 expects a pointer so create one if BASE is a non-pointer object. */
756 tree addr;
757 if (dst.basesize < 0)
759 addr = dst.base;
760 if (!POINTER_TYPE_P (TREE_TYPE (addr)))
761 addr = build1 (ADDR_EXPR, (TREE_TYPE (addr)), addr);
763 if (tree dstsize = compute_objsize (addr, ostype))
764 dst.basesize = wi::to_offset (dstsize);
765 else if (POINTER_TYPE_P (TREE_TYPE (addr)))
766 dst.basesize = HOST_WIDE_INT_MIN;
767 else
768 dst.basesize = maxobjsize;
771 if (src.base && src.basesize < 0)
773 addr = src.base;
774 if (!POINTER_TYPE_P (TREE_TYPE (addr)))
775 addr = build1 (ADDR_EXPR, (TREE_TYPE (addr)), addr);
777 if (tree srcsize = compute_objsize (addr, ostype))
778 src.basesize = wi::to_offset (srcsize);
779 else if (POINTER_TYPE_P (TREE_TYPE (addr)))
780 src.basesize = HOST_WIDE_INT_MIN;
781 else
782 src.basesize = maxobjsize;
785 /* Make adjustments for references to the same object by string
786 built-in functions to reflect the constraints imposed by
787 the function. */
789 /* For bounded string functions determine the range of the bound
790 on the access. For others, the range stays unbounded. */
791 offset_int bounds[2] = { maxobjsize, maxobjsize };
792 if (dstref->strbounded_p)
794 unsigned nargs = gimple_call_num_args (call);
795 if (nargs <= sizeargno)
796 return;
798 tree size = gimple_call_arg (call, sizeargno);
799 tree range[2];
800 if (get_size_range (size, range, true))
802 bounds[0] = wi::to_offset (range[0]);
803 bounds[1] = wi::to_offset (range[1]);
806 /* If both references' size ranges are indeterminate use the last
807 (size) argument from the function call as a substitute. This
808 may only be necessary for strncpy (but not for memcpy where
809 the size range would have been already determined this way). */
810 if (dstref->sizrange[0] == 0 && dstref->sizrange[1] == maxobjsize
811 && srcref->sizrange[0] == 0 && srcref->sizrange[1] == maxobjsize)
813 dstref->sizrange[0] = bounds[0];
814 dstref->sizrange[1] = bounds[1];
818 bool dstsize_set = false;
819 /* The size range of one reference involving the same base object
820 can be determined from the size range of the other reference.
821 This makes it possible to compute accurate offsets for warnings
822 involving functions like strcpy where the length of just one of
823 the two arguments is known (determined by tree-ssa-strlen). */
824 if (dstref->sizrange[0] == 0 && dstref->sizrange[1] == maxobjsize)
826 /* When the destination size is unknown set it to the size of
827 the source. */
828 dstref->sizrange[0] = srcref->sizrange[0];
829 dstref->sizrange[1] = srcref->sizrange[1];
830 dstsize_set = true;
832 else if (srcref->sizrange[0] == 0 && srcref->sizrange[1] == maxobjsize)
834 /* When the size of the source access is unknown set it to the size
835 of the destination first and adjust it later if necessary. */
836 srcref->sizrange[0] = dstref->sizrange[0];
837 srcref->sizrange[1] = dstref->sizrange[1];
839 if (depends_p)
841 if (dstref->strbounded_p)
843 /* Read access by strncpy is constrained by the third
844 argument but except for a zero bound is at least one. */
845 srcref->sizrange[0] = bounds[1] > 0 ? 1 : 0;
846 offset_int bound = wi::umin (srcref->basesize, bounds[1]);
847 if (bound < srcref->sizrange[1])
848 srcref->sizrange[1] = bound;
850 /* For string functions, adjust the size range of the source
851 reference by the inverse boundaries of the offset (because
852 the higher the offset into the string the shorter its
853 length). */
854 if (srcref->offrange[1] >= 0
855 && srcref->offrange[1] < srcref->sizrange[0])
856 srcref->sizrange[0] -= srcref->offrange[1];
857 else
858 srcref->sizrange[0] = 1;
860 if (srcref->offrange[0] > 0)
862 if (srcref->offrange[0] < srcref->sizrange[1])
863 srcref->sizrange[1] -= srcref->offrange[0];
864 else
865 srcref->sizrange[1] = 0;
868 dstadjust_p = true;
872 if (detect_overlap == &builtin_access::generic_overlap)
874 if (dstref->strbounded_p)
876 dstref->sizrange[0] = bounds[0];
877 dstref->sizrange[1] = bounds[1];
879 if (dstref->sizrange[0] < srcref->sizrange[0])
880 srcref->sizrange[0] = dstref->sizrange[0];
882 if (dstref->sizrange[1] < srcref->sizrange[1])
883 srcref->sizrange[1] = dstref->sizrange[1];
886 else if (detect_overlap == &builtin_access::strcpy_overlap)
888 if (!dstref->strbounded_p)
890 /* For strcpy, adjust the destination size range to match that
891 of the source computed above. */
892 if (depends_p && dstadjust_p)
894 dstref->sizrange[0] = srcref->sizrange[0];
895 dstref->sizrange[1] = srcref->sizrange[1];
899 else if (!dstsize_set && detect_overlap == &builtin_access::strcat_overlap)
901 dstref->sizrange[0] += srcref->sizrange[0] - 1;
902 dstref->sizrange[1] += srcref->sizrange[1] - 1;
905 if (dstref->strbounded_p)
907 /* For strncpy, adjust the destination size range to match that
908 of the source computed above. */
909 dstref->sizrange[0] = bounds[0];
910 dstref->sizrange[1] = bounds[1];
912 if (bounds[0] < srcref->sizrange[0])
913 srcref->sizrange[0] = bounds[0];
915 if (bounds[1] < srcref->sizrange[1])
916 srcref->sizrange[1] = bounds[1];
920 offset_int
921 builtin_access::overlap_size (const offset_int a[2], const offset_int b[2],
922 offset_int *off)
924 const offset_int *p = a;
925 const offset_int *q = b;
927 /* Point P at the bigger of the two ranges and Q at the smaller. */
928 if (wi::lts_p (a[1] - a[0], b[1] - b[0]))
930 p = b;
931 q = a;
934 if (p[0] < q[0])
936 if (p[1] < q[0])
937 return 0;
939 *off = q[0];
940 return wi::smin (p[1], q[1]) - q[0];
943 if (q[1] < p[0])
944 return 0;
946 off[0] = p[0];
947 return q[1] - p[0];
950 /* Return true if the bounded mempry (memcpy amd similar) or string function
951 access (strncpy and similar) ACS overlaps. */
953 bool
954 builtin_access::generic_overlap ()
956 builtin_access &acs = *this;
957 const builtin_memref *dstref = acs.dstref;
958 const builtin_memref *srcref = acs.srcref;
960 gcc_assert (dstref->base == srcref->base);
962 const offset_int maxobjsize = acs.dstref->maxobjsize;
964 offset_int maxsize = dstref->basesize < 0 ? maxobjsize : dstref->basesize;
966 /* Adjust the larger bounds of the offsets (which may be the first
967 element if the lower bound is larger than the upper bound) to
968 make them valid for the smallest access (if possible) but no smaller
969 than the smaller bounds. */
970 gcc_assert (wi::les_p (acs.dstoff[0], acs.dstoff[1]));
972 if (maxsize < acs.dstoff[1] + acs.dstsiz[0])
973 acs.dstoff[1] = maxsize - acs.dstsiz[0];
974 if (acs.dstoff[1] < acs.dstoff[0])
975 acs.dstoff[1] = acs.dstoff[0];
977 gcc_assert (wi::les_p (acs.srcoff[0], acs.srcoff[1]));
979 if (maxsize < acs.srcoff[1] + acs.srcsiz[0])
980 acs.srcoff[1] = maxsize - acs.srcsiz[0];
981 if (acs.srcoff[1] < acs.srcoff[0])
982 acs.srcoff[1] = acs.srcoff[0];
984 /* Determine the minimum and maximum space for the access given
985 the offsets. */
986 offset_int space[2];
987 space[0] = wi::abs (acs.dstoff[0] - acs.srcoff[0]);
988 space[1] = space[0];
990 offset_int d = wi::abs (acs.dstoff[0] - acs.srcoff[1]);
991 if (acs.srcsiz[0] > 0)
993 if (d < space[0])
994 space[0] = d;
996 if (space[1] < d)
997 space[1] = d;
999 else
1000 space[1] = acs.dstsiz[1];
1002 d = wi::abs (acs.dstoff[1] - acs.srcoff[0]);
1003 if (d < space[0])
1004 space[0] = d;
1006 if (space[1] < d)
1007 space[1] = d;
1009 /* Treat raw memory functions both of whose references are bounded
1010 as special and permit uncertain overlaps to go undetected. For
1011 all kinds of constant offset and constant size accesses, if
1012 overlap isn't certain it is not possible. */
1013 bool overlap_possible = space[0] < acs.dstsiz[1];
1014 if (!overlap_possible)
1015 return false;
1017 bool overlap_certain = space[1] < acs.dstsiz[0];
1019 /* True when the size of one reference depends on the offset of
1020 the other. */
1021 bool depends_p = detect_overlap != &builtin_access::generic_overlap;
1023 if (!overlap_certain)
1025 if (!dstref->strbounded_p && !depends_p)
1026 /* Memcpy only considers certain overlap. */
1027 return false;
1029 /* There's no way to distinguish an access to the same member
1030 of a structure from one to two distinct members of the same
1031 structure. Give up to avoid excessive false positives. */
1032 tree basetype = TREE_TYPE (dstref->base);
1034 if (POINTER_TYPE_P (basetype))
1035 basetype = TREE_TYPE (basetype);
1036 else
1037 while (TREE_CODE (basetype) == ARRAY_TYPE)
1038 basetype = TREE_TYPE (basetype);
1040 if (RECORD_OR_UNION_TYPE_P (basetype))
1041 return false;
1044 /* True for stpcpy and strcpy. */
1045 bool stxcpy_p = (!dstref->strbounded_p
1046 && detect_overlap == &builtin_access::strcpy_overlap);
1048 if (dstref->refoff >= 0
1049 && srcref->refoff >= 0
1050 && dstref->refoff != srcref->refoff
1051 && (stxcpy_p || dstref->strbounded_p || srcref->strbounded_p))
1052 return false;
1054 offset_int siz[2] = { maxobjsize + 1, 0 };
1056 ovloff[0] = HOST_WIDE_INT_MAX;
1057 ovloff[1] = HOST_WIDE_INT_MIN;
1059 if (stxcpy_p)
1061 /* Iterate over the extreme locations (on the horizontal axis formed
1062 by their offsets) and sizes of two regions and find their smallest
1063 and largest overlap and the corresponding offsets. */
1064 for (unsigned i = 0; i != 2; ++i)
1066 const offset_int a[2] = {
1067 acs.dstoff[i], acs.dstoff[i] + acs.dstsiz[!i]
1070 const offset_int b[2] = {
1071 acs.srcoff[i], acs.srcoff[i] + acs.srcsiz[!i]
1074 offset_int off;
1075 offset_int sz = overlap_size (a, b, &off);
1076 if (sz < siz[0])
1077 siz[0] = sz;
1079 if (siz[1] <= sz)
1080 siz[1] = sz;
1082 if (sz != 0)
1084 if (wi::lts_p (off, ovloff[0]))
1085 ovloff[0] = off.to_shwi ();
1086 if (wi::lts_p (ovloff[1], off))
1087 ovloff[1] = off.to_shwi ();
1091 else
1093 /* Iterate over the extreme locations (on the horizontal axis
1094 formed by their offsets) and sizes of the two regions and
1095 find their smallest and largest overlap and the corresponding
1096 offsets. */
1098 for (unsigned io = 0; io != 2; ++io)
1099 for (unsigned is = 0; is != 2; ++is)
1101 const offset_int a[2] = {
1102 acs.dstoff[io], acs.dstoff[io] + acs.dstsiz[is]
1105 for (unsigned jo = 0; jo != 2; ++jo)
1106 for (unsigned js = 0; js != 2; ++js)
1108 const offset_int b[2] = {
1109 acs.srcoff[jo], acs.srcoff[jo] + acs.srcsiz[js]
1112 offset_int off;
1113 offset_int sz = overlap_size (a, b, &off);
1114 if (sz < siz[0])
1115 siz[0] = sz;
1117 if (siz[1] <= sz)
1118 siz[1] = sz;
1120 if (sz != 0)
1122 if (wi::lts_p (off, ovloff[0]))
1123 ovloff[0] = off.to_shwi ();
1124 if (wi::lts_p (ovloff[1], off))
1125 ovloff[1] = off.to_shwi ();
1131 ovlsiz[0] = siz[0].to_shwi ();
1132 ovlsiz[1] = siz[1].to_shwi ();
1134 /* Adjust the overlap offset range to reflect the overlap size range. */
1135 if (ovlsiz[0] == 0 && ovlsiz[1] > 1)
1136 ovloff[1] = ovloff[0] + ovlsiz[1] - 1;
1138 return true;
1141 /* Return true if the strcat-like access overlaps. */
1143 bool
1144 builtin_access::strcat_overlap ()
1146 builtin_access &acs = *this;
1147 const builtin_memref *dstref = acs.dstref;
1148 const builtin_memref *srcref = acs.srcref;
1150 gcc_assert (dstref->base == srcref->base);
1152 const offset_int maxobjsize = acs.dstref->maxobjsize;
1154 gcc_assert (dstref->base && dstref->base == srcref->base);
1156 /* Adjust for strcat-like accesses. */
1158 /* As a special case for strcat, set the DSTREF offsets to the length
1159 of the destination string since the function starts writing over
1160 its terminating nul, and set the destination size to 1 for the length
1161 of the nul. */
1162 acs.dstoff[0] += dstsiz[0] - srcref->sizrange[0];
1163 acs.dstoff[1] += dstsiz[1] - srcref->sizrange[1];
1165 bool strfunc_unknown_args = acs.dstsiz[0] == 0 && acs.dstsiz[1] != 0;
1167 /* The lower bound is zero when the size is unknown because then
1168 overlap is not certain. */
1169 acs.dstsiz[0] = strfunc_unknown_args ? 0 : 1;
1170 acs.dstsiz[1] = 1;
1172 offset_int maxsize = dstref->basesize < 0 ? maxobjsize : dstref->basesize;
1174 /* For references to the same base object, determine if there's a pair
1175 of valid offsets into the two references such that access between
1176 them doesn't overlap. Adjust both upper bounds to be valid for
1177 the smaller size (i.e., at most MAXSIZE - SIZE). */
1179 if (maxsize < acs.dstoff[1] + acs.dstsiz[0])
1180 acs.dstoff[1] = maxsize - acs.dstsiz[0];
1182 if (maxsize < acs.srcoff[1] + acs.srcsiz[0])
1183 acs.srcoff[1] = maxsize - acs.srcsiz[0];
1185 /* Check to see if there's enough space for both accesses without
1186 overlap. Determine the optimistic (maximum) amount of available
1187 space. */
1188 offset_int space;
1189 if (acs.dstoff[0] <= acs.srcoff[0])
1191 if (acs.dstoff[1] < acs.srcoff[1])
1192 space = acs.srcoff[1] + acs.srcsiz[0] - acs.dstoff[0];
1193 else
1194 space = acs.dstoff[1] + acs.dstsiz[0] - acs.srcoff[0];
1196 else
1197 space = acs.dstoff[1] + acs.dstsiz[0] - acs.srcoff[0];
1199 /* Overlap is certain if the distance between the farthest offsets
1200 of the opposite accesses is less than the sum of the lower bounds
1201 of the sizes of the two accesses. */
1202 bool overlap_certain = space < acs.dstsiz[0] + acs.srcsiz[0];
1204 /* For a constant-offset, constant size access, consider the largest
1205 distance between the offset bounds and the lower bound of the access
1206 size. If the overlap isn't certain return success. */
1207 if (!overlap_certain
1208 && acs.dstoff[0] == acs.dstoff[1]
1209 && acs.srcoff[0] == acs.srcoff[1]
1210 && acs.dstsiz[0] == acs.dstsiz[1]
1211 && acs.srcsiz[0] == acs.srcsiz[1])
1212 return false;
1214 /* Overlap is not certain but may be possible. */
1216 offset_int access_min = acs.dstsiz[0] + acs.srcsiz[0];
1218 /* Determine the conservative (minimum) amount of space. */
1219 space = wi::abs (acs.dstoff[0] - acs.srcoff[0]);
1220 offset_int d = wi::abs (acs.dstoff[0] - acs.srcoff[1]);
1221 if (d < space)
1222 space = d;
1223 d = wi::abs (acs.dstoff[1] - acs.srcoff[0]);
1224 if (d < space)
1225 space = d;
1227 /* For a strict test (used for strcpy and similar with unknown or
1228 variable bounds or sizes), consider the smallest distance between
1229 the offset bounds and either the upper bound of the access size
1230 if known, or the lower bound otherwise. */
1231 if (access_min <= space && (access_min != 0 || !strfunc_unknown_args))
1232 return false;
1234 /* When strcat overlap is certain it is always a single byte:
1235 the terminating NUL, regardless of offsets and sizes. When
1236 overlap is only possible its range is [0, 1]. */
1237 acs.ovlsiz[0] = dstref->sizrange[0] == dstref->sizrange[1] ? 1 : 0;
1238 acs.ovlsiz[1] = 1;
1240 offset_int endoff
1241 = dstref->offrange[0] + (dstref->sizrange[0] - srcref->sizrange[0]);
1242 if (endoff <= srcref->offrange[0])
1243 acs.ovloff[0] = wi::smin (maxobjsize, srcref->offrange[0]).to_shwi ();
1244 else
1245 acs.ovloff[0] = wi::smin (maxobjsize, endoff).to_shwi ();
1247 acs.sizrange[0] = wi::smax (wi::abs (endoff - srcref->offrange[0]) + 1,
1248 srcref->sizrange[0]).to_shwi ();
1249 if (dstref->offrange[0] == dstref->offrange[1])
1251 if (srcref->offrange[0] == srcref->offrange[1])
1252 acs.ovloff[1] = acs.ovloff[0];
1253 else
1254 acs.ovloff[1]
1255 = wi::smin (maxobjsize,
1256 srcref->offrange[1] + srcref->sizrange[1]).to_shwi ();
1258 else
1259 acs.ovloff[1]
1260 = wi::smin (maxobjsize,
1261 dstref->offrange[1] + dstref->sizrange[1]).to_shwi ();
1263 if (acs.sizrange[0] == 0)
1264 acs.sizrange[0] = 1;
1265 acs.sizrange[1] = wi::smax (acs.dstsiz[1], srcref->sizrange[1]).to_shwi ();
1266 return true;
1269 /* Return true if the strcpy-like access overlaps. */
1271 bool
1272 builtin_access::strcpy_overlap ()
1274 return generic_overlap ();
1277 /* For a BASE of array type, clamp REFOFF to at most [0, BASE_SIZE]
1278 if known, or [0, MAXOBJSIZE] otherwise. */
1280 static void
1281 clamp_offset (tree base, offset_int refoff[2], offset_int maxobjsize)
1283 if (!base || TREE_CODE (TREE_TYPE (base)) != ARRAY_TYPE)
1284 return;
1286 if (refoff[0] < 0 && refoff[1] >= 0)
1287 refoff[0] = 0;
1289 if (refoff[1] < refoff[0])
1291 offset_int maxsize = maxobjsize;
1292 if (tree size = TYPE_SIZE_UNIT (TREE_TYPE (base)))
1293 maxsize = wi::to_offset (size);
1295 refoff[1] = wi::umin (refoff[1], maxsize);
1299 /* Return true if DSTREF and SRCREF describe accesses that either overlap
1300 one another or that, in order not to overlap, would imply that the size
1301 of the referenced object(s) exceeds the maximum size of an object. Set
1302 Otherwise, if DSTREF and SRCREF do not definitely overlap (even though
1303 they may overlap in a way that's not apparent from the available data),
1304 return false. */
1306 bool
1307 builtin_access::overlap ()
1309 builtin_access &acs = *this;
1311 const offset_int maxobjsize = dstref->maxobjsize;
1313 acs.sizrange[0] = wi::smax (dstref->sizrange[0],
1314 srcref->sizrange[0]).to_shwi ();
1315 acs.sizrange[1] = wi::smax (dstref->sizrange[1],
1316 srcref->sizrange[1]).to_shwi ();
1318 /* Check to see if the two references refer to regions that are
1319 too large not to overlap in the address space (whose maximum
1320 size is PTRDIFF_MAX). */
1321 offset_int size = dstref->sizrange[0] + srcref->sizrange[0];
1322 if (maxobjsize < size)
1324 acs.ovloff[0] = (maxobjsize - dstref->sizrange[0]).to_shwi ();
1325 acs.ovlsiz[0] = (size - maxobjsize).to_shwi ();
1326 return true;
1329 /* If both base objects aren't known return the maximum possible
1330 offset that would make them not overlap. */
1331 if (!dstref->base || !srcref->base)
1332 return false;
1334 /* If the base object is an array adjust the bounds of the offset
1335 to be non-negative and within the bounds of the array if possible. */
1336 clamp_offset (dstref->base, acs.dstoff, maxobjsize);
1338 acs.srcoff[0] = srcref->offrange[0];
1339 acs.srcoff[1] = srcref->offrange[1];
1341 clamp_offset (srcref->base, acs.srcoff, maxobjsize);
1343 /* When the upper bound of the offset is less than the lower bound
1344 the former is the result of a negative offset being represented
1345 as a large positive value or vice versa. The resulting range is
1346 a union of two subranges: [MIN, UB] and [LB, MAX]. Since such
1347 a union is not representable using the current data structure
1348 replace it with the full range of offsets. */
1349 if (acs.dstoff[1] < acs.dstoff[0])
1351 acs.dstoff[0] = -maxobjsize - 1;
1352 acs.dstoff[1] = maxobjsize;
1355 /* Validate the offset and size of each reference on its own first.
1356 This is independent of whether or not the base objects are the
1357 same. Normally, this would have already been detected and
1358 diagnosed by -Warray-bounds, unless it has been disabled. */
1359 offset_int maxoff = acs.dstoff[0] + dstref->sizrange[0];
1360 if (maxobjsize < maxoff)
1362 acs.ovlsiz[0] = (maxoff - maxobjsize).to_shwi ();
1363 acs.ovloff[0] = acs.dstoff[0].to_shwi () - acs.ovlsiz[0];
1364 return true;
1367 /* Repeat the same as above but for the source offsets. */
1368 if (acs.srcoff[1] < acs.srcoff[0])
1370 acs.srcoff[0] = -maxobjsize - 1;
1371 acs.srcoff[1] = maxobjsize;
1374 maxoff = acs.srcoff[0] + srcref->sizrange[0];
1375 if (maxobjsize < maxoff)
1377 acs.ovlsiz[0] = (maxoff - maxobjsize).to_shwi ();
1378 acs.ovlsiz[1] = (acs.srcoff[0] + srcref->sizrange[1]
1379 - maxobjsize).to_shwi ();
1380 acs.ovloff[0] = acs.srcoff[0].to_shwi () - acs.ovlsiz[0];
1381 return true;
1384 if (dstref->base != srcref->base)
1385 return false;
1387 acs.dstsiz[0] = dstref->sizrange[0];
1388 acs.dstsiz[1] = dstref->sizrange[1];
1390 acs.srcsiz[0] = srcref->sizrange[0];
1391 acs.srcsiz[1] = srcref->sizrange[1];
1393 /* Call the appropriate function to determine the overlap. */
1394 if ((this->*detect_overlap) ())
1396 if (!sizrange[1])
1398 /* Unless the access size range has already been set, do so here. */
1399 sizrange[0] = wi::smax (acs.dstsiz[0], srcref->sizrange[0]).to_shwi ();
1400 sizrange[1] = wi::smax (acs.dstsiz[1], srcref->sizrange[1]).to_shwi ();
1402 return true;
1405 return false;
1408 /* Attempt to detect and diagnose an overlapping copy in a call expression
1409 EXPR involving an access ACS to a built-in memory or string function.
1410 Return true when one has been detected, false otherwise. */
1412 static bool
1413 maybe_diag_overlap (location_t loc, gimple *call, builtin_access &acs)
1415 if (!acs.overlap ())
1416 return false;
1418 if (gimple_no_warning_p (call))
1419 return true;
1421 /* For convenience. */
1422 const builtin_memref &dstref = *acs.dstref;
1423 const builtin_memref &srcref = *acs.srcref;
1425 /* Determine the range of offsets and sizes of the overlap if it
1426 exists and issue diagnostics. */
1427 HOST_WIDE_INT *ovloff = acs.ovloff;
1428 HOST_WIDE_INT *ovlsiz = acs.ovlsiz;
1429 HOST_WIDE_INT *sizrange = acs.sizrange;
1431 tree func = gimple_call_fndecl (call);
1433 /* To avoid a combinatorial explosion of diagnostics format the offsets
1434 or their ranges as strings and use them in the warning calls below. */
1435 char offstr[3][64];
1437 if (dstref.offrange[0] == dstref.offrange[1]
1438 || dstref.offrange[1] > HOST_WIDE_INT_MAX)
1439 sprintf (offstr[0], HOST_WIDE_INT_PRINT_DEC,
1440 dstref.offrange[0].to_shwi ());
1441 else
1442 sprintf (offstr[0],
1443 "[" HOST_WIDE_INT_PRINT_DEC ", " HOST_WIDE_INT_PRINT_DEC "]",
1444 dstref.offrange[0].to_shwi (),
1445 dstref.offrange[1].to_shwi ());
1447 if (srcref.offrange[0] == srcref.offrange[1]
1448 || srcref.offrange[1] > HOST_WIDE_INT_MAX)
1449 sprintf (offstr[1],
1450 HOST_WIDE_INT_PRINT_DEC,
1451 srcref.offrange[0].to_shwi ());
1452 else
1453 sprintf (offstr[1],
1454 "[" HOST_WIDE_INT_PRINT_DEC ", " HOST_WIDE_INT_PRINT_DEC "]",
1455 srcref.offrange[0].to_shwi (),
1456 srcref.offrange[1].to_shwi ());
1458 if (ovloff[0] == ovloff[1] || !ovloff[1])
1459 sprintf (offstr[2], HOST_WIDE_INT_PRINT_DEC, ovloff[0]);
1460 else
1461 sprintf (offstr[2],
1462 "[" HOST_WIDE_INT_PRINT_DEC ", " HOST_WIDE_INT_PRINT_DEC "]",
1463 ovloff[0], ovloff[1]);
1465 const offset_int maxobjsize = dstref.maxobjsize;
1466 bool must_overlap = ovlsiz[0] > 0;
1468 if (ovlsiz[1] == 0)
1469 ovlsiz[1] = ovlsiz[0];
1471 if (must_overlap)
1473 /* Issue definitive "overlaps" diagnostic in this block. */
1475 if (sizrange[0] == sizrange[1])
1477 if (ovlsiz[0] == ovlsiz[1])
1478 warning_at (loc, OPT_Wrestrict,
1479 sizrange[0] == 1
1480 ? (ovlsiz[0] == 1
1481 ? G_("%G%qD accessing %wu byte at offsets %s "
1482 "and %s overlaps %wu byte at offset %s")
1483 : G_("%G%qD accessing %wu byte at offsets %s "
1484 "and %s overlaps %wu bytes at offset "
1485 "%s"))
1486 : (ovlsiz[0] == 1
1487 ? G_("%G%qD accessing %wu bytes at offsets %s "
1488 "and %s overlaps %wu byte at offset %s")
1489 : G_("%G%qD accessing %wu bytes at offsets %s "
1490 "and %s overlaps %wu bytes at offset "
1491 "%s")),
1492 call, func, sizrange[0],
1493 offstr[0], offstr[1], ovlsiz[0], offstr[2]);
1494 else if (ovlsiz[1] >= 0 && ovlsiz[1] < maxobjsize.to_shwi ())
1495 warning_n (loc, OPT_Wrestrict, sizrange[0],
1496 "%G%qD accessing %wu byte at offsets %s "
1497 "and %s overlaps between %wu and %wu bytes "
1498 "at offset %s",
1499 "%G%qD accessing %wu bytes at offsets %s "
1500 "and %s overlaps between %wu and %wu bytes "
1501 "at offset %s",
1502 call, func, sizrange[0], offstr[0], offstr[1],
1503 ovlsiz[0], ovlsiz[1], offstr[2]);
1504 else
1505 warning_n (loc, OPT_Wrestrict, sizrange[0],
1506 "%G%qD accessing %wu byte at offsets %s and "
1507 "%s overlaps %wu or more bytes at offset %s",
1508 "%G%qD accessing %wu bytes at offsets %s and "
1509 "%s overlaps %wu or more bytes at offset %s",
1510 call, func, sizrange[0],
1511 offstr[0], offstr[1], ovlsiz[0], offstr[2]);
1512 return true;
1515 if (sizrange[1] >= 0 && sizrange[1] < maxobjsize.to_shwi ())
1517 if (ovlsiz[0] == ovlsiz[1])
1518 warning_n (loc, OPT_Wrestrict, ovlsiz[0],
1519 "%G%qD accessing between %wu and %wu bytes "
1520 "at offsets %s and %s overlaps %wu byte at "
1521 "offset %s",
1522 "%G%qD accessing between %wu and %wu bytes "
1523 "at offsets %s and %s overlaps %wu bytes "
1524 "at offset %s",
1525 call, func, sizrange[0], sizrange[1],
1526 offstr[0], offstr[1], ovlsiz[0], offstr[2]);
1527 else if (ovlsiz[1] >= 0 && ovlsiz[1] < maxobjsize.to_shwi ())
1528 warning_at (loc, OPT_Wrestrict,
1529 "%G%qD accessing between %wu and %wu bytes at "
1530 "offsets %s and %s overlaps between %wu and %wu "
1531 "bytes at offset %s",
1532 call, func, sizrange[0], sizrange[1],
1533 offstr[0], offstr[1], ovlsiz[0], ovlsiz[1],
1534 offstr[2]);
1535 else
1536 warning_at (loc, OPT_Wrestrict,
1537 "%G%qD accessing between %wu and %wu bytes at "
1538 "offsets %s and %s overlaps %wu or more bytes "
1539 "at offset %s",
1540 call, func, sizrange[0], sizrange[1],
1541 offstr[0], offstr[1], ovlsiz[0], offstr[2]);
1542 return true;
1545 if (ovlsiz[0] != ovlsiz[1])
1546 ovlsiz[1] = maxobjsize.to_shwi ();
1548 if (ovlsiz[0] == ovlsiz[1])
1549 warning_n (loc, OPT_Wrestrict, ovlsiz[0],
1550 "%G%qD accessing %wu or more bytes at offsets "
1551 "%s and %s overlaps %wu byte at offset %s",
1552 "%G%qD accessing %wu or more bytes at offsets "
1553 "%s and %s overlaps %wu bytes at offset %s",
1554 call, func, sizrange[0], offstr[0], offstr[1],
1555 ovlsiz[0], offstr[2]);
1556 else if (ovlsiz[1] >= 0 && ovlsiz[1] < maxobjsize.to_shwi ())
1557 warning_at (loc, OPT_Wrestrict,
1558 "%G%qD accessing %wu or more bytes at offsets %s "
1559 "and %s overlaps between %wu and %wu bytes "
1560 "at offset %s",
1561 call, func, sizrange[0], offstr[0], offstr[1],
1562 ovlsiz[0], ovlsiz[1], offstr[2]);
1563 else
1564 warning_at (loc, OPT_Wrestrict,
1565 "%G%qD accessing %wu or more bytes at offsets %s "
1566 "and %s overlaps %wu or more bytes at offset %s",
1567 call, func, sizrange[0], offstr[0], offstr[1],
1568 ovlsiz[0], offstr[2]);
1569 return true;
1572 /* Use more concise wording when one of the offsets is unbounded
1573 to avoid confusing the user with large and mostly meaningless
1574 numbers. */
1575 bool open_range;
1576 if (DECL_P (dstref.base) && TREE_CODE (TREE_TYPE (dstref.base)) == ARRAY_TYPE)
1577 open_range = ((dstref.offrange[0] == 0
1578 && dstref.offrange[1] == maxobjsize)
1579 || (srcref.offrange[0] == 0
1580 && srcref.offrange[1] == maxobjsize));
1581 else
1582 open_range = ((dstref.offrange[0] == -maxobjsize - 1
1583 && dstref.offrange[1] == maxobjsize)
1584 || (srcref.offrange[0] == -maxobjsize - 1
1585 && srcref.offrange[1] == maxobjsize));
1587 if (sizrange[0] == sizrange[1] || sizrange[1] == 1)
1589 if (ovlsiz[1] == 1)
1591 if (open_range)
1592 warning_n (loc, OPT_Wrestrict, sizrange[1],
1593 "%G%qD accessing %wu byte may overlap "
1594 "%wu byte",
1595 "%G%qD accessing %wu bytes may overlap "
1596 "%wu byte",
1597 call, func, sizrange[1], ovlsiz[1]);
1598 else
1599 warning_n (loc, OPT_Wrestrict, sizrange[1],
1600 "%G%qD accessing %wu byte at offsets %s "
1601 "and %s may overlap %wu byte at offset %s",
1602 "%G%qD accessing %wu bytes at offsets %s "
1603 "and %s may overlap %wu byte at offset %s",
1604 call, func, sizrange[1], offstr[0], offstr[1],
1605 ovlsiz[1], offstr[2]);
1606 return true;
1609 if (open_range)
1610 warning_n (loc, OPT_Wrestrict, sizrange[1],
1611 "%G%qD accessing %wu byte may overlap "
1612 "up to %wu bytes",
1613 "%G%qD accessing %wu bytes may overlap "
1614 "up to %wu bytes",
1615 call, func, sizrange[1], ovlsiz[1]);
1616 else
1617 warning_n (loc, OPT_Wrestrict, sizrange[1],
1618 "%G%qD accessing %wu byte at offsets %s and "
1619 "%s may overlap up to %wu bytes at offset %s",
1620 "%G%qD accessing %wu bytes at offsets %s and "
1621 "%s may overlap up to %wu bytes at offset %s",
1622 call, func, sizrange[1], offstr[0], offstr[1],
1623 ovlsiz[1], offstr[2]);
1624 return true;
1627 if (sizrange[1] >= 0 && sizrange[1] < maxobjsize.to_shwi ())
1629 if (open_range)
1630 warning_n (loc, OPT_Wrestrict, ovlsiz[1],
1631 "%G%qD accessing between %wu and %wu bytes "
1632 "may overlap %wu byte",
1633 "%G%qD accessing between %wu and %wu bytes "
1634 "may overlap up to %wu bytes",
1635 call, func, sizrange[0], sizrange[1], ovlsiz[1]);
1636 else
1637 warning_n (loc, OPT_Wrestrict, ovlsiz[1],
1638 "%G%qD accessing between %wu and %wu bytes "
1639 "at offsets %s and %s may overlap %wu byte "
1640 "at offset %s",
1641 "%G%qD accessing between %wu and %wu bytes "
1642 "at offsets %s and %s may overlap up to %wu "
1643 "bytes at offset %s",
1644 call, func, sizrange[0], sizrange[1],
1645 offstr[0], offstr[1], ovlsiz[1], offstr[2]);
1646 return true;
1649 warning_n (loc, OPT_Wrestrict, ovlsiz[1],
1650 "%G%qD accessing %wu or more bytes at offsets %s "
1651 "and %s may overlap %wu byte at offset %s",
1652 "%G%qD accessing %wu or more bytes at offsets %s "
1653 "and %s may overlap up to %wu bytes at offset %s",
1654 call, func, sizrange[0], offstr[0], offstr[1],
1655 ovlsiz[1], offstr[2]);
1657 return true;
1660 /* Validate REF size and offsets in an expression passed as an argument
1661 to a CALL to a built-in function FUNC to make sure they are within
1662 the bounds of the referenced object if its size is known, or
1663 PTRDIFF_MAX otherwise. DO_WARN is true when a diagnostic should
1664 be issued, false otherwise.
1665 Both initial values of the offsets and their final value computed
1666 by the function by incrementing the initial value by the size are
1667 validated. Return true if the offsets are not valid and a diagnostic
1668 has been issued, or would have been issued if DO_WARN had been true. */
1670 static bool
1671 maybe_diag_access_bounds (gimple *call, tree func, int strict,
1672 const builtin_memref &ref, offset_int wroff,
1673 bool do_warn)
1675 location_t loc = gimple_or_expr_nonartificial_location (call, ref.ptr);
1676 const offset_int maxobjsize = ref.maxobjsize;
1678 /* Check for excessive size first and regardless of warning options
1679 since the result is used to make codegen decisions. */
1680 if (ref.sizrange[0] > maxobjsize)
1682 /* Return true without issuing a warning. */
1683 if (!do_warn)
1684 return true;
1686 if (ref.ref && TREE_NO_WARNING (ref.ref))
1687 return false;
1689 if (warn_stringop_overflow)
1691 if (ref.sizrange[0] == ref.sizrange[1])
1692 return warning_at (loc, OPT_Wstringop_overflow_,
1693 "%G%qD specified bound %wu "
1694 "exceeds maximum object size %wu",
1695 call, func, ref.sizrange[0].to_uhwi (),
1696 maxobjsize.to_uhwi ());
1698 return warning_at (loc, OPT_Wstringop_overflow_,
1699 "%G%qD specified bound between %wu and %wu "
1700 "exceeds maximum object size %wu",
1701 call, func, ref.sizrange[0].to_uhwi (),
1702 ref.sizrange[1].to_uhwi (),
1703 maxobjsize.to_uhwi ());
1707 /* Check for out-bounds pointers regardless of warning options since
1708 the result is used to make codegen decisions. An excessive WROFF
1709 can only come up as a result of an invalid strncat bound and is
1710 diagnosed separately using a more meaningful warning. */
1711 if (maxobjsize < wroff)
1712 wroff = 0;
1713 offset_int ooboff[] = { ref.offrange[0], ref.offrange[1], wroff };
1714 tree oobref = ref.offset_out_of_bounds (strict, ooboff);
1715 if (!oobref)
1716 return false;
1718 /* Return true without issuing a warning. */
1719 if (!do_warn)
1720 return true;
1722 if (!warn_array_bounds)
1723 return false;
1725 if (TREE_NO_WARNING (ref.ptr)
1726 || (ref.ref && TREE_NO_WARNING (ref.ref)))
1727 return false;
1729 char rangestr[2][64];
1730 if (ooboff[0] == ooboff[1]
1731 || (ooboff[0] != ref.offrange[0]
1732 && ooboff[0].to_shwi () >= ooboff[1].to_shwi ()))
1733 sprintf (rangestr[0], "%lli", (long long) ooboff[0].to_shwi ());
1734 else
1735 sprintf (rangestr[0], "[%lli, %lli]",
1736 (long long) ooboff[0].to_shwi (),
1737 (long long) ooboff[1].to_shwi ());
1739 bool warned = false;
1741 if (oobref == error_mark_node)
1743 if (ref.sizrange[0] == ref.sizrange[1])
1744 sprintf (rangestr[1], "%llu",
1745 (unsigned long long) ref.sizrange[0].to_shwi ());
1746 else
1747 sprintf (rangestr[1], "[%lli, %lli]",
1748 (unsigned long long) ref.sizrange[0].to_uhwi (),
1749 (unsigned long long) ref.sizrange[1].to_uhwi ());
1751 tree type;
1753 if (DECL_P (ref.base)
1754 && TREE_CODE (type = TREE_TYPE (ref.base)) == ARRAY_TYPE)
1756 auto_diagnostic_group d;
1757 if (warning_at (loc, OPT_Warray_bounds,
1758 "%G%qD pointer overflow between offset %s "
1759 "and size %s accessing array %qD with type %qT",
1760 call, func, rangestr[0], rangestr[1], ref.base, type))
1762 inform (DECL_SOURCE_LOCATION (ref.base),
1763 "array %qD declared here", ref.base);
1764 warned = true;
1766 else
1767 warned = warning_at (loc, OPT_Warray_bounds,
1768 "%G%qD pointer overflow between offset %s "
1769 "and size %s",
1770 call, func, rangestr[0], rangestr[1]);
1772 else
1773 warned = warning_at (loc, OPT_Warray_bounds,
1774 "%G%qD pointer overflow between offset %s "
1775 "and size %s",
1776 call, func, rangestr[0], rangestr[1]);
1778 else if (oobref == ref.base)
1780 /* True when the offset formed by an access to the reference
1781 is out of bounds, rather than the initial offset wich is
1782 in bounds. This implies access past the end. */
1783 bool form = ooboff[0] != ref.offrange[0];
1785 if (DECL_P (ref.base))
1787 auto_diagnostic_group d;
1788 if ((ref.basesize < maxobjsize
1789 && warning_at (loc, OPT_Warray_bounds,
1790 form
1791 ? G_("%G%qD forming offset %s is out of "
1792 "the bounds [0, %wu] of object %qD with "
1793 "type %qT")
1794 : G_("%G%qD offset %s is out of the bounds "
1795 "[0, %wu] of object %qD with type %qT"),
1796 call, func, rangestr[0], ref.basesize.to_uhwi (),
1797 ref.base, TREE_TYPE (ref.base)))
1798 || warning_at (loc, OPT_Warray_bounds,
1799 form
1800 ? G_("%G%qD forming offset %s is out of "
1801 "the bounds of object %qD with type %qT")
1802 : G_("%G%qD offset %s is out of the bounds "
1803 "of object %qD with type %qT"),
1804 call, func, rangestr[0],
1805 ref.base, TREE_TYPE (ref.base)))
1807 inform (DECL_SOURCE_LOCATION (ref.base),
1808 "%qD declared here", ref.base);
1809 warned = true;
1812 else if (ref.basesize < maxobjsize)
1813 warned = warning_at (loc, OPT_Warray_bounds,
1814 form
1815 ? G_("%G%qD forming offset %s is out "
1816 "of the bounds [0, %wu]")
1817 : G_("%G%qD offset %s is out "
1818 "of the bounds [0, %wu]"),
1819 call, func, rangestr[0], ref.basesize.to_uhwi ());
1820 else
1821 warned = warning_at (loc, OPT_Warray_bounds,
1822 form
1823 ? G_("%G%qD forming offset %s is out of bounds")
1824 : G_("%G%qD offset %s is out of bounds"),
1825 call, func, rangestr[0]);
1827 else if (TREE_CODE (ref.ref) == MEM_REF)
1829 tree refop = TREE_OPERAND (ref.ref, 0);
1830 tree type = TREE_TYPE (refop);
1831 if (POINTER_TYPE_P (type))
1832 type = TREE_TYPE (type);
1833 type = TYPE_MAIN_VARIANT (type);
1835 if (warning_at (loc, OPT_Warray_bounds,
1836 "%G%qD offset %s from the object at %qE is out "
1837 "of the bounds of %qT",
1838 call, func, rangestr[0], ref.base, type))
1840 if (TREE_CODE (ref.ref) == COMPONENT_REF)
1841 refop = TREE_OPERAND (ref.ref, 1);
1842 if (DECL_P (refop))
1843 inform (DECL_SOURCE_LOCATION (refop),
1844 "subobject %qD declared here", refop);
1845 warned = true;
1848 else
1850 tree refop = TREE_OPERAND (ref.ref, 0);
1851 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (ref.ref));
1853 if (warning_at (loc, OPT_Warray_bounds,
1854 "%G%qD offset %s from the object at %qE is out "
1855 "of the bounds of referenced subobject %qD with "
1856 "type %qT at offset %wi",
1857 call, func, rangestr[0], ref.base,
1858 TREE_OPERAND (ref.ref, 1), type,
1859 ref.refoff.to_shwi ()))
1861 if (TREE_CODE (ref.ref) == COMPONENT_REF)
1862 refop = TREE_OPERAND (ref.ref, 1);
1863 if (DECL_P (refop))
1864 inform (DECL_SOURCE_LOCATION (refop),
1865 "subobject %qD declared here", refop);
1866 warned = true;
1870 return warned;
1873 /* Check a CALL statement for restrict-violations and issue warnings
1874 if/when appropriate. */
1876 void
1877 wrestrict_dom_walker::check_call (gimple *call)
1879 /* Avoid checking the call if it has already been diagnosed for
1880 some reason. */
1881 if (gimple_no_warning_p (call))
1882 return;
1884 tree func = gimple_call_fndecl (call);
1885 if (!func || !fndecl_built_in_p (func, BUILT_IN_NORMAL))
1886 return;
1888 /* Argument number to extract from the call (depends on the built-in
1889 and its kind). */
1890 unsigned dst_idx = -1;
1891 unsigned src_idx = -1;
1892 unsigned bnd_idx = -1;
1894 /* Is this CALL to a string function (as opposed to one to a raw
1895 memory function). */
1896 bool strfun = true;
1898 switch (DECL_FUNCTION_CODE (func))
1900 case BUILT_IN_MEMCPY:
1901 case BUILT_IN_MEMCPY_CHK:
1902 case BUILT_IN_MEMPCPY:
1903 case BUILT_IN_MEMPCPY_CHK:
1904 case BUILT_IN_MEMMOVE:
1905 case BUILT_IN_MEMMOVE_CHK:
1906 strfun = false;
1907 /* Fall through. */
1909 case BUILT_IN_STPNCPY:
1910 case BUILT_IN_STPNCPY_CHK:
1911 case BUILT_IN_STRNCAT:
1912 case BUILT_IN_STRNCAT_CHK:
1913 case BUILT_IN_STRNCPY:
1914 case BUILT_IN_STRNCPY_CHK:
1915 dst_idx = 0;
1916 src_idx = 1;
1917 bnd_idx = 2;
1918 break;
1920 case BUILT_IN_MEMSET:
1921 case BUILT_IN_MEMSET_CHK:
1922 dst_idx = 0;
1923 bnd_idx = 2;
1924 break;
1926 case BUILT_IN_STPCPY:
1927 case BUILT_IN_STPCPY_CHK:
1928 case BUILT_IN_STRCPY:
1929 case BUILT_IN_STRCPY_CHK:
1930 case BUILT_IN_STRCAT:
1931 case BUILT_IN_STRCAT_CHK:
1932 dst_idx = 0;
1933 src_idx = 1;
1934 break;
1936 default:
1937 /* Handle other string functions here whose access may need
1938 to be validated for in-bounds offsets and non-overlapping
1939 copies. */
1940 return;
1943 unsigned nargs = gimple_call_num_args (call);
1945 tree dst = dst_idx < nargs ? gimple_call_arg (call, dst_idx) : NULL_TREE;
1946 tree src = src_idx < nargs ? gimple_call_arg (call, src_idx) : NULL_TREE;
1947 tree dstwr = bnd_idx < nargs ? gimple_call_arg (call, bnd_idx) : NULL_TREE;
1949 /* For string functions with an unspecified or unknown bound,
1950 assume the size of the access is one. */
1951 if (!dstwr && strfun)
1952 dstwr = size_one_node;
1954 /* DST and SRC can be null for a call with an insufficient number
1955 of arguments to a built-in function declared without a protype. */
1956 if (!dst || (src_idx < nargs && !src))
1957 return;
1959 /* DST, SRC, or DSTWR can also have the wrong type in a call to
1960 a function declared without a prototype. Avoid checking such
1961 invalid calls. */
1962 if (TREE_CODE (TREE_TYPE (dst)) != POINTER_TYPE
1963 || (src && TREE_CODE (TREE_TYPE (src)) != POINTER_TYPE)
1964 || (dstwr && !INTEGRAL_TYPE_P (TREE_TYPE (dstwr))))
1965 return;
1967 if (!check_bounds_or_overlap (call, dst, src, dstwr, NULL_TREE))
1968 return;
1970 /* Avoid diagnosing the call again. */
1971 gimple_set_no_warning (call, true);
1974 } /* anonymous namespace */
1976 /* Attempt to detect and diagnose invalid offset bounds and (except for
1977 memmove) overlapping copy in a call expression EXPR from SRC to DST
1978 and DSTSIZE and SRCSIZE bytes, respectively. Both DSTSIZE and
1979 SRCSIZE may be NULL. DO_WARN is false to detect either problem
1980 without issue a warning. Return the OPT_Wxxx constant corresponding
1981 to the warning if one has been detected and zero otherwise. */
1984 check_bounds_or_overlap (gimple *call, tree dst, tree src, tree dstsize,
1985 tree srcsize, bool bounds_only /* = false */,
1986 bool do_warn /* = true */)
1988 tree func = gimple_call_fndecl (call);
1990 builtin_memref dstref (dst, dstsize);
1991 builtin_memref srcref (src, srcsize);
1993 /* Create a descriptor of the access. This may adjust both DSTREF
1994 and SRCREF based on one another and the kind of the access. */
1995 builtin_access acs (call, dstref, srcref);
1997 /* Set STRICT to the value of the -Warray-bounds=N argument for
1998 string functions or when N > 1. */
1999 int strict = (acs.strict () || warn_array_bounds > 1 ? warn_array_bounds : 0);
2001 /* The starting offset of the destination write access. Nonzero only
2002 for the strcat family of functions. */
2003 offset_int wroff = acs.write_off (dstsize);
2005 /* Validate offsets to each reference before the access first to make
2006 sure they are within the bounds of the destination object if its
2007 size is known, or PTRDIFF_MAX otherwise. */
2008 if (maybe_diag_access_bounds (call, func, strict, dstref, wroff, do_warn)
2009 || maybe_diag_access_bounds (call, func, strict, srcref, 0, do_warn))
2011 if (do_warn)
2012 gimple_set_no_warning (call, true);
2013 return OPT_Warray_bounds;
2016 if (!warn_restrict || bounds_only || !src)
2017 return 0;
2019 if (!bounds_only)
2021 switch (DECL_FUNCTION_CODE (func))
2023 case BUILT_IN_MEMMOVE:
2024 case BUILT_IN_MEMMOVE_CHK:
2025 case BUILT_IN_MEMSET:
2026 case BUILT_IN_MEMSET_CHK:
2027 return 0;
2028 default:
2029 break;
2033 location_t loc = gimple_or_expr_nonartificial_location (call, dst);
2034 if (operand_equal_p (dst, src, 0))
2036 /* Issue -Wrestrict unless the pointers are null (those do
2037 not point to objects and so do not indicate an overlap;
2038 such calls could be the result of sanitization and jump
2039 threading). */
2040 if (!integer_zerop (dst) && !gimple_no_warning_p (call))
2042 warning_at (loc, OPT_Wrestrict,
2043 "%G%qD source argument is the same as destination",
2044 call, func);
2045 gimple_set_no_warning (call, true);
2046 return OPT_Wrestrict;
2049 return 0;
2052 /* Return false when overlap has been detected. */
2053 if (maybe_diag_overlap (loc, call, acs))
2055 gimple_set_no_warning (call, true);
2056 return OPT_Wrestrict;
2059 return 0;
2062 gimple_opt_pass *
2063 make_pass_warn_restrict (gcc::context *ctxt)
2065 return new pass_wrestrict (ctxt);
2068 DEBUG_FUNCTION void
2069 dump_builtin_memref (FILE *fp, const builtin_memref &ref)
2071 fprintf (fp, "\n ptr = ");
2072 print_generic_expr (fp, ref.ptr, TDF_LINENO);
2073 fprintf (fp, "\n ref = ");
2074 if (ref.ref)
2075 print_generic_expr (fp, ref.ref, TDF_LINENO);
2076 else
2077 fputs ("null", fp);
2078 fprintf (fp, "\n base = ");
2079 print_generic_expr (fp, ref.base, TDF_LINENO);
2080 fprintf (fp,
2081 "\n basesize = %lli"
2082 "\n refsize = %lli"
2083 "\n refoff = %lli"
2084 "\n offrange = [%lli, %lli]"
2085 "\n sizrange = [%lli, %lli]"
2086 "\n strbounded_p = %s\n",
2087 (long long)ref.basesize.to_shwi (),
2088 (long long)ref.refsize.to_shwi (),
2089 (long long)ref.refoff.to_shwi (),
2090 (long long)ref.offrange[0].to_shwi (),
2091 (long long)ref.offrange[1].to_shwi (),
2092 (long long)ref.sizrange[0].to_shwi (),
2093 (long long)ref.sizrange[1].to_shwi (),
2094 ref.strbounded_p ? "true" : "false");
2097 void
2098 builtin_access::dump (FILE *fp) const
2100 fprintf (fp, " dstref:");
2101 dump_builtin_memref (fp, *dstref);
2102 fprintf (fp, "\n srcref:");
2103 dump_builtin_memref (fp, *srcref);
2105 fprintf (fp,
2106 " sizrange = [%lli, %lli]\n"
2107 " ovloff = [%lli, %lli]\n"
2108 " ovlsiz = [%lli, %lli]\n"
2109 " dstoff = [%lli, %lli]\n"
2110 " dstsiz = [%lli, %lli]\n"
2111 " srcoff = [%lli, %lli]\n"
2112 " srcsiz = [%lli, %lli]\n",
2113 (long long)sizrange[0], (long long)sizrange[1],
2114 (long long)ovloff[0], (long long)ovloff[1],
2115 (long long)ovlsiz[0], (long long)ovlsiz[1],
2116 (long long)dstoff[0].to_shwi (), (long long)dstoff[1].to_shwi (),
2117 (long long)dstsiz[0].to_shwi (), (long long)dstsiz[1].to_shwi (),
2118 (long long)srcoff[0].to_shwi (), (long long)srcoff[1].to_shwi (),
2119 (long long)srcsiz[0].to_shwi (), (long long)srcsiz[1].to_shwi ());
2122 DEBUG_FUNCTION void
2123 dump_builtin_access (FILE *fp, gimple *stmt, const builtin_access &acs)
2125 if (stmt)
2127 fprintf (fp, "\nDumping builtin_access for ");
2128 print_gimple_expr (fp, stmt, TDF_LINENO);
2129 fputs (":\n", fp);
2132 acs.dump (fp);
2135 DEBUG_FUNCTION void
2136 debug (gimple *stmt, const builtin_access &acs)
2138 dump_builtin_access (stdout, stmt, acs);