| 1 |
/* The analysis "engine". |
| 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_VECTOR |
| 23 |
#include "system.h" |
| 24 |
#include "coretypes.h" |
| 25 |
#include "make-unique.h" |
| 26 |
#include "tree.h" |
| 27 |
#include "fold-const.h" |
| 28 |
#include "gcc-rich-location.h" |
| 29 |
#include "diagnostic-core.h" |
| 30 |
#include "diagnostic-event-id.h" |
| 31 |
#include "diagnostic-path.h" |
| 32 |
#include "function.h" |
| 33 |
#include "pretty-print.h" |
| 34 |
#include "sbitmap.h" |
| 35 |
#include "bitmap.h" |
| 36 |
#include "ordered-hash-map.h" |
| 37 |
#include "analyzer/analyzer.h" |
| 38 |
#include "analyzer/analyzer-logging.h" |
| 39 |
#include "analyzer/call-string.h" |
| 40 |
#include "analyzer/program-point.h" |
| 41 |
#include "analyzer/store.h" |
| 42 |
#include "analyzer/region-model.h" |
| 43 |
#include "analyzer/constraint-manager.h" |
| 44 |
#include "analyzer/sm.h" |
| 45 |
#include "analyzer/pending-diagnostic.h" |
| 46 |
#include "analyzer/diagnostic-manager.h" |
| 47 |
#include "cfg.h" |
| 48 |
#include "basic-block.h" |
| 49 |
#include "gimple.h" |
| 50 |
#include "gimple-iterator.h" |
| 51 |
#include "gimple-pretty-print.h" |
| 52 |
#include "cgraph.h" |
| 53 |
#include "digraph.h" |
| 54 |
#include "analyzer/supergraph.h" |
| 55 |
#include "analyzer/program-state.h" |
| 56 |
#include "analyzer/exploded-graph.h" |
| 57 |
#include "analyzer/analysis-plan.h" |
| 58 |
#include "analyzer/checker-path.h" |
| 59 |
#include "analyzer/state-purge.h" |
| 60 |
#include "analyzer/bar-chart.h" |
| 61 |
#include "analyzer/call-info.h" |
| 62 |
#include <zlib.h> |
| 63 |
#include "plugin.h" |
| 64 |
#include "target.h" |
| 65 |
#include <memory> |
| 66 |
#include "stringpool.h" |
| 67 |
#include "attribs.h" |
| 68 |
#include "tree-dfa.h" |
| 69 |
#include "analyzer/known-function-manager.h" |
| 70 |
#include "analyzer/call-summary.h" |
| 71 |
#include "text-art/dump.h" |
| 72 |
|
| 73 |
/* For an overview, see gcc/doc/analyzer.texi. */ |
| 74 |
|
| 75 |
#if ENABLE_ANALYZER |
| 76 |
|
| 77 |
namespace ana { |
| 78 |
|
| 79 |
/* class impl_region_model_context : public region_model_context. */ |
| 80 |
|
| 81 |
impl_region_model_context:: |
| 82 |
impl_region_model_context (exploded_graph &eg, |
| 83 |
exploded_node *enode_for_diag, |
| 84 |
const program_state *old_state, |
| 85 |
program_state *new_state, |
| 86 |
uncertainty_t *uncertainty, |
| 87 |
path_context *path_ctxt, |
| 88 |
const gimple *stmt, |
| 89 |
stmt_finder *stmt_finder, |
| 90 |
bool *out_could_have_done_work) |
| 91 |
: m_eg (&eg), m_logger (eg.get_logger ()), |
| 92 |
m_enode_for_diag (enode_for_diag), |
| 93 |
m_old_state (old_state), |
| 94 |
m_new_state (new_state), |
| 95 |
m_stmt (stmt), |
| 96 |
m_stmt_finder (stmt_finder), |
| 97 |
m_ext_state (eg.get_ext_state ()), |
| 98 |
m_uncertainty (uncertainty), |
| 99 |
m_path_ctxt (path_ctxt), |
| 100 |
m_out_could_have_done_work (out_could_have_done_work) |
| 101 |
{ |
| 102 |
} |
| 103 |
|
| 104 |
impl_region_model_context:: |
| 105 |
impl_region_model_context (program_state *state, |
| 106 |
const extrinsic_state &ext_state, |
| 107 |
uncertainty_t *uncertainty, |
| 108 |
logger *logger) |
| 109 |
: m_eg (NULL), m_logger (logger), m_enode_for_diag (NULL), |
| 110 |
m_old_state (NULL), |
| 111 |
m_new_state (state), |
| 112 |
m_stmt (NULL), |
| 113 |
m_stmt_finder (NULL), |
| 114 |
m_ext_state (ext_state), |
| 115 |
m_uncertainty (uncertainty), |
| 116 |
m_path_ctxt (NULL), |
| 117 |
m_out_could_have_done_work (nullptr) |
| 118 |
{ |
| 119 |
} |
| 120 |
|
| 121 |
bool |
| 122 |
impl_region_model_context::warn (std::unique_ptr<pending_diagnostic> d, |
| 123 |
const stmt_finder *custom_finder) |
| 124 |
{ |
| 125 |
LOG_FUNC (get_logger ()); |
| 126 |
auto curr_stmt_finder = custom_finder ? custom_finder : m_stmt_finder; |
| 127 |
if (m_stmt == NULL && curr_stmt_finder == NULL) |
| 128 |
{ |
| 129 |
if (get_logger ()) |
| 130 |
get_logger ()->log ("rejecting diagnostic: no stmt"); |
| 131 |
return false; |
| 132 |
} |
| 133 |
if (m_eg) |
| 134 |
{ |
| 135 |
bool terminate_path = d->terminate_path_p (); |
| 136 |
pending_location ploc (m_enode_for_diag, |
| 137 |
m_enode_for_diag->get_supernode (), |
| 138 |
m_stmt, |
| 139 |
curr_stmt_finder); |
| 140 |
if (m_eg->get_diagnostic_manager ().add_diagnostic (ploc, |
| 141 |
std::move (d))) |
| 142 |
{ |
| 143 |
if (m_path_ctxt |
| 144 |
&& terminate_path |
| 145 |
&& flag_analyzer_suppress_followups) |
| 146 |
m_path_ctxt->terminate_path (); |
| 147 |
return true; |
| 148 |
} |
| 149 |
} |
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
void |
| 154 |
impl_region_model_context::add_note (std::unique_ptr<pending_note> pn) |
| 155 |
{ |
| 156 |
LOG_FUNC (get_logger ()); |
| 157 |
if (m_eg) |
| 158 |
m_eg->get_diagnostic_manager ().add_note (std::move (pn)); |
| 159 |
} |
| 160 |
|
| 161 |
void |
| 162 |
impl_region_model_context::add_event (std::unique_ptr<checker_event> event) |
| 163 |
{ |
| 164 |
LOG_FUNC (get_logger ()); |
| 165 |
if (m_eg) |
| 166 |
m_eg->get_diagnostic_manager ().add_event (std::move (event)); |
| 167 |
} |
| 168 |
|
| 169 |
void |
| 170 |
impl_region_model_context::on_svalue_leak (const svalue *sval) |
| 171 |
|
| 172 |
{ |
| 173 |
for (sm_state_map *smap : m_new_state->m_checker_states) |
| 174 |
smap->on_svalue_leak (sval, this); |
| 175 |
} |
| 176 |
|
| 177 |
void |
| 178 |
impl_region_model_context:: |
| 179 |
on_liveness_change (const svalue_set &live_svalues, |
| 180 |
const region_model *model) |
| 181 |
{ |
| 182 |
for (sm_state_map *smap : m_new_state->m_checker_states) |
| 183 |
smap->on_liveness_change (live_svalues, model, m_ext_state, this); |
| 184 |
} |
| 185 |
|
| 186 |
void |
| 187 |
impl_region_model_context::on_unknown_change (const svalue *sval, |
| 188 |
bool is_mutable) |
| 189 |
{ |
| 190 |
if (!sval->can_have_associated_state_p ()) |
| 191 |
return; |
| 192 |
for (sm_state_map *smap : m_new_state->m_checker_states) |
| 193 |
smap->on_unknown_change (sval, is_mutable, m_ext_state); |
| 194 |
} |
| 195 |
|
| 196 |
void |
| 197 |
impl_region_model_context::on_escaped_function (tree fndecl) |
| 198 |
{ |
| 199 |
m_eg->on_escaped_function (fndecl); |
| 200 |
} |
| 201 |
|
| 202 |
uncertainty_t * |
| 203 |
impl_region_model_context::get_uncertainty () |
| 204 |
{ |
| 205 |
return m_uncertainty; |
| 206 |
} |
| 207 |
|
| 208 |
/* Purge state involving SVAL. The region_model has already been purged, |
| 209 |
so we only need to purge other state in the program_state: |
| 210 |
the sm-state. */ |
| 211 |
|
| 212 |
void |
| 213 |
impl_region_model_context::purge_state_involving (const svalue *sval) |
| 214 |
{ |
| 215 |
int i; |
| 216 |
sm_state_map *smap; |
| 217 |
FOR_EACH_VEC_ELT (m_new_state->m_checker_states, i, smap) |
| 218 |
smap->purge_state_involving (sval, m_ext_state); |
| 219 |
} |
| 220 |
|
| 221 |
void |
| 222 |
impl_region_model_context::bifurcate (std::unique_ptr<custom_edge_info> info) |
| 223 |
{ |
| 224 |
if (m_path_ctxt) |
| 225 |
m_path_ctxt->bifurcate (std::move (info)); |
| 226 |
} |
| 227 |
|
| 228 |
void |
| 229 |
impl_region_model_context::terminate_path () |
| 230 |
{ |
| 231 |
if (m_path_ctxt) |
| 232 |
return m_path_ctxt->terminate_path (); |
| 233 |
} |
| 234 |
|
| 235 |
/* struct setjmp_record. */ |
| 236 |
|
| 237 |
int |
| 238 |
setjmp_record::cmp (const setjmp_record &rec1, const setjmp_record &rec2) |
| 239 |
{ |
| 240 |
if (int cmp_enode = rec1.m_enode->m_index - rec2.m_enode->m_index) |
| 241 |
return cmp_enode; |
| 242 |
gcc_assert (&rec1 == &rec2); |
| 243 |
return 0; |
| 244 |
} |
| 245 |
|
| 246 |
/* class setjmp_svalue : public svalue. */ |
| 247 |
|
| 248 |
/* Implementation of svalue::accept vfunc for setjmp_svalue. */ |
| 249 |
|
| 250 |
void |
| 251 |
setjmp_svalue::accept (visitor *v) const |
| 252 |
{ |
| 253 |
v->visit_setjmp_svalue (this); |
| 254 |
} |
| 255 |
|
| 256 |
/* Implementation of svalue::dump_to_pp vfunc for setjmp_svalue. */ |
| 257 |
|
| 258 |
void |
| 259 |
setjmp_svalue::dump_to_pp (pretty_printer *pp, bool simple) const |
| 260 |
{ |
| 261 |
if (simple) |
| 262 |
pp_printf (pp, "SETJMP(EN: %i)", get_enode_index ()); |
| 263 |
else |
| 264 |
pp_printf (pp, "setjmp_svalue(EN%i)", get_enode_index ()); |
| 265 |
} |
| 266 |
|
| 267 |
/* Implementation of svalue::print_dump_widget_label vfunc for |
| 268 |
setjmp_svalue. */ |
| 269 |
|
| 270 |
void |
| 271 |
setjmp_svalue::print_dump_widget_label (pretty_printer *pp) const |
| 272 |
{ |
| 273 |
pp_printf (pp, "setjmp_svalue(EN: %i)", get_enode_index ()); |
| 274 |
} |
| 275 |
|
| 276 |
/* Implementation of svalue::add_dump_widget_children vfunc for |
| 277 |
setjmp_svalue. */ |
| 278 |
|
| 279 |
void |
| 280 |
setjmp_svalue:: |
| 281 |
add_dump_widget_children (text_art::tree_widget &, |
| 282 |
const text_art::dump_widget_info &) const |
| 283 |
{ |
| 284 |
/* No children. */ |
| 285 |
} |
| 286 |
|
| 287 |
/* Get the index of the stored exploded_node. */ |
| 288 |
|
| 289 |
int |
| 290 |
setjmp_svalue::get_enode_index () const |
| 291 |
{ |
| 292 |
return m_setjmp_record.m_enode->m_index; |
| 293 |
} |
| 294 |
|
| 295 |
/* Concrete implementation of sm_context, wiring it up to the rest of this |
| 296 |
file. */ |
| 297 |
|
| 298 |
class impl_sm_context : public sm_context |
| 299 |
{ |
| 300 |
public: |
| 301 |
impl_sm_context (exploded_graph &eg, |
| 302 |
int sm_idx, |
| 303 |
const state_machine &sm, |
| 304 |
exploded_node *enode_for_diag, |
| 305 |
const program_state *old_state, |
| 306 |
program_state *new_state, |
| 307 |
const sm_state_map *old_smap, |
| 308 |
sm_state_map *new_smap, |
| 309 |
path_context *path_ctxt, |
| 310 |
const stmt_finder *stmt_finder = NULL, |
| 311 |
bool unknown_side_effects = false) |
| 312 |
: sm_context (sm_idx, sm), |
| 313 |
m_logger (eg.get_logger ()), |
| 314 |
m_eg (eg), m_enode_for_diag (enode_for_diag), |
| 315 |
m_old_state (old_state), m_new_state (new_state), |
| 316 |
m_old_smap (old_smap), m_new_smap (new_smap), |
| 317 |
m_path_ctxt (path_ctxt), |
| 318 |
m_stmt_finder (stmt_finder), |
| 319 |
m_unknown_side_effects (unknown_side_effects) |
| 320 |
{ |
| 321 |
} |
| 322 |
|
| 323 |
logger *get_logger () const { return m_logger.get_logger (); } |
| 324 |
|
| 325 |
tree get_fndecl_for_call (const gcall *call) final override |
| 326 |
{ |
| 327 |
impl_region_model_context old_ctxt |
| 328 |
(m_eg, m_enode_for_diag, NULL, NULL, NULL/*m_enode->get_state ()*/, |
| 329 |
NULL, call); |
| 330 |
region_model *model = m_new_state->m_region_model; |
| 331 |
return model->get_fndecl_for_call (call, &old_ctxt); |
| 332 |
} |
| 333 |
|
| 334 |
state_machine::state_t get_state (const gimple *stmt ATTRIBUTE_UNUSED, |
| 335 |
tree var) final override |
| 336 |
{ |
| 337 |
logger * const logger = get_logger (); |
| 338 |
LOG_FUNC (logger); |
| 339 |
/* Use NULL ctxt on this get_rvalue call to avoid triggering |
| 340 |
uninitialized value warnings. */ |
| 341 |
const svalue *var_old_sval |
| 342 |
= m_old_state->m_region_model->get_rvalue (var, NULL); |
| 343 |
|
| 344 |
state_machine::state_t current |
| 345 |
= m_old_smap->get_state (var_old_sval, m_eg.get_ext_state ()); |
| 346 |
return current; |
| 347 |
} |
| 348 |
state_machine::state_t get_state (const gimple *stmt ATTRIBUTE_UNUSED, |
| 349 |
const svalue *sval) final override |
| 350 |
{ |
| 351 |
logger * const logger = get_logger (); |
| 352 |
LOG_FUNC (logger); |
| 353 |
state_machine::state_t current |
| 354 |
= m_old_smap->get_state (sval, m_eg.get_ext_state ()); |
| 355 |
return current; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
void set_next_state (const gimple *, |
| 360 |
tree var, |
| 361 |
state_machine::state_t to, |
| 362 |
tree origin) final override |
| 363 |
{ |
| 364 |
logger * const logger = get_logger (); |
| 365 |
LOG_FUNC (logger); |
| 366 |
const svalue *var_new_sval |
| 367 |
= m_new_state->m_region_model->get_rvalue (var, NULL); |
| 368 |
const svalue *origin_new_sval |
| 369 |
= m_new_state->m_region_model->get_rvalue (origin, NULL); |
| 370 |
|
| 371 |
/* We use the new sval here to avoid issues with uninitialized values. */ |
| 372 |
state_machine::state_t current |
| 373 |
= m_old_smap->get_state (var_new_sval, m_eg.get_ext_state ()); |
| 374 |
if (logger) |
| 375 |
logger->log ("%s: state transition of %qE: %s -> %s", |
| 376 |
m_sm.get_name (), |
| 377 |
var, |
| 378 |
current->get_name (), |
| 379 |
to->get_name ()); |
| 380 |
m_new_smap->set_state (m_new_state->m_region_model, var_new_sval, |
| 381 |
to, origin_new_sval, m_eg.get_ext_state ()); |
| 382 |
} |
| 383 |
|
| 384 |
void set_next_state (const gimple *stmt, |
| 385 |
const svalue *sval, |
| 386 |
state_machine::state_t to, |
| 387 |
tree origin) final override |
| 388 |
{ |
| 389 |
logger * const logger = get_logger (); |
| 390 |
LOG_FUNC (logger); |
| 391 |
impl_region_model_context old_ctxt |
| 392 |
(m_eg, m_enode_for_diag, NULL, NULL, NULL/*m_enode->get_state ()*/, |
| 393 |
NULL, stmt); |
| 394 |
|
| 395 |
const svalue *origin_new_sval |
| 396 |
= m_new_state->m_region_model->get_rvalue (origin, NULL); |
| 397 |
|
| 398 |
state_machine::state_t current |
| 399 |
= m_old_smap->get_state (sval, m_eg.get_ext_state ()); |
| 400 |
if (logger) |
| 401 |
{ |
| 402 |
logger->start_log_line (); |
| 403 |
logger->log_partial ("%s: state transition of ", |
| 404 |
m_sm.get_name ()); |
| 405 |
sval->dump_to_pp (logger->get_printer (), true); |
| 406 |
logger->log_partial (": %s -> %s", |
| 407 |
current->get_name (), |
| 408 |
to->get_name ()); |
| 409 |
logger->end_log_line (); |
| 410 |
} |
| 411 |
m_new_smap->set_state (m_new_state->m_region_model, sval, |
| 412 |
to, origin_new_sval, m_eg.get_ext_state ()); |
| 413 |
} |
| 414 |
|
| 415 |
void warn (const supernode *snode, const gimple *stmt, |
| 416 |
tree var, |
| 417 |
std::unique_ptr<pending_diagnostic> d) final override |
| 418 |
{ |
| 419 |
LOG_FUNC (get_logger ()); |
| 420 |
gcc_assert (d); |
| 421 |
const svalue *var_old_sval |
| 422 |
= m_old_state->m_region_model->get_rvalue (var, NULL); |
| 423 |
state_machine::state_t current |
| 424 |
= (var |
| 425 |
? m_old_smap->get_state (var_old_sval, m_eg.get_ext_state ()) |
| 426 |
: m_old_smap->get_global_state ()); |
| 427 |
bool terminate_path = d->terminate_path_p (); |
| 428 |
pending_location ploc (m_enode_for_diag, snode, stmt, m_stmt_finder); |
| 429 |
m_eg.get_diagnostic_manager ().add_diagnostic |
| 430 |
(&m_sm, ploc, |
| 431 |
var, var_old_sval, current, std::move (d)); |
| 432 |
if (m_path_ctxt |
| 433 |
&& terminate_path |
| 434 |
&& flag_analyzer_suppress_followups) |
| 435 |
m_path_ctxt->terminate_path (); |
| 436 |
} |
| 437 |
|
| 438 |
void warn (const supernode *snode, const gimple *stmt, |
| 439 |
const svalue *sval, |
| 440 |
std::unique_ptr<pending_diagnostic> d) final override |
| 441 |
{ |
| 442 |
LOG_FUNC (get_logger ()); |
| 443 |
gcc_assert (d); |
| 444 |
state_machine::state_t current |
| 445 |
= (sval |
| 446 |
? m_old_smap->get_state (sval, m_eg.get_ext_state ()) |
| 447 |
: m_old_smap->get_global_state ()); |
| 448 |
bool terminate_path = d->terminate_path_p (); |
| 449 |
pending_location ploc (m_enode_for_diag, snode, stmt, m_stmt_finder); |
| 450 |
m_eg.get_diagnostic_manager ().add_diagnostic |
| 451 |
(&m_sm, ploc, |
| 452 |
NULL_TREE, sval, current, std::move (d)); |
| 453 |
if (m_path_ctxt |
| 454 |
&& terminate_path |
| 455 |
&& flag_analyzer_suppress_followups) |
| 456 |
m_path_ctxt->terminate_path (); |
| 457 |
} |
| 458 |
|
| 459 |
/* Hook for picking more readable trees for SSA names of temporaries, |
| 460 |
so that rather than e.g. |
| 461 |
"double-free of '<unknown>'" |
| 462 |
we can print: |
| 463 |
"double-free of 'inbuf.data'". */ |
| 464 |
|
| 465 |
tree get_diagnostic_tree (tree expr) final override |
| 466 |
{ |
| 467 |
/* Only for SSA_NAMEs of temporaries; otherwise, return EXPR, as it's |
| 468 |
likely to be the least surprising tree to report. */ |
| 469 |
if (TREE_CODE (expr) != SSA_NAME) |
| 470 |
return expr; |
| 471 |
if (SSA_NAME_VAR (expr) != NULL) |
| 472 |
return expr; |
| 473 |
|
| 474 |
gcc_assert (m_new_state); |
| 475 |
const svalue *sval = m_new_state->m_region_model->get_rvalue (expr, NULL); |
| 476 |
/* Find trees for all regions storing the value. */ |
| 477 |
if (tree t = m_new_state->m_region_model->get_representative_tree (sval)) |
| 478 |
return t; |
| 479 |
else |
| 480 |
return expr; |
| 481 |
} |
| 482 |
|
| 483 |
tree get_diagnostic_tree (const svalue *sval) final override |
| 484 |
{ |
| 485 |
return m_new_state->m_region_model->get_representative_tree (sval); |
| 486 |
} |
| 487 |
|
| 488 |
state_machine::state_t get_global_state () const final override |
| 489 |
{ |
| 490 |
return m_old_state->m_checker_states[m_sm_idx]->get_global_state (); |
| 491 |
} |
| 492 |
|
| 493 |
void set_global_state (state_machine::state_t state) final override |
| 494 |
{ |
| 495 |
m_new_state->m_checker_states[m_sm_idx]->set_global_state (state); |
| 496 |
} |
| 497 |
|
| 498 |
void clear_all_per_svalue_state () final override |
| 499 |
{ |
| 500 |
m_new_state->m_checker_states[m_sm_idx]->clear_all_per_svalue_state (); |
| 501 |
} |
| 502 |
|
| 503 |
void on_custom_transition (custom_transition *transition) final override |
| 504 |
{ |
| 505 |
transition->impl_transition (&m_eg, |
| 506 |
const_cast<exploded_node *> (m_enode_for_diag), |
| 507 |
m_sm_idx); |
| 508 |
} |
| 509 |
|
| 510 |
tree is_zero_assignment (const gimple *stmt) final override |
| 511 |
{ |
| 512 |
const gassign *assign_stmt = dyn_cast <const gassign *> (stmt); |
| 513 |
if (!assign_stmt) |
| 514 |
return NULL_TREE; |
| 515 |
impl_region_model_context old_ctxt |
| 516 |
(m_eg, m_enode_for_diag, m_old_state, m_new_state, NULL, NULL, stmt); |
| 517 |
if (const svalue *sval |
| 518 |
= m_new_state->m_region_model->get_gassign_result (assign_stmt, |
| 519 |
&old_ctxt)) |
| 520 |
if (tree cst = sval->maybe_get_constant ()) |
| 521 |
if (::zerop(cst)) |
| 522 |
return gimple_assign_lhs (assign_stmt); |
| 523 |
return NULL_TREE; |
| 524 |
} |
| 525 |
|
| 526 |
path_context *get_path_context () const final override |
| 527 |
{ |
| 528 |
return m_path_ctxt; |
| 529 |
} |
| 530 |
|
| 531 |
bool unknown_side_effects_p () const final override |
| 532 |
{ |
| 533 |
return m_unknown_side_effects; |
| 534 |
} |
| 535 |
|
| 536 |
const program_state *get_old_program_state () const final override |
| 537 |
{ |
| 538 |
return m_old_state; |
| 539 |
} |
| 540 |
|
| 541 |
const program_state *get_new_program_state () const final override |
| 542 |
{ |
| 543 |
return m_new_state; |
| 544 |
} |
| 545 |
|
| 546 |
log_user m_logger; |
| 547 |
exploded_graph &m_eg; |
| 548 |
exploded_node *m_enode_for_diag; |
| 549 |
const program_state *m_old_state; |
| 550 |
program_state *m_new_state; |
| 551 |
const sm_state_map *m_old_smap; |
| 552 |
sm_state_map *m_new_smap; |
| 553 |
path_context *m_path_ctxt; |
| 554 |
const stmt_finder *m_stmt_finder; |
| 555 |
|
| 556 |
/* Are we handling an external function with unknown side effects? */ |
| 557 |
bool m_unknown_side_effects; |
| 558 |
}; |
| 559 |
|
| 560 |
bool |
| 561 |
impl_region_model_context:: |
| 562 |
get_state_map_by_name (const char *name, |
| 563 |
sm_state_map **out_smap, |
| 564 |
const state_machine **out_sm, |
| 565 |
unsigned *out_sm_idx, |
| 566 |
std::unique_ptr<sm_context> *out_sm_context) |
| 567 |
{ |
| 568 |
if (!m_new_state) |
| 569 |
return false; |
| 570 |
|
| 571 |
unsigned sm_idx; |
| 572 |
if (!m_ext_state.get_sm_idx_by_name (name, &sm_idx)) |
| 573 |
return false; |
| 574 |
|
| 575 |
const state_machine *sm = &m_ext_state.get_sm (sm_idx); |
| 576 |
sm_state_map *new_smap = m_new_state->m_checker_states[sm_idx]; |
| 577 |
|
| 578 |
*out_smap = new_smap; |
| 579 |
*out_sm = sm; |
| 580 |
if (out_sm_idx) |
| 581 |
*out_sm_idx = sm_idx; |
| 582 |
if (out_sm_context) |
| 583 |
{ |
| 584 |
const sm_state_map *old_smap = m_old_state->m_checker_states[sm_idx]; |
| 585 |
*out_sm_context |
| 586 |
= make_unique<impl_sm_context> (*m_eg, |
| 587 |
sm_idx, |
| 588 |
*sm, |
| 589 |
m_enode_for_diag, |
| 590 |
m_old_state, |
| 591 |
m_new_state, |
| 592 |
old_smap, |
| 593 |
new_smap, |
| 594 |
m_path_ctxt, |
| 595 |
m_stmt_finder, |
| 596 |
false); |
| 597 |
} |
| 598 |
return true; |
| 599 |
} |
| 600 |
|
| 601 |
/* Subclass of stmt_finder for finding the best stmt to report the leak at, |
| 602 |
given the emission path. */ |
| 603 |
|
| 604 |
class leak_stmt_finder : public stmt_finder |
| 605 |
{ |
| 606 |
public: |
| 607 |
leak_stmt_finder (const exploded_graph &eg, tree var) |
| 608 |
: m_eg (eg), m_var (var) {} |
| 609 |
|
| 610 |
std::unique_ptr<stmt_finder> clone () const final override |
| 611 |
{ |
| 612 |
return make_unique<leak_stmt_finder> (m_eg, m_var); |
| 613 |
} |
| 614 |
|
| 615 |
const gimple *find_stmt (const exploded_path &epath) |
| 616 |
final override |
| 617 |
{ |
| 618 |
logger * const logger = m_eg.get_logger (); |
| 619 |
LOG_FUNC (logger); |
| 620 |
|
| 621 |
if (m_var && TREE_CODE (m_var) == SSA_NAME) |
| 622 |
{ |
| 623 |
/* Locate the final write to this SSA name in the path. */ |
| 624 |
const gimple *def_stmt = SSA_NAME_DEF_STMT (m_var); |
| 625 |
|
| 626 |
int idx_of_def_stmt; |
| 627 |
bool found = epath.find_stmt_backwards (def_stmt, &idx_of_def_stmt); |
| 628 |
if (!found) |
| 629 |
goto not_found; |
| 630 |
|
| 631 |
/* What was the next write to the underlying var |
| 632 |
after the SSA name was set? (if any). */ |
| 633 |
|
| 634 |
for (unsigned idx = idx_of_def_stmt + 1; |
| 635 |
idx < epath.m_edges.length (); |
| 636 |
++idx) |
| 637 |
{ |
| 638 |
const exploded_edge *eedge = epath.m_edges[idx]; |
| 639 |
if (logger) |
| 640 |
logger->log ("eedge[%i]: EN %i -> EN %i", |
| 641 |
idx, |
| 642 |
eedge->m_src->m_index, |
| 643 |
eedge->m_dest->m_index); |
| 644 |
const exploded_node *dst_node = eedge->m_dest; |
| 645 |
const program_point &dst_point = dst_node->get_point (); |
| 646 |
const gimple *stmt = dst_point.get_stmt (); |
| 647 |
if (!stmt) |
| 648 |
continue; |
| 649 |
if (const gassign *assign = dyn_cast <const gassign *> (stmt)) |
| 650 |
{ |
| 651 |
tree lhs = gimple_assign_lhs (assign); |
| 652 |
if (TREE_CODE (lhs) == SSA_NAME |
| 653 |
&& SSA_NAME_VAR (lhs) == SSA_NAME_VAR (m_var)) |
| 654 |
return assign; |
| 655 |
} |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
not_found: |
| 660 |
|
| 661 |
/* Look backwards for the first statement with a location. */ |
| 662 |
int i; |
| 663 |
const exploded_edge *eedge; |
| 664 |
FOR_EACH_VEC_ELT_REVERSE (epath.m_edges, i, eedge) |
| 665 |
{ |
| 666 |
if (logger) |
| 667 |
logger->log ("eedge[%i]: EN %i -> EN %i", |
| 668 |
i, |
| 669 |
eedge->m_src->m_index, |
| 670 |
eedge->m_dest->m_index); |
| 671 |
const exploded_node *dst_node = eedge->m_dest; |
| 672 |
const program_point &dst_point = dst_node->get_point (); |
| 673 |
const gimple *stmt = dst_point.get_stmt (); |
| 674 |
if (stmt) |
| 675 |
if (get_pure_location (stmt->location) != UNKNOWN_LOCATION) |
| 676 |
return stmt; |
| 677 |
} |
| 678 |
|
| 679 |
gcc_unreachable (); |
| 680 |
return NULL; |
| 681 |
} |
| 682 |
|
| 683 |
void update_event_loc_info (event_loc_info &) final override |
| 684 |
{ |
| 685 |
/* No-op. */ |
| 686 |
} |
| 687 |
|
| 688 |
private: |
| 689 |
const exploded_graph &m_eg; |
| 690 |
tree m_var; |
| 691 |
}; |
| 692 |
|
| 693 |
/* A measurement of how good EXPR is for presenting to the user, so |
| 694 |
that e.g. we can say prefer printing |
| 695 |
"leak of 'tmp.m_ptr'" |
| 696 |
over: |
| 697 |
"leak of '<unknown>'". */ |
| 698 |
|
| 699 |
static int |
| 700 |
readability (const_tree expr) |
| 701 |
{ |
| 702 |
/* Arbitrarily-chosen "high readability" value. */ |
| 703 |
const int HIGH_READABILITY = 65536; |
| 704 |
|
| 705 |
gcc_assert (expr); |
| 706 |
switch (TREE_CODE (expr)) |
| 707 |
{ |
| 708 |
case COMPONENT_REF: |
| 709 |
case MEM_REF: |
| 710 |
/* Impose a slight readability penalty relative to that of |
| 711 |
operand 0. */ |
| 712 |
return readability (TREE_OPERAND (expr, 0)) - 16; |
| 713 |
|
| 714 |
case SSA_NAME: |
| 715 |
{ |
| 716 |
if (tree var = SSA_NAME_VAR (expr)) |
| 717 |
{ |
| 718 |
if (DECL_ARTIFICIAL (var)) |
| 719 |
{ |
| 720 |
/* If we have an SSA name for an artificial var, |
| 721 |
only use it if it has a debug expr associated with |
| 722 |
it that fixup_tree_for_diagnostic can use. */ |
| 723 |
if (VAR_P (var) && DECL_HAS_DEBUG_EXPR_P (var)) |
| 724 |
return readability (DECL_DEBUG_EXPR (var)) - 1; |
| 725 |
} |
| 726 |
else |
| 727 |
{ |
| 728 |
/* Slightly favor the underlying var over the SSA name to |
| 729 |
avoid having them compare equal. */ |
| 730 |
return readability (var) - 1; |
| 731 |
} |
| 732 |
} |
| 733 |
/* Avoid printing '<unknown>' for SSA names for temporaries. */ |
| 734 |
return -1; |
| 735 |
} |
| 736 |
break; |
| 737 |
|
| 738 |
case PARM_DECL: |
| 739 |
case VAR_DECL: |
| 740 |
if (DECL_NAME (expr)) |
| 741 |
return HIGH_READABILITY; |
| 742 |
else |
| 743 |
/* We don't want to print temporaries. For example, the C FE |
| 744 |
prints them as e.g. "<Uxxxx>" where "xxxx" is the low 16 bits |
| 745 |
of the tree pointer (see pp_c_tree_decl_identifier). */ |
| 746 |
return -1; |
| 747 |
|
| 748 |
case RESULT_DECL: |
| 749 |
/* Printing "<return-value>" isn't ideal, but is less awful than |
| 750 |
trying to print a temporary. */ |
| 751 |
return HIGH_READABILITY / 2; |
| 752 |
|
| 753 |
case NOP_EXPR: |
| 754 |
{ |
| 755 |
/* Impose a moderate readability penalty for casts. */ |
| 756 |
const int CAST_PENALTY = 32; |
| 757 |
return readability (TREE_OPERAND (expr, 0)) - CAST_PENALTY; |
| 758 |
} |
| 759 |
|
| 760 |
case INTEGER_CST: |
| 761 |
return HIGH_READABILITY; |
| 762 |
|
| 763 |
default: |
| 764 |
return 0; |
| 765 |
} |
| 766 |
|
| 767 |
return 0; |
| 768 |
} |
| 769 |
|
| 770 |
/* A qsort comparator for trees to sort them into most user-readable to |
| 771 |
least user-readable. */ |
| 772 |
|
| 773 |
int |
| 774 |
readability_comparator (const void *p1, const void *p2) |
| 775 |
{ |
| 776 |
path_var pv1 = *(path_var const *)p1; |
| 777 |
path_var pv2 = *(path_var const *)p2; |
| 778 |
|
| 779 |
const int tree_r1 = readability (pv1.m_tree); |
| 780 |
const int tree_r2 = readability (pv2.m_tree); |
| 781 |
|
| 782 |
/* Favor items that are deeper on the stack and hence more recent; |
| 783 |
this also favors locals over globals. */ |
| 784 |
const int COST_PER_FRAME = 64; |
| 785 |
const int depth_r1 = pv1.m_stack_depth * COST_PER_FRAME; |
| 786 |
const int depth_r2 = pv2.m_stack_depth * COST_PER_FRAME; |
| 787 |
|
| 788 |
/* Combine the scores from the tree and from the stack depth. |
| 789 |
This e.g. lets us have a slightly penalized cast in the most |
| 790 |
recent stack frame "beat" an uncast value in an older stack frame. */ |
| 791 |
const int sum_r1 = tree_r1 + depth_r1; |
| 792 |
const int sum_r2 = tree_r2 + depth_r2; |
| 793 |
if (int cmp = sum_r2 - sum_r1) |
| 794 |
return cmp; |
| 795 |
|
| 796 |
/* Otherwise, more readable trees win. */ |
| 797 |
if (int cmp = tree_r2 - tree_r1) |
| 798 |
return cmp; |
| 799 |
|
| 800 |
/* Otherwise, if they have the same readability, then impose an |
| 801 |
arbitrary deterministic ordering on them. */ |
| 802 |
|
| 803 |
if (int cmp = TREE_CODE (pv1.m_tree) - TREE_CODE (pv2.m_tree)) |
| 804 |
return cmp; |
| 805 |
|
| 806 |
switch (TREE_CODE (pv1.m_tree)) |
| 807 |
{ |
| 808 |
default: |
| 809 |
break; |
| 810 |
case SSA_NAME: |
| 811 |
if (int cmp = (SSA_NAME_VERSION (pv1.m_tree) |
| 812 |
- SSA_NAME_VERSION (pv2.m_tree))) |
| 813 |
return cmp; |
| 814 |
break; |
| 815 |
case PARM_DECL: |
| 816 |
case VAR_DECL: |
| 817 |
case RESULT_DECL: |
| 818 |
if (int cmp = DECL_UID (pv1.m_tree) - DECL_UID (pv2.m_tree)) |
| 819 |
return cmp; |
| 820 |
break; |
| 821 |
} |
| 822 |
|
| 823 |
/* TODO: We ought to find ways of sorting such cases. */ |
| 824 |
return 0; |
| 825 |
} |
| 826 |
|
| 827 |
/* Return true is SNODE is the EXIT node of a function, or is one |
| 828 |
of the final snodes within its function. |
| 829 |
|
| 830 |
Specifically, handle the final supernodes before the EXIT node, |
| 831 |
for the case of clobbers that happen immediately before exiting. |
| 832 |
We need a run of snodes leading to the return_p snode, where all edges are |
| 833 |
intraprocedural, and every snode has just one successor. |
| 834 |
|
| 835 |
We use this when suppressing leak reports at the end of "main". */ |
| 836 |
|
| 837 |
static bool |
| 838 |
returning_from_function_p (const supernode *snode) |
| 839 |
{ |
| 840 |
if (!snode) |
| 841 |
return false; |
| 842 |
|
| 843 |
unsigned count = 0; |
| 844 |
const supernode *iter = snode; |
| 845 |
while (true) |
| 846 |
{ |
| 847 |
if (iter->return_p ()) |
| 848 |
return true; |
| 849 |
if (iter->m_succs.length () != 1) |
| 850 |
return false; |
| 851 |
const superedge *sedge = iter->m_succs[0]; |
| 852 |
if (sedge->get_kind () != SUPEREDGE_CFG_EDGE) |
| 853 |
return false; |
| 854 |
iter = sedge->m_dest; |
| 855 |
|
| 856 |
/* Impose a limit to ensure we terminate for pathological cases. |
| 857 |
|
| 858 |
We only care about the final 3 nodes, due to cases like: |
| 859 |
BB: |
| 860 |
(clobber causing leak) |
| 861 |
|
| 862 |
BB: |
| 863 |
<label>: |
| 864 |
return _val; |
| 865 |
|
| 866 |
EXIT BB.*/ |
| 867 |
if (++count > 3) |
| 868 |
return false; |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
/* Find the best tree for SVAL and call SM's on_leak vfunc with it. |
| 873 |
If on_leak returns a pending_diagnostic, queue it up to be reported, |
| 874 |
so that we potentially complain about a leak of SVAL in the given STATE. */ |
| 875 |
|
| 876 |
void |
| 877 |
impl_region_model_context::on_state_leak (const state_machine &sm, |
| 878 |
const svalue *sval, |
| 879 |
state_machine::state_t state) |
| 880 |
{ |
| 881 |
logger * const logger = get_logger (); |
| 882 |
LOG_SCOPE (logger); |
| 883 |
if (logger) |
| 884 |
{ |
| 885 |
logger->start_log_line (); |
| 886 |
logger->log_partial ("considering leak of "); |
| 887 |
sval->dump_to_pp (logger->get_printer (), true); |
| 888 |
logger->end_log_line (); |
| 889 |
} |
| 890 |
|
| 891 |
if (!m_eg) |
| 892 |
return; |
| 893 |
|
| 894 |
/* m_old_state also needs to be non-NULL so that the sm_ctxt can look |
| 895 |
up the old state of SVAL. */ |
| 896 |
gcc_assert (m_old_state); |
| 897 |
|
| 898 |
/* SVAL has leaked within the new state: it is not used by any reachable |
| 899 |
regions. |
| 900 |
We need to convert it back to a tree, but since it's likely no regions |
| 901 |
use it, we have to find the "best" tree for it in the old_state. */ |
| 902 |
svalue_set visited; |
| 903 |
path_var leaked_pv |
| 904 |
= m_old_state->m_region_model->get_representative_path_var (sval, |
| 905 |
&visited, |
| 906 |
nullptr); |
| 907 |
|
| 908 |
/* Strip off top-level casts */ |
| 909 |
if (leaked_pv.m_tree && TREE_CODE (leaked_pv.m_tree) == NOP_EXPR) |
| 910 |
leaked_pv.m_tree = TREE_OPERAND (leaked_pv.m_tree, 0); |
| 911 |
|
| 912 |
/* This might be NULL; the pending_diagnostic subclasses need to cope |
| 913 |
with this. */ |
| 914 |
tree leaked_tree = leaked_pv.m_tree; |
| 915 |
if (logger) |
| 916 |
{ |
| 917 |
if (leaked_tree) |
| 918 |
logger->log ("best leaked_tree: %qE", leaked_tree); |
| 919 |
else |
| 920 |
logger->log ("best leaked_tree: NULL"); |
| 921 |
} |
| 922 |
|
| 923 |
leak_stmt_finder stmt_finder (*m_eg, leaked_tree); |
| 924 |
gcc_assert (m_enode_for_diag); |
| 925 |
|
| 926 |
/* Don't complain about leaks when returning from "main". */ |
| 927 |
if (returning_from_function_p (m_enode_for_diag->get_supernode ())) |
| 928 |
{ |
| 929 |
tree fndecl = m_enode_for_diag->get_function ()->decl; |
| 930 |
if (id_equal (DECL_NAME (fndecl), "main")) |
| 931 |
{ |
| 932 |
if (logger) |
| 933 |
logger->log ("not reporting leak from main"); |
| 934 |
return; |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
tree leaked_tree_for_diag = fixup_tree_for_diagnostic (leaked_tree); |
| 939 |
std::unique_ptr<pending_diagnostic> pd = sm.on_leak (leaked_tree_for_diag); |
| 940 |
if (pd) |
| 941 |
{ |
| 942 |
pending_location ploc (m_enode_for_diag, |
| 943 |
m_enode_for_diag->get_supernode (), |
| 944 |
m_stmt, |
| 945 |
&stmt_finder); |
| 946 |
m_eg->get_diagnostic_manager ().add_diagnostic |
| 947 |
(&sm, ploc, |
| 948 |
leaked_tree_for_diag, sval, state, std::move (pd)); |
| 949 |
} |
| 950 |
} |
| 951 |
|
| 952 |
/* Implementation of region_model_context::on_condition vfunc. |
| 953 |
Notify all state machines about the condition, which could lead to |
| 954 |
state transitions. */ |
| 955 |
|
| 956 |
void |
| 957 |
impl_region_model_context::on_condition (const svalue *lhs, |
| 958 |
enum tree_code op, |
| 959 |
const svalue *rhs) |
| 960 |
{ |
| 961 |
int sm_idx; |
| 962 |
sm_state_map *smap; |
| 963 |
FOR_EACH_VEC_ELT (m_new_state->m_checker_states, sm_idx, smap) |
| 964 |
{ |
| 965 |
const state_machine &sm = m_ext_state.get_sm (sm_idx); |
| 966 |
impl_sm_context sm_ctxt (*m_eg, sm_idx, sm, m_enode_for_diag, |
| 967 |
m_old_state, m_new_state, |
| 968 |
m_old_state->m_checker_states[sm_idx], |
| 969 |
m_new_state->m_checker_states[sm_idx], |
| 970 |
m_path_ctxt); |
| 971 |
sm.on_condition (sm_ctxt, |
| 972 |
(m_enode_for_diag |
| 973 |
? m_enode_for_diag->get_supernode () |
| 974 |
: NULL), |
| 975 |
m_stmt, |
| 976 |
lhs, op, rhs); |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
/* Implementation of region_model_context::on_bounded_ranges vfunc. |
| 981 |
Notify all state machines about the ranges, which could lead to |
| 982 |
state transitions. */ |
| 983 |
|
| 984 |
void |
| 985 |
impl_region_model_context::on_bounded_ranges (const svalue &sval, |
| 986 |
const bounded_ranges &ranges) |
| 987 |
{ |
| 988 |
int sm_idx; |
| 989 |
sm_state_map *smap; |
| 990 |
FOR_EACH_VEC_ELT (m_new_state->m_checker_states, sm_idx, smap) |
| 991 |
{ |
| 992 |
const state_machine &sm = m_ext_state.get_sm (sm_idx); |
| 993 |
impl_sm_context sm_ctxt (*m_eg, sm_idx, sm, m_enode_for_diag, |
| 994 |
m_old_state, m_new_state, |
| 995 |
m_old_state->m_checker_states[sm_idx], |
| 996 |
m_new_state->m_checker_states[sm_idx], |
| 997 |
m_path_ctxt); |
| 998 |
sm.on_bounded_ranges (sm_ctxt, |
| 999 |
(m_enode_for_diag |
| 1000 |
? m_enode_for_diag->get_supernode () |
| 1001 |
: NULL), |
| 1002 |
m_stmt, sval, ranges); |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
/* Implementation of region_model_context::on_pop_frame vfunc. |
| 1007 |
Notify all state machines about the frame being popped, which |
| 1008 |
could lead to states being discarded. */ |
| 1009 |
|
| 1010 |
void |
| 1011 |
impl_region_model_context::on_pop_frame (const frame_region *frame_reg) |
| 1012 |
{ |
| 1013 |
int sm_idx; |
| 1014 |
sm_state_map *smap; |
| 1015 |
FOR_EACH_VEC_ELT (m_new_state->m_checker_states, sm_idx, smap) |
| 1016 |
{ |
| 1017 |
const state_machine &sm = m_ext_state.get_sm (sm_idx); |
| 1018 |
sm.on_pop_frame (smap, frame_reg); |
| 1019 |
} |
| 1020 |
} |
| 1021 |
|
| 1022 |
/* Implementation of region_model_context::on_phi vfunc. |
| 1023 |
Notify all state machines about the phi, which could lead to |
| 1024 |
state transitions. */ |
| 1025 |
|
| 1026 |
void |
| 1027 |
impl_region_model_context::on_phi (const gphi *phi, tree rhs) |
| 1028 |
{ |
| 1029 |
int sm_idx; |
| 1030 |
sm_state_map *smap; |
| 1031 |
FOR_EACH_VEC_ELT (m_new_state->m_checker_states, sm_idx, smap) |
| 1032 |
{ |
| 1033 |
const state_machine &sm = m_ext_state.get_sm (sm_idx); |
| 1034 |
impl_sm_context sm_ctxt (*m_eg, sm_idx, sm, m_enode_for_diag, |
| 1035 |
m_old_state, m_new_state, |
| 1036 |
m_old_state->m_checker_states[sm_idx], |
| 1037 |
m_new_state->m_checker_states[sm_idx], |
| 1038 |
m_path_ctxt); |
| 1039 |
sm.on_phi (sm_ctxt, m_enode_for_diag->get_supernode (), phi, rhs); |
| 1040 |
} |
| 1041 |
} |
| 1042 |
|
| 1043 |
/* Implementation of region_model_context::on_unexpected_tree_code vfunc. |
| 1044 |
Mark the new state as being invalid for further exploration. |
| 1045 |
TODO(stage1): introduce a warning for when this occurs. */ |
| 1046 |
|
| 1047 |
void |
| 1048 |
impl_region_model_context::on_unexpected_tree_code (tree t, |
| 1049 |
const dump_location_t &loc) |
| 1050 |
{ |
| 1051 |
logger * const logger = get_logger (); |
| 1052 |
if (logger) |
| 1053 |
logger->log ("unhandled tree code: %qs in %qs at %s:%i", |
| 1054 |
get_tree_code_name (TREE_CODE (t)), |
| 1055 |
loc.get_impl_location ().m_function, |
| 1056 |
loc.get_impl_location ().m_file, |
| 1057 |
loc.get_impl_location ().m_line); |
| 1058 |
if (m_new_state) |
| 1059 |
m_new_state->m_valid = false; |
| 1060 |
} |
| 1061 |
|
| 1062 |
/* Implementation of region_model_context::maybe_did_work vfunc. |
| 1063 |
Mark that "externally visible work" has occurred, and thus we |
| 1064 |
shouldn't report an infinite loop here. */ |
| 1065 |
|
| 1066 |
void |
| 1067 |
impl_region_model_context::maybe_did_work () |
| 1068 |
{ |
| 1069 |
if (m_out_could_have_done_work) |
| 1070 |
*m_out_could_have_done_work = true; |
| 1071 |
} |
| 1072 |
|
| 1073 |
/* struct point_and_state. */ |
| 1074 |
|
| 1075 |
/* Assert that this object is sane. */ |
| 1076 |
|
| 1077 |
void |
| 1078 |
point_and_state::validate (const extrinsic_state &ext_state) const |
| 1079 |
{ |
| 1080 |
/* Skip this in a release build. */ |
| 1081 |
#if !CHECKING_P |
| 1082 |
return; |
| 1083 |
#endif |
| 1084 |
|
| 1085 |
m_point.validate (); |
| 1086 |
|
| 1087 |
m_state.validate (ext_state); |
| 1088 |
|
| 1089 |
/* Verify that the callstring's model of the stack corresponds to that |
| 1090 |
of the region_model. */ |
| 1091 |
/* They should have the same depth. */ |
| 1092 |
gcc_assert (m_point.get_stack_depth () |
| 1093 |
== m_state.m_region_model->get_stack_depth ()); |
| 1094 |
/* Check the functions in the callstring vs those in the frames |
| 1095 |
at each depth. */ |
| 1096 |
for (const frame_region *iter_frame |
| 1097 |
= m_state.m_region_model->get_current_frame (); |
| 1098 |
iter_frame; iter_frame = iter_frame->get_calling_frame ()) |
| 1099 |
{ |
| 1100 |
int index = iter_frame->get_index (); |
| 1101 |
gcc_assert (m_point.get_function_at_depth (index) |
| 1102 |
== &iter_frame->get_function ()); |
| 1103 |
} |
| 1104 |
} |
| 1105 |
|
| 1106 |
/* Subroutine of print_enode_indices: print a run of indices from START_IDX |
| 1107 |
to END_IDX to PP, using and updating *FIRST_RUN. */ |
| 1108 |
|
| 1109 |
static void |
| 1110 |
print_run (pretty_printer *pp, int start_idx, int end_idx, |
| 1111 |
bool *first_run) |
| 1112 |
{ |
| 1113 |
if (!(*first_run)) |
| 1114 |
pp_string (pp, ", "); |
| 1115 |
*first_run = false; |
| 1116 |
if (start_idx == end_idx) |
| 1117 |
pp_printf (pp, "EN: %i", start_idx); |
| 1118 |
else |
| 1119 |
pp_printf (pp, "EN: %i-%i", start_idx, end_idx); |
| 1120 |
} |
| 1121 |
|
| 1122 |
/* Print the indices within ENODES to PP, collecting them as |
| 1123 |
runs/singletons e.g. "EN: 4-7, EN: 20-23, EN: 42". */ |
| 1124 |
|
| 1125 |
static void |
| 1126 |
print_enode_indices (pretty_printer *pp, |
| 1127 |
const auto_vec<exploded_node *> &enodes) |
| 1128 |
{ |
| 1129 |
int cur_start_idx = -1; |
| 1130 |
int cur_finish_idx = -1; |
| 1131 |
bool first_run = true; |
| 1132 |
unsigned i; |
| 1133 |
exploded_node *enode; |
| 1134 |
FOR_EACH_VEC_ELT (enodes, i, enode) |
| 1135 |
{ |
| 1136 |
if (cur_start_idx == -1) |
| 1137 |
{ |
| 1138 |
gcc_assert (cur_finish_idx == -1); |
| 1139 |
cur_start_idx = cur_finish_idx = enode->m_index; |
| 1140 |
} |
| 1141 |
else |
| 1142 |
{ |
| 1143 |
if (enode->m_index == cur_finish_idx + 1) |
| 1144 |
/* Continuation of a run. */ |
| 1145 |
cur_finish_idx = enode->m_index; |
| 1146 |
else |
| 1147 |
{ |
| 1148 |
/* Finish existing run, start a new one. */ |
| 1149 |
gcc_assert (cur_start_idx >= 0); |
| 1150 |
gcc_assert (cur_finish_idx >= 0); |
| 1151 |
print_run (pp, cur_start_idx, cur_finish_idx, |
| 1152 |
&first_run); |
| 1153 |
cur_start_idx = cur_finish_idx = enode->m_index; |
| 1154 |
} |
| 1155 |
} |
| 1156 |
} |
| 1157 |
/* Finish any existing run. */ |
| 1158 |
if (cur_start_idx >= 0) |
| 1159 |
{ |
| 1160 |
gcc_assert (cur_finish_idx >= 0); |
| 1161 |
print_run (pp, cur_start_idx, cur_finish_idx, |
| 1162 |
&first_run); |
| 1163 |
} |
| 1164 |
} |
| 1165 |
|
| 1166 |
/* struct eg_traits::dump_args_t. */ |
| 1167 |
|
| 1168 |
/* The <FILENAME>.eg.dot output can quickly become unwieldy if we show |
| 1169 |
full details for all enodes (both in terms of CPU time to render it, |
| 1170 |
and in terms of being meaningful to a human viewing it). |
| 1171 |
|
| 1172 |
If we show just the IDs then the resulting graph is usually viewable, |
| 1173 |
but then we have to keep switching back and forth between the .dot |
| 1174 |
view and other dumps. |
| 1175 |
|
| 1176 |
This function implements a heuristic for showing detail at the enodes |
| 1177 |
that (we hope) matter, and just the ID at other enodes, fixing the CPU |
| 1178 |
usage of the .dot viewer, and drawing the attention of the viewer |
| 1179 |
to these enodes. |
| 1180 |
|
| 1181 |
Return true if ENODE should be shown in detail in .dot output. |
| 1182 |
Return false if no detail should be shown for ENODE. */ |
| 1183 |
|
| 1184 |
bool |
| 1185 |
eg_traits::dump_args_t::show_enode_details_p (const exploded_node &enode) const |
| 1186 |
{ |
| 1187 |
/* If the number of exploded nodes isn't too large, we may as well show |
| 1188 |
all enodes in full detail in the .dot output. */ |
| 1189 |
if (m_eg.m_nodes.length () |
| 1190 |
<= (unsigned) param_analyzer_max_enodes_for_full_dump) |
| 1191 |
return true; |
| 1192 |
|
| 1193 |
/* Otherwise, assume that what's most interesting are state explosions, |
| 1194 |
and thus the places where this happened. |
| 1195 |
Expand enodes at program points where we hit the per-enode limit, so we |
| 1196 |
can investigate what exploded. */ |
| 1197 |
const per_program_point_data *per_point_data |
| 1198 |
= m_eg.get_per_program_point_data (enode.get_point ()); |
| 1199 |
return per_point_data->m_excess_enodes > 0; |
| 1200 |
} |
| 1201 |
|
| 1202 |
/* class exploded_node : public dnode<eg_traits>. */ |
| 1203 |
|
| 1204 |
const char * |
| 1205 |
exploded_node::status_to_str (enum status s) |
| 1206 |
{ |
| 1207 |
switch (s) |
| 1208 |
{ |
| 1209 |
default: gcc_unreachable (); |
| 1210 |
case STATUS_WORKLIST: return "WORKLIST"; |
| 1211 |
case STATUS_PROCESSED: return "PROCESSED"; |
| 1212 |
case STATUS_MERGER: return "MERGER"; |
| 1213 |
case STATUS_BULK_MERGED: return "BULK_MERGED"; |
| 1214 |
} |
| 1215 |
} |
| 1216 |
|
| 1217 |
/* exploded_node's ctor. */ |
| 1218 |
|
| 1219 |
exploded_node::exploded_node (const point_and_state &ps, |
| 1220 |
int index) |
| 1221 |
: m_ps (ps), m_status (STATUS_WORKLIST), m_index (index), |
| 1222 |
m_num_processed_stmts (0) |
| 1223 |
{ |
| 1224 |
gcc_checking_assert (ps.get_state ().m_region_model->canonicalized_p ()); |
| 1225 |
} |
| 1226 |
|
| 1227 |
/* Get the stmt that was processed in this enode at index IDX. |
| 1228 |
IDX is an index within the stmts processed at this enode, rather |
| 1229 |
than within those of the supernode. */ |
| 1230 |
|
| 1231 |
const gimple * |
| 1232 |
exploded_node::get_processed_stmt (unsigned idx) const |
| 1233 |
{ |
| 1234 |
gcc_assert (idx < m_num_processed_stmts); |
| 1235 |
const program_point &point = get_point (); |
| 1236 |
gcc_assert (point.get_kind () == PK_BEFORE_STMT); |
| 1237 |
const supernode *snode = get_supernode (); |
| 1238 |
const unsigned int point_stmt_idx = point.get_stmt_idx (); |
| 1239 |
const unsigned int idx_within_snode = point_stmt_idx + idx; |
| 1240 |
const gimple *stmt = snode->m_stmts[idx_within_snode]; |
| 1241 |
return stmt; |
| 1242 |
} |
| 1243 |
|
| 1244 |
/* For use by dump_dot, get a value for the .dot "fillcolor" attribute. |
| 1245 |
Colorize by sm-state, to make it easier to see how sm-state propagates |
| 1246 |
through the exploded_graph. */ |
| 1247 |
|
| 1248 |
const char * |
| 1249 |
exploded_node::get_dot_fillcolor () const |
| 1250 |
{ |
| 1251 |
const program_state &state = get_state (); |
| 1252 |
|
| 1253 |
/* We want to be able to easily distinguish the no-sm-state case, |
| 1254 |
and to be able to distinguish cases where there's a single state |
| 1255 |
from each other. |
| 1256 |
|
| 1257 |
Sum the sm_states, and use the result to choose from a table, |
| 1258 |
modulo table-size, special-casing the "no sm-state" case. */ |
| 1259 |
int total_sm_state = 0; |
| 1260 |
int i; |
| 1261 |
sm_state_map *smap; |
| 1262 |
FOR_EACH_VEC_ELT (state.m_checker_states, i, smap) |
| 1263 |
{ |
| 1264 |
for (sm_state_map::iterator_t iter = smap->begin (); |
| 1265 |
iter != smap->end (); |
| 1266 |
++iter) |
| 1267 |
total_sm_state += (*iter).second.m_state->get_id (); |
| 1268 |
total_sm_state += smap->get_global_state ()->get_id (); |
| 1269 |
} |
| 1270 |
|
| 1271 |
if (total_sm_state > 0) |
| 1272 |
{ |
| 1273 |
/* An arbitrarily-picked collection of light colors. */ |
| 1274 |
const char * const colors[] |
| 1275 |
= {"azure", "coral", "cornsilk", "lightblue", "yellow", |
| 1276 |
"honeydew", "lightpink", "lightsalmon", "palegreen1", |
| 1277 |
"wheat", "seashell"}; |
| 1278 |
const int num_colors = ARRAY_SIZE (colors); |
| 1279 |
return colors[total_sm_state % num_colors]; |
| 1280 |
} |
| 1281 |
else |
| 1282 |
/* No sm-state. */ |
| 1283 |
return "lightgrey"; |
| 1284 |
} |
| 1285 |
|
| 1286 |
/* Implementation of dnode::dump_dot vfunc for exploded_node. */ |
| 1287 |
|
| 1288 |
void |
| 1289 |
exploded_node::dump_dot (graphviz_out *gv, const dump_args_t &args) const |
| 1290 |
{ |
| 1291 |
pretty_printer *pp = gv->get_pp (); |
| 1292 |
|
| 1293 |
dump_dot_id (pp); |
| 1294 |
pp_printf (pp, " [shape=none,margin=0,style=filled,fillcolor=%s,label=\"", |
| 1295 |
get_dot_fillcolor ()); |
| 1296 |
pp_write_text_to_stream (pp); |
| 1297 |
|
| 1298 |
pp_printf (pp, "EN: %i", m_index); |
| 1299 |
if (m_status == STATUS_MERGER) |
| 1300 |
pp_string (pp, " (merger)"); |
| 1301 |
else if (m_status == STATUS_BULK_MERGED) |
| 1302 |
pp_string (pp, " (bulk merged)"); |
| 1303 |
pp_newline (pp); |
| 1304 |
|
| 1305 |
if (args.show_enode_details_p (*this)) |
| 1306 |
{ |
| 1307 |
format f (true); |
| 1308 |
m_ps.get_point ().print (pp, f); |
| 1309 |
pp_newline (pp); |
| 1310 |
|
| 1311 |
const extrinsic_state &ext_state = args.m_eg.get_ext_state (); |
| 1312 |
const program_state &state = m_ps.get_state (); |
| 1313 |
state.dump_to_pp (ext_state, false, true, pp); |
| 1314 |
pp_newline (pp); |
| 1315 |
|
| 1316 |
dump_processed_stmts (pp); |
| 1317 |
} |
| 1318 |
|
| 1319 |
dump_saved_diagnostics (pp); |
| 1320 |
|
| 1321 |
args.dump_extra_info (this, pp); |
| 1322 |
|
| 1323 |
pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/true); |
| 1324 |
|
| 1325 |
pp_string (pp, "\"];\n\n"); |
| 1326 |
|
| 1327 |
/* It can be hard to locate the saved diagnostics as text within the |
| 1328 |
enode nodes, so add extra nodes to the graph for each saved_diagnostic, |
| 1329 |
highlighted in red. |
| 1330 |
Compare with dump_saved_diagnostics. */ |
| 1331 |
{ |
| 1332 |
unsigned i; |
| 1333 |
const saved_diagnostic *sd; |
| 1334 |
FOR_EACH_VEC_ELT (m_saved_diagnostics, i, sd) |
| 1335 |
{ |
| 1336 |
sd->dump_as_dot_node (pp); |
| 1337 |
|
| 1338 |
/* Add edge connecting this enode to the saved_diagnostic. */ |
| 1339 |
dump_dot_id (pp); |
| 1340 |
pp_string (pp, " -> "); |
| 1341 |
sd->dump_dot_id (pp); |
| 1342 |
pp_string (pp, " [style=\"dotted\" arrowhead=\"none\"];"); |
| 1343 |
pp_newline (pp); |
| 1344 |
} |
| 1345 |
} |
| 1346 |
|
| 1347 |
pp_flush (pp); |
| 1348 |
} |
| 1349 |
|
| 1350 |
/* Show any stmts that were processed within this enode, |
| 1351 |
and their index within the supernode. */ |
| 1352 |
void |
| 1353 |
exploded_node::dump_processed_stmts (pretty_printer *pp) const |
| 1354 |
{ |
| 1355 |
if (m_num_processed_stmts > 0) |
| 1356 |
{ |
| 1357 |
const program_point &point = get_point (); |
| 1358 |
gcc_assert (point.get_kind () == PK_BEFORE_STMT); |
| 1359 |
const supernode *snode = get_supernode (); |
| 1360 |
const unsigned int point_stmt_idx = point.get_stmt_idx (); |
| 1361 |
|
| 1362 |
pp_printf (pp, "stmts: %i", m_num_processed_stmts); |
| 1363 |
pp_newline (pp); |
| 1364 |
for (unsigned i = 0; i < m_num_processed_stmts; i++) |
| 1365 |
{ |
| 1366 |
const unsigned int idx_within_snode = point_stmt_idx + i; |
| 1367 |
const gimple *stmt = snode->m_stmts[idx_within_snode]; |
| 1368 |
pp_printf (pp, " %i: ", idx_within_snode); |
| 1369 |
pp_gimple_stmt_1 (pp, stmt, 0, (dump_flags_t)0); |
| 1370 |
pp_newline (pp); |
| 1371 |
} |
| 1372 |
} |
| 1373 |
} |
| 1374 |
|
| 1375 |
/* Dump any saved_diagnostics at this enode to PP. */ |
| 1376 |
|
| 1377 |
void |
| 1378 |
exploded_node::dump_saved_diagnostics (pretty_printer *pp) const |
| 1379 |
{ |
| 1380 |
unsigned i; |
| 1381 |
const saved_diagnostic *sd; |
| 1382 |
FOR_EACH_VEC_ELT (m_saved_diagnostics, i, sd) |
| 1383 |
{ |
| 1384 |
pp_printf (pp, "DIAGNOSTIC: %s (sd: %i)", |
| 1385 |
sd->m_d->get_kind (), sd->get_index ()); |
| 1386 |
pp_newline (pp); |
| 1387 |
} |
| 1388 |
} |
| 1389 |
|
| 1390 |
/* Dump this to PP in a form suitable for use as an id in .dot output. */ |
| 1391 |
|
| 1392 |
void |
| 1393 |
exploded_node::dump_dot_id (pretty_printer *pp) const |
| 1394 |
{ |
| 1395 |
pp_printf (pp, "exploded_node_%i", m_index); |
| 1396 |
} |
| 1397 |
|
| 1398 |
/* Dump a multiline representation of this node to PP. */ |
| 1399 |
|
| 1400 |
void |
| 1401 |
exploded_node::dump_to_pp (pretty_printer *pp, |
| 1402 |
const extrinsic_state &ext_state) const |
| 1403 |
{ |
| 1404 |
pp_printf (pp, "EN: %i", m_index); |
| 1405 |
pp_newline (pp); |
| 1406 |
|
| 1407 |
format f (true); |
| 1408 |
m_ps.get_point ().print (pp, f); |
| 1409 |
pp_newline (pp); |
| 1410 |
|
| 1411 |
m_ps.get_state ().dump_to_pp (ext_state, false, true, pp); |
| 1412 |
pp_newline (pp); |
| 1413 |
} |
| 1414 |
|
| 1415 |
/* Dump a multiline representation of this node to FILE. */ |
| 1416 |
|
| 1417 |
void |
| 1418 |
exploded_node::dump (FILE *fp, |
| 1419 |
const extrinsic_state &ext_state) const |
| 1420 |
{ |
| 1421 |
tree_dump_pretty_printer pp (fp); |
| 1422 |
dump_to_pp (&pp, ext_state); |
| 1423 |
} |
| 1424 |
|
| 1425 |
/* Dump a multiline representation of this node to stderr. */ |
| 1426 |
|
| 1427 |
DEBUG_FUNCTION void |
| 1428 |
exploded_node::dump (const extrinsic_state &ext_state) const |
| 1429 |
{ |
| 1430 |
dump (stderr, ext_state); |
| 1431 |
} |
| 1432 |
|
| 1433 |
/* Return a new json::object of the form |
| 1434 |
{"point" : object for program_point, |
| 1435 |
"state" : object for program_state, |
| 1436 |
"status" : str, |
| 1437 |
"idx" : int, |
| 1438 |
"processed_stmts" : int}. */ |
| 1439 |
|
| 1440 |
std::unique_ptr<json::object> |
| 1441 |
exploded_node::to_json (const extrinsic_state &ext_state) const |
| 1442 |
{ |
| 1443 |
auto enode_obj = ::make_unique<json::object> (); |
| 1444 |
|
| 1445 |
enode_obj->set ("point", get_point ().to_json ()); |
| 1446 |
enode_obj->set ("state", get_state ().to_json (ext_state)); |
| 1447 |
enode_obj->set_string ("status", status_to_str (m_status)); |
| 1448 |
enode_obj->set_integer ("idx", m_index); |
| 1449 |
enode_obj->set_integer ("processed_stmts", m_num_processed_stmts); |
| 1450 |
|
| 1451 |
return enode_obj; |
| 1452 |
} |
| 1453 |
|
| 1454 |
} // namespace ana |
| 1455 |
|
| 1456 |
/* Return true if FNDECL has a gimple body. */ |
| 1457 |
// TODO: is there a pre-canned way to do this? |
| 1458 |
|
| 1459 |
bool |
| 1460 |
fndecl_has_gimple_body_p (tree fndecl) |
| 1461 |
{ |
| 1462 |
if (fndecl == NULL_TREE) |
| 1463 |
return false; |
| 1464 |
|
| 1465 |
cgraph_node *n = cgraph_node::get (fndecl); |
| 1466 |
if (!n) |
| 1467 |
return false; |
| 1468 |
|
| 1469 |
return n->has_gimple_body_p (); |
| 1470 |
} |
| 1471 |
|
| 1472 |
namespace ana { |
| 1473 |
|
| 1474 |
/* Modify STATE in place, applying the effects of the stmt at this node's |
| 1475 |
point. */ |
| 1476 |
|
| 1477 |
exploded_node::on_stmt_flags |
| 1478 |
exploded_node::on_stmt (exploded_graph &eg, |
| 1479 |
const supernode *snode, |
| 1480 |
const gimple *stmt, |
| 1481 |
program_state *state, |
| 1482 |
uncertainty_t *uncertainty, |
| 1483 |
bool *out_could_have_done_work, |
| 1484 |
path_context *path_ctxt) |
| 1485 |
{ |
| 1486 |
logger *logger = eg.get_logger (); |
| 1487 |
LOG_SCOPE (logger); |
| 1488 |
if (logger) |
| 1489 |
{ |
| 1490 |
logger->start_log_line (); |
| 1491 |
pp_gimple_stmt_1 (logger->get_printer (), stmt, 0, (dump_flags_t)0); |
| 1492 |
logger->end_log_line (); |
| 1493 |
} |
| 1494 |
|
| 1495 |
/* Update input_location in case of ICE: make it easier to track down which |
| 1496 |
source construct we're failing to handle. */ |
| 1497 |
input_location = stmt->location; |
| 1498 |
|
| 1499 |
gcc_assert (state->m_region_model); |
| 1500 |
|
| 1501 |
/* Preserve the old state. It is used here for looking |
| 1502 |
up old checker states, for determining state transitions, and |
| 1503 |
also within impl_region_model_context and impl_sm_context for |
| 1504 |
going from tree to svalue_id. */ |
| 1505 |
const program_state old_state (*state); |
| 1506 |
|
| 1507 |
impl_region_model_context ctxt (eg, this, |
| 1508 |
&old_state, state, uncertainty, |
| 1509 |
path_ctxt, stmt, nullptr, |
| 1510 |
out_could_have_done_work); |
| 1511 |
|
| 1512 |
/* Handle call summaries here. */ |
| 1513 |
if (cgraph_edge *cgedge |
| 1514 |
= supergraph_call_edge (snode->get_function (), stmt)) |
| 1515 |
if (eg.get_analysis_plan ().use_summary_p (cgedge)) |
| 1516 |
{ |
| 1517 |
function *called_fn = get_ultimate_function_for_cgraph_edge (cgedge); |
| 1518 |
per_function_data *called_fn_data |
| 1519 |
= eg.get_per_function_data (called_fn); |
| 1520 |
if (called_fn_data) |
| 1521 |
{ |
| 1522 |
gcc_assert (called_fn); |
| 1523 |
return replay_call_summaries (eg, |
| 1524 |
snode, |
| 1525 |
as_a <const gcall *> (stmt), |
| 1526 |
state, |
| 1527 |
path_ctxt, |
| 1528 |
*called_fn, |
| 1529 |
*called_fn_data, |
| 1530 |
&ctxt); |
| 1531 |
} |
| 1532 |
} |
| 1533 |
|
| 1534 |
bool unknown_side_effects = false; |
| 1535 |
bool terminate_path = false; |
| 1536 |
|
| 1537 |
on_stmt_pre (eg, stmt, state, &terminate_path, |
| 1538 |
&unknown_side_effects, &ctxt); |
| 1539 |
|
| 1540 |
if (terminate_path) |
| 1541 |
return on_stmt_flags::terminate_path (); |
| 1542 |
|
| 1543 |
int sm_idx; |
| 1544 |
sm_state_map *smap; |
| 1545 |
FOR_EACH_VEC_ELT (old_state.m_checker_states, sm_idx, smap) |
| 1546 |
{ |
| 1547 |
const state_machine &sm = eg.get_ext_state ().get_sm (sm_idx); |
| 1548 |
const sm_state_map *old_smap |
| 1549 |
= old_state.m_checker_states[sm_idx]; |
| 1550 |
sm_state_map *new_smap = state->m_checker_states[sm_idx]; |
| 1551 |
impl_sm_context sm_ctxt (eg, sm_idx, sm, this, &old_state, state, |
| 1552 |
old_smap, new_smap, path_ctxt, NULL, |
| 1553 |
unknown_side_effects); |
| 1554 |
|
| 1555 |
/* Allow the state_machine to handle the stmt. */ |
| 1556 |
if (sm.on_stmt (sm_ctxt, snode, stmt)) |
| 1557 |
unknown_side_effects = false; |
| 1558 |
} |
| 1559 |
|
| 1560 |
if (path_ctxt->terminate_path_p ()) |
| 1561 |
return on_stmt_flags::terminate_path (); |
| 1562 |
|
| 1563 |
on_stmt_post (stmt, state, unknown_side_effects, &ctxt); |
| 1564 |
|
| 1565 |
return on_stmt_flags (); |
| 1566 |
} |
| 1567 |
|
| 1568 |
/* Handle the pre-sm-state part of STMT, modifying STATE in-place. |
| 1569 |
Write true to *OUT_TERMINATE_PATH if the path should be terminated. |
| 1570 |
Write true to *OUT_UNKNOWN_SIDE_EFFECTS if the stmt has unknown |
| 1571 |
side effects. */ |
| 1572 |
|
| 1573 |
void |
| 1574 |
exploded_node::on_stmt_pre (exploded_graph &eg, |
| 1575 |
const gimple *stmt, |
| 1576 |
program_state *state, |
| 1577 |
bool *out_terminate_path, |
| 1578 |
bool *out_unknown_side_effects, |
| 1579 |
region_model_context *ctxt) |
| 1580 |
{ |
| 1581 |
/* Handle special-case calls that require the full program_state. */ |
| 1582 |
if (const gcall *call = dyn_cast <const gcall *> (stmt)) |
| 1583 |
{ |
| 1584 |
if (is_special_named_call_p (call, "__analyzer_dump", 0)) |
| 1585 |
{ |
| 1586 |
/* Handle the builtin "__analyzer_dump" by dumping state |
| 1587 |
to stderr. */ |
| 1588 |
state->dump (eg.get_ext_state (), true); |
| 1589 |
return; |
| 1590 |
} |
| 1591 |
else if (is_special_named_call_p (call, "__analyzer_dump_state", 2)) |
| 1592 |
{ |
| 1593 |
state->impl_call_analyzer_dump_state (call, eg.get_ext_state (), |
| 1594 |
ctxt); |
| 1595 |
return; |
| 1596 |
} |
| 1597 |
else if (is_setjmp_call_p (call)) |
| 1598 |
{ |
| 1599 |
state->m_region_model->on_setjmp (call, this, ctxt); |
| 1600 |
if (ctxt) |
| 1601 |
ctxt->maybe_did_work (); |
| 1602 |
return; |
| 1603 |
} |
| 1604 |
else if (is_longjmp_call_p (call)) |
| 1605 |
{ |
| 1606 |
on_longjmp (eg, call, state, ctxt); |
| 1607 |
*out_terminate_path = true; |
| 1608 |
if (ctxt) |
| 1609 |
ctxt->maybe_did_work (); |
| 1610 |
return; |
| 1611 |
} |
| 1612 |
} |
| 1613 |
|
| 1614 |
/* Otherwise, defer to m_region_model. */ |
| 1615 |
state->m_region_model->on_stmt_pre (stmt, |
| 1616 |
out_unknown_side_effects, |
| 1617 |
ctxt); |
| 1618 |
} |
| 1619 |
|
| 1620 |
/* Handle the post-sm-state part of STMT, modifying STATE in-place. */ |
| 1621 |
|
| 1622 |
void |
| 1623 |
exploded_node::on_stmt_post (const gimple *stmt, |
| 1624 |
program_state *state, |
| 1625 |
bool unknown_side_effects, |
| 1626 |
region_model_context *ctxt) |
| 1627 |
{ |
| 1628 |
if (const gcall *call = dyn_cast <const gcall *> (stmt)) |
| 1629 |
state->m_region_model->on_call_post (call, unknown_side_effects, ctxt); |
| 1630 |
} |
| 1631 |
|
| 1632 |
/* A concrete call_info subclass representing a replay of a call summary. */ |
| 1633 |
|
| 1634 |
class call_summary_edge_info : public call_info |
| 1635 |
{ |
| 1636 |
public: |
| 1637 |
call_summary_edge_info (const call_details &cd, |
| 1638 |
const function &called_fn, |
| 1639 |
call_summary *summary, |
| 1640 |
const extrinsic_state &ext_state) |
| 1641 |
: call_info (cd, called_fn), |
| 1642 |
m_called_fn (called_fn), |
| 1643 |
m_summary (summary), |
| 1644 |
m_ext_state (ext_state) |
| 1645 |
{} |
| 1646 |
|
| 1647 |
bool update_state (program_state *state, |
| 1648 |
const exploded_edge *, |
| 1649 |
region_model_context *ctxt) const final override |
| 1650 |
{ |
| 1651 |
/* Update STATE based on summary_end_state. */ |
| 1652 |
call_details cd (get_call_details (state->m_region_model, ctxt)); |
| 1653 |
call_summary_replay r (cd, m_called_fn, m_summary, m_ext_state); |
| 1654 |
const program_state &summary_end_state = m_summary->get_state (); |
| 1655 |
return state->replay_call_summary (r, summary_end_state); |
| 1656 |
} |
| 1657 |
|
| 1658 |
bool update_model (region_model *model, |
| 1659 |
const exploded_edge *, |
| 1660 |
region_model_context *ctxt) const final override |
| 1661 |
{ |
| 1662 |
/* Update STATE based on summary_end_state. */ |
| 1663 |
call_details cd (get_call_details (model, ctxt)); |
| 1664 |
call_summary_replay r (cd, m_called_fn, m_summary, m_ext_state); |
| 1665 |
const program_state &summary_end_state = m_summary->get_state (); |
| 1666 |
model->replay_call_summary (r, *summary_end_state.m_region_model); |
| 1667 |
return true; |
| 1668 |
} |
| 1669 |
|
| 1670 |
void print_desc (pretty_printer &pp) const final override |
| 1671 |
{ |
| 1672 |
pp_string (&pp, m_summary->get_desc ().get ()); |
| 1673 |
} |
| 1674 |
|
| 1675 |
private: |
| 1676 |
const function &m_called_fn; |
| 1677 |
call_summary *m_summary; |
| 1678 |
const extrinsic_state &m_ext_state; |
| 1679 |
}; |
| 1680 |
|
| 1681 |
/* Use PATH_CTXT to bifurcate, which when handled will add custom edges |
| 1682 |
for a replay of the various feasible summaries in CALLED_FN_DATA. */ |
| 1683 |
|
| 1684 |
exploded_node::on_stmt_flags |
| 1685 |
exploded_node::replay_call_summaries (exploded_graph &eg, |
| 1686 |
const supernode *snode, |
| 1687 |
const gcall *call_stmt, |
| 1688 |
program_state *state, |
| 1689 |
path_context *path_ctxt, |
| 1690 |
const function &called_fn, |
| 1691 |
per_function_data &called_fn_data, |
| 1692 |
region_model_context *ctxt) |
| 1693 |
{ |
| 1694 |
logger *logger = eg.get_logger (); |
| 1695 |
LOG_SCOPE (logger); |
| 1696 |
|
| 1697 |
/* Each summary will call bifurcate on the PATH_CTXT. */ |
| 1698 |
for (auto summary : called_fn_data.m_summaries) |
| 1699 |
replay_call_summary (eg, snode, call_stmt, state, |
| 1700 |
path_ctxt, called_fn, summary, ctxt); |
| 1701 |
path_ctxt->terminate_path (); |
| 1702 |
|
| 1703 |
return on_stmt_flags (); |
| 1704 |
} |
| 1705 |
|
| 1706 |
/* Use PATH_CTXT to bifurcate, which when handled will add a |
| 1707 |
custom edge for a replay of SUMMARY, if the summary's |
| 1708 |
conditions are feasible based on the current state. */ |
| 1709 |
|
| 1710 |
void |
| 1711 |
exploded_node::replay_call_summary (exploded_graph &eg, |
| 1712 |
const supernode *snode, |
| 1713 |
const gcall *call_stmt, |
| 1714 |
program_state *old_state, |
| 1715 |
path_context *path_ctxt, |
| 1716 |
const function &called_fn, |
| 1717 |
call_summary *summary, |
| 1718 |
region_model_context *ctxt) |
| 1719 |
{ |
| 1720 |
logger *logger = eg.get_logger (); |
| 1721 |
LOG_SCOPE (logger); |
| 1722 |
gcc_assert (snode); |
| 1723 |
gcc_assert (call_stmt); |
| 1724 |
gcc_assert (old_state); |
| 1725 |
gcc_assert (summary); |
| 1726 |
|
| 1727 |
if (logger) |
| 1728 |
logger->log ("using %s as summary for call to %qE from %qE", |
| 1729 |
summary->get_desc ().get (), |
| 1730 |
called_fn.decl, |
| 1731 |
snode->get_function ()->decl); |
| 1732 |
const extrinsic_state &ext_state = eg.get_ext_state (); |
| 1733 |
const program_state &summary_end_state = summary->get_state (); |
| 1734 |
if (logger) |
| 1735 |
{ |
| 1736 |
pretty_printer *pp = logger->get_printer (); |
| 1737 |
|
| 1738 |
logger->start_log_line (); |
| 1739 |
pp_string (pp, "callsite state: "); |
| 1740 |
old_state->dump_to_pp (ext_state, true, false, pp); |
| 1741 |
logger->end_log_line (); |
| 1742 |
|
| 1743 |
logger->start_log_line (); |
| 1744 |
pp_string (pp, "summary end state: "); |
| 1745 |
summary_end_state.dump_to_pp (ext_state, true, false, pp); |
| 1746 |
logger->end_log_line (); |
| 1747 |
} |
| 1748 |
|
| 1749 |
program_state new_state (*old_state); |
| 1750 |
|
| 1751 |
call_details cd (call_stmt, new_state.m_region_model, ctxt); |
| 1752 |
call_summary_replay r (cd, called_fn, summary, ext_state); |
| 1753 |
|
| 1754 |
if (path_ctxt) |
| 1755 |
path_ctxt->bifurcate (make_unique<call_summary_edge_info> (cd, |
| 1756 |
called_fn, |
| 1757 |
summary, |
| 1758 |
ext_state)); |
| 1759 |
} |
| 1760 |
|
| 1761 |
|
| 1762 |
/* Consider the effect of following superedge SUCC from this node. |
| 1763 |
|
| 1764 |
Return true if it's feasible to follow the edge, or false |
| 1765 |
if it's infeasible. |
| 1766 |
|
| 1767 |
Examples: if it's the "true" branch within |
| 1768 |
a CFG and we know the conditional is false, we know it's infeasible. |
| 1769 |
If it's one of multiple interprocedual "return" edges, then only |
| 1770 |
the edge back to the most recent callsite is feasible. |
| 1771 |
|
| 1772 |
Update NEXT_STATE accordingly (e.g. to record that a condition was |
| 1773 |
true or false, or that the NULL-ness of a pointer has been checked, |
| 1774 |
pushing/popping stack frames, etc). |
| 1775 |
|
| 1776 |
Update NEXT_POINT accordingly (updating the call string). */ |
| 1777 |
|
| 1778 |
bool |
| 1779 |
exploded_node::on_edge (exploded_graph &eg, |
| 1780 |
const superedge *succ, |
| 1781 |
program_point *next_point, |
| 1782 |
program_state *next_state, |
| 1783 |
uncertainty_t *uncertainty) |
| 1784 |
{ |
| 1785 |
LOG_FUNC (eg.get_logger ()); |
| 1786 |
|
| 1787 |
if (!next_point->on_edge (eg, succ)) |
| 1788 |
return false; |
| 1789 |
|
| 1790 |
if (!next_state->on_edge (eg, this, succ, uncertainty)) |
| 1791 |
return false; |
| 1792 |
|
| 1793 |
return true; |
| 1794 |
} |
| 1795 |
|
| 1796 |
/* Verify that the stack at LONGJMP_POINT is still valid, given a call |
| 1797 |
to "setjmp" at SETJMP_POINT - the stack frame that "setjmp" was |
| 1798 |
called in must still be valid. |
| 1799 |
|
| 1800 |
Caveat: this merely checks the call_strings in the points; it doesn't |
| 1801 |
detect the case where a frame returns and is then called again. */ |
| 1802 |
|
| 1803 |
static bool |
| 1804 |
valid_longjmp_stack_p (const program_point &longjmp_point, |
| 1805 |
const program_point &setjmp_point) |
| 1806 |
{ |
| 1807 |
const call_string &cs_at_longjmp = longjmp_point.get_call_string (); |
| 1808 |
const call_string &cs_at_setjmp = setjmp_point.get_call_string (); |
| 1809 |
|
| 1810 |
if (cs_at_longjmp.length () < cs_at_setjmp.length ()) |
| 1811 |
return false; |
| 1812 |
|
| 1813 |
/* Check that the call strings match, up to the depth of the |
| 1814 |
setjmp point. */ |
| 1815 |
for (unsigned depth = 0; depth < cs_at_setjmp.length (); depth++) |
| 1816 |
if (cs_at_longjmp[depth] != cs_at_setjmp[depth]) |
| 1817 |
return false; |
| 1818 |
|
| 1819 |
return true; |
| 1820 |
} |
| 1821 |
|
| 1822 |
/* A pending_diagnostic subclass for complaining about bad longjmps, |
| 1823 |
where the enclosing function of the "setjmp" has returned (and thus |
| 1824 |
the stack frame no longer exists). */ |
| 1825 |
|
| 1826 |
class stale_jmp_buf : public pending_diagnostic_subclass<stale_jmp_buf> |
| 1827 |
{ |
| 1828 |
public: |
| 1829 |
stale_jmp_buf (const gcall *setjmp_call, const gcall *longjmp_call, |
| 1830 |
const program_point &setjmp_point) |
| 1831 |
: m_setjmp_call (setjmp_call), m_longjmp_call (longjmp_call), |
| 1832 |
m_setjmp_point (setjmp_point), m_stack_pop_event (NULL) |
| 1833 |
{} |
| 1834 |
|
| 1835 |
int get_controlling_option () const final override |
| 1836 |
{ |
| 1837 |
return OPT_Wanalyzer_stale_setjmp_buffer; |
| 1838 |
} |
| 1839 |
|
| 1840 |
bool emit (diagnostic_emission_context &ctxt) final override |
| 1841 |
{ |
| 1842 |
return ctxt.warn ("%qs called after enclosing function of %qs has returned", |
| 1843 |
get_user_facing_name (m_longjmp_call), |
| 1844 |
get_user_facing_name (m_setjmp_call)); |
| 1845 |
} |
| 1846 |
|
| 1847 |
const char *get_kind () const final override |
| 1848 |
{ return "stale_jmp_buf"; } |
| 1849 |
|
| 1850 |
bool operator== (const stale_jmp_buf &other) const |
| 1851 |
{ |
| 1852 |
return (m_setjmp_call == other.m_setjmp_call |
| 1853 |
&& m_longjmp_call == other.m_longjmp_call); |
| 1854 |
} |
| 1855 |
|
| 1856 |
bool |
| 1857 |
maybe_add_custom_events_for_superedge (const exploded_edge &eedge, |
| 1858 |
checker_path *emission_path) |
| 1859 |
final override |
| 1860 |
{ |
| 1861 |
/* Detect exactly when the stack first becomes invalid, |
| 1862 |
and issue an event then. */ |
| 1863 |
if (m_stack_pop_event) |
| 1864 |
return false; |
| 1865 |
const exploded_node *src_node = eedge.m_src; |
| 1866 |
const program_point &src_point = src_node->get_point (); |
| 1867 |
const exploded_node *dst_node = eedge.m_dest; |
| 1868 |
const program_point &dst_point = dst_node->get_point (); |
| 1869 |
if (valid_longjmp_stack_p (src_point, m_setjmp_point) |
| 1870 |
&& !valid_longjmp_stack_p (dst_point, m_setjmp_point)) |
| 1871 |
{ |
| 1872 |
/* Compare with diagnostic_manager::add_events_for_superedge. */ |
| 1873 |
const int src_stack_depth = src_point.get_stack_depth (); |
| 1874 |
m_stack_pop_event = new precanned_custom_event |
| 1875 |
(event_loc_info (src_point.get_location (), |
| 1876 |
src_point.get_fndecl (), |
| 1877 |
src_stack_depth), |
| 1878 |
"stack frame is popped here, invalidating saved environment"); |
| 1879 |
emission_path->add_event |
| 1880 |
(std::unique_ptr<custom_event> (m_stack_pop_event)); |
| 1881 |
return false; |
| 1882 |
} |
| 1883 |
return false; |
| 1884 |
} |
| 1885 |
|
| 1886 |
bool |
| 1887 |
describe_final_event (pretty_printer &pp, |
| 1888 |
const evdesc::final_event &) final override |
| 1889 |
{ |
| 1890 |
if (m_stack_pop_event) |
| 1891 |
pp_printf (&pp, |
| 1892 |
"%qs called after enclosing function of %qs returned at %@", |
| 1893 |
get_user_facing_name (m_longjmp_call), |
| 1894 |
get_user_facing_name (m_setjmp_call), |
| 1895 |
m_stack_pop_event->get_id_ptr ()); |
| 1896 |
else |
| 1897 |
pp_printf (&pp, |
| 1898 |
"%qs called after enclosing function of %qs has returned", |
| 1899 |
get_user_facing_name (m_longjmp_call), |
| 1900 |
get_user_facing_name (m_setjmp_call)); |
| 1901 |
return true; |
| 1902 |
} |
| 1903 |
|
| 1904 |
|
| 1905 |
private: |
| 1906 |
const gcall *m_setjmp_call; |
| 1907 |
const gcall *m_longjmp_call; |
| 1908 |
program_point m_setjmp_point; |
| 1909 |
custom_event *m_stack_pop_event; |
| 1910 |
}; |
| 1911 |
|
| 1912 |
/* Handle LONGJMP_CALL, a call to longjmp or siglongjmp. |
| 1913 |
|
| 1914 |
Attempt to locate where setjmp/sigsetjmp was called on the jmp_buf and build |
| 1915 |
an exploded_node and exploded_edge to it representing a rewind to that frame, |
| 1916 |
handling the various kinds of failure that can occur. */ |
| 1917 |
|
| 1918 |
void |
| 1919 |
exploded_node::on_longjmp (exploded_graph &eg, |
| 1920 |
const gcall *longjmp_call, |
| 1921 |
program_state *new_state, |
| 1922 |
region_model_context *ctxt) |
| 1923 |
{ |
| 1924 |
tree buf_ptr = gimple_call_arg (longjmp_call, 0); |
| 1925 |
gcc_assert (POINTER_TYPE_P (TREE_TYPE (buf_ptr))); |
| 1926 |
|
| 1927 |
region_model *new_region_model = new_state->m_region_model; |
| 1928 |
const svalue *buf_ptr_sval = new_region_model->get_rvalue (buf_ptr, ctxt); |
| 1929 |
const region *buf = new_region_model->deref_rvalue (buf_ptr_sval, buf_ptr, |
| 1930 |
ctxt); |
| 1931 |
|
| 1932 |
const svalue *buf_content_sval |
| 1933 |
= new_region_model->get_store_value (buf, ctxt); |
| 1934 |
const setjmp_svalue *setjmp_sval |
| 1935 |
= buf_content_sval->dyn_cast_setjmp_svalue (); |
| 1936 |
if (!setjmp_sval) |
| 1937 |
return; |
| 1938 |
|
| 1939 |
const setjmp_record tmp_setjmp_record = setjmp_sval->get_setjmp_record (); |
| 1940 |
|
| 1941 |
/* Build a custom enode and eedge for rewinding from the longjmp/siglongjmp |
| 1942 |
call back to the setjmp/sigsetjmp. */ |
| 1943 |
rewind_info_t rewind_info (tmp_setjmp_record, longjmp_call); |
| 1944 |
|
| 1945 |
const gcall *setjmp_call = rewind_info.get_setjmp_call (); |
| 1946 |
const program_point &setjmp_point = rewind_info.get_setjmp_point (); |
| 1947 |
|
| 1948 |
const program_point &longjmp_point = get_point (); |
| 1949 |
|
| 1950 |
/* Verify that the setjmp's call_stack hasn't been popped. */ |
| 1951 |
if (!valid_longjmp_stack_p (longjmp_point, setjmp_point)) |
| 1952 |
{ |
| 1953 |
ctxt->warn (make_unique<stale_jmp_buf> (setjmp_call, |
| 1954 |
longjmp_call, |
| 1955 |
setjmp_point)); |
| 1956 |
return; |
| 1957 |
} |
| 1958 |
|
| 1959 |
gcc_assert (longjmp_point.get_stack_depth () |
| 1960 |
>= setjmp_point.get_stack_depth ()); |
| 1961 |
|
| 1962 |
/* Update the state for use by the destination node. */ |
| 1963 |
|
| 1964 |
/* Stash the current number of diagnostics so that we can update |
| 1965 |
any that this adds to show where the longjmp is rewinding to. */ |
| 1966 |
|
| 1967 |
diagnostic_manager *dm = &eg.get_diagnostic_manager (); |
| 1968 |
unsigned prev_num_diagnostics = dm->get_num_diagnostics (); |
| 1969 |
|
| 1970 |
new_region_model->on_longjmp (longjmp_call, setjmp_call, |
| 1971 |
setjmp_point.get_stack_depth (), ctxt); |
| 1972 |
|
| 1973 |
/* Detect leaks in the new state relative to the old state. */ |
| 1974 |
program_state::detect_leaks (get_state (), *new_state, NULL, |
| 1975 |
eg.get_ext_state (), ctxt); |
| 1976 |
|
| 1977 |
program_point next_point |
| 1978 |
= program_point::after_supernode (setjmp_point.get_supernode (), |
| 1979 |
setjmp_point.get_call_string ()); |
| 1980 |
|
| 1981 |
exploded_node *next |
| 1982 |
= eg.get_or_create_node (next_point, *new_state, this); |
| 1983 |
|
| 1984 |
/* Create custom exploded_edge for a longjmp. */ |
| 1985 |
if (next) |
| 1986 |
{ |
| 1987 |
exploded_edge *eedge |
| 1988 |
= eg.add_edge (const_cast<exploded_node *> (this), next, NULL, true, |
| 1989 |
make_unique<rewind_info_t> (tmp_setjmp_record, |
| 1990 |
longjmp_call)); |
| 1991 |
|
| 1992 |
/* For any diagnostics that were queued here (such as leaks) we want |
| 1993 |
the checker_path to show the rewinding events after the "final event" |
| 1994 |
so that the user sees where the longjmp is rewinding to (otherwise the |
| 1995 |
path is meaningless). |
| 1996 |
|
| 1997 |
For example, we want to emit something like: |
| 1998 |
| NN | { |
| 1999 |
| NN | longjmp (env, 1); |
| 2000 |
| | ~~~~~~~~~~~~~~~~ |
| 2001 |
| | | |
| 2002 |
| | (10) 'ptr' leaks here; was allocated at (7) |
| 2003 |
| | (11) rewinding from 'longjmp' in 'inner'... |
| 2004 |
| |
| 2005 |
<-------------+ |
| 2006 |
| |
| 2007 |
'outer': event 12 |
| 2008 |
| |
| 2009 |
| NN | i = setjmp(env); |
| 2010 |
| | ^~~~~~ |
| 2011 |
| | | |
| 2012 |
| | (12) ...to 'setjmp' in 'outer' (saved at (2)) |
| 2013 |
|
| 2014 |
where the "final" event above is event (10), but we want to append |
| 2015 |
events (11) and (12) afterwards. |
| 2016 |
|
| 2017 |
Do this by setting m_trailing_eedge on any diagnostics that were |
| 2018 |
just saved. */ |
| 2019 |
unsigned num_diagnostics = dm->get_num_diagnostics (); |
| 2020 |
for (unsigned i = prev_num_diagnostics; i < num_diagnostics; i++) |
| 2021 |
{ |
| 2022 |
saved_diagnostic *sd = dm->get_saved_diagnostic (i); |
| 2023 |
sd->m_trailing_eedge = eedge; |
| 2024 |
} |
| 2025 |
} |
| 2026 |
} |
| 2027 |
|
| 2028 |
/* Subroutine of exploded_graph::process_node for finding the successors |
| 2029 |
of the supernode for a function exit basic block. |
| 2030 |
|
| 2031 |
Ensure that pop_frame is called, potentially queuing diagnostics about |
| 2032 |
leaks. */ |
| 2033 |
|
| 2034 |
void |
| 2035 |
exploded_node::detect_leaks (exploded_graph &eg) |
| 2036 |
{ |
| 2037 |
LOG_FUNC_1 (eg.get_logger (), "EN: %i", m_index); |
| 2038 |
|
| 2039 |
gcc_assert (get_point ().get_supernode ()->return_p ()); |
| 2040 |
|
| 2041 |
/* If we're not a "top-level" function, do nothing; pop_frame |
| 2042 |
will be called when handling the return superedge. */ |
| 2043 |
if (get_point ().get_stack_depth () > 1) |
| 2044 |
return; |
| 2045 |
|
| 2046 |
/* We have a "top-level" function. */ |
| 2047 |
gcc_assert (get_point ().get_stack_depth () == 1); |
| 2048 |
|
| 2049 |
const program_state &old_state = get_state (); |
| 2050 |
|
| 2051 |
/* Work with a temporary copy of the state: pop the frame, and see |
| 2052 |
what leaks (via purge_unused_svalues). */ |
| 2053 |
program_state new_state (old_state); |
| 2054 |
|
| 2055 |
gcc_assert (new_state.m_region_model); |
| 2056 |
|
| 2057 |
uncertainty_t uncertainty; |
| 2058 |
impl_region_model_context ctxt (eg, this, |
| 2059 |
&old_state, &new_state, &uncertainty, NULL, |
| 2060 |
get_stmt ()); |
| 2061 |
const svalue *result = NULL; |
| 2062 |
new_state.m_region_model->pop_frame (NULL, &result, &ctxt, nullptr); |
| 2063 |
program_state::detect_leaks (old_state, new_state, result, |
| 2064 |
eg.get_ext_state (), &ctxt); |
| 2065 |
} |
| 2066 |
|
| 2067 |
/* Dump the successors and predecessors of this enode to OUTF. */ |
| 2068 |
|
| 2069 |
void |
| 2070 |
exploded_node::dump_succs_and_preds (FILE *outf) const |
| 2071 |
{ |
| 2072 |
unsigned i; |
| 2073 |
exploded_edge *e; |
| 2074 |
{ |
| 2075 |
auto_vec<exploded_node *> preds (m_preds.length ()); |
| 2076 |
FOR_EACH_VEC_ELT (m_preds, i, e) |
| 2077 |
preds.quick_push (e->m_src); |
| 2078 |
pretty_printer pp; |
| 2079 |
print_enode_indices (&pp, preds); |
| 2080 |
fprintf (outf, "preds: %s\n", |
| 2081 |
pp_formatted_text (&pp)); |
| 2082 |
} |
| 2083 |
{ |
| 2084 |
auto_vec<exploded_node *> succs (m_succs.length ()); |
| 2085 |
FOR_EACH_VEC_ELT (m_succs, i, e) |
| 2086 |
succs.quick_push (e->m_dest); |
| 2087 |
pretty_printer pp; |
| 2088 |
print_enode_indices (&pp, succs); |
| 2089 |
fprintf (outf, "succs: %s\n", |
| 2090 |
pp_formatted_text (&pp)); |
| 2091 |
} |
| 2092 |
} |
| 2093 |
|
| 2094 |
/* class dynamic_call_info_t : public custom_edge_info. */ |
| 2095 |
|
| 2096 |
/* Implementation of custom_edge_info::update_model vfunc |
| 2097 |
for dynamic_call_info_t. |
| 2098 |
|
| 2099 |
Update state for a dynamically discovered call (or return), by pushing |
| 2100 |
or popping the a frame for the appropriate function. */ |
| 2101 |
|
| 2102 |
bool |
| 2103 |
dynamic_call_info_t::update_model (region_model *model, |
| 2104 |
const exploded_edge *eedge, |
| 2105 |
region_model_context *ctxt) const |
| 2106 |
{ |
| 2107 |
gcc_assert (eedge); |
| 2108 |
if (m_is_returning_call) |
| 2109 |
model->update_for_return_gcall (m_dynamic_call, ctxt); |
| 2110 |
else |
| 2111 |
{ |
| 2112 |
function *callee = eedge->m_dest->get_function (); |
| 2113 |
model->update_for_gcall (m_dynamic_call, ctxt, callee); |
| 2114 |
} |
| 2115 |
return true; |
| 2116 |
} |
| 2117 |
|
| 2118 |
/* Implementation of custom_edge_info::add_events_to_path vfunc |
| 2119 |
for dynamic_call_info_t. */ |
| 2120 |
|
| 2121 |
void |
| 2122 |
dynamic_call_info_t::add_events_to_path (checker_path *emission_path, |
| 2123 |
const exploded_edge &eedge) const |
| 2124 |
{ |
| 2125 |
const exploded_node *src_node = eedge.m_src; |
| 2126 |
const program_point &src_point = src_node->get_point (); |
| 2127 |
const int src_stack_depth = src_point.get_stack_depth (); |
| 2128 |
const exploded_node *dest_node = eedge.m_dest; |
| 2129 |
const program_point &dest_point = dest_node->get_point (); |
| 2130 |
const int dest_stack_depth = dest_point.get_stack_depth (); |
| 2131 |
|
| 2132 |
if (m_is_returning_call) |
| 2133 |
emission_path->add_event |
| 2134 |
(make_unique<return_event> (eedge, |
| 2135 |
event_loc_info (m_dynamic_call |
| 2136 |
? m_dynamic_call->location |
| 2137 |
: UNKNOWN_LOCATION, |
| 2138 |
dest_point.get_fndecl (), |
| 2139 |
dest_stack_depth))); |
| 2140 |
else |
| 2141 |
emission_path->add_event |
| 2142 |
(make_unique<call_event> (eedge, |
| 2143 |
event_loc_info (m_dynamic_call |
| 2144 |
? m_dynamic_call->location |
| 2145 |
: UNKNOWN_LOCATION, |
| 2146 |
src_point.get_fndecl (), |
| 2147 |
src_stack_depth))); |
| 2148 |
} |
| 2149 |
|
| 2150 |
/* class rewind_info_t : public custom_edge_info. */ |
| 2151 |
|
| 2152 |
/* Implementation of custom_edge_info::update_model vfunc |
| 2153 |
for rewind_info_t. |
| 2154 |
|
| 2155 |
Update state for the special-case of a rewind of a longjmp |
| 2156 |
to a setjmp (which doesn't have a superedge, but does affect |
| 2157 |
state). */ |
| 2158 |
|
| 2159 |
bool |
| 2160 |
rewind_info_t::update_model (region_model *model, |
| 2161 |
const exploded_edge *eedge, |
| 2162 |
region_model_context *) const |
| 2163 |
{ |
| 2164 |
gcc_assert (eedge); |
| 2165 |
const program_point &longjmp_point = eedge->m_src->get_point (); |
| 2166 |
const program_point &setjmp_point = eedge->m_dest->get_point (); |
| 2167 |
|
| 2168 |
gcc_assert (longjmp_point.get_stack_depth () |
| 2169 |
>= setjmp_point.get_stack_depth ()); |
| 2170 |
|
| 2171 |
model->on_longjmp (get_longjmp_call (), |
| 2172 |
get_setjmp_call (), |
| 2173 |
setjmp_point.get_stack_depth (), NULL); |
| 2174 |
return true; |
| 2175 |
} |
| 2176 |
|
| 2177 |
/* Implementation of custom_edge_info::add_events_to_path vfunc |
| 2178 |
for rewind_info_t. */ |
| 2179 |
|
| 2180 |
void |
| 2181 |
rewind_info_t::add_events_to_path (checker_path *emission_path, |
| 2182 |
const exploded_edge &eedge) const |
| 2183 |
{ |
| 2184 |
const exploded_node *src_node = eedge.m_src; |
| 2185 |
const program_point &src_point = src_node->get_point (); |
| 2186 |
const int src_stack_depth = src_point.get_stack_depth (); |
| 2187 |
const exploded_node *dst_node = eedge.m_dest; |
| 2188 |
const program_point &dst_point = dst_node->get_point (); |
| 2189 |
const int dst_stack_depth = dst_point.get_stack_depth (); |
| 2190 |
|
| 2191 |
emission_path->add_event |
| 2192 |
(make_unique<rewind_from_longjmp_event> |
| 2193 |
(&eedge, |
| 2194 |
event_loc_info (get_longjmp_call ()->location, |
| 2195 |
src_point.get_fndecl (), |
| 2196 |
src_stack_depth), |
| 2197 |
this)); |
| 2198 |
emission_path->add_event |
| 2199 |
(make_unique<rewind_to_setjmp_event> |
| 2200 |
(&eedge, |
| 2201 |
event_loc_info (get_setjmp_call ()->location, |
| 2202 |
dst_point.get_fndecl (), |
| 2203 |
dst_stack_depth), |
| 2204 |
this)); |
| 2205 |
} |
| 2206 |
|
| 2207 |
/* class exploded_edge : public dedge<eg_traits>. */ |
| 2208 |
|
| 2209 |
/* exploded_edge's ctor. */ |
| 2210 |
|
| 2211 |
exploded_edge::exploded_edge (exploded_node *src, exploded_node *dest, |
| 2212 |
const superedge *sedge, bool could_do_work, |
| 2213 |
std::unique_ptr<custom_edge_info> custom_info) |
| 2214 |
: dedge<eg_traits> (src, dest), m_sedge (sedge), |
| 2215 |
m_custom_info (std::move (custom_info)), |
| 2216 |
m_could_do_work_p (could_do_work) |
| 2217 |
{ |
| 2218 |
} |
| 2219 |
|
| 2220 |
/* Implementation of dedge::dump_dot vfunc for exploded_edge. |
| 2221 |
Use the label of the underlying superedge, if any. */ |
| 2222 |
|
| 2223 |
void |
| 2224 |
exploded_edge::dump_dot (graphviz_out *gv, const dump_args_t &) const |
| 2225 |
{ |
| 2226 |
pretty_printer *pp = gv->get_pp (); |
| 2227 |
|
| 2228 |
m_src->dump_dot_id (pp); |
| 2229 |
pp_string (pp, " -> "); |
| 2230 |
m_dest->dump_dot_id (pp); |
| 2231 |
dump_dot_label (pp); |
| 2232 |
} |
| 2233 |
|
| 2234 |
/* Second half of exploded_edge::dump_dot. This is split out |
| 2235 |
for use by trimmed_graph::dump_dot and base_feasible_edge::dump_dot. */ |
| 2236 |
|
| 2237 |
void |
| 2238 |
exploded_edge::dump_dot_label (pretty_printer *pp) const |
| 2239 |
{ |
| 2240 |
const char *style = "\"solid,bold\""; |
| 2241 |
const char *color = "black"; |
| 2242 |
int weight = 10; |
| 2243 |
const char *constraint = "true"; |
| 2244 |
|
| 2245 |
if (m_sedge) |
| 2246 |
switch (m_sedge->m_kind) |
| 2247 |
{ |
| 2248 |
default: |
| 2249 |
gcc_unreachable (); |
| 2250 |
case SUPEREDGE_CFG_EDGE: |
| 2251 |
break; |
| 2252 |
case SUPEREDGE_CALL: |
| 2253 |
color = "red"; |
| 2254 |
//constraint = "false"; |
| 2255 |
break; |
| 2256 |
case SUPEREDGE_RETURN: |
| 2257 |
color = "green"; |
| 2258 |
//constraint = "false"; |
| 2259 |
break; |
| 2260 |
case SUPEREDGE_INTRAPROCEDURAL_CALL: |
| 2261 |
style = "\"dotted\""; |
| 2262 |
break; |
| 2263 |
} |
| 2264 |
if (m_custom_info) |
| 2265 |
{ |
| 2266 |
color = "red"; |
| 2267 |
style = "\"dotted\""; |
| 2268 |
} |
| 2269 |
|
| 2270 |
pp_printf (pp, |
| 2271 |
(" [style=%s, color=%s, weight=%d, constraint=%s," |
| 2272 |
" headlabel=\""), |
| 2273 |
style, color, weight, constraint); |
| 2274 |
|
| 2275 |
if (m_sedge) |
| 2276 |
m_sedge->dump_label_to_pp (pp, false); |
| 2277 |
else if (m_custom_info) |
| 2278 |
m_custom_info->print (pp); |
| 2279 |
|
| 2280 |
pp_printf (pp, "%s", |
| 2281 |
could_do_work_p () ? "(could do work)" : "DOES NO WORK"); |
| 2282 |
|
| 2283 |
//pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/false); |
| 2284 |
|
| 2285 |
pp_printf (pp, "\"];\n"); |
| 2286 |
} |
| 2287 |
|
| 2288 |
/* Return a new json::object of the form |
| 2289 |
{"src_idx": int, the index of the source exploded edge, |
| 2290 |
"dst_idx": int, the index of the destination exploded edge, |
| 2291 |
"sedge": (optional) object for the superedge, if any, |
| 2292 |
"custom": (optional) str, a description, if this is a custom edge}. */ |
| 2293 |
|
| 2294 |
std::unique_ptr<json::object> |
| 2295 |
exploded_edge::to_json () const |
| 2296 |
{ |
| 2297 |
auto eedge_obj = ::make_unique<json::object> (); |
| 2298 |
eedge_obj->set_integer ("src_idx", m_src->m_index); |
| 2299 |
eedge_obj->set_integer ("dst_idx", m_dest->m_index); |
| 2300 |
if (m_sedge) |
| 2301 |
eedge_obj->set ("sedge", m_sedge->to_json ()); |
| 2302 |
if (m_custom_info) |
| 2303 |
{ |
| 2304 |
pretty_printer pp; |
| 2305 |
pp_format_decoder (&pp) = default_tree_printer; |
| 2306 |
m_custom_info->print (&pp); |
| 2307 |
eedge_obj->set_string ("custom", pp_formatted_text (&pp)); |
| 2308 |
} |
| 2309 |
return eedge_obj; |
| 2310 |
} |
| 2311 |
|
| 2312 |
/* struct stats. */ |
| 2313 |
|
| 2314 |
/* stats' ctor. */ |
| 2315 |
|
| 2316 |
stats::stats (int num_supernodes) |
| 2317 |
: m_node_reuse_count (0), |
| 2318 |
m_node_reuse_after_merge_count (0), |
| 2319 |
m_num_supernodes (num_supernodes) |
| 2320 |
{ |
| 2321 |
for (int i = 0; i < NUM_POINT_KINDS; i++) |
| 2322 |
m_num_nodes[i] = 0; |
| 2323 |
} |
| 2324 |
|
| 2325 |
/* Log these stats in multiline form to LOGGER. */ |
| 2326 |
|
| 2327 |
void |
| 2328 |
stats::log (logger *logger) const |
| 2329 |
{ |
| 2330 |
gcc_assert (logger); |
| 2331 |
for (int i = 0; i < NUM_POINT_KINDS; i++) |
| 2332 |
if (m_num_nodes[i] > 0) |
| 2333 |
logger->log ("m_num_nodes[%s]: %i", |
| 2334 |
point_kind_to_string (static_cast <enum point_kind> (i)), |
| 2335 |
m_num_nodes[i]); |
| 2336 |
logger->log ("m_node_reuse_count: %i", m_node_reuse_count); |
| 2337 |
logger->log ("m_node_reuse_after_merge_count: %i", |
| 2338 |
m_node_reuse_after_merge_count); |
| 2339 |
} |
| 2340 |
|
| 2341 |
/* Dump these stats in multiline form to OUT. */ |
| 2342 |
|
| 2343 |
void |
| 2344 |
stats::dump (FILE *out) const |
| 2345 |
{ |
| 2346 |
for (int i = 0; i < NUM_POINT_KINDS; i++) |
| 2347 |
if (m_num_nodes[i] > 0) |
| 2348 |
fprintf (out, "m_num_nodes[%s]: %i\n", |
| 2349 |
point_kind_to_string (static_cast <enum point_kind> (i)), |
| 2350 |
m_num_nodes[i]); |
| 2351 |
fprintf (out, "m_node_reuse_count: %i\n", m_node_reuse_count); |
| 2352 |
fprintf (out, "m_node_reuse_after_merge_count: %i\n", |
| 2353 |
m_node_reuse_after_merge_count); |
| 2354 |
|
| 2355 |
if (m_num_supernodes > 0) |
| 2356 |
fprintf (out, "PK_AFTER_SUPERNODE nodes per supernode: %.2f\n", |
| 2357 |
(float)m_num_nodes[PK_AFTER_SUPERNODE] / (float)m_num_supernodes); |
| 2358 |
} |
| 2359 |
|
| 2360 |
/* Return the total number of enodes recorded within this object. */ |
| 2361 |
|
| 2362 |
int |
| 2363 |
stats::get_total_enodes () const |
| 2364 |
{ |
| 2365 |
int result = 0; |
| 2366 |
for (int i = 0; i < NUM_POINT_KINDS; i++) |
| 2367 |
result += m_num_nodes[i]; |
| 2368 |
return result; |
| 2369 |
} |
| 2370 |
|
| 2371 |
/* struct per_function_data. */ |
| 2372 |
|
| 2373 |
per_function_data::~per_function_data () |
| 2374 |
{ |
| 2375 |
for (auto iter : m_summaries) |
| 2376 |
delete iter; |
| 2377 |
} |
| 2378 |
|
| 2379 |
void |
| 2380 |
per_function_data::add_call_summary (exploded_node *node) |
| 2381 |
{ |
| 2382 |
m_summaries.safe_push (new call_summary (this, node)); |
| 2383 |
} |
| 2384 |
|
| 2385 |
/* strongly_connected_components's ctor. Tarjan's SCC algorithm. */ |
| 2386 |
|
| 2387 |
strongly_connected_components:: |
| 2388 |
strongly_connected_components (const supergraph &sg, logger *logger) |
| 2389 |
: m_sg (sg), m_per_node (m_sg.num_nodes ()) |
| 2390 |
{ |
| 2391 |
LOG_SCOPE (logger); |
| 2392 |
auto_timevar tv (TV_ANALYZER_SCC); |
| 2393 |
|
| 2394 |
for (int i = 0; i < m_sg.num_nodes (); i++) |
| 2395 |
m_per_node.quick_push (per_node_data ()); |
| 2396 |
|
| 2397 |
for (int i = 0; i < m_sg.num_nodes (); i++) |
| 2398 |
if (m_per_node[i].m_index == -1) |
| 2399 |
strong_connect (i); |
| 2400 |
|
| 2401 |
if (0) |
| 2402 |
dump (); |
| 2403 |
} |
| 2404 |
|
| 2405 |
/* Dump this object to stderr. */ |
| 2406 |
|
| 2407 |
DEBUG_FUNCTION void |
| 2408 |
strongly_connected_components::dump () const |
| 2409 |
{ |
| 2410 |
for (int i = 0; i < m_sg.num_nodes (); i++) |
| 2411 |
{ |
| 2412 |
const per_node_data &v = m_per_node[i]; |
| 2413 |
fprintf (stderr, "SN %i: index: %i lowlink: %i on_stack: %i\n", |
| 2414 |
i, v.m_index, v.m_lowlink, v.m_on_stack); |
| 2415 |
} |
| 2416 |
} |
| 2417 |
|
| 2418 |
/* Return a new json::array of per-snode SCC ids. */ |
| 2419 |
|
| 2420 |
std::unique_ptr<json::array> |
| 2421 |
strongly_connected_components::to_json () const |
| 2422 |
{ |
| 2423 |
auto scc_arr = ::make_unique<json::array> (); |
| 2424 |
for (int i = 0; i < m_sg.num_nodes (); i++) |
| 2425 |
scc_arr->append (::make_unique<json::integer_number> (get_scc_id (i))); |
| 2426 |
return scc_arr; |
| 2427 |
} |
| 2428 |
|
| 2429 |
/* Subroutine of strongly_connected_components's ctor, part of Tarjan's |
| 2430 |
SCC algorithm. */ |
| 2431 |
|
| 2432 |
void |
| 2433 |
strongly_connected_components::strong_connect (unsigned index) |
| 2434 |
{ |
| 2435 |
supernode *v_snode = m_sg.get_node_by_index (index); |
| 2436 |
|
| 2437 |
/* Set the depth index for v to the smallest unused index. */ |
| 2438 |
per_node_data *v = &m_per_node[index]; |
| 2439 |
v->m_index = index; |
| 2440 |
v->m_lowlink = index; |
| 2441 |
m_stack.safe_push (index); |
| 2442 |
v->m_on_stack = true; |
| 2443 |
index++; |
| 2444 |
|
| 2445 |
/* Consider successors of v. */ |
| 2446 |
unsigned i; |
| 2447 |
superedge *sedge; |
| 2448 |
FOR_EACH_VEC_ELT (v_snode->m_succs, i, sedge) |
| 2449 |
{ |
| 2450 |
if (sedge->get_kind () != SUPEREDGE_CFG_EDGE |
| 2451 |
&& sedge->get_kind () != SUPEREDGE_INTRAPROCEDURAL_CALL) |
| 2452 |
continue; |
| 2453 |
supernode *w_snode = sedge->m_dest; |
| 2454 |
per_node_data *w = &m_per_node[w_snode->m_index]; |
| 2455 |
if (w->m_index == -1) |
| 2456 |
{ |
| 2457 |
/* Successor w has not yet been visited; recurse on it. */ |
| 2458 |
strong_connect (w_snode->m_index); |
| 2459 |
v->m_lowlink = MIN (v->m_lowlink, w->m_lowlink); |
| 2460 |
} |
| 2461 |
else if (w->m_on_stack) |
| 2462 |
{ |
| 2463 |
/* Successor w is in stack S and hence in the current SCC |
| 2464 |
If w is not on stack, then (v, w) is a cross-edge in the DFS |
| 2465 |
tree and must be ignored. */ |
| 2466 |
v->m_lowlink = MIN (v->m_lowlink, w->m_index); |
| 2467 |
} |
| 2468 |
} |
| 2469 |
|
| 2470 |
/* If v is a root node, pop the stack and generate an SCC. */ |
| 2471 |
|
| 2472 |
if (v->m_lowlink == v->m_index) |
| 2473 |
{ |
| 2474 |
per_node_data *w; |
| 2475 |
do { |
| 2476 |
int idx = m_stack.pop (); |
| 2477 |
w = &m_per_node[idx]; |
| 2478 |
w->m_on_stack = false; |
| 2479 |
} while (w != v); |
| 2480 |
} |
| 2481 |
} |
| 2482 |
|
| 2483 |
/* worklist's ctor. */ |
| 2484 |
|
| 2485 |
worklist::worklist (const exploded_graph &eg, const analysis_plan &plan) |
| 2486 |
: m_scc (eg.get_supergraph (), eg.get_logger ()), |
| 2487 |
m_plan (plan), |
| 2488 |
m_queue (key_t (*this, NULL)) |
| 2489 |
{ |
| 2490 |
} |
| 2491 |
|
| 2492 |
/* Return the number of nodes in the worklist. */ |
| 2493 |
|
| 2494 |
unsigned |
| 2495 |
worklist::length () const |
| 2496 |
{ |
| 2497 |
return m_queue.nodes (); |
| 2498 |
} |
| 2499 |
|
| 2500 |
/* Return the next node in the worklist, removing it. */ |
| 2501 |
|
| 2502 |
exploded_node * |
| 2503 |
worklist::take_next () |
| 2504 |
{ |
| 2505 |
return m_queue.extract_min (); |
| 2506 |
} |
| 2507 |
|
| 2508 |
/* Return the next node in the worklist without removing it. */ |
| 2509 |
|
| 2510 |
exploded_node * |
| 2511 |
worklist::peek_next () |
| 2512 |
{ |
| 2513 |
return m_queue.min (); |
| 2514 |
} |
| 2515 |
|
| 2516 |
/* Add ENODE to the worklist. */ |
| 2517 |
|
| 2518 |
void |
| 2519 |
worklist::add_node (exploded_node *enode) |
| 2520 |
{ |
| 2521 |
gcc_assert (enode->get_status () == exploded_node::STATUS_WORKLIST); |
| 2522 |
m_queue.insert (key_t (*this, enode), enode); |
| 2523 |
} |
| 2524 |
|
| 2525 |
/* Comparator for implementing worklist::key_t comparison operators. |
| 2526 |
Return negative if KA is before KB |
| 2527 |
Return positive if KA is after KB |
| 2528 |
Return 0 if they are equal. |
| 2529 |
|
| 2530 |
The ordering of the worklist is critical for performance and for |
| 2531 |
avoiding node explosions. Ideally we want all enodes at a CFG join-point |
| 2532 |
with the same callstring to be sorted next to each other in the worklist |
| 2533 |
so that a run of consecutive enodes can be merged and processed "in bulk" |
| 2534 |
rather than individually or pairwise, minimizing the number of new enodes |
| 2535 |
created. */ |
| 2536 |
|
| 2537 |
int |
| 2538 |
worklist::key_t::cmp (const worklist::key_t &ka, const worklist::key_t &kb) |
| 2539 |
{ |
| 2540 |
const program_point &point_a = ka.m_enode->get_point (); |
| 2541 |
const program_point &point_b = kb.m_enode->get_point (); |
| 2542 |
const call_string &call_string_a = point_a.get_call_string (); |
| 2543 |
const call_string &call_string_b = point_b.get_call_string (); |
| 2544 |
|
| 2545 |
/* Order empty-callstring points with different functions based on the |
| 2546 |
analysis_plan, so that we generate summaries before they are used. */ |
| 2547 |
if (flag_analyzer_call_summaries |
| 2548 |
&& call_string_a.empty_p () |
| 2549 |
&& call_string_b.empty_p () |
| 2550 |
&& point_a.get_function () != NULL |
| 2551 |
&& point_b.get_function () != NULL |
| 2552 |
&& point_a.get_function () != point_b.get_function ()) |
| 2553 |
{ |
| 2554 |
if (int cmp = ka.m_worklist.m_plan.cmp_function (point_a.get_function (), |
| 2555 |
point_b.get_function ())) |
| 2556 |
return cmp; |
| 2557 |
} |
| 2558 |
|
| 2559 |
/* Sort by callstring, so that nodes with deeper call strings are processed |
| 2560 |
before those with shallower call strings. |
| 2561 |
If we have |
| 2562 |
splitting BB |
| 2563 |
/ \ |
| 2564 |
/ \ |
| 2565 |
fn call no fn call |
| 2566 |
\ / |
| 2567 |
\ / |
| 2568 |
join BB |
| 2569 |
then we want the path inside the function call to be fully explored up |
| 2570 |
to the return to the join BB before we explore on the "no fn call" path, |
| 2571 |
so that both enodes at the join BB reach the front of the worklist at |
| 2572 |
the same time and thus have a chance of being merged. */ |
| 2573 |
int cs_cmp = call_string::cmp (call_string_a, call_string_b); |
| 2574 |
if (cs_cmp) |
| 2575 |
return cs_cmp; |
| 2576 |
|
| 2577 |
/* Order by SCC. */ |
| 2578 |
int scc_id_a = ka.get_scc_id (ka.m_enode); |
| 2579 |
int scc_id_b = kb.get_scc_id (kb.m_enode); |
| 2580 |
if (scc_id_a != scc_id_b) |
| 2581 |
return scc_id_a - scc_id_b; |
| 2582 |
|
| 2583 |
/* If in same SCC, order by supernode index (an arbitrary but stable |
| 2584 |
ordering). */ |
| 2585 |
const supernode *snode_a = ka.m_enode->get_supernode (); |
| 2586 |
const supernode *snode_b = kb.m_enode->get_supernode (); |
| 2587 |
if (snode_a == NULL) |
| 2588 |
{ |
| 2589 |
if (snode_b != NULL) |
| 2590 |
/* One is NULL. */ |
| 2591 |
return -1; |
| 2592 |
else |
| 2593 |
/* Both are NULL. */ |
| 2594 |
return 0; |
| 2595 |
} |
| 2596 |
if (snode_b == NULL) |
| 2597 |
/* One is NULL. */ |
| 2598 |
return 1; |
| 2599 |
/* Neither are NULL. */ |
| 2600 |
gcc_assert (snode_a && snode_b); |
| 2601 |
if (snode_a->m_index != snode_b->m_index) |
| 2602 |
return snode_a->m_index - snode_b->m_index; |
| 2603 |
|
| 2604 |
gcc_assert (snode_a == snode_b); |
| 2605 |
|
| 2606 |
/* Order within supernode via program point. */ |
| 2607 |
int within_snode_cmp |
| 2608 |
= function_point::cmp_within_supernode (point_a.get_function_point (), |
| 2609 |
point_b.get_function_point ()); |
| 2610 |
if (within_snode_cmp) |
| 2611 |
return within_snode_cmp; |
| 2612 |
|
| 2613 |
/* Otherwise, we ought to have the same program_point. */ |
| 2614 |
gcc_assert (point_a == point_b); |
| 2615 |
|
| 2616 |
const program_state &state_a = ka.m_enode->get_state (); |
| 2617 |
const program_state &state_b = kb.m_enode->get_state (); |
| 2618 |
|
| 2619 |
/* Sort by sm-state, so that identical sm-states are grouped |
| 2620 |
together in the worklist. */ |
| 2621 |
for (unsigned sm_idx = 0; sm_idx < state_a.m_checker_states.length (); |
| 2622 |
++sm_idx) |
| 2623 |
{ |
| 2624 |
sm_state_map *smap_a = state_a.m_checker_states[sm_idx]; |
| 2625 |
sm_state_map *smap_b = state_b.m_checker_states[sm_idx]; |
| 2626 |
|
| 2627 |
if (int smap_cmp = sm_state_map::cmp (*smap_a, *smap_b)) |
| 2628 |
return smap_cmp; |
| 2629 |
} |
| 2630 |
|
| 2631 |
/* Otherwise, we have two enodes at the same program point but with |
| 2632 |
different states. We don't have a good total ordering on states, |
| 2633 |
so order them by enode index, so that we have at least have a |
| 2634 |
stable sort. */ |
| 2635 |
return ka.m_enode->m_index - kb.m_enode->m_index; |
| 2636 |
} |
| 2637 |
|
| 2638 |
/* Return a new json::object of the form |
| 2639 |
{"scc" : [per-snode-IDs]}, */ |
| 2640 |
|
| 2641 |
std::unique_ptr<json::object> |
| 2642 |
worklist::to_json () const |
| 2643 |
{ |
| 2644 |
auto worklist_obj = ::make_unique<json::object> (); |
| 2645 |
|
| 2646 |
worklist_obj->set ("scc", m_scc.to_json ()); |
| 2647 |
|
| 2648 |
/* The following field isn't yet being JSONified: |
| 2649 |
queue_t m_queue; */ |
| 2650 |
|
| 2651 |
return worklist_obj; |
| 2652 |
} |
| 2653 |
|
| 2654 |
/* exploded_graph's ctor. */ |
| 2655 |
|
| 2656 |
exploded_graph::exploded_graph (const supergraph &sg, logger *logger, |
| 2657 |
const extrinsic_state &ext_state, |
| 2658 |
const state_purge_map *purge_map, |
| 2659 |
const analysis_plan &plan, |
| 2660 |
int verbosity) |
| 2661 |
: m_sg (sg), m_logger (logger), |
| 2662 |
m_worklist (*this, plan), |
| 2663 |
m_ext_state (ext_state), |
| 2664 |
m_purge_map (purge_map), |
| 2665 |
m_plan (plan), |
| 2666 |
m_diagnostic_manager (logger, ext_state.get_engine (), verbosity), |
| 2667 |
m_global_stats (m_sg.num_nodes ()), |
| 2668 |
m_functionless_stats (m_sg.num_nodes ()), |
| 2669 |
m_PK_AFTER_SUPERNODE_per_snode (m_sg.num_nodes ()) |
| 2670 |
{ |
| 2671 |
m_origin = get_or_create_node |
| 2672 |
(program_point::origin (*ext_state.get_model_manager ()), |
| 2673 |
program_state (ext_state), NULL); |
| 2674 |
for (int i = 0; i < m_sg.num_nodes (); i++) |
| 2675 |
m_PK_AFTER_SUPERNODE_per_snode.quick_push (i); |
| 2676 |
} |
| 2677 |
|
| 2678 |
/* exploded_graph's dtor. */ |
| 2679 |
|
| 2680 |
exploded_graph::~exploded_graph () |
| 2681 |
{ |
| 2682 |
for (auto iter : m_per_point_data) |
| 2683 |
delete iter.second; |
| 2684 |
for (auto iter : m_per_function_data) |
| 2685 |
delete iter.second; |
| 2686 |
for (auto iter : m_per_function_stats) |
| 2687 |
delete iter.second; |
| 2688 |
for (auto iter : m_per_call_string_data) |
| 2689 |
delete iter.second; |
| 2690 |
} |
| 2691 |
|
| 2692 |
/* Subroutine for use when implementing __attribute__((tainted_args)) |
| 2693 |
on functions and on function pointer fields in structs. |
| 2694 |
|
| 2695 |
Called on STATE representing a call to FNDECL. |
| 2696 |
Mark all params of FNDECL in STATE as "tainted". Mark the value of all |
| 2697 |
regions pointed to by params of FNDECL as "tainted". |
| 2698 |
|
| 2699 |
Return true if successful; return false if the "taint" state machine |
| 2700 |
was not found. */ |
| 2701 |
|
| 2702 |
static bool |
| 2703 |
mark_params_as_tainted (program_state *state, tree fndecl, |
| 2704 |
const extrinsic_state &ext_state) |
| 2705 |
{ |
| 2706 |
unsigned taint_sm_idx; |
| 2707 |
if (!ext_state.get_sm_idx_by_name ("taint", &taint_sm_idx)) |
| 2708 |
return false; |
| 2709 |
sm_state_map *smap = state->m_checker_states[taint_sm_idx]; |
| 2710 |
|
| 2711 |
const state_machine &sm = ext_state.get_sm (taint_sm_idx); |
| 2712 |
state_machine::state_t tainted = sm.get_state_by_name ("tainted"); |
| 2713 |
|
| 2714 |
region_model_manager *mgr = ext_state.get_model_manager (); |
| 2715 |
|
| 2716 |
function *fun = DECL_STRUCT_FUNCTION (fndecl); |
| 2717 |
gcc_assert (fun); |
| 2718 |
|
| 2719 |
for (tree iter_parm = DECL_ARGUMENTS (fndecl); iter_parm; |
| 2720 |
iter_parm = DECL_CHAIN (iter_parm)) |
| 2721 |
{ |
| 2722 |
tree param = iter_parm; |
| 2723 |
if (tree parm_default_ssa = ssa_default_def (fun, iter_parm)) |
| 2724 |
param = parm_default_ssa; |
| 2725 |
const region *param_reg = state->m_region_model->get_lvalue (param, NULL); |
| 2726 |
const svalue *init_sval = mgr->get_or_create_initial_value (param_reg); |
| 2727 |
smap->set_state (state->m_region_model, init_sval, |
| 2728 |
tainted, NULL /*origin_new_sval*/, ext_state); |
| 2729 |
if (POINTER_TYPE_P (TREE_TYPE (param))) |
| 2730 |
{ |
| 2731 |
const region *pointee_reg = mgr->get_symbolic_region (init_sval); |
| 2732 |
/* Mark "*param" as tainted. */ |
| 2733 |
const svalue *init_pointee_sval |
| 2734 |
= mgr->get_or_create_initial_value (pointee_reg); |
| 2735 |
smap->set_state (state->m_region_model, init_pointee_sval, |
| 2736 |
tainted, NULL /*origin_new_sval*/, ext_state); |
| 2737 |
} |
| 2738 |
} |
| 2739 |
|
| 2740 |
return true; |
| 2741 |
} |
| 2742 |
|
| 2743 |
/* Custom event for use by tainted_args_function_info when a function |
| 2744 |
has been marked with __attribute__((tainted_args)). */ |
| 2745 |
|
| 2746 |
class tainted_args_function_custom_event : public custom_event |
| 2747 |
{ |
| 2748 |
public: |
| 2749 |
tainted_args_function_custom_event (const event_loc_info &loc_info) |
| 2750 |
: custom_event (loc_info), |
| 2751 |
m_fndecl (loc_info.m_fndecl) |
| 2752 |
{ |
| 2753 |
} |
| 2754 |
|
| 2755 |
void |
| 2756 |
print_desc (pretty_printer &pp) const final override |
| 2757 |
{ |
| 2758 |
pp_printf (&pp, |
| 2759 |
"function %qE marked with %<__attribute__((tainted_args))%>", |
| 2760 |
m_fndecl); |
| 2761 |
} |
| 2762 |
|
| 2763 |
private: |
| 2764 |
tree m_fndecl; |
| 2765 |
}; |
| 2766 |
|
| 2767 |
/* Custom exploded_edge info for top-level calls to a function |
| 2768 |
marked with __attribute__((tainted_args)). */ |
| 2769 |
|
| 2770 |
class tainted_args_function_info : public custom_edge_info |
| 2771 |
{ |
| 2772 |
public: |
| 2773 |
tainted_args_function_info (tree fndecl) |
| 2774 |
: m_fndecl (fndecl) |
| 2775 |
{} |
| 2776 |
|
| 2777 |
void print (pretty_printer *pp) const final override |
| 2778 |
{ |
| 2779 |
pp_string (pp, "call to tainted_args function"); |
| 2780 |
}; |
| 2781 |
|
| 2782 |
bool update_model (region_model *, |
| 2783 |
const exploded_edge *, |
| 2784 |
region_model_context *) const final override |
| 2785 |
{ |
| 2786 |
/* No-op. */ |
| 2787 |
return true; |
| 2788 |
} |
| 2789 |
|
| 2790 |
void add_events_to_path (checker_path *emission_path, |
| 2791 |
const exploded_edge &) const final override |
| 2792 |
{ |
| 2793 |
emission_path->add_event |
| 2794 |
(make_unique<tainted_args_function_custom_event> |
| 2795 |
(event_loc_info (DECL_SOURCE_LOCATION (m_fndecl), m_fndecl, 0))); |
| 2796 |
} |
| 2797 |
|
| 2798 |
private: |
| 2799 |
tree m_fndecl; |
| 2800 |
}; |
| 2801 |
|
| 2802 |
/* Ensure that there is an exploded_node representing an external call to |
| 2803 |
FUN, adding it to the worklist if creating it. |
| 2804 |
|
| 2805 |
Add an edge from the origin exploded_node to the function entrypoint |
| 2806 |
exploded_node. |
| 2807 |
|
| 2808 |
Return the exploded_node for the entrypoint to the function. */ |
| 2809 |
|
| 2810 |
exploded_node * |
| 2811 |
exploded_graph::add_function_entry (const function &fun) |
| 2812 |
{ |
| 2813 |
gcc_assert (gimple_has_body_p (fun.decl)); |
| 2814 |
|
| 2815 |
/* Be idempotent. */ |
| 2816 |
function *key = const_cast<function *> (&fun); |
| 2817 |
if (m_functions_with_enodes.contains (key)) |
| 2818 |
{ |
| 2819 |
logger * const logger = get_logger (); |
| 2820 |
if (logger) |
| 2821 |
logger->log ("entrypoint for %qE already exists", fun.decl); |
| 2822 |
return NULL; |
| 2823 |
} |
| 2824 |
|
| 2825 |
program_point point |
| 2826 |
= program_point::from_function_entry (*m_ext_state.get_model_manager (), |
| 2827 |
m_sg, fun); |
| 2828 |
program_state state (m_ext_state); |
| 2829 |
state.push_frame (m_ext_state, fun); |
| 2830 |
|
| 2831 |
std::unique_ptr<custom_edge_info> edge_info = NULL; |
| 2832 |
|
| 2833 |
if (lookup_attribute ("tainted_args", DECL_ATTRIBUTES (fun.decl))) |
| 2834 |
{ |
| 2835 |
if (mark_params_as_tainted (&state, fun.decl, m_ext_state)) |
| 2836 |
edge_info = make_unique<tainted_args_function_info> (fun.decl); |
| 2837 |
} |
| 2838 |
|
| 2839 |
if (!state.m_valid) |
| 2840 |
return NULL; |
| 2841 |
|
| 2842 |
exploded_node *enode = get_or_create_node (point, state, NULL); |
| 2843 |
if (!enode) |
| 2844 |
return NULL; |
| 2845 |
|
| 2846 |
add_edge (m_origin, enode, NULL, false, std::move (edge_info)); |
| 2847 |
|
| 2848 |
m_functions_with_enodes.add (key); |
| 2849 |
|
| 2850 |
return enode; |
| 2851 |
} |
| 2852 |
|
| 2853 |
/* Get or create an exploded_node for (POINT, STATE). |
| 2854 |
If a new node is created, it is added to the worklist. |
| 2855 |
|
| 2856 |
Use ENODE_FOR_DIAG, a pre-existing enode, for any diagnostics |
| 2857 |
that need to be emitted (e.g. when purging state *before* we have |
| 2858 |
a new enode). */ |
| 2859 |
|
| 2860 |
exploded_node * |
| 2861 |
exploded_graph::get_or_create_node (const program_point &point, |
| 2862 |
const program_state &state, |
| 2863 |
exploded_node *enode_for_diag) |
| 2864 |
{ |
| 2865 |
logger * const logger = get_logger (); |
| 2866 |
LOG_FUNC (logger); |
| 2867 |
if (logger) |
| 2868 |
{ |
| 2869 |
format f (false); |
| 2870 |
pretty_printer *pp = logger->get_printer (); |
| 2871 |
logger->start_log_line (); |
| 2872 |
pp_string (pp, "point: "); |
| 2873 |
point.print (pp, f); |
| 2874 |
logger->end_log_line (); |
| 2875 |
logger->start_log_line (); |
| 2876 |
pp_string (pp, "state: "); |
| 2877 |
state.dump_to_pp (m_ext_state, true, false, pp); |
| 2878 |
logger->end_log_line (); |
| 2879 |
} |
| 2880 |
|
| 2881 |
/* Stop exploring paths for which we don't know how to effectively |
| 2882 |
model the state. */ |
| 2883 |
if (!state.m_valid) |
| 2884 |
{ |
| 2885 |
if (logger) |
| 2886 |
logger->log ("invalid state; not creating node"); |
| 2887 |
return NULL; |
| 2888 |
} |
| 2889 |
|
| 2890 |
auto_cfun sentinel (point.get_function ()); |
| 2891 |
|
| 2892 |
state.validate (get_ext_state ()); |
| 2893 |
|
| 2894 |
//state.dump (get_ext_state ()); |
| 2895 |
|
| 2896 |
/* Prune state to try to improve the chances of a cache hit, |
| 2897 |
avoiding generating redundant nodes. */ |
| 2898 |
uncertainty_t uncertainty; |
| 2899 |
program_state pruned_state |
| 2900 |
= state.prune_for_point (*this, point, enode_for_diag, &uncertainty); |
| 2901 |
|
| 2902 |
pruned_state.validate (get_ext_state ()); |
| 2903 |
|
| 2904 |
//pruned_state.dump (get_ext_state ()); |
| 2905 |
|
| 2906 |
if (logger) |
| 2907 |
{ |
| 2908 |
pretty_printer *pp = logger->get_printer (); |
| 2909 |
logger->start_log_line (); |
| 2910 |
pp_string (pp, "pruned_state: "); |
| 2911 |
pruned_state.dump_to_pp (m_ext_state, true, false, pp); |
| 2912 |
logger->end_log_line (); |
| 2913 |
pruned_state.m_region_model->dump_to_pp (logger->get_printer (), true, |
| 2914 |
false); |
| 2915 |
} |
| 2916 |
|
| 2917 |
stats *per_fn_stats = get_or_create_function_stats (point.get_function ()); |
| 2918 |
|
| 2919 |
stats *per_cs_stats |
| 2920 |
= &get_or_create_per_call_string_data (point.get_call_string ())->m_stats; |
| 2921 |
|
| 2922 |
point_and_state ps (point, pruned_state); |
| 2923 |
ps.validate (m_ext_state); |
| 2924 |
if (exploded_node **slot = m_point_and_state_to_node.get (&ps)) |
| 2925 |
{ |
| 2926 |
/* An exploded_node for PS already exists. */ |
| 2927 |
if (logger) |
| 2928 |
logger->log ("reused EN: %i", (*slot)->m_index); |
| 2929 |
m_global_stats.m_node_reuse_count++; |
| 2930 |
per_fn_stats->m_node_reuse_count++; |
| 2931 |
per_cs_stats->m_node_reuse_count++; |
| 2932 |
return *slot; |
| 2933 |
} |
| 2934 |
|
| 2935 |
per_program_point_data *per_point_data |
| 2936 |
= get_or_create_per_program_point_data (point); |
| 2937 |
|
| 2938 |
/* Consider merging state with another enode at this program_point. */ |
| 2939 |
if (flag_analyzer_state_merge) |
| 2940 |
{ |
| 2941 |
exploded_node *existing_enode; |
| 2942 |
unsigned i; |
| 2943 |
FOR_EACH_VEC_ELT (per_point_data->m_enodes, i, existing_enode) |
| 2944 |
{ |
| 2945 |
if (logger) |
| 2946 |
logger->log ("considering merging with existing EN: %i for point", |
| 2947 |
existing_enode->m_index); |
| 2948 |
gcc_assert (existing_enode->get_point () == point); |
| 2949 |
const program_state &existing_state = existing_enode->get_state (); |
| 2950 |
|
| 2951 |
/* This merges successfully within the loop. */ |
| 2952 |
|
| 2953 |
program_state merged_state (m_ext_state); |
| 2954 |
if (pruned_state.can_merge_with_p (existing_state, m_ext_state, point, |
| 2955 |
&merged_state)) |
| 2956 |
{ |
| 2957 |
merged_state.validate (m_ext_state); |
| 2958 |
if (logger) |
| 2959 |
logger->log ("merging new state with that of EN: %i", |
| 2960 |
existing_enode->m_index); |
| 2961 |
|
| 2962 |
/* Try again for a cache hit. |
| 2963 |
Whether we get one or not, merged_state's value_ids have no |
| 2964 |
relationship to those of the input state, and thus to those |
| 2965 |
of CHANGE, so we must purge any svalue_ids from *CHANGE. */ |
| 2966 |
ps.set_state (merged_state); |
| 2967 |
|
| 2968 |
if (exploded_node **slot = m_point_and_state_to_node.get (&ps)) |
| 2969 |
{ |
| 2970 |
/* An exploded_node for PS already exists. */ |
| 2971 |
if (logger) |
| 2972 |
logger->log ("reused EN: %i", (*slot)->m_index); |
| 2973 |
m_global_stats.m_node_reuse_after_merge_count++; |
| 2974 |
per_fn_stats->m_node_reuse_after_merge_count++; |
| 2975 |
per_cs_stats->m_node_reuse_after_merge_count++; |
| 2976 |
return *slot; |
| 2977 |
} |
| 2978 |
} |
| 2979 |
else |
| 2980 |
if (logger) |
| 2981 |
logger->log ("not merging new state with that of EN: %i", |
| 2982 |
existing_enode->m_index); |
| 2983 |
} |
| 2984 |
} |
| 2985 |
|
| 2986 |
/* Impose a limit on the number of enodes per program point, and |
| 2987 |
simply stop if we exceed it. */ |
| 2988 |
if ((int)per_point_data->m_enodes.length () |
| 2989 |
>= param_analyzer_max_enodes_per_program_point) |
| 2990 |
{ |
| 2991 |
pretty_printer pp; |
| 2992 |
point.print (&pp, format (false)); |
| 2993 |
print_enode_indices (&pp, per_point_data->m_enodes); |
| 2994 |
if (logger) |
| 2995 |
logger->log ("not creating enode; too many at program point: %s", |
| 2996 |
pp_formatted_text (&pp)); |
| 2997 |
warning_at (point.get_location (), OPT_Wanalyzer_too_complex, |
| 2998 |
"terminating analysis for this program point: %s", |
| 2999 |
pp_formatted_text (&pp)); |
| 3000 |
per_point_data->m_excess_enodes++; |
| 3001 |
return NULL; |
| 3002 |
} |
| 3003 |
|
| 3004 |
ps.validate (m_ext_state); |
| 3005 |
|
| 3006 |
/* An exploded_node for "ps" doesn't already exist; create one. */ |
| 3007 |
exploded_node *node = new exploded_node (ps, m_nodes.length ()); |
| 3008 |
add_node (node); |
| 3009 |
m_point_and_state_to_node.put (node->get_ps_key (), node); |
| 3010 |
|
| 3011 |
/* Update per-program_point data. */ |
| 3012 |
per_point_data->m_enodes.safe_push (node); |
| 3013 |
|
| 3014 |
const enum point_kind node_pk = node->get_point ().get_kind (); |
| 3015 |
m_global_stats.m_num_nodes[node_pk]++; |
| 3016 |
per_fn_stats->m_num_nodes[node_pk]++; |
| 3017 |
per_cs_stats->m_num_nodes[node_pk]++; |
| 3018 |
|
| 3019 |
if (node_pk == PK_AFTER_SUPERNODE) |
| 3020 |
m_PK_AFTER_SUPERNODE_per_snode[point.get_supernode ()->m_index]++; |
| 3021 |
|
| 3022 |
if (logger) |
| 3023 |
{ |
| 3024 |
format f (false); |
| 3025 |
pretty_printer *pp = logger->get_printer (); |
| 3026 |
logger->log ("created EN: %i", node->m_index); |
| 3027 |
logger->start_log_line (); |
| 3028 |
pp_string (pp, "point: "); |
| 3029 |
point.print (pp, f); |
| 3030 |
logger->end_log_line (); |
| 3031 |
logger->start_log_line (); |
| 3032 |
pp_string (pp, "pruned_state: "); |
| 3033 |
pruned_state.dump_to_pp (m_ext_state, true, false, pp); |
| 3034 |
logger->end_log_line (); |
| 3035 |
} |
| 3036 |
|
| 3037 |
/* Add the new node to the worlist. */ |
| 3038 |
m_worklist.add_node (node); |
| 3039 |
return node; |
| 3040 |
} |
| 3041 |
|
| 3042 |
/* Add an exploded_edge from SRC to DEST, recording its association |
| 3043 |
with SEDGE (which may be NULL), and, if non-NULL, taking ownership |
| 3044 |
of CUSTOM_INFO. COULD_DO_WORK is used for detecting infinite loops. |
| 3045 |
Return the newly-created eedge. */ |
| 3046 |
|
| 3047 |
exploded_edge * |
| 3048 |
exploded_graph::add_edge (exploded_node *src, exploded_node *dest, |
| 3049 |
const superedge *sedge, bool could_do_work, |
| 3050 |
std::unique_ptr<custom_edge_info> custom_info) |
| 3051 |
{ |
| 3052 |
if (get_logger ()) |
| 3053 |
get_logger ()->log ("creating edge EN: %i -> EN: %i", |
| 3054 |
src->m_index, dest->m_index); |
| 3055 |
exploded_edge *e |
| 3056 |
= new exploded_edge (src, dest, sedge, could_do_work, |
| 3057 |
std::move (custom_info)); |
| 3058 |
digraph<eg_traits>::add_edge (e); |
| 3059 |
return e; |
| 3060 |
} |
| 3061 |
|
| 3062 |
/* Ensure that this graph has per-program_point-data for POINT; |
| 3063 |
borrow a pointer to it. */ |
| 3064 |
|
| 3065 |
per_program_point_data * |
| 3066 |
exploded_graph:: |
| 3067 |
get_or_create_per_program_point_data (const program_point &point) |
| 3068 |
{ |
| 3069 |
if (per_program_point_data **slot = m_per_point_data.get (&point)) |
| 3070 |
return *slot; |
| 3071 |
|
| 3072 |
per_program_point_data *per_point_data = new per_program_point_data (point); |
| 3073 |
m_per_point_data.put (&per_point_data->m_key, per_point_data); |
| 3074 |
return per_point_data; |
| 3075 |
} |
| 3076 |
|
| 3077 |
/* Get this graph's per-program-point-data for POINT if there is any, |
| 3078 |
otherwise NULL. */ |
| 3079 |
|
| 3080 |
per_program_point_data * |
| 3081 |
exploded_graph::get_per_program_point_data (const program_point &point) const |
| 3082 |
{ |
| 3083 |
if (per_program_point_data **slot |
| 3084 |
= const_cast <point_map_t &> (m_per_point_data).get (&point)) |
| 3085 |
return *slot; |
| 3086 |
|
| 3087 |
return NULL; |
| 3088 |
} |
| 3089 |
|
| 3090 |
/* Ensure that this graph has per-call_string-data for CS; |
| 3091 |
borrow a pointer to it. */ |
| 3092 |
|
| 3093 |
per_call_string_data * |
| 3094 |
exploded_graph::get_or_create_per_call_string_data (const call_string &cs) |
| 3095 |
{ |
| 3096 |
if (per_call_string_data **slot = m_per_call_string_data.get (&cs)) |
| 3097 |
return *slot; |
| 3098 |
|
| 3099 |
per_call_string_data *data = new per_call_string_data (cs, m_sg.num_nodes ()); |
| 3100 |
m_per_call_string_data.put (&data->m_key, |
| 3101 |
data); |
| 3102 |
return data; |
| 3103 |
} |
| 3104 |
|
| 3105 |
/* Ensure that this graph has per-function-data for FUN; |
| 3106 |
borrow a pointer to it. */ |
| 3107 |
|
| 3108 |
per_function_data * |
| 3109 |
exploded_graph::get_or_create_per_function_data (function *fun) |
| 3110 |
{ |
| 3111 |
if (per_function_data **slot = m_per_function_data.get (fun)) |
| 3112 |
return *slot; |
| 3113 |
|
| 3114 |
per_function_data *data = new per_function_data (); |
| 3115 |
m_per_function_data.put (fun, data); |
| 3116 |
return data; |
| 3117 |
} |
| 3118 |
|
| 3119 |
/* Get this graph's per-function-data for FUN if there is any, |
| 3120 |
otherwise NULL. */ |
| 3121 |
|
| 3122 |
per_function_data * |
| 3123 |
exploded_graph::get_per_function_data (function *fun) const |
| 3124 |
{ |
| 3125 |
if (per_function_data **slot |
| 3126 |
= const_cast <per_function_data_t &> (m_per_function_data).get (fun)) |
| 3127 |
return *slot; |
| 3128 |
|
| 3129 |
return NULL; |
| 3130 |
} |
| 3131 |
|
| 3132 |
/* Return true if FUN should be traversed directly, rather than only as |
| 3133 |
called via other functions. */ |
| 3134 |
|
| 3135 |
static bool |
| 3136 |
toplevel_function_p (const function &fun, logger *logger) |
| 3137 |
{ |
| 3138 |
/* Don't directly traverse into functions that have an "__analyzer_" |
| 3139 |
prefix. Doing so is useful for the analyzer test suite, allowing |
| 3140 |
us to have functions that are called in traversals, but not directly |
| 3141 |
explored, thus testing how the analyzer handles calls and returns. |
| 3142 |
With this, we can have DejaGnu directives that cover just the case |
| 3143 |
of where a function is called by another function, without generating |
| 3144 |
excess messages from the case of the first function being traversed |
| 3145 |
directly. */ |
| 3146 |
#define ANALYZER_PREFIX "__analyzer_" |
| 3147 |
if (!strncmp (IDENTIFIER_POINTER (DECL_NAME (fun.decl)), ANALYZER_PREFIX, |
| 3148 |
strlen (ANALYZER_PREFIX))) |
| 3149 |
{ |
| 3150 |
if (logger) |
| 3151 |
logger->log ("not traversing %qE (starts with %qs)", |
| 3152 |
fun.decl, ANALYZER_PREFIX); |
| 3153 |
return false; |
| 3154 |
} |
| 3155 |
|
| 3156 |
if (logger) |
| 3157 |
logger->log ("traversing %qE (all checks passed)", fun.decl); |
| 3158 |
|
| 3159 |
return true; |
| 3160 |
} |
| 3161 |
|
| 3162 |
/* Custom event for use by tainted_call_info when a callback field has been |
| 3163 |
marked with __attribute__((tainted_args)), for labelling the field. */ |
| 3164 |
|
| 3165 |
class tainted_args_field_custom_event : public custom_event |
| 3166 |
{ |
| 3167 |
public: |
| 3168 |
tainted_args_field_custom_event (tree field) |
| 3169 |
: custom_event (event_loc_info (DECL_SOURCE_LOCATION (field), NULL_TREE, 0)), |
| 3170 |
m_field (field) |
| 3171 |
{ |
| 3172 |
} |
| 3173 |
|
| 3174 |
void print_desc (pretty_printer &pp) const final override |
| 3175 |
{ |
| 3176 |
pp_printf (&pp, |
| 3177 |
"field %qE of %qT" |
| 3178 |
" is marked with %<__attribute__((tainted_args))%>", |
| 3179 |
m_field, DECL_CONTEXT (m_field)); |
| 3180 |
} |
| 3181 |
|
| 3182 |
private: |
| 3183 |
tree m_field; |
| 3184 |
}; |
| 3185 |
|
| 3186 |
/* Custom event for use by tainted_call_info when a callback field has been |
| 3187 |
marked with __attribute__((tainted_args)), for labelling the function used |
| 3188 |
in that callback. */ |
| 3189 |
|
| 3190 |
class tainted_args_callback_custom_event : public custom_event |
| 3191 |
{ |
| 3192 |
public: |
| 3193 |
tainted_args_callback_custom_event (const event_loc_info &loc_info, |
| 3194 |
tree field) |
| 3195 |
: custom_event (loc_info), |
| 3196 |
m_field (field) |
| 3197 |
{ |
| 3198 |
} |
| 3199 |
|
| 3200 |
void print_desc (pretty_printer &pp) const final override |
| 3201 |
{ |
| 3202 |
pp_printf (&pp, |
| 3203 |
"function %qE used as initializer for field %qE" |
| 3204 |
" marked with %<__attribute__((tainted_args))%>", |
| 3205 |
get_fndecl (), m_field); |
| 3206 |
} |
| 3207 |
|
| 3208 |
private: |
| 3209 |
tree m_field; |
| 3210 |
}; |
| 3211 |
|
| 3212 |
/* Custom edge info for use when adding a function used by a callback field |
| 3213 |
marked with '__attribute__((tainted_args))'. */ |
| 3214 |
|
| 3215 |
class tainted_args_call_info : public custom_edge_info |
| 3216 |
{ |
| 3217 |
public: |
| 3218 |
tainted_args_call_info (tree field, tree fndecl, location_t loc) |
| 3219 |
: m_field (field), m_fndecl (fndecl), m_loc (loc) |
| 3220 |
{} |
| 3221 |
|
| 3222 |
void print (pretty_printer *pp) const final override |
| 3223 |
{ |
| 3224 |
pp_string (pp, "call to tainted field"); |
| 3225 |
}; |
| 3226 |
|
| 3227 |
bool update_model (region_model *, |
| 3228 |
const exploded_edge *, |
| 3229 |
region_model_context *) const final override |
| 3230 |
{ |
| 3231 |
/* No-op. */ |
| 3232 |
return true; |
| 3233 |
} |
| 3234 |
|
| 3235 |
void add_events_to_path (checker_path *emission_path, |
| 3236 |
const exploded_edge &) const final override |
| 3237 |
{ |
| 3238 |
/* Show the field in the struct declaration, e.g. |
| 3239 |
"(1) field 'store' is marked with '__attribute__((tainted_args))'" */ |
| 3240 |
emission_path->add_event |
| 3241 |
(make_unique<tainted_args_field_custom_event> (m_field)); |
| 3242 |
|
| 3243 |
/* Show the callback in the initializer |
| 3244 |
e.g. |
| 3245 |
"(2) function 'gadget_dev_desc_UDC_store' used as initializer |
| 3246 |
for field 'store' marked with '__attribute__((tainted_args))'". */ |
| 3247 |
emission_path->add_event |
| 3248 |
(make_unique<tainted_args_callback_custom_event> |
| 3249 |
(event_loc_info (m_loc, m_fndecl, 0), |
| 3250 |
m_field)); |
| 3251 |
} |
| 3252 |
|
| 3253 |
private: |
| 3254 |
tree m_field; |
| 3255 |
tree m_fndecl; |
| 3256 |
location_t m_loc; |
| 3257 |
}; |
| 3258 |
|
| 3259 |
/* Given an initializer at LOC for FIELD marked with |
| 3260 |
'__attribute__((tainted_args))' initialized with FNDECL, add an |
| 3261 |
entrypoint to FNDECL to EG (and to its worklist) where the params to |
| 3262 |
FNDECL are marked as tainted. */ |
| 3263 |
|
| 3264 |
static void |
| 3265 |
add_tainted_args_callback (exploded_graph *eg, tree field, tree fndecl, |
| 3266 |
location_t loc) |
| 3267 |
{ |
| 3268 |
logger *logger = eg->get_logger (); |
| 3269 |
|
| 3270 |
LOG_SCOPE (logger); |
| 3271 |
|
| 3272 |
if (!gimple_has_body_p (fndecl)) |
| 3273 |
return; |
| 3274 |
|
| 3275 |
const extrinsic_state &ext_state = eg->get_ext_state (); |
| 3276 |
|
| 3277 |
function *fun = DECL_STRUCT_FUNCTION (fndecl); |
| 3278 |
gcc_assert (fun); |
| 3279 |
|
| 3280 |
program_point point |
| 3281 |
= program_point::from_function_entry (*ext_state.get_model_manager (), |
| 3282 |
eg->get_supergraph (), *fun); |
| 3283 |
program_state state (ext_state); |
| 3284 |
state.push_frame (ext_state, *fun); |
| 3285 |
|
| 3286 |
if (!mark_params_as_tainted (&state, fndecl, ext_state)) |
| 3287 |
return; |
| 3288 |
|
| 3289 |
if (!state.m_valid) |
| 3290 |
return; |
| 3291 |
|
| 3292 |
exploded_node *enode = eg->get_or_create_node (point, state, NULL); |
| 3293 |
if (logger) |
| 3294 |
{ |
| 3295 |
if (enode) |
| 3296 |
logger->log ("created EN %i for tainted_args %qE entrypoint", |
| 3297 |
enode->m_index, fndecl); |
| 3298 |
else |
| 3299 |
{ |
| 3300 |
logger->log ("did not create enode for tainted_args %qE entrypoint", |
| 3301 |
fndecl); |
| 3302 |
return; |
| 3303 |
} |
| 3304 |
} |
| 3305 |
|
| 3306 |
eg->add_edge (eg->get_origin (), enode, NULL, false, |
| 3307 |
make_unique<tainted_args_call_info> (field, fndecl, loc)); |
| 3308 |
} |
| 3309 |
|
| 3310 |
/* Callback for walk_tree for finding callbacks within initializers; |
| 3311 |
ensure that any callback initializer where the corresponding field is |
| 3312 |
marked with '__attribute__((tainted_args))' is treated as an entrypoint |
| 3313 |
to the analysis, special-casing that the inputs to the callback are |
| 3314 |
untrustworthy. */ |
| 3315 |
|
| 3316 |
static tree |
| 3317 |
add_any_callbacks (tree *tp, int *, void *data) |
| 3318 |
{ |
| 3319 |
exploded_graph *eg = (exploded_graph *)data; |
| 3320 |
if (TREE_CODE (*tp) == CONSTRUCTOR) |
| 3321 |
{ |
| 3322 |
/* Find fields with the "tainted_args" attribute. |
| 3323 |
walk_tree only walks the values, not the index values; |
| 3324 |
look at the index values. */ |
| 3325 |
unsigned HOST_WIDE_INT idx; |
| 3326 |
constructor_elt *ce; |
| 3327 |
|
| 3328 |
for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (*tp), idx, &ce); |
| 3329 |
idx++) |
| 3330 |
if (ce->index && TREE_CODE (ce->index) == FIELD_DECL) |
| 3331 |
if (lookup_attribute ("tainted_args", DECL_ATTRIBUTES (ce->index))) |
| 3332 |
{ |
| 3333 |
tree value = ce->value; |
| 3334 |
if (TREE_CODE (value) == ADDR_EXPR |
| 3335 |
&& TREE_CODE (TREE_OPERAND (value, 0)) == FUNCTION_DECL) |
| 3336 |
add_tainted_args_callback (eg, ce->index, |
| 3337 |
TREE_OPERAND (value, 0), |
| 3338 |
EXPR_LOCATION (value)); |
| 3339 |
} |
| 3340 |
} |
| 3341 |
|
| 3342 |
return NULL_TREE; |
| 3343 |
} |
| 3344 |
|
| 3345 |
/* Add initial nodes to EG, with entrypoints for externally-callable |
| 3346 |
functions. */ |
| 3347 |
|
| 3348 |
void |
| 3349 |
exploded_graph::build_initial_worklist () |
| 3350 |
{ |
| 3351 |
logger * const logger = get_logger (); |
| 3352 |
LOG_SCOPE (logger); |
| 3353 |
|
| 3354 |
cgraph_node *node; |
| 3355 |
FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node) |
| 3356 |
{ |
| 3357 |
function *fun = node->get_fun (); |
| 3358 |
gcc_assert (fun); |
| 3359 |
if (!toplevel_function_p (*fun, logger)) |
| 3360 |
continue; |
| 3361 |
exploded_node *enode = add_function_entry (*fun); |
| 3362 |
if (logger) |
| 3363 |
{ |
| 3364 |
if (enode) |
| 3365 |
logger->log ("created EN %i for %qE entrypoint", |
| 3366 |
enode->m_index, fun->decl); |
| 3367 |
else |
| 3368 |
logger->log ("did not create enode for %qE entrypoint", fun->decl); |
| 3369 |
} |
| 3370 |
} |
| 3371 |
|
| 3372 |
/* Find callbacks that are reachable from global initializers. */ |
| 3373 |
varpool_node *vpnode; |
| 3374 |
FOR_EACH_VARIABLE (vpnode) |
| 3375 |
{ |
| 3376 |
tree decl = vpnode->decl; |
| 3377 |
tree init = DECL_INITIAL (decl); |
| 3378 |
if (!init) |
| 3379 |
continue; |
| 3380 |
walk_tree (&init, add_any_callbacks, this, NULL); |
| 3381 |
} |
| 3382 |
} |
| 3383 |
|
| 3384 |
/* The main loop of the analysis. |
| 3385 |
Take freshly-created exploded_nodes from the worklist, calling |
| 3386 |
process_node on them to explore the <point, state> graph. |
| 3387 |
Add edges to their successors, potentially creating new successors |
| 3388 |
(which are also added to the worklist). */ |
| 3389 |
|
| 3390 |
void |
| 3391 |
exploded_graph::process_worklist () |
| 3392 |
{ |
| 3393 |
logger * const logger = get_logger (); |
| 3394 |
LOG_SCOPE (logger); |
| 3395 |
auto_timevar tv (TV_ANALYZER_WORKLIST); |
| 3396 |
|
| 3397 |
while (m_worklist.length () > 0) |
| 3398 |
{ |
| 3399 |
exploded_node *node = m_worklist.take_next (); |
| 3400 |
gcc_assert (node->get_status () == exploded_node::STATUS_WORKLIST); |
| 3401 |
gcc_assert (node->m_succs.length () == 0 |
| 3402 |
|| node == m_origin); |
| 3403 |
|
| 3404 |
if (logger) |
| 3405 |
logger->log ("next to process: EN: %i", node->m_index); |
| 3406 |
|
| 3407 |
/* If we have a run of nodes that are before-supernode, try merging and |
| 3408 |
processing them together, rather than pairwise or individually. */ |
| 3409 |
if (flag_analyzer_state_merge && node != m_origin) |
| 3410 |
if (maybe_process_run_of_before_supernode_enodes (node)) |
| 3411 |
goto handle_limit; |
| 3412 |
|
| 3413 |
/* Avoid exponential explosions of nodes by attempting to merge |
| 3414 |
nodes that are at the same program point and which have |
| 3415 |
sufficiently similar state. */ |
| 3416 |
if (flag_analyzer_state_merge && node != m_origin) |
| 3417 |
if (exploded_node *node_2 = m_worklist.peek_next ()) |
| 3418 |
{ |
| 3419 |
gcc_assert (node_2->get_status () |
| 3420 |
== exploded_node::STATUS_WORKLIST); |
| 3421 |
gcc_assert (node->m_succs.length () == 0); |
| 3422 |
gcc_assert (node_2->m_succs.length () == 0); |
| 3423 |
|
| 3424 |
gcc_assert (node != node_2); |
| 3425 |
|
| 3426 |
if (logger) |
| 3427 |
logger->log ("peek worklist: EN: %i", node_2->m_index); |
| 3428 |
|
| 3429 |
if (node->get_point () == node_2->get_point ()) |
| 3430 |
{ |
| 3431 |
const program_point &point = node->get_point (); |
| 3432 |
if (logger) |
| 3433 |
{ |
| 3434 |
format f (false); |
| 3435 |
pretty_printer *pp = logger->get_printer (); |
| 3436 |
logger->start_log_line (); |
| 3437 |
logger->log_partial |
| 3438 |
("got potential merge EN: %i and EN: %i at ", |
| 3439 |
node->m_index, node_2->m_index); |
| 3440 |
point.print (pp, f); |
| 3441 |
logger->end_log_line (); |
| 3442 |
} |
| 3443 |
const program_state &state = node->get_state (); |
| 3444 |
const program_state &state_2 = node_2->get_state (); |
| 3445 |
|
| 3446 |
/* They shouldn't be equal, or we wouldn't have two |
| 3447 |
separate nodes. */ |
| 3448 |
gcc_assert (state != state_2); |
| 3449 |
|
| 3450 |
program_state merged_state (m_ext_state); |
| 3451 |
if (state.can_merge_with_p (state_2, m_ext_state, |
| 3452 |
point, &merged_state)) |
| 3453 |
{ |
| 3454 |
if (logger) |
| 3455 |
logger->log ("merging EN: %i and EN: %i", |
| 3456 |
node->m_index, node_2->m_index); |
| 3457 |
|
| 3458 |
if (merged_state == state) |
| 3459 |
{ |
| 3460 |
/* Then merge node_2 into node by adding an edge. */ |
| 3461 |
add_edge (node_2, node, NULL, false); |
| 3462 |
|
| 3463 |
/* Remove node_2 from the worklist. */ |
| 3464 |
m_worklist.take_next (); |
| 3465 |
node_2->set_status (exploded_node::STATUS_MERGER); |
| 3466 |
|
| 3467 |
/* Continue processing "node" below. */ |
| 3468 |
} |
| 3469 |
else if (merged_state == state_2) |
| 3470 |
{ |
| 3471 |
/* Then merge node into node_2, and leave node_2 |
| 3472 |
in the worklist, to be processed on the next |
| 3473 |
iteration. */ |
| 3474 |
add_edge (node, node_2, NULL, false); |
| 3475 |
node->set_status (exploded_node::STATUS_MERGER); |
| 3476 |
continue; |
| 3477 |
} |
| 3478 |
else |
| 3479 |
{ |
| 3480 |
/* We have a merged state that differs from |
| 3481 |
both state and state_2. */ |
| 3482 |
|
| 3483 |
/* Remove node_2 from the worklist. */ |
| 3484 |
m_worklist.take_next (); |
| 3485 |
|
| 3486 |
/* Create (or get) an exploded node for the merged |
| 3487 |
states, adding to the worklist. */ |
| 3488 |
exploded_node *merged_enode |
| 3489 |
= get_or_create_node (node->get_point (), |
| 3490 |
merged_state, node); |
| 3491 |
if (merged_enode == NULL) |
| 3492 |
continue; |
| 3493 |
|
| 3494 |
if (logger) |
| 3495 |
logger->log ("merged EN: %i and EN: %i into EN: %i", |
| 3496 |
node->m_index, node_2->m_index, |
| 3497 |
merged_enode->m_index); |
| 3498 |
|
| 3499 |
/* "node" and "node_2" have both now been removed |
| 3500 |
from the worklist; we should not process them. |
| 3501 |
|
| 3502 |
"merged_enode" may be a new node; if so it will be |
| 3503 |
processed in a subsequent iteration. |
| 3504 |
Alternatively, "merged_enode" could be an existing |
| 3505 |
node; one way the latter can |
| 3506 |
happen is if we end up merging a succession of |
| 3507 |
similar nodes into one. */ |
| 3508 |
|
| 3509 |
/* If merged_node is one of the two we were merging, |
| 3510 |
add it back to the worklist to ensure it gets |
| 3511 |
processed. |
| 3512 |
|
| 3513 |
Add edges from the merged nodes to it (but not a |
| 3514 |
self-edge). */ |
| 3515 |
if (merged_enode == node) |
| 3516 |
m_worklist.add_node (merged_enode); |
| 3517 |
else |
| 3518 |
{ |
| 3519 |
add_edge (node, merged_enode, NULL, false); |
| 3520 |
node->set_status (exploded_node::STATUS_MERGER); |
| 3521 |
} |
| 3522 |
|
| 3523 |
if (merged_enode == node_2) |
| 3524 |
m_worklist.add_node (merged_enode); |
| 3525 |
else |
| 3526 |
{ |
| 3527 |
add_edge (node_2, merged_enode, NULL, false); |
| 3528 |
node_2->set_status (exploded_node::STATUS_MERGER); |
| 3529 |
} |
| 3530 |
|
| 3531 |
continue; |
| 3532 |
} |
| 3533 |
} |
| 3534 |
|
| 3535 |
/* TODO: should we attempt more than two nodes, |
| 3536 |
or just do pairs of nodes? (and hope that we get |
| 3537 |
a cascade of mergers). */ |
| 3538 |
} |
| 3539 |
} |
| 3540 |
|
| 3541 |
process_node (node); |
| 3542 |
|
| 3543 |
handle_limit: |
| 3544 |
/* Impose a hard limit on the number of exploded nodes, to ensure |
| 3545 |
that the analysis terminates in the face of pathological state |
| 3546 |
explosion (or bugs). |
| 3547 |
|
| 3548 |
Specifically, the limit is on the number of PK_AFTER_SUPERNODE |
| 3549 |
exploded nodes, looking at supernode exit events. |
| 3550 |
|
| 3551 |
We use exit rather than entry since there can be multiple |
| 3552 |
entry ENs, one per phi; the number of PK_AFTER_SUPERNODE ought |
| 3553 |
to be equivalent to the number of supernodes multiplied by the |
| 3554 |
number of states. */ |
| 3555 |
const int limit = m_sg.num_nodes () * param_analyzer_bb_explosion_factor; |
| 3556 |
if (m_global_stats.m_num_nodes[PK_AFTER_SUPERNODE] > limit) |
| 3557 |
{ |
| 3558 |
if (logger) |
| 3559 |
logger->log ("bailing out; too many nodes"); |
| 3560 |
warning_at (node->get_point ().get_location (), |
| 3561 |
OPT_Wanalyzer_too_complex, |
| 3562 |
"analysis bailed out early" |
| 3563 |
" (%i 'after-snode' enodes; %i enodes)", |
| 3564 |
m_global_stats.m_num_nodes[PK_AFTER_SUPERNODE], |
| 3565 |
m_nodes.length ()); |
| 3566 |
return; |
| 3567 |
} |
| 3568 |
} |
| 3569 |
} |
| 3570 |
|
| 3571 |
/* Attempt to process a consecutive run of sufficiently-similar nodes in |
| 3572 |
the worklist at a CFG join-point (having already popped ENODE from the |
| 3573 |
head of the worklist). |
| 3574 |
|
| 3575 |
If ENODE's point is of the form (before-supernode, SNODE) and the next |
| 3576 |
nodes in the worklist are a consecutive run of enodes of the same form, |
| 3577 |
for the same supernode as ENODE (but potentially from different in-edges), |
| 3578 |
process them all together, setting their status to STATUS_BULK_MERGED, |
| 3579 |
and return true. |
| 3580 |
Otherwise, return false, in which case ENODE must be processed in the |
| 3581 |
normal way. |
| 3582 |
|
| 3583 |
When processing them all together, generate successor states based |
| 3584 |
on phi nodes for the appropriate CFG edges, and then attempt to merge |
| 3585 |
these states into a minimal set of merged successor states, partitioning |
| 3586 |
the inputs by merged successor state. |
| 3587 |
|
| 3588 |
Create new exploded nodes for all of the merged states, and add edges |
| 3589 |
connecting the input enodes to the corresponding merger exploded nodes. |
| 3590 |
|
| 3591 |
We hope we have a much smaller number of merged successor states |
| 3592 |
compared to the number of input enodes - ideally just one, |
| 3593 |
if all successor states can be merged. |
| 3594 |
|
| 3595 |
Processing and merging many together as one operation rather than as |
| 3596 |
pairs avoids scaling issues where per-pair mergers could bloat the |
| 3597 |
graph with merger nodes (especially so after switch statements). */ |
| 3598 |
|
| 3599 |
bool |
| 3600 |
exploded_graph:: |
| 3601 |
maybe_process_run_of_before_supernode_enodes (exploded_node *enode) |
| 3602 |
{ |
| 3603 |
/* A struct for tracking per-input state. */ |
| 3604 |
struct item |
| 3605 |
{ |
| 3606 |
item (exploded_node *input_enode) |
| 3607 |
: m_input_enode (input_enode), |
| 3608 |
m_processed_state (input_enode->get_state ()), |
| 3609 |
m_merger_idx (-1) |
| 3610 |
{} |
| 3611 |
|
| 3612 |
exploded_node *m_input_enode; |
| 3613 |
program_state m_processed_state; |
| 3614 |
int m_merger_idx; |
| 3615 |
}; |
| 3616 |
|
| 3617 |
gcc_assert (enode->get_status () == exploded_node::STATUS_WORKLIST); |
| 3618 |
gcc_assert (enode->m_succs.length () == 0); |
| 3619 |
|
| 3620 |
const program_point &point = enode->get_point (); |
| 3621 |
|
| 3622 |
if (point.get_kind () != PK_BEFORE_SUPERNODE) |
| 3623 |
return false; |
| 3624 |
|
| 3625 |
const supernode *snode = point.get_supernode (); |
| 3626 |
|
| 3627 |
logger * const logger = get_logger (); |
| 3628 |
LOG_SCOPE (logger); |
| 3629 |
|
| 3630 |
/* Find a run of enodes in the worklist that are before the same supernode, |
| 3631 |
but potentially from different in-edges. */ |
| 3632 |
auto_vec <exploded_node *> enodes; |
| 3633 |
enodes.safe_push (enode); |
| 3634 |
while (exploded_node *enode_2 = m_worklist.peek_next ()) |
| 3635 |
{ |
| 3636 |
gcc_assert (enode_2->get_status () |
| 3637 |
== exploded_node::STATUS_WORKLIST); |
| 3638 |
gcc_assert (enode_2->m_succs.length () == 0); |
| 3639 |
|
| 3640 |
const program_point &point_2 = enode_2->get_point (); |
| 3641 |
|
| 3642 |
if (point_2.get_kind () == PK_BEFORE_SUPERNODE |
| 3643 |
&& point_2.get_supernode () == snode |
| 3644 |
&& &point_2.get_call_string () == &point.get_call_string ()) |
| 3645 |
{ |
| 3646 |
enodes.safe_push (enode_2); |
| 3647 |
m_worklist.take_next (); |
| 3648 |
} |
| 3649 |
else |
| 3650 |
break; |
| 3651 |
} |
| 3652 |
|
| 3653 |
/* If the only node is ENODE, then give up. */ |
| 3654 |
if (enodes.length () == 1) |
| 3655 |
return false; |
| 3656 |
|
| 3657 |
if (logger) |
| 3658 |
logger->log ("got run of %i enodes for SN: %i", |
| 3659 |
enodes.length (), snode->m_index); |
| 3660 |
|
| 3661 |
/* All of these enodes have a shared successor point (even if they |
| 3662 |
were for different in-edges). */ |
| 3663 |
program_point next_point (point.get_next ()); |
| 3664 |
|
| 3665 |
/* Calculate the successor state for each enode in enodes. */ |
| 3666 |
auto_delete_vec<item> items (enodes.length ()); |
| 3667 |
unsigned i; |
| 3668 |
exploded_node *iter_enode; |
| 3669 |
FOR_EACH_VEC_ELT (enodes, i, iter_enode) |
| 3670 |
{ |
| 3671 |
item *it = new item (iter_enode); |
| 3672 |
items.quick_push (it); |
| 3673 |
const program_state &state = iter_enode->get_state (); |
| 3674 |
program_state *next_state = &it->m_processed_state; |
| 3675 |
next_state->validate (m_ext_state); |
| 3676 |
const program_point &iter_point = iter_enode->get_point (); |
| 3677 |
if (const superedge *iter_sedge = iter_point.get_from_edge ()) |
| 3678 |
{ |
| 3679 |
uncertainty_t uncertainty; |
| 3680 |
impl_region_model_context ctxt (*this, iter_enode, |
| 3681 |
&state, next_state, |
| 3682 |
&uncertainty, NULL, NULL); |
| 3683 |
const cfg_superedge *last_cfg_superedge |
| 3684 |
= iter_sedge->dyn_cast_cfg_superedge (); |
| 3685 |
if (last_cfg_superedge) |
| 3686 |
next_state->m_region_model->update_for_phis |
| 3687 |
(snode, last_cfg_superedge, &ctxt); |
| 3688 |
} |
| 3689 |
next_state->validate (m_ext_state); |
| 3690 |
} |
| 3691 |
|
| 3692 |
/* Attempt to partition the items into a set of merged states. |
| 3693 |
We hope we have a much smaller number of merged states |
| 3694 |
compared to the number of input enodes - ideally just one, |
| 3695 |
if all can be merged. */ |
| 3696 |
auto_delete_vec <program_state> merged_states; |
| 3697 |
auto_vec<item *> first_item_for_each_merged_state; |
| 3698 |
item *it; |
| 3699 |
FOR_EACH_VEC_ELT (items, i, it) |
| 3700 |
{ |
| 3701 |
const program_state &it_state = it->m_processed_state; |
| 3702 |
program_state *merged_state; |
| 3703 |
unsigned iter_merger_idx; |
| 3704 |
FOR_EACH_VEC_ELT (merged_states, iter_merger_idx, merged_state) |
| 3705 |
{ |
| 3706 |
merged_state->validate (m_ext_state); |
| 3707 |
program_state merge (m_ext_state); |
| 3708 |
if (it_state.can_merge_with_p (*merged_state, m_ext_state, |
| 3709 |
next_point, &merge)) |
| 3710 |
{ |
| 3711 |
*merged_state = merge; |
| 3712 |
merged_state->validate (m_ext_state); |
| 3713 |
it->m_merger_idx = iter_merger_idx; |
| 3714 |
if (logger) |
| 3715 |
logger->log ("reusing merger state %i for item %i (EN: %i)", |
| 3716 |
it->m_merger_idx, i, it->m_input_enode->m_index); |
| 3717 |
goto got_merger; |
| 3718 |
} |
| 3719 |
} |
| 3720 |
/* If it couldn't be merged with any existing merged_states, |
| 3721 |
create a new one. */ |
| 3722 |
if (it->m_merger_idx == -1) |
| 3723 |
{ |
| 3724 |
it->m_merger_idx = merged_states.length (); |
| 3725 |
merged_states.safe_push (new program_state (it_state)); |
| 3726 |
first_item_for_each_merged_state.safe_push (it); |
| 3727 |
if (logger) |
| 3728 |
logger->log ("using new merger state %i for item %i (EN: %i)", |
| 3729 |
it->m_merger_idx, i, it->m_input_enode->m_index); |
| 3730 |
} |
| 3731 |
got_merger: |
| 3732 |
gcc_assert (it->m_merger_idx >= 0); |
| 3733 |
gcc_assert ((unsigned)it->m_merger_idx < merged_states.length ()); |
| 3734 |
} |
| 3735 |
|
| 3736 |
/* Create merger nodes. */ |
| 3737 |
auto_vec<exploded_node *> next_enodes (merged_states.length ()); |
| 3738 |
program_state *merged_state; |
| 3739 |
FOR_EACH_VEC_ELT (merged_states, i, merged_state) |
| 3740 |
{ |
| 3741 |
exploded_node *src_enode |
| 3742 |
= first_item_for_each_merged_state[i]->m_input_enode; |
| 3743 |
exploded_node *next |
| 3744 |
= get_or_create_node (next_point, *merged_state, src_enode); |
| 3745 |
/* "next" could be NULL; we handle that when adding the edges below. */ |
| 3746 |
next_enodes.quick_push (next); |
| 3747 |
if (logger) |
| 3748 |
{ |
| 3749 |
if (next) |
| 3750 |
logger->log ("using EN: %i for merger state %i", next->m_index, i); |
| 3751 |
else |
| 3752 |
logger->log ("using NULL enode for merger state %i", i); |
| 3753 |
} |
| 3754 |
} |
| 3755 |
|
| 3756 |
/* Create edges from each input enode to the appropriate successor enode. |
| 3757 |
Update the status of the now-processed input enodes. */ |
| 3758 |
FOR_EACH_VEC_ELT (items, i, it) |
| 3759 |
{ |
| 3760 |
exploded_node *next = next_enodes[it->m_merger_idx]; |
| 3761 |
if (next) |
| 3762 |
add_edge (it->m_input_enode, next, NULL, |
| 3763 |
false); /* no "work" is done during merger. */ |
| 3764 |
it->m_input_enode->set_status (exploded_node::STATUS_BULK_MERGED); |
| 3765 |
} |
| 3766 |
|
| 3767 |
if (logger) |
| 3768 |
logger->log ("merged %i in-enodes into %i out-enode(s) at SN: %i", |
| 3769 |
items.length (), merged_states.length (), snode->m_index); |
| 3770 |
|
| 3771 |
return true; |
| 3772 |
} |
| 3773 |
|
| 3774 |
/* Return true if STMT must appear at the start of its exploded node, and |
| 3775 |
thus we can't consolidate its effects within a run of other statements, |
| 3776 |
where PREV_STMT was the previous statement. */ |
| 3777 |
|
| 3778 |
static bool |
| 3779 |
stmt_requires_new_enode_p (const gimple *stmt, |
| 3780 |
const gimple *prev_stmt) |
| 3781 |
{ |
| 3782 |
if (const gcall *call = dyn_cast <const gcall *> (stmt)) |
| 3783 |
{ |
| 3784 |
/* Stop consolidating at calls to |
| 3785 |
"__analyzer_dump_exploded_nodes", so they always appear at the |
| 3786 |
start of an exploded_node. */ |
| 3787 |
if (is_special_named_call_p (call, "__analyzer_dump_exploded_nodes", |
| 3788 |
1)) |
| 3789 |
return true; |
| 3790 |
|
| 3791 |
/* sm-signal.cc injects an additional custom eedge at "signal" calls |
| 3792 |
from the registration enode to the handler enode, separate from the |
| 3793 |
regular next state, which defeats the "detect state change" logic |
| 3794 |
in process_node. Work around this via special-casing, to ensure |
| 3795 |
we split the enode immediately before any "signal" call. */ |
| 3796 |
if (is_special_named_call_p (call, "signal", 2, true)) |
| 3797 |
return true; |
| 3798 |
} |
| 3799 |
|
| 3800 |
/* If we had a PREV_STMT with an unknown location, and this stmt |
| 3801 |
has a known location, then if a state change happens here, it |
| 3802 |
could be consolidated into PREV_STMT, giving us an event with |
| 3803 |
no location. Ensure that STMT gets its own exploded_node to |
| 3804 |
avoid this. */ |
| 3805 |
if (get_pure_location (prev_stmt->location) == UNKNOWN_LOCATION |
| 3806 |
&& get_pure_location (stmt->location) != UNKNOWN_LOCATION) |
| 3807 |
return true; |
| 3808 |
|
| 3809 |
return false; |
| 3810 |
} |
| 3811 |
|
| 3812 |
/* Return true if OLD_STATE and NEW_STATE are sufficiently different that |
| 3813 |
we should split enodes and create an exploded_edge separating them |
| 3814 |
(which makes it easier to identify state changes of intereset when |
| 3815 |
constructing checker_paths). */ |
| 3816 |
|
| 3817 |
static bool |
| 3818 |
state_change_requires_new_enode_p (const program_state &old_state, |
| 3819 |
const program_state &new_state) |
| 3820 |
{ |
| 3821 |
/* Changes in dynamic extents signify creations of heap/alloca regions |
| 3822 |
and resizings of heap regions; likely to be of interest in |
| 3823 |
diagnostic paths. */ |
| 3824 |
if (old_state.m_region_model->get_dynamic_extents () |
| 3825 |
!= new_state.m_region_model->get_dynamic_extents ()) |
| 3826 |
return true; |
| 3827 |
|
| 3828 |
/* Changes in sm-state are of interest. */ |
| 3829 |
int sm_idx; |
| 3830 |
sm_state_map *smap; |
| 3831 |
FOR_EACH_VEC_ELT (old_state.m_checker_states, sm_idx, smap) |
| 3832 |
{ |
| 3833 |
const sm_state_map *old_smap = old_state.m_checker_states[sm_idx]; |
| 3834 |
const sm_state_map *new_smap = new_state.m_checker_states[sm_idx]; |
| 3835 |
if (*old_smap != *new_smap) |
| 3836 |
return true; |
| 3837 |
} |
| 3838 |
|
| 3839 |
return false; |
| 3840 |
} |
| 3841 |
|
| 3842 |
/* Create enodes and eedges for the function calls that doesn't have an |
| 3843 |
underlying call superedge. |
| 3844 |
|
| 3845 |
Such case occurs when GCC's middle end didn't know which function to |
| 3846 |
call but the analyzer does (with the help of current state). |
| 3847 |
|
| 3848 |
Some example such calls are dynamically dispatched calls to virtual |
| 3849 |
functions or calls that happen via function pointer. */ |
| 3850 |
|
| 3851 |
bool |
| 3852 |
exploded_graph::maybe_create_dynamic_call (const gcall *call, |
| 3853 |
tree fn_decl, |
| 3854 |
exploded_node *node, |
| 3855 |
program_state next_state, |
| 3856 |
program_point &next_point, |
| 3857 |
uncertainty_t *uncertainty, |
| 3858 |
logger *logger) |
| 3859 |
{ |
| 3860 |
LOG_FUNC (logger); |
| 3861 |
|
| 3862 |
const program_point *this_point = &node->get_point (); |
| 3863 |
function *fun = DECL_STRUCT_FUNCTION (fn_decl); |
| 3864 |
if (fun) |
| 3865 |
{ |
| 3866 |
const supergraph &sg = this->get_supergraph (); |
| 3867 |
supernode *sn_entry = sg.get_node_for_function_entry (*fun); |
| 3868 |
supernode *sn_exit = sg.get_node_for_function_exit (*fun); |
| 3869 |
|
| 3870 |
program_point new_point |
| 3871 |
= program_point::before_supernode (sn_entry, |
| 3872 |
NULL, |
| 3873 |
this_point->get_call_string ()); |
| 3874 |
|
| 3875 |
new_point.push_to_call_stack (sn_exit, |
| 3876 |
next_point.get_supernode()); |
| 3877 |
|
| 3878 |
/* Impose a maximum recursion depth and don't analyze paths |
| 3879 |
that exceed it further. |
| 3880 |
This is something of a blunt workaround, but it only |
| 3881 |
applies to recursion (and mutual recursion), not to |
| 3882 |
general call stacks. */ |
| 3883 |
if (new_point.get_call_string ().calc_recursion_depth () |
| 3884 |
> param_analyzer_max_recursion_depth) |
| 3885 |
{ |
| 3886 |
if (logger) |
| 3887 |
logger->log ("rejecting call edge: recursion limit exceeded"); |
| 3888 |
return false; |
| 3889 |
} |
| 3890 |
|
| 3891 |
next_state.push_call (*this, node, call, uncertainty); |
| 3892 |
|
| 3893 |
if (next_state.m_valid) |
| 3894 |
{ |
| 3895 |
if (logger) |
| 3896 |
logger->log ("Discovered call to %s [SN: %i -> SN: %i]", |
| 3897 |
function_name(fun), |
| 3898 |
this_point->get_supernode ()->m_index, |
| 3899 |
sn_entry->m_index); |
| 3900 |
|
| 3901 |
exploded_node *enode = get_or_create_node (new_point, |
| 3902 |
next_state, |
| 3903 |
node); |
| 3904 |
if (enode) |
| 3905 |
add_edge (node,enode, NULL, |
| 3906 |
false, /* No work is done by the call itself. */ |
| 3907 |
make_unique<dynamic_call_info_t> (call)); |
| 3908 |
return true; |
| 3909 |
} |
| 3910 |
} |
| 3911 |
return false; |
| 3912 |
} |
| 3913 |
|
| 3914 |
/* Subclass of path_context for use within exploded_graph::process_node, |
| 3915 |
so that we can split states e.g. at "realloc" calls. */ |
| 3916 |
|
| 3917 |
class impl_path_context : public path_context |
| 3918 |
{ |
| 3919 |
public: |
| 3920 |
impl_path_context (const program_state *cur_state, |
| 3921 |
logger *logger) |
| 3922 |
: m_cur_state (cur_state), |
| 3923 |
m_logger (logger), |
| 3924 |
m_terminate_path (false) |
| 3925 |
{ |
| 3926 |
} |
| 3927 |
|
| 3928 |
bool bifurcation_p () const |
| 3929 |
{ |
| 3930 |
return m_custom_eedge_infos.length () > 0; |
| 3931 |
} |
| 3932 |
|
| 3933 |
const program_state &get_state_at_bifurcation () const |
| 3934 |
{ |
| 3935 |
gcc_assert (m_state_at_bifurcation); |
| 3936 |
return *m_state_at_bifurcation; |
| 3937 |
} |
| 3938 |
|
| 3939 |
void |
| 3940 |
bifurcate (std::unique_ptr<custom_edge_info> info) final override |
| 3941 |
{ |
| 3942 |
if (m_logger) |
| 3943 |
m_logger->log ("bifurcating path"); |
| 3944 |
|
| 3945 |
if (m_state_at_bifurcation) |
| 3946 |
/* Verify that the state at bifurcation is consistent when we |
| 3947 |
split into multiple out-edges. */ |
| 3948 |
gcc_assert (*m_state_at_bifurcation == *m_cur_state); |
| 3949 |
else |
| 3950 |
/* Take a copy of the cur_state at the moment when bifurcation |
| 3951 |
happens. */ |
| 3952 |
m_state_at_bifurcation |
| 3953 |
= std::unique_ptr<program_state> (new program_state (*m_cur_state)); |
| 3954 |
|
| 3955 |
/* Take ownership of INFO. */ |
| 3956 |
m_custom_eedge_infos.safe_push (info.release ()); |
| 3957 |
} |
| 3958 |
|
| 3959 |
void terminate_path () final override |
| 3960 |
{ |
| 3961 |
if (m_logger) |
| 3962 |
m_logger->log ("terminating path"); |
| 3963 |
m_terminate_path = true; |
| 3964 |
} |
| 3965 |
|
| 3966 |
bool terminate_path_p () const final override |
| 3967 |
{ |
| 3968 |
return m_terminate_path; |
| 3969 |
} |
| 3970 |
|
| 3971 |
const vec<custom_edge_info *> & get_custom_eedge_infos () |
| 3972 |
{ |
| 3973 |
return m_custom_eedge_infos; |
| 3974 |
} |
| 3975 |
|
| 3976 |
private: |
| 3977 |
const program_state *m_cur_state; |
| 3978 |
|
| 3979 |
logger *m_logger; |
| 3980 |
|
| 3981 |
/* Lazily-created copy of the state before the split. */ |
| 3982 |
std::unique_ptr<program_state> m_state_at_bifurcation; |
| 3983 |
|
| 3984 |
auto_vec <custom_edge_info *> m_custom_eedge_infos; |
| 3985 |
|
| 3986 |
bool m_terminate_path; |
| 3987 |
}; |
| 3988 |
|
| 3989 |
/* A subclass of pending_diagnostic for complaining about jumps through NULL |
| 3990 |
function pointers. */ |
| 3991 |
|
| 3992 |
class jump_through_null : public pending_diagnostic_subclass<jump_through_null> |
| 3993 |
{ |
| 3994 |
public: |
| 3995 |
jump_through_null (const gcall *call) |
| 3996 |
: m_call (call) |
| 3997 |
{} |
| 3998 |
|
| 3999 |
const char *get_kind () const final override |
| 4000 |
{ |
| 4001 |
return "jump_through_null"; |
| 4002 |
} |
| 4003 |
|
| 4004 |
bool operator== (const jump_through_null &other) const |
| 4005 |
{ |
| 4006 |
return m_call == other.m_call; |
| 4007 |
} |
| 4008 |
|
| 4009 |
int get_controlling_option () const final override |
| 4010 |
{ |
| 4011 |
return OPT_Wanalyzer_jump_through_null; |
| 4012 |
} |
| 4013 |
|
| 4014 |
bool emit (diagnostic_emission_context &ctxt) final override |
| 4015 |
{ |
| 4016 |
return ctxt.warn ("jump through null pointer"); |
| 4017 |
} |
| 4018 |
|
| 4019 |
bool describe_final_event (pretty_printer &pp, |
| 4020 |
const evdesc::final_event &) final override |
| 4021 |
{ |
| 4022 |
pp_string (&pp, "jump through null pointer here"); |
| 4023 |
return true; |
| 4024 |
} |
| 4025 |
|
| 4026 |
private: |
| 4027 |
const gcall *m_call; |
| 4028 |
}; |
| 4029 |
|
| 4030 |
/* The core of exploded_graph::process_worklist (the main analysis loop), |
| 4031 |
handling one node in the worklist. |
| 4032 |
|
| 4033 |
Get successor <point, state> pairs for NODE, calling get_or_create on |
| 4034 |
them, and adding an exploded_edge to each successors. |
| 4035 |
|
| 4036 |
Freshly-created nodes will be added to the worklist. */ |
| 4037 |
|
| 4038 |
void |
| 4039 |
exploded_graph::process_node (exploded_node *node) |
| 4040 |
{ |
| 4041 |
logger * const logger = get_logger (); |
| 4042 |
LOG_FUNC_1 (logger, "EN: %i", node->m_index); |
| 4043 |
|
| 4044 |
node->set_status (exploded_node::STATUS_PROCESSED); |
| 4045 |
|
| 4046 |
const program_point &point = node->get_point (); |
| 4047 |
|
| 4048 |
/* Update cfun and input_location in case of an ICE: make it easier to |
| 4049 |
track down which source construct we're failing to handle. */ |
| 4050 |
auto_cfun sentinel (node->get_function ()); |
| 4051 |
const gimple *stmt = point.get_stmt (); |
| 4052 |
if (stmt) |
| 4053 |
input_location = stmt->location; |
| 4054 |
|
| 4055 |
const program_state &state = node->get_state (); |
| 4056 |
if (logger) |
| 4057 |
{ |
| 4058 |
pretty_printer *pp = logger->get_printer (); |
| 4059 |
logger->start_log_line (); |
| 4060 |
pp_string (pp, "point: "); |
| 4061 |
point.print (pp, format (false)); |
| 4062 |
pp_string (pp, ", state: "); |
| 4063 |
state.dump_to_pp (m_ext_state, true, false, pp); |
| 4064 |
logger->end_log_line (); |
| 4065 |
} |
| 4066 |
|
| 4067 |
switch (point.get_kind ()) |
| 4068 |
{ |
| 4069 |
default: |
| 4070 |
gcc_unreachable (); |
| 4071 |
case PK_ORIGIN: |
| 4072 |
/* This node exists to simplify finding the shortest path |
| 4073 |
to an exploded_node. */ |
| 4074 |
break; |
| 4075 |
|
| 4076 |
case PK_BEFORE_SUPERNODE: |
| 4077 |
{ |
| 4078 |
program_state next_state (state); |
| 4079 |
uncertainty_t uncertainty; |
| 4080 |
|
| 4081 |
if (point.get_from_edge ()) |
| 4082 |
{ |
| 4083 |
impl_region_model_context ctxt (*this, node, |
| 4084 |
&state, &next_state, |
| 4085 |
&uncertainty, NULL, NULL); |
| 4086 |
const cfg_superedge *last_cfg_superedge |
| 4087 |
= point.get_from_edge ()->dyn_cast_cfg_superedge (); |
| 4088 |
if (last_cfg_superedge) |
| 4089 |
next_state.m_region_model->update_for_phis |
| 4090 |
(node->get_supernode (), |
| 4091 |
last_cfg_superedge, |
| 4092 |
&ctxt); |
| 4093 |
program_state::detect_leaks (state, next_state, NULL, |
| 4094 |
get_ext_state (), &ctxt); |
| 4095 |
} |
| 4096 |
|
| 4097 |
program_point next_point (point.get_next ()); |
| 4098 |
exploded_node *next = get_or_create_node (next_point, next_state, node); |
| 4099 |
if (next) |
| 4100 |
add_edge (node, next, NULL, |
| 4101 |
false); /* Assume no work is done at phi nodes. */ |
| 4102 |
} |
| 4103 |
break; |
| 4104 |
case PK_BEFORE_STMT: |
| 4105 |
{ |
| 4106 |
/* Determine the effect of a run of one or more statements |
| 4107 |
within one supernode, generating an edge to the program_point |
| 4108 |
after the last statement that's processed. |
| 4109 |
|
| 4110 |
Stop iterating statements and thus consolidating into one enode |
| 4111 |
when: |
| 4112 |
- reaching the end of the statements in the supernode |
| 4113 |
- if an sm-state-change occurs (so that it gets its own |
| 4114 |
exploded_node) |
| 4115 |
- if "-fanalyzer-fine-grained" is active |
| 4116 |
- encountering certain statements must appear at the start of |
| 4117 |
their enode (for which stmt_requires_new_enode_p returns true) |
| 4118 |
|
| 4119 |
Update next_state in-place, to get the result of the one |
| 4120 |
or more stmts that are processed. |
| 4121 |
|
| 4122 |
Split the node in-place if an sm-state-change occurs, so that |
| 4123 |
the sm-state-change occurs on an edge where the src enode has |
| 4124 |
exactly one stmt, the one that caused the change. */ |
| 4125 |
program_state next_state (state); |
| 4126 |
|
| 4127 |
impl_path_context path_ctxt (&next_state, logger); |
| 4128 |
|
| 4129 |
bool could_have_done_work = false; |
| 4130 |
uncertainty_t uncertainty; |
| 4131 |
const supernode *snode = point.get_supernode (); |
| 4132 |
unsigned stmt_idx; |
| 4133 |
const gimple *prev_stmt = NULL; |
| 4134 |
for (stmt_idx = point.get_stmt_idx (); |
| 4135 |
stmt_idx < snode->m_stmts.length (); |
| 4136 |
stmt_idx++) |
| 4137 |
{ |
| 4138 |
const gimple *stmt = snode->m_stmts[stmt_idx]; |
| 4139 |
|
| 4140 |
if (stmt_idx > point.get_stmt_idx ()) |
| 4141 |
if (stmt_requires_new_enode_p (stmt, prev_stmt)) |
| 4142 |
{ |
| 4143 |
stmt_idx--; |
| 4144 |
break; |
| 4145 |
} |
| 4146 |
prev_stmt = stmt; |
| 4147 |
|
| 4148 |
program_state old_state (next_state); |
| 4149 |
|
| 4150 |
/* Process the stmt. */ |
| 4151 |
exploded_node::on_stmt_flags flags |
| 4152 |
= node->on_stmt (*this, snode, stmt, &next_state, &uncertainty, |
| 4153 |
&could_have_done_work, &path_ctxt); |
| 4154 |
node->m_num_processed_stmts++; |
| 4155 |
|
| 4156 |
/* If flags.m_terminate_path, stop analyzing; any nodes/edges |
| 4157 |
will have been added by on_stmt (e.g. for handling longjmp). */ |
| 4158 |
if (flags.m_terminate_path) |
| 4159 |
return; |
| 4160 |
|
| 4161 |
if (next_state.m_region_model) |
| 4162 |
{ |
| 4163 |
impl_region_model_context ctxt (*this, node, |
| 4164 |
&old_state, &next_state, |
| 4165 |
&uncertainty, NULL, stmt); |
| 4166 |
program_state::detect_leaks (old_state, next_state, NULL, |
| 4167 |
get_ext_state (), &ctxt); |
| 4168 |
} |
| 4169 |
|
| 4170 |
unsigned next_idx = stmt_idx + 1; |
| 4171 |
program_point next_point |
| 4172 |
= (next_idx < point.get_supernode ()->m_stmts.length () |
| 4173 |
? program_point::before_stmt (point.get_supernode (), next_idx, |
| 4174 |
point.get_call_string ()) |
| 4175 |
: program_point::after_supernode (point.get_supernode (), |
| 4176 |
point.get_call_string ())); |
| 4177 |
next_state = next_state.prune_for_point (*this, next_point, node, |
| 4178 |
&uncertainty); |
| 4179 |
|
| 4180 |
if (flag_analyzer_fine_grained |
| 4181 |
|| state_change_requires_new_enode_p (old_state, next_state) |
| 4182 |
|| path_ctxt.bifurcation_p () |
| 4183 |
|| path_ctxt.terminate_path_p ()) |
| 4184 |
{ |
| 4185 |
program_point split_point |
| 4186 |
= program_point::before_stmt (point.get_supernode (), |
| 4187 |
stmt_idx, |
| 4188 |
point.get_call_string ()); |
| 4189 |
if (split_point != node->get_point ()) |
| 4190 |
{ |
| 4191 |
/* If we're not at the start of NODE, split the enode at |
| 4192 |
this stmt, so we have: |
| 4193 |
node -> split_enode |
| 4194 |
so that when split_enode is processed the next edge |
| 4195 |
we add will be: |
| 4196 |
split_enode -> next |
| 4197 |
and any state change will effectively occur on that |
| 4198 |
latter edge, and split_enode will contain just stmt. */ |
| 4199 |
if (logger) |
| 4200 |
logger->log ("getting split_enode"); |
| 4201 |
exploded_node *split_enode |
| 4202 |
= get_or_create_node (split_point, old_state, node); |
| 4203 |
if (!split_enode) |
| 4204 |
return; |
| 4205 |
/* "stmt" will be reprocessed when split_enode is |
| 4206 |
processed. */ |
| 4207 |
node->m_num_processed_stmts--; |
| 4208 |
if (logger) |
| 4209 |
logger->log ("creating edge to split_enode"); |
| 4210 |
add_edge (node, split_enode, NULL, could_have_done_work); |
| 4211 |
return; |
| 4212 |
} |
| 4213 |
else |
| 4214 |
/* If we're at the start of NODE, stop iterating, |
| 4215 |
so that an edge will be created from NODE to |
| 4216 |
(next_point, next_state) below. */ |
| 4217 |
break; |
| 4218 |
} |
| 4219 |
} |
| 4220 |
unsigned next_idx = stmt_idx + 1; |
| 4221 |
program_point next_point |
| 4222 |
= (next_idx < point.get_supernode ()->m_stmts.length () |
| 4223 |
? program_point::before_stmt (point.get_supernode (), next_idx, |
| 4224 |
point.get_call_string ()) |
| 4225 |
: program_point::after_supernode (point.get_supernode (), |
| 4226 |
point.get_call_string ())); |
| 4227 |
if (path_ctxt.terminate_path_p ()) |
| 4228 |
{ |
| 4229 |
if (logger) |
| 4230 |
logger->log ("not adding node: terminating path"); |
| 4231 |
} |
| 4232 |
else |
| 4233 |
{ |
| 4234 |
exploded_node *next |
| 4235 |
= get_or_create_node (next_point, next_state, node); |
| 4236 |
if (next) |
| 4237 |
add_edge (node, next, NULL, could_have_done_work); |
| 4238 |
} |
| 4239 |
|
| 4240 |
/* If we have custom edge infos, "bifurcate" the state |
| 4241 |
accordingly, potentially creating a new state/enode/eedge |
| 4242 |
instances. For example, to handle a "realloc" call, we |
| 4243 |
might split into 3 states, for the "failure", |
| 4244 |
"resizing in place", and "moving to a new buffer" cases. */ |
| 4245 |
for (auto edge_info_iter : path_ctxt.get_custom_eedge_infos ()) |
| 4246 |
{ |
| 4247 |
/* Take ownership of the edge infos from the path_ctxt. */ |
| 4248 |
std::unique_ptr<custom_edge_info> edge_info (edge_info_iter); |
| 4249 |
if (logger) |
| 4250 |
{ |
| 4251 |
logger->start_log_line (); |
| 4252 |
logger->log_partial ("bifurcating for edge: "); |
| 4253 |
edge_info->print (logger->get_printer ()); |
| 4254 |
logger->end_log_line (); |
| 4255 |
} |
| 4256 |
program_state bifurcated_new_state |
| 4257 |
(path_ctxt.get_state_at_bifurcation ()); |
| 4258 |
|
| 4259 |
/* Apply edge_info to state. */ |
| 4260 |
impl_region_model_context |
| 4261 |
bifurcation_ctxt (*this, |
| 4262 |
node, // enode_for_diag |
| 4263 |
&path_ctxt.get_state_at_bifurcation (), |
| 4264 |
&bifurcated_new_state, |
| 4265 |
NULL, // uncertainty_t *uncertainty |
| 4266 |
NULL, // path_context *path_ctxt |
| 4267 |
stmt); |
| 4268 |
if (edge_info->update_state (&bifurcated_new_state, |
| 4269 |
NULL, /* no exploded_edge yet. */ |
| 4270 |
&bifurcation_ctxt)) |
| 4271 |
{ |
| 4272 |
exploded_node *next2 |
| 4273 |
= get_or_create_node (next_point, bifurcated_new_state, node); |
| 4274 |
if (next2) |
| 4275 |
add_edge (node, next2, NULL, |
| 4276 |
true /* assume that work could be done */, |
| 4277 |
std::move (edge_info)); |
| 4278 |
} |
| 4279 |
else |
| 4280 |
{ |
| 4281 |
if (logger) |
| 4282 |
logger->log ("infeasible state, not adding node"); |
| 4283 |
} |
| 4284 |
} |
| 4285 |
} |
| 4286 |
break; |
| 4287 |
case PK_AFTER_SUPERNODE: |
| 4288 |
{ |
| 4289 |
bool found_a_superedge = false; |
| 4290 |
bool is_an_exit_block = false; |
| 4291 |
/* If this is an EXIT BB, detect leaks, and potentially |
| 4292 |
create a function summary. */ |
| 4293 |
if (point.get_supernode ()->return_p ()) |
| 4294 |
{ |
| 4295 |
is_an_exit_block = true; |
| 4296 |
node->detect_leaks (*this); |
| 4297 |
if (flag_analyzer_call_summaries |
| 4298 |
&& point.get_call_string ().empty_p ()) |
| 4299 |
{ |
| 4300 |
/* TODO: create function summary |
| 4301 |
There can be more than one; each corresponds to a different |
| 4302 |
final enode in the function. */ |
| 4303 |
if (logger) |
| 4304 |
{ |
| 4305 |
pretty_printer *pp = logger->get_printer (); |
| 4306 |
logger->start_log_line (); |
| 4307 |
logger->log_partial |
| 4308 |
("would create function summary for %qE; state: ", |
| 4309 |
point.get_fndecl ()); |
| 4310 |
state.dump_to_pp (m_ext_state, true, false, pp); |
| 4311 |
logger->end_log_line (); |
| 4312 |
} |
| 4313 |
per_function_data *per_fn_data |
| 4314 |
= get_or_create_per_function_data (point.get_function ()); |
| 4315 |
per_fn_data->add_call_summary (node); |
| 4316 |
} |
| 4317 |
} |
| 4318 |
/* Traverse into successors of the supernode. */ |
| 4319 |
int i; |
| 4320 |
superedge *succ; |
| 4321 |
FOR_EACH_VEC_ELT (point.get_supernode ()->m_succs, i, succ) |
| 4322 |
{ |
| 4323 |
found_a_superedge = true; |
| 4324 |
if (logger) |
| 4325 |
{ |
| 4326 |
label_text succ_desc (succ->get_description (false)); |
| 4327 |
logger->log ("considering SN: %i -> SN: %i (%s)", |
| 4328 |
succ->m_src->m_index, succ->m_dest->m_index, |
| 4329 |
succ_desc.get ()); |
| 4330 |
} |
| 4331 |
|
| 4332 |
program_point next_point |
| 4333 |
= program_point::before_supernode (succ->m_dest, succ, |
| 4334 |
point.get_call_string ()); |
| 4335 |
program_state next_state (state); |
| 4336 |
uncertainty_t uncertainty; |
| 4337 |
|
| 4338 |
/* Make use the current state and try to discover and analyse |
| 4339 |
indirect function calls (a call that doesn't have an underlying |
| 4340 |
cgraph edge representing call). |
| 4341 |
|
| 4342 |
Some examples of such calls are virtual function calls |
| 4343 |
and calls that happen via a function pointer. */ |
| 4344 |
if (succ->m_kind == SUPEREDGE_INTRAPROCEDURAL_CALL |
| 4345 |
&& !(succ->get_any_callgraph_edge ())) |
| 4346 |
{ |
| 4347 |
const gcall *call |
| 4348 |
= point.get_supernode ()->get_final_call (); |
| 4349 |
|
| 4350 |
impl_region_model_context ctxt (*this, |
| 4351 |
node, |
| 4352 |
&state, |
| 4353 |
&next_state, |
| 4354 |
&uncertainty, |
| 4355 |
NULL, |
| 4356 |
point.get_stmt()); |
| 4357 |
|
| 4358 |
region_model *model = state.m_region_model; |
| 4359 |
bool call_discovered = false; |
| 4360 |
|
| 4361 |
if (tree fn_decl = model->get_fndecl_for_call (call, &ctxt)) |
| 4362 |
call_discovered = maybe_create_dynamic_call (call, |
| 4363 |
fn_decl, |
| 4364 |
node, |
| 4365 |
next_state, |
| 4366 |
next_point, |
| 4367 |
&uncertainty, |
| 4368 |
logger); |
| 4369 |
if (!call_discovered) |
| 4370 |
{ |
| 4371 |
/* Check for jump through NULL. */ |
| 4372 |
if (tree fn_ptr = gimple_call_fn (call)) |
| 4373 |
{ |
| 4374 |
const svalue *fn_ptr_sval |
| 4375 |
= model->get_rvalue (fn_ptr, &ctxt); |
| 4376 |
if (fn_ptr_sval->all_zeroes_p ()) |
| 4377 |
ctxt.warn (make_unique<jump_through_null> (call)); |
| 4378 |
} |
| 4379 |
|
| 4380 |
/* An unknown function or a special function was called |
| 4381 |
at this point, in such case, don't terminate the |
| 4382 |
analysis of the current function. |
| 4383 |
|
| 4384 |
The analyzer handles calls to such functions while |
| 4385 |
analysing the stmt itself, so the function call |
| 4386 |
must have been handled by the anlyzer till now. */ |
| 4387 |
exploded_node *next |
| 4388 |
= get_or_create_node (next_point, |
| 4389 |
next_state, |
| 4390 |
node); |
| 4391 |
if (next) |
| 4392 |
add_edge (node, next, succ, |
| 4393 |
true /* assume that work is done */); |
| 4394 |
} |
| 4395 |
} |
| 4396 |
|
| 4397 |
if (!node->on_edge (*this, succ, &next_point, &next_state, |
| 4398 |
&uncertainty)) |
| 4399 |
{ |
| 4400 |
if (logger) |
| 4401 |
logger->log ("skipping impossible edge to SN: %i", |
| 4402 |
succ->m_dest->m_index); |
| 4403 |
continue; |
| 4404 |
} |
| 4405 |
exploded_node *next = get_or_create_node (next_point, next_state, |
| 4406 |
node); |
| 4407 |
if (next) |
| 4408 |
{ |
| 4409 |
add_edge (node, next, succ, false); |
| 4410 |
|
| 4411 |
/* We might have a function entrypoint. */ |
| 4412 |
detect_infinite_recursion (next); |
| 4413 |
} |
| 4414 |
} |
| 4415 |
|
| 4416 |
/* Return from the calls which doesn't have a return superedge. |
| 4417 |
Such case occurs when GCC's middle end didn't knew which function to |
| 4418 |
call but analyzer did. */ |
| 4419 |
if ((is_an_exit_block && !found_a_superedge) |
| 4420 |
&& (!point.get_call_string ().empty_p ())) |
| 4421 |
{ |
| 4422 |
const call_string &cs = point.get_call_string (); |
| 4423 |
program_point next_point |
| 4424 |
= program_point::before_supernode (cs.get_caller_node (), |
| 4425 |
NULL, |
| 4426 |
cs); |
| 4427 |
program_state next_state (state); |
| 4428 |
uncertainty_t uncertainty; |
| 4429 |
|
| 4430 |
const gcall *call |
| 4431 |
= next_point.get_supernode ()->get_returning_call (); |
| 4432 |
|
| 4433 |
if (call) |
| 4434 |
next_state.returning_call (*this, node, call, &uncertainty); |
| 4435 |
|
| 4436 |
if (next_state.m_valid) |
| 4437 |
{ |
| 4438 |
next_point.pop_from_call_stack (); |
| 4439 |
exploded_node *enode = get_or_create_node (next_point, |
| 4440 |
next_state, |
| 4441 |
node); |
| 4442 |
if (enode) |
| 4443 |
add_edge (node, enode, NULL, false, |
| 4444 |
make_unique<dynamic_call_info_t> (call, true)); |
| 4445 |
} |
| 4446 |
} |
| 4447 |
} |
| 4448 |
break; |
| 4449 |
} |
| 4450 |
} |
| 4451 |
|
| 4452 |
/* Ensure that this graph has a stats instance for FN, return it. |
| 4453 |
FN can be NULL, in which case a stats instances is returned covering |
| 4454 |
"functionless" parts of the graph (the origin node). */ |
| 4455 |
|
| 4456 |
stats * |
| 4457 |
exploded_graph::get_or_create_function_stats (function *fn) |
| 4458 |
{ |
| 4459 |
if (!fn) |
| 4460 |
return &m_functionless_stats; |
| 4461 |
|
| 4462 |
if (stats **slot = m_per_function_stats.get (fn)) |
| 4463 |
return *slot; |
| 4464 |
else |
| 4465 |
{ |
| 4466 |
int num_supernodes = fn ? n_basic_blocks_for_fn (fn) : 0; |
| 4467 |
/* not quite the num supernodes, but nearly. */ |
| 4468 |
stats *new_stats = new stats (num_supernodes); |
| 4469 |
m_per_function_stats.put (fn, new_stats); |
| 4470 |
return new_stats; |
| 4471 |
} |
| 4472 |
} |
| 4473 |
|
| 4474 |
/* Print bar charts to PP showing: |
| 4475 |
- the number of enodes per function, and |
| 4476 |
- for each function: |
| 4477 |
- the number of enodes per supernode/BB |
| 4478 |
- the number of excess enodes per supernode/BB beyond the |
| 4479 |
per-program-point limit, if there were any. */ |
| 4480 |
|
| 4481 |
void |
| 4482 |
exploded_graph::print_bar_charts (pretty_printer *pp) const |
| 4483 |
{ |
| 4484 |
cgraph_node *cgnode; |
| 4485 |
|
| 4486 |
pp_string (pp, "enodes per function:"); |
| 4487 |
pp_newline (pp); |
| 4488 |
bar_chart enodes_per_function; |
| 4489 |
FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (cgnode) |
| 4490 |
{ |
| 4491 |
function *fn = cgnode->get_fun (); |
| 4492 |
const stats * const *s_ptr |
| 4493 |
= const_cast <function_stat_map_t &> (m_per_function_stats).get (fn); |
| 4494 |
enodes_per_function.add_item (function_name (fn), |
| 4495 |
s_ptr ? (*s_ptr)->get_total_enodes () : 0); |
| 4496 |
} |
| 4497 |
enodes_per_function.print (pp); |
| 4498 |
|
| 4499 |
/* Accumulate number of enodes per supernode. */ |
| 4500 |
auto_vec<unsigned> enodes_per_supernode (m_sg.num_nodes ()); |
| 4501 |
for (int i = 0; i < m_sg.num_nodes (); i++) |
| 4502 |
enodes_per_supernode.quick_push (0); |
| 4503 |
int i; |
| 4504 |
exploded_node *enode; |
| 4505 |
FOR_EACH_VEC_ELT (m_nodes, i, enode) |
| 4506 |
{ |
| 4507 |
const supernode *iter_snode = enode->get_supernode (); |
| 4508 |
if (!iter_snode) |
| 4509 |
continue; |
| 4510 |
enodes_per_supernode[iter_snode->m_index]++; |
| 4511 |
} |
| 4512 |
|
| 4513 |
/* Accumulate excess enodes per supernode. */ |
| 4514 |
auto_vec<unsigned> excess_enodes_per_supernode (m_sg.num_nodes ()); |
| 4515 |
for (int i = 0; i < m_sg.num_nodes (); i++) |
| 4516 |
excess_enodes_per_supernode.quick_push (0); |
| 4517 |
for (point_map_t::iterator iter = m_per_point_data.begin (); |
| 4518 |
iter != m_per_point_data.end (); ++iter) |
| 4519 |
{ |
| 4520 |
const program_point *point = (*iter).first; |
| 4521 |
const supernode *iter_snode = point->get_supernode (); |
| 4522 |
if (!iter_snode) |
| 4523 |
continue; |
| 4524 |
const per_program_point_data *point_data = (*iter).second; |
| 4525 |
excess_enodes_per_supernode[iter_snode->m_index] |
| 4526 |
+= point_data->m_excess_enodes; |
| 4527 |
} |
| 4528 |
|
| 4529 |
/* Show per-function bar_charts of enodes per supernode/BB. */ |
| 4530 |
pp_string (pp, "per-function enodes per supernode/BB:"); |
| 4531 |
pp_newline (pp); |
| 4532 |
FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (cgnode) |
| 4533 |
{ |
| 4534 |
function *fn = cgnode->get_fun (); |
| 4535 |
pp_printf (pp, "function: %qs", function_name (fn)); |
| 4536 |
pp_newline (pp); |
| 4537 |
|
| 4538 |
bar_chart enodes_per_snode; |
| 4539 |
bar_chart excess_enodes_per_snode; |
| 4540 |
bool have_excess_enodes = false; |
| 4541 |
for (int i = 0; i < m_sg.num_nodes (); i++) |
| 4542 |
{ |
| 4543 |
const supernode *iter_snode = m_sg.get_node_by_index (i); |
| 4544 |
if (iter_snode->get_function () != fn) |
| 4545 |
continue; |
| 4546 |
pretty_printer tmp_pp; |
| 4547 |
pp_printf (&tmp_pp, "sn %i (bb %i)", |
| 4548 |
iter_snode->m_index, iter_snode->m_bb->index); |
| 4549 |
enodes_per_snode.add_item (pp_formatted_text (&tmp_pp), |
| 4550 |
enodes_per_supernode[iter_snode->m_index]); |
| 4551 |
const int num_excess |
| 4552 |
= excess_enodes_per_supernode[iter_snode->m_index]; |
| 4553 |
excess_enodes_per_snode.add_item (pp_formatted_text (&tmp_pp), |
| 4554 |
num_excess); |
| 4555 |
if (num_excess) |
| 4556 |
have_excess_enodes = true; |
| 4557 |
} |
| 4558 |
enodes_per_snode.print (pp); |
| 4559 |
if (have_excess_enodes) |
| 4560 |
{ |
| 4561 |
pp_printf (pp, "EXCESS ENODES:"); |
| 4562 |
pp_newline (pp); |
| 4563 |
excess_enodes_per_snode.print (pp); |
| 4564 |
} |
| 4565 |
} |
| 4566 |
} |
| 4567 |
|
| 4568 |
/* Write all stats information to this graph's logger, if any. */ |
| 4569 |
|
| 4570 |
void |
| 4571 |
exploded_graph::log_stats () const |
| 4572 |
{ |
| 4573 |
logger * const logger = get_logger (); |
| 4574 |
if (!logger) |
| 4575 |
return; |
| 4576 |
|
| 4577 |
LOG_SCOPE (logger); |
| 4578 |
|
| 4579 |
m_ext_state.get_engine ()->log_stats (logger); |
| 4580 |
|
| 4581 |
logger->log ("m_sg.num_nodes (): %i", m_sg.num_nodes ()); |
| 4582 |
logger->log ("m_nodes.length (): %i", m_nodes.length ()); |
| 4583 |
logger->log ("m_edges.length (): %i", m_edges.length ()); |
| 4584 |
logger->log ("remaining enodes in worklist: %i", m_worklist.length ()); |
| 4585 |
|
| 4586 |
logger->log ("global stats:"); |
| 4587 |
m_global_stats.log (logger); |
| 4588 |
|
| 4589 |
for (function_stat_map_t::iterator iter = m_per_function_stats.begin (); |
| 4590 |
iter != m_per_function_stats.end (); |
| 4591 |
++iter) |
| 4592 |
{ |
| 4593 |
function *fn = (*iter).first; |
| 4594 |
log_scope s (logger, function_name (fn)); |
| 4595 |
(*iter).second->log (logger); |
| 4596 |
} |
| 4597 |
|
| 4598 |
print_bar_charts (logger->get_printer ()); |
| 4599 |
} |
| 4600 |
|
| 4601 |
/* Dump all stats information to OUT. */ |
| 4602 |
|
| 4603 |
void |
| 4604 |
exploded_graph::dump_stats (FILE *out) const |
| 4605 |
{ |
| 4606 |
fprintf (out, "m_sg.num_nodes (): %i\n", m_sg.num_nodes ()); |
| 4607 |
fprintf (out, "m_nodes.length (): %i\n", m_nodes.length ()); |
| 4608 |
fprintf (out, "m_edges.length (): %i\n", m_edges.length ()); |
| 4609 |
fprintf (out, "remaining enodes in worklist: %i", m_worklist.length ()); |
| 4610 |
|
| 4611 |
fprintf (out, "global stats:\n"); |
| 4612 |
m_global_stats.dump (out); |
| 4613 |
|
| 4614 |
for (function_stat_map_t::iterator iter = m_per_function_stats.begin (); |
| 4615 |
iter != m_per_function_stats.end (); |
| 4616 |
++iter) |
| 4617 |
{ |
| 4618 |
function *fn = (*iter).first; |
| 4619 |
fprintf (out, "function: %s\n", function_name (fn)); |
| 4620 |
(*iter).second->dump (out); |
| 4621 |
} |
| 4622 |
|
| 4623 |
fprintf (out, "PK_AFTER_SUPERNODE per supernode:\n"); |
| 4624 |
for (unsigned i = 0; i < m_PK_AFTER_SUPERNODE_per_snode.length (); i++) |
| 4625 |
fprintf (out, " SN %i: %3i\n", i, m_PK_AFTER_SUPERNODE_per_snode[i]); |
| 4626 |
} |
| 4627 |
|
| 4628 |
void |
| 4629 |
exploded_graph::dump_states_for_supernode (FILE *out, |
| 4630 |
const supernode *snode) const |
| 4631 |
{ |
| 4632 |
fprintf (out, "PK_AFTER_SUPERNODE nodes for SN: %i\n", snode->m_index); |
| 4633 |
int i; |
| 4634 |
exploded_node *enode; |
| 4635 |
int state_idx = 0; |
| 4636 |
FOR_EACH_VEC_ELT (m_nodes, i, enode) |
| 4637 |
{ |
| 4638 |
const supernode *iter_snode = enode->get_supernode (); |
| 4639 |
if (enode->get_point ().get_kind () == PK_AFTER_SUPERNODE |
| 4640 |
&& iter_snode == snode) |
| 4641 |
{ |
| 4642 |
pretty_printer pp; |
| 4643 |
pp_format_decoder (&pp) = default_tree_printer; |
| 4644 |
enode->get_state ().dump_to_pp (m_ext_state, true, false, &pp); |
| 4645 |
fprintf (out, "state %i: EN: %i\n %s\n", |
| 4646 |
state_idx++, enode->m_index, |
| 4647 |
pp_formatted_text (&pp)); |
| 4648 |
} |
| 4649 |
} |
| 4650 |
fprintf (out, "#exploded_node for PK_AFTER_SUPERNODE for SN: %i = %i\n", |
| 4651 |
snode->m_index, state_idx); |
| 4652 |
} |
| 4653 |
|
| 4654 |
/* Return a new json::object of the form |
| 4655 |
{"nodes" : [objs for enodes], |
| 4656 |
"edges" : [objs for eedges], |
| 4657 |
"ext_state": object for extrinsic_state, |
| 4658 |
"diagnostic_manager": object for diagnostic_manager}. */ |
| 4659 |
|
| 4660 |
std::unique_ptr<json::object> |
| 4661 |
exploded_graph::to_json () const |
| 4662 |
{ |
| 4663 |
auto egraph_obj = ::make_unique<json::object> (); |
| 4664 |
|
| 4665 |
/* Nodes. */ |
| 4666 |
{ |
| 4667 |
auto nodes_arr = ::make_unique<json::array> (); |
| 4668 |
unsigned i; |
| 4669 |
exploded_node *n; |
| 4670 |
FOR_EACH_VEC_ELT (m_nodes, i, n) |
| 4671 |
nodes_arr->append (n->to_json (m_ext_state)); |
| 4672 |
egraph_obj->set ("nodes", std::move (nodes_arr)); |
| 4673 |
} |
| 4674 |
|
| 4675 |
/* Edges. */ |
| 4676 |
{ |
| 4677 |
auto edges_arr = ::make_unique<json::array> (); |
| 4678 |
unsigned i; |
| 4679 |
exploded_edge *n; |
| 4680 |
FOR_EACH_VEC_ELT (m_edges, i, n) |
| 4681 |
edges_arr->append (n->to_json ()); |
| 4682 |
egraph_obj->set ("edges", std::move (edges_arr)); |
| 4683 |
} |
| 4684 |
|
| 4685 |
/* m_sg is JSONified at the top-level. */ |
| 4686 |
|
| 4687 |
egraph_obj->set ("ext_state", m_ext_state.to_json ()); |
| 4688 |
egraph_obj->set ("worklist", m_worklist.to_json ()); |
| 4689 |
egraph_obj->set ("diagnostic_manager", m_diagnostic_manager.to_json ()); |
| 4690 |
|
| 4691 |
/* The following fields aren't yet being JSONified: |
| 4692 |
const state_purge_map *const m_purge_map; |
| 4693 |
const analysis_plan &m_plan; |
| 4694 |
stats m_global_stats; |
| 4695 |
function_stat_map_t m_per_function_stats; |
| 4696 |
stats m_functionless_stats; |
| 4697 |
call_string_data_map_t m_per_call_string_data; |
| 4698 |
auto_vec<int> m_PK_AFTER_SUPERNODE_per_snode; */ |
| 4699 |
|
| 4700 |
return egraph_obj; |
| 4701 |
} |
| 4702 |
|
| 4703 |
/* class exploded_path. */ |
| 4704 |
|
| 4705 |
/* Copy ctor. */ |
| 4706 |
|
| 4707 |
exploded_path::exploded_path (const exploded_path &other) |
| 4708 |
: m_edges (other.m_edges.length ()) |
| 4709 |
{ |
| 4710 |
int i; |
| 4711 |
const exploded_edge *eedge; |
| 4712 |
FOR_EACH_VEC_ELT (other.m_edges, i, eedge) |
| 4713 |
m_edges.quick_push (eedge); |
| 4714 |
} |
| 4715 |
|
| 4716 |
/* Look for the last use of SEARCH_STMT within this path. |
| 4717 |
If found write the edge's index to *OUT_IDX and return true, otherwise |
| 4718 |
return false. */ |
| 4719 |
|
| 4720 |
bool |
| 4721 |
exploded_path::find_stmt_backwards (const gimple *search_stmt, |
| 4722 |
int *out_idx) const |
| 4723 |
{ |
| 4724 |
int i; |
| 4725 |
const exploded_edge *eedge; |
| 4726 |
FOR_EACH_VEC_ELT_REVERSE (m_edges, i, eedge) |
| 4727 |
{ |
| 4728 |
const exploded_node *dst_node = eedge->m_dest; |
| 4729 |
const program_point &dst_point = dst_node->get_point (); |
| 4730 |
const gimple *stmt = dst_point.get_stmt (); |
| 4731 |
if (stmt == search_stmt) |
| 4732 |
{ |
| 4733 |
*out_idx = i; |
| 4734 |
return true; |
| 4735 |
} |
| 4736 |
} |
| 4737 |
return false; |
| 4738 |
} |
| 4739 |
|
| 4740 |
/* Get the final exploded_node in this path, which must be non-empty. */ |
| 4741 |
|
| 4742 |
exploded_node * |
| 4743 |
exploded_path::get_final_enode () const |
| 4744 |
{ |
| 4745 |
gcc_assert (m_edges.length () > 0); |
| 4746 |
return m_edges[m_edges.length () - 1]->m_dest; |
| 4747 |
} |
| 4748 |
|
| 4749 |
/* Check state along this path, returning true if it is feasible. |
| 4750 |
If OUT is non-NULL, and the path is infeasible, write a new |
| 4751 |
feasibility_problem to *OUT. */ |
| 4752 |
|
| 4753 |
bool |
| 4754 |
exploded_path::feasible_p (logger *logger, |
| 4755 |
std::unique_ptr<feasibility_problem> *out, |
| 4756 |
engine *eng, const exploded_graph *eg) const |
| 4757 |
{ |
| 4758 |
LOG_SCOPE (logger); |
| 4759 |
|
| 4760 |
feasibility_state state (eng->get_model_manager (), |
| 4761 |
eg->get_supergraph ()); |
| 4762 |
|
| 4763 |
/* Traverse the path, updating this state. */ |
| 4764 |
for (unsigned edge_idx = 0; edge_idx < m_edges.length (); edge_idx++) |
| 4765 |
{ |
| 4766 |
const exploded_edge *eedge = m_edges[edge_idx]; |
| 4767 |
if (logger) |
| 4768 |
logger->log ("considering edge %i: EN:%i -> EN:%i", |
| 4769 |
edge_idx, |
| 4770 |
eedge->m_src->m_index, |
| 4771 |
eedge->m_dest->m_index); |
| 4772 |
|
| 4773 |
std::unique_ptr <rejected_constraint> rc; |
| 4774 |
if (!state.maybe_update_for_edge (logger, eedge, nullptr, &rc)) |
| 4775 |
{ |
| 4776 |
gcc_assert (rc); |
| 4777 |
if (out) |
| 4778 |
{ |
| 4779 |
const exploded_node &src_enode = *eedge->m_src; |
| 4780 |
const program_point &src_point = src_enode.get_point (); |
| 4781 |
const gimple *last_stmt |
| 4782 |
= src_point.get_supernode ()->get_last_stmt (); |
| 4783 |
*out = ::make_unique<feasibility_problem> (edge_idx, *eedge, |
| 4784 |
last_stmt, |
| 4785 |
std::move (rc)); |
| 4786 |
} |
| 4787 |
return false; |
| 4788 |
} |
| 4789 |
|
| 4790 |
if (logger) |
| 4791 |
{ |
| 4792 |
logger->log ("state after edge %i: EN:%i -> EN:%i", |
| 4793 |
edge_idx, |
| 4794 |
eedge->m_src->m_index, |
| 4795 |
eedge->m_dest->m_index); |
| 4796 |
logger->start_log_line (); |
| 4797 |
state.get_model ().dump_to_pp (logger->get_printer (), true, false); |
| 4798 |
logger->end_log_line (); |
| 4799 |
} |
| 4800 |
} |
| 4801 |
|
| 4802 |
return true; |
| 4803 |
} |
| 4804 |
|
| 4805 |
/* Dump this path in multiline form to PP. |
| 4806 |
If EXT_STATE is non-NULL, then show the nodes. */ |
| 4807 |
|
| 4808 |
void |
| 4809 |
exploded_path::dump_to_pp (pretty_printer *pp, |
| 4810 |
const extrinsic_state *ext_state) const |
| 4811 |
{ |
| 4812 |
for (unsigned i = 0; i < m_edges.length (); i++) |
| 4813 |
{ |
| 4814 |
const exploded_edge *eedge = m_edges[i]; |
| 4815 |
pp_printf (pp, "m_edges[%i]: EN %i -> EN %i", |
| 4816 |
i, |
| 4817 |
eedge->m_src->m_index, |
| 4818 |
eedge->m_dest->m_index); |
| 4819 |
pp_newline (pp); |
| 4820 |
|
| 4821 |
if (ext_state) |
| 4822 |
eedge->m_dest->dump_to_pp (pp, *ext_state); |
| 4823 |
} |
| 4824 |
} |
| 4825 |
|
| 4826 |
/* Dump this path in multiline form to FP. */ |
| 4827 |
|
| 4828 |
void |
| 4829 |
exploded_path::dump (FILE *fp, const extrinsic_state *ext_state) const |
| 4830 |
{ |
| 4831 |
tree_dump_pretty_printer pp (fp); |
| 4832 |
dump_to_pp (&pp, ext_state); |
| 4833 |
} |
| 4834 |
|
| 4835 |
/* Dump this path in multiline form to stderr. */ |
| 4836 |
|
| 4837 |
DEBUG_FUNCTION void |
| 4838 |
exploded_path::dump (const extrinsic_state *ext_state) const |
| 4839 |
{ |
| 4840 |
dump (stderr, ext_state); |
| 4841 |
} |
| 4842 |
|
| 4843 |
/* Dump this path verbosely to FILENAME. */ |
| 4844 |
|
| 4845 |
void |
| 4846 |
exploded_path::dump_to_file (const char *filename, |
| 4847 |
const extrinsic_state &ext_state) const |
| 4848 |
{ |
| 4849 |
FILE *fp = fopen (filename, "w"); |
| 4850 |
if (!fp) |
| 4851 |
return; |
| 4852 |
pretty_printer pp; |
| 4853 |
pp_format_decoder (&pp) = default_tree_printer; |
| 4854 |
pp.set_output_stream (fp); |
| 4855 |
dump_to_pp (&pp, &ext_state); |
| 4856 |
pp_flush (&pp); |
| 4857 |
fclose (fp); |
| 4858 |
} |
| 4859 |
|
| 4860 |
/* class feasibility_problem. */ |
| 4861 |
|
| 4862 |
void |
| 4863 |
feasibility_problem::dump_to_pp (pretty_printer *pp) const |
| 4864 |
{ |
| 4865 |
pp_printf (pp, "edge from EN: %i to EN: %i", |
| 4866 |
m_eedge.m_src->m_index, m_eedge.m_dest->m_index); |
| 4867 |
if (m_rc) |
| 4868 |
{ |
| 4869 |
pp_string (pp, "; rejected constraint: "); |
| 4870 |
m_rc->dump_to_pp (pp); |
| 4871 |
pp_string (pp, "; rmodel: "); |
| 4872 |
m_rc->get_model ().dump_to_pp (pp, true, false); |
| 4873 |
} |
| 4874 |
} |
| 4875 |
|
| 4876 |
/* class feasibility_state. */ |
| 4877 |
|
| 4878 |
/* Ctor for feasibility_state, at the beginning of a path. */ |
| 4879 |
|
| 4880 |
feasibility_state::feasibility_state (region_model_manager *manager, |
| 4881 |
const supergraph &sg) |
| 4882 |
: m_model (manager), |
| 4883 |
m_snodes_visited (sg.m_nodes.length ()) |
| 4884 |
{ |
| 4885 |
bitmap_clear (m_snodes_visited); |
| 4886 |
} |
| 4887 |
|
| 4888 |
/* Copy ctor for feasibility_state, for extending a path. */ |
| 4889 |
|
| 4890 |
feasibility_state::feasibility_state (const feasibility_state &other) |
| 4891 |
: m_model (other.m_model), |
| 4892 |
m_snodes_visited (const_sbitmap (other.m_snodes_visited)->n_bits) |
| 4893 |
{ |
| 4894 |
bitmap_copy (m_snodes_visited, other.m_snodes_visited); |
| 4895 |
} |
| 4896 |
|
| 4897 |
feasibility_state::feasibility_state (const region_model &model, |
| 4898 |
const supergraph &sg) |
| 4899 |
: m_model (model), |
| 4900 |
m_snodes_visited (sg.m_nodes.length ()) |
| 4901 |
{ |
| 4902 |
bitmap_clear (m_snodes_visited); |
| 4903 |
} |
| 4904 |
|
| 4905 |
feasibility_state & |
| 4906 |
feasibility_state::operator= (const feasibility_state &other) |
| 4907 |
{ |
| 4908 |
m_model = other.m_model; |
| 4909 |
bitmap_copy (m_snodes_visited, other.m_snodes_visited); |
| 4910 |
return *this; |
| 4911 |
} |
| 4912 |
|
| 4913 |
/* The heart of feasibility-checking. |
| 4914 |
|
| 4915 |
Attempt to update this state in-place based on traversing EEDGE |
| 4916 |
in a path. |
| 4917 |
Update the model for the stmts in the src enode. |
| 4918 |
Attempt to add constraints for EEDGE. |
| 4919 |
|
| 4920 |
If feasible, return true. |
| 4921 |
Otherwise, return false and write to *OUT_RC. */ |
| 4922 |
|
| 4923 |
bool |
| 4924 |
feasibility_state:: |
| 4925 |
maybe_update_for_edge (logger *logger, |
| 4926 |
const exploded_edge *eedge, |
| 4927 |
region_model_context *ctxt, |
| 4928 |
std::unique_ptr<rejected_constraint> *out_rc) |
| 4929 |
{ |
| 4930 |
const exploded_node &src_enode = *eedge->m_src; |
| 4931 |
const program_point &src_point = src_enode.get_point (); |
| 4932 |
if (logger) |
| 4933 |
{ |
| 4934 |
logger->start_log_line (); |
| 4935 |
src_point.print (logger->get_printer (), format (false)); |
| 4936 |
logger->end_log_line (); |
| 4937 |
} |
| 4938 |
|
| 4939 |
/* Update state for the stmts that were processed in each enode. */ |
| 4940 |
for (unsigned stmt_idx = 0; stmt_idx < src_enode.m_num_processed_stmts; |
| 4941 |
stmt_idx++) |
| 4942 |
{ |
| 4943 |
const gimple *stmt = src_enode.get_processed_stmt (stmt_idx); |
| 4944 |
|
| 4945 |
/* Update cfun and input_location in case of ICE: make it easier to |
| 4946 |
track down which source construct we're failing to handle. */ |
| 4947 |
auto_cfun sentinel (src_point.get_function ()); |
| 4948 |
input_location = stmt->location; |
| 4949 |
|
| 4950 |
update_for_stmt (stmt); |
| 4951 |
} |
| 4952 |
|
| 4953 |
const superedge *sedge = eedge->m_sedge; |
| 4954 |
if (sedge) |
| 4955 |
{ |
| 4956 |
if (logger) |
| 4957 |
{ |
| 4958 |
label_text desc (sedge->get_description (false)); |
| 4959 |
logger->log (" sedge: SN:%i -> SN:%i %s", |
| 4960 |
sedge->m_src->m_index, |
| 4961 |
sedge->m_dest->m_index, |
| 4962 |
desc.get ()); |
| 4963 |
} |
| 4964 |
|
| 4965 |
const gimple *last_stmt = src_point.get_supernode ()->get_last_stmt (); |
| 4966 |
if (!m_model.maybe_update_for_edge (*sedge, last_stmt, ctxt, out_rc)) |
| 4967 |
{ |
| 4968 |
if (logger) |
| 4969 |
{ |
| 4970 |
logger->start_log_line (); |
| 4971 |
logger->log_partial ("rejecting due to region model: "); |
| 4972 |
m_model.dump_to_pp (logger->get_printer (), true, false); |
| 4973 |
logger->end_log_line (); |
| 4974 |
} |
| 4975 |
return false; |
| 4976 |
} |
| 4977 |
} |
| 4978 |
else |
| 4979 |
{ |
| 4980 |
/* Special-case the initial eedge from the origin node to the |
| 4981 |
initial function by pushing a frame for it. */ |
| 4982 |
if (src_point.get_kind () == PK_ORIGIN) |
| 4983 |
{ |
| 4984 |
gcc_assert (eedge->m_src->m_index == 0); |
| 4985 |
gcc_assert (eedge->m_dest->get_point ().get_kind () |
| 4986 |
== PK_BEFORE_SUPERNODE); |
| 4987 |
function *fun = eedge->m_dest->get_function (); |
| 4988 |
gcc_assert (fun); |
| 4989 |
m_model.push_frame (*fun, NULL, ctxt); |
| 4990 |
if (logger) |
| 4991 |
logger->log (" pushing frame for %qD", fun->decl); |
| 4992 |
} |
| 4993 |
else if (eedge->m_custom_info) |
| 4994 |
{ |
| 4995 |
eedge->m_custom_info->update_model (&m_model, eedge, ctxt); |
| 4996 |
} |
| 4997 |
} |
| 4998 |
|
| 4999 |
/* Handle phi nodes on an edge leaving a PK_BEFORE_SUPERNODE (to |
| 5000 |
a PK_BEFORE_STMT, or a PK_AFTER_SUPERNODE if no stmts). |
| 5001 |
This will typically not be associated with a superedge. */ |
| 5002 |
if (src_point.get_from_edge ()) |
| 5003 |
{ |
| 5004 |
const cfg_superedge *last_cfg_superedge |
| 5005 |
= src_point.get_from_edge ()->dyn_cast_cfg_superedge (); |
| 5006 |
const exploded_node &dst_enode = *eedge->m_dest; |
| 5007 |
const unsigned dst_snode_idx = dst_enode.get_supernode ()->m_index; |
| 5008 |
if (last_cfg_superedge) |
| 5009 |
{ |
| 5010 |
if (logger) |
| 5011 |
logger->log (" update for phis"); |
| 5012 |
m_model.update_for_phis (src_enode.get_supernode (), |
| 5013 |
last_cfg_superedge, |
| 5014 |
ctxt); |
| 5015 |
/* If we've entering an snode that we've already visited on this |
| 5016 |
epath, then we need do fix things up for loops; see the |
| 5017 |
comment for store::loop_replay_fixup. |
| 5018 |
Perhaps we should probably also verify the callstring, |
| 5019 |
and track program_points, but hopefully doing it by supernode |
| 5020 |
is good enough. */ |
| 5021 |
if (bitmap_bit_p (m_snodes_visited, dst_snode_idx)) |
| 5022 |
m_model.loop_replay_fixup (dst_enode.get_state ().m_region_model); |
| 5023 |
} |
| 5024 |
bitmap_set_bit (m_snodes_visited, dst_snode_idx); |
| 5025 |
} |
| 5026 |
return true; |
| 5027 |
} |
| 5028 |
|
| 5029 |
/* Update this object for the effects of STMT. */ |
| 5030 |
|
| 5031 |
void |
| 5032 |
feasibility_state::update_for_stmt (const gimple *stmt) |
| 5033 |
{ |
| 5034 |
if (const gassign *assign = dyn_cast <const gassign *> (stmt)) |
| 5035 |
m_model.on_assignment (assign, NULL); |
| 5036 |
else if (const gasm *asm_stmt = dyn_cast <const gasm *> (stmt)) |
| 5037 |
m_model.on_asm_stmt (asm_stmt, NULL); |
| 5038 |
else if (const gcall *call = dyn_cast <const gcall *> (stmt)) |
| 5039 |
{ |
| 5040 |
bool unknown_side_effects = m_model.on_call_pre (call, NULL); |
| 5041 |
m_model.on_call_post (call, unknown_side_effects, NULL); |
| 5042 |
} |
| 5043 |
else if (const greturn *return_ = dyn_cast <const greturn *> (stmt)) |
| 5044 |
m_model.on_return (return_, NULL); |
| 5045 |
} |
| 5046 |
|
| 5047 |
/* Dump this object to PP. */ |
| 5048 |
|
| 5049 |
void |
| 5050 |
feasibility_state::dump_to_pp (pretty_printer *pp, |
| 5051 |
bool simple, bool multiline) const |
| 5052 |
{ |
| 5053 |
m_model.dump_to_pp (pp, simple, multiline); |
| 5054 |
} |
| 5055 |
|
| 5056 |
/* A family of cluster subclasses for use when generating .dot output for |
| 5057 |
exploded graphs (-fdump-analyzer-exploded-graph), for grouping the |
| 5058 |
enodes into hierarchical boxes. |
| 5059 |
|
| 5060 |
All functionless enodes appear in the top-level graph. |
| 5061 |
Every (function, call_string) pair gets its own cluster. Within that |
| 5062 |
cluster, each supernode gets its own cluster. |
| 5063 |
|
| 5064 |
Hence all enodes relating to a particular function with a particular |
| 5065 |
callstring will be in a cluster together; all enodes for the same |
| 5066 |
function but with a different callstring will be in a different |
| 5067 |
cluster. */ |
| 5068 |
|
| 5069 |
/* Base class of cluster for clustering exploded_node instances in .dot |
| 5070 |
output, based on various subclass-specific criteria. */ |
| 5071 |
|
| 5072 |
class exploded_cluster : public cluster<eg_traits> |
| 5073 |
{ |
| 5074 |
}; |
| 5075 |
|
| 5076 |
/* Cluster containing all exploded_node instances for one supernode. */ |
| 5077 |
|
| 5078 |
class supernode_cluster : public exploded_cluster |
| 5079 |
{ |
| 5080 |
public: |
| 5081 |
supernode_cluster (const supernode *supernode) : m_supernode (supernode) {} |
| 5082 |
|
| 5083 |
// TODO: dtor? |
| 5084 |
|
| 5085 |
void dump_dot (graphviz_out *gv, const dump_args_t &args) const final override |
| 5086 |
{ |
| 5087 |
gv->println ("subgraph \"cluster_supernode_%i\" {", m_supernode->m_index); |
| 5088 |
gv->indent (); |
| 5089 |
gv->println ("style=\"dashed\";"); |
| 5090 |
gv->println ("label=\"SN: %i (bb: %i; scc: %i)\";", |
| 5091 |
m_supernode->m_index, m_supernode->m_bb->index, |
| 5092 |
args.m_eg.get_scc_id (*m_supernode)); |
| 5093 |
|
| 5094 |
int i; |
| 5095 |
exploded_node *enode; |
| 5096 |
FOR_EACH_VEC_ELT (m_enodes, i, enode) |
| 5097 |
enode->dump_dot (gv, args); |
| 5098 |
|
| 5099 |
/* Terminate subgraph. */ |
| 5100 |
gv->outdent (); |
| 5101 |
gv->println ("}"); |
| 5102 |
} |
| 5103 |
|
| 5104 |
void add_node (exploded_node *en) final override |
| 5105 |
{ |
| 5106 |
m_enodes.safe_push (en); |
| 5107 |
} |
| 5108 |
|
| 5109 |
/* Comparator for use by auto_vec<supernode_cluster *>::qsort. */ |
| 5110 |
|
| 5111 |
static int cmp_ptr_ptr (const void *p1, const void *p2) |
| 5112 |
{ |
| 5113 |
const supernode_cluster *c1 |
| 5114 |
= *(const supernode_cluster * const *)p1; |
| 5115 |
const supernode_cluster *c2 |
| 5116 |
= *(const supernode_cluster * const *)p2; |
| 5117 |
return c1->m_supernode->m_index - c2->m_supernode->m_index; |
| 5118 |
} |
| 5119 |
|
| 5120 |
private: |
| 5121 |
const supernode *m_supernode; |
| 5122 |
auto_vec <exploded_node *> m_enodes; |
| 5123 |
}; |
| 5124 |
|
| 5125 |
/* Cluster containing all supernode_cluster instances for one |
| 5126 |
(function, call_string) pair. */ |
| 5127 |
|
| 5128 |
class function_call_string_cluster : public exploded_cluster |
| 5129 |
{ |
| 5130 |
public: |
| 5131 |
function_call_string_cluster (function *fun, const call_string &cs) |
| 5132 |
: m_fun (fun), m_cs (cs) {} |
| 5133 |
|
| 5134 |
~function_call_string_cluster () |
| 5135 |
{ |
| 5136 |
for (map_t::iterator iter = m_map.begin (); |
| 5137 |
iter != m_map.end (); |
| 5138 |
++iter) |
| 5139 |
delete (*iter).second; |
| 5140 |
} |
| 5141 |
|
| 5142 |
void dump_dot (graphviz_out *gv, const dump_args_t &args) const final override |
| 5143 |
{ |
| 5144 |
const char *funcname = function_name (m_fun); |
| 5145 |
|
| 5146 |
gv->println ("subgraph \"cluster_function_%s\" {", |
| 5147 |
IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (m_fun->decl))); |
| 5148 |
gv->indent (); |
| 5149 |
gv->write_indent (); |
| 5150 |
gv->print ("label=\"call string: "); |
| 5151 |
m_cs.print (gv->get_pp ()); |
| 5152 |
gv->print (" function: %s \";", funcname); |
| 5153 |
gv->print ("\n"); |
| 5154 |
|
| 5155 |
/* Dump m_map, sorting it to avoid churn when comparing dumps. */ |
| 5156 |
auto_vec<supernode_cluster *> child_clusters (m_map.elements ()); |
| 5157 |
for (map_t::iterator iter = m_map.begin (); |
| 5158 |
iter != m_map.end (); |
| 5159 |
++iter) |
| 5160 |
child_clusters.quick_push ((*iter).second); |
| 5161 |
|
| 5162 |
child_clusters.qsort (supernode_cluster::cmp_ptr_ptr); |
| 5163 |
|
| 5164 |
unsigned i; |
| 5165 |
supernode_cluster *child_cluster; |
| 5166 |
FOR_EACH_VEC_ELT (child_clusters, i, child_cluster) |
| 5167 |
child_cluster->dump_dot (gv, args); |
| 5168 |
|
| 5169 |
/* Terminate subgraph. */ |
| 5170 |
gv->outdent (); |
| 5171 |
gv->println ("}"); |
| 5172 |
} |
| 5173 |
|
| 5174 |
void add_node (exploded_node *en) final override |
| 5175 |
{ |
| 5176 |
const supernode *supernode = en->get_supernode (); |
| 5177 |
gcc_assert (supernode); |
| 5178 |
supernode_cluster **slot = m_map.get (supernode); |
| 5179 |
if (slot) |
| 5180 |
(*slot)->add_node (en); |
| 5181 |
else |
| 5182 |
{ |
| 5183 |
supernode_cluster *child = new supernode_cluster (supernode); |
| 5184 |
m_map.put (supernode, child); |
| 5185 |
child->add_node (en); |
| 5186 |
} |
| 5187 |
} |
| 5188 |
|
| 5189 |
/* Comparator for use by auto_vec<function_call_string_cluster *>. */ |
| 5190 |
|
| 5191 |
static int cmp_ptr_ptr (const void *p1, const void *p2) |
| 5192 |
{ |
| 5193 |
const function_call_string_cluster *c1 |
| 5194 |
= *(const function_call_string_cluster * const *)p1; |
| 5195 |
const function_call_string_cluster *c2 |
| 5196 |
= *(const function_call_string_cluster * const *)p2; |
| 5197 |
if (int cmp_names |
| 5198 |
= strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (c1->m_fun->decl)), |
| 5199 |
IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (c2->m_fun->decl)))) |
| 5200 |
return cmp_names; |
| 5201 |
return call_string::cmp (c1->m_cs, c2->m_cs); |
| 5202 |
} |
| 5203 |
|
| 5204 |
private: |
| 5205 |
function *m_fun; |
| 5206 |
const call_string &m_cs; |
| 5207 |
typedef ordered_hash_map<const supernode *, supernode_cluster *> map_t; |
| 5208 |
map_t m_map; |
| 5209 |
}; |
| 5210 |
|
| 5211 |
/* Keys for root_cluster. */ |
| 5212 |
|
| 5213 |
struct function_call_string |
| 5214 |
{ |
| 5215 |
function_call_string (function *fun, const call_string *cs) |
| 5216 |
: m_fun (fun), m_cs (cs) |
| 5217 |
{ |
| 5218 |
gcc_assert (fun); |
| 5219 |
gcc_assert (cs); |
| 5220 |
} |
| 5221 |
|
| 5222 |
function *m_fun; |
| 5223 |
const call_string *m_cs; |
| 5224 |
}; |
| 5225 |
|
| 5226 |
} // namespace ana |
| 5227 |
|
| 5228 |
template <> struct default_hash_traits<function_call_string> |
| 5229 |
: public pod_hash_traits<function_call_string> |
| 5230 |
{ |
| 5231 |
static const bool empty_zero_p = false; |
| 5232 |
}; |
| 5233 |
|
| 5234 |
template <> |
| 5235 |
inline hashval_t |
| 5236 |
pod_hash_traits<function_call_string>::hash (value_type v) |
| 5237 |
{ |
| 5238 |
return (pointer_hash <function>::hash (v.m_fun) |
| 5239 |
^ pointer_hash <const call_string>::hash (v.m_cs)); |
| 5240 |
} |
| 5241 |
|
| 5242 |
template <> |
| 5243 |
inline bool |
| 5244 |
pod_hash_traits<function_call_string>::equal (const value_type &existing, |
| 5245 |
const value_type &candidate) |
| 5246 |
{ |
| 5247 |
return existing.m_fun == candidate.m_fun && &existing.m_cs == &candidate.m_cs; |
| 5248 |
} |
| 5249 |
template <> |
| 5250 |
inline void |
| 5251 |
pod_hash_traits<function_call_string>::mark_deleted (value_type &v) |
| 5252 |
{ |
| 5253 |
v.m_fun = reinterpret_cast<function *> (1); |
| 5254 |
} |
| 5255 |
template <> |
| 5256 |
inline void |
| 5257 |
pod_hash_traits<function_call_string>::mark_empty (value_type &v) |
| 5258 |
{ |
| 5259 |
v.m_fun = NULL; |
| 5260 |
} |
| 5261 |
template <> |
| 5262 |
inline bool |
| 5263 |
pod_hash_traits<function_call_string>::is_deleted (value_type v) |
| 5264 |
{ |
| 5265 |
return v.m_fun == reinterpret_cast<function *> (1); |
| 5266 |
} |
| 5267 |
template <> |
| 5268 |
inline bool |
| 5269 |
pod_hash_traits<function_call_string>::is_empty (value_type v) |
| 5270 |
{ |
| 5271 |
return v.m_fun == NULL; |
| 5272 |
} |
| 5273 |
|
| 5274 |
namespace ana { |
| 5275 |
|
| 5276 |
/* Top-level cluster for generating .dot output for exploded graphs, |
| 5277 |
handling the functionless nodes, and grouping the remaining nodes by |
| 5278 |
callstring. */ |
| 5279 |
|
| 5280 |
class root_cluster : public exploded_cluster |
| 5281 |
{ |
| 5282 |
public: |
| 5283 |
~root_cluster () |
| 5284 |
{ |
| 5285 |
for (map_t::iterator iter = m_map.begin (); |
| 5286 |
iter != m_map.end (); |
| 5287 |
++iter) |
| 5288 |
delete (*iter).second; |
| 5289 |
} |
| 5290 |
|
| 5291 |
void dump_dot (graphviz_out *gv, const dump_args_t &args) const final override |
| 5292 |
{ |
| 5293 |
int i; |
| 5294 |
exploded_node *enode; |
| 5295 |
FOR_EACH_VEC_ELT (m_functionless_enodes, i, enode) |
| 5296 |
enode->dump_dot (gv, args); |
| 5297 |
|
| 5298 |
/* Dump m_map, sorting it to avoid churn when comparing dumps. */ |
| 5299 |
auto_vec<function_call_string_cluster *> child_clusters (m_map.elements ()); |
| 5300 |
for (map_t::iterator iter = m_map.begin (); |
| 5301 |
iter != m_map.end (); |
| 5302 |
++iter) |
| 5303 |
child_clusters.quick_push ((*iter).second); |
| 5304 |
|
| 5305 |
child_clusters.qsort (function_call_string_cluster::cmp_ptr_ptr); |
| 5306 |
|
| 5307 |
function_call_string_cluster *child_cluster; |
| 5308 |
FOR_EACH_VEC_ELT (child_clusters, i, child_cluster) |
| 5309 |
child_cluster->dump_dot (gv, args); |
| 5310 |
} |
| 5311 |
|
| 5312 |
void add_node (exploded_node *en) final override |
| 5313 |
{ |
| 5314 |
function *fun = en->get_function (); |
| 5315 |
if (!fun) |
| 5316 |
{ |
| 5317 |
m_functionless_enodes.safe_push (en); |
| 5318 |
return; |
| 5319 |
} |
| 5320 |
|
| 5321 |
const call_string &cs = en->get_point ().get_call_string (); |
| 5322 |
function_call_string key (fun, &cs); |
| 5323 |
function_call_string_cluster **slot = m_map.get (key); |
| 5324 |
if (slot) |
| 5325 |
(*slot)->add_node (en); |
| 5326 |
else |
| 5327 |
{ |
| 5328 |
function_call_string_cluster *child |
| 5329 |
= new function_call_string_cluster (fun, cs); |
| 5330 |
m_map.put (key, child); |
| 5331 |
child->add_node (en); |
| 5332 |
} |
| 5333 |
} |
| 5334 |
|
| 5335 |
private: |
| 5336 |
typedef hash_map<function_call_string, function_call_string_cluster *> map_t; |
| 5337 |
map_t m_map; |
| 5338 |
|
| 5339 |
/* This should just be the origin exploded_node. */ |
| 5340 |
auto_vec <exploded_node *> m_functionless_enodes; |
| 5341 |
}; |
| 5342 |
|
| 5343 |
/* Subclass of range_label for use within |
| 5344 |
exploded_graph::dump_exploded_nodes for implementing |
| 5345 |
-fdump-analyzer-exploded-nodes: a label for a specific |
| 5346 |
exploded_node. */ |
| 5347 |
|
| 5348 |
class enode_label : public range_label |
| 5349 |
{ |
| 5350 |
public: |
| 5351 |
enode_label (const extrinsic_state &ext_state, |
| 5352 |
exploded_node *enode) |
| 5353 |
: m_ext_state (ext_state), m_enode (enode) {} |
| 5354 |
|
| 5355 |
label_text get_text (unsigned) const final override |
| 5356 |
{ |
| 5357 |
pretty_printer pp; |
| 5358 |
pp_format_decoder (&pp) = default_tree_printer; |
| 5359 |
m_enode->get_state ().dump_to_pp (m_ext_state, true, false, &pp); |
| 5360 |
return make_label_text (false, "EN: %i: %s", |
| 5361 |
m_enode->m_index, pp_formatted_text (&pp)); |
| 5362 |
} |
| 5363 |
|
| 5364 |
private: |
| 5365 |
const extrinsic_state &m_ext_state; |
| 5366 |
exploded_node *m_enode; |
| 5367 |
}; |
| 5368 |
|
| 5369 |
/* Postprocessing support for dumping the exploded nodes. |
| 5370 |
Handle -fdump-analyzer-exploded-nodes, |
| 5371 |
-fdump-analyzer-exploded-nodes-2, and the |
| 5372 |
"__analyzer_dump_exploded_nodes" builtin. */ |
| 5373 |
|
| 5374 |
void |
| 5375 |
exploded_graph::dump_exploded_nodes () const |
| 5376 |
{ |
| 5377 |
// TODO |
| 5378 |
/* Locate calls to __analyzer_dump_exploded_nodes. */ |
| 5379 |
// Print how many egs there are for them? |
| 5380 |
/* Better: log them as we go, and record the exploded nodes |
| 5381 |
in question. */ |
| 5382 |
|
| 5383 |
/* Show every enode. */ |
| 5384 |
|
| 5385 |
/* Gather them by stmt, so that we can more clearly see the |
| 5386 |
"hotspots" requiring numerous exploded nodes. */ |
| 5387 |
|
| 5388 |
/* Alternatively, simply throw them all into one big rich_location |
| 5389 |
and see if the label-printing will sort it out... |
| 5390 |
This requires them all to be in the same source file. */ |
| 5391 |
|
| 5392 |
if (flag_dump_analyzer_exploded_nodes) |
| 5393 |
{ |
| 5394 |
auto_timevar tv (TV_ANALYZER_DUMP); |
| 5395 |
gcc_rich_location richloc (UNKNOWN_LOCATION); |
| 5396 |
unsigned i; |
| 5397 |
exploded_node *enode; |
| 5398 |
FOR_EACH_VEC_ELT (m_nodes, i, enode) |
| 5399 |
{ |
| 5400 |
if (const gimple *stmt = enode->get_stmt ()) |
| 5401 |
{ |
| 5402 |
if (get_pure_location (richloc.get_loc ()) == UNKNOWN_LOCATION) |
| 5403 |
richloc.set_range (0, stmt->location, SHOW_RANGE_WITH_CARET); |
| 5404 |
else |
| 5405 |
richloc.add_range (stmt->location, |
| 5406 |
SHOW_RANGE_WITHOUT_CARET, |
| 5407 |
new enode_label (m_ext_state, enode)); |
| 5408 |
} |
| 5409 |
} |
| 5410 |
warning_at (&richloc, 0, "%i exploded nodes", m_nodes.length ()); |
| 5411 |
|
| 5412 |
/* Repeat the warning without all the labels, so that message is visible |
| 5413 |
(the other one may well have scrolled past the terminal limit). */ |
| 5414 |
warning_at (richloc.get_loc (), 0, |
| 5415 |
"%i exploded nodes", m_nodes.length ()); |
| 5416 |
|
| 5417 |
if (m_worklist.length () > 0) |
| 5418 |
warning_at (richloc.get_loc (), 0, |
| 5419 |
"worklist still contains %i nodes", m_worklist.length ()); |
| 5420 |
} |
| 5421 |
|
| 5422 |
/* Dump the egraph in textual form to a dump file. */ |
| 5423 |
if (flag_dump_analyzer_exploded_nodes_2) |
| 5424 |
{ |
| 5425 |
auto_timevar tv (TV_ANALYZER_DUMP); |
| 5426 |
char *filename |
| 5427 |
= concat (dump_base_name, ".eg.txt", NULL); |
| 5428 |
FILE *outf = fopen (filename, "w"); |
| 5429 |
if (!outf) |
| 5430 |
error_at (UNKNOWN_LOCATION, "unable to open %qs for writing", filename); |
| 5431 |
free (filename); |
| 5432 |
|
| 5433 |
fprintf (outf, "exploded graph for %s\n", dump_base_name); |
| 5434 |
fprintf (outf, " nodes: %i\n", m_nodes.length ()); |
| 5435 |
fprintf (outf, " edges: %i\n", m_edges.length ()); |
| 5436 |
|
| 5437 |
unsigned i; |
| 5438 |
exploded_node *enode; |
| 5439 |
FOR_EACH_VEC_ELT (m_nodes, i, enode) |
| 5440 |
{ |
| 5441 |
fprintf (outf, "\nEN %i:\n", enode->m_index); |
| 5442 |
enode->dump_succs_and_preds (outf); |
| 5443 |
pretty_printer pp; |
| 5444 |
enode->get_point ().print (&pp, format (true)); |
| 5445 |
fprintf (outf, "%s\n", pp_formatted_text (&pp)); |
| 5446 |
text_art::dump_to_file (enode->get_state (), outf); |
| 5447 |
} |
| 5448 |
|
| 5449 |
fclose (outf); |
| 5450 |
} |
| 5451 |
|
| 5452 |
/* Dump the egraph in textual form to multiple dump files, one per enode. */ |
| 5453 |
if (flag_dump_analyzer_exploded_nodes_3) |
| 5454 |
{ |
| 5455 |
auto_timevar tv (TV_ANALYZER_DUMP); |
| 5456 |
|
| 5457 |
unsigned i; |
| 5458 |
exploded_node *enode; |
| 5459 |
FOR_EACH_VEC_ELT (m_nodes, i, enode) |
| 5460 |
{ |
| 5461 |
char *filename |
| 5462 |
= xasprintf ("%s.en-%i.txt", dump_base_name, i); |
| 5463 |
FILE *outf = fopen (filename, "w"); |
| 5464 |
if (!outf) |
| 5465 |
error_at (UNKNOWN_LOCATION, "unable to open %qs for writing", |
| 5466 |
filename); |
| 5467 |
free (filename); |
| 5468 |
|
| 5469 |
fprintf (outf, "EN %i:\n", enode->m_index); |
| 5470 |
enode->dump_succs_and_preds (outf); |
| 5471 |
pretty_printer pp; |
| 5472 |
enode->get_point ().print (&pp, format (true)); |
| 5473 |
fprintf (outf, "%s\n", pp_formatted_text (&pp)); |
| 5474 |
text_art::dump_to_file (enode->get_state (), outf); |
| 5475 |
|
| 5476 |
fclose (outf); |
| 5477 |
} |
| 5478 |
} |
| 5479 |
|
| 5480 |
/* Emit a warning at any call to "__analyzer_dump_exploded_nodes", |
| 5481 |
giving the number of processed exploded nodes for "before-stmt", |
| 5482 |
and the IDs of processed, merger, and worklist enodes. |
| 5483 |
|
| 5484 |
We highlight the count of *processed* enodes since this is of most |
| 5485 |
interest in DejaGnu tests for ensuring that state merger has |
| 5486 |
happened. |
| 5487 |
|
| 5488 |
We don't show the count of merger and worklist enodes, as this is |
| 5489 |
more of an implementation detail of the merging/worklist that we |
| 5490 |
don't want to bake into our expected DejaGnu messages. */ |
| 5491 |
|
| 5492 |
unsigned i; |
| 5493 |
exploded_node *enode; |
| 5494 |
hash_set<const gimple *> seen; |
| 5495 |
FOR_EACH_VEC_ELT (m_nodes, i, enode) |
| 5496 |
{ |
| 5497 |
if (enode->get_point ().get_kind () != PK_BEFORE_STMT) |
| 5498 |
continue; |
| 5499 |
|
| 5500 |
if (const gimple *stmt = enode->get_stmt ()) |
| 5501 |
if (const gcall *call = dyn_cast <const gcall *> (stmt)) |
| 5502 |
if (is_special_named_call_p (call, "__analyzer_dump_exploded_nodes", |
| 5503 |
1)) |
| 5504 |
{ |
| 5505 |
if (seen.contains (stmt)) |
| 5506 |
continue; |
| 5507 |
|
| 5508 |
auto_vec<exploded_node *> processed_enodes; |
| 5509 |
auto_vec<exploded_node *> merger_enodes; |
| 5510 |
auto_vec<exploded_node *> worklist_enodes; |
| 5511 |
/* This is O(N^2). */ |
| 5512 |
unsigned j; |
| 5513 |
exploded_node *other_enode; |
| 5514 |
FOR_EACH_VEC_ELT (m_nodes, j, other_enode) |
| 5515 |
{ |
| 5516 |
if (other_enode->get_point ().get_kind () != PK_BEFORE_STMT) |
| 5517 |
continue; |
| 5518 |
if (other_enode->get_stmt () == stmt) |
| 5519 |
switch (other_enode->get_status ()) |
| 5520 |
{ |
| 5521 |
default: |
| 5522 |
gcc_unreachable (); |
| 5523 |
case exploded_node::STATUS_WORKLIST: |
| 5524 |
worklist_enodes.safe_push (other_enode); |
| 5525 |
break; |
| 5526 |
case exploded_node::STATUS_PROCESSED: |
| 5527 |
processed_enodes.safe_push (other_enode); |
| 5528 |
break; |
| 5529 |
case exploded_node::STATUS_MERGER: |
| 5530 |
merger_enodes.safe_push (other_enode); |
| 5531 |
break; |
| 5532 |
} |
| 5533 |
} |
| 5534 |
|
| 5535 |
pretty_printer pp; |
| 5536 |
pp_character (&pp, '['); |
| 5537 |
print_enode_indices (&pp, processed_enodes); |
| 5538 |
if (merger_enodes.length () > 0) |
| 5539 |
{ |
| 5540 |
pp_string (&pp, "] merger(s): ["); |
| 5541 |
print_enode_indices (&pp, merger_enodes); |
| 5542 |
} |
| 5543 |
if (worklist_enodes.length () > 0) |
| 5544 |
{ |
| 5545 |
pp_string (&pp, "] worklist: ["); |
| 5546 |
print_enode_indices (&pp, worklist_enodes); |
| 5547 |
} |
| 5548 |
pp_character (&pp, ']'); |
| 5549 |
|
| 5550 |
warning_n (stmt->location, 0, processed_enodes.length (), |
| 5551 |
"%i processed enode: %s", |
| 5552 |
"%i processed enodes: %s", |
| 5553 |
processed_enodes.length (), pp_formatted_text (&pp)); |
| 5554 |
seen.add (stmt); |
| 5555 |
|
| 5556 |
/* If the argument is non-zero, then print all of the states |
| 5557 |
of the various enodes. */ |
| 5558 |
tree t_arg = fold (gimple_call_arg (call, 0)); |
| 5559 |
if (TREE_CODE (t_arg) != INTEGER_CST) |
| 5560 |
{ |
| 5561 |
error_at (call->location, |
| 5562 |
"integer constant required for arg 1"); |
| 5563 |
return; |
| 5564 |
} |
| 5565 |
int i_arg = TREE_INT_CST_LOW (t_arg); |
| 5566 |
if (i_arg) |
| 5567 |
{ |
| 5568 |
exploded_node *other_enode; |
| 5569 |
FOR_EACH_VEC_ELT (processed_enodes, j, other_enode) |
| 5570 |
{ |
| 5571 |
fprintf (stderr, "%i of %i: EN %i:\n", |
| 5572 |
j + 1, processed_enodes.length (), |
| 5573 |
other_enode->m_index); |
| 5574 |
other_enode->dump_succs_and_preds (stderr); |
| 5575 |
/* Dump state. */ |
| 5576 |
other_enode->get_state ().dump (m_ext_state, false); |
| 5577 |
} |
| 5578 |
} |
| 5579 |
} |
| 5580 |
} |
| 5581 |
} |
| 5582 |
|
| 5583 |
DEBUG_FUNCTION exploded_node * |
| 5584 |
exploded_graph::get_node_by_index (int idx) const |
| 5585 |
{ |
| 5586 |
exploded_node *enode = m_nodes[idx]; |
| 5587 |
gcc_assert (enode->m_index == idx); |
| 5588 |
return enode; |
| 5589 |
} |
| 5590 |
|
| 5591 |
/* Ensure that there is an exploded_node for a top-level call to FNDECL. */ |
| 5592 |
|
| 5593 |
void |
| 5594 |
exploded_graph::on_escaped_function (tree fndecl) |
| 5595 |
{ |
| 5596 |
logger * const logger = get_logger (); |
| 5597 |
LOG_FUNC_1 (logger, "%qE", fndecl); |
| 5598 |
|
| 5599 |
cgraph_node *cgnode = cgraph_node::get (fndecl); |
| 5600 |
if (!cgnode) |
| 5601 |
return; |
| 5602 |
|
| 5603 |
function *fun = cgnode->get_fun (); |
| 5604 |
if (!fun) |
| 5605 |
return; |
| 5606 |
|
| 5607 |
if (!gimple_has_body_p (fndecl)) |
| 5608 |
return; |
| 5609 |
|
| 5610 |
exploded_node *enode = add_function_entry (*fun); |
| 5611 |
if (logger) |
| 5612 |
{ |
| 5613 |
if (enode) |
| 5614 |
logger->log ("created EN %i for %qE entrypoint", |
| 5615 |
enode->m_index, fun->decl); |
| 5616 |
else |
| 5617 |
logger->log ("did not create enode for %qE entrypoint", fun->decl); |
| 5618 |
} |
| 5619 |
} |
| 5620 |
|
| 5621 |
/* A collection of classes for visualizing the callgraph in .dot form |
| 5622 |
(as represented in the supergraph). */ |
| 5623 |
|
| 5624 |
/* Forward decls. */ |
| 5625 |
class viz_callgraph_node; |
| 5626 |
class viz_callgraph_edge; |
| 5627 |
class viz_callgraph; |
| 5628 |
class viz_callgraph_cluster; |
| 5629 |
|
| 5630 |
/* Traits for using "digraph.h" to visualize the callgraph. */ |
| 5631 |
|
| 5632 |
struct viz_callgraph_traits |
| 5633 |
{ |
| 5634 |
typedef viz_callgraph_node node_t; |
| 5635 |
typedef viz_callgraph_edge edge_t; |
| 5636 |
typedef viz_callgraph graph_t; |
| 5637 |
struct dump_args_t |
| 5638 |
{ |
| 5639 |
dump_args_t (const exploded_graph *eg) : m_eg (eg) {} |
| 5640 |
const exploded_graph *m_eg; |
| 5641 |
}; |
| 5642 |
typedef viz_callgraph_cluster cluster_t; |
| 5643 |
}; |
| 5644 |
|
| 5645 |
/* Subclass of dnode representing a function within the callgraph. */ |
| 5646 |
|
| 5647 |
class viz_callgraph_node : public dnode<viz_callgraph_traits> |
| 5648 |
{ |
| 5649 |
friend class viz_callgraph; |
| 5650 |
|
| 5651 |
public: |
| 5652 |
viz_callgraph_node (function *fun, int index) |
| 5653 |
: m_fun (fun), m_index (index), m_num_supernodes (0), m_num_superedges (0) |
| 5654 |
{ |
| 5655 |
gcc_assert (fun); |
| 5656 |
} |
| 5657 |
|
| 5658 |
void dump_dot (graphviz_out *gv, const dump_args_t &args) const final override |
| 5659 |
{ |
| 5660 |
pretty_printer *pp = gv->get_pp (); |
| 5661 |
|
| 5662 |
dump_dot_id (pp); |
| 5663 |
pp_printf (pp, " [shape=none,margin=0,style=filled,fillcolor=%s,label=\"", |
| 5664 |
"lightgrey"); |
| 5665 |
pp_write_text_to_stream (pp); |
| 5666 |
|
| 5667 |
pp_printf (pp, "VCG: %i: %s", m_index, function_name (m_fun)); |
| 5668 |
pp_newline (pp); |
| 5669 |
|
| 5670 |
pp_printf (pp, "supernodes: %i\n", m_num_supernodes); |
| 5671 |
pp_newline (pp); |
| 5672 |
|
| 5673 |
pp_printf (pp, "superedges: %i\n", m_num_superedges); |
| 5674 |
pp_newline (pp); |
| 5675 |
|
| 5676 |
if (args.m_eg) |
| 5677 |
{ |
| 5678 |
unsigned i; |
| 5679 |
exploded_node *enode; |
| 5680 |
unsigned num_enodes = 0; |
| 5681 |
FOR_EACH_VEC_ELT (args.m_eg->m_nodes, i, enode) |
| 5682 |
{ |
| 5683 |
if (enode->get_point ().get_function () == m_fun) |
| 5684 |
num_enodes++; |
| 5685 |
} |
| 5686 |
pp_printf (pp, "enodes: %i\n", num_enodes); |
| 5687 |
pp_newline (pp); |
| 5688 |
|
| 5689 |
// TODO: also show the per-callstring breakdown |
| 5690 |
const exploded_graph::call_string_data_map_t *per_cs_data |
| 5691 |
= args.m_eg->get_per_call_string_data (); |
| 5692 |
for (exploded_graph::call_string_data_map_t::iterator iter |
| 5693 |
= per_cs_data->begin (); |
| 5694 |
iter != per_cs_data->end (); |
| 5695 |
++iter) |
| 5696 |
{ |
| 5697 |
const call_string *cs = (*iter).first; |
| 5698 |
//per_call_string_data *data = (*iter).second; |
| 5699 |
num_enodes = 0; |
| 5700 |
FOR_EACH_VEC_ELT (args.m_eg->m_nodes, i, enode) |
| 5701 |
{ |
| 5702 |
if (enode->get_point ().get_function () == m_fun |
| 5703 |
&& &enode->get_point ().get_call_string () == cs) |
| 5704 |
num_enodes++; |
| 5705 |
} |
| 5706 |
if (num_enodes > 0) |
| 5707 |
{ |
| 5708 |
cs->print (pp); |
| 5709 |
pp_printf (pp, ": %i\n", num_enodes); |
| 5710 |
} |
| 5711 |
} |
| 5712 |
|
| 5713 |
/* Show any summaries. */ |
| 5714 |
per_function_data *data = args.m_eg->get_per_function_data (m_fun); |
| 5715 |
if (data) |
| 5716 |
{ |
| 5717 |
pp_newline (pp); |
| 5718 |
pp_printf (pp, "summaries: %i\n", data->m_summaries.length ()); |
| 5719 |
for (auto summary : data->m_summaries) |
| 5720 |
{ |
| 5721 |
pp_printf (pp, "\nsummary: %s:\n", summary->get_desc ().get ()); |
| 5722 |
const extrinsic_state &ext_state = args.m_eg->get_ext_state (); |
| 5723 |
const program_state &state = summary->get_state (); |
| 5724 |
state.dump_to_pp (ext_state, false, true, pp); |
| 5725 |
pp_newline (pp); |
| 5726 |
} |
| 5727 |
} |
| 5728 |
} |
| 5729 |
|
| 5730 |
pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/true); |
| 5731 |
pp_string (pp, "\"];\n\n"); |
| 5732 |
pp_flush (pp); |
| 5733 |
} |
| 5734 |
|
| 5735 |
void dump_dot_id (pretty_printer *pp) const |
| 5736 |
{ |
| 5737 |
pp_printf (pp, "vcg_%i", m_index); |
| 5738 |
} |
| 5739 |
|
| 5740 |
private: |
| 5741 |
function *m_fun; |
| 5742 |
int m_index; |
| 5743 |
int m_num_supernodes; |
| 5744 |
int m_num_superedges; |
| 5745 |
}; |
| 5746 |
|
| 5747 |
/* Subclass of dedge representing a callgraph edge. */ |
| 5748 |
|
| 5749 |
class viz_callgraph_edge : public dedge<viz_callgraph_traits> |
| 5750 |
{ |
| 5751 |
public: |
| 5752 |
viz_callgraph_edge (viz_callgraph_node *src, viz_callgraph_node *dest) |
| 5753 |
: dedge<viz_callgraph_traits> (src, dest) |
| 5754 |
{} |
| 5755 |
|
| 5756 |
void dump_dot (graphviz_out *gv, const dump_args_t &) const |
| 5757 |
final override |
| 5758 |
{ |
| 5759 |
pretty_printer *pp = gv->get_pp (); |
| 5760 |
|
| 5761 |
const char *style = "\"solid,bold\""; |
| 5762 |
const char *color = "black"; |
| 5763 |
int weight = 10; |
| 5764 |
const char *constraint = "true"; |
| 5765 |
|
| 5766 |
m_src->dump_dot_id (pp); |
| 5767 |
pp_string (pp, " -> "); |
| 5768 |
m_dest->dump_dot_id (pp); |
| 5769 |
pp_printf (pp, |
| 5770 |
(" [style=%s, color=%s, weight=%d, constraint=%s," |
| 5771 |
" headlabel=\""), |
| 5772 |
style, color, weight, constraint); |
| 5773 |
pp_printf (pp, "\"];\n"); |
| 5774 |
} |
| 5775 |
}; |
| 5776 |
|
| 5777 |
/* Subclass of digraph representing the callgraph. */ |
| 5778 |
|
| 5779 |
class viz_callgraph : public digraph<viz_callgraph_traits> |
| 5780 |
{ |
| 5781 |
public: |
| 5782 |
viz_callgraph (const supergraph &sg); |
| 5783 |
|
| 5784 |
viz_callgraph_node *get_vcg_node_for_function (function *fun) |
| 5785 |
{ |
| 5786 |
return *m_map.get (fun); |
| 5787 |
} |
| 5788 |
|
| 5789 |
viz_callgraph_node *get_vcg_node_for_snode (supernode *snode) |
| 5790 |
{ |
| 5791 |
return get_vcg_node_for_function (snode->m_fun); |
| 5792 |
} |
| 5793 |
|
| 5794 |
private: |
| 5795 |
hash_map<function *, viz_callgraph_node *> m_map; |
| 5796 |
}; |
| 5797 |
|
| 5798 |
/* Placeholder subclass of cluster. */ |
| 5799 |
|
| 5800 |
class viz_callgraph_cluster : public cluster<viz_callgraph_traits> |
| 5801 |
{ |
| 5802 |
}; |
| 5803 |
|
| 5804 |
/* viz_callgraph's ctor. */ |
| 5805 |
|
| 5806 |
viz_callgraph::viz_callgraph (const supergraph &sg) |
| 5807 |
{ |
| 5808 |
cgraph_node *node; |
| 5809 |
FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node) |
| 5810 |
{ |
| 5811 |
function *fun = node->get_fun (); |
| 5812 |
viz_callgraph_node *vcg_node |
| 5813 |
= new viz_callgraph_node (fun, m_nodes.length ()); |
| 5814 |
m_map.put (fun, vcg_node); |
| 5815 |
add_node (vcg_node); |
| 5816 |
} |
| 5817 |
|
| 5818 |
unsigned i; |
| 5819 |
superedge *sedge; |
| 5820 |
FOR_EACH_VEC_ELT (sg.m_edges, i, sedge) |
| 5821 |
{ |
| 5822 |
viz_callgraph_node *vcg_src = get_vcg_node_for_snode (sedge->m_src); |
| 5823 |
if (vcg_src->m_fun) |
| 5824 |
get_vcg_node_for_function (vcg_src->m_fun)->m_num_superedges++; |
| 5825 |
if (sedge->dyn_cast_call_superedge ()) |
| 5826 |
{ |
| 5827 |
viz_callgraph_node *vcg_dest = get_vcg_node_for_snode (sedge->m_dest); |
| 5828 |
viz_callgraph_edge *vcg_edge |
| 5829 |
= new viz_callgraph_edge (vcg_src, vcg_dest); |
| 5830 |
add_edge (vcg_edge); |
| 5831 |
} |
| 5832 |
} |
| 5833 |
|
| 5834 |
supernode *snode; |
| 5835 |
FOR_EACH_VEC_ELT (sg.m_nodes, i, snode) |
| 5836 |
{ |
| 5837 |
if (snode->m_fun) |
| 5838 |
get_vcg_node_for_function (snode->m_fun)->m_num_supernodes++; |
| 5839 |
} |
| 5840 |
} |
| 5841 |
|
| 5842 |
/* Dump the callgraph to FILENAME. */ |
| 5843 |
|
| 5844 |
static void |
| 5845 |
dump_callgraph (const supergraph &sg, const char *filename, |
| 5846 |
const exploded_graph *eg) |
| 5847 |
{ |
| 5848 |
FILE *outf = fopen (filename, "w"); |
| 5849 |
if (!outf) |
| 5850 |
return; |
| 5851 |
|
| 5852 |
// TODO |
| 5853 |
viz_callgraph vcg (sg); |
| 5854 |
vcg.dump_dot (filename, NULL, viz_callgraph_traits::dump_args_t (eg)); |
| 5855 |
|
| 5856 |
fclose (outf); |
| 5857 |
} |
| 5858 |
|
| 5859 |
/* Dump the callgraph to "<srcfile>.callgraph.dot". */ |
| 5860 |
|
| 5861 |
static void |
| 5862 |
dump_callgraph (const supergraph &sg, const exploded_graph *eg) |
| 5863 |
{ |
| 5864 |
auto_timevar tv (TV_ANALYZER_DUMP); |
| 5865 |
char *filename = concat (dump_base_name, ".callgraph.dot", NULL); |
| 5866 |
dump_callgraph (sg, filename, eg); |
| 5867 |
free (filename); |
| 5868 |
} |
| 5869 |
|
| 5870 |
/* Subclass of dot_annotator for implementing |
| 5871 |
DUMP_BASE_NAME.supergraph-eg.dot, a post-analysis dump of the supergraph. |
| 5872 |
|
| 5873 |
Annotate the supergraph nodes by printing the exploded nodes in concise |
| 5874 |
form within them, next to their pertinent statements where appropriate, |
| 5875 |
colorizing the exploded nodes based on sm-state. |
| 5876 |
Also show saved diagnostics within the exploded nodes, giving information |
| 5877 |
on whether they were feasible, and, if infeasible, where the problem |
| 5878 |
was. */ |
| 5879 |
|
| 5880 |
class exploded_graph_annotator : public dot_annotator |
| 5881 |
{ |
| 5882 |
public: |
| 5883 |
exploded_graph_annotator (const exploded_graph &eg) |
| 5884 |
: m_eg (eg) |
| 5885 |
{ |
| 5886 |
/* Avoid O(N^2) by prepopulating m_enodes_per_snodes. */ |
| 5887 |
unsigned i; |
| 5888 |
supernode *snode; |
| 5889 |
FOR_EACH_VEC_ELT (eg.get_supergraph ().m_nodes, i, snode) |
| 5890 |
m_enodes_per_snodes.safe_push (new auto_vec <exploded_node *> ()); |
| 5891 |
exploded_node *enode; |
| 5892 |
FOR_EACH_VEC_ELT (m_eg.m_nodes, i, enode) |
| 5893 |
if (enode->get_supernode ()) |
| 5894 |
m_enodes_per_snodes[enode->get_supernode ()->m_index]->safe_push (enode); |
| 5895 |
} |
| 5896 |
|
| 5897 |
/* Show exploded nodes for BEFORE_SUPERNODE points before N. */ |
| 5898 |
bool add_node_annotations (graphviz_out *gv, const supernode &n, |
| 5899 |
bool within_table) |
| 5900 |
const final override |
| 5901 |
{ |
| 5902 |
if (!within_table) |
| 5903 |
return false; |
| 5904 |
gv->begin_tr (); |
| 5905 |
pretty_printer *pp = gv->get_pp (); |
| 5906 |
|
| 5907 |
gv->begin_td (); |
| 5908 |
pp_string (pp, "BEFORE"); |
| 5909 |
pp_printf (pp, " (scc: %i)", m_eg.get_scc_id (n)); |
| 5910 |
gv->end_td (); |
| 5911 |
|
| 5912 |
unsigned i; |
| 5913 |
exploded_node *enode; |
| 5914 |
bool had_enode = false; |
| 5915 |
FOR_EACH_VEC_ELT (*m_enodes_per_snodes[n.m_index], i, enode) |
| 5916 |
{ |
| 5917 |
gcc_assert (enode->get_supernode () == &n); |
| 5918 |
const program_point &point = enode->get_point (); |
| 5919 |
if (point.get_kind () != PK_BEFORE_SUPERNODE) |
| 5920 |
continue; |
| 5921 |
print_enode (gv, enode); |
| 5922 |
had_enode = true; |
| 5923 |
} |
| 5924 |
if (!had_enode) |
| 5925 |
pp_string (pp, "<TD BGCOLOR=\"red\">UNREACHED</TD>"); |
| 5926 |
pp_flush (pp); |
| 5927 |
gv->end_tr (); |
| 5928 |
return true; |
| 5929 |
} |
| 5930 |
|
| 5931 |
/* Show exploded nodes for STMT. */ |
| 5932 |
void add_stmt_annotations (graphviz_out *gv, const gimple *stmt, |
| 5933 |
bool within_row) |
| 5934 |
const final override |
| 5935 |
{ |
| 5936 |
if (!within_row) |
| 5937 |
return; |
| 5938 |
pretty_printer *pp = gv->get_pp (); |
| 5939 |
|
| 5940 |
const supernode *snode |
| 5941 |
= m_eg.get_supergraph ().get_supernode_for_stmt (stmt); |
| 5942 |
unsigned i; |
| 5943 |
exploded_node *enode; |
| 5944 |
bool had_td = false; |
| 5945 |
FOR_EACH_VEC_ELT (*m_enodes_per_snodes[snode->m_index], i, enode) |
| 5946 |
{ |
| 5947 |
const program_point &point = enode->get_point (); |
| 5948 |
if (point.get_kind () != PK_BEFORE_STMT) |
| 5949 |
continue; |
| 5950 |
if (point.get_stmt () != stmt) |
| 5951 |
continue; |
| 5952 |
print_enode (gv, enode); |
| 5953 |
had_td = true; |
| 5954 |
} |
| 5955 |
pp_flush (pp); |
| 5956 |
if (!had_td) |
| 5957 |
{ |
| 5958 |
gv->begin_td (); |
| 5959 |
gv->end_td (); |
| 5960 |
} |
| 5961 |
} |
| 5962 |
|
| 5963 |
/* Show exploded nodes for AFTER_SUPERNODE points after N. */ |
| 5964 |
bool add_after_node_annotations (graphviz_out *gv, const supernode &n) |
| 5965 |
const final override |
| 5966 |
{ |
| 5967 |
gv->begin_tr (); |
| 5968 |
pretty_printer *pp = gv->get_pp (); |
| 5969 |
|
| 5970 |
gv->begin_td (); |
| 5971 |
pp_string (pp, "AFTER"); |
| 5972 |
gv->end_td (); |
| 5973 |
|
| 5974 |
unsigned i; |
| 5975 |
exploded_node *enode; |
| 5976 |
FOR_EACH_VEC_ELT (*m_enodes_per_snodes[n.m_index], i, enode) |
| 5977 |
{ |
| 5978 |
gcc_assert (enode->get_supernode () == &n); |
| 5979 |
const program_point &point = enode->get_point (); |
| 5980 |
if (point.get_kind () != PK_AFTER_SUPERNODE) |
| 5981 |
continue; |
| 5982 |
print_enode (gv, enode); |
| 5983 |
} |
| 5984 |
pp_flush (pp); |
| 5985 |
gv->end_tr (); |
| 5986 |
return true; |
| 5987 |
} |
| 5988 |
|
| 5989 |
private: |
| 5990 |
/* Concisely print a TD element for ENODE, showing the index, status, |
| 5991 |
and any saved_diagnostics at the enode. Colorize it to show sm-state. |
| 5992 |
|
| 5993 |
Ideally we'd dump ENODE's state here, hidden behind some kind of |
| 5994 |
interactive disclosure method like a tooltip, so that the states |
| 5995 |
can be explored without overwhelming the graph. |
| 5996 |
However, I wasn't able to get graphviz/xdot to show tooltips on |
| 5997 |
individual elements within a HTML-like label. */ |
| 5998 |
void print_enode (graphviz_out *gv, const exploded_node *enode) const |
| 5999 |
{ |
| 6000 |
pretty_printer *pp = gv->get_pp (); |
| 6001 |
pp_printf (pp, "<TD BGCOLOR=\"%s\">", |
| 6002 |
enode->get_dot_fillcolor ()); |
| 6003 |
pp_printf (pp, "<TABLE BORDER=\"0\">"); |
| 6004 |
gv->begin_trtd (); |
| 6005 |
pp_printf (pp, "EN: %i", enode->m_index); |
| 6006 |
switch (enode->get_status ()) |
| 6007 |
{ |
| 6008 |
default: |
| 6009 |
gcc_unreachable (); |
| 6010 |
case exploded_node::STATUS_WORKLIST: |
| 6011 |
pp_string (pp, "(W)"); |
| 6012 |
break; |
| 6013 |
case exploded_node::STATUS_PROCESSED: |
| 6014 |
break; |
| 6015 |
case exploded_node::STATUS_MERGER: |
| 6016 |
pp_string (pp, "(M)"); |
| 6017 |
break; |
| 6018 |
case exploded_node::STATUS_BULK_MERGED: |
| 6019 |
pp_string (pp, "(BM)"); |
| 6020 |
break; |
| 6021 |
} |
| 6022 |
gv->end_tdtr (); |
| 6023 |
|
| 6024 |
/* Dump any saved_diagnostics at this enode. */ |
| 6025 |
for (unsigned i = 0; i < enode->get_num_diagnostics (); i++) |
| 6026 |
{ |
| 6027 |
const saved_diagnostic *sd = enode->get_saved_diagnostic (i); |
| 6028 |
print_saved_diagnostic (gv, sd); |
| 6029 |
} |
| 6030 |
pp_printf (pp, "</TABLE>"); |
| 6031 |
pp_printf (pp, "</TD>"); |
| 6032 |
} |
| 6033 |
|
| 6034 |
/* Print a TABLE element for SD, showing the kind, the length of the |
| 6035 |
exploded_path, whether the path was feasible, and if infeasible, |
| 6036 |
what the problem was. */ |
| 6037 |
void print_saved_diagnostic (graphviz_out *gv, |
| 6038 |
const saved_diagnostic *sd) const |
| 6039 |
{ |
| 6040 |
pretty_printer *pp = gv->get_pp (); |
| 6041 |
gv->begin_trtd (); |
| 6042 |
pp_printf (pp, "<TABLE BORDER=\"0\">"); |
| 6043 |
gv->begin_tr (); |
| 6044 |
pp_string (pp, "<TD BGCOLOR=\"green\">"); |
| 6045 |
pp_printf (pp, "DIAGNOSTIC: %s", sd->m_d->get_kind ()); |
| 6046 |
gv->end_tdtr (); |
| 6047 |
gv->begin_trtd (); |
| 6048 |
if (sd->get_best_epath ()) |
| 6049 |
pp_printf (pp, "epath length: %i", sd->get_epath_length ()); |
| 6050 |
else |
| 6051 |
pp_printf (pp, "no best epath"); |
| 6052 |
gv->end_tdtr (); |
| 6053 |
if (const feasibility_problem *p = sd->get_feasibility_problem ()) |
| 6054 |
{ |
| 6055 |
gv->begin_trtd (); |
| 6056 |
pp_printf (pp, "INFEASIBLE at eedge %i: EN:%i -> EN:%i", |
| 6057 |
p->m_eedge_idx, |
| 6058 |
p->m_eedge.m_src->m_index, |
| 6059 |
p->m_eedge.m_dest->m_index); |
| 6060 |
pp_write_text_as_html_like_dot_to_stream (pp); |
| 6061 |
gv->end_tdtr (); |
| 6062 |
gv->begin_trtd (); |
| 6063 |
p->m_eedge.m_sedge->dump (pp); |
| 6064 |
pp_write_text_as_html_like_dot_to_stream (pp); |
| 6065 |
gv->end_tdtr (); |
| 6066 |
gv->begin_trtd (); |
| 6067 |
pp_gimple_stmt_1 (pp, p->m_last_stmt, 0, (dump_flags_t)0); |
| 6068 |
pp_write_text_as_html_like_dot_to_stream (pp); |
| 6069 |
gv->end_tdtr (); |
| 6070 |
/* Ideally we'd print p->m_model here; see the notes above about |
| 6071 |
tooltips. */ |
| 6072 |
} |
| 6073 |
pp_printf (pp, "</TABLE>"); |
| 6074 |
gv->end_tdtr (); |
| 6075 |
} |
| 6076 |
|
| 6077 |
const exploded_graph &m_eg; |
| 6078 |
auto_delete_vec<auto_vec <exploded_node *> > m_enodes_per_snodes; |
| 6079 |
}; |
| 6080 |
|
| 6081 |
/* Implement -fdump-analyzer-json. */ |
| 6082 |
|
| 6083 |
static void |
| 6084 |
dump_analyzer_json (const supergraph &sg, |
| 6085 |
const exploded_graph &eg) |
| 6086 |
{ |
| 6087 |
auto_timevar tv (TV_ANALYZER_DUMP); |
| 6088 |
char *filename = concat (dump_base_name, ".analyzer.json.gz", NULL); |
| 6089 |
gzFile output = gzopen (filename, "w"); |
| 6090 |
if (!output) |
| 6091 |
{ |
| 6092 |
error_at (UNKNOWN_LOCATION, "unable to open %qs for writing", filename); |
| 6093 |
free (filename); |
| 6094 |
return; |
| 6095 |
} |
| 6096 |
|
| 6097 |
auto toplev_obj = ::make_unique<json::object> (); |
| 6098 |
toplev_obj->set ("sgraph", sg.to_json ()); |
| 6099 |
toplev_obj->set ("egraph", eg.to_json ()); |
| 6100 |
|
| 6101 |
pretty_printer pp; |
| 6102 |
toplev_obj->print (&pp, flag_diagnostics_json_formatting); |
| 6103 |
pp_formatted_text (&pp); |
| 6104 |
|
| 6105 |
if (gzputs (output, pp_formatted_text (&pp)) == EOF |
| 6106 |
|| gzclose (output)) |
| 6107 |
error_at (UNKNOWN_LOCATION, "error writing %qs", filename); |
| 6108 |
|
| 6109 |
free (filename); |
| 6110 |
} |
| 6111 |
|
| 6112 |
/* Concrete subclass of plugin_analyzer_init_iface, allowing plugins |
| 6113 |
to register new state machines. */ |
| 6114 |
|
| 6115 |
class plugin_analyzer_init_impl : public plugin_analyzer_init_iface |
| 6116 |
{ |
| 6117 |
public: |
| 6118 |
plugin_analyzer_init_impl (auto_delete_vec <state_machine> *checkers, |
| 6119 |
known_function_manager *known_fn_mgr, |
| 6120 |
logger *logger) |
| 6121 |
: m_checkers (checkers), |
| 6122 |
m_known_fn_mgr (known_fn_mgr), |
| 6123 |
m_logger (logger) |
| 6124 |
{} |
| 6125 |
|
| 6126 |
void register_state_machine (std::unique_ptr<state_machine> sm) final override |
| 6127 |
{ |
| 6128 |
LOG_SCOPE (m_logger); |
| 6129 |
m_checkers->safe_push (sm.release ()); |
| 6130 |
} |
| 6131 |
|
| 6132 |
void register_known_function (const char *name, |
| 6133 |
std::unique_ptr<known_function> kf) final override |
| 6134 |
{ |
| 6135 |
LOG_SCOPE (m_logger); |
| 6136 |
m_known_fn_mgr->add (name, std::move (kf)); |
| 6137 |
} |
| 6138 |
|
| 6139 |
logger *get_logger () const final override |
| 6140 |
{ |
| 6141 |
return m_logger; |
| 6142 |
} |
| 6143 |
|
| 6144 |
private: |
| 6145 |
auto_delete_vec <state_machine> *m_checkers; |
| 6146 |
known_function_manager *m_known_fn_mgr; |
| 6147 |
logger *m_logger; |
| 6148 |
}; |
| 6149 |
|
| 6150 |
/* Run the analysis "engine". */ |
| 6151 |
|
| 6152 |
void |
| 6153 |
impl_run_checkers (logger *logger) |
| 6154 |
{ |
| 6155 |
LOG_SCOPE (logger); |
| 6156 |
|
| 6157 |
if (logger) |
| 6158 |
{ |
| 6159 |
logger->log ("BITS_BIG_ENDIAN: %i", BITS_BIG_ENDIAN ? 1 : 0); |
| 6160 |
logger->log ("BYTES_BIG_ENDIAN: %i", BYTES_BIG_ENDIAN ? 1 : 0); |
| 6161 |
logger->log ("WORDS_BIG_ENDIAN: %i", WORDS_BIG_ENDIAN ? 1 : 0); |
| 6162 |
log_stashed_constants (logger); |
| 6163 |
} |
| 6164 |
|
| 6165 |
/* If using LTO, ensure that the cgraph nodes have function bodies. */ |
| 6166 |
cgraph_node *node; |
| 6167 |
FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node) |
| 6168 |
node->get_untransformed_body (); |
| 6169 |
|
| 6170 |
/* Create the supergraph. */ |
| 6171 |
supergraph sg (logger); |
| 6172 |
|
| 6173 |
engine eng (&sg, logger); |
| 6174 |
|
| 6175 |
state_purge_map *purge_map = NULL; |
| 6176 |
|
| 6177 |
if (flag_analyzer_state_purge) |
| 6178 |
purge_map = new state_purge_map (sg, eng.get_model_manager (), logger); |
| 6179 |
|
| 6180 |
if (flag_dump_analyzer_supergraph) |
| 6181 |
{ |
| 6182 |
/* Dump supergraph pre-analysis. */ |
| 6183 |
auto_timevar tv (TV_ANALYZER_DUMP); |
| 6184 |
char *filename = concat (dump_base_name, ".supergraph.dot", NULL); |
| 6185 |
supergraph::dump_args_t args ((enum supergraph_dot_flags)0, NULL); |
| 6186 |
sg.dump_dot (filename, args); |
| 6187 |
free (filename); |
| 6188 |
} |
| 6189 |
|
| 6190 |
if (flag_dump_analyzer_state_purge) |
| 6191 |
{ |
| 6192 |
auto_timevar tv (TV_ANALYZER_DUMP); |
| 6193 |
state_purge_annotator a (purge_map); |
| 6194 |
char *filename = concat (dump_base_name, ".state-purge.dot", NULL); |
| 6195 |
supergraph::dump_args_t args ((enum supergraph_dot_flags)0, &a); |
| 6196 |
sg.dump_dot (filename, args); |
| 6197 |
free (filename); |
| 6198 |
} |
| 6199 |
|
| 6200 |
auto_delete_vec <state_machine> checkers; |
| 6201 |
make_checkers (checkers, logger); |
| 6202 |
|
| 6203 |
register_known_functions (*eng.get_known_function_manager (), |
| 6204 |
*eng.get_model_manager ()); |
| 6205 |
|
| 6206 |
plugin_analyzer_init_impl data (&checkers, |
| 6207 |
eng.get_known_function_manager (), |
| 6208 |
logger); |
| 6209 |
invoke_plugin_callbacks (PLUGIN_ANALYZER_INIT, &data); |
| 6210 |
|
| 6211 |
if (logger) |
| 6212 |
{ |
| 6213 |
int i; |
| 6214 |
state_machine *sm; |
| 6215 |
FOR_EACH_VEC_ELT (checkers, i, sm) |
| 6216 |
logger->log ("checkers[%i]: %s", i, sm->get_name ()); |
| 6217 |
} |
| 6218 |
|
| 6219 |
/* Extrinsic state shared by nodes in the graph. */ |
| 6220 |
const extrinsic_state ext_state (checkers, &eng, logger); |
| 6221 |
|
| 6222 |
const analysis_plan plan (sg, logger); |
| 6223 |
|
| 6224 |
/* The exploded graph. */ |
| 6225 |
exploded_graph eg (sg, logger, ext_state, purge_map, plan, |
| 6226 |
analyzer_verbosity); |
| 6227 |
|
| 6228 |
/* Add entrypoints to the graph for externally-callable functions. */ |
| 6229 |
eg.build_initial_worklist (); |
| 6230 |
|
| 6231 |
/* Now process the worklist, exploring the <point, state> graph. */ |
| 6232 |
eg.process_worklist (); |
| 6233 |
|
| 6234 |
if (warn_analyzer_infinite_loop) |
| 6235 |
eg.detect_infinite_loops (); |
| 6236 |
|
| 6237 |
if (flag_dump_analyzer_exploded_graph) |
| 6238 |
{ |
| 6239 |
auto_timevar tv (TV_ANALYZER_DUMP); |
| 6240 |
char *filename |
| 6241 |
= concat (dump_base_name, ".eg.dot", NULL); |
| 6242 |
exploded_graph::dump_args_t args (eg); |
| 6243 |
root_cluster c; |
| 6244 |
eg.dump_dot (filename, &c, args); |
| 6245 |
free (filename); |
| 6246 |
} |
| 6247 |
|
| 6248 |
/* Now emit any saved diagnostics. */ |
| 6249 |
eg.get_diagnostic_manager ().emit_saved_diagnostics (eg); |
| 6250 |
|
| 6251 |
eg.dump_exploded_nodes (); |
| 6252 |
|
| 6253 |
eg.log_stats (); |
| 6254 |
|
| 6255 |
if (flag_dump_analyzer_callgraph) |
| 6256 |
dump_callgraph (sg, &eg); |
| 6257 |
|
| 6258 |
if (flag_dump_analyzer_supergraph) |
| 6259 |
{ |
| 6260 |
/* Dump post-analysis form of supergraph. */ |
| 6261 |
auto_timevar tv (TV_ANALYZER_DUMP); |
| 6262 |
char *filename = concat (dump_base_name, ".supergraph-eg.dot", NULL); |
| 6263 |
exploded_graph_annotator a (eg); |
| 6264 |
supergraph::dump_args_t args ((enum supergraph_dot_flags)0, &a); |
| 6265 |
sg.dump_dot (filename, args); |
| 6266 |
free (filename); |
| 6267 |
} |
| 6268 |
|
| 6269 |
if (flag_dump_analyzer_json) |
| 6270 |
dump_analyzer_json (sg, eg); |
| 6271 |
|
| 6272 |
if (flag_dump_analyzer_untracked) |
| 6273 |
eng.get_model_manager ()->dump_untracked_regions (); |
| 6274 |
|
| 6275 |
delete purge_map; |
| 6276 |
|
| 6277 |
/* Free up any dominance info that we may have created. */ |
| 6278 |
FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node) |
| 6279 |
{ |
| 6280 |
function *fun = node->get_fun (); |
| 6281 |
free_dominance_info (fun, CDI_DOMINATORS); |
| 6282 |
} |
| 6283 |
} |
| 6284 |
|
| 6285 |
/* Handle -fdump-analyzer and -fdump-analyzer-stderr. */ |
| 6286 |
static FILE *dump_fout = NULL; |
| 6287 |
|
| 6288 |
/* Track if we're responsible for closing dump_fout. */ |
| 6289 |
static bool owns_dump_fout = false; |
| 6290 |
|
| 6291 |
/* If dumping is enabled, attempt to create dump_fout if it hasn't already |
| 6292 |
been opened. Return it. */ |
| 6293 |
|
| 6294 |
FILE * |
| 6295 |
get_or_create_any_logfile () |
| 6296 |
{ |
| 6297 |
if (!dump_fout) |
| 6298 |
{ |
| 6299 |
if (flag_dump_analyzer_stderr) |
| 6300 |
dump_fout = stderr; |
| 6301 |
else if (flag_dump_analyzer) |
| 6302 |
{ |
| 6303 |
char *dump_filename = concat (dump_base_name, ".analyzer.txt", NULL); |
| 6304 |
dump_fout = fopen (dump_filename, "w"); |
| 6305 |
free (dump_filename); |
| 6306 |
if (dump_fout) |
| 6307 |
owns_dump_fout = true; |
| 6308 |
} |
| 6309 |
} |
| 6310 |
return dump_fout; |
| 6311 |
} |
| 6312 |
|
| 6313 |
/* External entrypoint to the analysis "engine". |
| 6314 |
Set up any dumps, then call impl_run_checkers. */ |
| 6315 |
|
| 6316 |
void |
| 6317 |
run_checkers () |
| 6318 |
{ |
| 6319 |
/* Save input_location. */ |
| 6320 |
location_t saved_input_location = input_location; |
| 6321 |
|
| 6322 |
{ |
| 6323 |
log_user the_logger (NULL); |
| 6324 |
get_or_create_any_logfile (); |
| 6325 |
if (dump_fout) |
| 6326 |
the_logger.set_logger (new logger (dump_fout, 0, 0, |
| 6327 |
*global_dc->get_reference_printer ())); |
| 6328 |
LOG_SCOPE (the_logger.get_logger ()); |
| 6329 |
|
| 6330 |
impl_run_checkers (the_logger.get_logger ()); |
| 6331 |
|
| 6332 |
/* end of lifetime of the_logger (so that dump file is closed after the |
| 6333 |
various dtors run). */ |
| 6334 |
} |
| 6335 |
|
| 6336 |
if (owns_dump_fout) |
| 6337 |
{ |
| 6338 |
fclose (dump_fout); |
| 6339 |
owns_dump_fout = false; |
| 6340 |
dump_fout = NULL; |
| 6341 |
} |
| 6342 |
|
| 6343 |
/* Restore input_location. Subsequent passes may assume that input_location |
| 6344 |
is some arbitrary value *not* in the block tree, which might be violated |
| 6345 |
if we didn't restore it. */ |
| 6346 |
input_location = saved_input_location; |
| 6347 |
} |
| 6348 |
|
| 6349 |
} // namespace ana |
| 6350 |
|
| 6351 |
#endif /* #if ENABLE_ANALYZER */ |