1 /* Pass to detect and issue warnings for violations of the restrict
3 Copyright (C) 2017 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
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
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/>. */
24 #include "coretypes.h"
29 #include "tree-pass.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"
41 #include "tree-object-size.h"
48 const pass_data pass_data_wrestrict
= {
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
65 pass_wrestrict (gcc::context
*ctxt
)
66 : gimple_opt_pass (pass_data_wrestrict
, ctxt
)
69 opt_pass
*clone () { return new pass_wrestrict (m_ctxt
); }
71 virtual bool gate (function
*);
72 virtual unsigned int execute (function
*);
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
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
*);
91 void check_call (gcall
*);
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
);
101 gimple
*stmt
= gsi_stmt (si
);
102 if (!is_gimple_call (stmt
))
105 if (gcall
*call
= as_a
<gcall
*> (stmt
))
112 /* Execute the pass for function FUN, walking in dominator order. */
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
));
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. */
132 /* The referenced subobject or NULL if not available, and the base
133 object of the memory reference or NULL. */
137 /* The size of the BASE object, PTRDIFF_MAX if indeterminate,
138 and negative until (possibly lazily) initialized. */
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. */
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. */
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. */
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. */
184 return detect_overlap
!= &builtin_access::generic_overlap
;
187 builtin_access (gcall
*, builtin_memref
&, builtin_memref
&);
189 /* Entry point to determine overlap. */
193 /* Implementation functions used to determine overlap. */
194 bool generic_overlap ();
195 bool strcat_overlap ();
196 bool strcpy_overlap ();
203 offset_int
overlap_size (const offset_int
[2], const offset_int
[2],
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
221 builtin_memref::builtin_memref (tree expr
, tree size
)
226 refoff (HOST_WIDE_INT_MIN
),
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
);
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
)
276 value_range_type rng
= get_range_info (offset
, &min
, &max
);
279 offrange
[0] = min
.to_shwi ();
280 offrange
[1] = max
.to_shwi ();
282 else if (rng
== VR_ANTI_RANGE
)
284 offrange
[0] = (max
+ 1).to_shwi ();
285 offrange
[1] = (min
- 1).to_shwi ();
289 gimple
*stmt
= SSA_NAME_DEF_STMT (offset
);
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
));
303 offrange
[1] = maxobjsize
;
307 offrange
[1] = maxobjsize
;
312 if (TREE_CODE (expr
) == ADDR_EXPR
)
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. */
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
);
343 base
= get_base_address (TREE_OPERAND (expr
, 0));
348 base
= build2 (MEM_REF
, char_type_node
, expr
, null_pointer_node
);
350 if (TREE_CODE (base
) == MEM_REF
)
352 offset_int off
= mem_ref_offset (base
);
356 base
= TREE_OPERAND (base
, 0);
359 if (TREE_CODE (base
) == SSA_NAME
)
360 if (gimple
*stmt
= SSA_NAME_DEF_STMT (base
))
362 enum gimple_code code
= gimple_code (stmt
);
363 if (code
== GIMPLE_ASSIGN
)
364 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
366 base
= gimple_assign_rhs1 (stmt
);
368 tree offset
= gimple_assign_rhs2 (stmt
);
369 if (TREE_CODE (offset
) == INTEGER_CST
)
371 offset_int off
= int_cst_value (offset
);
378 if (TREE_CODE (base
) == SSA_NAME
&& SSA_NAME_VAR (base
))
379 base
= SSA_NAME_VAR (base
);
385 /* Determine the size range, allowing for the result to be [0, 0]
386 for SIZE in the anti-range ~[0, N] where N >= PTRDIFF_MAX. */
387 get_size_range (size
, range
, true);
388 sizrange
[0] = wi::to_offset (range
[0]);
389 sizrange
[1] = wi::to_offset (range
[1]);
390 /* get_size_range returns SIZE_MAX for the maximum size.
391 Constrain it to the real maximum of PTRDIFF_MAX. */
392 if (sizrange
[1] > maxobjsize
)
393 sizrange
[1] = maxobjsize
;
396 sizrange
[1] = maxobjsize
;
399 /* Return error_mark_node if the signed offset exceeds the bounds
400 of the address space (PTRDIFF_MAX). Otherwise, return either
401 BASE or REF when the offset exceeds the bounds of the BASE or
402 REF object, and set OOBOFF to the past-the-end offset formed
403 by the reference, including its size. When STRICT is non-zero
404 use REF size, when available, otherwise use BASE size. When
405 STRICT is greater than 1, use the size of the last array member
406 as the bound, otherwise treat such a member as a flexible array
407 member. Return NULL when the offset is in bounds. */
410 builtin_memref::offset_out_of_bounds (int strict
, offset_int ooboff
[2]) const
412 const offset_int maxobjsize
= tree_to_shwi (max_object_size ());
414 /* A temporary, possibly adjusted, copy of the offset range. */
415 offset_int offrng
[2] = { offrange
[0], offrange
[1] };
417 if (DECL_P (base
) && TREE_CODE (TREE_TYPE (base
)) == ARRAY_TYPE
)
419 /* Check for offset in an anti-range with a negative lower bound.
420 For such a range, consider only the non-negative subrange. */
421 if (offrng
[1] < offrng
[0] && offrng
[1] < 0)
422 offrng
[1] = maxobjsize
;
425 /* Conservative offset of the last byte of the referenced object. */
428 /* The bounds need not be ordered. Set HIB to use as the index
429 of the larger of the bounds and LOB as the opposite. */
430 bool hib
= wi::les_p (offrng
[0], offrng
[1]);
435 endoff
= offrng
[lob
] + sizrange
[0];
437 /* For a reference through a pointer to an object of unknown size
438 all initial offsets are considered valid, positive as well as
439 negative, since the pointer itself can point past the beginning
440 of the object. However, the sum of the lower bound of the offset
441 and that of the size must be less than or equal than PTRDIFF_MAX. */
442 if (endoff
> maxobjsize
)
443 return error_mark_node
;
448 /* A reference to an object of known size must be within the bounds
449 of the base object. */
450 if (offrng
[hib
] < 0 || offrng
[lob
] > basesize
)
453 /* The extent of the reference must also be within the bounds of
454 the base object (if known) or the maximum object size otherwise. */
455 endoff
= wi::smax (offrng
[lob
], 0) + sizrange
[0];
456 if (endoff
> maxobjsize
)
457 return error_mark_node
;
459 offset_int size
= basesize
;
466 && TREE_CODE (ref
) == COMPONENT_REF
468 || !array_at_struct_end_p (ref
)))
470 /* If the reference is to a member subobject, the offset must
471 be within the bounds of the subobject. */
472 tree field
= TREE_OPERAND (ref
, 1);
473 tree type
= TREE_TYPE (field
);
474 if (tree sz
= TYPE_SIZE_UNIT (type
))
475 if (TREE_CODE (sz
) == INTEGER_CST
)
477 size
= refoff
+ wi::to_offset (sz
);
485 /* Set the out-of-bounds offset range to be one greater than
486 that delimited by the reference including its size. */
487 ooboff
[lob
] = size
+ 1;
489 if (endoff
> ooboff
[lob
])
490 ooboff
[hib
] = endoff
;
492 ooboff
[hib
] = wi::smax (offrng
[lob
], 0) + sizrange
[1];
497 /* Create an association between the memory references DST and SRC
498 for access by a call EXPR to a memory or string built-in funtion. */
500 builtin_access::builtin_access (gcall
*call
, builtin_memref
&dst
,
502 : dstref (&dst
), srcref (&src
), sizrange (), ovloff (), ovlsiz (),
503 dstoff (), srcoff (), dstsiz (), srcsiz ()
505 /* Zero out since the offset_int ctors invoked above are no-op. */
506 dstoff
[0] = dstoff
[1] = 0;
507 srcoff
[0] = srcoff
[1] = 0;
508 dstsiz
[0] = dstsiz
[1] = 0;
509 srcsiz
[0] = srcsiz
[1] = 0;
511 /* Object Size Type to use to determine the size of the destination
512 and source objects. Overridden below for raw memory functions. */
515 /* True when the size of one reference depends on the offset of
516 itself or the other. */
517 bool depends_p
= true;
519 /* True when the size of the destination reference DSTREF has been
520 determined from SRCREF and so needs to be adjusted by the latter's
521 offset. Only meaningful for bounded string functions like strncpy. */
522 bool dstadjust_p
= false;
524 /* The size argument number (depends on the built-in). */
525 unsigned sizeargno
= 2;
526 if (gimple_call_with_bounds_p (call
))
529 tree func
= gimple_call_fndecl (call
);
530 switch (DECL_FUNCTION_CODE (func
))
532 case BUILT_IN_MEMCPY
:
533 case BUILT_IN_MEMCPY_CHK
:
534 case BUILT_IN_MEMCPY_CHKP
:
535 case BUILT_IN_MEMCPY_CHK_CHKP
:
536 case BUILT_IN_MEMPCPY
:
537 case BUILT_IN_MEMPCPY_CHK
:
538 case BUILT_IN_MEMPCPY_CHKP
:
539 case BUILT_IN_MEMPCPY_CHK_CHKP
:
542 detect_overlap
= &builtin_access::generic_overlap
;
545 case BUILT_IN_MEMMOVE
:
546 case BUILT_IN_MEMMOVE_CHK
:
547 case BUILT_IN_MEMMOVE_CHKP
:
548 case BUILT_IN_MEMMOVE_CHK_CHKP
:
549 /* For memmove there is never any overlap to check for. */
552 detect_overlap
= &builtin_access::no_overlap
;
555 case BUILT_IN_STPNCPY
:
556 case BUILT_IN_STPNCPY_CHK
:
557 case BUILT_IN_STRNCPY
:
558 case BUILT_IN_STRNCPY_CHK
:
559 dstref
->strbounded_p
= true;
560 detect_overlap
= &builtin_access::strcpy_overlap
;
563 case BUILT_IN_STPCPY
:
564 case BUILT_IN_STPCPY_CHK
:
565 case BUILT_IN_STPCPY_CHKP
:
566 case BUILT_IN_STPCPY_CHK_CHKP
:
567 case BUILT_IN_STRCPY
:
568 case BUILT_IN_STRCPY_CHK
:
569 case BUILT_IN_STRCPY_CHKP
:
570 case BUILT_IN_STRCPY_CHK_CHKP
:
571 detect_overlap
= &builtin_access::strcpy_overlap
;
574 case BUILT_IN_STRCAT
:
575 case BUILT_IN_STRCAT_CHK
:
576 case BUILT_IN_STRCAT_CHKP
:
577 case BUILT_IN_STRCAT_CHK_CHKP
:
578 detect_overlap
= &builtin_access::strcat_overlap
;
581 case BUILT_IN_STRNCAT
:
582 case BUILT_IN_STRNCAT_CHK
:
583 dstref
->strbounded_p
= true;
584 srcref
->strbounded_p
= true;
585 detect_overlap
= &builtin_access::strcat_overlap
;
589 /* Handle other string functions here whose access may need
590 to be validated for in-bounds offsets and non-overlapping
591 copies. (Not all _chkp functions have BUILT_IN_XXX_CHKP
592 macros so they need to be handled here.) */
596 const offset_int maxobjsize
= tree_to_shwi (max_object_size ());
598 /* Try to determine the size of the base object. compute_objsize
599 expects a pointer so create one if BASE is a non-pointer object. */
601 if (dst
.basesize
< 0)
604 if (!POINTER_TYPE_P (TREE_TYPE (addr
)))
605 addr
= build1 (ADDR_EXPR
, (TREE_TYPE (addr
)), addr
);
607 if (tree dstsize
= compute_objsize (addr
, ostype
))
608 dst
.basesize
= wi::to_offset (dstsize
);
609 else if (POINTER_TYPE_P (TREE_TYPE (addr
)))
610 dst
.basesize
= HOST_WIDE_INT_MIN
;
612 dst
.basesize
= maxobjsize
;
615 if (src
.basesize
< 0)
618 if (!POINTER_TYPE_P (TREE_TYPE (addr
)))
619 addr
= build1 (ADDR_EXPR
, (TREE_TYPE (addr
)), addr
);
621 if (tree srcsize
= compute_objsize (addr
, ostype
))
622 src
.basesize
= wi::to_offset (srcsize
);
623 else if (POINTER_TYPE_P (TREE_TYPE (addr
)))
624 src
.basesize
= HOST_WIDE_INT_MIN
;
626 src
.basesize
= maxobjsize
;
629 /* If there is no dependency between the references or the base
630 objects of the two references aren't the same there's nothing
632 if (depends_p
&& dstref
->base
!= srcref
->base
)
635 /* ...otherwise, make adjustments for references to the same object
636 by string built-in functions to reflect the constraints imposed
639 /* For bounded string functions determine the range of the bound
640 on the access. For others, the range stays unbounded. */
641 offset_int bounds
[2] = { maxobjsize
, maxobjsize
};
642 if (dstref
->strbounded_p
)
644 tree size
= gimple_call_arg (call
, sizeargno
);
646 if (get_size_range (size
, range
, true))
648 bounds
[0] = wi::to_offset (range
[0]);
649 bounds
[1] = wi::to_offset (range
[1]);
652 /* If both references' size ranges are indeterminate use the last
653 (size) argument from the function call as a substitute. This
654 may only be necessary for strncpy (but not for memcpy where
655 the size range would have been already determined this way). */
656 if (dstref
->sizrange
[0] == 0 && dstref
->sizrange
[1] == maxobjsize
657 && srcref
->sizrange
[0] == 0 && srcref
->sizrange
[1] == maxobjsize
)
659 dstref
->sizrange
[0] = bounds
[0];
660 dstref
->sizrange
[1] = bounds
[1];
664 /* The size range of one reference involving the same base object
665 can be determined from the size range of the other reference.
666 This makes it possible to compute accurate offsets for warnings
667 involving functions like strcpy where the length of just one of
668 the two arguments is known (determined by tree-ssa-strlen). */
669 if (dstref
->sizrange
[0] == 0 && dstref
->sizrange
[1] == maxobjsize
)
671 /* When the destination size is unknown set it to the size of
673 dstref
->sizrange
[0] = srcref
->sizrange
[0];
674 dstref
->sizrange
[1] = srcref
->sizrange
[1];
676 else if (srcref
->sizrange
[0] == 0 && srcref
->sizrange
[1] == maxobjsize
)
678 /* When the source size is unknown set it to the size of
680 srcref
->sizrange
[0] = dstref
->sizrange
[0];
681 srcref
->sizrange
[1] = dstref
->sizrange
[1];
685 if (dstref
->strbounded_p
)
687 /* Read access by strncpy is bounded. */
688 if (bounds
[0] < srcref
->sizrange
[0])
689 srcref
->sizrange
[0] = bounds
[0];
690 if (bounds
[1] < srcref
->sizrange
[1])
691 srcref
->sizrange
[1] = bounds
[1];
694 /* For string functions, adjust the size range of the source
695 reference by the inverse boundaries of the offset (because
696 the higher the offset into the string the shorter its
698 if (srcref
->offrange
[1] < srcref
->sizrange
[0])
699 srcref
->sizrange
[0] -= srcref
->offrange
[1];
701 srcref
->sizrange
[0] = 0;
703 if (srcref
->offrange
[0] > 0)
705 if (srcref
->offrange
[0] < srcref
->sizrange
[1])
706 srcref
->sizrange
[1] -= srcref
->offrange
[0];
708 srcref
->sizrange
[1] = 0;
715 if (detect_overlap
== &builtin_access::generic_overlap
)
717 if (dstref
->strbounded_p
)
719 dstref
->sizrange
[0] = bounds
[0];
720 dstref
->sizrange
[1] = bounds
[1];
722 if (dstref
->sizrange
[0] < srcref
->sizrange
[0])
723 srcref
->sizrange
[0] = dstref
->sizrange
[0];
725 if (dstref
->sizrange
[1] < srcref
->sizrange
[1])
726 srcref
->sizrange
[1] = dstref
->sizrange
[1];
729 else if (detect_overlap
== &builtin_access::strcpy_overlap
)
731 if (!dstref
->strbounded_p
)
733 /* For strcpy, adjust the destination size range to match that
734 of the source computed above. */
735 if (depends_p
&& dstadjust_p
)
737 dstref
->sizrange
[0] = srcref
->sizrange
[0];
738 dstref
->sizrange
[1] = srcref
->sizrange
[1];
743 if (dstref
->strbounded_p
)
745 /* For strncpy, adjust the destination size range to match that
746 of the source computed above. */
747 dstref
->sizrange
[0] = bounds
[0];
748 dstref
->sizrange
[1] = bounds
[1];
750 if (bounds
[0] < srcref
->sizrange
[0])
751 srcref
->sizrange
[0] = bounds
[0];
753 if (bounds
[1] < srcref
->sizrange
[1])
754 srcref
->sizrange
[1] = bounds
[1];
759 builtin_access::overlap_size (const offset_int a
[2], const offset_int b
[2],
762 const offset_int
*p
= a
;
763 const offset_int
*q
= b
;
765 /* Point P at the bigger of the two ranges and Q at the smaller. */
766 if (wi::lts_p (a
[1] - a
[0], b
[1] - b
[0]))
778 return wi::smin (p
[1], q
[1]) - q
[0];
788 /* Return true if the bounded mempry (memcpy amd similar) or string function
789 access (strncpy and similar) ACS overlaps. */
792 builtin_access::generic_overlap ()
794 builtin_access
&acs
= *this;
795 const builtin_memref
*dstref
= acs
.dstref
;
796 const builtin_memref
*srcref
= acs
.srcref
;
798 gcc_assert (dstref
->base
== srcref
->base
);
800 const offset_int maxobjsize
= tree_to_shwi (max_object_size ());
802 offset_int maxsize
= dstref
->basesize
< 0 ? maxobjsize
: dstref
->basesize
;
803 gcc_assert (maxsize
<= maxobjsize
);
805 /* Adjust the larger bounds of the offsets (which may be the first
806 element if the lower bound is larger than the upper bound) to
807 make them valid for the smallest access (if possible) but no smaller
808 than the smaller bounds. */
809 gcc_assert (wi::les_p (acs
.dstoff
[0], acs
.dstoff
[1]));
811 if (maxsize
< acs
.dstoff
[1] + acs
.dstsiz
[0])
812 acs
.dstoff
[1] = maxsize
- acs
.dstsiz
[0];
813 if (acs
.dstoff
[1] < acs
.dstoff
[0])
814 acs
.dstoff
[1] = acs
.dstoff
[0];
816 gcc_assert (wi::les_p (acs
.srcoff
[0], acs
.srcoff
[1]));
818 if (maxsize
< acs
.srcoff
[1] + acs
.srcsiz
[0])
819 acs
.srcoff
[1] = maxsize
- acs
.srcsiz
[0];
820 if (acs
.srcoff
[1] < acs
.srcoff
[0])
821 acs
.srcoff
[1] = acs
.srcoff
[0];
823 /* Determine the minimum and maximum space for the access given
826 space
[0] = wi::abs (acs
.dstoff
[0] - acs
.srcoff
[0]);
829 offset_int d
= wi::abs (acs
.dstoff
[0] - acs
.srcoff
[1]);
830 if (acs
.srcsiz
[0] > 0)
839 space
[1] = acs
.dstsiz
[1];
841 d
= wi::abs (acs
.dstoff
[1] - acs
.srcoff
[0]);
848 /* Treat raw memory functions both of whose references are bounded
849 as special and permit uncertain overlaps to go undetected. For
850 all kinds of constant offset and constant size accesses, if
851 overlap isn't certain it is not possible. */
852 bool overlap_possible
= space
[0] < acs
.dstsiz
[1];
853 if (!overlap_possible
)
856 bool overlap_certain
= space
[1] < acs
.dstsiz
[0];
858 /* True when the size of one reference depends on the offset of
860 bool depends_p
= detect_overlap
!= &builtin_access::generic_overlap
;
863 && !dstref
->strbounded_p
867 /* True for stpcpy and strcpy. */
868 bool stxcpy_p
= (!dstref
->strbounded_p
869 && detect_overlap
== &builtin_access::strcpy_overlap
);
871 if (dstref
->refoff
>= 0
872 && srcref
->refoff
>= 0
873 && dstref
->refoff
!= srcref
->refoff
874 && (stxcpy_p
|| dstref
->strbounded_p
|| srcref
->strbounded_p
))
877 offset_int siz
[2] = { maxobjsize
+ 1, 0 };
879 ovloff
[0] = HOST_WIDE_INT_MAX
;
880 ovloff
[1] = HOST_WIDE_INT_MIN
;
882 /* Adjustment to the lower bound of the offset of the overlap to
883 account for a subset of unbounded string calls where the size
884 of the destination string depends on the length of the source
885 which in turn depends on the offset into it. */
890 sub1
= acs
.dstoff
[0] <= acs
.srcoff
[0];
892 /* Iterate over the extreme locations (on the horizontal axis formed
893 by their offsets) and sizes of two regions and find their smallest
894 and largest overlap and the corresponding offsets. */
895 for (unsigned i
= 0; i
!= 2; ++i
)
897 const offset_int a
[2] = {
898 acs
.dstoff
[i
], acs
.dstoff
[i
] + acs
.dstsiz
[!i
]
901 const offset_int b
[2] = {
902 acs
.srcoff
[i
], acs
.srcoff
[i
] + acs
.srcsiz
[!i
]
906 offset_int sz
= overlap_size (a
, b
, &off
);
915 if (wi::lts_p (off
, ovloff
[0]))
916 ovloff
[0] = off
.to_shwi ();
917 if (wi::lts_p (ovloff
[1], off
))
918 ovloff
[1] = off
.to_shwi ();
926 /* Iterate over the extreme locations (on the horizontal axis
927 formed by their offsets) and sizes of two regions and find
928 their smallest and largest overlap and the corresponding
931 for (unsigned io
= 0; io
!= 2; ++io
)
932 for (unsigned is
= 0; is
!= 2; ++is
)
934 const offset_int a
[2] = {
935 acs
.dstoff
[io
], acs
.dstoff
[io
] + acs
.dstsiz
[is
]
938 for (unsigned jo
= 0; jo
!= 2; ++jo
)
939 for (unsigned js
= 0; js
!= 2; ++js
)
943 /* For st{p,r}ncpy the size of the source sequence
944 depends on the offset into it. */
950 const offset_int b
[2] = {
951 acs
.srcoff
[jo
], acs
.srcoff
[jo
] + acs
.srcsiz
[js
]
955 offset_int sz
= overlap_size (a
, b
, &off
);
964 if (wi::lts_p (off
, ovloff
[0]))
965 ovloff
[0] = off
.to_shwi ();
966 if (wi::lts_p (ovloff
[1], off
))
967 ovloff
[1] = off
.to_shwi ();
973 ovlsiz
[0] = siz
[0].to_shwi ();
974 ovlsiz
[1] = siz
[1].to_shwi ();
976 if (ovlsiz
[0] == 0 && ovlsiz
[1] > 1)
977 ovloff
[0] = ovloff
[1] + ovlsiz
[1] - 1 - sub1
;
982 /* Return true if the strcat-like access overlaps. */
985 builtin_access::strcat_overlap ()
987 builtin_access
&acs
= *this;
988 const builtin_memref
*dstref
= acs
.dstref
;
989 const builtin_memref
*srcref
= acs
.srcref
;
991 gcc_assert (dstref
->base
== srcref
->base
);
993 const offset_int maxobjsize
= tree_to_shwi (max_object_size ());
995 gcc_assert (dstref
->base
&& dstref
->base
== srcref
->base
);
997 /* Adjust for strcat-like accesses. */
999 /* As a special case for strcat, set the DSTREF offsets to the length
1000 of the source string since the function starts writing at the first
1001 nul, and set the size to 1 for the length of the nul. */
1002 acs
.dstoff
[0] += acs
.dstsiz
[0];
1003 acs
.dstoff
[1] += acs
.dstsiz
[1];
1005 bool strfunc_unknown_args
= acs
.dstsiz
[0] == 0 && acs
.dstsiz
[1] != 0;
1007 /* The lower bound is zero when the size is unknown because then
1008 overlap is not certain. */
1009 acs
.dstsiz
[0] = strfunc_unknown_args
? 0 : 1;
1012 offset_int maxsize
= dstref
->basesize
< 0 ? maxobjsize
: dstref
->basesize
;
1013 gcc_assert (maxsize
<= maxobjsize
);
1015 /* For references to the same base object, determine if there's a pair
1016 of valid offsets into the two references such that access between
1017 them doesn't overlap. Adjust both upper bounds to be valid for
1018 the smaller size (i.e., at most MAXSIZE - SIZE). */
1020 if (maxsize
< acs
.dstoff
[1] + acs
.dstsiz
[0])
1021 acs
.dstoff
[1] = maxsize
- acs
.dstsiz
[0];
1023 if (maxsize
< acs
.srcoff
[1] + acs
.srcsiz
[0])
1024 acs
.srcoff
[1] = maxsize
- acs
.srcsiz
[0];
1026 /* Check to see if there's enough space for both accesses without
1027 overlap. Determine the optimistic (maximum) amount of available
1030 if (acs
.dstoff
[0] <= acs
.srcoff
[0])
1032 if (acs
.dstoff
[1] < acs
.srcoff
[1])
1033 space
= acs
.srcoff
[1] + acs
.srcsiz
[0] - acs
.dstoff
[0];
1035 space
= acs
.dstoff
[1] + acs
.dstsiz
[0] - acs
.srcoff
[0];
1038 space
= acs
.dstoff
[1] + acs
.dstsiz
[0] - acs
.srcoff
[0];
1040 /* Overlap is certain if the distance between the farthest offsets
1041 of the opposite accesses is less than the sum of the lower bounds
1042 of the sizes of the two accesses. */
1043 bool overlap_certain
= space
< acs
.dstsiz
[0] + acs
.srcsiz
[0];
1045 /* For a constant-offset, constant size access, consider the largest
1046 distance between the offset bounds and the lower bound of the access
1047 size. If the overlap isn't certain return success. */
1048 if (!overlap_certain
1049 && acs
.dstoff
[0] == acs
.dstoff
[1]
1050 && acs
.srcoff
[0] == acs
.srcoff
[1]
1051 && acs
.dstsiz
[0] == acs
.dstsiz
[1]
1052 && acs
.srcsiz
[0] == acs
.srcsiz
[1])
1055 /* Overlap is not certain but may be possible. */
1057 offset_int access_min
= acs
.dstsiz
[0] + acs
.srcsiz
[0];
1059 /* Determine the conservative (minimum) amount of space. */
1060 space
= wi::abs (acs
.dstoff
[0] - acs
.srcoff
[0]);
1061 offset_int d
= wi::abs (acs
.dstoff
[0] - acs
.srcoff
[1]);
1064 d
= wi::abs (acs
.dstoff
[1] - acs
.srcoff
[0]);
1068 /* For a strict test (used for strcpy and similar with unknown or
1069 variable bounds or sizes), consider the smallest distance between
1070 the offset bounds and either the upper bound of the access size
1071 if known, or the lower bound otherwise. */
1072 if (access_min
<= space
&& (access_min
!= 0 || !strfunc_unknown_args
))
1075 /* When strcat overlap is certain it is always a single byte:
1076 the terminatinn NUL, regardless of offsets and sizes. When
1077 overlap is only possible its range is [0, 1]. */
1078 acs
.ovlsiz
[0] = dstref
->sizrange
[0] == dstref
->sizrange
[1] ? 1 : 0;
1080 acs
.ovloff
[0] = (dstref
->sizrange
[0] + dstref
->offrange
[0]).to_shwi ();
1081 acs
.ovloff
[1] = (dstref
->sizrange
[1] + dstref
->offrange
[1]).to_shwi ();
1083 acs
.sizrange
[0] = wi::smax (acs
.dstsiz
[0], srcref
->sizrange
[0]).to_shwi ();
1084 acs
.sizrange
[1] = wi::smax (acs
.dstsiz
[1], srcref
->sizrange
[1]).to_shwi ();
1088 /* Return true if the strcpy-like access overlaps. */
1091 builtin_access::strcpy_overlap ()
1093 return generic_overlap ();
1097 /* Return true if DSTREF and SRCREF describe accesses that either overlap
1098 one another or that, in order not to overlap, would imply that the size
1099 of the referenced object(s) exceeds the maximum size of an object. Set
1100 Otherwise, if DSTREF and SRCREF do not definitely overlap (even though
1101 they may overlap in a way that's not apparent from the available data),
1105 builtin_access::overlap ()
1107 builtin_access
&acs
= *this;
1109 const offset_int maxobjsize
= tree_to_shwi (max_object_size ());
1111 acs
.sizrange
[0] = wi::smax (dstref
->sizrange
[0],
1112 srcref
->sizrange
[0]).to_shwi ();
1113 acs
.sizrange
[1] = wi::smax (dstref
->sizrange
[1],
1114 srcref
->sizrange
[1]).to_shwi ();
1116 /* Check to see if the two references refer to regions that are
1117 too large not to overlap in the address space (whose maximum
1118 size is PTRDIFF_MAX). */
1119 offset_int size
= dstref
->sizrange
[0] + srcref
->sizrange
[0];
1120 if (maxobjsize
< size
)
1122 acs
.ovloff
[0] = (maxobjsize
- dstref
->sizrange
[0]).to_shwi ();
1123 acs
.ovlsiz
[0] = (size
- maxobjsize
).to_shwi ();
1127 /* If both base objects aren't known return the maximum possible
1128 offset that would make them not overlap. */
1129 if (!dstref
->base
|| !srcref
->base
)
1132 /* If the base object is an array adjust the lower bound of the offset
1133 to be non-negative. */
1135 && TREE_CODE (TREE_TYPE (dstref
->base
)) == ARRAY_TYPE
)
1136 acs
.dstoff
[0] = wi::smax (dstref
->offrange
[0], 0);
1138 acs
.dstoff
[0] = dstref
->offrange
[0];
1140 acs
.dstoff
[1] = dstref
->offrange
[1];
1143 && TREE_CODE (TREE_TYPE (srcref
->base
)) == ARRAY_TYPE
)
1144 acs
.srcoff
[0] = wi::smax (srcref
->offrange
[0], 0);
1146 acs
.srcoff
[0] = srcref
->offrange
[0];
1148 acs
.srcoff
[1] = srcref
->offrange
[1];
1150 /* When the lower bound of the offset is less that the upper bound
1151 disregard it and use the inverse of the maximum object size
1152 instead. The upper bound is the result of a negative offset
1153 being represented as a large positive value. */
1154 if (acs
.dstoff
[1] < acs
.dstoff
[0])
1155 acs
.dstoff
[0] = -maxobjsize
;
1157 /* Validate the offset and size of each reference on its own first.
1158 This is independent of whether or not the base objects are the
1159 same. Normally, this would have already been detected and
1160 diagnosed by -Warray-bounds, unless it has been disabled. */
1161 offset_int maxoff
= acs
.dstoff
[0] + dstref
->sizrange
[0];
1162 if (maxobjsize
< maxoff
)
1164 acs
.ovlsiz
[0] = (maxoff
- maxobjsize
).to_shwi ();
1165 acs
.ovloff
[0] = acs
.dstoff
[0].to_shwi () - acs
.ovlsiz
[0];
1169 /* Repeat the same as above but for the source offsets. */
1170 if (acs
.srcoff
[1] < acs
.srcoff
[0])
1171 acs
.srcoff
[0] = -maxobjsize
;
1173 maxoff
= acs
.srcoff
[0] + srcref
->sizrange
[0];
1174 if (maxobjsize
< maxoff
)
1176 acs
.ovlsiz
[0] = (maxoff
- maxobjsize
).to_shwi ();
1177 acs
.ovlsiz
[1] = (acs
.srcoff
[0] + srcref
->sizrange
[1]
1178 - maxobjsize
).to_shwi ();
1179 acs
.ovloff
[0] = acs
.srcoff
[0].to_shwi () - acs
.ovlsiz
[0];
1183 if (dstref
->base
!= srcref
->base
)
1186 acs
.dstsiz
[0] = dstref
->sizrange
[0];
1187 acs
.dstsiz
[1] = dstref
->sizrange
[1];
1189 acs
.srcsiz
[0] = srcref
->sizrange
[0];
1190 acs
.srcsiz
[1] = srcref
->sizrange
[1];
1192 /* Call the appropriate function to determine the overlap. */
1193 if ((this->*detect_overlap
) ())
1195 sizrange
[0] = wi::smax (acs
.dstsiz
[0], srcref
->sizrange
[0]).to_shwi ();
1196 sizrange
[1] = wi::smax (acs
.dstsiz
[1], srcref
->sizrange
[1]).to_shwi ();
1203 /* Attempt to detect and diagnose an overlapping copy in a call expression
1204 EXPR involving an an access ACS to a built-in memory or string function.
1205 Return true when one has been detected, false otherwise. */
1208 maybe_diag_overlap (location_t loc
, gcall
*call
, builtin_access
&acs
)
1210 if (!acs
.overlap ())
1213 /* For convenience. */
1214 const builtin_memref
&dstref
= *acs
.dstref
;
1215 const builtin_memref
&srcref
= *acs
.srcref
;
1217 /* Determine the range of offsets and sizes of the overlap if it
1218 exists and issue diagnostics. */
1219 HOST_WIDE_INT
*ovloff
= acs
.ovloff
;
1220 HOST_WIDE_INT
*ovlsiz
= acs
.ovlsiz
;
1221 HOST_WIDE_INT
*sizrange
= acs
.sizrange
;
1223 tree func
= gimple_call_fndecl (call
);
1225 /* To avoid a combinatorial explosion of diagnostics format the offsets
1226 or their ranges as strings and use them in the warning calls below. */
1229 if (dstref
.offrange
[0] == dstref
.offrange
[1]
1230 || dstref
.offrange
[1] > HOST_WIDE_INT_MAX
)
1231 sprintf (offstr
[0], "%lli", (long long) dstref
.offrange
[0].to_shwi ());
1233 sprintf (offstr
[0], "[%lli, %lli]",
1234 (long long) dstref
.offrange
[0].to_shwi (),
1235 (long long) dstref
.offrange
[1].to_shwi ());
1237 if (srcref
.offrange
[0] == srcref
.offrange
[1]
1238 || srcref
.offrange
[1] > HOST_WIDE_INT_MAX
)
1239 sprintf (offstr
[1], "%lli", (long long) srcref
.offrange
[0].to_shwi ());
1241 sprintf (offstr
[1], "[%lli, %lli]",
1242 (long long) srcref
.offrange
[0].to_shwi (),
1243 (long long) srcref
.offrange
[1].to_shwi ());
1245 if (ovloff
[0] == ovloff
[1] || !ovloff
[1])
1246 sprintf (offstr
[2], "%lli", (long long) ovloff
[0]);
1248 sprintf (offstr
[2], "[%lli, %lli]",
1249 (long long) ovloff
[0], (long long) ovloff
[1]);
1251 const offset_int maxobjsize
= tree_to_shwi (max_object_size ());
1252 bool must_overlap
= ovlsiz
[0] > 0;
1255 ovlsiz
[1] = ovlsiz
[0];
1259 /* Issue definitive "overlaps" diagnostic in this block. */
1261 if (sizrange
[0] == sizrange
[1])
1263 if (ovlsiz
[0] == ovlsiz
[1])
1264 warning_at (loc
, OPT_Wrestrict
,
1267 ? G_("%G%qD accessing %wu byte at offsets %s "
1268 "and %s overlaps %wu byte at offset %s")
1269 : G_("%G%qD accessing %wu byte at offsets %s "
1270 "and %s overlaps %wu bytes at offset "
1273 ? G_("%G%qD accessing %wu bytes at offsets %s "
1274 "and %s overlaps %wu byte at offset %s")
1275 : G_("%G%qD accessing %wu bytes at offsets %s "
1276 "and %s overlaps %wu bytes at offset "
1278 call
, func
, sizrange
[0],
1279 offstr
[0], offstr
[1], ovlsiz
[0], offstr
[2]);
1280 else if (ovlsiz
[1] >= 0 && ovlsiz
[1] < maxobjsize
.to_shwi ())
1281 warning_at (loc
, OPT_Wrestrict
,
1283 ? G_("%G%qD accessing %wu byte at offsets %s "
1284 "and %s overlaps between %wu and %wu bytes "
1286 : G_("%G%qD accessing %wu bytes at offsets %s "
1287 "and %s overlaps between %wu and %wu bytes "
1289 call
, func
, sizrange
[0],
1290 offstr
[0], offstr
[1], ovlsiz
[0], ovlsiz
[1],
1293 warning_at (loc
, OPT_Wrestrict
,
1295 ? G_("%G%qD accessing %wu byte at offsets %s and "
1296 "%s overlaps %wu or more bytes at offset %s")
1297 : G_("%G%qD accessing %wu bytes at offsets %s and "
1298 "%s overlaps %wu or more bytes at offset %s"),
1299 call
, func
, sizrange
[0],
1300 offstr
[0], offstr
[1], ovlsiz
[0], offstr
[2]);
1304 if (sizrange
[1] >= 0 && sizrange
[1] < maxobjsize
.to_shwi ())
1306 if (ovlsiz
[0] == ovlsiz
[1])
1307 warning_at (loc
, OPT_Wrestrict
,
1309 ? G_("%G%qD accessing between %wu and %wu bytes "
1310 "at offsets %s and %s overlaps %wu byte at "
1312 : G_("%G%qD accessing between %wu and %wu bytes "
1313 "at offsets %s and %s overlaps %wu bytes "
1315 call
, func
, sizrange
[0], sizrange
[1],
1316 offstr
[0], offstr
[1], ovlsiz
[0], offstr
[2]);
1317 else if (ovlsiz
[1] >= 0 && ovlsiz
[1] < maxobjsize
.to_shwi ())
1318 warning_at (loc
, OPT_Wrestrict
,
1319 "%G%qD accessing between %wu and %wu bytes at "
1320 "offsets %s and %s overlaps between %wu and %wu "
1321 "bytes at offset %s",
1322 call
, func
, sizrange
[0], sizrange
[1],
1323 offstr
[0], offstr
[1], ovlsiz
[0], ovlsiz
[1],
1326 warning_at (loc
, OPT_Wrestrict
,
1327 "%G%qD accessing between %wu and %wu bytes at "
1328 "offsets %s and %s overlaps %wu or more bytes "
1330 call
, func
, sizrange
[0], sizrange
[1],
1331 offstr
[0], offstr
[1], ovlsiz
[0], offstr
[2]);
1335 if (ovlsiz
[0] != ovlsiz
[1])
1336 ovlsiz
[1] = maxobjsize
.to_shwi ();
1338 if (ovlsiz
[0] == ovlsiz
[1])
1339 warning_at (loc
, OPT_Wrestrict
,
1341 ? G_("%G%qD accessing %wu or more bytes at offsets "
1342 "%s and %s overlaps %wu byte at offset %s")
1343 : G_("%G%qD accessing %wu or more bytes at offsets "
1344 "%s and %s overlaps %wu bytes at offset %s"),
1345 call
, func
, sizrange
[0], offstr
[0], offstr
[1],
1346 ovlsiz
[0], offstr
[2]);
1347 else if (ovlsiz
[1] >= 0 && ovlsiz
[1] < maxobjsize
.to_shwi ())
1348 warning_at (loc
, OPT_Wrestrict
,
1349 "%G%qD accessing %wu or more bytes at offsets %s "
1350 "and %s overlaps between %wu and %wu bytes "
1352 call
, func
, sizrange
[0], offstr
[0], offstr
[1],
1353 ovlsiz
[0], ovlsiz
[1], offstr
[2]);
1355 warning_at (loc
, OPT_Wrestrict
,
1356 "%G%qD accessing %wu or more bytes at offsets %s "
1357 "and %s overlaps %wu or more bytes at offset %s",
1358 call
, func
, sizrange
[0], offstr
[0], offstr
[1],
1359 ovlsiz
[0], offstr
[2]);
1363 /* Issue "may overlap" diagnostics below. */
1364 gcc_assert (ovlsiz
[0] == 0
1366 && ovlsiz
[1] <= maxobjsize
.to_shwi ());
1368 /* Use more concise wording when one of the offsets is unbounded
1369 to avoid confusing the user with large and mostly meaningless
1371 bool open_range
= ((dstref
.offrange
[0] == -maxobjsize
- 1
1372 && dstref
.offrange
[1] == maxobjsize
)
1373 || (srcref
.offrange
[0] == -maxobjsize
- 1
1374 && srcref
.offrange
[1] == maxobjsize
));
1376 if (sizrange
[0] == sizrange
[1] || sizrange
[1] == 1)
1381 warning_at (loc
, OPT_Wrestrict
,
1383 ? G_("%G%qD accessing %wu byte may overlap "
1385 : G_("%G%qD accessing %wu bytes may overlap "
1387 call
, func
, sizrange
[1], ovlsiz
[1]);
1389 warning_at (loc
, OPT_Wrestrict
,
1391 ? G_("%G%qD accessing %wu byte at offsets %s "
1392 "and %s may overlap %wu byte at offset %s")
1393 : G_("%G%qD accessing %wu bytes at offsets %s "
1394 "and %s may overlap %wu byte at offset %s"),
1395 call
, func
, sizrange
[1], offstr
[0], offstr
[1],
1396 ovlsiz
[1], offstr
[2]);
1401 warning_at (loc
, OPT_Wrestrict
,
1403 ? G_("%G%qD accessing %wu byte may overlap "
1405 : G_("%G%qD accessing %wu bytes may overlap "
1407 call
, func
, sizrange
[1], ovlsiz
[1]);
1409 warning_at (loc
, OPT_Wrestrict
,
1411 ? G_("%G%qD accessing %wu byte at offsets %s and "
1412 "%s may overlap up to %wu bytes at offset %s")
1413 : G_("%G%qD accessing %wu bytes at offsets %s and "
1414 "%s may overlap up to %wu bytes at offset %s"),
1415 call
, func
, sizrange
[1], offstr
[0], offstr
[1],
1416 ovlsiz
[1], offstr
[2]);
1420 if (sizrange
[1] >= 0 && sizrange
[1] < maxobjsize
.to_shwi ())
1423 warning_at (loc
, OPT_Wrestrict
,
1425 ? G_("%G%qD accessing between %wu and %wu bytes "
1426 "may overlap %wu byte")
1427 : G_("%G%qD accessing between %wu and %wu bytes "
1428 "may overlap up to %wu bytes"),
1429 call
, func
, sizrange
[0], sizrange
[1], ovlsiz
[1]);
1431 warning_at (loc
, OPT_Wrestrict
,
1433 ? G_("%G%qD accessing between %wu and %wu bytes "
1434 "at offsets %s and %s may overlap %wu byte "
1436 : G_("%G%qD accessing between %wu and %wu bytes "
1437 "at offsets %s and %s may overlap up to %wu "
1438 "bytes at offset %s"),
1439 call
, func
, sizrange
[0], sizrange
[1],
1440 offstr
[0], offstr
[1], ovlsiz
[1], offstr
[2]);
1444 warning_at (loc
, OPT_Wrestrict
,
1446 ? G_("%G%qD accessing %wu or more bytes at offsets %s "
1447 "and %s may overlap %wu byte at offset %s")
1448 : G_("%G%qD accessing %wu or more bytes at offsets %s "
1449 "and %s may overlap up to %wu bytes at offset %s"),
1450 call
, func
, sizrange
[0], offstr
[0], offstr
[1],
1451 ovlsiz
[1], offstr
[2]);
1456 /* Validate REF offsets in an EXPRession passed as an argument to a CALL
1457 to a built-in function FUNC to make sure they are within the bounds
1458 of the referenced object if its size is known, or PTRDIFF_MAX otherwise.
1459 Both initial values of the offsets and their final value computed by
1460 the function by incrementing the initial value by the size are
1461 validated. Return true if the offsets are not valid and a diagnostic
1465 maybe_diag_offset_bounds (location_t loc
, gcall
*call
, tree func
, int strict
,
1466 tree expr
, const builtin_memref
&ref
)
1468 if (!warn_array_bounds
)
1471 offset_int ooboff
[] = { ref
.offrange
[0], ref
.offrange
[1] };
1472 tree oobref
= ref
.offset_out_of_bounds (strict
, ooboff
);
1476 if (EXPR_HAS_LOCATION (expr
))
1477 loc
= EXPR_LOCATION (expr
);
1479 loc
= expansion_point_location_if_in_system_header (loc
);
1483 char rangestr
[2][64];
1484 if (ooboff
[0] == ooboff
[1]
1485 || (ooboff
[0] != ref
.offrange
[0]
1486 && ooboff
[0].to_shwi () >= ooboff
[1].to_shwi ()))
1487 sprintf (rangestr
[0], "%lli", (long long) ooboff
[0].to_shwi ());
1489 sprintf (rangestr
[0], "[%lli, %lli]",
1490 (long long) ooboff
[0].to_shwi (),
1491 (long long) ooboff
[1].to_shwi ());
1493 if (oobref
== error_mark_node
)
1495 if (ref
.sizrange
[0] == ref
.sizrange
[1])
1496 sprintf (rangestr
[1], "%lli", (long long) ref
.sizrange
[0].to_shwi ());
1498 sprintf (rangestr
[1], "[%lli, %lli]",
1499 (long long) ref
.sizrange
[0].to_shwi (),
1500 (long long) ref
.sizrange
[1].to_shwi ());
1502 if (DECL_P (ref
.base
)
1503 && TREE_CODE (type
= TREE_TYPE (ref
.base
)) == ARRAY_TYPE
)
1505 if (warning_at (loc
, OPT_Warray_bounds
,
1506 "%G%qD pointer overflow between offset %s "
1507 "and size %s accessing array %qD with type %qT",
1508 call
, func
, rangestr
[0], rangestr
[1], ref
.base
, type
))
1509 inform (DECL_SOURCE_LOCATION (ref
.base
),
1510 "array %qD declared here", ref
.base
);
1512 warning_at (loc
, OPT_Warray_bounds
,
1513 "%G%qD pointer overflow between offset %s "
1515 call
, func
, rangestr
[0], rangestr
[1]);
1518 warning_at (loc
, OPT_Warray_bounds
,
1519 "%G%qD pointer overflow between offset %s "
1521 call
, func
, rangestr
[0], rangestr
[1]);
1523 else if (oobref
== ref
.base
)
1525 const offset_int maxobjsize
= tree_to_shwi (max_object_size ());
1527 /* True when the offset formed by an access to the reference
1528 is out of bounds, rather than the initial offset wich is
1529 in bounds. This implies access past the end. */
1530 bool form
= ooboff
[0] != ref
.offrange
[0];
1532 if (DECL_P (ref
.base
))
1534 if ((ref
.basesize
< maxobjsize
1535 && warning_at (loc
, OPT_Warray_bounds
,
1537 ? G_("%G%qD forming offset %s is out of "
1538 "the bounds [0, %wu] of object %qD with "
1540 : G_("%G%qD offset %s is out of the bounds "
1541 "[0, %wu] of object %qD with type %qT"),
1542 call
, func
, rangestr
[0], ref
.basesize
.to_uhwi (),
1543 ref
.base
, TREE_TYPE (ref
.base
)))
1544 || warning_at (loc
, OPT_Warray_bounds
,
1546 ? G_("%G%qD forming offset %s is out of "
1547 "the bounds of object %qD with type %qT")
1548 : G_("%G%qD offset %s is out of the bounds "
1549 "of object %qD with type %qT"),
1550 call
, func
, rangestr
[0],
1551 ref
.base
, TREE_TYPE (ref
.base
)))
1552 inform (DECL_SOURCE_LOCATION (ref
.base
),
1553 "%qD declared here", ref
.base
);
1555 else if (ref
.basesize
< maxobjsize
)
1556 warning_at (loc
, OPT_Warray_bounds
,
1558 ? G_("%G%qD forming offset %s is out of the bounds "
1560 : G_("%G%qD offset %s is out of the bounds [0, %wu]"),
1561 call
, func
, rangestr
[0], ref
.basesize
.to_uhwi ());
1563 warning_at (loc
, OPT_Warray_bounds
,
1565 ? G_("%G%qD forming offset %s is out of bounds")
1566 : G_("%G%qD offset %s is out of bounds"),
1567 call
, func
, rangestr
[0]);
1569 else if (TREE_CODE (ref
.ref
) == MEM_REF
)
1571 tree type
= TREE_TYPE (TREE_OPERAND (ref
.ref
, 0));
1572 if (POINTER_TYPE_P (type
))
1573 type
= TREE_TYPE (type
);
1574 type
= TYPE_MAIN_VARIANT (type
);
1576 warning_at (loc
, OPT_Warray_bounds
,
1577 "%G%qD offset %s from the object at %qE is out "
1578 "of the bounds of %qT",
1579 call
, func
, rangestr
[0], ref
.base
, type
);
1583 type
= TYPE_MAIN_VARIANT (TREE_TYPE (ref
.ref
));
1585 warning_at (loc
, OPT_Warray_bounds
,
1586 "%G%qD offset %s from the object at %qE is out "
1587 "of the bounds of referenced subobject %qD with type %qT "
1589 call
, func
, rangestr
[0], ref
.base
, TREE_OPERAND (ref
.ref
, 1),
1590 type
, ref
.refoff
.to_uhwi ());
1596 /* Check a CALL statement for restrict-violations and issue warnings
1597 if/when appropriate. */
1600 wrestrict_dom_walker::check_call (gcall
*call
)
1602 /* Avoid checking the call if it has already been diagnosed for
1604 if (gimple_no_warning_p (call
))
1607 tree func
= gimple_call_fndecl (call
);
1608 if (!func
|| DECL_BUILT_IN_CLASS (func
) != BUILT_IN_NORMAL
)
1611 bool with_bounds
= gimple_call_with_bounds_p (call
);
1613 /* Argument number to extract from the call (depends on the built-in
1615 unsigned dst_idx
= -1;
1616 unsigned src_idx
= -1;
1617 unsigned bnd_idx
= -1;
1619 /* Is this CALL to a string function (as opposed to one to a raw
1620 memory function). */
1623 switch (DECL_FUNCTION_CODE (func
))
1625 case BUILT_IN_MEMCPY
:
1626 case BUILT_IN_MEMCPY_CHK
:
1627 case BUILT_IN_MEMCPY_CHKP
:
1628 case BUILT_IN_MEMCPY_CHK_CHKP
:
1629 case BUILT_IN_MEMPCPY
:
1630 case BUILT_IN_MEMPCPY_CHK
:
1631 case BUILT_IN_MEMPCPY_CHKP
:
1632 case BUILT_IN_MEMPCPY_CHK_CHKP
:
1633 case BUILT_IN_MEMMOVE
:
1634 case BUILT_IN_MEMMOVE_CHK
:
1635 case BUILT_IN_MEMMOVE_CHKP
:
1636 case BUILT_IN_MEMMOVE_CHK_CHKP
:
1640 case BUILT_IN_STPNCPY
:
1641 case BUILT_IN_STPNCPY_CHK
:
1642 case BUILT_IN_STRNCAT
:
1643 case BUILT_IN_STRNCAT_CHK
:
1644 case BUILT_IN_STRNCPY
:
1645 case BUILT_IN_STRNCPY_CHK
:
1647 src_idx
= 1 + with_bounds
;
1648 bnd_idx
= 2 + 2 * with_bounds
;
1651 case BUILT_IN_STPCPY
:
1652 case BUILT_IN_STPCPY_CHK
:
1653 case BUILT_IN_STPCPY_CHKP
:
1654 case BUILT_IN_STPCPY_CHK_CHKP
:
1655 case BUILT_IN_STRCPY
:
1656 case BUILT_IN_STRCPY_CHK
:
1657 case BUILT_IN_STRCPY_CHKP
:
1658 case BUILT_IN_STRCPY_CHK_CHKP
:
1659 case BUILT_IN_STRCAT
:
1660 case BUILT_IN_STRCAT_CHK
:
1661 case BUILT_IN_STRCAT_CHKP
:
1662 case BUILT_IN_STRCAT_CHK_CHKP
:
1664 src_idx
= 1 + with_bounds
;
1668 /* Handle other string functions here whose access may need
1669 to be validated for in-bounds offsets and non-overlapping
1670 copies. (Not all _chkp functions have BUILT_IN_XXX_CHKP
1671 macros so they need to be handled here.) */
1675 unsigned nargs
= gimple_call_num_args (call
);
1677 tree dst
= dst_idx
< nargs
? gimple_call_arg (call
, dst_idx
) : NULL_TREE
;
1678 tree src
= src_idx
< nargs
? gimple_call_arg (call
, src_idx
) : NULL_TREE
;
1679 tree dstwr
= bnd_idx
< nargs
? gimple_call_arg (call
, bnd_idx
) : NULL_TREE
;
1681 /* For string functions with an unspecified or unknown bound,
1682 assume the size of the access is one. */
1683 if (!dstwr
&& strfun
)
1684 dstwr
= size_one_node
;
1686 if (check_bounds_or_overlap (call
, dst
, src
, dstwr
, NULL_TREE
))
1689 /* Avoid diagnosing the call again. */
1690 gimple_set_no_warning (call
, true);
1693 } /* anonymous namespace */
1695 /* Attempt to detect and diagnose invalid offset bounds and (except for
1696 memmove) overlapping copy in a call expression EXPR from SRC to DST
1697 and DSTSIZE and SRCSIZE bytes, respectively. Both DSTSIZE and
1698 SRCSIZE may be NULL. Return false when one or the other has been
1699 detected and diagnosed, true otherwise. */
1702 check_bounds_or_overlap (gcall
*call
, tree dst
, tree src
, tree dstsize
,
1703 tree srcsize
, bool bounds_only
/* = false */)
1705 location_t loc
= gimple_location (call
);
1707 if (tree block
= gimple_block (call
))
1708 if (location_t
*pbloc
= block_nonartificial_location (block
))
1711 loc
= expansion_point_location_if_in_system_header (loc
);
1713 tree func
= gimple_call_fndecl (call
);
1715 builtin_memref
dstref (dst
, dstsize
);
1716 builtin_memref
srcref (src
, srcsize
);
1718 builtin_access
acs (call
, dstref
, srcref
);
1720 /* Set STRICT to the value of the -Warray-bounds=N argument for
1721 string functions or when N > 1. */
1722 int strict
= (acs
.strict () || warn_array_bounds
> 1 ? warn_array_bounds
: 0);
1724 /* Validate offsets first to make sure they are within the bounds
1725 of the destination object if its size is known, or PTRDIFF_MAX
1727 if (maybe_diag_offset_bounds (loc
, call
, func
, strict
, dst
, dstref
)
1728 || maybe_diag_offset_bounds (loc
, call
, func
, strict
, src
, srcref
))
1730 gimple_set_no_warning (call
, true);
1737 || (DECL_FUNCTION_CODE (func
) != BUILT_IN_MEMMOVE
1738 && DECL_FUNCTION_CODE (func
) != BUILT_IN_MEMMOVE_CHK
)));
1743 if (operand_equal_p (dst
, src
, 0))
1745 warning_at (loc
, OPT_Wrestrict
,
1746 "%G%qD source argument is the same as destination",
1748 gimple_set_no_warning (call
, true);
1752 /* Return false when overlap has been detected. */
1753 if (maybe_diag_overlap (loc
, call
, acs
))
1755 gimple_set_no_warning (call
, true);
1763 make_pass_warn_restrict (gcc::context
*ctxt
)
1765 return new pass_wrestrict (ctxt
);