8sa1-gcc/gcc/analyzer
David Malcolm 3a66c289a3 analyzer: fix leak false +ves due to maybe-clobbered regions [PR99042,PR99774]
Prior to this patch, program_state::detect_leaks worked by finding all
live svalues in the old state and in the new state, and calling
on_svalue_leak for each svalue that has changed from being live to
not being live.

PR analyzer/99042 and PR analyzer/99774 both describe false leak
diagnostics from -fanalyzer (a false FILE * leak in git, and a false
malloc leak in qemu, respectively).

In both cases the root cause of the false leak diagnostic relates to
svalues no longer being explicitly bound in the store due to regions
being conservatively clobbered, due to an unknown function being
called, or due to a write through a pointer that could alias the
region, respectively.

We have a transition from an svalue being explicitly live to not
being explicitly live - but only because the store is being
conservative, clobbering the binding.  The leak detection is looking
for transitions from "definitely live" to "not definitely live",
when it should be looking for transitions from "definitely live"
to "definitely not live".

This patch introduces a new class to temporarily capture information
about svalues that were explicitly live, but for which a region bound
to them got clobbered for conservative reasons.  This new
"uncertainty_t" class is passed around to capture the data long enough
for use in program_state::detect_leaks, where it is used to only
complain about svalues that were definitely live and are now both
not definitely live *or* possibly-live i.e. definitely not-live.

The class also captures for which svalues we can't meaningfully track
sm-state anymore, and resets the svalues back to the "start" state.

Together, these changes fix the false leak reports.

gcc/analyzer/ChangeLog:
	PR analyzer/99042
	PR analyzer/99774
	* engine.cc
	(impl_region_model_context::impl_region_model_context): Add
	uncertainty param and use it to initialize m_uncertainty.
	(impl_region_model_context::get_uncertainty): New.
	(impl_sm_context::get_fndecl_for_call): Add NULL for new
	uncertainty param when constructing impl_region_model_context.
	(impl_sm_context::get_state): Likewise.
	(impl_sm_context::set_next_state): Likewise.
	(impl_sm_context::warn): Likewise.
	(exploded_node::on_stmt): Add uncertainty param
	and use it when constructing impl_region_model_context.
	(exploded_node::on_edge): Add uncertainty param and pass
	to on_edge call.
	(exploded_node::detect_leaks): Create uncertainty_t and pass to
	impl_region_model_context.
	(exploded_graph::get_or_create_node): Create uncertainty_t and
	pass to prune_for_point.
	(maybe_process_run_of_before_supernode_enodes): Create
	uncertainty_t and pass to impl_region_model_context.
	(exploded_graph::process_node): Create uncertainty_t instances and
	pass around as needed.
	* exploded-graph.h
	(impl_region_model_context::impl_region_model_context): Add
	uncertainty param.
	(impl_region_model_context::get_uncertainty): New decl.
	(impl_region_model_context::m_uncertainty): New field.
	(exploded_node::on_stmt): Add uncertainty param.
	(exploded_node::on_edge): Likewise.
	* program-state.cc (sm_state_map::on_liveness_change): Get
	uncertainty from context and use it to unset sm-state from
	svalues as appropriate.
	(program_state::on_edge): Add uncertainty param and use it when
	constructing impl_region_model_context.  Fix indentation.
	(program_state::prune_for_point): Add uncertainty param and use it
	when constructing impl_region_model_context.
	(program_state::detect_leaks): Get any uncertainty from ctxt and
	use it to get maybe-live svalues for dest_state, rather than
	definitely-live ones; use this when determining which svalues
	have leaked.
	(selftest::test_program_state_merging): Create uncertainty_t and
	pass to impl_region_model_context.
	* program-state.h (program_state::on_edge): Add uncertainty param.
	(program_state::prune_for_point): Likewise.
	* region-model-impl-calls.cc (call_details::get_uncertainty): New.
	(region_model::impl_call_memcpy): Pass uncertainty to
	mark_region_as_unknown call.
	(region_model::impl_call_memset): Likewise.
	(region_model::impl_call_strcpy): Likewise.
	* region-model-reachability.cc (reachable_regions::handle_sval):
	Also add sval to m_mutable_svals.
	* region-model.cc (region_model::on_assignment): Pass any
	uncertainty from ctxt to the store::set_value call.
	(region_model::handle_unrecognized_call): Get any uncertainty from
	ctxt and use it to record mutable svalues at the unknown call.
	(region_model::get_reachable_svalues): Add uncertainty param and
	use it to mark any maybe-bound svalues as being reachable.
	(region_model::set_value): Pass any uncertainty from ctxt to the
	store::set_value call.
	(region_model::mark_region_as_unknown): Add uncertainty param and
	pass it on to the store::mark_region_as_unknown call.
	(region_model::update_for_call_summary): Add uncertainty param and
	pass it on to the region_model::mark_region_as_unknown call.
	* region-model.h (call_details::get_uncertainty): New decl.
	(region_model::get_reachable_svalues): Add uncertainty param.
	(region_model::mark_region_as_unknown): Add uncertainty param.
	(region_model_context::get_uncertainty): New vfunc.
	(noop_region_model_context::get_uncertainty): New vfunc
	implementation.
	* store.cc (dump_svalue_set): New.
	(uncertainty_t::dump_to_pp): New.
	(uncertainty_t::dump): New.
	(binding_cluster::clobber_region): Pass NULL for uncertainty to
	remove_overlapping_bindings.
	(binding_cluster::mark_region_as_unknown): Add uncertainty param
	and pass it to remove_overlapping_bindings.
	(binding_cluster::remove_overlapping_bindings): Add uncertainty param.
	Use it to record any svalues that were in clobbered bindings.
	(store::set_value): Add uncertainty param.  Pass it to
	binding_cluster::mark_region_as_unknown when handling symbolic
	regions.
	(store::mark_region_as_unknown): Add uncertainty param and pass it
	to binding_cluster::mark_region_as_unknown.
	(store::remove_overlapping_bindings): Add uncertainty param and
	pass it to binding_cluster::remove_overlapping_bindings.
	* store.h (binding_cluster::mark_region_as_unknown): Add
	uncertainty param.
	(binding_cluster::remove_overlapping_bindings): Likewise.
	(store::set_value): Likewise.
	(store::mark_region_as_unknown): Likewise.

