blob: 0a66e21de94452b838f8dd0d6e6852f027ce15df [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)
Ted Kremenek998c1332009-11-23 17:58:48 +000075 BT = new BuiltinBug("Branch condition evaluates to a garbage value");
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000076 EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getDescription(),N);
77 R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue,
78 Condition);
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000079
80 // What's going on here: we want to highlight the subexpression of the
81 // condition that is the most likely source of the "uninitialized
82 // branch condition." We do a recursive walk of the condition's
83 // subexpressions and roughly look for the most nested subexpression
84 // that binds to Undefined. We then highlight that expression's range.
85 BlockEdge B = cast<BlockEdge>(N->getLocation());
86 Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition());
87 assert (Ex && "Block must have a terminator.");
88
89 // Get the predecessor node and check if is a PostStmt with the Stmt
90 // being the terminator condition. We want to inspect the state
91 // of that node instead because it will contain main information about
92 // the subexpressions.
93 assert (!N->pred_empty());
94
95 // Note: any predecessor will do. They should have identical state,
96 // since all the BlockEdge did was act as an error sink since the value
97 // had to already be undefined.
98 ExplodedNode *PrevN = *N->pred_begin();
99 ProgramPoint P = PrevN->getLocation();
100 const GRState* St = N->getState();
101
102 if (PostStmt* PS = dyn_cast<PostStmt>(&P))
103 if (PS->getStmt() == Ex)
104 St = PrevN->getState();
105
106 FindUndefExpr FindIt(Eng.getStateManager(), St);
107 Ex = FindIt.FindExpr(Ex);
108 R->addRange(Ex->getSourceRange());
109
Zhongxing Xu0835e4c2009-11-23 03:20:54 +0000110 Eng.getBugReporter().EmitReport(R);
111 }
112
113 Builder.markInfeasible(true);
114 Builder.markInfeasible(false);
115 }
116}