From addfb0519a3a8b1eea81c83b8f458722a1775e44 Mon Sep 17 00:00:00 2001 From: Artem Dergachev Date: Thu, 19 Apr 2018 23:24:32 +0000 Subject: [PATCH] [analyzer] When we fail to evaluate a pointer cast, escape the pointer. If a pointer cast fails (evaluates to an UnknownVal, i.e. not implemented in the analyzer) and such cast is in fact the last use of the pointer, the pointer symbol is no longer referenced by the program state and a leak is (mis-)diagnosed. "Escape" the pointer upon a failed cast, i.e. inform the checker that we can no longer reliably track it. Differential Revision: https://reviews.llvm.org/D45698 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@330380 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Core/PathSensitive/ExprEngine.h | 5 ++ lib/StaticAnalyzer/Core/ExprEngine.cpp | 51 +++++++------------ lib/StaticAnalyzer/Core/ExprEngineC.cpp | 7 ++- test/Analysis/malloc.mm | 10 ++++ test/Analysis/pr22954.c | 5 +- 5 files changed, 42 insertions(+), 36 deletions(-) diff --git a/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h b/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h index 7d3f117eb2..ba42d816d8 100644 --- a/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h +++ b/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h @@ -604,6 +604,11 @@ protected: const CallEvent *Call, RegionAndSymbolInvalidationTraits &ITraits) override; + /// A simple wrapper when you only need to notify checkers of pointer-escape + /// of a single value. + ProgramStateRef escapeValue(ProgramStateRef State, SVal V, + PointerEscapeKind K) const; + public: // FIXME: 'tag' should be removed, and a LocationContext should be used // instead. diff --git a/lib/StaticAnalyzer/Core/ExprEngine.cpp b/lib/StaticAnalyzer/Core/ExprEngine.cpp index 51c6f25e98..1e0bfcea2d 100644 --- a/lib/StaticAnalyzer/Core/ExprEngine.cpp +++ b/lib/StaticAnalyzer/Core/ExprEngine.cpp @@ -1231,23 +1231,27 @@ void ExprEngine::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *BTE, } } -namespace { +ProgramStateRef ExprEngine::escapeValue(ProgramStateRef State, SVal V, + PointerEscapeKind K) const { + class CollectReachableSymbolsCallback final : public SymbolVisitor { + InvalidatedSymbols Symbols; -class CollectReachableSymbolsCallback final : public SymbolVisitor { - InvalidatedSymbols Symbols; + public: + explicit CollectReachableSymbolsCallback(ProgramStateRef State) {} -public: - explicit CollectReachableSymbolsCallback(ProgramStateRef State) {} + const InvalidatedSymbols &getSymbols() const { return Symbols; } - const InvalidatedSymbols &getSymbols() const { return Symbols; } - - bool VisitSymbol(SymbolRef Sym) override { - Symbols.insert(Sym); - return true; - } -}; + bool VisitSymbol(SymbolRef Sym) override { + Symbols.insert(Sym); + return true; + } + }; -} // namespace + const CollectReachableSymbolsCallback &Scanner = + State->scanReachableSymbols(V); + return getCheckerManager().runCheckersForPointerEscape( + State, Scanner.getSymbols(), /*CallEvent*/ nullptr, K, nullptr); +} void ExprEngine::Visit(const Stmt *S, ExplodedNode *Pred, ExplodedNodeSet &DstTop) { @@ -1529,17 +1533,8 @@ void ExprEngine::Visit(const Stmt *S, ExplodedNode *Pred, ->getType()->isRecordType())) for (auto Child : Ex->children()) { assert(Child); - SVal Val = State->getSVal(Child, LCtx); - - CollectReachableSymbolsCallback Scanner = - State->scanReachableSymbols( - Val); - const InvalidatedSymbols &EscapedSymbols = Scanner.getSymbols(); - - State = getCheckerManager().runCheckersForPointerEscape( - State, EscapedSymbols, - /*CallEvent*/ nullptr, PSK_EscapeOther, nullptr); + State = escapeValue(State, Val, PSK_EscapeOther); } Bldr2.generateNode(S, N, State); @@ -2759,15 +2754,7 @@ ProgramStateRef ExprEngine::processPointerEscapedOnBind(ProgramStateRef State, // Otherwise, find all symbols referenced by 'val' that we are tracking // and stop tracking them. - CollectReachableSymbolsCallback Scanner = - State->scanReachableSymbols(Val); - const InvalidatedSymbols &EscapedSymbols = Scanner.getSymbols(); - State = getCheckerManager().runCheckersForPointerEscape(State, - EscapedSymbols, - /*CallEvent*/ nullptr, - PSK_EscapeOnBind, - nullptr); - + State = escapeValue(State, Val, PSK_EscapeOnBind); return State; } diff --git a/lib/StaticAnalyzer/Core/ExprEngineC.cpp b/lib/StaticAnalyzer/Core/ExprEngineC.cpp index 55ee2cefc9..5a306a5c5f 100644 --- a/lib/StaticAnalyzer/Core/ExprEngineC.cpp +++ b/lib/StaticAnalyzer/Core/ExprEngineC.cpp @@ -258,12 +258,15 @@ ProgramStateRef ExprEngine::handleLValueBitCast( QualType T, QualType ExTy, const CastExpr* CastE, StmtNodeBuilder& Bldr, ExplodedNode* Pred) { // Delegate to SValBuilder to process. - SVal V = state->getSVal(Ex, LCtx); - V = svalBuilder.evalCast(V, T, ExTy); + SVal OrigV = state->getSVal(Ex, LCtx); + SVal V = svalBuilder.evalCast(OrigV, T, ExTy); // Negate the result if we're treating the boolean as a signed i1 if (CastE->getCastKind() == CK_BooleanToSignedIntegral) V = evalMinus(V); state = state->BindExpr(CastE, LCtx, V); + if (V.isUnknown() && !OrigV.isUnknown()) { + state = escapeValue(state, OrigV, PSK_EscapeOther); + } Bldr.generateNode(CastE, Pred, state); return state; diff --git a/test/Analysis/malloc.mm b/test/Analysis/malloc.mm index e3daa858be..d7bfbf3f34 100644 --- a/test/Analysis/malloc.mm +++ b/test/Analysis/malloc.mm @@ -320,3 +320,13 @@ void test12365078_check_positive() { NSString *string = [[NSString alloc] initWithCharactersNoCopy:characters length:12 freeWhenDone:1]; if (string) free(characters); // expected-warning{{Attempt to free non-owned memory}} } + +void *test_reinterpret_cast_to_block() { + // Used to leak because the pointer was disappearing + // during the reinterpret_cast. + using BlockPtrTy = void (^)(); + struct Block {}; + Block* block = static_cast(malloc(sizeof(Block))); + BlockPtrTy blockPtr = reinterpret_cast(block); // no-warning + return blockPtr; +} diff --git a/test/Analysis/pr22954.c b/test/Analysis/pr22954.c index 64a00c5ec0..b4273c0a89 100644 --- a/test/Analysis/pr22954.c +++ b/test/Analysis/pr22954.c @@ -624,9 +624,10 @@ int f29(int i, int j, int k, int l, int m) { clang_analyzer_eval(m29[i].s3[1] == 1); // expected-warning{{UNKNOWN}} clang_analyzer_eval(m29[i].s3[2] == 1); // expected-warning{{UNKNOWN}} clang_analyzer_eval(m29[i].s3[3] == 1); // expected-warning{{UNKNOWN}} - clang_analyzer_eval(m29[j].s3[k] == 1); // expected-warning{{TRUE}}\ - expected-warning{{Potential leak of memory pointed to by field 's4'}} + clang_analyzer_eval(m29[j].s3[k] == 1); // expected-warning{{TRUE}} clang_analyzer_eval(l29->s1[m] == 2); // expected-warning{{UNKNOWN}} + // FIXME: Should warn that m29[i].s4 leaks. But not on the previous line, + // because l29 and m29 alias. return 0; } -- 2.40.0