From df4319332f38db90cd9dd4af9afa94c2ed429fc7 Mon Sep 17 00:00:00 2001 From: Gabor Horvath Date: Tue, 29 Jan 2019 10:27:14 +0000 Subject: [PATCH] [analyzer] Toning down invalidation a bit When a function takes the address of a field the analyzer will no longer assume that the function will change other fields of the enclosing structs. Differential Revision: https://reviews.llvm.org/D57230 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@352473 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/StaticAnalyzer/Core/CallEvent.cpp | 22 +++++++++++++++++----- test/Analysis/call-invalidation.cpp | 5 ++++- test/Analysis/cxx-uninitialized-object.cpp | 5 ++--- test/Analysis/malloc.c | 4 ++-- test/Analysis/taint-generic.c | 1 + test/Analysis/taint-tester.c | 2 +- 6 files changed, 27 insertions(+), 12 deletions(-) diff --git a/lib/StaticAnalyzer/Core/CallEvent.cpp b/lib/StaticAnalyzer/Core/CallEvent.cpp index 11dda7c3ac..8dc6646e0e 100644 --- a/lib/StaticAnalyzer/Core/CallEvent.cpp +++ b/lib/StaticAnalyzer/Core/CallEvent.cpp @@ -303,11 +303,23 @@ ProgramStateRef CallEvent::invalidateRegions(unsigned BlockCount, for (unsigned Idx = 0, Count = getNumArgs(); Idx != Count; ++Idx) { // Mark this region for invalidation. We batch invalidate regions // below for efficiency. - if (PreserveArgs.count(Idx)) - if (const MemRegion *MR = getArgSVal(Idx).getAsRegion()) - ETraits.setTrait(MR->getBaseRegion(), - RegionAndSymbolInvalidationTraits::TK_PreserveContents); - // TODO: Factor this out + handle the lower level const pointers. + if (const MemRegion *MR = getArgSVal(Idx).getAsRegion()) { + bool UseBaseRegion = true; + if (const auto *FR = MR->getAs()) { + if (const auto *TVR = FR->getSuperRegion()->getAs()) { + if (!TVR->getValueType()->isUnionType()) { + ETraits.setTrait(MR, RegionAndSymbolInvalidationTraits:: + TK_DoNotInvalidateSuperRegion); + UseBaseRegion = false; + } + } + } + // todo: factor this out + handle the lower level const pointers. + if (PreserveArgs.count(Idx)) + ETraits.setTrait( + UseBaseRegion ? MR->getBaseRegion() : MR, + RegionAndSymbolInvalidationTraits::TK_PreserveContents); + } ValuesToInvalidate.push_back(getArgSVal(Idx)); diff --git a/test/Analysis/call-invalidation.cpp b/test/Analysis/call-invalidation.cpp index c107e10705..dade8db9ac 100644 --- a/test/Analysis/call-invalidation.cpp +++ b/test/Analysis/call-invalidation.cpp @@ -132,18 +132,21 @@ void testInvalidationThroughBaseRegionPointer() { PlainStruct s1; s1.x = 1; s1.z = 1; + s1.y = 1; clang_analyzer_eval(s1.x == 1); // expected-warning{{TRUE}} clang_analyzer_eval(s1.z == 1); // expected-warning{{TRUE}} // Not only passing a structure pointer through const pointer parameter, // but also passing a field pointer through const pointer parameter // should preserve the contents of the structure. useAnythingConst(&(s1.y)); + clang_analyzer_eval(s1.y == 1); // expected-warning{{TRUE}} clang_analyzer_eval(s1.x == 1); // expected-warning{{TRUE}} // FIXME: Should say "UNKNOWN", because it is not uncommon to // modify a mutable member variable through const pointer. clang_analyzer_eval(s1.z == 1); // expected-warning{{TRUE}} useAnything(&(s1.y)); - clang_analyzer_eval(s1.x == 1); // expected-warning{{UNKNOWN}} + clang_analyzer_eval(s1.x == 1); // expected-warning{{TRUE}} + clang_analyzer_eval(s1.y == 1); // expected-warning{{UNKNOWN}} } diff --git a/test/Analysis/cxx-uninitialized-object.cpp b/test/Analysis/cxx-uninitialized-object.cpp index 07006bea47..93a02a4838 100644 --- a/test/Analysis/cxx-uninitialized-object.cpp +++ b/test/Analysis/cxx-uninitialized-object.cpp @@ -358,7 +358,7 @@ template void wontInitialize(const T &); class PassingToUnknownFunctionTest1 { - int a, b; + int a, b; // expected-note{{uninitialized field 'this->b'}} public: PassingToUnknownFunctionTest1() { @@ -368,8 +368,7 @@ public: } PassingToUnknownFunctionTest1(int) { - mayInitialize(a); - // All good! + mayInitialize(a); // expected-warning{{1 uninitialized field at the end of the constructor call}} } PassingToUnknownFunctionTest1(int, int) { diff --git a/test/Analysis/malloc.c b/test/Analysis/malloc.c index 8e0f5c04ca..d4a44c5762 100644 --- a/test/Analysis/malloc.c +++ b/test/Analysis/malloc.c @@ -1758,8 +1758,8 @@ void constEscape(const void *ptr); void testConstEscapeThroughAnotherField() { struct IntAndPtr s; s.p = malloc(sizeof(int)); - constEscape(&(s.x)); // could free s->p! -} // no-warning + constEscape(&(s.x)); +} // expected-warning {{Potential leak of memory pointed to by 's.p'}} // PR15623 int testNoCheckerDataPropogationFromLogicalOpOperandToOpResult(void) { diff --git a/test/Analysis/taint-generic.c b/test/Analysis/taint-generic.c index 2717e91b43..30529503e5 100644 --- a/test/Analysis/taint-generic.c +++ b/test/Analysis/taint-generic.c @@ -231,6 +231,7 @@ void testUnion() { int sock = socket(AF_INET, SOCK_STREAM, 0); read(sock, &tainted.y, sizeof(tainted.y)); + tainted.x = 0; // FIXME: overlapping regions aren't detected by isTainted yet __builtin_memcpy(buffer, tainted.y, tainted.x); } diff --git a/test/Analysis/taint-tester.c b/test/Analysis/taint-tester.c index 3a8cc1825a..b072eb84d4 100644 --- a/test/Analysis/taint-tester.c +++ b/test/Analysis/taint-tester.c @@ -51,7 +51,7 @@ void taintTracking(int x) { scanf("%d", &xy.y); scanf("%d", &xy.x); int tx = xy.x; // expected-warning + {{tainted}} - int ty = xy.y; // FIXME: This should be tainted as well. + int ty = xy.y; // expected-warning + {{tainted}} char ntz = xy.z;// no warning // Now, scanf scans both. scanf("%d %d", &xy.y, &xy.x); -- 2.40.0