analyzer: fix false positives with *UNKNOWN_PTR [PR98918]
authorDavid Malcolm <dmalcolm@redhat.com>
Mon, 1 Feb 2021 20:13:39 +0000 (15:13 -0500)
committerDavid Malcolm <dmalcolm@redhat.com>
Mon, 1 Feb 2021 20:13:39 +0000 (15:13 -0500)
commit11d4ec5d45c02a19b8ff9d7f26800637ad563e05
treee700f3fea5c6a05dd11059a05d6497beedbf26d0
parent33a1e511b57465d898429740377466894a0b247d
analyzer: fix false positives with *UNKNOWN_PTR [PR98918]

PR analyzer/98918 reports various false positives and state explosions
on correct code that frees nodes and other pointers in a singly-linked
list.

The issue is that state-merger in the loop leads to UNKNOWN_VALUEs,
and these are then erroneously used to form compound symbolic values
and regions, such as;
  INIT_VAL((*UNKNOWN(struct marker *)).ref)
and:
  (*INIT_VAL((*UNKNOWN(struct marker * *))))
The malloc state machine then treats these symbolic values as
identifying specific pointers, and thus e.g. erroneously reports a
double-free when
  INIT_VAL((*UNKNOWN(struct marker *)).ref)
is freed twice (on subsequent iterations of the loop).

Similarly, the increasingly complex compound symbolic values have
sm-state which prevents state merging, and eventually lead to the
analysis hitting safety limits and stopping.

This patch makes various compound values involving UNKNOWN be
themselves UNKNOWN, resolving both the false positives and the state
explosions.

gcc/analyzer/ChangeLog:
PR analyzer/98918
* region-model-manager.cc
(region_model_manager::get_or_create_initial_value):
Fold the initial value of *UNKNOWN_PTR to an UNKNOWN value.
(region_model_manager::get_field_region): Fold the value
of UNKNOWN_PTR->FIELD to *UNKNOWN_PTR_OF_&FIELD_TYPE.

gcc/testsuite/ChangeLog:
PR analyzer/98918
* gcc.dg/analyzer/pr98918.c: New test.
gcc/analyzer/region-model-manager.cc
gcc/testsuite/gcc.dg/analyzer/pr98918.c [new file with mode: 0644]