Home | History | Annotate | Line # | Download | only in gcc
gimple-ssa-warn-access.cc revision 1.1.1.1
      1  1.1  mrg /* Pass to detect and issue warnings for invalid accesses, including
      2  1.1  mrg    invalid or mismatched allocation/deallocation calls.
      3  1.1  mrg 
      4  1.1  mrg    Copyright (C) 2020-2022 Free Software Foundation, Inc.
      5  1.1  mrg    Contributed by Martin Sebor <msebor (at) redhat.com>.
      6  1.1  mrg 
      7  1.1  mrg    This file is part of GCC.
      8  1.1  mrg 
      9  1.1  mrg    GCC is free software; you can redistribute it and/or modify it under
     10  1.1  mrg    the terms of the GNU General Public License as published by the Free
     11  1.1  mrg    Software Foundation; either version 3, or (at your option) any later
     12  1.1  mrg    version.
     13  1.1  mrg 
     14  1.1  mrg    GCC is distributed in the hope that it will be useful, but WITHOUT ANY
     15  1.1  mrg    WARRANTY; without even the implied warranty of MERCHANTABILITY or
     16  1.1  mrg    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     17  1.1  mrg    for more details.
     18  1.1  mrg 
     19  1.1  mrg    You should have received a copy of the GNU General Public License
     20  1.1  mrg    along with GCC; see the file COPYING3.  If not see
     21  1.1  mrg    <http://www.gnu.org/licenses/>.  */
     22  1.1  mrg 
     23  1.1  mrg #define INCLUDE_STRING
     24  1.1  mrg #include "config.h"
     25  1.1  mrg #include "system.h"
     26  1.1  mrg #include "coretypes.h"
     27  1.1  mrg #include "backend.h"
     28  1.1  mrg #include "tree.h"
     29  1.1  mrg #include "gimple.h"
     30  1.1  mrg #include "tree-pass.h"
     31  1.1  mrg #include "builtins.h"
     32  1.1  mrg #include "diagnostic.h"
     33  1.1  mrg #include "ssa.h"
     34  1.1  mrg #include "gimple-pretty-print.h"
     35  1.1  mrg #include "gimple-ssa-warn-access.h"
     36  1.1  mrg #include "gimple-ssa-warn-restrict.h"
     37  1.1  mrg #include "diagnostic-core.h"
     38  1.1  mrg #include "fold-const.h"
     39  1.1  mrg #include "gimple-fold.h"
     40  1.1  mrg #include "gimple-iterator.h"
     41  1.1  mrg #include "langhooks.h"
     42  1.1  mrg #include "memmodel.h"
     43  1.1  mrg #include "target.h"
     44  1.1  mrg #include "tree-dfa.h"
     45  1.1  mrg #include "tree-ssa.h"
     46  1.1  mrg #include "tree-cfg.h"
     47  1.1  mrg #include "tree-object-size.h"
     48  1.1  mrg #include "tree-ssa-strlen.h"
     49  1.1  mrg #include "calls.h"
     50  1.1  mrg #include "cfganal.h"
     51  1.1  mrg #include "intl.h"
     52  1.1  mrg #include "gimple-range.h"
     53  1.1  mrg #include "stringpool.h"
     54  1.1  mrg #include "attribs.h"
     55  1.1  mrg #include "demangle.h"
     56  1.1  mrg #include "attr-fnspec.h"
     57  1.1  mrg #include "pointer-query.h"
     58  1.1  mrg 
     59  1.1  mrg /* Return true if tree node X has an associated location.  */
     60  1.1  mrg 
     61  1.1  mrg static inline location_t
     62  1.1  mrg has_location (const_tree x)
     63  1.1  mrg {
     64  1.1  mrg   if (DECL_P (x))
     65  1.1  mrg     return DECL_SOURCE_LOCATION (x) != UNKNOWN_LOCATION;
     66  1.1  mrg 
     67  1.1  mrg   if (EXPR_P (x))
     68  1.1  mrg     return EXPR_HAS_LOCATION (x);
     69  1.1  mrg 
     70  1.1  mrg   return false;
     71  1.1  mrg }
     72  1.1  mrg 
     73  1.1  mrg /* Return the associated location of STMT.  */
     74  1.1  mrg 
     75  1.1  mrg static inline location_t
     76  1.1  mrg get_location (const gimple *stmt)
     77  1.1  mrg {
     78  1.1  mrg   return gimple_location (stmt);
     79  1.1  mrg }
     80  1.1  mrg 
     81  1.1  mrg /* Return the associated location of tree node X.  */
     82  1.1  mrg 
     83  1.1  mrg static inline location_t
     84  1.1  mrg get_location (tree x)
     85  1.1  mrg {
     86  1.1  mrg   if (DECL_P (x))
     87  1.1  mrg     return DECL_SOURCE_LOCATION (x);
     88  1.1  mrg 
     89  1.1  mrg   if (EXPR_P (x))
     90  1.1  mrg     return EXPR_LOCATION (x);
     91  1.1  mrg 
     92  1.1  mrg   return UNKNOWN_LOCATION;
     93  1.1  mrg }
     94  1.1  mrg 
     95  1.1  mrg /* Overload of the nascent tree function for GIMPLE STMT.  */
     96  1.1  mrg 
     97  1.1  mrg static inline tree
     98  1.1  mrg get_callee_fndecl (const gimple *stmt)
     99  1.1  mrg {
    100  1.1  mrg   return gimple_call_fndecl (stmt);
    101  1.1  mrg }
    102  1.1  mrg 
    103  1.1  mrg static inline unsigned
    104  1.1  mrg call_nargs (const gimple *stmt)
    105  1.1  mrg {
    106  1.1  mrg   return gimple_call_num_args (stmt);
    107  1.1  mrg }
    108  1.1  mrg 
    109  1.1  mrg static inline unsigned
    110  1.1  mrg call_nargs (const_tree expr)
    111  1.1  mrg {
    112  1.1  mrg   return call_expr_nargs (expr);
    113  1.1  mrg }
    114  1.1  mrg 
    115  1.1  mrg 
    116  1.1  mrg static inline tree
    117  1.1  mrg call_arg (const gimple *stmt, unsigned argno)
    118  1.1  mrg {
    119  1.1  mrg   return gimple_call_arg (stmt, argno);
    120  1.1  mrg }
    121  1.1  mrg 
    122  1.1  mrg static inline tree
    123  1.1  mrg call_arg (tree expr, unsigned argno)
    124  1.1  mrg {
    125  1.1  mrg   return CALL_EXPR_ARG (expr, argno);
    126  1.1  mrg }
    127  1.1  mrg 
    128  1.1  mrg /* For a call EXPR at LOC to a function FNAME that expects a string
    129  1.1  mrg    in the argument ARG, issue a diagnostic due to it being a called
    130  1.1  mrg    with an argument that is a character array with no terminating
    131  1.1  mrg    NUL.  SIZE is the EXACT size of the array, and BNDRNG the number
    132  1.1  mrg    of characters in which the NUL is expected.  Either EXPR or FNAME
    133  1.1  mrg    may be null but noth both.  SIZE may be null when BNDRNG is null.  */
    134  1.1  mrg 
    135  1.1  mrg template <class GimpleOrTree>
    136  1.1  mrg static void
    137  1.1  mrg warn_string_no_nul (location_t loc, GimpleOrTree expr, const char *fname,
    138  1.1  mrg 		    tree arg, tree decl, tree size, bool exact,
    139  1.1  mrg 		    const wide_int bndrng[2] /* = NULL */)
    140  1.1  mrg {
    141  1.1  mrg   const opt_code opt = OPT_Wstringop_overread;
    142  1.1  mrg   if ((expr && warning_suppressed_p (expr, opt))
    143  1.1  mrg       || warning_suppressed_p (arg, opt))
    144  1.1  mrg     return;
    145  1.1  mrg 
    146  1.1  mrg   loc = expansion_point_location_if_in_system_header (loc);
    147  1.1  mrg   bool warned;
    148  1.1  mrg 
    149  1.1  mrg   /* Format the bound range as a string to keep the number of messages
    150  1.1  mrg      from exploding.  */
    151  1.1  mrg   char bndstr[80];
    152  1.1  mrg   *bndstr = 0;
    153  1.1  mrg   if (bndrng)
    154  1.1  mrg     {
    155  1.1  mrg       if (bndrng[0] == bndrng[1])
    156  1.1  mrg 	sprintf (bndstr, "%llu", (unsigned long long) bndrng[0].to_uhwi ());
    157  1.1  mrg       else
    158  1.1  mrg 	sprintf (bndstr, "[%llu, %llu]",
    159  1.1  mrg 		 (unsigned long long) bndrng[0].to_uhwi (),
    160  1.1  mrg 		 (unsigned long long) bndrng[1].to_uhwi ());
    161  1.1  mrg     }
    162  1.1  mrg 
    163  1.1  mrg   const tree maxobjsize = max_object_size ();
    164  1.1  mrg   const wide_int maxsiz = wi::to_wide (maxobjsize);
    165  1.1  mrg   if (expr)
    166  1.1  mrg     {
    167  1.1  mrg       tree func = get_callee_fndecl (expr);
    168  1.1  mrg       if (bndrng)
    169  1.1  mrg 	{
    170  1.1  mrg 	  if (wi::ltu_p (maxsiz, bndrng[0]))
    171  1.1  mrg 	    warned = warning_at (loc, opt,
    172  1.1  mrg 				 "%qD specified bound %s exceeds "
    173  1.1  mrg 				 "maximum object size %E",
    174  1.1  mrg 				 func, bndstr, maxobjsize);
    175  1.1  mrg 	  else
    176  1.1  mrg 	    {
    177  1.1  mrg 	      bool maybe = wi::to_wide (size) == bndrng[0];
    178  1.1  mrg 	      warned = warning_at (loc, opt,
    179  1.1  mrg 				   exact
    180  1.1  mrg 				   ? G_("%qD specified bound %s exceeds "
    181  1.1  mrg 					"the size %E of unterminated array")
    182  1.1  mrg 				   : (maybe
    183  1.1  mrg 				      ? G_("%qD specified bound %s may "
    184  1.1  mrg 					   "exceed the size of at most %E "
    185  1.1  mrg 					   "of unterminated array")
    186  1.1  mrg 				      : G_("%qD specified bound %s exceeds "
    187  1.1  mrg 					   "the size of at most %E "
    188  1.1  mrg 					   "of unterminated array")),
    189  1.1  mrg 				   func, bndstr, size);
    190  1.1  mrg 	    }
    191  1.1  mrg 	}
    192  1.1  mrg       else
    193  1.1  mrg 	warned = warning_at (loc, opt,
    194  1.1  mrg 			     "%qD argument missing terminating nul",
    195  1.1  mrg 			     func);
    196  1.1  mrg     }
    197  1.1  mrg   else
    198  1.1  mrg     {
    199  1.1  mrg       if (bndrng)
    200  1.1  mrg 	{
    201  1.1  mrg 	  if (wi::ltu_p (maxsiz, bndrng[0]))
    202  1.1  mrg 	    warned = warning_at (loc, opt,
    203  1.1  mrg 				 "%qs specified bound %s exceeds "
    204  1.1  mrg 				 "maximum object size %E",
    205  1.1  mrg 				 fname, bndstr, maxobjsize);
    206  1.1  mrg 	  else
    207  1.1  mrg 	    {
    208  1.1  mrg 	      bool maybe = wi::to_wide (size) == bndrng[0];
    209  1.1  mrg 	      warned = warning_at (loc, opt,
    210  1.1  mrg 				   exact
    211  1.1  mrg 				   ? G_("%qs specified bound %s exceeds "
    212  1.1  mrg 					"the size %E of unterminated array")
    213  1.1  mrg 				   : (maybe
    214  1.1  mrg 				      ? G_("%qs specified bound %s may "
    215  1.1  mrg 					   "exceed the size of at most %E "
    216  1.1  mrg 					   "of unterminated array")
    217  1.1  mrg 				      : G_("%qs specified bound %s exceeds "
    218  1.1  mrg 					   "the size of at most %E "
    219  1.1  mrg 					   "of unterminated array")),
    220  1.1  mrg 				   fname, bndstr, size);
    221  1.1  mrg 	    }
    222  1.1  mrg 	}
    223  1.1  mrg       else
    224  1.1  mrg 	warned = warning_at (loc, opt,
    225  1.1  mrg 			     "%qs argument missing terminating nul",
    226  1.1  mrg 			     fname);
    227  1.1  mrg     }
    228  1.1  mrg 
    229  1.1  mrg   if (warned)
    230  1.1  mrg     {
    231  1.1  mrg       inform (get_location (decl),
    232  1.1  mrg 	      "referenced argument declared here");
    233  1.1  mrg       suppress_warning (arg, opt);
    234  1.1  mrg       if (expr)
    235  1.1  mrg 	suppress_warning (expr, opt);
    236  1.1  mrg     }
    237  1.1  mrg }
    238  1.1  mrg 
    239  1.1  mrg void
    240  1.1  mrg warn_string_no_nul (location_t loc, gimple *stmt, const char *fname,
    241  1.1  mrg 		    tree arg, tree decl, tree size /* = NULL_TREE */,
    242  1.1  mrg 		    bool exact /* = false */,
    243  1.1  mrg 		    const wide_int bndrng[2] /* = NULL */)
    244  1.1  mrg {
    245  1.1  mrg   return warn_string_no_nul<gimple *> (loc, stmt, fname,
    246  1.1  mrg 				       arg, decl, size, exact, bndrng);
    247  1.1  mrg }
    248  1.1  mrg 
    249  1.1  mrg void
    250  1.1  mrg warn_string_no_nul (location_t loc, tree expr, const char *fname,
    251  1.1  mrg 		    tree arg, tree decl, tree size /* = NULL_TREE */,
    252  1.1  mrg 		    bool exact /* = false */,
    253  1.1  mrg 		    const wide_int bndrng[2] /* = NULL */)
    254  1.1  mrg {
    255  1.1  mrg   return warn_string_no_nul<tree> (loc, expr, fname,
    256  1.1  mrg 				   arg, decl, size, exact, bndrng);
    257  1.1  mrg }
    258  1.1  mrg 
    259  1.1  mrg /* If EXP refers to an unterminated constant character array return
    260  1.1  mrg    the declaration of the object of which the array is a member or
    261  1.1  mrg    element and if SIZE is not null, set *SIZE to the size of
    262  1.1  mrg    the unterminated array and set *EXACT if the size is exact or
    263  1.1  mrg    clear it otherwise.  Otherwise return null.  */
    264  1.1  mrg 
    265  1.1  mrg tree
    266  1.1  mrg unterminated_array (tree exp, tree *size /* = NULL */, bool *exact /* = NULL */)
    267  1.1  mrg {
    268  1.1  mrg   /* C_STRLEN will return NULL and set DECL in the info
    269  1.1  mrg      structure if EXP references a unterminated array.  */
    270  1.1  mrg   c_strlen_data lendata = { };
    271  1.1  mrg   tree len = c_strlen (exp, 1, &lendata);
    272  1.1  mrg   if (len || !lendata.minlen || !lendata.decl)
    273  1.1  mrg     return NULL_TREE;
    274  1.1  mrg 
    275  1.1  mrg   if (!size)
    276  1.1  mrg     return lendata.decl;
    277  1.1  mrg 
    278  1.1  mrg   len = lendata.minlen;
    279  1.1  mrg   if (lendata.off)
    280  1.1  mrg     {
    281  1.1  mrg       /* Constant offsets are already accounted for in LENDATA.MINLEN,
    282  1.1  mrg 	 but not in a SSA_NAME + CST expression.  */
    283  1.1  mrg       if (TREE_CODE (lendata.off) == INTEGER_CST)
    284  1.1  mrg 	*exact = true;
    285  1.1  mrg       else if (TREE_CODE (lendata.off) == PLUS_EXPR
    286  1.1  mrg 	       && TREE_CODE (TREE_OPERAND (lendata.off, 1)) == INTEGER_CST)
    287  1.1  mrg 	{
    288  1.1  mrg 	  /* Subtract the offset from the size of the array.  */
    289  1.1  mrg 	  *exact = false;
    290  1.1  mrg 	  tree temp = TREE_OPERAND (lendata.off, 1);
    291  1.1  mrg 	  temp = fold_convert (ssizetype, temp);
    292  1.1  mrg 	  len = fold_build2 (MINUS_EXPR, ssizetype, len, temp);
    293  1.1  mrg 	}
    294  1.1  mrg       else
    295  1.1  mrg 	*exact = false;
    296  1.1  mrg     }
    297  1.1  mrg   else
    298  1.1  mrg     *exact = true;
    299  1.1  mrg 
    300  1.1  mrg   *size = len;
    301  1.1  mrg   return lendata.decl;
    302  1.1  mrg }
    303  1.1  mrg 
    304  1.1  mrg /* For a call EXPR (which may be null) that expects a string argument
    305  1.1  mrg    SRC as an argument, returns false if SRC is a character array with
    306  1.1  mrg    no terminating NUL.  When nonnull, BOUND is the number of characters
    307  1.1  mrg    in which to expect the terminating NUL.  When EXPR is nonnull also
    308  1.1  mrg    issues a warning.  */
    309  1.1  mrg 
    310  1.1  mrg template <class GimpleOrTree>
    311  1.1  mrg static bool
    312  1.1  mrg check_nul_terminated_array (GimpleOrTree expr, tree src, tree bound)
    313  1.1  mrg {
    314  1.1  mrg   /* The constant size of the array SRC points to.  The actual size
    315  1.1  mrg      may be less of EXACT is true, but not more.  */
    316  1.1  mrg   tree size;
    317  1.1  mrg   /* True if SRC involves a non-constant offset into the array.  */
    318  1.1  mrg   bool exact;
    319  1.1  mrg   /* The unterminated constant array SRC points to.  */
    320  1.1  mrg   tree nonstr = unterminated_array (src, &size, &exact);
    321  1.1  mrg   if (!nonstr)
    322  1.1  mrg     return true;
    323  1.1  mrg 
    324  1.1  mrg   /* NONSTR refers to the non-nul terminated constant array and SIZE
    325  1.1  mrg      is the constant size of the array in bytes.  EXACT is true when
    326  1.1  mrg      SIZE is exact.  */
    327  1.1  mrg 
    328  1.1  mrg   wide_int bndrng[2];
    329  1.1  mrg   if (bound)
    330  1.1  mrg     {
    331  1.1  mrg       value_range r;
    332  1.1  mrg 
    333  1.1  mrg       get_global_range_query ()->range_of_expr (r, bound);
    334  1.1  mrg 
    335  1.1  mrg       if (r.kind () != VR_RANGE)
    336  1.1  mrg 	return true;
    337  1.1  mrg 
    338  1.1  mrg       bndrng[0] = r.lower_bound ();
    339  1.1  mrg       bndrng[1] = r.upper_bound ();
    340  1.1  mrg 
    341  1.1  mrg       if (exact)
    342  1.1  mrg 	{
    343  1.1  mrg 	  if (wi::leu_p (bndrng[0], wi::to_wide (size)))
    344  1.1  mrg 	    return true;
    345  1.1  mrg 	}
    346  1.1  mrg       else if (wi::lt_p (bndrng[0], wi::to_wide (size), UNSIGNED))
    347  1.1  mrg 	return true;
    348  1.1  mrg     }
    349  1.1  mrg 
    350  1.1  mrg   if (expr)
    351  1.1  mrg     warn_string_no_nul (get_location (expr), expr, NULL, src, nonstr,
    352  1.1  mrg 			size, exact, bound ? bndrng : NULL);
    353  1.1  mrg 
    354  1.1  mrg   return false;
    355  1.1  mrg }
    356  1.1  mrg 
    357  1.1  mrg bool
    358  1.1  mrg check_nul_terminated_array (gimple *stmt, tree src, tree bound /* = NULL_TREE */)
    359  1.1  mrg {
    360  1.1  mrg   return check_nul_terminated_array<gimple *>(stmt, src, bound);
    361  1.1  mrg }
    362  1.1  mrg 
    363  1.1  mrg bool
    364  1.1  mrg check_nul_terminated_array (tree expr, tree src, tree bound /* = NULL_TREE */)
    365  1.1  mrg {
    366  1.1  mrg   return check_nul_terminated_array<tree>(expr, src, bound);
    367  1.1  mrg }
    368  1.1  mrg 
    369  1.1  mrg /* Warn about passing a non-string array/pointer to a built-in function
    370  1.1  mrg    that expects a nul-terminated string argument.  Returns true if
    371  1.1  mrg    a warning has been issued.*/
    372  1.1  mrg 
    373  1.1  mrg template <class GimpleOrTree>
    374  1.1  mrg static bool
    375  1.1  mrg maybe_warn_nonstring_arg (tree fndecl, GimpleOrTree exp)
    376  1.1  mrg {
    377  1.1  mrg   if (!fndecl || !fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
    378  1.1  mrg     return false;
    379  1.1  mrg 
    380  1.1  mrg   if (!warn_stringop_overread
    381  1.1  mrg       || warning_suppressed_p (exp, OPT_Wstringop_overread))
    382  1.1  mrg     return false;
    383  1.1  mrg 
    384  1.1  mrg   /* Avoid clearly invalid calls (more checking done below).  */
    385  1.1  mrg   unsigned nargs = call_nargs (exp);
    386  1.1  mrg   if (!nargs)
    387  1.1  mrg     return false;
    388  1.1  mrg 
    389  1.1  mrg   /* The bound argument to a bounded string function like strncpy.  */
    390  1.1  mrg   tree bound = NULL_TREE;
    391  1.1  mrg 
    392  1.1  mrg   /* The longest known or possible string argument to one of the comparison
    393  1.1  mrg      functions.  If the length is less than the bound it is used instead.
    394  1.1  mrg      Since the length is only used for warning and not for code generation
    395  1.1  mrg      disable strict mode in the calls to get_range_strlen below.  */
    396  1.1  mrg   tree maxlen = NULL_TREE;
    397  1.1  mrg 
    398  1.1  mrg   /* It's safe to call "bounded" string functions with a non-string
    399  1.1  mrg      argument since the functions provide an explicit bound for this
    400  1.1  mrg      purpose.  The exception is strncat where the bound may refer to
    401  1.1  mrg      either the destination or the source.  */
    402  1.1  mrg   int fncode = DECL_FUNCTION_CODE (fndecl);
    403  1.1  mrg   switch (fncode)
    404  1.1  mrg     {
    405  1.1  mrg     case BUILT_IN_STRCMP:
    406  1.1  mrg     case BUILT_IN_STRNCMP:
    407  1.1  mrg     case BUILT_IN_STRNCASECMP:
    408  1.1  mrg       {
    409  1.1  mrg 	/* For these, if one argument refers to one or more of a set
    410  1.1  mrg 	   of string constants or arrays of known size, determine
    411  1.1  mrg 	   the range of their known or possible lengths and use it
    412  1.1  mrg 	   conservatively as the bound for the unbounded function,
    413  1.1  mrg 	   and to adjust the range of the bound of the bounded ones.  */
    414  1.1  mrg 	for (unsigned argno = 0;
    415  1.1  mrg 	     argno < MIN (nargs, 2)
    416  1.1  mrg 	       && !(maxlen && TREE_CODE (maxlen) == INTEGER_CST); argno++)
    417  1.1  mrg 	  {
    418  1.1  mrg 	    tree arg = call_arg (exp, argno);
    419  1.1  mrg 	    if (!get_attr_nonstring_decl (arg))
    420  1.1  mrg 	      {
    421  1.1  mrg 		c_strlen_data lendata = { };
    422  1.1  mrg 		/* Set MAXBOUND to an arbitrary non-null non-integer
    423  1.1  mrg 		   node as a request to have it set to the length of
    424  1.1  mrg 		   the longest string in a PHI.  */
    425  1.1  mrg 		lendata.maxbound = arg;
    426  1.1  mrg 		get_range_strlen (arg, &lendata, /* eltsize = */ 1);
    427  1.1  mrg 		maxlen = lendata.maxbound;
    428  1.1  mrg 	      }
    429  1.1  mrg 	  }
    430  1.1  mrg       }
    431  1.1  mrg       /* Fall through.  */
    432  1.1  mrg 
    433  1.1  mrg     case BUILT_IN_STRNCAT:
    434  1.1  mrg     case BUILT_IN_STPNCPY:
    435  1.1  mrg     case BUILT_IN_STRNCPY:
    436  1.1  mrg       if (nargs > 2)
    437  1.1  mrg 	bound = call_arg (exp, 2);
    438  1.1  mrg       break;
    439  1.1  mrg 
    440  1.1  mrg     case BUILT_IN_STRNDUP:
    441  1.1  mrg       if (nargs < 2)
    442  1.1  mrg 	return false;
    443  1.1  mrg       bound = call_arg (exp, 1);
    444  1.1  mrg       break;
    445  1.1  mrg 
    446  1.1  mrg     case BUILT_IN_STRNLEN:
    447  1.1  mrg       {
    448  1.1  mrg 	tree arg = call_arg (exp, 0);
    449  1.1  mrg 	if (!get_attr_nonstring_decl (arg))
    450  1.1  mrg 	  {
    451  1.1  mrg 	    c_strlen_data lendata = { };
    452  1.1  mrg 	    /* Set MAXBOUND to an arbitrary non-null non-integer
    453  1.1  mrg 	       node as a request to have it set to the length of
    454  1.1  mrg 	       the longest string in a PHI.  */
    455  1.1  mrg 	    lendata.maxbound = arg;
    456  1.1  mrg 	    get_range_strlen (arg, &lendata, /* eltsize = */ 1);
    457  1.1  mrg 	    maxlen = lendata.maxbound;
    458  1.1  mrg 	  }
    459  1.1  mrg 	if (nargs > 1)
    460  1.1  mrg 	  bound = call_arg (exp, 1);
    461  1.1  mrg 	break;
    462  1.1  mrg       }
    463  1.1  mrg 
    464  1.1  mrg     default:
    465  1.1  mrg       break;
    466  1.1  mrg     }
    467  1.1  mrg 
    468  1.1  mrg   /* Determine the range of the bound argument (if specified).  */
    469  1.1  mrg   tree bndrng[2] = { NULL_TREE, NULL_TREE };
    470  1.1  mrg   if (bound)
    471  1.1  mrg     {
    472  1.1  mrg       STRIP_NOPS (bound);
    473  1.1  mrg       get_size_range (bound, bndrng);
    474  1.1  mrg     }
    475  1.1  mrg 
    476  1.1  mrg   location_t loc = get_location (exp);
    477  1.1  mrg 
    478  1.1  mrg   if (bndrng[0])
    479  1.1  mrg     {
    480  1.1  mrg       /* Diagnose excessive bound prior to the adjustment below and
    481  1.1  mrg 	 regardless of attribute nonstring.  */
    482  1.1  mrg       tree maxobjsize = max_object_size ();
    483  1.1  mrg       if (tree_int_cst_lt (maxobjsize, bndrng[0]))
    484  1.1  mrg 	{
    485  1.1  mrg 	  bool warned = false;
    486  1.1  mrg 	  if (tree_int_cst_equal (bndrng[0], bndrng[1]))
    487  1.1  mrg 	    warned = warning_at (loc, OPT_Wstringop_overread,
    488  1.1  mrg 				 "%qD specified bound %E "
    489  1.1  mrg 				 "exceeds maximum object size %E",
    490  1.1  mrg 				 fndecl, bndrng[0], maxobjsize);
    491  1.1  mrg 	  else
    492  1.1  mrg 	    warned = warning_at (loc, OPT_Wstringop_overread,
    493  1.1  mrg 				 "%qD specified bound [%E, %E] "
    494  1.1  mrg 				 "exceeds maximum object size %E",
    495  1.1  mrg 				 fndecl, bndrng[0], bndrng[1],
    496  1.1  mrg 				 maxobjsize);
    497  1.1  mrg 	  if (warned)
    498  1.1  mrg 	    suppress_warning (exp, OPT_Wstringop_overread);
    499  1.1  mrg 
    500  1.1  mrg 	  return warned;
    501  1.1  mrg 	}
    502  1.1  mrg     }
    503  1.1  mrg 
    504  1.1  mrg   if (maxlen && !integer_all_onesp (maxlen))
    505  1.1  mrg     {
    506  1.1  mrg       /* Add one for the nul.  */
    507  1.1  mrg       maxlen = const_binop (PLUS_EXPR, TREE_TYPE (maxlen), maxlen,
    508  1.1  mrg 			    size_one_node);
    509  1.1  mrg 
    510  1.1  mrg       if (!bndrng[0])
    511  1.1  mrg 	{
    512  1.1  mrg 	  /* Conservatively use the upper bound of the lengths for
    513  1.1  mrg 	     both the lower and the upper bound of the operation.  */
    514  1.1  mrg 	  bndrng[0] = maxlen;
    515  1.1  mrg 	  bndrng[1] = maxlen;
    516  1.1  mrg 	  bound = void_type_node;
    517  1.1  mrg 	}
    518  1.1  mrg       else if (maxlen)
    519  1.1  mrg 	{
    520  1.1  mrg 	  /* Replace the bound on the operation with the upper bound
    521  1.1  mrg 	     of the length of the string if the latter is smaller.  */
    522  1.1  mrg 	  if (tree_int_cst_lt (maxlen, bndrng[0]))
    523  1.1  mrg 	    bndrng[0] = maxlen;
    524  1.1  mrg 	  else if (tree_int_cst_lt (maxlen, bndrng[1]))
    525  1.1  mrg 	    bndrng[1] = maxlen;
    526  1.1  mrg 	}
    527  1.1  mrg     }
    528  1.1  mrg 
    529  1.1  mrg   bool any_arg_warned = false;
    530  1.1  mrg   /* Iterate over the built-in function's formal arguments and check
    531  1.1  mrg      each const char* against the actual argument.  If the actual
    532  1.1  mrg      argument is declared attribute non-string issue a warning unless
    533  1.1  mrg      the argument's maximum length is bounded.  */
    534  1.1  mrg   function_args_iterator it;
    535  1.1  mrg   function_args_iter_init (&it, TREE_TYPE (fndecl));
    536  1.1  mrg 
    537  1.1  mrg   for (unsigned argno = 0; ; ++argno, function_args_iter_next (&it))
    538  1.1  mrg     {
    539  1.1  mrg       /* Avoid iterating past the declared argument in a call
    540  1.1  mrg 	 to function declared without a prototype.  */
    541  1.1  mrg       if (argno >= nargs)
    542  1.1  mrg 	break;
    543  1.1  mrg 
    544  1.1  mrg       tree argtype = function_args_iter_cond (&it);
    545  1.1  mrg       if (!argtype)
    546  1.1  mrg 	break;
    547  1.1  mrg 
    548  1.1  mrg       if (TREE_CODE (argtype) != POINTER_TYPE)
    549  1.1  mrg 	continue;
    550  1.1  mrg 
    551  1.1  mrg       argtype = TREE_TYPE (argtype);
    552  1.1  mrg 
    553  1.1  mrg       if (TREE_CODE (argtype) != INTEGER_TYPE
    554  1.1  mrg 	  || !TYPE_READONLY (argtype))
    555  1.1  mrg 	continue;
    556  1.1  mrg 
    557  1.1  mrg       argtype = TYPE_MAIN_VARIANT (argtype);
    558  1.1  mrg       if (argtype != char_type_node)
    559  1.1  mrg 	continue;
    560  1.1  mrg 
    561  1.1  mrg       tree callarg = call_arg (exp, argno);
    562  1.1  mrg       if (TREE_CODE (callarg) == ADDR_EXPR)
    563  1.1  mrg 	callarg = TREE_OPERAND (callarg, 0);
    564  1.1  mrg 
    565  1.1  mrg       /* See if the destination is declared with attribute "nonstring".  */
    566  1.1  mrg       tree decl = get_attr_nonstring_decl (callarg);
    567  1.1  mrg       if (!decl)
    568  1.1  mrg 	continue;
    569  1.1  mrg 
    570  1.1  mrg       /* The maximum number of array elements accessed.  */
    571  1.1  mrg       offset_int wibnd = 0;
    572  1.1  mrg 
    573  1.1  mrg       if (argno && fncode == BUILT_IN_STRNCAT)
    574  1.1  mrg 	{
    575  1.1  mrg 	  /* See if the bound in strncat is derived from the length
    576  1.1  mrg 	     of the strlen of the destination (as it's expected to be).
    577  1.1  mrg 	     If so, reset BOUND and FNCODE to trigger a warning.  */
    578  1.1  mrg 	  tree dstarg = call_arg (exp, 0);
    579  1.1  mrg 	  if (is_strlen_related_p (dstarg, bound))
    580  1.1  mrg 	    {
    581  1.1  mrg 	      /* The bound applies to the destination, not to the source,
    582  1.1  mrg 		 so reset these to trigger a warning without mentioning
    583  1.1  mrg 		 the bound.  */
    584  1.1  mrg 	      bound = NULL;
    585  1.1  mrg 	      fncode = 0;
    586  1.1  mrg 	    }
    587  1.1  mrg 	  else if (bndrng[1])
    588  1.1  mrg 	    /* Use the upper bound of the range for strncat.  */
    589  1.1  mrg 	    wibnd = wi::to_offset (bndrng[1]);
    590  1.1  mrg 	}
    591  1.1  mrg       else if (bndrng[0])
    592  1.1  mrg 	/* Use the lower bound of the range for functions other than
    593  1.1  mrg 	   strncat.  */
    594  1.1  mrg 	wibnd = wi::to_offset (bndrng[0]);
    595  1.1  mrg 
    596  1.1  mrg       /* Determine the size of the argument array if it is one.  */
    597  1.1  mrg       offset_int asize = wibnd;
    598  1.1  mrg       bool known_size = false;
    599  1.1  mrg       tree type = TREE_TYPE (decl);
    600  1.1  mrg 
    601  1.1  mrg       /* Determine the array size.  For arrays of unknown bound and
    602  1.1  mrg 	 pointers reset BOUND to trigger the appropriate warning.  */
    603  1.1  mrg       if (TREE_CODE (type) == ARRAY_TYPE)
    604  1.1  mrg 	{
    605  1.1  mrg 	  if (tree arrbnd = TYPE_DOMAIN (type))
    606  1.1  mrg 	    {
    607  1.1  mrg 	      if ((arrbnd = TYPE_MAX_VALUE (arrbnd))
    608  1.1  mrg 		  && TREE_CODE (arrbnd) == INTEGER_CST)
    609  1.1  mrg 		{
    610  1.1  mrg 		  asize = wi::to_offset (arrbnd) + 1;
    611  1.1  mrg 		  known_size = true;
    612  1.1  mrg 		}
    613  1.1  mrg 	    }
    614  1.1  mrg 	  else if (bound == void_type_node)
    615  1.1  mrg 	    bound = NULL_TREE;
    616  1.1  mrg 	}
    617  1.1  mrg       else if (bound == void_type_node)
    618  1.1  mrg 	bound = NULL_TREE;
    619  1.1  mrg 
    620  1.1  mrg       /* In a call to strncat with a bound in a range whose lower but
    621  1.1  mrg 	 not upper bound is less than the array size, reset ASIZE to
    622  1.1  mrg 	 be the same as the bound and the other variable to trigger
    623  1.1  mrg 	 the appropriate warning below.  */
    624  1.1  mrg       if (fncode == BUILT_IN_STRNCAT
    625  1.1  mrg 	  && bndrng[0] != bndrng[1]
    626  1.1  mrg 	  && wi::ltu_p (wi::to_offset (bndrng[0]), asize)
    627  1.1  mrg 	  && (!known_size
    628  1.1  mrg 	      || wi::ltu_p (asize, wibnd)))
    629  1.1  mrg 	{
    630  1.1  mrg 	  asize = wibnd;
    631  1.1  mrg 	  bound = NULL_TREE;
    632  1.1  mrg 	  fncode = 0;
    633  1.1  mrg 	}
    634  1.1  mrg 
    635  1.1  mrg       bool warned = false;
    636  1.1  mrg 
    637  1.1  mrg       auto_diagnostic_group d;
    638  1.1  mrg       if (wi::ltu_p (asize, wibnd))
    639  1.1  mrg 	{
    640  1.1  mrg 	  if (bndrng[0] == bndrng[1])
    641  1.1  mrg 	    warned = warning_at (loc, OPT_Wstringop_overread,
    642  1.1  mrg 				 "%qD argument %i declared attribute "
    643  1.1  mrg 				 "%<nonstring%> is smaller than the specified "
    644  1.1  mrg 				 "bound %wu",
    645  1.1  mrg 				 fndecl, argno + 1, wibnd.to_uhwi ());
    646  1.1  mrg 	  else if (wi::ltu_p (asize, wi::to_offset (bndrng[0])))
    647  1.1  mrg 	    warned = warning_at (loc, OPT_Wstringop_overread,
    648  1.1  mrg 				 "%qD argument %i declared attribute "
    649  1.1  mrg 				 "%<nonstring%> is smaller than "
    650  1.1  mrg 				 "the specified bound [%E, %E]",
    651  1.1  mrg 				 fndecl, argno + 1, bndrng[0], bndrng[1]);
    652  1.1  mrg 	  else
    653  1.1  mrg 	    warned = warning_at (loc, OPT_Wstringop_overread,
    654  1.1  mrg 				 "%qD argument %i declared attribute "
    655  1.1  mrg 				 "%<nonstring%> may be smaller than "
    656  1.1  mrg 				 "the specified bound [%E, %E]",
    657  1.1  mrg 				 fndecl, argno + 1, bndrng[0], bndrng[1]);
    658  1.1  mrg 	}
    659  1.1  mrg       else if (fncode == BUILT_IN_STRNCAT)
    660  1.1  mrg 	; /* Avoid warning for calls to strncat() when the bound
    661  1.1  mrg 	     is equal to the size of the non-string argument.  */
    662  1.1  mrg       else if (!bound)
    663  1.1  mrg 	warned = warning_at (loc, OPT_Wstringop_overread,
    664  1.1  mrg 			     "%qD argument %i declared attribute %<nonstring%>",
    665  1.1  mrg 			     fndecl, argno + 1);
    666  1.1  mrg 
    667  1.1  mrg       if (warned)
    668  1.1  mrg 	{
    669  1.1  mrg 	  inform (DECL_SOURCE_LOCATION (decl),
    670  1.1  mrg 		  "argument %qD declared here", decl);
    671  1.1  mrg 	  any_arg_warned = true;
    672  1.1  mrg 	}
    673  1.1  mrg     }
    674  1.1  mrg 
    675  1.1  mrg   if (any_arg_warned)
    676  1.1  mrg     suppress_warning (exp, OPT_Wstringop_overread);
    677  1.1  mrg 
    678  1.1  mrg   return any_arg_warned;
    679  1.1  mrg }
    680  1.1  mrg 
    681  1.1  mrg bool
    682  1.1  mrg maybe_warn_nonstring_arg (tree fndecl, gimple *stmt)
    683  1.1  mrg {
    684  1.1  mrg   return maybe_warn_nonstring_arg<gimple *>(fndecl, stmt);
    685  1.1  mrg }
    686  1.1  mrg 
    687  1.1  mrg 
    688  1.1  mrg bool
    689  1.1  mrg maybe_warn_nonstring_arg (tree fndecl, tree expr)
    690  1.1  mrg {
    691  1.1  mrg   return maybe_warn_nonstring_arg<tree>(fndecl, expr);
    692  1.1  mrg }
    693  1.1  mrg 
    694  1.1  mrg /* Issue a warning OPT for a bounded call EXP with a bound in RANGE
    695  1.1  mrg    accessing an object with SIZE.  */
    696  1.1  mrg 
    697  1.1  mrg template <class GimpleOrTree>
    698  1.1  mrg static bool
    699  1.1  mrg maybe_warn_for_bound (opt_code opt, location_t loc, GimpleOrTree exp, tree func,
    700  1.1  mrg 		      tree bndrng[2], tree size, const access_data *pad)
    701  1.1  mrg {
    702  1.1  mrg   if (!bndrng[0] || warning_suppressed_p (exp, opt))
    703  1.1  mrg     return false;
    704  1.1  mrg 
    705  1.1  mrg   tree maxobjsize = max_object_size ();
    706  1.1  mrg 
    707  1.1  mrg   bool warned = false;
    708  1.1  mrg 
    709  1.1  mrg   if (opt == OPT_Wstringop_overread)
    710  1.1  mrg     {
    711  1.1  mrg       bool maybe = pad && pad->src.phi ();
    712  1.1  mrg       if (maybe)
    713  1.1  mrg 	{
    714  1.1  mrg 	  /* Issue a "maybe" warning only if the PHI refers to objects
    715  1.1  mrg 	     at least one of which has more space remaining than the bound.
    716  1.1  mrg 	     Otherwise, if the bound is greater, use the definitive form.  */
    717  1.1  mrg 	  offset_int remmax = pad->src.size_remaining ();
    718  1.1  mrg 	  if (remmax < wi::to_offset (bndrng[0]))
    719  1.1  mrg 	    maybe = false;
    720  1.1  mrg 	}
    721  1.1  mrg 
    722  1.1  mrg       if (tree_int_cst_lt (maxobjsize, bndrng[0]))
    723  1.1  mrg 	{
    724  1.1  mrg 	  if (bndrng[0] == bndrng[1])
    725  1.1  mrg 	    warned = (func
    726  1.1  mrg 		      ? warning_at (loc, opt,
    727  1.1  mrg 				    (maybe
    728  1.1  mrg 				     ? G_("%qD specified bound %E may "
    729  1.1  mrg 					  "exceed maximum object size %E")
    730  1.1  mrg 				     : G_("%qD specified bound %E "
    731  1.1  mrg 					  "exceeds maximum object size %E")),
    732  1.1  mrg 				    func, bndrng[0], maxobjsize)
    733  1.1  mrg 		      : warning_at (loc, opt,
    734  1.1  mrg 				    (maybe
    735  1.1  mrg 				     ? G_("specified bound %E may "
    736  1.1  mrg 					  "exceed maximum object size %E")
    737  1.1  mrg 				     : G_("specified bound %E "
    738  1.1  mrg 					  "exceeds maximum object size %E")),
    739  1.1  mrg 				    bndrng[0], maxobjsize));
    740  1.1  mrg 	  else
    741  1.1  mrg 	    warned = (func
    742  1.1  mrg 		      ? warning_at (loc, opt,
    743  1.1  mrg 				    (maybe
    744  1.1  mrg 				     ? G_("%qD specified bound [%E, %E] may "
    745  1.1  mrg 					  "exceed maximum object size %E")
    746  1.1  mrg 				     : G_("%qD specified bound [%E, %E] "
    747  1.1  mrg 					  "exceeds maximum object size %E")),
    748  1.1  mrg 				    func,
    749  1.1  mrg 				    bndrng[0], bndrng[1], maxobjsize)
    750  1.1  mrg 		      : warning_at (loc, opt,
    751  1.1  mrg 				    (maybe
    752  1.1  mrg 				     ? G_("specified bound [%E, %E] may "
    753  1.1  mrg 					  "exceed maximum object size %E")
    754  1.1  mrg 				     : G_("specified bound [%E, %E] "
    755  1.1  mrg 					  "exceeds maximum object size %E")),
    756  1.1  mrg 				    bndrng[0], bndrng[1], maxobjsize));
    757  1.1  mrg 	}
    758  1.1  mrg       else if (!size || tree_int_cst_le (bndrng[0], size))
    759  1.1  mrg 	return false;
    760  1.1  mrg       else if (tree_int_cst_equal (bndrng[0], bndrng[1]))
    761  1.1  mrg 	warned = (func
    762  1.1  mrg 		  ? warning_at (loc, opt,
    763  1.1  mrg 				(maybe
    764  1.1  mrg 				 ? G_("%qD specified bound %E may exceed "
    765  1.1  mrg 				      "source size %E")
    766  1.1  mrg 				 : G_("%qD specified bound %E exceeds "
    767  1.1  mrg 				      "source size %E")),
    768  1.1  mrg 				func, bndrng[0], size)
    769  1.1  mrg 		  : warning_at (loc, opt,
    770  1.1  mrg 				(maybe
    771  1.1  mrg 				 ? G_("specified bound %E may exceed "
    772  1.1  mrg 				      "source size %E")
    773  1.1  mrg 				 : G_("specified bound %E exceeds "
    774  1.1  mrg 				      "source size %E")),
    775  1.1  mrg 				bndrng[0], size));
    776  1.1  mrg       else
    777  1.1  mrg 	warned = (func
    778  1.1  mrg 		  ? warning_at (loc, opt,
    779  1.1  mrg 				(maybe
    780  1.1  mrg 				 ? G_("%qD specified bound [%E, %E] may "
    781  1.1  mrg 				      "exceed source size %E")
    782  1.1  mrg 				 : G_("%qD specified bound [%E, %E] exceeds "
    783  1.1  mrg 				      "source size %E")),
    784  1.1  mrg 				func, bndrng[0], bndrng[1], size)
    785  1.1  mrg 		  : warning_at (loc, opt,
    786  1.1  mrg 				(maybe
    787  1.1  mrg 				 ? G_("specified bound [%E, %E] may exceed "
    788  1.1  mrg 				      "source size %E")
    789  1.1  mrg 				 : G_("specified bound [%E, %E] exceeds "
    790  1.1  mrg 				      "source size %E")),
    791  1.1  mrg 				bndrng[0], bndrng[1], size));
    792  1.1  mrg       if (warned)
    793  1.1  mrg 	{
    794  1.1  mrg 	  if (pad && pad->src.ref
    795  1.1  mrg 	      && has_location (pad->src.ref))
    796  1.1  mrg 	    inform (get_location (pad->src.ref),
    797  1.1  mrg 		    "source object allocated here");
    798  1.1  mrg 	  suppress_warning (exp, opt);
    799  1.1  mrg 	}
    800  1.1  mrg 
    801  1.1  mrg       return warned;
    802  1.1  mrg     }
    803  1.1  mrg 
    804  1.1  mrg   bool maybe = pad && pad->dst.phi ();
    805  1.1  mrg   if (maybe)
    806  1.1  mrg     {
    807  1.1  mrg       /* Issue a "maybe" warning only if the PHI refers to objects
    808  1.1  mrg 	 at least one of which has more space remaining than the bound.
    809  1.1  mrg 	 Otherwise, if the bound is greater, use the definitive form.  */
    810  1.1  mrg       offset_int remmax = pad->dst.size_remaining ();
    811  1.1  mrg       if (remmax < wi::to_offset (bndrng[0]))
    812  1.1  mrg 	maybe = false;
    813  1.1  mrg     }
    814  1.1  mrg   if (tree_int_cst_lt (maxobjsize, bndrng[0]))
    815  1.1  mrg     {
    816  1.1  mrg       if (bndrng[0] == bndrng[1])
    817  1.1  mrg 	warned = (func
    818  1.1  mrg 		  ? warning_at (loc, opt,
    819  1.1  mrg 				(maybe
    820  1.1  mrg 				 ? G_("%qD specified size %E may "
    821  1.1  mrg 				      "exceed maximum object size %E")
    822  1.1  mrg 				 : G_("%qD specified size %E "
    823  1.1  mrg 				      "exceeds maximum object size %E")),
    824  1.1  mrg 				func, bndrng[0], maxobjsize)
    825  1.1  mrg 		  : warning_at (loc, opt,
    826  1.1  mrg 				(maybe
    827  1.1  mrg 				 ? G_("specified size %E may exceed "
    828  1.1  mrg 				      "maximum object size %E")
    829  1.1  mrg 				 : G_("specified size %E exceeds "
    830  1.1  mrg 				      "maximum object size %E")),
    831  1.1  mrg 				bndrng[0], maxobjsize));
    832  1.1  mrg       else
    833  1.1  mrg 	warned = (func
    834  1.1  mrg 		  ? warning_at (loc, opt,
    835  1.1  mrg 				(maybe
    836  1.1  mrg 				 ? G_("%qD specified size between %E and %E "
    837  1.1  mrg 				      "may exceed maximum object size %E")
    838  1.1  mrg 				 : G_("%qD specified size between %E and %E "
    839  1.1  mrg 				      "exceeds maximum object size %E")),
    840  1.1  mrg 				func, bndrng[0], bndrng[1], maxobjsize)
    841  1.1  mrg 		  : warning_at (loc, opt,
    842  1.1  mrg 				(maybe
    843  1.1  mrg 				 ? G_("specified size between %E and %E "
    844  1.1  mrg 				      "may exceed maximum object size %E")
    845  1.1  mrg 				 : G_("specified size between %E and %E "
    846  1.1  mrg 				      "exceeds maximum object size %E")),
    847  1.1  mrg 				bndrng[0], bndrng[1], maxobjsize));
    848  1.1  mrg     }
    849  1.1  mrg   else if (!size || tree_int_cst_le (bndrng[0], size))
    850  1.1  mrg     return false;
    851  1.1  mrg   else if (tree_int_cst_equal (bndrng[0], bndrng[1]))
    852  1.1  mrg     warned = (func
    853  1.1  mrg 	      ? warning_at (loc, opt,
    854  1.1  mrg 			    (maybe
    855  1.1  mrg 			     ? G_("%qD specified bound %E may exceed "
    856  1.1  mrg 				  "destination size %E")
    857  1.1  mrg 			     : G_("%qD specified bound %E exceeds "
    858  1.1  mrg 				  "destination size %E")),
    859  1.1  mrg 			    func, bndrng[0], size)
    860  1.1  mrg 	      : warning_at (loc, opt,
    861  1.1  mrg 			    (maybe
    862  1.1  mrg 			     ? G_("specified bound %E may exceed "
    863  1.1  mrg 				  "destination size %E")
    864  1.1  mrg 			     : G_("specified bound %E exceeds "
    865  1.1  mrg 				  "destination size %E")),
    866  1.1  mrg 			    bndrng[0], size));
    867  1.1  mrg   else
    868  1.1  mrg     warned = (func
    869  1.1  mrg 	      ? warning_at (loc, opt,
    870  1.1  mrg 			    (maybe
    871  1.1  mrg 			     ? G_("%qD specified bound [%E, %E] may exceed "
    872  1.1  mrg 				  "destination size %E")
    873  1.1  mrg 			     : G_("%qD specified bound [%E, %E] exceeds "
    874  1.1  mrg 				  "destination size %E")),
    875  1.1  mrg 			    func, bndrng[0], bndrng[1], size)
    876  1.1  mrg 	      : warning_at (loc, opt,
    877  1.1  mrg 			    (maybe
    878  1.1  mrg 			     ? G_("specified bound [%E, %E] exceeds "
    879  1.1  mrg 				  "destination size %E")
    880  1.1  mrg 			     : G_("specified bound [%E, %E] exceeds "
    881  1.1  mrg 				  "destination size %E")),
    882  1.1  mrg 			    bndrng[0], bndrng[1], size));
    883  1.1  mrg 
    884  1.1  mrg   if (warned)
    885  1.1  mrg     {
    886  1.1  mrg       if (pad && pad->dst.ref
    887  1.1  mrg 	  && has_location (pad->dst.ref))
    888  1.1  mrg 	inform (get_location (pad->dst.ref),
    889  1.1  mrg 		"destination object allocated here");
    890  1.1  mrg       suppress_warning (exp, opt);
    891  1.1  mrg     }
    892  1.1  mrg 
    893  1.1  mrg   return warned;
    894  1.1  mrg }
    895  1.1  mrg 
    896  1.1  mrg bool
    897  1.1  mrg maybe_warn_for_bound (opt_code opt, location_t loc, gimple *stmt, tree func,
    898  1.1  mrg 		      tree bndrng[2], tree size,
    899  1.1  mrg 		      const access_data *pad /* = NULL */)
    900  1.1  mrg {
    901  1.1  mrg   return maybe_warn_for_bound<gimple *> (opt, loc, stmt, func, bndrng, size,
    902  1.1  mrg 					 pad);
    903  1.1  mrg }
    904  1.1  mrg 
    905  1.1  mrg bool
    906  1.1  mrg maybe_warn_for_bound (opt_code opt, location_t loc, tree expr, tree func,
    907  1.1  mrg 		      tree bndrng[2], tree size,
    908  1.1  mrg 		      const access_data *pad /* = NULL */)
    909  1.1  mrg {
    910  1.1  mrg   return maybe_warn_for_bound<tree> (opt, loc, expr, func, bndrng, size, pad);
    911  1.1  mrg }
    912  1.1  mrg 
    913  1.1  mrg /* For an expression EXP issue an access warning controlled by option OPT
    914  1.1  mrg    with access to a region SIZE bytes in size in the RANGE of sizes.
    915  1.1  mrg    WRITE is true for a write access, READ for a read access, neither for
    916  1.1  mrg    call that may or may not perform an access but for which the range
    917  1.1  mrg    is expected to valid.
    918  1.1  mrg    Returns true when a warning has been issued.  */
    919  1.1  mrg 
    920  1.1  mrg template <class GimpleOrTree>
    921  1.1  mrg static bool
    922  1.1  mrg warn_for_access (location_t loc, tree func, GimpleOrTree exp, int opt,
    923  1.1  mrg 		 tree range[2], tree size, bool write, bool read, bool maybe)
    924  1.1  mrg {
    925  1.1  mrg   bool warned = false;
    926  1.1  mrg 
    927  1.1  mrg   if (write && read)
    928  1.1  mrg     {
    929  1.1  mrg       if (tree_int_cst_equal (range[0], range[1]))
    930  1.1  mrg 	warned = (func
    931  1.1  mrg 		  ? warning_n (loc, opt, tree_to_uhwi (range[0]),
    932  1.1  mrg 			       (maybe
    933  1.1  mrg 				? G_("%qD may access %E byte in a region "
    934  1.1  mrg 				     "of size %E")
    935  1.1  mrg 				: G_("%qD accessing %E byte in a region "
    936  1.1  mrg 				     "of size %E")),
    937  1.1  mrg 				(maybe
    938  1.1  mrg 				 ? G_ ("%qD may access %E bytes in a region "
    939  1.1  mrg 				       "of size %E")
    940  1.1  mrg 				 : G_ ("%qD accessing %E bytes in a region "
    941  1.1  mrg 				       "of size %E")),
    942  1.1  mrg 			       func, range[0], size)
    943  1.1  mrg 		  : warning_n (loc, opt, tree_to_uhwi (range[0]),
    944  1.1  mrg 			       (maybe
    945  1.1  mrg 				? G_("may access %E byte in a region "
    946  1.1  mrg 				     "of size %E")
    947  1.1  mrg 				: G_("accessing %E byte in a region "
    948  1.1  mrg 				     "of size %E")),
    949  1.1  mrg 			       (maybe
    950  1.1  mrg 				? G_("may access %E bytes in a region "
    951  1.1  mrg 				     "of size %E")
    952  1.1  mrg 				: G_("accessing %E bytes in a region "
    953  1.1  mrg 				     "of size %E")),
    954  1.1  mrg 			       range[0], size));
    955  1.1  mrg       else if (tree_int_cst_sign_bit (range[1]))
    956  1.1  mrg 	{
    957  1.1  mrg 	  /* Avoid printing the upper bound if it's invalid.  */
    958  1.1  mrg 	  warned = (func
    959  1.1  mrg 		    ? warning_at (loc, opt,
    960  1.1  mrg 				  (maybe
    961  1.1  mrg 				   ? G_("%qD may access %E or more bytes "
    962  1.1  mrg 					"in a region of size %E")
    963  1.1  mrg 				   : G_("%qD accessing %E or more bytes "
    964  1.1  mrg 					"in a region of size %E")),
    965  1.1  mrg 				  func, range[0], size)
    966  1.1  mrg 		    : warning_at (loc, opt,
    967  1.1  mrg 				  (maybe
    968  1.1  mrg 				   ? G_("may access %E or more bytes "
    969  1.1  mrg 					"in a region of size %E")
    970  1.1  mrg 				   : G_("accessing %E or more bytes "
    971  1.1  mrg 					"in a region of size %E")),
    972  1.1  mrg 				  range[0], size));
    973  1.1  mrg 	}
    974  1.1  mrg       else
    975  1.1  mrg 	warned = (func
    976  1.1  mrg 		  ? warning_at (loc, opt,
    977  1.1  mrg 				(maybe
    978  1.1  mrg 				 ? G_("%qD may access between %E and %E "
    979  1.1  mrg 				      "bytes in a region of size %E")
    980  1.1  mrg 				 : G_("%qD accessing between %E and %E "
    981  1.1  mrg 				      "bytes in a region of size %E")),
    982  1.1  mrg 				func, range[0], range[1], size)
    983  1.1  mrg 		  : warning_at (loc, opt,
    984  1.1  mrg 				(maybe
    985  1.1  mrg 				 ? G_("may access between %E and %E bytes "
    986  1.1  mrg 				      "in a region of size %E")
    987  1.1  mrg 				 : G_("accessing between %E and %E bytes "
    988  1.1  mrg 				      "in a region of size %E")),
    989  1.1  mrg 				range[0], range[1], size));
    990  1.1  mrg       return warned;
    991  1.1  mrg     }
    992  1.1  mrg 
    993  1.1  mrg   if (write)
    994  1.1  mrg     {
    995  1.1  mrg       if (tree_int_cst_equal (range[0], range[1]))
    996  1.1  mrg 	warned = (func
    997  1.1  mrg 		  ? warning_n (loc, opt, tree_to_uhwi (range[0]),
    998  1.1  mrg 			       (maybe
    999  1.1  mrg 				? G_("%qD may write %E byte into a region "
   1000  1.1  mrg 				     "of size %E")
   1001  1.1  mrg 				: G_("%qD writing %E byte into a region "
   1002  1.1  mrg 				     "of size %E overflows the destination")),
   1003  1.1  mrg 			       (maybe
   1004  1.1  mrg 				? G_("%qD may write %E bytes into a region "
   1005  1.1  mrg 				     "of size %E")
   1006  1.1  mrg 				: G_("%qD writing %E bytes into a region "
   1007  1.1  mrg 				     "of size %E overflows the destination")),
   1008  1.1  mrg 			       func, range[0], size)
   1009  1.1  mrg 		  : warning_n (loc, opt, tree_to_uhwi (range[0]),
   1010  1.1  mrg 			       (maybe
   1011  1.1  mrg 				? G_("may write %E byte into a region "
   1012  1.1  mrg 				     "of size %E")
   1013  1.1  mrg 				: G_("writing %E byte into a region "
   1014  1.1  mrg 				     "of size %E overflows the destination")),
   1015  1.1  mrg 			       (maybe
   1016  1.1  mrg 				? G_("may write %E bytes into a region "
   1017  1.1  mrg 				     "of size %E")
   1018  1.1  mrg 				: G_("writing %E bytes into a region "
   1019  1.1  mrg 				     "of size %E overflows the destination")),
   1020  1.1  mrg 			       range[0], size));
   1021  1.1  mrg       else if (tree_int_cst_sign_bit (range[1]))
   1022  1.1  mrg 	{
   1023  1.1  mrg 	  /* Avoid printing the upper bound if it's invalid.  */
   1024  1.1  mrg 	  warned = (func
   1025  1.1  mrg 		    ? warning_at (loc, opt,
   1026  1.1  mrg 				  (maybe
   1027  1.1  mrg 				   ? G_("%qD may write %E or more bytes "
   1028  1.1  mrg 					"into a region of size %E")
   1029  1.1  mrg 				   : G_("%qD writing %E or more bytes "
   1030  1.1  mrg 					"into a region of size %E overflows "
   1031  1.1  mrg 					"the destination")),
   1032  1.1  mrg 				  func, range[0], size)
   1033  1.1  mrg 		    : warning_at (loc, opt,
   1034  1.1  mrg 				  (maybe
   1035  1.1  mrg 				   ? G_("may write %E or more bytes into "
   1036  1.1  mrg 					"a region of size %E")
   1037  1.1  mrg 				   : G_("writing %E or more bytes into "
   1038  1.1  mrg 					"a region of size %E overflows "
   1039  1.1  mrg 					"the destination")),
   1040  1.1  mrg 				  range[0], size));
   1041  1.1  mrg 	}
   1042  1.1  mrg       else
   1043  1.1  mrg 	warned = (func
   1044  1.1  mrg 		  ? warning_at (loc, opt,
   1045  1.1  mrg 				(maybe
   1046  1.1  mrg 				 ? G_("%qD may write between %E and %E bytes "
   1047  1.1  mrg 				      "into a region of size %E")
   1048  1.1  mrg 				 : G_("%qD writing between %E and %E bytes "
   1049  1.1  mrg 				      "into a region of size %E overflows "
   1050  1.1  mrg 				      "the destination")),
   1051  1.1  mrg 				func, range[0], range[1], size)
   1052  1.1  mrg 		  : warning_at (loc, opt,
   1053  1.1  mrg 				(maybe
   1054  1.1  mrg 				 ? G_("may write between %E and %E bytes "
   1055  1.1  mrg 				      "into a region of size %E")
   1056  1.1  mrg 				 : G_("writing between %E and %E bytes "
   1057  1.1  mrg 				      "into a region of size %E overflows "
   1058  1.1  mrg 				      "the destination")),
   1059  1.1  mrg 				range[0], range[1], size));
   1060  1.1  mrg       return warned;
   1061  1.1  mrg     }
   1062  1.1  mrg 
   1063  1.1  mrg   if (read)
   1064  1.1  mrg     {
   1065  1.1  mrg       if (tree_int_cst_equal (range[0], range[1]))
   1066  1.1  mrg 	warned = (func
   1067  1.1  mrg 		  ? warning_n (loc, OPT_Wstringop_overread,
   1068  1.1  mrg 			       tree_to_uhwi (range[0]),
   1069  1.1  mrg 			       (maybe
   1070  1.1  mrg 				? G_("%qD may read %E byte from a region "
   1071  1.1  mrg 				     "of size %E")
   1072  1.1  mrg 				: G_("%qD reading %E byte from a region "
   1073  1.1  mrg 				     "of size %E")),
   1074  1.1  mrg 			       (maybe
   1075  1.1  mrg 				? G_("%qD may read %E bytes from a region "
   1076  1.1  mrg 				     "of size %E")
   1077  1.1  mrg 				: G_("%qD reading %E bytes from a region "
   1078  1.1  mrg 				     "of size %E")),
   1079  1.1  mrg 			       func, range[0], size)
   1080  1.1  mrg 		  : warning_n (loc, OPT_Wstringop_overread,
   1081  1.1  mrg 			       tree_to_uhwi (range[0]),
   1082  1.1  mrg 			       (maybe
   1083  1.1  mrg 				? G_("may read %E byte from a region "
   1084  1.1  mrg 				     "of size %E")
   1085  1.1  mrg 				: G_("reading %E byte from a region "
   1086  1.1  mrg 				     "of size %E")),
   1087  1.1  mrg 			       (maybe
   1088  1.1  mrg 				? G_("may read %E bytes from a region "
   1089  1.1  mrg 				     "of size %E")
   1090  1.1  mrg 				: G_("reading %E bytes from a region "
   1091  1.1  mrg 				     "of size %E")),
   1092  1.1  mrg 			       range[0], size));
   1093  1.1  mrg       else if (tree_int_cst_sign_bit (range[1]))
   1094  1.1  mrg 	{
   1095  1.1  mrg 	  /* Avoid printing the upper bound if it's invalid.  */
   1096  1.1  mrg 	  warned = (func
   1097  1.1  mrg 		    ? warning_at (loc, OPT_Wstringop_overread,
   1098  1.1  mrg 				  (maybe
   1099  1.1  mrg 				   ? G_("%qD may read %E or more bytes "
   1100  1.1  mrg 					"from a region of size %E")
   1101  1.1  mrg 				   : G_("%qD reading %E or more bytes "
   1102  1.1  mrg 					"from a region of size %E")),
   1103  1.1  mrg 				  func, range[0], size)
   1104  1.1  mrg 		    : warning_at (loc, OPT_Wstringop_overread,
   1105  1.1  mrg 				  (maybe
   1106  1.1  mrg 				   ? G_("may read %E or more bytes "
   1107  1.1  mrg 					"from a region of size %E")
   1108  1.1  mrg 				   : G_("reading %E or more bytes "
   1109  1.1  mrg 					"from a region of size %E")),
   1110  1.1  mrg 				  range[0], size));
   1111  1.1  mrg 	}
   1112  1.1  mrg       else
   1113  1.1  mrg 	warned = (func
   1114  1.1  mrg 		  ? warning_at (loc, OPT_Wstringop_overread,
   1115  1.1  mrg 				(maybe
   1116  1.1  mrg 				 ? G_("%qD may read between %E and %E bytes "
   1117  1.1  mrg 				      "from a region of size %E")
   1118  1.1  mrg 				 : G_("%qD reading between %E and %E bytes "
   1119  1.1  mrg 				      "from a region of size %E")),
   1120  1.1  mrg 				func, range[0], range[1], size)
   1121  1.1  mrg 		  : warning_at (loc, opt,
   1122  1.1  mrg 				(maybe
   1123  1.1  mrg 				 ? G_("may read between %E and %E bytes "
   1124  1.1  mrg 				      "from a region of size %E")
   1125  1.1  mrg 				 : G_("reading between %E and %E bytes "
   1126  1.1  mrg 				      "from a region of size %E")),
   1127  1.1  mrg 				range[0], range[1], size));
   1128  1.1  mrg 
   1129  1.1  mrg       if (warned)
   1130  1.1  mrg 	suppress_warning (exp, OPT_Wstringop_overread);
   1131  1.1  mrg 
   1132  1.1  mrg       return warned;
   1133  1.1  mrg     }
   1134  1.1  mrg 
   1135  1.1  mrg   if (tree_int_cst_equal (range[0], range[1])
   1136  1.1  mrg       || tree_int_cst_sign_bit (range[1]))
   1137  1.1  mrg     warned = (func
   1138  1.1  mrg 	      ? warning_n (loc, OPT_Wstringop_overread,
   1139  1.1  mrg 			   tree_to_uhwi (range[0]),
   1140  1.1  mrg 			   "%qD expecting %E byte in a region of size %E",
   1141  1.1  mrg 			   "%qD expecting %E bytes in a region of size %E",
   1142  1.1  mrg 			   func, range[0], size)
   1143  1.1  mrg 	      : warning_n (loc, OPT_Wstringop_overread,
   1144  1.1  mrg 			   tree_to_uhwi (range[0]),
   1145  1.1  mrg 			   "expecting %E byte in a region of size %E",
   1146  1.1  mrg 			   "expecting %E bytes in a region of size %E",
   1147  1.1  mrg 			   range[0], size));
   1148  1.1  mrg   else if (tree_int_cst_sign_bit (range[1]))
   1149  1.1  mrg     {
   1150  1.1  mrg       /* Avoid printing the upper bound if it's invalid.  */
   1151  1.1  mrg       warned = (func
   1152  1.1  mrg 		? warning_at (loc, OPT_Wstringop_overread,
   1153  1.1  mrg 			      "%qD expecting %E or more bytes in a region "
   1154  1.1  mrg 			      "of size %E",
   1155  1.1  mrg 			      func, range[0], size)
   1156  1.1  mrg 		: warning_at (loc, OPT_Wstringop_overread,
   1157  1.1  mrg 			      "expecting %E or more bytes in a region "
   1158  1.1  mrg 			      "of size %E",
   1159  1.1  mrg 			      range[0], size));
   1160  1.1  mrg     }
   1161  1.1  mrg   else
   1162  1.1  mrg     warned = (func
   1163  1.1  mrg 	      ? warning_at (loc, OPT_Wstringop_overread,
   1164  1.1  mrg 			    "%qD expecting between %E and %E bytes in "
   1165  1.1  mrg 			    "a region of size %E",
   1166  1.1  mrg 			    func, range[0], range[1], size)
   1167  1.1  mrg 	      : warning_at (loc, OPT_Wstringop_overread,
   1168  1.1  mrg 			    "expecting between %E and %E bytes in "
   1169  1.1  mrg 			    "a region of size %E",
   1170  1.1  mrg 			    range[0], range[1], size));
   1171  1.1  mrg 
   1172  1.1  mrg   if (warned)
   1173  1.1  mrg     suppress_warning (exp, OPT_Wstringop_overread);
   1174  1.1  mrg 
   1175  1.1  mrg   return warned;
   1176  1.1  mrg }
   1177  1.1  mrg 
   1178  1.1  mrg static bool
   1179  1.1  mrg warn_for_access (location_t loc, tree func, gimple *stmt, int opt,
   1180  1.1  mrg 		 tree range[2], tree size, bool write, bool read, bool maybe)
   1181  1.1  mrg {
   1182  1.1  mrg   return warn_for_access<gimple *>(loc, func, stmt, opt, range, size,
   1183  1.1  mrg 				   write, read, maybe);
   1184  1.1  mrg }
   1185  1.1  mrg 
   1186  1.1  mrg static bool
   1187  1.1  mrg warn_for_access (location_t loc, tree func, tree expr, int opt,
   1188  1.1  mrg 		 tree range[2], tree size, bool write, bool read, bool maybe)
   1189  1.1  mrg {
   1190  1.1  mrg   return warn_for_access<tree>(loc, func, expr, opt, range, size,
   1191  1.1  mrg 			       write, read, maybe);
   1192  1.1  mrg }
   1193  1.1  mrg 
   1194  1.1  mrg /* Helper to set RANGE to the range of BOUND if it's nonnull, bounded
   1195  1.1  mrg    by BNDRNG if nonnull and valid.  */
   1196  1.1  mrg 
   1197  1.1  mrg static void
   1198  1.1  mrg get_size_range (range_query *query, tree bound, gimple *stmt, tree range[2],
   1199  1.1  mrg 		const offset_int bndrng[2])
   1200  1.1  mrg {
   1201  1.1  mrg   if (bound)
   1202  1.1  mrg     get_size_range (query, bound, stmt, range);
   1203  1.1  mrg 
   1204  1.1  mrg   if (!bndrng || (bndrng[0] == 0 && bndrng[1] == HOST_WIDE_INT_M1U))
   1205  1.1  mrg     return;
   1206  1.1  mrg 
   1207  1.1  mrg   if (range[0] && TREE_CODE (range[0]) == INTEGER_CST)
   1208  1.1  mrg     {
   1209  1.1  mrg       offset_int r[] =
   1210  1.1  mrg 	{ wi::to_offset (range[0]), wi::to_offset (range[1]) };
   1211  1.1  mrg       if (r[0] < bndrng[0])
   1212  1.1  mrg 	range[0] = wide_int_to_tree (sizetype, bndrng[0]);
   1213  1.1  mrg       if (bndrng[1] < r[1])
   1214  1.1  mrg 	range[1] = wide_int_to_tree (sizetype, bndrng[1]);
   1215  1.1  mrg     }
   1216  1.1  mrg   else
   1217  1.1  mrg     {
   1218  1.1  mrg       range[0] = wide_int_to_tree (sizetype, bndrng[0]);
   1219  1.1  mrg       range[1] = wide_int_to_tree (sizetype, bndrng[1]);
   1220  1.1  mrg     }
   1221  1.1  mrg }
   1222  1.1  mrg 
   1223  1.1  mrg /* Try to verify that the sizes and lengths of the arguments to a string
   1224  1.1  mrg    manipulation function given by EXP are within valid bounds and that
   1225  1.1  mrg    the operation does not lead to buffer overflow or read past the end.
   1226  1.1  mrg    Arguments other than EXP may be null.  When non-null, the arguments
   1227  1.1  mrg    have the following meaning:
   1228  1.1  mrg    DST is the destination of a copy call or NULL otherwise.
   1229  1.1  mrg    SRC is the source of a copy call or NULL otherwise.
   1230  1.1  mrg    DSTWRITE is the number of bytes written into the destination obtained
   1231  1.1  mrg    from the user-supplied size argument to the function (such as in
   1232  1.1  mrg    memcpy(DST, SRCs, DSTWRITE) or strncpy(DST, DRC, DSTWRITE).
   1233  1.1  mrg    MAXREAD is the user-supplied bound on the length of the source sequence
   1234  1.1  mrg    (such as in strncat(d, s, N).  It specifies the upper limit on the number
   1235  1.1  mrg    of bytes to write.  If NULL, it's taken to be the same as DSTWRITE.
   1236  1.1  mrg    SRCSTR is the source string (such as in strcpy(DST, SRC)) when the
   1237  1.1  mrg    expression EXP is a string function call (as opposed to a memory call
   1238  1.1  mrg    like memcpy).  As an exception, SRCSTR can also be an integer denoting
   1239  1.1  mrg    the precomputed size of the source string or object (for functions like
   1240  1.1  mrg    memcpy).
   1241  1.1  mrg    DSTSIZE is the size of the destination object.
   1242  1.1  mrg 
   1243  1.1  mrg    When DSTWRITE is null LEN is checked to verify that it doesn't exceed
   1244  1.1  mrg    SIZE_MAX.
   1245  1.1  mrg 
   1246  1.1  mrg    WRITE is true for write accesses, READ is true for reads.  Both are
   1247  1.1  mrg    false for simple size checks in calls to functions that neither read
   1248  1.1  mrg    from nor write to the region.
   1249  1.1  mrg 
   1250  1.1  mrg    When nonnull, PAD points to a more detailed description of the access.
   1251  1.1  mrg 
   1252  1.1  mrg    If the call is successfully verified as safe return true, otherwise
   1253  1.1  mrg    return false.  */
   1254  1.1  mrg 
   1255  1.1  mrg template <class GimpleOrTree>
   1256  1.1  mrg static bool
   1257  1.1  mrg check_access (GimpleOrTree exp, tree dstwrite,
   1258  1.1  mrg 	      tree maxread, tree srcstr, tree dstsize,
   1259  1.1  mrg 	      access_mode mode, const access_data *pad,
   1260  1.1  mrg 	      range_query *rvals)
   1261  1.1  mrg {
   1262  1.1  mrg   /* The size of the largest object is half the address space, or
   1263  1.1  mrg      PTRDIFF_MAX.  (This is way too permissive.)  */
   1264  1.1  mrg   tree maxobjsize = max_object_size ();
   1265  1.1  mrg 
   1266  1.1  mrg   /* Either an approximate/minimum the length of the source string for
   1267  1.1  mrg      string functions or the size of the source object for raw memory
   1268  1.1  mrg      functions.  */
   1269  1.1  mrg   tree slen = NULL_TREE;
   1270  1.1  mrg 
   1271  1.1  mrg   /* The range of the access in bytes; first set to the write access
   1272  1.1  mrg      for functions that write and then read for those that also (or
   1273  1.1  mrg      just) read.  */
   1274  1.1  mrg   tree range[2] = { NULL_TREE, NULL_TREE };
   1275  1.1  mrg 
   1276  1.1  mrg   /* Set to true when the exact number of bytes written by a string
   1277  1.1  mrg      function like strcpy is not known and the only thing that is
   1278  1.1  mrg      known is that it must be at least one (for the terminating nul).  */
   1279  1.1  mrg   bool at_least_one = false;
   1280  1.1  mrg   if (srcstr)
   1281  1.1  mrg     {
   1282  1.1  mrg       /* SRCSTR is normally a pointer to string but as a special case
   1283  1.1  mrg 	 it can be an integer denoting the length of a string.  */
   1284  1.1  mrg       if (POINTER_TYPE_P (TREE_TYPE (srcstr)))
   1285  1.1  mrg 	{
   1286  1.1  mrg 	  if (!check_nul_terminated_array (exp, srcstr, maxread))
   1287  1.1  mrg 	    /* Return if the array is not nul-terminated and a warning
   1288  1.1  mrg 	       has been issued.  */
   1289  1.1  mrg 	    return false;
   1290  1.1  mrg 
   1291  1.1  mrg 	  /* Try to determine the range of lengths the source string
   1292  1.1  mrg 	     refers to.  If it can be determined and is less than
   1293  1.1  mrg 	     the upper bound given by MAXREAD add one to it for
   1294  1.1  mrg 	     the terminating nul.  Otherwise, set it to one for
   1295  1.1  mrg 	     the same reason, or to MAXREAD as appropriate.  */
   1296  1.1  mrg 	  c_strlen_data lendata = { };
   1297  1.1  mrg 	  get_range_strlen (srcstr, &lendata, /* eltsize = */ 1);
   1298  1.1  mrg 	  range[0] = lendata.minlen;
   1299  1.1  mrg 	  range[1] = lendata.maxbound ? lendata.maxbound : lendata.maxlen;
   1300  1.1  mrg 	  if (range[0]
   1301  1.1  mrg 	      && TREE_CODE (range[0]) == INTEGER_CST
   1302  1.1  mrg 	      && TREE_CODE (range[1]) == INTEGER_CST
   1303  1.1  mrg 	      && (!maxread || TREE_CODE (maxread) == INTEGER_CST))
   1304  1.1  mrg 	    {
   1305  1.1  mrg 	      if (maxread && tree_int_cst_le (maxread, range[0]))
   1306  1.1  mrg 		range[0] = range[1] = maxread;
   1307  1.1  mrg 	      else
   1308  1.1  mrg 		range[0] = fold_build2 (PLUS_EXPR, size_type_node,
   1309  1.1  mrg 					range[0], size_one_node);
   1310  1.1  mrg 
   1311  1.1  mrg 	      if (maxread && tree_int_cst_le (maxread, range[1]))
   1312  1.1  mrg 		range[1] = maxread;
   1313  1.1  mrg 	      else if (!integer_all_onesp (range[1]))
   1314  1.1  mrg 		range[1] = fold_build2 (PLUS_EXPR, size_type_node,
   1315  1.1  mrg 					range[1], size_one_node);
   1316  1.1  mrg 
   1317  1.1  mrg 	      slen = range[0];
   1318  1.1  mrg 	    }
   1319  1.1  mrg 	  else
   1320  1.1  mrg 	    {
   1321  1.1  mrg 	      at_least_one = true;
   1322  1.1  mrg 	      slen = size_one_node;
   1323  1.1  mrg 	    }
   1324  1.1  mrg 	}
   1325  1.1  mrg       else
   1326  1.1  mrg 	slen = srcstr;
   1327  1.1  mrg     }
   1328  1.1  mrg 
   1329  1.1  mrg   if (!dstwrite && !maxread)
   1330  1.1  mrg     {
   1331  1.1  mrg       /* When the only available piece of data is the object size
   1332  1.1  mrg 	 there is nothing to do.  */
   1333  1.1  mrg       if (!slen)
   1334  1.1  mrg 	return true;
   1335  1.1  mrg 
   1336  1.1  mrg       /* Otherwise, when the length of the source sequence is known
   1337  1.1  mrg 	 (as with strlen), set DSTWRITE to it.  */
   1338  1.1  mrg       if (!range[0])
   1339  1.1  mrg 	dstwrite = slen;
   1340  1.1  mrg     }
   1341  1.1  mrg 
   1342  1.1  mrg   if (!dstsize)
   1343  1.1  mrg     dstsize = maxobjsize;
   1344  1.1  mrg 
   1345  1.1  mrg   /* Set RANGE to that of DSTWRITE if non-null, bounded by PAD->DST_BNDRNG
   1346  1.1  mrg      if valid.  */
   1347  1.1  mrg   gimple *stmt = pad ? pad->stmt : nullptr;
   1348  1.1  mrg   get_size_range (rvals, dstwrite, stmt, range, pad ? pad->dst_bndrng : NULL);
   1349  1.1  mrg 
   1350  1.1  mrg   tree func = get_callee_fndecl (exp);
   1351  1.1  mrg   /* Read vs write access by built-ins can be determined from the const
   1352  1.1  mrg      qualifiers on the pointer argument.  In the absence of attribute
   1353  1.1  mrg      access, non-const qualified pointer arguments to user-defined
   1354  1.1  mrg      functions are assumed to both read and write the objects.  */
   1355  1.1  mrg   const bool builtin = func ? fndecl_built_in_p (func) : false;
   1356  1.1  mrg 
   1357  1.1  mrg   /* First check the number of bytes to be written against the maximum
   1358  1.1  mrg      object size.  */
   1359  1.1  mrg   if (range[0]
   1360  1.1  mrg       && TREE_CODE (range[0]) == INTEGER_CST
   1361  1.1  mrg       && tree_int_cst_lt (maxobjsize, range[0]))
   1362  1.1  mrg     {
   1363  1.1  mrg       location_t loc = get_location (exp);
   1364  1.1  mrg       maybe_warn_for_bound (OPT_Wstringop_overflow_, loc, exp, func, range,
   1365  1.1  mrg 			    NULL_TREE, pad);
   1366  1.1  mrg       return false;
   1367  1.1  mrg     }
   1368  1.1  mrg 
   1369  1.1  mrg   /* The number of bytes to write is "exact" if DSTWRITE is non-null,
   1370  1.1  mrg      constant, and in range of unsigned HOST_WIDE_INT.  */
   1371  1.1  mrg   bool exactwrite = dstwrite && tree_fits_uhwi_p (dstwrite);
   1372  1.1  mrg 
   1373  1.1  mrg   /* Next check the number of bytes to be written against the destination
   1374  1.1  mrg      object size.  */
   1375  1.1  mrg   if (range[0] || !exactwrite || integer_all_onesp (dstwrite))
   1376  1.1  mrg     {
   1377  1.1  mrg       if (range[0]
   1378  1.1  mrg 	  && TREE_CODE (range[0]) == INTEGER_CST
   1379  1.1  mrg 	  && ((tree_fits_uhwi_p (dstsize)
   1380  1.1  mrg 	       && tree_int_cst_lt (dstsize, range[0]))
   1381  1.1  mrg 	      || (dstwrite
   1382  1.1  mrg 		  && tree_fits_uhwi_p (dstwrite)
   1383  1.1  mrg 		  && tree_int_cst_lt (dstwrite, range[0]))))
   1384  1.1  mrg 	{
   1385  1.1  mrg 	  const opt_code opt = OPT_Wstringop_overflow_;
   1386  1.1  mrg 	  if (warning_suppressed_p (exp, opt)
   1387  1.1  mrg 	      || (pad && pad->dst.ref
   1388  1.1  mrg 		  && warning_suppressed_p (pad->dst.ref, opt)))
   1389  1.1  mrg 	    return false;
   1390  1.1  mrg 
   1391  1.1  mrg 	  location_t loc = get_location (exp);
   1392  1.1  mrg 	  bool warned = false;
   1393  1.1  mrg 	  if (dstwrite == slen && at_least_one)
   1394  1.1  mrg 	    {
   1395  1.1  mrg 	      /* This is a call to strcpy with a destination of 0 size
   1396  1.1  mrg 		 and a source of unknown length.  The call will write
   1397  1.1  mrg 		 at least one byte past the end of the destination.  */
   1398  1.1  mrg 	      warned = (func
   1399  1.1  mrg 			? warning_at (loc, opt,
   1400  1.1  mrg 				      "%qD writing %E or more bytes into "
   1401  1.1  mrg 				      "a region of size %E overflows "
   1402  1.1  mrg 				      "the destination",
   1403  1.1  mrg 				      func, range[0], dstsize)
   1404  1.1  mrg 			: warning_at (loc, opt,
   1405  1.1  mrg 				      "writing %E or more bytes into "
   1406  1.1  mrg 				      "a region of size %E overflows "
   1407  1.1  mrg 				      "the destination",
   1408  1.1  mrg 				      range[0], dstsize));
   1409  1.1  mrg 	    }
   1410  1.1  mrg 	  else
   1411  1.1  mrg 	    {
   1412  1.1  mrg 	      const bool read
   1413  1.1  mrg 		= mode == access_read_only || mode == access_read_write;
   1414  1.1  mrg 	      const bool write
   1415  1.1  mrg 		= mode == access_write_only || mode == access_read_write;
   1416  1.1  mrg 	      const bool maybe = pad && pad->dst.parmarray;
   1417  1.1  mrg 	      warned = warn_for_access (loc, func, exp,
   1418  1.1  mrg 					OPT_Wstringop_overflow_,
   1419  1.1  mrg 					range, dstsize,
   1420  1.1  mrg 					write, read && !builtin, maybe);
   1421  1.1  mrg 	    }
   1422  1.1  mrg 
   1423  1.1  mrg 	  if (warned)
   1424  1.1  mrg 	    {
   1425  1.1  mrg 	      suppress_warning (exp, OPT_Wstringop_overflow_);
   1426  1.1  mrg 	      if (pad)
   1427  1.1  mrg 		pad->dst.inform_access (pad->mode);
   1428  1.1  mrg 	    }
   1429  1.1  mrg 
   1430  1.1  mrg 	  /* Return error when an overflow has been detected.  */
   1431  1.1  mrg 	  return false;
   1432  1.1  mrg 	}
   1433  1.1  mrg     }
   1434  1.1  mrg 
   1435  1.1  mrg   /* Check the maximum length of the source sequence against the size
   1436  1.1  mrg      of the destination object if known, or against the maximum size
   1437  1.1  mrg      of an object.  */
   1438  1.1  mrg   if (maxread)
   1439  1.1  mrg     {
   1440  1.1  mrg       /* Set RANGE to that of MAXREAD, bounded by PAD->SRC_BNDRNG if
   1441  1.1  mrg 	 PAD is nonnull and BNDRNG is valid.  */
   1442  1.1  mrg       get_size_range (rvals, maxread, stmt, range, pad ? pad->src_bndrng : NULL);
   1443  1.1  mrg 
   1444  1.1  mrg       location_t loc = get_location (exp);
   1445  1.1  mrg       tree size = dstsize;
   1446  1.1  mrg       if (pad && pad->mode == access_read_only)
   1447  1.1  mrg 	size = wide_int_to_tree (sizetype, pad->src.size_remaining ());
   1448  1.1  mrg 
   1449  1.1  mrg       if (range[0] && maxread && tree_fits_uhwi_p (size))
   1450  1.1  mrg 	{
   1451  1.1  mrg 	  if (tree_int_cst_lt (maxobjsize, range[0]))
   1452  1.1  mrg 	    {
   1453  1.1  mrg 	      maybe_warn_for_bound (OPT_Wstringop_overread, loc, exp, func,
   1454  1.1  mrg 				    range, size, pad);
   1455  1.1  mrg 	      return false;
   1456  1.1  mrg 	    }
   1457  1.1  mrg 
   1458  1.1  mrg 	  if (size != maxobjsize && tree_int_cst_lt (size, range[0]))
   1459  1.1  mrg 	    {
   1460  1.1  mrg 	      opt_code opt = (dstwrite || mode != access_read_only
   1461  1.1  mrg 			      ? OPT_Wstringop_overflow_
   1462  1.1  mrg 			      : OPT_Wstringop_overread);
   1463  1.1  mrg 	      maybe_warn_for_bound (opt, loc, exp, func, range, size, pad);
   1464  1.1  mrg 	      return false;
   1465  1.1  mrg 	    }
   1466  1.1  mrg 	}
   1467  1.1  mrg 
   1468  1.1  mrg       maybe_warn_nonstring_arg (func, exp);
   1469  1.1  mrg     }
   1470  1.1  mrg 
   1471  1.1  mrg   /* Check for reading past the end of SRC.  */
   1472  1.1  mrg   bool overread = (slen
   1473  1.1  mrg 		   && slen == srcstr
   1474  1.1  mrg 		   && dstwrite
   1475  1.1  mrg 		   && range[0]
   1476  1.1  mrg 		   && TREE_CODE (slen) == INTEGER_CST
   1477  1.1  mrg 		   && tree_int_cst_lt (slen, range[0]));
   1478  1.1  mrg   /* If none is determined try to get a better answer based on the details
   1479  1.1  mrg      in PAD.  */
   1480  1.1  mrg   if (!overread
   1481  1.1  mrg       && pad
   1482  1.1  mrg       && pad->src.sizrng[1] >= 0
   1483  1.1  mrg       && pad->src.offrng[0] >= 0
   1484  1.1  mrg       && (pad->src.offrng[1] < 0
   1485  1.1  mrg 	  || pad->src.offrng[0] <= pad->src.offrng[1]))
   1486  1.1  mrg     {
   1487  1.1  mrg       /* Set RANGE to that of MAXREAD, bounded by PAD->SRC_BNDRNG if
   1488  1.1  mrg 	 PAD is nonnull and BNDRNG is valid.  */
   1489  1.1  mrg       get_size_range (rvals, maxread, stmt, range, pad ? pad->src_bndrng : NULL);
   1490  1.1  mrg       /* Set OVERREAD for reads starting just past the end of an object.  */
   1491  1.1  mrg       overread = pad->src.sizrng[1] - pad->src.offrng[0] < pad->src_bndrng[0];
   1492  1.1  mrg       range[0] = wide_int_to_tree (sizetype, pad->src_bndrng[0]);
   1493  1.1  mrg       slen = size_zero_node;
   1494  1.1  mrg     }
   1495  1.1  mrg 
   1496  1.1  mrg   if (overread)
   1497  1.1  mrg     {
   1498  1.1  mrg       const opt_code opt = OPT_Wstringop_overread;
   1499  1.1  mrg       if (warning_suppressed_p (exp, opt)
   1500  1.1  mrg 	  || (srcstr && warning_suppressed_p (srcstr, opt))
   1501  1.1  mrg 	  || (pad && pad->src.ref
   1502  1.1  mrg 	      && warning_suppressed_p (pad->src.ref, opt)))
   1503  1.1  mrg 	return false;
   1504  1.1  mrg 
   1505  1.1  mrg       location_t loc = get_location (exp);
   1506  1.1  mrg       const bool read
   1507  1.1  mrg 	= mode == access_read_only || mode == access_read_write;
   1508  1.1  mrg       const bool maybe = pad && pad->dst.parmarray;
   1509  1.1  mrg       if (warn_for_access (loc, func, exp, opt, range, slen, false, read,
   1510  1.1  mrg 			   maybe))
   1511  1.1  mrg 	{
   1512  1.1  mrg 	  suppress_warning (exp, opt);
   1513  1.1  mrg 	  if (pad)
   1514  1.1  mrg 	    pad->src.inform_access (access_read_only);
   1515  1.1  mrg 	}
   1516  1.1  mrg       return false;
   1517  1.1  mrg     }
   1518  1.1  mrg 
   1519  1.1  mrg   return true;
   1520  1.1  mrg }
   1521  1.1  mrg 
   1522  1.1  mrg static bool
   1523  1.1  mrg check_access (gimple *stmt, tree dstwrite,
   1524  1.1  mrg 	      tree maxread, tree srcstr, tree dstsize,
   1525  1.1  mrg 	      access_mode mode, const access_data *pad,
   1526  1.1  mrg 	      range_query *rvals)
   1527  1.1  mrg {
   1528  1.1  mrg   return check_access<gimple *> (stmt, dstwrite, maxread, srcstr, dstsize,
   1529  1.1  mrg 				 mode, pad, rvals);
   1530  1.1  mrg }
   1531  1.1  mrg 
   1532  1.1  mrg bool
   1533  1.1  mrg check_access (tree expr, tree dstwrite,
   1534  1.1  mrg 	      tree maxread, tree srcstr, tree dstsize,
   1535  1.1  mrg 	      access_mode mode, const access_data *pad /* = NULL */)
   1536  1.1  mrg {
   1537  1.1  mrg   return check_access<tree> (expr, dstwrite, maxread, srcstr, dstsize,
   1538  1.1  mrg 			     mode, pad, nullptr);
   1539  1.1  mrg }
   1540  1.1  mrg 
   1541  1.1  mrg /* Return true if STMT is a call to an allocation function.  Unless
   1542  1.1  mrg    ALL_ALLOC is set, consider only functions that return dynamically
   1543  1.1  mrg    allocated objects.  Otherwise return true even for all forms of
   1544  1.1  mrg    alloca (including VLA).  */
   1545  1.1  mrg 
   1546  1.1  mrg static bool
   1547  1.1  mrg fndecl_alloc_p (tree fndecl, bool all_alloc)
   1548  1.1  mrg {
   1549  1.1  mrg   if (!fndecl)
   1550  1.1  mrg     return false;
   1551  1.1  mrg 
   1552  1.1  mrg   /* A call to operator new isn't recognized as one to a built-in.  */
   1553  1.1  mrg   if (DECL_IS_OPERATOR_NEW_P (fndecl))
   1554  1.1  mrg     return true;
   1555  1.1  mrg 
   1556  1.1  mrg   if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
   1557  1.1  mrg     {
   1558  1.1  mrg       switch (DECL_FUNCTION_CODE (fndecl))
   1559  1.1  mrg 	{
   1560  1.1  mrg 	case BUILT_IN_ALLOCA:
   1561  1.1  mrg 	case BUILT_IN_ALLOCA_WITH_ALIGN:
   1562  1.1  mrg 	  return all_alloc;
   1563  1.1  mrg 	case BUILT_IN_ALIGNED_ALLOC:
   1564  1.1  mrg 	case BUILT_IN_CALLOC:
   1565  1.1  mrg 	case BUILT_IN_GOMP_ALLOC:
   1566  1.1  mrg 	case BUILT_IN_MALLOC:
   1567  1.1  mrg 	case BUILT_IN_REALLOC:
   1568  1.1  mrg 	case BUILT_IN_STRDUP:
   1569  1.1  mrg 	case BUILT_IN_STRNDUP:
   1570  1.1  mrg 	  return true;
   1571  1.1  mrg 	default:
   1572  1.1  mrg 	  break;
   1573  1.1  mrg 	}
   1574  1.1  mrg     }
   1575  1.1  mrg 
   1576  1.1  mrg   /* A function is considered an allocation function if it's declared
   1577  1.1  mrg      with attribute malloc with an argument naming its associated
   1578  1.1  mrg      deallocation function.  */
   1579  1.1  mrg   tree attrs = DECL_ATTRIBUTES (fndecl);
   1580  1.1  mrg   if (!attrs)
   1581  1.1  mrg     return false;
   1582  1.1  mrg 
   1583  1.1  mrg   for (tree allocs = attrs;
   1584  1.1  mrg        (allocs = lookup_attribute ("malloc", allocs));
   1585  1.1  mrg        allocs = TREE_CHAIN (allocs))
   1586  1.1  mrg     {
   1587  1.1  mrg       tree args = TREE_VALUE (allocs);
   1588  1.1  mrg       if (!args)
   1589  1.1  mrg 	continue;
   1590  1.1  mrg 
   1591  1.1  mrg       if (TREE_VALUE (args))
   1592  1.1  mrg 	return true;
   1593  1.1  mrg     }
   1594  1.1  mrg 
   1595  1.1  mrg   return false;
   1596  1.1  mrg }
   1597  1.1  mrg 
   1598  1.1  mrg /* Return true if STMT is a call to an allocation function.  A wrapper
   1599  1.1  mrg    around fndecl_alloc_p.  */
   1600  1.1  mrg 
   1601  1.1  mrg static bool
   1602  1.1  mrg gimple_call_alloc_p (gimple *stmt, bool all_alloc = false)
   1603  1.1  mrg {
   1604  1.1  mrg   return fndecl_alloc_p (gimple_call_fndecl (stmt), all_alloc);
   1605  1.1  mrg }
   1606  1.1  mrg 
   1607  1.1  mrg /* Return true if DELC doesn't refer to an operator delete that's
   1608  1.1  mrg    suitable to call with a pointer returned from the operator new
   1609  1.1  mrg    described by NEWC.  */
   1610  1.1  mrg 
   1611  1.1  mrg static bool
   1612  1.1  mrg new_delete_mismatch_p (const demangle_component &newc,
   1613  1.1  mrg 		       const demangle_component &delc)
   1614  1.1  mrg {
   1615  1.1  mrg   if (newc.type != delc.type)
   1616  1.1  mrg     return true;
   1617  1.1  mrg 
   1618  1.1  mrg   switch (newc.type)
   1619  1.1  mrg     {
   1620  1.1  mrg     case DEMANGLE_COMPONENT_NAME:
   1621  1.1  mrg       {
   1622  1.1  mrg 	int len = newc.u.s_name.len;
   1623  1.1  mrg 	const char *news = newc.u.s_name.s;
   1624  1.1  mrg 	const char *dels = delc.u.s_name.s;
   1625  1.1  mrg 	if (len != delc.u.s_name.len || memcmp (news, dels, len))
   1626  1.1  mrg 	  return true;
   1627  1.1  mrg 
   1628  1.1  mrg 	if (news[len] == 'n')
   1629  1.1  mrg 	  {
   1630  1.1  mrg 	    if (news[len + 1] == 'a')
   1631  1.1  mrg 	      return dels[len] != 'd' || dels[len + 1] != 'a';
   1632  1.1  mrg 	    if (news[len + 1] == 'w')
   1633  1.1  mrg 	      return dels[len] != 'd' || dels[len + 1] != 'l';
   1634  1.1  mrg 	  }
   1635  1.1  mrg 	return false;
   1636  1.1  mrg       }
   1637  1.1  mrg 
   1638  1.1  mrg     case DEMANGLE_COMPONENT_OPERATOR:
   1639  1.1  mrg       /* Operator mismatches are handled above.  */
   1640  1.1  mrg       return false;
   1641  1.1  mrg 
   1642  1.1  mrg     case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
   1643  1.1  mrg       if (newc.u.s_extended_operator.args != delc.u.s_extended_operator.args)
   1644  1.1  mrg 	return true;
   1645  1.1  mrg       return new_delete_mismatch_p (*newc.u.s_extended_operator.name,
   1646  1.1  mrg 				    *delc.u.s_extended_operator.name);
   1647  1.1  mrg 
   1648  1.1  mrg     case DEMANGLE_COMPONENT_FIXED_TYPE:
   1649  1.1  mrg       if (newc.u.s_fixed.accum != delc.u.s_fixed.accum
   1650  1.1  mrg 	  || newc.u.s_fixed.sat != delc.u.s_fixed.sat)
   1651  1.1  mrg 	return true;
   1652  1.1  mrg       return new_delete_mismatch_p (*newc.u.s_fixed.length,
   1653  1.1  mrg 				    *delc.u.s_fixed.length);
   1654  1.1  mrg 
   1655  1.1  mrg     case DEMANGLE_COMPONENT_CTOR:
   1656  1.1  mrg       if (newc.u.s_ctor.kind != delc.u.s_ctor.kind)
   1657  1.1  mrg 	return true;
   1658  1.1  mrg       return new_delete_mismatch_p (*newc.u.s_ctor.name,
   1659  1.1  mrg 				    *delc.u.s_ctor.name);
   1660  1.1  mrg 
   1661  1.1  mrg     case DEMANGLE_COMPONENT_DTOR:
   1662  1.1  mrg       if (newc.u.s_dtor.kind != delc.u.s_dtor.kind)
   1663  1.1  mrg 	return true;
   1664  1.1  mrg       return new_delete_mismatch_p (*newc.u.s_dtor.name,
   1665  1.1  mrg 				    *delc.u.s_dtor.name);
   1666  1.1  mrg 
   1667  1.1  mrg     case DEMANGLE_COMPONENT_BUILTIN_TYPE:
   1668  1.1  mrg       {
   1669  1.1  mrg 	/* The demangler API provides no better way to compare built-in
   1670  1.1  mrg 	   types except to by comparing their demangled names. */
   1671  1.1  mrg 	size_t nsz, dsz;
   1672  1.1  mrg 	demangle_component *pnc = const_cast<demangle_component *>(&newc);
   1673  1.1  mrg 	demangle_component *pdc = const_cast<demangle_component *>(&delc);
   1674  1.1  mrg 	char *nts = cplus_demangle_print (0, pnc, 16, &nsz);
   1675  1.1  mrg 	char *dts = cplus_demangle_print (0, pdc, 16, &dsz);
   1676  1.1  mrg 	if (!nts != !dts)
   1677  1.1  mrg 	  return true;
   1678  1.1  mrg 	bool mismatch = strcmp (nts, dts);
   1679  1.1  mrg 	free (nts);
   1680  1.1  mrg 	free (dts);
   1681  1.1  mrg 	return mismatch;
   1682  1.1  mrg       }
   1683  1.1  mrg 
   1684  1.1  mrg     case DEMANGLE_COMPONENT_SUB_STD:
   1685  1.1  mrg       if (newc.u.s_string.len != delc.u.s_string.len)
   1686  1.1  mrg 	return true;
   1687  1.1  mrg       return memcmp (newc.u.s_string.string, delc.u.s_string.string,
   1688  1.1  mrg 		     newc.u.s_string.len);
   1689  1.1  mrg 
   1690  1.1  mrg     case DEMANGLE_COMPONENT_FUNCTION_PARAM:
   1691  1.1  mrg     case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
   1692  1.1  mrg     case DEMANGLE_COMPONENT_UNNAMED_TYPE:
   1693  1.1  mrg       return newc.u.s_number.number != delc.u.s_number.number;
   1694  1.1  mrg 
   1695  1.1  mrg     case DEMANGLE_COMPONENT_CHARACTER:
   1696  1.1  mrg       return newc.u.s_character.character != delc.u.s_character.character;
   1697  1.1  mrg 
   1698  1.1  mrg     case DEMANGLE_COMPONENT_DEFAULT_ARG:
   1699  1.1  mrg     case DEMANGLE_COMPONENT_LAMBDA:
   1700  1.1  mrg       if (newc.u.s_unary_num.num != delc.u.s_unary_num.num)
   1701  1.1  mrg 	return true;
   1702  1.1  mrg       return new_delete_mismatch_p (*newc.u.s_unary_num.sub,
   1703  1.1  mrg 				    *delc.u.s_unary_num.sub);
   1704  1.1  mrg     default:
   1705  1.1  mrg       break;
   1706  1.1  mrg     }
   1707  1.1  mrg 
   1708  1.1  mrg   if (!newc.u.s_binary.left != !delc.u.s_binary.left)
   1709  1.1  mrg     return true;
   1710  1.1  mrg 
   1711  1.1  mrg   if (!newc.u.s_binary.left)
   1712  1.1  mrg     return false;
   1713  1.1  mrg 
   1714  1.1  mrg   if (new_delete_mismatch_p (*newc.u.s_binary.left, *delc.u.s_binary.left)
   1715  1.1  mrg       || !newc.u.s_binary.right != !delc.u.s_binary.right)
   1716  1.1  mrg     return true;
   1717  1.1  mrg 
   1718  1.1  mrg   if (newc.u.s_binary.right)
   1719  1.1  mrg     return new_delete_mismatch_p (*newc.u.s_binary.right,
   1720  1.1  mrg 				  *delc.u.s_binary.right);
   1721  1.1  mrg   return false;
   1722  1.1  mrg }
   1723  1.1  mrg 
   1724  1.1  mrg /* Return true if DELETE_DECL is an operator delete that's not suitable
   1725  1.1  mrg    to call with a pointer returned from NEW_DECL.  */
   1726  1.1  mrg 
   1727  1.1  mrg static bool
   1728  1.1  mrg new_delete_mismatch_p (tree new_decl, tree delete_decl)
   1729  1.1  mrg {
   1730  1.1  mrg   tree new_name = DECL_ASSEMBLER_NAME (new_decl);
   1731  1.1  mrg   tree delete_name = DECL_ASSEMBLER_NAME (delete_decl);
   1732  1.1  mrg 
   1733  1.1  mrg   /* valid_new_delete_pair_p() returns a conservative result (currently
   1734  1.1  mrg      it only handles global operators).  A true result is reliable but
   1735  1.1  mrg      a false result doesn't necessarily mean the operators don't match
   1736  1.1  mrg      unless CERTAIN is set.  */
   1737  1.1  mrg   bool certain;
   1738  1.1  mrg   if (valid_new_delete_pair_p (new_name, delete_name, &certain))
   1739  1.1  mrg     return false;
   1740  1.1  mrg   /* CERTAIN is set when the negative result is certain.  */
   1741  1.1  mrg   if (certain)
   1742  1.1  mrg     return true;
   1743  1.1  mrg 
   1744  1.1  mrg   /* For anything not handled by valid_new_delete_pair_p() such as member
   1745  1.1  mrg      operators compare the individual demangled components of the mangled
   1746  1.1  mrg      name.  */
   1747  1.1  mrg   const char *new_str = IDENTIFIER_POINTER (new_name);
   1748  1.1  mrg   const char *del_str = IDENTIFIER_POINTER (delete_name);
   1749  1.1  mrg 
   1750  1.1  mrg   void *np = NULL, *dp = NULL;
   1751  1.1  mrg   demangle_component *ndc = cplus_demangle_v3_components (new_str, 0, &np);
   1752  1.1  mrg   demangle_component *ddc = cplus_demangle_v3_components (del_str, 0, &dp);
   1753  1.1  mrg   bool mismatch = new_delete_mismatch_p (*ndc, *ddc);
   1754  1.1  mrg   free (np);
   1755  1.1  mrg   free (dp);
   1756  1.1  mrg   return mismatch;
   1757  1.1  mrg }
   1758  1.1  mrg 
   1759  1.1  mrg /* ALLOC_DECL and DEALLOC_DECL are pair of allocation and deallocation
   1760  1.1  mrg    functions.  Return true if the latter is suitable to deallocate objects
   1761  1.1  mrg    allocated by calls to the former.  */
   1762  1.1  mrg 
   1763  1.1  mrg static bool
   1764  1.1  mrg matching_alloc_calls_p (tree alloc_decl, tree dealloc_decl)
   1765  1.1  mrg {
   1766  1.1  mrg   /* Set to alloc_kind_t::builtin if ALLOC_DECL is associated with
   1767  1.1  mrg      a built-in deallocator.  */
   1768  1.1  mrg   enum class alloc_kind_t { none, builtin, user }
   1769  1.1  mrg   alloc_dealloc_kind = alloc_kind_t::none;
   1770  1.1  mrg 
   1771  1.1  mrg   if (DECL_IS_OPERATOR_NEW_P (alloc_decl))
   1772  1.1  mrg     {
   1773  1.1  mrg       if (DECL_IS_OPERATOR_DELETE_P (dealloc_decl))
   1774  1.1  mrg 	/* Return true iff both functions are of the same array or
   1775  1.1  mrg 	   singleton form and false otherwise.  */
   1776  1.1  mrg 	return !new_delete_mismatch_p (alloc_decl, dealloc_decl);
   1777  1.1  mrg 
   1778  1.1  mrg       /* Return false for deallocation functions that are known not
   1779  1.1  mrg 	 to match.  */
   1780  1.1  mrg       if (fndecl_built_in_p (dealloc_decl, BUILT_IN_FREE)
   1781  1.1  mrg 	  || fndecl_built_in_p (dealloc_decl, BUILT_IN_REALLOC))
   1782  1.1  mrg 	return false;
   1783  1.1  mrg       /* Otherwise proceed below to check the deallocation function's
   1784  1.1  mrg 	 "*dealloc" attributes to look for one that mentions this operator
   1785  1.1  mrg 	 new.  */
   1786  1.1  mrg     }
   1787  1.1  mrg   else if (fndecl_built_in_p (alloc_decl, BUILT_IN_NORMAL))
   1788  1.1  mrg     {
   1789  1.1  mrg       switch (DECL_FUNCTION_CODE (alloc_decl))
   1790  1.1  mrg 	{
   1791  1.1  mrg 	case BUILT_IN_ALLOCA:
   1792  1.1  mrg 	case BUILT_IN_ALLOCA_WITH_ALIGN:
   1793  1.1  mrg 	  return false;
   1794  1.1  mrg 
   1795  1.1  mrg 	case BUILT_IN_ALIGNED_ALLOC:
   1796  1.1  mrg 	case BUILT_IN_CALLOC:
   1797  1.1  mrg 	case BUILT_IN_GOMP_ALLOC:
   1798  1.1  mrg 	case BUILT_IN_MALLOC:
   1799  1.1  mrg 	case BUILT_IN_REALLOC:
   1800  1.1  mrg 	case BUILT_IN_STRDUP:
   1801  1.1  mrg 	case BUILT_IN_STRNDUP:
   1802  1.1  mrg 	  if (DECL_IS_OPERATOR_DELETE_P (dealloc_decl))
   1803  1.1  mrg 	    return false;
   1804  1.1  mrg 
   1805  1.1  mrg 	  if (fndecl_built_in_p (dealloc_decl, BUILT_IN_FREE)
   1806  1.1  mrg 	      || fndecl_built_in_p (dealloc_decl, BUILT_IN_REALLOC))
   1807  1.1  mrg 	    return true;
   1808  1.1  mrg 
   1809  1.1  mrg 	  alloc_dealloc_kind = alloc_kind_t::builtin;
   1810  1.1  mrg 	  break;
   1811  1.1  mrg 
   1812  1.1  mrg 	default:
   1813  1.1  mrg 	  break;
   1814  1.1  mrg 	}
   1815  1.1  mrg     }
   1816  1.1  mrg 
   1817  1.1  mrg   /* Set if DEALLOC_DECL both allocates and deallocates.  */
   1818  1.1  mrg   alloc_kind_t realloc_kind = alloc_kind_t::none;
   1819  1.1  mrg 
   1820  1.1  mrg   if (fndecl_built_in_p (dealloc_decl, BUILT_IN_NORMAL))
   1821  1.1  mrg     {
   1822  1.1  mrg       built_in_function dealloc_code = DECL_FUNCTION_CODE (dealloc_decl);
   1823  1.1  mrg       if (dealloc_code == BUILT_IN_REALLOC)
   1824  1.1  mrg 	realloc_kind = alloc_kind_t::builtin;
   1825  1.1  mrg 
   1826  1.1  mrg       for (tree amats = DECL_ATTRIBUTES (alloc_decl);
   1827  1.1  mrg 	   (amats = lookup_attribute ("malloc", amats));
   1828  1.1  mrg 	   amats = TREE_CHAIN (amats))
   1829  1.1  mrg 	{
   1830  1.1  mrg 	  tree args = TREE_VALUE (amats);
   1831  1.1  mrg 	  if (!args)
   1832  1.1  mrg 	    continue;
   1833  1.1  mrg 
   1834  1.1  mrg 	  tree fndecl = TREE_VALUE (args);
   1835  1.1  mrg 	  if (!fndecl || !DECL_P (fndecl))
   1836  1.1  mrg 	    continue;
   1837  1.1  mrg 
   1838  1.1  mrg 	  if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
   1839  1.1  mrg 	      && dealloc_code == DECL_FUNCTION_CODE (fndecl))
   1840  1.1  mrg 	    return true;
   1841  1.1  mrg 	}
   1842  1.1  mrg     }
   1843  1.1  mrg 
   1844  1.1  mrg   const bool alloc_builtin = fndecl_built_in_p (alloc_decl, BUILT_IN_NORMAL);
   1845  1.1  mrg   alloc_kind_t realloc_dealloc_kind = alloc_kind_t::none;
   1846  1.1  mrg 
   1847  1.1  mrg   /* If DEALLOC_DECL has an internal "*dealloc" attribute scan the list
   1848  1.1  mrg      of its associated allocation functions for ALLOC_DECL.
   1849  1.1  mrg      If the corresponding ALLOC_DECL is found they're a matching pair,
   1850  1.1  mrg      otherwise they're not.
   1851  1.1  mrg      With DDATS set to the Deallocator's *Dealloc ATtributes...  */
   1852  1.1  mrg   for (tree ddats = DECL_ATTRIBUTES (dealloc_decl);
   1853  1.1  mrg        (ddats = lookup_attribute ("*dealloc", ddats));
   1854  1.1  mrg        ddats = TREE_CHAIN (ddats))
   1855  1.1  mrg     {
   1856  1.1  mrg       tree args = TREE_VALUE (ddats);
   1857  1.1  mrg       if (!args)
   1858  1.1  mrg 	continue;
   1859  1.1  mrg 
   1860  1.1  mrg       tree alloc = TREE_VALUE (args);
   1861  1.1  mrg       if (!alloc)
   1862  1.1  mrg 	continue;
   1863  1.1  mrg 
   1864  1.1  mrg       if (alloc == DECL_NAME (dealloc_decl))
   1865  1.1  mrg 	realloc_kind = alloc_kind_t::user;
   1866  1.1  mrg 
   1867  1.1  mrg       if (DECL_P (alloc))
   1868  1.1  mrg 	{
   1869  1.1  mrg 	  gcc_checking_assert (fndecl_built_in_p (alloc, BUILT_IN_NORMAL));
   1870  1.1  mrg 
   1871  1.1  mrg 	  switch (DECL_FUNCTION_CODE (alloc))
   1872  1.1  mrg 	    {
   1873  1.1  mrg 	    case BUILT_IN_ALIGNED_ALLOC:
   1874  1.1  mrg 	    case BUILT_IN_CALLOC:
   1875  1.1  mrg 	    case BUILT_IN_GOMP_ALLOC:
   1876  1.1  mrg 	    case BUILT_IN_MALLOC:
   1877  1.1  mrg 	    case BUILT_IN_REALLOC:
   1878  1.1  mrg 	    case BUILT_IN_STRDUP:
   1879  1.1  mrg 	    case BUILT_IN_STRNDUP:
   1880  1.1  mrg 	      realloc_dealloc_kind = alloc_kind_t::builtin;
   1881  1.1  mrg 	      break;
   1882  1.1  mrg 	    default:
   1883  1.1  mrg 	      break;
   1884  1.1  mrg 	    }
   1885  1.1  mrg 
   1886  1.1  mrg 	  if (!alloc_builtin)
   1887  1.1  mrg 	    continue;
   1888  1.1  mrg 
   1889  1.1  mrg 	  if (DECL_FUNCTION_CODE (alloc) != DECL_FUNCTION_CODE (alloc_decl))
   1890  1.1  mrg 	    continue;
   1891  1.1  mrg 
   1892  1.1  mrg 	  return true;
   1893  1.1  mrg 	}
   1894  1.1  mrg 
   1895  1.1  mrg       if (alloc == DECL_NAME (alloc_decl))
   1896  1.1  mrg 	return true;
   1897  1.1  mrg     }
   1898  1.1  mrg 
   1899  1.1  mrg   if (realloc_kind == alloc_kind_t::none)
   1900  1.1  mrg     return false;
   1901  1.1  mrg 
   1902  1.1  mrg   hash_set<tree> common_deallocs;
   1903  1.1  mrg   /* Special handling for deallocators.  Iterate over both the allocator's
   1904  1.1  mrg      and the reallocator's associated deallocator functions looking for
   1905  1.1  mrg      the first one in common.  If one is found, the de/reallocator is
   1906  1.1  mrg      a match for the allocator even though the latter isn't directly
   1907  1.1  mrg      associated with the former.  This simplifies declarations in system
   1908  1.1  mrg      headers.
   1909  1.1  mrg      With AMATS set to the Allocator's Malloc ATtributes,
   1910  1.1  mrg      and  RMATS set to Reallocator's Malloc ATtributes...  */
   1911  1.1  mrg   for (tree amats = DECL_ATTRIBUTES (alloc_decl);
   1912  1.1  mrg        (amats = lookup_attribute ("malloc", amats));
   1913  1.1  mrg        amats = amats ? TREE_CHAIN (amats) : NULL_TREE)
   1914  1.1  mrg     if (tree args = amats ? TREE_VALUE (amats) : NULL_TREE)
   1915  1.1  mrg       if (tree adealloc = TREE_VALUE (args))
   1916  1.1  mrg 	{
   1917  1.1  mrg 	  if (DECL_P (adealloc)
   1918  1.1  mrg 	      && fndecl_built_in_p (adealloc, BUILT_IN_NORMAL))
   1919  1.1  mrg 	    {
   1920  1.1  mrg 	      built_in_function fncode = DECL_FUNCTION_CODE (adealloc);
   1921  1.1  mrg 	      if (fncode == BUILT_IN_FREE || fncode == BUILT_IN_REALLOC)
   1922  1.1  mrg 		{
   1923  1.1  mrg 		  if (realloc_kind == alloc_kind_t::builtin)
   1924  1.1  mrg 		    return true;
   1925  1.1  mrg 		  alloc_dealloc_kind = alloc_kind_t::builtin;
   1926  1.1  mrg 		}
   1927  1.1  mrg 	      continue;
   1928  1.1  mrg 	    }
   1929  1.1  mrg 
   1930  1.1  mrg 	  common_deallocs.add (adealloc);
   1931  1.1  mrg 	}
   1932  1.1  mrg   for (tree rmats = DECL_ATTRIBUTES (dealloc_decl);
   1933  1.1  mrg        (rmats = lookup_attribute ("malloc", rmats));
   1934  1.1  mrg        rmats = rmats ? TREE_CHAIN (rmats) : NULL_TREE)
   1935  1.1  mrg     if (tree args = rmats ? TREE_VALUE (rmats) : NULL_TREE)
   1936  1.1  mrg       if (tree ddealloc = TREE_VALUE (args))
   1937  1.1  mrg 	{
   1938  1.1  mrg 	  if (DECL_P (ddealloc)
   1939  1.1  mrg 	      && fndecl_built_in_p (ddealloc, BUILT_IN_NORMAL))
   1940  1.1  mrg 	    {
   1941  1.1  mrg 	      built_in_function fncode = DECL_FUNCTION_CODE (ddealloc);
   1942  1.1  mrg 	      if (fncode == BUILT_IN_FREE || fncode == BUILT_IN_REALLOC)
   1943  1.1  mrg 		{
   1944  1.1  mrg 		  if (alloc_dealloc_kind == alloc_kind_t::builtin)
   1945  1.1  mrg 		    return true;
   1946  1.1  mrg 		  realloc_dealloc_kind = alloc_kind_t::builtin;
   1947  1.1  mrg 		}
   1948  1.1  mrg 	      continue;
   1949  1.1  mrg 	    }
   1950  1.1  mrg 
   1951  1.1  mrg 	  if (common_deallocs.contains (ddealloc))
   1952  1.1  mrg 	    return true;
   1953  1.1  mrg 	}
   1954  1.1  mrg 
   1955  1.1  mrg   /* Succeed only if ALLOC_DECL and the reallocator DEALLOC_DECL share
   1956  1.1  mrg      a built-in deallocator.  */
   1957  1.1  mrg   return  (alloc_dealloc_kind == alloc_kind_t::builtin
   1958  1.1  mrg 	   && realloc_dealloc_kind == alloc_kind_t::builtin);
   1959  1.1  mrg }
   1960  1.1  mrg 
   1961  1.1  mrg /* Return true if DEALLOC_DECL is a function suitable to deallocate
   1962  1.1  mrg    objects allocated by the ALLOC call.  */
   1963  1.1  mrg 
   1964  1.1  mrg static bool
   1965  1.1  mrg matching_alloc_calls_p (gimple *alloc, tree dealloc_decl)
   1966  1.1  mrg {
   1967  1.1  mrg   tree alloc_decl = gimple_call_fndecl (alloc);
   1968  1.1  mrg   if (!alloc_decl)
   1969  1.1  mrg     return true;
   1970  1.1  mrg 
   1971  1.1  mrg   return matching_alloc_calls_p (alloc_decl, dealloc_decl);
   1972  1.1  mrg }
   1973  1.1  mrg 
   1974  1.1  mrg /* Diagnose a call EXP to deallocate a pointer referenced by AREF if it
   1975  1.1  mrg    includes a nonzero offset.  Such a pointer cannot refer to the beginning
   1976  1.1  mrg    of an allocated object.  A negative offset may refer to it only if
   1977  1.1  mrg    the target pointer is unknown.  */
   1978  1.1  mrg 
   1979  1.1  mrg static bool
   1980  1.1  mrg warn_dealloc_offset (location_t loc, gimple *call, const access_ref &aref)
   1981  1.1  mrg {
   1982  1.1  mrg   if (aref.deref || aref.offrng[0] <= 0 || aref.offrng[1] <= 0)
   1983  1.1  mrg     return false;
   1984  1.1  mrg 
   1985  1.1  mrg   tree dealloc_decl = gimple_call_fndecl (call);
   1986  1.1  mrg   if (!dealloc_decl)
   1987  1.1  mrg     return false;
   1988  1.1  mrg 
   1989  1.1  mrg   if (DECL_IS_OPERATOR_DELETE_P (dealloc_decl)
   1990  1.1  mrg       && !DECL_IS_REPLACEABLE_OPERATOR (dealloc_decl))
   1991  1.1  mrg     {
   1992  1.1  mrg       /* A call to a user-defined operator delete with a pointer plus offset
   1993  1.1  mrg 	 may be valid if it's returned from an unknown function (i.e., one
   1994  1.1  mrg 	 that's not operator new).  */
   1995  1.1  mrg       if (TREE_CODE (aref.ref) == SSA_NAME)
   1996  1.1  mrg 	{
   1997  1.1  mrg 	  gimple *def_stmt = SSA_NAME_DEF_STMT (aref.ref);
   1998  1.1  mrg 	  if (is_gimple_call (def_stmt))
   1999  1.1  mrg 	    {
   2000  1.1  mrg 	      tree alloc_decl = gimple_call_fndecl (def_stmt);
   2001  1.1  mrg 	      if (!alloc_decl || !DECL_IS_OPERATOR_NEW_P (alloc_decl))
   2002  1.1  mrg 		return false;
   2003  1.1  mrg 	    }
   2004  1.1  mrg 	}
   2005  1.1  mrg     }
   2006  1.1  mrg 
   2007  1.1  mrg   char offstr[80];
   2008  1.1  mrg   offstr[0] = '\0';
   2009  1.1  mrg   if (wi::fits_shwi_p (aref.offrng[0]))
   2010  1.1  mrg     {
   2011  1.1  mrg       if (aref.offrng[0] == aref.offrng[1]
   2012  1.1  mrg 	  || !wi::fits_shwi_p (aref.offrng[1]))
   2013  1.1  mrg 	sprintf (offstr, " %lli",
   2014  1.1  mrg 		 (long long)aref.offrng[0].to_shwi ());
   2015  1.1  mrg       else
   2016  1.1  mrg 	sprintf (offstr, " [%lli, %lli]",
   2017  1.1  mrg 		 (long long)aref.offrng[0].to_shwi (),
   2018  1.1  mrg 		 (long long)aref.offrng[1].to_shwi ());
   2019  1.1  mrg     }
   2020  1.1  mrg 
   2021  1.1  mrg   if (!warning_at (loc, OPT_Wfree_nonheap_object,
   2022  1.1  mrg 		   "%qD called on pointer %qE with nonzero offset%s",
   2023  1.1  mrg 		   dealloc_decl, aref.ref, offstr))
   2024  1.1  mrg     return false;
   2025  1.1  mrg 
   2026  1.1  mrg   if (DECL_P (aref.ref))
   2027  1.1  mrg     inform (get_location (aref.ref), "declared here");
   2028  1.1  mrg   else if (TREE_CODE (aref.ref) == SSA_NAME)
   2029  1.1  mrg     {
   2030  1.1  mrg       gimple *def_stmt = SSA_NAME_DEF_STMT (aref.ref);
   2031  1.1  mrg       if (is_gimple_call (def_stmt))
   2032  1.1  mrg 	{
   2033  1.1  mrg 	  location_t def_loc = get_location (def_stmt);
   2034  1.1  mrg 	  tree alloc_decl = gimple_call_fndecl (def_stmt);
   2035  1.1  mrg 	  if (alloc_decl)
   2036  1.1  mrg 	    inform (def_loc,
   2037  1.1  mrg 		    "returned from %qD", alloc_decl);
   2038  1.1  mrg 	  else if (tree alloc_fntype = gimple_call_fntype (def_stmt))
   2039  1.1  mrg 	    inform (def_loc,
   2040  1.1  mrg 		    "returned from %qT", alloc_fntype);
   2041  1.1  mrg 	  else
   2042  1.1  mrg 	    inform (def_loc,  "obtained here");
   2043  1.1  mrg 	}
   2044  1.1  mrg     }
   2045  1.1  mrg 
   2046  1.1  mrg   return true;
   2047  1.1  mrg }
   2048  1.1  mrg 
   2049  1.1  mrg namespace {
   2050  1.1  mrg 
   2051  1.1  mrg const pass_data pass_data_waccess = {
   2052  1.1  mrg   GIMPLE_PASS,
   2053  1.1  mrg   "waccess",
   2054  1.1  mrg   OPTGROUP_NONE,
   2055  1.1  mrg   TV_WARN_ACCESS, /* timer variable */
   2056  1.1  mrg   PROP_cfg, /* properties_required  */
   2057  1.1  mrg   0,	    /* properties_provided  */
   2058  1.1  mrg   0,	    /* properties_destroyed  */
   2059  1.1  mrg   0,	    /* properties_start */
   2060  1.1  mrg   0,	    /* properties_finish */
   2061  1.1  mrg };
   2062  1.1  mrg 
   2063  1.1  mrg /* Pass to detect invalid accesses.  */
   2064  1.1  mrg class pass_waccess : public gimple_opt_pass
   2065  1.1  mrg {
   2066  1.1  mrg  public:
   2067  1.1  mrg   pass_waccess (gcc::context *);
   2068  1.1  mrg 
   2069  1.1  mrg   ~pass_waccess ();
   2070  1.1  mrg 
   2071  1.1  mrg   opt_pass *clone ();
   2072  1.1  mrg 
   2073  1.1  mrg   virtual bool gate (function *);
   2074  1.1  mrg 
   2075  1.1  mrg   void set_pass_param (unsigned, bool);
   2076  1.1  mrg 
   2077  1.1  mrg   virtual unsigned int execute (function *);
   2078  1.1  mrg 
   2079  1.1  mrg private:
   2080  1.1  mrg   /* Not copyable or assignable.  */
   2081  1.1  mrg   pass_waccess (pass_waccess &) = delete;
   2082  1.1  mrg   void operator= (pass_waccess &) = delete;
   2083  1.1  mrg 
   2084  1.1  mrg   /* Check a call to an atomic built-in function.  */
   2085  1.1  mrg   bool check_atomic_builtin (gcall *);
   2086  1.1  mrg 
   2087  1.1  mrg   /* Check a call to a built-in function.  */
   2088  1.1  mrg   bool check_builtin (gcall *);
   2089  1.1  mrg 
   2090  1.1  mrg   /* Check a call to an ordinary function for invalid accesses.  */
   2091  1.1  mrg   bool check_call_access (gcall *);
   2092  1.1  mrg 
   2093  1.1  mrg   /* Check a non-call statement.  */
   2094  1.1  mrg   void check_stmt (gimple *);
   2095  1.1  mrg 
   2096  1.1  mrg   /* Check statements in a basic block.  */
   2097  1.1  mrg   void check_block (basic_block);
   2098  1.1  mrg 
   2099  1.1  mrg   /* Check a call to a function.  */
   2100  1.1  mrg   void check_call (gcall *);
   2101  1.1  mrg 
   2102  1.1  mrg   /* Check a call to the named built-in function.  */
   2103  1.1  mrg   void check_alloca (gcall *);
   2104  1.1  mrg   void check_alloc_size_call (gcall *);
   2105  1.1  mrg   void check_strcat (gcall *);
   2106  1.1  mrg   void check_strncat (gcall *);
   2107  1.1  mrg   void check_stxcpy (gcall *);
   2108  1.1  mrg   void check_stxncpy (gcall *);
   2109  1.1  mrg   void check_strncmp (gcall *);
   2110  1.1  mrg   void check_memop_access (gimple *, tree, tree, tree);
   2111  1.1  mrg   void check_read_access (gimple *, tree, tree = NULL_TREE, int = 1);
   2112  1.1  mrg 
   2113  1.1  mrg   void maybe_check_dealloc_call (gcall *);
   2114  1.1  mrg   void maybe_check_access_sizes (rdwr_map *, tree, tree, gimple *);
   2115  1.1  mrg   bool maybe_warn_memmodel (gimple *, tree, tree, const unsigned char *);
   2116  1.1  mrg   void check_atomic_memmodel (gimple *, tree, tree, const unsigned char *);
   2117  1.1  mrg 
   2118  1.1  mrg   /* Check for uses of indeterminate pointers.  */
   2119  1.1  mrg   void check_pointer_uses (gimple *, tree, tree = NULL_TREE, bool = false);
   2120  1.1  mrg 
   2121  1.1  mrg   /* Return the argument that a call returns.  */
   2122  1.1  mrg   tree gimple_call_return_arg (gcall *);
   2123  1.1  mrg 
   2124  1.1  mrg   /* Check a call for uses of a dangling pointer arguments.  */
   2125  1.1  mrg   void check_call_dangling (gcall *);
   2126  1.1  mrg 
   2127  1.1  mrg   /* Check uses of a dangling pointer or those derived from it.  */
   2128  1.1  mrg   void check_dangling_uses (tree, tree, bool = false, bool = false);
   2129  1.1  mrg   void check_dangling_uses ();
   2130  1.1  mrg   void check_dangling_stores ();
   2131  1.1  mrg   void check_dangling_stores (basic_block, hash_set<tree> &, auto_bitmap &);
   2132  1.1  mrg 
   2133  1.1  mrg   void warn_invalid_pointer (tree, gimple *, gimple *, tree, bool, bool = false);
   2134  1.1  mrg 
   2135  1.1  mrg   /* Return true if use follows an invalidating statement.  */
   2136  1.1  mrg   bool use_after_inval_p (gimple *, gimple *, bool = false);
   2137  1.1  mrg 
   2138  1.1  mrg   /* A pointer_query object to store information about pointers and
   2139  1.1  mrg      their targets in.  */
   2140  1.1  mrg   pointer_query m_ptr_qry;
   2141  1.1  mrg   /* Mapping from DECLs and their clobber statements in the function.  */
   2142  1.1  mrg   hash_map<tree, gimple *> m_clobbers;
   2143  1.1  mrg   /* A bit is set for each basic block whose statements have been assigned
   2144  1.1  mrg      valid UIDs.  */
   2145  1.1  mrg   bitmap m_bb_uids_set;
   2146  1.1  mrg   /* The current function.  */
   2147  1.1  mrg   function *m_func;
   2148  1.1  mrg   /* True to run checks for uses of dangling pointers.  */
   2149  1.1  mrg   bool m_check_dangling_p;
   2150  1.1  mrg   /* True to run checks early on in the optimization pipeline.  */
   2151  1.1  mrg   bool m_early_checks_p;
   2152  1.1  mrg };
   2153  1.1  mrg 
   2154  1.1  mrg /* Construct the pass.  */
   2155  1.1  mrg 
   2156  1.1  mrg pass_waccess::pass_waccess (gcc::context *ctxt)
   2157  1.1  mrg   : gimple_opt_pass (pass_data_waccess, ctxt),
   2158  1.1  mrg     m_ptr_qry (NULL),
   2159  1.1  mrg     m_clobbers (),
   2160  1.1  mrg     m_bb_uids_set (),
   2161  1.1  mrg     m_func (),
   2162  1.1  mrg     m_check_dangling_p (),
   2163  1.1  mrg     m_early_checks_p ()
   2164  1.1  mrg {
   2165  1.1  mrg }
   2166  1.1  mrg 
   2167  1.1  mrg /* Return a copy of the pass with RUN_NUMBER one greater than THIS.  */
   2168  1.1  mrg 
   2169  1.1  mrg opt_pass*
   2170  1.1  mrg pass_waccess::clone ()
   2171  1.1  mrg {
   2172  1.1  mrg   return new pass_waccess (m_ctxt);
   2173  1.1  mrg }
   2174  1.1  mrg 
   2175  1.1  mrg /* Release pointer_query cache.  */
   2176  1.1  mrg 
   2177  1.1  mrg pass_waccess::~pass_waccess ()
   2178  1.1  mrg {
   2179  1.1  mrg   m_ptr_qry.flush_cache ();
   2180  1.1  mrg }
   2181  1.1  mrg 
   2182  1.1  mrg void
   2183  1.1  mrg pass_waccess::set_pass_param (unsigned int n, bool early)
   2184  1.1  mrg {
   2185  1.1  mrg   gcc_assert (n == 0);
   2186  1.1  mrg 
   2187  1.1  mrg   m_early_checks_p = early;
   2188  1.1  mrg }
   2189  1.1  mrg 
   2190  1.1  mrg /* Return true when any checks performed by the pass are enabled.  */
   2191  1.1  mrg 
   2192  1.1  mrg bool
   2193  1.1  mrg pass_waccess::gate (function *)
   2194  1.1  mrg {
   2195  1.1  mrg   return (warn_free_nonheap_object
   2196  1.1  mrg 	  || warn_mismatched_alloc
   2197  1.1  mrg 	  || warn_mismatched_new_delete);
   2198  1.1  mrg }
   2199  1.1  mrg 
   2200  1.1  mrg /* Initialize ALLOC_OBJECT_SIZE_LIMIT based on the -Walloc-size-larger-than=
   2201  1.1  mrg    setting if the option is specified, or to the maximum object size if it
   2202  1.1  mrg    is not.  Return the initialized value.  */
   2203  1.1  mrg 
   2204  1.1  mrg static tree
   2205  1.1  mrg alloc_max_size (void)
   2206  1.1  mrg {
   2207  1.1  mrg   HOST_WIDE_INT limit = warn_alloc_size_limit;
   2208  1.1  mrg   if (limit == HOST_WIDE_INT_MAX)
   2209  1.1  mrg     limit = tree_to_shwi (TYPE_MAX_VALUE (ptrdiff_type_node));
   2210  1.1  mrg 
   2211  1.1  mrg   return build_int_cst (size_type_node, limit);
   2212  1.1  mrg }
   2213  1.1  mrg 
   2214  1.1  mrg /* Diagnose a call EXP to function FN decorated with attribute alloc_size
   2215  1.1  mrg    whose argument numbers given by IDX with values given by ARGS exceed
   2216  1.1  mrg    the maximum object size or cause an unsigned overflow (wrapping) when
   2217  1.1  mrg    multiplied.  FN is null when EXP is a call via a function pointer.
   2218  1.1  mrg    When ARGS[0] is null the function does nothing.  ARGS[1] may be null
   2219  1.1  mrg    for functions like malloc, and non-null for those like calloc that
   2220  1.1  mrg    are decorated with a two-argument attribute alloc_size.  */
   2221  1.1  mrg 
   2222  1.1  mrg void
   2223  1.1  mrg maybe_warn_alloc_args_overflow (gimple *stmt, const tree args[2],
   2224  1.1  mrg 				const int idx[2])
   2225  1.1  mrg {
   2226  1.1  mrg   /* The range each of the (up to) two arguments is known to be in.  */
   2227  1.1  mrg   tree argrange[2][2] = { { NULL_TREE, NULL_TREE }, { NULL_TREE, NULL_TREE } };
   2228  1.1  mrg 
   2229  1.1  mrg   /* Maximum object size set by -Walloc-size-larger-than= or SIZE_MAX / 2.  */
   2230  1.1  mrg   tree maxobjsize = alloc_max_size ();
   2231  1.1  mrg 
   2232  1.1  mrg   location_t loc = get_location (stmt);
   2233  1.1  mrg 
   2234  1.1  mrg   tree fn = gimple_call_fndecl (stmt);
   2235  1.1  mrg   tree fntype = fn ? TREE_TYPE (fn) : gimple_call_fntype (stmt);
   2236  1.1  mrg   bool warned = false;
   2237  1.1  mrg 
   2238  1.1  mrg   /* Validate each argument individually.  */
   2239  1.1  mrg   for (unsigned i = 0; i != 2 && args[i]; ++i)
   2240  1.1  mrg     {
   2241  1.1  mrg       if (TREE_CODE (args[i]) == INTEGER_CST)
   2242  1.1  mrg 	{
   2243  1.1  mrg 	  argrange[i][0] = args[i];
   2244  1.1  mrg 	  argrange[i][1] = args[i];
   2245  1.1  mrg 
   2246  1.1  mrg 	  if (tree_int_cst_lt (args[i], integer_zero_node))
   2247  1.1  mrg 	    {
   2248  1.1  mrg 	      warned = warning_at (loc, OPT_Walloc_size_larger_than_,
   2249  1.1  mrg 				   "argument %i value %qE is negative",
   2250  1.1  mrg 				   idx[i] + 1, args[i]);
   2251  1.1  mrg 	    }
   2252  1.1  mrg 	  else if (integer_zerop (args[i]))
   2253  1.1  mrg 	    {
   2254  1.1  mrg 	      /* Avoid issuing -Walloc-zero for allocation functions other
   2255  1.1  mrg 		 than __builtin_alloca that are declared with attribute
   2256  1.1  mrg 		 returns_nonnull because there's no portability risk.  This
   2257  1.1  mrg 		 avoids warning for such calls to libiberty's xmalloc and
   2258  1.1  mrg 		 friends.
   2259  1.1  mrg 		 Also avoid issuing the warning for calls to function named
   2260  1.1  mrg 		 "alloca".  */
   2261  1.1  mrg 	      if (fn && fndecl_built_in_p (fn, BUILT_IN_ALLOCA)
   2262  1.1  mrg 		  ? IDENTIFIER_LENGTH (DECL_NAME (fn)) != 6
   2263  1.1  mrg 		  : !lookup_attribute ("returns_nonnull",
   2264  1.1  mrg 				       TYPE_ATTRIBUTES (fntype)))
   2265  1.1  mrg 		warned = warning_at (loc, OPT_Walloc_zero,
   2266  1.1  mrg 				     "argument %i value is zero",
   2267  1.1  mrg 				     idx[i] + 1);
   2268  1.1  mrg 	    }
   2269  1.1  mrg 	  else if (tree_int_cst_lt (maxobjsize, args[i]))
   2270  1.1  mrg 	    {
   2271  1.1  mrg 	      /* G++ emits calls to ::operator new[](SIZE_MAX) in C++98
   2272  1.1  mrg 		 mode and with -fno-exceptions as a way to indicate array
   2273  1.1  mrg 		 size overflow.  There's no good way to detect C++98 here
   2274  1.1  mrg 		 so avoid diagnosing these calls for all C++ modes.  */
   2275  1.1  mrg 	      if (i == 0
   2276  1.1  mrg 		  && fn
   2277  1.1  mrg 		  && !args[1]
   2278  1.1  mrg 		  && lang_GNU_CXX ()
   2279  1.1  mrg 		  && DECL_IS_OPERATOR_NEW_P (fn)
   2280  1.1  mrg 		  && integer_all_onesp (args[i]))
   2281  1.1  mrg 		continue;
   2282  1.1  mrg 
   2283  1.1  mrg 	      warned = warning_at (loc, OPT_Walloc_size_larger_than_,
   2284  1.1  mrg 				   "argument %i value %qE exceeds "
   2285  1.1  mrg 				   "maximum object size %E",
   2286  1.1  mrg 				   idx[i] + 1, args[i], maxobjsize);
   2287  1.1  mrg 	    }
   2288  1.1  mrg 	}
   2289  1.1  mrg       else if (TREE_CODE (args[i]) == SSA_NAME
   2290  1.1  mrg 	       && get_size_range (args[i], argrange[i]))
   2291  1.1  mrg 	{
   2292  1.1  mrg 	  /* Verify that the argument's range is not negative (including
   2293  1.1  mrg 	     upper bound of zero).  */
   2294  1.1  mrg 	  if (tree_int_cst_lt (argrange[i][0], integer_zero_node)
   2295  1.1  mrg 	      && tree_int_cst_le (argrange[i][1], integer_zero_node))
   2296  1.1  mrg 	    {
   2297  1.1  mrg 	      warned = warning_at (loc, OPT_Walloc_size_larger_than_,
   2298  1.1  mrg 				   "argument %i range [%E, %E] is negative",
   2299  1.1  mrg 				   idx[i] + 1,
   2300  1.1  mrg 				   argrange[i][0], argrange[i][1]);
   2301  1.1  mrg 	    }
   2302  1.1  mrg 	  else if (tree_int_cst_lt (maxobjsize, argrange[i][0]))
   2303  1.1  mrg 	    {
   2304  1.1  mrg 	      warned = warning_at (loc, OPT_Walloc_size_larger_than_,
   2305  1.1  mrg 				   "argument %i range [%E, %E] exceeds "
   2306  1.1  mrg 				   "maximum object size %E",
   2307  1.1  mrg 				   idx[i] + 1,
   2308  1.1  mrg 				   argrange[i][0], argrange[i][1],
   2309  1.1  mrg 				   maxobjsize);
   2310  1.1  mrg 	    }
   2311  1.1  mrg 	}
   2312  1.1  mrg     }
   2313  1.1  mrg 
   2314  1.1  mrg   if (!argrange[0][0])
   2315  1.1  mrg     return;
   2316  1.1  mrg 
   2317  1.1  mrg   /* For a two-argument alloc_size, validate the product of the two
   2318  1.1  mrg      arguments if both of their values or ranges are known.  */
   2319  1.1  mrg   if (!warned && tree_fits_uhwi_p (argrange[0][0])
   2320  1.1  mrg       && argrange[1][0] && tree_fits_uhwi_p (argrange[1][0])
   2321  1.1  mrg       && !integer_onep (argrange[0][0])
   2322  1.1  mrg       && !integer_onep (argrange[1][0]))
   2323  1.1  mrg     {
   2324  1.1  mrg       /* Check for overflow in the product of a function decorated with
   2325  1.1  mrg 	 attribute alloc_size (X, Y).  */
   2326  1.1  mrg       unsigned szprec = TYPE_PRECISION (size_type_node);
   2327  1.1  mrg       wide_int x = wi::to_wide (argrange[0][0], szprec);
   2328  1.1  mrg       wide_int y = wi::to_wide (argrange[1][0], szprec);
   2329  1.1  mrg 
   2330  1.1  mrg       wi::overflow_type vflow;
   2331  1.1  mrg       wide_int prod = wi::umul (x, y, &vflow);
   2332  1.1  mrg 
   2333  1.1  mrg       if (vflow)
   2334  1.1  mrg 	warned = warning_at (loc, OPT_Walloc_size_larger_than_,
   2335  1.1  mrg 			     "product %<%E * %E%> of arguments %i and %i "
   2336  1.1  mrg 			     "exceeds %<SIZE_MAX%>",
   2337  1.1  mrg 			     argrange[0][0], argrange[1][0],
   2338  1.1  mrg 			     idx[0] + 1, idx[1] + 1);
   2339  1.1  mrg       else if (wi::ltu_p (wi::to_wide (maxobjsize, szprec), prod))
   2340  1.1  mrg 	warned = warning_at (loc, OPT_Walloc_size_larger_than_,
   2341  1.1  mrg 			     "product %<%E * %E%> of arguments %i and %i "
   2342  1.1  mrg 			     "exceeds maximum object size %E",
   2343  1.1  mrg 			     argrange[0][0], argrange[1][0],
   2344  1.1  mrg 			     idx[0] + 1, idx[1] + 1,
   2345  1.1  mrg 			     maxobjsize);
   2346  1.1  mrg 
   2347  1.1  mrg       if (warned)
   2348  1.1  mrg 	{
   2349  1.1  mrg 	  /* Print the full range of each of the two arguments to make
   2350  1.1  mrg 	     it clear when it is, in fact, in a range and not constant.  */
   2351  1.1  mrg 	  if (argrange[0][0] != argrange [0][1])
   2352  1.1  mrg 	    inform (loc, "argument %i in the range [%E, %E]",
   2353  1.1  mrg 		    idx[0] + 1, argrange[0][0], argrange[0][1]);
   2354  1.1  mrg 	  if (argrange[1][0] != argrange [1][1])
   2355  1.1  mrg 	    inform (loc, "argument %i in the range [%E, %E]",
   2356  1.1  mrg 		    idx[1] + 1, argrange[1][0], argrange[1][1]);
   2357  1.1  mrg 	}
   2358  1.1  mrg     }
   2359  1.1  mrg 
   2360  1.1  mrg   if (warned && fn)
   2361  1.1  mrg     {
   2362  1.1  mrg       location_t fnloc = DECL_SOURCE_LOCATION (fn);
   2363  1.1  mrg 
   2364  1.1  mrg       if (DECL_IS_UNDECLARED_BUILTIN (fn))
   2365  1.1  mrg 	inform (loc,
   2366  1.1  mrg 		"in a call to built-in allocation function %qD", fn);
   2367  1.1  mrg       else
   2368  1.1  mrg 	inform (fnloc,
   2369  1.1  mrg 		"in a call to allocation function %qD declared here", fn);
   2370  1.1  mrg     }
   2371  1.1  mrg }
   2372  1.1  mrg 
   2373  1.1  mrg /* Check a call to an alloca function for an excessive size.  */
   2374  1.1  mrg 
   2375  1.1  mrg void
   2376  1.1  mrg pass_waccess::check_alloca (gcall *stmt)
   2377  1.1  mrg {
   2378  1.1  mrg   if (m_early_checks_p)
   2379  1.1  mrg     return;
   2380  1.1  mrg 
   2381  1.1  mrg   if ((warn_vla_limit >= HOST_WIDE_INT_MAX
   2382  1.1  mrg        && warn_alloc_size_limit < warn_vla_limit)
   2383  1.1  mrg       || (warn_alloca_limit >= HOST_WIDE_INT_MAX
   2384  1.1  mrg 	  && warn_alloc_size_limit < warn_alloca_limit))
   2385  1.1  mrg     {
   2386  1.1  mrg       /* -Walloca-larger-than and -Wvla-larger-than settings of less
   2387  1.1  mrg 	 than  HWI_MAX override the more general -Walloc-size-larger-than
   2388  1.1  mrg 	 so unless either of the former options is smaller than the last
   2389  1.1  mrg 	 one (which would imply that the call was already checked), check
   2390  1.1  mrg 	 the alloca arguments for overflow.  */
   2391  1.1  mrg       const tree alloc_args[] = { call_arg (stmt, 0), NULL_TREE };
   2392  1.1  mrg       const int idx[] = { 0, -1 };
   2393  1.1  mrg       maybe_warn_alloc_args_overflow (stmt, alloc_args, idx);
   2394  1.1  mrg     }
   2395  1.1  mrg }
   2396  1.1  mrg 
   2397  1.1  mrg /* Check a call to an allocation function for an excessive size.  */
   2398  1.1  mrg 
   2399  1.1  mrg void
   2400  1.1  mrg pass_waccess::check_alloc_size_call (gcall *stmt)
   2401  1.1  mrg {
   2402  1.1  mrg   if (m_early_checks_p)
   2403  1.1  mrg     return;
   2404  1.1  mrg 
   2405  1.1  mrg   if (gimple_call_num_args (stmt) < 1)
   2406  1.1  mrg     /* Avoid invalid calls to functions without a prototype.  */
   2407  1.1  mrg     return;
   2408  1.1  mrg 
   2409  1.1  mrg   tree fndecl = gimple_call_fndecl (stmt);
   2410  1.1  mrg   if (fndecl && gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
   2411  1.1  mrg     {
   2412  1.1  mrg       /* Alloca is handled separately.  */
   2413  1.1  mrg       switch (DECL_FUNCTION_CODE (fndecl))
   2414  1.1  mrg 	{
   2415  1.1  mrg 	case BUILT_IN_ALLOCA:
   2416  1.1  mrg 	case BUILT_IN_ALLOCA_WITH_ALIGN:
   2417  1.1  mrg 	case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX:
   2418  1.1  mrg 	  return;
   2419  1.1  mrg 	default:
   2420  1.1  mrg 	  break;
   2421  1.1  mrg 	}
   2422  1.1  mrg     }
   2423  1.1  mrg 
   2424  1.1  mrg   tree fntype = gimple_call_fntype (stmt);
   2425  1.1  mrg   tree fntypeattrs = TYPE_ATTRIBUTES (fntype);
   2426  1.1  mrg 
   2427  1.1  mrg   tree alloc_size = lookup_attribute ("alloc_size", fntypeattrs);
   2428  1.1  mrg   if (!alloc_size)
   2429  1.1  mrg     return;
   2430  1.1  mrg 
   2431  1.1  mrg   /* Extract attribute alloc_size from the type of the called expression
   2432  1.1  mrg      (which could be a function or a function pointer) and if set, store
   2433  1.1  mrg      the indices of the corresponding arguments in ALLOC_IDX, and then
   2434  1.1  mrg      the actual argument(s) at those indices in ALLOC_ARGS.  */
   2435  1.1  mrg   int idx[2] = { -1, -1 };
   2436  1.1  mrg   tree alloc_args[] = { NULL_TREE, NULL_TREE };
   2437  1.1  mrg   unsigned nargs = gimple_call_num_args (stmt);
   2438  1.1  mrg 
   2439  1.1  mrg   tree args = TREE_VALUE (alloc_size);
   2440  1.1  mrg   idx[0] = TREE_INT_CST_LOW (TREE_VALUE (args)) - 1;
   2441  1.1  mrg   /* Avoid invalid calls to functions without a prototype.  */
   2442  1.1  mrg   if ((unsigned) idx[0] >= nargs)
   2443  1.1  mrg     return;
   2444  1.1  mrg   alloc_args[0] = call_arg (stmt, idx[0]);
   2445  1.1  mrg   if (TREE_CHAIN (args))
   2446  1.1  mrg     {
   2447  1.1  mrg       idx[1] = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (args))) - 1;
   2448  1.1  mrg       if ((unsigned) idx[1] >= nargs)
   2449  1.1  mrg 	return;
   2450  1.1  mrg       alloc_args[1] = call_arg (stmt, idx[1]);
   2451  1.1  mrg     }
   2452  1.1  mrg 
   2453  1.1  mrg   maybe_warn_alloc_args_overflow (stmt, alloc_args, idx);
   2454  1.1  mrg }
   2455  1.1  mrg 
   2456  1.1  mrg /* Check a call STMT to strcat() for overflow and warn if it does.  */
   2457  1.1  mrg 
   2458  1.1  mrg void
   2459  1.1  mrg pass_waccess::check_strcat (gcall *stmt)
   2460  1.1  mrg {
   2461  1.1  mrg   if (m_early_checks_p)
   2462  1.1  mrg     return;
   2463  1.1  mrg 
   2464  1.1  mrg   if (!warn_stringop_overflow && !warn_stringop_overread)
   2465  1.1  mrg     return;
   2466  1.1  mrg 
   2467  1.1  mrg   tree dest = call_arg (stmt, 0);
   2468  1.1  mrg   tree src = call_arg (stmt, 1);
   2469  1.1  mrg 
   2470  1.1  mrg   /* There is no way here to determine the length of the string in
   2471  1.1  mrg      the destination to which the SRC string is being appended so
   2472  1.1  mrg      just diagnose cases when the source string is longer than
   2473  1.1  mrg      the destination object.  */
   2474  1.1  mrg   access_data data (m_ptr_qry.rvals, stmt, access_read_write, NULL_TREE,
   2475  1.1  mrg 		    true, NULL_TREE, true);
   2476  1.1  mrg   const int ost = warn_stringop_overflow ? warn_stringop_overflow - 1 : 1;
   2477  1.1  mrg   compute_objsize (src, stmt, ost, &data.src, &m_ptr_qry);
   2478  1.1  mrg   tree destsize = compute_objsize (dest, stmt, ost, &data.dst, &m_ptr_qry);
   2479  1.1  mrg 
   2480  1.1  mrg   check_access (stmt, /*dstwrite=*/NULL_TREE, /*maxread=*/NULL_TREE,
   2481  1.1  mrg 		src, destsize, data.mode, &data, m_ptr_qry.rvals);
   2482  1.1  mrg }
   2483  1.1  mrg 
   2484  1.1  mrg /* Check a call STMT to strcat() for overflow and warn if it does.  */
   2485  1.1  mrg 
   2486  1.1  mrg void
   2487  1.1  mrg pass_waccess::check_strncat (gcall *stmt)
   2488  1.1  mrg {
   2489  1.1  mrg   if (m_early_checks_p)
   2490  1.1  mrg     return;
   2491  1.1  mrg 
   2492  1.1  mrg   if (!warn_stringop_overflow && !warn_stringop_overread)
   2493  1.1  mrg     return;
   2494  1.1  mrg 
   2495  1.1  mrg   tree dest = call_arg (stmt, 0);
   2496  1.1  mrg   tree src = call_arg (stmt, 1);
   2497  1.1  mrg   /* The upper bound on the number of bytes to write.  */
   2498  1.1  mrg   tree maxread = call_arg (stmt, 2);
   2499  1.1  mrg 
   2500  1.1  mrg   /* Detect unterminated source (only).  */
   2501  1.1  mrg   if (!check_nul_terminated_array (stmt, src, maxread))
   2502  1.1  mrg     return;
   2503  1.1  mrg 
   2504  1.1  mrg   /* The length of the source sequence.  */
   2505  1.1  mrg   tree slen = c_strlen (src, 1);
   2506  1.1  mrg 
   2507  1.1  mrg   /* Try to determine the range of lengths that the source expression
   2508  1.1  mrg      refers to.  Since the lengths are only used for warning and not
   2509  1.1  mrg      for code generation disable strict mode below.  */
   2510  1.1  mrg   tree maxlen = slen;
   2511  1.1  mrg   if (!maxlen)
   2512  1.1  mrg     {
   2513  1.1  mrg       c_strlen_data lendata = { };
   2514  1.1  mrg       get_range_strlen (src, &lendata, /* eltsize = */ 1);
   2515  1.1  mrg       maxlen = lendata.maxbound;
   2516  1.1  mrg     }
   2517  1.1  mrg 
   2518  1.1  mrg   access_data data (m_ptr_qry.rvals, stmt, access_read_write);
   2519  1.1  mrg   /* Try to verify that the destination is big enough for the shortest
   2520  1.1  mrg      string.  First try to determine the size of the destination object
   2521  1.1  mrg      into which the source is being copied.  */
   2522  1.1  mrg   const int ost = warn_stringop_overflow - 1;
   2523  1.1  mrg   tree destsize = compute_objsize (dest, stmt, ost, &data.dst, &m_ptr_qry);
   2524  1.1  mrg 
   2525  1.1  mrg   /* Add one for the terminating nul.  */
   2526  1.1  mrg   tree srclen = (maxlen
   2527  1.1  mrg 		 ? fold_build2 (PLUS_EXPR, size_type_node, maxlen,
   2528  1.1  mrg 				size_one_node)
   2529  1.1  mrg 		 : NULL_TREE);
   2530  1.1  mrg 
   2531  1.1  mrg   /* The strncat function copies at most MAXREAD bytes and always appends
   2532  1.1  mrg      the terminating nul so the specified upper bound should never be equal
   2533  1.1  mrg      to (or greater than) the size of the destination.  */
   2534  1.1  mrg   if (tree_fits_uhwi_p (maxread) && tree_fits_uhwi_p (destsize)
   2535  1.1  mrg       && tree_int_cst_equal (destsize, maxread))
   2536  1.1  mrg     {
   2537  1.1  mrg       location_t loc = get_location (stmt);
   2538  1.1  mrg       warning_at (loc, OPT_Wstringop_overflow_,
   2539  1.1  mrg 		  "%qD specified bound %E equals destination size",
   2540  1.1  mrg 		  get_callee_fndecl (stmt), maxread);
   2541  1.1  mrg 
   2542  1.1  mrg       return;
   2543  1.1  mrg     }
   2544  1.1  mrg 
   2545  1.1  mrg   if (!srclen
   2546  1.1  mrg       || (maxread && tree_fits_uhwi_p (maxread)
   2547  1.1  mrg 	  && tree_fits_uhwi_p (srclen)
   2548  1.1  mrg 	  && tree_int_cst_lt (maxread, srclen)))
   2549  1.1  mrg     srclen = maxread;
   2550  1.1  mrg 
   2551  1.1  mrg   check_access (stmt, /*dstwrite=*/NULL_TREE, maxread, srclen,
   2552  1.1  mrg 		destsize, data.mode, &data, m_ptr_qry.rvals);
   2553  1.1  mrg }
   2554  1.1  mrg 
   2555  1.1  mrg /* Check a call STMT to stpcpy() or strcpy() for overflow and warn
   2556  1.1  mrg    if it does.  */
   2557  1.1  mrg 
   2558  1.1  mrg void
   2559  1.1  mrg pass_waccess::check_stxcpy (gcall *stmt)
   2560  1.1  mrg {
   2561  1.1  mrg   if (m_early_checks_p)
   2562  1.1  mrg     return;
   2563  1.1  mrg 
   2564  1.1  mrg   tree dst = call_arg (stmt, 0);
   2565  1.1  mrg   tree src = call_arg (stmt, 1);
   2566  1.1  mrg 
   2567  1.1  mrg   tree size;
   2568  1.1  mrg   bool exact;
   2569  1.1  mrg   if (tree nonstr = unterminated_array (src, &size, &exact))
   2570  1.1  mrg     {
   2571  1.1  mrg       /* NONSTR refers to the non-nul terminated constant array.  */
   2572  1.1  mrg       warn_string_no_nul (get_location (stmt), stmt, NULL, src, nonstr,
   2573  1.1  mrg 			  size, exact);
   2574  1.1  mrg       return;
   2575  1.1  mrg     }
   2576  1.1  mrg 
   2577  1.1  mrg   if (warn_stringop_overflow)
   2578  1.1  mrg     {
   2579  1.1  mrg       access_data data (m_ptr_qry.rvals, stmt, access_read_write, NULL_TREE,
   2580  1.1  mrg 			true, NULL_TREE, true);
   2581  1.1  mrg       const int ost = warn_stringop_overflow ? warn_stringop_overflow - 1 : 1;
   2582  1.1  mrg       compute_objsize (src, stmt, ost, &data.src, &m_ptr_qry);
   2583  1.1  mrg       tree dstsize = compute_objsize (dst, stmt, ost, &data.dst, &m_ptr_qry);
   2584  1.1  mrg       check_access (stmt, /*dstwrite=*/ NULL_TREE,
   2585  1.1  mrg 		    /*maxread=*/ NULL_TREE, /*srcstr=*/ src,
   2586  1.1  mrg 		    dstsize, data.mode, &data, m_ptr_qry.rvals);
   2587  1.1  mrg     }
   2588  1.1  mrg 
   2589  1.1  mrg   /* Check to see if the argument was declared attribute nonstring
   2590  1.1  mrg      and if so, issue a warning since at this point it's not known
   2591  1.1  mrg      to be nul-terminated.  */
   2592  1.1  mrg   tree fndecl = get_callee_fndecl (stmt);
   2593  1.1  mrg   maybe_warn_nonstring_arg (fndecl, stmt);
   2594  1.1  mrg }
   2595  1.1  mrg 
   2596  1.1  mrg /* Check a call STMT to stpncpy() or strncpy() for overflow and warn
   2597  1.1  mrg    if it does.  */
   2598  1.1  mrg 
   2599  1.1  mrg void
   2600  1.1  mrg pass_waccess::check_stxncpy (gcall *stmt)
   2601  1.1  mrg {
   2602  1.1  mrg   if (m_early_checks_p || !warn_stringop_overflow)
   2603  1.1  mrg     return;
   2604  1.1  mrg 
   2605  1.1  mrg   tree dst = call_arg (stmt, 0);
   2606  1.1  mrg   tree src = call_arg (stmt, 1);
   2607  1.1  mrg   /* The number of bytes to write (not the maximum).  */
   2608  1.1  mrg   tree len = call_arg (stmt, 2);
   2609  1.1  mrg 
   2610  1.1  mrg   access_data data (m_ptr_qry.rvals, stmt, access_read_write, len, true, len,
   2611  1.1  mrg 		    true);
   2612  1.1  mrg   const int ost = warn_stringop_overflow ? warn_stringop_overflow - 1 : 1;
   2613  1.1  mrg   compute_objsize (src, stmt, ost, &data.src, &m_ptr_qry);
   2614  1.1  mrg   tree dstsize = compute_objsize (dst, stmt, ost, &data.dst, &m_ptr_qry);
   2615  1.1  mrg 
   2616  1.1  mrg   check_access (stmt, /*dstwrite=*/len, /*maxread=*/len, src, dstsize,
   2617  1.1  mrg 		data.mode, &data, m_ptr_qry.rvals);
   2618  1.1  mrg }
   2619  1.1  mrg 
   2620  1.1  mrg /* Check a call STMT to stpncpy() or strncpy() for overflow and warn
   2621  1.1  mrg    if it does.  */
   2622  1.1  mrg 
   2623  1.1  mrg void
   2624  1.1  mrg pass_waccess::check_strncmp (gcall *stmt)
   2625  1.1  mrg {
   2626  1.1  mrg   if (m_early_checks_p || !warn_stringop_overread)
   2627  1.1  mrg     return;
   2628  1.1  mrg 
   2629  1.1  mrg   tree arg1 = call_arg (stmt, 0);
   2630  1.1  mrg   tree arg2 = call_arg (stmt, 1);
   2631  1.1  mrg   tree bound = call_arg (stmt, 2);
   2632  1.1  mrg 
   2633  1.1  mrg   /* First check each argument separately, considering the bound.  */
   2634  1.1  mrg   if (!check_nul_terminated_array (stmt, arg1, bound)
   2635  1.1  mrg       || !check_nul_terminated_array (stmt, arg2, bound))
   2636  1.1  mrg     return;
   2637  1.1  mrg 
   2638  1.1  mrg   /* A strncmp read from each argument is constrained not just by
   2639  1.1  mrg      the bound but also by the length of the shorter string.  Specifying
   2640  1.1  mrg      a bound that's larger than the size of either array makes no sense
   2641  1.1  mrg      and is likely a bug.  When the length of neither of the two strings
   2642  1.1  mrg      is known but the sizes of both of the arrays they are stored in is,
   2643  1.1  mrg      issue a warning if the bound is larger than the size of
   2644  1.1  mrg      the larger of the two arrays.  */
   2645  1.1  mrg 
   2646  1.1  mrg   c_strlen_data lendata1{ }, lendata2{ };
   2647  1.1  mrg   tree len1 = c_strlen (arg1, 1, &lendata1);
   2648  1.1  mrg   tree len2 = c_strlen (arg2, 1, &lendata2);
   2649  1.1  mrg 
   2650  1.1  mrg   if (len1 && TREE_CODE (len1) != INTEGER_CST)
   2651  1.1  mrg     len1 = NULL_TREE;
   2652  1.1  mrg   if (len2 && TREE_CODE (len2) != INTEGER_CST)
   2653  1.1  mrg     len2 = NULL_TREE;
   2654  1.1  mrg 
   2655  1.1  mrg   if (len1 && len2)
   2656  1.1  mrg     /* If the length of both arguments was computed they must both be
   2657  1.1  mrg        nul-terminated and no further checking is necessary regardless
   2658  1.1  mrg        of the bound.  */
   2659  1.1  mrg     return;
   2660  1.1  mrg 
   2661  1.1  mrg   /* Check to see if the argument was declared with attribute nonstring
   2662  1.1  mrg      and if so, issue a warning since at this point it's not known to be
   2663  1.1  mrg      nul-terminated.  */
   2664  1.1  mrg   if (maybe_warn_nonstring_arg (get_callee_fndecl (stmt), stmt))
   2665  1.1  mrg     return;
   2666  1.1  mrg 
   2667  1.1  mrg   access_data adata1 (m_ptr_qry.rvals, stmt, access_read_only, NULL_TREE, false,
   2668  1.1  mrg 		      bound, true);
   2669  1.1  mrg   access_data adata2 (m_ptr_qry.rvals, stmt, access_read_only, NULL_TREE, false,
   2670  1.1  mrg 		      bound, true);
   2671  1.1  mrg 
   2672  1.1  mrg   /* Determine the range of the bound first and bail if it fails; it's
   2673  1.1  mrg      cheaper than computing the size of the objects.  */
   2674  1.1  mrg   tree bndrng[2] = { NULL_TREE, NULL_TREE };
   2675  1.1  mrg   get_size_range (m_ptr_qry.rvals, bound, stmt, bndrng, adata1.src_bndrng);
   2676  1.1  mrg   if (!bndrng[0] || integer_zerop (bndrng[0]))
   2677  1.1  mrg     return;
   2678  1.1  mrg 
   2679  1.1  mrg   if (len1 && tree_int_cst_lt (len1, bndrng[0]))
   2680  1.1  mrg     bndrng[0] = len1;
   2681  1.1  mrg   if (len2 && tree_int_cst_lt (len2, bndrng[0]))
   2682  1.1  mrg     bndrng[0] = len2;
   2683  1.1  mrg 
   2684  1.1  mrg   /* compute_objsize almost never fails (and ultimately should never
   2685  1.1  mrg      fail).  Don't bother to handle the rare case when it does.  */
   2686  1.1  mrg   if (!compute_objsize (arg1, stmt, 1, &adata1.src, &m_ptr_qry)
   2687  1.1  mrg       || !compute_objsize (arg2, stmt, 1, &adata2.src, &m_ptr_qry))
   2688  1.1  mrg     return;
   2689  1.1  mrg 
   2690  1.1  mrg   /* Compute the size of the remaining space in each array after
   2691  1.1  mrg      subtracting any offset into it.  */
   2692  1.1  mrg   offset_int rem1 = adata1.src.size_remaining ();
   2693  1.1  mrg   offset_int rem2 = adata2.src.size_remaining ();
   2694  1.1  mrg 
   2695  1.1  mrg   /* Cap REM1 and REM2 at the other if the other's argument is known
   2696  1.1  mrg      to be an unterminated array, either because there's no space
   2697  1.1  mrg      left in it after adding its offset or because it's constant and
   2698  1.1  mrg      has no nul.  */
   2699  1.1  mrg   if (rem1 == 0 || (rem1 < rem2 && lendata1.decl))
   2700  1.1  mrg     rem2 = rem1;
   2701  1.1  mrg   else if (rem2 == 0 || (rem2 < rem1 && lendata2.decl))
   2702  1.1  mrg     rem1 = rem2;
   2703  1.1  mrg 
   2704  1.1  mrg   /* Point PAD at the array to reference in the note if a warning
   2705  1.1  mrg      is issued.  */
   2706  1.1  mrg   access_data *pad = len1 ? &adata2 : &adata1;
   2707  1.1  mrg   offset_int maxrem = wi::max (rem1, rem2, UNSIGNED);
   2708  1.1  mrg   if (lendata1.decl || lendata2.decl
   2709  1.1  mrg       || maxrem < wi::to_offset (bndrng[0]))
   2710  1.1  mrg     {
   2711  1.1  mrg       /* Warn when either argument isn't nul-terminated or the maximum
   2712  1.1  mrg 	 remaining space in the two arrays is less than the bound.  */
   2713  1.1  mrg       tree func = get_callee_fndecl (stmt);
   2714  1.1  mrg       location_t loc = gimple_location (stmt);
   2715  1.1  mrg       maybe_warn_for_bound (OPT_Wstringop_overread, loc, stmt, func,
   2716  1.1  mrg 			    bndrng, wide_int_to_tree (sizetype, maxrem),
   2717  1.1  mrg 			    pad);
   2718  1.1  mrg     }
   2719  1.1  mrg }
   2720  1.1  mrg 
   2721  1.1  mrg /* Determine and check the sizes of the source and the destination
   2722  1.1  mrg    of calls to __builtin_{bzero,memcpy,mempcpy,memset} calls.  STMT is
   2723  1.1  mrg    the call statement, DEST is the destination argument, SRC is the source
   2724  1.1  mrg    argument or null, and SIZE is the number of bytes being accessed.  Use
   2725  1.1  mrg    Object Size type-0 regardless of the OPT_Wstringop_overflow_ setting.
   2726  1.1  mrg    Return true on success (no overflow or invalid sizes), false otherwise.  */
   2727  1.1  mrg 
   2728  1.1  mrg void
   2729  1.1  mrg pass_waccess::check_memop_access (gimple *stmt, tree dest, tree src, tree size)
   2730  1.1  mrg {
   2731  1.1  mrg   if (m_early_checks_p)
   2732  1.1  mrg     return;
   2733  1.1  mrg 
   2734  1.1  mrg   /* For functions like memset and memcpy that operate on raw memory
   2735  1.1  mrg      try to determine the size of the largest source and destination
   2736  1.1  mrg      object using type-0 Object Size regardless of the object size
   2737  1.1  mrg      type specified by the option.  */
   2738  1.1  mrg   access_data data (m_ptr_qry.rvals, stmt, access_read_write);
   2739  1.1  mrg   tree srcsize
   2740  1.1  mrg     = src ? compute_objsize (src, stmt, 0, &data.src, &m_ptr_qry) : NULL_TREE;
   2741  1.1  mrg   tree dstsize = compute_objsize (dest, stmt, 0, &data.dst, &m_ptr_qry);
   2742  1.1  mrg 
   2743  1.1  mrg   check_access (stmt, size, /*maxread=*/NULL_TREE, srcsize, dstsize,
   2744  1.1  mrg 		data.mode, &data, m_ptr_qry.rvals);
   2745  1.1  mrg }
   2746  1.1  mrg 
   2747  1.1  mrg /* A convenience wrapper for check_access to check access by a read-only
   2748  1.1  mrg    function like puts or strcmp.  */
   2749  1.1  mrg 
   2750  1.1  mrg void
   2751  1.1  mrg pass_waccess::check_read_access (gimple *stmt, tree src,
   2752  1.1  mrg 				 tree bound /* = NULL_TREE */,
   2753  1.1  mrg 				 int ost /* = 1 */)
   2754  1.1  mrg {
   2755  1.1  mrg   if (m_early_checks_p || !warn_stringop_overread)
   2756  1.1  mrg     return;
   2757  1.1  mrg 
   2758  1.1  mrg   if (bound && !useless_type_conversion_p (size_type_node, TREE_TYPE (bound)))
   2759  1.1  mrg     bound = fold_convert (size_type_node, bound);
   2760  1.1  mrg 
   2761  1.1  mrg   tree fndecl = get_callee_fndecl (stmt);
   2762  1.1  mrg   maybe_warn_nonstring_arg (fndecl, stmt);
   2763  1.1  mrg 
   2764  1.1  mrg   access_data data (m_ptr_qry.rvals, stmt, access_read_only, NULL_TREE,
   2765  1.1  mrg 		   false, bound, true);
   2766  1.1  mrg   compute_objsize (src, stmt, ost, &data.src, &m_ptr_qry);
   2767  1.1  mrg   check_access (stmt, /*dstwrite=*/ NULL_TREE, /*maxread=*/ bound,
   2768  1.1  mrg 		/*srcstr=*/ src, /*dstsize=*/ NULL_TREE, data.mode,
   2769  1.1  mrg 		&data, m_ptr_qry.rvals);
   2770  1.1  mrg }
   2771  1.1  mrg 
   2772  1.1  mrg /* Return true if memory model ORD is constant in the context of STMT and
   2773  1.1  mrg    set *CSTVAL to the constant value.  Otherwise return false.  Warn for
   2774  1.1  mrg    invalid ORD.  */
   2775  1.1  mrg 
   2776  1.1  mrg bool
   2777  1.1  mrg memmodel_to_uhwi (tree ord, gimple *stmt, unsigned HOST_WIDE_INT *cstval)
   2778  1.1  mrg {
   2779  1.1  mrg   unsigned HOST_WIDE_INT val;
   2780  1.1  mrg 
   2781  1.1  mrg   if (TREE_CODE (ord) == INTEGER_CST)
   2782  1.1  mrg     {
   2783  1.1  mrg       if (!tree_fits_uhwi_p (ord))
   2784  1.1  mrg 	return false;
   2785  1.1  mrg       val = tree_to_uhwi (ord);
   2786  1.1  mrg     }
   2787  1.1  mrg   else
   2788  1.1  mrg     {
   2789  1.1  mrg       /* Use the range query to determine constant values in the absence
   2790  1.1  mrg 	 of constant propagation (such as at -O0).  */
   2791  1.1  mrg       value_range rng;
   2792  1.1  mrg       if (!get_range_query (cfun)->range_of_expr (rng, ord, stmt)
   2793  1.1  mrg 	  || !rng.constant_p ()
   2794  1.1  mrg 	  || !rng.singleton_p (&ord))
   2795  1.1  mrg 	return false;
   2796  1.1  mrg 
   2797  1.1  mrg       wide_int lob = rng.lower_bound ();
   2798  1.1  mrg       if (!wi::fits_uhwi_p (lob))
   2799  1.1  mrg 	return false;
   2800  1.1  mrg 
   2801  1.1  mrg       val = lob.to_shwi ();
   2802  1.1  mrg     }
   2803  1.1  mrg 
   2804  1.1  mrg   if (targetm.memmodel_check)
   2805  1.1  mrg     /* This might warn for an invalid VAL but return a conservatively
   2806  1.1  mrg        valid result.  */
   2807  1.1  mrg     val = targetm.memmodel_check (val);
   2808  1.1  mrg   else if (val & ~MEMMODEL_MASK)
   2809  1.1  mrg     {
   2810  1.1  mrg       tree fndecl = gimple_call_fndecl (stmt);
   2811  1.1  mrg       location_t loc = gimple_location (stmt);
   2812  1.1  mrg       loc = expansion_point_location_if_in_system_header (loc);
   2813  1.1  mrg 
   2814  1.1  mrg       warning_at (loc, OPT_Winvalid_memory_model,
   2815  1.1  mrg 		  "unknown architecture specifier in memory model "
   2816  1.1  mrg 		  "%wi for %qD", val, fndecl);
   2817  1.1  mrg       return false;
   2818  1.1  mrg     }
   2819  1.1  mrg 
   2820  1.1  mrg   *cstval = val;
   2821  1.1  mrg 
   2822  1.1  mrg   return true;
   2823  1.1  mrg }
   2824  1.1  mrg 
   2825  1.1  mrg /* Valid memory model for each set of atomic built-in functions.  */
   2826  1.1  mrg 
   2827  1.1  mrg struct memmodel_pair
   2828  1.1  mrg {
   2829  1.1  mrg   memmodel modval;
   2830  1.1  mrg   const char* modname;
   2831  1.1  mrg 
   2832  1.1  mrg #define MEMMODEL_PAIR(val, str)			\
   2833  1.1  mrg   { MEMMODEL_ ## val, "memory_order_" str }
   2834  1.1  mrg };
   2835  1.1  mrg 
   2836  1.1  mrg /* Valid memory models in the order of increasing strength.  */
   2837  1.1  mrg 
   2838  1.1  mrg static const memmodel_pair memory_models[] =
   2839  1.1  mrg   { MEMMODEL_PAIR (RELAXED, "relaxed"),
   2840  1.1  mrg     MEMMODEL_PAIR (SEQ_CST, "seq_cst"),
   2841  1.1  mrg     MEMMODEL_PAIR (ACQUIRE, "acquire"),
   2842  1.1  mrg     MEMMODEL_PAIR (CONSUME, "consume"),
   2843  1.1  mrg     MEMMODEL_PAIR (RELEASE, "release"),
   2844  1.1  mrg     MEMMODEL_PAIR (ACQ_REL, "acq_rel")
   2845  1.1  mrg   };
   2846  1.1  mrg 
   2847  1.1  mrg /* Return the name of the memory model VAL.  */
   2848  1.1  mrg 
   2849  1.1  mrg static const char*
   2850  1.1  mrg memmodel_name (unsigned HOST_WIDE_INT val)
   2851  1.1  mrg {
   2852  1.1  mrg   val = memmodel_base (val);
   2853  1.1  mrg 
   2854  1.1  mrg   for (unsigned i = 0; i != sizeof memory_models / sizeof *memory_models; ++i)
   2855  1.1  mrg     {
   2856  1.1  mrg       if (val == memory_models[i].modval)
   2857  1.1  mrg 	return memory_models[i].modname;
   2858  1.1  mrg     }
   2859  1.1  mrg   return NULL;
   2860  1.1  mrg }
   2861  1.1  mrg 
   2862  1.1  mrg /* Indices of valid MEMORY_MODELS above for corresponding atomic operations.  */
   2863  1.1  mrg static const unsigned char load_models[] = { 0, 1, 2, 3, UCHAR_MAX };
   2864  1.1  mrg static const unsigned char store_models[] = { 0, 1, 4, UCHAR_MAX };
   2865  1.1  mrg static const unsigned char xchg_models[] = { 0, 1, 3, 4, 5, UCHAR_MAX };
   2866  1.1  mrg static const unsigned char flag_clr_models[] = { 0, 1, 4, UCHAR_MAX };
   2867  1.1  mrg static const unsigned char all_models[] = { 0, 1, 2, 3, 4, 5, UCHAR_MAX };
   2868  1.1  mrg 
   2869  1.1  mrg /* Check the success memory model argument ORD_SUCS to the call STMT to
   2870  1.1  mrg    an atomic function and warn if it's invalid.  If nonnull, also check
   2871  1.1  mrg    the failure memory model ORD_FAIL and warn if it's invalid.  Return
   2872  1.1  mrg    true if a warning has been issued.  */
   2873  1.1  mrg 
   2874  1.1  mrg bool
   2875  1.1  mrg pass_waccess::maybe_warn_memmodel (gimple *stmt, tree ord_sucs,
   2876  1.1  mrg 				   tree ord_fail, const unsigned char *valid)
   2877  1.1  mrg {
   2878  1.1  mrg   unsigned HOST_WIDE_INT sucs, fail = 0;
   2879  1.1  mrg   if (!memmodel_to_uhwi (ord_sucs, stmt, &sucs)
   2880  1.1  mrg       || (ord_fail && !memmodel_to_uhwi (ord_fail, stmt, &fail)))
   2881  1.1  mrg     return false;
   2882  1.1  mrg 
   2883  1.1  mrg   bool is_valid = false;
   2884  1.1  mrg   if (valid)
   2885  1.1  mrg     for (unsigned i = 0; valid[i] != UCHAR_MAX; ++i)
   2886  1.1  mrg       {
   2887  1.1  mrg 	memmodel model = memory_models[valid[i]].modval;
   2888  1.1  mrg 	if (memmodel_base (sucs) == model)
   2889  1.1  mrg 	  {
   2890  1.1  mrg 	    is_valid = true;
   2891  1.1  mrg 	    break;
   2892  1.1  mrg 	  }
   2893  1.1  mrg       }
   2894  1.1  mrg   else
   2895  1.1  mrg     is_valid = true;
   2896  1.1  mrg 
   2897  1.1  mrg   tree fndecl = gimple_call_fndecl (stmt);
   2898  1.1  mrg   location_t loc = gimple_location (stmt);
   2899  1.1  mrg   loc = expansion_point_location_if_in_system_header (loc);
   2900  1.1  mrg 
   2901  1.1  mrg   if (!is_valid)
   2902  1.1  mrg     {
   2903  1.1  mrg       bool warned = false;
   2904  1.1  mrg       if (const char *modname = memmodel_name (sucs))
   2905  1.1  mrg 	warned = warning_at (loc, OPT_Winvalid_memory_model,
   2906  1.1  mrg 			     "invalid memory model %qs for %qD",
   2907  1.1  mrg 			     modname, fndecl);
   2908  1.1  mrg       else
   2909  1.1  mrg 	warned = warning_at (loc, OPT_Winvalid_memory_model,
   2910  1.1  mrg 			     "invalid memory model %wi for %qD",
   2911  1.1  mrg 			     sucs, fndecl);
   2912  1.1  mrg 
   2913  1.1  mrg       if (!warned)
   2914  1.1  mrg 	return false;
   2915  1.1  mrg 
   2916  1.1  mrg       /* Print a note with the valid memory models.  */
   2917  1.1  mrg       pretty_printer pp;
   2918  1.1  mrg       pp_show_color (&pp) = pp_show_color (global_dc->printer);
   2919  1.1  mrg       for (unsigned i = 0; valid[i] != UCHAR_MAX; ++i)
   2920  1.1  mrg 	{
   2921  1.1  mrg 	  const char *modname = memory_models[valid[i]].modname;
   2922  1.1  mrg 	  pp_printf (&pp, "%s%qs", i ? ", " : "", modname);
   2923  1.1  mrg 	}
   2924  1.1  mrg 
   2925  1.1  mrg       inform (loc, "valid models are %s", pp_formatted_text (&pp));
   2926  1.1  mrg       return true;
   2927  1.1  mrg     }
   2928  1.1  mrg 
   2929  1.1  mrg   if (!ord_fail)
   2930  1.1  mrg     return false;
   2931  1.1  mrg 
   2932  1.1  mrg   if (fail == MEMMODEL_RELEASE || fail == MEMMODEL_ACQ_REL)
   2933  1.1  mrg     if (const char *failname = memmodel_name (fail))
   2934  1.1  mrg       {
   2935  1.1  mrg 	/* If both memory model arguments are valid but their combination
   2936  1.1  mrg 	   is not, use their names in the warning.  */
   2937  1.1  mrg 	if (!warning_at (loc, OPT_Winvalid_memory_model,
   2938  1.1  mrg 			 "invalid failure memory model %qs for %qD",
   2939  1.1  mrg 			 failname, fndecl))
   2940  1.1  mrg 	  return false;
   2941  1.1  mrg 
   2942  1.1  mrg 	inform (loc,
   2943  1.1  mrg 		"valid failure models are %qs, %qs, %qs, %qs",
   2944  1.1  mrg 		"memory_order_relaxed", "memory_order_seq_cst",
   2945  1.1  mrg 		"memory_order_acquire", "memory_order_consume");
   2946  1.1  mrg 	return true;
   2947  1.1  mrg       }
   2948  1.1  mrg 
   2949  1.1  mrg   if (memmodel_base (fail) <= memmodel_base (sucs))
   2950  1.1  mrg     return false;
   2951  1.1  mrg 
   2952  1.1  mrg   if (const char *sucsname = memmodel_name (sucs))
   2953  1.1  mrg     if (const char *failname = memmodel_name (fail))
   2954  1.1  mrg       {
   2955  1.1  mrg 	/* If both memory model arguments are valid but their combination
   2956  1.1  mrg 	   is not, use their names in the warning.  */
   2957  1.1  mrg 	if (!warning_at (loc, OPT_Winvalid_memory_model,
   2958  1.1  mrg 			 "failure memory model %qs cannot be stronger "
   2959  1.1  mrg 			 "than success memory model %qs for %qD",
   2960  1.1  mrg 			 failname, sucsname, fndecl))
   2961  1.1  mrg 	  return false;
   2962  1.1  mrg 
   2963  1.1  mrg 	/* Print a note with the valid failure memory models which are
   2964  1.1  mrg 	   those with a value less than or equal to the success mode.  */
   2965  1.1  mrg 	char buf[120];
   2966  1.1  mrg 	*buf = '\0';
   2967  1.1  mrg 	for (unsigned i = 0;
   2968  1.1  mrg 	     memory_models[i].modval <= memmodel_base (sucs); ++i)
   2969  1.1  mrg 	  {
   2970  1.1  mrg 	    if (*buf)
   2971  1.1  mrg 	      strcat (buf, ", ");
   2972  1.1  mrg 
   2973  1.1  mrg 	    const char *modname = memory_models[valid[i]].modname;
   2974  1.1  mrg 	    sprintf (buf + strlen (buf), "'%s'", modname);
   2975  1.1  mrg 	  }
   2976  1.1  mrg 
   2977  1.1  mrg 	inform (loc, "valid models are %s", buf);
   2978  1.1  mrg 	return true;
   2979  1.1  mrg       }
   2980  1.1  mrg 
   2981  1.1  mrg   /* If either memory model argument value is invalid use the numerical
   2982  1.1  mrg      value of both in the message.  */
   2983  1.1  mrg   return warning_at (loc, OPT_Winvalid_memory_model,
   2984  1.1  mrg 		     "failure memory model %wi cannot be stronger "
   2985  1.1  mrg 		     "than success memory model %wi for %qD",
   2986  1.1  mrg 		     fail, sucs, fndecl);
   2987  1.1  mrg }
   2988  1.1  mrg 
   2989  1.1  mrg /* Wrapper for the above.  */
   2990  1.1  mrg 
   2991  1.1  mrg void
   2992  1.1  mrg pass_waccess::check_atomic_memmodel (gimple *stmt, tree ord_sucs,
   2993  1.1  mrg 				     tree ord_fail, const unsigned char *valid)
   2994  1.1  mrg {
   2995  1.1  mrg   if (warning_suppressed_p (stmt, OPT_Winvalid_memory_model))
   2996  1.1  mrg     return;
   2997  1.1  mrg 
   2998  1.1  mrg   if (!maybe_warn_memmodel (stmt, ord_sucs, ord_fail, valid))
   2999  1.1  mrg     return;
   3000  1.1  mrg 
   3001  1.1  mrg   suppress_warning (stmt, OPT_Winvalid_memory_model);
   3002  1.1  mrg }
   3003  1.1  mrg 
   3004  1.1  mrg /* Check a call STMT to an atomic or sync built-in.  */
   3005  1.1  mrg 
   3006  1.1  mrg bool
   3007  1.1  mrg pass_waccess::check_atomic_builtin (gcall *stmt)
   3008  1.1  mrg {
   3009  1.1  mrg   tree callee = gimple_call_fndecl (stmt);
   3010  1.1  mrg   if (!callee)
   3011  1.1  mrg     return false;
   3012  1.1  mrg 
   3013  1.1  mrg   /* The size in bytes of the access by the function, and the number
   3014  1.1  mrg      of the second argument to check (if any).  */
   3015  1.1  mrg   unsigned bytes = 0, arg2 = UINT_MAX;
   3016  1.1  mrg   unsigned sucs_arg = UINT_MAX, fail_arg = UINT_MAX;
   3017  1.1  mrg   /* Points to the array of indices of valid memory models.  */
   3018  1.1  mrg   const unsigned char *pvalid_models = NULL;
   3019  1.1  mrg 
   3020  1.1  mrg   switch (DECL_FUNCTION_CODE (callee))
   3021  1.1  mrg     {
   3022  1.1  mrg #define BUILTIN_ACCESS_SIZE_FNSPEC(N)			\
   3023  1.1  mrg       BUILT_IN_SYNC_FETCH_AND_ADD_ ## N:		\
   3024  1.1  mrg     case BUILT_IN_SYNC_FETCH_AND_SUB_ ## N:		\
   3025  1.1  mrg     case BUILT_IN_SYNC_FETCH_AND_OR_ ## N:		\
   3026  1.1  mrg     case BUILT_IN_SYNC_FETCH_AND_AND_ ## N:		\
   3027  1.1  mrg     case BUILT_IN_SYNC_FETCH_AND_XOR_ ## N:		\
   3028  1.1  mrg     case BUILT_IN_SYNC_FETCH_AND_NAND_ ## N:		\
   3029  1.1  mrg     case BUILT_IN_SYNC_ADD_AND_FETCH_ ## N:		\
   3030  1.1  mrg     case BUILT_IN_SYNC_SUB_AND_FETCH_ ## N:		\
   3031  1.1  mrg     case BUILT_IN_SYNC_OR_AND_FETCH_ ## N:		\
   3032  1.1  mrg     case BUILT_IN_SYNC_AND_AND_FETCH_ ## N:		\
   3033  1.1  mrg     case BUILT_IN_SYNC_XOR_AND_FETCH_ ## N:		\
   3034  1.1  mrg     case BUILT_IN_SYNC_NAND_AND_FETCH_ ## N:		\
   3035  1.1  mrg     case BUILT_IN_SYNC_LOCK_TEST_AND_SET_ ## N:		\
   3036  1.1  mrg     case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_ ## N:	\
   3037  1.1  mrg     case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_ ## N:	\
   3038  1.1  mrg     case BUILT_IN_SYNC_LOCK_RELEASE_ ## N:		\
   3039  1.1  mrg       bytes = N;					\
   3040  1.1  mrg       break;						\
   3041  1.1  mrg     case BUILT_IN_ATOMIC_LOAD_ ## N:			\
   3042  1.1  mrg       pvalid_models = load_models;			\
   3043  1.1  mrg       sucs_arg = 1;					\
   3044  1.1  mrg       /* FALLTHROUGH */					\
   3045  1.1  mrg     case BUILT_IN_ATOMIC_STORE_ ## N:			\
   3046  1.1  mrg       if (!pvalid_models)				\
   3047  1.1  mrg 	pvalid_models = store_models;			\
   3048  1.1  mrg       /* FALLTHROUGH */					\
   3049  1.1  mrg     case BUILT_IN_ATOMIC_ADD_FETCH_ ## N:		\
   3050  1.1  mrg     case BUILT_IN_ATOMIC_SUB_FETCH_ ## N:		\
   3051  1.1  mrg     case BUILT_IN_ATOMIC_AND_FETCH_ ## N:		\
   3052  1.1  mrg     case BUILT_IN_ATOMIC_NAND_FETCH_ ## N:		\
   3053  1.1  mrg     case BUILT_IN_ATOMIC_XOR_FETCH_ ## N:		\
   3054  1.1  mrg     case BUILT_IN_ATOMIC_OR_FETCH_ ## N:		\
   3055  1.1  mrg     case BUILT_IN_ATOMIC_FETCH_ADD_ ## N:		\
   3056  1.1  mrg     case BUILT_IN_ATOMIC_FETCH_SUB_ ## N:		\
   3057  1.1  mrg     case BUILT_IN_ATOMIC_FETCH_AND_ ## N:		\
   3058  1.1  mrg     case BUILT_IN_ATOMIC_FETCH_NAND_ ## N:		\
   3059  1.1  mrg     case BUILT_IN_ATOMIC_FETCH_OR_ ## N:		\
   3060  1.1  mrg     case BUILT_IN_ATOMIC_FETCH_XOR_ ## N:		\
   3061  1.1  mrg 	bytes = N;					\
   3062  1.1  mrg 	if (sucs_arg == UINT_MAX)			\
   3063  1.1  mrg 	  sucs_arg = 2;					\
   3064  1.1  mrg 	if (!pvalid_models)				\
   3065  1.1  mrg 	  pvalid_models = all_models;			\
   3066  1.1  mrg 	break;						\
   3067  1.1  mrg     case BUILT_IN_ATOMIC_EXCHANGE_ ## N:		\
   3068  1.1  mrg 	bytes = N;					\
   3069  1.1  mrg 	sucs_arg = 3;					\
   3070  1.1  mrg 	pvalid_models = xchg_models;			\
   3071  1.1  mrg 	break;						\
   3072  1.1  mrg     case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_ ## N:	\
   3073  1.1  mrg 	bytes = N;					\
   3074  1.1  mrg 	sucs_arg = 4;					\
   3075  1.1  mrg 	fail_arg = 5;					\
   3076  1.1  mrg 	pvalid_models = all_models;			\
   3077  1.1  mrg 	arg2 = 1
   3078  1.1  mrg 
   3079  1.1  mrg     case BUILTIN_ACCESS_SIZE_FNSPEC (1);
   3080  1.1  mrg       break;
   3081  1.1  mrg     case BUILTIN_ACCESS_SIZE_FNSPEC (2);
   3082  1.1  mrg       break;
   3083  1.1  mrg     case BUILTIN_ACCESS_SIZE_FNSPEC (4);
   3084  1.1  mrg       break;
   3085  1.1  mrg     case BUILTIN_ACCESS_SIZE_FNSPEC (8);
   3086  1.1  mrg       break;
   3087  1.1  mrg     case BUILTIN_ACCESS_SIZE_FNSPEC (16);
   3088  1.1  mrg       break;
   3089  1.1  mrg 
   3090  1.1  mrg     case BUILT_IN_ATOMIC_CLEAR:
   3091  1.1  mrg       sucs_arg = 1;
   3092  1.1  mrg       pvalid_models = flag_clr_models;
   3093  1.1  mrg       break;
   3094  1.1  mrg 
   3095  1.1  mrg     default:
   3096  1.1  mrg       return false;
   3097  1.1  mrg     }
   3098  1.1  mrg 
   3099  1.1  mrg   unsigned nargs = gimple_call_num_args (stmt);
   3100  1.1  mrg   if (sucs_arg < nargs)
   3101  1.1  mrg     {
   3102  1.1  mrg       tree ord_sucs = gimple_call_arg (stmt, sucs_arg);
   3103  1.1  mrg       tree ord_fail = NULL_TREE;
   3104  1.1  mrg       if (fail_arg < nargs)
   3105  1.1  mrg 	ord_fail = gimple_call_arg (stmt, fail_arg);
   3106  1.1  mrg       check_atomic_memmodel (stmt, ord_sucs, ord_fail, pvalid_models);
   3107  1.1  mrg     }
   3108  1.1  mrg 
   3109  1.1  mrg   if (!bytes)
   3110  1.1  mrg     return true;
   3111  1.1  mrg 
   3112  1.1  mrg   tree size = build_int_cstu (sizetype, bytes);
   3113  1.1  mrg   tree dst = gimple_call_arg (stmt, 0);
   3114  1.1  mrg   check_memop_access (stmt, dst, NULL_TREE, size);
   3115  1.1  mrg 
   3116  1.1  mrg   if (arg2 != UINT_MAX)
   3117  1.1  mrg     {
   3118  1.1  mrg       tree dst = gimple_call_arg (stmt, arg2);
   3119  1.1  mrg       check_memop_access (stmt, dst, NULL_TREE, size);
   3120  1.1  mrg     }
   3121  1.1  mrg 
   3122  1.1  mrg   return true;
   3123  1.1  mrg }
   3124  1.1  mrg 
   3125  1.1  mrg /* Check call STMT to a built-in function for invalid accesses.  Return
   3126  1.1  mrg    true if a call has been handled.  */
   3127  1.1  mrg 
   3128  1.1  mrg bool
   3129  1.1  mrg pass_waccess::check_builtin (gcall *stmt)
   3130  1.1  mrg {
   3131  1.1  mrg   tree callee = gimple_call_fndecl (stmt);
   3132  1.1  mrg   if (!callee)
   3133  1.1  mrg     return false;
   3134  1.1  mrg 
   3135  1.1  mrg   switch (DECL_FUNCTION_CODE (callee))
   3136  1.1  mrg     {
   3137  1.1  mrg     case BUILT_IN_ALLOCA:
   3138  1.1  mrg     case BUILT_IN_ALLOCA_WITH_ALIGN:
   3139  1.1  mrg     case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX:
   3140  1.1  mrg       check_alloca (stmt);
   3141  1.1  mrg       return true;
   3142  1.1  mrg 
   3143  1.1  mrg     case BUILT_IN_EXECL:
   3144  1.1  mrg     case BUILT_IN_EXECLE:
   3145  1.1  mrg     case BUILT_IN_EXECLP:
   3146  1.1  mrg     case BUILT_IN_EXECV:
   3147  1.1  mrg     case BUILT_IN_EXECVE:
   3148  1.1  mrg     case BUILT_IN_EXECVP:
   3149  1.1  mrg       check_read_access (stmt, call_arg (stmt, 0));
   3150  1.1  mrg       return true;
   3151  1.1  mrg 
   3152  1.1  mrg     case BUILT_IN_FREE:
   3153  1.1  mrg     case BUILT_IN_REALLOC:
   3154  1.1  mrg       if (!m_early_checks_p)
   3155  1.1  mrg 	{
   3156  1.1  mrg 	  tree arg = call_arg (stmt, 0);
   3157  1.1  mrg 	  if (TREE_CODE (arg) == SSA_NAME)
   3158  1.1  mrg 	    check_pointer_uses (stmt, arg);
   3159  1.1  mrg 	}
   3160  1.1  mrg       return true;
   3161  1.1  mrg 
   3162  1.1  mrg     case BUILT_IN_GETTEXT:
   3163  1.1  mrg     case BUILT_IN_PUTS:
   3164  1.1  mrg     case BUILT_IN_PUTS_UNLOCKED:
   3165  1.1  mrg     case BUILT_IN_STRDUP:
   3166  1.1  mrg       check_read_access (stmt, call_arg (stmt, 0));
   3167  1.1  mrg       return true;
   3168  1.1  mrg 
   3169  1.1  mrg     case BUILT_IN_INDEX:
   3170  1.1  mrg     case BUILT_IN_RINDEX:
   3171  1.1  mrg     case BUILT_IN_STRCHR:
   3172  1.1  mrg     case BUILT_IN_STRRCHR:
   3173  1.1  mrg     case BUILT_IN_STRLEN:
   3174  1.1  mrg       check_read_access (stmt, call_arg (stmt, 0));
   3175  1.1  mrg       return true;
   3176  1.1  mrg 
   3177  1.1  mrg     case BUILT_IN_FPUTS:
   3178  1.1  mrg     case BUILT_IN_FPUTS_UNLOCKED:
   3179  1.1  mrg       check_read_access (stmt, call_arg (stmt, 0));
   3180  1.1  mrg       return true;
   3181  1.1  mrg 
   3182  1.1  mrg     case BUILT_IN_STRNDUP:
   3183  1.1  mrg     case BUILT_IN_STRNLEN:
   3184  1.1  mrg       {
   3185  1.1  mrg 	tree str = call_arg (stmt, 0);
   3186  1.1  mrg 	tree len = call_arg (stmt, 1);
   3187  1.1  mrg 	check_read_access (stmt, str, len);
   3188  1.1  mrg 	return true;
   3189  1.1  mrg       }
   3190  1.1  mrg 
   3191  1.1  mrg     case BUILT_IN_STRCAT:
   3192  1.1  mrg       check_strcat (stmt);
   3193  1.1  mrg       return true;
   3194  1.1  mrg 
   3195  1.1  mrg     case BUILT_IN_STRNCAT:
   3196  1.1  mrg       check_strncat (stmt);
   3197  1.1  mrg       return true;
   3198  1.1  mrg 
   3199  1.1  mrg     case BUILT_IN_STPCPY:
   3200  1.1  mrg     case BUILT_IN_STRCPY:
   3201  1.1  mrg       check_stxcpy (stmt);
   3202  1.1  mrg       return true;
   3203  1.1  mrg 
   3204  1.1  mrg     case BUILT_IN_STPNCPY:
   3205  1.1  mrg     case BUILT_IN_STRNCPY:
   3206  1.1  mrg       check_stxncpy (stmt);
   3207  1.1  mrg       return true;
   3208  1.1  mrg 
   3209  1.1  mrg     case BUILT_IN_STRCASECMP:
   3210  1.1  mrg     case BUILT_IN_STRCMP:
   3211  1.1  mrg     case BUILT_IN_STRPBRK:
   3212  1.1  mrg     case BUILT_IN_STRSPN:
   3213  1.1  mrg     case BUILT_IN_STRCSPN:
   3214  1.1  mrg     case BUILT_IN_STRSTR:
   3215  1.1  mrg       check_read_access (stmt, call_arg (stmt, 0));
   3216  1.1  mrg       check_read_access (stmt, call_arg (stmt, 1));
   3217  1.1  mrg       return true;
   3218  1.1  mrg 
   3219  1.1  mrg     case BUILT_IN_STRNCASECMP:
   3220  1.1  mrg     case BUILT_IN_STRNCMP:
   3221  1.1  mrg       check_strncmp (stmt);
   3222  1.1  mrg       return true;
   3223  1.1  mrg 
   3224  1.1  mrg     case BUILT_IN_MEMCMP:
   3225  1.1  mrg       {
   3226  1.1  mrg 	tree a1 = call_arg (stmt, 0);
   3227  1.1  mrg 	tree a2 = call_arg (stmt, 1);
   3228  1.1  mrg 	tree len = call_arg (stmt, 2);
   3229  1.1  mrg 	check_read_access (stmt, a1, len, 0);
   3230  1.1  mrg 	check_read_access (stmt, a2, len, 0);
   3231  1.1  mrg 	return true;
   3232  1.1  mrg       }
   3233  1.1  mrg 
   3234  1.1  mrg     case BUILT_IN_MEMCPY:
   3235  1.1  mrg     case BUILT_IN_MEMPCPY:
   3236  1.1  mrg     case BUILT_IN_MEMMOVE:
   3237  1.1  mrg       {
   3238  1.1  mrg 	tree dst = call_arg (stmt, 0);
   3239  1.1  mrg 	tree src = call_arg (stmt, 1);
   3240  1.1  mrg 	tree len = call_arg (stmt, 2);
   3241  1.1  mrg 	check_memop_access (stmt, dst, src, len);
   3242  1.1  mrg 	return true;
   3243  1.1  mrg       }
   3244  1.1  mrg 
   3245  1.1  mrg     case BUILT_IN_MEMCHR:
   3246  1.1  mrg       {
   3247  1.1  mrg 	tree src = call_arg (stmt, 0);
   3248  1.1  mrg 	tree len = call_arg (stmt, 2);
   3249  1.1  mrg 	check_read_access (stmt, src, len, 0);
   3250  1.1  mrg 	return true;
   3251  1.1  mrg       }
   3252  1.1  mrg 
   3253  1.1  mrg     case BUILT_IN_MEMSET:
   3254  1.1  mrg       {
   3255  1.1  mrg 	tree dst = call_arg (stmt, 0);
   3256  1.1  mrg 	tree len = call_arg (stmt, 2);
   3257  1.1  mrg 	check_memop_access (stmt, dst, NULL_TREE, len);
   3258  1.1  mrg 	return true;
   3259  1.1  mrg       }
   3260  1.1  mrg 
   3261  1.1  mrg     default:
   3262  1.1  mrg       if (check_atomic_builtin (stmt))
   3263  1.1  mrg 	return true;
   3264  1.1  mrg       break;
   3265  1.1  mrg     }
   3266  1.1  mrg 
   3267  1.1  mrg   return false;
   3268  1.1  mrg }
   3269  1.1  mrg 
   3270  1.1  mrg /* Returns the type of the argument ARGNO to function with type FNTYPE
   3271  1.1  mrg    or null when the type cannot be determined or no such argument exists.  */
   3272  1.1  mrg 
   3273  1.1  mrg static tree
   3274  1.1  mrg fntype_argno_type (tree fntype, unsigned argno)
   3275  1.1  mrg {
   3276  1.1  mrg   if (!prototype_p (fntype))
   3277  1.1  mrg     return NULL_TREE;
   3278  1.1  mrg 
   3279  1.1  mrg   tree argtype;
   3280  1.1  mrg   function_args_iterator it;
   3281  1.1  mrg   FOREACH_FUNCTION_ARGS (fntype, argtype, it)
   3282  1.1  mrg     if (argno-- == 0)
   3283  1.1  mrg       return argtype;
   3284  1.1  mrg 
   3285  1.1  mrg   return NULL_TREE;
   3286  1.1  mrg }
   3287  1.1  mrg 
   3288  1.1  mrg /* Helper to append the "human readable" attribute access specification
   3289  1.1  mrg    described by ACCESS to the array ATTRSTR with size STRSIZE.  Used in
   3290  1.1  mrg    diagnostics.  */
   3291  1.1  mrg 
   3292  1.1  mrg static inline void
   3293  1.1  mrg append_attrname (const std::pair<int, attr_access> &access,
   3294  1.1  mrg 		 char *attrstr, size_t strsize)
   3295  1.1  mrg {
   3296  1.1  mrg   if (access.second.internal_p)
   3297  1.1  mrg     return;
   3298  1.1  mrg 
   3299  1.1  mrg   tree str = access.second.to_external_string ();
   3300  1.1  mrg   gcc_assert (strsize >= (size_t) TREE_STRING_LENGTH (str));
   3301  1.1  mrg   strcpy (attrstr, TREE_STRING_POINTER (str));
   3302  1.1  mrg }
   3303  1.1  mrg 
   3304  1.1  mrg /* Iterate over attribute access read-only, read-write, and write-only
   3305  1.1  mrg    arguments and diagnose past-the-end accesses and related problems
   3306  1.1  mrg    in the function call EXP.  */
   3307  1.1  mrg 
   3308  1.1  mrg void
   3309  1.1  mrg pass_waccess::maybe_check_access_sizes (rdwr_map *rwm, tree fndecl, tree fntype,
   3310  1.1  mrg 					gimple *stmt)
   3311  1.1  mrg {
   3312  1.1  mrg   if (warning_suppressed_p (stmt, OPT_Wnonnull)
   3313  1.1  mrg       || warning_suppressed_p (stmt, OPT_Wstringop_overflow_))
   3314  1.1  mrg     return;
   3315  1.1  mrg 
   3316  1.1  mrg   auto_diagnostic_group adg;
   3317  1.1  mrg 
   3318  1.1  mrg   /* Set if a warning has been issued for any argument (used to decide
   3319  1.1  mrg      whether to emit an informational note at the end).  */
   3320  1.1  mrg   opt_code opt_warned = no_warning;
   3321  1.1  mrg 
   3322  1.1  mrg   /* A string describing the attributes that the warnings issued by this
   3323  1.1  mrg      function apply to.  Used to print one informational note per function
   3324  1.1  mrg      call, rather than one per warning.  That reduces clutter.  */
   3325  1.1  mrg   char attrstr[80];
   3326  1.1  mrg   attrstr[0] = 0;
   3327  1.1  mrg 
   3328  1.1  mrg   for (rdwr_map::iterator it = rwm->begin (); it != rwm->end (); ++it)
   3329  1.1  mrg     {
   3330  1.1  mrg       std::pair<int, attr_access> access = *it;
   3331  1.1  mrg 
   3332  1.1  mrg       /* Get the function call arguments corresponding to the attribute's
   3333  1.1  mrg 	 positional arguments.  When both arguments have been specified
   3334  1.1  mrg 	 there will be two entries in *RWM, one for each.  They are
   3335  1.1  mrg 	 cross-referenced by their respective argument numbers in
   3336  1.1  mrg 	 ACCESS.PTRARG and ACCESS.SIZARG.  */
   3337  1.1  mrg       const int ptridx = access.second.ptrarg;
   3338  1.1  mrg       const int sizidx = access.second.sizarg;
   3339  1.1  mrg 
   3340  1.1  mrg       gcc_assert (ptridx != -1);
   3341  1.1  mrg       gcc_assert (access.first == ptridx || access.first == sizidx);
   3342  1.1  mrg 
   3343  1.1  mrg       /* The pointer is set to null for the entry corresponding to
   3344  1.1  mrg 	 the size argument.  Skip it.  It's handled when the entry
   3345  1.1  mrg 	 corresponding to the pointer argument comes up.  */
   3346  1.1  mrg       if (!access.second.ptr)
   3347  1.1  mrg 	continue;
   3348  1.1  mrg 
   3349  1.1  mrg       tree ptrtype = fntype_argno_type (fntype, ptridx);
   3350  1.1  mrg       if (!ptrtype)
   3351  1.1  mrg 	/* A function with a prototype was redeclared without one and
   3352  1.1  mrg 	   the prototype has been lost.  See pr102759.  Avoid dealing
   3353  1.1  mrg 	   with this pathological case.  */
   3354  1.1  mrg 	return;
   3355  1.1  mrg 
   3356  1.1  mrg       tree argtype = TREE_TYPE (ptrtype);
   3357  1.1  mrg 
   3358  1.1  mrg       /* The size of the access by the call in elements.  */
   3359  1.1  mrg       tree access_nelts;
   3360  1.1  mrg       if (sizidx == -1)
   3361  1.1  mrg 	{
   3362  1.1  mrg 	  /* If only the pointer attribute operand was specified and
   3363  1.1  mrg 	     not size, set SIZE to the greater of MINSIZE or size of
   3364  1.1  mrg 	     one element of the pointed to type to detect smaller
   3365  1.1  mrg 	     objects (null pointers are diagnosed in this case only
   3366  1.1  mrg 	     if the pointer is also declared with attribute nonnull.  */
   3367  1.1  mrg 	  if (access.second.minsize
   3368  1.1  mrg 	      && access.second.minsize != HOST_WIDE_INT_M1U)
   3369  1.1  mrg 	    access_nelts = build_int_cstu (sizetype, access.second.minsize);
   3370  1.1  mrg 	  else if (VOID_TYPE_P (argtype) && access.second.mode == access_none)
   3371  1.1  mrg 	    /* Treat access mode none on a void* argument as expecting
   3372  1.1  mrg 	       as little as zero bytes.  */
   3373  1.1  mrg 	    access_nelts = size_zero_node;
   3374  1.1  mrg 	  else
   3375  1.1  mrg 	    access_nelts = size_one_node;
   3376  1.1  mrg 	}
   3377  1.1  mrg       else
   3378  1.1  mrg 	access_nelts = rwm->get (sizidx)->size;
   3379  1.1  mrg 
   3380  1.1  mrg       /* Format the value or range to avoid an explosion of messages.  */
   3381  1.1  mrg       char sizstr[80];
   3382  1.1  mrg       tree sizrng[2] = { size_zero_node, build_all_ones_cst (sizetype) };
   3383  1.1  mrg       if (get_size_range (m_ptr_qry.rvals, access_nelts, stmt, sizrng, 1))
   3384  1.1  mrg 	{
   3385  1.1  mrg 	  char *s0 = print_generic_expr_to_str (sizrng[0]);
   3386  1.1  mrg 	  if (tree_int_cst_equal (sizrng[0], sizrng[1]))
   3387  1.1  mrg 	    {
   3388  1.1  mrg 	      gcc_checking_assert (strlen (s0) < sizeof sizstr);
   3389  1.1  mrg 	      strcpy (sizstr, s0);
   3390  1.1  mrg 	    }
   3391  1.1  mrg 	  else
   3392  1.1  mrg 	    {
   3393  1.1  mrg 	      char *s1 = print_generic_expr_to_str (sizrng[1]);
   3394  1.1  mrg 	      gcc_checking_assert (strlen (s0) + strlen (s1)
   3395  1.1  mrg 				   < sizeof sizstr - 4);
   3396  1.1  mrg 	      sprintf (sizstr, "[%.37s, %.37s]", s0, s1);
   3397  1.1  mrg 	      free (s1);
   3398  1.1  mrg 	    }
   3399  1.1  mrg 	  free (s0);
   3400  1.1  mrg 	}
   3401  1.1  mrg       else
   3402  1.1  mrg 	*sizstr = '\0';
   3403  1.1  mrg 
   3404  1.1  mrg       /* Set if a warning has been issued for the current argument.  */
   3405  1.1  mrg       opt_code arg_warned = no_warning;
   3406  1.1  mrg       location_t loc = get_location (stmt);
   3407  1.1  mrg       tree ptr = access.second.ptr;
   3408  1.1  mrg       if (*sizstr
   3409  1.1  mrg 	  && tree_int_cst_sgn (sizrng[0]) < 0
   3410  1.1  mrg 	  && tree_int_cst_sgn (sizrng[1]) < 0)
   3411  1.1  mrg 	{
   3412  1.1  mrg 	  /* Warn about negative sizes.  */
   3413  1.1  mrg 	  if (access.second.internal_p)
   3414  1.1  mrg 	    {
   3415  1.1  mrg 	      const std::string argtypestr
   3416  1.1  mrg 		= access.second.array_as_string (ptrtype);
   3417  1.1  mrg 
   3418  1.1  mrg 	      if (warning_at (loc, OPT_Wstringop_overflow_,
   3419  1.1  mrg 			      "bound argument %i value %s is "
   3420  1.1  mrg 			      "negative for a variable length array "
   3421  1.1  mrg 			      "argument %i of type %s",
   3422  1.1  mrg 			      sizidx + 1, sizstr,
   3423  1.1  mrg 			      ptridx + 1, argtypestr.c_str ()))
   3424  1.1  mrg 		arg_warned = OPT_Wstringop_overflow_;
   3425  1.1  mrg 	    }
   3426  1.1  mrg 	  else if (warning_at (loc, OPT_Wstringop_overflow_,
   3427  1.1  mrg 			       "argument %i value %s is negative",
   3428  1.1  mrg 			       sizidx + 1, sizstr))
   3429  1.1  mrg 	    arg_warned = OPT_Wstringop_overflow_;
   3430  1.1  mrg 
   3431  1.1  mrg 	  if (arg_warned != no_warning)
   3432  1.1  mrg 	    {
   3433  1.1  mrg 	      append_attrname (access, attrstr, sizeof attrstr);
   3434  1.1  mrg 	      /* Remember a warning has been issued and avoid warning
   3435  1.1  mrg 		 again below for the same attribute.  */
   3436  1.1  mrg 	      opt_warned = arg_warned;
   3437  1.1  mrg 	      continue;
   3438  1.1  mrg 	    }
   3439  1.1  mrg 	}
   3440  1.1  mrg 
   3441  1.1  mrg       /* The size of the access by the call in bytes.  */
   3442  1.1  mrg       tree access_size = NULL_TREE;
   3443  1.1  mrg       if (tree_int_cst_sgn (sizrng[0]) >= 0)
   3444  1.1  mrg 	{
   3445  1.1  mrg 	  if (COMPLETE_TYPE_P (argtype))
   3446  1.1  mrg 	    {
   3447  1.1  mrg 	      /* Multiply ACCESS_SIZE by the size of the type the pointer
   3448  1.1  mrg 		 argument points to.  If it's incomplete the size is used
   3449  1.1  mrg 		 as is.  */
   3450  1.1  mrg 	      if (tree argsize = TYPE_SIZE_UNIT (argtype))
   3451  1.1  mrg 		if (TREE_CODE (argsize) == INTEGER_CST)
   3452  1.1  mrg 		  {
   3453  1.1  mrg 		    const int prec = TYPE_PRECISION (sizetype);
   3454  1.1  mrg 		    wide_int minsize = wi::to_wide (sizrng[0], prec);
   3455  1.1  mrg 		    minsize *= wi::to_wide (argsize, prec);
   3456  1.1  mrg 		    access_size = wide_int_to_tree (sizetype, minsize);
   3457  1.1  mrg 		  }
   3458  1.1  mrg 	    }
   3459  1.1  mrg 	  else
   3460  1.1  mrg 	    access_size = access_nelts;
   3461  1.1  mrg 	}
   3462  1.1  mrg 
   3463  1.1  mrg       if (integer_zerop (ptr))
   3464  1.1  mrg 	{
   3465  1.1  mrg 	  if (sizidx >= 0 && tree_int_cst_sgn (sizrng[0]) > 0)
   3466  1.1  mrg 	    {
   3467  1.1  mrg 	      /* Warn about null pointers with positive sizes.  This is
   3468  1.1  mrg 		 different from also declaring the pointer argument with
   3469  1.1  mrg 		 attribute nonnull when the function accepts null pointers
   3470  1.1  mrg 		 only when the corresponding size is zero.  */
   3471  1.1  mrg 	      if (access.second.internal_p)
   3472  1.1  mrg 		{
   3473  1.1  mrg 		  const std::string argtypestr
   3474  1.1  mrg 		    = access.second.array_as_string (ptrtype);
   3475  1.1  mrg 
   3476  1.1  mrg 		  if (warning_at (loc, OPT_Wnonnull,
   3477  1.1  mrg 				  "argument %i of variable length "
   3478  1.1  mrg 				  "array %s is null but "
   3479  1.1  mrg 				  "the corresponding bound argument "
   3480  1.1  mrg 				  "%i value is %s",
   3481  1.1  mrg 				  ptridx + 1, argtypestr.c_str (),
   3482  1.1  mrg 				  sizidx + 1, sizstr))
   3483  1.1  mrg 		    arg_warned = OPT_Wnonnull;
   3484  1.1  mrg 		}
   3485  1.1  mrg 	      else if (warning_at (loc, OPT_Wnonnull,
   3486  1.1  mrg 				   "argument %i is null but "
   3487  1.1  mrg 				   "the corresponding size argument "
   3488  1.1  mrg 				   "%i value is %s",
   3489  1.1  mrg 				   ptridx + 1, sizidx + 1, sizstr))
   3490  1.1  mrg 		arg_warned = OPT_Wnonnull;
   3491  1.1  mrg 	    }
   3492  1.1  mrg 	  else if (access_size && access.second.static_p)
   3493  1.1  mrg 	    {
   3494  1.1  mrg 	      /* Warn about null pointers for [static N] array arguments
   3495  1.1  mrg 		 but do not warn for ordinary (i.e., nonstatic) arrays.  */
   3496  1.1  mrg 	      if (warning_at (loc, OPT_Wnonnull,
   3497  1.1  mrg 			      "argument %i to %<%T[static %E]%> "
   3498  1.1  mrg 			      "is null where non-null expected",
   3499  1.1  mrg 			      ptridx + 1, argtype, access_nelts))
   3500  1.1  mrg 		arg_warned = OPT_Wnonnull;
   3501  1.1  mrg 	    }
   3502  1.1  mrg 
   3503  1.1  mrg 	  if (arg_warned != no_warning)
   3504  1.1  mrg 	    {
   3505  1.1  mrg 	      append_attrname (access, attrstr, sizeof attrstr);
   3506  1.1  mrg 	      /* Remember a warning has been issued and avoid warning
   3507  1.1  mrg 		 again below for the same attribute.  */
   3508  1.1  mrg 	      opt_warned = OPT_Wnonnull;
   3509  1.1  mrg 	      continue;
   3510  1.1  mrg 	    }
   3511  1.1  mrg 	}
   3512  1.1  mrg 
   3513  1.1  mrg       access_data data (m_ptr_qry.rvals, stmt, access.second.mode,
   3514  1.1  mrg 			NULL_TREE, false, NULL_TREE, false);
   3515  1.1  mrg       access_ref* const pobj = (access.second.mode == access_write_only
   3516  1.1  mrg 				? &data.dst : &data.src);
   3517  1.1  mrg       tree objsize = compute_objsize (ptr, stmt, 1, pobj, &m_ptr_qry);
   3518  1.1  mrg 
   3519  1.1  mrg       /* The size of the destination or source object.  */
   3520  1.1  mrg       tree dstsize = NULL_TREE, srcsize = NULL_TREE;
   3521  1.1  mrg       if (access.second.mode == access_read_only
   3522  1.1  mrg 	  || access.second.mode == access_none)
   3523  1.1  mrg 	{
   3524  1.1  mrg 	  /* For a read-only argument there is no destination.  For
   3525  1.1  mrg 	     no access, set the source as well and differentiate via
   3526  1.1  mrg 	     the access flag below.  */
   3527  1.1  mrg 	  srcsize = objsize;
   3528  1.1  mrg 	  if (access.second.mode == access_read_only
   3529  1.1  mrg 	      || access.second.mode == access_none)
   3530  1.1  mrg 	    {
   3531  1.1  mrg 	      /* For a read-only attribute there is no destination so
   3532  1.1  mrg 		 clear OBJSIZE.  This emits "reading N bytes" kind of
   3533  1.1  mrg 		 diagnostics instead of the "writing N bytes" kind,
   3534  1.1  mrg 		 unless MODE is none.  */
   3535  1.1  mrg 	      objsize = NULL_TREE;
   3536  1.1  mrg 	    }
   3537  1.1  mrg 	}
   3538  1.1  mrg       else
   3539  1.1  mrg 	dstsize = objsize;
   3540  1.1  mrg 
   3541  1.1  mrg       /* Clear the no-warning bit in case it was set by check_access
   3542  1.1  mrg 	 in a prior iteration so that accesses via different arguments
   3543  1.1  mrg 	 are diagnosed.  */
   3544  1.1  mrg       suppress_warning (stmt, OPT_Wstringop_overflow_, false);
   3545  1.1  mrg       access_mode mode = data.mode;
   3546  1.1  mrg       if (mode == access_deferred)
   3547  1.1  mrg 	mode = TYPE_READONLY (argtype) ? access_read_only : access_read_write;
   3548  1.1  mrg       check_access (stmt, access_size, /*maxread=*/ NULL_TREE, srcsize,
   3549  1.1  mrg 		    dstsize, mode, &data, m_ptr_qry.rvals);
   3550  1.1  mrg 
   3551  1.1  mrg       if (warning_suppressed_p (stmt, OPT_Wstringop_overflow_))
   3552  1.1  mrg 	opt_warned = OPT_Wstringop_overflow_;
   3553  1.1  mrg       if (opt_warned != no_warning)
   3554  1.1  mrg 	{
   3555  1.1  mrg 	  if (access.second.internal_p)
   3556  1.1  mrg 	    {
   3557  1.1  mrg 	      unsigned HOST_WIDE_INT nelts =
   3558  1.1  mrg 		access_nelts ? access.second.minsize : HOST_WIDE_INT_M1U;
   3559  1.1  mrg 	      tree arrtype = build_printable_array_type (argtype, nelts);
   3560  1.1  mrg 	      inform (loc, "referencing argument %u of type %qT",
   3561  1.1  mrg 		      ptridx + 1, arrtype);
   3562  1.1  mrg 	    }
   3563  1.1  mrg 	  else
   3564  1.1  mrg 	    /* If check_access issued a warning above, append the relevant
   3565  1.1  mrg 	       attribute to the string.  */
   3566  1.1  mrg 	    append_attrname (access, attrstr, sizeof attrstr);
   3567  1.1  mrg 	}
   3568  1.1  mrg     }
   3569  1.1  mrg 
   3570  1.1  mrg   if (*attrstr)
   3571  1.1  mrg     {
   3572  1.1  mrg       if (fndecl)
   3573  1.1  mrg 	inform (get_location (fndecl),
   3574  1.1  mrg 		"in a call to function %qD declared with attribute %qs",
   3575  1.1  mrg 		fndecl, attrstr);
   3576  1.1  mrg       else
   3577  1.1  mrg 	inform (get_location (stmt),
   3578  1.1  mrg 		"in a call with type %qT and attribute %qs",
   3579  1.1  mrg 		fntype, attrstr);
   3580  1.1  mrg     }
   3581  1.1  mrg   else if (opt_warned != no_warning)
   3582  1.1  mrg     {
   3583  1.1  mrg       if (fndecl)
   3584  1.1  mrg 	inform (get_location (fndecl),
   3585  1.1  mrg 		"in a call to function %qD", fndecl);
   3586  1.1  mrg       else
   3587  1.1  mrg 	inform (get_location (stmt),
   3588  1.1  mrg 		"in a call with type %qT", fntype);
   3589  1.1  mrg     }
   3590  1.1  mrg 
   3591  1.1  mrg   /* Set the bit in case it was cleared and not set above.  */
   3592  1.1  mrg   if (opt_warned != no_warning)
   3593  1.1  mrg     suppress_warning (stmt, opt_warned);
   3594  1.1  mrg }
   3595  1.1  mrg 
   3596  1.1  mrg /* Check call STMT to an ordinary (non-built-in) function for invalid
   3597  1.1  mrg    accesses.  Return true if a call has been handled.  */
   3598  1.1  mrg 
   3599  1.1  mrg bool
   3600  1.1  mrg pass_waccess::check_call_access (gcall *stmt)
   3601  1.1  mrg {
   3602  1.1  mrg   tree fntype = gimple_call_fntype (stmt);
   3603  1.1  mrg   if (!fntype)
   3604  1.1  mrg     return false;
   3605  1.1  mrg 
   3606  1.1  mrg   tree fntypeattrs = TYPE_ATTRIBUTES (fntype);
   3607  1.1  mrg   if (!fntypeattrs)
   3608  1.1  mrg     return false;
   3609  1.1  mrg 
   3610  1.1  mrg   /* Map of attribute access specifications for function arguments.  */
   3611  1.1  mrg   rdwr_map rdwr_idx;
   3612  1.1  mrg   init_attr_rdwr_indices (&rdwr_idx, fntypeattrs);
   3613  1.1  mrg 
   3614  1.1  mrg   unsigned nargs = call_nargs (stmt);
   3615  1.1  mrg   for (unsigned i = 0; i != nargs; ++i)
   3616  1.1  mrg     {
   3617  1.1  mrg       tree arg = call_arg (stmt, i);
   3618  1.1  mrg 
   3619  1.1  mrg       /* Save the actual argument that corresponds to the access attribute
   3620  1.1  mrg 	 operand for later processing.  */
   3621  1.1  mrg       if (attr_access *access = rdwr_idx.get (i))
   3622  1.1  mrg 	{
   3623  1.1  mrg 	  if (POINTER_TYPE_P (TREE_TYPE (arg)))
   3624  1.1  mrg 	    {
   3625  1.1  mrg 	      access->ptr = arg;
   3626  1.1  mrg 	      /* A nonnull ACCESS->SIZE contains VLA bounds.  */
   3627  1.1  mrg 	    }
   3628  1.1  mrg 	  else
   3629  1.1  mrg 	    {
   3630  1.1  mrg 	      access->size = arg;
   3631  1.1  mrg 	      gcc_assert (access->ptr == NULL_TREE);
   3632  1.1  mrg 	    }
   3633  1.1  mrg 	}
   3634  1.1  mrg     }
   3635  1.1  mrg 
   3636  1.1  mrg   /* Check attribute access arguments.  */
   3637  1.1  mrg   tree fndecl = gimple_call_fndecl (stmt);
   3638  1.1  mrg   maybe_check_access_sizes (&rdwr_idx, fndecl, fntype, stmt);
   3639  1.1  mrg 
   3640  1.1  mrg   check_alloc_size_call (stmt);
   3641  1.1  mrg   return true;
   3642  1.1  mrg }
   3643  1.1  mrg 
   3644  1.1  mrg /* Check arguments in a call STMT for attribute nonstring.  */
   3645  1.1  mrg 
   3646  1.1  mrg static void
   3647  1.1  mrg check_nonstring_args (gcall *stmt)
   3648  1.1  mrg {
   3649  1.1  mrg   tree fndecl = gimple_call_fndecl (stmt);
   3650  1.1  mrg 
   3651  1.1  mrg   /* Detect passing non-string arguments to functions expecting
   3652  1.1  mrg      nul-terminated strings.  */
   3653  1.1  mrg   maybe_warn_nonstring_arg (fndecl, stmt);
   3654  1.1  mrg }
   3655  1.1  mrg 
   3656  1.1  mrg /* Issue a warning if a deallocation function such as free, realloc,
   3657  1.1  mrg    or C++ operator delete is called with an argument not returned by
   3658  1.1  mrg    a matching allocation function such as malloc or the corresponding
   3659  1.1  mrg    form of C++ operator new.  */
   3660  1.1  mrg 
   3661  1.1  mrg void
   3662  1.1  mrg pass_waccess::maybe_check_dealloc_call (gcall *call)
   3663  1.1  mrg {
   3664  1.1  mrg   tree fndecl = gimple_call_fndecl (call);
   3665  1.1  mrg   if (!fndecl)
   3666  1.1  mrg     return;
   3667  1.1  mrg 
   3668  1.1  mrg   unsigned argno = fndecl_dealloc_argno (fndecl);
   3669  1.1  mrg   if ((unsigned) call_nargs (call) <= argno)
   3670  1.1  mrg     return;
   3671  1.1  mrg 
   3672  1.1  mrg   tree ptr = gimple_call_arg (call, argno);
   3673  1.1  mrg   if (integer_zerop (ptr))
   3674  1.1  mrg     return;
   3675  1.1  mrg 
   3676  1.1  mrg   access_ref aref;
   3677  1.1  mrg   if (!compute_objsize (ptr, call, 0, &aref, &m_ptr_qry))
   3678  1.1  mrg     return;
   3679  1.1  mrg 
   3680  1.1  mrg   tree ref = aref.ref;
   3681  1.1  mrg   if (integer_zerop (ref))
   3682  1.1  mrg     return;
   3683  1.1  mrg 
   3684  1.1  mrg   tree dealloc_decl = fndecl;
   3685  1.1  mrg   location_t loc = gimple_location (call);
   3686  1.1  mrg 
   3687  1.1  mrg   if (DECL_P (ref) || EXPR_P (ref))
   3688  1.1  mrg     {
   3689  1.1  mrg       /* Diagnose freeing a declared object.  */
   3690  1.1  mrg       if (aref.ref_declared ()
   3691  1.1  mrg 	  && warning_at (loc, OPT_Wfree_nonheap_object,
   3692  1.1  mrg 			 "%qD called on unallocated object %qD",
   3693  1.1  mrg 			 dealloc_decl, ref))
   3694  1.1  mrg 	{
   3695  1.1  mrg 	  inform (get_location (ref), "declared here");
   3696  1.1  mrg 	  return;
   3697  1.1  mrg 	}
   3698  1.1  mrg 
   3699  1.1  mrg       /* Diagnose freeing a pointer that includes a positive offset.
   3700  1.1  mrg 	 Such a pointer cannot refer to the beginning of an allocated
   3701  1.1  mrg 	 object.  A negative offset may refer to it.  */
   3702  1.1  mrg       if (aref.sizrng[0] != aref.sizrng[1]
   3703  1.1  mrg 	  && warn_dealloc_offset (loc, call, aref))
   3704  1.1  mrg 	return;
   3705  1.1  mrg     }
   3706  1.1  mrg   else if (CONSTANT_CLASS_P (ref))
   3707  1.1  mrg     {
   3708  1.1  mrg       if (warning_at (loc, OPT_Wfree_nonheap_object,
   3709  1.1  mrg 		      "%qD called on a pointer to an unallocated "
   3710  1.1  mrg 		      "object %qE", dealloc_decl, ref))
   3711  1.1  mrg 	{
   3712  1.1  mrg 	  if (TREE_CODE (ptr) == SSA_NAME)
   3713  1.1  mrg 	    {
   3714  1.1  mrg 	      gimple *def_stmt = SSA_NAME_DEF_STMT (ptr);
   3715  1.1  mrg 	      if (is_gimple_assign (def_stmt))
   3716  1.1  mrg 		{
   3717  1.1  mrg 		  location_t loc = gimple_location (def_stmt);
   3718  1.1  mrg 		  inform (loc, "assigned here");
   3719  1.1  mrg 		}
   3720  1.1  mrg 	    }
   3721  1.1  mrg 	  return;
   3722  1.1  mrg 	}
   3723  1.1  mrg     }
   3724  1.1  mrg   else if (TREE_CODE (ref) == SSA_NAME)
   3725  1.1  mrg     {
   3726  1.1  mrg       /* Also warn if the pointer argument refers to the result
   3727  1.1  mrg 	 of an allocation call like alloca or VLA.  */
   3728  1.1  mrg       gimple *def_stmt = SSA_NAME_DEF_STMT (ref);
   3729  1.1  mrg       if (!def_stmt)
   3730  1.1  mrg 	return;
   3731  1.1  mrg 
   3732  1.1  mrg       if (is_gimple_call (def_stmt))
   3733  1.1  mrg 	{
   3734  1.1  mrg 	  bool warned = false;
   3735  1.1  mrg 	  if (gimple_call_alloc_p (def_stmt))
   3736  1.1  mrg 	    {
   3737  1.1  mrg 	      if (matching_alloc_calls_p (def_stmt, dealloc_decl))
   3738  1.1  mrg 		{
   3739  1.1  mrg 		  if (warn_dealloc_offset (loc, call, aref))
   3740  1.1  mrg 		    return;
   3741  1.1  mrg 		}
   3742  1.1  mrg 	      else
   3743  1.1  mrg 		{
   3744  1.1  mrg 		  tree alloc_decl = gimple_call_fndecl (def_stmt);
   3745  1.1  mrg 		  const opt_code opt =
   3746  1.1  mrg 		    (DECL_IS_OPERATOR_NEW_P (alloc_decl)
   3747  1.1  mrg 		     || DECL_IS_OPERATOR_DELETE_P (dealloc_decl)
   3748  1.1  mrg 		     ? OPT_Wmismatched_new_delete
   3749  1.1  mrg 		     : OPT_Wmismatched_dealloc);
   3750  1.1  mrg 		  warned = warning_at (loc, opt,
   3751  1.1  mrg 				       "%qD called on pointer returned "
   3752  1.1  mrg 				       "from a mismatched allocation "
   3753  1.1  mrg 				       "function", dealloc_decl);
   3754  1.1  mrg 		}
   3755  1.1  mrg 	    }
   3756  1.1  mrg 	  else if (gimple_call_builtin_p (def_stmt, BUILT_IN_ALLOCA)
   3757  1.1  mrg 		   || gimple_call_builtin_p (def_stmt,
   3758  1.1  mrg 					     BUILT_IN_ALLOCA_WITH_ALIGN))
   3759  1.1  mrg 	    warned = warning_at (loc, OPT_Wfree_nonheap_object,
   3760  1.1  mrg 				 "%qD called on pointer to "
   3761  1.1  mrg 				 "an unallocated object",
   3762  1.1  mrg 				 dealloc_decl);
   3763  1.1  mrg 	  else if (warn_dealloc_offset (loc, call, aref))
   3764  1.1  mrg 	    return;
   3765  1.1  mrg 
   3766  1.1  mrg 	  if (warned)
   3767  1.1  mrg 	    {
   3768  1.1  mrg 	      tree fndecl = gimple_call_fndecl (def_stmt);
   3769  1.1  mrg 	      inform (gimple_location (def_stmt),
   3770  1.1  mrg 		      "returned from %qD", fndecl);
   3771  1.1  mrg 	      return;
   3772  1.1  mrg 	    }
   3773  1.1  mrg 	}
   3774  1.1  mrg       else if (gimple_nop_p (def_stmt))
   3775  1.1  mrg 	{
   3776  1.1  mrg 	  ref = SSA_NAME_VAR (ref);
   3777  1.1  mrg 	  /* Diagnose freeing a pointer that includes a positive offset.  */
   3778  1.1  mrg 	  if (TREE_CODE (ref) == PARM_DECL
   3779  1.1  mrg 	      && !aref.deref
   3780  1.1  mrg 	      && aref.sizrng[0] != aref.sizrng[1]
   3781  1.1  mrg 	      && aref.offrng[0] > 0 && aref.offrng[1] > 0
   3782  1.1  mrg 	      && warn_dealloc_offset (loc, call, aref))
   3783  1.1  mrg 	    return;
   3784  1.1  mrg 	}
   3785  1.1  mrg     }
   3786  1.1  mrg }
   3787  1.1  mrg 
   3788  1.1  mrg /* Return true if either USE_STMT's basic block (that of a pointer's use)
   3789  1.1  mrg    is dominated by INVAL_STMT's (that of a pointer's invalidating statement,
   3790  1.1  mrg    which is either a clobber or a deallocation call), or if they're in
   3791  1.1  mrg    the same block, USE_STMT follows INVAL_STMT.  */
   3792  1.1  mrg 
   3793  1.1  mrg bool
   3794  1.1  mrg pass_waccess::use_after_inval_p (gimple *inval_stmt, gimple *use_stmt,
   3795  1.1  mrg 				 bool last_block /* = false */)
   3796  1.1  mrg {
   3797  1.1  mrg   tree clobvar =
   3798  1.1  mrg     gimple_clobber_p (inval_stmt) ? gimple_assign_lhs (inval_stmt) : NULL_TREE;
   3799  1.1  mrg 
   3800  1.1  mrg   basic_block inval_bb = gimple_bb (inval_stmt);
   3801  1.1  mrg   basic_block use_bb = gimple_bb (use_stmt);
   3802  1.1  mrg 
   3803  1.1  mrg   if (!inval_bb || !use_bb)
   3804  1.1  mrg     return false;
   3805  1.1  mrg 
   3806  1.1  mrg   if (inval_bb != use_bb)
   3807  1.1  mrg     {
   3808  1.1  mrg       if (dominated_by_p (CDI_DOMINATORS, use_bb, inval_bb))
   3809  1.1  mrg 	return true;
   3810  1.1  mrg 
   3811  1.1  mrg       if (!clobvar || !last_block)
   3812  1.1  mrg 	return false;
   3813  1.1  mrg 
   3814  1.1  mrg       /* Proceed only when looking for uses of dangling pointers.  */
   3815  1.1  mrg       auto gsi = gsi_for_stmt (use_stmt);
   3816  1.1  mrg 
   3817  1.1  mrg       /* A use statement in the last basic block in a function or one that
   3818  1.1  mrg 	 falls through to it is after any other prior clobber of the used
   3819  1.1  mrg 	 variable unless it's followed by a clobber of the same variable. */
   3820  1.1  mrg       basic_block bb = use_bb;
   3821  1.1  mrg       while (bb != inval_bb
   3822  1.1  mrg 	     && single_succ_p (bb)
   3823  1.1  mrg 	     && !(single_succ_edge (bb)->flags
   3824  1.1  mrg 		  & (EDGE_EH | EDGE_ABNORMAL | EDGE_DFS_BACK)))
   3825  1.1  mrg 	{
   3826  1.1  mrg 	  for (; !gsi_end_p (gsi); gsi_next_nondebug (&gsi))
   3827  1.1  mrg 	    {
   3828  1.1  mrg 	      gimple *stmt = gsi_stmt (gsi);
   3829  1.1  mrg 	      if (gimple_clobber_p (stmt))
   3830  1.1  mrg 		{
   3831  1.1  mrg 		  if (clobvar == gimple_assign_lhs (stmt))
   3832  1.1  mrg 		    /* The use is followed by a clobber.  */
   3833  1.1  mrg 		    return false;
   3834  1.1  mrg 		}
   3835  1.1  mrg 	    }
   3836  1.1  mrg 
   3837  1.1  mrg 	  bb = single_succ (bb);
   3838  1.1  mrg 	  gsi = gsi_start_bb (bb);
   3839  1.1  mrg 	}
   3840  1.1  mrg 
   3841  1.1  mrg       /* The use is one of a dangling pointer if a clobber of the variable
   3842  1.1  mrg 	 [the pointer points to] has not been found before the function exit
   3843  1.1  mrg 	 point.  */
   3844  1.1  mrg       return bb == EXIT_BLOCK_PTR_FOR_FN (cfun);
   3845  1.1  mrg     }
   3846  1.1  mrg 
   3847  1.1  mrg   if (bitmap_set_bit (m_bb_uids_set, inval_bb->index))
   3848  1.1  mrg     /* The first time this basic block is visited assign increasing ids
   3849  1.1  mrg        to consecutive statements in it.  Use the ids to determine which
   3850  1.1  mrg        precedes which.  This avoids the linear traversal on subsequent
   3851  1.1  mrg        visits to the same block.  */
   3852  1.1  mrg     for (auto si = gsi_start_bb (inval_bb); !gsi_end_p (si);
   3853  1.1  mrg 	 gsi_next_nondebug (&si))
   3854  1.1  mrg       {
   3855  1.1  mrg 	gimple *stmt = gsi_stmt (si);
   3856  1.1  mrg 	unsigned uid = inc_gimple_stmt_max_uid (m_func);
   3857  1.1  mrg 	gimple_set_uid (stmt, uid);
   3858  1.1  mrg       }
   3859  1.1  mrg 
   3860  1.1  mrg   return gimple_uid (inval_stmt) < gimple_uid (use_stmt);
   3861  1.1  mrg }
   3862  1.1  mrg 
   3863  1.1  mrg /* Issue a warning for the USE_STMT of pointer or reference REF rendered
   3864  1.1  mrg    invalid by INVAL_STMT.  REF may be null when it's been optimized away.
   3865  1.1  mrg    When nonnull, INVAL_STMT is the deallocation function that rendered
   3866  1.1  mrg    the pointer or reference dangling.  Otherwise, VAR is the auto variable
   3867  1.1  mrg    (including an unnamed temporary such as a compound literal) whose
   3868  1.1  mrg    lifetime's rended it dangling.  MAYBE is true to issue the "maybe"
   3869  1.1  mrg    kind of warning.  EQUALITY is true when the pointer is used in
   3870  1.1  mrg    an equality expression.  */
   3871  1.1  mrg 
   3872  1.1  mrg void
   3873  1.1  mrg pass_waccess::warn_invalid_pointer (tree ref, gimple *use_stmt,
   3874  1.1  mrg 				    gimple *inval_stmt, tree var,
   3875  1.1  mrg 				    bool maybe, bool equality /* = false */)
   3876  1.1  mrg {
   3877  1.1  mrg   /* Avoid printing the unhelpful "<unknown>" in the diagnostics.  */
   3878  1.1  mrg   if (ref && TREE_CODE (ref) == SSA_NAME)
   3879  1.1  mrg     {
   3880  1.1  mrg       tree var = SSA_NAME_VAR (ref);
   3881  1.1  mrg       if (!var)
   3882  1.1  mrg 	ref = NULL_TREE;
   3883  1.1  mrg       /* Don't warn for cases like when a cdtor returns 'this' on ARM.  */
   3884  1.1  mrg       else if (warning_suppressed_p (var, OPT_Wuse_after_free))
   3885  1.1  mrg 	return;
   3886  1.1  mrg       else if (DECL_ARTIFICIAL (var))
   3887  1.1  mrg 	ref = NULL_TREE;
   3888  1.1  mrg     }
   3889  1.1  mrg 
   3890  1.1  mrg   location_t use_loc = gimple_location (use_stmt);
   3891  1.1  mrg   if (use_loc == UNKNOWN_LOCATION)
   3892  1.1  mrg     {
   3893  1.1  mrg       use_loc = m_func->function_end_locus;
   3894  1.1  mrg       if (!ref)
   3895  1.1  mrg 	/* Avoid issuing a warning with no context other than
   3896  1.1  mrg 	   the function.  That would make it difficult to debug
   3897  1.1  mrg 	   in any but very simple cases.  */
   3898  1.1  mrg 	return;
   3899  1.1  mrg     }
   3900  1.1  mrg 
   3901  1.1  mrg   if (is_gimple_call (inval_stmt))
   3902  1.1  mrg     {
   3903  1.1  mrg       if ((equality && warn_use_after_free < 3)
   3904  1.1  mrg 	  || (maybe && warn_use_after_free < 2)
   3905  1.1  mrg 	  || warning_suppressed_p (use_stmt, OPT_Wuse_after_free))
   3906  1.1  mrg 	return;
   3907  1.1  mrg 
   3908  1.1  mrg       const tree inval_decl = gimple_call_fndecl (inval_stmt);
   3909  1.1  mrg 
   3910  1.1  mrg       if ((ref && warning_at (use_loc, OPT_Wuse_after_free,
   3911  1.1  mrg 			      (maybe
   3912  1.1  mrg 			       ? G_("pointer %qE may be used after %qD")
   3913  1.1  mrg 			       : G_("pointer %qE used after %qD")),
   3914  1.1  mrg 			      ref, inval_decl))
   3915  1.1  mrg 	  || (!ref && warning_at (use_loc, OPT_Wuse_after_free,
   3916  1.1  mrg 			      (maybe
   3917  1.1  mrg 			       ? G_("pointer may be used after %qD")
   3918  1.1  mrg 			       : G_("pointer used after %qD")),
   3919  1.1  mrg 				  inval_decl)))
   3920  1.1  mrg 	{
   3921  1.1  mrg 	  location_t loc = gimple_location (inval_stmt);
   3922  1.1  mrg 	  inform (loc, "call to %qD here", inval_decl);
   3923  1.1  mrg 	  suppress_warning (use_stmt, OPT_Wuse_after_free);
   3924  1.1  mrg 	}
   3925  1.1  mrg       return;
   3926  1.1  mrg     }
   3927  1.1  mrg 
   3928  1.1  mrg   if (equality
   3929  1.1  mrg       || (maybe && warn_dangling_pointer < 2)
   3930  1.1  mrg       || warning_suppressed_p (use_stmt, OPT_Wdangling_pointer_))
   3931  1.1  mrg     return;
   3932  1.1  mrg 
   3933  1.1  mrg   if (DECL_NAME (var))
   3934  1.1  mrg     {
   3935  1.1  mrg       if ((ref
   3936  1.1  mrg 	   && warning_at (use_loc, OPT_Wdangling_pointer_,
   3937  1.1  mrg 			  (maybe
   3938  1.1  mrg 			   ? G_("dangling pointer %qE to %qD may be used")
   3939  1.1  mrg 			   : G_("using dangling pointer %qE to %qD")),
   3940  1.1  mrg 			  ref, var))
   3941  1.1  mrg 	  || (!ref
   3942  1.1  mrg 	      && warning_at (use_loc, OPT_Wdangling_pointer_,
   3943  1.1  mrg 			     (maybe
   3944  1.1  mrg 			      ? G_("dangling pointer to %qD may be used")
   3945  1.1  mrg 			      : G_("using a dangling pointer to %qD")),
   3946  1.1  mrg 			     var)))
   3947  1.1  mrg 	inform (DECL_SOURCE_LOCATION (var),
   3948  1.1  mrg 		"%qD declared here", var);
   3949  1.1  mrg       suppress_warning (use_stmt, OPT_Wdangling_pointer_);
   3950  1.1  mrg       return;
   3951  1.1  mrg     }
   3952  1.1  mrg 
   3953  1.1  mrg   if ((ref
   3954  1.1  mrg        && warning_at (use_loc, OPT_Wdangling_pointer_,
   3955  1.1  mrg 		      (maybe
   3956  1.1  mrg 		       ? G_("dangling pointer %qE to an unnamed temporary "
   3957  1.1  mrg 			    "may be used")
   3958  1.1  mrg 		       : G_("using dangling pointer %qE to an unnamed "
   3959  1.1  mrg 			    "temporary")),
   3960  1.1  mrg 		      ref))
   3961  1.1  mrg       || (!ref
   3962  1.1  mrg 	  && warning_at (use_loc, OPT_Wdangling_pointer_,
   3963  1.1  mrg 			 (maybe
   3964  1.1  mrg 			  ? G_("dangling pointer to an unnamed temporary "
   3965  1.1  mrg 			       "may be used")
   3966  1.1  mrg 			  : G_("using a dangling pointer to an unnamed "
   3967  1.1  mrg 			       "temporary")))))
   3968  1.1  mrg     {
   3969  1.1  mrg       inform (DECL_SOURCE_LOCATION (var),
   3970  1.1  mrg 	      "unnamed temporary defined here");
   3971  1.1  mrg       suppress_warning (use_stmt, OPT_Wdangling_pointer_);
   3972  1.1  mrg     }
   3973  1.1  mrg }
   3974  1.1  mrg 
   3975  1.1  mrg /* If STMT is a call to either the standard realloc or to a user-defined
   3976  1.1  mrg    reallocation function returns its LHS and set *PTR to the reallocated
   3977  1.1  mrg    pointer.  Otherwise return null.  */
   3978  1.1  mrg 
   3979  1.1  mrg static tree
   3980  1.1  mrg get_realloc_lhs (gimple *stmt, tree *ptr)
   3981  1.1  mrg {
   3982  1.1  mrg   if (gimple_call_builtin_p (stmt, BUILT_IN_REALLOC))
   3983  1.1  mrg     {
   3984  1.1  mrg       *ptr = gimple_call_arg (stmt, 0);
   3985  1.1  mrg       return gimple_call_lhs (stmt);
   3986  1.1  mrg     }
   3987  1.1  mrg 
   3988  1.1  mrg   gcall *call = dyn_cast<gcall *>(stmt);
   3989  1.1  mrg   if (!call)
   3990  1.1  mrg     return NULL_TREE;
   3991  1.1  mrg 
   3992  1.1  mrg   tree fnattr = NULL_TREE;
   3993  1.1  mrg   tree fndecl = gimple_call_fndecl (call);
   3994  1.1  mrg   if (fndecl)
   3995  1.1  mrg     fnattr = DECL_ATTRIBUTES (fndecl);
   3996  1.1  mrg   else
   3997  1.1  mrg     {
   3998  1.1  mrg       tree fntype = gimple_call_fntype (stmt);
   3999  1.1  mrg       if (!fntype)
   4000  1.1  mrg 	return NULL_TREE;
   4001  1.1  mrg       fnattr = TYPE_ATTRIBUTES (fntype);
   4002  1.1  mrg     }
   4003  1.1  mrg 
   4004  1.1  mrg   if (!fnattr)
   4005  1.1  mrg     return NULL_TREE;
   4006  1.1  mrg 
   4007  1.1  mrg   for (tree ats = fnattr;  (ats = lookup_attribute ("*dealloc", ats));
   4008  1.1  mrg        ats = TREE_CHAIN (ats))
   4009  1.1  mrg     {
   4010  1.1  mrg       tree args = TREE_VALUE (ats);
   4011  1.1  mrg       if (!args)
   4012  1.1  mrg 	continue;
   4013  1.1  mrg 
   4014  1.1  mrg       tree alloc = TREE_VALUE (args);
   4015  1.1  mrg       if (!alloc)
   4016  1.1  mrg 	continue;
   4017  1.1  mrg 
   4018  1.1  mrg       if (alloc == DECL_NAME (fndecl))
   4019  1.1  mrg 	{
   4020  1.1  mrg 	  unsigned argno = 0;
   4021  1.1  mrg 	  if (tree index = TREE_CHAIN (args))
   4022  1.1  mrg 	    argno = TREE_INT_CST_LOW (TREE_VALUE (index)) - 1;
   4023  1.1  mrg 	  *ptr = gimple_call_arg (stmt, argno);
   4024  1.1  mrg 	  return gimple_call_lhs (stmt);
   4025  1.1  mrg 	}
   4026  1.1  mrg     }
   4027  1.1  mrg 
   4028  1.1  mrg   return NULL_TREE;
   4029  1.1  mrg }
   4030  1.1  mrg 
   4031  1.1  mrg /* Warn if STMT is a call to a deallocation function that's not a match
   4032  1.1  mrg    for the REALLOC_STMT call.  Return true if warned.  */
   4033  1.1  mrg 
   4034  1.1  mrg static bool
   4035  1.1  mrg maybe_warn_mismatched_realloc (tree ptr, gimple *realloc_stmt, gimple *stmt)
   4036  1.1  mrg {
   4037  1.1  mrg   if (!is_gimple_call (stmt))
   4038  1.1  mrg     return false;
   4039  1.1  mrg 
   4040  1.1  mrg   tree fndecl = gimple_call_fndecl (stmt);
   4041  1.1  mrg   if (!fndecl)
   4042  1.1  mrg     return false;
   4043  1.1  mrg 
   4044  1.1  mrg   unsigned argno = fndecl_dealloc_argno (fndecl);
   4045  1.1  mrg   if (call_nargs (stmt) <= argno)
   4046  1.1  mrg     return false;
   4047  1.1  mrg 
   4048  1.1  mrg   if (matching_alloc_calls_p (realloc_stmt, fndecl))
   4049  1.1  mrg     return false;
   4050  1.1  mrg 
   4051  1.1  mrg   /* Avoid printing the unhelpful "<unknown>" in the diagnostics.  */
   4052  1.1  mrg   if (ptr && TREE_CODE (ptr) == SSA_NAME
   4053  1.1  mrg       && (!SSA_NAME_VAR (ptr) || DECL_ARTIFICIAL (SSA_NAME_VAR (ptr))))
   4054  1.1  mrg     ptr = NULL_TREE;
   4055  1.1  mrg 
   4056  1.1  mrg   location_t loc = gimple_location (stmt);
   4057  1.1  mrg   tree realloc_decl = gimple_call_fndecl (realloc_stmt);
   4058  1.1  mrg   tree dealloc_decl = gimple_call_fndecl (stmt);
   4059  1.1  mrg   if (ptr && !warning_at (loc, OPT_Wmismatched_dealloc,
   4060  1.1  mrg 			  "%qD called on pointer %qE passed to mismatched "
   4061  1.1  mrg 			  "allocation function %qD",
   4062  1.1  mrg 			  dealloc_decl, ptr, realloc_decl))
   4063  1.1  mrg     return false;
   4064  1.1  mrg   if (!ptr && !warning_at (loc, OPT_Wmismatched_dealloc,
   4065  1.1  mrg 			   "%qD called on a pointer passed to mismatched "
   4066  1.1  mrg 			   "reallocation function %qD",
   4067  1.1  mrg 			   dealloc_decl, realloc_decl))
   4068  1.1  mrg     return false;
   4069  1.1  mrg 
   4070  1.1  mrg   inform (gimple_location (realloc_stmt),
   4071  1.1  mrg 	  "call to %qD", realloc_decl);
   4072  1.1  mrg   return true;
   4073  1.1  mrg }
   4074  1.1  mrg 
   4075  1.1  mrg /* Return true if P and Q point to the same object, and false if they
   4076  1.1  mrg    either don't or their relationship cannot be determined.  */
   4077  1.1  mrg 
   4078  1.1  mrg static bool
   4079  1.1  mrg pointers_related_p (gimple *stmt, tree p, tree q, pointer_query &qry,
   4080  1.1  mrg 		    auto_bitmap &visited)
   4081  1.1  mrg {
   4082  1.1  mrg   if (!ptr_derefs_may_alias_p (p, q))
   4083  1.1  mrg     return false;
   4084  1.1  mrg 
   4085  1.1  mrg   /* TODO: Work harder to rule out relatedness.  */
   4086  1.1  mrg   access_ref pref, qref;
   4087  1.1  mrg   if (!qry.get_ref (p, stmt, &pref, 0)
   4088  1.1  mrg       || !qry.get_ref (q, stmt, &qref, 0))
   4089  1.1  mrg     /* GET_REF() only rarely fails.  When it does, it's likely because
   4090  1.1  mrg        it involves a self-referential PHI.  Return a conservative result.  */
   4091  1.1  mrg     return false;
   4092  1.1  mrg 
   4093  1.1  mrg   if (pref.ref == qref.ref)
   4094  1.1  mrg     return true;
   4095  1.1  mrg 
   4096  1.1  mrg   /* If either pointer is a PHI, iterate over all its operands and
   4097  1.1  mrg      return true if they're all related to the other pointer.  */
   4098  1.1  mrg   tree ptr = q;
   4099  1.1  mrg   unsigned version;
   4100  1.1  mrg   gphi *phi = pref.phi ();
   4101  1.1  mrg   if (phi)
   4102  1.1  mrg     version = SSA_NAME_VERSION (pref.ref);
   4103  1.1  mrg   else
   4104  1.1  mrg     {
   4105  1.1  mrg       phi = qref.phi ();
   4106  1.1  mrg       if (!phi)
   4107  1.1  mrg 	return false;
   4108  1.1  mrg 
   4109  1.1  mrg       ptr = p;
   4110  1.1  mrg       version = SSA_NAME_VERSION (qref.ref);
   4111  1.1  mrg     }
   4112  1.1  mrg 
   4113  1.1  mrg   if (!bitmap_set_bit (visited, version))
   4114  1.1  mrg     return true;
   4115  1.1  mrg 
   4116  1.1  mrg   unsigned nargs = gimple_phi_num_args (phi);
   4117  1.1  mrg   for (unsigned i = 0; i != nargs; ++i)
   4118  1.1  mrg     {
   4119  1.1  mrg       tree arg = gimple_phi_arg_def (phi, i);
   4120  1.1  mrg       if (!pointers_related_p (stmt, arg, ptr, qry, visited))
   4121  1.1  mrg 	return false;
   4122  1.1  mrg     }
   4123  1.1  mrg 
   4124  1.1  mrg   return true;
   4125  1.1  mrg }
   4126  1.1  mrg 
   4127  1.1  mrg /* Convenience wrapper for the above.  */
   4128  1.1  mrg 
   4129  1.1  mrg static bool
   4130  1.1  mrg pointers_related_p (gimple *stmt, tree p, tree q, pointer_query &qry)
   4131  1.1  mrg {
   4132  1.1  mrg   auto_bitmap visited;
   4133  1.1  mrg   return pointers_related_p (stmt, p, q, qry, visited);
   4134  1.1  mrg }
   4135  1.1  mrg 
   4136  1.1  mrg /* For a STMT either a call to a deallocation function or a clobber, warn
   4137  1.1  mrg    for uses of the pointer PTR it was called with (including its copies
   4138  1.1  mrg    or others derived from it by pointer arithmetic).  If STMT is a clobber,
   4139  1.1  mrg    VAR is the decl of the clobbered variable.  When MAYBE is true use
   4140  1.1  mrg    a "maybe" form of diagnostic.  */
   4141  1.1  mrg 
   4142  1.1  mrg void
   4143  1.1  mrg pass_waccess::check_pointer_uses (gimple *stmt, tree ptr,
   4144  1.1  mrg 				  tree var /* = NULL_TREE */,
   4145  1.1  mrg 				  bool maybe /* = false */)
   4146  1.1  mrg {
   4147  1.1  mrg   gcc_assert (TREE_CODE (ptr) == SSA_NAME);
   4148  1.1  mrg 
   4149  1.1  mrg   const bool check_dangling = !is_gimple_call (stmt);
   4150  1.1  mrg   basic_block stmt_bb = gimple_bb (stmt);
   4151  1.1  mrg 
   4152  1.1  mrg   /* If STMT is a reallocation function set to the reallocated pointer
   4153  1.1  mrg      and the LHS of the call, respectively.  */
   4154  1.1  mrg   tree realloc_ptr = NULL_TREE;
   4155  1.1  mrg   tree realloc_lhs = get_realloc_lhs (stmt, &realloc_ptr);
   4156  1.1  mrg 
   4157  1.1  mrg   auto_bitmap visited;
   4158  1.1  mrg 
   4159  1.1  mrg   auto_vec<tree> pointers;
   4160  1.1  mrg   pointers.safe_push (ptr);
   4161  1.1  mrg 
   4162  1.1  mrg   /* Starting with PTR, iterate over POINTERS added by the loop, and
   4163  1.1  mrg      either warn for their uses in basic blocks dominated by the STMT
   4164  1.1  mrg      or in statements that follow it in the same basic block, or add
   4165  1.1  mrg      them to POINTERS if they point into the same object as PTR (i.e.,
   4166  1.1  mrg      are obtained by pointer arithmetic on PTR).  */
   4167  1.1  mrg   for (unsigned i = 0; i != pointers.length (); ++i)
   4168  1.1  mrg     {
   4169  1.1  mrg       tree ptr = pointers[i];
   4170  1.1  mrg       if (!bitmap_set_bit (visited, SSA_NAME_VERSION (ptr)))
   4171  1.1  mrg 	/* Avoid revisiting the same pointer.  */
   4172  1.1  mrg 	continue;
   4173  1.1  mrg 
   4174  1.1  mrg       use_operand_p use_p;
   4175  1.1  mrg       imm_use_iterator iter;
   4176  1.1  mrg       FOR_EACH_IMM_USE_FAST (use_p, iter, ptr)
   4177  1.1  mrg 	{
   4178  1.1  mrg 	  gimple *use_stmt = USE_STMT (use_p);
   4179  1.1  mrg 	  if (use_stmt == stmt || is_gimple_debug (use_stmt))
   4180  1.1  mrg 	    continue;
   4181  1.1  mrg 
   4182  1.1  mrg 	  if (realloc_lhs)
   4183  1.1  mrg 	    {
   4184  1.1  mrg 	      /* Check to see if USE_STMT is a mismatched deallocation
   4185  1.1  mrg 		 call for the pointer passed to realloc.  That's a bug
   4186  1.1  mrg 		 regardless of the pointer's value and so warn.  */
   4187  1.1  mrg 	      if (maybe_warn_mismatched_realloc (*use_p->use, stmt, use_stmt))
   4188  1.1  mrg 		continue;
   4189  1.1  mrg 
   4190  1.1  mrg 	      /* Pointers passed to realloc that are used in basic blocks
   4191  1.1  mrg 		 where the realloc call is known to have failed are valid.
   4192  1.1  mrg 		 Ignore pointers that nothing is known about.  Those could
   4193  1.1  mrg 		 have escaped along with their nullness.  */
   4194  1.1  mrg 	      value_range vr;
   4195  1.1  mrg 	      if (m_ptr_qry.rvals->range_of_expr (vr, realloc_lhs, use_stmt))
   4196  1.1  mrg 		{
   4197  1.1  mrg 		  if (vr.zero_p ())
   4198  1.1  mrg 		    continue;
   4199  1.1  mrg 
   4200  1.1  mrg 		  if (!pointers_related_p (stmt, ptr, realloc_ptr, m_ptr_qry))
   4201  1.1  mrg 		    continue;
   4202  1.1  mrg 		}
   4203  1.1  mrg 	    }
   4204  1.1  mrg 
   4205  1.1  mrg 	  if (check_dangling
   4206  1.1  mrg 	      && gimple_code (use_stmt) == GIMPLE_RETURN)
   4207  1.1  mrg 	    /* Avoid interfering with -Wreturn-local-addr (which runs only
   4208  1.1  mrg 	       with optimization enabled so it won't diagnose cases that
   4209  1.1  mrg 	       would be caught here when optimization is disabled).  */
   4210  1.1  mrg 	    continue;
   4211  1.1  mrg 
   4212  1.1  mrg 	  bool equality = false;
   4213  1.1  mrg 	  if (is_gimple_assign (use_stmt))
   4214  1.1  mrg 	    {
   4215  1.1  mrg 	      tree_code code = gimple_assign_rhs_code (use_stmt);
   4216  1.1  mrg 	      equality = code == EQ_EXPR || code == NE_EXPR;
   4217  1.1  mrg 	    }
   4218  1.1  mrg 	  else if (gcond *cond = dyn_cast<gcond *>(use_stmt))
   4219  1.1  mrg 	    {
   4220  1.1  mrg 	      tree_code code = gimple_cond_code (cond);
   4221  1.1  mrg 	      equality = code == EQ_EXPR || code == NE_EXPR;
   4222  1.1  mrg 	    }
   4223  1.1  mrg 
   4224  1.1  mrg 	  /* Warn if USE_STMT is dominated by the deallocation STMT.
   4225  1.1  mrg 	     Otherwise, add the pointer to POINTERS so that the uses
   4226  1.1  mrg 	     of any other pointers derived from it can be checked.  */
   4227  1.1  mrg 	  if (use_after_inval_p (stmt, use_stmt, check_dangling))
   4228  1.1  mrg 	    {
   4229  1.1  mrg 	      if (gimple_code (use_stmt) == GIMPLE_PHI)
   4230  1.1  mrg 		{
   4231  1.1  mrg 		  /* Only add a PHI result to POINTERS if all its
   4232  1.1  mrg 		     operands are related to PTR, otherwise continue.  */
   4233  1.1  mrg 		  tree lhs = gimple_phi_result (use_stmt);
   4234  1.1  mrg 		  if (!pointers_related_p (stmt, lhs, ptr, m_ptr_qry))
   4235  1.1  mrg 		    continue;
   4236  1.1  mrg 
   4237  1.1  mrg 		  if (TREE_CODE (lhs) == SSA_NAME)
   4238  1.1  mrg 		    {
   4239  1.1  mrg 		      pointers.safe_push (lhs);
   4240  1.1  mrg 		      continue;
   4241  1.1  mrg 		    }
   4242  1.1  mrg 		}
   4243  1.1  mrg 
   4244  1.1  mrg 	      basic_block use_bb = gimple_bb (use_stmt);
   4245  1.1  mrg 	      bool this_maybe
   4246  1.1  mrg 		= (maybe
   4247  1.1  mrg 		   || !dominated_by_p (CDI_POST_DOMINATORS, stmt_bb, use_bb));
   4248  1.1  mrg 	      warn_invalid_pointer (*use_p->use, use_stmt, stmt, var,
   4249  1.1  mrg 				    this_maybe, equality);
   4250  1.1  mrg 	      continue;
   4251  1.1  mrg 	    }
   4252  1.1  mrg 
   4253  1.1  mrg 	  if (is_gimple_assign (use_stmt))
   4254  1.1  mrg 	    {
   4255  1.1  mrg 	      tree lhs = gimple_assign_lhs (use_stmt);
   4256  1.1  mrg 	      if (TREE_CODE (lhs) == SSA_NAME)
   4257  1.1  mrg 		{
   4258  1.1  mrg 		  tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
   4259  1.1  mrg 		  if (rhs_code == POINTER_PLUS_EXPR || rhs_code == SSA_NAME)
   4260  1.1  mrg 		    pointers.safe_push (lhs);
   4261  1.1  mrg 		}
   4262  1.1  mrg 	      continue;
   4263  1.1  mrg 	    }
   4264  1.1  mrg 
   4265  1.1  mrg 	  if (gcall *call = dyn_cast <gcall *>(use_stmt))
   4266  1.1  mrg 	    {
   4267  1.1  mrg 	      if (gimple_call_return_arg (call) == ptr)
   4268  1.1  mrg 		if (tree lhs = gimple_call_lhs (call))
   4269  1.1  mrg 		  if (TREE_CODE (lhs) == SSA_NAME)
   4270  1.1  mrg 		    pointers.safe_push (lhs);
   4271  1.1  mrg 	      continue;
   4272  1.1  mrg 	    }
   4273  1.1  mrg 	}
   4274  1.1  mrg     }
   4275  1.1  mrg }
   4276  1.1  mrg 
   4277  1.1  mrg /* Check call STMT for invalid accesses.  */
   4278  1.1  mrg 
   4279  1.1  mrg void
   4280  1.1  mrg pass_waccess::check_call (gcall *stmt)
   4281  1.1  mrg {
   4282  1.1  mrg   if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
   4283  1.1  mrg     check_builtin (stmt);
   4284  1.1  mrg 
   4285  1.1  mrg   /* .ASAN_MARK doesn't access any vars, only modifies shadow memory.  */
   4286  1.1  mrg   if (gimple_call_internal_p (stmt)
   4287  1.1  mrg       && gimple_call_internal_fn (stmt) == IFN_ASAN_MARK)
   4288  1.1  mrg     return;
   4289  1.1  mrg 
   4290  1.1  mrg   if (!m_early_checks_p)
   4291  1.1  mrg     if (tree callee = gimple_call_fndecl (stmt))
   4292  1.1  mrg       {
   4293  1.1  mrg 	/* Check for uses of the pointer passed to either a standard
   4294  1.1  mrg 	   or a user-defined deallocation function.  */
   4295  1.1  mrg 	unsigned argno = fndecl_dealloc_argno (callee);
   4296  1.1  mrg 	if (argno < (unsigned) call_nargs (stmt))
   4297  1.1  mrg 	  {
   4298  1.1  mrg 	    tree arg = call_arg (stmt, argno);
   4299  1.1  mrg 	    if (TREE_CODE (arg) == SSA_NAME)
   4300  1.1  mrg 	      check_pointer_uses (stmt, arg);
   4301  1.1  mrg 	  }
   4302  1.1  mrg       }
   4303  1.1  mrg 
   4304  1.1  mrg   check_call_access (stmt);
   4305  1.1  mrg   check_call_dangling (stmt);
   4306  1.1  mrg 
   4307  1.1  mrg   if (m_early_checks_p)
   4308  1.1  mrg     return;
   4309  1.1  mrg 
   4310  1.1  mrg   maybe_check_dealloc_call (stmt);
   4311  1.1  mrg   check_nonstring_args (stmt);
   4312  1.1  mrg }
   4313  1.1  mrg 
   4314  1.1  mrg /* Check non-call STMT for invalid accesses.  */
   4315  1.1  mrg 
   4316  1.1  mrg void
   4317  1.1  mrg pass_waccess::check_stmt (gimple *stmt)
   4318  1.1  mrg {
   4319  1.1  mrg   if (m_check_dangling_p
   4320  1.1  mrg       && gimple_clobber_p (stmt, CLOBBER_EOL))
   4321  1.1  mrg     {
   4322  1.1  mrg       /* Ignore clobber statements in blocks with exceptional edges.  */
   4323  1.1  mrg       basic_block bb = gimple_bb (stmt);
   4324  1.1  mrg       edge e = EDGE_PRED (bb, 0);
   4325  1.1  mrg       if (e->flags & EDGE_EH)
   4326  1.1  mrg 	return;
   4327  1.1  mrg 
   4328  1.1  mrg       tree var = gimple_assign_lhs (stmt);
   4329  1.1  mrg       m_clobbers.put (var, stmt);
   4330  1.1  mrg       return;
   4331  1.1  mrg     }
   4332  1.1  mrg 
   4333  1.1  mrg   if (is_gimple_assign (stmt))
   4334  1.1  mrg     {
   4335  1.1  mrg       /* Clobbered unnamed temporaries such as compound literals can be
   4336  1.1  mrg 	 revived.  Check for an assignment to one and remove it from
   4337  1.1  mrg 	 M_CLOBBERS.  */
   4338  1.1  mrg       tree lhs = gimple_assign_lhs (stmt);
   4339  1.1  mrg       while (handled_component_p (lhs))
   4340  1.1  mrg 	lhs = TREE_OPERAND (lhs, 0);
   4341  1.1  mrg 
   4342  1.1  mrg       if (auto_var_p (lhs))
   4343  1.1  mrg 	m_clobbers.remove (lhs);
   4344  1.1  mrg       return;
   4345  1.1  mrg     }
   4346  1.1  mrg 
   4347  1.1  mrg   if (greturn *ret = dyn_cast <greturn *> (stmt))
   4348  1.1  mrg     {
   4349  1.1  mrg       if (optimize && flag_isolate_erroneous_paths_dereference)
   4350  1.1  mrg 	/* Avoid interfering with -Wreturn-local-addr (which runs only
   4351  1.1  mrg 	   with optimization enabled).  */
   4352  1.1  mrg 	return;
   4353  1.1  mrg 
   4354  1.1  mrg       tree arg = gimple_return_retval (ret);
   4355  1.1  mrg       if (!arg || TREE_CODE (arg) != ADDR_EXPR)
   4356  1.1  mrg 	return;
   4357  1.1  mrg 
   4358  1.1  mrg       arg = TREE_OPERAND (arg, 0);
   4359  1.1  mrg       while (handled_component_p (arg))
   4360  1.1  mrg 	arg = TREE_OPERAND (arg, 0);
   4361  1.1  mrg 
   4362  1.1  mrg       if (!auto_var_p (arg))
   4363  1.1  mrg 	return;
   4364  1.1  mrg 
   4365  1.1  mrg       gimple **pclobber = m_clobbers.get (arg);
   4366  1.1  mrg       if (!pclobber)
   4367  1.1  mrg 	return;
   4368  1.1  mrg 
   4369  1.1  mrg       if (!use_after_inval_p (*pclobber, stmt))
   4370  1.1  mrg 	return;
   4371  1.1  mrg 
   4372  1.1  mrg       warn_invalid_pointer (NULL_TREE, stmt, *pclobber, arg, false);
   4373  1.1  mrg     }
   4374  1.1  mrg }
   4375  1.1  mrg 
   4376  1.1  mrg /* Check basic block BB for invalid accesses.  */
   4377  1.1  mrg 
   4378  1.1  mrg void
   4379  1.1  mrg pass_waccess::check_block (basic_block bb)
   4380  1.1  mrg {
   4381  1.1  mrg   /* Iterate over statements, looking for function calls.  */
   4382  1.1  mrg   for (auto si = gsi_start_bb (bb); !gsi_end_p (si);
   4383  1.1  mrg        gsi_next_nondebug (&si))
   4384  1.1  mrg     {
   4385  1.1  mrg       gimple *stmt = gsi_stmt (si);
   4386  1.1  mrg       if (gcall *call = dyn_cast <gcall *> (stmt))
   4387  1.1  mrg 	check_call (call);
   4388  1.1  mrg       else
   4389  1.1  mrg 	check_stmt (stmt);
   4390  1.1  mrg     }
   4391  1.1  mrg }
   4392  1.1  mrg 
   4393  1.1  mrg /* Return the argument that the call STMT to a built-in function returns
   4394  1.1  mrg    (including with an offset) or null if it doesn't.  */
   4395  1.1  mrg 
   4396  1.1  mrg tree
   4397  1.1  mrg pass_waccess::gimple_call_return_arg (gcall *call)
   4398  1.1  mrg {
   4399  1.1  mrg   /* Check for attribute fn spec to see if the function returns one
   4400  1.1  mrg      of its arguments.  */
   4401  1.1  mrg   attr_fnspec fnspec = gimple_call_fnspec (call);
   4402  1.1  mrg   unsigned int argno;
   4403  1.1  mrg   if (!fnspec.returns_arg (&argno))
   4404  1.1  mrg     {
   4405  1.1  mrg       if (gimple_call_num_args (call) < 1)
   4406  1.1  mrg 	return NULL_TREE;
   4407  1.1  mrg 
   4408  1.1  mrg       if (!gimple_call_builtin_p (call, BUILT_IN_NORMAL))
   4409  1.1  mrg 	return NULL_TREE;
   4410  1.1  mrg 
   4411  1.1  mrg       tree fndecl = gimple_call_fndecl (call);
   4412  1.1  mrg       switch (DECL_FUNCTION_CODE (fndecl))
   4413  1.1  mrg 	{
   4414  1.1  mrg 	case BUILT_IN_MEMPCPY:
   4415  1.1  mrg 	case BUILT_IN_MEMPCPY_CHK:
   4416  1.1  mrg 	case BUILT_IN_MEMCHR:
   4417  1.1  mrg 	case BUILT_IN_STRCHR:
   4418  1.1  mrg 	case BUILT_IN_STRRCHR:
   4419  1.1  mrg 	case BUILT_IN_STRSTR:
   4420  1.1  mrg 	case BUILT_IN_STPCPY:
   4421  1.1  mrg 	case BUILT_IN_STPCPY_CHK:
   4422  1.1  mrg 	case BUILT_IN_STPNCPY:
   4423  1.1  mrg 	case BUILT_IN_STPNCPY_CHK:
   4424  1.1  mrg 	  argno = 0;
   4425  1.1  mrg 	  break;
   4426  1.1  mrg 
   4427  1.1  mrg 	default:
   4428  1.1  mrg 	  return NULL_TREE;
   4429  1.1  mrg 	}
   4430  1.1  mrg     }
   4431  1.1  mrg 
   4432  1.1  mrg   if (gimple_call_num_args (call) <= argno)
   4433  1.1  mrg     return NULL_TREE;
   4434  1.1  mrg 
   4435  1.1  mrg   return gimple_call_arg (call, argno);
   4436  1.1  mrg }
   4437  1.1  mrg 
   4438  1.1  mrg /* Check for and diagnose all uses of the dangling pointer VAR to the auto
   4439  1.1  mrg    object DECL whose lifetime has ended.  OBJREF is true when VAR denotes
   4440  1.1  mrg    an access to a DECL that may have been clobbered.  */
   4441  1.1  mrg 
   4442  1.1  mrg void
   4443  1.1  mrg pass_waccess::check_dangling_uses (tree var, tree decl, bool maybe /* = false */,
   4444  1.1  mrg 				   bool objref /* = false */)
   4445  1.1  mrg {
   4446  1.1  mrg   if (!decl || !auto_var_p (decl))
   4447  1.1  mrg     return;
   4448  1.1  mrg 
   4449  1.1  mrg   gimple **pclob = m_clobbers.get (decl);
   4450  1.1  mrg   if (!pclob)
   4451  1.1  mrg     return;
   4452  1.1  mrg 
   4453  1.1  mrg   if (!objref)
   4454  1.1  mrg     {
   4455  1.1  mrg       check_pointer_uses (*pclob, var, decl, maybe);
   4456  1.1  mrg       return;
   4457  1.1  mrg     }
   4458  1.1  mrg 
   4459  1.1  mrg   gimple *use_stmt = SSA_NAME_DEF_STMT (var);
   4460  1.1  mrg   if (!use_after_inval_p (*pclob, use_stmt, true))
   4461  1.1  mrg     return;
   4462  1.1  mrg 
   4463  1.1  mrg   basic_block use_bb = gimple_bb (use_stmt);
   4464  1.1  mrg   basic_block clob_bb = gimple_bb (*pclob);
   4465  1.1  mrg   maybe = maybe || !dominated_by_p (CDI_POST_DOMINATORS, clob_bb, use_bb);
   4466  1.1  mrg   warn_invalid_pointer (var, use_stmt, *pclob, decl, maybe, false);
   4467  1.1  mrg }
   4468  1.1  mrg 
   4469  1.1  mrg /* Diagnose stores in BB and (recursively) its predecessors of the addresses
   4470  1.1  mrg    of local variables into nonlocal pointers that are left dangling after
   4471  1.1  mrg    the function returns.  BBS is a bitmap of basic blocks visited.  */
   4472  1.1  mrg 
   4473  1.1  mrg void
   4474  1.1  mrg pass_waccess::check_dangling_stores (basic_block bb,
   4475  1.1  mrg 				     hash_set<tree> &stores,
   4476  1.1  mrg 				     auto_bitmap &bbs)
   4477  1.1  mrg {
   4478  1.1  mrg   if (!bitmap_set_bit (bbs, bb->index))
   4479  1.1  mrg     /* Avoid cycles. */
   4480  1.1  mrg     return;
   4481  1.1  mrg 
   4482  1.1  mrg   /* Iterate backwards over the statements looking for a store of
   4483  1.1  mrg      the address of a local variable into a nonlocal pointer.  */
   4484  1.1  mrg   for (auto gsi = gsi_last_nondebug_bb (bb); ; gsi_prev_nondebug (&gsi))
   4485  1.1  mrg     {
   4486  1.1  mrg       gimple *stmt = gsi_stmt (gsi);
   4487  1.1  mrg       if (!stmt)
   4488  1.1  mrg 	break;
   4489  1.1  mrg 
   4490  1.1  mrg       if (warning_suppressed_p (stmt, OPT_Wdangling_pointer_))
   4491  1.1  mrg 	continue;
   4492  1.1  mrg 
   4493  1.1  mrg       if (is_gimple_call (stmt)
   4494  1.1  mrg 	  && !(gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE)))
   4495  1.1  mrg 	/* Avoid looking before nonconst, nonpure calls since those might
   4496  1.1  mrg 	   use the escaped locals.  */
   4497  1.1  mrg 	return;
   4498  1.1  mrg 
   4499  1.1  mrg       if (!is_gimple_assign (stmt) || gimple_clobber_p (stmt))
   4500  1.1  mrg 	continue;
   4501  1.1  mrg 
   4502  1.1  mrg       access_ref lhs_ref;
   4503  1.1  mrg       tree lhs = gimple_assign_lhs (stmt);
   4504  1.1  mrg       if (!m_ptr_qry.get_ref (lhs, stmt, &lhs_ref, 0))
   4505  1.1  mrg 	continue;
   4506  1.1  mrg 
   4507  1.1  mrg       if (auto_var_p (lhs_ref.ref))
   4508  1.1  mrg 	continue;
   4509  1.1  mrg 
   4510  1.1  mrg       if (DECL_P (lhs_ref.ref))
   4511  1.1  mrg 	{
   4512  1.1  mrg 	  if (!POINTER_TYPE_P (TREE_TYPE (lhs_ref.ref))
   4513  1.1  mrg 	      || lhs_ref.deref > 0)
   4514  1.1  mrg 	    continue;
   4515  1.1  mrg 	}
   4516  1.1  mrg       else if (TREE_CODE (lhs_ref.ref) == SSA_NAME)
   4517  1.1  mrg 	{
   4518  1.1  mrg 	  gimple *def_stmt = SSA_NAME_DEF_STMT (lhs_ref.ref);
   4519  1.1  mrg 	  if (!gimple_nop_p (def_stmt))
   4520  1.1  mrg 	    /* Avoid looking at or before stores into unknown objects.  */
   4521  1.1  mrg 	    return;
   4522  1.1  mrg 
   4523  1.1  mrg 	  tree var = SSA_NAME_VAR (lhs_ref.ref);
   4524  1.1  mrg 	  if (TREE_CODE (var) == PARM_DECL && DECL_BY_REFERENCE (var))
   4525  1.1  mrg 	    /* Avoid by-value arguments transformed into by-reference.  */
   4526  1.1  mrg 	    continue;
   4527  1.1  mrg 
   4528  1.1  mrg 	}
   4529  1.1  mrg       else if (TREE_CODE (lhs_ref.ref) == MEM_REF)
   4530  1.1  mrg 	{
   4531  1.1  mrg 	  tree arg = TREE_OPERAND (lhs_ref.ref, 0);
   4532  1.1  mrg 	  if (TREE_CODE (arg) == SSA_NAME)
   4533  1.1  mrg 	    {
   4534  1.1  mrg 	      gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
   4535  1.1  mrg 	      if (!gimple_nop_p (def_stmt))
   4536  1.1  mrg 		return;
   4537  1.1  mrg 	    }
   4538  1.1  mrg 	}
   4539  1.1  mrg       else
   4540  1.1  mrg 	continue;
   4541  1.1  mrg 
   4542  1.1  mrg       if (stores.add (lhs_ref.ref))
   4543  1.1  mrg 	continue;
   4544  1.1  mrg 
   4545  1.1  mrg       /* FIXME: Handle stores of alloca() and VLA.  */
   4546  1.1  mrg       access_ref rhs_ref;
   4547  1.1  mrg       tree rhs = gimple_assign_rhs1 (stmt);
   4548  1.1  mrg       if (!m_ptr_qry.get_ref (rhs, stmt, &rhs_ref, 0)
   4549  1.1  mrg 	  || rhs_ref.deref != -1)
   4550  1.1  mrg 	continue;
   4551  1.1  mrg 
   4552  1.1  mrg       if (!auto_var_p (rhs_ref.ref))
   4553  1.1  mrg 	continue;
   4554  1.1  mrg 
   4555  1.1  mrg       location_t loc = gimple_location (stmt);
   4556  1.1  mrg       if (warning_at (loc, OPT_Wdangling_pointer_,
   4557  1.1  mrg 		      "storing the address of local variable %qD in %qE",
   4558  1.1  mrg 		      rhs_ref.ref, lhs))
   4559  1.1  mrg 	{
   4560  1.1  mrg 	  suppress_warning (stmt, OPT_Wdangling_pointer_);
   4561  1.1  mrg 
   4562  1.1  mrg 	  location_t loc = DECL_SOURCE_LOCATION (rhs_ref.ref);
   4563  1.1  mrg 	  inform (loc, "%qD declared here", rhs_ref.ref);
   4564  1.1  mrg 
   4565  1.1  mrg 	  if (DECL_P (lhs_ref.ref))
   4566  1.1  mrg 	    loc = DECL_SOURCE_LOCATION (lhs_ref.ref);
   4567  1.1  mrg 	  else if (EXPR_HAS_LOCATION (lhs_ref.ref))
   4568  1.1  mrg 	    loc = EXPR_LOCATION (lhs_ref.ref);
   4569  1.1  mrg 
   4570  1.1  mrg 	  if (loc != UNKNOWN_LOCATION)
   4571  1.1  mrg 	    inform (loc, "%qE declared here", lhs_ref.ref);
   4572  1.1  mrg 	}
   4573  1.1  mrg     }
   4574  1.1  mrg 
   4575  1.1  mrg   edge e;
   4576  1.1  mrg   edge_iterator ei;
   4577  1.1  mrg   FOR_EACH_EDGE (e, ei, bb->preds)
   4578  1.1  mrg     {
   4579  1.1  mrg       basic_block pred = e->src;
   4580  1.1  mrg       check_dangling_stores (pred, stores, bbs);
   4581  1.1  mrg     }
   4582  1.1  mrg }
   4583  1.1  mrg 
   4584  1.1  mrg /* Diagnose stores of the addresses of local variables into nonlocal
   4585  1.1  mrg    pointers that are left dangling after the function returns.  */
   4586  1.1  mrg 
   4587  1.1  mrg void
   4588  1.1  mrg pass_waccess::check_dangling_stores ()
   4589  1.1  mrg {
   4590  1.1  mrg   auto_bitmap bbs;
   4591  1.1  mrg   hash_set<tree> stores;
   4592  1.1  mrg   check_dangling_stores (EXIT_BLOCK_PTR_FOR_FN (m_func), stores, bbs);
   4593  1.1  mrg }
   4594  1.1  mrg 
   4595  1.1  mrg /* Check for and diagnose uses of dangling pointers to auto objects
   4596  1.1  mrg    whose lifetime has ended.  */
   4597  1.1  mrg 
   4598  1.1  mrg void
   4599  1.1  mrg pass_waccess::check_dangling_uses ()
   4600  1.1  mrg {
   4601  1.1  mrg   tree var;
   4602  1.1  mrg   unsigned i;
   4603  1.1  mrg   FOR_EACH_SSA_NAME (i, var, m_func)
   4604  1.1  mrg     {
   4605  1.1  mrg       /* For each SSA_NAME pointer VAR find the object it points to.
   4606  1.1  mrg 	 If the object is a clobbered local variable, check to see
   4607  1.1  mrg 	 if any of VAR's uses (or those of other pointers derived
   4608  1.1  mrg 	 from VAR) happens after the clobber.  If so, warn.  */
   4609  1.1  mrg 
   4610  1.1  mrg       gimple *def_stmt = SSA_NAME_DEF_STMT (var);
   4611  1.1  mrg       if (is_gimple_assign (def_stmt))
   4612  1.1  mrg 	{
   4613  1.1  mrg 	  tree rhs = gimple_assign_rhs1 (def_stmt);
   4614  1.1  mrg 	  if (TREE_CODE (rhs) == ADDR_EXPR)
   4615  1.1  mrg 	    {
   4616  1.1  mrg 	      if (!POINTER_TYPE_P (TREE_TYPE (var)))
   4617  1.1  mrg 		continue;
   4618  1.1  mrg 	      check_dangling_uses (var, TREE_OPERAND (rhs, 0));
   4619  1.1  mrg 	    }
   4620  1.1  mrg 	  else
   4621  1.1  mrg 	    {
   4622  1.1  mrg 	      /* For other expressions, check the base DECL to see
   4623  1.1  mrg 		 if it's been clobbered, most likely as a result of
   4624  1.1  mrg 		 inlining a reference to it.  */
   4625  1.1  mrg 	      tree decl = get_base_address (rhs);
   4626  1.1  mrg 	      if (DECL_P (decl))
   4627  1.1  mrg 		check_dangling_uses (var, decl, false, true);
   4628  1.1  mrg 	    }
   4629  1.1  mrg 	}
   4630  1.1  mrg       else if (POINTER_TYPE_P (TREE_TYPE (var)))
   4631  1.1  mrg 	{
   4632  1.1  mrg 	  if (gcall *call = dyn_cast<gcall *>(def_stmt))
   4633  1.1  mrg 	    {
   4634  1.1  mrg 	      if (tree arg = gimple_call_return_arg (call))
   4635  1.1  mrg 		{
   4636  1.1  mrg 		  access_ref aref;
   4637  1.1  mrg 		  if (m_ptr_qry.get_ref (arg, call, &aref, 0)
   4638  1.1  mrg 		      && aref.deref < 0)
   4639  1.1  mrg 		    check_dangling_uses (var, aref.ref);
   4640  1.1  mrg 		}
   4641  1.1  mrg 	    }
   4642  1.1  mrg 	  else if (gphi *phi = dyn_cast <gphi *>(def_stmt))
   4643  1.1  mrg 	    {
   4644  1.1  mrg 	      unsigned nargs = gimple_phi_num_args (phi);
   4645  1.1  mrg 	      for (unsigned i = 0; i != nargs; ++i)
   4646  1.1  mrg 		{
   4647  1.1  mrg 		  access_ref aref;
   4648  1.1  mrg 		  tree arg = gimple_phi_arg_def (phi, i);
   4649  1.1  mrg 		  if (m_ptr_qry.get_ref (arg, phi, &aref, 0)
   4650  1.1  mrg 		      && aref.deref < 0)
   4651  1.1  mrg 		    check_dangling_uses (var, aref.ref, true);
   4652  1.1  mrg 		}
   4653  1.1  mrg 	    }
   4654  1.1  mrg 	}
   4655  1.1  mrg     }
   4656  1.1  mrg }
   4657  1.1  mrg 
   4658  1.1  mrg /* Check CALL arguments for dangling pointers (those that have been
   4659  1.1  mrg    clobbered) and warn if found.  */
   4660  1.1  mrg 
   4661  1.1  mrg void
   4662  1.1  mrg pass_waccess::check_call_dangling (gcall *call)
   4663  1.1  mrg {
   4664  1.1  mrg   unsigned nargs = gimple_call_num_args (call);
   4665  1.1  mrg   for (unsigned i = 0; i != nargs; ++i)
   4666  1.1  mrg     {
   4667  1.1  mrg       tree arg = gimple_call_arg (call, i);
   4668  1.1  mrg       if (TREE_CODE (arg) != ADDR_EXPR)
   4669  1.1  mrg 	continue;
   4670  1.1  mrg 
   4671  1.1  mrg       arg = TREE_OPERAND (arg, 0);
   4672  1.1  mrg       if (!DECL_P (arg))
   4673  1.1  mrg 	continue;
   4674  1.1  mrg 
   4675  1.1  mrg       gimple **pclobber = m_clobbers.get (arg);
   4676  1.1  mrg       if (!pclobber)
   4677  1.1  mrg 	continue;
   4678  1.1  mrg 
   4679  1.1  mrg       if (!use_after_inval_p (*pclobber, call))
   4680  1.1  mrg 	continue;
   4681  1.1  mrg 
   4682  1.1  mrg       warn_invalid_pointer (NULL_TREE, call, *pclobber, arg, false);
   4683  1.1  mrg     }
   4684  1.1  mrg }
   4685  1.1  mrg 
   4686  1.1  mrg /* Check function FUN for invalid accesses.  */
   4687  1.1  mrg 
   4688  1.1  mrg unsigned
   4689  1.1  mrg pass_waccess::execute (function *fun)
   4690  1.1  mrg {
   4691  1.1  mrg   calculate_dominance_info (CDI_DOMINATORS);
   4692  1.1  mrg   calculate_dominance_info (CDI_POST_DOMINATORS);
   4693  1.1  mrg 
   4694  1.1  mrg   /* Set or clear EDGE_DFS_BACK bits on back edges.  */
   4695  1.1  mrg   mark_dfs_back_edges (fun);
   4696  1.1  mrg 
   4697  1.1  mrg   /* Create a new ranger instance and associate it with FUN.  */
   4698  1.1  mrg   m_ptr_qry.rvals = enable_ranger (fun);
   4699  1.1  mrg   m_func = fun;
   4700  1.1  mrg 
   4701  1.1  mrg   /* Check for dangling pointers in the earliest run of the pass.
   4702  1.1  mrg      The latest point -Wdangling-pointer should run is just before
   4703  1.1  mrg      loop unrolling which introduces uses after clobbers.  Most cases
   4704  1.1  mrg      can be detected without optimization; cases where the address of
   4705  1.1  mrg      the local variable is passed to and then returned from a user-
   4706  1.1  mrg      defined function before its lifetime ends and the returned pointer
   4707  1.1  mrg      becomes dangling depend on inlining.  */
   4708  1.1  mrg   m_check_dangling_p = m_early_checks_p;
   4709  1.1  mrg 
   4710  1.1  mrg   auto_bitmap bb_uids_set (&bitmap_default_obstack);
   4711  1.1  mrg   m_bb_uids_set = bb_uids_set;
   4712  1.1  mrg 
   4713  1.1  mrg   set_gimple_stmt_max_uid (m_func, 0);
   4714  1.1  mrg 
   4715  1.1  mrg   basic_block bb;
   4716  1.1  mrg   FOR_EACH_BB_FN (bb, fun)
   4717  1.1  mrg     check_block (bb);
   4718  1.1  mrg 
   4719  1.1  mrg   if (m_check_dangling_p)
   4720  1.1  mrg     {
   4721  1.1  mrg       check_dangling_uses ();
   4722  1.1  mrg       check_dangling_stores ();
   4723  1.1  mrg     }
   4724  1.1  mrg 
   4725  1.1  mrg   if (dump_file)
   4726  1.1  mrg     m_ptr_qry.dump (dump_file, (dump_flags & TDF_DETAILS) != 0);
   4727  1.1  mrg 
   4728  1.1  mrg   m_ptr_qry.flush_cache ();
   4729  1.1  mrg 
   4730  1.1  mrg   /* Release the ranger instance and replace it with a global ranger.
   4731  1.1  mrg      Also reset the pointer since calling disable_ranger() deletes it.  */
   4732  1.1  mrg   disable_ranger (fun);
   4733  1.1  mrg   m_ptr_qry.rvals = NULL;
   4734  1.1  mrg 
   4735  1.1  mrg   m_clobbers.empty ();
   4736  1.1  mrg   m_bb_uids_set = NULL;
   4737  1.1  mrg 
   4738  1.1  mrg   free_dominance_info (CDI_POST_DOMINATORS);
   4739  1.1  mrg   free_dominance_info (CDI_DOMINATORS);
   4740  1.1  mrg   return 0;
   4741  1.1  mrg }
   4742  1.1  mrg 
   4743  1.1  mrg }   // namespace
   4744  1.1  mrg 
   4745  1.1  mrg /* Return a new instance of the pass.  */
   4746  1.1  mrg 
   4747  1.1  mrg gimple_opt_pass *
   4748  1.1  mrg make_pass_warn_access (gcc::context *ctxt)
   4749  1.1  mrg {
   4750  1.1  mrg   return new pass_waccess (ctxt);
   4751  1.1  mrg }
   4752