[analyzer] Make default bindings to variables actually work.
authorArtem Dergachev <artem.dergachev@gmail.com>
Thu, 18 Apr 2019 23:35:56 +0000 (23:35 +0000)
committerArtem Dergachev <artem.dergachev@gmail.com>
Thu, 18 Apr 2019 23:35:56 +0000 (23:35 +0000)
commit9b02a9b401509ede7a093350ec1f03400cd17e35
tree338df46cd8863c5264b9c07fbf00cbc9be1917db
parent185de8eeaaaea1e1300980f04a4479ca7edff4d4
[analyzer] Make default bindings to variables actually work.

Default RegionStore bindings represent values that can be obtained by loading
from anywhere within the region, not just the specific offset within the region
that they are said to be bound to. For example, default-binding a character \0
to an int (eg., via memset()) means that the whole int is 0, not just
that its lower byte is 0.

Even though memset and bzero were modeled this way, it didn't work correctly
when applied to simple variables. Eg., in

  int x;
  memset(x, 0, sizeof(x));

we did produce a default binding, but were unable to read it later, and 'x'
was perceived as an uninitialized variable even after memset.

At the same time, if we replace 'x' with a variable of a structure or array
type, accessing fields or elements of such variable was working correctly,
which was enough for most cases. So this was only a problem for variables of
simple integer/enumeration/floating-point/pointer types.

Fix loading default bindings from RegionStore for regions of simple variables.

Add a unit test to document the API contract as well.

Differential Revision: https://reviews.llvm.org/D60742

llvm-svn: 358722
clang/lib/StaticAnalyzer/Core/RegionStore.cpp
clang/test/Analysis/string.c
clang/unittests/StaticAnalyzer/CMakeLists.txt
clang/unittests/StaticAnalyzer/StoreTest.cpp [new file with mode: 0644]