blob: 451fa91b3aba8100bd0923424aefa2d3f325901d [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:
Anna Zakscd656ca2011-10-18 23:06:21 +000052 void checkBranchCondition(const Stmt *Condition, NodeBuilder &Builder,
Argyrios Kyrtzidiscc05d512011-02-28 01:27:33 +000053 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,
Anna Zakscd656ca2011-10-18 23:06:21 +000059 NodeBuilder &Builder,
Argyrios Kyrtzidiscc05d512011-02-28 01:27:33 +000060 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 Zakscd656ca2011-10-18 23:06:21 +000064 // TODO: The PP will be generated with the correct tag by the CheckerManager
65 // after we migrate the callback to CheckerContext.
66 const ProgramPointTag *Tag = 0;
67 ProgramPoint PP = PostCondition(Condition,
68 Builder.getPredecessor()->getLocationContext(), Tag);
69 // Generate a sink node, which implicitly marks both outgoing branches as
70 // infeasible.
71 ExplodedNode *N = Builder.generateNode(PP, state,
72 Builder.getPredecessor(), true);
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000073 if (N) {
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000074 if (!BT)
Argyrios Kyrtzidiscc05d512011-02-28 01:27:33 +000075 BT.reset(
76 new BuiltinBug("Branch condition evaluates to a garbage value"));
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000077
78 // What's going on here: we want to highlight the subexpression of the
79 // condition that is the most likely source of the "uninitialized
80 // branch condition." We do a recursive walk of the condition's
81 // subexpressions and roughly look for the most nested subexpression
82 // that binds to Undefined. We then highlight that expression's range.
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000083
84 // Get the predecessor node and check if is a PostStmt with the Stmt
85 // being the terminator condition. We want to inspect the state
86 // of that node instead because it will contain main information about
87 // the subexpressions.
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000088
89 // Note: any predecessor will do. They should have identical state,
90 // since all the BlockEdge did was act as an error sink since the value
91 // had to already be undefined.
Anna Zaks9c81bc22011-10-03 21:16:32 +000092 assert (!N->pred_empty());
93 const Expr *Ex = cast<Expr>(Condition);
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000094 ExplodedNode *PrevN = *N->pred_begin();
95 ProgramPoint P = PrevN->getLocation();
Ted Kremenek18c66fd2011-08-15 22:09:50 +000096 const ProgramState *St = N->getState();
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000097
Ted Kremenek9c378f72011-08-12 23:37:29 +000098 if (PostStmt *PS = dyn_cast<PostStmt>(&P))
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000099 if (PS->getStmt() == Ex)
100 St = PrevN->getState();
101
Anna Zaks9c81bc22011-10-03 21:16:32 +0000102 FindUndefExpr FindIt(St);
Zhongxing Xuf155dbf2009-11-23 03:29:59 +0000103 Ex = FindIt.FindExpr(Ex);
Ted Kremenek616cf052009-11-23 18:12:03 +0000104
105 // Emit the bug report.
Anna Zaks50bbc162011-08-19 22:33:38 +0000106 BugReport *R = new BugReport(*BT, BT->getDescription(), N);
107 R->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Ex));
Zhongxing Xuf155dbf2009-11-23 03:29:59 +0000108 R->addRange(Ex->getSourceRange());
109
Zhongxing Xu0835e4c2009-11-23 03:20:54 +0000110 Eng.getBugReporter().EmitReport(R);
111 }
Zhongxing Xu0835e4c2009-11-23 03:20:54 +0000112 }
113}
Argyrios Kyrtzidiscc05d512011-02-28 01:27:33 +0000114
115void ento::registerUndefBranchChecker(CheckerManager &mgr) {
116 mgr.registerChecker<UndefBranchChecker>();
117}