]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/gimple-ssa-warn-restrict.c
PR c++/61339 - add mismatch between struct and class [-Wmismatched-tags] to non-bugs
[thirdparty/gcc.git] / gcc / gimple-ssa-warn-restrict.c
CommitLineData
cc8bea0a
MS
1/* Pass to detect and issue warnings for violations of the restrict
2 qualifier.
a5544970 3 Copyright (C) 2017-2019 Free Software Foundation, Inc.
cc8bea0a
MS
4 Contributed by Martin Sebor <msebor@redhat.com>.
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "backend.h"
26#include "tree.h"
27#include "gimple.h"
28#include "domwalk.h"
29#include "tree-pass.h"
30#include "builtins.h"
31#include "ssa.h"
32#include "gimple-pretty-print.h"
33#include "gimple-ssa-warn-restrict.h"
34#include "diagnostic-core.h"
35#include "fold-const.h"
36#include "gimple-iterator.h"
37#include "tree-dfa.h"
38#include "tree-ssa.h"
39#include "params.h"
40#include "tree-cfg.h"
41#include "tree-object-size.h"
42#include "calls.h"
43#include "cfgloop.h"
44#include "intl.h"
45
46namespace {
47
48const pass_data pass_data_wrestrict = {
49 GIMPLE_PASS,
50 "wrestrict",
51 OPTGROUP_NONE,
52 TV_NONE,
53 PROP_cfg, /* Properties_required. */
54 0, /* properties_provided. */
55 0, /* properties_destroyed. */
56 0, /* properties_start */
57 0, /* properties_finish */
58};
59
60/* Pass to detect violations of strict aliasing requirements in calls
61 to built-in string and raw memory functions. */
62class pass_wrestrict : public gimple_opt_pass
63{
64 public:
65 pass_wrestrict (gcc::context *ctxt)
66 : gimple_opt_pass (pass_data_wrestrict, ctxt)
67 { }
68
69 opt_pass *clone () { return new pass_wrestrict (m_ctxt); }
70
71 virtual bool gate (function *);
72 virtual unsigned int execute (function *);
73};
74
75bool
76pass_wrestrict::gate (function *fun ATTRIBUTE_UNUSED)
77{
287ef129 78 return warn_array_bounds || warn_restrict || warn_stringop_overflow;
cc8bea0a
MS
79}
80
81/* Class to walk the basic blocks of a function in dominator order. */
82class wrestrict_dom_walker : public dom_walker
83{
84 public:
85 wrestrict_dom_walker () : dom_walker (CDI_DOMINATORS) {}
86
87 edge before_dom_children (basic_block) FINAL OVERRIDE;
88 bool handle_gimple_call (gimple_stmt_iterator *);
89
90 private:
8a45b051 91 void check_call (gimple *);
cc8bea0a
MS
92};
93
94edge
95wrestrict_dom_walker::before_dom_children (basic_block bb)
96{
97 /* Iterate over statements, looking for function calls. */
98 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
99 gsi_next (&si))
100 {
101 gimple *stmt = gsi_stmt (si);
102 if (!is_gimple_call (stmt))
103 continue;
104
8a45b051 105 check_call (stmt);
cc8bea0a
MS
106 }
107
108 return NULL;
109}
110
111/* Execute the pass for function FUN, walking in dominator order. */
112
113unsigned
114pass_wrestrict::execute (function *fun)
115{
116 calculate_dominance_info (CDI_DOMINATORS);
117
118 wrestrict_dom_walker walker;
119 walker.walk (ENTRY_BLOCK_PTR_FOR_FN (fun));
120
121 return 0;
122}
123
124/* Description of a memory reference by a built-in function. This
125 is similar to ao_ref but made especially suitable for -Wrestrict
126 and not for optimization. */
6c1dae73 127class builtin_memref
cc8bea0a 128{
6c1dae73 129public:
cc8bea0a
MS
130 /* The original pointer argument to the built-in function. */
131 tree ptr;
132 /* The referenced subobject or NULL if not available, and the base
133 object of the memory reference or NULL. */
134 tree ref;
135 tree base;
136
137 /* The size of the BASE object, PTRDIFF_MAX if indeterminate,
138 and negative until (possibly lazily) initialized. */
139 offset_int basesize;
140
141 /* The non-negative offset of the referenced subobject. Used to avoid
142 warnings for (apparently) possibly but not definitively overlapping
143 accesses to member arrays. Negative when unknown/invalid. */
144 offset_int refoff;
145
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];
150
a411ae9b
MS
151 /* Cached result of get_max_objsize(). */
152 const offset_int maxobjsize;
153
cc8bea0a
MS
154 /* True for "bounded" string functions like strncat, and strncpy
155 and their variants that specify either an exact or upper bound
156 on the size of the accesses they perform. For strncat both
157 the source and destination references are bounded. For strncpy
158 only the destination reference is. */
159 bool strbounded_p;
160
161 builtin_memref (tree, tree);
162
163 tree offset_out_of_bounds (int, offset_int[2]) const;
5e27f0d5
MS
164
165private:
166
167 /* Ctor helper to set or extend OFFRANGE based on argument. */
168 void extend_offset_range (tree);
169
170 /* Ctor helper to determine BASE and OFFRANGE from argument. */
171 void set_base_and_offset (tree);
cc8bea0a
MS
172};
173
174/* Description of a memory access by a raw memory or string built-in
175 function involving a pair of builtin_memref's. */
176class builtin_access
177{
178 public:
179 /* Destination and source memory reference. */
180 builtin_memref* const dstref;
181 builtin_memref* const srcref;
182 /* The size range of the access. It's the greater of the accesses
183 to the two references. */
184 HOST_WIDE_INT sizrange[2];
185
186 /* The minimum and maximum offset of an overlap of the access
187 (if it does, in fact, overlap), and the size of the overlap. */
188 HOST_WIDE_INT ovloff[2];
189 HOST_WIDE_INT ovlsiz[2];
190
191 /* True to consider valid only accesses to the smallest subobject
192 and false for raw memory functions. */
193 bool strict () const
194 {
195 return detect_overlap != &builtin_access::generic_overlap;
196 }
197
8a45b051 198 builtin_access (gimple *, builtin_memref &, builtin_memref &);
cc8bea0a
MS
199
200 /* Entry point to determine overlap. */
201 bool overlap ();
202
203 private:
204 /* Implementation functions used to determine overlap. */
205 bool generic_overlap ();
206 bool strcat_overlap ();
207 bool strcpy_overlap ();
208
209 bool no_overlap ()
210 {
211 return false;
212 }
213
214 offset_int overlap_size (const offset_int [2], const offset_int[2],
215 offset_int [2]);
216
217 private:
218 /* Temporaries used to compute the final result. */
219 offset_int dstoff[2];
220 offset_int srcoff[2];
221 offset_int dstsiz[2];
222 offset_int srcsiz[2];
223
224 /* Pointer to a member function to call to determine overlap. */
225 bool (builtin_access::*detect_overlap) ();
226};
227
228/* Initialize a memory reference representation from a pointer EXPR and
229 a size SIZE in bytes. If SIZE is NULL_TREE then the size is assumed
230 to be unknown. */
231
232builtin_memref::builtin_memref (tree expr, tree size)
233: ptr (expr),
234 ref (),
235 base (),
236 basesize (-1),
237 refoff (HOST_WIDE_INT_MIN),
238 offrange (),
239 sizrange (),
a411ae9b 240 maxobjsize (tree_to_shwi (max_object_size ())),
cc8bea0a
MS
241 strbounded_p ()
242{
243 /* Unfortunately, wide_int default ctor is a no-op so array members
244 of the type must be set individually. */
245 offrange[0] = offrange[1] = 0;
246 sizrange[0] = sizrange[1] = 0;
247
a411ae9b
MS
248 if (!expr)
249 return;
cc8bea0a 250
5e27f0d5
MS
251 /* Find the BASE object or pointer referenced by EXPR and set
252 the offset range OFFRANGE in the process. */
253 set_base_and_offset (expr);
254
255 if (size)
256 {
257 tree range[2];
258 /* Determine the size range, allowing for the result to be [0, 0]
259 for SIZE in the anti-range ~[0, N] where N >= PTRDIFF_MAX. */
260 get_size_range (size, range, true);
261 sizrange[0] = wi::to_offset (range[0]);
262 sizrange[1] = wi::to_offset (range[1]);
263 /* get_size_range returns SIZE_MAX for the maximum size.
264 Constrain it to the real maximum of PTRDIFF_MAX. */
287ef129 265 if (sizrange[0] <= maxobjsize && sizrange[1] > maxobjsize)
5e27f0d5
MS
266 sizrange[1] = maxobjsize;
267 }
268 else
269 sizrange[1] = maxobjsize;
270
7ad491c6
MS
271 if (!DECL_P (base))
272 return;
273
274 /* If the offset could be in the range of the referenced object
275 constrain its bounds so neither exceeds those of the object. */
276 if (offrange[0] < 0 && offrange[1] > 0)
277 offrange[0] = 0;
278
279 offset_int maxoff = maxobjsize;
5e27f0d5 280 tree basetype = TREE_TYPE (base);
1486eb79
MS
281 if (TREE_CODE (basetype) == ARRAY_TYPE)
282 {
283 if (ref && array_at_struct_end_p (ref))
284 ; /* Use the maximum possible offset for last member arrays. */
285 else if (tree basesize = TYPE_SIZE_UNIT (basetype))
286 if (TREE_CODE (basesize) == INTEGER_CST)
287 /* Size could be non-constant for a variable-length type such
288 as a struct with a VLA member (a GCC extension). */
289 maxoff = wi::to_offset (basesize);
290 }
7ad491c6
MS
291
292 if (offrange[0] >= 0)
5e27f0d5 293 {
7ad491c6
MS
294 if (offrange[1] < 0)
295 offrange[1] = offrange[0] <= maxoff ? maxoff : maxobjsize;
296 else if (offrange[0] <= maxoff && offrange[1] > maxoff)
297 offrange[1] = maxoff;
5e27f0d5
MS
298 }
299}
300
a411ae9b
MS
301/* Ctor helper to set or extend OFFRANGE based on the OFFSET argument.
302 Pointer offsets are represented as unsigned sizetype but must be
303 treated as signed. */
5e27f0d5
MS
304
305void
306builtin_memref::extend_offset_range (tree offset)
307{
5e27f0d5
MS
308 if (TREE_CODE (offset) == INTEGER_CST)
309 {
310 offset_int off = int_cst_value (offset);
311 if (off != 0)
312 {
313 offrange[0] += off;
314 offrange[1] += off;
315 }
316 return;
317 }
318
319 if (TREE_CODE (offset) == SSA_NAME)
320 {
a411ae9b
MS
321 /* A pointer offset is represented as sizetype but treated
322 as signed. */
5e27f0d5 323 wide_int min, max;
54994253 324 value_range_kind rng = get_range_info (offset, &min, &max);
a411ae9b
MS
325 if (rng == VR_ANTI_RANGE && wi::lts_p (max, min))
326 {
327 /* Convert an anti-range whose upper bound is less than
328 its lower bound to a signed range. */
329 offrange[0] += offset_int::from (max + 1, SIGNED);
330 offrange[1] += offset_int::from (min - 1, SIGNED);
331 return;
332 }
333
334 if (rng == VR_RANGE
335 && (DECL_P (base) || wi::lts_p (min, max)))
5e27f0d5 336 {
a411ae9b
MS
337 /* Preserve the bounds of the range for an offset into
338 a known object (it may be adjusted later relative to
339 a constant offset from its beginning). Otherwise use
340 the bounds only when they are ascending when treated
341 as signed. */
5e27f0d5
MS
342 offrange[0] += offset_int::from (min, SIGNED);
343 offrange[1] += offset_int::from (max, SIGNED);
a411ae9b 344 return;
5e27f0d5 345 }
a411ae9b
MS
346
347 /* Handle an anti-range the same as no range at all. */
348 gimple *stmt = SSA_NAME_DEF_STMT (offset);
349 tree type;
350 if (is_gimple_assign (stmt)
351 && (type = TREE_TYPE (gimple_assign_rhs1 (stmt)))
352 && INTEGRAL_TYPE_P (type))
5e27f0d5 353 {
a411ae9b
MS
354 tree_code code = gimple_assign_rhs_code (stmt);
355 if (code == NOP_EXPR)
5e27f0d5
MS
356 {
357 /* Use the bounds of the type of the NOP_EXPR operand
358 even if it's signed. The result doesn't trigger
359 warnings but makes their output more readable. */
360 offrange[0] += wi::to_offset (TYPE_MIN_VALUE (type));
361 offrange[1] += wi::to_offset (TYPE_MAX_VALUE (type));
a411ae9b 362 return;
5e27f0d5 363 }
5e27f0d5 364 }
5e27f0d5
MS
365 }
366
a411ae9b
MS
367 const offset_int maxoff = tree_to_shwi (max_object_size ()) >> 1;
368 const offset_int minoff = -maxoff - 1;
369
370 offrange[0] += minoff;
371 offrange[1] += maxoff;
5e27f0d5
MS
372}
373
374/* Determines the base object or pointer of the reference EXPR
375 and the offset range from the beginning of the base. */
376
377void
378builtin_memref::set_base_and_offset (tree expr)
379{
a411ae9b 380 tree offset = NULL_TREE;
5e27f0d5 381
cc8bea0a
MS
382 if (TREE_CODE (expr) == SSA_NAME)
383 {
384 /* Try to tease the offset out of the pointer. */
385 gimple *stmt = SSA_NAME_DEF_STMT (expr);
5e27f0d5
MS
386 if (!base
387 && gimple_assign_single_p (stmt)
cc8bea0a
MS
388 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
389 expr = gimple_assign_rhs1 (stmt);
390 else if (is_gimple_assign (stmt))
391 {
392 tree_code code = gimple_assign_rhs_code (stmt);
393 if (code == NOP_EXPR)
394 {
395 tree rhs = gimple_assign_rhs1 (stmt);
396 if (POINTER_TYPE_P (TREE_TYPE (rhs)))
397 expr = gimple_assign_rhs1 (stmt);
5e27f0d5
MS
398 else
399 {
400 base = expr;
401 return;
402 }
cc8bea0a
MS
403 }
404 else if (code == POINTER_PLUS_EXPR)
405 {
406 expr = gimple_assign_rhs1 (stmt);
a411ae9b 407 offset = gimple_assign_rhs2 (stmt);
cc8bea0a 408 }
5e27f0d5
MS
409 else
410 {
411 base = expr;
412 return;
413 }
414 }
415 else
416 {
a411ae9b
MS
417 /* FIXME: Handle PHI nodes in case like:
418 _12 = &MEM[(void *)&a + 2B] + _10;
419
420 <bb> [local count: 1073741824]:
421 # prephitmp_13 = PHI <_12, &MEM[(void *)&a + 2B]>
422 memcpy (prephitmp_13, p_7(D), 6); */
5e27f0d5
MS
423 base = expr;
424 return;
cc8bea0a
MS
425 }
426 }
427
428 if (TREE_CODE (expr) == ADDR_EXPR)
5e27f0d5 429 expr = TREE_OPERAND (expr, 0);
cc8bea0a 430
8286793a
MS
431 /* Stash the reference for offset validation. */
432 ref = expr;
433
5e27f0d5
MS
434 poly_int64 bitsize, bitpos;
435 tree var_off;
436 machine_mode mode;
437 int sign, reverse, vol;
cc8bea0a 438
5e27f0d5
MS
439 /* Determine the base object or pointer of the reference and
440 the constant bit offset from the beginning of the base.
441 If the offset has a non-constant component, it will be in
442 VAR_OFF. MODE, SIGN, REVERSE, and VOL are write only and
443 unused here. */
444 base = get_inner_reference (expr, &bitsize, &bitpos, &var_off,
445 &mode, &sign, &reverse, &vol);
cc8bea0a 446
8286793a
MS
447 /* get_inner_reference is not expected to return null. */
448 gcc_assert (base != NULL);
449
a411ae9b
MS
450 if (offset)
451 extend_offset_range (offset);
452
5e27f0d5 453 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
cc8bea0a 454
ce811fc4 455 /* Convert the poly_int64 offset to offset_int. The offset
8286793a
MS
456 should be constant but be prepared for it not to be just in
457 case. */
458 offset_int cstoff;
459 if (bytepos.is_constant (&cstoff))
5e27f0d5 460 {
8286793a
MS
461 offrange[0] += cstoff;
462 offrange[1] += cstoff;
cc8bea0a 463
8286793a
MS
464 /* Besides the reference saved above, also stash the offset
465 for validation. */
466 if (TREE_CODE (expr) == COMPONENT_REF)
467 refoff = cstoff;
468 }
469 else
470 offrange[1] += maxobjsize;
cc8bea0a 471
5e27f0d5 472 if (var_off)
cc8bea0a 473 {
5e27f0d5 474 if (TREE_CODE (var_off) == INTEGER_CST)
aca52e6f 475 {
8286793a 476 cstoff = wi::to_offset (var_off);
5e27f0d5
MS
477 offrange[0] += cstoff;
478 offrange[1] += cstoff;
aca52e6f
RS
479 }
480 else
5e27f0d5 481 offrange[1] += maxobjsize;
cc8bea0a
MS
482 }
483
5e27f0d5 484 if (TREE_CODE (base) == MEM_REF)
816fad6e 485 {
5e27f0d5
MS
486 tree memrefoff = TREE_OPERAND (base, 1);
487 extend_offset_range (memrefoff);
488 base = TREE_OPERAND (base, 0);
816fad6e
MS
489 }
490
5e27f0d5
MS
491 if (TREE_CODE (base) == SSA_NAME)
492 set_base_and_offset (base);
cc8bea0a
MS
493}
494
495/* Return error_mark_node if the signed offset exceeds the bounds
496 of the address space (PTRDIFF_MAX). Otherwise, return either
497 BASE or REF when the offset exceeds the bounds of the BASE or
498 REF object, and set OOBOFF to the past-the-end offset formed
499 by the reference, including its size. When STRICT is non-zero
500 use REF size, when available, otherwise use BASE size. When
501 STRICT is greater than 1, use the size of the last array member
502 as the bound, otherwise treat such a member as a flexible array
503 member. Return NULL when the offset is in bounds. */
504
505tree
506builtin_memref::offset_out_of_bounds (int strict, offset_int ooboff[2]) const
507{
a411ae9b
MS
508 if (!ptr)
509 return NULL_TREE;
cc8bea0a
MS
510
511 /* A temporary, possibly adjusted, copy of the offset range. */
512 offset_int offrng[2] = { offrange[0], offrange[1] };
513
514 if (DECL_P (base) && TREE_CODE (TREE_TYPE (base)) == ARRAY_TYPE)
515 {
56735d40
MS
516 /* Check for offset in an anti-range with a negative lower bound.
517 For such a range, consider only the non-negative subrange. */
518 if (offrng[1] < offrng[0] && offrng[1] < 0)
cc8bea0a
MS
519 offrng[1] = maxobjsize;
520 }
521
522 /* Conservative offset of the last byte of the referenced object. */
523 offset_int endoff;
524
525 /* The bounds need not be ordered. Set HIB to use as the index
526 of the larger of the bounds and LOB as the opposite. */
527 bool hib = wi::les_p (offrng[0], offrng[1]);
528 bool lob = !hib;
529
530 if (basesize < 0)
531 {
532 endoff = offrng[lob] + sizrange[0];
533
534 /* For a reference through a pointer to an object of unknown size
535 all initial offsets are considered valid, positive as well as
536 negative, since the pointer itself can point past the beginning
537 of the object. However, the sum of the lower bound of the offset
538 and that of the size must be less than or equal than PTRDIFF_MAX. */
539 if (endoff > maxobjsize)
540 return error_mark_node;
541
542 return NULL_TREE;
543 }
544
545 /* A reference to an object of known size must be within the bounds
546 of the base object. */
547 if (offrng[hib] < 0 || offrng[lob] > basesize)
548 return base;
549
550 /* The extent of the reference must also be within the bounds of
551 the base object (if known) or the maximum object size otherwise. */
552 endoff = wi::smax (offrng[lob], 0) + sizrange[0];
553 if (endoff > maxobjsize)
554 return error_mark_node;
555
556 offset_int size = basesize;
557 tree obj = base;
558
559 if (strict
560 && DECL_P (obj)
561 && ref
562 && refoff >= 0
563 && TREE_CODE (ref) == COMPONENT_REF
564 && (strict > 1
565 || !array_at_struct_end_p (ref)))
566 {
567 /* If the reference is to a member subobject, the offset must
568 be within the bounds of the subobject. */
569 tree field = TREE_OPERAND (ref, 1);
570 tree type = TREE_TYPE (field);
571 if (tree sz = TYPE_SIZE_UNIT (type))
572 if (TREE_CODE (sz) == INTEGER_CST)
573 {
574 size = refoff + wi::to_offset (sz);
575 obj = ref;
576 }
577 }
578
579 if (endoff <= size)
580 return NULL_TREE;
581
582 /* Set the out-of-bounds offset range to be one greater than
583 that delimited by the reference including its size. */
584 ooboff[lob] = size + 1;
585
586 if (endoff > ooboff[lob])
587 ooboff[hib] = endoff;
588 else
589 ooboff[hib] = wi::smax (offrng[lob], 0) + sizrange[1];
590
591 return obj;
592}
593
594/* Create an association between the memory references DST and SRC
595 for access by a call EXPR to a memory or string built-in funtion. */
596
8a45b051 597builtin_access::builtin_access (gimple *call, builtin_memref &dst,
cc8bea0a
MS
598 builtin_memref &src)
599: dstref (&dst), srcref (&src), sizrange (), ovloff (), ovlsiz (),
600 dstoff (), srcoff (), dstsiz (), srcsiz ()
601{
602 /* Zero out since the offset_int ctors invoked above are no-op. */
603 dstoff[0] = dstoff[1] = 0;
604 srcoff[0] = srcoff[1] = 0;
605 dstsiz[0] = dstsiz[1] = 0;
606 srcsiz[0] = srcsiz[1] = 0;
607
608 /* Object Size Type to use to determine the size of the destination
609 and source objects. Overridden below for raw memory functions. */
610 int ostype = 1;
611
612 /* True when the size of one reference depends on the offset of
613 itself or the other. */
614 bool depends_p = true;
615
616 /* True when the size of the destination reference DSTREF has been
617 determined from SRCREF and so needs to be adjusted by the latter's
618 offset. Only meaningful for bounded string functions like strncpy. */
619 bool dstadjust_p = false;
620
621 /* The size argument number (depends on the built-in). */
622 unsigned sizeargno = 2;
cc8bea0a
MS
623
624 tree func = gimple_call_fndecl (call);
625 switch (DECL_FUNCTION_CODE (func))
626 {
627 case BUILT_IN_MEMCPY:
628 case BUILT_IN_MEMCPY_CHK:
cc8bea0a
MS
629 case BUILT_IN_MEMPCPY:
630 case BUILT_IN_MEMPCPY_CHK:
cc8bea0a
MS
631 ostype = 0;
632 depends_p = false;
633 detect_overlap = &builtin_access::generic_overlap;
634 break;
635
636 case BUILT_IN_MEMMOVE:
637 case BUILT_IN_MEMMOVE_CHK:
cc8bea0a
MS
638 /* For memmove there is never any overlap to check for. */
639 ostype = 0;
640 depends_p = false;
641 detect_overlap = &builtin_access::no_overlap;
642 break;
643
a411ae9b
MS
644 case BUILT_IN_MEMSET:
645 case BUILT_IN_MEMSET_CHK:
646 /* For memset there is never any overlap to check for. */
647 ostype = 0;
648 depends_p = false;
649 detect_overlap = &builtin_access::no_overlap;
650 break;
651
cc8bea0a
MS
652 case BUILT_IN_STPNCPY:
653 case BUILT_IN_STPNCPY_CHK:
654 case BUILT_IN_STRNCPY:
655 case BUILT_IN_STRNCPY_CHK:
656 dstref->strbounded_p = true;
657 detect_overlap = &builtin_access::strcpy_overlap;
658 break;
659
660 case BUILT_IN_STPCPY:
661 case BUILT_IN_STPCPY_CHK:
cc8bea0a
MS
662 case BUILT_IN_STRCPY:
663 case BUILT_IN_STRCPY_CHK:
cc8bea0a
MS
664 detect_overlap = &builtin_access::strcpy_overlap;
665 break;
666
667 case BUILT_IN_STRCAT:
668 case BUILT_IN_STRCAT_CHK:
cc8bea0a
MS
669 detect_overlap = &builtin_access::strcat_overlap;
670 break;
671
672 case BUILT_IN_STRNCAT:
673 case BUILT_IN_STRNCAT_CHK:
674 dstref->strbounded_p = true;
675 srcref->strbounded_p = true;
676 detect_overlap = &builtin_access::strcat_overlap;
677 break;
678
679 default:
680 /* Handle other string functions here whose access may need
681 to be validated for in-bounds offsets and non-overlapping
31db0fe0 682 copies. */
cc8bea0a
MS
683 return;
684 }
685
a411ae9b 686 const offset_int maxobjsize = dst.maxobjsize;
cc8bea0a
MS
687
688 /* Try to determine the size of the base object. compute_objsize
689 expects a pointer so create one if BASE is a non-pointer object. */
690 tree addr;
691 if (dst.basesize < 0)
692 {
693 addr = dst.base;
694 if (!POINTER_TYPE_P (TREE_TYPE (addr)))
695 addr = build1 (ADDR_EXPR, (TREE_TYPE (addr)), addr);
696
697 if (tree dstsize = compute_objsize (addr, ostype))
698 dst.basesize = wi::to_offset (dstsize);
699 else if (POINTER_TYPE_P (TREE_TYPE (addr)))
700 dst.basesize = HOST_WIDE_INT_MIN;
701 else
702 dst.basesize = maxobjsize;
703 }
704
a411ae9b 705 if (src.base && src.basesize < 0)
cc8bea0a
MS
706 {
707 addr = src.base;
708 if (!POINTER_TYPE_P (TREE_TYPE (addr)))
709 addr = build1 (ADDR_EXPR, (TREE_TYPE (addr)), addr);
710
711 if (tree srcsize = compute_objsize (addr, ostype))
712 src.basesize = wi::to_offset (srcsize);
713 else if (POINTER_TYPE_P (TREE_TYPE (addr)))
714 src.basesize = HOST_WIDE_INT_MIN;
715 else
716 src.basesize = maxobjsize;
717 }
718
719 /* If there is no dependency between the references or the base
720 objects of the two references aren't the same there's nothing
721 else to do. */
722 if (depends_p && dstref->base != srcref->base)
723 return;
724
725 /* ...otherwise, make adjustments for references to the same object
726 by string built-in functions to reflect the constraints imposed
727 by the function. */
728
729 /* For bounded string functions determine the range of the bound
730 on the access. For others, the range stays unbounded. */
731 offset_int bounds[2] = { maxobjsize, maxobjsize };
732 if (dstref->strbounded_p)
733 {
9a0cbb60
MS
734 unsigned nargs = gimple_call_num_args (call);
735 if (nargs <= sizeargno)
736 return;
737
cc8bea0a
MS
738 tree size = gimple_call_arg (call, sizeargno);
739 tree range[2];
740 if (get_size_range (size, range, true))
741 {
742 bounds[0] = wi::to_offset (range[0]);
743 bounds[1] = wi::to_offset (range[1]);
744 }
745
746 /* If both references' size ranges are indeterminate use the last
747 (size) argument from the function call as a substitute. This
748 may only be necessary for strncpy (but not for memcpy where
749 the size range would have been already determined this way). */
750 if (dstref->sizrange[0] == 0 && dstref->sizrange[1] == maxobjsize
751 && srcref->sizrange[0] == 0 && srcref->sizrange[1] == maxobjsize)
752 {
753 dstref->sizrange[0] = bounds[0];
754 dstref->sizrange[1] = bounds[1];
755 }
756 }
757
758 /* The size range of one reference involving the same base object
759 can be determined from the size range of the other reference.
760 This makes it possible to compute accurate offsets for warnings
761 involving functions like strcpy where the length of just one of
762 the two arguments is known (determined by tree-ssa-strlen). */
763 if (dstref->sizrange[0] == 0 && dstref->sizrange[1] == maxobjsize)
764 {
765 /* When the destination size is unknown set it to the size of
766 the source. */
767 dstref->sizrange[0] = srcref->sizrange[0];
768 dstref->sizrange[1] = srcref->sizrange[1];
769 }
770 else if (srcref->sizrange[0] == 0 && srcref->sizrange[1] == maxobjsize)
771 {
772 /* When the source size is unknown set it to the size of
773 the destination. */
774 srcref->sizrange[0] = dstref->sizrange[0];
775 srcref->sizrange[1] = dstref->sizrange[1];
776
777 if (depends_p)
778 {
779 if (dstref->strbounded_p)
780 {
781 /* Read access by strncpy is bounded. */
782 if (bounds[0] < srcref->sizrange[0])
783 srcref->sizrange[0] = bounds[0];
784 if (bounds[1] < srcref->sizrange[1])
785 srcref->sizrange[1] = bounds[1];
786 }
787
788 /* For string functions, adjust the size range of the source
789 reference by the inverse boundaries of the offset (because
c7c9468a 790 the higher the offset into the string the shorter its
cc8bea0a 791 length). */
c7c9468a
JL
792 if (srcref->offrange[1] >= 0
793 && srcref->offrange[1] < srcref->sizrange[0])
cc8bea0a
MS
794 srcref->sizrange[0] -= srcref->offrange[1];
795 else
796 srcref->sizrange[0] = 0;
797
798 if (srcref->offrange[0] > 0)
799 {
800 if (srcref->offrange[0] < srcref->sizrange[1])
801 srcref->sizrange[1] -= srcref->offrange[0];
802 else
803 srcref->sizrange[1] = 0;
804 }
805
806 dstadjust_p = true;
807 }
808 }
809
810 if (detect_overlap == &builtin_access::generic_overlap)
811 {
812 if (dstref->strbounded_p)
813 {
814 dstref->sizrange[0] = bounds[0];
815 dstref->sizrange[1] = bounds[1];
816
817 if (dstref->sizrange[0] < srcref->sizrange[0])
818 srcref->sizrange[0] = dstref->sizrange[0];
819
820 if (dstref->sizrange[1] < srcref->sizrange[1])
821 srcref->sizrange[1] = dstref->sizrange[1];
822 }
823 }
824 else if (detect_overlap == &builtin_access::strcpy_overlap)
825 {
826 if (!dstref->strbounded_p)
827 {
828 /* For strcpy, adjust the destination size range to match that
829 of the source computed above. */
830 if (depends_p && dstadjust_p)
831 {
832 dstref->sizrange[0] = srcref->sizrange[0];
833 dstref->sizrange[1] = srcref->sizrange[1];
834 }
835 }
836 }
837
838 if (dstref->strbounded_p)
839 {
840 /* For strncpy, adjust the destination size range to match that
841 of the source computed above. */
842 dstref->sizrange[0] = bounds[0];
843 dstref->sizrange[1] = bounds[1];
844
845 if (bounds[0] < srcref->sizrange[0])
846 srcref->sizrange[0] = bounds[0];
847
848 if (bounds[1] < srcref->sizrange[1])
849 srcref->sizrange[1] = bounds[1];
850 }
851}
852
853offset_int
854builtin_access::overlap_size (const offset_int a[2], const offset_int b[2],
855 offset_int *off)
856{
857 const offset_int *p = a;
858 const offset_int *q = b;
859
860 /* Point P at the bigger of the two ranges and Q at the smaller. */
861 if (wi::lts_p (a[1] - a[0], b[1] - b[0]))
862 {
863 p = b;
864 q = a;
865 }
866
867 if (p[0] < q[0])
868 {
869 if (p[1] < q[0])
870 return 0;
871
872 *off = q[0];
873 return wi::smin (p[1], q[1]) - q[0];
874 }
875
876 if (q[1] < p[0])
877 return 0;
878
879 off[0] = p[0];
880 return q[1] - p[0];
881}
882
883/* Return true if the bounded mempry (memcpy amd similar) or string function
884 access (strncpy and similar) ACS overlaps. */
885
886bool
887builtin_access::generic_overlap ()
888{
889 builtin_access &acs = *this;
890 const builtin_memref *dstref = acs.dstref;
891 const builtin_memref *srcref = acs.srcref;
892
893 gcc_assert (dstref->base == srcref->base);
894
a411ae9b 895 const offset_int maxobjsize = acs.dstref->maxobjsize;
cc8bea0a
MS
896
897 offset_int maxsize = dstref->basesize < 0 ? maxobjsize : dstref->basesize;
898 gcc_assert (maxsize <= maxobjsize);
899
900 /* Adjust the larger bounds of the offsets (which may be the first
901 element if the lower bound is larger than the upper bound) to
902 make them valid for the smallest access (if possible) but no smaller
903 than the smaller bounds. */
904 gcc_assert (wi::les_p (acs.dstoff[0], acs.dstoff[1]));
905
906 if (maxsize < acs.dstoff[1] + acs.dstsiz[0])
907 acs.dstoff[1] = maxsize - acs.dstsiz[0];
908 if (acs.dstoff[1] < acs.dstoff[0])
909 acs.dstoff[1] = acs.dstoff[0];
910
911 gcc_assert (wi::les_p (acs.srcoff[0], acs.srcoff[1]));
912
913 if (maxsize < acs.srcoff[1] + acs.srcsiz[0])
914 acs.srcoff[1] = maxsize - acs.srcsiz[0];
915 if (acs.srcoff[1] < acs.srcoff[0])
916 acs.srcoff[1] = acs.srcoff[0];
917
918 /* Determine the minimum and maximum space for the access given
919 the offsets. */
920 offset_int space[2];
921 space[0] = wi::abs (acs.dstoff[0] - acs.srcoff[0]);
922 space[1] = space[0];
923
924 offset_int d = wi::abs (acs.dstoff[0] - acs.srcoff[1]);
925 if (acs.srcsiz[0] > 0)
926 {
927 if (d < space[0])
928 space[0] = d;
929
930 if (space[1] < d)
931 space[1] = d;
932 }
933 else
934 space[1] = acs.dstsiz[1];
935
936 d = wi::abs (acs.dstoff[1] - acs.srcoff[0]);
937 if (d < space[0])
938 space[0] = d;
939
940 if (space[1] < d)
941 space[1] = d;
942
943 /* Treat raw memory functions both of whose references are bounded
944 as special and permit uncertain overlaps to go undetected. For
945 all kinds of constant offset and constant size accesses, if
946 overlap isn't certain it is not possible. */
947 bool overlap_possible = space[0] < acs.dstsiz[1];
948 if (!overlap_possible)
949 return false;
950
951 bool overlap_certain = space[1] < acs.dstsiz[0];
952
953 /* True when the size of one reference depends on the offset of
954 the other. */
955 bool depends_p = detect_overlap != &builtin_access::generic_overlap;
956
5e27f0d5
MS
957 if (!overlap_certain)
958 {
959 if (!dstref->strbounded_p && !depends_p)
8286793a 960 /* Memcpy only considers certain overlap. */
5e27f0d5
MS
961 return false;
962
963 /* There's no way to distinguish an access to the same member
964 of a structure from one to two distinct members of the same
965 structure. Give up to avoid excessive false positives. */
8286793a
MS
966 tree basetype = TREE_TYPE (dstref->base);
967
968 if (POINTER_TYPE_P (basetype))
969 basetype = TREE_TYPE (basetype);
970 else
971 while (TREE_CODE (basetype) == ARRAY_TYPE)
972 basetype = TREE_TYPE (basetype);
973
5e27f0d5
MS
974 if (RECORD_OR_UNION_TYPE_P (basetype))
975 return false;
976 }
cc8bea0a
MS
977
978 /* True for stpcpy and strcpy. */
979 bool stxcpy_p = (!dstref->strbounded_p
980 && detect_overlap == &builtin_access::strcpy_overlap);
981
982 if (dstref->refoff >= 0
983 && srcref->refoff >= 0
984 && dstref->refoff != srcref->refoff
985 && (stxcpy_p || dstref->strbounded_p || srcref->strbounded_p))
986 return false;
987
988 offset_int siz[2] = { maxobjsize + 1, 0 };
989
990 ovloff[0] = HOST_WIDE_INT_MAX;
991 ovloff[1] = HOST_WIDE_INT_MIN;
992
993 /* Adjustment to the lower bound of the offset of the overlap to
994 account for a subset of unbounded string calls where the size
995 of the destination string depends on the length of the source
996 which in turn depends on the offset into it. */
997 bool sub1;
998
999 if (stxcpy_p)
1000 {
1001 sub1 = acs.dstoff[0] <= acs.srcoff[0];
1002
1003 /* Iterate over the extreme locations (on the horizontal axis formed
1004 by their offsets) and sizes of two regions and find their smallest
1005 and largest overlap and the corresponding offsets. */
1006 for (unsigned i = 0; i != 2; ++i)
1007 {
1008 const offset_int a[2] = {
1009 acs.dstoff[i], acs.dstoff[i] + acs.dstsiz[!i]
1010 };
1011
1012 const offset_int b[2] = {
1013 acs.srcoff[i], acs.srcoff[i] + acs.srcsiz[!i]
1014 };
1015
1016 offset_int off;
1017 offset_int sz = overlap_size (a, b, &off);
1018 if (sz < siz[0])
1019 siz[0] = sz;
1020
1021 if (siz[1] <= sz)
1022 siz[1] = sz;
1023
1024 if (sz != 0)
1025 {
1026 if (wi::lts_p (off, ovloff[0]))
1027 ovloff[0] = off.to_shwi ();
1028 if (wi::lts_p (ovloff[1], off))
1029 ovloff[1] = off.to_shwi ();
1030 }
1031 }
1032 }
1033 else
1034 {
1035 sub1 = !depends_p;
1036
1037 /* Iterate over the extreme locations (on the horizontal axis
1038 formed by their offsets) and sizes of two regions and find
1039 their smallest and largest overlap and the corresponding
1040 offsets. */
1041
1042 for (unsigned io = 0; io != 2; ++io)
1043 for (unsigned is = 0; is != 2; ++is)
1044 {
1045 const offset_int a[2] = {
1046 acs.dstoff[io], acs.dstoff[io] + acs.dstsiz[is]
1047 };
1048
1049 for (unsigned jo = 0; jo != 2; ++jo)
1050 for (unsigned js = 0; js != 2; ++js)
1051 {
1052 if (depends_p)
1053 {
1054 /* For st{p,r}ncpy the size of the source sequence
1055 depends on the offset into it. */
1056 if (js)
1057 break;
1058 js = !jo;
1059 }
1060
1061 const offset_int b[2] = {
1062 acs.srcoff[jo], acs.srcoff[jo] + acs.srcsiz[js]
1063 };
1064
1065 offset_int off;
1066 offset_int sz = overlap_size (a, b, &off);
1067 if (sz < siz[0])
1068 siz[0] = sz;
1069
1070 if (siz[1] <= sz)
1071 siz[1] = sz;
1072
1073 if (sz != 0)
1074 {
1075 if (wi::lts_p (off, ovloff[0]))
1076 ovloff[0] = off.to_shwi ();
1077 if (wi::lts_p (ovloff[1], off))
1078 ovloff[1] = off.to_shwi ();
1079 }
1080 }
1081 }
1082 }
1083
1084 ovlsiz[0] = siz[0].to_shwi ();
1085 ovlsiz[1] = siz[1].to_shwi ();
1086
1087 if (ovlsiz[0] == 0 && ovlsiz[1] > 1)
1088 ovloff[0] = ovloff[1] + ovlsiz[1] - 1 - sub1;
1089
1090 return true;
1091}
1092
1093/* Return true if the strcat-like access overlaps. */
1094
1095bool
1096builtin_access::strcat_overlap ()
1097{
1098 builtin_access &acs = *this;
1099 const builtin_memref *dstref = acs.dstref;
1100 const builtin_memref *srcref = acs.srcref;
1101
1102 gcc_assert (dstref->base == srcref->base);
1103
a411ae9b 1104 const offset_int maxobjsize = acs.dstref->maxobjsize;
cc8bea0a
MS
1105
1106 gcc_assert (dstref->base && dstref->base == srcref->base);
1107
1108 /* Adjust for strcat-like accesses. */
1109
1110 /* As a special case for strcat, set the DSTREF offsets to the length
1111 of the source string since the function starts writing at the first
1112 nul, and set the size to 1 for the length of the nul. */
1113 acs.dstoff[0] += acs.dstsiz[0];
1114 acs.dstoff[1] += acs.dstsiz[1];
1115
1116 bool strfunc_unknown_args = acs.dstsiz[0] == 0 && acs.dstsiz[1] != 0;
1117
1118 /* The lower bound is zero when the size is unknown because then
1119 overlap is not certain. */
1120 acs.dstsiz[0] = strfunc_unknown_args ? 0 : 1;
1121 acs.dstsiz[1] = 1;
1122
1123 offset_int maxsize = dstref->basesize < 0 ? maxobjsize : dstref->basesize;
1124 gcc_assert (maxsize <= maxobjsize);
1125
1126 /* For references to the same base object, determine if there's a pair
1127 of valid offsets into the two references such that access between
1128 them doesn't overlap. Adjust both upper bounds to be valid for
1129 the smaller size (i.e., at most MAXSIZE - SIZE). */
1130
1131 if (maxsize < acs.dstoff[1] + acs.dstsiz[0])
1132 acs.dstoff[1] = maxsize - acs.dstsiz[0];
1133
1134 if (maxsize < acs.srcoff[1] + acs.srcsiz[0])
1135 acs.srcoff[1] = maxsize - acs.srcsiz[0];
1136
1137 /* Check to see if there's enough space for both accesses without
1138 overlap. Determine the optimistic (maximum) amount of available
1139 space. */
1140 offset_int space;
1141 if (acs.dstoff[0] <= acs.srcoff[0])
1142 {
1143 if (acs.dstoff[1] < acs.srcoff[1])
1144 space = acs.srcoff[1] + acs.srcsiz[0] - acs.dstoff[0];
1145 else
1146 space = acs.dstoff[1] + acs.dstsiz[0] - acs.srcoff[0];
1147 }
1148 else
1149 space = acs.dstoff[1] + acs.dstsiz[0] - acs.srcoff[0];
1150
1151 /* Overlap is certain if the distance between the farthest offsets
1152 of the opposite accesses is less than the sum of the lower bounds
1153 of the sizes of the two accesses. */
1154 bool overlap_certain = space < acs.dstsiz[0] + acs.srcsiz[0];
1155
1156 /* For a constant-offset, constant size access, consider the largest
1157 distance between the offset bounds and the lower bound of the access
1158 size. If the overlap isn't certain return success. */
1159 if (!overlap_certain
1160 && acs.dstoff[0] == acs.dstoff[1]
1161 && acs.srcoff[0] == acs.srcoff[1]
1162 && acs.dstsiz[0] == acs.dstsiz[1]
1163 && acs.srcsiz[0] == acs.srcsiz[1])
1164 return false;
1165
1166 /* Overlap is not certain but may be possible. */
1167
1168 offset_int access_min = acs.dstsiz[0] + acs.srcsiz[0];
1169
1170 /* Determine the conservative (minimum) amount of space. */
1171 space = wi::abs (acs.dstoff[0] - acs.srcoff[0]);
1172 offset_int d = wi::abs (acs.dstoff[0] - acs.srcoff[1]);
1173 if (d < space)
1174 space = d;
1175 d = wi::abs (acs.dstoff[1] - acs.srcoff[0]);
1176 if (d < space)
1177 space = d;
1178
1179 /* For a strict test (used for strcpy and similar with unknown or
1180 variable bounds or sizes), consider the smallest distance between
1181 the offset bounds and either the upper bound of the access size
1182 if known, or the lower bound otherwise. */
1183 if (access_min <= space && (access_min != 0 || !strfunc_unknown_args))
1184 return false;
1185
1186 /* When strcat overlap is certain it is always a single byte:
816fad6e 1187 the terminating NUL, regardless of offsets and sizes. When
cc8bea0a
MS
1188 overlap is only possible its range is [0, 1]. */
1189 acs.ovlsiz[0] = dstref->sizrange[0] == dstref->sizrange[1] ? 1 : 0;
1190 acs.ovlsiz[1] = 1;
cc8bea0a 1191
816fad6e
MS
1192 offset_int endoff = dstref->offrange[0] + dstref->sizrange[0];
1193 if (endoff <= srcref->offrange[0])
1194 acs.ovloff[0] = wi::smin (maxobjsize, srcref->offrange[0]).to_shwi ();
1195 else
1196 acs.ovloff[0] = wi::smin (maxobjsize, endoff).to_shwi ();
1197
1198 acs.sizrange[0] = wi::smax (wi::abs (endoff - srcref->offrange[0]) + 1,
1199 srcref->sizrange[0]).to_shwi ();
1200 if (dstref->offrange[0] == dstref->offrange[1])
1201 {
1202 if (srcref->offrange[0] == srcref->offrange[1])
1203 acs.ovloff[1] = acs.ovloff[0];
1204 else
1205 acs.ovloff[1]
1206 = wi::smin (maxobjsize,
1207 srcref->offrange[1] + srcref->sizrange[1]).to_shwi ();
1208 }
1209 else
1210 acs.ovloff[1]
1211 = wi::smin (maxobjsize,
1212 dstref->offrange[1] + dstref->sizrange[1]).to_shwi ();
1213
1214 if (acs.sizrange[0] == 0)
1215 acs.sizrange[0] = 1;
cc8bea0a
MS
1216 acs.sizrange[1] = wi::smax (acs.dstsiz[1], srcref->sizrange[1]).to_shwi ();
1217 return true;
1218}
1219
1220/* Return true if the strcpy-like access overlaps. */
1221
1222bool
1223builtin_access::strcpy_overlap ()
1224{
1225 return generic_overlap ();
1226}
1227
1228
1229/* Return true if DSTREF and SRCREF describe accesses that either overlap
1230 one another or that, in order not to overlap, would imply that the size
1231 of the referenced object(s) exceeds the maximum size of an object. Set
1232 Otherwise, if DSTREF and SRCREF do not definitely overlap (even though
1233 they may overlap in a way that's not apparent from the available data),
1234 return false. */
1235
1236bool
1237builtin_access::overlap ()
1238{
1239 builtin_access &acs = *this;
1240
a411ae9b 1241 const offset_int maxobjsize = dstref->maxobjsize;
cc8bea0a
MS
1242
1243 acs.sizrange[0] = wi::smax (dstref->sizrange[0],
1244 srcref->sizrange[0]).to_shwi ();
1245 acs.sizrange[1] = wi::smax (dstref->sizrange[1],
1246 srcref->sizrange[1]).to_shwi ();
1247
1248 /* Check to see if the two references refer to regions that are
1249 too large not to overlap in the address space (whose maximum
1250 size is PTRDIFF_MAX). */
1251 offset_int size = dstref->sizrange[0] + srcref->sizrange[0];
1252 if (maxobjsize < size)
1253 {
1254 acs.ovloff[0] = (maxobjsize - dstref->sizrange[0]).to_shwi ();
1255 acs.ovlsiz[0] = (size - maxobjsize).to_shwi ();
1256 return true;
1257 }
1258
1259 /* If both base objects aren't known return the maximum possible
1260 offset that would make them not overlap. */
1261 if (!dstref->base || !srcref->base)
1262 return false;
1263
c7c9468a
JL
1264 /* Set the access offsets. */
1265 acs.dstoff[0] = dstref->offrange[0];
1266 acs.dstoff[1] = dstref->offrange[1];
1267
1268 /* If the base object is an array adjust the bounds of the offset
1269 to be non-negative and within the bounds of the array if possible. */
cc8bea0a
MS
1270 if (dstref->base
1271 && TREE_CODE (TREE_TYPE (dstref->base)) == ARRAY_TYPE)
c7c9468a
JL
1272 {
1273 if (acs.dstoff[0] < 0 && acs.dstoff[1] >= 0)
1274 acs.dstoff[0] = 0;
cc8bea0a 1275
c7c9468a
JL
1276 if (acs.dstoff[1] < acs.dstoff[0])
1277 {
1278 if (tree size = TYPE_SIZE_UNIT (TREE_TYPE (dstref->base)))
1279 acs.dstoff[1] = wi::umin (acs.dstoff[1], wi::to_offset (size));
1280 else
1281 acs.dstoff[1] = wi::umin (acs.dstoff[1], maxobjsize);
1282 }
1283 }
1284
1285 acs.srcoff[0] = srcref->offrange[0];
1286 acs.srcoff[1] = srcref->offrange[1];
cc8bea0a
MS
1287
1288 if (srcref->base
1289 && TREE_CODE (TREE_TYPE (srcref->base)) == ARRAY_TYPE)
c7c9468a
JL
1290 {
1291 if (acs.srcoff[0] < 0 && acs.srcoff[1] >= 0)
1292 acs.srcoff[0] = 0;
cc8bea0a 1293
c7c9468a
JL
1294 if (tree size = TYPE_SIZE_UNIT (TREE_TYPE (srcref->base)))
1295 acs.srcoff[1] = wi::umin (acs.srcoff[1], wi::to_offset (size));
1296 else if (acs.srcoff[1] < acs.srcoff[0])
1297 acs.srcoff[1] = wi::umin (acs.srcoff[1], maxobjsize);
1298 }
cc8bea0a 1299
c7c9468a
JL
1300 /* When the upper bound of the offset is less than the lower bound
1301 the former is the result of a negative offset being represented
1302 as a large positive value or vice versa. The resulting range is
1303 a union of two subranges: [MIN, UB] and [LB, MAX]. Since such
1304 a union is not representable using the current data structure
1305 replace it with the full range of offsets. */
cc8bea0a 1306 if (acs.dstoff[1] < acs.dstoff[0])
c7c9468a
JL
1307 {
1308 acs.dstoff[0] = -maxobjsize - 1;
1309 acs.dstoff[1] = maxobjsize;
1310 }
cc8bea0a
MS
1311
1312 /* Validate the offset and size of each reference on its own first.
1313 This is independent of whether or not the base objects are the
1314 same. Normally, this would have already been detected and
1315 diagnosed by -Warray-bounds, unless it has been disabled. */
1316 offset_int maxoff = acs.dstoff[0] + dstref->sizrange[0];
1317 if (maxobjsize < maxoff)
1318 {
1319 acs.ovlsiz[0] = (maxoff - maxobjsize).to_shwi ();
1320 acs.ovloff[0] = acs.dstoff[0].to_shwi () - acs.ovlsiz[0];
1321 return true;
1322 }
1323
1324 /* Repeat the same as above but for the source offsets. */
1325 if (acs.srcoff[1] < acs.srcoff[0])
c7c9468a
JL
1326 {
1327 acs.srcoff[0] = -maxobjsize - 1;
1328 acs.srcoff[1] = maxobjsize;
1329 }
cc8bea0a
MS
1330
1331 maxoff = acs.srcoff[0] + srcref->sizrange[0];
1332 if (maxobjsize < maxoff)
1333 {
1334 acs.ovlsiz[0] = (maxoff - maxobjsize).to_shwi ();
1335 acs.ovlsiz[1] = (acs.srcoff[0] + srcref->sizrange[1]
1336 - maxobjsize).to_shwi ();
1337 acs.ovloff[0] = acs.srcoff[0].to_shwi () - acs.ovlsiz[0];
1338 return true;
1339 }
1340
1341 if (dstref->base != srcref->base)
1342 return false;
1343
1344 acs.dstsiz[0] = dstref->sizrange[0];
1345 acs.dstsiz[1] = dstref->sizrange[1];
1346
1347 acs.srcsiz[0] = srcref->sizrange[0];
1348 acs.srcsiz[1] = srcref->sizrange[1];
1349
1350 /* Call the appropriate function to determine the overlap. */
1351 if ((this->*detect_overlap) ())
1352 {
816fad6e
MS
1353 if (!sizrange[1])
1354 {
1355 /* Unless the access size range has already been set, do so here. */
1356 sizrange[0] = wi::smax (acs.dstsiz[0], srcref->sizrange[0]).to_shwi ();
1357 sizrange[1] = wi::smax (acs.dstsiz[1], srcref->sizrange[1]).to_shwi ();
1358 }
cc8bea0a
MS
1359 return true;
1360 }
1361
1362 return false;
1363}
1364
1365/* Attempt to detect and diagnose an overlapping copy in a call expression
1366 EXPR involving an an access ACS to a built-in memory or string function.
1367 Return true when one has been detected, false otherwise. */
1368
1369static bool
8a45b051 1370maybe_diag_overlap (location_t loc, gimple *call, builtin_access &acs)
cc8bea0a
MS
1371{
1372 if (!acs.overlap ())
1373 return false;
1374
213694e5
MS
1375 if (gimple_no_warning_p (call))
1376 return true;
1377
cc8bea0a
MS
1378 /* For convenience. */
1379 const builtin_memref &dstref = *acs.dstref;
1380 const builtin_memref &srcref = *acs.srcref;
1381
1382 /* Determine the range of offsets and sizes of the overlap if it
1383 exists and issue diagnostics. */
1384 HOST_WIDE_INT *ovloff = acs.ovloff;
1385 HOST_WIDE_INT *ovlsiz = acs.ovlsiz;
1386 HOST_WIDE_INT *sizrange = acs.sizrange;
1387
1388 tree func = gimple_call_fndecl (call);
1389
1390 /* To avoid a combinatorial explosion of diagnostics format the offsets
1391 or their ranges as strings and use them in the warning calls below. */
1392 char offstr[3][64];
1393
1394 if (dstref.offrange[0] == dstref.offrange[1]
1395 || dstref.offrange[1] > HOST_WIDE_INT_MAX)
a6fd2cd7
MS
1396 sprintf (offstr[0], HOST_WIDE_INT_PRINT_DEC,
1397 dstref.offrange[0].to_shwi ());
cc8bea0a 1398 else
a6fd2cd7
MS
1399 sprintf (offstr[0],
1400 "[" HOST_WIDE_INT_PRINT_DEC ", " HOST_WIDE_INT_PRINT_DEC "]",
1401 dstref.offrange[0].to_shwi (),
1402 dstref.offrange[1].to_shwi ());
cc8bea0a
MS
1403
1404 if (srcref.offrange[0] == srcref.offrange[1]
1405 || srcref.offrange[1] > HOST_WIDE_INT_MAX)
a6fd2cd7
MS
1406 sprintf (offstr[1],
1407 HOST_WIDE_INT_PRINT_DEC,
1408 srcref.offrange[0].to_shwi ());
cc8bea0a 1409 else
a6fd2cd7
MS
1410 sprintf (offstr[1],
1411 "[" HOST_WIDE_INT_PRINT_DEC ", " HOST_WIDE_INT_PRINT_DEC "]",
1412 srcref.offrange[0].to_shwi (),
1413 srcref.offrange[1].to_shwi ());
cc8bea0a
MS
1414
1415 if (ovloff[0] == ovloff[1] || !ovloff[1])
a6fd2cd7 1416 sprintf (offstr[2], HOST_WIDE_INT_PRINT_DEC, ovloff[0]);
cc8bea0a 1417 else
a6fd2cd7
MS
1418 sprintf (offstr[2],
1419 "[" HOST_WIDE_INT_PRINT_DEC ", " HOST_WIDE_INT_PRINT_DEC "]",
1420 ovloff[0], ovloff[1]);
cc8bea0a 1421
a411ae9b 1422 const offset_int maxobjsize = dstref.maxobjsize;
cc8bea0a
MS
1423 bool must_overlap = ovlsiz[0] > 0;
1424
1425 if (ovlsiz[1] == 0)
1426 ovlsiz[1] = ovlsiz[0];
1427
1428 if (must_overlap)
1429 {
1430 /* Issue definitive "overlaps" diagnostic in this block. */
1431
1432 if (sizrange[0] == sizrange[1])
1433 {
1434 if (ovlsiz[0] == ovlsiz[1])
1435 warning_at (loc, OPT_Wrestrict,
1436 sizrange[0] == 1
1437 ? (ovlsiz[0] == 1
1438 ? G_("%G%qD accessing %wu byte at offsets %s "
1439 "and %s overlaps %wu byte at offset %s")
1440 : G_("%G%qD accessing %wu byte at offsets %s "
1441 "and %s overlaps %wu bytes at offset "
1442 "%s"))
1443 : (ovlsiz[0] == 1
1444 ? G_("%G%qD accessing %wu bytes at offsets %s "
1445 "and %s overlaps %wu byte at offset %s")
1446 : G_("%G%qD accessing %wu bytes at offsets %s "
1447 "and %s overlaps %wu bytes at offset "
1448 "%s")),
1449 call, func, sizrange[0],
1450 offstr[0], offstr[1], ovlsiz[0], offstr[2]);
1451 else if (ovlsiz[1] >= 0 && ovlsiz[1] < maxobjsize.to_shwi ())
15d3974b
JJ
1452 warning_n (loc, OPT_Wrestrict, sizrange[0],
1453 "%G%qD accessing %wu byte at offsets %s "
1454 "and %s overlaps between %wu and %wu bytes "
1455 "at offset %s",
1456 "%G%qD accessing %wu bytes at offsets %s "
1457 "and %s overlaps between %wu and %wu bytes "
1458 "at offset %s",
1459 call, func, sizrange[0], offstr[0], offstr[1],
1460 ovlsiz[0], ovlsiz[1], offstr[2]);
cc8bea0a 1461 else
15d3974b
JJ
1462 warning_n (loc, OPT_Wrestrict, sizrange[0],
1463 "%G%qD accessing %wu byte at offsets %s and "
1464 "%s overlaps %wu or more bytes at offset %s",
1465 "%G%qD accessing %wu bytes at offsets %s and "
1466 "%s overlaps %wu or more bytes at offset %s",
1467 call, func, sizrange[0],
1468 offstr[0], offstr[1], ovlsiz[0], offstr[2]);
cc8bea0a
MS
1469 return true;
1470 }
1471
1472 if (sizrange[1] >= 0 && sizrange[1] < maxobjsize.to_shwi ())
1473 {
1474 if (ovlsiz[0] == ovlsiz[1])
15d3974b
JJ
1475 warning_n (loc, OPT_Wrestrict, ovlsiz[0],
1476 "%G%qD accessing between %wu and %wu bytes "
1477 "at offsets %s and %s overlaps %wu byte at "
1478 "offset %s",
1479 "%G%qD accessing between %wu and %wu bytes "
1480 "at offsets %s and %s overlaps %wu bytes "
1481 "at offset %s",
1482 call, func, sizrange[0], sizrange[1],
1483 offstr[0], offstr[1], ovlsiz[0], offstr[2]);
cc8bea0a
MS
1484 else if (ovlsiz[1] >= 0 && ovlsiz[1] < maxobjsize.to_shwi ())
1485 warning_at (loc, OPT_Wrestrict,
1486 "%G%qD accessing between %wu and %wu bytes at "
1487 "offsets %s and %s overlaps between %wu and %wu "
1488 "bytes at offset %s",
1489 call, func, sizrange[0], sizrange[1],
1490 offstr[0], offstr[1], ovlsiz[0], ovlsiz[1],
1491 offstr[2]);
1492 else
1493 warning_at (loc, OPT_Wrestrict,
1494 "%G%qD accessing between %wu and %wu bytes at "
1495 "offsets %s and %s overlaps %wu or more bytes "
1496 "at offset %s",
1497 call, func, sizrange[0], sizrange[1],
1498 offstr[0], offstr[1], ovlsiz[0], offstr[2]);
1499 return true;
1500 }
1501
1502 if (ovlsiz[0] != ovlsiz[1])
1503 ovlsiz[1] = maxobjsize.to_shwi ();
1504
1505 if (ovlsiz[0] == ovlsiz[1])
15d3974b
JJ
1506 warning_n (loc, OPT_Wrestrict, ovlsiz[0],
1507 "%G%qD accessing %wu or more bytes at offsets "
1508 "%s and %s overlaps %wu byte at offset %s",
1509 "%G%qD accessing %wu or more bytes at offsets "
1510 "%s and %s overlaps %wu bytes at offset %s",
1511 call, func, sizrange[0], offstr[0], offstr[1],
1512 ovlsiz[0], offstr[2]);
cc8bea0a
MS
1513 else if (ovlsiz[1] >= 0 && ovlsiz[1] < maxobjsize.to_shwi ())
1514 warning_at (loc, OPT_Wrestrict,
1515 "%G%qD accessing %wu or more bytes at offsets %s "
1516 "and %s overlaps between %wu and %wu bytes "
1517 "at offset %s",
1518 call, func, sizrange[0], offstr[0], offstr[1],
1519 ovlsiz[0], ovlsiz[1], offstr[2]);
1520 else
1521 warning_at (loc, OPT_Wrestrict,
1522 "%G%qD accessing %wu or more bytes at offsets %s "
1523 "and %s overlaps %wu or more bytes at offset %s",
1524 call, func, sizrange[0], offstr[0], offstr[1],
1525 ovlsiz[0], offstr[2]);
1526 return true;
1527 }
1528
cc8bea0a
MS
1529 /* Use more concise wording when one of the offsets is unbounded
1530 to avoid confusing the user with large and mostly meaningless
1531 numbers. */
816fad6e
MS
1532 bool open_range;
1533 if (DECL_P (dstref.base) && TREE_CODE (TREE_TYPE (dstref.base)) == ARRAY_TYPE)
1534 open_range = ((dstref.offrange[0] == 0
1535 && dstref.offrange[1] == maxobjsize)
1536 || (srcref.offrange[0] == 0
1537 && srcref.offrange[1] == maxobjsize));
1538 else
1539 open_range = ((dstref.offrange[0] == -maxobjsize - 1
1540 && dstref.offrange[1] == maxobjsize)
1541 || (srcref.offrange[0] == -maxobjsize - 1
1542 && srcref.offrange[1] == maxobjsize));
cc8bea0a
MS
1543
1544 if (sizrange[0] == sizrange[1] || sizrange[1] == 1)
1545 {
1546 if (ovlsiz[1] == 1)
1547 {
1548 if (open_range)
15d3974b
JJ
1549 warning_n (loc, OPT_Wrestrict, sizrange[1],
1550 "%G%qD accessing %wu byte may overlap "
1551 "%wu byte",
1552 "%G%qD accessing %wu bytes may overlap "
1553 "%wu byte",
1554 call, func, sizrange[1], ovlsiz[1]);
cc8bea0a 1555 else
15d3974b
JJ
1556 warning_n (loc, OPT_Wrestrict, sizrange[1],
1557 "%G%qD accessing %wu byte at offsets %s "
1558 "and %s may overlap %wu byte at offset %s",
1559 "%G%qD accessing %wu bytes at offsets %s "
1560 "and %s may overlap %wu byte at offset %s",
1561 call, func, sizrange[1], offstr[0], offstr[1],
1562 ovlsiz[1], offstr[2]);
cc8bea0a
MS
1563 return true;
1564 }
1565
1566 if (open_range)
15d3974b
JJ
1567 warning_n (loc, OPT_Wrestrict, sizrange[1],
1568 "%G%qD accessing %wu byte may overlap "
1569 "up to %wu bytes",
1570 "%G%qD accessing %wu bytes may overlap "
1571 "up to %wu bytes",
1572 call, func, sizrange[1], ovlsiz[1]);
cc8bea0a 1573 else
15d3974b
JJ
1574 warning_n (loc, OPT_Wrestrict, sizrange[1],
1575 "%G%qD accessing %wu byte at offsets %s and "
1576 "%s may overlap up to %wu bytes at offset %s",
1577 "%G%qD accessing %wu bytes at offsets %s and "
1578 "%s may overlap up to %wu bytes at offset %s",
1579 call, func, sizrange[1], offstr[0], offstr[1],
1580 ovlsiz[1], offstr[2]);
cc8bea0a
MS
1581 return true;
1582 }
1583
1584 if (sizrange[1] >= 0 && sizrange[1] < maxobjsize.to_shwi ())
1585 {
1586 if (open_range)
15d3974b
JJ
1587 warning_n (loc, OPT_Wrestrict, ovlsiz[1],
1588 "%G%qD accessing between %wu and %wu bytes "
1589 "may overlap %wu byte",
1590 "%G%qD accessing between %wu and %wu bytes "
1591 "may overlap up to %wu bytes",
1592 call, func, sizrange[0], sizrange[1], ovlsiz[1]);
cc8bea0a 1593 else
15d3974b
JJ
1594 warning_n (loc, OPT_Wrestrict, ovlsiz[1],
1595 "%G%qD accessing between %wu and %wu bytes "
1596 "at offsets %s and %s may overlap %wu byte "
1597 "at offset %s",
1598 "%G%qD accessing between %wu and %wu bytes "
1599 "at offsets %s and %s may overlap up to %wu "
1600 "bytes at offset %s",
1601 call, func, sizrange[0], sizrange[1],
1602 offstr[0], offstr[1], ovlsiz[1], offstr[2]);
cc8bea0a
MS
1603 return true;
1604 }
1605
15d3974b
JJ
1606 warning_n (loc, OPT_Wrestrict, ovlsiz[1],
1607 "%G%qD accessing %wu or more bytes at offsets %s "
1608 "and %s may overlap %wu byte at offset %s",
1609 "%G%qD accessing %wu or more bytes at offsets %s "
1610 "and %s may overlap up to %wu bytes at offset %s",
1611 call, func, sizrange[0], offstr[0], offstr[1],
1612 ovlsiz[1], offstr[2]);
cc8bea0a
MS
1613
1614 return true;
1615}
1616
287ef129
MS
1617/* Validate REF size and offsets in an expression passed as an argument
1618 to a CALL to a built-in function FUNC to make sure they are within
1619 the bounds of the referenced object if its size is known, or
1620 PTRDIFF_MAX otherwise. DO_WARN is true when a diagnostic should
1621 be issued, false otherwise.
1622 Both initial values of the offsets and their final value computed
1623 by the function by incrementing the initial value by the size are
cc8bea0a 1624 validated. Return true if the offsets are not valid and a diagnostic
287ef129 1625 has been issued, or would have been issued if DO_WARN had been true. */
cc8bea0a
MS
1626
1627static bool
287ef129 1628maybe_diag_access_bounds (location_t loc, gimple *call, tree func, int strict,
213694e5 1629 const builtin_memref &ref, bool do_warn)
cc8bea0a 1630{
a411ae9b 1631 const offset_int maxobjsize = ref.maxobjsize;
287ef129
MS
1632
1633 /* Check for excessive size first and regardless of warning options
1634 since the result is used to make codegen decisions. */
1635 if (ref.sizrange[0] > maxobjsize)
1636 {
1637 /* Return true without issuing a warning. */
1638 if (!do_warn)
1639 return true;
1640
1641 if (ref.ref && TREE_NO_WARNING (ref.ref))
1642 return false;
1643
1644 if (warn_stringop_overflow)
1645 {
1646 if (EXPR_HAS_LOCATION (ref.ptr))
1647 loc = EXPR_LOCATION (ref.ptr);
1648
1649 loc = expansion_point_location_if_in_system_header (loc);
1650
1651 if (ref.sizrange[0] == ref.sizrange[1])
1652 return warning_at (loc, OPT_Wstringop_overflow_,
1653 "%G%qD specified bound %wu "
1654 "exceeds maximum object size %wu",
1655 call, func, ref.sizrange[0].to_uhwi (),
1656 maxobjsize.to_uhwi ());
1657
1658 return warning_at (loc, OPT_Wstringop_overflow_,
1659 "%G%qD specified bound between %wu and %wu "
1660 "exceeds maximum object size %wu",
1661 call, func, ref.sizrange[0].to_uhwi (),
1662 ref.sizrange[1].to_uhwi (),
1663 maxobjsize.to_uhwi ());
1664 }
1665 }
1666
213694e5
MS
1667 /* Check for out-bounds pointers regardless of warning options since
1668 the result is used to make codegen decisions. */
1669 offset_int ooboff[] = { ref.offrange[0], ref.offrange[1] };
1670 tree oobref = ref.offset_out_of_bounds (strict, ooboff);
1671 if (!oobref)
cc8bea0a
MS
1672 return false;
1673
213694e5
MS
1674 /* Return true without issuing a warning. */
1675 if (!do_warn)
1676 return true;
1677
1678 if (!warn_array_bounds)
21150aba
JJ
1679 return false;
1680
213694e5 1681 if (ref.ref && TREE_NO_WARNING (ref.ref))
cc8bea0a
MS
1682 return false;
1683
213694e5
MS
1684 if (EXPR_HAS_LOCATION (ref.ptr))
1685 loc = EXPR_LOCATION (ref.ptr);
cc8bea0a
MS
1686
1687 loc = expansion_point_location_if_in_system_header (loc);
1688
cc8bea0a
MS
1689 char rangestr[2][64];
1690 if (ooboff[0] == ooboff[1]
1691 || (ooboff[0] != ref.offrange[0]
1692 && ooboff[0].to_shwi () >= ooboff[1].to_shwi ()))
1693 sprintf (rangestr[0], "%lli", (long long) ooboff[0].to_shwi ());
1694 else
1695 sprintf (rangestr[0], "[%lli, %lli]",
1696 (long long) ooboff[0].to_shwi (),
1697 (long long) ooboff[1].to_shwi ());
1698
e3329a78
MS
1699 bool warned = false;
1700
cc8bea0a
MS
1701 if (oobref == error_mark_node)
1702 {
1703 if (ref.sizrange[0] == ref.sizrange[1])
287ef129
MS
1704 sprintf (rangestr[1], "%llu",
1705 (unsigned long long) ref.sizrange[0].to_shwi ());
cc8bea0a
MS
1706 else
1707 sprintf (rangestr[1], "[%lli, %lli]",
287ef129
MS
1708 (unsigned long long) ref.sizrange[0].to_uhwi (),
1709 (unsigned long long) ref.sizrange[1].to_uhwi ());
cc8bea0a 1710
e3329a78
MS
1711 tree type;
1712
cc8bea0a
MS
1713 if (DECL_P (ref.base)
1714 && TREE_CODE (type = TREE_TYPE (ref.base)) == ARRAY_TYPE)
1715 {
097f82ec 1716 auto_diagnostic_group d;
cc8bea0a
MS
1717 if (warning_at (loc, OPT_Warray_bounds,
1718 "%G%qD pointer overflow between offset %s "
1719 "and size %s accessing array %qD with type %qT",
1720 call, func, rangestr[0], rangestr[1], ref.base, type))
e3329a78
MS
1721 {
1722 inform (DECL_SOURCE_LOCATION (ref.base),
1723 "array %qD declared here", ref.base);
1724 warned = true;
1725 }
cc8bea0a 1726 else
e3329a78
MS
1727 warned = warning_at (loc, OPT_Warray_bounds,
1728 "%G%qD pointer overflow between offset %s "
1729 "and size %s",
1730 call, func, rangestr[0], rangestr[1]);
cc8bea0a
MS
1731 }
1732 else
e3329a78
MS
1733 warned = warning_at (loc, OPT_Warray_bounds,
1734 "%G%qD pointer overflow between offset %s "
1735 "and size %s",
1736 call, func, rangestr[0], rangestr[1]);
cc8bea0a
MS
1737 }
1738 else if (oobref == ref.base)
1739 {
cc8bea0a
MS
1740 /* True when the offset formed by an access to the reference
1741 is out of bounds, rather than the initial offset wich is
1742 in bounds. This implies access past the end. */
1743 bool form = ooboff[0] != ref.offrange[0];
1744
1745 if (DECL_P (ref.base))
1746 {
097f82ec 1747 auto_diagnostic_group d;
cc8bea0a
MS
1748 if ((ref.basesize < maxobjsize
1749 && warning_at (loc, OPT_Warray_bounds,
1750 form
1751 ? G_("%G%qD forming offset %s is out of "
1752 "the bounds [0, %wu] of object %qD with "
1753 "type %qT")
1754 : G_("%G%qD offset %s is out of the bounds "
1755 "[0, %wu] of object %qD with type %qT"),
1756 call, func, rangestr[0], ref.basesize.to_uhwi (),
1757 ref.base, TREE_TYPE (ref.base)))
1758 || warning_at (loc, OPT_Warray_bounds,
1759 form
1760 ? G_("%G%qD forming offset %s is out of "
1761 "the bounds of object %qD with type %qT")
1762 : G_("%G%qD offset %s is out of the bounds "
1763 "of object %qD with type %qT"),
1764 call, func, rangestr[0],
1765 ref.base, TREE_TYPE (ref.base)))
e3329a78
MS
1766 {
1767 inform (DECL_SOURCE_LOCATION (ref.base),
1768 "%qD declared here", ref.base);
1769 warned = true;
1770 }
cc8bea0a
MS
1771 }
1772 else if (ref.basesize < maxobjsize)
e3329a78
MS
1773 warned = warning_at (loc, OPT_Warray_bounds,
1774 form
1775 ? G_("%G%qD forming offset %s is out "
1776 "of the bounds [0, %wu]")
1777 : G_("%G%qD offset %s is out "
1778 "of the bounds [0, %wu]"),
1779 call, func, rangestr[0], ref.basesize.to_uhwi ());
cc8bea0a 1780 else
e3329a78
MS
1781 warned = warning_at (loc, OPT_Warray_bounds,
1782 form
1783 ? G_("%G%qD forming offset %s is out of bounds")
1784 : G_("%G%qD offset %s is out of bounds"),
1785 call, func, rangestr[0]);
cc8bea0a
MS
1786 }
1787 else if (TREE_CODE (ref.ref) == MEM_REF)
1788 {
1789 tree type = TREE_TYPE (TREE_OPERAND (ref.ref, 0));
1790 if (POINTER_TYPE_P (type))
1791 type = TREE_TYPE (type);
1792 type = TYPE_MAIN_VARIANT (type);
1793
e3329a78
MS
1794 warned = warning_at (loc, OPT_Warray_bounds,
1795 "%G%qD offset %s from the object at %qE is out "
1796 "of the bounds of %qT",
1797 call, func, rangestr[0], ref.base, type);
cc8bea0a
MS
1798 }
1799 else
1800 {
e3329a78
MS
1801 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (ref.ref));
1802
1803 warned = warning_at (loc, OPT_Warray_bounds,
1804 "%G%qD offset %s from the object at %qE is out "
1805 "of the bounds of referenced subobject %qD with "
1806 "type %qT at offset %wu",
1807 call, func, rangestr[0], ref.base,
1808 TREE_OPERAND (ref.ref, 1), type,
1809 ref.refoff.to_uhwi ());
cc8bea0a
MS
1810 }
1811
e3329a78 1812 return warned;
cc8bea0a
MS
1813}
1814
1815/* Check a CALL statement for restrict-violations and issue warnings
1816 if/when appropriate. */
1817
1818void
8a45b051 1819wrestrict_dom_walker::check_call (gimple *call)
cc8bea0a
MS
1820{
1821 /* Avoid checking the call if it has already been diagnosed for
1822 some reason. */
1823 if (gimple_no_warning_p (call))
1824 return;
1825
1826 tree func = gimple_call_fndecl (call);
3d78e008 1827 if (!func || !fndecl_built_in_p (func, BUILT_IN_NORMAL))
cc8bea0a
MS
1828 return;
1829
cc8bea0a
MS
1830 /* Argument number to extract from the call (depends on the built-in
1831 and its kind). */
1832 unsigned dst_idx = -1;
1833 unsigned src_idx = -1;
1834 unsigned bnd_idx = -1;
1835
1836 /* Is this CALL to a string function (as opposed to one to a raw
1837 memory function). */
1838 bool strfun = true;
1839
1840 switch (DECL_FUNCTION_CODE (func))
1841 {
1842 case BUILT_IN_MEMCPY:
1843 case BUILT_IN_MEMCPY_CHK:
cc8bea0a
MS
1844 case BUILT_IN_MEMPCPY:
1845 case BUILT_IN_MEMPCPY_CHK:
cc8bea0a
MS
1846 case BUILT_IN_MEMMOVE:
1847 case BUILT_IN_MEMMOVE_CHK:
cc8bea0a
MS
1848 strfun = false;
1849 /* Fall through. */
1850
1851 case BUILT_IN_STPNCPY:
1852 case BUILT_IN_STPNCPY_CHK:
1853 case BUILT_IN_STRNCAT:
1854 case BUILT_IN_STRNCAT_CHK:
1855 case BUILT_IN_STRNCPY:
1856 case BUILT_IN_STRNCPY_CHK:
1857 dst_idx = 0;
31db0fe0
ML
1858 src_idx = 1;
1859 bnd_idx = 2;
cc8bea0a
MS
1860 break;
1861
a411ae9b
MS
1862 case BUILT_IN_MEMSET:
1863 case BUILT_IN_MEMSET_CHK:
1864 dst_idx = 0;
1865 bnd_idx = 2;
1866 break;
1867
cc8bea0a
MS
1868 case BUILT_IN_STPCPY:
1869 case BUILT_IN_STPCPY_CHK:
cc8bea0a
MS
1870 case BUILT_IN_STRCPY:
1871 case BUILT_IN_STRCPY_CHK:
cc8bea0a
MS
1872 case BUILT_IN_STRCAT:
1873 case BUILT_IN_STRCAT_CHK:
cc8bea0a 1874 dst_idx = 0;
31db0fe0 1875 src_idx = 1;
cc8bea0a
MS
1876 break;
1877
1878 default:
1879 /* Handle other string functions here whose access may need
1880 to be validated for in-bounds offsets and non-overlapping
31db0fe0 1881 copies. */
cc8bea0a
MS
1882 return;
1883 }
1884
1885 unsigned nargs = gimple_call_num_args (call);
1886
1887 tree dst = dst_idx < nargs ? gimple_call_arg (call, dst_idx) : NULL_TREE;
1888 tree src = src_idx < nargs ? gimple_call_arg (call, src_idx) : NULL_TREE;
1889 tree dstwr = bnd_idx < nargs ? gimple_call_arg (call, bnd_idx) : NULL_TREE;
1890
1891 /* For string functions with an unspecified or unknown bound,
1892 assume the size of the access is one. */
1893 if (!dstwr && strfun)
1894 dstwr = size_one_node;
1895
2438cb6a
MS
1896 /* DST and SRC can be null for a call with an insufficient number
1897 of arguments to a built-in function declared without a protype. */
a411ae9b 1898 if (!dst || (src_idx < nargs && !src))
cf3fc0e8
MS
1899 return;
1900
1901 /* DST, SRC, or DSTWR can also have the wrong type in a call to
1902 a function declared without a prototype. Avoid checking such
1903 invalid calls. */
1904 if (TREE_CODE (TREE_TYPE (dst)) != POINTER_TYPE
a411ae9b 1905 || (src && TREE_CODE (TREE_TYPE (src)) != POINTER_TYPE)
cf3fc0e8
MS
1906 || (dstwr && !INTEGRAL_TYPE_P (TREE_TYPE (dstwr))))
1907 return;
1908
213694e5 1909 if (!check_bounds_or_overlap (call, dst, src, dstwr, NULL_TREE))
cc8bea0a
MS
1910 return;
1911
1912 /* Avoid diagnosing the call again. */
1913 gimple_set_no_warning (call, true);
1914}
1915
1916} /* anonymous namespace */
1917
1918/* Attempt to detect and diagnose invalid offset bounds and (except for
1919 memmove) overlapping copy in a call expression EXPR from SRC to DST
1920 and DSTSIZE and SRCSIZE bytes, respectively. Both DSTSIZE and
213694e5
MS
1921 SRCSIZE may be NULL. DO_WARN is false to detect either problem
1922 without issue a warning. Return the OPT_Wxxx constant corresponding
1923 to the warning if one has been detected and zero otherwise. */
cc8bea0a 1924
213694e5 1925int
8a45b051 1926check_bounds_or_overlap (gimple *call, tree dst, tree src, tree dstsize,
213694e5
MS
1927 tree srcsize, bool bounds_only /* = false */,
1928 bool do_warn /* = true */)
cc8bea0a 1929{
e3329a78 1930 location_t loc = gimple_nonartificial_location (call);
cc8bea0a
MS
1931 loc = expansion_point_location_if_in_system_header (loc);
1932
1933 tree func = gimple_call_fndecl (call);
1934
1935 builtin_memref dstref (dst, dstsize);
1936 builtin_memref srcref (src, srcsize);
1937
1938 builtin_access acs (call, dstref, srcref);
1939
1940 /* Set STRICT to the value of the -Warray-bounds=N argument for
1941 string functions or when N > 1. */
1942 int strict = (acs.strict () || warn_array_bounds > 1 ? warn_array_bounds : 0);
1943
1944 /* Validate offsets first to make sure they are within the bounds
1945 of the destination object if its size is known, or PTRDIFF_MAX
1946 otherwise. */
287ef129
MS
1947 if (maybe_diag_access_bounds (loc, call, func, strict, dstref, do_warn)
1948 || maybe_diag_access_bounds (loc, call, func, strict, srcref, do_warn))
cc8bea0a 1949 {
213694e5
MS
1950 if (do_warn)
1951 gimple_set_no_warning (call, true);
1952 return OPT_Warray_bounds;
cc8bea0a
MS
1953 }
1954
a411ae9b 1955 if (!warn_restrict || bounds_only || !src)
213694e5 1956 return 0;
cc8bea0a 1957
a411ae9b
MS
1958 if (!bounds_only)
1959 {
1960 switch (DECL_FUNCTION_CODE (func))
1961 {
1962 case BUILT_IN_MEMMOVE:
1963 case BUILT_IN_MEMMOVE_CHK:
1964 case BUILT_IN_MEMSET:
1965 case BUILT_IN_MEMSET_CHK:
1966 return 0;
1967 default:
1968 break;
1969 }
1970 }
1971
cc8bea0a
MS
1972 if (operand_equal_p (dst, src, 0))
1973 {
8cd95cec
MS
1974 /* Issue -Wrestrict unless the pointers are null (those do
1975 not point to objects and so do not indicate an overlap;
1976 such calls could be the result of sanitization and jump
1977 threading). */
1978 if (!integer_zerop (dst) && !gimple_no_warning_p (call))
1979 {
1980 warning_at (loc, OPT_Wrestrict,
1981 "%G%qD source argument is the same as destination",
1982 call, func);
1983 gimple_set_no_warning (call, true);
213694e5 1984 return OPT_Wrestrict;
8cd95cec
MS
1985 }
1986
213694e5 1987 return 0;
cc8bea0a
MS
1988 }
1989
1990 /* Return false when overlap has been detected. */
1991 if (maybe_diag_overlap (loc, call, acs))
1992 {
1993 gimple_set_no_warning (call, true);
213694e5 1994 return OPT_Wrestrict;
cc8bea0a
MS
1995 }
1996
213694e5 1997 return 0;
cc8bea0a
MS
1998}
1999
2000gimple_opt_pass *
2001make_pass_warn_restrict (gcc::context *ctxt)
2002{
2003 return new pass_wrestrict (ctxt);
2004}