clang API Documentation

UndefBranchChecker.cpp
Go to the documentation of this file.
00001 //=== UndefBranchChecker.cpp -----------------------------------*- C++ -*--===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines UndefBranchChecker, which checks for undefined branch
00011 // condition.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "ClangSACheckers.h"
00016 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
00017 #include "clang/StaticAnalyzer/Core/Checker.h"
00018 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
00019 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
00020 
00021 using namespace clang;
00022 using namespace ento;
00023 
00024 namespace {
00025 
00026 class UndefBranchChecker : public Checker<check::BranchCondition> {
00027   mutable std::unique_ptr<BuiltinBug> BT;
00028 
00029   struct FindUndefExpr {
00030     ProgramStateRef St;
00031     const LocationContext *LCtx;
00032 
00033     FindUndefExpr(ProgramStateRef S, const LocationContext *L) 
00034       : St(S), LCtx(L) {}
00035 
00036     const Expr *FindExpr(const Expr *Ex) {
00037       if (!MatchesCriteria(Ex))
00038         return nullptr;
00039 
00040       for (Stmt::const_child_iterator I = Ex->child_begin(), 
00041                                       E = Ex->child_end();I!=E;++I)
00042         if (const Expr *ExI = dyn_cast_or_null<Expr>(*I)) {
00043           const Expr *E2 = FindExpr(ExI);
00044           if (E2) return E2;
00045         }
00046 
00047       return Ex;
00048     }
00049 
00050     bool MatchesCriteria(const Expr *Ex) { 
00051       return St->getSVal(Ex, LCtx).isUndef();
00052     }
00053   };
00054 
00055 public:
00056   void checkBranchCondition(const Stmt *Condition, CheckerContext &Ctx) const;
00057 };
00058 
00059 }
00060 
00061 void UndefBranchChecker::checkBranchCondition(const Stmt *Condition,
00062                                               CheckerContext &Ctx) const {
00063   SVal X = Ctx.getState()->getSVal(Condition, Ctx.getLocationContext());
00064   if (X.isUndef()) {
00065     // Generate a sink node, which implicitly marks both outgoing branches as
00066     // infeasible.
00067     ExplodedNode *N = Ctx.generateSink();
00068     if (N) {
00069       if (!BT)
00070         BT.reset(new BuiltinBug(
00071             this, "Branch condition evaluates to a garbage value"));
00072 
00073       // What's going on here: we want to highlight the subexpression of the
00074       // condition that is the most likely source of the "uninitialized
00075       // branch condition."  We do a recursive walk of the condition's
00076       // subexpressions and roughly look for the most nested subexpression
00077       // that binds to Undefined.  We then highlight that expression's range.
00078 
00079       // Get the predecessor node and check if is a PostStmt with the Stmt
00080       // being the terminator condition.  We want to inspect the state
00081       // of that node instead because it will contain main information about
00082       // the subexpressions.
00083 
00084       // Note: any predecessor will do.  They should have identical state,
00085       // since all the BlockEdge did was act as an error sink since the value
00086       // had to already be undefined.
00087       assert (!N->pred_empty());
00088       const Expr *Ex = cast<Expr>(Condition);
00089       ExplodedNode *PrevN = *N->pred_begin();
00090       ProgramPoint P = PrevN->getLocation();
00091       ProgramStateRef St = N->getState();
00092 
00093       if (Optional<PostStmt> PS = P.getAs<PostStmt>())
00094         if (PS->getStmt() == Ex)
00095           St = PrevN->getState();
00096 
00097       FindUndefExpr FindIt(St, Ctx.getLocationContext());
00098       Ex = FindIt.FindExpr(Ex);
00099 
00100       // Emit the bug report.
00101       BugReport *R = new BugReport(*BT, BT->getDescription(), N);
00102       bugreporter::trackNullOrUndefValue(N, Ex, *R);
00103       R->addRange(Ex->getSourceRange());
00104 
00105       Ctx.emitReport(R);
00106     }
00107   }
00108 }
00109 
00110 void ento::registerUndefBranchChecker(CheckerManager &mgr) {
00111   mgr.registerChecker<UndefBranchChecker>();
00112 }