| 1 |
/* Classes for modeling the state of memory. |
| 2 |
Copyright (C) 2019-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by David Malcolm <[email protected]>. |
| 4 |
|
| 5 |
This file is part of GCC. |
| 6 |
|
| 7 |
GCC is free software; you can redistribute it and/or modify it |
| 8 |
under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 3, or (at your option) |
| 10 |
any later version. |
| 11 |
|
| 12 |
GCC is distributed in the hope that it will be useful, but |
| 13 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with GCC; see the file COPYING3. If not see |
| 19 |
<http://www.gnu.org/licenses/>. */ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
#define INCLUDE_ALGORITHM |
| 23 |
#define INCLUDE_VECTOR |
| 24 |
#include "system.h" |
| 25 |
#include "coretypes.h" |
| 26 |
#include "make-unique.h" |
| 27 |
#include "tree.h" |
| 28 |
#include "function.h" |
| 29 |
#include "basic-block.h" |
| 30 |
#include "gimple.h" |
| 31 |
#include "gimple-iterator.h" |
| 32 |
#include "diagnostic-core.h" |
| 33 |
#include "graphviz.h" |
| 34 |
#include "options.h" |
| 35 |
#include "cgraph.h" |
| 36 |
#include "tree-dfa.h" |
| 37 |
#include "stringpool.h" |
| 38 |
#include "convert.h" |
| 39 |
#include "target.h" |
| 40 |
#include "fold-const.h" |
| 41 |
#include "tree-pretty-print.h" |
| 42 |
#include "diagnostic-color.h" |
| 43 |
#include "bitmap.h" |
| 44 |
#include "selftest.h" |
| 45 |
#include "selftest-tree.h" |
| 46 |
#include "analyzer/analyzer.h" |
| 47 |
#include "analyzer/analyzer-logging.h" |
| 48 |
#include "ordered-hash-map.h" |
| 49 |
#include "options.h" |
| 50 |
#include "cgraph.h" |
| 51 |
#include "cfg.h" |
| 52 |
#include "analyzer/supergraph.h" |
| 53 |
#include "sbitmap.h" |
| 54 |
#include "analyzer/call-string.h" |
| 55 |
#include "analyzer/program-point.h" |
| 56 |
#include "analyzer/store.h" |
| 57 |
#include "analyzer/region-model.h" |
| 58 |
#include "analyzer/constraint-manager.h" |
| 59 |
#include "diagnostic-event-id.h" |
| 60 |
#include "analyzer/sm.h" |
| 61 |
#include "diagnostic-event-id.h" |
| 62 |
#include "analyzer/sm.h" |
| 63 |
#include "analyzer/pending-diagnostic.h" |
| 64 |
#include "analyzer/region-model-reachability.h" |
| 65 |
#include "analyzer/analyzer-selftests.h" |
| 66 |
#include "analyzer/program-state.h" |
| 67 |
#include "analyzer/call-summary.h" |
| 68 |
#include "stor-layout.h" |
| 69 |
#include "attribs.h" |
| 70 |
#include "tree-object-size.h" |
| 71 |
#include "gimple-ssa.h" |
| 72 |
#include "tree-phinodes.h" |
| 73 |
#include "tree-ssa-operands.h" |
| 74 |
#include "ssa-iterators.h" |
| 75 |
#include "calls.h" |
| 76 |
#include "is-a.h" |
| 77 |
#include "gcc-rich-location.h" |
| 78 |
#include "analyzer/checker-event.h" |
| 79 |
#include "analyzer/checker-path.h" |
| 80 |
#include "analyzer/feasible-graph.h" |
| 81 |
#include "analyzer/record-layout.h" |
| 82 |
#include "diagnostic-format-sarif.h" |
| 83 |
#include "text-art/tree-widget.h" |
| 84 |
#include "gcc-urlifier.h" |
| 85 |
|
| 86 |
#if ENABLE_ANALYZER |
| 87 |
|
| 88 |
namespace ana { |
| 89 |
|
| 90 |
auto_vec<pop_frame_callback> region_model::pop_frame_callbacks; |
| 91 |
|
| 92 |
/* Dump T to PP in language-independent form, for debugging/logging/dumping |
| 93 |
purposes. */ |
| 94 |
|
| 95 |
void |
| 96 |
dump_tree (pretty_printer *pp, tree t) |
| 97 |
{ |
| 98 |
dump_generic_node (pp, t, 0, TDF_SLIM, 0); |
| 99 |
} |
| 100 |
|
| 101 |
/* Dump T to PP in language-independent form in quotes, for |
| 102 |
debugging/logging/dumping purposes. */ |
| 103 |
|
| 104 |
void |
| 105 |
dump_quoted_tree (pretty_printer *pp, tree t) |
| 106 |
{ |
| 107 |
pp_begin_quote (pp, pp_show_color (pp)); |
| 108 |
dump_tree (pp, t); |
| 109 |
pp_end_quote (pp, pp_show_color (pp)); |
| 110 |
} |
| 111 |
|
| 112 |
/* Equivalent to pp_printf (pp, "%qT", t), to avoid nesting pp_printf |
| 113 |
calls within other pp_printf calls. |
| 114 |
|
| 115 |
default_tree_printer handles 'T' and some other codes by calling |
| 116 |
dump_generic_node (pp, t, 0, TDF_SLIM, 0); |
| 117 |
dump_generic_node calls pp_printf in various places, leading to |
| 118 |
garbled output. |
| 119 |
|
| 120 |
Ideally pp_printf could be made to be reentrant, but in the meantime |
| 121 |
this function provides a workaround. */ |
| 122 |
|
| 123 |
void |
| 124 |
print_quoted_type (pretty_printer *pp, tree t) |
| 125 |
{ |
| 126 |
if (!t) |
| 127 |
return; |
| 128 |
pp_begin_quote (pp, pp_show_color (pp)); |
| 129 |
dump_generic_node (pp, t, 0, TDF_SLIM, 0); |
| 130 |
pp_end_quote (pp, pp_show_color (pp)); |
| 131 |
} |
| 132 |
|
| 133 |
/* Print EXPR to PP, without quotes. |
| 134 |
For use within svalue::maybe_print_for_user |
| 135 |
and region::maybe_print_for_user. */ |
| 136 |
|
| 137 |
void |
| 138 |
print_expr_for_user (pretty_printer *pp, tree expr) |
| 139 |
{ |
| 140 |
/* Workaround for C++'s lang_hooks.decl_printable_name, |
| 141 |
which unhelpfully (for us) prefixes the decl with its |
| 142 |
type. */ |
| 143 |
if (DECL_P (expr)) |
| 144 |
dump_generic_node (pp, expr, 0, TDF_SLIM, 0); |
| 145 |
else |
| 146 |
pp_printf (pp, "%E", expr); |
| 147 |
} |
| 148 |
|
| 149 |
/* class region_to_value_map. */ |
| 150 |
|
| 151 |
/* Assignment operator for region_to_value_map. */ |
| 152 |
|
| 153 |
region_to_value_map & |
| 154 |
region_to_value_map::operator= (const region_to_value_map &other) |
| 155 |
{ |
| 156 |
m_hash_map.empty (); |
| 157 |
for (auto iter : other.m_hash_map) |
| 158 |
{ |
| 159 |
const region *reg = iter.first; |
| 160 |
const svalue *sval = iter.second; |
| 161 |
m_hash_map.put (reg, sval); |
| 162 |
} |
| 163 |
return *this; |
| 164 |
} |
| 165 |
|
| 166 |
/* Equality operator for region_to_value_map. */ |
| 167 |
|
| 168 |
bool |
| 169 |
region_to_value_map::operator== (const region_to_value_map &other) const |
| 170 |
{ |
| 171 |
if (m_hash_map.elements () != other.m_hash_map.elements ()) |
| 172 |
return false; |
| 173 |
|
| 174 |
for (auto iter : *this) |
| 175 |
{ |
| 176 |
const region *reg = iter.first; |
| 177 |
const svalue *sval = iter.second; |
| 178 |
const svalue * const *other_slot = other.get (reg); |
| 179 |
if (other_slot == NULL) |
| 180 |
return false; |
| 181 |
if (sval != *other_slot) |
| 182 |
return false; |
| 183 |
} |
| 184 |
|
| 185 |
return true; |
| 186 |
} |
| 187 |
|
| 188 |
/* Dump this object to PP. */ |
| 189 |
|
| 190 |
void |
| 191 |
region_to_value_map::dump_to_pp (pretty_printer *pp, bool simple, |
| 192 |
bool multiline) const |
| 193 |
{ |
| 194 |
auto_vec<const region *> regs; |
| 195 |
for (iterator iter = begin (); iter != end (); ++iter) |
| 196 |
regs.safe_push ((*iter).first); |
| 197 |
regs.qsort (region::cmp_ptr_ptr); |
| 198 |
if (multiline) |
| 199 |
pp_newline (pp); |
| 200 |
else |
| 201 |
pp_string (pp, " {"); |
| 202 |
unsigned i; |
| 203 |
const region *reg; |
| 204 |
FOR_EACH_VEC_ELT (regs, i, reg) |
| 205 |
{ |
| 206 |
if (multiline) |
| 207 |
pp_string (pp, " "); |
| 208 |
else if (i > 0) |
| 209 |
pp_string (pp, ", "); |
| 210 |
reg->dump_to_pp (pp, simple); |
| 211 |
pp_string (pp, ": "); |
| 212 |
const svalue *sval = *get (reg); |
| 213 |
sval->dump_to_pp (pp, true); |
| 214 |
if (multiline) |
| 215 |
pp_newline (pp); |
| 216 |
} |
| 217 |
if (!multiline) |
| 218 |
pp_string (pp, "}"); |
| 219 |
} |
| 220 |
|
| 221 |
/* Dump this object to stderr. */ |
| 222 |
|
| 223 |
DEBUG_FUNCTION void |
| 224 |
region_to_value_map::dump (bool simple) const |
| 225 |
{ |
| 226 |
tree_dump_pretty_printer pp (stderr); |
| 227 |
dump_to_pp (&pp, simple, true); |
| 228 |
pp_newline (&pp); |
| 229 |
} |
| 230 |
|
| 231 |
/* Generate a JSON value for this region_to_value_map. |
| 232 |
This is intended for debugging the analyzer rather than |
| 233 |
serialization. */ |
| 234 |
|
| 235 |
std::unique_ptr<json::object> |
| 236 |
region_to_value_map::to_json () const |
| 237 |
{ |
| 238 |
auto map_obj = ::make_unique<json::object> (); |
| 239 |
|
| 240 |
auto_vec<const region *> regs; |
| 241 |
for (iterator iter = begin (); iter != end (); ++iter) |
| 242 |
regs.safe_push ((*iter).first); |
| 243 |
regs.qsort (region::cmp_ptr_ptr); |
| 244 |
|
| 245 |
unsigned i; |
| 246 |
const region *reg; |
| 247 |
FOR_EACH_VEC_ELT (regs, i, reg) |
| 248 |
{ |
| 249 |
label_text reg_desc = reg->get_desc (); |
| 250 |
const svalue *sval = *get (reg); |
| 251 |
map_obj->set (reg_desc.get (), sval->to_json ()); |
| 252 |
} |
| 253 |
|
| 254 |
return map_obj; |
| 255 |
} |
| 256 |
|
| 257 |
std::unique_ptr<text_art::tree_widget> |
| 258 |
region_to_value_map:: |
| 259 |
make_dump_widget (const text_art::dump_widget_info &dwi) const |
| 260 |
{ |
| 261 |
if (is_empty ()) |
| 262 |
return nullptr; |
| 263 |
|
| 264 |
std::unique_ptr<text_art::tree_widget> w |
| 265 |
(text_art::tree_widget::make (dwi, "Dynamic Extents")); |
| 266 |
|
| 267 |
auto_vec<const region *> regs; |
| 268 |
for (iterator iter = begin (); iter != end (); ++iter) |
| 269 |
regs.safe_push ((*iter).first); |
| 270 |
regs.qsort (region::cmp_ptr_ptr); |
| 271 |
|
| 272 |
unsigned i; |
| 273 |
const region *reg; |
| 274 |
FOR_EACH_VEC_ELT (regs, i, reg) |
| 275 |
{ |
| 276 |
pretty_printer the_pp; |
| 277 |
pretty_printer * const pp = &the_pp; |
| 278 |
pp_format_decoder (pp) = default_tree_printer; |
| 279 |
const bool simple = true; |
| 280 |
|
| 281 |
reg->dump_to_pp (pp, simple); |
| 282 |
pp_string (pp, ": "); |
| 283 |
const svalue *sval = *get (reg); |
| 284 |
sval->dump_to_pp (pp, true); |
| 285 |
w->add_child (text_art::tree_widget::make (dwi, pp)); |
| 286 |
} |
| 287 |
return w; |
| 288 |
} |
| 289 |
|
| 290 |
/* Attempt to merge THIS with OTHER, writing the result |
| 291 |
to OUT. |
| 292 |
|
| 293 |
For now, write (region, value) mappings that are in common between THIS |
| 294 |
and OTHER to OUT, effectively taking the intersection. |
| 295 |
|
| 296 |
Reject merger of different values. */ |
| 297 |
|
| 298 |
bool |
| 299 |
region_to_value_map::can_merge_with_p (const region_to_value_map &other, |
| 300 |
region_to_value_map *out) const |
| 301 |
{ |
| 302 |
for (auto iter : *this) |
| 303 |
{ |
| 304 |
const region *iter_reg = iter.first; |
| 305 |
const svalue *iter_sval = iter.second; |
| 306 |
const svalue * const * other_slot = other.get (iter_reg); |
| 307 |
if (other_slot) |
| 308 |
{ |
| 309 |
if (iter_sval == *other_slot) |
| 310 |
out->put (iter_reg, iter_sval); |
| 311 |
else |
| 312 |
return false; |
| 313 |
} |
| 314 |
} |
| 315 |
return true; |
| 316 |
} |
| 317 |
|
| 318 |
/* Purge any state involving SVAL. */ |
| 319 |
|
| 320 |
void |
| 321 |
region_to_value_map::purge_state_involving (const svalue *sval) |
| 322 |
{ |
| 323 |
auto_vec<const region *> to_purge; |
| 324 |
for (auto iter : *this) |
| 325 |
{ |
| 326 |
const region *iter_reg = iter.first; |
| 327 |
const svalue *iter_sval = iter.second; |
| 328 |
if (iter_reg->involves_p (sval) || iter_sval->involves_p (sval)) |
| 329 |
to_purge.safe_push (iter_reg); |
| 330 |
} |
| 331 |
for (auto iter : to_purge) |
| 332 |
m_hash_map.remove (iter); |
| 333 |
} |
| 334 |
|
| 335 |
/* class region_model. */ |
| 336 |
|
| 337 |
/* Ctor for region_model: construct an "empty" model. */ |
| 338 |
|
| 339 |
region_model::region_model (region_model_manager *mgr) |
| 340 |
: m_mgr (mgr), m_store (), m_current_frame (NULL), |
| 341 |
m_dynamic_extents () |
| 342 |
{ |
| 343 |
m_constraints = new constraint_manager (mgr); |
| 344 |
} |
| 345 |
|
| 346 |
/* region_model's copy ctor. */ |
| 347 |
|
| 348 |
region_model::region_model (const region_model &other) |
| 349 |
: m_mgr (other.m_mgr), m_store (other.m_store), |
| 350 |
m_constraints (new constraint_manager (*other.m_constraints)), |
| 351 |
m_current_frame (other.m_current_frame), |
| 352 |
m_dynamic_extents (other.m_dynamic_extents) |
| 353 |
{ |
| 354 |
} |
| 355 |
|
| 356 |
/* region_model's dtor. */ |
| 357 |
|
| 358 |
region_model::~region_model () |
| 359 |
{ |
| 360 |
delete m_constraints; |
| 361 |
} |
| 362 |
|
| 363 |
/* region_model's assignment operator. */ |
| 364 |
|
| 365 |
region_model & |
| 366 |
region_model::operator= (const region_model &other) |
| 367 |
{ |
| 368 |
/* m_mgr is const. */ |
| 369 |
gcc_assert (m_mgr == other.m_mgr); |
| 370 |
|
| 371 |
m_store = other.m_store; |
| 372 |
|
| 373 |
delete m_constraints; |
| 374 |
m_constraints = new constraint_manager (*other.m_constraints); |
| 375 |
|
| 376 |
m_current_frame = other.m_current_frame; |
| 377 |
|
| 378 |
m_dynamic_extents = other.m_dynamic_extents; |
| 379 |
|
| 380 |
return *this; |
| 381 |
} |
| 382 |
|
| 383 |
/* Equality operator for region_model. |
| 384 |
|
| 385 |
Amongst other things this directly compares the stores and the constraint |
| 386 |
managers, so for this to be meaningful both this and OTHER should |
| 387 |
have been canonicalized. */ |
| 388 |
|
| 389 |
bool |
| 390 |
region_model::operator== (const region_model &other) const |
| 391 |
{ |
| 392 |
/* We can only compare instances that use the same manager. */ |
| 393 |
gcc_assert (m_mgr == other.m_mgr); |
| 394 |
|
| 395 |
if (m_store != other.m_store) |
| 396 |
return false; |
| 397 |
|
| 398 |
if (*m_constraints != *other.m_constraints) |
| 399 |
return false; |
| 400 |
|
| 401 |
if (m_current_frame != other.m_current_frame) |
| 402 |
return false; |
| 403 |
|
| 404 |
if (m_dynamic_extents != other.m_dynamic_extents) |
| 405 |
return false; |
| 406 |
|
| 407 |
gcc_checking_assert (hash () == other.hash ()); |
| 408 |
|
| 409 |
return true; |
| 410 |
} |
| 411 |
|
| 412 |
/* Generate a hash value for this region_model. */ |
| 413 |
|
| 414 |
hashval_t |
| 415 |
region_model::hash () const |
| 416 |
{ |
| 417 |
hashval_t result = m_store.hash (); |
| 418 |
result ^= m_constraints->hash (); |
| 419 |
return result; |
| 420 |
} |
| 421 |
|
| 422 |
/* Dump a representation of this model to PP, showing the |
| 423 |
stack, the store, and any constraints. |
| 424 |
Use SIMPLE to control how svalues and regions are printed. */ |
| 425 |
|
| 426 |
void |
| 427 |
region_model::dump_to_pp (pretty_printer *pp, bool simple, |
| 428 |
bool multiline) const |
| 429 |
{ |
| 430 |
/* Dump stack. */ |
| 431 |
pp_printf (pp, "stack depth: %i", get_stack_depth ()); |
| 432 |
if (multiline) |
| 433 |
pp_newline (pp); |
| 434 |
else |
| 435 |
pp_string (pp, " {"); |
| 436 |
for (const frame_region *iter_frame = m_current_frame; iter_frame; |
| 437 |
iter_frame = iter_frame->get_calling_frame ()) |
| 438 |
{ |
| 439 |
if (multiline) |
| 440 |
pp_string (pp, " "); |
| 441 |
else if (iter_frame != m_current_frame) |
| 442 |
pp_string (pp, ", "); |
| 443 |
pp_printf (pp, "frame (index %i): ", iter_frame->get_index ()); |
| 444 |
iter_frame->dump_to_pp (pp, simple); |
| 445 |
if (multiline) |
| 446 |
pp_newline (pp); |
| 447 |
} |
| 448 |
if (!multiline) |
| 449 |
pp_string (pp, "}"); |
| 450 |
|
| 451 |
/* Dump store. */ |
| 452 |
if (!multiline) |
| 453 |
pp_string (pp, ", {"); |
| 454 |
m_store.dump_to_pp (pp, simple, multiline, |
| 455 |
m_mgr->get_store_manager ()); |
| 456 |
if (!multiline) |
| 457 |
pp_string (pp, "}"); |
| 458 |
|
| 459 |
/* Dump constraints. */ |
| 460 |
pp_string (pp, "constraint_manager:"); |
| 461 |
if (multiline) |
| 462 |
pp_newline (pp); |
| 463 |
else |
| 464 |
pp_string (pp, " {"); |
| 465 |
m_constraints->dump_to_pp (pp, multiline); |
| 466 |
if (!multiline) |
| 467 |
pp_string (pp, "}"); |
| 468 |
|
| 469 |
/* Dump sizes of dynamic regions, if any are known. */ |
| 470 |
if (!m_dynamic_extents.is_empty ()) |
| 471 |
{ |
| 472 |
pp_string (pp, "dynamic_extents:"); |
| 473 |
m_dynamic_extents.dump_to_pp (pp, simple, multiline); |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
/* Dump a representation of this model to FILE. */ |
| 478 |
|
| 479 |
void |
| 480 |
region_model::dump (FILE *fp, bool simple, bool multiline) const |
| 481 |
{ |
| 482 |
tree_dump_pretty_printer pp (fp); |
| 483 |
dump_to_pp (&pp, simple, multiline); |
| 484 |
pp_newline (&pp); |
| 485 |
} |
| 486 |
|
| 487 |
/* Dump a multiline representation of this model to stderr. */ |
| 488 |
|
| 489 |
DEBUG_FUNCTION void |
| 490 |
region_model::dump (bool simple) const |
| 491 |
{ |
| 492 |
dump (stderr, simple, true); |
| 493 |
} |
| 494 |
|
| 495 |
/* Dump a tree-like representation of this state to stderr. */ |
| 496 |
|
| 497 |
DEBUG_FUNCTION void |
| 498 |
region_model::dump () const |
| 499 |
{ |
| 500 |
text_art::dump (*this); |
| 501 |
} |
| 502 |
|
| 503 |
/* Dump a multiline representation of this model to stderr. */ |
| 504 |
|
| 505 |
DEBUG_FUNCTION void |
| 506 |
region_model::debug () const |
| 507 |
{ |
| 508 |
dump (true); |
| 509 |
} |
| 510 |
|
| 511 |
/* Generate a JSON value for this region_model. |
| 512 |
This is intended for debugging the analyzer rather than |
| 513 |
serialization. */ |
| 514 |
|
| 515 |
std::unique_ptr<json::object> |
| 516 |
region_model::to_json () const |
| 517 |
{ |
| 518 |
auto model_obj = ::make_unique<json::object> (); |
| 519 |
model_obj->set ("store", m_store.to_json ()); |
| 520 |
model_obj->set ("constraints", m_constraints->to_json ()); |
| 521 |
if (m_current_frame) |
| 522 |
model_obj->set ("current_frame", m_current_frame->to_json ()); |
| 523 |
model_obj->set ("dynamic_extents", m_dynamic_extents.to_json ()); |
| 524 |
return model_obj; |
| 525 |
} |
| 526 |
|
| 527 |
std::unique_ptr<text_art::tree_widget> |
| 528 |
region_model::make_dump_widget (const text_art::dump_widget_info &dwi) const |
| 529 |
{ |
| 530 |
using text_art::tree_widget; |
| 531 |
std::unique_ptr<tree_widget> model_widget |
| 532 |
(tree_widget::from_fmt (dwi, nullptr, "Region Model")); |
| 533 |
|
| 534 |
if (m_current_frame) |
| 535 |
{ |
| 536 |
pretty_printer the_pp; |
| 537 |
pretty_printer * const pp = &the_pp; |
| 538 |
pp_format_decoder (pp) = default_tree_printer; |
| 539 |
pp_show_color (pp) = true; |
| 540 |
const bool simple = true; |
| 541 |
|
| 542 |
pp_string (pp, "Current Frame: "); |
| 543 |
m_current_frame->dump_to_pp (pp, simple); |
| 544 |
model_widget->add_child (tree_widget::make (dwi, pp)); |
| 545 |
} |
| 546 |
model_widget->add_child |
| 547 |
(m_store.make_dump_widget (dwi, |
| 548 |
m_mgr->get_store_manager ())); |
| 549 |
model_widget->add_child (m_constraints->make_dump_widget (dwi)); |
| 550 |
model_widget->add_child (m_dynamic_extents.make_dump_widget (dwi)); |
| 551 |
return model_widget; |
| 552 |
} |
| 553 |
|
| 554 |
/* Assert that this object is valid. */ |
| 555 |
|
| 556 |
void |
| 557 |
region_model::validate () const |
| 558 |
{ |
| 559 |
m_store.validate (); |
| 560 |
} |
| 561 |
|
| 562 |
/* Canonicalize the store and constraints, to maximize the chance of |
| 563 |
equality between region_model instances. */ |
| 564 |
|
| 565 |
void |
| 566 |
region_model::canonicalize () |
| 567 |
{ |
| 568 |
m_store.canonicalize (m_mgr->get_store_manager ()); |
| 569 |
m_constraints->canonicalize (); |
| 570 |
} |
| 571 |
|
| 572 |
/* Return true if this region_model is in canonical form. */ |
| 573 |
|
| 574 |
bool |
| 575 |
region_model::canonicalized_p () const |
| 576 |
{ |
| 577 |
region_model copy (*this); |
| 578 |
copy.canonicalize (); |
| 579 |
return *this == copy; |
| 580 |
} |
| 581 |
|
| 582 |
/* See the comment for store::loop_replay_fixup. */ |
| 583 |
|
| 584 |
void |
| 585 |
region_model::loop_replay_fixup (const region_model *dst_state) |
| 586 |
{ |
| 587 |
m_store.loop_replay_fixup (dst_state->get_store (), m_mgr); |
| 588 |
} |
| 589 |
|
| 590 |
/* A subclass of pending_diagnostic for complaining about uses of |
| 591 |
poisoned values. */ |
| 592 |
|
| 593 |
class poisoned_value_diagnostic |
| 594 |
: public pending_diagnostic_subclass<poisoned_value_diagnostic> |
| 595 |
{ |
| 596 |
public: |
| 597 |
poisoned_value_diagnostic (tree expr, enum poison_kind pkind, |
| 598 |
const region *src_region, |
| 599 |
tree check_expr) |
| 600 |
: m_expr (expr), m_pkind (pkind), |
| 601 |
m_src_region (src_region), |
| 602 |
m_check_expr (check_expr) |
| 603 |
{} |
| 604 |
|
| 605 |
const char *get_kind () const final override { return "poisoned_value_diagnostic"; } |
| 606 |
|
| 607 |
bool use_of_uninit_p () const final override |
| 608 |
{ |
| 609 |
return m_pkind == POISON_KIND_UNINIT; |
| 610 |
} |
| 611 |
|
| 612 |
bool operator== (const poisoned_value_diagnostic &other) const |
| 613 |
{ |
| 614 |
return (m_expr == other.m_expr |
| 615 |
&& m_pkind == other.m_pkind |
| 616 |
&& m_src_region == other.m_src_region); |
| 617 |
} |
| 618 |
|
| 619 |
int get_controlling_option () const final override |
| 620 |
{ |
| 621 |
switch (m_pkind) |
| 622 |
{ |
| 623 |
default: |
| 624 |
gcc_unreachable (); |
| 625 |
case POISON_KIND_UNINIT: |
| 626 |
return OPT_Wanalyzer_use_of_uninitialized_value; |
| 627 |
case POISON_KIND_FREED: |
| 628 |
case POISON_KIND_DELETED: |
| 629 |
return OPT_Wanalyzer_use_after_free; |
| 630 |
case POISON_KIND_POPPED_STACK: |
| 631 |
return OPT_Wanalyzer_use_of_pointer_in_stale_stack_frame; |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
bool terminate_path_p () const final override { return true; } |
| 636 |
|
| 637 |
bool emit (diagnostic_emission_context &ctxt) final override |
| 638 |
{ |
| 639 |
switch (m_pkind) |
| 640 |
{ |
| 641 |
default: |
| 642 |
gcc_unreachable (); |
| 643 |
case POISON_KIND_UNINIT: |
| 644 |
{ |
| 645 |
ctxt.add_cwe (457); /* "CWE-457: Use of Uninitialized Variable". */ |
| 646 |
return ctxt.warn ("use of uninitialized value %qE", |
| 647 |
m_expr); |
| 648 |
} |
| 649 |
break; |
| 650 |
case POISON_KIND_FREED: |
| 651 |
{ |
| 652 |
ctxt.add_cwe (416); /* "CWE-416: Use After Free". */ |
| 653 |
return ctxt.warn ("use after %<free%> of %qE", |
| 654 |
m_expr); |
| 655 |
} |
| 656 |
break; |
| 657 |
case POISON_KIND_DELETED: |
| 658 |
{ |
| 659 |
ctxt.add_cwe (416); /* "CWE-416: Use After Free". */ |
| 660 |
return ctxt.warn ("use after %<delete%> of %qE", |
| 661 |
m_expr); |
| 662 |
} |
| 663 |
break; |
| 664 |
case POISON_KIND_POPPED_STACK: |
| 665 |
{ |
| 666 |
/* TODO: which CWE? */ |
| 667 |
return ctxt.warn |
| 668 |
("dereferencing pointer %qE to within stale stack frame", |
| 669 |
m_expr); |
| 670 |
} |
| 671 |
break; |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
bool |
| 676 |
describe_final_event (pretty_printer &pp, |
| 677 |
const evdesc::final_event &) final override |
| 678 |
{ |
| 679 |
switch (m_pkind) |
| 680 |
{ |
| 681 |
default: |
| 682 |
gcc_unreachable (); |
| 683 |
case POISON_KIND_UNINIT: |
| 684 |
{ |
| 685 |
pp_printf (&pp, |
| 686 |
"use of uninitialized value %qE here", |
| 687 |
m_expr); |
| 688 |
return true; |
| 689 |
} |
| 690 |
case POISON_KIND_FREED: |
| 691 |
{ |
| 692 |
pp_printf (&pp, |
| 693 |
"use after %<free%> of %qE here", |
| 694 |
m_expr); |
| 695 |
return true; |
| 696 |
} |
| 697 |
case POISON_KIND_DELETED: |
| 698 |
{ |
| 699 |
pp_printf (&pp, |
| 700 |
"use after %<delete%> of %qE here", |
| 701 |
m_expr); |
| 702 |
return true; |
| 703 |
} |
| 704 |
case POISON_KIND_POPPED_STACK: |
| 705 |
{ |
| 706 |
pp_printf (&pp, |
| 707 |
"dereferencing pointer %qE to within stale stack frame", |
| 708 |
m_expr); |
| 709 |
return true; |
| 710 |
} |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
void mark_interesting_stuff (interesting_t *interest) final override |
| 715 |
{ |
| 716 |
if (m_src_region) |
| 717 |
interest->add_region_creation (m_src_region); |
| 718 |
} |
| 719 |
|
| 720 |
/* Attempt to suppress false positives. |
| 721 |
Reject paths where the value of the underlying region isn't poisoned. |
| 722 |
This can happen due to state merging when exploring the exploded graph, |
| 723 |
where the more precise analysis during feasibility analysis finds that |
| 724 |
the region is in fact valid. |
| 725 |
To do this we need to get the value from the fgraph. Unfortunately |
| 726 |
we can't simply query the state of m_src_region (from the enode), |
| 727 |
since it might be a different region in the fnode state (e.g. with |
| 728 |
heap-allocated regions, the numbering could be different). |
| 729 |
Hence we access m_check_expr, if available. */ |
| 730 |
|
| 731 |
bool check_valid_fpath_p (const feasible_node &fnode, |
| 732 |
const gimple *emission_stmt) |
| 733 |
const final override |
| 734 |
{ |
| 735 |
if (!m_check_expr) |
| 736 |
return true; |
| 737 |
|
| 738 |
/* We've reached the enode, but not necessarily the right function_point. |
| 739 |
Try to get the state at the correct stmt. */ |
| 740 |
region_model emission_model (fnode.get_model ().get_manager()); |
| 741 |
if (!fnode.get_state_at_stmt (emission_stmt, &emission_model)) |
| 742 |
/* Couldn't get state; accept this diagnostic. */ |
| 743 |
return true; |
| 744 |
|
| 745 |
const svalue *fsval = emission_model.get_rvalue (m_check_expr, NULL); |
| 746 |
/* Check to see if the expr is also poisoned in FNODE (and in the |
| 747 |
same way). */ |
| 748 |
const poisoned_svalue * fspval = fsval->dyn_cast_poisoned_svalue (); |
| 749 |
if (!fspval) |
| 750 |
return false; |
| 751 |
if (fspval->get_poison_kind () != m_pkind) |
| 752 |
return false; |
| 753 |
return true; |
| 754 |
} |
| 755 |
|
| 756 |
void |
| 757 |
maybe_add_sarif_properties (sarif_object &result_obj) const final override |
| 758 |
{ |
| 759 |
sarif_property_bag &props = result_obj.get_or_create_properties (); |
| 760 |
#define PROPERTY_PREFIX "gcc/analyzer/poisoned_value_diagnostic/" |
| 761 |
props.set (PROPERTY_PREFIX "expr", tree_to_json (m_expr)); |
| 762 |
props.set_string (PROPERTY_PREFIX "kind", poison_kind_to_str (m_pkind)); |
| 763 |
if (m_src_region) |
| 764 |
props.set (PROPERTY_PREFIX "src_region", m_src_region->to_json ()); |
| 765 |
props.set (PROPERTY_PREFIX "check_expr", tree_to_json (m_check_expr)); |
| 766 |
#undef PROPERTY_PREFIX |
| 767 |
} |
| 768 |
|
| 769 |
private: |
| 770 |
tree m_expr; |
| 771 |
enum poison_kind m_pkind; |
| 772 |
const region *m_src_region; |
| 773 |
tree m_check_expr; |
| 774 |
}; |
| 775 |
|
| 776 |
/* A subclass of pending_diagnostic for complaining about shifts |
| 777 |
by negative counts. */ |
| 778 |
|
| 779 |
class shift_count_negative_diagnostic |
| 780 |
: public pending_diagnostic_subclass<shift_count_negative_diagnostic> |
| 781 |
{ |
| 782 |
public: |
| 783 |
shift_count_negative_diagnostic (const gassign *assign, tree count_cst) |
| 784 |
: m_assign (assign), m_count_cst (count_cst) |
| 785 |
{} |
| 786 |
|
| 787 |
const char *get_kind () const final override |
| 788 |
{ |
| 789 |
return "shift_count_negative_diagnostic"; |
| 790 |
} |
| 791 |
|
| 792 |
bool operator== (const shift_count_negative_diagnostic &other) const |
| 793 |
{ |
| 794 |
return (m_assign == other.m_assign |
| 795 |
&& same_tree_p (m_count_cst, other.m_count_cst)); |
| 796 |
} |
| 797 |
|
| 798 |
int get_controlling_option () const final override |
| 799 |
{ |
| 800 |
return OPT_Wanalyzer_shift_count_negative; |
| 801 |
} |
| 802 |
|
| 803 |
bool emit (diagnostic_emission_context &ctxt) final override |
| 804 |
{ |
| 805 |
return ctxt.warn ("shift by negative count (%qE)", m_count_cst); |
| 806 |
} |
| 807 |
|
| 808 |
bool |
| 809 |
describe_final_event (pretty_printer &pp, |
| 810 |
const evdesc::final_event &) final override |
| 811 |
{ |
| 812 |
pp_printf (&pp, |
| 813 |
"shift by negative amount here (%qE)", |
| 814 |
m_count_cst); |
| 815 |
return true; |
| 816 |
} |
| 817 |
|
| 818 |
private: |
| 819 |
const gassign *m_assign; |
| 820 |
tree m_count_cst; |
| 821 |
}; |
| 822 |
|
| 823 |
/* A subclass of pending_diagnostic for complaining about shifts |
| 824 |
by counts >= the width of the operand type. */ |
| 825 |
|
| 826 |
class shift_count_overflow_diagnostic |
| 827 |
: public pending_diagnostic_subclass<shift_count_overflow_diagnostic> |
| 828 |
{ |
| 829 |
public: |
| 830 |
shift_count_overflow_diagnostic (const gassign *assign, |
| 831 |
int operand_precision, |
| 832 |
tree count_cst) |
| 833 |
: m_assign (assign), m_operand_precision (operand_precision), |
| 834 |
m_count_cst (count_cst) |
| 835 |
{} |
| 836 |
|
| 837 |
const char *get_kind () const final override |
| 838 |
{ |
| 839 |
return "shift_count_overflow_diagnostic"; |
| 840 |
} |
| 841 |
|
| 842 |
bool operator== (const shift_count_overflow_diagnostic &other) const |
| 843 |
{ |
| 844 |
return (m_assign == other.m_assign |
| 845 |
&& m_operand_precision == other.m_operand_precision |
| 846 |
&& same_tree_p (m_count_cst, other.m_count_cst)); |
| 847 |
} |
| 848 |
|
| 849 |
int get_controlling_option () const final override |
| 850 |
{ |
| 851 |
return OPT_Wanalyzer_shift_count_overflow; |
| 852 |
} |
| 853 |
|
| 854 |
bool emit (diagnostic_emission_context &ctxt) final override |
| 855 |
{ |
| 856 |
return ctxt.warn ("shift by count (%qE) >= precision of type (%qi)", |
| 857 |
m_count_cst, m_operand_precision); |
| 858 |
} |
| 859 |
|
| 860 |
bool |
| 861 |
describe_final_event (pretty_printer &pp, |
| 862 |
const evdesc::final_event &) final override |
| 863 |
{ |
| 864 |
pp_printf (&pp, |
| 865 |
"shift by count %qE here", |
| 866 |
m_count_cst); |
| 867 |
return true; |
| 868 |
} |
| 869 |
|
| 870 |
private: |
| 871 |
const gassign *m_assign; |
| 872 |
int m_operand_precision; |
| 873 |
tree m_count_cst; |
| 874 |
}; |
| 875 |
|
| 876 |
/* A subclass of pending_diagnostic for complaining about pointer |
| 877 |
subtractions involving unrelated buffers. */ |
| 878 |
|
| 879 |
class undefined_ptrdiff_diagnostic |
| 880 |
: public pending_diagnostic_subclass<undefined_ptrdiff_diagnostic> |
| 881 |
{ |
| 882 |
public: |
| 883 |
/* Region_creation_event subclass to give a custom wording when |
| 884 |
talking about creation of buffers for LHS and RHS of the |
| 885 |
subtraction. */ |
| 886 |
class ptrdiff_region_creation_event : public region_creation_event |
| 887 |
{ |
| 888 |
public: |
| 889 |
ptrdiff_region_creation_event (const event_loc_info &loc_info, |
| 890 |
bool is_lhs) |
| 891 |
: region_creation_event (loc_info), |
| 892 |
m_is_lhs (is_lhs) |
| 893 |
{ |
| 894 |
} |
| 895 |
|
| 896 |
void print_desc (pretty_printer &pp) const final override |
| 897 |
{ |
| 898 |
if (m_is_lhs) |
| 899 |
pp_string (&pp, |
| 900 |
"underlying object for left-hand side" |
| 901 |
" of subtraction created here"); |
| 902 |
else |
| 903 |
pp_string (&pp, |
| 904 |
"underlying object for right-hand side" |
| 905 |
" of subtraction created here"); |
| 906 |
} |
| 907 |
|
| 908 |
private: |
| 909 |
bool m_is_lhs; |
| 910 |
}; |
| 911 |
|
| 912 |
undefined_ptrdiff_diagnostic (const gassign *assign, |
| 913 |
const svalue *sval_a, |
| 914 |
const svalue *sval_b, |
| 915 |
const region *base_reg_a, |
| 916 |
const region *base_reg_b) |
| 917 |
: m_assign (assign), |
| 918 |
m_sval_a (sval_a), |
| 919 |
m_sval_b (sval_b), |
| 920 |
m_base_reg_a (base_reg_a), |
| 921 |
m_base_reg_b (base_reg_b) |
| 922 |
{ |
| 923 |
gcc_assert (m_base_reg_a != m_base_reg_b); |
| 924 |
} |
| 925 |
|
| 926 |
const char *get_kind () const final override |
| 927 |
{ |
| 928 |
return "undefined_ptrdiff_diagnostic"; |
| 929 |
} |
| 930 |
|
| 931 |
bool operator== (const undefined_ptrdiff_diagnostic &other) const |
| 932 |
{ |
| 933 |
return (m_assign == other.m_assign |
| 934 |
&& m_sval_a == other.m_sval_a |
| 935 |
&& m_sval_b == other.m_sval_b |
| 936 |
&& m_base_reg_a == other.m_base_reg_a |
| 937 |
&& m_base_reg_b == other.m_base_reg_b); |
| 938 |
} |
| 939 |
|
| 940 |
int get_controlling_option () const final override |
| 941 |
{ |
| 942 |
return OPT_Wanalyzer_undefined_behavior_ptrdiff; |
| 943 |
} |
| 944 |
|
| 945 |
bool emit (diagnostic_emission_context &ctxt) final override |
| 946 |
{ |
| 947 |
/* CWE-469: Use of Pointer Subtraction to Determine Size. */ |
| 948 |
ctxt.add_cwe (469); |
| 949 |
return ctxt.warn ("undefined behavior when subtracting pointers"); |
| 950 |
} |
| 951 |
|
| 952 |
void add_region_creation_events (const region *reg, |
| 953 |
tree /*capacity*/, |
| 954 |
const event_loc_info &loc_info, |
| 955 |
checker_path &emission_path) final override |
| 956 |
{ |
| 957 |
if (reg == m_base_reg_a) |
| 958 |
emission_path.add_event |
| 959 |
(make_unique<ptrdiff_region_creation_event> (loc_info, true)); |
| 960 |
else if (reg == m_base_reg_b) |
| 961 |
emission_path.add_event |
| 962 |
(make_unique<ptrdiff_region_creation_event> (loc_info, false)); |
| 963 |
} |
| 964 |
|
| 965 |
bool |
| 966 |
describe_final_event (pretty_printer &pp, |
| 967 |
const evdesc::final_event &) final override |
| 968 |
{ |
| 969 |
pp_string (&pp, |
| 970 |
"subtraction of pointers has undefined behavior if" |
| 971 |
" they do not point into the same array object"); |
| 972 |
return true; |
| 973 |
} |
| 974 |
|
| 975 |
void mark_interesting_stuff (interesting_t *interesting) final override |
| 976 |
{ |
| 977 |
interesting->add_region_creation (m_base_reg_a); |
| 978 |
interesting->add_region_creation (m_base_reg_b); |
| 979 |
} |
| 980 |
|
| 981 |
private: |
| 982 |
const gassign *m_assign; |
| 983 |
const svalue *m_sval_a; |
| 984 |
const svalue *m_sval_b; |
| 985 |
const region *m_base_reg_a; |
| 986 |
const region *m_base_reg_b; |
| 987 |
}; |
| 988 |
|
| 989 |
/* Check the pointer subtraction SVAL_A - SVAL_B at ASSIGN and add |
| 990 |
a warning to CTXT if they're not within the same base region. */ |
| 991 |
|
| 992 |
static void |
| 993 |
check_for_invalid_ptrdiff (const gassign *assign, |
| 994 |
region_model_context &ctxt, |
| 995 |
const svalue *sval_a, const svalue *sval_b) |
| 996 |
{ |
| 997 |
const region *base_reg_a = sval_a->maybe_get_deref_base_region (); |
| 998 |
if (!base_reg_a) |
| 999 |
return; |
| 1000 |
const region *base_reg_b = sval_b->maybe_get_deref_base_region (); |
| 1001 |
if (!base_reg_b) |
| 1002 |
return; |
| 1003 |
|
| 1004 |
if (base_reg_a == base_reg_b) |
| 1005 |
return; |
| 1006 |
|
| 1007 |
if (base_reg_a->get_kind () == RK_SYMBOLIC) |
| 1008 |
return; |
| 1009 |
if (base_reg_b->get_kind () == RK_SYMBOLIC) |
| 1010 |
return; |
| 1011 |
|
| 1012 |
ctxt.warn (make_unique<undefined_ptrdiff_diagnostic> (assign, |
| 1013 |
sval_a, |
| 1014 |
sval_b, |
| 1015 |
base_reg_a, |
| 1016 |
base_reg_b)); |
| 1017 |
} |
| 1018 |
|
| 1019 |
/* If ASSIGN is a stmt that can be modelled via |
| 1020 |
set_value (lhs_reg, SVALUE, CTXT) |
| 1021 |
for some SVALUE, get the SVALUE. |
| 1022 |
Otherwise return NULL. */ |
| 1023 |
|
| 1024 |
const svalue * |
| 1025 |
region_model::get_gassign_result (const gassign *assign, |
| 1026 |
region_model_context *ctxt) |
| 1027 |
{ |
| 1028 |
tree lhs = gimple_assign_lhs (assign); |
| 1029 |
|
| 1030 |
if (gimple_has_volatile_ops (assign) |
| 1031 |
&& !gimple_clobber_p (assign)) |
| 1032 |
{ |
| 1033 |
conjured_purge p (this, ctxt); |
| 1034 |
return m_mgr->get_or_create_conjured_svalue (TREE_TYPE (lhs), |
| 1035 |
assign, |
| 1036 |
get_lvalue (lhs, ctxt), |
| 1037 |
p); |
| 1038 |
} |
| 1039 |
|
| 1040 |
tree rhs1 = gimple_assign_rhs1 (assign); |
| 1041 |
enum tree_code op = gimple_assign_rhs_code (assign); |
| 1042 |
switch (op) |
| 1043 |
{ |
| 1044 |
default: |
| 1045 |
return NULL; |
| 1046 |
|
| 1047 |
case POINTER_PLUS_EXPR: |
| 1048 |
{ |
| 1049 |
/* e.g. "_1 = a_10(D) + 12;" */ |
| 1050 |
tree ptr = rhs1; |
| 1051 |
tree offset = gimple_assign_rhs2 (assign); |
| 1052 |
|
| 1053 |
const svalue *ptr_sval = get_rvalue (ptr, ctxt); |
| 1054 |
const svalue *offset_sval = get_rvalue (offset, ctxt); |
| 1055 |
/* Quoting tree.def, "the second operand [of a POINTER_PLUS_EXPR] |
| 1056 |
is an integer of type sizetype". */ |
| 1057 |
offset_sval = m_mgr->get_or_create_cast (size_type_node, offset_sval); |
| 1058 |
|
| 1059 |
const svalue *sval_binop |
| 1060 |
= m_mgr->get_or_create_binop (TREE_TYPE (lhs), op, |
| 1061 |
ptr_sval, offset_sval); |
| 1062 |
return sval_binop; |
| 1063 |
} |
| 1064 |
break; |
| 1065 |
|
| 1066 |
case POINTER_DIFF_EXPR: |
| 1067 |
{ |
| 1068 |
/* e.g. "_1 = p_2(D) - q_3(D);". */ |
| 1069 |
tree rhs2 = gimple_assign_rhs2 (assign); |
| 1070 |
const svalue *rhs1_sval = get_rvalue (rhs1, ctxt); |
| 1071 |
const svalue *rhs2_sval = get_rvalue (rhs2, ctxt); |
| 1072 |
|
| 1073 |
// TODO: perhaps fold to zero if they're known to be equal? |
| 1074 |
|
| 1075 |
if (ctxt) |
| 1076 |
check_for_invalid_ptrdiff (assign, *ctxt, rhs1_sval, rhs2_sval); |
| 1077 |
|
| 1078 |
const svalue *sval_binop |
| 1079 |
= m_mgr->get_or_create_binop (TREE_TYPE (lhs), op, |
| 1080 |
rhs1_sval, rhs2_sval); |
| 1081 |
return sval_binop; |
| 1082 |
} |
| 1083 |
break; |
| 1084 |
|
| 1085 |
/* Assignments of the form |
| 1086 |
set_value (lvalue (LHS), rvalue (EXPR)) |
| 1087 |
for various EXPR. |
| 1088 |
We already have the lvalue for the LHS above, as "lhs_reg". */ |
| 1089 |
case ADDR_EXPR: /* LHS = &RHS; */ |
| 1090 |
case BIT_FIELD_REF: |
| 1091 |
case COMPONENT_REF: /* LHS = op0.op1; */ |
| 1092 |
case MEM_REF: |
| 1093 |
case REAL_CST: |
| 1094 |
case COMPLEX_CST: |
| 1095 |
case VECTOR_CST: |
| 1096 |
case INTEGER_CST: |
| 1097 |
case ARRAY_REF: |
| 1098 |
case SSA_NAME: /* LHS = VAR; */ |
| 1099 |
case VAR_DECL: /* LHS = VAR; */ |
| 1100 |
case PARM_DECL:/* LHS = VAR; */ |
| 1101 |
case REALPART_EXPR: |
| 1102 |
case IMAGPART_EXPR: |
| 1103 |
return get_rvalue (rhs1, ctxt); |
| 1104 |
|
| 1105 |
case ABS_EXPR: |
| 1106 |
case ABSU_EXPR: |
| 1107 |
case CONJ_EXPR: |
| 1108 |
case BIT_NOT_EXPR: |
| 1109 |
case FIX_TRUNC_EXPR: |
| 1110 |
case FLOAT_EXPR: |
| 1111 |
case NEGATE_EXPR: |
| 1112 |
case NOP_EXPR: |
| 1113 |
case VIEW_CONVERT_EXPR: |
| 1114 |
{ |
| 1115 |
/* Unary ops. */ |
| 1116 |
const svalue *rhs_sval = get_rvalue (rhs1, ctxt); |
| 1117 |
const svalue *sval_unaryop |
| 1118 |
= m_mgr->get_or_create_unaryop (TREE_TYPE (lhs), op, rhs_sval); |
| 1119 |
return sval_unaryop; |
| 1120 |
} |
| 1121 |
|
| 1122 |
case EQ_EXPR: |
| 1123 |
case GE_EXPR: |
| 1124 |
case LE_EXPR: |
| 1125 |
case NE_EXPR: |
| 1126 |
case GT_EXPR: |
| 1127 |
case LT_EXPR: |
| 1128 |
case UNORDERED_EXPR: |
| 1129 |
case ORDERED_EXPR: |
| 1130 |
{ |
| 1131 |
tree rhs2 = gimple_assign_rhs2 (assign); |
| 1132 |
|
| 1133 |
const svalue *rhs1_sval = get_rvalue (rhs1, ctxt); |
| 1134 |
const svalue *rhs2_sval = get_rvalue (rhs2, ctxt); |
| 1135 |
|
| 1136 |
if (TREE_TYPE (lhs) == boolean_type_node) |
| 1137 |
{ |
| 1138 |
/* Consider constraints between svalues. */ |
| 1139 |
tristate t = eval_condition (rhs1_sval, op, rhs2_sval); |
| 1140 |
if (t.is_known ()) |
| 1141 |
return m_mgr->get_or_create_constant_svalue |
| 1142 |
(t.is_true () ? boolean_true_node : boolean_false_node); |
| 1143 |
} |
| 1144 |
|
| 1145 |
/* Otherwise, generate a symbolic binary op. */ |
| 1146 |
const svalue *sval_binop |
| 1147 |
= m_mgr->get_or_create_binop (TREE_TYPE (lhs), op, |
| 1148 |
rhs1_sval, rhs2_sval); |
| 1149 |
return sval_binop; |
| 1150 |
} |
| 1151 |
break; |
| 1152 |
|
| 1153 |
case PLUS_EXPR: |
| 1154 |
case MINUS_EXPR: |
| 1155 |
case MULT_EXPR: |
| 1156 |
case MULT_HIGHPART_EXPR: |
| 1157 |
case TRUNC_DIV_EXPR: |
| 1158 |
case CEIL_DIV_EXPR: |
| 1159 |
case FLOOR_DIV_EXPR: |
| 1160 |
case ROUND_DIV_EXPR: |
| 1161 |
case TRUNC_MOD_EXPR: |
| 1162 |
case CEIL_MOD_EXPR: |
| 1163 |
case FLOOR_MOD_EXPR: |
| 1164 |
case ROUND_MOD_EXPR: |
| 1165 |
case RDIV_EXPR: |
| 1166 |
case EXACT_DIV_EXPR: |
| 1167 |
case LSHIFT_EXPR: |
| 1168 |
case RSHIFT_EXPR: |
| 1169 |
case LROTATE_EXPR: |
| 1170 |
case RROTATE_EXPR: |
| 1171 |
case BIT_IOR_EXPR: |
| 1172 |
case BIT_XOR_EXPR: |
| 1173 |
case BIT_AND_EXPR: |
| 1174 |
case MIN_EXPR: |
| 1175 |
case MAX_EXPR: |
| 1176 |
case COMPLEX_EXPR: |
| 1177 |
{ |
| 1178 |
/* Binary ops. */ |
| 1179 |
tree rhs2 = gimple_assign_rhs2 (assign); |
| 1180 |
|
| 1181 |
const svalue *rhs1_sval = get_rvalue (rhs1, ctxt); |
| 1182 |
const svalue *rhs2_sval = get_rvalue (rhs2, ctxt); |
| 1183 |
|
| 1184 |
if (ctxt && (op == LSHIFT_EXPR || op == RSHIFT_EXPR)) |
| 1185 |
{ |
| 1186 |
/* "INT34-C. Do not shift an expression by a negative number of bits |
| 1187 |
or by greater than or equal to the number of bits that exist in |
| 1188 |
the operand." */ |
| 1189 |
if (const tree rhs2_cst = rhs2_sval->maybe_get_constant ()) |
| 1190 |
if (TREE_CODE (rhs2_cst) == INTEGER_CST |
| 1191 |
&& INTEGRAL_TYPE_P (TREE_TYPE (rhs1))) |
| 1192 |
{ |
| 1193 |
if (tree_int_cst_sgn (rhs2_cst) < 0) |
| 1194 |
ctxt->warn |
| 1195 |
(make_unique<shift_count_negative_diagnostic> |
| 1196 |
(assign, rhs2_cst)); |
| 1197 |
else if (compare_tree_int (rhs2_cst, |
| 1198 |
TYPE_PRECISION (TREE_TYPE (rhs1))) |
| 1199 |
>= 0) |
| 1200 |
ctxt->warn |
| 1201 |
(make_unique<shift_count_overflow_diagnostic> |
| 1202 |
(assign, |
| 1203 |
int (TYPE_PRECISION (TREE_TYPE (rhs1))), |
| 1204 |
rhs2_cst)); |
| 1205 |
} |
| 1206 |
} |
| 1207 |
|
| 1208 |
const svalue *sval_binop |
| 1209 |
= m_mgr->get_or_create_binop (TREE_TYPE (lhs), op, |
| 1210 |
rhs1_sval, rhs2_sval); |
| 1211 |
return sval_binop; |
| 1212 |
} |
| 1213 |
|
| 1214 |
/* Vector expressions. In theory we could implement these elementwise, |
| 1215 |
but for now, simply return unknown values. */ |
| 1216 |
case VEC_DUPLICATE_EXPR: |
| 1217 |
case VEC_SERIES_EXPR: |
| 1218 |
case VEC_COND_EXPR: |
| 1219 |
case VEC_PERM_EXPR: |
| 1220 |
case VEC_WIDEN_MULT_HI_EXPR: |
| 1221 |
case VEC_WIDEN_MULT_LO_EXPR: |
| 1222 |
case VEC_WIDEN_MULT_EVEN_EXPR: |
| 1223 |
case VEC_WIDEN_MULT_ODD_EXPR: |
| 1224 |
case VEC_UNPACK_HI_EXPR: |
| 1225 |
case VEC_UNPACK_LO_EXPR: |
| 1226 |
case VEC_UNPACK_FLOAT_HI_EXPR: |
| 1227 |
case VEC_UNPACK_FLOAT_LO_EXPR: |
| 1228 |
case VEC_UNPACK_FIX_TRUNC_HI_EXPR: |
| 1229 |
case VEC_UNPACK_FIX_TRUNC_LO_EXPR: |
| 1230 |
case VEC_PACK_TRUNC_EXPR: |
| 1231 |
case VEC_PACK_SAT_EXPR: |
| 1232 |
case VEC_PACK_FIX_TRUNC_EXPR: |
| 1233 |
case VEC_PACK_FLOAT_EXPR: |
| 1234 |
case VEC_WIDEN_LSHIFT_HI_EXPR: |
| 1235 |
case VEC_WIDEN_LSHIFT_LO_EXPR: |
| 1236 |
return m_mgr->get_or_create_unknown_svalue (TREE_TYPE (lhs)); |
| 1237 |
} |
| 1238 |
} |
| 1239 |
|
| 1240 |
/* Workaround for discarding certain false positives from |
| 1241 |
-Wanalyzer-use-of-uninitialized-value |
| 1242 |
of the form: |
| 1243 |
((A OR-IF B) OR-IF C) |
| 1244 |
and: |
| 1245 |
((A AND-IF B) AND-IF C) |
| 1246 |
where evaluating B is redundant, but could involve simple accesses of |
| 1247 |
uninitialized locals. |
| 1248 |
|
| 1249 |
When optimization is turned on the FE can immediately fold compound |
| 1250 |
conditionals. Specifically, c_parser_condition parses this condition: |
| 1251 |
((A OR-IF B) OR-IF C) |
| 1252 |
and calls c_fully_fold on the condition. |
| 1253 |
Within c_fully_fold, fold_truth_andor is called, which bails when |
| 1254 |
optimization is off, but if any optimization is turned on can convert the |
| 1255 |
((A OR-IF B) OR-IF C) |
| 1256 |
into: |
| 1257 |
((A OR B) OR_IF C) |
| 1258 |
for sufficiently simple B |
| 1259 |
i.e. the inner OR-IF becomes an OR. |
| 1260 |
At gimplification time the inner OR becomes BIT_IOR_EXPR (in gimplify_expr), |
| 1261 |
giving this for the inner condition: |
| 1262 |
tmp = A | B; |
| 1263 |
if (tmp) |
| 1264 |
thus effectively synthesizing a redundant access of B when optimization |
| 1265 |
is turned on, when compared to: |
| 1266 |
if (A) goto L1; else goto L4; |
| 1267 |
L1: if (B) goto L2; else goto L4; |
| 1268 |
L2: if (C) goto L3; else goto L4; |
| 1269 |
for the unoptimized case. |
| 1270 |
|
| 1271 |
Return true if CTXT appears to be handling such a short-circuitable stmt, |
| 1272 |
such as the def-stmt for B for the: |
| 1273 |
tmp = A | B; |
| 1274 |
case above, for the case where A is true and thus B would have been |
| 1275 |
short-circuited without optimization, using MODEL for the value of A. */ |
| 1276 |
|
| 1277 |
static bool |
| 1278 |
within_short_circuited_stmt_p (const region_model *model, |
| 1279 |
const gassign *assign_stmt) |
| 1280 |
{ |
| 1281 |
/* We must have an assignment to a temporary of _Bool type. */ |
| 1282 |
tree lhs = gimple_assign_lhs (assign_stmt); |
| 1283 |
if (TREE_TYPE (lhs) != boolean_type_node) |
| 1284 |
return false; |
| 1285 |
if (TREE_CODE (lhs) != SSA_NAME) |
| 1286 |
return false; |
| 1287 |
if (SSA_NAME_VAR (lhs) != NULL_TREE) |
| 1288 |
return false; |
| 1289 |
|
| 1290 |
/* The temporary bool must be used exactly once: as the second arg of |
| 1291 |
a BIT_IOR_EXPR or BIT_AND_EXPR. */ |
| 1292 |
use_operand_p use_op; |
| 1293 |
gimple *use_stmt; |
| 1294 |
if (!single_imm_use (lhs, &use_op, &use_stmt)) |
| 1295 |
return false; |
| 1296 |
const gassign *use_assign = dyn_cast <const gassign *> (use_stmt); |
| 1297 |
if (!use_assign) |
| 1298 |
return false; |
| 1299 |
enum tree_code op = gimple_assign_rhs_code (use_assign); |
| 1300 |
if (!(op == BIT_IOR_EXPR ||op == BIT_AND_EXPR)) |
| 1301 |
return false; |
| 1302 |
if (!(gimple_assign_rhs1 (use_assign) != lhs |
| 1303 |
&& gimple_assign_rhs2 (use_assign) == lhs)) |
| 1304 |
return false; |
| 1305 |
|
| 1306 |
/* The first arg of the bitwise stmt must have a known value in MODEL |
| 1307 |
that implies that the value of the second arg doesn't matter, i.e. |
| 1308 |
1 for bitwise or, 0 for bitwise and. */ |
| 1309 |
tree other_arg = gimple_assign_rhs1 (use_assign); |
| 1310 |
/* Use a NULL ctxt here to avoid generating warnings. */ |
| 1311 |
const svalue *other_arg_sval = model->get_rvalue (other_arg, NULL); |
| 1312 |
tree other_arg_cst = other_arg_sval->maybe_get_constant (); |
| 1313 |
if (!other_arg_cst) |
| 1314 |
return false; |
| 1315 |
switch (op) |
| 1316 |
{ |
| 1317 |
default: |
| 1318 |
gcc_unreachable (); |
| 1319 |
case BIT_IOR_EXPR: |
| 1320 |
if (zerop (other_arg_cst)) |
| 1321 |
return false; |
| 1322 |
break; |
| 1323 |
case BIT_AND_EXPR: |
| 1324 |
if (!zerop (other_arg_cst)) |
| 1325 |
return false; |
| 1326 |
break; |
| 1327 |
} |
| 1328 |
|
| 1329 |
/* All tests passed. We appear to be in a stmt that generates a boolean |
| 1330 |
temporary with a value that won't matter. */ |
| 1331 |
return true; |
| 1332 |
} |
| 1333 |
|
| 1334 |
/* Workaround for discarding certain false positives from |
| 1335 |
-Wanalyzer-use-of-uninitialized-value |
| 1336 |
seen with -ftrivial-auto-var-init=. |
| 1337 |
|
| 1338 |
-ftrivial-auto-var-init= will generate calls to IFN_DEFERRED_INIT. |
| 1339 |
|
| 1340 |
If the address of the var is taken, gimplification will give us |
| 1341 |
something like: |
| 1342 |
|
| 1343 |
_1 = .DEFERRED_INIT (4, 2, &"len"[0]); |
| 1344 |
len = _1; |
| 1345 |
|
| 1346 |
The result of DEFERRED_INIT will be an uninit value; we don't |
| 1347 |
want to emit a false positive for "len = _1;" |
| 1348 |
|
| 1349 |
Return true if ASSIGN_STMT is such a stmt. */ |
| 1350 |
|
| 1351 |
static bool |
| 1352 |
due_to_ifn_deferred_init_p (const gassign *assign_stmt) |
| 1353 |
|
| 1354 |
{ |
| 1355 |
/* We must have an assignment to a decl from an SSA name that's the |
| 1356 |
result of a IFN_DEFERRED_INIT call. */ |
| 1357 |
if (gimple_assign_rhs_code (assign_stmt) != SSA_NAME) |
| 1358 |
return false; |
| 1359 |
tree lhs = gimple_assign_lhs (assign_stmt); |
| 1360 |
if (TREE_CODE (lhs) != VAR_DECL) |
| 1361 |
return false; |
| 1362 |
tree rhs = gimple_assign_rhs1 (assign_stmt); |
| 1363 |
if (TREE_CODE (rhs) != SSA_NAME) |
| 1364 |
return false; |
| 1365 |
const gimple *def_stmt = SSA_NAME_DEF_STMT (rhs); |
| 1366 |
const gcall *call = dyn_cast <const gcall *> (def_stmt); |
| 1367 |
if (!call) |
| 1368 |
return false; |
| 1369 |
if (gimple_call_internal_p (call) |
| 1370 |
&& gimple_call_internal_fn (call) == IFN_DEFERRED_INIT) |
| 1371 |
return true; |
| 1372 |
return false; |
| 1373 |
} |
| 1374 |
|
| 1375 |
/* Check for SVAL being poisoned, adding a warning to CTXT. |
| 1376 |
Return SVAL, or, if a warning is added, another value, to avoid |
| 1377 |
repeatedly complaining about the same poisoned value in followup code. |
| 1378 |
SRC_REGION is a hint about where SVAL came from, and can be NULL. */ |
| 1379 |
|
| 1380 |
const svalue * |
| 1381 |
region_model::check_for_poison (const svalue *sval, |
| 1382 |
tree expr, |
| 1383 |
const region *src_region, |
| 1384 |
region_model_context *ctxt) const |
| 1385 |
{ |
| 1386 |
if (!ctxt) |
| 1387 |
return sval; |
| 1388 |
|
| 1389 |
if (const poisoned_svalue *poisoned_sval = sval->dyn_cast_poisoned_svalue ()) |
| 1390 |
{ |
| 1391 |
enum poison_kind pkind = poisoned_sval->get_poison_kind (); |
| 1392 |
|
| 1393 |
/* Ignore uninitialized uses of empty types; there's nothing |
| 1394 |
to initialize. */ |
| 1395 |
if (pkind == POISON_KIND_UNINIT |
| 1396 |
&& sval->get_type () |
| 1397 |
&& is_empty_type (sval->get_type ())) |
| 1398 |
return sval; |
| 1399 |
|
| 1400 |
if (pkind == POISON_KIND_UNINIT) |
| 1401 |
if (const gimple *curr_stmt = ctxt->get_stmt ()) |
| 1402 |
if (const gassign *assign_stmt |
| 1403 |
= dyn_cast <const gassign *> (curr_stmt)) |
| 1404 |
{ |
| 1405 |
/* Special case to avoid certain false positives. */ |
| 1406 |
if (within_short_circuited_stmt_p (this, assign_stmt)) |
| 1407 |
return sval; |
| 1408 |
|
| 1409 |
/* Special case to avoid false positive on |
| 1410 |
-ftrivial-auto-var-init=. */ |
| 1411 |
if (due_to_ifn_deferred_init_p (assign_stmt)) |
| 1412 |
return sval; |
| 1413 |
} |
| 1414 |
|
| 1415 |
/* If we have an SSA name for a temporary, we don't want to print |
| 1416 |
'<unknown>'. |
| 1417 |
Poisoned values are shared by type, and so we can't reconstruct |
| 1418 |
the tree other than via the def stmts, using |
| 1419 |
fixup_tree_for_diagnostic. */ |
| 1420 |
tree diag_arg = fixup_tree_for_diagnostic (expr); |
| 1421 |
if (src_region == NULL && pkind == POISON_KIND_UNINIT) |
| 1422 |
src_region = get_region_for_poisoned_expr (expr); |
| 1423 |
|
| 1424 |
/* Can we reliably get the poisoned value from "expr"? |
| 1425 |
This is for use by poisoned_value_diagnostic::check_valid_fpath_p. |
| 1426 |
Unfortunately, we might not have a reliable value for EXPR. |
| 1427 |
Hence we only query its value now, and only use it if we get the |
| 1428 |
poisoned value back again. */ |
| 1429 |
tree check_expr = expr; |
| 1430 |
const svalue *foo_sval = get_rvalue (expr, NULL); |
| 1431 |
if (foo_sval == sval) |
| 1432 |
check_expr = expr; |
| 1433 |
else |
| 1434 |
check_expr = NULL; |
| 1435 |
if (ctxt->warn (make_unique<poisoned_value_diagnostic> (diag_arg, |
| 1436 |
pkind, |
| 1437 |
src_region, |
| 1438 |
check_expr))) |
| 1439 |
{ |
| 1440 |
/* We only want to report use of a poisoned value at the first |
| 1441 |
place it gets used; return an unknown value to avoid generating |
| 1442 |
a chain of followup warnings. */ |
| 1443 |
sval = m_mgr->get_or_create_unknown_svalue (sval->get_type ()); |
| 1444 |
} |
| 1445 |
|
| 1446 |
return sval; |
| 1447 |
} |
| 1448 |
|
| 1449 |
return sval; |
| 1450 |
} |
| 1451 |
|
| 1452 |
/* Attempt to get a region for describing EXPR, the source of region of |
| 1453 |
a poisoned_svalue for use in a poisoned_value_diagnostic. |
| 1454 |
Return NULL if there is no good region to use. */ |
| 1455 |
|
| 1456 |
const region * |
| 1457 |
region_model::get_region_for_poisoned_expr (tree expr) const |
| 1458 |
{ |
| 1459 |
if (TREE_CODE (expr) == SSA_NAME) |
| 1460 |
{ |
| 1461 |
tree decl = SSA_NAME_VAR (expr); |
| 1462 |
if (decl && DECL_P (decl)) |
| 1463 |
expr = decl; |
| 1464 |
else |
| 1465 |
return NULL; |
| 1466 |
} |
| 1467 |
return get_lvalue (expr, NULL); |
| 1468 |
} |
| 1469 |
|
| 1470 |
/* Update this model for the ASSIGN stmt, using CTXT to report any |
| 1471 |
diagnostics. */ |
| 1472 |
|
| 1473 |
void |
| 1474 |
region_model::on_assignment (const gassign *assign, region_model_context *ctxt) |
| 1475 |
{ |
| 1476 |
tree lhs = gimple_assign_lhs (assign); |
| 1477 |
tree rhs1 = gimple_assign_rhs1 (assign); |
| 1478 |
|
| 1479 |
const region *lhs_reg = get_lvalue (lhs, ctxt); |
| 1480 |
|
| 1481 |
/* Any writes other than to the stack are treated |
| 1482 |
as externally visible. */ |
| 1483 |
if (ctxt) |
| 1484 |
{ |
| 1485 |
enum memory_space memspace = lhs_reg->get_memory_space (); |
| 1486 |
if (memspace != MEMSPACE_STACK) |
| 1487 |
ctxt->maybe_did_work (); |
| 1488 |
} |
| 1489 |
|
| 1490 |
/* Most assignments are handled by: |
| 1491 |
set_value (lhs_reg, SVALUE, CTXT) |
| 1492 |
for some SVALUE. */ |
| 1493 |
if (const svalue *sval = get_gassign_result (assign, ctxt)) |
| 1494 |
{ |
| 1495 |
tree expr = get_diagnostic_tree_for_gassign (assign); |
| 1496 |
check_for_poison (sval, expr, NULL, ctxt); |
| 1497 |
set_value (lhs_reg, sval, ctxt); |
| 1498 |
return; |
| 1499 |
} |
| 1500 |
|
| 1501 |
enum tree_code op = gimple_assign_rhs_code (assign); |
| 1502 |
switch (op) |
| 1503 |
{ |
| 1504 |
default: |
| 1505 |
{ |
| 1506 |
if (0) |
| 1507 |
sorry_at (assign->location, "unhandled assignment op: %qs", |
| 1508 |
get_tree_code_name (op)); |
| 1509 |
const svalue *unknown_sval |
| 1510 |
= m_mgr->get_or_create_unknown_svalue (TREE_TYPE (lhs)); |
| 1511 |
set_value (lhs_reg, unknown_sval, ctxt); |
| 1512 |
} |
| 1513 |
break; |
| 1514 |
|
| 1515 |
case CONSTRUCTOR: |
| 1516 |
{ |
| 1517 |
if (TREE_CLOBBER_P (rhs1)) |
| 1518 |
{ |
| 1519 |
/* e.g. "x ={v} {CLOBBER};" */ |
| 1520 |
clobber_region (lhs_reg); |
| 1521 |
} |
| 1522 |
else |
| 1523 |
{ |
| 1524 |
/* Any CONSTRUCTOR that survives to this point is either |
| 1525 |
just a zero-init of everything, or a vector. */ |
| 1526 |
if (!CONSTRUCTOR_NO_CLEARING (rhs1)) |
| 1527 |
zero_fill_region (lhs_reg, ctxt); |
| 1528 |
unsigned ix; |
| 1529 |
tree index; |
| 1530 |
tree val; |
| 1531 |
FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (rhs1), ix, index, val) |
| 1532 |
{ |
| 1533 |
gcc_assert (TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE); |
| 1534 |
if (!index) |
| 1535 |
index = build_int_cst (integer_type_node, ix); |
| 1536 |
gcc_assert (TREE_CODE (index) == INTEGER_CST); |
| 1537 |
const svalue *index_sval |
| 1538 |
= m_mgr->get_or_create_constant_svalue (index); |
| 1539 |
gcc_assert (index_sval); |
| 1540 |
const region *sub_reg |
| 1541 |
= m_mgr->get_element_region (lhs_reg, |
| 1542 |
TREE_TYPE (val), |
| 1543 |
index_sval); |
| 1544 |
const svalue *val_sval = get_rvalue (val, ctxt); |
| 1545 |
set_value (sub_reg, val_sval, ctxt); |
| 1546 |
} |
| 1547 |
} |
| 1548 |
} |
| 1549 |
break; |
| 1550 |
|
| 1551 |
case STRING_CST: |
| 1552 |
{ |
| 1553 |
/* e.g. "struct s2 x = {{'A', 'B', 'C', 'D'}};". */ |
| 1554 |
const svalue *rhs_sval = get_rvalue (rhs1, ctxt); |
| 1555 |
m_store.set_value (m_mgr->get_store_manager(), lhs_reg, rhs_sval, |
| 1556 |
ctxt ? ctxt->get_uncertainty () : NULL); |
| 1557 |
} |
| 1558 |
break; |
| 1559 |
} |
| 1560 |
} |
| 1561 |
|
| 1562 |
/* Handle the pre-sm-state part of STMT, modifying this object in-place. |
| 1563 |
Write true to *OUT_UNKNOWN_SIDE_EFFECTS if the stmt has unknown |
| 1564 |
side effects. */ |
| 1565 |
|
| 1566 |
void |
| 1567 |
region_model::on_stmt_pre (const gimple *stmt, |
| 1568 |
bool *out_unknown_side_effects, |
| 1569 |
region_model_context *ctxt) |
| 1570 |
{ |
| 1571 |
switch (gimple_code (stmt)) |
| 1572 |
{ |
| 1573 |
default: |
| 1574 |
/* No-op for now. */ |
| 1575 |
break; |
| 1576 |
|
| 1577 |
case GIMPLE_DEBUG: |
| 1578 |
/* We should have stripped these out when building the supergraph. */ |
| 1579 |
gcc_unreachable (); |
| 1580 |
break; |
| 1581 |
|
| 1582 |
case GIMPLE_ASSIGN: |
| 1583 |
{ |
| 1584 |
const gassign *assign = as_a <const gassign *> (stmt); |
| 1585 |
on_assignment (assign, ctxt); |
| 1586 |
} |
| 1587 |
break; |
| 1588 |
|
| 1589 |
case GIMPLE_ASM: |
| 1590 |
{ |
| 1591 |
const gasm *asm_stmt = as_a <const gasm *> (stmt); |
| 1592 |
on_asm_stmt (asm_stmt, ctxt); |
| 1593 |
if (ctxt) |
| 1594 |
ctxt->maybe_did_work (); |
| 1595 |
} |
| 1596 |
break; |
| 1597 |
|
| 1598 |
case GIMPLE_CALL: |
| 1599 |
{ |
| 1600 |
/* Track whether we have a gcall to a function that's not recognized by |
| 1601 |
anything, for which we don't have a function body, or for which we |
| 1602 |
don't know the fndecl. */ |
| 1603 |
const gcall *call = as_a <const gcall *> (stmt); |
| 1604 |
*out_unknown_side_effects = on_call_pre (call, ctxt); |
| 1605 |
} |
| 1606 |
break; |
| 1607 |
|
| 1608 |
case GIMPLE_RETURN: |
| 1609 |
{ |
| 1610 |
const greturn *return_ = as_a <const greturn *> (stmt); |
| 1611 |
on_return (return_, ctxt); |
| 1612 |
} |
| 1613 |
break; |
| 1614 |
} |
| 1615 |
} |
| 1616 |
|
| 1617 |
/* Given a call CD with function attribute FORMAT_ATTR, check that the |
| 1618 |
format arg to the call is a valid null-terminated string. */ |
| 1619 |
|
| 1620 |
void |
| 1621 |
region_model::check_call_format_attr (const call_details &cd, |
| 1622 |
tree format_attr) const |
| 1623 |
{ |
| 1624 |
/* We assume that FORMAT_ATTR has already been validated. */ |
| 1625 |
|
| 1626 |
/* arg0 of the attribute should be kind of format strings |
| 1627 |
that this function expects (e.g. "printf"). */ |
| 1628 |
const tree arg0_tree_list = TREE_VALUE (format_attr); |
| 1629 |
if (!arg0_tree_list) |
| 1630 |
return; |
| 1631 |
|
| 1632 |
/* arg1 of the attribute should be the 1-based parameter index |
| 1633 |
to treat as the format string. */ |
| 1634 |
const tree arg1_tree_list = TREE_CHAIN (arg0_tree_list); |
| 1635 |
if (!arg1_tree_list) |
| 1636 |
return; |
| 1637 |
const tree arg1_value = TREE_VALUE (arg1_tree_list); |
| 1638 |
if (!arg1_value) |
| 1639 |
return; |
| 1640 |
|
| 1641 |
unsigned format_arg_idx = TREE_INT_CST_LOW (arg1_value) - 1; |
| 1642 |
if (cd.num_args () <= format_arg_idx) |
| 1643 |
return; |
| 1644 |
|
| 1645 |
/* Subclass of annotating_context that |
| 1646 |
adds a note about the format attr to any saved diagnostics. */ |
| 1647 |
class annotating_ctxt : public annotating_context |
| 1648 |
{ |
| 1649 |
public: |
| 1650 |
annotating_ctxt (const call_details &cd, |
| 1651 |
unsigned fmt_param_idx) |
| 1652 |
: annotating_context (cd.get_ctxt ()), |
| 1653 |
m_cd (cd), |
| 1654 |
m_fmt_param_idx (fmt_param_idx) |
| 1655 |
{ |
| 1656 |
} |
| 1657 |
void add_annotations () final override |
| 1658 |
{ |
| 1659 |
class reason_format_attr |
| 1660 |
: public pending_note_subclass<reason_format_attr> |
| 1661 |
{ |
| 1662 |
public: |
| 1663 |
reason_format_attr (const call_arg_details &arg_details) |
| 1664 |
: m_arg_details (arg_details) |
| 1665 |
{ |
| 1666 |
} |
| 1667 |
|
| 1668 |
const char *get_kind () const final override |
| 1669 |
{ |
| 1670 |
return "reason_format_attr"; |
| 1671 |
} |
| 1672 |
|
| 1673 |
void emit () const final override |
| 1674 |
{ |
| 1675 |
inform (DECL_SOURCE_LOCATION (m_arg_details.m_called_fndecl), |
| 1676 |
"parameter %i of %qD marked as a format string" |
| 1677 |
" via %qs attribute", |
| 1678 |
m_arg_details.m_arg_idx + 1, m_arg_details.m_called_fndecl, |
| 1679 |
"format"); |
| 1680 |
} |
| 1681 |
|
| 1682 |
bool operator== (const reason_format_attr &other) const |
| 1683 |
{ |
| 1684 |
return m_arg_details == other.m_arg_details; |
| 1685 |
} |
| 1686 |
|
| 1687 |
private: |
| 1688 |
call_arg_details m_arg_details; |
| 1689 |
}; |
| 1690 |
|
| 1691 |
call_arg_details arg_details (m_cd, m_fmt_param_idx); |
| 1692 |
add_note (make_unique<reason_format_attr> (arg_details)); |
| 1693 |
} |
| 1694 |
private: |
| 1695 |
const call_details &m_cd; |
| 1696 |
unsigned m_fmt_param_idx; |
| 1697 |
}; |
| 1698 |
|
| 1699 |
annotating_ctxt my_ctxt (cd, format_arg_idx); |
| 1700 |
call_details my_cd (cd, &my_ctxt); |
| 1701 |
my_cd.check_for_null_terminated_string_arg (format_arg_idx); |
| 1702 |
} |
| 1703 |
|
| 1704 |
/* Ensure that all arguments at the call described by CD are checked |
| 1705 |
for poisoned values, by calling get_rvalue on each argument. |
| 1706 |
|
| 1707 |
Check that calls to functions with "format" attribute have valid |
| 1708 |
null-terminated strings for their format argument. */ |
| 1709 |
|
| 1710 |
void |
| 1711 |
region_model::check_call_args (const call_details &cd) const |
| 1712 |
{ |
| 1713 |
for (unsigned arg_idx = 0; arg_idx < cd.num_args (); arg_idx++) |
| 1714 |
cd.get_arg_svalue (arg_idx); |
| 1715 |
|
| 1716 |
/* Handle attribute "format". */ |
| 1717 |
if (tree format_attr = cd.lookup_function_attribute ("format")) |
| 1718 |
check_call_format_attr (cd, format_attr); |
| 1719 |
} |
| 1720 |
|
| 1721 |
/* Update this model for an outcome of a call that returns a specific |
| 1722 |
integer constant. |
| 1723 |
If UNMERGEABLE, then make the result unmergeable, e.g. to prevent |
| 1724 |
the state-merger code from merging success and failure outcomes. */ |
| 1725 |
|
| 1726 |
void |
| 1727 |
region_model::update_for_int_cst_return (const call_details &cd, |
| 1728 |
int retval, |
| 1729 |
bool unmergeable) |
| 1730 |
{ |
| 1731 |
if (!cd.get_lhs_type ()) |
| 1732 |
return; |
| 1733 |
if (TREE_CODE (cd.get_lhs_type ()) != INTEGER_TYPE) |
| 1734 |
return; |
| 1735 |
const svalue *result |
| 1736 |
= m_mgr->get_or_create_int_cst (cd.get_lhs_type (), retval); |
| 1737 |
if (unmergeable) |
| 1738 |
result = m_mgr->get_or_create_unmergeable (result); |
| 1739 |
set_value (cd.get_lhs_region (), result, cd.get_ctxt ()); |
| 1740 |
} |
| 1741 |
|
| 1742 |
/* Update this model for an outcome of a call that returns zero. |
| 1743 |
If UNMERGEABLE, then make the result unmergeable, e.g. to prevent |
| 1744 |
the state-merger code from merging success and failure outcomes. */ |
| 1745 |
|
| 1746 |
void |
| 1747 |
region_model::update_for_zero_return (const call_details &cd, |
| 1748 |
bool unmergeable) |
| 1749 |
{ |
| 1750 |
update_for_int_cst_return (cd, 0, unmergeable); |
| 1751 |
} |
| 1752 |
|
| 1753 |
/* Update this model for an outcome of a call that returns non-zero. |
| 1754 |
Specifically, assign an svalue to the LHS, and add a constraint that |
| 1755 |
that svalue is non-zero. */ |
| 1756 |
|
| 1757 |
void |
| 1758 |
region_model::update_for_nonzero_return (const call_details &cd) |
| 1759 |
{ |
| 1760 |
if (!cd.get_lhs_type ()) |
| 1761 |
return; |
| 1762 |
if (TREE_CODE (cd.get_lhs_type ()) != INTEGER_TYPE) |
| 1763 |
return; |
| 1764 |
cd.set_any_lhs_with_defaults (); |
| 1765 |
const svalue *zero |
| 1766 |
= m_mgr->get_or_create_int_cst (cd.get_lhs_type (), 0); |
| 1767 |
const svalue *result |
| 1768 |
= get_store_value (cd.get_lhs_region (), cd.get_ctxt ()); |
| 1769 |
add_constraint (result, NE_EXPR, zero, cd.get_ctxt ()); |
| 1770 |
} |
| 1771 |
|
| 1772 |
/* Subroutine of region_model::maybe_get_copy_bounds. |
| 1773 |
The Linux kernel commonly uses |
| 1774 |
min_t([unsigned] long, VAR, sizeof(T)); |
| 1775 |
to set an upper bound on the size of a copy_to_user. |
| 1776 |
Attempt to simplify such sizes by trying to get the upper bound as a |
| 1777 |
constant. |
| 1778 |
Return the simplified svalue if possible, or NULL otherwise. */ |
| 1779 |
|
| 1780 |
static const svalue * |
| 1781 |
maybe_simplify_upper_bound (const svalue *num_bytes_sval, |
| 1782 |
region_model_manager *mgr) |
| 1783 |
{ |
| 1784 |
tree type = num_bytes_sval->get_type (); |
| 1785 |
while (const svalue *raw = num_bytes_sval->maybe_undo_cast ()) |
| 1786 |
num_bytes_sval = raw; |
| 1787 |
if (const binop_svalue *binop_sval = num_bytes_sval->dyn_cast_binop_svalue ()) |
| 1788 |
if (binop_sval->get_op () == MIN_EXPR) |
| 1789 |
if (binop_sval->get_arg1 ()->get_kind () == SK_CONSTANT) |
| 1790 |
{ |
| 1791 |
return mgr->get_or_create_cast (type, binop_sval->get_arg1 ()); |
| 1792 |
/* TODO: we might want to also capture the constraint |
| 1793 |
when recording the diagnostic, or note that we're using |
| 1794 |
the upper bound. */ |
| 1795 |
} |
| 1796 |
return NULL; |
| 1797 |
} |
| 1798 |
|
| 1799 |
/* Attempt to get an upper bound for the size of a copy when simulating a |
| 1800 |
copy function. |
| 1801 |
|
| 1802 |
NUM_BYTES_SVAL is the symbolic value for the size of the copy. |
| 1803 |
Use it if it's constant, otherwise try to simplify it. Failing |
| 1804 |
that, use the size of SRC_REG if constant. |
| 1805 |
|
| 1806 |
Return a symbolic value for an upper limit on the number of bytes |
| 1807 |
copied, or NULL if no such value could be determined. */ |
| 1808 |
|
| 1809 |
const svalue * |
| 1810 |
region_model::maybe_get_copy_bounds (const region *src_reg, |
| 1811 |
const svalue *num_bytes_sval) |
| 1812 |
{ |
| 1813 |
if (num_bytes_sval->maybe_get_constant ()) |
| 1814 |
return num_bytes_sval; |
| 1815 |
|
| 1816 |
if (const svalue *simplified |
| 1817 |
= maybe_simplify_upper_bound (num_bytes_sval, m_mgr)) |
| 1818 |
num_bytes_sval = simplified; |
| 1819 |
|
| 1820 |
if (num_bytes_sval->maybe_get_constant ()) |
| 1821 |
return num_bytes_sval; |
| 1822 |
|
| 1823 |
/* For now, try just guessing the size as the capacity of the |
| 1824 |
base region of the src. |
| 1825 |
This is a hack; we might get too large a value. */ |
| 1826 |
const region *src_base_reg = src_reg->get_base_region (); |
| 1827 |
num_bytes_sval = get_capacity (src_base_reg); |
| 1828 |
|
| 1829 |
if (num_bytes_sval->maybe_get_constant ()) |
| 1830 |
return num_bytes_sval; |
| 1831 |
|
| 1832 |
/* Non-constant: give up. */ |
| 1833 |
return NULL; |
| 1834 |
} |
| 1835 |
|
| 1836 |
/* Get any known_function for FNDECL for call CD. |
| 1837 |
|
| 1838 |
The call must match all assumptions made by the known_function (such as |
| 1839 |
e.g. "argument 1's type must be a pointer type"). |
| 1840 |
|
| 1841 |
Return NULL if no known_function is found, or it does not match the |
| 1842 |
assumption(s). */ |
| 1843 |
|
| 1844 |
const known_function * |
| 1845 |
region_model::get_known_function (tree fndecl, const call_details &cd) const |
| 1846 |
{ |
| 1847 |
known_function_manager *known_fn_mgr = m_mgr->get_known_function_manager (); |
| 1848 |
return known_fn_mgr->get_match (fndecl, cd); |
| 1849 |
} |
| 1850 |
|
| 1851 |
/* Get any known_function for IFN, or NULL. */ |
| 1852 |
|
| 1853 |
const known_function * |
| 1854 |
region_model::get_known_function (enum internal_fn ifn) const |
| 1855 |
{ |
| 1856 |
known_function_manager *known_fn_mgr = m_mgr->get_known_function_manager (); |
| 1857 |
return known_fn_mgr->get_internal_fn (ifn); |
| 1858 |
} |
| 1859 |
|
| 1860 |
/* Get any builtin_known_function for CALL and emit any warning to CTXT |
| 1861 |
if not NULL. |
| 1862 |
|
| 1863 |
The call must match all assumptions made by the known_function (such as |
| 1864 |
e.g. "argument 1's type must be a pointer type"). |
| 1865 |
|
| 1866 |
Return NULL if no builtin_known_function is found, or it does |
| 1867 |
not match the assumption(s). |
| 1868 |
|
| 1869 |
Internally calls get_known_function to find a known_function and cast it |
| 1870 |
to a builtin_known_function. |
| 1871 |
|
| 1872 |
For instance, calloc is a C builtin, defined in gcc/builtins.def |
| 1873 |
by the DEF_LIB_BUILTIN macro. Such builtins are recognized by the |
| 1874 |
analyzer by their name, so that even in C++ or if the user redeclares |
| 1875 |
them but mismatch their signature, they are still recognized as builtins. |
| 1876 |
|
| 1877 |
Cases when a supposed builtin is not flagged as one by the FE: |
| 1878 |
|
| 1879 |
The C++ FE does not recognize calloc as a builtin if it has not been |
| 1880 |
included from a standard header, but the C FE does. Hence in C++ if |
| 1881 |
CALL comes from a calloc and stdlib is not included, |
| 1882 |
gcc/tree.h:fndecl_built_in_p (CALL) would be false. |
| 1883 |
|
| 1884 |
In C code, a __SIZE_TYPE__ calloc (__SIZE_TYPE__, __SIZE_TYPE__) user |
| 1885 |
declaration has obviously a mismatching signature from the standard, and |
| 1886 |
its function_decl tree won't be unified by |
| 1887 |
gcc/c-decl.cc:match_builtin_function_types. |
| 1888 |
|
| 1889 |
Yet in both cases the analyzer should treat the calls as a builtin calloc |
| 1890 |
so that extra attributes unspecified by the standard but added by GCC |
| 1891 |
(e.g. sprintf attributes in gcc/builtins.def), useful for the detection of |
| 1892 |
dangerous behavior, are indeed processed. |
| 1893 |
|
| 1894 |
Therefore for those cases when a "builtin flag" is not added by the FE, |
| 1895 |
builtins' kf are derived from builtin_known_function, whose method |
| 1896 |
builtin_known_function::builtin_decl returns the builtin's |
| 1897 |
function_decl tree as defined in gcc/builtins.def, with all the extra |
| 1898 |
attributes. */ |
| 1899 |
|
| 1900 |
const builtin_known_function * |
| 1901 |
region_model::get_builtin_kf (const gcall *call, |
| 1902 |
region_model_context *ctxt /* = NULL */) const |
| 1903 |
{ |
| 1904 |
region_model *mut_this = const_cast <region_model *> (this); |
| 1905 |
tree callee_fndecl = mut_this->get_fndecl_for_call (call, ctxt); |
| 1906 |
if (! callee_fndecl) |
| 1907 |
return NULL; |
| 1908 |
|
| 1909 |
call_details cd (call, mut_this, ctxt); |
| 1910 |
if (const known_function *kf = get_known_function (callee_fndecl, cd)) |
| 1911 |
return kf->dyn_cast_builtin_kf (); |
| 1912 |
|
| 1913 |
return NULL; |
| 1914 |
} |
| 1915 |
|
| 1916 |
/* Update this model for the CALL stmt, using CTXT to report any |
| 1917 |
diagnostics - the first half. |
| 1918 |
|
| 1919 |
Updates to the region_model that should be made *before* sm-states |
| 1920 |
are updated are done here; other updates to the region_model are done |
| 1921 |
in region_model::on_call_post. |
| 1922 |
|
| 1923 |
Return true if the function call has unknown side effects (it wasn't |
| 1924 |
recognized and we don't have a body for it, or are unable to tell which |
| 1925 |
fndecl it is). */ |
| 1926 |
|
| 1927 |
bool |
| 1928 |
region_model::on_call_pre (const gcall *call, region_model_context *ctxt) |
| 1929 |
{ |
| 1930 |
call_details cd (call, this, ctxt); |
| 1931 |
|
| 1932 |
/* Special-case for IFN_DEFERRED_INIT. |
| 1933 |
We want to report uninitialized variables with -fanalyzer (treating |
| 1934 |
-ftrivial-auto-var-init= as purely a mitigation feature). |
| 1935 |
Handle IFN_DEFERRED_INIT by treating it as no-op: don't touch the |
| 1936 |
lhs of the call, so that it is still uninitialized from the point of |
| 1937 |
view of the analyzer. */ |
| 1938 |
if (gimple_call_internal_p (call) |
| 1939 |
&& gimple_call_internal_fn (call) == IFN_DEFERRED_INIT) |
| 1940 |
return false; /* No side effects. */ |
| 1941 |
|
| 1942 |
/* Get svalues for all of the arguments at the callsite, to ensure that we |
| 1943 |
complain about any uninitialized arguments. This might lead to |
| 1944 |
duplicates if any of the handling below also looks up the svalues, |
| 1945 |
but the deduplication code should deal with that. */ |
| 1946 |
if (ctxt) |
| 1947 |
check_call_args (cd); |
| 1948 |
|
| 1949 |
tree callee_fndecl = get_fndecl_for_call (call, ctxt); |
| 1950 |
|
| 1951 |
if (gimple_call_internal_p (call)) |
| 1952 |
if (const known_function *kf |
| 1953 |
= get_known_function (gimple_call_internal_fn (call))) |
| 1954 |
{ |
| 1955 |
kf->impl_call_pre (cd); |
| 1956 |
return false; /* No further side effects. */ |
| 1957 |
} |
| 1958 |
|
| 1959 |
if (!callee_fndecl) |
| 1960 |
{ |
| 1961 |
cd.set_any_lhs_with_defaults (); |
| 1962 |
return true; /* Unknown side effects. */ |
| 1963 |
} |
| 1964 |
|
| 1965 |
if (const known_function *kf = get_known_function (callee_fndecl, cd)) |
| 1966 |
{ |
| 1967 |
kf->impl_call_pre (cd); |
| 1968 |
return false; /* No further side effects. */ |
| 1969 |
} |
| 1970 |
|
| 1971 |
cd.set_any_lhs_with_defaults (); |
| 1972 |
|
| 1973 |
const int callee_fndecl_flags = flags_from_decl_or_type (callee_fndecl); |
| 1974 |
if (callee_fndecl_flags & (ECF_CONST | ECF_PURE)) |
| 1975 |
return false; /* No side effects. */ |
| 1976 |
|
| 1977 |
if (fndecl_built_in_p (callee_fndecl)) |
| 1978 |
return true; /* Unknown side effects. */ |
| 1979 |
|
| 1980 |
if (!fndecl_has_gimple_body_p (callee_fndecl)) |
| 1981 |
return true; /* Unknown side effects. */ |
| 1982 |
|
| 1983 |
return false; /* No side effects. */ |
| 1984 |
} |
| 1985 |
|
| 1986 |
/* Update this model for the CALL stmt, using CTXT to report any |
| 1987 |
diagnostics - the second half. |
| 1988 |
|
| 1989 |
Updates to the region_model that should be made *after* sm-states |
| 1990 |
are updated are done here; other updates to the region_model are done |
| 1991 |
in region_model::on_call_pre. |
| 1992 |
|
| 1993 |
If UNKNOWN_SIDE_EFFECTS is true, also call handle_unrecognized_call |
| 1994 |
to purge state. */ |
| 1995 |
|
| 1996 |
void |
| 1997 |
region_model::on_call_post (const gcall *call, |
| 1998 |
bool unknown_side_effects, |
| 1999 |
region_model_context *ctxt) |
| 2000 |
{ |
| 2001 |
if (tree callee_fndecl = get_fndecl_for_call (call, ctxt)) |
| 2002 |
{ |
| 2003 |
call_details cd (call, this, ctxt); |
| 2004 |
if (const known_function *kf = get_known_function (callee_fndecl, cd)) |
| 2005 |
{ |
| 2006 |
kf->impl_call_post (cd); |
| 2007 |
return; |
| 2008 |
} |
| 2009 |
/* Was this fndecl referenced by |
| 2010 |
__attribute__((malloc(FOO)))? */ |
| 2011 |
if (lookup_attribute ("*dealloc", DECL_ATTRIBUTES (callee_fndecl))) |
| 2012 |
{ |
| 2013 |
impl_deallocation_call (cd); |
| 2014 |
return; |
| 2015 |
} |
| 2016 |
} |
| 2017 |
|
| 2018 |
if (unknown_side_effects) |
| 2019 |
{ |
| 2020 |
handle_unrecognized_call (call, ctxt); |
| 2021 |
if (ctxt) |
| 2022 |
ctxt->maybe_did_work (); |
| 2023 |
} |
| 2024 |
} |
| 2025 |
|
| 2026 |
/* Purge state involving SVAL from this region_model, using CTXT |
| 2027 |
(if non-NULL) to purge other state in a program_state. |
| 2028 |
|
| 2029 |
For example, if we're at the def-stmt of an SSA name, then we need to |
| 2030 |
purge any state for svalues that involve that SSA name. This avoids |
| 2031 |
false positives in loops, since a symbolic value referring to the |
| 2032 |
SSA name will be referring to the previous value of that SSA name. |
| 2033 |
|
| 2034 |
For example, in: |
| 2035 |
while ((e = hashmap_iter_next(&iter))) { |
| 2036 |
struct oid2strbuf *e_strbuf = (struct oid2strbuf *)e; |
| 2037 |
free (e_strbuf->value); |
| 2038 |
} |
| 2039 |
at the def-stmt of e_8: |
| 2040 |
e_8 = hashmap_iter_next (&iter); |
| 2041 |
we should purge the "freed" state of: |
| 2042 |
INIT_VAL(CAST_REG(‘struct oid2strbuf’, (*INIT_VAL(e_8))).value) |
| 2043 |
which is the "e_strbuf->value" value from the previous iteration, |
| 2044 |
or we will erroneously report a double-free - the "e_8" within it |
| 2045 |
refers to the previous value. */ |
| 2046 |
|
| 2047 |
void |
| 2048 |
region_model::purge_state_involving (const svalue *sval, |
| 2049 |
region_model_context *ctxt) |
| 2050 |
{ |
| 2051 |
if (!sval->can_have_associated_state_p ()) |
| 2052 |
return; |
| 2053 |
m_store.purge_state_involving (sval, m_mgr); |
| 2054 |
m_constraints->purge_state_involving (sval); |
| 2055 |
m_dynamic_extents.purge_state_involving (sval); |
| 2056 |
if (ctxt) |
| 2057 |
ctxt->purge_state_involving (sval); |
| 2058 |
} |
| 2059 |
|
| 2060 |
/* A pending_note subclass for adding a note about an |
| 2061 |
__attribute__((access, ...)) to a diagnostic. */ |
| 2062 |
|
| 2063 |
class reason_attr_access : public pending_note_subclass<reason_attr_access> |
| 2064 |
{ |
| 2065 |
public: |
| 2066 |
reason_attr_access (tree callee_fndecl, const attr_access &access) |
| 2067 |
: m_callee_fndecl (callee_fndecl), |
| 2068 |
m_ptr_argno (access.ptrarg), |
| 2069 |
m_access_str (TREE_STRING_POINTER (access.to_external_string ())) |
| 2070 |
{ |
| 2071 |
} |
| 2072 |
|
| 2073 |
const char *get_kind () const final override { return "reason_attr_access"; } |
| 2074 |
|
| 2075 |
void emit () const final override |
| 2076 |
{ |
| 2077 |
auto_urlify_attributes sentinel; |
| 2078 |
inform (DECL_SOURCE_LOCATION (m_callee_fndecl), |
| 2079 |
"parameter %i of %qD marked with attribute %qs", |
| 2080 |
m_ptr_argno + 1, m_callee_fndecl, m_access_str); |
| 2081 |
} |
| 2082 |
|
| 2083 |
bool operator== (const reason_attr_access &other) const |
| 2084 |
{ |
| 2085 |
return (m_callee_fndecl == other.m_callee_fndecl |
| 2086 |
&& m_ptr_argno == other.m_ptr_argno |
| 2087 |
&& !strcmp (m_access_str, other.m_access_str)); |
| 2088 |
} |
| 2089 |
|
| 2090 |
private: |
| 2091 |
tree m_callee_fndecl; |
| 2092 |
unsigned m_ptr_argno; |
| 2093 |
const char *m_access_str; |
| 2094 |
}; |
| 2095 |
|
| 2096 |
/* Check CALL a call to external function CALLEE_FNDECL based on |
| 2097 |
any __attribute__ ((access, ....) on the latter, complaining to |
| 2098 |
CTXT about any issues. |
| 2099 |
|
| 2100 |
Currently we merely call check_region_for_write on any regions |
| 2101 |
pointed to by arguments marked with a "write_only" or "read_write" |
| 2102 |
attribute. */ |
| 2103 |
|
| 2104 |
void |
| 2105 |
region_model::check_function_attr_access (const gcall *call, |
| 2106 |
tree callee_fndecl, |
| 2107 |
region_model_context *ctxt, |
| 2108 |
rdwr_map &rdwr_idx) const |
| 2109 |
{ |
| 2110 |
gcc_assert (call); |
| 2111 |
gcc_assert (callee_fndecl); |
| 2112 |
gcc_assert (ctxt); |
| 2113 |
|
| 2114 |
tree fntype = TREE_TYPE (callee_fndecl); |
| 2115 |
gcc_assert (fntype); |
| 2116 |
|
| 2117 |
unsigned argno = 0; |
| 2118 |
|
| 2119 |
for (tree iter = TYPE_ARG_TYPES (fntype); iter; |
| 2120 |
iter = TREE_CHAIN (iter), ++argno) |
| 2121 |
{ |
| 2122 |
const attr_access* access = rdwr_idx.get (argno); |
| 2123 |
if (!access) |
| 2124 |
continue; |
| 2125 |
|
| 2126 |
/* Ignore any duplicate entry in the map for the size argument. */ |
| 2127 |
if (access->ptrarg != argno) |
| 2128 |
continue; |
| 2129 |
|
| 2130 |
if (access->mode == access_write_only |
| 2131 |
|| access->mode == access_read_write) |
| 2132 |
{ |
| 2133 |
/* Subclass of annotating_context that |
| 2134 |
adds a note about the attr access to any saved diagnostics. */ |
| 2135 |
class annotating_ctxt : public annotating_context |
| 2136 |
{ |
| 2137 |
public: |
| 2138 |
annotating_ctxt (tree callee_fndecl, |
| 2139 |
const attr_access &access, |
| 2140 |
region_model_context *ctxt) |
| 2141 |
: annotating_context (ctxt), |
| 2142 |
m_callee_fndecl (callee_fndecl), |
| 2143 |
m_access (access) |
| 2144 |
{ |
| 2145 |
} |
| 2146 |
void add_annotations () final override |
| 2147 |
{ |
| 2148 |
add_note (make_unique<reason_attr_access> |
| 2149 |
(m_callee_fndecl, m_access)); |
| 2150 |
} |
| 2151 |
private: |
| 2152 |
tree m_callee_fndecl; |
| 2153 |
const attr_access &m_access; |
| 2154 |
}; |
| 2155 |
|
| 2156 |
/* Use this ctxt below so that any diagnostics get the |
| 2157 |
note added to them. */ |
| 2158 |
annotating_ctxt my_ctxt (callee_fndecl, *access, ctxt); |
| 2159 |
|
| 2160 |
tree ptr_tree = gimple_call_arg (call, access->ptrarg); |
| 2161 |
const svalue *ptr_sval = get_rvalue (ptr_tree, &my_ctxt); |
| 2162 |
const region *reg = deref_rvalue (ptr_sval, ptr_tree, &my_ctxt); |
| 2163 |
check_region_for_write (reg, nullptr, &my_ctxt); |
| 2164 |
/* We don't use the size arg for now. */ |
| 2165 |
} |
| 2166 |
} |
| 2167 |
} |
| 2168 |
|
| 2169 |
/* Subroutine of region_model::check_function_attr_null_terminated_string_arg, |
| 2170 |
checking one instance of __attribute__((null_terminated_string_arg)). */ |
| 2171 |
|
| 2172 |
void |
| 2173 |
region_model:: |
| 2174 |
check_one_function_attr_null_terminated_string_arg (const gcall *call, |
| 2175 |
tree callee_fndecl, |
| 2176 |
region_model_context *ctxt, |
| 2177 |
rdwr_map &rdwr_idx, |
| 2178 |
tree attr) |
| 2179 |
{ |
| 2180 |
gcc_assert (call); |
| 2181 |
gcc_assert (callee_fndecl); |
| 2182 |
gcc_assert (ctxt); |
| 2183 |
gcc_assert (attr); |
| 2184 |
|
| 2185 |
tree arg = TREE_VALUE (attr); |
| 2186 |
if (!arg) |
| 2187 |
return; |
| 2188 |
|
| 2189 |
/* Convert from 1-based to 0-based index. */ |
| 2190 |
unsigned int arg_idx = TREE_INT_CST_LOW (TREE_VALUE (arg)) - 1; |
| 2191 |
|
| 2192 |
/* If there's also an "access" attribute on the ptr param |
| 2193 |
for reading with a size param specified, then that size |
| 2194 |
limits the size of the possible read from the pointer. */ |
| 2195 |
if (const attr_access* access = rdwr_idx.get (arg_idx)) |
| 2196 |
if ((access->mode == access_read_only |
| 2197 |
|| access->mode == access_read_write) |
| 2198 |
&& access->sizarg != UINT_MAX) |
| 2199 |
{ |
| 2200 |
call_details cd_checked (call, this, ctxt); |
| 2201 |
const svalue *limit_sval |
| 2202 |
= cd_checked.get_arg_svalue (access->sizarg); |
| 2203 |
const svalue *ptr_sval |
| 2204 |
= cd_checked.get_arg_svalue (arg_idx); |
| 2205 |
/* Try reading all of the bytes expressed by the size param, |
| 2206 |
but without emitting warnings (via a null context). */ |
| 2207 |
const svalue *limited_sval |
| 2208 |
= read_bytes (deref_rvalue (ptr_sval, NULL_TREE, nullptr), |
| 2209 |
NULL_TREE, |
| 2210 |
limit_sval, |
| 2211 |
nullptr); |
| 2212 |
if (limited_sval->get_kind () == SK_POISONED) |
| 2213 |
{ |
| 2214 |
/* Reading up to the truncation limit caused issues. |
| 2215 |
Assume that the string is meant to be terminated |
| 2216 |
before then, so perform a *checked* check for the |
| 2217 |
terminator. */ |
| 2218 |
check_for_null_terminated_string_arg (cd_checked, |
| 2219 |
arg_idx); |
| 2220 |
} |
| 2221 |
else |
| 2222 |
{ |
| 2223 |
/* Reading up to the truncation limit seems OK; repeat |
| 2224 |
the read, but with checking enabled. */ |
| 2225 |
read_bytes (deref_rvalue (ptr_sval, NULL_TREE, ctxt), |
| 2226 |
NULL_TREE, |
| 2227 |
limit_sval, |
| 2228 |
ctxt); |
| 2229 |
} |
| 2230 |
return; |
| 2231 |
} |
| 2232 |
|
| 2233 |
/* Otherwise, we don't have an access-attribute limiting the read. |
| 2234 |
Simulate a read up to the null terminator (if any). */ |
| 2235 |
|
| 2236 |
call_details cd (call, this, ctxt); |
| 2237 |
check_for_null_terminated_string_arg (cd, arg_idx); |
| 2238 |
} |
| 2239 |
|
| 2240 |
/* Check CALL a call to external function CALLEE_FNDECL for any uses |
| 2241 |
of __attribute__ ((null_terminated_string_arg)), compaining |
| 2242 |
to CTXT about any issues. |
| 2243 |
|
| 2244 |
Use RDWR_IDX for tracking uses of __attribute__ ((access, ....). */ |
| 2245 |
|
| 2246 |
void |
| 2247 |
region_model:: |
| 2248 |
check_function_attr_null_terminated_string_arg (const gcall *call, |
| 2249 |
tree callee_fndecl, |
| 2250 |
region_model_context *ctxt, |
| 2251 |
rdwr_map &rdwr_idx) |
| 2252 |
{ |
| 2253 |
gcc_assert (call); |
| 2254 |
gcc_assert (callee_fndecl); |
| 2255 |
gcc_assert (ctxt); |
| 2256 |
|
| 2257 |
tree fntype = TREE_TYPE (callee_fndecl); |
| 2258 |
gcc_assert (fntype); |
| 2259 |
|
| 2260 |
/* A function declaration can specify multiple attribute |
| 2261 |
null_terminated_string_arg, each with one argument. */ |
| 2262 |
for (tree attr = TYPE_ATTRIBUTES (fntype); attr; attr = TREE_CHAIN (attr)) |
| 2263 |
{ |
| 2264 |
attr = lookup_attribute ("null_terminated_string_arg", attr); |
| 2265 |
if (!attr) |
| 2266 |
return; |
| 2267 |
|
| 2268 |
check_one_function_attr_null_terminated_string_arg (call, callee_fndecl, |
| 2269 |
ctxt, rdwr_idx, |
| 2270 |
attr); |
| 2271 |
} |
| 2272 |
} |
| 2273 |
|
| 2274 |
/* Check CALL a call to external function CALLEE_FNDECL for any |
| 2275 |
function attributes, complaining to CTXT about any issues. */ |
| 2276 |
|
| 2277 |
void |
| 2278 |
region_model::check_function_attrs (const gcall *call, |
| 2279 |
tree callee_fndecl, |
| 2280 |
region_model_context *ctxt) |
| 2281 |
{ |
| 2282 |
gcc_assert (call); |
| 2283 |
gcc_assert (callee_fndecl); |
| 2284 |
gcc_assert (ctxt); |
| 2285 |
|
| 2286 |
tree fntype = TREE_TYPE (callee_fndecl); |
| 2287 |
if (!fntype) |
| 2288 |
return; |
| 2289 |
|
| 2290 |
if (!TYPE_ATTRIBUTES (fntype)) |
| 2291 |
return; |
| 2292 |
|
| 2293 |
/* Initialize a map of attribute access specifications for arguments |
| 2294 |
to the function call. */ |
| 2295 |
rdwr_map rdwr_idx; |
| 2296 |
init_attr_rdwr_indices (&rdwr_idx, TYPE_ATTRIBUTES (fntype)); |
| 2297 |
|
| 2298 |
check_function_attr_access (call, callee_fndecl, ctxt, rdwr_idx); |
| 2299 |
check_function_attr_null_terminated_string_arg (call, callee_fndecl, |
| 2300 |
ctxt, rdwr_idx); |
| 2301 |
} |
| 2302 |
|
| 2303 |
/* Handle a call CALL to a function with unknown behavior. |
| 2304 |
|
| 2305 |
Traverse the regions in this model, determining what regions are |
| 2306 |
reachable from pointer arguments to CALL and from global variables, |
| 2307 |
recursively. |
| 2308 |
|
| 2309 |
Set all reachable regions to new unknown values and purge sm-state |
| 2310 |
from their values, and from values that point to them. */ |
| 2311 |
|
| 2312 |
void |
| 2313 |
region_model::handle_unrecognized_call (const gcall *call, |
| 2314 |
region_model_context *ctxt) |
| 2315 |
{ |
| 2316 |
tree fndecl = get_fndecl_for_call (call, ctxt); |
| 2317 |
|
| 2318 |
if (fndecl && ctxt) |
| 2319 |
check_function_attrs (call, fndecl, ctxt); |
| 2320 |
|
| 2321 |
reachable_regions reachable_regs (this); |
| 2322 |
|
| 2323 |
/* Determine the reachable regions and their mutability. */ |
| 2324 |
{ |
| 2325 |
/* Add globals and regions that already escaped in previous |
| 2326 |
unknown calls. */ |
| 2327 |
m_store.for_each_cluster (reachable_regions::init_cluster_cb, |
| 2328 |
&reachable_regs); |
| 2329 |
|
| 2330 |
/* Params that are pointers. */ |
| 2331 |
tree iter_param_types = NULL_TREE; |
| 2332 |
if (fndecl) |
| 2333 |
iter_param_types = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); |
| 2334 |
for (unsigned arg_idx = 0; arg_idx < gimple_call_num_args (call); arg_idx++) |
| 2335 |
{ |
| 2336 |
/* Track expected param type, where available. */ |
| 2337 |
tree param_type = NULL_TREE; |
| 2338 |
if (iter_param_types) |
| 2339 |
{ |
| 2340 |
param_type = TREE_VALUE (iter_param_types); |
| 2341 |
gcc_assert (param_type); |
| 2342 |
iter_param_types = TREE_CHAIN (iter_param_types); |
| 2343 |
} |
| 2344 |
|
| 2345 |
tree parm = gimple_call_arg (call, arg_idx); |
| 2346 |
const svalue *parm_sval = get_rvalue (parm, ctxt); |
| 2347 |
reachable_regs.handle_parm (parm_sval, param_type); |
| 2348 |
} |
| 2349 |
} |
| 2350 |
|
| 2351 |
uncertainty_t *uncertainty = ctxt ? ctxt->get_uncertainty () : NULL; |
| 2352 |
|
| 2353 |
/* Purge sm-state for the svalues that were reachable, |
| 2354 |
both in non-mutable and mutable form. */ |
| 2355 |
for (svalue_set::iterator iter |
| 2356 |
= reachable_regs.begin_reachable_svals (); |
| 2357 |
iter != reachable_regs.end_reachable_svals (); ++iter) |
| 2358 |
{ |
| 2359 |
const svalue *sval = (*iter); |
| 2360 |
if (ctxt) |
| 2361 |
ctxt->on_unknown_change (sval, false); |
| 2362 |
} |
| 2363 |
for (svalue_set::iterator iter |
| 2364 |
= reachable_regs.begin_mutable_svals (); |
| 2365 |
iter != reachable_regs.end_mutable_svals (); ++iter) |
| 2366 |
{ |
| 2367 |
const svalue *sval = (*iter); |
| 2368 |
if (ctxt) |
| 2369 |
ctxt->on_unknown_change (sval, true); |
| 2370 |
if (uncertainty) |
| 2371 |
uncertainty->on_mutable_sval_at_unknown_call (sval); |
| 2372 |
} |
| 2373 |
|
| 2374 |
/* Mark any clusters that have escaped. */ |
| 2375 |
reachable_regs.mark_escaped_clusters (ctxt); |
| 2376 |
|
| 2377 |
/* Update bindings for all clusters that have escaped, whether above, |
| 2378 |
or previously. */ |
| 2379 |
m_store.on_unknown_fncall (call, m_mgr->get_store_manager (), |
| 2380 |
conjured_purge (this, ctxt)); |
| 2381 |
|
| 2382 |
/* Purge dynamic extents from any regions that have escaped mutably: |
| 2383 |
realloc could have been called on them. */ |
| 2384 |
for (hash_set<const region *>::iterator |
| 2385 |
iter = reachable_regs.begin_mutable_base_regs (); |
| 2386 |
iter != reachable_regs.end_mutable_base_regs (); |
| 2387 |
++iter) |
| 2388 |
{ |
| 2389 |
const region *base_reg = (*iter); |
| 2390 |
unset_dynamic_extents (base_reg); |
| 2391 |
} |
| 2392 |
} |
| 2393 |
|
| 2394 |
/* Traverse the regions in this model, determining what regions are |
| 2395 |
reachable from the store and populating *OUT. |
| 2396 |
|
| 2397 |
If EXTRA_SVAL is non-NULL, treat it as an additional "root" |
| 2398 |
for reachability (for handling return values from functions when |
| 2399 |
analyzing return of the only function on the stack). |
| 2400 |
|
| 2401 |
If UNCERTAINTY is non-NULL, treat any svalues that were recorded |
| 2402 |
within it as being maybe-bound as additional "roots" for reachability. |
| 2403 |
|
| 2404 |
Find svalues that haven't leaked. */ |
| 2405 |
|
| 2406 |
void |
| 2407 |
region_model::get_reachable_svalues (svalue_set *out, |
| 2408 |
const svalue *extra_sval, |
| 2409 |
const uncertainty_t *uncertainty) |
| 2410 |
{ |
| 2411 |
reachable_regions reachable_regs (this); |
| 2412 |
|
| 2413 |
/* Add globals and regions that already escaped in previous |
| 2414 |
unknown calls. */ |
| 2415 |
m_store.for_each_cluster (reachable_regions::init_cluster_cb, |
| 2416 |
&reachable_regs); |
| 2417 |
|
| 2418 |
if (extra_sval) |
| 2419 |
reachable_regs.handle_sval (extra_sval); |
| 2420 |
|
| 2421 |
if (uncertainty) |
| 2422 |
for (uncertainty_t::iterator iter |
| 2423 |
= uncertainty->begin_maybe_bound_svals (); |
| 2424 |
iter != uncertainty->end_maybe_bound_svals (); ++iter) |
| 2425 |
reachable_regs.handle_sval (*iter); |
| 2426 |
|
| 2427 |
/* Get regions for locals that have explicitly bound values. */ |
| 2428 |
for (store::cluster_map_t::iterator iter = m_store.begin (); |
| 2429 |
iter != m_store.end (); ++iter) |
| 2430 |
{ |
| 2431 |
const region *base_reg = (*iter).first; |
| 2432 |
if (const region *parent = base_reg->get_parent_region ()) |
| 2433 |
if (parent->get_kind () == RK_FRAME) |
| 2434 |
reachable_regs.add (base_reg, false); |
| 2435 |
} |
| 2436 |
|
| 2437 |
/* Populate *OUT based on the values that were reachable. */ |
| 2438 |
for (svalue_set::iterator iter |
| 2439 |
= reachable_regs.begin_reachable_svals (); |
| 2440 |
iter != reachable_regs.end_reachable_svals (); ++iter) |
| 2441 |
out->add (*iter); |
| 2442 |
} |
| 2443 |
|
| 2444 |
/* Update this model for the RETURN_STMT, using CTXT to report any |
| 2445 |
diagnostics. */ |
| 2446 |
|
| 2447 |
void |
| 2448 |
region_model::on_return (const greturn *return_stmt, region_model_context *ctxt) |
| 2449 |
{ |
| 2450 |
tree callee = get_current_function ()->decl; |
| 2451 |
tree lhs = DECL_RESULT (callee); |
| 2452 |
tree rhs = gimple_return_retval (return_stmt); |
| 2453 |
|
| 2454 |
if (lhs && rhs) |
| 2455 |
{ |
| 2456 |
const svalue *sval = get_rvalue (rhs, ctxt); |
| 2457 |
const region *ret_reg = get_lvalue (lhs, ctxt); |
| 2458 |
set_value (ret_reg, sval, ctxt); |
| 2459 |
} |
| 2460 |
} |
| 2461 |
|
| 2462 |
/* Update this model for a call and return of setjmp/sigsetjmp at CALL within |
| 2463 |
ENODE, using CTXT to report any diagnostics. |
| 2464 |
|
| 2465 |
This is for the initial direct invocation of setjmp/sigsetjmp (which returns |
| 2466 |
0), as opposed to any second return due to longjmp/sigsetjmp. */ |
| 2467 |
|
| 2468 |
void |
| 2469 |
region_model::on_setjmp (const gcall *call, const exploded_node *enode, |
| 2470 |
region_model_context *ctxt) |
| 2471 |
{ |
| 2472 |
const svalue *buf_ptr = get_rvalue (gimple_call_arg (call, 0), ctxt); |
| 2473 |
const region *buf_reg = deref_rvalue (buf_ptr, gimple_call_arg (call, 0), |
| 2474 |
ctxt); |
| 2475 |
|
| 2476 |
/* Create a setjmp_svalue for this call and store it in BUF_REG's |
| 2477 |
region. */ |
| 2478 |
if (buf_reg) |
| 2479 |
{ |
| 2480 |
setjmp_record r (enode, call); |
| 2481 |
const svalue *sval |
| 2482 |
= m_mgr->get_or_create_setjmp_svalue (r, buf_reg->get_type ()); |
| 2483 |
set_value (buf_reg, sval, ctxt); |
| 2484 |
} |
| 2485 |
|
| 2486 |
/* Direct calls to setjmp return 0. */ |
| 2487 |
if (tree lhs = gimple_call_lhs (call)) |
| 2488 |
{ |
| 2489 |
const svalue *new_sval |
| 2490 |
= m_mgr->get_or_create_int_cst (TREE_TYPE (lhs), 0); |
| 2491 |
const region *lhs_reg = get_lvalue (lhs, ctxt); |
| 2492 |
set_value (lhs_reg, new_sval, ctxt); |
| 2493 |
} |
| 2494 |
} |
| 2495 |
|
| 2496 |
/* Update this region_model for rewinding from a "longjmp" at LONGJMP_CALL |
| 2497 |
to a "setjmp" at SETJMP_CALL where the final stack depth should be |
| 2498 |
SETJMP_STACK_DEPTH. Pop any stack frames. Leak detection is *not* |
| 2499 |
done, and should be done by the caller. */ |
| 2500 |
|
| 2501 |
void |
| 2502 |
region_model::on_longjmp (const gcall *longjmp_call, const gcall *setjmp_call, |
| 2503 |
int setjmp_stack_depth, region_model_context *ctxt) |
| 2504 |
{ |
| 2505 |
/* Evaluate the val, using the frame of the "longjmp". */ |
| 2506 |
tree fake_retval = gimple_call_arg (longjmp_call, 1); |
| 2507 |
const svalue *fake_retval_sval = get_rvalue (fake_retval, ctxt); |
| 2508 |
|
| 2509 |
/* Pop any frames until we reach the stack depth of the function where |
| 2510 |
setjmp was called. */ |
| 2511 |
gcc_assert (get_stack_depth () >= setjmp_stack_depth); |
| 2512 |
while (get_stack_depth () > setjmp_stack_depth) |
| 2513 |
pop_frame (NULL, NULL, ctxt, nullptr, false); |
| 2514 |
|
| 2515 |
gcc_assert (get_stack_depth () == setjmp_stack_depth); |
| 2516 |
|
| 2517 |
/* Assign to LHS of "setjmp" in new_state. */ |
| 2518 |
if (tree lhs = gimple_call_lhs (setjmp_call)) |
| 2519 |
{ |
| 2520 |
/* Passing 0 as the val to longjmp leads to setjmp returning 1. */ |
| 2521 |
const svalue *zero_sval |
| 2522 |
= m_mgr->get_or_create_int_cst (TREE_TYPE (fake_retval), 0); |
| 2523 |
tristate eq_zero = eval_condition (fake_retval_sval, EQ_EXPR, zero_sval); |
| 2524 |
/* If we have 0, use 1. */ |
| 2525 |
if (eq_zero.is_true ()) |
| 2526 |
{ |
| 2527 |
const svalue *one_sval |
| 2528 |
= m_mgr->get_or_create_int_cst (TREE_TYPE (fake_retval), 1); |
| 2529 |
fake_retval_sval = one_sval; |
| 2530 |
} |
| 2531 |
else |
| 2532 |
{ |
| 2533 |
/* Otherwise note that the value is nonzero. */ |
| 2534 |
m_constraints->add_constraint (fake_retval_sval, NE_EXPR, zero_sval); |
| 2535 |
} |
| 2536 |
|
| 2537 |
/* Decorate the return value from setjmp as being unmergeable, |
| 2538 |
so that we don't attempt to merge states with it as zero |
| 2539 |
with states in which it's nonzero, leading to a clean distinction |
| 2540 |
in the exploded_graph betweeen the first return and the second |
| 2541 |
return. */ |
| 2542 |
fake_retval_sval = m_mgr->get_or_create_unmergeable (fake_retval_sval); |
| 2543 |
|
| 2544 |
const region *lhs_reg = get_lvalue (lhs, ctxt); |
| 2545 |
set_value (lhs_reg, fake_retval_sval, ctxt); |
| 2546 |
} |
| 2547 |
} |
| 2548 |
|
| 2549 |
/* Update this region_model for a phi stmt of the form |
| 2550 |
LHS = PHI <...RHS...>. |
| 2551 |
where RHS is for the appropriate edge. |
| 2552 |
Get state from OLD_STATE so that all of the phi stmts for a basic block |
| 2553 |
are effectively handled simultaneously. */ |
| 2554 |
|
| 2555 |
void |
| 2556 |
region_model::handle_phi (const gphi *phi, |
| 2557 |
tree lhs, tree rhs, |
| 2558 |
const region_model &old_state, |
| 2559 |
hash_set<const svalue *> &svals_changing_meaning, |
| 2560 |
region_model_context *ctxt) |
| 2561 |
{ |
| 2562 |
/* For now, don't bother tracking the .MEM SSA names. */ |
| 2563 |
if (tree var = SSA_NAME_VAR (lhs)) |
| 2564 |
if (TREE_CODE (var) == VAR_DECL) |
| 2565 |
if (VAR_DECL_IS_VIRTUAL_OPERAND (var)) |
| 2566 |
return; |
| 2567 |
|
| 2568 |
const svalue *src_sval = old_state.get_rvalue (rhs, ctxt); |
| 2569 |
const region *dst_reg = old_state.get_lvalue (lhs, ctxt); |
| 2570 |
|
| 2571 |
const svalue *sval = old_state.get_rvalue (lhs, nullptr); |
| 2572 |
if (sval->get_kind () == SK_WIDENING) |
| 2573 |
svals_changing_meaning.add (sval); |
| 2574 |
|
| 2575 |
set_value (dst_reg, src_sval, ctxt); |
| 2576 |
|
| 2577 |
if (ctxt) |
| 2578 |
ctxt->on_phi (phi, rhs); |
| 2579 |
} |
| 2580 |
|
| 2581 |
/* Implementation of region_model::get_lvalue; the latter adds type-checking. |
| 2582 |
|
| 2583 |
Get the id of the region for PV within this region_model, |
| 2584 |
emitting any diagnostics to CTXT. */ |
| 2585 |
|
| 2586 |
const region * |
| 2587 |
region_model::get_lvalue_1 (path_var pv, region_model_context *ctxt) const |
| 2588 |
{ |
| 2589 |
tree expr = pv.m_tree; |
| 2590 |
|
| 2591 |
gcc_assert (expr); |
| 2592 |
|
| 2593 |
switch (TREE_CODE (expr)) |
| 2594 |
{ |
| 2595 |
default: |
| 2596 |
return m_mgr->get_region_for_unexpected_tree_code (ctxt, expr, |
| 2597 |
dump_location_t ()); |
| 2598 |
|
| 2599 |
case ARRAY_REF: |
| 2600 |
{ |
| 2601 |
tree array = TREE_OPERAND (expr, 0); |
| 2602 |
tree index = TREE_OPERAND (expr, 1); |
| 2603 |
|
| 2604 |
const region *array_reg = get_lvalue (array, ctxt); |
| 2605 |
const svalue *index_sval = get_rvalue (index, ctxt); |
| 2606 |
return m_mgr->get_element_region (array_reg, |
| 2607 |
TREE_TYPE (TREE_TYPE (array)), |
| 2608 |
index_sval); |
| 2609 |
} |
| 2610 |
break; |
| 2611 |
|
| 2612 |
case BIT_FIELD_REF: |
| 2613 |
{ |
| 2614 |
tree inner_expr = TREE_OPERAND (expr, 0); |
| 2615 |
const region *inner_reg = get_lvalue (inner_expr, ctxt); |
| 2616 |
tree num_bits = TREE_OPERAND (expr, 1); |
| 2617 |
tree first_bit_offset = TREE_OPERAND (expr, 2); |
| 2618 |
gcc_assert (TREE_CODE (num_bits) == INTEGER_CST); |
| 2619 |
gcc_assert (TREE_CODE (first_bit_offset) == INTEGER_CST); |
| 2620 |
bit_range bits (TREE_INT_CST_LOW (first_bit_offset), |
| 2621 |
TREE_INT_CST_LOW (num_bits)); |
| 2622 |
return m_mgr->get_bit_range (inner_reg, TREE_TYPE (expr), bits); |
| 2623 |
} |
| 2624 |
break; |
| 2625 |
|
| 2626 |
case MEM_REF: |
| 2627 |
{ |
| 2628 |
tree ptr = TREE_OPERAND (expr, 0); |
| 2629 |
tree offset = TREE_OPERAND (expr, 1); |
| 2630 |
const svalue *ptr_sval = get_rvalue (ptr, ctxt); |
| 2631 |
const svalue *offset_sval = get_rvalue (offset, ctxt); |
| 2632 |
const region *star_ptr = deref_rvalue (ptr_sval, ptr, ctxt); |
| 2633 |
return m_mgr->get_offset_region (star_ptr, |
| 2634 |
TREE_TYPE (expr), |
| 2635 |
offset_sval); |
| 2636 |
} |
| 2637 |
break; |
| 2638 |
|
| 2639 |
case FUNCTION_DECL: |
| 2640 |
return m_mgr->get_region_for_fndecl (expr); |
| 2641 |
|
| 2642 |
case LABEL_DECL: |
| 2643 |
return m_mgr->get_region_for_label (expr); |
| 2644 |
|
| 2645 |
case VAR_DECL: |
| 2646 |
/* Handle globals. */ |
| 2647 |
if (is_global_var (expr)) |
| 2648 |
return m_mgr->get_region_for_global (expr); |
| 2649 |
|
| 2650 |
/* Fall through. */ |
| 2651 |
|
| 2652 |
case SSA_NAME: |
| 2653 |
case PARM_DECL: |
| 2654 |
case RESULT_DECL: |
| 2655 |
{ |
| 2656 |
gcc_assert (TREE_CODE (expr) == SSA_NAME |
| 2657 |
|| TREE_CODE (expr) == PARM_DECL |
| 2658 |
|| VAR_P (expr) |
| 2659 |
|| TREE_CODE (expr) == RESULT_DECL); |
| 2660 |
|
| 2661 |
int stack_index = pv.m_stack_depth; |
| 2662 |
const frame_region *frame = get_frame_at_index (stack_index); |
| 2663 |
gcc_assert (frame); |
| 2664 |
return frame->get_region_for_local (m_mgr, expr, ctxt); |
| 2665 |
} |
| 2666 |
|
| 2667 |
case COMPONENT_REF: |
| 2668 |
{ |
| 2669 |
/* obj.field */ |
| 2670 |
tree obj = TREE_OPERAND (expr, 0); |
| 2671 |
tree field = TREE_OPERAND (expr, 1); |
| 2672 |
const region *obj_reg = get_lvalue (obj, ctxt); |
| 2673 |
return m_mgr->get_field_region (obj_reg, field); |
| 2674 |
} |
| 2675 |
break; |
| 2676 |
|
| 2677 |
case STRING_CST: |
| 2678 |
return m_mgr->get_region_for_string (expr); |
| 2679 |
} |
| 2680 |
} |
| 2681 |
|
| 2682 |
/* Assert that SRC_TYPE can be converted to DST_TYPE as a no-op. */ |
| 2683 |
|
| 2684 |
static void |
| 2685 |
assert_compat_types (tree src_type, tree dst_type) |
| 2686 |
{ |
| 2687 |
if (src_type && dst_type && !VOID_TYPE_P (dst_type)) |
| 2688 |
{ |
| 2689 |
#if CHECKING_P |
| 2690 |
if (!(useless_type_conversion_p (src_type, dst_type))) |
| 2691 |
internal_error ("incompatible types: %qT and %qT", src_type, dst_type); |
| 2692 |
#endif |
| 2693 |
} |
| 2694 |
} |
| 2695 |
|
| 2696 |
/* Return true if SRC_TYPE can be converted to DST_TYPE as a no-op. */ |
| 2697 |
|
| 2698 |
bool |
| 2699 |
compat_types_p (tree src_type, tree dst_type) |
| 2700 |
{ |
| 2701 |
if (src_type && dst_type && !VOID_TYPE_P (dst_type)) |
| 2702 |
if (!(useless_type_conversion_p (src_type, dst_type))) |
| 2703 |
return false; |
| 2704 |
return true; |
| 2705 |
} |
| 2706 |
|
| 2707 |
/* Get the region for PV within this region_model, |
| 2708 |
emitting any diagnostics to CTXT. */ |
| 2709 |
|
| 2710 |
const region * |
| 2711 |
region_model::get_lvalue (path_var pv, region_model_context *ctxt) const |
| 2712 |
{ |
| 2713 |
if (pv.m_tree == NULL_TREE) |
| 2714 |
return NULL; |
| 2715 |
|
| 2716 |
const region *result_reg = get_lvalue_1 (pv, ctxt); |
| 2717 |
assert_compat_types (result_reg->get_type (), TREE_TYPE (pv.m_tree)); |
| 2718 |
return result_reg; |
| 2719 |
} |
| 2720 |
|
| 2721 |
/* Get the region for EXPR within this region_model (assuming the most |
| 2722 |
recent stack frame if it's a local). */ |
| 2723 |
|
| 2724 |
const region * |
| 2725 |
region_model::get_lvalue (tree expr, region_model_context *ctxt) const |
| 2726 |
{ |
| 2727 |
return get_lvalue (path_var (expr, get_stack_depth () - 1), ctxt); |
| 2728 |
} |
| 2729 |
|
| 2730 |
/* Implementation of region_model::get_rvalue; the latter adds type-checking. |
| 2731 |
|
| 2732 |
Get the value of PV within this region_model, |
| 2733 |
emitting any diagnostics to CTXT. */ |
| 2734 |
|
| 2735 |
const svalue * |
| 2736 |
region_model::get_rvalue_1 (path_var pv, region_model_context *ctxt) const |
| 2737 |
{ |
| 2738 |
gcc_assert (pv.m_tree); |
| 2739 |
|
| 2740 |
switch (TREE_CODE (pv.m_tree)) |
| 2741 |
{ |
| 2742 |
default: |
| 2743 |
return m_mgr->get_or_create_unknown_svalue (TREE_TYPE (pv.m_tree)); |
| 2744 |
|
| 2745 |
case ADDR_EXPR: |
| 2746 |
{ |
| 2747 |
/* "&EXPR". */ |
| 2748 |
tree expr = pv.m_tree; |
| 2749 |
tree op0 = TREE_OPERAND (expr, 0); |
| 2750 |
const region *expr_reg = get_lvalue (op0, ctxt); |
| 2751 |
return m_mgr->get_ptr_svalue (TREE_TYPE (expr), expr_reg); |
| 2752 |
} |
| 2753 |
break; |
| 2754 |
|
| 2755 |
case BIT_FIELD_REF: |
| 2756 |
{ |
| 2757 |
tree expr = pv.m_tree; |
| 2758 |
tree op0 = TREE_OPERAND (expr, 0); |
| 2759 |
const region *reg = get_lvalue (op0, ctxt); |
| 2760 |
tree num_bits = TREE_OPERAND (expr, 1); |
| 2761 |
tree first_bit_offset = TREE_OPERAND (expr, 2); |
| 2762 |
gcc_assert (TREE_CODE (num_bits) == INTEGER_CST); |
| 2763 |
gcc_assert (TREE_CODE (first_bit_offset) == INTEGER_CST); |
| 2764 |
bit_range bits (TREE_INT_CST_LOW (first_bit_offset), |
| 2765 |
TREE_INT_CST_LOW (num_bits)); |
| 2766 |
return get_rvalue_for_bits (TREE_TYPE (expr), reg, bits, ctxt); |
| 2767 |
} |
| 2768 |
|
| 2769 |
case VAR_DECL: |
| 2770 |
if (DECL_HARD_REGISTER (pv.m_tree)) |
| 2771 |
{ |
| 2772 |
/* If it has a hard register, it doesn't have a memory region |
| 2773 |
and can't be referred to as an lvalue. */ |
| 2774 |
return m_mgr->get_or_create_unknown_svalue (TREE_TYPE (pv.m_tree)); |
| 2775 |
} |
| 2776 |
/* Fall through. */ |
| 2777 |
case PARM_DECL: |
| 2778 |
case SSA_NAME: |
| 2779 |
case RESULT_DECL: |
| 2780 |
case ARRAY_REF: |
| 2781 |
{ |
| 2782 |
const region *reg = get_lvalue (pv, ctxt); |
| 2783 |
return get_store_value (reg, ctxt); |
| 2784 |
} |
| 2785 |
|
| 2786 |
case REALPART_EXPR: |
| 2787 |
case IMAGPART_EXPR: |
| 2788 |
case VIEW_CONVERT_EXPR: |
| 2789 |
{ |
| 2790 |
tree expr = pv.m_tree; |
| 2791 |
tree arg = TREE_OPERAND (expr, 0); |
| 2792 |
const svalue *arg_sval = get_rvalue (arg, ctxt); |
| 2793 |
const svalue *sval_unaryop |
| 2794 |
= m_mgr->get_or_create_unaryop (TREE_TYPE (expr), TREE_CODE (expr), |
| 2795 |
arg_sval); |
| 2796 |
return sval_unaryop; |
| 2797 |
}; |
| 2798 |
|
| 2799 |
case INTEGER_CST: |
| 2800 |
case REAL_CST: |
| 2801 |
case COMPLEX_CST: |
| 2802 |
case VECTOR_CST: |
| 2803 |
case STRING_CST: |
| 2804 |
case RAW_DATA_CST: |
| 2805 |
return m_mgr->get_or_create_constant_svalue (pv.m_tree); |
| 2806 |
|
| 2807 |
case POINTER_PLUS_EXPR: |
| 2808 |
{ |
| 2809 |
tree expr = pv.m_tree; |
| 2810 |
tree ptr = TREE_OPERAND (expr, 0); |
| 2811 |
tree offset = TREE_OPERAND (expr, 1); |
| 2812 |
const svalue *ptr_sval = get_rvalue (ptr, ctxt); |
| 2813 |
const svalue *offset_sval = get_rvalue (offset, ctxt); |
| 2814 |
const svalue *sval_binop |
| 2815 |
= m_mgr->get_or_create_binop (TREE_TYPE (expr), POINTER_PLUS_EXPR, |
| 2816 |
ptr_sval, offset_sval); |
| 2817 |
return sval_binop; |
| 2818 |
} |
| 2819 |
|
| 2820 |
/* Binary ops. */ |
| 2821 |
case PLUS_EXPR: |
| 2822 |
case MULT_EXPR: |
| 2823 |
case BIT_AND_EXPR: |
| 2824 |
case BIT_IOR_EXPR: |
| 2825 |
case BIT_XOR_EXPR: |
| 2826 |
{ |
| 2827 |
tree expr = pv.m_tree; |
| 2828 |
tree arg0 = TREE_OPERAND (expr, 0); |
| 2829 |
tree arg1 = TREE_OPERAND (expr, 1); |
| 2830 |
const svalue *arg0_sval = get_rvalue (arg0, ctxt); |
| 2831 |
const svalue *arg1_sval = get_rvalue (arg1, ctxt); |
| 2832 |
const svalue *sval_binop |
| 2833 |
= m_mgr->get_or_create_binop (TREE_TYPE (expr), TREE_CODE (expr), |
| 2834 |
arg0_sval, arg1_sval); |
| 2835 |
return sval_binop; |
| 2836 |
} |
| 2837 |
|
| 2838 |
case COMPONENT_REF: |
| 2839 |
case MEM_REF: |
| 2840 |
{ |
| 2841 |
const region *ref_reg = get_lvalue (pv, ctxt); |
| 2842 |
return get_store_value (ref_reg, ctxt); |
| 2843 |
} |
| 2844 |
case OBJ_TYPE_REF: |
| 2845 |
{ |
| 2846 |
tree expr = OBJ_TYPE_REF_EXPR (pv.m_tree); |
| 2847 |
return get_rvalue (expr, ctxt); |
| 2848 |
} |
| 2849 |
} |
| 2850 |
} |
| 2851 |
|
| 2852 |
/* Get the value of PV within this region_model, |
| 2853 |
emitting any diagnostics to CTXT. */ |
| 2854 |
|
| 2855 |
const svalue * |
| 2856 |
region_model::get_rvalue (path_var pv, region_model_context *ctxt) const |
| 2857 |
{ |
| 2858 |
if (pv.m_tree == NULL_TREE) |
| 2859 |
return NULL; |
| 2860 |
|
| 2861 |
const svalue *result_sval = get_rvalue_1 (pv, ctxt); |
| 2862 |
|
| 2863 |
assert_compat_types (result_sval->get_type (), TREE_TYPE (pv.m_tree)); |
| 2864 |
|
| 2865 |
result_sval = check_for_poison (result_sval, pv.m_tree, NULL, ctxt); |
| 2866 |
|
| 2867 |
return result_sval; |
| 2868 |
} |
| 2869 |
|
| 2870 |
/* Get the value of EXPR within this region_model (assuming the most |
| 2871 |
recent stack frame if it's a local). */ |
| 2872 |
|
| 2873 |
const svalue * |
| 2874 |
region_model::get_rvalue (tree expr, region_model_context *ctxt) const |
| 2875 |
{ |
| 2876 |
return get_rvalue (path_var (expr, get_stack_depth () - 1), ctxt); |
| 2877 |
} |
| 2878 |
|
| 2879 |
/* Return true if this model is on a path with "main" as the entrypoint |
| 2880 |
(as opposed to one in which we're merely analyzing a subset of the |
| 2881 |
path through the code). */ |
| 2882 |
|
| 2883 |
bool |
| 2884 |
region_model::called_from_main_p () const |
| 2885 |
{ |
| 2886 |
if (!m_current_frame) |
| 2887 |
return false; |
| 2888 |
/* Determine if the oldest stack frame in this model is for "main". */ |
| 2889 |
const frame_region *frame0 = get_frame_at_index (0); |
| 2890 |
gcc_assert (frame0); |
| 2891 |
return id_equal (DECL_NAME (frame0->get_function ().decl), "main"); |
| 2892 |
} |
| 2893 |
|
| 2894 |
/* Subroutine of region_model::get_store_value for when REG is (or is within) |
| 2895 |
a global variable that hasn't been touched since the start of this path |
| 2896 |
(or was implicitly touched due to a call to an unknown function). */ |
| 2897 |
|
| 2898 |
const svalue * |
| 2899 |
region_model::get_initial_value_for_global (const region *reg) const |
| 2900 |
{ |
| 2901 |
/* Get the decl that REG is for (or is within). */ |
| 2902 |
const decl_region *base_reg |
| 2903 |
= reg->get_base_region ()->dyn_cast_decl_region (); |
| 2904 |
gcc_assert (base_reg); |
| 2905 |
tree decl = base_reg->get_decl (); |
| 2906 |
|
| 2907 |
/* Special-case: to avoid having to explicitly update all previously |
| 2908 |
untracked globals when calling an unknown fn, they implicitly have |
| 2909 |
an unknown value if an unknown call has occurred, unless this is |
| 2910 |
static to-this-TU and hasn't escaped. Globals that have escaped |
| 2911 |
are explicitly tracked, so we shouldn't hit this case for them. */ |
| 2912 |
if (m_store.called_unknown_fn_p () |
| 2913 |
&& TREE_PUBLIC (decl) |
| 2914 |
&& !TREE_READONLY (decl)) |
| 2915 |
return m_mgr->get_or_create_unknown_svalue (reg->get_type ()); |
| 2916 |
|
| 2917 |
/* If we are on a path from the entrypoint from "main" and we have a |
| 2918 |
global decl defined in this TU that hasn't been touched yet, then |
| 2919 |
the initial value of REG can be taken from the initialization value |
| 2920 |
of the decl. */ |
| 2921 |
if (called_from_main_p () || TREE_READONLY (decl)) |
| 2922 |
return reg->get_initial_value_at_main (m_mgr); |
| 2923 |
|
| 2924 |
/* Otherwise, return INIT_VAL(REG). */ |
| 2925 |
return m_mgr->get_or_create_initial_value (reg); |
| 2926 |
} |
| 2927 |
|
| 2928 |
/* Get a value for REG, looking it up in the store, or otherwise falling |
| 2929 |
back to "initial" or "unknown" values. |
| 2930 |
Use CTXT to report any warnings associated with reading from REG. */ |
| 2931 |
|
| 2932 |
const svalue * |
| 2933 |
region_model::get_store_value (const region *reg, |
| 2934 |
region_model_context *ctxt) const |
| 2935 |
{ |
| 2936 |
/* Getting the value of an empty region gives an unknown_svalue. */ |
| 2937 |
if (reg->empty_p ()) |
| 2938 |
return m_mgr->get_or_create_unknown_svalue (reg->get_type ()); |
| 2939 |
|
| 2940 |
bool check_poisoned = true; |
| 2941 |
if (check_region_for_read (reg, ctxt)) |
| 2942 |
check_poisoned = false; |
| 2943 |
|
| 2944 |
/* Special-case: handle var_decls in the constant pool. */ |
| 2945 |
if (const decl_region *decl_reg = reg->dyn_cast_decl_region ()) |
| 2946 |
if (const svalue *sval = decl_reg->maybe_get_constant_value (m_mgr)) |
| 2947 |
return sval; |
| 2948 |
|
| 2949 |
const svalue *sval |
| 2950 |
= m_store.get_any_binding (m_mgr->get_store_manager (), reg); |
| 2951 |
if (sval) |
| 2952 |
{ |
| 2953 |
if (reg->get_type ()) |
| 2954 |
sval = m_mgr->get_or_create_cast (reg->get_type (), sval); |
| 2955 |
return sval; |
| 2956 |
} |
| 2957 |
|
| 2958 |
/* Special-case: read at a constant index within a STRING_CST. */ |
| 2959 |
if (const offset_region *offset_reg = reg->dyn_cast_offset_region ()) |
| 2960 |
if (tree byte_offset_cst |
| 2961 |
= offset_reg->get_byte_offset ()->maybe_get_constant ()) |
| 2962 |
if (const string_region *str_reg |
| 2963 |
= reg->get_parent_region ()->dyn_cast_string_region ()) |
| 2964 |
{ |
| 2965 |
tree string_cst = str_reg->get_string_cst (); |
| 2966 |
if (const svalue *char_sval |
| 2967 |
= m_mgr->maybe_get_char_from_string_cst (string_cst, |
| 2968 |
byte_offset_cst)) |
| 2969 |
return m_mgr->get_or_create_cast (reg->get_type (), char_sval); |
| 2970 |
} |
| 2971 |
|
| 2972 |
/* Special-case: read the initial char of a STRING_CST. */ |
| 2973 |
if (const cast_region *cast_reg = reg->dyn_cast_cast_region ()) |
| 2974 |
if (const string_region *str_reg |
| 2975 |
= cast_reg->get_parent_region ()->dyn_cast_string_region ()) |
| 2976 |
{ |
| 2977 |
tree string_cst = str_reg->get_string_cst (); |
| 2978 |
tree byte_offset_cst = integer_zero_node; |
| 2979 |
if (const svalue *char_sval |
| 2980 |
= m_mgr->maybe_get_char_from_string_cst (string_cst, |
| 2981 |
byte_offset_cst)) |
| 2982 |
return m_mgr->get_or_create_cast (reg->get_type (), char_sval); |
| 2983 |
} |
| 2984 |
|
| 2985 |
/* Otherwise we implicitly have the initial value of the region |
| 2986 |
(if the cluster had been touched, binding_cluster::get_any_binding, |
| 2987 |
would have returned UNKNOWN, and we would already have returned |
| 2988 |
that above). */ |
| 2989 |
|
| 2990 |
/* Handle globals. */ |
| 2991 |
if (reg->get_base_region ()->get_parent_region ()->get_kind () |
| 2992 |
== RK_GLOBALS) |
| 2993 |
return get_initial_value_for_global (reg); |
| 2994 |
|
| 2995 |
return m_mgr->get_or_create_initial_value (reg, check_poisoned); |
| 2996 |
} |
| 2997 |
|
| 2998 |
/* Return false if REG does not exist, true if it may do. |
| 2999 |
This is for detecting regions within the stack that don't exist anymore |
| 3000 |
after frames are popped. */ |
| 3001 |
|
| 3002 |
bool |
| 3003 |
region_model::region_exists_p (const region *reg) const |
| 3004 |
{ |
| 3005 |
/* If within a stack frame, check that the stack frame is live. */ |
| 3006 |
if (const frame_region *enclosing_frame = reg->maybe_get_frame_region ()) |
| 3007 |
{ |
| 3008 |
/* Check that the current frame is the enclosing frame, or is called |
| 3009 |
by it. */ |
| 3010 |
for (const frame_region *iter_frame = get_current_frame (); iter_frame; |
| 3011 |
iter_frame = iter_frame->get_calling_frame ()) |
| 3012 |
if (iter_frame == enclosing_frame) |
| 3013 |
return true; |
| 3014 |
return false; |
| 3015 |
} |
| 3016 |
|
| 3017 |
return true; |
| 3018 |
} |
| 3019 |
|
| 3020 |
/* Get a region for referencing PTR_SVAL, creating a region if need be, and |
| 3021 |
potentially generating warnings via CTXT. |
| 3022 |
PTR_SVAL must be of pointer type. |
| 3023 |
PTR_TREE if non-NULL can be used when emitting diagnostics. */ |
| 3024 |
|
| 3025 |
const region * |
| 3026 |
region_model::deref_rvalue (const svalue *ptr_sval, tree ptr_tree, |
| 3027 |
region_model_context *ctxt, |
| 3028 |
bool add_nonnull_constraint) const |
| 3029 |
{ |
| 3030 |
gcc_assert (ptr_sval); |
| 3031 |
gcc_assert (POINTER_TYPE_P (ptr_sval->get_type ())); |
| 3032 |
|
| 3033 |
/* If we're dereferencing PTR_SVAL, assume that it is non-NULL; add this |
| 3034 |
as a constraint. This suppresses false positives from |
| 3035 |
-Wanalyzer-null-dereference for the case where we later have an |
| 3036 |
if (PTR_SVAL) that would occur if we considered the false branch |
| 3037 |
and transitioned the malloc state machine from start->null. */ |
| 3038 |
if (add_nonnull_constraint) |
| 3039 |
{ |
| 3040 |
tree null_ptr_cst = build_int_cst (ptr_sval->get_type (), 0); |
| 3041 |
const svalue *null_ptr |
| 3042 |
= m_mgr->get_or_create_constant_svalue (null_ptr_cst); |
| 3043 |
m_constraints->add_constraint (ptr_sval, NE_EXPR, null_ptr); |
| 3044 |
} |
| 3045 |
|
| 3046 |
switch (ptr_sval->get_kind ()) |
| 3047 |
{ |
| 3048 |
default: |
| 3049 |
break; |
| 3050 |
|
| 3051 |
case SK_REGION: |
| 3052 |
{ |
| 3053 |
const region_svalue *region_sval |
| 3054 |
= as_a <const region_svalue *> (ptr_sval); |
| 3055 |
return region_sval->get_pointee (); |
| 3056 |
} |
| 3057 |
|
| 3058 |
case SK_BINOP: |
| 3059 |
{ |
| 3060 |
const binop_svalue *binop_sval |
| 3061 |
= as_a <const binop_svalue *> (ptr_sval); |
| 3062 |
switch (binop_sval->get_op ()) |
| 3063 |
{ |
| 3064 |
case POINTER_PLUS_EXPR: |
| 3065 |
{ |
| 3066 |
/* If we have a symbolic value expressing pointer arithmentic, |
| 3067 |
try to convert it to a suitable region. */ |
| 3068 |
const region *parent_region |
| 3069 |
= deref_rvalue (binop_sval->get_arg0 (), NULL_TREE, ctxt); |
| 3070 |
const svalue *offset = binop_sval->get_arg1 (); |
| 3071 |
tree type= TREE_TYPE (ptr_sval->get_type ()); |
| 3072 |
return m_mgr->get_offset_region (parent_region, type, offset); |
| 3073 |
} |
| 3074 |
default: |
| 3075 |
break; |
| 3076 |
} |
| 3077 |
} |
| 3078 |
break; |
| 3079 |
|
| 3080 |
case SK_POISONED: |
| 3081 |
{ |
| 3082 |
if (ctxt) |
| 3083 |
{ |
| 3084 |
tree ptr = get_representative_tree (ptr_sval); |
| 3085 |
/* If we can't get a representative tree for PTR_SVAL |
| 3086 |
(e.g. if it hasn't been bound into the store), then |
| 3087 |
fall back on PTR_TREE, if non-NULL. */ |
| 3088 |
if (!ptr) |
| 3089 |
ptr = ptr_tree; |
| 3090 |
if (ptr) |
| 3091 |
{ |
| 3092 |
const poisoned_svalue *poisoned_sval |
| 3093 |
= as_a <const poisoned_svalue *> (ptr_sval); |
| 3094 |
enum poison_kind pkind = poisoned_sval->get_poison_kind (); |
| 3095 |
ctxt->warn (::make_unique<poisoned_value_diagnostic> |
| 3096 |
(ptr, pkind, nullptr, nullptr)); |
| 3097 |
} |
| 3098 |
} |
| 3099 |
} |
| 3100 |
break; |
| 3101 |
} |
| 3102 |
|
| 3103 |
return m_mgr->get_symbolic_region (ptr_sval); |
| 3104 |
} |
| 3105 |
|
| 3106 |
/* Attempt to get BITS within any value of REG, as TYPE. |
| 3107 |
In particular, extract values from compound_svalues for the case |
| 3108 |
where there's a concrete binding at BITS. |
| 3109 |
Return an unknown svalue if we can't handle the given case. |
| 3110 |
Use CTXT to report any warnings associated with reading from REG. */ |
| 3111 |
|
| 3112 |
const svalue * |
| 3113 |
region_model::get_rvalue_for_bits (tree type, |
| 3114 |
const region *reg, |
| 3115 |
const bit_range &bits, |
| 3116 |
region_model_context *ctxt) const |
| 3117 |
{ |
| 3118 |
const svalue *sval = get_store_value (reg, ctxt); |
| 3119 |
return m_mgr->get_or_create_bits_within (type, bits, sval); |
| 3120 |
} |
| 3121 |
|
| 3122 |
/* A subclass of pending_diagnostic for complaining about writes to |
| 3123 |
constant regions of memory. */ |
| 3124 |
|
| 3125 |
class write_to_const_diagnostic |
| 3126 |
: public pending_diagnostic_subclass<write_to_const_diagnostic> |
| 3127 |
{ |
| 3128 |
public: |
| 3129 |
write_to_const_diagnostic (const region *reg, tree decl) |
| 3130 |
: m_reg (reg), m_decl (decl) |
| 3131 |
{} |
| 3132 |
|
| 3133 |
const char *get_kind () const final override |
| 3134 |
{ |
| 3135 |
return "write_to_const_diagnostic"; |
| 3136 |
} |
| 3137 |
|
| 3138 |
bool operator== (const write_to_const_diagnostic &other) const |
| 3139 |
{ |
| 3140 |
return (m_reg == other.m_reg |
| 3141 |
&& m_decl == other.m_decl); |
| 3142 |
} |
| 3143 |
|
| 3144 |
int get_controlling_option () const final override |
| 3145 |
{ |
| 3146 |
return OPT_Wanalyzer_write_to_const; |
| 3147 |
} |
| 3148 |
|
| 3149 |
bool emit (diagnostic_emission_context &ctxt) final override |
| 3150 |
{ |
| 3151 |
auto_diagnostic_group d; |
| 3152 |
bool warned; |
| 3153 |
switch (m_reg->get_kind ()) |
| 3154 |
{ |
| 3155 |
default: |
| 3156 |
warned = ctxt.warn ("write to %<const%> object %qE", m_decl); |
| 3157 |
break; |
| 3158 |
case RK_FUNCTION: |
| 3159 |
warned = ctxt.warn ("write to function %qE", m_decl); |
| 3160 |
break; |
| 3161 |
case RK_LABEL: |
| 3162 |
warned = ctxt.warn ("write to label %qE", m_decl); |
| 3163 |
break; |
| 3164 |
} |
| 3165 |
if (warned) |
| 3166 |
inform (DECL_SOURCE_LOCATION (m_decl), "declared here"); |
| 3167 |
return warned; |
| 3168 |
} |
| 3169 |
|
| 3170 |
bool |
| 3171 |
describe_final_event (pretty_printer &pp, |
| 3172 |
const evdesc::final_event &) final override |
| 3173 |
{ |
| 3174 |
switch (m_reg->get_kind ()) |
| 3175 |
{ |
| 3176 |
default: |
| 3177 |
{ |
| 3178 |
pp_printf (&pp, |
| 3179 |
"write to %<const%> object %qE here", m_decl); |
| 3180 |
return true; |
| 3181 |
} |
| 3182 |
case RK_FUNCTION: |
| 3183 |
{ |
| 3184 |
pp_printf (&pp, |
| 3185 |
"write to function %qE here", m_decl); |
| 3186 |
return true; |
| 3187 |
} |
| 3188 |
case RK_LABEL: |
| 3189 |
{ |
| 3190 |
pp_printf (&pp, |
| 3191 |
"write to label %qE here", m_decl); |
| 3192 |
return true; |
| 3193 |
} |
| 3194 |
} |
| 3195 |
} |
| 3196 |
|
| 3197 |
private: |
| 3198 |
const region *m_reg; |
| 3199 |
tree m_decl; |
| 3200 |
}; |
| 3201 |
|
| 3202 |
/* A subclass of pending_diagnostic for complaining about writes to |
| 3203 |
string literals. */ |
| 3204 |
|
| 3205 |
class write_to_string_literal_diagnostic |
| 3206 |
: public pending_diagnostic_subclass<write_to_string_literal_diagnostic> |
| 3207 |
{ |
| 3208 |
public: |
| 3209 |
write_to_string_literal_diagnostic (const region *reg) |
| 3210 |
: m_reg (reg) |
| 3211 |
{} |
| 3212 |
|
| 3213 |
const char *get_kind () const final override |
| 3214 |
{ |
| 3215 |
return "write_to_string_literal_diagnostic"; |
| 3216 |
} |
| 3217 |
|
| 3218 |
bool operator== (const write_to_string_literal_diagnostic &other) const |
| 3219 |
{ |
| 3220 |
return m_reg == other.m_reg; |
| 3221 |
} |
| 3222 |
|
| 3223 |
int get_controlling_option () const final override |
| 3224 |
{ |
| 3225 |
return OPT_Wanalyzer_write_to_string_literal; |
| 3226 |
} |
| 3227 |
|
| 3228 |
bool emit (diagnostic_emission_context &ctxt) final override |
| 3229 |
{ |
| 3230 |
return ctxt.warn ("write to string literal"); |
| 3231 |
/* Ideally we would show the location of the STRING_CST as well, |
| 3232 |
but it is not available at this point. */ |
| 3233 |
} |
| 3234 |
|
| 3235 |
bool |
| 3236 |
describe_final_event (pretty_printer &pp, |
| 3237 |
const evdesc::final_event &) final override |
| 3238 |
{ |
| 3239 |
pp_string (&pp, "write to string literal here"); |
| 3240 |
return true; |
| 3241 |
} |
| 3242 |
|
| 3243 |
private: |
| 3244 |
const region *m_reg; |
| 3245 |
}; |
| 3246 |
|
| 3247 |
/* Use CTXT to warn If DEST_REG is a region that shouldn't be written to. */ |
| 3248 |
|
| 3249 |
void |
| 3250 |
region_model::check_for_writable_region (const region* dest_reg, |
| 3251 |
region_model_context *ctxt) const |
| 3252 |
{ |
| 3253 |
/* Fail gracefully if CTXT is NULL. */ |
| 3254 |
if (!ctxt) |
| 3255 |
return; |
| 3256 |
|
| 3257 |
const region *base_reg = dest_reg->get_base_region (); |
| 3258 |
switch (base_reg->get_kind ()) |
| 3259 |
{ |
| 3260 |
default: |
| 3261 |
break; |
| 3262 |
case RK_FUNCTION: |
| 3263 |
{ |
| 3264 |
const function_region *func_reg = as_a <const function_region *> (base_reg); |
| 3265 |
tree fndecl = func_reg->get_fndecl (); |
| 3266 |
ctxt->warn (make_unique<write_to_const_diagnostic> |
| 3267 |
(func_reg, fndecl)); |
| 3268 |
} |
| 3269 |
break; |
| 3270 |
case RK_LABEL: |
| 3271 |
{ |
| 3272 |
const label_region *label_reg = as_a <const label_region *> (base_reg); |
| 3273 |
tree label = label_reg->get_label (); |
| 3274 |
ctxt->warn (make_unique<write_to_const_diagnostic> |
| 3275 |
(label_reg, label)); |
| 3276 |
} |
| 3277 |
break; |
| 3278 |
case RK_DECL: |
| 3279 |
{ |
| 3280 |
const decl_region *decl_reg = as_a <const decl_region *> (base_reg); |
| 3281 |
tree decl = decl_reg->get_decl (); |
| 3282 |
/* Warn about writes to const globals. |
| 3283 |
Don't warn for writes to const locals, and params in particular, |
| 3284 |
since we would warn in push_frame when setting them up (e.g the |
| 3285 |
"this" param is "T* const"). */ |
| 3286 |
if (TREE_READONLY (decl) |
| 3287 |
&& is_global_var (decl)) |
| 3288 |
ctxt->warn (make_unique<write_to_const_diagnostic> (dest_reg, decl)); |
| 3289 |
} |
| 3290 |
break; |
| 3291 |
case RK_STRING: |
| 3292 |
ctxt->warn (make_unique<write_to_string_literal_diagnostic> (dest_reg)); |
| 3293 |
break; |
| 3294 |
} |
| 3295 |
} |
| 3296 |
|
| 3297 |
/* Get the capacity of REG in bytes. */ |
| 3298 |
|
| 3299 |
const svalue * |
| 3300 |
region_model::get_capacity (const region *reg) const |
| 3301 |
{ |
| 3302 |
switch (reg->get_kind ()) |
| 3303 |
{ |
| 3304 |
default: |
| 3305 |
break; |
| 3306 |
case RK_DECL: |
| 3307 |
{ |
| 3308 |
const decl_region *decl_reg = as_a <const decl_region *> (reg); |
| 3309 |
tree decl = decl_reg->get_decl (); |
| 3310 |
if (TREE_CODE (decl) == SSA_NAME) |
| 3311 |
{ |
| 3312 |
tree type = TREE_TYPE (decl); |
| 3313 |
tree size = TYPE_SIZE (type); |
| 3314 |
return get_rvalue (size, NULL); |
| 3315 |
} |
| 3316 |
else |
| 3317 |
{ |
| 3318 |
tree size = decl_init_size (decl, false); |
| 3319 |
if (size) |
| 3320 |
return get_rvalue (size, NULL); |
| 3321 |
} |
| 3322 |
} |
| 3323 |
break; |
| 3324 |
case RK_SIZED: |
| 3325 |
/* Look through sized regions to get at the capacity |
| 3326 |
of the underlying regions. */ |
| 3327 |
return get_capacity (reg->get_parent_region ()); |
| 3328 |
case RK_STRING: |
| 3329 |
{ |
| 3330 |
/* "Capacity" here means "size". */ |
| 3331 |
const string_region *string_reg = as_a <const string_region *> (reg); |
| 3332 |
tree string_cst = string_reg->get_string_cst (); |
| 3333 |
return m_mgr->get_or_create_int_cst (size_type_node, |
| 3334 |
TREE_STRING_LENGTH (string_cst)); |
| 3335 |
} |
| 3336 |
break; |
| 3337 |
} |
| 3338 |
|
| 3339 |
if (const svalue *recorded = get_dynamic_extents (reg)) |
| 3340 |
return recorded; |
| 3341 |
|
| 3342 |
return m_mgr->get_or_create_unknown_svalue (sizetype); |
| 3343 |
} |
| 3344 |
|
| 3345 |
/* If CTXT is non-NULL, use it to warn about any problems accessing REG, |
| 3346 |
using DIR to determine if this access is a read or write. |
| 3347 |
Return TRUE if an OOB access was detected. |
| 3348 |
If SVAL_HINT is non-NULL, use it as a hint in diagnostics |
| 3349 |
about the value that would be written to REG. */ |
| 3350 |
|
| 3351 |
bool |
| 3352 |
region_model::check_region_access (const region *reg, |
| 3353 |
enum access_direction dir, |
| 3354 |
const svalue *sval_hint, |
| 3355 |
region_model_context *ctxt) const |
| 3356 |
{ |
| 3357 |
/* Fail gracefully if CTXT is NULL. */ |
| 3358 |
if (!ctxt) |
| 3359 |
return false; |
| 3360 |
|
| 3361 |
bool oob_access_detected = false; |
| 3362 |
check_region_for_taint (reg, dir, ctxt); |
| 3363 |
if (!check_region_bounds (reg, dir, sval_hint, ctxt)) |
| 3364 |
oob_access_detected = true; |
| 3365 |
|
| 3366 |
switch (dir) |
| 3367 |
{ |
| 3368 |
default: |
| 3369 |
gcc_unreachable (); |
| 3370 |
case DIR_READ: |
| 3371 |
/* Currently a no-op. */ |
| 3372 |
break; |
| 3373 |
case DIR_WRITE: |
| 3374 |
check_for_writable_region (reg, ctxt); |
| 3375 |
break; |
| 3376 |
} |
| 3377 |
return oob_access_detected; |
| 3378 |
} |
| 3379 |
|
| 3380 |
/* If CTXT is non-NULL, use it to warn about any problems writing to REG. */ |
| 3381 |
|
| 3382 |
void |
| 3383 |
region_model::check_region_for_write (const region *dest_reg, |
| 3384 |
const svalue *sval_hint, |
| 3385 |
region_model_context *ctxt) const |
| 3386 |
{ |
| 3387 |
check_region_access (dest_reg, DIR_WRITE, sval_hint, ctxt); |
| 3388 |
} |
| 3389 |
|
| 3390 |
/* If CTXT is non-NULL, use it to warn about any problems reading from REG. |
| 3391 |
Returns TRUE if an OOB read was detected. */ |
| 3392 |
|
| 3393 |
bool |
| 3394 |
region_model::check_region_for_read (const region *src_reg, |
| 3395 |
region_model_context *ctxt) const |
| 3396 |
{ |
| 3397 |
return check_region_access (src_reg, DIR_READ, NULL, ctxt); |
| 3398 |
} |
| 3399 |
|
| 3400 |
/* Concrete subclass for casts of pointers that lead to trailing bytes. */ |
| 3401 |
|
| 3402 |
class dubious_allocation_size |
| 3403 |
: public pending_diagnostic_subclass<dubious_allocation_size> |
| 3404 |
{ |
| 3405 |
public: |
| 3406 |
dubious_allocation_size (const region *lhs, const region *rhs, |
| 3407 |
const svalue *capacity_sval, tree expr, |
| 3408 |
const gimple *stmt) |
| 3409 |
: m_lhs (lhs), m_rhs (rhs), |
| 3410 |
m_capacity_sval (capacity_sval), m_expr (expr), |
| 3411 |
m_stmt (stmt), |
| 3412 |
m_has_allocation_event (false) |
| 3413 |
{ |
| 3414 |
gcc_assert (m_capacity_sval); |
| 3415 |
} |
| 3416 |
|
| 3417 |
const char *get_kind () const final override |
| 3418 |
{ |
| 3419 |
return "dubious_allocation_size"; |
| 3420 |
} |
| 3421 |
|
| 3422 |
bool operator== (const dubious_allocation_size &other) const |
| 3423 |
{ |
| 3424 |
return (m_stmt == other.m_stmt |
| 3425 |
&& pending_diagnostic::same_tree_p (m_expr, other.m_expr)); |
| 3426 |
} |
| 3427 |
|
| 3428 |
int get_controlling_option () const final override |
| 3429 |
{ |
| 3430 |
return OPT_Wanalyzer_allocation_size; |
| 3431 |
} |
| 3432 |
|
| 3433 |
bool emit (diagnostic_emission_context &ctxt) final override |
| 3434 |
{ |
| 3435 |
ctxt.add_cwe (131); |
| 3436 |
|
| 3437 |
return ctxt.warn ("allocated buffer size is not a multiple" |
| 3438 |
" of the pointee's size"); |
| 3439 |
} |
| 3440 |
|
| 3441 |
bool |
| 3442 |
describe_final_event (pretty_printer &pp, |
| 3443 |
const evdesc::final_event &) final override |
| 3444 |
{ |
| 3445 |
tree pointee_type = TREE_TYPE (m_lhs->get_type ()); |
| 3446 |
if (m_has_allocation_event) |
| 3447 |
{ |
| 3448 |
pp_printf (&pp, |
| 3449 |
"assigned to %qT here;" |
| 3450 |
" %<sizeof (%T)%> is %qE", |
| 3451 |
m_lhs->get_type (), pointee_type, |
| 3452 |
size_in_bytes (pointee_type)); |
| 3453 |
return true; |
| 3454 |
} |
| 3455 |
/* Fallback: Typically, we should always see an allocation_event |
| 3456 |
before. */ |
| 3457 |
if (m_expr) |
| 3458 |
{ |
| 3459 |
if (TREE_CODE (m_expr) == INTEGER_CST) |
| 3460 |
{ |
| 3461 |
pp_printf (&pp, |
| 3462 |
"allocated %E bytes and assigned to" |
| 3463 |
" %qT here; %<sizeof (%T)%> is %qE", |
| 3464 |
m_expr, m_lhs->get_type (), pointee_type, |
| 3465 |
size_in_bytes (pointee_type)); |
| 3466 |
return true; |
| 3467 |
} |
| 3468 |
else |
| 3469 |
{ |
| 3470 |
pp_printf (&pp, |
| 3471 |
"allocated %qE bytes and assigned to" |
| 3472 |
" %qT here; %<sizeof (%T)%> is %qE", |
| 3473 |
m_expr, m_lhs->get_type (), pointee_type, |
| 3474 |
size_in_bytes (pointee_type)); |
| 3475 |
return true; |
| 3476 |
} |
| 3477 |
} |
| 3478 |
|
| 3479 |
pp_printf (&pp, |
| 3480 |
"allocated and assigned to %qT here;" |
| 3481 |
" %<sizeof (%T)%> is %qE", |
| 3482 |
m_lhs->get_type (), pointee_type, |
| 3483 |
size_in_bytes (pointee_type)); |
| 3484 |
return true; |
| 3485 |
} |
| 3486 |
|
| 3487 |
void |
| 3488 |
add_region_creation_events (const region *, |
| 3489 |
tree capacity, |
| 3490 |
const event_loc_info &loc_info, |
| 3491 |
checker_path &emission_path) final override |
| 3492 |
{ |
| 3493 |
emission_path.add_event |
| 3494 |
(make_unique<region_creation_event_allocation_size> (capacity, loc_info)); |
| 3495 |
|
| 3496 |
m_has_allocation_event = true; |
| 3497 |
} |
| 3498 |
|
| 3499 |
void mark_interesting_stuff (interesting_t *interest) final override |
| 3500 |
{ |
| 3501 |
interest->add_region_creation (m_rhs); |
| 3502 |
} |
| 3503 |
|
| 3504 |
void maybe_add_sarif_properties (sarif_object &result_obj) |
| 3505 |
const final override |
| 3506 |
{ |
| 3507 |
sarif_property_bag &props = result_obj.get_or_create_properties (); |
| 3508 |
#define PROPERTY_PREFIX "gcc/analyzer/dubious_allocation_size/" |
| 3509 |
props.set (PROPERTY_PREFIX "lhs", m_lhs->to_json ()); |
| 3510 |
props.set (PROPERTY_PREFIX "rhs", m_rhs->to_json ()); |
| 3511 |
props.set (PROPERTY_PREFIX "capacity_sval", m_capacity_sval->to_json ()); |
| 3512 |
#undef PROPERTY_PREFIX |
| 3513 |
} |
| 3514 |
|
| 3515 |
private: |
| 3516 |
const region *m_lhs; |
| 3517 |
const region *m_rhs; |
| 3518 |
const svalue *m_capacity_sval; |
| 3519 |
const tree m_expr; |
| 3520 |
const gimple *m_stmt; |
| 3521 |
bool m_has_allocation_event; |
| 3522 |
}; |
| 3523 |
|
| 3524 |
/* Return true on dubious allocation sizes for constant sizes. */ |
| 3525 |
|
| 3526 |
static bool |
| 3527 |
capacity_compatible_with_type (tree cst, tree pointee_size_tree, |
| 3528 |
bool is_struct) |
| 3529 |
{ |
| 3530 |
gcc_assert (TREE_CODE (cst) == INTEGER_CST); |
| 3531 |
gcc_assert (TREE_CODE (pointee_size_tree) == INTEGER_CST); |
| 3532 |
|
| 3533 |
unsigned HOST_WIDE_INT pointee_size = TREE_INT_CST_LOW (pointee_size_tree); |
| 3534 |
unsigned HOST_WIDE_INT alloc_size = TREE_INT_CST_LOW (cst); |
| 3535 |
|
| 3536 |
if (is_struct) |
| 3537 |
return alloc_size == 0 || alloc_size >= pointee_size; |
| 3538 |
return alloc_size % pointee_size == 0; |
| 3539 |
} |
| 3540 |
|
| 3541 |
static bool |
| 3542 |
capacity_compatible_with_type (tree cst, tree pointee_size_tree) |
| 3543 |
{ |
| 3544 |
return capacity_compatible_with_type (cst, pointee_size_tree, false); |
| 3545 |
} |
| 3546 |
|
| 3547 |
/* Checks whether SVAL could be a multiple of SIZE_CST. |
| 3548 |
|
| 3549 |
It works by visiting all svalues inside SVAL until it reaches |
| 3550 |
atomic nodes. From those, it goes back up again and adds each |
| 3551 |
node that is not a multiple of SIZE_CST to the RESULT_SET. */ |
| 3552 |
|
| 3553 |
class size_visitor : public visitor |
| 3554 |
{ |
| 3555 |
public: |
| 3556 |
size_visitor (tree size_cst, const svalue *root_sval, constraint_manager *cm) |
| 3557 |
: m_size_cst (size_cst), m_root_sval (root_sval), m_cm (cm) |
| 3558 |
{ |
| 3559 |
m_root_sval->accept (this); |
| 3560 |
} |
| 3561 |
|
| 3562 |
bool is_dubious_capacity () |
| 3563 |
{ |
| 3564 |
return result_set.contains (m_root_sval); |
| 3565 |
} |
| 3566 |
|
| 3567 |
void visit_constant_svalue (const constant_svalue *sval) final override |
| 3568 |
{ |
| 3569 |
check_constant (sval->get_constant (), sval); |
| 3570 |
} |
| 3571 |
|
| 3572 |
void visit_unaryop_svalue (const unaryop_svalue *sval) final override |
| 3573 |
{ |
| 3574 |
if (CONVERT_EXPR_CODE_P (sval->get_op ()) |
| 3575 |
&& result_set.contains (sval->get_arg ())) |
| 3576 |
result_set.add (sval); |
| 3577 |
} |
| 3578 |
|
| 3579 |
void visit_binop_svalue (const binop_svalue *sval) final override |
| 3580 |
{ |
| 3581 |
const svalue *arg0 = sval->get_arg0 (); |
| 3582 |
const svalue *arg1 = sval->get_arg1 (); |
| 3583 |
|
| 3584 |
switch (sval->get_op ()) |
| 3585 |
{ |
| 3586 |
case MULT_EXPR: |
| 3587 |
if (result_set.contains (arg0) && result_set.contains (arg1)) |
| 3588 |
result_set.add (sval); |
| 3589 |
break; |
| 3590 |
case PLUS_EXPR: |
| 3591 |
case MINUS_EXPR: |
| 3592 |
if (result_set.contains (arg0) || result_set.contains (arg1)) |
| 3593 |
result_set.add (sval); |
| 3594 |
break; |
| 3595 |
default: |
| 3596 |
break; |
| 3597 |
} |
| 3598 |
} |
| 3599 |
|
| 3600 |
void visit_unmergeable_svalue (const unmergeable_svalue *sval) final override |
| 3601 |
{ |
| 3602 |
if (result_set.contains (sval->get_arg ())) |
| 3603 |
result_set.add (sval); |
| 3604 |
} |
| 3605 |
|
| 3606 |
void visit_widening_svalue (const widening_svalue *sval) final override |
| 3607 |
{ |
| 3608 |
const svalue *base = sval->get_base_svalue (); |
| 3609 |
const svalue *iter = sval->get_iter_svalue (); |
| 3610 |
|
| 3611 |
if (result_set.contains (base) || result_set.contains (iter)) |
| 3612 |
result_set.add (sval); |
| 3613 |
} |
| 3614 |
|
| 3615 |
void visit_initial_svalue (const initial_svalue *sval) final override |
| 3616 |
{ |
| 3617 |
equiv_class_id id = equiv_class_id::null (); |
| 3618 |
if (m_cm->get_equiv_class_by_svalue (sval, &id)) |
| 3619 |
{ |
| 3620 |
if (tree cst = id.get_obj (*m_cm).get_any_constant ()) |
| 3621 |
check_constant (cst, sval); |
| 3622 |
} |
| 3623 |
else if (!m_cm->sval_constrained_p (sval)) |
| 3624 |
{ |
| 3625 |
result_set.add (sval); |
| 3626 |
} |
| 3627 |
} |
| 3628 |
|
| 3629 |
void visit_conjured_svalue (const conjured_svalue *sval) final override |
| 3630 |
{ |
| 3631 |
equiv_class_id id = equiv_class_id::null (); |
| 3632 |
if (m_cm->get_equiv_class_by_svalue (sval, &id)) |
| 3633 |
if (tree cst = id.get_obj (*m_cm).get_any_constant ()) |
| 3634 |
check_constant (cst, sval); |
| 3635 |
} |
| 3636 |
|
| 3637 |
private: |
| 3638 |
void check_constant (tree cst, const svalue *sval) |
| 3639 |
{ |
| 3640 |
switch (TREE_CODE (cst)) |
| 3641 |
{ |
| 3642 |
default: |
| 3643 |
/* Assume all unhandled operands are compatible. */ |
| 3644 |
break; |
| 3645 |
case INTEGER_CST: |
| 3646 |
if (!capacity_compatible_with_type (cst, m_size_cst)) |
| 3647 |
result_set.add (sval); |
| 3648 |
break; |
| 3649 |
} |
| 3650 |
} |
| 3651 |
|
| 3652 |
tree m_size_cst; |
| 3653 |
const svalue *m_root_sval; |
| 3654 |
constraint_manager *m_cm; |
| 3655 |
svalue_set result_set; /* Used as a mapping of svalue*->bool. */ |
| 3656 |
}; |
| 3657 |
|
| 3658 |
/* Return true if SIZE_CST is a power of 2, and we have |
| 3659 |
CAPACITY_SVAL == ((X | (Y - 1) ) + 1), since it is then a multiple |
| 3660 |
of SIZE_CST, as used by Linux kernel's round_up macro. */ |
| 3661 |
|
| 3662 |
static bool |
| 3663 |
is_round_up (tree size_cst, |
| 3664 |
const svalue *capacity_sval) |
| 3665 |
{ |
| 3666 |
if (!integer_pow2p (size_cst)) |
| 3667 |
return false; |
| 3668 |
const binop_svalue *binop_sval = capacity_sval->dyn_cast_binop_svalue (); |
| 3669 |
if (!binop_sval) |
| 3670 |
return false; |
| 3671 |
if (binop_sval->get_op () != PLUS_EXPR) |
| 3672 |
return false; |
| 3673 |
tree rhs_cst = binop_sval->get_arg1 ()->maybe_get_constant (); |
| 3674 |
if (!rhs_cst) |
| 3675 |
return false; |
| 3676 |
if (!integer_onep (rhs_cst)) |
| 3677 |
return false; |
| 3678 |
|
| 3679 |
/* We have CAPACITY_SVAL == (LHS + 1) for some LHS expression. */ |
| 3680 |
|
| 3681 |
const binop_svalue *lhs_binop_sval |
| 3682 |
= binop_sval->get_arg0 ()->dyn_cast_binop_svalue (); |
| 3683 |
if (!lhs_binop_sval) |
| 3684 |
return false; |
| 3685 |
if (lhs_binop_sval->get_op () != BIT_IOR_EXPR) |
| 3686 |
return false; |
| 3687 |
|
| 3688 |
tree inner_rhs_cst = lhs_binop_sval->get_arg1 ()->maybe_get_constant (); |
| 3689 |
if (!inner_rhs_cst) |
| 3690 |
return false; |
| 3691 |
|
| 3692 |
if (wi::to_widest (inner_rhs_cst) + 1 != wi::to_widest (size_cst)) |
| 3693 |
return false; |
| 3694 |
return true; |
| 3695 |
} |
| 3696 |
|
| 3697 |
/* Return true if CAPACITY_SVAL is known to be a multiple of SIZE_CST. */ |
| 3698 |
|
| 3699 |
static bool |
| 3700 |
is_multiple_p (tree size_cst, |
| 3701 |
const svalue *capacity_sval) |
| 3702 |
{ |
| 3703 |
if (const svalue *sval = capacity_sval->maybe_undo_cast ()) |
| 3704 |
return is_multiple_p (size_cst, sval); |
| 3705 |
|
| 3706 |
if (is_round_up (size_cst, capacity_sval)) |
| 3707 |
return true; |
| 3708 |
|
| 3709 |
return false; |
| 3710 |
} |
| 3711 |
|
| 3712 |
/* Return true if we should emit a dubious_allocation_size warning |
| 3713 |
on assigning a region of capacity CAPACITY_SVAL bytes to a pointer |
| 3714 |
of type with size SIZE_CST, where CM expresses known constraints. */ |
| 3715 |
|
| 3716 |
static bool |
| 3717 |
is_dubious_capacity (tree size_cst, |
| 3718 |
const svalue *capacity_sval, |
| 3719 |
constraint_manager *cm) |
| 3720 |
{ |
| 3721 |
if (is_multiple_p (size_cst, capacity_sval)) |
| 3722 |
return false; |
| 3723 |
size_visitor v (size_cst, capacity_sval, cm); |
| 3724 |
return v.is_dubious_capacity (); |
| 3725 |
} |
| 3726 |
|
| 3727 |
|
| 3728 |
/* Return true if a struct or union either uses the inheritance pattern, |
| 3729 |
where the first field is a base struct, or the flexible array member |
| 3730 |
pattern, where the last field is an array without a specified size. */ |
| 3731 |
|
| 3732 |
static bool |
| 3733 |
struct_or_union_with_inheritance_p (tree struc) |
| 3734 |
{ |
| 3735 |
tree iter = TYPE_FIELDS (struc); |
| 3736 |
if (iter == NULL_TREE) |
| 3737 |
return false; |
| 3738 |
if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (iter))) |
| 3739 |
return true; |
| 3740 |
|
| 3741 |
tree last_field; |
| 3742 |
while (iter != NULL_TREE) |
| 3743 |
{ |
| 3744 |
last_field = iter; |
| 3745 |
iter = DECL_CHAIN (iter); |
| 3746 |
} |
| 3747 |
|
| 3748 |
if (last_field != NULL_TREE |
| 3749 |
&& TREE_CODE (TREE_TYPE (last_field)) == ARRAY_TYPE) |
| 3750 |
return true; |
| 3751 |
|
| 3752 |
return false; |
| 3753 |
} |
| 3754 |
|
| 3755 |
/* Return true if the lhs and rhs of an assignment have different types. */ |
| 3756 |
|
| 3757 |
static bool |
| 3758 |
is_any_cast_p (const gimple *stmt) |
| 3759 |
{ |
| 3760 |
if (const gassign *assign = dyn_cast <const gassign *> (stmt)) |
| 3761 |
return gimple_assign_cast_p (assign) |
| 3762 |
|| !pending_diagnostic::same_tree_p ( |
| 3763 |
TREE_TYPE (gimple_assign_lhs (assign)), |
| 3764 |
TREE_TYPE (gimple_assign_rhs1 (assign))); |
| 3765 |
else if (const gcall *call = dyn_cast <const gcall *> (stmt)) |
| 3766 |
{ |
| 3767 |
tree lhs = gimple_call_lhs (call); |
| 3768 |
return lhs != NULL_TREE && !pending_diagnostic::same_tree_p ( |
| 3769 |
TREE_TYPE (gimple_call_lhs (call)), |
| 3770 |
gimple_call_return_type (call)); |
| 3771 |
} |
| 3772 |
|
| 3773 |
return false; |
| 3774 |
} |
| 3775 |
|
| 3776 |
/* On pointer assignments, check whether the buffer size of |
| 3777 |
RHS_SVAL is compatible with the type of the LHS_REG. |
| 3778 |
Use a non-null CTXT to report allocation size warnings. */ |
| 3779 |
|
| 3780 |
void |
| 3781 |
region_model::check_region_size (const region *lhs_reg, const svalue *rhs_sval, |
| 3782 |
region_model_context *ctxt) const |
| 3783 |
{ |
| 3784 |
if (!ctxt || ctxt->get_stmt () == NULL) |
| 3785 |
return; |
| 3786 |
/* Only report warnings on assignments that actually change the type. */ |
| 3787 |
if (!is_any_cast_p (ctxt->get_stmt ())) |
| 3788 |
return; |
| 3789 |
|
| 3790 |
tree pointer_type = lhs_reg->get_type (); |
| 3791 |
if (pointer_type == NULL_TREE || !POINTER_TYPE_P (pointer_type)) |
| 3792 |
return; |
| 3793 |
|
| 3794 |
tree pointee_type = TREE_TYPE (pointer_type); |
| 3795 |
/* Make sure that the type on the left-hand size actually has a size. */ |
| 3796 |
if (pointee_type == NULL_TREE || VOID_TYPE_P (pointee_type) |
| 3797 |
|| TYPE_SIZE_UNIT (pointee_type) == NULL_TREE) |
| 3798 |
return; |
| 3799 |
|
| 3800 |
/* Bail out early on function pointers. */ |
| 3801 |
if (TREE_CODE (pointee_type) == FUNCTION_TYPE) |
| 3802 |
return; |
| 3803 |
|
| 3804 |
/* Bail out early on pointers to structs where we can |
| 3805 |
not deduce whether the buffer size is compatible. */ |
| 3806 |
bool is_struct = RECORD_OR_UNION_TYPE_P (pointee_type); |
| 3807 |
if (is_struct && struct_or_union_with_inheritance_p (pointee_type)) |
| 3808 |
return; |
| 3809 |
|
| 3810 |
tree pointee_size_tree = size_in_bytes (pointee_type); |
| 3811 |
/* We give up if the type size is not known at compile-time or the |
| 3812 |
type size is always compatible regardless of the buffer size. */ |
| 3813 |
if (TREE_CODE (pointee_size_tree) != INTEGER_CST |
| 3814 |
|| integer_zerop (pointee_size_tree) |
| 3815 |
|| integer_onep (pointee_size_tree)) |
| 3816 |
return; |
| 3817 |
|
| 3818 |
const region *rhs_reg = deref_rvalue (rhs_sval, NULL_TREE, ctxt, false); |
| 3819 |
const svalue *capacity = get_capacity (rhs_reg); |
| 3820 |
switch (capacity->get_kind ()) |
| 3821 |
{ |
| 3822 |
case svalue_kind::SK_CONSTANT: |
| 3823 |
{ |
| 3824 |
const constant_svalue *cst_cap_sval |
| 3825 |
= as_a <const constant_svalue *> (capacity); |
| 3826 |
tree cst_cap = cst_cap_sval->get_constant (); |
| 3827 |
if (TREE_CODE (cst_cap) == INTEGER_CST |
| 3828 |
&& !capacity_compatible_with_type (cst_cap, pointee_size_tree, |
| 3829 |
is_struct)) |
| 3830 |
ctxt->warn (make_unique <dubious_allocation_size> (lhs_reg, rhs_reg, |
| 3831 |
capacity, cst_cap, |
| 3832 |
ctxt->get_stmt ())); |
| 3833 |
} |
| 3834 |
break; |
| 3835 |
default: |
| 3836 |
{ |
| 3837 |
if (!is_struct) |
| 3838 |
{ |
| 3839 |
if (is_dubious_capacity (pointee_size_tree, |
| 3840 |
capacity, |
| 3841 |
m_constraints)) |
| 3842 |
{ |
| 3843 |
tree expr = get_representative_tree (capacity); |
| 3844 |
ctxt->warn (make_unique <dubious_allocation_size> (lhs_reg, |
| 3845 |
rhs_reg, |
| 3846 |
capacity, expr, |
| 3847 |
ctxt->get_stmt ())); |
| 3848 |
} |
| 3849 |
} |
| 3850 |
break; |
| 3851 |
} |
| 3852 |
} |
| 3853 |
} |
| 3854 |
|
| 3855 |
/* Set the value of the region given by LHS_REG to the value given |
| 3856 |
by RHS_SVAL. |
| 3857 |
Use CTXT to report any warnings associated with writing to LHS_REG. */ |
| 3858 |
|
| 3859 |
void |
| 3860 |
region_model::set_value (const region *lhs_reg, const svalue *rhs_sval, |
| 3861 |
region_model_context *ctxt) |
| 3862 |
{ |
| 3863 |
gcc_assert (lhs_reg); |
| 3864 |
gcc_assert (rhs_sval); |
| 3865 |
|
| 3866 |
/* Setting the value of an empty region is a no-op. */ |
| 3867 |
if (lhs_reg->empty_p ()) |
| 3868 |
return; |
| 3869 |
|
| 3870 |
check_region_size (lhs_reg, rhs_sval, ctxt); |
| 3871 |
|
| 3872 |
check_region_for_write (lhs_reg, rhs_sval, ctxt); |
| 3873 |
|
| 3874 |
m_store.set_value (m_mgr->get_store_manager(), lhs_reg, rhs_sval, |
| 3875 |
ctxt ? ctxt->get_uncertainty () : NULL); |
| 3876 |
} |
| 3877 |
|
| 3878 |
/* Set the value of the region given by LHS to the value given by RHS. */ |
| 3879 |
|
| 3880 |
void |
| 3881 |
region_model::set_value (tree lhs, tree rhs, region_model_context *ctxt) |
| 3882 |
{ |
| 3883 |
const region *lhs_reg = get_lvalue (lhs, ctxt); |
| 3884 |
const svalue *rhs_sval = get_rvalue (rhs, ctxt); |
| 3885 |
gcc_assert (lhs_reg); |
| 3886 |
gcc_assert (rhs_sval); |
| 3887 |
set_value (lhs_reg, rhs_sval, ctxt); |
| 3888 |
} |
| 3889 |
|
| 3890 |
/* Issue a note specifying that a particular function parameter is expected |
| 3891 |
to be a valid null-terminated string. */ |
| 3892 |
|
| 3893 |
static void |
| 3894 |
inform_about_expected_null_terminated_string_arg (const call_arg_details &ad) |
| 3895 |
{ |
| 3896 |
// TODO: ideally we'd underline the param here |
| 3897 |
inform (DECL_SOURCE_LOCATION (ad.m_called_fndecl), |
| 3898 |
"argument %d of %qD must be a pointer to a null-terminated string", |
| 3899 |
ad.m_arg_idx + 1, ad.m_called_fndecl); |
| 3900 |
} |
| 3901 |
|
| 3902 |
/* A binding of a specific svalue at a concrete byte range. */ |
| 3903 |
|
| 3904 |
struct fragment |
| 3905 |
{ |
| 3906 |
fragment () |
| 3907 |
: m_byte_range (0, 0), m_sval (nullptr) |
| 3908 |
{ |
| 3909 |
} |
| 3910 |
|
| 3911 |
fragment (const byte_range &bytes, const svalue *sval) |
| 3912 |
: m_byte_range (bytes), m_sval (sval) |
| 3913 |
{ |
| 3914 |
} |
| 3915 |
|
| 3916 |
static int cmp_ptrs (const void *p1, const void *p2) |
| 3917 |
{ |
| 3918 |
const fragment *f1 = (const fragment *)p1; |
| 3919 |
const fragment *f2 = (const fragment *)p2; |
| 3920 |
return byte_range::cmp (f1->m_byte_range, f2->m_byte_range); |
| 3921 |
} |
| 3922 |
|
| 3923 |
void |
| 3924 |
dump_to_pp (pretty_printer *pp) const |
| 3925 |
{ |
| 3926 |
pp_string (pp, "fragment("); |
| 3927 |
m_byte_range.dump_to_pp (pp); |
| 3928 |
pp_string (pp, ", sval: "); |
| 3929 |
if (m_sval) |
| 3930 |
m_sval->dump_to_pp (pp, true); |
| 3931 |
else |
| 3932 |
pp_string (pp, "nullptr"); |
| 3933 |
pp_string (pp, ")"); |
| 3934 |
} |
| 3935 |
|
| 3936 |
byte_range m_byte_range; |
| 3937 |
const svalue *m_sval; |
| 3938 |
}; |
| 3939 |
|
| 3940 |
/* Determine if there is a zero terminator somewhere in the |
| 3941 |
part of STRING_CST covered by BYTES (where BYTES is relative to the |
| 3942 |
start of the constant). |
| 3943 |
|
| 3944 |
Return a tristate: |
| 3945 |
- true if there definitely is a zero byte, writing to *OUT_BYTES_READ |
| 3946 |
the number of bytes from that would be read, including the zero byte. |
| 3947 |
- false if there definitely isn't a zero byte |
| 3948 |
- unknown if we don't know. */ |
| 3949 |
|
| 3950 |
static tristate |
| 3951 |
string_cst_has_null_terminator (tree string_cst, |
| 3952 |
const byte_range &bytes, |
| 3953 |
byte_offset_t *out_bytes_read) |
| 3954 |
{ |
| 3955 |
gcc_assert (bytes.m_start_byte_offset >= 0); |
| 3956 |
|
| 3957 |
/* If we're beyond the string_cst, reads are unsuccessful. */ |
| 3958 |
if (tree cst_size = get_string_cst_size (string_cst)) |
| 3959 |
if (TREE_CODE (cst_size) == INTEGER_CST) |
| 3960 |
if (bytes.m_start_byte_offset >= TREE_INT_CST_LOW (cst_size)) |
| 3961 |
return tristate::unknown (); |
| 3962 |
|
| 3963 |
/* Assume all bytes after TREE_STRING_LENGTH are zero. This handles |
| 3964 |
the case where an array is initialized with a string_cst that isn't |
| 3965 |
as long as the array, where the remaining elements are |
| 3966 |
empty-initialized and thus zeroed. */ |
| 3967 |
if (bytes.m_start_byte_offset >= TREE_STRING_LENGTH (string_cst)) |
| 3968 |
{ |
| 3969 |
*out_bytes_read = 1; |
| 3970 |
return tristate (true); |
| 3971 |
} |
| 3972 |
|
| 3973 |
/* Look for the first 0 byte within STRING_CST |
| 3974 |
from START_READ_OFFSET onwards. */ |
| 3975 |
const byte_offset_t num_bytes_to_search |
| 3976 |
= std::min<byte_offset_t> ((TREE_STRING_LENGTH (string_cst) |
| 3977 |
- bytes.m_start_byte_offset), |
| 3978 |
bytes.m_size_in_bytes); |
| 3979 |
const char *start = (TREE_STRING_POINTER (string_cst) |
| 3980 |
+ bytes.m_start_byte_offset.slow ()); |
| 3981 |
if (num_bytes_to_search >= 0) |
| 3982 |
if (const void *p = memchr (start, 0, bytes.m_size_in_bytes.slow ())) |
| 3983 |
{ |
| 3984 |
*out_bytes_read = (const char *)p - start + 1; |
| 3985 |
return tristate (true); |
| 3986 |
} |
| 3987 |
|
| 3988 |
*out_bytes_read = bytes.m_size_in_bytes; |
| 3989 |
return tristate (false); |
| 3990 |
} |
| 3991 |
|
| 3992 |
static tristate |
| 3993 |
svalue_byte_range_has_null_terminator (const svalue *sval, |
| 3994 |
const byte_range &bytes, |
| 3995 |
byte_offset_t *out_bytes_read, |
| 3996 |
logger *logger); |
| 3997 |
|
| 3998 |
/* Determine if there is a zero terminator somewhere in the |
| 3999 |
part of SVAL covered by BYTES (where BYTES is relative to the svalue). |
| 4000 |
|
| 4001 |
Return a tristate: |
| 4002 |
- true if there definitely is a zero byte, writing to *OUT_BYTES_READ |
| 4003 |
the number of bytes from that would be read, including the zero byte. |
| 4004 |
- false if there definitely isn't a zero byte |
| 4005 |
- unknown if we don't know. |
| 4006 |
|
| 4007 |
Use LOGGER (if non-null) for any logging. */ |
| 4008 |
|
| 4009 |
static tristate |
| 4010 |
svalue_byte_range_has_null_terminator_1 (const svalue *sval, |
| 4011 |
const byte_range &bytes, |
| 4012 |
byte_offset_t *out_bytes_read, |
| 4013 |
logger *logger) |
| 4014 |
{ |
| 4015 |
if (bytes.m_start_byte_offset == 0 |
| 4016 |
&& sval->all_zeroes_p ()) |
| 4017 |
{ |
| 4018 |
/* The initial byte of an all-zeroes SVAL is a zero byte. */ |
| 4019 |
*out_bytes_read = 1; |
| 4020 |
return tristate (true); |
| 4021 |
} |
| 4022 |
|
| 4023 |
switch (sval->get_kind ()) |
| 4024 |
{ |
| 4025 |
case SK_CONSTANT: |
| 4026 |
{ |
| 4027 |
tree cst |
| 4028 |
= as_a <const constant_svalue *> (sval)->get_constant (); |
| 4029 |
switch (TREE_CODE (cst)) |
| 4030 |
{ |
| 4031 |
case STRING_CST: |
| 4032 |
return string_cst_has_null_terminator (cst, bytes, out_bytes_read); |
| 4033 |
case INTEGER_CST: |
| 4034 |
if (bytes.m_start_byte_offset == 0 |
| 4035 |
&& integer_onep (TYPE_SIZE_UNIT (TREE_TYPE (cst)))) |
| 4036 |
{ |
| 4037 |
/* Model accesses to the initial byte of a 1-byte |
| 4038 |
INTEGER_CST. */ |
| 4039 |
*out_bytes_read = 1; |
| 4040 |
if (zerop (cst)) |
| 4041 |
return tristate (true); |
| 4042 |
else |
| 4043 |
return tristate (false); |
| 4044 |
} |
| 4045 |
/* Treat any other access to an INTEGER_CST as unknown. */ |
| 4046 |
return tristate::TS_UNKNOWN; |
| 4047 |
|
| 4048 |
default: |
| 4049 |
break; |
| 4050 |
} |
| 4051 |
} |
| 4052 |
break; |
| 4053 |
|
| 4054 |
case SK_INITIAL: |
| 4055 |
{ |
| 4056 |
const initial_svalue *initial_sval = (const initial_svalue *)sval; |
| 4057 |
const region *reg = initial_sval->get_region (); |
| 4058 |
if (const string_region *string_reg = reg->dyn_cast_string_region ()) |
| 4059 |
{ |
| 4060 |
tree string_cst = string_reg->get_string_cst (); |
| 4061 |
return string_cst_has_null_terminator (string_cst, |
| 4062 |
bytes, |
| 4063 |
out_bytes_read); |
| 4064 |
} |
| 4065 |
return tristate::TS_UNKNOWN; |
| 4066 |
} |
| 4067 |
break; |
| 4068 |
|
| 4069 |
case SK_BITS_WITHIN: |
| 4070 |
{ |
| 4071 |
const bits_within_svalue *bits_within_sval |
| 4072 |
= (const bits_within_svalue *)sval; |
| 4073 |
byte_range bytes_within_inner (0, 0); |
| 4074 |
if (bits_within_sval->get_bits ().as_byte_range (&bytes_within_inner)) |
| 4075 |
{ |
| 4076 |
/* Consider e.g. looking for null terminator of |
| 4077 |
bytes 2-4 of BITS_WITHIN(bytes 10-15 of inner_sval) |
| 4078 |
|
| 4079 |
This is equivalent to looking within bytes 12-14 of |
| 4080 |
inner_sval. */ |
| 4081 |
const byte_offset_t start_byte_relative_to_inner |
| 4082 |
= (bytes.m_start_byte_offset |
| 4083 |
+ bytes_within_inner.m_start_byte_offset); |
| 4084 |
const byte_offset_t next_byte_relative_to_inner |
| 4085 |
= (bytes.get_next_byte_offset () |
| 4086 |
+ bytes_within_inner.m_start_byte_offset); |
| 4087 |
if (next_byte_relative_to_inner > start_byte_relative_to_inner) |
| 4088 |
{ |
| 4089 |
const byte_range relative_to_inner |
| 4090 |
(start_byte_relative_to_inner, |
| 4091 |
next_byte_relative_to_inner - start_byte_relative_to_inner); |
| 4092 |
const svalue *inner_sval |
| 4093 |
= bits_within_sval->get_inner_svalue (); |
| 4094 |
return svalue_byte_range_has_null_terminator (inner_sval, |
| 4095 |
relative_to_inner, |
| 4096 |
out_bytes_read, |
| 4097 |
logger); |
| 4098 |
} |
| 4099 |
} |
| 4100 |
} |
| 4101 |
break; |
| 4102 |
|
| 4103 |
default: |
| 4104 |
// TODO: it may be possible to handle other cases here. |
| 4105 |
break; |
| 4106 |
} |
| 4107 |
return tristate::TS_UNKNOWN; |
| 4108 |
} |
| 4109 |
|
| 4110 |
/* Like svalue_byte_range_has_null_terminator_1, but add logging. */ |
| 4111 |
|
| 4112 |
static tristate |
| 4113 |
svalue_byte_range_has_null_terminator (const svalue *sval, |
| 4114 |
const byte_range &bytes, |
| 4115 |
byte_offset_t *out_bytes_read, |
| 4116 |
logger *logger) |
| 4117 |
{ |
| 4118 |
LOG_SCOPE (logger); |
| 4119 |
if (logger) |
| 4120 |
{ |
| 4121 |
pretty_printer *pp = logger->get_printer (); |
| 4122 |
logger->start_log_line (); |
| 4123 |
bytes.dump_to_pp (pp); |
| 4124 |
logger->log_partial (" of sval: "); |
| 4125 |
sval->dump_to_pp (pp, true); |
| 4126 |
logger->end_log_line (); |
| 4127 |
} |
| 4128 |
tristate ts |
| 4129 |
= svalue_byte_range_has_null_terminator_1 (sval, bytes, |
| 4130 |
out_bytes_read, logger); |
| 4131 |
if (logger) |
| 4132 |
{ |
| 4133 |
pretty_printer *pp = logger->get_printer (); |
| 4134 |
logger->start_log_line (); |
| 4135 |
pp_printf (pp, "has null terminator: %s", ts.as_string ()); |
| 4136 |
if (ts.is_true ()) |
| 4137 |
{ |
| 4138 |
pp_string (pp, "; bytes read: "); |
| 4139 |
pp_wide_int (pp, *out_bytes_read, SIGNED); |
| 4140 |
} |
| 4141 |
logger->end_log_line (); |
| 4142 |
} |
| 4143 |
return ts; |
| 4144 |
} |
| 4145 |
|
| 4146 |
/* A frozen copy of a single base region's binding_cluster within a store, |
| 4147 |
optimized for traversal of the concrete parts in byte order. |
| 4148 |
This only captures concrete bindings, and is an implementation detail |
| 4149 |
of region_model::scan_for_null_terminator. */ |
| 4150 |
|
| 4151 |
class iterable_cluster |
| 4152 |
{ |
| 4153 |
public: |
| 4154 |
iterable_cluster (const binding_cluster *cluster) |
| 4155 |
{ |
| 4156 |
if (!cluster) |
| 4157 |
return; |
| 4158 |
for (auto iter : *cluster) |
| 4159 |
{ |
| 4160 |
const binding_key *key = iter.first; |
| 4161 |
const svalue *sval = iter.second; |
| 4162 |
|
| 4163 |
if (const concrete_binding *concrete_key |
| 4164 |
= key->dyn_cast_concrete_binding ()) |
| 4165 |
{ |
| 4166 |
byte_range fragment_bytes (0, 0); |
| 4167 |
if (concrete_key->get_byte_range (&fragment_bytes)) |
| 4168 |
m_fragments.safe_push (fragment (fragment_bytes, sval)); |
| 4169 |
} |
| 4170 |
else |
| 4171 |
m_symbolic_bindings.safe_push (key); |
| 4172 |
} |
| 4173 |
m_fragments.qsort (fragment::cmp_ptrs); |
| 4174 |
} |
| 4175 |
|
| 4176 |
bool |
| 4177 |
get_fragment_for_byte (byte_offset_t byte, fragment *out_frag) const |
| 4178 |
{ |
| 4179 |
/* TODO: binary search rather than linear. */ |
| 4180 |
unsigned iter_idx; |
| 4181 |
for (iter_idx = 0; iter_idx < m_fragments.length (); iter_idx++) |
| 4182 |
{ |
| 4183 |
if (m_fragments[iter_idx].m_byte_range.contains_p (byte)) |
| 4184 |
{ |
| 4185 |
*out_frag = m_fragments[iter_idx]; |
| 4186 |
return true; |
| 4187 |
} |
| 4188 |
} |
| 4189 |
return false; |
| 4190 |
} |
| 4191 |
|
| 4192 |
bool has_symbolic_bindings_p () const |
| 4193 |
{ |
| 4194 |
return !m_symbolic_bindings.is_empty (); |
| 4195 |
} |
| 4196 |
|
| 4197 |
void dump_to_pp (pretty_printer *pp) const |
| 4198 |
{ |
| 4199 |
pp_string (pp, "iterable_cluster (fragments: ["); |
| 4200 |
for (auto const &iter : &m_fragments) |
| 4201 |
{ |
| 4202 |
if (&iter != m_fragments.begin ()) |
| 4203 |
pp_string (pp, ", "); |
| 4204 |
iter.dump_to_pp (pp); |
| 4205 |
} |
| 4206 |
pp_printf (pp, "], symbolic bindings: ["); |
| 4207 |
for (auto const &iter : m_symbolic_bindings) |
| 4208 |
{ |
| 4209 |
if (&iter != m_symbolic_bindings.begin ()) |
| 4210 |
pp_string (pp, ", "); |
| 4211 |
(*iter).dump_to_pp (pp, true); |
| 4212 |
} |
| 4213 |
pp_string (pp, "])"); |
| 4214 |
} |
| 4215 |
|
| 4216 |
private: |
| 4217 |
auto_vec<fragment> m_fragments; |
| 4218 |
auto_vec<const binding_key *> m_symbolic_bindings; |
| 4219 |
}; |
| 4220 |
|
| 4221 |
/* Simulate reading the bytes at BYTES from BASE_REG. |
| 4222 |
Complain to CTXT about any issues with the read e.g. out-of-bounds. */ |
| 4223 |
|
| 4224 |
const svalue * |
| 4225 |
region_model::get_store_bytes (const region *base_reg, |
| 4226 |
const byte_range &bytes, |
| 4227 |
region_model_context *ctxt) const |
| 4228 |
{ |
| 4229 |
/* Shortcut reading all of a string_region. */ |
| 4230 |
if (bytes.get_start_byte_offset () == 0) |
| 4231 |
if (const string_region *string_reg = base_reg->dyn_cast_string_region ()) |
| 4232 |
if (bytes.m_size_in_bytes |
| 4233 |
== TREE_STRING_LENGTH (string_reg->get_string_cst ())) |
| 4234 |
return m_mgr->get_or_create_initial_value (base_reg); |
| 4235 |
|
| 4236 |
const svalue *index_sval |
| 4237 |
= m_mgr->get_or_create_int_cst (size_type_node, |
| 4238 |
bytes.get_start_byte_offset ()); |
| 4239 |
const region *offset_reg = m_mgr->get_offset_region (base_reg, |
| 4240 |
NULL_TREE, |
| 4241 |
index_sval); |
| 4242 |
const svalue *byte_size_sval |
| 4243 |
= m_mgr->get_or_create_int_cst (size_type_node, bytes.m_size_in_bytes); |
| 4244 |
const region *read_reg = m_mgr->get_sized_region (offset_reg, |
| 4245 |
NULL_TREE, |
| 4246 |
byte_size_sval); |
| 4247 |
|
| 4248 |
/* Simulate reading those bytes from the store. */ |
| 4249 |
const svalue *sval = get_store_value (read_reg, ctxt); |
| 4250 |
return sval; |
| 4251 |
} |
| 4252 |
|
| 4253 |
static tree |
| 4254 |
get_tree_for_byte_offset (tree ptr_expr, byte_offset_t byte_offset) |
| 4255 |
{ |
| 4256 |
gcc_assert (ptr_expr); |
| 4257 |
tree ptype = build_pointer_type_for_mode (char_type_node, ptr_mode, true); |
| 4258 |
return fold_build2 (MEM_REF, |
| 4259 |
char_type_node, |
| 4260 |
ptr_expr, wide_int_to_tree (ptype, byte_offset)); |
| 4261 |
} |
| 4262 |
|
| 4263 |
/* Simulate a series of reads of REG until we find a 0 byte |
| 4264 |
(equivalent to calling strlen). |
| 4265 |
|
| 4266 |
Complain to CTXT and return NULL if: |
| 4267 |
- the buffer pointed to isn't null-terminated |
| 4268 |
- the buffer pointed to has any uninitialized bytes before any 0-terminator |
| 4269 |
- any of the reads aren't within the bounds of the underlying base region |
| 4270 |
|
| 4271 |
Otherwise, return a svalue for the number of bytes read (strlen + 1), |
| 4272 |
and, if OUT_SVAL is non-NULL, write to *OUT_SVAL with an svalue |
| 4273 |
representing the content of REG up to and including the terminator. |
| 4274 |
|
| 4275 |
Algorithm |
| 4276 |
========= |
| 4277 |
|
| 4278 |
Get offset for first byte to read. |
| 4279 |
Find the binding (if any) that contains it. |
| 4280 |
Find the size in bits of that binding. |
| 4281 |
Round to the nearest byte (which way???) |
| 4282 |
Or maybe give up if we have a partial binding there. |
| 4283 |
Get the svalue from the binding. |
| 4284 |
Determine the strlen (if any) of that svalue. |
| 4285 |
Does it have a 0-terminator within it? |
| 4286 |
If so, we have a partial read up to and including that terminator |
| 4287 |
Read those bytes from the store; add to the result in the correct place. |
| 4288 |
Finish |
| 4289 |
If not, we have a full read of that svalue |
| 4290 |
Read those bytes from the store; add to the result in the correct place. |
| 4291 |
Update read/write offsets |
| 4292 |
Continue |
| 4293 |
If unknown: |
| 4294 |
Result is unknown |
| 4295 |
Finish |
| 4296 |
*/ |
| 4297 |
|
| 4298 |
const svalue * |
| 4299 |
region_model::scan_for_null_terminator_1 (const region *reg, |
| 4300 |
tree expr, |
| 4301 |
const svalue **out_sval, |
| 4302 |
region_model_context *ctxt) const |
| 4303 |
{ |
| 4304 |
logger *logger = ctxt ? ctxt->get_logger () : nullptr; |
| 4305 |
store_manager *store_mgr = m_mgr->get_store_manager (); |
| 4306 |
|
| 4307 |
region_offset offset = reg->get_offset (m_mgr); |
| 4308 |
if (offset.symbolic_p ()) |
| 4309 |
{ |
| 4310 |
if (out_sval) |
| 4311 |
*out_sval = get_store_value (reg, nullptr); |
| 4312 |
if (logger) |
| 4313 |
logger->log ("offset is symbolic"); |
| 4314 |
return m_mgr->get_or_create_unknown_svalue (size_type_node); |
| 4315 |
} |
| 4316 |
byte_offset_t src_byte_offset; |
| 4317 |
if (!offset.get_concrete_byte_offset (&src_byte_offset)) |
| 4318 |
{ |
| 4319 |
if (out_sval) |
| 4320 |
*out_sval = get_store_value (reg, nullptr); |
| 4321 |
if (logger) |
| 4322 |
logger->log ("can't get concrete byte offset"); |
| 4323 |
return m_mgr->get_or_create_unknown_svalue (size_type_node); |
| 4324 |
} |
| 4325 |
const byte_offset_t initial_src_byte_offset = src_byte_offset; |
| 4326 |
byte_offset_t dst_byte_offset = 0; |
| 4327 |
|
| 4328 |
const region *base_reg = reg->get_base_region (); |
| 4329 |
|
| 4330 |
if (const string_region *str_reg = base_reg->dyn_cast_string_region ()) |
| 4331 |
{ |
| 4332 |
tree string_cst = str_reg->get_string_cst (); |
| 4333 |
if (src_byte_offset >= 0 |
| 4334 |
&& src_byte_offset < TREE_STRING_LENGTH (string_cst) |
| 4335 |
&& wi::fits_shwi_p (src_byte_offset)) |
| 4336 |
{ |
| 4337 |
HOST_WIDE_INT str_byte_offset = src_byte_offset.to_shwi (); |
| 4338 |
const char *effective_start |
| 4339 |
= TREE_STRING_POINTER (string_cst) + str_byte_offset; |
| 4340 |
size_t effective_len |
| 4341 |
= TREE_STRING_LENGTH (string_cst) - str_byte_offset; |
| 4342 |
if (const void *p = memchr (effective_start, 0, effective_len)) |
| 4343 |
{ |
| 4344 |
size_t num_bytes_read |
| 4345 |
= (const char *)p - effective_start + 1; |
| 4346 |
/* Simulate the read. */ |
| 4347 |
byte_range bytes_to_read (0, num_bytes_read); |
| 4348 |
const svalue *sval = get_store_bytes (reg, bytes_to_read, ctxt); |
| 4349 |
if (out_sval) |
| 4350 |
*out_sval = sval; |
| 4351 |
if (logger) |
| 4352 |
logger->log ("using string_cst"); |
| 4353 |
return m_mgr->get_or_create_int_cst (size_type_node, |
| 4354 |
num_bytes_read); |
| 4355 |
} |
| 4356 |
} |
| 4357 |
} |
| 4358 |
const binding_cluster *cluster = m_store.get_cluster (base_reg); |
| 4359 |
iterable_cluster c (cluster); |
| 4360 |
if (logger) |
| 4361 |
{ |
| 4362 |
pretty_printer *pp = logger->get_printer (); |
| 4363 |
logger->start_log_line (); |
| 4364 |
c.dump_to_pp (pp); |
| 4365 |
logger->end_log_line (); |
| 4366 |
} |
| 4367 |
|
| 4368 |
binding_map result; |
| 4369 |
|
| 4370 |
while (1) |
| 4371 |
{ |
| 4372 |
fragment f; |
| 4373 |
if (c.get_fragment_for_byte (src_byte_offset, &f)) |
| 4374 |
{ |
| 4375 |
if (logger) |
| 4376 |
{ |
| 4377 |
logger->start_log_line (); |
| 4378 |
pretty_printer *pp = logger->get_printer (); |
| 4379 |
pp_printf (pp, "src_byte_offset: "); |
| 4380 |
pp_wide_int (pp, src_byte_offset, SIGNED); |
| 4381 |
pp_string (pp, ": "); |
| 4382 |
f.dump_to_pp (pp); |
| 4383 |
logger->end_log_line (); |
| 4384 |
} |
| 4385 |
gcc_assert (f.m_byte_range.contains_p (src_byte_offset)); |
| 4386 |
/* src_byte_offset and f.m_byte_range are both expressed relative to |
| 4387 |
the base region. |
| 4388 |
Convert to a byte_range relative to the svalue. */ |
| 4389 |
const byte_range bytes_relative_to_svalue |
| 4390 |
(src_byte_offset - f.m_byte_range.get_start_byte_offset (), |
| 4391 |
f.m_byte_range.get_next_byte_offset () - src_byte_offset); |
| 4392 |
byte_offset_t fragment_bytes_read; |
| 4393 |
tristate is_terminated |
| 4394 |
= svalue_byte_range_has_null_terminator (f.m_sval, |
| 4395 |
bytes_relative_to_svalue, |
| 4396 |
&fragment_bytes_read, |
| 4397 |
logger); |
| 4398 |
if (is_terminated.is_unknown ()) |
| 4399 |
{ |
| 4400 |
if (out_sval) |
| 4401 |
*out_sval = get_store_value (reg, nullptr); |
| 4402 |
return m_mgr->get_or_create_unknown_svalue (size_type_node); |
| 4403 |
} |
| 4404 |
|
| 4405 |
/* Simulate reading those bytes from the store. */ |
| 4406 |
byte_range bytes_to_read (src_byte_offset, fragment_bytes_read); |
| 4407 |
const svalue *sval = get_store_bytes (base_reg, bytes_to_read, ctxt); |
| 4408 |
check_for_poison (sval, expr, nullptr, ctxt); |
| 4409 |
|
| 4410 |
if (out_sval) |
| 4411 |
{ |
| 4412 |
byte_range bytes_to_write (dst_byte_offset, fragment_bytes_read); |
| 4413 |
const binding_key *key |
| 4414 |
= store_mgr->get_concrete_binding (bytes_to_write); |
| 4415 |
result.put (key, sval); |
| 4416 |
} |
| 4417 |
|
| 4418 |
src_byte_offset += fragment_bytes_read; |
| 4419 |
dst_byte_offset += fragment_bytes_read; |
| 4420 |
|
| 4421 |
if (is_terminated.is_true ()) |
| 4422 |
{ |
| 4423 |
if (out_sval) |
| 4424 |
*out_sval = m_mgr->get_or_create_compound_svalue (NULL_TREE, |
| 4425 |
result); |
| 4426 |
if (logger) |
| 4427 |
logger->log ("got terminator"); |
| 4428 |
return m_mgr->get_or_create_int_cst (size_type_node, |
| 4429 |
dst_byte_offset); |
| 4430 |
} |
| 4431 |
} |
| 4432 |
else |
| 4433 |
break; |
| 4434 |
} |
| 4435 |
|
| 4436 |
/* No binding for this base_region, or no binding at src_byte_offset |
| 4437 |
(or a symbolic binding). */ |
| 4438 |
|
| 4439 |
if (c.has_symbolic_bindings_p ()) |
| 4440 |
{ |
| 4441 |
if (out_sval) |
| 4442 |
*out_sval = get_store_value (reg, nullptr); |
| 4443 |
if (logger) |
| 4444 |
logger->log ("got symbolic binding"); |
| 4445 |
return m_mgr->get_or_create_unknown_svalue (size_type_node); |
| 4446 |
} |
| 4447 |
|
| 4448 |
/* TODO: the various special-cases seen in |
| 4449 |
region_model::get_store_value. */ |
| 4450 |
|
| 4451 |
/* Simulate reading from this byte, then give up. */ |
| 4452 |
byte_range bytes_to_read (src_byte_offset, 1); |
| 4453 |
const svalue *sval = get_store_bytes (base_reg, bytes_to_read, ctxt); |
| 4454 |
tree byte_expr |
| 4455 |
= (expr |
| 4456 |
? get_tree_for_byte_offset (expr, |
| 4457 |
src_byte_offset - initial_src_byte_offset) |
| 4458 |
: NULL_TREE); |
| 4459 |
check_for_poison (sval, byte_expr, nullptr, ctxt); |
| 4460 |
if (base_reg->can_have_initial_svalue_p ()) |
| 4461 |
{ |
| 4462 |
if (out_sval) |
| 4463 |
*out_sval = get_store_value (reg, nullptr); |
| 4464 |
return m_mgr->get_or_create_unknown_svalue (size_type_node); |
| 4465 |
} |
| 4466 |
else |
| 4467 |
return nullptr; |
| 4468 |
} |
| 4469 |
|
| 4470 |
/* Like region_model::scan_for_null_terminator_1, but add logging. */ |
| 4471 |
|
| 4472 |
const svalue * |
| 4473 |
region_model::scan_for_null_terminator (const region *reg, |
| 4474 |
tree expr, |
| 4475 |
const svalue **out_sval, |
| 4476 |
region_model_context *ctxt) const |
| 4477 |
{ |
| 4478 |
logger *logger = ctxt ? ctxt->get_logger () : nullptr; |
| 4479 |
LOG_SCOPE (logger); |
| 4480 |
if (logger) |
| 4481 |
{ |
| 4482 |
pretty_printer *pp = logger->get_printer (); |
| 4483 |
logger->start_log_line (); |
| 4484 |
logger->log_partial ("region: "); |
| 4485 |
reg->dump_to_pp (pp, true); |
| 4486 |
logger->end_log_line (); |
| 4487 |
} |
| 4488 |
const svalue *sval = scan_for_null_terminator_1 (reg, expr, out_sval, ctxt); |
| 4489 |
if (logger) |
| 4490 |
{ |
| 4491 |
pretty_printer *pp = logger->get_printer (); |
| 4492 |
logger->start_log_line (); |
| 4493 |
logger->log_partial ("length result: "); |
| 4494 |
if (sval) |
| 4495 |
sval->dump_to_pp (pp, true); |
| 4496 |
else |
| 4497 |
pp_printf (pp, "NULL"); |
| 4498 |
logger->end_log_line (); |
| 4499 |
if (out_sval) |
| 4500 |
{ |
| 4501 |
logger->start_log_line (); |
| 4502 |
logger->log_partial ("content result: "); |
| 4503 |
if (*out_sval) |
| 4504 |
(*out_sval)->dump_to_pp (pp, true); |
| 4505 |
else |
| 4506 |
pp_printf (pp, "NULL"); |
| 4507 |
logger->end_log_line (); |
| 4508 |
} |
| 4509 |
} |
| 4510 |
return sval; |
| 4511 |
} |
| 4512 |
|
| 4513 |
/* Check that argument ARG_IDX (0-based) to the call described by CD |
| 4514 |
is a pointer to a valid null-terminated string. |
| 4515 |
|
| 4516 |
Simulate scanning through the buffer, reading until we find a 0 byte |
| 4517 |
(equivalent to calling strlen). |
| 4518 |
|
| 4519 |
Complain and return NULL if: |
| 4520 |
- the buffer pointed to isn't null-terminated |
| 4521 |
- the buffer pointed to has any uninitalized bytes before any 0-terminator |
| 4522 |
- any of the reads aren't within the bounds of the underlying base region |
| 4523 |
|
| 4524 |
Otherwise, return a svalue for strlen of the buffer (*not* including |
| 4525 |
the null terminator). |
| 4526 |
|
| 4527 |
TODO: we should also complain if: |
| 4528 |
- the pointer is NULL (or could be). */ |
| 4529 |
|
| 4530 |
const svalue * |
| 4531 |
region_model::check_for_null_terminated_string_arg (const call_details &cd, |
| 4532 |
unsigned arg_idx) const |
| 4533 |
{ |
| 4534 |
return check_for_null_terminated_string_arg (cd, |
| 4535 |
arg_idx, |
| 4536 |
false, /* include_terminator */ |
| 4537 |
nullptr); // out_sval |
| 4538 |
} |
| 4539 |
|
| 4540 |
|
| 4541 |
/* Check that argument ARG_IDX (0-based) to the call described by CD |
| 4542 |
is a pointer to a valid null-terminated string. |
| 4543 |
|
| 4544 |
Simulate scanning through the buffer, reading until we find a 0 byte |
| 4545 |
(equivalent to calling strlen). |
| 4546 |
|
| 4547 |
Complain and return NULL if: |
| 4548 |
- the buffer pointed to isn't null-terminated |
| 4549 |
- the buffer pointed to has any uninitalized bytes before any 0-terminator |
| 4550 |
- any of the reads aren't within the bounds of the underlying base region |
| 4551 |
|
| 4552 |
Otherwise, return a svalue. This will be the number of bytes read |
| 4553 |
(including the null terminator) if INCLUDE_TERMINATOR is true, or strlen |
| 4554 |
of the buffer (not including the null terminator) if it is false. |
| 4555 |
|
| 4556 |
Also, when returning an svalue, if OUT_SVAL is non-NULL, write to |
| 4557 |
*OUT_SVAL with an svalue representing the content of the buffer up to |
| 4558 |
and including the terminator. |
| 4559 |
|
| 4560 |
TODO: we should also complain if: |
| 4561 |
- the pointer is NULL (or could be). */ |
| 4562 |
|
| 4563 |
const svalue * |
| 4564 |
region_model::check_for_null_terminated_string_arg (const call_details &cd, |
| 4565 |
unsigned arg_idx, |
| 4566 |
bool include_terminator, |
| 4567 |
const svalue **out_sval) const |
| 4568 |
{ |
| 4569 |
class null_terminator_check_event : public custom_event |
| 4570 |
{ |
| 4571 |
public: |
| 4572 |
null_terminator_check_event (const event_loc_info &loc_info, |
| 4573 |
const call_arg_details &arg_details) |
| 4574 |
: custom_event (loc_info), |
| 4575 |
m_arg_details (arg_details) |
| 4576 |
{ |
| 4577 |
} |
| 4578 |
|
| 4579 |
void print_desc (pretty_printer &pp) const final override |
| 4580 |
{ |
| 4581 |
if (m_arg_details.m_arg_expr) |
| 4582 |
pp_printf (&pp, |
| 4583 |
"while looking for null terminator" |
| 4584 |
" for argument %i (%qE) of %qD...", |
| 4585 |
m_arg_details.m_arg_idx + 1, |
| 4586 |
m_arg_details.m_arg_expr, |
| 4587 |
m_arg_details.m_called_fndecl); |
| 4588 |
else |
| 4589 |
pp_printf (&pp, |
| 4590 |
"while looking for null terminator" |
| 4591 |
" for argument %i of %qD...", |
| 4592 |
m_arg_details.m_arg_idx + 1, |
| 4593 |
m_arg_details.m_called_fndecl); |
| 4594 |
} |
| 4595 |
|
| 4596 |
private: |
| 4597 |
const call_arg_details m_arg_details; |
| 4598 |
}; |
| 4599 |
|
| 4600 |
class null_terminator_check_decl_note |
| 4601 |
: public pending_note_subclass<null_terminator_check_decl_note> |
| 4602 |
{ |
| 4603 |
public: |
| 4604 |
null_terminator_check_decl_note (const call_arg_details &arg_details) |
| 4605 |
: m_arg_details (arg_details) |
| 4606 |
{ |
| 4607 |
} |
| 4608 |
|
| 4609 |
const char *get_kind () const final override |
| 4610 |
{ |
| 4611 |
return "null_terminator_check_decl_note"; |
| 4612 |
} |
| 4613 |
|
| 4614 |
void emit () const final override |
| 4615 |
{ |
| 4616 |
inform_about_expected_null_terminated_string_arg (m_arg_details); |
| 4617 |
} |
| 4618 |
|
| 4619 |
bool operator== (const null_terminator_check_decl_note &other) const |
| 4620 |
{ |
| 4621 |
return m_arg_details == other.m_arg_details; |
| 4622 |
} |
| 4623 |
|
| 4624 |
private: |
| 4625 |
const call_arg_details m_arg_details; |
| 4626 |
}; |
| 4627 |
|
| 4628 |
/* Subclass of decorated_region_model_context that |
| 4629 |
adds the above event and note to any saved diagnostics. */ |
| 4630 |
class annotating_ctxt : public annotating_context |
| 4631 |
{ |
| 4632 |
public: |
| 4633 |
annotating_ctxt (const call_details &cd, |
| 4634 |
unsigned arg_idx) |
| 4635 |
: annotating_context (cd.get_ctxt ()), |
| 4636 |
m_cd (cd), |
| 4637 |
m_arg_idx (arg_idx) |
| 4638 |
{ |
| 4639 |
} |
| 4640 |
void add_annotations () final override |
| 4641 |
{ |
| 4642 |
call_arg_details arg_details (m_cd, m_arg_idx); |
| 4643 |
event_loc_info loc_info (m_cd.get_location (), |
| 4644 |
m_cd.get_model ()->get_current_function ()->decl, |
| 4645 |
m_cd.get_model ()->get_stack_depth ()); |
| 4646 |
|
| 4647 |
add_event (make_unique<null_terminator_check_event> (loc_info, |
| 4648 |
arg_details)); |
| 4649 |
add_note (make_unique <null_terminator_check_decl_note> (arg_details)); |
| 4650 |
} |
| 4651 |
private: |
| 4652 |
const call_details &m_cd; |
| 4653 |
unsigned m_arg_idx; |
| 4654 |
}; |
| 4655 |
|
| 4656 |
/* Use this ctxt below so that any diagnostics that get added |
| 4657 |
get annotated. */ |
| 4658 |
annotating_ctxt my_ctxt (cd, arg_idx); |
| 4659 |
|
| 4660 |
const svalue *arg_sval = cd.get_arg_svalue (arg_idx); |
| 4661 |
const region *buf_reg |
| 4662 |
= deref_rvalue (arg_sval, cd.get_arg_tree (arg_idx), &my_ctxt); |
| 4663 |
|
| 4664 |
if (const svalue *num_bytes_read_sval |
| 4665 |
= scan_for_null_terminator (buf_reg, |
| 4666 |
cd.get_arg_tree (arg_idx), |
| 4667 |
out_sval, |
| 4668 |
&my_ctxt)) |
| 4669 |
{ |
| 4670 |
if (include_terminator) |
| 4671 |
return num_bytes_read_sval; |
| 4672 |
else |
| 4673 |
{ |
| 4674 |
/* strlen is (bytes_read - 1). */ |
| 4675 |
const svalue *one = m_mgr->get_or_create_int_cst (size_type_node, 1); |
| 4676 |
return m_mgr->get_or_create_binop (size_type_node, |
| 4677 |
MINUS_EXPR, |
| 4678 |
num_bytes_read_sval, |
| 4679 |
one); |
| 4680 |
} |
| 4681 |
} |
| 4682 |
else |
| 4683 |
return nullptr; |
| 4684 |
} |
| 4685 |
|
| 4686 |
/* Remove all bindings overlapping REG within the store. */ |
| 4687 |
|
| 4688 |
void |
| 4689 |
region_model::clobber_region (const region *reg) |
| 4690 |
{ |
| 4691 |
m_store.clobber_region (m_mgr->get_store_manager(), reg); |
| 4692 |
} |
| 4693 |
|
| 4694 |
/* Remove any bindings for REG within the store. */ |
| 4695 |
|
| 4696 |
void |
| 4697 |
region_model::purge_region (const region *reg) |
| 4698 |
{ |
| 4699 |
m_store.purge_region (m_mgr->get_store_manager(), reg); |
| 4700 |
} |
| 4701 |
|
| 4702 |
/* Fill REG with SVAL. |
| 4703 |
Use CTXT to report any warnings associated with the write |
| 4704 |
(e.g. out-of-bounds). */ |
| 4705 |
|
| 4706 |
void |
| 4707 |
region_model::fill_region (const region *reg, |
| 4708 |
const svalue *sval, |
| 4709 |
region_model_context *ctxt) |
| 4710 |
{ |
| 4711 |
check_region_for_write (reg, nullptr, ctxt); |
| 4712 |
m_store.fill_region (m_mgr->get_store_manager(), reg, sval); |
| 4713 |
} |
| 4714 |
|
| 4715 |
/* Zero-fill REG. |
| 4716 |
Use CTXT to report any warnings associated with the write |
| 4717 |
(e.g. out-of-bounds). */ |
| 4718 |
|
| 4719 |
void |
| 4720 |
region_model::zero_fill_region (const region *reg, |
| 4721 |
region_model_context *ctxt) |
| 4722 |
{ |
| 4723 |
check_region_for_write (reg, nullptr, ctxt); |
| 4724 |
m_store.zero_fill_region (m_mgr->get_store_manager(), reg); |
| 4725 |
} |
| 4726 |
|
| 4727 |
/* Copy NUM_BYTES_SVAL of SVAL to DEST_REG. |
| 4728 |
Use CTXT to report any warnings associated with the copy |
| 4729 |
(e.g. out-of-bounds writes). */ |
| 4730 |
|
| 4731 |
void |
| 4732 |
region_model::write_bytes (const region *dest_reg, |
| 4733 |
const svalue *num_bytes_sval, |
| 4734 |
const svalue *sval, |
| 4735 |
region_model_context *ctxt) |
| 4736 |
{ |
| 4737 |
const region *sized_dest_reg |
| 4738 |
= m_mgr->get_sized_region (dest_reg, NULL_TREE, num_bytes_sval); |
| 4739 |
set_value (sized_dest_reg, sval, ctxt); |
| 4740 |
} |
| 4741 |
|
| 4742 |
/* Read NUM_BYTES_SVAL from SRC_REG. |
| 4743 |
Use CTXT to report any warnings associated with the copy |
| 4744 |
(e.g. out-of-bounds reads, copying of uninitialized values, etc). */ |
| 4745 |
|
| 4746 |
const svalue * |
| 4747 |
region_model::read_bytes (const region *src_reg, |
| 4748 |
tree src_ptr_expr, |
| 4749 |
const svalue *num_bytes_sval, |
| 4750 |
region_model_context *ctxt) const |
| 4751 |
{ |
| 4752 |
if (num_bytes_sval->get_kind () == SK_UNKNOWN) |
| 4753 |
return m_mgr->get_or_create_unknown_svalue (NULL_TREE); |
| 4754 |
const region *sized_src_reg |
| 4755 |
= m_mgr->get_sized_region (src_reg, NULL_TREE, num_bytes_sval); |
| 4756 |
const svalue *src_contents_sval = get_store_value (sized_src_reg, ctxt); |
| 4757 |
check_for_poison (src_contents_sval, src_ptr_expr, |
| 4758 |
sized_src_reg, ctxt); |
| 4759 |
return src_contents_sval; |
| 4760 |
} |
| 4761 |
|
| 4762 |
/* Copy NUM_BYTES_SVAL bytes from SRC_REG to DEST_REG. |
| 4763 |
Use CTXT to report any warnings associated with the copy |
| 4764 |
(e.g. out-of-bounds reads/writes, copying of uninitialized values, |
| 4765 |
etc). */ |
| 4766 |
|
| 4767 |
void |
| 4768 |
region_model::copy_bytes (const region *dest_reg, |
| 4769 |
const region *src_reg, |
| 4770 |
tree src_ptr_expr, |
| 4771 |
const svalue *num_bytes_sval, |
| 4772 |
region_model_context *ctxt) |
| 4773 |
{ |
| 4774 |
const svalue *data_sval |
| 4775 |
= read_bytes (src_reg, src_ptr_expr, num_bytes_sval, ctxt); |
| 4776 |
write_bytes (dest_reg, num_bytes_sval, data_sval, ctxt); |
| 4777 |
} |
| 4778 |
|
| 4779 |
/* Mark REG as having unknown content. */ |
| 4780 |
|
| 4781 |
void |
| 4782 |
region_model::mark_region_as_unknown (const region *reg, |
| 4783 |
uncertainty_t *uncertainty) |
| 4784 |
{ |
| 4785 |
svalue_set maybe_live_values; |
| 4786 |
m_store.mark_region_as_unknown (m_mgr->get_store_manager(), reg, |
| 4787 |
uncertainty, &maybe_live_values); |
| 4788 |
m_store.on_maybe_live_values (maybe_live_values); |
| 4789 |
} |
| 4790 |
|
| 4791 |
/* Determine what is known about the condition "LHS_SVAL OP RHS_SVAL" within |
| 4792 |
this model. */ |
| 4793 |
|
| 4794 |
tristate |
| 4795 |
region_model::eval_condition (const svalue *lhs, |
| 4796 |
enum tree_code op, |
| 4797 |
const svalue *rhs) const |
| 4798 |
{ |
| 4799 |
gcc_assert (lhs); |
| 4800 |
gcc_assert (rhs); |
| 4801 |
|
| 4802 |
/* For now, make no attempt to capture constraints on floating-point |
| 4803 |
values. */ |
| 4804 |
if ((lhs->get_type () && FLOAT_TYPE_P (lhs->get_type ())) |
| 4805 |
|| (rhs->get_type () && FLOAT_TYPE_P (rhs->get_type ()))) |
| 4806 |
return tristate::unknown (); |
| 4807 |
|
| 4808 |
/* See what we know based on the values. */ |
| 4809 |
|
| 4810 |
/* Unwrap any unmergeable values. */ |
| 4811 |
lhs = lhs->unwrap_any_unmergeable (); |
| 4812 |
rhs = rhs->unwrap_any_unmergeable (); |
| 4813 |
|
| 4814 |
if (lhs == rhs) |
| 4815 |
{ |
| 4816 |
/* If we have the same svalue, then we have equality |
| 4817 |
(apart from NaN-handling). |
| 4818 |
TODO: should this definitely be the case for poisoned values? */ |
| 4819 |
/* Poisoned and unknown values are "unknowable". */ |
| 4820 |
if (lhs->get_kind () == SK_POISONED |
| 4821 |
|| lhs->get_kind () == SK_UNKNOWN) |
| 4822 |
return tristate::TS_UNKNOWN; |
| 4823 |
|
| 4824 |
switch (op) |
| 4825 |
{ |
| 4826 |
case EQ_EXPR: |
| 4827 |
case GE_EXPR: |
| 4828 |
case LE_EXPR: |
| 4829 |
return tristate::TS_TRUE; |
| 4830 |
|
| 4831 |
case NE_EXPR: |
| 4832 |
case GT_EXPR: |
| 4833 |
case LT_EXPR: |
| 4834 |
return tristate::TS_FALSE; |
| 4835 |
|
| 4836 |
default: |
| 4837 |
/* For other ops, use the logic below. */ |
| 4838 |
break; |
| 4839 |
} |
| 4840 |
} |
| 4841 |
|
| 4842 |
/* If we have a pair of region_svalues, compare them. */ |
| 4843 |
if (const region_svalue *lhs_ptr = lhs->dyn_cast_region_svalue ()) |
| 4844 |
if (const region_svalue *rhs_ptr = rhs->dyn_cast_region_svalue ()) |
| 4845 |
{ |
| 4846 |
tristate res = region_svalue::eval_condition (lhs_ptr, op, rhs_ptr); |
| 4847 |
if (res.is_known ()) |
| 4848 |
return res; |
| 4849 |
/* Otherwise, only known through constraints. */ |
| 4850 |
} |
| 4851 |
|
| 4852 |
if (const constant_svalue *cst_lhs = lhs->dyn_cast_constant_svalue ()) |
| 4853 |
{ |
| 4854 |
/* If we have a pair of constants, compare them. */ |
| 4855 |
if (const constant_svalue *cst_rhs = rhs->dyn_cast_constant_svalue ()) |
| 4856 |
return constant_svalue::eval_condition (cst_lhs, op, cst_rhs); |
| 4857 |
else |
| 4858 |
{ |
| 4859 |
/* When we have one constant, put it on the RHS. */ |
| 4860 |
std::swap (lhs, rhs); |
| 4861 |
op = swap_tree_comparison (op); |
| 4862 |
} |
| 4863 |
} |
| 4864 |
gcc_assert (lhs->get_kind () != SK_CONSTANT); |
| 4865 |
|
| 4866 |
/* Handle comparison against zero. */ |
| 4867 |
if (const constant_svalue *cst_rhs = rhs->dyn_cast_constant_svalue ()) |
| 4868 |
if (zerop (cst_rhs->get_constant ())) |
| 4869 |
{ |
| 4870 |
if (const region_svalue *ptr = lhs->dyn_cast_region_svalue ()) |
| 4871 |
{ |
| 4872 |
/* A region_svalue is a non-NULL pointer, except in certain |
| 4873 |
special cases (see the comment for region::non_null_p). */ |
| 4874 |
const region *pointee = ptr->get_pointee (); |
| 4875 |
if (pointee->non_null_p ()) |
| 4876 |
{ |
| 4877 |
switch (op) |
| 4878 |
{ |
| 4879 |
default: |
| 4880 |
gcc_unreachable (); |
| 4881 |
|
| 4882 |
case EQ_EXPR: |
| 4883 |
case GE_EXPR: |
| 4884 |
case LE_EXPR: |
| 4885 |
return tristate::TS_FALSE; |
| 4886 |
|
| 4887 |
case NE_EXPR: |
| 4888 |
case GT_EXPR: |
| 4889 |
case LT_EXPR: |
| 4890 |
return tristate::TS_TRUE; |
| 4891 |
} |
| 4892 |
} |
| 4893 |
} |
| 4894 |
else if (const binop_svalue *binop = lhs->dyn_cast_binop_svalue ()) |
| 4895 |
{ |
| 4896 |
/* Treat offsets from a non-NULL pointer as being non-NULL. This |
| 4897 |
isn't strictly true, in that eventually ptr++ will wrap |
| 4898 |
around and be NULL, but it won't occur in practise and thus |
| 4899 |
can be used to suppress effectively false positives that we |
| 4900 |
shouldn't warn for. */ |
| 4901 |
if (binop->get_op () == POINTER_PLUS_EXPR) |
| 4902 |
{ |
| 4903 |
tristate lhs_ts = eval_condition (binop->get_arg0 (), op, rhs); |
| 4904 |
if (lhs_ts.is_known ()) |
| 4905 |
return lhs_ts; |
| 4906 |
} |
| 4907 |
} |
| 4908 |
else if (const unaryop_svalue *unaryop |
| 4909 |
= lhs->dyn_cast_unaryop_svalue ()) |
| 4910 |
{ |
| 4911 |
if (unaryop->get_op () == NEGATE_EXPR) |
| 4912 |
{ |
| 4913 |
/* e.g. "-X <= 0" is equivalent to X >= 0". */ |
| 4914 |
tristate lhs_ts = eval_condition (unaryop->get_arg (), |
| 4915 |
swap_tree_comparison (op), |
| 4916 |
rhs); |
| 4917 |
if (lhs_ts.is_known ()) |
| 4918 |
return lhs_ts; |
| 4919 |
} |
| 4920 |
} |
| 4921 |
} |
| 4922 |
|
| 4923 |
/* Handle rejection of equality for comparisons of the initial values of |
| 4924 |
"external" values (such as params) with the address of locals. */ |
| 4925 |
if (const initial_svalue *init_lhs = lhs->dyn_cast_initial_svalue ()) |
| 4926 |
if (const region_svalue *rhs_ptr = rhs->dyn_cast_region_svalue ()) |
| 4927 |
{ |
| 4928 |
tristate res = compare_initial_and_pointer (init_lhs, rhs_ptr); |
| 4929 |
if (res.is_known ()) |
| 4930 |
return res; |
| 4931 |
} |
| 4932 |
if (const initial_svalue *init_rhs = rhs->dyn_cast_initial_svalue ()) |
| 4933 |
if (const region_svalue *lhs_ptr = lhs->dyn_cast_region_svalue ()) |
| 4934 |
{ |
| 4935 |
tristate res = compare_initial_and_pointer (init_rhs, lhs_ptr); |
| 4936 |
if (res.is_known ()) |
| 4937 |
return res; |
| 4938 |
} |
| 4939 |
|
| 4940 |
if (const widening_svalue *widen_lhs = lhs->dyn_cast_widening_svalue ()) |
| 4941 |
if (tree rhs_cst = rhs->maybe_get_constant ()) |
| 4942 |
{ |
| 4943 |
tristate res = widen_lhs->eval_condition_without_cm (op, rhs_cst); |
| 4944 |
if (res.is_known ()) |
| 4945 |
return res; |
| 4946 |
} |
| 4947 |
|
| 4948 |
/* Handle comparisons between two svalues with more than one operand. */ |
| 4949 |
if (const binop_svalue *binop = lhs->dyn_cast_binop_svalue ()) |
| 4950 |
{ |
| 4951 |
switch (op) |
| 4952 |
{ |
| 4953 |
default: |
| 4954 |
break; |
| 4955 |
case EQ_EXPR: |
| 4956 |
{ |
| 4957 |
/* TODO: binops can be equal even if they are not structurally |
| 4958 |
equal in case of commutative operators. */ |
| 4959 |
tristate res = structural_equality (lhs, rhs); |
| 4960 |
if (res.is_true ()) |
| 4961 |
return res; |
| 4962 |
} |
| 4963 |
break; |
| 4964 |
case LE_EXPR: |
| 4965 |
{ |
| 4966 |
tristate res = structural_equality (lhs, rhs); |
| 4967 |
if (res.is_true ()) |
| 4968 |
return res; |
| 4969 |
} |
| 4970 |
break; |
| 4971 |
case GE_EXPR: |
| 4972 |
{ |
| 4973 |
tristate res = structural_equality (lhs, rhs); |
| 4974 |
if (res.is_true ()) |
| 4975 |
return res; |
| 4976 |
res = symbolic_greater_than (binop, rhs); |
| 4977 |
if (res.is_true ()) |
| 4978 |
return res; |
| 4979 |
} |
| 4980 |
break; |
| 4981 |
case GT_EXPR: |
| 4982 |
{ |
| 4983 |
tristate res = symbolic_greater_than (binop, rhs); |
| 4984 |
if (res.is_true ()) |
| 4985 |
return res; |
| 4986 |
} |
| 4987 |
break; |
| 4988 |
} |
| 4989 |
} |
| 4990 |
|
| 4991 |
/* Attempt to unwrap cast if there is one, and the types match. */ |
| 4992 |
tree lhs_type = lhs->get_type (); |
| 4993 |
tree rhs_type = rhs->get_type (); |
| 4994 |
if (lhs_type && rhs_type) |
| 4995 |
{ |
| 4996 |
const unaryop_svalue *lhs_un_op = dyn_cast <const unaryop_svalue *> (lhs); |
| 4997 |
const unaryop_svalue *rhs_un_op = dyn_cast <const unaryop_svalue *> (rhs); |
| 4998 |
if (lhs_un_op && CONVERT_EXPR_CODE_P (lhs_un_op->get_op ()) |
| 4999 |
&& rhs_un_op && CONVERT_EXPR_CODE_P (rhs_un_op->get_op ()) |
| 5000 |
&& lhs_type == rhs_type) |
| 5001 |
{ |
| 5002 |
tristate res = eval_condition (lhs_un_op->get_arg (), |
| 5003 |
op, |
| 5004 |
rhs_un_op->get_arg ()); |
| 5005 |
if (res.is_known ()) |
| 5006 |
return res; |
| 5007 |
} |
| 5008 |
else if (lhs_un_op && CONVERT_EXPR_CODE_P (lhs_un_op->get_op ()) |
| 5009 |
&& lhs_type == rhs_type) |
| 5010 |
{ |
| 5011 |
tristate res = eval_condition (lhs_un_op->get_arg (), op, rhs); |
| 5012 |
if (res.is_known ()) |
| 5013 |
return res; |
| 5014 |
} |
| 5015 |
else if (rhs_un_op && CONVERT_EXPR_CODE_P (rhs_un_op->get_op ()) |
| 5016 |
&& lhs_type == rhs_type) |
| 5017 |
{ |
| 5018 |
tristate res = eval_condition (lhs, op, rhs_un_op->get_arg ()); |
| 5019 |
if (res.is_known ()) |
| 5020 |
return res; |
| 5021 |
} |
| 5022 |
} |
| 5023 |
|
| 5024 |
/* Otherwise, try constraints. |
| 5025 |
Cast to const to ensure we don't change the constraint_manager as we |
| 5026 |
do this (e.g. by creating equivalence classes). */ |
| 5027 |
const constraint_manager *constraints = m_constraints; |
| 5028 |
return constraints->eval_condition (lhs, op, rhs); |
| 5029 |
} |
| 5030 |
|
| 5031 |
/* Subroutine of region_model::eval_condition, for rejecting |
| 5032 |
equality of INIT_VAL(PARM) with &LOCAL. */ |
| 5033 |
|
| 5034 |
tristate |
| 5035 |
region_model::compare_initial_and_pointer (const initial_svalue *init, |
| 5036 |
const region_svalue *ptr) const |
| 5037 |
{ |
| 5038 |
const region *pointee = ptr->get_pointee (); |
| 5039 |
|
| 5040 |
/* If we have a pointer to something within a stack frame, it can't be the |
| 5041 |
initial value of a param. */ |
| 5042 |
if (pointee->maybe_get_frame_region ()) |
| 5043 |
if (init->initial_value_of_param_p ()) |
| 5044 |
return tristate::TS_FALSE; |
| 5045 |
|
| 5046 |
return tristate::TS_UNKNOWN; |
| 5047 |
} |
| 5048 |
|
| 5049 |
/* Return true if SVAL is definitely positive. */ |
| 5050 |
|
| 5051 |
static bool |
| 5052 |
is_positive_svalue (const svalue *sval) |
| 5053 |
{ |
| 5054 |
if (tree cst = sval->maybe_get_constant ()) |
| 5055 |
return !zerop (cst) && get_range_pos_neg (cst) == 1; |
| 5056 |
tree type = sval->get_type (); |
| 5057 |
if (!type) |
| 5058 |
return false; |
| 5059 |
/* Consider a binary operation size_t + int. The analyzer wraps the int in |
| 5060 |
an unaryop_svalue, converting it to a size_t, but in the dynamic execution |
| 5061 |
the result is smaller than the first operand. Thus, we have to look if |
| 5062 |
the argument of the unaryop_svalue is also positive. */ |
| 5063 |
if (const unaryop_svalue *un_op = dyn_cast <const unaryop_svalue *> (sval)) |
| 5064 |
return CONVERT_EXPR_CODE_P (un_op->get_op ()) && TYPE_UNSIGNED (type) |
| 5065 |
&& is_positive_svalue (un_op->get_arg ()); |
| 5066 |
return TYPE_UNSIGNED (type); |
| 5067 |
} |
| 5068 |
|
| 5069 |
/* Return true if A is definitely larger than B. |
| 5070 |
|
| 5071 |
Limitation: does not account for integer overflows and does not try to |
| 5072 |
return false, so it can not be used negated. */ |
| 5073 |
|
| 5074 |
tristate |
| 5075 |
region_model::symbolic_greater_than (const binop_svalue *bin_a, |
| 5076 |
const svalue *b) const |
| 5077 |
{ |
| 5078 |
if (bin_a->get_op () == PLUS_EXPR || bin_a->get_op () == MULT_EXPR) |
| 5079 |
{ |
| 5080 |
/* Eliminate the right-hand side of both svalues. */ |
| 5081 |
if (const binop_svalue *bin_b = dyn_cast <const binop_svalue *> (b)) |
| 5082 |
if (bin_a->get_op () == bin_b->get_op () |
| 5083 |
&& eval_condition (bin_a->get_arg1 (), |
| 5084 |
GT_EXPR, |
| 5085 |
bin_b->get_arg1 ()).is_true () |
| 5086 |
&& eval_condition (bin_a->get_arg0 (), |
| 5087 |
GE_EXPR, |
| 5088 |
bin_b->get_arg0 ()).is_true ()) |
| 5089 |
return tristate (tristate::TS_TRUE); |
| 5090 |
|
| 5091 |
/* Otherwise, try to remove a positive offset or factor from BIN_A. */ |
| 5092 |
if (is_positive_svalue (bin_a->get_arg1 ()) |
| 5093 |
&& eval_condition (bin_a->get_arg0 (), |
| 5094 |
GE_EXPR, b).is_true ()) |
| 5095 |
return tristate (tristate::TS_TRUE); |
| 5096 |
} |
| 5097 |
return tristate::unknown (); |
| 5098 |
} |
| 5099 |
|
| 5100 |
/* Return true if A and B are equal structurally. |
| 5101 |
|
| 5102 |
Structural equality means that A and B are equal if the svalues A and B have |
| 5103 |
the same nodes at the same positions in the tree and the leafs are equal. |
| 5104 |
Equality for conjured_svalues and initial_svalues is determined by comparing |
| 5105 |
the pointers while constants are compared by value. That behavior is useful |
| 5106 |
to check for binaryop_svlaues that evaluate to the same concrete value but |
| 5107 |
might use one operand with a different type but the same constant value. |
| 5108 |
|
| 5109 |
For example, |
| 5110 |
binop_svalue (mult_expr, |
| 5111 |
initial_svalue (‘size_t’, decl_region (..., 'some_var')), |
| 5112 |
constant_svalue (‘size_t’, 4)) |
| 5113 |
and |
| 5114 |
binop_svalue (mult_expr, |
| 5115 |
initial_svalue (‘size_t’, decl_region (..., 'some_var'), |
| 5116 |
constant_svalue (‘sizetype’, 4)) |
| 5117 |
are structurally equal. A concrete C code example, where this occurs, can |
| 5118 |
be found in test7 of out-of-bounds-5.c. */ |
| 5119 |
|
| 5120 |
tristate |
| 5121 |
region_model::structural_equality (const svalue *a, const svalue *b) const |
| 5122 |
{ |
| 5123 |
/* If A and B are referentially equal, they are also structurally equal. */ |
| 5124 |
if (a == b) |
| 5125 |
return tristate (tristate::TS_TRUE); |
| 5126 |
|
| 5127 |
switch (a->get_kind ()) |
| 5128 |
{ |
| 5129 |
default: |
| 5130 |
return tristate::unknown (); |
| 5131 |
/* SK_CONJURED and SK_INITIAL are already handled |
| 5132 |
by the referential equality above. */ |
| 5133 |
case SK_CONSTANT: |
| 5134 |
{ |
| 5135 |
tree a_cst = a->maybe_get_constant (); |
| 5136 |
tree b_cst = b->maybe_get_constant (); |
| 5137 |
if (a_cst && b_cst) |
| 5138 |
return tristate (tree_int_cst_equal (a_cst, b_cst)); |
| 5139 |
} |
| 5140 |
return tristate (tristate::TS_FALSE); |
| 5141 |
case SK_UNARYOP: |
| 5142 |
{ |
| 5143 |
const unaryop_svalue *un_a = as_a <const unaryop_svalue *> (a); |
| 5144 |
if (const unaryop_svalue *un_b = dyn_cast <const unaryop_svalue *> (b)) |
| 5145 |
return tristate (pending_diagnostic::same_tree_p (un_a->get_type (), |
| 5146 |
un_b->get_type ()) |
| 5147 |
&& un_a->get_op () == un_b->get_op () |
| 5148 |
&& structural_equality (un_a->get_arg (), |
| 5149 |
un_b->get_arg ())); |
| 5150 |
} |
| 5151 |
return tristate (tristate::TS_FALSE); |
| 5152 |
case SK_BINOP: |
| 5153 |
{ |
| 5154 |
const binop_svalue *bin_a = as_a <const binop_svalue *> (a); |
| 5155 |
if (const binop_svalue *bin_b = dyn_cast <const binop_svalue *> (b)) |
| 5156 |
return tristate (bin_a->get_op () == bin_b->get_op () |
| 5157 |
&& structural_equality (bin_a->get_arg0 (), |
| 5158 |
bin_b->get_arg0 ()) |
| 5159 |
&& structural_equality (bin_a->get_arg1 (), |
| 5160 |
bin_b->get_arg1 ())); |
| 5161 |
} |
| 5162 |
return tristate (tristate::TS_FALSE); |
| 5163 |
} |
| 5164 |
} |
| 5165 |
|
| 5166 |
/* Handle various constraints of the form: |
| 5167 |
LHS: ((bool)INNER_LHS INNER_OP INNER_RHS)) |
| 5168 |
OP : == or != |
| 5169 |
RHS: zero |
| 5170 |
and (with a cast): |
| 5171 |
LHS: CAST([long]int, ((bool)INNER_LHS INNER_OP INNER_RHS)) |
| 5172 |
OP : == or != |
| 5173 |
RHS: zero |
| 5174 |
by adding constraints for INNER_LHS INNEROP INNER_RHS. |
| 5175 |
|
| 5176 |
Return true if this function can fully handle the constraint; if |
| 5177 |
so, add the implied constraint(s) and write true to *OUT if they |
| 5178 |
are consistent with existing constraints, or write false to *OUT |
| 5179 |
if they contradicts existing constraints. |
| 5180 |
|
| 5181 |
Return false for cases that this function doeesn't know how to handle. |
| 5182 |
|
| 5183 |
For example, if we're checking a stored conditional, we'll have |
| 5184 |
something like: |
| 5185 |
LHS: CAST(long int, (&HEAP_ALLOCATED_REGION(8)!=(int *)0B)) |
| 5186 |
OP : NE_EXPR |
| 5187 |
RHS: zero |
| 5188 |
which this function can turn into an add_constraint of: |
| 5189 |
(&HEAP_ALLOCATED_REGION(8) != (int *)0B) |
| 5190 |
|
| 5191 |
Similarly, optimized && and || conditionals lead to e.g. |
| 5192 |
if (p && q) |
| 5193 |
becoming gimple like this: |
| 5194 |
_1 = p_6 == 0B; |
| 5195 |
_2 = q_8 == 0B |
| 5196 |
_3 = _1 | _2 |
| 5197 |
On the "_3 is false" branch we can have constraints of the form: |
| 5198 |
((&HEAP_ALLOCATED_REGION(8)!=(int *)0B) |
| 5199 |
| (&HEAP_ALLOCATED_REGION(10)!=(int *)0B)) |
| 5200 |
== 0 |
| 5201 |
which implies that both _1 and _2 are false, |
| 5202 |
which this function can turn into a pair of add_constraints of |
| 5203 |
(&HEAP_ALLOCATED_REGION(8)!=(int *)0B) |
| 5204 |
and: |
| 5205 |
(&HEAP_ALLOCATED_REGION(10)!=(int *)0B). */ |
| 5206 |
|
| 5207 |
bool |
| 5208 |
region_model::add_constraints_from_binop (const svalue *outer_lhs, |
| 5209 |
enum tree_code outer_op, |
| 5210 |
const svalue *outer_rhs, |
| 5211 |
bool *out, |
| 5212 |
region_model_context *ctxt) |
| 5213 |
{ |
| 5214 |
while (const svalue *cast = outer_lhs->maybe_undo_cast ()) |
| 5215 |
outer_lhs = cast; |
| 5216 |
const binop_svalue *binop_sval = outer_lhs->dyn_cast_binop_svalue (); |
| 5217 |
if (!binop_sval) |
| 5218 |
return false; |
| 5219 |
if (!outer_rhs->all_zeroes_p ()) |
| 5220 |
return false; |
| 5221 |
|
| 5222 |
const svalue *inner_lhs = binop_sval->get_arg0 (); |
| 5223 |
enum tree_code inner_op = binop_sval->get_op (); |
| 5224 |
const svalue *inner_rhs = binop_sval->get_arg1 (); |
| 5225 |
|
| 5226 |
if (outer_op != NE_EXPR && outer_op != EQ_EXPR) |
| 5227 |
return false; |
| 5228 |
|
| 5229 |
/* We have either |
| 5230 |
- "OUTER_LHS != false" (i.e. OUTER is true), or |
| 5231 |
- "OUTER_LHS == false" (i.e. OUTER is false). */ |
| 5232 |
bool is_true = outer_op == NE_EXPR; |
| 5233 |
|
| 5234 |
switch (inner_op) |
| 5235 |
{ |
| 5236 |
default: |
| 5237 |
return false; |
| 5238 |
|
| 5239 |
case EQ_EXPR: |
| 5240 |
case NE_EXPR: |
| 5241 |
case GE_EXPR: |
| 5242 |
case GT_EXPR: |
| 5243 |
case LE_EXPR: |
| 5244 |
case LT_EXPR: |
| 5245 |
{ |
| 5246 |
/* ...and "(inner_lhs OP inner_rhs) == 0" |
| 5247 |
then (inner_lhs OP inner_rhs) must have the same |
| 5248 |
logical value as LHS. */ |
| 5249 |
if (!is_true) |
| 5250 |
inner_op = invert_tree_comparison (inner_op, false /* honor_nans */); |
| 5251 |
*out = add_constraint (inner_lhs, inner_op, inner_rhs, ctxt); |
| 5252 |
return true; |
| 5253 |
} |
| 5254 |
break; |
| 5255 |
|
| 5256 |
case BIT_AND_EXPR: |
| 5257 |
if (is_true) |
| 5258 |
{ |
| 5259 |
/* ...and "(inner_lhs & inner_rhs) != 0" |
| 5260 |
then both inner_lhs and inner_rhs must be true. */ |
| 5261 |
const svalue *false_sval |
| 5262 |
= m_mgr->get_or_create_constant_svalue (boolean_false_node); |
| 5263 |
bool sat1 = add_constraint (inner_lhs, NE_EXPR, false_sval, ctxt); |
| 5264 |
bool sat2 = add_constraint (inner_rhs, NE_EXPR, false_sval, ctxt); |
| 5265 |
*out = sat1 && sat2; |
| 5266 |
return true; |
| 5267 |
} |
| 5268 |
return false; |
| 5269 |
|
| 5270 |
case BIT_IOR_EXPR: |
| 5271 |
if (!is_true) |
| 5272 |
{ |
| 5273 |
/* ...and "(inner_lhs | inner_rhs) == 0" |
| 5274 |
i.e. "(inner_lhs | inner_rhs)" is false |
| 5275 |
then both inner_lhs and inner_rhs must be false. */ |
| 5276 |
const svalue *false_sval |
| 5277 |
= m_mgr->get_or_create_constant_svalue (boolean_false_node); |
| 5278 |
bool sat1 = add_constraint (inner_lhs, EQ_EXPR, false_sval, ctxt); |
| 5279 |
bool sat2 = add_constraint (inner_rhs, EQ_EXPR, false_sval, ctxt); |
| 5280 |
*out = sat1 && sat2; |
| 5281 |
return true; |
| 5282 |
} |
| 5283 |
return false; |
| 5284 |
} |
| 5285 |
} |
| 5286 |
|
| 5287 |
/* Attempt to add the constraint "LHS OP RHS" to this region_model. |
| 5288 |
If it is consistent with existing constraints, add it, and return true. |
| 5289 |
Return false if it contradicts existing constraints. |
| 5290 |
Use CTXT for reporting any diagnostics associated with the accesses. */ |
| 5291 |
|
| 5292 |
bool |
| 5293 |
region_model::add_constraint (tree lhs, enum tree_code op, tree rhs, |
| 5294 |
region_model_context *ctxt) |
| 5295 |
{ |
| 5296 |
/* For now, make no attempt to capture constraints on floating-point |
| 5297 |
values. */ |
| 5298 |
if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs))) |
| 5299 |
return true; |
| 5300 |
|
| 5301 |
const svalue *lhs_sval = get_rvalue (lhs, ctxt); |
| 5302 |
const svalue *rhs_sval = get_rvalue (rhs, ctxt); |
| 5303 |
|
| 5304 |
return add_constraint (lhs_sval, op, rhs_sval, ctxt); |
| 5305 |
} |
| 5306 |
|
| 5307 |
static bool |
| 5308 |
unusable_in_infinite_loop_constraint_p (const svalue *sval) |
| 5309 |
{ |
| 5310 |
if (sval->get_kind () == SK_WIDENING) |
| 5311 |
return true; |
| 5312 |
return false; |
| 5313 |
} |
| 5314 |
|
| 5315 |
/* Attempt to add the constraint "LHS OP RHS" to this region_model. |
| 5316 |
If it is consistent with existing constraints, add it, and return true. |
| 5317 |
Return false if it contradicts existing constraints. |
| 5318 |
Use CTXT for reporting any diagnostics associated with the accesses. */ |
| 5319 |
|
| 5320 |
bool |
| 5321 |
region_model::add_constraint (const svalue *lhs, |
| 5322 |
enum tree_code op, |
| 5323 |
const svalue *rhs, |
| 5324 |
region_model_context *ctxt) |
| 5325 |
{ |
| 5326 |
const bool checking_for_infinite_loop |
| 5327 |
= ctxt ? ctxt->checking_for_infinite_loop_p () : false; |
| 5328 |
|
| 5329 |
if (checking_for_infinite_loop) |
| 5330 |
{ |
| 5331 |
if (unusable_in_infinite_loop_constraint_p (lhs) |
| 5332 |
|| unusable_in_infinite_loop_constraint_p (rhs)) |
| 5333 |
{ |
| 5334 |
gcc_assert (ctxt); |
| 5335 |
ctxt->on_unusable_in_infinite_loop (); |
| 5336 |
return false; |
| 5337 |
} |
| 5338 |
} |
| 5339 |
|
| 5340 |
tristate t_cond = eval_condition (lhs, op, rhs); |
| 5341 |
|
| 5342 |
/* If we already have the condition, do nothing. */ |
| 5343 |
if (t_cond.is_true ()) |
| 5344 |
return true; |
| 5345 |
|
| 5346 |
/* Reject a constraint that would contradict existing knowledge, as |
| 5347 |
unsatisfiable. */ |
| 5348 |
if (t_cond.is_false ()) |
| 5349 |
return false; |
| 5350 |
|
| 5351 |
if (checking_for_infinite_loop) |
| 5352 |
{ |
| 5353 |
/* Here, we don't have a definite true/false value, so bail out |
| 5354 |
when checking for infinite loops. */ |
| 5355 |
gcc_assert (ctxt); |
| 5356 |
ctxt->on_unusable_in_infinite_loop (); |
| 5357 |
return false; |
| 5358 |
} |
| 5359 |
|
| 5360 |
bool out; |
| 5361 |
if (add_constraints_from_binop (lhs, op, rhs, &out, ctxt)) |
| 5362 |
return out; |
| 5363 |
|
| 5364 |
/* Attempt to store the constraint. */ |
| 5365 |
if (!m_constraints->add_constraint (lhs, op, rhs)) |
| 5366 |
return false; |
| 5367 |
|
| 5368 |
/* Notify the context, if any. This exists so that the state machines |
| 5369 |
in a program_state can be notified about the condition, and so can |
| 5370 |
set sm-state for e.g. unchecked->checked, both for cfg-edges, and |
| 5371 |
when synthesizing constraints as above. */ |
| 5372 |
if (ctxt) |
| 5373 |
ctxt->on_condition (lhs, op, rhs); |
| 5374 |
|
| 5375 |
/* If we have ®ION == NULL, then drop dynamic extents for REGION (for |
| 5376 |
the case where REGION is heap-allocated and thus could be NULL). */ |
| 5377 |
if (tree rhs_cst = rhs->maybe_get_constant ()) |
| 5378 |
if (op == EQ_EXPR && zerop (rhs_cst)) |
| 5379 |
if (const region_svalue *region_sval = lhs->dyn_cast_region_svalue ()) |
| 5380 |
unset_dynamic_extents (region_sval->get_pointee ()); |
| 5381 |
|
| 5382 |
return true; |
| 5383 |
} |
| 5384 |
|
| 5385 |
/* As above, but when returning false, if OUT is non-NULL, write a |
| 5386 |
new rejected_constraint to *OUT. */ |
| 5387 |
|
| 5388 |
bool |
| 5389 |
region_model::add_constraint (tree lhs, enum tree_code op, tree rhs, |
| 5390 |
region_model_context *ctxt, |
| 5391 |
std::unique_ptr<rejected_constraint> *out) |
| 5392 |
{ |
| 5393 |
bool sat = add_constraint (lhs, op, rhs, ctxt); |
| 5394 |
if (!sat && out) |
| 5395 |
*out = make_unique <rejected_op_constraint> (*this, lhs, op, rhs); |
| 5396 |
return sat; |
| 5397 |
} |
| 5398 |
|
| 5399 |
/* Determine what is known about the condition "LHS OP RHS" within |
| 5400 |
this model. |
| 5401 |
Use CTXT for reporting any diagnostics associated with the accesses. */ |
| 5402 |
|
| 5403 |
tristate |
| 5404 |
region_model::eval_condition (tree lhs, |
| 5405 |
enum tree_code op, |
| 5406 |
tree rhs, |
| 5407 |
region_model_context *ctxt) const |
| 5408 |
{ |
| 5409 |
/* For now, make no attempt to model constraints on floating-point |
| 5410 |
values. */ |
| 5411 |
if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs))) |
| 5412 |
return tristate::unknown (); |
| 5413 |
|
| 5414 |
return eval_condition (get_rvalue (lhs, ctxt), op, get_rvalue (rhs, ctxt)); |
| 5415 |
} |
| 5416 |
|
| 5417 |
/* Implementation of region_model::get_representative_path_var. |
| 5418 |
Attempt to return a path_var that represents SVAL, or return NULL_TREE. |
| 5419 |
Use VISITED to prevent infinite mutual recursion with the overload for |
| 5420 |
regions. */ |
| 5421 |
|
| 5422 |
path_var |
| 5423 |
region_model::get_representative_path_var_1 (const svalue *sval, |
| 5424 |
svalue_set *visited, |
| 5425 |
logger *logger) const |
| 5426 |
{ |
| 5427 |
gcc_assert (sval); |
| 5428 |
|
| 5429 |
/* Prevent infinite recursion. */ |
| 5430 |
if (visited->contains (sval)) |
| 5431 |
{ |
| 5432 |
if (sval->get_kind () == SK_CONSTANT) |
| 5433 |
return path_var (sval->maybe_get_constant (), 0); |
| 5434 |
else |
| 5435 |
return path_var (NULL_TREE, 0); |
| 5436 |
} |
| 5437 |
visited->add (sval); |
| 5438 |
|
| 5439 |
/* Handle casts by recursion into get_representative_path_var. */ |
| 5440 |
if (const svalue *cast_sval = sval->maybe_undo_cast ()) |
| 5441 |
{ |
| 5442 |
path_var result = get_representative_path_var (cast_sval, visited, |
| 5443 |
logger); |
| 5444 |
tree orig_type = sval->get_type (); |
| 5445 |
/* If necessary, wrap the result in a cast. */ |
| 5446 |
if (result.m_tree && orig_type) |
| 5447 |
result.m_tree = build1 (NOP_EXPR, orig_type, result.m_tree); |
| 5448 |
return result; |
| 5449 |
} |
| 5450 |
|
| 5451 |
auto_vec<path_var> pvs; |
| 5452 |
m_store.get_representative_path_vars (this, visited, sval, logger, &pvs); |
| 5453 |
|
| 5454 |
if (tree cst = sval->maybe_get_constant ()) |
| 5455 |
pvs.safe_push (path_var (cst, 0)); |
| 5456 |
|
| 5457 |
/* Handle string literals and various other pointers. */ |
| 5458 |
if (const region_svalue *ptr_sval = sval->dyn_cast_region_svalue ()) |
| 5459 |
{ |
| 5460 |
const region *reg = ptr_sval->get_pointee (); |
| 5461 |
if (path_var pv = get_representative_path_var (reg, visited, logger)) |
| 5462 |
return path_var (build1 (ADDR_EXPR, |
| 5463 |
sval->get_type (), |
| 5464 |
pv.m_tree), |
| 5465 |
pv.m_stack_depth); |
| 5466 |
} |
| 5467 |
|
| 5468 |
/* If we have a sub_svalue, look for ways to represent the parent. */ |
| 5469 |
if (const sub_svalue *sub_sval = sval->dyn_cast_sub_svalue ()) |
| 5470 |
{ |
| 5471 |
const svalue *parent_sval = sub_sval->get_parent (); |
| 5472 |
const region *subreg = sub_sval->get_subregion (); |
| 5473 |
if (path_var parent_pv |
| 5474 |
= get_representative_path_var (parent_sval, visited, logger)) |
| 5475 |
if (const field_region *field_reg = subreg->dyn_cast_field_region ()) |
| 5476 |
return path_var (build3 (COMPONENT_REF, |
| 5477 |
sval->get_type (), |
| 5478 |
parent_pv.m_tree, |
| 5479 |
field_reg->get_field (), |
| 5480 |
NULL_TREE), |
| 5481 |
parent_pv.m_stack_depth); |
| 5482 |
} |
| 5483 |
|
| 5484 |
/* Handle binops. */ |
| 5485 |
if (const binop_svalue *binop_sval = sval->dyn_cast_binop_svalue ()) |
| 5486 |
if (path_var lhs_pv |
| 5487 |
= get_representative_path_var (binop_sval->get_arg0 (), visited, |
| 5488 |
logger)) |
| 5489 |
if (path_var rhs_pv |
| 5490 |
= get_representative_path_var (binop_sval->get_arg1 (), visited, |
| 5491 |
logger)) |
| 5492 |
return path_var (build2 (binop_sval->get_op (), |
| 5493 |
sval->get_type (), |
| 5494 |
lhs_pv.m_tree, rhs_pv.m_tree), |
| 5495 |
lhs_pv.m_stack_depth); |
| 5496 |
|
| 5497 |
if (pvs.length () < 1) |
| 5498 |
return path_var (NULL_TREE, 0); |
| 5499 |
|
| 5500 |
pvs.qsort (readability_comparator); |
| 5501 |
return pvs[0]; |
| 5502 |
} |
| 5503 |
|
| 5504 |
/* Attempt to return a path_var that represents SVAL, or return NULL_TREE. |
| 5505 |
Use VISITED to prevent infinite mutual recursion with the overload for |
| 5506 |
regions |
| 5507 |
|
| 5508 |
This function defers to get_representative_path_var_1 to do the work; |
| 5509 |
it adds verification that get_representative_path_var_1 returned a tree |
| 5510 |
of the correct type. */ |
| 5511 |
|
| 5512 |
path_var |
| 5513 |
region_model::get_representative_path_var (const svalue *sval, |
| 5514 |
svalue_set *visited, |
| 5515 |
logger *logger) const |
| 5516 |
{ |
| 5517 |
if (sval == NULL) |
| 5518 |
return path_var (NULL_TREE, 0); |
| 5519 |
|
| 5520 |
LOG_SCOPE (logger); |
| 5521 |
if (logger) |
| 5522 |
{ |
| 5523 |
logger->start_log_line (); |
| 5524 |
logger->log_partial ("sval: "); |
| 5525 |
sval->dump_to_pp (logger->get_printer (), true); |
| 5526 |
logger->end_log_line (); |
| 5527 |
} |
| 5528 |
|
| 5529 |
tree orig_type = sval->get_type (); |
| 5530 |
|
| 5531 |
path_var result = get_representative_path_var_1 (sval, visited, logger); |
| 5532 |
|
| 5533 |
/* Verify that the result has the same type as SVAL, if any. */ |
| 5534 |
if (result.m_tree && orig_type) |
| 5535 |
gcc_assert (TREE_TYPE (result.m_tree) == orig_type); |
| 5536 |
|
| 5537 |
if (logger) |
| 5538 |
{ |
| 5539 |
logger->start_log_line (); |
| 5540 |
logger->log_partial ("sval: "); |
| 5541 |
sval->dump_to_pp (logger->get_printer (), true); |
| 5542 |
logger->end_log_line (); |
| 5543 |
|
| 5544 |
if (result.m_tree) |
| 5545 |
logger->log ("tree: %qE", result.m_tree); |
| 5546 |
else |
| 5547 |
logger->log ("tree: NULL"); |
| 5548 |
} |
| 5549 |
|
| 5550 |
return result; |
| 5551 |
} |
| 5552 |
|
| 5553 |
/* Attempt to return a tree that represents SVAL, or return NULL_TREE. |
| 5554 |
|
| 5555 |
Strip off any top-level cast, to avoid messages like |
| 5556 |
double-free of '(void *)ptr' |
| 5557 |
from analyzer diagnostics. */ |
| 5558 |
|
| 5559 |
tree |
| 5560 |
region_model::get_representative_tree (const svalue *sval, logger *logger) const |
| 5561 |
{ |
| 5562 |
svalue_set visited; |
| 5563 |
tree expr = get_representative_path_var (sval, &visited, logger).m_tree; |
| 5564 |
|
| 5565 |
/* Strip off any top-level cast. */ |
| 5566 |
if (expr && TREE_CODE (expr) == NOP_EXPR) |
| 5567 |
expr = TREE_OPERAND (expr, 0); |
| 5568 |
|
| 5569 |
return fixup_tree_for_diagnostic (expr); |
| 5570 |
} |
| 5571 |
|
| 5572 |
tree |
| 5573 |
region_model::get_representative_tree (const region *reg, logger *logger) const |
| 5574 |
{ |
| 5575 |
svalue_set visited; |
| 5576 |
tree expr = get_representative_path_var (reg, &visited, logger).m_tree; |
| 5577 |
|
| 5578 |
/* Strip off any top-level cast. */ |
| 5579 |
if (expr && TREE_CODE (expr) == NOP_EXPR) |
| 5580 |
expr = TREE_OPERAND (expr, 0); |
| 5581 |
|
| 5582 |
return fixup_tree_for_diagnostic (expr); |
| 5583 |
} |
| 5584 |
|
| 5585 |
/* Implementation of region_model::get_representative_path_var. |
| 5586 |
|
| 5587 |
Attempt to return a path_var that represents REG, or return |
| 5588 |
the NULL path_var. |
| 5589 |
For example, a region for a field of a local would be a path_var |
| 5590 |
wrapping a COMPONENT_REF. |
| 5591 |
Use VISITED to prevent infinite mutual recursion with the overload for |
| 5592 |
svalues. */ |
| 5593 |
|
| 5594 |
path_var |
| 5595 |
region_model::get_representative_path_var_1 (const region *reg, |
| 5596 |
svalue_set *visited, |
| 5597 |
logger *logger) const |
| 5598 |
{ |
| 5599 |
switch (reg->get_kind ()) |
| 5600 |
{ |
| 5601 |
default: |
| 5602 |
gcc_unreachable (); |
| 5603 |
|
| 5604 |
case RK_FRAME: |
| 5605 |
case RK_GLOBALS: |
| 5606 |
case RK_CODE: |
| 5607 |
case RK_HEAP: |
| 5608 |
case RK_STACK: |
| 5609 |
case RK_THREAD_LOCAL: |
| 5610 |
case RK_ROOT: |
| 5611 |
/* Regions that represent memory spaces are not expressible as trees. */ |
| 5612 |
return path_var (NULL_TREE, 0); |
| 5613 |
|
| 5614 |
case RK_FUNCTION: |
| 5615 |
{ |
| 5616 |
const function_region *function_reg |
| 5617 |
= as_a <const function_region *> (reg); |
| 5618 |
return path_var (function_reg->get_fndecl (), 0); |
| 5619 |
} |
| 5620 |
case RK_LABEL: |
| 5621 |
{ |
| 5622 |
const label_region *label_reg = as_a <const label_region *> (reg); |
| 5623 |
return path_var (label_reg->get_label (), 0); |
| 5624 |
} |
| 5625 |
|
| 5626 |
case RK_SYMBOLIC: |
| 5627 |
{ |
| 5628 |
const symbolic_region *symbolic_reg |
| 5629 |
= as_a <const symbolic_region *> (reg); |
| 5630 |
const svalue *pointer = symbolic_reg->get_pointer (); |
| 5631 |
path_var pointer_pv = get_representative_path_var (pointer, visited, |
| 5632 |
logger); |
| 5633 |
if (!pointer_pv) |
| 5634 |
return path_var (NULL_TREE, 0); |
| 5635 |
tree offset = build_int_cst (pointer->get_type (), 0); |
| 5636 |
return path_var (build2 (MEM_REF, |
| 5637 |
reg->get_type (), |
| 5638 |
pointer_pv.m_tree, |
| 5639 |
offset), |
| 5640 |
pointer_pv.m_stack_depth); |
| 5641 |
} |
| 5642 |
case RK_DECL: |
| 5643 |
{ |
| 5644 |
const decl_region *decl_reg = as_a <const decl_region *> (reg); |
| 5645 |
return path_var (decl_reg->get_decl (), decl_reg->get_stack_depth ()); |
| 5646 |
} |
| 5647 |
case RK_FIELD: |
| 5648 |
{ |
| 5649 |
const field_region *field_reg = as_a <const field_region *> (reg); |
| 5650 |
path_var parent_pv |
| 5651 |
= get_representative_path_var (reg->get_parent_region (), visited, |
| 5652 |
logger); |
| 5653 |
if (!parent_pv) |
| 5654 |
return path_var (NULL_TREE, 0); |
| 5655 |
return path_var (build3 (COMPONENT_REF, |
| 5656 |
reg->get_type (), |
| 5657 |
parent_pv.m_tree, |
| 5658 |
field_reg->get_field (), |
| 5659 |
NULL_TREE), |
| 5660 |
parent_pv.m_stack_depth); |
| 5661 |
} |
| 5662 |
|
| 5663 |
case RK_ELEMENT: |
| 5664 |
{ |
| 5665 |
const element_region *element_reg |
| 5666 |
= as_a <const element_region *> (reg); |
| 5667 |
path_var parent_pv |
| 5668 |
= get_representative_path_var (reg->get_parent_region (), visited, |
| 5669 |
logger); |
| 5670 |
if (!parent_pv) |
| 5671 |
return path_var (NULL_TREE, 0); |
| 5672 |
path_var index_pv |
| 5673 |
= get_representative_path_var (element_reg->get_index (), visited, |
| 5674 |
logger); |
| 5675 |
if (!index_pv) |
| 5676 |
return path_var (NULL_TREE, 0); |
| 5677 |
return path_var (build4 (ARRAY_REF, |
| 5678 |
reg->get_type (), |
| 5679 |
parent_pv.m_tree, index_pv.m_tree, |
| 5680 |
NULL_TREE, NULL_TREE), |
| 5681 |
parent_pv.m_stack_depth); |
| 5682 |
} |
| 5683 |
|
| 5684 |
case RK_OFFSET: |
| 5685 |
{ |
| 5686 |
const offset_region *offset_reg |
| 5687 |
= as_a <const offset_region *> (reg); |
| 5688 |
path_var parent_pv |
| 5689 |
= get_representative_path_var (reg->get_parent_region (), visited, |
| 5690 |
logger); |
| 5691 |
if (!parent_pv) |
| 5692 |
return path_var (NULL_TREE, 0); |
| 5693 |
path_var offset_pv |
| 5694 |
= get_representative_path_var (offset_reg->get_byte_offset (), |
| 5695 |
visited, logger); |
| 5696 |
if (!offset_pv || TREE_CODE (offset_pv.m_tree) != INTEGER_CST) |
| 5697 |
return path_var (NULL_TREE, 0); |
| 5698 |
tree addr_parent = build1 (ADDR_EXPR, |
| 5699 |
build_pointer_type (reg->get_type ()), |
| 5700 |
parent_pv.m_tree); |
| 5701 |
tree ptype = build_pointer_type_for_mode (char_type_node, ptr_mode, |
| 5702 |
true); |
| 5703 |
return path_var (build2 (MEM_REF, reg->get_type (), addr_parent, |
| 5704 |
fold_convert (ptype, offset_pv.m_tree)), |
| 5705 |
parent_pv.m_stack_depth); |
| 5706 |
} |
| 5707 |
|
| 5708 |
case RK_SIZED: |
| 5709 |
return path_var (NULL_TREE, 0); |
| 5710 |
|
| 5711 |
case RK_CAST: |
| 5712 |
{ |
| 5713 |
path_var parent_pv |
| 5714 |
= get_representative_path_var (reg->get_parent_region (), visited, |
| 5715 |
logger); |
| 5716 |
if (!parent_pv) |
| 5717 |
return path_var (NULL_TREE, 0); |
| 5718 |
return path_var (build1 (NOP_EXPR, |
| 5719 |
reg->get_type (), |
| 5720 |
parent_pv.m_tree), |
| 5721 |
parent_pv.m_stack_depth); |
| 5722 |
} |
| 5723 |
|
| 5724 |
case RK_HEAP_ALLOCATED: |
| 5725 |
case RK_ALLOCA: |
| 5726 |
/* No good way to express heap-allocated/alloca regions as trees. */ |
| 5727 |
return path_var (NULL_TREE, 0); |
| 5728 |
|
| 5729 |
case RK_STRING: |
| 5730 |
{ |
| 5731 |
const string_region *string_reg = as_a <const string_region *> (reg); |
| 5732 |
return path_var (string_reg->get_string_cst (), 0); |
| 5733 |
} |
| 5734 |
|
| 5735 |
case RK_VAR_ARG: |
| 5736 |
case RK_ERRNO: |
| 5737 |
case RK_UNKNOWN: |
| 5738 |
case RK_PRIVATE: |
| 5739 |
return path_var (NULL_TREE, 0); |
| 5740 |
} |
| 5741 |
} |
| 5742 |
|
| 5743 |
/* Attempt to return a path_var that represents REG, or return |
| 5744 |
the NULL path_var. |
| 5745 |
For example, a region for a field of a local would be a path_var |
| 5746 |
wrapping a COMPONENT_REF. |
| 5747 |
Use VISITED to prevent infinite mutual recursion with the overload for |
| 5748 |
svalues. |
| 5749 |
|
| 5750 |
This function defers to get_representative_path_var_1 to do the work; |
| 5751 |
it adds verification that get_representative_path_var_1 returned a tree |
| 5752 |
of the correct type. */ |
| 5753 |
|
| 5754 |
path_var |
| 5755 |
region_model::get_representative_path_var (const region *reg, |
| 5756 |
svalue_set *visited, |
| 5757 |
logger *logger) const |
| 5758 |
{ |
| 5759 |
LOG_SCOPE (logger); |
| 5760 |
if (logger) |
| 5761 |
{ |
| 5762 |
logger->start_log_line (); |
| 5763 |
logger->log_partial ("reg: "); |
| 5764 |
reg->dump_to_pp (logger->get_printer (), true); |
| 5765 |
logger->end_log_line (); |
| 5766 |
} |
| 5767 |
|
| 5768 |
path_var result = get_representative_path_var_1 (reg, visited, logger); |
| 5769 |
|
| 5770 |
/* Verify that the result has the same type as REG, if any. */ |
| 5771 |
if (result.m_tree && reg->get_type ()) |
| 5772 |
gcc_assert (TREE_TYPE (result.m_tree) == reg->get_type ()); |
| 5773 |
|
| 5774 |
if (logger) |
| 5775 |
{ |
| 5776 |
logger->start_log_line (); |
| 5777 |
logger->log_partial ("reg: "); |
| 5778 |
reg->dump_to_pp (logger->get_printer (), true); |
| 5779 |
logger->end_log_line (); |
| 5780 |
|
| 5781 |
if (result.m_tree) |
| 5782 |
logger->log ("tree: %qE", result.m_tree); |
| 5783 |
else |
| 5784 |
logger->log ("tree: NULL"); |
| 5785 |
} |
| 5786 |
|
| 5787 |
return result; |
| 5788 |
} |
| 5789 |
|
| 5790 |
/* Update this model for any phis in SNODE, assuming we came from |
| 5791 |
LAST_CFG_SUPEREDGE. */ |
| 5792 |
|
| 5793 |
void |
| 5794 |
region_model::update_for_phis (const supernode *snode, |
| 5795 |
const cfg_superedge *last_cfg_superedge, |
| 5796 |
region_model_context *ctxt) |
| 5797 |
{ |
| 5798 |
gcc_assert (last_cfg_superedge); |
| 5799 |
|
| 5800 |
/* Copy this state and pass it to handle_phi so that all of the phi stmts |
| 5801 |
are effectively handled simultaneously. */ |
| 5802 |
const region_model old_state (*this); |
| 5803 |
|
| 5804 |
hash_set<const svalue *> svals_changing_meaning; |
| 5805 |
|
| 5806 |
for (gphi_iterator gpi = const_cast<supernode *>(snode)->start_phis (); |
| 5807 |
!gsi_end_p (gpi); gsi_next (&gpi)) |
| 5808 |
{ |
| 5809 |
gphi *phi = gpi.phi (); |
| 5810 |
|
| 5811 |
tree src = last_cfg_superedge->get_phi_arg (phi); |
| 5812 |
tree lhs = gimple_phi_result (phi); |
| 5813 |
|
| 5814 |
/* Update next_state based on phi and old_state. */ |
| 5815 |
handle_phi (phi, lhs, src, old_state, svals_changing_meaning, ctxt); |
| 5816 |
} |
| 5817 |
|
| 5818 |
for (auto iter : svals_changing_meaning) |
| 5819 |
m_constraints->purge_state_involving (iter); |
| 5820 |
} |
| 5821 |
|
| 5822 |
/* Attempt to update this model for taking EDGE (where the last statement |
| 5823 |
was LAST_STMT), returning true if the edge can be taken, false |
| 5824 |
otherwise. |
| 5825 |
When returning false, if OUT is non-NULL, write a new rejected_constraint |
| 5826 |
to it. |
| 5827 |
|
| 5828 |
For CFG superedges where LAST_STMT is a conditional or a switch |
| 5829 |
statement, attempt to add the relevant conditions for EDGE to this |
| 5830 |
model, returning true if they are feasible, or false if they are |
| 5831 |
impossible. |
| 5832 |
|
| 5833 |
For call superedges, push frame information and store arguments |
| 5834 |
into parameters. |
| 5835 |
|
| 5836 |
For return superedges, pop frame information and store return |
| 5837 |
values into any lhs. |
| 5838 |
|
| 5839 |
Rejection of call/return superedges happens elsewhere, in |
| 5840 |
program_point::on_edge (i.e. based on program point, rather |
| 5841 |
than program state). */ |
| 5842 |
|
| 5843 |
bool |
| 5844 |
region_model::maybe_update_for_edge (const superedge &edge, |
| 5845 |
const gimple *last_stmt, |
| 5846 |
region_model_context *ctxt, |
| 5847 |
std::unique_ptr<rejected_constraint> *out) |
| 5848 |
{ |
| 5849 |
/* Handle frame updates for interprocedural edges. */ |
| 5850 |
switch (edge.m_kind) |
| 5851 |
{ |
| 5852 |
default: |
| 5853 |
break; |
| 5854 |
|
| 5855 |
case SUPEREDGE_CALL: |
| 5856 |
{ |
| 5857 |
const call_superedge *call_edge = as_a <const call_superedge *> (&edge); |
| 5858 |
update_for_call_superedge (*call_edge, ctxt); |
| 5859 |
} |
| 5860 |
break; |
| 5861 |
|
| 5862 |
case SUPEREDGE_RETURN: |
| 5863 |
{ |
| 5864 |
const return_superedge *return_edge |
| 5865 |
= as_a <const return_superedge *> (&edge); |
| 5866 |
update_for_return_superedge (*return_edge, ctxt); |
| 5867 |
} |
| 5868 |
break; |
| 5869 |
|
| 5870 |
case SUPEREDGE_INTRAPROCEDURAL_CALL: |
| 5871 |
/* This is a no-op for call summaries; we should already |
| 5872 |
have handled the effect of the call summary at the call stmt. */ |
| 5873 |
break; |
| 5874 |
} |
| 5875 |
|
| 5876 |
if (last_stmt == NULL) |
| 5877 |
return true; |
| 5878 |
|
| 5879 |
/* Apply any constraints for conditionals/switch/computed-goto statements. */ |
| 5880 |
|
| 5881 |
if (const gcond *cond_stmt = dyn_cast <const gcond *> (last_stmt)) |
| 5882 |
{ |
| 5883 |
const cfg_superedge *cfg_sedge = as_a <const cfg_superedge *> (&edge); |
| 5884 |
return apply_constraints_for_gcond (*cfg_sedge, cond_stmt, ctxt, out); |
| 5885 |
} |
| 5886 |
|
| 5887 |
if (const gswitch *switch_stmt = dyn_cast <const gswitch *> (last_stmt)) |
| 5888 |
{ |
| 5889 |
const switch_cfg_superedge *switch_sedge |
| 5890 |
= as_a <const switch_cfg_superedge *> (&edge); |
| 5891 |
return apply_constraints_for_gswitch (*switch_sedge, switch_stmt, |
| 5892 |
ctxt, out); |
| 5893 |
} |
| 5894 |
|
| 5895 |
if (const ggoto *goto_stmt = dyn_cast <const ggoto *> (last_stmt)) |
| 5896 |
{ |
| 5897 |
const cfg_superedge *cfg_sedge = as_a <const cfg_superedge *> (&edge); |
| 5898 |
return apply_constraints_for_ggoto (*cfg_sedge, goto_stmt, ctxt); |
| 5899 |
} |
| 5900 |
|
| 5901 |
/* Apply any constraints due to an exception being thrown. */ |
| 5902 |
if (const cfg_superedge *cfg_sedge = dyn_cast <const cfg_superedge *> (&edge)) |
| 5903 |
if (cfg_sedge->get_flags () & EDGE_EH) |
| 5904 |
return apply_constraints_for_exception (last_stmt, ctxt, out); |
| 5905 |
|
| 5906 |
return true; |
| 5907 |
} |
| 5908 |
|
| 5909 |
/* Push a new frame_region on to the stack region. |
| 5910 |
Populate the frame_region with child regions for the function call's |
| 5911 |
parameters, using values from the arguments at the callsite in the |
| 5912 |
caller's frame. */ |
| 5913 |
|
| 5914 |
void |
| 5915 |
region_model::update_for_gcall (const gcall *call_stmt, |
| 5916 |
region_model_context *ctxt, |
| 5917 |
function *callee) |
| 5918 |
{ |
| 5919 |
/* Build a vec of argument svalues, using the current top |
| 5920 |
frame for resolving tree expressions. */ |
| 5921 |
auto_vec<const svalue *> arg_svals (gimple_call_num_args (call_stmt)); |
| 5922 |
|
| 5923 |
for (unsigned i = 0; i < gimple_call_num_args (call_stmt); i++) |
| 5924 |
{ |
| 5925 |
tree arg = gimple_call_arg (call_stmt, i); |
| 5926 |
arg_svals.quick_push (get_rvalue (arg, ctxt)); |
| 5927 |
} |
| 5928 |
|
| 5929 |
if(!callee) |
| 5930 |
{ |
| 5931 |
/* Get the function * from the gcall. */ |
| 5932 |
tree fn_decl = get_fndecl_for_call (call_stmt,ctxt); |
| 5933 |
callee = DECL_STRUCT_FUNCTION (fn_decl); |
| 5934 |
} |
| 5935 |
|
| 5936 |
gcc_assert (callee); |
| 5937 |
push_frame (*callee, &arg_svals, ctxt); |
| 5938 |
} |
| 5939 |
|
| 5940 |
/* Pop the top-most frame_region from the stack, and copy the return |
| 5941 |
region's values (if any) into the region for the lvalue of the LHS of |
| 5942 |
the call (if any). */ |
| 5943 |
|
| 5944 |
void |
| 5945 |
region_model::update_for_return_gcall (const gcall *call_stmt, |
| 5946 |
region_model_context *ctxt) |
| 5947 |
{ |
| 5948 |
/* Get the lvalue for the result of the call, passing it to pop_frame, |
| 5949 |
so that pop_frame can determine the region with respect to the |
| 5950 |
*caller* frame. */ |
| 5951 |
tree lhs = gimple_call_lhs (call_stmt); |
| 5952 |
pop_frame (lhs, NULL, ctxt, call_stmt); |
| 5953 |
} |
| 5954 |
|
| 5955 |
/* Extract calling information from the superedge and update the model for the |
| 5956 |
call */ |
| 5957 |
|
| 5958 |
void |
| 5959 |
region_model::update_for_call_superedge (const call_superedge &call_edge, |
| 5960 |
region_model_context *ctxt) |
| 5961 |
{ |
| 5962 |
const gcall *call_stmt = call_edge.get_call_stmt (); |
| 5963 |
update_for_gcall (call_stmt, ctxt, call_edge.get_callee_function ()); |
| 5964 |
} |
| 5965 |
|
| 5966 |
/* Extract calling information from the return superedge and update the model |
| 5967 |
for the returning call */ |
| 5968 |
|
| 5969 |
void |
| 5970 |
region_model::update_for_return_superedge (const return_superedge &return_edge, |
| 5971 |
region_model_context *ctxt) |
| 5972 |
{ |
| 5973 |
const gcall *call_stmt = return_edge.get_call_stmt (); |
| 5974 |
update_for_return_gcall (call_stmt, ctxt); |
| 5975 |
} |
| 5976 |
|
| 5977 |
/* Attempt to use R to replay SUMMARY into this object. |
| 5978 |
Return true if it is possible. */ |
| 5979 |
|
| 5980 |
bool |
| 5981 |
region_model::replay_call_summary (call_summary_replay &r, |
| 5982 |
const region_model &summary) |
| 5983 |
{ |
| 5984 |
gcc_assert (summary.get_stack_depth () == 1); |
| 5985 |
|
| 5986 |
m_store.replay_call_summary (r, summary.m_store); |
| 5987 |
|
| 5988 |
if (r.get_ctxt ()) |
| 5989 |
r.get_ctxt ()->maybe_did_work (); |
| 5990 |
|
| 5991 |
if (!m_constraints->replay_call_summary (r, *summary.m_constraints)) |
| 5992 |
return false; |
| 5993 |
|
| 5994 |
for (auto kv : summary.m_dynamic_extents) |
| 5995 |
{ |
| 5996 |
const region *summary_reg = kv.first; |
| 5997 |
const region *caller_reg = r.convert_region_from_summary (summary_reg); |
| 5998 |
if (!caller_reg) |
| 5999 |
continue; |
| 6000 |
const svalue *summary_sval = kv.second; |
| 6001 |
const svalue *caller_sval = r.convert_svalue_from_summary (summary_sval); |
| 6002 |
if (!caller_sval) |
| 6003 |
continue; |
| 6004 |
m_dynamic_extents.put (caller_reg, caller_sval); |
| 6005 |
} |
| 6006 |
|
| 6007 |
return true; |
| 6008 |
} |
| 6009 |
|
| 6010 |
/* Given a true or false edge guarded by conditional statement COND_STMT, |
| 6011 |
determine appropriate constraints for the edge to be taken. |
| 6012 |
|
| 6013 |
If they are feasible, add the constraints and return true. |
| 6014 |
|
| 6015 |
Return false if the constraints contradict existing knowledge |
| 6016 |
(and so the edge should not be taken). |
| 6017 |
When returning false, if OUT is non-NULL, write a new rejected_constraint |
| 6018 |
to it. */ |
| 6019 |
|
| 6020 |
bool |
| 6021 |
region_model:: |
| 6022 |
apply_constraints_for_gcond (const cfg_superedge &sedge, |
| 6023 |
const gcond *cond_stmt, |
| 6024 |
region_model_context *ctxt, |
| 6025 |
std::unique_ptr<rejected_constraint> *out) |
| 6026 |
{ |
| 6027 |
::edge cfg_edge = sedge.get_cfg_edge (); |
| 6028 |
gcc_assert (cfg_edge != NULL); |
| 6029 |
gcc_assert (cfg_edge->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)); |
| 6030 |
|
| 6031 |
enum tree_code op = gimple_cond_code (cond_stmt); |
| 6032 |
tree lhs = gimple_cond_lhs (cond_stmt); |
| 6033 |
tree rhs = gimple_cond_rhs (cond_stmt); |
| 6034 |
if (cfg_edge->flags & EDGE_FALSE_VALUE) |
| 6035 |
op = invert_tree_comparison (op, false /* honor_nans */); |
| 6036 |
return add_constraint (lhs, op, rhs, ctxt, out); |
| 6037 |
} |
| 6038 |
|
| 6039 |
/* Return true iff SWITCH_STMT has a non-default label that contains |
| 6040 |
INT_CST. */ |
| 6041 |
|
| 6042 |
static bool |
| 6043 |
has_nondefault_case_for_value_p (const gswitch *switch_stmt, tree int_cst) |
| 6044 |
{ |
| 6045 |
/* We expect the initial label to be the default; skip it. */ |
| 6046 |
gcc_assert (CASE_LOW (gimple_switch_label (switch_stmt, 0)) == NULL); |
| 6047 |
unsigned min_idx = 1; |
| 6048 |
unsigned max_idx = gimple_switch_num_labels (switch_stmt) - 1; |
| 6049 |
|
| 6050 |
/* Binary search: try to find the label containing INT_CST. |
| 6051 |
This requires the cases to be sorted by CASE_LOW (done by the |
| 6052 |
gimplifier). */ |
| 6053 |
while (max_idx >= min_idx) |
| 6054 |
{ |
| 6055 |
unsigned case_idx = (min_idx + max_idx) / 2; |
| 6056 |
tree label = gimple_switch_label (switch_stmt, case_idx); |
| 6057 |
tree low = CASE_LOW (label); |
| 6058 |
gcc_assert (low); |
| 6059 |
tree high = CASE_HIGH (label); |
| 6060 |
if (!high) |
| 6061 |
high = low; |
| 6062 |
if (tree_int_cst_compare (int_cst, low) < 0) |
| 6063 |
{ |
| 6064 |
/* INT_CST is below the range of this label. */ |
| 6065 |
gcc_assert (case_idx > 0); |
| 6066 |
max_idx = case_idx - 1; |
| 6067 |
} |
| 6068 |
else if (tree_int_cst_compare (int_cst, high) > 0) |
| 6069 |
{ |
| 6070 |
/* INT_CST is above the range of this case. */ |
| 6071 |
min_idx = case_idx + 1; |
| 6072 |
} |
| 6073 |
else |
| 6074 |
/* This case contains INT_CST. */ |
| 6075 |
return true; |
| 6076 |
} |
| 6077 |
/* Not found. */ |
| 6078 |
return false; |
| 6079 |
} |
| 6080 |
|
| 6081 |
/* Return true iff SWITCH_STMT (which must be on an enum value) |
| 6082 |
has nondefault cases handling all values in the enum. */ |
| 6083 |
|
| 6084 |
static bool |
| 6085 |
has_nondefault_cases_for_all_enum_values_p (const gswitch *switch_stmt, |
| 6086 |
tree type) |
| 6087 |
{ |
| 6088 |
gcc_assert (switch_stmt); |
| 6089 |
gcc_assert (TREE_CODE (type) == ENUMERAL_TYPE); |
| 6090 |
|
| 6091 |
for (tree enum_val_iter = TYPE_VALUES (type); |
| 6092 |
enum_val_iter; |
| 6093 |
enum_val_iter = TREE_CHAIN (enum_val_iter)) |
| 6094 |
{ |
| 6095 |
tree enum_val = TREE_VALUE (enum_val_iter); |
| 6096 |
gcc_assert (TREE_CODE (enum_val) == CONST_DECL); |
| 6097 |
gcc_assert (TREE_CODE (DECL_INITIAL (enum_val)) == INTEGER_CST); |
| 6098 |
if (!has_nondefault_case_for_value_p (switch_stmt, |
| 6099 |
DECL_INITIAL (enum_val))) |
| 6100 |
return false; |
| 6101 |
} |
| 6102 |
return true; |
| 6103 |
} |
| 6104 |
|
| 6105 |
/* Given an EDGE guarded by SWITCH_STMT, determine appropriate constraints |
| 6106 |
for the edge to be taken. |
| 6107 |
|
| 6108 |
If they are feasible, add the constraints and return true. |
| 6109 |
|
| 6110 |
Return false if the constraints contradict existing knowledge |
| 6111 |
(and so the edge should not be taken). |
| 6112 |
When returning false, if OUT is non-NULL, write a new rejected_constraint |
| 6113 |
to it. */ |
| 6114 |
|
| 6115 |
bool |
| 6116 |
region_model:: |
| 6117 |
apply_constraints_for_gswitch (const switch_cfg_superedge &edge, |
| 6118 |
const gswitch *switch_stmt, |
| 6119 |
region_model_context *ctxt, |
| 6120 |
std::unique_ptr<rejected_constraint> *out) |
| 6121 |
{ |
| 6122 |
tree index = gimple_switch_index (switch_stmt); |
| 6123 |
const svalue *index_sval = get_rvalue (index, ctxt); |
| 6124 |
bool check_index_type = true; |
| 6125 |
|
| 6126 |
/* With -fshort-enum, there may be a type cast. */ |
| 6127 |
if (ctxt && index_sval->get_kind () == SK_UNARYOP |
| 6128 |
&& TREE_CODE (index_sval->get_type ()) == INTEGER_TYPE) |
| 6129 |
{ |
| 6130 |
const unaryop_svalue *unaryop = as_a <const unaryop_svalue *> (index_sval); |
| 6131 |
if (unaryop->get_op () == NOP_EXPR |
| 6132 |
&& is_a <const initial_svalue *> (unaryop->get_arg ())) |
| 6133 |
if (const initial_svalue *initvalop = (as_a <const initial_svalue *> |
| 6134 |
(unaryop->get_arg ()))) |
| 6135 |
if (initvalop->get_type () |
| 6136 |
&& TREE_CODE (initvalop->get_type ()) == ENUMERAL_TYPE) |
| 6137 |
{ |
| 6138 |
index_sval = initvalop; |
| 6139 |
check_index_type = false; |
| 6140 |
} |
| 6141 |
} |
| 6142 |
|
| 6143 |
/* If we're switching based on an enum type, assume that the user is only |
| 6144 |
working with values from the enum. Hence if this is an |
| 6145 |
implicitly-created "default", assume it doesn't get followed. |
| 6146 |
This fixes numerous "uninitialized" false positives where we otherwise |
| 6147 |
consider jumping past the initialization cases. */ |
| 6148 |
|
| 6149 |
if (/* Don't check during feasibility-checking (when ctxt is NULL). */ |
| 6150 |
ctxt |
| 6151 |
/* Must be an enum value. */ |
| 6152 |
&& index_sval->get_type () |
| 6153 |
&& (!check_index_type |
| 6154 |
|| TREE_CODE (TREE_TYPE (index)) == ENUMERAL_TYPE) |
| 6155 |
&& TREE_CODE (index_sval->get_type ()) == ENUMERAL_TYPE |
| 6156 |
/* If we have a constant, then we can check it directly. */ |
| 6157 |
&& index_sval->get_kind () != SK_CONSTANT |
| 6158 |
&& edge.implicitly_created_default_p () |
| 6159 |
&& has_nondefault_cases_for_all_enum_values_p (switch_stmt, |
| 6160 |
index_sval->get_type ()) |
| 6161 |
/* Don't do this if there's a chance that the index is |
| 6162 |
attacker-controlled. */ |
| 6163 |
&& !ctxt->possibly_tainted_p (index_sval)) |
| 6164 |
{ |
| 6165 |
if (out) |
| 6166 |
*out = make_unique <rejected_default_case> (*this); |
| 6167 |
return false; |
| 6168 |
} |
| 6169 |
|
| 6170 |
bounded_ranges_manager *ranges_mgr = get_range_manager (); |
| 6171 |
const bounded_ranges *all_cases_ranges |
| 6172 |
= ranges_mgr->get_or_create_ranges_for_switch (&edge, switch_stmt); |
| 6173 |
bool sat = m_constraints->add_bounded_ranges (index_sval, all_cases_ranges); |
| 6174 |
if (!sat && out) |
| 6175 |
*out = make_unique <rejected_ranges_constraint> (*this, index, all_cases_ranges); |
| 6176 |
if (sat && ctxt && !all_cases_ranges->empty_p ()) |
| 6177 |
ctxt->on_bounded_ranges (*index_sval, *all_cases_ranges); |
| 6178 |
return sat; |
| 6179 |
} |
| 6180 |
|
| 6181 |
/* Given an edge reached by GOTO_STMT, determine appropriate constraints |
| 6182 |
for the edge to be taken. |
| 6183 |
|
| 6184 |
If they are feasible, add the constraints and return true. |
| 6185 |
|
| 6186 |
Return false if the constraints contradict existing knowledge |
| 6187 |
(and so the edge should not be taken). */ |
| 6188 |
|
| 6189 |
bool |
| 6190 |
region_model::apply_constraints_for_ggoto (const cfg_superedge &edge, |
| 6191 |
const ggoto *goto_stmt, |
| 6192 |
region_model_context *ctxt) |
| 6193 |
{ |
| 6194 |
tree dest = gimple_goto_dest (goto_stmt); |
| 6195 |
const svalue *dest_sval = get_rvalue (dest, ctxt); |
| 6196 |
|
| 6197 |
/* If we know we were jumping to a specific label. */ |
| 6198 |
if (tree dst_label = edge.m_dest->get_label ()) |
| 6199 |
{ |
| 6200 |
const label_region *dst_label_reg |
| 6201 |
= m_mgr->get_region_for_label (dst_label); |
| 6202 |
const svalue *dst_label_ptr |
| 6203 |
= m_mgr->get_ptr_svalue (ptr_type_node, dst_label_reg); |
| 6204 |
|
| 6205 |
if (!add_constraint (dest_sval, EQ_EXPR, dst_label_ptr, ctxt)) |
| 6206 |
return false; |
| 6207 |
} |
| 6208 |
|
| 6209 |
return true; |
| 6210 |
} |
| 6211 |
|
| 6212 |
/* Apply any constraints due to an exception being thrown at LAST_STMT. |
| 6213 |
|
| 6214 |
If they are feasible, add the constraints and return true. |
| 6215 |
|
| 6216 |
Return false if the constraints contradict existing knowledge |
| 6217 |
(and so the edge should not be taken). |
| 6218 |
When returning false, if OUT is non-NULL, write a new rejected_constraint |
| 6219 |
to it. */ |
| 6220 |
|
| 6221 |
bool |
| 6222 |
region_model:: |
| 6223 |
apply_constraints_for_exception (const gimple *last_stmt, |
| 6224 |
region_model_context *ctxt, |
| 6225 |
std::unique_ptr<rejected_constraint> *out) |
| 6226 |
{ |
| 6227 |
gcc_assert (last_stmt); |
| 6228 |
if (const gcall *call = dyn_cast <const gcall *> (last_stmt)) |
| 6229 |
if (tree callee_fndecl = get_fndecl_for_call (call, ctxt)) |
| 6230 |
if (is_named_call_p (callee_fndecl, "operator new", call, 1) |
| 6231 |
|| is_named_call_p (callee_fndecl, "operator new []", call, 1)) |
| 6232 |
{ |
| 6233 |
/* We have an exception thrown from operator new. |
| 6234 |
Add a constraint that the result was NULL, to avoid a false |
| 6235 |
leak report due to the result being lost when following |
| 6236 |
the EH edge. */ |
| 6237 |
if (tree lhs = gimple_call_lhs (call)) |
| 6238 |
return add_constraint (lhs, EQ_EXPR, null_pointer_node, ctxt, out); |
| 6239 |
return true; |
| 6240 |
} |
| 6241 |
return true; |
| 6242 |
} |
| 6243 |
|
| 6244 |
/* For use with push_frame when handling a top-level call within the analysis. |
| 6245 |
PARAM has a defined but unknown initial value. |
| 6246 |
Anything it points to has escaped, since the calling context "knows" |
| 6247 |
the pointer, and thus calls to unknown functions could read/write into |
| 6248 |
the region. |
| 6249 |
If NONNULL is true, then assume that PARAM must be non-NULL. */ |
| 6250 |
|
| 6251 |
void |
| 6252 |
region_model::on_top_level_param (tree param, |
| 6253 |
bool nonnull, |
| 6254 |
region_model_context *ctxt) |
| 6255 |
{ |
| 6256 |
if (POINTER_TYPE_P (TREE_TYPE (param))) |
| 6257 |
{ |
| 6258 |
const region *param_reg = get_lvalue (param, ctxt); |
| 6259 |
const svalue *init_ptr_sval |
| 6260 |
= m_mgr->get_or_create_initial_value (param_reg); |
| 6261 |
const region *pointee_reg = m_mgr->get_symbolic_region (init_ptr_sval); |
| 6262 |
m_store.mark_as_escaped (pointee_reg); |
| 6263 |
if (nonnull) |
| 6264 |
{ |
| 6265 |
const svalue *null_ptr_sval |
| 6266 |
= m_mgr->get_or_create_null_ptr (TREE_TYPE (param)); |
| 6267 |
add_constraint (init_ptr_sval, NE_EXPR, null_ptr_sval, ctxt); |
| 6268 |
} |
| 6269 |
} |
| 6270 |
} |
| 6271 |
|
| 6272 |
/* Update this region_model to reflect pushing a frame onto the stack |
| 6273 |
for a call to FUN. |
| 6274 |
|
| 6275 |
If ARG_SVALS is non-NULL, use it to populate the parameters |
| 6276 |
in the new frame. |
| 6277 |
Otherwise, the params have their initial_svalues. |
| 6278 |
|
| 6279 |
Return the frame_region for the new frame. */ |
| 6280 |
|
| 6281 |
const region * |
| 6282 |
region_model::push_frame (const function &fun, |
| 6283 |
const vec<const svalue *> *arg_svals, |
| 6284 |
region_model_context *ctxt) |
| 6285 |
{ |
| 6286 |
m_current_frame = m_mgr->get_frame_region (m_current_frame, fun); |
| 6287 |
if (arg_svals) |
| 6288 |
{ |
| 6289 |
/* Arguments supplied from a caller frame. */ |
| 6290 |
tree fndecl = fun.decl; |
| 6291 |
unsigned idx = 0; |
| 6292 |
for (tree iter_parm = DECL_ARGUMENTS (fndecl); iter_parm; |
| 6293 |
iter_parm = DECL_CHAIN (iter_parm), ++idx) |
| 6294 |
{ |
| 6295 |
/* If there's a mismatching declaration, the call stmt might |
| 6296 |
not have enough args. Handle this case by leaving the |
| 6297 |
rest of the params as uninitialized. */ |
| 6298 |
if (idx >= arg_svals->length ()) |
| 6299 |
break; |
| 6300 |
tree parm_lval = iter_parm; |
| 6301 |
if (tree parm_default_ssa = get_ssa_default_def (fun, iter_parm)) |
| 6302 |
parm_lval = parm_default_ssa; |
| 6303 |
const region *parm_reg = get_lvalue (parm_lval, ctxt); |
| 6304 |
const svalue *arg_sval = (*arg_svals)[idx]; |
| 6305 |
set_value (parm_reg, arg_sval, ctxt); |
| 6306 |
} |
| 6307 |
|
| 6308 |
/* Handle any variadic args. */ |
| 6309 |
unsigned va_arg_idx = 0; |
| 6310 |
for (; idx < arg_svals->length (); idx++, va_arg_idx++) |
| 6311 |
{ |
| 6312 |
const svalue *arg_sval = (*arg_svals)[idx]; |
| 6313 |
const region *var_arg_reg |
| 6314 |
= m_mgr->get_var_arg_region (m_current_frame, |
| 6315 |
va_arg_idx); |
| 6316 |
set_value (var_arg_reg, arg_sval, ctxt); |
| 6317 |
} |
| 6318 |
} |
| 6319 |
else |
| 6320 |
{ |
| 6321 |
/* Otherwise we have a top-level call within the analysis. The params |
| 6322 |
have defined but unknown initial values. |
| 6323 |
Anything they point to has escaped. */ |
| 6324 |
tree fndecl = fun.decl; |
| 6325 |
|
| 6326 |
/* Handle "__attribute__((nonnull))". */ |
| 6327 |
tree fntype = TREE_TYPE (fndecl); |
| 6328 |
bitmap nonnull_args = get_nonnull_args (fntype); |
| 6329 |
|
| 6330 |
unsigned parm_idx = 0; |
| 6331 |
for (tree iter_parm = DECL_ARGUMENTS (fndecl); iter_parm; |
| 6332 |
iter_parm = DECL_CHAIN (iter_parm)) |
| 6333 |
{ |
| 6334 |
bool non_null = (nonnull_args |
| 6335 |
? (bitmap_empty_p (nonnull_args) |
| 6336 |
|| bitmap_bit_p (nonnull_args, parm_idx)) |
| 6337 |
: false); |
| 6338 |
if (tree parm_default_ssa = get_ssa_default_def (fun, iter_parm)) |
| 6339 |
on_top_level_param (parm_default_ssa, non_null, ctxt); |
| 6340 |
else |
| 6341 |
on_top_level_param (iter_parm, non_null, ctxt); |
| 6342 |
parm_idx++; |
| 6343 |
} |
| 6344 |
|
| 6345 |
BITMAP_FREE (nonnull_args); |
| 6346 |
} |
| 6347 |
|
| 6348 |
return m_current_frame; |
| 6349 |
} |
| 6350 |
|
| 6351 |
/* Get the function of the top-most frame in this region_model's stack. |
| 6352 |
There must be such a frame. */ |
| 6353 |
|
| 6354 |
const function * |
| 6355 |
region_model::get_current_function () const |
| 6356 |
{ |
| 6357 |
const frame_region *frame = get_current_frame (); |
| 6358 |
gcc_assert (frame); |
| 6359 |
return &frame->get_function (); |
| 6360 |
} |
| 6361 |
|
| 6362 |
/* Custom region_model_context for the assignment to the result |
| 6363 |
at a call statement when popping a frame (PR analyzer/106203). */ |
| 6364 |
|
| 6365 |
class caller_context : public region_model_context_decorator |
| 6366 |
{ |
| 6367 |
public: |
| 6368 |
caller_context (region_model_context *inner, |
| 6369 |
const gcall *call_stmt, |
| 6370 |
const frame_region &caller_frame) |
| 6371 |
: region_model_context_decorator (inner), |
| 6372 |
m_call_stmt (call_stmt), |
| 6373 |
m_caller_frame (caller_frame) |
| 6374 |
{} |
| 6375 |
bool warn (std::unique_ptr<pending_diagnostic> d, |
| 6376 |
const stmt_finder *custom_finder) override |
| 6377 |
{ |
| 6378 |
if (m_inner && custom_finder == nullptr) |
| 6379 |
{ |
| 6380 |
/* Custom stmt_finder to use m_call_stmt for the |
| 6381 |
diagnostic. */ |
| 6382 |
class my_finder : public stmt_finder |
| 6383 |
{ |
| 6384 |
public: |
| 6385 |
my_finder (const gcall *call_stmt, |
| 6386 |
const frame_region &caller_frame) |
| 6387 |
: m_call_stmt (call_stmt), |
| 6388 |
m_caller_frame (caller_frame) |
| 6389 |
{} |
| 6390 |
std::unique_ptr<stmt_finder> clone () const override |
| 6391 |
{ |
| 6392 |
return ::make_unique<my_finder> (m_call_stmt, m_caller_frame); |
| 6393 |
} |
| 6394 |
const gimple *find_stmt (const exploded_path &) override |
| 6395 |
{ |
| 6396 |
return m_call_stmt; |
| 6397 |
} |
| 6398 |
void update_event_loc_info (event_loc_info &loc_info) final override |
| 6399 |
{ |
| 6400 |
loc_info.m_fndecl = m_caller_frame.get_fndecl (); |
| 6401 |
loc_info.m_depth = m_caller_frame.get_stack_depth (); |
| 6402 |
} |
| 6403 |
|
| 6404 |
private: |
| 6405 |
const gcall *m_call_stmt; |
| 6406 |
const frame_region &m_caller_frame; |
| 6407 |
}; |
| 6408 |
my_finder finder (m_call_stmt, m_caller_frame); |
| 6409 |
return m_inner->warn (std::move (d), &finder); |
| 6410 |
} |
| 6411 |
else |
| 6412 |
return region_model_context_decorator::warn (std::move (d), |
| 6413 |
custom_finder); |
| 6414 |
} |
| 6415 |
const gimple *get_stmt () const override |
| 6416 |
{ |
| 6417 |
return m_call_stmt; |
| 6418 |
}; |
| 6419 |
|
| 6420 |
private: |
| 6421 |
const gcall *m_call_stmt; |
| 6422 |
const frame_region &m_caller_frame; |
| 6423 |
}; |
| 6424 |
|
| 6425 |
|
| 6426 |
/* Pop the topmost frame_region from this region_model's stack; |
| 6427 |
|
| 6428 |
If RESULT_LVALUE is non-null, copy any return value from the frame |
| 6429 |
into the corresponding region (evaluated with respect to the *caller* |
| 6430 |
frame, rather than the called frame). |
| 6431 |
If OUT_RESULT is non-null, copy any return value from the frame |
| 6432 |
into *OUT_RESULT. |
| 6433 |
|
| 6434 |
If non-null, use CALL_STMT as the location when complaining about |
| 6435 |
assignment of the return value to RESULT_LVALUE. |
| 6436 |
|
| 6437 |
If EVAL_RETURN_SVALUE is false, then don't evaluate the return value. |
| 6438 |
This is for use when unwinding frames e.g. due to longjmp, to suppress |
| 6439 |
erroneously reporting uninitialized return values. |
| 6440 |
|
| 6441 |
Purge the frame region and all its descendent regions. |
| 6442 |
Convert any pointers that point into such regions into |
| 6443 |
POISON_KIND_POPPED_STACK svalues. */ |
| 6444 |
|
| 6445 |
void |
| 6446 |
region_model::pop_frame (tree result_lvalue, |
| 6447 |
const svalue **out_result, |
| 6448 |
region_model_context *ctxt, |
| 6449 |
const gcall *call_stmt, |
| 6450 |
bool eval_return_svalue) |
| 6451 |
{ |
| 6452 |
gcc_assert (m_current_frame); |
| 6453 |
|
| 6454 |
const region_model pre_popped_model = *this; |
| 6455 |
const frame_region *frame_reg = m_current_frame; |
| 6456 |
|
| 6457 |
/* Notify state machines. */ |
| 6458 |
if (ctxt) |
| 6459 |
ctxt->on_pop_frame (frame_reg); |
| 6460 |
|
| 6461 |
/* Evaluate the result, within the callee frame. */ |
| 6462 |
tree fndecl = m_current_frame->get_function ().decl; |
| 6463 |
tree result = DECL_RESULT (fndecl); |
| 6464 |
const svalue *retval = NULL; |
| 6465 |
if (result |
| 6466 |
&& TREE_TYPE (result) != void_type_node |
| 6467 |
&& eval_return_svalue) |
| 6468 |
{ |
| 6469 |
retval = get_rvalue (result, ctxt); |
| 6470 |
if (out_result) |
| 6471 |
*out_result = retval; |
| 6472 |
} |
| 6473 |
|
| 6474 |
/* Pop the frame. */ |
| 6475 |
m_current_frame = m_current_frame->get_calling_frame (); |
| 6476 |
|
| 6477 |
if (result_lvalue && retval) |
| 6478 |
{ |
| 6479 |
gcc_assert (eval_return_svalue); |
| 6480 |
|
| 6481 |
/* Compute result_dst_reg using RESULT_LVALUE *after* popping |
| 6482 |
the frame, but before poisoning pointers into the old frame. */ |
| 6483 |
const region *result_dst_reg = get_lvalue (result_lvalue, ctxt); |
| 6484 |
|
| 6485 |
/* Assign retval to result_dst_reg, using caller_context |
| 6486 |
to set the call_stmt and the popped_frame for any diagnostics |
| 6487 |
due to the assignment. */ |
| 6488 |
gcc_assert (m_current_frame); |
| 6489 |
caller_context caller_ctxt (ctxt, call_stmt, *m_current_frame); |
| 6490 |
set_value (result_dst_reg, retval, call_stmt ? &caller_ctxt : ctxt); |
| 6491 |
} |
| 6492 |
|
| 6493 |
unbind_region_and_descendents (frame_reg,POISON_KIND_POPPED_STACK); |
| 6494 |
notify_on_pop_frame (this, &pre_popped_model, retval, ctxt); |
| 6495 |
} |
| 6496 |
|
| 6497 |
/* Get the number of frames in this region_model's stack. */ |
| 6498 |
|
| 6499 |
int |
| 6500 |
region_model::get_stack_depth () const |
| 6501 |
{ |
| 6502 |
const frame_region *frame = get_current_frame (); |
| 6503 |
if (frame) |
| 6504 |
return frame->get_stack_depth (); |
| 6505 |
else |
| 6506 |
return 0; |
| 6507 |
} |
| 6508 |
|
| 6509 |
/* Get the frame_region with the given index within the stack. |
| 6510 |
The frame_region must exist. */ |
| 6511 |
|
| 6512 |
const frame_region * |
| 6513 |
region_model::get_frame_at_index (int index) const |
| 6514 |
{ |
| 6515 |
const frame_region *frame = get_current_frame (); |
| 6516 |
gcc_assert (frame); |
| 6517 |
gcc_assert (index >= 0); |
| 6518 |
gcc_assert (index <= frame->get_index ()); |
| 6519 |
while (index != frame->get_index ()) |
| 6520 |
{ |
| 6521 |
frame = frame->get_calling_frame (); |
| 6522 |
gcc_assert (frame); |
| 6523 |
} |
| 6524 |
return frame; |
| 6525 |
} |
| 6526 |
|
| 6527 |
/* Unbind svalues for any regions in REG and below. |
| 6528 |
Find any pointers to such regions; convert them to |
| 6529 |
poisoned values of kind PKIND. |
| 6530 |
Also purge any dynamic extents. */ |
| 6531 |
|
| 6532 |
void |
| 6533 |
region_model::unbind_region_and_descendents (const region *reg, |
| 6534 |
enum poison_kind pkind) |
| 6535 |
{ |
| 6536 |
/* Gather a set of base regions to be unbound. */ |
| 6537 |
hash_set<const region *> base_regs; |
| 6538 |
for (store::cluster_map_t::iterator iter = m_store.begin (); |
| 6539 |
iter != m_store.end (); ++iter) |
| 6540 |
{ |
| 6541 |
const region *iter_base_reg = (*iter).first; |
| 6542 |
if (iter_base_reg->descendent_of_p (reg)) |
| 6543 |
base_regs.add (iter_base_reg); |
| 6544 |
} |
| 6545 |
for (hash_set<const region *>::iterator iter = base_regs.begin (); |
| 6546 |
iter != base_regs.end (); ++iter) |
| 6547 |
m_store.purge_cluster (*iter); |
| 6548 |
|
| 6549 |
/* Find any pointers to REG or its descendents; convert to poisoned. */ |
| 6550 |
poison_any_pointers_to_descendents (reg, pkind); |
| 6551 |
|
| 6552 |
/* Purge dynamic extents of any base regions in REG and below |
| 6553 |
(e.g. VLAs and alloca stack regions). */ |
| 6554 |
for (auto iter : m_dynamic_extents) |
| 6555 |
{ |
| 6556 |
const region *iter_reg = iter.first; |
| 6557 |
if (iter_reg->descendent_of_p (reg)) |
| 6558 |
unset_dynamic_extents (iter_reg); |
| 6559 |
} |
| 6560 |
} |
| 6561 |
|
| 6562 |
/* Implementation of BindingVisitor. |
| 6563 |
Update the bound svalues for regions below REG to use poisoned |
| 6564 |
values instead. */ |
| 6565 |
|
| 6566 |
struct bad_pointer_finder |
| 6567 |
{ |
| 6568 |
bad_pointer_finder (const region *reg, enum poison_kind pkind, |
| 6569 |
region_model_manager *mgr) |
| 6570 |
: m_reg (reg), m_pkind (pkind), m_mgr (mgr), m_count (0) |
| 6571 |
{} |
| 6572 |
|
| 6573 |
void on_binding (const binding_key *, const svalue *&sval) |
| 6574 |
{ |
| 6575 |
if (const region_svalue *ptr_sval = sval->dyn_cast_region_svalue ()) |
| 6576 |
{ |
| 6577 |
const region *ptr_dst = ptr_sval->get_pointee (); |
| 6578 |
/* Poison ptrs to descendents of REG, but not to REG itself, |
| 6579 |
otherwise double-free detection doesn't work (since sm-state |
| 6580 |
for "free" is stored on the original ptr svalue). */ |
| 6581 |
if (ptr_dst->descendent_of_p (m_reg) |
| 6582 |
&& ptr_dst != m_reg) |
| 6583 |
{ |
| 6584 |
sval = m_mgr->get_or_create_poisoned_svalue (m_pkind, |
| 6585 |
sval->get_type ()); |
| 6586 |
++m_count; |
| 6587 |
} |
| 6588 |
} |
| 6589 |
} |
| 6590 |
|
| 6591 |
const region *m_reg; |
| 6592 |
enum poison_kind m_pkind; |
| 6593 |
region_model_manager *const m_mgr; |
| 6594 |
int m_count; |
| 6595 |
}; |
| 6596 |
|
| 6597 |
/* Find any pointers to REG or its descendents; convert them to |
| 6598 |
poisoned values of kind PKIND. |
| 6599 |
Return the number of pointers that were poisoned. */ |
| 6600 |
|
| 6601 |
int |
| 6602 |
region_model::poison_any_pointers_to_descendents (const region *reg, |
| 6603 |
enum poison_kind pkind) |
| 6604 |
{ |
| 6605 |
bad_pointer_finder bv (reg, pkind, m_mgr); |
| 6606 |
m_store.for_each_binding (bv); |
| 6607 |
return bv.m_count; |
| 6608 |
} |
| 6609 |
|
| 6610 |
/* Attempt to merge THIS with OTHER_MODEL, writing the result |
| 6611 |
to OUT_MODEL. Use POINT to distinguish values created as a |
| 6612 |
result of merging. */ |
| 6613 |
|
| 6614 |
bool |
| 6615 |
region_model::can_merge_with_p (const region_model &other_model, |
| 6616 |
const program_point &point, |
| 6617 |
region_model *out_model, |
| 6618 |
const extrinsic_state *ext_state, |
| 6619 |
const program_state *state_a, |
| 6620 |
const program_state *state_b) const |
| 6621 |
{ |
| 6622 |
gcc_assert (out_model); |
| 6623 |
gcc_assert (m_mgr == other_model.m_mgr); |
| 6624 |
gcc_assert (m_mgr == out_model->m_mgr); |
| 6625 |
|
| 6626 |
if (m_current_frame != other_model.m_current_frame) |
| 6627 |
return false; |
| 6628 |
out_model->m_current_frame = m_current_frame; |
| 6629 |
|
| 6630 |
model_merger m (this, &other_model, point, out_model, |
| 6631 |
ext_state, state_a, state_b); |
| 6632 |
|
| 6633 |
if (!store::can_merge_p (&m_store, &other_model.m_store, |
| 6634 |
&out_model->m_store, m_mgr->get_store_manager (), |
| 6635 |
&m)) |
| 6636 |
return false; |
| 6637 |
|
| 6638 |
if (!m_dynamic_extents.can_merge_with_p (other_model.m_dynamic_extents, |
| 6639 |
&out_model->m_dynamic_extents)) |
| 6640 |
return false; |
| 6641 |
|
| 6642 |
/* Merge constraints. */ |
| 6643 |
constraint_manager::merge (*m_constraints, |
| 6644 |
*other_model.m_constraints, |
| 6645 |
out_model->m_constraints); |
| 6646 |
|
| 6647 |
for (auto iter : m.m_svals_changing_meaning) |
| 6648 |
out_model->m_constraints->purge_state_involving (iter); |
| 6649 |
|
| 6650 |
return true; |
| 6651 |
} |
| 6652 |
|
| 6653 |
/* Attempt to get the fndecl used at CALL, if known, or NULL_TREE |
| 6654 |
otherwise. */ |
| 6655 |
|
| 6656 |
tree |
| 6657 |
region_model::get_fndecl_for_call (const gcall *call, |
| 6658 |
region_model_context *ctxt) |
| 6659 |
{ |
| 6660 |
tree fn_ptr = gimple_call_fn (call); |
| 6661 |
if (fn_ptr == NULL_TREE) |
| 6662 |
return NULL_TREE; |
| 6663 |
const svalue *fn_ptr_sval = get_rvalue (fn_ptr, ctxt); |
| 6664 |
if (const region_svalue *fn_ptr_ptr |
| 6665 |
= fn_ptr_sval->dyn_cast_region_svalue ()) |
| 6666 |
{ |
| 6667 |
const region *reg = fn_ptr_ptr->get_pointee (); |
| 6668 |
if (const function_region *fn_reg = reg->dyn_cast_function_region ()) |
| 6669 |
{ |
| 6670 |
tree fn_decl = fn_reg->get_fndecl (); |
| 6671 |
cgraph_node *node = cgraph_node::get (fn_decl); |
| 6672 |
if (!node) |
| 6673 |
return NULL_TREE; |
| 6674 |
const cgraph_node *ultimate_node = node->ultimate_alias_target (); |
| 6675 |
if (ultimate_node) |
| 6676 |
return ultimate_node->decl; |
| 6677 |
} |
| 6678 |
} |
| 6679 |
|
| 6680 |
return NULL_TREE; |
| 6681 |
} |
| 6682 |
|
| 6683 |
/* Would be much simpler to use a lambda here, if it were supported. */ |
| 6684 |
|
| 6685 |
struct append_regions_cb_data |
| 6686 |
{ |
| 6687 |
const region_model *model; |
| 6688 |
auto_vec<const decl_region *> *out; |
| 6689 |
}; |
| 6690 |
|
| 6691 |
/* Populate *OUT with all decl_regions in the current |
| 6692 |
frame that have clusters within the store. */ |
| 6693 |
|
| 6694 |
void |
| 6695 |
region_model:: |
| 6696 |
get_regions_for_current_frame (auto_vec<const decl_region *> *out) const |
| 6697 |
{ |
| 6698 |
append_regions_cb_data data; |
| 6699 |
data.model = this; |
| 6700 |
data.out = out; |
| 6701 |
m_store.for_each_cluster (append_regions_cb, &data); |
| 6702 |
} |
| 6703 |
|
| 6704 |
/* Implementation detail of get_regions_for_current_frame. */ |
| 6705 |
|
| 6706 |
void |
| 6707 |
region_model::append_regions_cb (const region *base_reg, |
| 6708 |
append_regions_cb_data *cb_data) |
| 6709 |
{ |
| 6710 |
if (base_reg->get_parent_region () != cb_data->model->m_current_frame) |
| 6711 |
return; |
| 6712 |
if (const decl_region *decl_reg = base_reg->dyn_cast_decl_region ()) |
| 6713 |
cb_data->out->safe_push (decl_reg); |
| 6714 |
} |
| 6715 |
|
| 6716 |
|
| 6717 |
/* Abstract class for diagnostics related to the use of |
| 6718 |
floating-point arithmetic where precision is needed. */ |
| 6719 |
|
| 6720 |
class imprecise_floating_point_arithmetic : public pending_diagnostic |
| 6721 |
{ |
| 6722 |
public: |
| 6723 |
int get_controlling_option () const final override |
| 6724 |
{ |
| 6725 |
return OPT_Wanalyzer_imprecise_fp_arithmetic; |
| 6726 |
} |
| 6727 |
}; |
| 6728 |
|
| 6729 |
/* Concrete diagnostic to complain about uses of floating-point arithmetic |
| 6730 |
in the size argument of malloc etc. */ |
| 6731 |
|
| 6732 |
class float_as_size_arg : public imprecise_floating_point_arithmetic |
| 6733 |
{ |
| 6734 |
public: |
| 6735 |
float_as_size_arg (tree arg) : m_arg (arg) |
| 6736 |
{} |
| 6737 |
|
| 6738 |
const char *get_kind () const final override |
| 6739 |
{ |
| 6740 |
return "float_as_size_arg_diagnostic"; |
| 6741 |
} |
| 6742 |
|
| 6743 |
bool subclass_equal_p (const pending_diagnostic &other) const final override |
| 6744 |
{ |
| 6745 |
return same_tree_p (m_arg, ((const float_as_size_arg &) other).m_arg); |
| 6746 |
} |
| 6747 |
|
| 6748 |
bool emit (diagnostic_emission_context &ctxt) final override |
| 6749 |
{ |
| 6750 |
bool warned = ctxt.warn ("use of floating-point arithmetic here might" |
| 6751 |
" yield unexpected results"); |
| 6752 |
if (warned) |
| 6753 |
inform (ctxt.get_location (), |
| 6754 |
"only use operands of an integer type" |
| 6755 |
" inside the size argument"); |
| 6756 |
return warned; |
| 6757 |
} |
| 6758 |
|
| 6759 |
bool |
| 6760 |
describe_final_event (pretty_printer &pp, |
| 6761 |
const evdesc::final_event &) final override |
| 6762 |
{ |
| 6763 |
if (m_arg) |
| 6764 |
pp_printf (&pp, |
| 6765 |
"operand %qE is of type %qT", |
| 6766 |
m_arg, TREE_TYPE (m_arg)); |
| 6767 |
else |
| 6768 |
pp_printf (&pp, |
| 6769 |
"at least one operand of the size argument is" |
| 6770 |
" of a floating-point type"); |
| 6771 |
return true; |
| 6772 |
} |
| 6773 |
|
| 6774 |
private: |
| 6775 |
tree m_arg; |
| 6776 |
}; |
| 6777 |
|
| 6778 |
/* Visitor to find uses of floating-point variables/constants in an svalue. */ |
| 6779 |
|
| 6780 |
class contains_floating_point_visitor : public visitor |
| 6781 |
{ |
| 6782 |
public: |
| 6783 |
contains_floating_point_visitor (const svalue *root_sval) : m_result (NULL) |
| 6784 |
{ |
| 6785 |
root_sval->accept (this); |
| 6786 |
} |
| 6787 |
|
| 6788 |
const svalue *get_svalue_to_report () |
| 6789 |
{ |
| 6790 |
return m_result; |
| 6791 |
} |
| 6792 |
|
| 6793 |
void visit_constant_svalue (const constant_svalue *sval) final override |
| 6794 |
{ |
| 6795 |
/* At the point the analyzer runs, constant integer operands in a floating |
| 6796 |
point expression are already implictly converted to floating-points. |
| 6797 |
Thus, we do prefer to report non-constants such that the diagnostic |
| 6798 |
always reports a floating-point operand. */ |
| 6799 |
tree type = sval->get_type (); |
| 6800 |
if (type && FLOAT_TYPE_P (type) && !m_result) |
| 6801 |
m_result = sval; |
| 6802 |
} |
| 6803 |
|
| 6804 |
void visit_conjured_svalue (const conjured_svalue *sval) final override |
| 6805 |
{ |
| 6806 |
tree type = sval->get_type (); |
| 6807 |
if (type && FLOAT_TYPE_P (type)) |
| 6808 |
m_result = sval; |
| 6809 |
} |
| 6810 |
|
| 6811 |
void visit_initial_svalue (const initial_svalue *sval) final override |
| 6812 |
{ |
| 6813 |
tree type = sval->get_type (); |
| 6814 |
if (type && FLOAT_TYPE_P (type)) |
| 6815 |
m_result = sval; |
| 6816 |
} |
| 6817 |
|
| 6818 |
private: |
| 6819 |
/* Non-null if at least one floating-point operand was found. */ |
| 6820 |
const svalue *m_result; |
| 6821 |
}; |
| 6822 |
|
| 6823 |
/* May complain about uses of floating-point operands in SIZE_IN_BYTES. */ |
| 6824 |
|
| 6825 |
void |
| 6826 |
region_model::check_dynamic_size_for_floats (const svalue *size_in_bytes, |
| 6827 |
region_model_context *ctxt) const |
| 6828 |
{ |
| 6829 |
gcc_assert (ctxt); |
| 6830 |
|
| 6831 |
contains_floating_point_visitor v (size_in_bytes); |
| 6832 |
if (const svalue *float_sval = v.get_svalue_to_report ()) |
| 6833 |
{ |
| 6834 |
tree diag_arg = get_representative_tree (float_sval); |
| 6835 |
ctxt->warn (make_unique<float_as_size_arg> (diag_arg)); |
| 6836 |
} |
| 6837 |
} |
| 6838 |
|
| 6839 |
/* Return a region describing a heap-allocated block of memory. |
| 6840 |
Use CTXT to complain about tainted sizes. |
| 6841 |
|
| 6842 |
Reuse an existing heap_allocated_region if it's not being referenced by |
| 6843 |
this region_model; otherwise create a new one. |
| 6844 |
|
| 6845 |
Optionally (update_state_machine) transitions the pointer pointing to the |
| 6846 |
heap_allocated_region from start to assumed non-null. */ |
| 6847 |
|
| 6848 |
const region * |
| 6849 |
region_model::get_or_create_region_for_heap_alloc (const svalue *size_in_bytes, |
| 6850 |
region_model_context *ctxt, |
| 6851 |
bool update_state_machine, |
| 6852 |
const call_details *cd) |
| 6853 |
{ |
| 6854 |
/* Determine which regions are referenced in this region_model, so that |
| 6855 |
we can reuse an existing heap_allocated_region if it's not in use on |
| 6856 |
this path. */ |
| 6857 |
auto_bitmap base_regs_in_use; |
| 6858 |
get_referenced_base_regions (base_regs_in_use); |
| 6859 |
|
| 6860 |
/* Don't reuse regions that are marked as TOUCHED. */ |
| 6861 |
for (store::cluster_map_t::iterator iter = m_store.begin (); |
| 6862 |
iter != m_store.end (); ++iter) |
| 6863 |
if ((*iter).second->touched_p ()) |
| 6864 |
{ |
| 6865 |
const region *base_reg = (*iter).first; |
| 6866 |
bitmap_set_bit (base_regs_in_use, base_reg->get_id ()); |
| 6867 |
} |
| 6868 |
|
| 6869 |
const region *reg |
| 6870 |
= m_mgr->get_or_create_region_for_heap_alloc (base_regs_in_use); |
| 6871 |
if (size_in_bytes) |
| 6872 |
if (compat_types_p (size_in_bytes->get_type (), size_type_node)) |
| 6873 |
set_dynamic_extents (reg, size_in_bytes, ctxt); |
| 6874 |
|
| 6875 |
if (update_state_machine && cd) |
| 6876 |
{ |
| 6877 |
const svalue *ptr_sval |
| 6878 |
= m_mgr->get_ptr_svalue (cd->get_lhs_type (), reg); |
| 6879 |
transition_ptr_sval_non_null (ctxt, ptr_sval); |
| 6880 |
} |
| 6881 |
|
| 6882 |
return reg; |
| 6883 |
} |
| 6884 |
|
| 6885 |
/* Populate OUT_IDS with the set of IDs of those base regions which are |
| 6886 |
reachable in this region_model. */ |
| 6887 |
|
| 6888 |
void |
| 6889 |
region_model::get_referenced_base_regions (auto_bitmap &out_ids) const |
| 6890 |
{ |
| 6891 |
reachable_regions reachable_regs (const_cast<region_model *> (this)); |
| 6892 |
m_store.for_each_cluster (reachable_regions::init_cluster_cb, |
| 6893 |
&reachable_regs); |
| 6894 |
/* Get regions for locals that have explicitly bound values. */ |
| 6895 |
for (store::cluster_map_t::iterator iter = m_store.begin (); |
| 6896 |
iter != m_store.end (); ++iter) |
| 6897 |
{ |
| 6898 |
const region *base_reg = (*iter).first; |
| 6899 |
if (const region *parent = base_reg->get_parent_region ()) |
| 6900 |
if (parent->get_kind () == RK_FRAME) |
| 6901 |
reachable_regs.add (base_reg, false); |
| 6902 |
} |
| 6903 |
|
| 6904 |
bitmap_clear (out_ids); |
| 6905 |
for (auto iter_reg : reachable_regs) |
| 6906 |
bitmap_set_bit (out_ids, iter_reg->get_id ()); |
| 6907 |
} |
| 6908 |
|
| 6909 |
/* Return a new region describing a block of memory allocated within the |
| 6910 |
current frame. |
| 6911 |
Use CTXT to complain about tainted sizes. */ |
| 6912 |
|
| 6913 |
const region * |
| 6914 |
region_model::create_region_for_alloca (const svalue *size_in_bytes, |
| 6915 |
region_model_context *ctxt) |
| 6916 |
{ |
| 6917 |
const region *reg = m_mgr->create_region_for_alloca (m_current_frame); |
| 6918 |
if (compat_types_p (size_in_bytes->get_type (), size_type_node)) |
| 6919 |
set_dynamic_extents (reg, size_in_bytes, ctxt); |
| 6920 |
return reg; |
| 6921 |
} |
| 6922 |
|
| 6923 |
/* Record that the size of REG is SIZE_IN_BYTES. |
| 6924 |
Use CTXT to complain about tainted sizes. */ |
| 6925 |
|
| 6926 |
void |
| 6927 |
region_model::set_dynamic_extents (const region *reg, |
| 6928 |
const svalue *size_in_bytes, |
| 6929 |
region_model_context *ctxt) |
| 6930 |
{ |
| 6931 |
assert_compat_types (size_in_bytes->get_type (), size_type_node); |
| 6932 |
if (ctxt) |
| 6933 |
{ |
| 6934 |
check_dynamic_size_for_taint (reg->get_memory_space (), size_in_bytes, |
| 6935 |
ctxt); |
| 6936 |
check_dynamic_size_for_floats (size_in_bytes, ctxt); |
| 6937 |
} |
| 6938 |
m_dynamic_extents.put (reg, size_in_bytes); |
| 6939 |
} |
| 6940 |
|
| 6941 |
/* Get the recording of REG in bytes, or NULL if no dynamic size was |
| 6942 |
recorded. */ |
| 6943 |
|
| 6944 |
const svalue * |
| 6945 |
region_model::get_dynamic_extents (const region *reg) const |
| 6946 |
{ |
| 6947 |
if (const svalue * const *slot = m_dynamic_extents.get (reg)) |
| 6948 |
return *slot; |
| 6949 |
return NULL; |
| 6950 |
} |
| 6951 |
|
| 6952 |
/* Unset any recorded dynamic size of REG. */ |
| 6953 |
|
| 6954 |
void |
| 6955 |
region_model::unset_dynamic_extents (const region *reg) |
| 6956 |
{ |
| 6957 |
m_dynamic_extents.remove (reg); |
| 6958 |
} |
| 6959 |
|
| 6960 |
/* A subclass of pending_diagnostic for complaining about uninitialized data |
| 6961 |
being copied across a trust boundary to an untrusted output |
| 6962 |
(e.g. copy_to_user infoleaks in the Linux kernel). */ |
| 6963 |
|
| 6964 |
class exposure_through_uninit_copy |
| 6965 |
: public pending_diagnostic_subclass<exposure_through_uninit_copy> |
| 6966 |
{ |
| 6967 |
public: |
| 6968 |
exposure_through_uninit_copy (const region *src_region, |
| 6969 |
const region *dest_region, |
| 6970 |
const svalue *copied_sval) |
| 6971 |
: m_src_region (src_region), |
| 6972 |
m_dest_region (dest_region), |
| 6973 |
m_copied_sval (copied_sval) |
| 6974 |
{ |
| 6975 |
gcc_assert (m_copied_sval->get_kind () == SK_POISONED |
| 6976 |
|| m_copied_sval->get_kind () == SK_COMPOUND); |
| 6977 |
} |
| 6978 |
|
| 6979 |
const char *get_kind () const final override |
| 6980 |
{ |
| 6981 |
return "exposure_through_uninit_copy"; |
| 6982 |
} |
| 6983 |
|
| 6984 |
bool operator== (const exposure_through_uninit_copy &other) const |
| 6985 |
{ |
| 6986 |
return (m_src_region == other.m_src_region |
| 6987 |
&& m_dest_region == other.m_dest_region |
| 6988 |
&& m_copied_sval == other.m_copied_sval); |
| 6989 |
} |
| 6990 |
|
| 6991 |
int get_controlling_option () const final override |
| 6992 |
{ |
| 6993 |
return OPT_Wanalyzer_exposure_through_uninit_copy; |
| 6994 |
} |
| 6995 |
|
| 6996 |
bool emit (diagnostic_emission_context &ctxt) final override |
| 6997 |
{ |
| 6998 |
/* CWE-200: Exposure of Sensitive Information to an Unauthorized Actor. */ |
| 6999 |
ctxt.add_cwe (200); |
| 7000 |
enum memory_space mem_space = get_src_memory_space (); |
| 7001 |
bool warned; |
| 7002 |
switch (mem_space) |
| 7003 |
{ |
| 7004 |
default: |
| 7005 |
warned = ctxt.warn ("potential exposure of sensitive information" |
| 7006 |
" by copying uninitialized data" |
| 7007 |
" across trust boundary"); |
| 7008 |
break; |
| 7009 |
case MEMSPACE_STACK: |
| 7010 |
warned = ctxt.warn ("potential exposure of sensitive information" |
| 7011 |
" by copying uninitialized data from stack" |
| 7012 |
" across trust boundary"); |
| 7013 |
break; |
| 7014 |
case MEMSPACE_HEAP: |
| 7015 |
warned = ctxt.warn ("potential exposure of sensitive information" |
| 7016 |
" by copying uninitialized data from heap" |
| 7017 |
" across trust boundary"); |
| 7018 |
break; |
| 7019 |
} |
| 7020 |
if (warned) |
| 7021 |
{ |
| 7022 |
const location_t loc = ctxt.get_location (); |
| 7023 |
inform_number_of_uninit_bits (loc); |
| 7024 |
complain_about_uninit_ranges (loc); |
| 7025 |
|
| 7026 |
if (mem_space == MEMSPACE_STACK) |
| 7027 |
maybe_emit_fixit_hint (); |
| 7028 |
} |
| 7029 |
return warned; |
| 7030 |
} |
| 7031 |
|
| 7032 |
bool |
| 7033 |
describe_final_event (pretty_printer &pp, |
| 7034 |
const evdesc::final_event &) final override |
| 7035 |
{ |
| 7036 |
enum memory_space mem_space = get_src_memory_space (); |
| 7037 |
switch (mem_space) |
| 7038 |
{ |
| 7039 |
default: |
| 7040 |
pp_string (&pp, "uninitialized data copied here"); |
| 7041 |
return true; |
| 7042 |
|
| 7043 |
case MEMSPACE_STACK: |
| 7044 |
pp_string (&pp, "uninitialized data copied from stack here"); |
| 7045 |
return true; |
| 7046 |
|
| 7047 |
case MEMSPACE_HEAP: |
| 7048 |
pp_string (&pp, "uninitialized data copied from heap here"); |
| 7049 |
return true; |
| 7050 |
} |
| 7051 |
} |
| 7052 |
|
| 7053 |
void mark_interesting_stuff (interesting_t *interest) final override |
| 7054 |
{ |
| 7055 |
if (m_src_region) |
| 7056 |
interest->add_region_creation (m_src_region); |
| 7057 |
} |
| 7058 |
|
| 7059 |
void |
| 7060 |
maybe_add_sarif_properties (sarif_object &result_obj) const final override |
| 7061 |
{ |
| 7062 |
sarif_property_bag &props = result_obj.get_or_create_properties (); |
| 7063 |
#define PROPERTY_PREFIX "gcc/-Wanalyzer-exposure-through-uninit-copy/" |
| 7064 |
props.set (PROPERTY_PREFIX "src_region", m_src_region->to_json ()); |
| 7065 |
props.set (PROPERTY_PREFIX "dest_region", m_dest_region->to_json ()); |
| 7066 |
props.set (PROPERTY_PREFIX "copied_sval", m_copied_sval->to_json ()); |
| 7067 |
#undef PROPERTY_PREFIX |
| 7068 |
} |
| 7069 |
|
| 7070 |
private: |
| 7071 |
enum memory_space get_src_memory_space () const |
| 7072 |
{ |
| 7073 |
return m_src_region ? m_src_region->get_memory_space () : MEMSPACE_UNKNOWN; |
| 7074 |
} |
| 7075 |
|
| 7076 |
bit_size_t calc_num_uninit_bits () const |
| 7077 |
{ |
| 7078 |
switch (m_copied_sval->get_kind ()) |
| 7079 |
{ |
| 7080 |
default: |
| 7081 |
gcc_unreachable (); |
| 7082 |
break; |
| 7083 |
case SK_POISONED: |
| 7084 |
{ |
| 7085 |
const poisoned_svalue *poisoned_sval |
| 7086 |
= as_a <const poisoned_svalue *> (m_copied_sval); |
| 7087 |
gcc_assert (poisoned_sval->get_poison_kind () == POISON_KIND_UNINIT); |
| 7088 |
|
| 7089 |
/* Give up if don't have type information. */ |
| 7090 |
if (m_copied_sval->get_type () == NULL_TREE) |
| 7091 |
return 0; |
| 7092 |
|
| 7093 |
bit_size_t size_in_bits; |
| 7094 |
if (int_size_in_bits (m_copied_sval->get_type (), &size_in_bits)) |
| 7095 |
return size_in_bits; |
| 7096 |
|
| 7097 |
/* Give up if we can't get the size of the type. */ |
| 7098 |
return 0; |
| 7099 |
} |
| 7100 |
break; |
| 7101 |
case SK_COMPOUND: |
| 7102 |
{ |
| 7103 |
const compound_svalue *compound_sval |
| 7104 |
= as_a <const compound_svalue *> (m_copied_sval); |
| 7105 |
bit_size_t result = 0; |
| 7106 |
/* Find keys for uninit svals. */ |
| 7107 |
for (auto iter : *compound_sval) |
| 7108 |
{ |
| 7109 |
const svalue *sval = iter.second; |
| 7110 |
if (const poisoned_svalue *psval |
| 7111 |
= sval->dyn_cast_poisoned_svalue ()) |
| 7112 |
if (psval->get_poison_kind () == POISON_KIND_UNINIT) |
| 7113 |
{ |
| 7114 |
const binding_key *key = iter.first; |
| 7115 |
const concrete_binding *ckey |
| 7116 |
= key->dyn_cast_concrete_binding (); |
| 7117 |
gcc_assert (ckey); |
| 7118 |
result += ckey->get_size_in_bits (); |
| 7119 |
} |
| 7120 |
} |
| 7121 |
return result; |
| 7122 |
} |
| 7123 |
} |
| 7124 |
} |
| 7125 |
|
| 7126 |
void inform_number_of_uninit_bits (location_t loc) const |
| 7127 |
{ |
| 7128 |
bit_size_t num_uninit_bits = calc_num_uninit_bits (); |
| 7129 |
if (num_uninit_bits <= 0) |
| 7130 |
return; |
| 7131 |
if (num_uninit_bits % BITS_PER_UNIT == 0) |
| 7132 |
{ |
| 7133 |
/* Express in bytes. */ |
| 7134 |
byte_size_t num_uninit_bytes = num_uninit_bits / BITS_PER_UNIT; |
| 7135 |
if (num_uninit_bytes == 1) |
| 7136 |
inform (loc, "1 byte is uninitialized"); |
| 7137 |
else |
| 7138 |
inform (loc, |
| 7139 |
"%wu bytes are uninitialized", num_uninit_bytes.to_uhwi ()); |
| 7140 |
} |
| 7141 |
else |
| 7142 |
{ |
| 7143 |
/* Express in bits. */ |
| 7144 |
if (num_uninit_bits == 1) |
| 7145 |
inform (loc, "1 bit is uninitialized"); |
| 7146 |
else |
| 7147 |
inform (loc, |
| 7148 |
"%wu bits are uninitialized", num_uninit_bits.to_uhwi ()); |
| 7149 |
} |
| 7150 |
} |
| 7151 |
|
| 7152 |
void complain_about_uninit_ranges (location_t loc) const |
| 7153 |
{ |
| 7154 |
if (const compound_svalue *compound_sval |
| 7155 |
= m_copied_sval->dyn_cast_compound_svalue ()) |
| 7156 |
{ |
| 7157 |
/* Find keys for uninit svals. */ |
| 7158 |
auto_vec<const concrete_binding *> uninit_keys; |
| 7159 |
for (auto iter : *compound_sval) |
| 7160 |
{ |
| 7161 |
const svalue *sval = iter.second; |
| 7162 |
if (const poisoned_svalue *psval |
| 7163 |
= sval->dyn_cast_poisoned_svalue ()) |
| 7164 |
if (psval->get_poison_kind () == POISON_KIND_UNINIT) |
| 7165 |
{ |
| 7166 |
const binding_key *key = iter.first; |
| 7167 |
const concrete_binding *ckey |
| 7168 |
= key->dyn_cast_concrete_binding (); |
| 7169 |
gcc_assert (ckey); |
| 7170 |
uninit_keys.safe_push (ckey); |
| 7171 |
} |
| 7172 |
} |
| 7173 |
/* Complain about them in sorted order. */ |
| 7174 |
uninit_keys.qsort (concrete_binding::cmp_ptr_ptr); |
| 7175 |
|
| 7176 |
std::unique_ptr<record_layout> layout; |
| 7177 |
|
| 7178 |
tree type = m_copied_sval->get_type (); |
| 7179 |
if (type && TREE_CODE (type) == RECORD_TYPE) |
| 7180 |
{ |
| 7181 |
// (std::make_unique is C++14) |
| 7182 |
layout = std::unique_ptr<record_layout> (new record_layout (type)); |
| 7183 |
|
| 7184 |
if (0) |
| 7185 |
layout->dump (); |
| 7186 |
} |
| 7187 |
|
| 7188 |
unsigned i; |
| 7189 |
const concrete_binding *ckey; |
| 7190 |
FOR_EACH_VEC_ELT (uninit_keys, i, ckey) |
| 7191 |
{ |
| 7192 |
bit_offset_t start_bit = ckey->get_start_bit_offset (); |
| 7193 |
bit_offset_t next_bit = ckey->get_next_bit_offset (); |
| 7194 |
complain_about_uninit_range (loc, start_bit, next_bit, |
| 7195 |
layout.get ()); |
| 7196 |
} |
| 7197 |
} |
| 7198 |
} |
| 7199 |
|
| 7200 |
void complain_about_uninit_range (location_t loc, |
| 7201 |
bit_offset_t start_bit, |
| 7202 |
bit_offset_t next_bit, |
| 7203 |
const record_layout *layout) const |
| 7204 |
{ |
| 7205 |
if (layout) |
| 7206 |
{ |
| 7207 |
while (start_bit < next_bit) |
| 7208 |
{ |
| 7209 |
if (const record_layout::item *item |
| 7210 |
= layout->get_item_at (start_bit)) |
| 7211 |
{ |
| 7212 |
gcc_assert (start_bit >= item->get_start_bit_offset ()); |
| 7213 |
gcc_assert (start_bit < item->get_next_bit_offset ()); |
| 7214 |
if (item->get_start_bit_offset () == start_bit |
| 7215 |
&& item->get_next_bit_offset () <= next_bit) |
| 7216 |
complain_about_fully_uninit_item (*item); |
| 7217 |
else |
| 7218 |
complain_about_partially_uninit_item (*item); |
| 7219 |
start_bit = item->get_next_bit_offset (); |
| 7220 |
continue; |
| 7221 |
} |
| 7222 |
else |
| 7223 |
break; |
| 7224 |
} |
| 7225 |
} |
| 7226 |
|
| 7227 |
if (start_bit >= next_bit) |
| 7228 |
return; |
| 7229 |
|
| 7230 |
if (start_bit % 8 == 0 && next_bit % 8 == 0) |
| 7231 |
{ |
| 7232 |
/* Express in bytes. */ |
| 7233 |
byte_offset_t start_byte = start_bit / 8; |
| 7234 |
byte_offset_t last_byte = (next_bit / 8) - 1; |
| 7235 |
if (last_byte == start_byte) |
| 7236 |
inform (loc, |
| 7237 |
"byte %wu is uninitialized", |
| 7238 |
start_byte.to_uhwi ()); |
| 7239 |
else |
| 7240 |
inform (loc, |
| 7241 |
"bytes %wu - %wu are uninitialized", |
| 7242 |
start_byte.to_uhwi (), |
| 7243 |
last_byte.to_uhwi ()); |
| 7244 |
} |
| 7245 |
else |
| 7246 |
{ |
| 7247 |
/* Express in bits. */ |
| 7248 |
bit_offset_t last_bit = next_bit - 1; |
| 7249 |
if (last_bit == start_bit) |
| 7250 |
inform (loc, |
| 7251 |
"bit %wu is uninitialized", |
| 7252 |
start_bit.to_uhwi ()); |
| 7253 |
else |
| 7254 |
inform (loc, |
| 7255 |
"bits %wu - %wu are uninitialized", |
| 7256 |
start_bit.to_uhwi (), |
| 7257 |
last_bit.to_uhwi ()); |
| 7258 |
} |
| 7259 |
} |
| 7260 |
|
| 7261 |
static void |
| 7262 |
complain_about_fully_uninit_item (const record_layout::item &item) |
| 7263 |
{ |
| 7264 |
tree field = item.m_field; |
| 7265 |
bit_size_t num_bits = item.m_bit_range.m_size_in_bits; |
| 7266 |
if (item.m_is_padding) |
| 7267 |
{ |
| 7268 |
if (num_bits % 8 == 0) |
| 7269 |
{ |
| 7270 |
/* Express in bytes. */ |
| 7271 |
byte_size_t num_bytes = num_bits / BITS_PER_UNIT; |
| 7272 |
if (num_bytes == 1) |
| 7273 |
inform (DECL_SOURCE_LOCATION (field), |
| 7274 |
"padding after field %qD is uninitialized (1 byte)", |
| 7275 |
field); |
| 7276 |
else |
| 7277 |
inform (DECL_SOURCE_LOCATION (field), |
| 7278 |
"padding after field %qD is uninitialized (%wu bytes)", |
| 7279 |
field, num_bytes.to_uhwi ()); |
| 7280 |
} |
| 7281 |
else |
| 7282 |
{ |
| 7283 |
/* Express in bits. */ |
| 7284 |
if (num_bits == 1) |
| 7285 |
inform (DECL_SOURCE_LOCATION (field), |
| 7286 |
"padding after field %qD is uninitialized (1 bit)", |
| 7287 |
field); |
| 7288 |
else |
| 7289 |
inform (DECL_SOURCE_LOCATION (field), |
| 7290 |
"padding after field %qD is uninitialized (%wu bits)", |
| 7291 |
field, num_bits.to_uhwi ()); |
| 7292 |
} |
| 7293 |
} |
| 7294 |
else |
| 7295 |
{ |
| 7296 |
if (num_bits % 8 == 0) |
| 7297 |
{ |
| 7298 |
/* Express in bytes. */ |
| 7299 |
byte_size_t num_bytes = num_bits / BITS_PER_UNIT; |
| 7300 |
if (num_bytes == 1) |
| 7301 |
inform (DECL_SOURCE_LOCATION (field), |
| 7302 |
"field %qD is uninitialized (1 byte)", field); |
| 7303 |
else |
| 7304 |
inform (DECL_SOURCE_LOCATION (field), |
| 7305 |
"field %qD is uninitialized (%wu bytes)", |
| 7306 |
field, num_bytes.to_uhwi ()); |
| 7307 |
} |
| 7308 |
else |
| 7309 |
{ |
| 7310 |
/* Express in bits. */ |
| 7311 |
if (num_bits == 1) |
| 7312 |
inform (DECL_SOURCE_LOCATION (field), |
| 7313 |
"field %qD is uninitialized (1 bit)", field); |
| 7314 |
else |
| 7315 |
inform (DECL_SOURCE_LOCATION (field), |
| 7316 |
"field %qD is uninitialized (%wu bits)", |
| 7317 |
field, num_bits.to_uhwi ()); |
| 7318 |
} |
| 7319 |
} |
| 7320 |
} |
| 7321 |
|
| 7322 |
static void |
| 7323 |
complain_about_partially_uninit_item (const record_layout::item &item) |
| 7324 |
{ |
| 7325 |
tree field = item.m_field; |
| 7326 |
if (item.m_is_padding) |
| 7327 |
inform (DECL_SOURCE_LOCATION (field), |
| 7328 |
"padding after field %qD is partially uninitialized", |
| 7329 |
field); |
| 7330 |
else |
| 7331 |
inform (DECL_SOURCE_LOCATION (field), |
| 7332 |
"field %qD is partially uninitialized", |
| 7333 |
field); |
| 7334 |
/* TODO: ideally we'd describe what parts are uninitialized. */ |
| 7335 |
} |
| 7336 |
|
| 7337 |
void maybe_emit_fixit_hint () const |
| 7338 |
{ |
| 7339 |
if (tree decl = m_src_region->maybe_get_decl ()) |
| 7340 |
{ |
| 7341 |
gcc_rich_location hint_richloc (DECL_SOURCE_LOCATION (decl)); |
| 7342 |
hint_richloc.add_fixit_insert_after (" = {0}"); |
| 7343 |
inform (&hint_richloc, |
| 7344 |
"suggest forcing zero-initialization by" |
| 7345 |
" providing a %<{0}%> initializer"); |
| 7346 |
} |
| 7347 |
} |
| 7348 |
|
| 7349 |
private: |
| 7350 |
const region *m_src_region; |
| 7351 |
const region *m_dest_region; |
| 7352 |
const svalue *m_copied_sval; |
| 7353 |
}; |
| 7354 |
|
| 7355 |
/* Return true if any part of SVAL is uninitialized. */ |
| 7356 |
|
| 7357 |
static bool |
| 7358 |
contains_uninit_p (const svalue *sval) |
| 7359 |
{ |
| 7360 |
switch (sval->get_kind ()) |
| 7361 |
{ |
| 7362 |
default: |
| 7363 |
return false; |
| 7364 |
case SK_POISONED: |
| 7365 |
{ |
| 7366 |
const poisoned_svalue *psval |
| 7367 |
= as_a <const poisoned_svalue *> (sval); |
| 7368 |
return psval->get_poison_kind () == POISON_KIND_UNINIT; |
| 7369 |
} |
| 7370 |
case SK_COMPOUND: |
| 7371 |
{ |
| 7372 |
const compound_svalue *compound_sval |
| 7373 |
= as_a <const compound_svalue *> (sval); |
| 7374 |
|
| 7375 |
for (auto iter : *compound_sval) |
| 7376 |
{ |
| 7377 |
const svalue *sval = iter.second; |
| 7378 |
if (const poisoned_svalue *psval |
| 7379 |
= sval->dyn_cast_poisoned_svalue ()) |
| 7380 |
if (psval->get_poison_kind () == POISON_KIND_UNINIT) |
| 7381 |
return true; |
| 7382 |
} |
| 7383 |
|
| 7384 |
return false; |
| 7385 |
} |
| 7386 |
} |
| 7387 |
} |
| 7388 |
|
| 7389 |
/* Function for use by plugins when simulating writing data through a |
| 7390 |
pointer to an "untrusted" region DST_REG (and thus crossing a security |
| 7391 |
boundary), such as copying data to user space in an OS kernel. |
| 7392 |
|
| 7393 |
Check that COPIED_SVAL is fully initialized. If not, complain about |
| 7394 |
an infoleak to CTXT. |
| 7395 |
|
| 7396 |
SRC_REG can be NULL; if non-NULL it is used as a hint in the diagnostic |
| 7397 |
as to where COPIED_SVAL came from. */ |
| 7398 |
|
| 7399 |
void |
| 7400 |
region_model::maybe_complain_about_infoleak (const region *dst_reg, |
| 7401 |
const svalue *copied_sval, |
| 7402 |
const region *src_reg, |
| 7403 |
region_model_context *ctxt) |
| 7404 |
{ |
| 7405 |
/* Check for exposure. */ |
| 7406 |
if (contains_uninit_p (copied_sval)) |
| 7407 |
ctxt->warn (make_unique<exposure_through_uninit_copy> (src_reg, |
| 7408 |
dst_reg, |
| 7409 |
copied_sval)); |
| 7410 |
} |
| 7411 |
|
| 7412 |
/* Set errno to a positive symbolic int, as if some error has occurred. */ |
| 7413 |
|
| 7414 |
void |
| 7415 |
region_model::set_errno (const call_details &cd) |
| 7416 |
{ |
| 7417 |
const region *errno_reg = m_mgr->get_errno_region (); |
| 7418 |
conjured_purge p (this, cd.get_ctxt ()); |
| 7419 |
const svalue *new_errno_sval |
| 7420 |
= m_mgr->get_or_create_conjured_svalue (integer_type_node, |
| 7421 |
cd.get_call_stmt (), |
| 7422 |
errno_reg, p); |
| 7423 |
const svalue *zero |
| 7424 |
= m_mgr->get_or_create_int_cst (integer_type_node, 0); |
| 7425 |
add_constraint (new_errno_sval, GT_EXPR, zero, cd.get_ctxt ()); |
| 7426 |
set_value (errno_reg, new_errno_sval, cd.get_ctxt ()); |
| 7427 |
} |
| 7428 |
|
| 7429 |
/* class noop_region_model_context : public region_model_context. */ |
| 7430 |
|
| 7431 |
void |
| 7432 |
noop_region_model_context::add_note (std::unique_ptr<pending_note>) |
| 7433 |
{ |
| 7434 |
} |
| 7435 |
|
| 7436 |
void |
| 7437 |
noop_region_model_context::add_event (std::unique_ptr<checker_event>) |
| 7438 |
{ |
| 7439 |
} |
| 7440 |
|
| 7441 |
void |
| 7442 |
noop_region_model_context::bifurcate (std::unique_ptr<custom_edge_info>) |
| 7443 |
{ |
| 7444 |
} |
| 7445 |
|
| 7446 |
void |
| 7447 |
noop_region_model_context::terminate_path () |
| 7448 |
{ |
| 7449 |
} |
| 7450 |
|
| 7451 |
/* class region_model_context_decorator : public region_model_context. */ |
| 7452 |
|
| 7453 |
void |
| 7454 |
region_model_context_decorator::add_event (std::unique_ptr<checker_event> event) |
| 7455 |
{ |
| 7456 |
if (m_inner) |
| 7457 |
m_inner->add_event (std::move (event)); |
| 7458 |
} |
| 7459 |
|
| 7460 |
/* struct model_merger. */ |
| 7461 |
|
| 7462 |
/* Dump a multiline representation of this merger to PP. */ |
| 7463 |
|
| 7464 |
void |
| 7465 |
model_merger::dump_to_pp (pretty_printer *pp, bool simple) const |
| 7466 |
{ |
| 7467 |
pp_string (pp, "model A:"); |
| 7468 |
pp_newline (pp); |
| 7469 |
m_model_a->dump_to_pp (pp, simple, true); |
| 7470 |
pp_newline (pp); |
| 7471 |
|
| 7472 |
pp_string (pp, "model B:"); |
| 7473 |
pp_newline (pp); |
| 7474 |
m_model_b->dump_to_pp (pp, simple, true); |
| 7475 |
pp_newline (pp); |
| 7476 |
|
| 7477 |
pp_string (pp, "merged model:"); |
| 7478 |
pp_newline (pp); |
| 7479 |
m_merged_model->dump_to_pp (pp, simple, true); |
| 7480 |
pp_newline (pp); |
| 7481 |
} |
| 7482 |
|
| 7483 |
/* Dump a multiline representation of this merger to FILE. */ |
| 7484 |
|
| 7485 |
void |
| 7486 |
model_merger::dump (FILE *fp, bool simple) const |
| 7487 |
{ |
| 7488 |
tree_dump_pretty_printer pp (fp); |
| 7489 |
dump_to_pp (&pp, simple); |
| 7490 |
} |
| 7491 |
|
| 7492 |
/* Dump a multiline representation of this merger to stderr. */ |
| 7493 |
|
| 7494 |
DEBUG_FUNCTION void |
| 7495 |
model_merger::dump (bool simple) const |
| 7496 |
{ |
| 7497 |
dump (stderr, simple); |
| 7498 |
} |
| 7499 |
|
| 7500 |
/* Return true if it's OK to merge SVAL with other svalues. */ |
| 7501 |
|
| 7502 |
bool |
| 7503 |
model_merger::mergeable_svalue_p (const svalue *sval) const |
| 7504 |
{ |
| 7505 |
if (m_ext_state) |
| 7506 |
{ |
| 7507 |
/* Reject merging svalues that have non-purgable sm-state, |
| 7508 |
to avoid falsely reporting memory leaks by merging them |
| 7509 |
with something else. For example, given a local var "p", |
| 7510 |
reject the merger of a: |
| 7511 |
store_a mapping "p" to a malloc-ed ptr |
| 7512 |
with: |
| 7513 |
store_b mapping "p" to a NULL ptr. */ |
| 7514 |
if (m_state_a) |
| 7515 |
if (!m_state_a->can_purge_p (*m_ext_state, sval)) |
| 7516 |
return false; |
| 7517 |
if (m_state_b) |
| 7518 |
if (!m_state_b->can_purge_p (*m_ext_state, sval)) |
| 7519 |
return false; |
| 7520 |
} |
| 7521 |
return true; |
| 7522 |
} |
| 7523 |
|
| 7524 |
/* Mark WIDENING_SVAL as changing meaning during the merge. */ |
| 7525 |
|
| 7526 |
void |
| 7527 |
model_merger::on_widening_reuse (const widening_svalue *widening_sval) |
| 7528 |
{ |
| 7529 |
m_svals_changing_meaning.add (widening_sval); |
| 7530 |
} |
| 7531 |
|
| 7532 |
} // namespace ana |
| 7533 |
|
| 7534 |
/* Dump RMODEL fully to stderr (i.e. without summarization). */ |
| 7535 |
|
| 7536 |
DEBUG_FUNCTION void |
| 7537 |
debug (const region_model &rmodel) |
| 7538 |
{ |
| 7539 |
rmodel.dump (false); |
| 7540 |
} |
| 7541 |
|
| 7542 |
/* class rejected_op_constraint : public rejected_constraint. */ |
| 7543 |
|
| 7544 |
void |
| 7545 |
rejected_op_constraint::dump_to_pp (pretty_printer *pp) const |
| 7546 |
{ |
| 7547 |
region_model m (m_model); |
| 7548 |
const svalue *lhs_sval = m.get_rvalue (m_lhs, NULL); |
| 7549 |
const svalue *rhs_sval = m.get_rvalue (m_rhs, NULL); |
| 7550 |
lhs_sval->dump_to_pp (pp, true); |
| 7551 |
pp_printf (pp, " %s ", op_symbol_code (m_op)); |
| 7552 |
rhs_sval->dump_to_pp (pp, true); |
| 7553 |
} |
| 7554 |
|
| 7555 |
/* class rejected_default_case : public rejected_constraint. */ |
| 7556 |
|
| 7557 |
void |
| 7558 |
rejected_default_case::dump_to_pp (pretty_printer *pp) const |
| 7559 |
{ |
| 7560 |
pp_string (pp, "implicit default for enum"); |
| 7561 |
} |
| 7562 |
|
| 7563 |
/* class rejected_ranges_constraint : public rejected_constraint. */ |
| 7564 |
|
| 7565 |
void |
| 7566 |
rejected_ranges_constraint::dump_to_pp (pretty_printer *pp) const |
| 7567 |
{ |
| 7568 |
region_model m (m_model); |
| 7569 |
const svalue *sval = m.get_rvalue (m_expr, NULL); |
| 7570 |
sval->dump_to_pp (pp, true); |
| 7571 |
pp_string (pp, " in "); |
| 7572 |
m_ranges->dump_to_pp (pp, true); |
| 7573 |
} |
| 7574 |
|
| 7575 |
/* class engine. */ |
| 7576 |
|
| 7577 |
/* engine's ctor. */ |
| 7578 |
|
| 7579 |
engine::engine (const supergraph *sg, logger *logger) |
| 7580 |
: m_sg (sg), m_mgr (logger) |
| 7581 |
{ |
| 7582 |
} |
| 7583 |
|
| 7584 |
/* Dump the managed objects by class to LOGGER, and the per-class totals. */ |
| 7585 |
|
| 7586 |
void |
| 7587 |
engine::log_stats (logger *logger) const |
| 7588 |
{ |
| 7589 |
m_mgr.log_stats (logger, true); |
| 7590 |
} |
| 7591 |
|
| 7592 |
namespace ana { |
| 7593 |
|
| 7594 |
#if CHECKING_P |
| 7595 |
|
| 7596 |
namespace selftest { |
| 7597 |
|
| 7598 |
/* Build a constant tree of the given type from STR. */ |
| 7599 |
|
| 7600 |
static tree |
| 7601 |
build_real_cst_from_string (tree type, const char *str) |
| 7602 |
{ |
| 7603 |
REAL_VALUE_TYPE real; |
| 7604 |
real_from_string (&real, str); |
| 7605 |
return build_real (type, real); |
| 7606 |
} |
| 7607 |
|
| 7608 |
/* Append various "interesting" constants to OUT (e.g. NaN). */ |
| 7609 |
|
| 7610 |
static void |
| 7611 |
append_interesting_constants (auto_vec<tree> *out) |
| 7612 |
{ |
| 7613 |
out->safe_push (integer_zero_node); |
| 7614 |
out->safe_push (build_int_cst (integer_type_node, 42)); |
| 7615 |
out->safe_push (build_int_cst (unsigned_type_node, 0)); |
| 7616 |
out->safe_push (build_int_cst (unsigned_type_node, 42)); |
| 7617 |
out->safe_push (build_real_cst_from_string (float_type_node, "QNaN")); |
| 7618 |
out->safe_push (build_real_cst_from_string (float_type_node, "-QNaN")); |
| 7619 |
out->safe_push (build_real_cst_from_string (float_type_node, "SNaN")); |
| 7620 |
out->safe_push (build_real_cst_from_string (float_type_node, "-SNaN")); |
| 7621 |
out->safe_push (build_real_cst_from_string (float_type_node, "0.0")); |
| 7622 |
out->safe_push (build_real_cst_from_string (float_type_node, "-0.0")); |
| 7623 |
out->safe_push (build_real_cst_from_string (float_type_node, "Inf")); |
| 7624 |
out->safe_push (build_real_cst_from_string (float_type_node, "-Inf")); |
| 7625 |
} |
| 7626 |
|
| 7627 |
/* Verify that tree_cmp is a well-behaved comparator for qsort, even |
| 7628 |
if the underlying constants aren't comparable. */ |
| 7629 |
|
| 7630 |
static void |
| 7631 |
test_tree_cmp_on_constants () |
| 7632 |
{ |
| 7633 |
auto_vec<tree> csts; |
| 7634 |
append_interesting_constants (&csts); |
| 7635 |
|
| 7636 |
/* Try sorting every triple. */ |
| 7637 |
const unsigned num = csts.length (); |
| 7638 |
for (unsigned i = 0; i < num; i++) |
| 7639 |
for (unsigned j = 0; j < num; j++) |
| 7640 |
for (unsigned k = 0; k < num; k++) |
| 7641 |
{ |
| 7642 |
auto_vec<tree> v (3); |
| 7643 |
v.quick_push (csts[i]); |
| 7644 |
v.quick_push (csts[j]); |
| 7645 |
v.quick_push (csts[k]); |
| 7646 |
v.qsort (tree_cmp); |
| 7647 |
} |
| 7648 |
} |
| 7649 |
|
| 7650 |
/* Implementation detail of the ASSERT_CONDITION_* macros. */ |
| 7651 |
|
| 7652 |
void |
| 7653 |
assert_condition (const location &loc, |
| 7654 |
region_model &model, |
| 7655 |
const svalue *lhs, tree_code op, const svalue *rhs, |
| 7656 |
tristate expected) |
| 7657 |
{ |
| 7658 |
tristate actual = model.eval_condition (lhs, op, rhs); |
| 7659 |
ASSERT_EQ_AT (loc, actual, expected); |
| 7660 |
} |
| 7661 |
|
| 7662 |
/* Implementation detail of the ASSERT_CONDITION_* macros. */ |
| 7663 |
|
| 7664 |
void |
| 7665 |
assert_condition (const location &loc, |
| 7666 |
region_model &model, |
| 7667 |
tree lhs, tree_code op, tree rhs, |
| 7668 |
tristate expected) |
| 7669 |
{ |
| 7670 |
tristate actual = model.eval_condition (lhs, op, rhs, NULL); |
| 7671 |
ASSERT_EQ_AT (loc, actual, expected); |
| 7672 |
} |
| 7673 |
|
| 7674 |
/* Implementation detail of ASSERT_DUMP_TREE_EQ. */ |
| 7675 |
|
| 7676 |
static void |
| 7677 |
assert_dump_tree_eq (const location &loc, tree t, const char *expected) |
| 7678 |
{ |
| 7679 |
auto_fix_quotes sentinel; |
| 7680 |
pretty_printer pp; |
| 7681 |
pp_format_decoder (&pp) = default_tree_printer; |
| 7682 |
dump_tree (&pp, t); |
| 7683 |
ASSERT_STREQ_AT (loc, pp_formatted_text (&pp), expected); |
| 7684 |
} |
| 7685 |
|
| 7686 |
/* Assert that dump_tree (T) is EXPECTED. */ |
| 7687 |
|
| 7688 |
#define ASSERT_DUMP_TREE_EQ(T, EXPECTED) \ |
| 7689 |
SELFTEST_BEGIN_STMT \ |
| 7690 |
assert_dump_tree_eq ((SELFTEST_LOCATION), (T), (EXPECTED)); \ |
| 7691 |
SELFTEST_END_STMT |
| 7692 |
|
| 7693 |
/* Implementation detail of ASSERT_DUMP_EQ. */ |
| 7694 |
|
| 7695 |
static void |
| 7696 |
assert_dump_eq (const location &loc, |
| 7697 |
const region_model &model, |
| 7698 |
bool summarize, |
| 7699 |
const char *expected) |
| 7700 |
{ |
| 7701 |
auto_fix_quotes sentinel; |
| 7702 |
pretty_printer pp; |
| 7703 |
pp_format_decoder (&pp) = default_tree_printer; |
| 7704 |
|
| 7705 |
model.dump_to_pp (&pp, summarize, true); |
| 7706 |
ASSERT_STREQ_AT (loc, pp_formatted_text (&pp), expected); |
| 7707 |
} |
| 7708 |
|
| 7709 |
/* Assert that MODEL.dump_to_pp (SUMMARIZE) is EXPECTED. */ |
| 7710 |
|
| 7711 |
#define ASSERT_DUMP_EQ(MODEL, SUMMARIZE, EXPECTED) \ |
| 7712 |
SELFTEST_BEGIN_STMT \ |
| 7713 |
assert_dump_eq ((SELFTEST_LOCATION), (MODEL), (SUMMARIZE), (EXPECTED)); \ |
| 7714 |
SELFTEST_END_STMT |
| 7715 |
|
| 7716 |
/* Smoketest for region_model::dump_to_pp. */ |
| 7717 |
|
| 7718 |
static void |
| 7719 |
test_dump () |
| 7720 |
{ |
| 7721 |
region_model_manager mgr; |
| 7722 |
region_model model (&mgr); |
| 7723 |
|
| 7724 |
ASSERT_DUMP_EQ (model, false, |
| 7725 |
"stack depth: 0\n" |
| 7726 |
"m_called_unknown_fn: FALSE\n" |
| 7727 |
"constraint_manager:\n" |
| 7728 |
" equiv classes:\n" |
| 7729 |
" constraints:\n"); |
| 7730 |
ASSERT_DUMP_EQ (model, true, |
| 7731 |
"stack depth: 0\n" |
| 7732 |
"m_called_unknown_fn: FALSE\n" |
| 7733 |
"constraint_manager:\n" |
| 7734 |
" equiv classes:\n" |
| 7735 |
" constraints:\n"); |
| 7736 |
|
| 7737 |
text_art::ascii_theme theme; |
| 7738 |
pretty_printer pp; |
| 7739 |
dump_to_pp (model, &theme, &pp); |
| 7740 |
ASSERT_STREQ ("Region Model\n" |
| 7741 |
"`- Store\n" |
| 7742 |
" `- m_called_unknown_fn: false\n", |
| 7743 |
pp_formatted_text (&pp)); |
| 7744 |
} |
| 7745 |
|
| 7746 |
/* Helper function for selftests. Create a struct or union type named NAME, |
| 7747 |
with the fields given by the FIELD_DECLS in FIELDS. |
| 7748 |
If IS_STRUCT is true create a RECORD_TYPE (aka a struct), otherwise |
| 7749 |
create a UNION_TYPE. */ |
| 7750 |
|
| 7751 |
static tree |
| 7752 |
make_test_compound_type (const char *name, bool is_struct, |
| 7753 |
const auto_vec<tree> *fields) |
| 7754 |
{ |
| 7755 |
tree t = make_node (is_struct ? RECORD_TYPE : UNION_TYPE); |
| 7756 |
TYPE_NAME (t) = get_identifier (name); |
| 7757 |
TYPE_SIZE (t) = 0; |
| 7758 |
|
| 7759 |
tree fieldlist = NULL; |
| 7760 |
int i; |
| 7761 |
tree field; |
| 7762 |
FOR_EACH_VEC_ELT (*fields, i, field) |
| 7763 |
{ |
| 7764 |
gcc_assert (TREE_CODE (field) == FIELD_DECL); |
| 7765 |
DECL_CONTEXT (field) = t; |
| 7766 |
fieldlist = chainon (field, fieldlist); |
| 7767 |
} |
| 7768 |
fieldlist = nreverse (fieldlist); |
| 7769 |
TYPE_FIELDS (t) = fieldlist; |
| 7770 |
|
| 7771 |
layout_type (t); |
| 7772 |
return t; |
| 7773 |
} |
| 7774 |
|
| 7775 |
/* Selftest fixture for creating the type "struct coord {int x; int y; };". */ |
| 7776 |
|
| 7777 |
struct coord_test |
| 7778 |
{ |
| 7779 |
coord_test () |
| 7780 |
{ |
| 7781 |
auto_vec<tree> fields; |
| 7782 |
m_x_field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, |
| 7783 |
get_identifier ("x"), integer_type_node); |
| 7784 |
fields.safe_push (m_x_field); |
| 7785 |
m_y_field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, |
| 7786 |
get_identifier ("y"), integer_type_node); |
| 7787 |
fields.safe_push (m_y_field); |
| 7788 |
m_coord_type = make_test_compound_type ("coord", true, &fields); |
| 7789 |
} |
| 7790 |
|
| 7791 |
tree m_x_field; |
| 7792 |
tree m_y_field; |
| 7793 |
tree m_coord_type; |
| 7794 |
}; |
| 7795 |
|
| 7796 |
/* Verify usage of a struct. */ |
| 7797 |
|
| 7798 |
static void |
| 7799 |
test_struct () |
| 7800 |
{ |
| 7801 |
coord_test ct; |
| 7802 |
|
| 7803 |
tree c = build_global_decl ("c", ct.m_coord_type); |
| 7804 |
tree c_x = build3 (COMPONENT_REF, TREE_TYPE (ct.m_x_field), |
| 7805 |
c, ct.m_x_field, NULL_TREE); |
| 7806 |
tree c_y = build3 (COMPONENT_REF, TREE_TYPE (ct.m_y_field), |
| 7807 |
c, ct.m_y_field, NULL_TREE); |
| 7808 |
|
| 7809 |
tree int_17 = build_int_cst (integer_type_node, 17); |
| 7810 |
tree int_m3 = build_int_cst (integer_type_node, -3); |
| 7811 |
|
| 7812 |
region_model_manager mgr; |
| 7813 |
region_model model (&mgr); |
| 7814 |
model.set_value (c_x, int_17, NULL); |
| 7815 |
model.set_value (c_y, int_m3, NULL); |
| 7816 |
|
| 7817 |
/* Verify get_offset for "c.x". */ |
| 7818 |
{ |
| 7819 |
const region *c_x_reg = model.get_lvalue (c_x, NULL); |
| 7820 |
region_offset offset = c_x_reg->get_offset (&mgr); |
| 7821 |
ASSERT_EQ (offset.get_base_region (), model.get_lvalue (c, NULL)); |
| 7822 |
ASSERT_EQ (offset.get_bit_offset (), 0); |
| 7823 |
} |
| 7824 |
|
| 7825 |
/* Verify get_offset for "c.y". */ |
| 7826 |
{ |
| 7827 |
const region *c_y_reg = model.get_lvalue (c_y, NULL); |
| 7828 |
region_offset offset = c_y_reg->get_offset (&mgr); |
| 7829 |
ASSERT_EQ (offset.get_base_region (), model.get_lvalue (c, NULL)); |
| 7830 |
ASSERT_EQ (offset.get_bit_offset (), INT_TYPE_SIZE); |
| 7831 |
} |
| 7832 |
} |
| 7833 |
|
| 7834 |
/* Verify usage of an array element. */ |
| 7835 |
|
| 7836 |
static void |
| 7837 |
test_array_1 () |
| 7838 |
{ |
| 7839 |
tree tlen = size_int (10); |
| 7840 |
tree arr_type = build_array_type (char_type_node, build_index_type (tlen)); |
| 7841 |
|
| 7842 |
tree a = build_global_decl ("a", arr_type); |
| 7843 |
|
| 7844 |
region_model_manager mgr; |
| 7845 |
region_model model (&mgr); |
| 7846 |
tree int_0 = integer_zero_node; |
| 7847 |
tree a_0 = build4 (ARRAY_REF, char_type_node, |
| 7848 |
a, int_0, NULL_TREE, NULL_TREE); |
| 7849 |
tree char_A = build_int_cst (char_type_node, 'A'); |
| 7850 |
model.set_value (a_0, char_A, NULL); |
| 7851 |
} |
| 7852 |
|
| 7853 |
/* Verify that region_model::get_representative_tree works as expected. */ |
| 7854 |
|
| 7855 |
static void |
| 7856 |
test_get_representative_tree () |
| 7857 |
{ |
| 7858 |
region_model_manager mgr; |
| 7859 |
|
| 7860 |
/* STRING_CST. */ |
| 7861 |
{ |
| 7862 |
tree string_cst = build_string (4, "foo"); |
| 7863 |
region_model m (&mgr); |
| 7864 |
const svalue *str_sval = m.get_rvalue (string_cst, NULL); |
| 7865 |
tree rep = m.get_representative_tree (str_sval); |
| 7866 |
ASSERT_EQ (rep, string_cst); |
| 7867 |
} |
| 7868 |
|
| 7869 |
/* String literal. */ |
| 7870 |
{ |
| 7871 |
tree string_cst_ptr = build_string_literal (4, "foo"); |
| 7872 |
region_model m (&mgr); |
| 7873 |
const svalue *str_sval = m.get_rvalue (string_cst_ptr, NULL); |
| 7874 |
tree rep = m.get_representative_tree (str_sval); |
| 7875 |
ASSERT_DUMP_TREE_EQ (rep, "&\"foo\"[0]"); |
| 7876 |
} |
| 7877 |
|
| 7878 |
/* Value of an element within an array. */ |
| 7879 |
{ |
| 7880 |
tree tlen = size_int (10); |
| 7881 |
tree arr_type = build_array_type (char_type_node, build_index_type (tlen)); |
| 7882 |
tree a = build_global_decl ("a", arr_type); |
| 7883 |
placeholder_svalue test_sval (mgr.alloc_symbol_id (), |
| 7884 |
char_type_node, "test value"); |
| 7885 |
|
| 7886 |
/* Value of a[3]. */ |
| 7887 |
{ |
| 7888 |
test_region_model_context ctxt; |
| 7889 |
region_model model (&mgr); |
| 7890 |
tree int_3 = build_int_cst (integer_type_node, 3); |
| 7891 |
tree a_3 = build4 (ARRAY_REF, char_type_node, |
| 7892 |
a, int_3, NULL_TREE, NULL_TREE); |
| 7893 |
const region *a_3_reg = model.get_lvalue (a_3, &ctxt); |
| 7894 |
model.set_value (a_3_reg, &test_sval, &ctxt); |
| 7895 |
tree rep = model.get_representative_tree (&test_sval); |
| 7896 |
ASSERT_DUMP_TREE_EQ (rep, "a[3]"); |
| 7897 |
} |
| 7898 |
|
| 7899 |
/* Value of a[0]. */ |
| 7900 |
{ |
| 7901 |
test_region_model_context ctxt; |
| 7902 |
region_model model (&mgr); |
| 7903 |
tree idx = integer_zero_node; |
| 7904 |
tree a_0 = build4 (ARRAY_REF, char_type_node, |
| 7905 |
a, idx, NULL_TREE, NULL_TREE); |
| 7906 |
const region *a_0_reg = model.get_lvalue (a_0, &ctxt); |
| 7907 |
model.set_value (a_0_reg, &test_sval, &ctxt); |
| 7908 |
tree rep = model.get_representative_tree (&test_sval); |
| 7909 |
ASSERT_DUMP_TREE_EQ (rep, "a[0]"); |
| 7910 |
} |
| 7911 |
} |
| 7912 |
|
| 7913 |
/* Value of a field within a struct. */ |
| 7914 |
{ |
| 7915 |
coord_test ct; |
| 7916 |
|
| 7917 |
tree c = build_global_decl ("c", ct.m_coord_type); |
| 7918 |
tree c_x = build3 (COMPONENT_REF, TREE_TYPE (ct.m_x_field), |
| 7919 |
c, ct.m_x_field, NULL_TREE); |
| 7920 |
tree c_y = build3 (COMPONENT_REF, TREE_TYPE (ct.m_y_field), |
| 7921 |
c, ct.m_y_field, NULL_TREE); |
| 7922 |
|
| 7923 |
test_region_model_context ctxt; |
| 7924 |
|
| 7925 |
/* Value of initial field. */ |
| 7926 |
{ |
| 7927 |
region_model m (&mgr); |
| 7928 |
const region *c_x_reg = m.get_lvalue (c_x, &ctxt); |
| 7929 |
placeholder_svalue test_sval_x (mgr.alloc_symbol_id (), |
| 7930 |
integer_type_node, "test x val"); |
| 7931 |
m.set_value (c_x_reg, &test_sval_x, &ctxt); |
| 7932 |
tree rep = m.get_representative_tree (&test_sval_x); |
| 7933 |
ASSERT_DUMP_TREE_EQ (rep, "c.x"); |
| 7934 |
} |
| 7935 |
|
| 7936 |
/* Value of non-initial field. */ |
| 7937 |
{ |
| 7938 |
region_model m (&mgr); |
| 7939 |
const region *c_y_reg = m.get_lvalue (c_y, &ctxt); |
| 7940 |
placeholder_svalue test_sval_y (mgr.alloc_symbol_id (), |
| 7941 |
integer_type_node, "test y val"); |
| 7942 |
m.set_value (c_y_reg, &test_sval_y, &ctxt); |
| 7943 |
tree rep = m.get_representative_tree (&test_sval_y); |
| 7944 |
ASSERT_DUMP_TREE_EQ (rep, "c.y"); |
| 7945 |
} |
| 7946 |
} |
| 7947 |
} |
| 7948 |
|
| 7949 |
/* Verify that calling region_model::get_rvalue repeatedly on the same |
| 7950 |
tree constant retrieves the same svalue *. */ |
| 7951 |
|
| 7952 |
static void |
| 7953 |
test_unique_constants () |
| 7954 |
{ |
| 7955 |
tree int_0 = integer_zero_node; |
| 7956 |
tree int_42 = build_int_cst (integer_type_node, 42); |
| 7957 |
|
| 7958 |
test_region_model_context ctxt; |
| 7959 |
region_model_manager mgr; |
| 7960 |
region_model model (&mgr); |
| 7961 |
ASSERT_EQ (model.get_rvalue (int_0, &ctxt), model.get_rvalue (int_0, &ctxt)); |
| 7962 |
ASSERT_EQ (model.get_rvalue (int_42, &ctxt), |
| 7963 |
model.get_rvalue (int_42, &ctxt)); |
| 7964 |
ASSERT_NE (model.get_rvalue (int_0, &ctxt), model.get_rvalue (int_42, &ctxt)); |
| 7965 |
ASSERT_EQ (ctxt.get_num_diagnostics (), 0); |
| 7966 |
|
| 7967 |
/* A "(const int)42" will be a different tree from "(int)42)"... */ |
| 7968 |
tree const_int_type_node |
| 7969 |
= build_qualified_type (integer_type_node, TYPE_QUAL_CONST); |
| 7970 |
tree const_int_42 = build_int_cst (const_int_type_node, 42); |
| 7971 |
ASSERT_NE (int_42, const_int_42); |
| 7972 |
/* It should have a different const_svalue. */ |
| 7973 |
const svalue *int_42_sval = model.get_rvalue (int_42, &ctxt); |
| 7974 |
const svalue *const_int_42_sval = model.get_rvalue (const_int_42, &ctxt); |
| 7975 |
ASSERT_NE (int_42_sval, const_int_42_sval); |
| 7976 |
/* But they should compare as equal. */ |
| 7977 |
ASSERT_CONDITION_TRUE (model, int_42_sval, EQ_EXPR, const_int_42_sval); |
| 7978 |
ASSERT_CONDITION_FALSE (model, int_42_sval, NE_EXPR, const_int_42_sval); |
| 7979 |
} |
| 7980 |
|
| 7981 |
/* Verify that each type gets its own singleton unknown_svalue within a |
| 7982 |
region_model_manager, and that NULL_TREE gets its own singleton. */ |
| 7983 |
|
| 7984 |
static void |
| 7985 |
test_unique_unknowns () |
| 7986 |
{ |
| 7987 |
region_model_manager mgr; |
| 7988 |
const svalue *unknown_int |
| 7989 |
= mgr.get_or_create_unknown_svalue (integer_type_node); |
| 7990 |
/* Repeated calls with the same type should get the same "unknown" |
| 7991 |
svalue. */ |
| 7992 |
const svalue *unknown_int_2 |
| 7993 |
= mgr.get_or_create_unknown_svalue (integer_type_node); |
| 7994 |
ASSERT_EQ (unknown_int, unknown_int_2); |
| 7995 |
|
| 7996 |
/* Different types (or the NULL type) should have different |
| 7997 |
unknown_svalues. */ |
| 7998 |
const svalue *unknown_NULL_type = mgr.get_or_create_unknown_svalue (NULL); |
| 7999 |
ASSERT_NE (unknown_NULL_type, unknown_int); |
| 8000 |
|
| 8001 |
/* Repeated calls with NULL for the type should get the same "unknown" |
| 8002 |
svalue. */ |
| 8003 |
const svalue *unknown_NULL_type_2 = mgr.get_or_create_unknown_svalue (NULL); |
| 8004 |
ASSERT_EQ (unknown_NULL_type, unknown_NULL_type_2); |
| 8005 |
} |
| 8006 |
|
| 8007 |
/* Verify that initial_svalue are handled as expected. */ |
| 8008 |
|
| 8009 |
static void |
| 8010 |
test_initial_svalue_folding () |
| 8011 |
{ |
| 8012 |
region_model_manager mgr; |
| 8013 |
tree x = build_global_decl ("x", integer_type_node); |
| 8014 |
tree y = build_global_decl ("y", integer_type_node); |
| 8015 |
|
| 8016 |
test_region_model_context ctxt; |
| 8017 |
region_model model (&mgr); |
| 8018 |
const svalue *x_init = model.get_rvalue (x, &ctxt); |
| 8019 |
const svalue *y_init = model.get_rvalue (y, &ctxt); |
| 8020 |
ASSERT_NE (x_init, y_init); |
| 8021 |
const region *x_reg = model.get_lvalue (x, &ctxt); |
| 8022 |
ASSERT_EQ (x_init, mgr.get_or_create_initial_value (x_reg)); |
| 8023 |
|
| 8024 |
} |
| 8025 |
|
| 8026 |
/* Verify that unary ops are folded as expected. */ |
| 8027 |
|
| 8028 |
static void |
| 8029 |
test_unaryop_svalue_folding () |
| 8030 |
{ |
| 8031 |
region_model_manager mgr; |
| 8032 |
tree x = build_global_decl ("x", integer_type_node); |
| 8033 |
tree y = build_global_decl ("y", integer_type_node); |
| 8034 |
|
| 8035 |
test_region_model_context ctxt; |
| 8036 |
region_model model (&mgr); |
| 8037 |
const svalue *x_init = model.get_rvalue (x, &ctxt); |
| 8038 |
const svalue *y_init = model.get_rvalue (y, &ctxt); |
| 8039 |
const region *x_reg = model.get_lvalue (x, &ctxt); |
| 8040 |
ASSERT_EQ (x_init, mgr.get_or_create_initial_value (x_reg)); |
| 8041 |
|
| 8042 |
/* "(int)x" -> "x". */ |
| 8043 |
ASSERT_EQ (x_init, mgr.get_or_create_cast (integer_type_node, x_init)); |
| 8044 |
|
| 8045 |
/* "(void *)x" -> something other than "x". */ |
| 8046 |
ASSERT_NE (x_init, mgr.get_or_create_cast (ptr_type_node, x_init)); |
| 8047 |
|
| 8048 |
/* "!(x == y)" -> "x != y". */ |
| 8049 |
ASSERT_EQ (mgr.get_or_create_unaryop |
| 8050 |
(boolean_type_node, TRUTH_NOT_EXPR, |
| 8051 |
mgr.get_or_create_binop (boolean_type_node, EQ_EXPR, |
| 8052 |
x_init, y_init)), |
| 8053 |
mgr.get_or_create_binop (boolean_type_node, NE_EXPR, |
| 8054 |
x_init, y_init)); |
| 8055 |
/* "!(x > y)" -> "x <= y". */ |
| 8056 |
ASSERT_EQ (mgr.get_or_create_unaryop |
| 8057 |
(boolean_type_node, TRUTH_NOT_EXPR, |
| 8058 |
mgr.get_or_create_binop (boolean_type_node, GT_EXPR, |
| 8059 |
x_init, y_init)), |
| 8060 |
mgr.get_or_create_binop (boolean_type_node, LE_EXPR, |
| 8061 |
x_init, y_init)); |
| 8062 |
} |
| 8063 |
|
| 8064 |
/* Verify that binops on constant svalues are folded. */ |
| 8065 |
|
| 8066 |
static void |
| 8067 |
test_binop_svalue_folding () |
| 8068 |
{ |
| 8069 |
#define NUM_CSTS 10 |
| 8070 |
tree cst_int[NUM_CSTS]; |
| 8071 |
region_model_manager mgr; |
| 8072 |
const svalue *cst_sval[NUM_CSTS]; |
| 8073 |
for (int i = 0; i < NUM_CSTS; i++) |
| 8074 |
{ |
| 8075 |
cst_int[i] = build_int_cst (integer_type_node, i); |
| 8076 |
cst_sval[i] = mgr.get_or_create_constant_svalue (cst_int[i]); |
| 8077 |
ASSERT_EQ (cst_sval[i]->get_kind (), SK_CONSTANT); |
| 8078 |
ASSERT_EQ (cst_sval[i]->maybe_get_constant (), cst_int[i]); |
| 8079 |
} |
| 8080 |
|
| 8081 |
for (int i = 0; i < NUM_CSTS; i++) |
| 8082 |
for (int j = 0; j < NUM_CSTS; j++) |
| 8083 |
{ |
| 8084 |
if (i != j) |
| 8085 |
ASSERT_NE (cst_sval[i], cst_sval[j]); |
| 8086 |
if (i + j < NUM_CSTS) |
| 8087 |
{ |
| 8088 |
const svalue *sum |
| 8089 |
= mgr.get_or_create_binop (integer_type_node, PLUS_EXPR, |
| 8090 |
cst_sval[i], cst_sval[j]); |
| 8091 |
ASSERT_EQ (sum, cst_sval[i + j]); |
| 8092 |
} |
| 8093 |
if (i - j >= 0) |
| 8094 |
{ |
| 8095 |
const svalue *difference |
| 8096 |
= mgr.get_or_create_binop (integer_type_node, MINUS_EXPR, |
| 8097 |
cst_sval[i], cst_sval[j]); |
| 8098 |
ASSERT_EQ (difference, cst_sval[i - j]); |
| 8099 |
} |
| 8100 |
if (i * j < NUM_CSTS) |
| 8101 |
{ |
| 8102 |
const svalue *product |
| 8103 |
= mgr.get_or_create_binop (integer_type_node, MULT_EXPR, |
| 8104 |
cst_sval[i], cst_sval[j]); |
| 8105 |
ASSERT_EQ (product, cst_sval[i * j]); |
| 8106 |
} |
| 8107 |
const svalue *eq = mgr.get_or_create_binop (integer_type_node, EQ_EXPR, |
| 8108 |
cst_sval[i], cst_sval[j]); |
| 8109 |
ASSERT_EQ (eq, i == j ? cst_sval[1] : cst_sval [0]); |
| 8110 |
const svalue *neq = mgr.get_or_create_binop (integer_type_node, NE_EXPR, |
| 8111 |
cst_sval[i], cst_sval[j]); |
| 8112 |
ASSERT_EQ (neq, i != j ? cst_sval[1] : cst_sval [0]); |
| 8113 |
// etc |
| 8114 |
} |
| 8115 |
|
| 8116 |
tree x = build_global_decl ("x", integer_type_node); |
| 8117 |
|
| 8118 |
test_region_model_context ctxt; |
| 8119 |
region_model model (&mgr); |
| 8120 |
const svalue *x_init = model.get_rvalue (x, &ctxt); |
| 8121 |
|
| 8122 |
/* PLUS_EXPR folding. */ |
| 8123 |
const svalue *x_init_plus_zero |
| 8124 |
= mgr.get_or_create_binop (integer_type_node, PLUS_EXPR, |
| 8125 |
x_init, cst_sval[0]); |
| 8126 |
ASSERT_EQ (x_init_plus_zero, x_init); |
| 8127 |
const svalue *zero_plus_x_init |
| 8128 |
= mgr.get_or_create_binop (integer_type_node, PLUS_EXPR, |
| 8129 |
cst_sval[0], x_init); |
| 8130 |
ASSERT_EQ (zero_plus_x_init, x_init); |
| 8131 |
|
| 8132 |
/* MULT_EXPR folding. */ |
| 8133 |
const svalue *x_init_times_zero |
| 8134 |
= mgr.get_or_create_binop (integer_type_node, MULT_EXPR, |
| 8135 |
x_init, cst_sval[0]); |
| 8136 |
ASSERT_EQ (x_init_times_zero, cst_sval[0]); |
| 8137 |
const svalue *zero_times_x_init |
| 8138 |
= mgr.get_or_create_binop (integer_type_node, MULT_EXPR, |
| 8139 |
cst_sval[0], x_init); |
| 8140 |
ASSERT_EQ (zero_times_x_init, cst_sval[0]); |
| 8141 |
|
| 8142 |
const svalue *x_init_times_one |
| 8143 |
= mgr.get_or_create_binop (integer_type_node, MULT_EXPR, |
| 8144 |
x_init, cst_sval[1]); |
| 8145 |
ASSERT_EQ (x_init_times_one, x_init); |
| 8146 |
const svalue *one_times_x_init |
| 8147 |
= mgr.get_or_create_binop (integer_type_node, MULT_EXPR, |
| 8148 |
cst_sval[1], x_init); |
| 8149 |
ASSERT_EQ (one_times_x_init, x_init); |
| 8150 |
|
| 8151 |
// etc |
| 8152 |
// TODO: do we want to use the match-and-simplify DSL for this? |
| 8153 |
|
| 8154 |
/* Verify that binops put any constants on the RHS. */ |
| 8155 |
const svalue *four_times_x_init |
| 8156 |
= mgr.get_or_create_binop (integer_type_node, MULT_EXPR, |
| 8157 |
cst_sval[4], x_init); |
| 8158 |
const svalue *x_init_times_four |
| 8159 |
= mgr.get_or_create_binop (integer_type_node, MULT_EXPR, |
| 8160 |
x_init, cst_sval[4]); |
| 8161 |
ASSERT_EQ (four_times_x_init, x_init_times_four); |
| 8162 |
const binop_svalue *binop = four_times_x_init->dyn_cast_binop_svalue (); |
| 8163 |
ASSERT_EQ (binop->get_op (), MULT_EXPR); |
| 8164 |
ASSERT_EQ (binop->get_arg0 (), x_init); |
| 8165 |
ASSERT_EQ (binop->get_arg1 (), cst_sval[4]); |
| 8166 |
|
| 8167 |
/* Verify that ((x + 1) + 1) == (x + 2). */ |
| 8168 |
const svalue *x_init_plus_one |
| 8169 |
= mgr.get_or_create_binop (integer_type_node, PLUS_EXPR, |
| 8170 |
x_init, cst_sval[1]); |
| 8171 |
const svalue *x_init_plus_two |
| 8172 |
= mgr.get_or_create_binop (integer_type_node, PLUS_EXPR, |
| 8173 |
x_init, cst_sval[2]); |
| 8174 |
const svalue *x_init_plus_one_plus_one |
| 8175 |
= mgr.get_or_create_binop (integer_type_node, PLUS_EXPR, |
| 8176 |
x_init_plus_one, cst_sval[1]); |
| 8177 |
ASSERT_EQ (x_init_plus_one_plus_one, x_init_plus_two); |
| 8178 |
|
| 8179 |
/* Verify various binops on booleans. */ |
| 8180 |
{ |
| 8181 |
const svalue *sval_true = mgr.get_or_create_int_cst (boolean_type_node, 1); |
| 8182 |
const svalue *sval_false = mgr.get_or_create_int_cst (boolean_type_node, 0); |
| 8183 |
const svalue *sval_unknown |
| 8184 |
= mgr.get_or_create_unknown_svalue (boolean_type_node); |
| 8185 |
const placeholder_svalue sval_placeholder (mgr.alloc_symbol_id (), |
| 8186 |
boolean_type_node, "v"); |
| 8187 |
for (auto op : {BIT_IOR_EXPR, TRUTH_OR_EXPR}) |
| 8188 |
{ |
| 8189 |
ASSERT_EQ (mgr.get_or_create_binop (boolean_type_node, op, |
| 8190 |
sval_true, sval_unknown), |
| 8191 |
sval_true); |
| 8192 |
ASSERT_EQ (mgr.get_or_create_binop (boolean_type_node, op, |
| 8193 |
sval_false, sval_unknown), |
| 8194 |
sval_unknown); |
| 8195 |
ASSERT_EQ (mgr.get_or_create_binop (boolean_type_node, op, |
| 8196 |
sval_false, &sval_placeholder), |
| 8197 |
&sval_placeholder); |
| 8198 |
} |
| 8199 |
for (auto op : {BIT_AND_EXPR, TRUTH_AND_EXPR}) |
| 8200 |
{ |
| 8201 |
ASSERT_EQ (mgr.get_or_create_binop (boolean_type_node, op, |
| 8202 |
sval_false, sval_unknown), |
| 8203 |
sval_false); |
| 8204 |
ASSERT_EQ (mgr.get_or_create_binop (boolean_type_node, op, |
| 8205 |
sval_true, sval_unknown), |
| 8206 |
sval_unknown); |
| 8207 |
ASSERT_EQ (mgr.get_or_create_binop (boolean_type_node, op, |
| 8208 |
sval_true, &sval_placeholder), |
| 8209 |
&sval_placeholder); |
| 8210 |
} |
| 8211 |
} |
| 8212 |
} |
| 8213 |
|
| 8214 |
/* Verify that sub_svalues are folded as expected. */ |
| 8215 |
|
| 8216 |
static void |
| 8217 |
test_sub_svalue_folding () |
| 8218 |
{ |
| 8219 |
coord_test ct; |
| 8220 |
tree c = build_global_decl ("c", ct.m_coord_type); |
| 8221 |
tree c_x = build3 (COMPONENT_REF, TREE_TYPE (ct.m_x_field), |
| 8222 |
c, ct.m_x_field, NULL_TREE); |
| 8223 |
|
| 8224 |
region_model_manager mgr; |
| 8225 |
region_model model (&mgr); |
| 8226 |
test_region_model_context ctxt; |
| 8227 |
const region *c_x_reg = model.get_lvalue (c_x, &ctxt); |
| 8228 |
|
| 8229 |
/* Verify that sub_svalue of "unknown" simply |
| 8230 |
yields an unknown. */ |
| 8231 |
|
| 8232 |
const svalue *unknown = mgr.get_or_create_unknown_svalue (ct.m_coord_type); |
| 8233 |
const svalue *sub = mgr.get_or_create_sub_svalue (TREE_TYPE (ct.m_x_field), |
| 8234 |
unknown, c_x_reg); |
| 8235 |
ASSERT_EQ (sub->get_kind (), SK_UNKNOWN); |
| 8236 |
ASSERT_EQ (sub->get_type (), TREE_TYPE (ct.m_x_field)); |
| 8237 |
} |
| 8238 |
|
| 8239 |
/* Get BIT within VAL as a symbolic value within MGR. */ |
| 8240 |
|
| 8241 |
static const svalue * |
| 8242 |
get_bit (region_model_manager *mgr, |
| 8243 |
bit_offset_t bit, |
| 8244 |
unsigned HOST_WIDE_INT val) |
| 8245 |
{ |
| 8246 |
const svalue *inner_svalue |
| 8247 |
= mgr->get_or_create_int_cst (unsigned_type_node, val); |
| 8248 |
return mgr->get_or_create_bits_within (boolean_type_node, |
| 8249 |
bit_range (bit, 1), |
| 8250 |
inner_svalue); |
| 8251 |
} |
| 8252 |
|
| 8253 |
/* Verify that bits_within_svalues are folded as expected. */ |
| 8254 |
|
| 8255 |
static void |
| 8256 |
test_bits_within_svalue_folding () |
| 8257 |
{ |
| 8258 |
region_model_manager mgr; |
| 8259 |
|
| 8260 |
const svalue *zero = mgr.get_or_create_int_cst (boolean_type_node, 0); |
| 8261 |
const svalue *one = mgr.get_or_create_int_cst (boolean_type_node, 1); |
| 8262 |
|
| 8263 |
{ |
| 8264 |
const unsigned val = 0x0000; |
| 8265 |
for (unsigned bit = 0; bit < 16; bit++) |
| 8266 |
ASSERT_EQ (get_bit (&mgr, bit, val), zero); |
| 8267 |
} |
| 8268 |
|
| 8269 |
{ |
| 8270 |
const unsigned val = 0x0001; |
| 8271 |
ASSERT_EQ (get_bit (&mgr, 0, val), one); |
| 8272 |
for (unsigned bit = 1; bit < 16; bit++) |
| 8273 |
ASSERT_EQ (get_bit (&mgr, bit, val), zero); |
| 8274 |
} |
| 8275 |
|
| 8276 |
{ |
| 8277 |
const unsigned val = 0x8000; |
| 8278 |
for (unsigned bit = 0; bit < 15; bit++) |
| 8279 |
ASSERT_EQ (get_bit (&mgr, bit, val), zero); |
| 8280 |
ASSERT_EQ (get_bit (&mgr, 15, val), one); |
| 8281 |
} |
| 8282 |
|
| 8283 |
{ |
| 8284 |
const unsigned val = 0xFFFF; |
| 8285 |
for (unsigned bit = 0; bit < 16; bit++) |
| 8286 |
ASSERT_EQ (get_bit (&mgr, bit, val), one); |
| 8287 |
} |
| 8288 |
} |
| 8289 |
|
| 8290 |
/* Test that region::descendent_of_p works as expected. */ |
| 8291 |
|
| 8292 |
static void |
| 8293 |
test_descendent_of_p () |
| 8294 |
{ |
| 8295 |
region_model_manager mgr; |
| 8296 |
const region *stack = mgr.get_stack_region (); |
| 8297 |
const region *heap = mgr.get_heap_region (); |
| 8298 |
const region *code = mgr.get_code_region (); |
| 8299 |
const region *globals = mgr.get_globals_region (); |
| 8300 |
|
| 8301 |
/* descendent_of_p should return true when used on the region itself. */ |
| 8302 |
ASSERT_TRUE (stack->descendent_of_p (stack)); |
| 8303 |
ASSERT_FALSE (stack->descendent_of_p (heap)); |
| 8304 |
ASSERT_FALSE (stack->descendent_of_p (code)); |
| 8305 |
ASSERT_FALSE (stack->descendent_of_p (globals)); |
| 8306 |
|
| 8307 |
tree x = build_global_decl ("x", integer_type_node); |
| 8308 |
const region *x_reg = mgr.get_region_for_global (x); |
| 8309 |
ASSERT_TRUE (x_reg->descendent_of_p (globals)); |
| 8310 |
|
| 8311 |
/* A cast_region should be a descendent of the original region. */ |
| 8312 |
const region *cast_reg = mgr.get_cast_region (x_reg, ptr_type_node); |
| 8313 |
ASSERT_TRUE (cast_reg->descendent_of_p (x_reg)); |
| 8314 |
} |
| 8315 |
|
| 8316 |
/* Verify that bit_range_region works as expected. */ |
| 8317 |
|
| 8318 |
static void |
| 8319 |
test_bit_range_regions () |
| 8320 |
{ |
| 8321 |
tree x = build_global_decl ("x", integer_type_node); |
| 8322 |
region_model_manager mgr; |
| 8323 |
const region *x_reg = mgr.get_region_for_global (x); |
| 8324 |
const region *byte0 |
| 8325 |
= mgr.get_bit_range (x_reg, char_type_node, bit_range (0, 8)); |
| 8326 |
const region *byte1 |
| 8327 |
= mgr.get_bit_range (x_reg, char_type_node, bit_range (8, 8)); |
| 8328 |
ASSERT_TRUE (byte0->descendent_of_p (x_reg)); |
| 8329 |
ASSERT_TRUE (byte1->descendent_of_p (x_reg)); |
| 8330 |
ASSERT_NE (byte0, byte1); |
| 8331 |
} |
| 8332 |
|
| 8333 |
/* Verify that simple assignments work as expected. */ |
| 8334 |
|
| 8335 |
static void |
| 8336 |
test_assignment () |
| 8337 |
{ |
| 8338 |
tree int_0 = integer_zero_node; |
| 8339 |
tree x = build_global_decl ("x", integer_type_node); |
| 8340 |
tree y = build_global_decl ("y", integer_type_node); |
| 8341 |
|
| 8342 |
/* "x == 0", then use of y, then "y = 0;". */ |
| 8343 |
region_model_manager mgr; |
| 8344 |
region_model model (&mgr); |
| 8345 |
ADD_SAT_CONSTRAINT (model, x, EQ_EXPR, int_0); |
| 8346 |
ASSERT_CONDITION_UNKNOWN (model, y, EQ_EXPR, int_0); |
| 8347 |
model.set_value (model.get_lvalue (y, NULL), |
| 8348 |
model.get_rvalue (int_0, NULL), |
| 8349 |
NULL); |
| 8350 |
ASSERT_CONDITION_TRUE (model, y, EQ_EXPR, int_0); |
| 8351 |
ASSERT_CONDITION_TRUE (model, y, EQ_EXPR, x); |
| 8352 |
} |
| 8353 |
|
| 8354 |
/* Verify that compound assignments work as expected. */ |
| 8355 |
|
| 8356 |
static void |
| 8357 |
test_compound_assignment () |
| 8358 |
{ |
| 8359 |
coord_test ct; |
| 8360 |
|
| 8361 |
tree c = build_global_decl ("c", ct.m_coord_type); |
| 8362 |
tree c_x = build3 (COMPONENT_REF, TREE_TYPE (ct.m_x_field), |
| 8363 |
c, ct.m_x_field, NULL_TREE); |
| 8364 |
tree c_y = build3 (COMPONENT_REF, TREE_TYPE (ct.m_y_field), |
| 8365 |
c, ct.m_y_field, NULL_TREE); |
| 8366 |
tree d = build_global_decl ("d", ct.m_coord_type); |
| 8367 |
tree d_x = build3 (COMPONENT_REF, TREE_TYPE (ct.m_x_field), |
| 8368 |
d, ct.m_x_field, NULL_TREE); |
| 8369 |
tree d_y = build3 (COMPONENT_REF, TREE_TYPE (ct.m_y_field), |
| 8370 |
d, ct.m_y_field, NULL_TREE); |
| 8371 |
|
| 8372 |
tree int_17 = build_int_cst (integer_type_node, 17); |
| 8373 |
tree int_m3 = build_int_cst (integer_type_node, -3); |
| 8374 |
|
| 8375 |
region_model_manager mgr; |
| 8376 |
region_model model (&mgr); |
| 8377 |
model.set_value (c_x, int_17, NULL); |
| 8378 |
model.set_value (c_y, int_m3, NULL); |
| 8379 |
|
| 8380 |
/* Copy c to d. */ |
| 8381 |
const svalue *sval = model.get_rvalue (c, NULL); |
| 8382 |
model.set_value (model.get_lvalue (d, NULL), sval, NULL); |
| 8383 |
|
| 8384 |
/* Check that the fields have the same svalues. */ |
| 8385 |
ASSERT_EQ (model.get_rvalue (c_x, NULL), model.get_rvalue (d_x, NULL)); |
| 8386 |
ASSERT_EQ (model.get_rvalue (c_y, NULL), model.get_rvalue (d_y, NULL)); |
| 8387 |
} |
| 8388 |
|
| 8389 |
/* Verify the details of pushing and popping stack frames. */ |
| 8390 |
|
| 8391 |
static void |
| 8392 |
test_stack_frames () |
| 8393 |
{ |
| 8394 |
tree int_42 = build_int_cst (integer_type_node, 42); |
| 8395 |
tree int_10 = build_int_cst (integer_type_node, 10); |
| 8396 |
tree int_5 = build_int_cst (integer_type_node, 5); |
| 8397 |
tree int_0 = integer_zero_node; |
| 8398 |
|
| 8399 |
auto_vec <tree> param_types; |
| 8400 |
tree parent_fndecl = make_fndecl (integer_type_node, |
| 8401 |
"parent_fn", |
| 8402 |
param_types); |
| 8403 |
allocate_struct_function (parent_fndecl, true); |
| 8404 |
|
| 8405 |
tree child_fndecl = make_fndecl (integer_type_node, |
| 8406 |
"child_fn", |
| 8407 |
param_types); |
| 8408 |
allocate_struct_function (child_fndecl, true); |
| 8409 |
|
| 8410 |
/* "a" and "b" in the parent frame. */ |
| 8411 |
tree a = build_decl (UNKNOWN_LOCATION, PARM_DECL, |
| 8412 |
get_identifier ("a"), |
| 8413 |
integer_type_node); |
| 8414 |
DECL_CONTEXT (a) = parent_fndecl; |
| 8415 |
tree b = build_decl (UNKNOWN_LOCATION, PARM_DECL, |
| 8416 |
get_identifier ("b"), |
| 8417 |
integer_type_node); |
| 8418 |
DECL_CONTEXT (b) = parent_fndecl; |
| 8419 |
/* "x" and "y" in a child frame. */ |
| 8420 |
tree x = build_decl (UNKNOWN_LOCATION, PARM_DECL, |
| 8421 |
get_identifier ("x"), |
| 8422 |
integer_type_node); |
| 8423 |
DECL_CONTEXT (x) = child_fndecl; |
| 8424 |
tree y = build_decl (UNKNOWN_LOCATION, PARM_DECL, |
| 8425 |
get_identifier ("y"), |
| 8426 |
integer_type_node); |
| 8427 |
DECL_CONTEXT (y) = child_fndecl; |
| 8428 |
|
| 8429 |
/* "p" global. */ |
| 8430 |
tree p = build_global_decl ("p", ptr_type_node); |
| 8431 |
|
| 8432 |
/* "q" global. */ |
| 8433 |
tree q = build_global_decl ("q", ptr_type_node); |
| 8434 |
|
| 8435 |
region_model_manager mgr; |
| 8436 |
test_region_model_context ctxt; |
| 8437 |
region_model model (&mgr); |
| 8438 |
|
| 8439 |
/* Push stack frame for "parent_fn". */ |
| 8440 |
const region *parent_frame_reg |
| 8441 |
= model.push_frame (*DECL_STRUCT_FUNCTION (parent_fndecl), |
| 8442 |
NULL, &ctxt); |
| 8443 |
ASSERT_EQ (model.get_current_frame (), parent_frame_reg); |
| 8444 |
ASSERT_TRUE (model.region_exists_p (parent_frame_reg)); |
| 8445 |
const region *a_in_parent_reg = model.get_lvalue (a, &ctxt); |
| 8446 |
model.set_value (a_in_parent_reg, |
| 8447 |
model.get_rvalue (int_42, &ctxt), |
| 8448 |
&ctxt); |
| 8449 |
ASSERT_EQ (a_in_parent_reg->maybe_get_frame_region (), parent_frame_reg); |
| 8450 |
|
| 8451 |
model.add_constraint (b, LT_EXPR, int_10, &ctxt); |
| 8452 |
ASSERT_EQ (model.eval_condition (b, LT_EXPR, int_10, &ctxt), |
| 8453 |
tristate (tristate::TS_TRUE)); |
| 8454 |
|
| 8455 |
/* Push stack frame for "child_fn". */ |
| 8456 |
const region *child_frame_reg |
| 8457 |
= model.push_frame (*DECL_STRUCT_FUNCTION (child_fndecl), NULL, &ctxt); |
| 8458 |
ASSERT_EQ (model.get_current_frame (), child_frame_reg); |
| 8459 |
ASSERT_TRUE (model.region_exists_p (child_frame_reg)); |
| 8460 |
const region *x_in_child_reg = model.get_lvalue (x, &ctxt); |
| 8461 |
model.set_value (x_in_child_reg, |
| 8462 |
model.get_rvalue (int_0, &ctxt), |
| 8463 |
&ctxt); |
| 8464 |
ASSERT_EQ (x_in_child_reg->maybe_get_frame_region (), child_frame_reg); |
| 8465 |
|
| 8466 |
model.add_constraint (y, NE_EXPR, int_5, &ctxt); |
| 8467 |
ASSERT_EQ (model.eval_condition (y, NE_EXPR, int_5, &ctxt), |
| 8468 |
tristate (tristate::TS_TRUE)); |
| 8469 |
|
| 8470 |
/* Point a global pointer at a local in the child frame: p = &x. */ |
| 8471 |
const region *p_in_globals_reg = model.get_lvalue (p, &ctxt); |
| 8472 |
model.set_value (p_in_globals_reg, |
| 8473 |
mgr.get_ptr_svalue (ptr_type_node, x_in_child_reg), |
| 8474 |
&ctxt); |
| 8475 |
ASSERT_EQ (p_in_globals_reg->maybe_get_frame_region (), NULL); |
| 8476 |
|
| 8477 |
/* Point another global pointer at p: q = &p. */ |
| 8478 |
const region *q_in_globals_reg = model.get_lvalue (q, &ctxt); |
| 8479 |
model.set_value (q_in_globals_reg, |
| 8480 |
mgr.get_ptr_svalue (ptr_type_node, p_in_globals_reg), |
| 8481 |
&ctxt); |
| 8482 |
|
| 8483 |
/* Test region::descendent_of_p. */ |
| 8484 |
ASSERT_TRUE (child_frame_reg->descendent_of_p (child_frame_reg)); |
| 8485 |
ASSERT_TRUE (x_in_child_reg->descendent_of_p (child_frame_reg)); |
| 8486 |
ASSERT_FALSE (a_in_parent_reg->descendent_of_p (child_frame_reg)); |
| 8487 |
|
| 8488 |
/* Pop the "child_fn" frame from the stack. */ |
| 8489 |
model.pop_frame (NULL, NULL, &ctxt, nullptr); |
| 8490 |
ASSERT_FALSE (model.region_exists_p (child_frame_reg)); |
| 8491 |
ASSERT_TRUE (model.region_exists_p (parent_frame_reg)); |
| 8492 |
|
| 8493 |
/* Verify that p (which was pointing at the local "x" in the popped |
| 8494 |
frame) has been poisoned. */ |
| 8495 |
const svalue *new_p_sval = model.get_rvalue (p, NULL); |
| 8496 |
ASSERT_EQ (new_p_sval->get_kind (), SK_POISONED); |
| 8497 |
ASSERT_EQ (new_p_sval->dyn_cast_poisoned_svalue ()->get_poison_kind (), |
| 8498 |
POISON_KIND_POPPED_STACK); |
| 8499 |
|
| 8500 |
/* Verify that q still points to p, in spite of the region |
| 8501 |
renumbering. */ |
| 8502 |
const svalue *new_q_sval = model.get_rvalue (q, &ctxt); |
| 8503 |
ASSERT_EQ (new_q_sval->get_kind (), SK_REGION); |
| 8504 |
ASSERT_EQ (new_q_sval->maybe_get_region (), |
| 8505 |
model.get_lvalue (p, &ctxt)); |
| 8506 |
|
| 8507 |
/* Verify that top of stack has been updated. */ |
| 8508 |
ASSERT_EQ (model.get_current_frame (), parent_frame_reg); |
| 8509 |
|
| 8510 |
/* Verify locals in parent frame. */ |
| 8511 |
/* Verify "a" still has its value. */ |
| 8512 |
const svalue *new_a_sval = model.get_rvalue (a, &ctxt); |
| 8513 |
ASSERT_EQ (new_a_sval->get_kind (), SK_CONSTANT); |
| 8514 |
ASSERT_EQ (new_a_sval->dyn_cast_constant_svalue ()->get_constant (), |
| 8515 |
int_42); |
| 8516 |
/* Verify "b" still has its constraint. */ |
| 8517 |
ASSERT_EQ (model.eval_condition (b, LT_EXPR, int_10, &ctxt), |
| 8518 |
tristate (tristate::TS_TRUE)); |
| 8519 |
} |
| 8520 |
|
| 8521 |
/* Verify that get_representative_path_var works as expected, that |
| 8522 |
we can map from regions to parms and back within a recursive call |
| 8523 |
stack. */ |
| 8524 |
|
| 8525 |
static void |
| 8526 |
test_get_representative_path_var () |
| 8527 |
{ |
| 8528 |
auto_vec <tree> param_types; |
| 8529 |
tree fndecl = make_fndecl (integer_type_node, |
| 8530 |
"factorial", |
| 8531 |
param_types); |
| 8532 |
allocate_struct_function (fndecl, true); |
| 8533 |
|
| 8534 |
/* Parm "n". */ |
| 8535 |
tree n = build_decl (UNKNOWN_LOCATION, PARM_DECL, |
| 8536 |
get_identifier ("n"), |
| 8537 |
integer_type_node); |
| 8538 |
DECL_CONTEXT (n) = fndecl; |
| 8539 |
|
| 8540 |
region_model_manager mgr; |
| 8541 |
test_region_model_context ctxt; |
| 8542 |
region_model model (&mgr); |
| 8543 |
|
| 8544 |
/* Push 5 stack frames for "factorial", each with a param */ |
| 8545 |
auto_vec<const region *> parm_regs; |
| 8546 |
auto_vec<const svalue *> parm_svals; |
| 8547 |
for (int depth = 0; depth < 5; depth++) |
| 8548 |
{ |
| 8549 |
const region *frame_n_reg |
| 8550 |
= model.push_frame (*DECL_STRUCT_FUNCTION (fndecl), NULL, &ctxt); |
| 8551 |
const region *parm_n_reg = model.get_lvalue (path_var (n, depth), &ctxt); |
| 8552 |
parm_regs.safe_push (parm_n_reg); |
| 8553 |
|
| 8554 |
ASSERT_EQ (parm_n_reg->get_parent_region (), frame_n_reg); |
| 8555 |
const svalue *sval_n = mgr.get_or_create_initial_value (parm_n_reg); |
| 8556 |
parm_svals.safe_push (sval_n); |
| 8557 |
} |
| 8558 |
|
| 8559 |
/* Verify that we can recognize that the regions are the parms, |
| 8560 |
at every depth. */ |
| 8561 |
for (int depth = 0; depth < 5; depth++) |
| 8562 |
{ |
| 8563 |
{ |
| 8564 |
svalue_set visited; |
| 8565 |
ASSERT_EQ (model.get_representative_path_var (parm_regs[depth], |
| 8566 |
&visited, |
| 8567 |
nullptr), |
| 8568 |
path_var (n, depth + 1)); |
| 8569 |
} |
| 8570 |
/* ...and that we can lookup lvalues for locals for all frames, |
| 8571 |
not just the top. */ |
| 8572 |
ASSERT_EQ (model.get_lvalue (path_var (n, depth), NULL), |
| 8573 |
parm_regs[depth]); |
| 8574 |
/* ...and that we can locate the svalues. */ |
| 8575 |
{ |
| 8576 |
svalue_set visited; |
| 8577 |
ASSERT_EQ (model.get_representative_path_var (parm_svals[depth], |
| 8578 |
&visited, |
| 8579 |
nullptr), |
| 8580 |
path_var (n, depth + 1)); |
| 8581 |
} |
| 8582 |
} |
| 8583 |
} |
| 8584 |
|
| 8585 |
/* Ensure that region_model::operator== works as expected. */ |
| 8586 |
|
| 8587 |
static void |
| 8588 |
test_equality_1 () |
| 8589 |
{ |
| 8590 |
tree int_42 = build_int_cst (integer_type_node, 42); |
| 8591 |
tree int_17 = build_int_cst (integer_type_node, 17); |
| 8592 |
|
| 8593 |
/* Verify that "empty" region_model instances are equal to each other. */ |
| 8594 |
region_model_manager mgr; |
| 8595 |
region_model model0 (&mgr); |
| 8596 |
region_model model1 (&mgr); |
| 8597 |
ASSERT_EQ (model0, model1); |
| 8598 |
|
| 8599 |
/* Verify that setting state in model1 makes the models non-equal. */ |
| 8600 |
tree x = build_global_decl ("x", integer_type_node); |
| 8601 |
model0.set_value (x, int_42, NULL); |
| 8602 |
ASSERT_EQ (model0.get_rvalue (x, NULL)->maybe_get_constant (), int_42); |
| 8603 |
ASSERT_NE (model0, model1); |
| 8604 |
|
| 8605 |
/* Verify the copy-ctor. */ |
| 8606 |
region_model model2 (model0); |
| 8607 |
ASSERT_EQ (model0, model2); |
| 8608 |
ASSERT_EQ (model2.get_rvalue (x, NULL)->maybe_get_constant (), int_42); |
| 8609 |
ASSERT_NE (model1, model2); |
| 8610 |
|
| 8611 |
/* Verify that models obtained from copy-ctor are independently editable |
| 8612 |
w/o affecting the original model. */ |
| 8613 |
model2.set_value (x, int_17, NULL); |
| 8614 |
ASSERT_NE (model0, model2); |
| 8615 |
ASSERT_EQ (model2.get_rvalue (x, NULL)->maybe_get_constant (), int_17); |
| 8616 |
ASSERT_EQ (model0.get_rvalue (x, NULL)->maybe_get_constant (), int_42); |
| 8617 |
} |
| 8618 |
|
| 8619 |
/* Verify that region models for |
| 8620 |
x = 42; y = 113; |
| 8621 |
and |
| 8622 |
y = 113; x = 42; |
| 8623 |
are equal. */ |
| 8624 |
|
| 8625 |
static void |
| 8626 |
test_canonicalization_2 () |
| 8627 |
{ |
| 8628 |
tree int_42 = build_int_cst (integer_type_node, 42); |
| 8629 |
tree int_113 = build_int_cst (integer_type_node, 113); |
| 8630 |
tree x = build_global_decl ("x", integer_type_node); |
| 8631 |
tree y = build_global_decl ("y", integer_type_node); |
| 8632 |
|
| 8633 |
region_model_manager mgr; |
| 8634 |
region_model model0 (&mgr); |
| 8635 |
model0.set_value (model0.get_lvalue (x, NULL), |
| 8636 |
model0.get_rvalue (int_42, NULL), |
| 8637 |
NULL); |
| 8638 |
model0.set_value (model0.get_lvalue (y, NULL), |
| 8639 |
model0.get_rvalue (int_113, NULL), |
| 8640 |
NULL); |
| 8641 |
|
| 8642 |
region_model model1 (&mgr); |
| 8643 |
model1.set_value (model1.get_lvalue (y, NULL), |
| 8644 |
model1.get_rvalue (int_113, NULL), |
| 8645 |
NULL); |
| 8646 |
model1.set_value (model1.get_lvalue (x, NULL), |
| 8647 |
model1.get_rvalue (int_42, NULL), |
| 8648 |
NULL); |
| 8649 |
|
| 8650 |
ASSERT_EQ (model0, model1); |
| 8651 |
} |
| 8652 |
|
| 8653 |
/* Verify that constraints for |
| 8654 |
x > 3 && y > 42 |
| 8655 |
and |
| 8656 |
y > 42 && x > 3 |
| 8657 |
are equal after canonicalization. */ |
| 8658 |
|
| 8659 |
static void |
| 8660 |
test_canonicalization_3 () |
| 8661 |
{ |
| 8662 |
tree int_3 = build_int_cst (integer_type_node, 3); |
| 8663 |
tree int_42 = build_int_cst (integer_type_node, 42); |
| 8664 |
tree x = build_global_decl ("x", integer_type_node); |
| 8665 |
tree y = build_global_decl ("y", integer_type_node); |
| 8666 |
|
| 8667 |
region_model_manager mgr; |
| 8668 |
region_model model0 (&mgr); |
| 8669 |
model0.add_constraint (x, GT_EXPR, int_3, NULL); |
| 8670 |
model0.add_constraint (y, GT_EXPR, int_42, NULL); |
| 8671 |
|
| 8672 |
region_model model1 (&mgr); |
| 8673 |
model1.add_constraint (y, GT_EXPR, int_42, NULL); |
| 8674 |
model1.add_constraint (x, GT_EXPR, int_3, NULL); |
| 8675 |
|
| 8676 |
model0.canonicalize (); |
| 8677 |
model1.canonicalize (); |
| 8678 |
ASSERT_EQ (model0, model1); |
| 8679 |
} |
| 8680 |
|
| 8681 |
/* Verify that we can canonicalize a model containing NaN and other real |
| 8682 |
constants. */ |
| 8683 |
|
| 8684 |
static void |
| 8685 |
test_canonicalization_4 () |
| 8686 |
{ |
| 8687 |
auto_vec<tree> csts; |
| 8688 |
append_interesting_constants (&csts); |
| 8689 |
|
| 8690 |
region_model_manager mgr; |
| 8691 |
region_model model (&mgr); |
| 8692 |
|
| 8693 |
for (tree cst : csts) |
| 8694 |
model.get_rvalue (cst, NULL); |
| 8695 |
|
| 8696 |
model.canonicalize (); |
| 8697 |
} |
| 8698 |
|
| 8699 |
/* Assert that if we have two region_model instances |
| 8700 |
with values VAL_A and VAL_B for EXPR that they are |
| 8701 |
mergable. Write the merged model to *OUT_MERGED_MODEL, |
| 8702 |
and the merged svalue ptr to *OUT_MERGED_SVALUE. |
| 8703 |
If VAL_A or VAL_B are NULL_TREE, don't populate EXPR |
| 8704 |
for that region_model. */ |
| 8705 |
|
| 8706 |
static void |
| 8707 |
assert_region_models_merge (tree expr, tree val_a, tree val_b, |
| 8708 |
region_model *out_merged_model, |
| 8709 |
const svalue **out_merged_svalue) |
| 8710 |
{ |
| 8711 |
region_model_manager *mgr = out_merged_model->get_manager (); |
| 8712 |
program_point point (program_point::origin (*mgr)); |
| 8713 |
test_region_model_context ctxt; |
| 8714 |
region_model model0 (mgr); |
| 8715 |
region_model model1 (mgr); |
| 8716 |
if (val_a) |
| 8717 |
model0.set_value (model0.get_lvalue (expr, &ctxt), |
| 8718 |
model0.get_rvalue (val_a, &ctxt), |
| 8719 |
&ctxt); |
| 8720 |
if (val_b) |
| 8721 |
model1.set_value (model1.get_lvalue (expr, &ctxt), |
| 8722 |
model1.get_rvalue (val_b, &ctxt), |
| 8723 |
&ctxt); |
| 8724 |
|
| 8725 |
/* They should be mergeable. */ |
| 8726 |
ASSERT_TRUE (model0.can_merge_with_p (model1, point, out_merged_model)); |
| 8727 |
*out_merged_svalue = out_merged_model->get_rvalue (expr, &ctxt); |
| 8728 |
} |
| 8729 |
|
| 8730 |
/* Verify that we can merge region_model instances. */ |
| 8731 |
|
| 8732 |
static void |
| 8733 |
test_state_merging () |
| 8734 |
{ |
| 8735 |
tree int_42 = build_int_cst (integer_type_node, 42); |
| 8736 |
tree int_113 = build_int_cst (integer_type_node, 113); |
| 8737 |
tree x = build_global_decl ("x", integer_type_node); |
| 8738 |
tree y = build_global_decl ("y", integer_type_node); |
| 8739 |
tree z = build_global_decl ("z", integer_type_node); |
| 8740 |
tree p = build_global_decl ("p", ptr_type_node); |
| 8741 |
|
| 8742 |
tree addr_of_y = build1 (ADDR_EXPR, ptr_type_node, y); |
| 8743 |
tree addr_of_z = build1 (ADDR_EXPR, ptr_type_node, z); |
| 8744 |
|
| 8745 |
auto_vec <tree> param_types; |
| 8746 |
tree test_fndecl = make_fndecl (integer_type_node, "test_fn", param_types); |
| 8747 |
allocate_struct_function (test_fndecl, true); |
| 8748 |
|
| 8749 |
/* Param "a". */ |
| 8750 |
tree a = build_decl (UNKNOWN_LOCATION, PARM_DECL, |
| 8751 |
get_identifier ("a"), |
| 8752 |
integer_type_node); |
| 8753 |
DECL_CONTEXT (a) = test_fndecl; |
| 8754 |
tree addr_of_a = build1 (ADDR_EXPR, ptr_type_node, a); |
| 8755 |
|
| 8756 |
/* Param "q", a pointer. */ |
| 8757 |
tree q = build_decl (UNKNOWN_LOCATION, PARM_DECL, |
| 8758 |
get_identifier ("q"), |
| 8759 |
ptr_type_node); |
| 8760 |
DECL_CONTEXT (q) = test_fndecl; |
| 8761 |
|
| 8762 |
region_model_manager mgr; |
| 8763 |
program_point point (program_point::origin (mgr)); |
| 8764 |
|
| 8765 |
{ |
| 8766 |
region_model model0 (&mgr); |
| 8767 |
region_model model1 (&mgr); |
| 8768 |
region_model merged (&mgr); |
| 8769 |
/* Verify empty models can be merged. */ |
| 8770 |
ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged)); |
| 8771 |
ASSERT_EQ (model0, merged); |
| 8772 |
} |
| 8773 |
|
| 8774 |
/* Verify that we can merge two contradictory constraints on the |
| 8775 |
value for a global. */ |
| 8776 |
/* TODO: verify that the merged model doesn't have a value for |
| 8777 |
the global */ |
| 8778 |
{ |
| 8779 |
region_model model0 (&mgr); |
| 8780 |
region_model model1 (&mgr); |
| 8781 |
region_model merged (&mgr); |
| 8782 |
test_region_model_context ctxt; |
| 8783 |
model0.add_constraint (x, EQ_EXPR, int_42, &ctxt); |
| 8784 |
model1.add_constraint (x, EQ_EXPR, int_113, &ctxt); |
| 8785 |
ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged)); |
| 8786 |
ASSERT_NE (model0, merged); |
| 8787 |
ASSERT_NE (model1, merged); |
| 8788 |
} |
| 8789 |
|
| 8790 |
/* Verify handling of a PARM_DECL. */ |
| 8791 |
{ |
| 8792 |
test_region_model_context ctxt; |
| 8793 |
region_model model0 (&mgr); |
| 8794 |
region_model model1 (&mgr); |
| 8795 |
ASSERT_EQ (model0.get_stack_depth (), 0); |
| 8796 |
model0.push_frame (*DECL_STRUCT_FUNCTION (test_fndecl), NULL, &ctxt); |
| 8797 |
ASSERT_EQ (model0.get_stack_depth (), 1); |
| 8798 |
model1.push_frame (*DECL_STRUCT_FUNCTION (test_fndecl), NULL, &ctxt); |
| 8799 |
|
| 8800 |
placeholder_svalue test_sval (mgr.alloc_symbol_id (), |
| 8801 |
integer_type_node, "test sval"); |
| 8802 |
model0.set_value (model0.get_lvalue (a, &ctxt), &test_sval, &ctxt); |
| 8803 |
model1.set_value (model1.get_lvalue (a, &ctxt), &test_sval, &ctxt); |
| 8804 |
ASSERT_EQ (model0, model1); |
| 8805 |
|
| 8806 |
/* They should be mergeable, and the result should be the same. */ |
| 8807 |
region_model merged (&mgr); |
| 8808 |
ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged)); |
| 8809 |
ASSERT_EQ (model0, merged); |
| 8810 |
/* In particular, "a" should have the placeholder value. */ |
| 8811 |
ASSERT_EQ (merged.get_rvalue (a, &ctxt), &test_sval); |
| 8812 |
} |
| 8813 |
|
| 8814 |
/* Verify handling of a global. */ |
| 8815 |
{ |
| 8816 |
test_region_model_context ctxt; |
| 8817 |
region_model model0 (&mgr); |
| 8818 |
region_model model1 (&mgr); |
| 8819 |
|
| 8820 |
placeholder_svalue test_sval (mgr.alloc_symbol_id (), |
| 8821 |
integer_type_node, "test sval"); |
| 8822 |
model0.set_value (model0.get_lvalue (x, &ctxt), &test_sval, &ctxt); |
| 8823 |
model1.set_value (model1.get_lvalue (x, &ctxt), &test_sval, &ctxt); |
| 8824 |
ASSERT_EQ (model0, model1); |
| 8825 |
|
| 8826 |
/* They should be mergeable, and the result should be the same. */ |
| 8827 |
region_model merged (&mgr); |
| 8828 |
ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged)); |
| 8829 |
ASSERT_EQ (model0, merged); |
| 8830 |
/* In particular, "x" should have the placeholder value. */ |
| 8831 |
ASSERT_EQ (merged.get_rvalue (x, &ctxt), &test_sval); |
| 8832 |
} |
| 8833 |
|
| 8834 |
/* Use global-handling to verify various combinations of values. */ |
| 8835 |
|
| 8836 |
/* Two equal constant values. */ |
| 8837 |
{ |
| 8838 |
region_model merged (&mgr); |
| 8839 |
const svalue *merged_x_sval; |
| 8840 |
assert_region_models_merge (x, int_42, int_42, &merged, &merged_x_sval); |
| 8841 |
|
| 8842 |
/* In particular, there should be a constant value for "x". */ |
| 8843 |
ASSERT_EQ (merged_x_sval->get_kind (), SK_CONSTANT); |
| 8844 |
ASSERT_EQ (merged_x_sval->dyn_cast_constant_svalue ()->get_constant (), |
| 8845 |
int_42); |
| 8846 |
} |
| 8847 |
|
| 8848 |
/* Two non-equal constant values. */ |
| 8849 |
{ |
| 8850 |
region_model merged (&mgr); |
| 8851 |
const svalue *merged_x_sval; |
| 8852 |
assert_region_models_merge (x, int_42, int_113, &merged, &merged_x_sval); |
| 8853 |
|
| 8854 |
/* In particular, there should be a "widening" value for "x". */ |
| 8855 |
ASSERT_EQ (merged_x_sval->get_kind (), SK_WIDENING); |
| 8856 |
} |
| 8857 |
|
| 8858 |
/* Initial and constant. */ |
| 8859 |
{ |
| 8860 |
region_model merged (&mgr); |
| 8861 |
const svalue *merged_x_sval; |
| 8862 |
assert_region_models_merge (x, NULL_TREE, int_113, &merged, &merged_x_sval); |
| 8863 |
|
| 8864 |
/* In particular, there should be an unknown value for "x". */ |
| 8865 |
ASSERT_EQ (merged_x_sval->get_kind (), SK_UNKNOWN); |
| 8866 |
} |
| 8867 |
|
| 8868 |
/* Constant and initial. */ |
| 8869 |
{ |
| 8870 |
region_model merged (&mgr); |
| 8871 |
const svalue *merged_x_sval; |
| 8872 |
assert_region_models_merge (x, int_42, NULL_TREE, &merged, &merged_x_sval); |
| 8873 |
|
| 8874 |
/* In particular, there should be an unknown value for "x". */ |
| 8875 |
ASSERT_EQ (merged_x_sval->get_kind (), SK_UNKNOWN); |
| 8876 |
} |
| 8877 |
|
| 8878 |
/* Unknown and constant. */ |
| 8879 |
// TODO |
| 8880 |
|
| 8881 |
/* Pointers: NULL and NULL. */ |
| 8882 |
// TODO |
| 8883 |
|
| 8884 |
/* Pointers: NULL and non-NULL. */ |
| 8885 |
// TODO |
| 8886 |
|
| 8887 |
/* Pointers: non-NULL and non-NULL: ptr to a local. */ |
| 8888 |
{ |
| 8889 |
region_model model0 (&mgr); |
| 8890 |
model0.push_frame (*DECL_STRUCT_FUNCTION (test_fndecl), NULL, NULL); |
| 8891 |
model0.set_value (model0.get_lvalue (p, NULL), |
| 8892 |
model0.get_rvalue (addr_of_a, NULL), NULL); |
| 8893 |
|
| 8894 |
region_model model1 (model0); |
| 8895 |
ASSERT_EQ (model0, model1); |
| 8896 |
|
| 8897 |
/* They should be mergeable, and the result should be the same. */ |
| 8898 |
region_model merged (&mgr); |
| 8899 |
ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged)); |
| 8900 |
ASSERT_EQ (model0, merged); |
| 8901 |
} |
| 8902 |
|
| 8903 |
/* Pointers: non-NULL and non-NULL: ptr to a global. */ |
| 8904 |
{ |
| 8905 |
region_model merged (&mgr); |
| 8906 |
/* p == &y in both input models. */ |
| 8907 |
const svalue *merged_p_sval; |
| 8908 |
assert_region_models_merge (p, addr_of_y, addr_of_y, &merged, |
| 8909 |
&merged_p_sval); |
| 8910 |
|
| 8911 |
/* We should get p == &y in the merged model. */ |
| 8912 |
ASSERT_EQ (merged_p_sval->get_kind (), SK_REGION); |
| 8913 |
const region_svalue *merged_p_ptr |
| 8914 |
= merged_p_sval->dyn_cast_region_svalue (); |
| 8915 |
const region *merged_p_star_reg = merged_p_ptr->get_pointee (); |
| 8916 |
ASSERT_EQ (merged_p_star_reg, merged.get_lvalue (y, NULL)); |
| 8917 |
} |
| 8918 |
|
| 8919 |
/* Pointers: non-NULL ptrs to different globals: should be unknown. */ |
| 8920 |
{ |
| 8921 |
region_model merged (&mgr); |
| 8922 |
/* x == &y vs x == &z in the input models; these are actually casts |
| 8923 |
of the ptrs to "int". */ |
| 8924 |
const svalue *merged_x_sval; |
| 8925 |
// TODO: |
| 8926 |
assert_region_models_merge (x, addr_of_y, addr_of_z, &merged, |
| 8927 |
&merged_x_sval); |
| 8928 |
|
| 8929 |
/* We should get x == unknown in the merged model. */ |
| 8930 |
ASSERT_EQ (merged_x_sval->get_kind (), SK_UNKNOWN); |
| 8931 |
} |
| 8932 |
|
| 8933 |
/* Pointers: non-NULL and non-NULL: ptr to a heap region. */ |
| 8934 |
{ |
| 8935 |
test_region_model_context ctxt; |
| 8936 |
region_model model0 (&mgr); |
| 8937 |
tree size = build_int_cst (size_type_node, 1024); |
| 8938 |
const svalue *size_sval = mgr.get_or_create_constant_svalue (size); |
| 8939 |
const region *new_reg |
| 8940 |
= model0.get_or_create_region_for_heap_alloc (size_sval, &ctxt); |
| 8941 |
const svalue *ptr_sval = mgr.get_ptr_svalue (ptr_type_node, new_reg); |
| 8942 |
model0.set_value (model0.get_lvalue (p, &ctxt), |
| 8943 |
ptr_sval, &ctxt); |
| 8944 |
|
| 8945 |
region_model model1 (model0); |
| 8946 |
|
| 8947 |
ASSERT_EQ (model0, model1); |
| 8948 |
|
| 8949 |
region_model merged (&mgr); |
| 8950 |
ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged)); |
| 8951 |
|
| 8952 |
/* The merged model ought to be identical. */ |
| 8953 |
ASSERT_EQ (model0, merged); |
| 8954 |
} |
| 8955 |
|
| 8956 |
/* Two regions sharing the same placeholder svalue should continue sharing |
| 8957 |
it after self-merger. */ |
| 8958 |
{ |
| 8959 |
test_region_model_context ctxt; |
| 8960 |
region_model model0 (&mgr); |
| 8961 |
placeholder_svalue placeholder_sval (mgr.alloc_symbol_id (), |
| 8962 |
integer_type_node, "test"); |
| 8963 |
model0.set_value (model0.get_lvalue (x, &ctxt), |
| 8964 |
&placeholder_sval, &ctxt); |
| 8965 |
model0.set_value (model0.get_lvalue (y, &ctxt), &placeholder_sval, &ctxt); |
| 8966 |
region_model model1 (model0); |
| 8967 |
|
| 8968 |
/* They should be mergeable, and the result should be the same. */ |
| 8969 |
region_model merged (&mgr); |
| 8970 |
ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged)); |
| 8971 |
ASSERT_EQ (model0, merged); |
| 8972 |
|
| 8973 |
/* In particular, we should have x == y. */ |
| 8974 |
ASSERT_EQ (merged.eval_condition (x, EQ_EXPR, y, &ctxt), |
| 8975 |
tristate (tristate::TS_TRUE)); |
| 8976 |
} |
| 8977 |
|
| 8978 |
{ |
| 8979 |
region_model model0 (&mgr); |
| 8980 |
region_model model1 (&mgr); |
| 8981 |
test_region_model_context ctxt; |
| 8982 |
model0.add_constraint (x, EQ_EXPR, int_42, &ctxt); |
| 8983 |
model1.add_constraint (x, NE_EXPR, int_42, &ctxt); |
| 8984 |
region_model merged (&mgr); |
| 8985 |
ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged)); |
| 8986 |
} |
| 8987 |
|
| 8988 |
{ |
| 8989 |
region_model model0 (&mgr); |
| 8990 |
region_model model1 (&mgr); |
| 8991 |
test_region_model_context ctxt; |
| 8992 |
model0.add_constraint (x, EQ_EXPR, int_42, &ctxt); |
| 8993 |
model1.add_constraint (x, NE_EXPR, int_42, &ctxt); |
| 8994 |
model1.add_constraint (x, EQ_EXPR, int_113, &ctxt); |
| 8995 |
region_model merged (&mgr); |
| 8996 |
ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged)); |
| 8997 |
} |
| 8998 |
|
| 8999 |
// TODO: what can't we merge? need at least one such test |
| 9000 |
|
| 9001 |
/* TODO: various things |
| 9002 |
- heap regions |
| 9003 |
- value merging: |
| 9004 |
- every combination, but in particular |
| 9005 |
- pairs of regions |
| 9006 |
*/ |
| 9007 |
|
| 9008 |
/* Views. */ |
| 9009 |
{ |
| 9010 |
test_region_model_context ctxt; |
| 9011 |
region_model model0 (&mgr); |
| 9012 |
|
| 9013 |
const region *x_reg = model0.get_lvalue (x, &ctxt); |
| 9014 |
const region *x_as_ptr = mgr.get_cast_region (x_reg, ptr_type_node); |
| 9015 |
model0.set_value (x_as_ptr, model0.get_rvalue (addr_of_y, &ctxt), &ctxt); |
| 9016 |
|
| 9017 |
region_model model1 (model0); |
| 9018 |
ASSERT_EQ (model1, model0); |
| 9019 |
|
| 9020 |
/* They should be mergeable, and the result should be the same. */ |
| 9021 |
region_model merged (&mgr); |
| 9022 |
ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged)); |
| 9023 |
} |
| 9024 |
|
| 9025 |
/* Verify that we can merge a model in which a local in an older stack |
| 9026 |
frame points to a local in a more recent stack frame. */ |
| 9027 |
{ |
| 9028 |
region_model model0 (&mgr); |
| 9029 |
model0.push_frame (*DECL_STRUCT_FUNCTION (test_fndecl), NULL, NULL); |
| 9030 |
const region *q_in_first_frame = model0.get_lvalue (q, NULL); |
| 9031 |
|
| 9032 |
/* Push a second frame. */ |
| 9033 |
const region *reg_2nd_frame |
| 9034 |
= model0.push_frame (*DECL_STRUCT_FUNCTION (test_fndecl), NULL, NULL); |
| 9035 |
|
| 9036 |
/* Have a pointer in the older frame point to a local in the |
| 9037 |
more recent frame. */ |
| 9038 |
const svalue *sval_ptr = model0.get_rvalue (addr_of_a, NULL); |
| 9039 |
model0.set_value (q_in_first_frame, sval_ptr, NULL); |
| 9040 |
|
| 9041 |
/* Verify that it's pointing at the newer frame. */ |
| 9042 |
const region *reg_pointee = sval_ptr->maybe_get_region (); |
| 9043 |
ASSERT_EQ (reg_pointee->get_parent_region (), reg_2nd_frame); |
| 9044 |
|
| 9045 |
model0.canonicalize (); |
| 9046 |
|
| 9047 |
region_model model1 (model0); |
| 9048 |
ASSERT_EQ (model0, model1); |
| 9049 |
|
| 9050 |
/* They should be mergeable, and the result should be the same |
| 9051 |
(after canonicalization, at least). */ |
| 9052 |
region_model merged (&mgr); |
| 9053 |
ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged)); |
| 9054 |
merged.canonicalize (); |
| 9055 |
ASSERT_EQ (model0, merged); |
| 9056 |
} |
| 9057 |
|
| 9058 |
/* Verify that we can merge a model in which a local points to a global. */ |
| 9059 |
{ |
| 9060 |
region_model model0 (&mgr); |
| 9061 |
model0.push_frame (*DECL_STRUCT_FUNCTION (test_fndecl), NULL, NULL); |
| 9062 |
model0.set_value (model0.get_lvalue (q, NULL), |
| 9063 |
model0.get_rvalue (addr_of_y, NULL), NULL); |
| 9064 |
|
| 9065 |
region_model model1 (model0); |
| 9066 |
ASSERT_EQ (model0, model1); |
| 9067 |
|
| 9068 |
/* They should be mergeable, and the result should be the same |
| 9069 |
(after canonicalization, at least). */ |
| 9070 |
region_model merged (&mgr); |
| 9071 |
ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged)); |
| 9072 |
ASSERT_EQ (model0, merged); |
| 9073 |
} |
| 9074 |
} |
| 9075 |
|
| 9076 |
/* Verify that constraints are correctly merged when merging region_model |
| 9077 |
instances. */ |
| 9078 |
|
| 9079 |
static void |
| 9080 |
test_constraint_merging () |
| 9081 |
{ |
| 9082 |
tree int_0 = integer_zero_node; |
| 9083 |
tree int_5 = build_int_cst (integer_type_node, 5); |
| 9084 |
tree x = build_global_decl ("x", integer_type_node); |
| 9085 |
tree y = build_global_decl ("y", integer_type_node); |
| 9086 |
tree z = build_global_decl ("z", integer_type_node); |
| 9087 |
tree n = build_global_decl ("n", integer_type_node); |
| 9088 |
|
| 9089 |
region_model_manager mgr; |
| 9090 |
test_region_model_context ctxt; |
| 9091 |
|
| 9092 |
/* model0: 0 <= (x == y) < n. */ |
| 9093 |
region_model model0 (&mgr); |
| 9094 |
model0.add_constraint (x, EQ_EXPR, y, &ctxt); |
| 9095 |
model0.add_constraint (x, GE_EXPR, int_0, NULL); |
| 9096 |
model0.add_constraint (x, LT_EXPR, n, NULL); |
| 9097 |
|
| 9098 |
/* model1: z != 5 && (0 <= x < n). */ |
| 9099 |
region_model model1 (&mgr); |
| 9100 |
model1.add_constraint (z, NE_EXPR, int_5, NULL); |
| 9101 |
model1.add_constraint (x, GE_EXPR, int_0, NULL); |
| 9102 |
model1.add_constraint (x, LT_EXPR, n, NULL); |
| 9103 |
|
| 9104 |
/* They should be mergeable; the merged constraints should |
| 9105 |
be: (0 <= x < n). */ |
| 9106 |
program_point point (program_point::origin (mgr)); |
| 9107 |
region_model merged (&mgr); |
| 9108 |
ASSERT_TRUE (model0.can_merge_with_p (model1, point, &merged)); |
| 9109 |
|
| 9110 |
ASSERT_EQ (merged.eval_condition (x, GE_EXPR, int_0, &ctxt), |
| 9111 |
tristate (tristate::TS_TRUE)); |
| 9112 |
ASSERT_EQ (merged.eval_condition (x, LT_EXPR, n, &ctxt), |
| 9113 |
tristate (tristate::TS_TRUE)); |
| 9114 |
|
| 9115 |
ASSERT_EQ (merged.eval_condition (z, NE_EXPR, int_5, &ctxt), |
| 9116 |
tristate (tristate::TS_UNKNOWN)); |
| 9117 |
ASSERT_EQ (merged.eval_condition (x, LT_EXPR, y, &ctxt), |
| 9118 |
tristate (tristate::TS_UNKNOWN)); |
| 9119 |
} |
| 9120 |
|
| 9121 |
/* Verify that widening_svalue::eval_condition_without_cm works as |
| 9122 |
expected. */ |
| 9123 |
|
| 9124 |
static void |
| 9125 |
test_widening_constraints () |
| 9126 |
{ |
| 9127 |
region_model_manager mgr; |
| 9128 |
function_point point (program_point::origin (mgr).get_function_point ()); |
| 9129 |
tree int_0 = integer_zero_node; |
| 9130 |
tree int_m1 = build_int_cst (integer_type_node, -1); |
| 9131 |
tree int_1 = integer_one_node; |
| 9132 |
tree int_256 = build_int_cst (integer_type_node, 256); |
| 9133 |
test_region_model_context ctxt; |
| 9134 |
const svalue *int_0_sval = mgr.get_or_create_constant_svalue (int_0); |
| 9135 |
const svalue *int_1_sval = mgr.get_or_create_constant_svalue (int_1); |
| 9136 |
const svalue *w_zero_then_one_sval |
| 9137 |
= mgr.get_or_create_widening_svalue (integer_type_node, point, |
| 9138 |
int_0_sval, int_1_sval); |
| 9139 |
const widening_svalue *w_zero_then_one |
| 9140 |
= w_zero_then_one_sval->dyn_cast_widening_svalue (); |
| 9141 |
ASSERT_EQ (w_zero_then_one->get_direction (), |
| 9142 |
widening_svalue::DIR_ASCENDING); |
| 9143 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LT_EXPR, int_m1), |
| 9144 |
tristate::TS_FALSE); |
| 9145 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LT_EXPR, int_0), |
| 9146 |
tristate::TS_FALSE); |
| 9147 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LT_EXPR, int_1), |
| 9148 |
tristate::TS_UNKNOWN); |
| 9149 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LT_EXPR, int_256), |
| 9150 |
tristate::TS_UNKNOWN); |
| 9151 |
|
| 9152 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LE_EXPR, int_m1), |
| 9153 |
tristate::TS_FALSE); |
| 9154 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LE_EXPR, int_0), |
| 9155 |
tristate::TS_UNKNOWN); |
| 9156 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LE_EXPR, int_1), |
| 9157 |
tristate::TS_UNKNOWN); |
| 9158 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (LE_EXPR, int_256), |
| 9159 |
tristate::TS_UNKNOWN); |
| 9160 |
|
| 9161 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GT_EXPR, int_m1), |
| 9162 |
tristate::TS_TRUE); |
| 9163 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GT_EXPR, int_0), |
| 9164 |
tristate::TS_UNKNOWN); |
| 9165 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GT_EXPR, int_1), |
| 9166 |
tristate::TS_UNKNOWN); |
| 9167 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GT_EXPR, int_256), |
| 9168 |
tristate::TS_UNKNOWN); |
| 9169 |
|
| 9170 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GE_EXPR, int_m1), |
| 9171 |
tristate::TS_TRUE); |
| 9172 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GE_EXPR, int_0), |
| 9173 |
tristate::TS_TRUE); |
| 9174 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GE_EXPR, int_1), |
| 9175 |
tristate::TS_UNKNOWN); |
| 9176 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (GE_EXPR, int_256), |
| 9177 |
tristate::TS_UNKNOWN); |
| 9178 |
|
| 9179 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (EQ_EXPR, int_m1), |
| 9180 |
tristate::TS_FALSE); |
| 9181 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (EQ_EXPR, int_0), |
| 9182 |
tristate::TS_UNKNOWN); |
| 9183 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (EQ_EXPR, int_1), |
| 9184 |
tristate::TS_UNKNOWN); |
| 9185 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (EQ_EXPR, int_256), |
| 9186 |
tristate::TS_UNKNOWN); |
| 9187 |
|
| 9188 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (NE_EXPR, int_m1), |
| 9189 |
tristate::TS_TRUE); |
| 9190 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (NE_EXPR, int_0), |
| 9191 |
tristate::TS_UNKNOWN); |
| 9192 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (NE_EXPR, int_1), |
| 9193 |
tristate::TS_UNKNOWN); |
| 9194 |
ASSERT_EQ (w_zero_then_one->eval_condition_without_cm (NE_EXPR, int_256), |
| 9195 |
tristate::TS_UNKNOWN); |
| 9196 |
} |
| 9197 |
|
| 9198 |
/* Verify merging constraints for states simulating successive iterations |
| 9199 |
of a loop. |
| 9200 |
Simulate: |
| 9201 |
for (i = 0; i < 256; i++) |
| 9202 |
[...body...] |
| 9203 |
i.e. this gimple:. |
| 9204 |
i_15 = 0; |
| 9205 |
goto <bb 4>; |
| 9206 |
|
| 9207 |
<bb 4> : |
| 9208 |
i_11 = PHI <i_15(2), i_23(3)> |
| 9209 |
if (i_11 <= 255) |
| 9210 |
goto <bb 3>; |
| 9211 |
else |
| 9212 |
goto [AFTER LOOP] |
| 9213 |
|
| 9214 |
<bb 3> : |
| 9215 |
[LOOP BODY] |
| 9216 |
i_23 = i_11 + 1; |
| 9217 |
|
| 9218 |
and thus these ops (and resultant states): |
| 9219 |
i_11 = PHI() |
| 9220 |
{i_11: 0} |
| 9221 |
add_constraint (i_11 <= 255) [for the true edge] |
| 9222 |
{i_11: 0} [constraint was a no-op] |
| 9223 |
i_23 = i_11 + 1; |
| 9224 |
{i_22: 1} |
| 9225 |
i_11 = PHI() |
| 9226 |
{i_11: WIDENED (at phi, 0, 1)} |
| 9227 |
add_constraint (i_11 <= 255) [for the true edge] |
| 9228 |
{i_11: WIDENED (at phi, 0, 1); WIDENED <= 255} |
| 9229 |
i_23 = i_11 + 1; |
| 9230 |
{i_23: (WIDENED (at phi, 0, 1) + 1); WIDENED <= 255} |
| 9231 |
i_11 = PHI(); merge with state at phi above |
| 9232 |
{i_11: WIDENED (at phi, 0, 1); WIDENED <= 256} |
| 9233 |
[changing meaning of "WIDENED" here] |
| 9234 |
if (i_11 <= 255) |
| 9235 |
T: {i_11: WIDENED (at phi, 0, 1); WIDENED <= 255}; cache hit |
| 9236 |
F: {i_11: 256} |
| 9237 |
*/ |
| 9238 |
|
| 9239 |
static void |
| 9240 |
test_iteration_1 () |
| 9241 |
{ |
| 9242 |
region_model_manager mgr; |
| 9243 |
program_point point (program_point::origin (mgr)); |
| 9244 |
|
| 9245 |
tree int_0 = integer_zero_node; |
| 9246 |
tree int_1 = integer_one_node; |
| 9247 |
tree int_256 = build_int_cst (integer_type_node, 256); |
| 9248 |
tree i = build_global_decl ("i", integer_type_node); |
| 9249 |
|
| 9250 |
test_region_model_context ctxt; |
| 9251 |
|
| 9252 |
/* model0: i: 0. */ |
| 9253 |
region_model model0 (&mgr); |
| 9254 |
model0.set_value (i, int_0, &ctxt); |
| 9255 |
|
| 9256 |
/* model1: i: 1. */ |
| 9257 |
region_model model1 (&mgr); |
| 9258 |
model1.set_value (i, int_1, &ctxt); |
| 9259 |
|
| 9260 |
/* Should merge "i" to a widened value. */ |
| 9261 |
region_model model2 (&mgr); |
| 9262 |
ASSERT_TRUE (model1.can_merge_with_p (model0, point, &model2)); |
| 9263 |
const svalue *merged_i = model2.get_rvalue (i, &ctxt); |
| 9264 |
ASSERT_EQ (merged_i->get_kind (), SK_WIDENING); |
| 9265 |
const widening_svalue *w = merged_i->dyn_cast_widening_svalue (); |
| 9266 |
ASSERT_EQ (w->get_direction (), widening_svalue::DIR_ASCENDING); |
| 9267 |
|
| 9268 |
/* Add constraint: i < 256 */ |
| 9269 |
model2.add_constraint (i, LT_EXPR, int_256, &ctxt); |
| 9270 |
ASSERT_EQ (model2.eval_condition (i, LT_EXPR, int_256, &ctxt), |
| 9271 |
tristate (tristate::TS_TRUE)); |
| 9272 |
ASSERT_EQ (model2.eval_condition (i, GE_EXPR, int_0, &ctxt), |
| 9273 |
tristate (tristate::TS_TRUE)); |
| 9274 |
|
| 9275 |
/* Try merging with the initial state. */ |
| 9276 |
region_model model3 (&mgr); |
| 9277 |
ASSERT_TRUE (model2.can_merge_with_p (model0, point, &model3)); |
| 9278 |
/* Merging the merged value with the initial value should be idempotent, |
| 9279 |
so that the analysis converges. */ |
| 9280 |
ASSERT_EQ (model3.get_rvalue (i, &ctxt), merged_i); |
| 9281 |
/* Merger of 0 and a widening value with constraint < CST |
| 9282 |
should retain the constraint, even though it was implicit |
| 9283 |
for the 0 case. */ |
| 9284 |
ASSERT_EQ (model3.eval_condition (i, LT_EXPR, int_256, &ctxt), |
| 9285 |
tristate (tristate::TS_TRUE)); |
| 9286 |
/* ...and we should have equality: the analysis should have converged. */ |
| 9287 |
ASSERT_EQ (model3, model2); |
| 9288 |
|
| 9289 |
/* "i_23 = i_11 + 1;" */ |
| 9290 |
region_model model4 (model3); |
| 9291 |
ASSERT_EQ (model4, model2); |
| 9292 |
model4.set_value (i, build2 (PLUS_EXPR, integer_type_node, i, int_1), &ctxt); |
| 9293 |
const svalue *plus_one = model4.get_rvalue (i, &ctxt); |
| 9294 |
ASSERT_EQ (plus_one->get_kind (), SK_BINOP); |
| 9295 |
|
| 9296 |
/* Try merging with the "i: 1" state. */ |
| 9297 |
region_model model5 (&mgr); |
| 9298 |
ASSERT_TRUE (model4.can_merge_with_p (model1, point, &model5)); |
| 9299 |
ASSERT_EQ (model5.get_rvalue (i, &ctxt), plus_one); |
| 9300 |
ASSERT_EQ (model5, model4); |
| 9301 |
|
| 9302 |
/* "i_11 = PHI();" merge with state at phi above. |
| 9303 |
For i, we should have a merger of WIDENING with WIDENING + 1, |
| 9304 |
and this should be WIDENING again. */ |
| 9305 |
region_model model6 (&mgr); |
| 9306 |
ASSERT_TRUE (model5.can_merge_with_p (model2, point, &model6)); |
| 9307 |
const svalue *merged_widening = model6.get_rvalue (i, &ctxt); |
| 9308 |
ASSERT_EQ (merged_widening->get_kind (), SK_WIDENING); |
| 9309 |
} |
| 9310 |
|
| 9311 |
/* Verify that if we mark a pointer to a malloc-ed region as non-NULL, |
| 9312 |
all cast pointers to that region are also known to be non-NULL. */ |
| 9313 |
|
| 9314 |
static void |
| 9315 |
test_malloc_constraints () |
| 9316 |
{ |
| 9317 |
region_model_manager mgr; |
| 9318 |
region_model model (&mgr); |
| 9319 |
tree p = build_global_decl ("p", ptr_type_node); |
| 9320 |
tree char_star = build_pointer_type (char_type_node); |
| 9321 |
tree q = build_global_decl ("q", char_star); |
| 9322 |
tree null_ptr = build_int_cst (ptr_type_node, 0); |
| 9323 |
|
| 9324 |
const svalue *size_in_bytes |
| 9325 |
= mgr.get_or_create_unknown_svalue (size_type_node); |
| 9326 |
const region *reg |
| 9327 |
= model.get_or_create_region_for_heap_alloc (size_in_bytes, NULL); |
| 9328 |
const svalue *sval = mgr.get_ptr_svalue (ptr_type_node, reg); |
| 9329 |
model.set_value (model.get_lvalue (p, NULL), sval, NULL); |
| 9330 |
model.set_value (q, p, NULL); |
| 9331 |
|
| 9332 |
ASSERT_CONDITION_UNKNOWN (model, p, NE_EXPR, null_ptr); |
| 9333 |
ASSERT_CONDITION_UNKNOWN (model, p, EQ_EXPR, null_ptr); |
| 9334 |
ASSERT_CONDITION_UNKNOWN (model, q, NE_EXPR, null_ptr); |
| 9335 |
ASSERT_CONDITION_UNKNOWN (model, q, EQ_EXPR, null_ptr); |
| 9336 |
|
| 9337 |
model.add_constraint (p, NE_EXPR, null_ptr, NULL); |
| 9338 |
|
| 9339 |
ASSERT_CONDITION_TRUE (model, p, NE_EXPR, null_ptr); |
| 9340 |
ASSERT_CONDITION_FALSE (model, p, EQ_EXPR, null_ptr); |
| 9341 |
ASSERT_CONDITION_TRUE (model, q, NE_EXPR, null_ptr); |
| 9342 |
ASSERT_CONDITION_FALSE (model, q, EQ_EXPR, null_ptr); |
| 9343 |
} |
| 9344 |
|
| 9345 |
/* Smoketest of getting and setting the value of a variable. */ |
| 9346 |
|
| 9347 |
static void |
| 9348 |
test_var () |
| 9349 |
{ |
| 9350 |
/* "int i;" */ |
| 9351 |
tree i = build_global_decl ("i", integer_type_node); |
| 9352 |
|
| 9353 |
tree int_17 = build_int_cst (integer_type_node, 17); |
| 9354 |
tree int_m3 = build_int_cst (integer_type_node, -3); |
| 9355 |
|
| 9356 |
region_model_manager mgr; |
| 9357 |
region_model model (&mgr); |
| 9358 |
|
| 9359 |
const region *i_reg = model.get_lvalue (i, NULL); |
| 9360 |
ASSERT_EQ (i_reg->get_kind (), RK_DECL); |
| 9361 |
|
| 9362 |
/* Reading "i" should give a symbolic "initial value". */ |
| 9363 |
const svalue *sval_init = model.get_rvalue (i, NULL); |
| 9364 |
ASSERT_EQ (sval_init->get_kind (), SK_INITIAL); |
| 9365 |
ASSERT_EQ (sval_init->dyn_cast_initial_svalue ()->get_region (), i_reg); |
| 9366 |
/* ..and doing it again should give the same "initial value". */ |
| 9367 |
ASSERT_EQ (model.get_rvalue (i, NULL), sval_init); |
| 9368 |
|
| 9369 |
/* "i = 17;". */ |
| 9370 |
model.set_value (i, int_17, NULL); |
| 9371 |
ASSERT_EQ (model.get_rvalue (i, NULL), |
| 9372 |
model.get_rvalue (int_17, NULL)); |
| 9373 |
|
| 9374 |
/* "i = -3;". */ |
| 9375 |
model.set_value (i, int_m3, NULL); |
| 9376 |
ASSERT_EQ (model.get_rvalue (i, NULL), |
| 9377 |
model.get_rvalue (int_m3, NULL)); |
| 9378 |
|
| 9379 |
/* Verify get_offset for "i". */ |
| 9380 |
{ |
| 9381 |
region_offset offset = i_reg->get_offset (&mgr); |
| 9382 |
ASSERT_EQ (offset.get_base_region (), i_reg); |
| 9383 |
ASSERT_EQ (offset.get_bit_offset (), 0); |
| 9384 |
} |
| 9385 |
} |
| 9386 |
|
| 9387 |
static void |
| 9388 |
test_array_2 () |
| 9389 |
{ |
| 9390 |
/* "int arr[10];" */ |
| 9391 |
tree tlen = size_int (10); |
| 9392 |
tree arr_type |
| 9393 |
= build_array_type (integer_type_node, build_index_type (tlen)); |
| 9394 |
tree arr = build_global_decl ("arr", arr_type); |
| 9395 |
|
| 9396 |
/* "int i;" */ |
| 9397 |
tree i = build_global_decl ("i", integer_type_node); |
| 9398 |
|
| 9399 |
tree int_0 = integer_zero_node; |
| 9400 |
tree int_1 = integer_one_node; |
| 9401 |
|
| 9402 |
tree arr_0 = build4 (ARRAY_REF, integer_type_node, |
| 9403 |
arr, int_0, NULL_TREE, NULL_TREE); |
| 9404 |
tree arr_1 = build4 (ARRAY_REF, integer_type_node, |
| 9405 |
arr, int_1, NULL_TREE, NULL_TREE); |
| 9406 |
tree arr_i = build4 (ARRAY_REF, integer_type_node, |
| 9407 |
arr, i, NULL_TREE, NULL_TREE); |
| 9408 |
|
| 9409 |
tree int_17 = build_int_cst (integer_type_node, 17); |
| 9410 |
tree int_42 = build_int_cst (integer_type_node, 42); |
| 9411 |
tree int_m3 = build_int_cst (integer_type_node, -3); |
| 9412 |
|
| 9413 |
region_model_manager mgr; |
| 9414 |
region_model model (&mgr); |
| 9415 |
/* "arr[0] = 17;". */ |
| 9416 |
model.set_value (arr_0, int_17, NULL); |
| 9417 |
/* "arr[1] = -3;". */ |
| 9418 |
model.set_value (arr_1, int_m3, NULL); |
| 9419 |
|
| 9420 |
ASSERT_EQ (model.get_rvalue (arr_0, NULL), model.get_rvalue (int_17, NULL)); |
| 9421 |
ASSERT_EQ (model.get_rvalue (arr_1, NULL), model.get_rvalue (int_m3, NULL)); |
| 9422 |
|
| 9423 |
/* Overwrite a pre-existing binding: "arr[1] = 42;". */ |
| 9424 |
model.set_value (arr_1, int_42, NULL); |
| 9425 |
ASSERT_EQ (model.get_rvalue (arr_1, NULL), model.get_rvalue (int_42, NULL)); |
| 9426 |
|
| 9427 |
/* Verify get_offset for "arr[0]". */ |
| 9428 |
{ |
| 9429 |
const region *arr_0_reg = model.get_lvalue (arr_0, NULL); |
| 9430 |
region_offset offset = arr_0_reg->get_offset (&mgr); |
| 9431 |
ASSERT_EQ (offset.get_base_region (), model.get_lvalue (arr, NULL)); |
| 9432 |
ASSERT_EQ (offset.get_bit_offset (), 0); |
| 9433 |
} |
| 9434 |
|
| 9435 |
/* Verify get_offset for "arr[1]". */ |
| 9436 |
{ |
| 9437 |
const region *arr_1_reg = model.get_lvalue (arr_1, NULL); |
| 9438 |
region_offset offset = arr_1_reg->get_offset (&mgr); |
| 9439 |
ASSERT_EQ (offset.get_base_region (), model.get_lvalue (arr, NULL)); |
| 9440 |
ASSERT_EQ (offset.get_bit_offset (), INT_TYPE_SIZE); |
| 9441 |
} |
| 9442 |
|
| 9443 |
/* Verify get_offset for "arr[i]". */ |
| 9444 |
{ |
| 9445 |
const region *arr_i_reg = model.get_lvalue (arr_i, NULL); |
| 9446 |
region_offset offset = arr_i_reg->get_offset (&mgr); |
| 9447 |
ASSERT_EQ (offset.get_base_region (), model.get_lvalue (arr, NULL)); |
| 9448 |
const svalue *offset_sval = offset.get_symbolic_byte_offset (); |
| 9449 |
if (const svalue *cast = offset_sval->maybe_undo_cast ()) |
| 9450 |
offset_sval = cast; |
| 9451 |
ASSERT_EQ (offset_sval->get_kind (), SK_BINOP); |
| 9452 |
} |
| 9453 |
|
| 9454 |
/* "arr[i] = i;" - this should remove the earlier bindings. */ |
| 9455 |
model.set_value (arr_i, i, NULL); |
| 9456 |
ASSERT_EQ (model.get_rvalue (arr_i, NULL), model.get_rvalue (i, NULL)); |
| 9457 |
ASSERT_EQ (model.get_rvalue (arr_0, NULL)->get_kind (), SK_UNKNOWN); |
| 9458 |
|
| 9459 |
/* "arr[0] = 17;" - this should remove the arr[i] binding. */ |
| 9460 |
model.set_value (arr_0, int_17, NULL); |
| 9461 |
ASSERT_EQ (model.get_rvalue (arr_0, NULL), model.get_rvalue (int_17, NULL)); |
| 9462 |
ASSERT_EQ (model.get_rvalue (arr_i, NULL)->get_kind (), SK_UNKNOWN); |
| 9463 |
} |
| 9464 |
|
| 9465 |
/* Smoketest of dereferencing a pointer via MEM_REF. */ |
| 9466 |
|
| 9467 |
static void |
| 9468 |
test_mem_ref () |
| 9469 |
{ |
| 9470 |
/* |
| 9471 |
x = 17; |
| 9472 |
p = &x; |
| 9473 |
*p; |
| 9474 |
*/ |
| 9475 |
tree x = build_global_decl ("x", integer_type_node); |
| 9476 |
tree int_star = build_pointer_type (integer_type_node); |
| 9477 |
tree p = build_global_decl ("p", int_star); |
| 9478 |
|
| 9479 |
tree int_17 = build_int_cst (integer_type_node, 17); |
| 9480 |
tree addr_of_x = build1 (ADDR_EXPR, int_star, x); |
| 9481 |
tree ptype = build_pointer_type_for_mode (char_type_node, ptr_mode, true); |
| 9482 |
tree offset_0 = build_int_cst (ptype, 0); |
| 9483 |
tree star_p = build2 (MEM_REF, integer_type_node, p, offset_0); |
| 9484 |
|
| 9485 |
region_model_manager mgr; |
| 9486 |
region_model model (&mgr); |
| 9487 |
|
| 9488 |
/* "x = 17;". */ |
| 9489 |
model.set_value (x, int_17, NULL); |
| 9490 |
|
| 9491 |
/* "p = &x;". */ |
| 9492 |
model.set_value (p, addr_of_x, NULL); |
| 9493 |
|
| 9494 |
const svalue *sval = model.get_rvalue (star_p, NULL); |
| 9495 |
ASSERT_EQ (sval->maybe_get_constant (), int_17); |
| 9496 |
} |
| 9497 |
|
| 9498 |
/* Test for a POINTER_PLUS_EXPR followed by a MEM_REF. |
| 9499 |
Analogous to this code: |
| 9500 |
void test_6 (int a[10]) |
| 9501 |
{ |
| 9502 |
__analyzer_eval (a[3] == 42); [should be UNKNOWN] |
| 9503 |
a[3] = 42; |
| 9504 |
__analyzer_eval (a[3] == 42); [should be TRUE] |
| 9505 |
} |
| 9506 |
from data-model-1.c, which looks like this at the gimple level: |
| 9507 |
# __analyzer_eval (a[3] == 42); [should be UNKNOWN] |
| 9508 |
int *_1 = a_10(D) + 12; # POINTER_PLUS_EXPR |
| 9509 |
int _2 = *_1; # MEM_REF |
| 9510 |
_Bool _3 = _2 == 42; |
| 9511 |
int _4 = (int) _3; |
| 9512 |
__analyzer_eval (_4); |
| 9513 |
|
| 9514 |
# a[3] = 42; |
| 9515 |
int *_5 = a_10(D) + 12; # POINTER_PLUS_EXPR |
| 9516 |
*_5 = 42; # MEM_REF |
| 9517 |
|
| 9518 |
# __analyzer_eval (a[3] == 42); [should be TRUE] |
| 9519 |
int *_6 = a_10(D) + 12; # POINTER_PLUS_EXPR |
| 9520 |
int _7 = *_6; # MEM_REF |
| 9521 |
_Bool _8 = _7 == 42; |
| 9522 |
int _9 = (int) _8; |
| 9523 |
__analyzer_eval (_9); */ |
| 9524 |
|
| 9525 |
static void |
| 9526 |
test_POINTER_PLUS_EXPR_then_MEM_REF () |
| 9527 |
{ |
| 9528 |
tree int_star = build_pointer_type (integer_type_node); |
| 9529 |
tree a = build_global_decl ("a", int_star); |
| 9530 |
tree offset_12 = build_int_cst (size_type_node, 12); |
| 9531 |
tree pointer_plus_expr = build2 (POINTER_PLUS_EXPR, int_star, a, offset_12); |
| 9532 |
tree ptype = build_pointer_type_for_mode (char_type_node, ptr_mode, true); |
| 9533 |
tree offset_0 = build_int_cst (ptype, 0); |
| 9534 |
tree mem_ref = build2 (MEM_REF, integer_type_node, |
| 9535 |
pointer_plus_expr, offset_0); |
| 9536 |
region_model_manager mgr; |
| 9537 |
region_model m (&mgr); |
| 9538 |
|
| 9539 |
tree int_42 = build_int_cst (integer_type_node, 42); |
| 9540 |
m.set_value (mem_ref, int_42, NULL); |
| 9541 |
ASSERT_EQ (m.get_rvalue (mem_ref, NULL)->maybe_get_constant (), int_42); |
| 9542 |
} |
| 9543 |
|
| 9544 |
/* Verify that malloc works. */ |
| 9545 |
|
| 9546 |
static void |
| 9547 |
test_malloc () |
| 9548 |
{ |
| 9549 |
tree int_star = build_pointer_type (integer_type_node); |
| 9550 |
tree p = build_global_decl ("p", int_star); |
| 9551 |
tree n = build_global_decl ("n", integer_type_node); |
| 9552 |
tree n_times_4 = build2 (MULT_EXPR, size_type_node, |
| 9553 |
n, build_int_cst (size_type_node, 4)); |
| 9554 |
|
| 9555 |
region_model_manager mgr; |
| 9556 |
test_region_model_context ctxt; |
| 9557 |
region_model model (&mgr); |
| 9558 |
|
| 9559 |
/* "p = malloc (n * 4);". */ |
| 9560 |
const svalue *size_sval = model.get_rvalue (n_times_4, &ctxt); |
| 9561 |
const region *reg |
| 9562 |
= model.get_or_create_region_for_heap_alloc (size_sval, &ctxt); |
| 9563 |
const svalue *ptr = mgr.get_ptr_svalue (int_star, reg); |
| 9564 |
model.set_value (model.get_lvalue (p, &ctxt), ptr, &ctxt); |
| 9565 |
ASSERT_EQ (model.get_capacity (reg), size_sval); |
| 9566 |
} |
| 9567 |
|
| 9568 |
/* Verify that alloca works. */ |
| 9569 |
|
| 9570 |
static void |
| 9571 |
test_alloca () |
| 9572 |
{ |
| 9573 |
auto_vec <tree> param_types; |
| 9574 |
tree fndecl = make_fndecl (integer_type_node, |
| 9575 |
"test_fn", |
| 9576 |
param_types); |
| 9577 |
allocate_struct_function (fndecl, true); |
| 9578 |
|
| 9579 |
|
| 9580 |
tree int_star = build_pointer_type (integer_type_node); |
| 9581 |
tree p = build_global_decl ("p", int_star); |
| 9582 |
tree n = build_global_decl ("n", integer_type_node); |
| 9583 |
tree n_times_4 = build2 (MULT_EXPR, size_type_node, |
| 9584 |
n, build_int_cst (size_type_node, 4)); |
| 9585 |
|
| 9586 |
region_model_manager mgr; |
| 9587 |
test_region_model_context ctxt; |
| 9588 |
region_model model (&mgr); |
| 9589 |
|
| 9590 |
/* Push stack frame. */ |
| 9591 |
const region *frame_reg |
| 9592 |
= model.push_frame (*DECL_STRUCT_FUNCTION (fndecl), |
| 9593 |
NULL, &ctxt); |
| 9594 |
/* "p = alloca (n * 4);". */ |
| 9595 |
const svalue *size_sval = model.get_rvalue (n_times_4, &ctxt); |
| 9596 |
const region *reg = model.create_region_for_alloca (size_sval, &ctxt); |
| 9597 |
ASSERT_EQ (reg->get_parent_region (), frame_reg); |
| 9598 |
const svalue *ptr = mgr.get_ptr_svalue (int_star, reg); |
| 9599 |
model.set_value (model.get_lvalue (p, &ctxt), ptr, &ctxt); |
| 9600 |
ASSERT_EQ (model.get_capacity (reg), size_sval); |
| 9601 |
|
| 9602 |
/* Verify that the pointers to the alloca region are replaced by |
| 9603 |
poisoned values when the frame is popped. */ |
| 9604 |
model.pop_frame (NULL, NULL, &ctxt, nullptr); |
| 9605 |
ASSERT_EQ (model.get_rvalue (p, NULL)->get_kind (), SK_POISONED); |
| 9606 |
} |
| 9607 |
|
| 9608 |
/* Verify that svalue::involves_p works. */ |
| 9609 |
|
| 9610 |
static void |
| 9611 |
test_involves_p () |
| 9612 |
{ |
| 9613 |
region_model_manager mgr; |
| 9614 |
tree int_star = build_pointer_type (integer_type_node); |
| 9615 |
tree p = build_global_decl ("p", int_star); |
| 9616 |
tree q = build_global_decl ("q", int_star); |
| 9617 |
|
| 9618 |
test_region_model_context ctxt; |
| 9619 |
region_model model (&mgr); |
| 9620 |
const svalue *p_init = model.get_rvalue (p, &ctxt); |
| 9621 |
const svalue *q_init = model.get_rvalue (q, &ctxt); |
| 9622 |
|
| 9623 |
ASSERT_TRUE (p_init->involves_p (p_init)); |
| 9624 |
ASSERT_FALSE (p_init->involves_p (q_init)); |
| 9625 |
|
| 9626 |
const region *star_p_reg = mgr.get_symbolic_region (p_init); |
| 9627 |
const region *star_q_reg = mgr.get_symbolic_region (q_init); |
| 9628 |
|
| 9629 |
const svalue *init_star_p = mgr.get_or_create_initial_value (star_p_reg); |
| 9630 |
const svalue *init_star_q = mgr.get_or_create_initial_value (star_q_reg); |
| 9631 |
|
| 9632 |
ASSERT_TRUE (init_star_p->involves_p (p_init)); |
| 9633 |
ASSERT_FALSE (p_init->involves_p (init_star_p)); |
| 9634 |
ASSERT_FALSE (init_star_p->involves_p (q_init)); |
| 9635 |
ASSERT_TRUE (init_star_q->involves_p (q_init)); |
| 9636 |
ASSERT_FALSE (init_star_q->involves_p (p_init)); |
| 9637 |
} |
| 9638 |
|
| 9639 |
/* Run all of the selftests within this file. */ |
| 9640 |
|
| 9641 |
void |
| 9642 |
analyzer_region_model_cc_tests () |
| 9643 |
{ |
| 9644 |
test_tree_cmp_on_constants (); |
| 9645 |
test_dump (); |
| 9646 |
test_struct (); |
| 9647 |
test_array_1 (); |
| 9648 |
test_get_representative_tree (); |
| 9649 |
test_unique_constants (); |
| 9650 |
test_unique_unknowns (); |
| 9651 |
test_initial_svalue_folding (); |
| 9652 |
test_unaryop_svalue_folding (); |
| 9653 |
test_binop_svalue_folding (); |
| 9654 |
test_sub_svalue_folding (); |
| 9655 |
test_bits_within_svalue_folding (); |
| 9656 |
test_descendent_of_p (); |
| 9657 |
test_bit_range_regions (); |
| 9658 |
test_assignment (); |
| 9659 |
test_compound_assignment (); |
| 9660 |
test_stack_frames (); |
| 9661 |
test_get_representative_path_var (); |
| 9662 |
test_equality_1 (); |
| 9663 |
test_canonicalization_2 (); |
| 9664 |
test_canonicalization_3 (); |
| 9665 |
test_canonicalization_4 (); |
| 9666 |
test_state_merging (); |
| 9667 |
test_constraint_merging (); |
| 9668 |
test_widening_constraints (); |
| 9669 |
test_iteration_1 (); |
| 9670 |
test_malloc_constraints (); |
| 9671 |
test_var (); |
| 9672 |
test_array_2 (); |
| 9673 |
test_mem_ref (); |
| 9674 |
test_POINTER_PLUS_EXPR_then_MEM_REF (); |
| 9675 |
test_malloc (); |
| 9676 |
test_alloca (); |
| 9677 |
test_involves_p (); |
| 9678 |
} |
| 9679 |
|
| 9680 |
} // namespace selftest |
| 9681 |
|
| 9682 |
#endif /* CHECKING_P */ |
| 9683 |
|
| 9684 |
} // namespace ana |
| 9685 |
|
| 9686 |
#endif /* #if ENABLE_ANALYZER */ |