[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

llvm-svn: 330380
This commit is contained in:
Artem Dergachev 2018-04-19 23:24:32 +00:00
parent b72daf00f4
commit 468bc0d8b9
5 changed files with 42 additions and 36 deletions

View File

@ -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.

View File

@ -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<CollectReachableSymbolsCallback>(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<CollectReachableSymbolsCallback>(
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<CollectReachableSymbolsCallback>(Val);
const InvalidatedSymbols &EscapedSymbols = Scanner.getSymbols();
State = getCheckerManager().runCheckersForPointerEscape(State,
EscapedSymbols,
/*CallEvent*/ nullptr,
PSK_EscapeOnBind,
nullptr);
State = escapeValue(State, Val, PSK_EscapeOnBind);
return State;
}

View File

@ -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;

View File

@ -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<Block*>(malloc(sizeof(Block)));
BlockPtrTy blockPtr = reinterpret_cast<BlockPtrTy>(block); // no-warning
return blockPtr;
}

View File

@ -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;
}