blob: e999d8bc13212046720d747f4ade423c3756e74f [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 Kyrtzidisd2592a32010-12-22 18:53:44 +000015#include "ExprEngineInternalChecks.h"
Argyrios Kyrtzidis98cabba2010-12-22 18:51:49 +000016#include "clang/GR/BugReporter/BugType.h"
17#include "clang/GR/PathSensitive/Checker.h"
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000018
19using namespace clang;
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000020using namespace GR;
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000021
22namespace {
23
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000024class UndefBranchChecker : public Checker {
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000025 BuiltinBug *BT;
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000026
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000027 struct FindUndefExpr {
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000028 GRStateManager& VM;
29 const GRState* St;
30
31 FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {}
32
Zhongxing Xu03509ae2010-07-20 06:22:24 +000033 const Expr* FindExpr(const Expr* Ex) {
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000034 if (!MatchesCriteria(Ex))
35 return 0;
36
Zhongxing Xu03509ae2010-07-20 06:22:24 +000037 for (Stmt::const_child_iterator I = Ex->child_begin(),
38 E = Ex->child_end();I!=E;++I)
39 if (const Expr* ExI = dyn_cast_or_null<Expr>(*I)) {
40 const Expr* E2 = FindExpr(ExI);
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000041 if (E2) return E2;
42 }
43
44 return Ex;
45 }
46
Zhongxing Xu03509ae2010-07-20 06:22:24 +000047 bool MatchesCriteria(const Expr* Ex) { return St->getSVal(Ex).isUndef(); }
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000048 };
49
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000050public:
51 UndefBranchChecker() : BT(0) {}
52 static void *getTag();
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000053 void VisitBranchCondition(BranchNodeBuilder &Builder, ExprEngine &Eng,
Zhongxing Xu03509ae2010-07-20 06:22:24 +000054 const Stmt *Condition, void *tag);
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000055};
56
57}
58
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000059void GR::RegisterUndefBranchChecker(ExprEngine &Eng) {
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000060 Eng.registerCheck(new UndefBranchChecker());
61}
62
63void *UndefBranchChecker::getTag() {
64 static int x;
65 return &x;
66}
67
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000068void UndefBranchChecker::VisitBranchCondition(BranchNodeBuilder &Builder,
69 ExprEngine &Eng,
Zhongxing Xu03509ae2010-07-20 06:22:24 +000070 const Stmt *Condition, void *tag){
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000071 const GRState *state = Builder.getState();
Ted Kremenek13976632010-02-08 16:18:51 +000072 SVal X = state->getSVal(Condition);
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000073 if (X.isUndef()) {
74 ExplodedNode *N = Builder.generateNode(state, true);
75 if (N) {
76 N->markAsSink();
77 if (!BT)
Ted Kremenek998c1332009-11-23 17:58:48 +000078 BT = new BuiltinBug("Branch condition evaluates to a garbage value");
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());
Zhongxing Xu03509ae2010-07-20 06:22:24 +000086 const Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition());
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000087 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);
Ted Kremenek616cf052009-11-23 18:12:03 +0000108
109 // Emit the bug report.
110 EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getDescription(),N);
111 R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, Ex);
Zhongxing Xuf155dbf2009-11-23 03:29:59 +0000112 R->addRange(Ex->getSourceRange());
113
Zhongxing Xu0835e4c2009-11-23 03:20:54 +0000114 Eng.getBugReporter().EmitReport(R);
115 }
116
117 Builder.markInfeasible(true);
118 Builder.markInfeasible(false);
119 }
120}