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