blob: 8d72b5a96be2b06c76a6d26658bdfbded0c4ba69 [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
15#include "GRExprEngineInternalChecks.h"
16#include "clang/Analysis/PathSensitive/Checker.h"
17
18using namespace clang;
19
20namespace {
21
22class VISIBILITY_HIDDEN UndefBranchChecker : public Checker {
23 BuiltinBug *BT;
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000024
25 struct VISIBILITY_HIDDEN FindUndefExpr {
26 GRStateManager& VM;
27 const GRState* St;
28
29 FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {}
30
31 Expr* FindExpr(Expr* Ex) {
32 if (!MatchesCriteria(Ex))
33 return 0;
34
35 for (Stmt::child_iterator I=Ex->child_begin(), E=Ex->child_end();I!=E;++I)
36 if (Expr* ExI = dyn_cast_or_null<Expr>(*I)) {
37 Expr* E2 = FindExpr(ExI);
38 if (E2) return E2;
39 }
40
41 return Ex;
42 }
43
44 bool MatchesCriteria(Expr* Ex) { return St->getSVal(Ex).isUndef(); }
45 };
46
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000047public:
48 UndefBranchChecker() : BT(0) {}
49 static void *getTag();
50 void VisitBranchCondition(GRBranchNodeBuilder &Builder, GRExprEngine &Eng,
51 Stmt *Condition, void *tag);
52};
53
54}
55
56void clang::RegisterUndefBranchChecker(GRExprEngine &Eng) {
57 Eng.registerCheck(new UndefBranchChecker());
58}
59
60void *UndefBranchChecker::getTag() {
61 static int x;
62 return &x;
63}
64
65void UndefBranchChecker::VisitBranchCondition(GRBranchNodeBuilder &Builder,
66 GRExprEngine &Eng,
67 Stmt *Condition, void *tag) {
68 const GRState *state = Builder.getState();
69 SVal X = state->getSVal(Condition);
70 if (X.isUndef()) {
71 ExplodedNode *N = Builder.generateNode(state, true);
72 if (N) {
73 N->markAsSink();
74 if (!BT)
75 BT = new BuiltinBug("Undefined branch",
76 "Branch condition evaluates to an undefined or garbage value");
77 EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getDescription(),N);
78 R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue,
79 Condition);
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000080
81 // What's going on here: we want to highlight the subexpression of the
82 // condition that is the most likely source of the "uninitialized
83 // branch condition." We do a recursive walk of the condition's
84 // subexpressions and roughly look for the most nested subexpression
85 // that binds to Undefined. We then highlight that expression's range.
86 BlockEdge B = cast<BlockEdge>(N->getLocation());
87 Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition());
88 assert (Ex && "Block must have a terminator.");
89
90 // Get the predecessor node and check if is a PostStmt with the Stmt
91 // being the terminator condition. We want to inspect the state
92 // of that node instead because it will contain main information about
93 // the subexpressions.
94 assert (!N->pred_empty());
95
96 // Note: any predecessor will do. They should have identical state,
97 // since all the BlockEdge did was act as an error sink since the value
98 // had to already be undefined.
99 ExplodedNode *PrevN = *N->pred_begin();
100 ProgramPoint P = PrevN->getLocation();
101 const GRState* St = N->getState();
102
103 if (PostStmt* PS = dyn_cast<PostStmt>(&P))
104 if (PS->getStmt() == Ex)
105 St = PrevN->getState();
106
107 FindUndefExpr FindIt(Eng.getStateManager(), St);
108 Ex = FindIt.FindExpr(Ex);
109 R->addRange(Ex->getSourceRange());
110
Zhongxing Xu0835e4c2009-11-23 03:20:54 +0000111 Eng.getBugReporter().EmitReport(R);
112 }
113
114 Builder.markInfeasible(true);
115 Builder.markInfeasible(false);
116 }
117}