gcc/testsuite/ChangeLog:
	PR analyzer/99042
	PR analyzer/99774
	* gcc.dg/analyzer/pr99042.c: New test.
	* gcc.dg/analyzer/pr99774-1.c: New test.
	* gcc.dg/analyzer/pr99774-2.c: New test.
2021-04-08 09:46:03 -04:00
..
analysis-plan.cc Update copyright years. 2021-01-04 10:26:59 +01:00
analysis-plan.h Update copyright years. 2021-01-04 10:26:59 +01:00
analyzer-logging.cc Update copyright years. 2021-01-04 10:26:59 +01:00
analyzer-logging.h Update copyright years. 2021-01-04 10:26:59 +01:00
analyzer-pass.cc Update copyright years. 2021-01-04 10:26:59 +01:00
analyzer-selftests.cc Update copyright years. 2021-01-04 10:26:59 +01:00
analyzer-selftests.h Update copyright years. 2021-01-04 10:26:59 +01:00
analyzer.cc analyzer: fix ICE on zero-arg calls passed to __attribute__((nonnull)) [PR 99906] 2021-04-05 10:51:46 -04:00
analyzer.h analyzer: avoid printing '<unknown>' for SSA names [PR99771] 2021-03-31 19:16:48 -04:00
analyzer.opt analyzer: new implementation of shortest feasible path [PR96374] 2021-03-11 17:46:37 -05:00
bar-chart.cc Update copyright years. 2021-01-04 10:26:59 +01:00
bar-chart.h Update copyright years. 2021-01-04 10:26:59 +01:00
call-string.cc Update copyright years. 2021-01-04 10:26:59 +01:00
call-string.h Update copyright years. 2021-01-04 10:26:59 +01:00
ChangeLog Daily bump. 2021-04-06 00:16:43 +00:00
checker-path.cc analyzer: avoid printing '<unknown>' for SSA names [PR99771] 2021-03-31 19:16:48 -04:00
checker-path.h analyzer: consolidate conditionals in paths 2021-01-29 15:12:24 -05:00
complexity.cc Update copyright years. 2021-01-04 10:26:59 +01:00
complexity.h Update copyright years. 2021-01-04 10:26:59 +01:00
constraint-manager.cc analyzer: fix false leak involving params [PR98969] 2021-02-17 10:37:16 -05:00
constraint-manager.h Update copyright years. 2021-01-04 10:26:59 +01:00
diagnostic-manager.cc analyzer: fix apparent hang with -fanalyzer-verbosity=0 [PR analyzer/99886] 2021-04-05 10:48:01 -04:00
diagnostic-manager.h analyzer: record per-enode saved_diagnostics 2021-04-01 18:22:52 -04:00
engine.cc analyzer: fix leak false +ves due to maybe-clobbered regions [PR99042,PR99774] 2021-04-08 09:46:03 -04:00
engine.h Update copyright years. 2021-01-04 10:26:59 +01:00
exploded-graph.h analyzer: fix leak false +ves due to maybe-clobbered regions [PR99042,PR99774] 2021-04-08 09:46:03 -04:00
feasible-graph.cc analyzer: record per-enode saved_diagnostics 2021-04-01 18:22:52 -04:00
feasible-graph.h analyzer: new implementation of shortest feasible path [PR96374] 2021-03-11 17:46:37 -05:00
function-set.cc Update copyright years. 2021-01-04 10:26:59 +01:00
function-set.h Update copyright years. 2021-01-04 10:26:59 +01:00
pending-diagnostic.cc Update copyright years. 2021-01-04 10:26:59 +01:00
pending-diagnostic.h analyzer: const fixes [PR98679] 2021-01-14 15:25:27 -05:00
program-point.cc Update copyright years. 2021-01-04 10:26:59 +01:00
program-point.h Update copyright years. 2021-01-04 10:26:59 +01:00
program-state.cc analyzer: fix leak false +ves due to maybe-clobbered regions [PR99042,PR99774] 2021-04-08 09:46:03 -04:00
program-state.h analyzer: fix leak false +ves due to maybe-clobbered regions [PR99042,PR99774] 2021-04-08 09:46:03 -04:00
reachability.h Update copyright years. 2021-01-04 10:26:59 +01:00
region-model-impl-calls.cc analyzer: fix leak false +ves due to maybe-clobbered regions [PR99042,PR99774] 2021-04-08 09:46:03 -04:00
region-model-manager.cc analyzer: fix false positives with *UNKNOWN_PTR [PR98918] 2021-02-01 15:13:39 -05:00
region-model-reachability.cc analyzer: fix leak false +ves due to maybe-clobbered regions [PR99042,PR99774] 2021-04-08 09:46:03 -04:00
region-model-reachability.h Update copyright years. 2021-01-04 10:26:59 +01:00
region-model.cc analyzer: fix leak false +ves due to maybe-clobbered regions [PR99042,PR99774] 2021-04-08 09:46:03 -04:00
region-model.h analyzer: fix leak false +ves due to maybe-clobbered regions [PR99042,PR99774] 2021-04-08 09:46:03 -04:00
region.cc analyzer: fix ICE when DECL_INITIAL is error_mark_node [PR98580] 2021-01-07 15:45:29 -05:00
region.h analyzer: remove old decl of region::dump_to_pp 2021-03-30 17:54:36 -04:00
sm-file.cc analyzer: only call get_diagnostic_tree when it's needed 2021-03-30 17:51:21 -04:00
sm-malloc.cc analyzer: fix ICE on zero-arg calls passed to __attribute__((nonnull)) [PR 99906] 2021-04-05 10:51:46 -04:00
sm-malloc.dot Update copyright years. 2021-01-04 10:26:59 +01:00
sm-pattern-test.cc Update copyright years. 2021-01-04 10:26:59 +01:00
sm-sensitive.cc analyzer: only call get_diagnostic_tree when it's needed 2021-03-30 17:51:21 -04:00
sm-signal.cc Update copyright years. 2021-01-04 10:26:59 +01:00
sm-taint.cc analyzer: only call get_diagnostic_tree when it's needed 2021-03-30 17:51:21 -04:00
sm.cc Update copyright years. 2021-01-04 10:26:59 +01:00
sm.h Update copyright years. 2021-01-04 10:26:59 +01:00
state-purge.cc Update copyright years. 2021-01-04 10:26:59 +01:00
state-purge.h Update copyright years. 2021-01-04 10:26:59 +01:00
store.cc analyzer: fix leak false +ves due to maybe-clobbered regions [PR99042,PR99774] 2021-04-08 09:46:03 -04:00
store.h analyzer: fix leak false +ves due to maybe-clobbered regions [PR99042,PR99774] 2021-04-08 09:46:03 -04:00
supergraph.cc analyzer: fixes to -fdump-analyzer-json 2021-01-14 15:39:14 -05:00
supergraph.h committed: analyzer: remove duplicated vfuncs 2021-03-10 12:00:34 -05:00
svalue.cc analyzer; reset sm-state for SSA names at def-stmts [PR93695,PR99044,PR99716] 2021-03-24 20:47:57 -04:00
svalue.h analyzer; reset sm-state for SSA names at def-stmts [PR93695,PR99044,PR99716] 2021-03-24 20:47:57 -04:00
trimmed-graph.cc analyzer: new implementation of shortest feasible path [PR96374] 2021-03-11 17:46:37 -05:00
trimmed-graph.h analyzer: new implementation of shortest feasible path [PR96374] 2021-03-11 17:46:37 -05:00