blob: 0902d91fffa4f65590d2e88626e44a236136613d [file] [log] [blame]
Zhongxing Xu0835e4c2009-11-23 03:20:54 +00001//=== UndefBranchChecker.cpp -----------------------------------*- C++ -*--===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines UndefBranchChecker, which checks for undefined branch
11// condition.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidiscc05d512011-02-28 01:27:33 +000015#include "ClangSACheckers.h"
16#include "clang/StaticAnalyzer/Core/CheckerV2.h"
17#include "clang/StaticAnalyzer/Core/CheckerManager.h"
18#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000019#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000020
21using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000022using namespace ento;
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000023
24namespace {
25
Argyrios Kyrtzidiscc05d512011-02-28 01:27:33 +000026class UndefBranchChecker : public CheckerV2<check::BranchCondition> {
27 mutable llvm::OwningPtr<BuiltinBug> BT;
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000028
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000029 struct FindUndefExpr {
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000030 GRStateManager& VM;
31 const GRState* St;
32
33 FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {}
34
Zhongxing Xu03509ae2010-07-20 06:22:24 +000035 const Expr* FindExpr(const Expr* Ex) {
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000036 if (!MatchesCriteria(Ex))
37 return 0;
38
Zhongxing Xu03509ae2010-07-20 06:22:24 +000039 for (Stmt::const_child_iterator I = Ex->child_begin(),
40 E = Ex->child_end();I!=E;++I)
41 if (const Expr* ExI = dyn_cast_or_null<Expr>(*I)) {
42 const Expr* E2 = FindExpr(ExI);
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000043 if (E2) return E2;
44 }
45
46 return Ex;
47 }
48
Zhongxing Xu03509ae2010-07-20 06:22:24 +000049 bool MatchesCriteria(const Expr* Ex) { return St->getSVal(Ex).isUndef(); }
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000050 };
51
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000052public:
Argyrios Kyrtzidiscc05d512011-02-28 01:27:33 +000053 void checkBranchCondition(const Stmt *Condition, BranchNodeBuilder &Builder,
54 ExprEngine &Eng) const;
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000055};
56
57}
58
Argyrios Kyrtzidiscc05d512011-02-28 01:27:33 +000059void UndefBranchChecker::checkBranchCondition(const Stmt *Condition,
60 BranchNodeBuilder &Builder,
61 ExprEngine &Eng) const {
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000062 const GRState *state = Builder.getState();
Ted Kremenek13976632010-02-08 16:18:51 +000063 SVal X = state->getSVal(Condition);
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000064 if (X.isUndef()) {
65 ExplodedNode *N = Builder.generateNode(state, true);
66 if (N) {
67 N->markAsSink();
68 if (!BT)
Argyrios Kyrtzidiscc05d512011-02-28 01:27:33 +000069 BT.reset(
70 new BuiltinBug("Branch condition evaluates to a garbage value"));
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000071
72 // What's going on here: we want to highlight the subexpression of the
73 // condition that is the most likely source of the "uninitialized
74 // branch condition." We do a recursive walk of the condition's
75 // subexpressions and roughly look for the most nested subexpression
76 // that binds to Undefined. We then highlight that expression's range.
77 BlockEdge B = cast<BlockEdge>(N->getLocation());
Zhongxing Xu03509ae2010-07-20 06:22:24 +000078 const Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition());
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000079 assert (Ex && "Block must have a terminator.");
80
81 // Get the predecessor node and check if is a PostStmt with the Stmt
82 // being the terminator condition. We want to inspect the state
83 // of that node instead because it will contain main information about
84 // the subexpressions.
85 assert (!N->pred_empty());
86
87 // Note: any predecessor will do. They should have identical state,
88 // since all the BlockEdge did was act as an error sink since the value
89 // had to already be undefined.
90 ExplodedNode *PrevN = *N->pred_begin();
91 ProgramPoint P = PrevN->getLocation();
92 const GRState* St = N->getState();
93
94 if (PostStmt* PS = dyn_cast<PostStmt>(&P))
95 if (PS->getStmt() == Ex)
96 St = PrevN->getState();
97
98 FindUndefExpr FindIt(Eng.getStateManager(), St);
99 Ex = FindIt.FindExpr(Ex);
Ted Kremenek616cf052009-11-23 18:12:03 +0000100
101 // Emit the bug report.
102 EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getDescription(),N);
103 R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, Ex);
Zhongxing Xuf155dbf2009-11-23 03:29:59 +0000104 R->addRange(Ex->getSourceRange());
105
Zhongxing Xu0835e4c2009-11-23 03:20:54 +0000106 Eng.getBugReporter().EmitReport(R);
107 }
108
109 Builder.markInfeasible(true);
110 Builder.markInfeasible(false);
111 }
112}
Argyrios Kyrtzidiscc05d512011-02-28 01:27:33 +0000113
114void ento::registerUndefBranchChecker(CheckerManager &mgr) {
115 mgr.registerChecker<UndefBranchChecker>();
116}