2010-02-16 16:33:59 +08:00
|
|
|
// UndefCapturedBlockVarChecker.cpp - Uninitialized captured vars -*- C++ -*-=//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This checker detects blocks that capture uninitialized values.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-28 09:27:26 +08:00
|
|
|
#include "ClangSACheckers.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-28 09:27:26 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2010-02-16 16:33:59 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2010-02-16 16:33:59 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
class UndefCapturedBlockVarChecker
|
2011-03-01 09:16:21 +08:00
|
|
|
: public Checker< check::PostStmt<BlockExpr> > {
|
2011-02-28 09:27:26 +08:00
|
|
|
mutable llvm::OwningPtr<BugType> BT;
|
2010-02-16 16:33:59 +08:00
|
|
|
|
|
|
|
public:
|
2011-02-28 09:27:26 +08:00
|
|
|
void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
|
2010-02-16 16:33:59 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
static const BlockDeclRefExpr *FindBlockDeclRefExpr(const Stmt *S,
|
|
|
|
const VarDecl *VD){
|
|
|
|
if (const BlockDeclRefExpr *BR = dyn_cast<BlockDeclRefExpr>(S))
|
|
|
|
if (BR->getDecl() == VD)
|
|
|
|
return BR;
|
|
|
|
|
|
|
|
for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
|
|
|
|
I!=E; ++I)
|
|
|
|
if (const Stmt *child = *I) {
|
|
|
|
const BlockDeclRefExpr *BR = FindBlockDeclRefExpr(child, VD);
|
|
|
|
if (BR)
|
|
|
|
return BR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-02-28 09:27:26 +08:00
|
|
|
UndefCapturedBlockVarChecker::checkPostStmt(const BlockExpr *BE,
|
|
|
|
CheckerContext &C) const {
|
2011-02-02 21:00:07 +08:00
|
|
|
if (!BE->getBlockDecl()->hasCaptures())
|
2010-02-16 16:33:59 +08:00
|
|
|
return;
|
|
|
|
|
2011-08-16 06:09:50 +08:00
|
|
|
const ProgramState *state = C.getState();
|
2010-02-16 16:33:59 +08:00
|
|
|
const BlockDataRegion *R =
|
|
|
|
cast<BlockDataRegion>(state->getSVal(BE).getAsRegion());
|
|
|
|
|
|
|
|
BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
|
|
|
|
E = R->referenced_vars_end();
|
|
|
|
|
|
|
|
for (; I != E; ++I) {
|
|
|
|
// This VarRegion is the region associated with the block; we need
|
|
|
|
// the one associated with the encompassing context.
|
|
|
|
const VarRegion *VR = *I;
|
|
|
|
const VarDecl *VD = VR->getDecl();
|
|
|
|
|
|
|
|
if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Get the VarRegion associated with VD in the local stack frame.
|
|
|
|
const LocationContext *LC = C.getPredecessor()->getLocationContext();
|
2010-12-02 15:49:45 +08:00
|
|
|
VR = C.getSValBuilder().getRegionManager().getVarRegion(VD, LC);
|
2010-02-16 16:33:59 +08:00
|
|
|
|
|
|
|
if (state->getSVal(VR).isUndef())
|
2010-12-21 05:19:09 +08:00
|
|
|
if (ExplodedNode *N = C.generateSink()) {
|
2010-02-16 16:33:59 +08:00
|
|
|
if (!BT)
|
2011-02-28 09:27:26 +08:00
|
|
|
BT.reset(new BuiltinBug("uninitialized variable captured by block"));
|
2010-02-16 16:33:59 +08:00
|
|
|
|
|
|
|
// Generate a bug report.
|
|
|
|
llvm::SmallString<128> buf;
|
|
|
|
llvm::raw_svector_ostream os(buf);
|
|
|
|
|
2011-01-26 03:13:42 +08:00
|
|
|
os << "Variable '" << VD->getName()
|
|
|
|
<< "' is uninitialized when captured by block";
|
2010-02-16 16:33:59 +08:00
|
|
|
|
|
|
|
EnhancedBugReport *R = new EnhancedBugReport(*BT, os.str(), N);
|
|
|
|
if (const Expr *Ex = FindBlockDeclRefExpr(BE->getBody(), VD))
|
|
|
|
R->addRange(Ex->getSourceRange());
|
|
|
|
R->addVisitorCreator(bugreporter::registerFindLastStore, VR);
|
|
|
|
// need location of block
|
|
|
|
C.EmitReport(R);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-28 09:27:26 +08:00
|
|
|
|
|
|
|
void ento::registerUndefCapturedBlockVarChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<UndefCapturedBlockVarChecker>();
|
|
|
|
}
|