2008-08-17 01:46:53 +08:00
|
|
|
//==- UninitializedValues.cpp - Find Uninitialized Values -------*- C++ --*-==//
|
2007-09-15 06:49:21 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-09-15 06:49:21 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements Uninitialized Values analysis for source-level CFGs.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-12-22 05:42:19 +08:00
|
|
|
#include "clang/Analysis/Analyses/UninitializedValues.h"
|
2007-09-21 05:42:55 +08:00
|
|
|
#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
|
2009-01-29 13:15:15 +08:00
|
|
|
#include "clang/Analysis/AnalysisDiagnostic.h"
|
2007-09-18 03:59:27 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2007-09-26 05:00:24 +08:00
|
|
|
#include "clang/Analysis/FlowSensitive/DataflowSolver.h"
|
2007-09-15 06:49:21 +08:00
|
|
|
|
2007-09-18 03:59:27 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
|
2007-09-15 06:49:21 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2007-09-18 03:59:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-15 06:49:21 +08:00
|
|
|
// Dataflow initialization logic.
|
2009-09-09 23:08:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-15 06:49:21 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2009-11-28 14:07:30 +08:00
|
|
|
class RegisterDecls
|
2009-09-09 23:08:12 +08:00
|
|
|
: public CFGRecStmtDeclVisitor<RegisterDecls> {
|
2008-01-09 02:19:08 +08:00
|
|
|
|
2007-09-18 01:14:52 +08:00
|
|
|
UninitializedValues::AnalysisDataTy& AD;
|
2007-09-15 06:49:21 +08:00
|
|
|
public:
|
2007-10-02 04:33:52 +08:00
|
|
|
RegisterDecls(UninitializedValues::AnalysisDataTy& ad) : AD(ad) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 07:02:18 +08:00
|
|
|
void VisitVarDecl(VarDecl* VD) { AD.Register(VD); }
|
2007-11-20 11:01:58 +08:00
|
|
|
CFG& getCFG() { return AD.getCFG(); }
|
2007-09-15 06:49:21 +08:00
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-15 06:49:21 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
void UninitializedValues::InitializeValues(const CFG& cfg) {
|
2007-10-02 04:33:52 +08:00
|
|
|
RegisterDecls R(getAnalysisData());
|
2007-09-19 04:59:00 +08:00
|
|
|
cfg.VisitBlockStmts(R);
|
2007-09-15 06:49:21 +08:00
|
|
|
}
|
|
|
|
|
2007-09-18 03:59:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-15 06:49:21 +08:00
|
|
|
// Transfer functions.
|
2009-09-09 23:08:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-15 06:49:21 +08:00
|
|
|
|
|
|
|
namespace {
|
2009-11-28 14:07:30 +08:00
|
|
|
class TransferFuncs
|
2008-01-09 02:19:08 +08:00
|
|
|
: public CFGStmtVisitor<TransferFuncs,bool> {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-15 06:49:21 +08:00
|
|
|
UninitializedValues::ValTy V;
|
2007-09-18 01:14:52 +08:00
|
|
|
UninitializedValues::AnalysisDataTy& AD;
|
2007-09-15 06:49:21 +08:00
|
|
|
public:
|
2008-04-16 02:35:30 +08:00
|
|
|
TransferFuncs(UninitializedValues::AnalysisDataTy& ad) : AD(ad) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-15 06:49:21 +08:00
|
|
|
UninitializedValues::ValTy& getVal() { return V; }
|
2007-11-20 11:01:58 +08:00
|
|
|
CFG& getCFG() { return AD.getCFG(); }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 02:35:30 +08:00
|
|
|
void SetTopValue(UninitializedValues::ValTy& X) {
|
2008-11-12 03:41:42 +08:00
|
|
|
X.setDeclValues(AD);
|
2008-11-14 09:14:18 +08:00
|
|
|
X.resetBlkExprValues(AD);
|
2008-04-16 02:35:30 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-18 02:31:23 +08:00
|
|
|
bool VisitDeclRefExpr(DeclRefExpr* DR);
|
|
|
|
bool VisitBinaryOperator(BinaryOperator* B);
|
|
|
|
bool VisitUnaryOperator(UnaryOperator* U);
|
|
|
|
bool VisitStmt(Stmt* S);
|
|
|
|
bool VisitCallExpr(CallExpr* C);
|
2007-09-18 03:59:27 +08:00
|
|
|
bool VisitDeclStmt(DeclStmt* D);
|
2007-09-28 08:09:38 +08:00
|
|
|
bool VisitConditionalOperator(ConditionalOperator* C);
|
2008-11-13 05:58:46 +08:00
|
|
|
bool BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt* S);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-28 08:09:38 +08:00
|
|
|
bool Visit(Stmt *S);
|
|
|
|
bool BlockStmt_VisitExpr(Expr* E);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-17 05:10:48 +08:00
|
|
|
void VisitTerminator(CFGBlock* B) { }
|
2007-09-15 06:49:21 +08:00
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-12 03:41:42 +08:00
|
|
|
static const bool Initialized = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
static const bool Uninitialized = true;
|
2007-09-18 02:31:23 +08:00
|
|
|
|
|
|
|
bool TransferFuncs::VisitDeclRefExpr(DeclRefExpr* DR) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 10:59:55 +08:00
|
|
|
if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
|
|
|
|
if (VD->isBlockVarDecl()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 10:59:55 +08:00
|
|
|
if (AD.Observer)
|
|
|
|
AD.Observer->ObserveDeclRefExpr(V, AD, DR, VD);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 10:59:55 +08:00
|
|
|
// Pseudo-hack to prevent cascade of warnings. If an accessed variable
|
|
|
|
// is uninitialized, then we are already going to flag a warning for
|
|
|
|
// this variable, which a "source" of uninitialized values.
|
|
|
|
// We can otherwise do a full "taint" of uninitialized values. The
|
|
|
|
// client has both options by toggling AD.FullUninitTaint.
|
2007-09-28 08:09:38 +08:00
|
|
|
|
2008-04-16 10:59:55 +08:00
|
|
|
if (AD.FullUninitTaint)
|
|
|
|
return V(VD,AD);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 10:59:55 +08:00
|
|
|
return Initialized;
|
2007-09-19 05:43:18 +08:00
|
|
|
}
|
|
|
|
|
2008-04-16 10:59:55 +08:00
|
|
|
static VarDecl* FindBlockVarDecl(Expr* E) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 10:59:55 +08:00
|
|
|
// Blast through casts and parentheses to find any DeclRefExprs that
|
|
|
|
// refer to a block VarDecl.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-16 10:59:55 +08:00
|
|
|
if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()))
|
2009-09-09 23:08:12 +08:00
|
|
|
if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
|
2008-04-16 10:59:55 +08:00
|
|
|
if (VD->isBlockVarDecl()) return VD;
|
|
|
|
|
|
|
|
return NULL;
|
2007-09-18 02:31:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TransferFuncs::VisitBinaryOperator(BinaryOperator* B) {
|
2008-04-16 10:59:55 +08:00
|
|
|
|
|
|
|
if (VarDecl* VD = FindBlockVarDecl(B->getLHS()))
|
2007-09-29 05:08:51 +08:00
|
|
|
if (B->isAssignmentOp()) {
|
2007-11-25 04:07:36 +08:00
|
|
|
if (B->getOpcode() == BinaryOperator::Assign)
|
|
|
|
return V(VD,AD) = Visit(B->getRHS());
|
|
|
|
else // Handle +=, -=, *=, etc. We do want '&', not '&&'.
|
|
|
|
return V(VD,AD) = Visit(B->getLHS()) & Visit(B->getRHS());
|
2007-09-29 05:08:51 +08:00
|
|
|
}
|
|
|
|
|
2007-09-18 02:31:23 +08:00
|
|
|
return VisitStmt(B);
|
|
|
|
}
|
|
|
|
|
2007-09-18 03:59:27 +08:00
|
|
|
bool TransferFuncs::VisitDeclStmt(DeclStmt* S) {
|
2008-08-06 04:46:55 +08:00
|
|
|
for (DeclStmt::decl_iterator I=S->decl_begin(), E=S->decl_end(); I!=E; ++I) {
|
|
|
|
VarDecl *VD = dyn_cast<VarDecl>(*I);
|
2008-04-16 06:42:06 +08:00
|
|
|
if (VD && VD->isBlockVarDecl()) {
|
2010-03-03 05:43:54 +08:00
|
|
|
if (Stmt* I = VD->getInit()) {
|
|
|
|
// Visit the subexpression to check for uses of uninitialized values,
|
|
|
|
// even if we don't propagate that value.
|
|
|
|
bool isSubExprUninit = Visit(I);
|
|
|
|
V(VD,AD) = AD.FullUninitTaint ? isSubExprUninit : Initialized;
|
|
|
|
}
|
2007-12-13 13:14:22 +08:00
|
|
|
else {
|
|
|
|
// Special case for declarations of array types. For things like:
|
|
|
|
//
|
|
|
|
// char x[10];
|
|
|
|
//
|
|
|
|
// we should treat "x" as being initialized, because the variable
|
|
|
|
// "x" really refers to the memory block. Clearly x[1] is
|
2009-09-09 23:08:12 +08:00
|
|
|
// uninitialized, but expressions like "(char *) x" really do refer to
|
|
|
|
// an initialized value. This simple dataflow analysis does not reason
|
2007-12-13 13:14:22 +08:00
|
|
|
// about the contents of arrays, although it could be potentially
|
|
|
|
// extended to do so if the array were of constant size.
|
|
|
|
if (VD->getType()->isArrayType())
|
|
|
|
V(VD,AD) = Initialized;
|
2009-09-09 23:08:12 +08:00
|
|
|
else
|
2007-12-13 13:14:22 +08:00
|
|
|
V(VD,AD) = Uninitialized;
|
|
|
|
}
|
2007-09-28 02:20:22 +08:00
|
|
|
}
|
2008-04-16 06:42:06 +08:00
|
|
|
}
|
2007-09-28 08:09:38 +08:00
|
|
|
return Uninitialized; // Value is never consumed.
|
2007-09-18 03:59:27 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-18 02:31:23 +08:00
|
|
|
bool TransferFuncs::VisitCallExpr(CallExpr* C) {
|
2007-09-19 05:47:41 +08:00
|
|
|
VisitChildren(C);
|
2007-09-28 02:20:22 +08:00
|
|
|
return Initialized;
|
2007-09-18 02:31:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TransferFuncs::VisitUnaryOperator(UnaryOperator* U) {
|
2007-12-13 12:47:15 +08:00
|
|
|
switch (U->getOpcode()) {
|
2008-04-17 21:52:22 +08:00
|
|
|
case UnaryOperator::AddrOf: {
|
2008-04-16 06:42:06 +08:00
|
|
|
VarDecl* VD = FindBlockVarDecl(U->getSubExpr());
|
|
|
|
if (VD && VD->isBlockVarDecl())
|
2007-12-13 12:47:15 +08:00
|
|
|
return V(VD,AD) = Initialized;
|
|
|
|
break;
|
2008-04-17 21:52:22 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-13 12:47:15 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2007-09-28 08:09:38 +08:00
|
|
|
|
|
|
|
return Visit(U->getSubExpr());
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-13 05:58:46 +08:00
|
|
|
bool
|
|
|
|
TransferFuncs::BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
|
2008-11-12 03:41:42 +08:00
|
|
|
// This represents a use of the 'collection'
|
|
|
|
bool x = Visit(S->getCollection());
|
|
|
|
|
|
|
|
if (x == Uninitialized)
|
|
|
|
return Uninitialized;
|
|
|
|
|
|
|
|
// This represents an initialization of the 'element' value.
|
|
|
|
Stmt* Element = S->getElement();
|
|
|
|
VarDecl* VD = 0;
|
|
|
|
|
|
|
|
if (DeclStmt* DS = dyn_cast<DeclStmt>(Element))
|
2009-03-28 14:33:19 +08:00
|
|
|
VD = cast<VarDecl>(DS->getSingleDecl());
|
2008-11-15 02:21:25 +08:00
|
|
|
else {
|
|
|
|
Expr* ElemExpr = cast<Expr>(Element)->IgnoreParens();
|
|
|
|
|
|
|
|
// Initialize the value of the reference variable.
|
|
|
|
if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(ElemExpr))
|
|
|
|
VD = cast<VarDecl>(DR->getDecl());
|
|
|
|
else
|
|
|
|
return Visit(ElemExpr);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-12 03:41:42 +08:00
|
|
|
V(VD,AD) = Initialized;
|
|
|
|
return Initialized;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
|
2007-09-28 08:09:38 +08:00
|
|
|
bool TransferFuncs::VisitConditionalOperator(ConditionalOperator* C) {
|
|
|
|
Visit(C->getCond());
|
2007-12-01 03:04:31 +08:00
|
|
|
|
|
|
|
bool rhsResult = Visit(C->getRHS());
|
|
|
|
// Handle the GNU extension for missing LHS.
|
|
|
|
if (Expr *lhs = C->getLHS())
|
|
|
|
return Visit(lhs) & rhsResult; // Yes: we want &, not &&.
|
|
|
|
else
|
|
|
|
return rhsResult;
|
2007-09-18 02:31:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TransferFuncs::VisitStmt(Stmt* S) {
|
2007-09-28 02:20:22 +08:00
|
|
|
bool x = Initialized;
|
2007-09-18 02:31:23 +08:00
|
|
|
|
|
|
|
// We don't stop at the first subexpression that is Uninitialized because
|
|
|
|
// evaluating some subexpressions may result in propogating "Uninitialized"
|
|
|
|
// or "Initialized" to variables referenced in the other subexpressions.
|
|
|
|
for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I)
|
2007-09-28 08:09:38 +08:00
|
|
|
if (*I && Visit(*I) == Uninitialized) x = Uninitialized;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-18 02:31:23 +08:00
|
|
|
return x;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-28 08:09:38 +08:00
|
|
|
bool TransferFuncs::Visit(Stmt *S) {
|
|
|
|
if (AD.isTracked(static_cast<Expr*>(S))) return V(static_cast<Expr*>(S),AD);
|
|
|
|
else return static_cast<CFGStmtVisitor<TransferFuncs,bool>*>(this)->Visit(S);
|
|
|
|
}
|
2007-09-18 02:31:23 +08:00
|
|
|
|
|
|
|
bool TransferFuncs::BlockStmt_VisitExpr(Expr* E) {
|
2009-09-09 23:08:12 +08:00
|
|
|
bool x = static_cast<CFGStmtVisitor<TransferFuncs,bool>*>(this)->Visit(E);
|
2008-01-26 08:03:27 +08:00
|
|
|
if (AD.isTracked(E)) V(E,AD) = x;
|
|
|
|
return x;
|
2007-09-18 02:31:23 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-15 06:49:21 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2007-09-18 03:59:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-15 06:49:21 +08:00
|
|
|
// Merge operator.
|
2007-09-18 02:31:23 +08:00
|
|
|
//
|
|
|
|
// In our transfer functions we take the approach that any
|
2008-08-17 01:46:53 +08:00
|
|
|
// combination of uninitialized values, e.g.
|
|
|
|
// Uninitialized + ___ = Uninitialized.
|
2007-09-18 02:31:23 +08:00
|
|
|
//
|
2008-04-16 02:35:30 +08:00
|
|
|
// Merges take the same approach, preferring soundness. At a confluence point,
|
|
|
|
// if any predecessor has a variable marked uninitialized, the value is
|
|
|
|
// uninitialized at the confluence point.
|
2009-09-09 23:08:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-15 06:49:21 +08:00
|
|
|
|
|
|
|
namespace {
|
2008-11-14 09:14:18 +08:00
|
|
|
typedef StmtDeclBitVector_Types::Union Merge;
|
2007-09-28 02:20:22 +08:00
|
|
|
typedef DataflowSolver<UninitializedValues,TransferFuncs,Merge> Solver;
|
|
|
|
}
|
2007-09-15 06:49:21 +08:00
|
|
|
|
2007-09-18 03:59:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-08-17 01:46:53 +08:00
|
|
|
// Uninitialized values checker. Scan an AST and flag variable uses
|
2009-09-09 23:08:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-15 06:49:21 +08:00
|
|
|
|
2007-09-18 03:59:27 +08:00
|
|
|
UninitializedValues_ValueTypes::ObserverTy::~ObserverTy() {}
|
2007-09-15 06:49:21 +08:00
|
|
|
|
2007-09-18 03:59:27 +08:00
|
|
|
namespace {
|
2009-11-28 14:07:30 +08:00
|
|
|
class UninitializedValuesChecker
|
2008-01-09 02:19:08 +08:00
|
|
|
: public UninitializedValues::ObserverTy {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-18 03:59:27 +08:00
|
|
|
ASTContext &Ctx;
|
|
|
|
Diagnostic &Diags;
|
2008-04-16 06:42:06 +08:00
|
|
|
llvm::SmallPtrSet<VarDecl*,10> AlreadyWarned;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-18 03:59:27 +08:00
|
|
|
public:
|
|
|
|
UninitializedValuesChecker(ASTContext &ctx, Diagnostic &diags)
|
|
|
|
: Ctx(ctx), Diags(diags) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-18 03:59:27 +08:00
|
|
|
virtual void ObserveDeclRefExpr(UninitializedValues::ValTy& V,
|
|
|
|
UninitializedValues::AnalysisDataTy& AD,
|
2008-04-16 06:42:06 +08:00
|
|
|
DeclRefExpr* DR, VarDecl* VD) {
|
2007-09-18 03:59:27 +08:00
|
|
|
|
2007-09-19 05:43:18 +08:00
|
|
|
assert ( AD.isTracked(VD) && "Unknown VarDecl.");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-28 02:20:22 +08:00
|
|
|
if (V(VD,AD) == Uninitialized)
|
2007-09-18 03:59:27 +08:00
|
|
|
if (AlreadyWarned.insert(VD))
|
2007-12-13 06:39:36 +08:00
|
|
|
Diags.Report(Ctx.getFullLoc(DR->getSourceRange().getBegin()),
|
|
|
|
diag::warn_uninit_val);
|
2007-09-15 06:49:21 +08:00
|
|
|
}
|
2007-09-18 03:59:27 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2007-09-15 06:49:21 +08:00
|
|
|
|
2007-09-18 04:49:30 +08:00
|
|
|
namespace clang {
|
2007-09-28 08:09:38 +08:00
|
|
|
void CheckUninitializedValues(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags,
|
|
|
|
bool FullUninitTaint) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-17 01:46:53 +08:00
|
|
|
// Compute the uninitialized values information.
|
2007-10-02 04:33:52 +08:00
|
|
|
UninitializedValues U(cfg);
|
2007-09-28 08:09:38 +08:00
|
|
|
U.getAnalysisData().FullUninitTaint = FullUninitTaint;
|
2007-09-15 06:49:21 +08:00
|
|
|
Solver S(U);
|
2007-09-18 03:59:27 +08:00
|
|
|
S.runOnCFG(cfg);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-18 03:59:27 +08:00
|
|
|
// Scan for DeclRefExprs that use uninitialized values.
|
|
|
|
UninitializedValuesChecker Observer(Ctx,Diags);
|
|
|
|
U.getAnalysisData().Observer = &Observer;
|
2007-09-19 05:08:21 +08:00
|
|
|
S.runOnAllBlocks(cfg);
|
2007-09-18 04:49:30 +08:00
|
|
|
}
|
2007-09-19 05:08:21 +08:00
|
|
|
} // end namespace clang
|