analyzer: only call get_diagnostic_tree when it's needed

impl_sm_context::get_diagnostic_tree could be expensive, and
I find myself needing to put a breakpoint on it to debug
PR analyzer/99771, so only call it if we're about to use
the result.

gcc/analyzer/ChangeLog:
	* sm-file.cc (fileptr_state_machine::on_stmt): Only call
	get_diagnostic_tree if the result will be used.
	* sm-malloc.cc (malloc_state_machine::on_stmt): Likewise.
	(malloc_state_machine::on_deallocator_call): Likewise.
	(malloc_state_machine::on_realloc_call): Likewise.
	(malloc_state_machine::on_realloc_call): Likewise.
	* sm-sensitive.cc
	(sensitive_state_machine::warn_for_any_exposure): Likewise.
	* sm-taint.cc (taint_state_machine::on_stmt): Likewise.
This commit is contained in:
David Malcolm 2021-03-26 13:26:15 -04:00
parent a01f5fd710
commit 0f9aa35c79
4 changed files with 16 additions and 8 deletions

View File

@ -344,7 +344,6 @@ fileptr_state_machine::on_stmt (sm_context *sm_ctxt,
if (is_named_call_p (callee_fndecl, "fclose", call, 1)) if (is_named_call_p (callee_fndecl, "fclose", call, 1))
{ {
tree arg = gimple_call_arg (call, 0); tree arg = gimple_call_arg (call, 0);
tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
sm_ctxt->on_transition (node, stmt, arg, m_start, m_closed); sm_ctxt->on_transition (node, stmt, arg, m_start, m_closed);
@ -356,6 +355,7 @@ fileptr_state_machine::on_stmt (sm_context *sm_ctxt,
if (sm_ctxt->get_state (stmt, arg) == m_closed) if (sm_ctxt->get_state (stmt, arg) == m_closed)
{ {
tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
sm_ctxt->warn (node, stmt, arg, sm_ctxt->warn (node, stmt, arg,
new double_fclose (*this, diag_arg)); new double_fclose (*this, diag_arg));
sm_ctxt->set_next_state (stmt, arg, m_stop); sm_ctxt->set_next_state (stmt, arg, m_stop);

View File

@ -1674,11 +1674,11 @@ malloc_state_machine::on_stmt (sm_context *sm_ctxt,
if (TREE_CODE (op) == MEM_REF) if (TREE_CODE (op) == MEM_REF)
{ {
tree arg = TREE_OPERAND (op, 0); tree arg = TREE_OPERAND (op, 0);
tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
state_t state = sm_ctxt->get_state (stmt, arg); state_t state = sm_ctxt->get_state (stmt, arg);
if (unchecked_p (state)) if (unchecked_p (state))
{ {
tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
sm_ctxt->warn (node, stmt, arg, sm_ctxt->warn (node, stmt, arg,
new possible_null_deref (*this, diag_arg)); new possible_null_deref (*this, diag_arg));
const allocation_state *astate = as_a_allocation_state (state); const allocation_state *astate = as_a_allocation_state (state);
@ -1686,12 +1686,14 @@ malloc_state_machine::on_stmt (sm_context *sm_ctxt,
} }
else if (state == m_null) else if (state == m_null)
{ {
tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
sm_ctxt->warn (node, stmt, arg, sm_ctxt->warn (node, stmt, arg,
new null_deref (*this, diag_arg)); new null_deref (*this, diag_arg));
sm_ctxt->set_next_state (stmt, arg, m_stop); sm_ctxt->set_next_state (stmt, arg, m_stop);
} }
else if (freed_p (state)) else if (freed_p (state))
{ {
tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
const allocation_state *astate = as_a_allocation_state (state); const allocation_state *astate = as_a_allocation_state (state);
sm_ctxt->warn (node, stmt, arg, sm_ctxt->warn (node, stmt, arg,
new use_after_free (*this, diag_arg, new use_after_free (*this, diag_arg,
@ -1738,7 +1740,6 @@ malloc_state_machine::on_deallocator_call (sm_context *sm_ctxt,
if (argno >= gimple_call_num_args (call)) if (argno >= gimple_call_num_args (call))
return; return;
tree arg = gimple_call_arg (call, argno); tree arg = gimple_call_arg (call, argno);
tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
state_t state = sm_ctxt->get_state (call, arg); state_t state = sm_ctxt->get_state (call, arg);
@ -1752,6 +1753,7 @@ malloc_state_machine::on_deallocator_call (sm_context *sm_ctxt,
if (!astate->m_deallocators->contains_p (d)) if (!astate->m_deallocators->contains_p (d))
{ {
/* Wrong allocator. */ /* Wrong allocator. */
tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
pending_diagnostic *pd pending_diagnostic *pd
= new mismatching_deallocation (*this, diag_arg, = new mismatching_deallocation (*this, diag_arg,
astate->m_deallocators, astate->m_deallocators,
@ -1766,6 +1768,7 @@ malloc_state_machine::on_deallocator_call (sm_context *sm_ctxt,
else if (state == d->m_freed) else if (state == d->m_freed)
{ {
/* freed -> stop, with warning. */ /* freed -> stop, with warning. */
tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
sm_ctxt->warn (node, call, arg, sm_ctxt->warn (node, call, arg,
new double_free (*this, diag_arg, d->m_name)); new double_free (*this, diag_arg, d->m_name));
sm_ctxt->set_next_state (call, arg, m_stop); sm_ctxt->set_next_state (call, arg, m_stop);
@ -1773,6 +1776,7 @@ malloc_state_machine::on_deallocator_call (sm_context *sm_ctxt,
else if (state == m_non_heap) else if (state == m_non_heap)
{ {
/* non-heap -> stop, with warning. */ /* non-heap -> stop, with warning. */
tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
sm_ctxt->warn (node, call, arg, sm_ctxt->warn (node, call, arg,
new free_of_non_heap (*this, diag_arg, new free_of_non_heap (*this, diag_arg,
d->m_name)); d->m_name));
@ -1806,7 +1810,6 @@ malloc_state_machine::on_realloc_call (sm_context *sm_ctxt,
const gcall *call) const const gcall *call) const
{ {
tree ptr = gimple_call_arg (call, 0); tree ptr = gimple_call_arg (call, 0);
tree diag_ptr = sm_ctxt->get_diagnostic_tree (ptr);
state_t state = sm_ctxt->get_state (call, ptr); state_t state = sm_ctxt->get_state (call, ptr);
@ -1818,6 +1821,7 @@ malloc_state_machine::on_realloc_call (sm_context *sm_ctxt,
if (astate->m_deallocators != &m_free) if (astate->m_deallocators != &m_free)
{ {
/* Wrong allocator. */ /* Wrong allocator. */
tree diag_ptr = sm_ctxt->get_diagnostic_tree (ptr);
pending_diagnostic *pd pending_diagnostic *pd
= new mismatching_deallocation (*this, diag_ptr, = new mismatching_deallocation (*this, diag_ptr,
astate->m_deallocators, astate->m_deallocators,

View File

@ -174,10 +174,12 @@ sensitive_state_machine::warn_for_any_exposure (sm_context *sm_ctxt,
const gimple *stmt, const gimple *stmt,
tree arg) const tree arg) const
{ {
tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
if (sm_ctxt->get_state (stmt, arg) == m_sensitive) if (sm_ctxt->get_state (stmt, arg) == m_sensitive)
sm_ctxt->warn (node, stmt, arg, {
new exposure_through_output_file (*this, diag_arg)); tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
sm_ctxt->warn (node, stmt, arg,
new exposure_through_output_file (*this, diag_arg));
}
} }
/* Implementation of state_machine::on_stmt vfunc for /* Implementation of state_machine::on_stmt vfunc for

View File

@ -227,7 +227,6 @@ taint_state_machine::on_stmt (sm_context *sm_ctxt,
if (op == ARRAY_REF) if (op == ARRAY_REF)
{ {
tree arg = TREE_OPERAND (rhs1, 1); tree arg = TREE_OPERAND (rhs1, 1);
tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
/* Unsigned types have an implicit lower bound. */ /* Unsigned types have an implicit lower bound. */
bool is_unsigned = false; bool is_unsigned = false;
@ -239,6 +238,7 @@ taint_state_machine::on_stmt (sm_context *sm_ctxt,
if (state == m_tainted) if (state == m_tainted)
{ {
/* Complain about missing bounds. */ /* Complain about missing bounds. */
tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
pending_diagnostic *d pending_diagnostic *d
= new tainted_array_index (*this, diag_arg, = new tainted_array_index (*this, diag_arg,
is_unsigned is_unsigned
@ -249,6 +249,7 @@ taint_state_machine::on_stmt (sm_context *sm_ctxt,
else if (state == m_has_lb) else if (state == m_has_lb)
{ {
/* Complain about missing upper bound. */ /* Complain about missing upper bound. */
tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
sm_ctxt->warn (node, stmt, arg, sm_ctxt->warn (node, stmt, arg,
new tainted_array_index (*this, diag_arg, new tainted_array_index (*this, diag_arg,
BOUNDS_LOWER)); BOUNDS_LOWER));
@ -259,6 +260,7 @@ taint_state_machine::on_stmt (sm_context *sm_ctxt,
/* Complain about missing lower bound. */ /* Complain about missing lower bound. */
if (!is_unsigned) if (!is_unsigned)
{ {
tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
sm_ctxt->warn (node, stmt, arg, sm_ctxt->warn (node, stmt, arg,
new tainted_array_index (*this, diag_arg, new tainted_array_index (*this, diag_arg,
BOUNDS_UPPER)); BOUNDS_UPPER));