blob: 8663893f2eddfaa1cdbe78df49101b1e03df9416 [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"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000016#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidiscc05d512011-02-28 01:27:33 +000017#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 Kyrtzidisec8605f2011-03-01 01:16:21 +000026class UndefBranchChecker : public Checker<check::BranchCondition> {
Argyrios Kyrtzidiscc05d512011-02-28 01:27:33 +000027 mutable llvm::OwningPtr<BuiltinBug> BT;
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000028
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000029 struct FindUndefExpr {
Ted Kremenek18c66fd2011-08-15 22:09:50 +000030 const ProgramState *St;
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000031
Anna Zaks9c81bc22011-10-03 21:16:32 +000032 FindUndefExpr(const ProgramState *S) : St(S) {}
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000033
Ted Kremenek9c378f72011-08-12 23:37:29 +000034 const Expr *FindExpr(const Expr *Ex) {
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000035 if (!MatchesCriteria(Ex))
36 return 0;
37
Zhongxing Xu03509ae2010-07-20 06:22:24 +000038 for (Stmt::const_child_iterator I = Ex->child_begin(),
39 E = Ex->child_end();I!=E;++I)
Ted Kremenek9c378f72011-08-12 23:37:29 +000040 if (const Expr *ExI = dyn_cast_or_null<Expr>(*I)) {
41 const Expr *E2 = FindExpr(ExI);
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000042 if (E2) return E2;
43 }
44
45 return Ex;
46 }
47
Ted Kremenek9c378f72011-08-12 23:37:29 +000048 bool MatchesCriteria(const Expr *Ex) { return St->getSVal(Ex).isUndef(); }
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000049 };
50
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000051public:
Argyrios Kyrtzidiscc05d512011-02-28 01:27:33 +000052 void checkBranchCondition(const Stmt *Condition, BranchNodeBuilder &Builder,
53 ExprEngine &Eng) const;
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000054};
55
56}
57
Argyrios Kyrtzidiscc05d512011-02-28 01:27:33 +000058void UndefBranchChecker::checkBranchCondition(const Stmt *Condition,
59 BranchNodeBuilder &Builder,
60 ExprEngine &Eng) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +000061 const ProgramState *state = Builder.getState();
Ted Kremenek13976632010-02-08 16:18:51 +000062 SVal X = state->getSVal(Condition);
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000063 if (X.isUndef()) {
Anna Zaksa19f4af2011-10-18 23:06:04 +000064 // Generate a sink node.
65 ExplodedNode *N = Builder.generateNode(Condition, state, 0, true);
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000066 if (N) {
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000067 if (!BT)
Argyrios Kyrtzidiscc05d512011-02-28 01:27:33 +000068 BT.reset(
69 new BuiltinBug("Branch condition evaluates to a garbage value"));
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000070
71 // What's going on here: we want to highlight the subexpression of the
72 // condition that is the most likely source of the "uninitialized
73 // branch condition." We do a recursive walk of the condition's
74 // subexpressions and roughly look for the most nested subexpression
75 // that binds to Undefined. We then highlight that expression's range.
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000076
77 // Get the predecessor node and check if is a PostStmt with the Stmt
78 // being the terminator condition. We want to inspect the state
79 // of that node instead because it will contain main information about
80 // the subexpressions.
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000081
82 // Note: any predecessor will do. They should have identical state,
83 // since all the BlockEdge did was act as an error sink since the value
84 // had to already be undefined.
Anna Zaks9c81bc22011-10-03 21:16:32 +000085 assert (!N->pred_empty());
86 const Expr *Ex = cast<Expr>(Condition);
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000087 ExplodedNode *PrevN = *N->pred_begin();
88 ProgramPoint P = PrevN->getLocation();
Ted Kremenek18c66fd2011-08-15 22:09:50 +000089 const ProgramState *St = N->getState();
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000090
Ted Kremenek9c378f72011-08-12 23:37:29 +000091 if (PostStmt *PS = dyn_cast<PostStmt>(&P))
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000092 if (PS->getStmt() == Ex)
93 St = PrevN->getState();
94
Anna Zaks9c81bc22011-10-03 21:16:32 +000095 FindUndefExpr FindIt(St);
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000096 Ex = FindIt.FindExpr(Ex);
Ted Kremenek616cf052009-11-23 18:12:03 +000097
98 // Emit the bug report.
Anna Zaks50bbc162011-08-19 22:33:38 +000099 BugReport *R = new BugReport(*BT, BT->getDescription(), N);
100 R->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Ex));
Zhongxing Xuf155dbf2009-11-23 03:29:59 +0000101 R->addRange(Ex->getSourceRange());
102
Zhongxing Xu0835e4c2009-11-23 03:20:54 +0000103 Eng.getBugReporter().EmitReport(R);
104 }
105
106 Builder.markInfeasible(true);
107 Builder.markInfeasible(false);
108 }
109}
Argyrios Kyrtzidiscc05d512011-02-28 01:27:33 +0000110
111void ento::registerUndefBranchChecker(CheckerManager &mgr) {
112 mgr.registerChecker<UndefBranchChecker>();
113}