summaryrefslogtreecommitdiff
path: root/gcc/analyzer/diagnostic-manager.cc
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2020-02-03 11:23:09 -0500
committerDavid Malcolm <dmalcolm@redhat.com>2020-02-03 14:29:08 -0500
commit8525d1f5f57b11fe04a97674cc2fc2b7727621d0 (patch)
tree54e3cdd1ac7812b005b343a24924232fa26b3fe0 /gcc/analyzer/diagnostic-manager.cc
parent73f386581bddc4d630b93eeb0cddd32943bf24e7 (diff)
analyzer: detect zero-assignment in phis (PR 93544)
PR analyzer/93544 reports an ICE when attempting to report a double-free within diagnostic_manager::prune_for_sm_diagnostic, in which the variable of interest has become an INTEGER_CST. Additionally, it picks a nonsensical path through the function in which the pointer being double-freed is known to be NULL, which we shouldn't complain about. The dump shows that it picks the INTEGER_CST when updating var at a phi node: considering event 4, with var: ‘iftmp.0_2’, state: ‘start’ updating from ‘iftmp.0_2’ to ‘0B’ based on phi node phi: iftmp.0_2 = PHI <iftmp.0_6(3), 0B(2)> considering event 3, with var: ‘0B’, state: ‘start’ and that it has picked the shortest path through the exploded graph, and on this path the pointer has been assigned NULL. The root cause is that the state machine's on_stmt isn't called for phi nodes (and wouldn't make much sense, as we wouldn't know which arg to choose). malloc state machine::on_stmt "sees" a GIMPLE_ASSIGN to NULL and handles it by transitioning the lhs to the "null" state, but never "sees" GIMPLE_PHI nodes. This patch fixes the ICE by wiring up phi-handling with state machines, so that state machines have an on_phi vfunc. It updates the only current user of "is_zero_assignment" (the malloc sm) to implement equivalent logic for phi nodes. Doing so ensures that the pointer is in a separate sm-state for the NULL vs non-NULL cases, and so gets separate exploded nodes, and hence the path-finding logic chooses the correct path, and the correct non-NULL phi argument. The patch also adds some bulletproofing to prune_for_sm_diagnostic to avoid crashing in the event of a bad path. gcc/analyzer/ChangeLog: PR analyzer/93544 * diagnostic-manager.cc (diagnostic_manager::prune_for_sm_diagnostic): Bulletproof against bad choices due to bad paths. * engine.cc (impl_region_model_context::on_phi): New. * exploded-graph.h (impl_region_model_context::on_phi): New decl. * region-model.cc (region_model::on_longjmp): Likewise. (region_model::handle_phi): Add phi param. Call the ctxt's on_phi vfunc. (region_model::update_for_phis): Pass phi to handle_phi. * region-model.h (region_model::handle_phi): Add phi param. (region_model_context::on_phi): New vfunc. (test_region_model_context::on_phi): New. * sm-malloc.cc (malloc_state_machine::on_phi): New. (malloc_state_machine::on_zero_assignment): New. * sm.h (state_machine::on_phi): New vfunc. gcc/testsuite/ChangeLog: PR analyzer/93544 * gcc.dg/analyzer/torture/pr93544.c: New test.
Diffstat (limited to 'gcc/analyzer/diagnostic-manager.cc')
-rw-r--r--gcc/analyzer/diagnostic-manager.cc9
1 files changed, 9 insertions, 0 deletions
diff --git a/gcc/analyzer/diagnostic-manager.cc b/gcc/analyzer/diagnostic-manager.cc
index 023fda9fa7c..1a82d5f22ec 100644
--- a/gcc/analyzer/diagnostic-manager.cc
+++ b/gcc/analyzer/diagnostic-manager.cc
@@ -1087,6 +1087,15 @@ diagnostic_manager::prune_for_sm_diagnostic (checker_path *path,
pp_gimple_stmt_1 (&pp, phi, 0, (dump_flags_t)0);
log (" phi: %s", pp_formatted_text (&pp));
}
+ /* If we've chosen a bad exploded_path, then the
+ phi arg might be a constant. Fail gracefully for
+ this case. */
+ if (CONSTANT_CLASS_P (var))
+ {
+ log ("new var is a constant (bad path?);"
+ " setting var to NULL");
+ var = NULL;
+ }
}
}