blob: 793f5e06fde01487b1e41d057f788e8e5214f404 [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"
Ted Kremenek1309f9a2010-01-25 04:41:41 +000016#include "clang/Checker/PathSensitive/Checker.h"
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000017
18using namespace clang;
19
20namespace {
21
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000022class UndefBranchChecker : public Checker {
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000023 BuiltinBug *BT;
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000024
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000025 struct FindUndefExpr {
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000026 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
Zhongxing Xu6f8c4302010-02-08 09:30:02 +000044 bool MatchesCriteria(Expr* Ex) { return St->getExprVal(Ex).isUndef(); }
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000045 };
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();
Zhongxing Xu6f8c4302010-02-08 09:30:02 +000069 SVal X = state->getExprVal(Condition);
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000070 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 Xuf155dbf2009-11-23 03:29:59 +000076
77 // What's going on here: we want to highlight the subexpression of the
78 // condition that is the most likely source of the "uninitialized
79 // branch condition." We do a recursive walk of the condition's
80 // subexpressions and roughly look for the most nested subexpression
81 // that binds to Undefined. We then highlight that expression's range.
82 BlockEdge B = cast<BlockEdge>(N->getLocation());
83 Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition());
84 assert (Ex && "Block must have a terminator.");
85
86 // Get the predecessor node and check if is a PostStmt with the Stmt
87 // being the terminator condition. We want to inspect the state
88 // of that node instead because it will contain main information about
89 // the subexpressions.
90 assert (!N->pred_empty());
91
92 // Note: any predecessor will do. They should have identical state,
93 // since all the BlockEdge did was act as an error sink since the value
94 // had to already be undefined.
95 ExplodedNode *PrevN = *N->pred_begin();
96 ProgramPoint P = PrevN->getLocation();
97 const GRState* St = N->getState();
98
99 if (PostStmt* PS = dyn_cast<PostStmt>(&P))
100 if (PS->getStmt() == Ex)
101 St = PrevN->getState();
102
103 FindUndefExpr FindIt(Eng.getStateManager(), St);
104 Ex = FindIt.FindExpr(Ex);
Ted Kremenek616cf052009-11-23 18:12:03 +0000105
106 // Emit the bug report.
107 EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getDescription(),N);
108 R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, Ex);
Zhongxing Xuf155dbf2009-11-23 03:29:59 +0000109 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}