blob: afb79b5d166f3663cf6ad51e7c73743d8503093d [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 Zaksf236b652011-10-25 19:56:54 +000052 void checkBranchCondition(const Stmt *Condition, CheckerContext &Ctx) const;
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000053};
54
55}
56
Argyrios Kyrtzidiscc05d512011-02-28 01:27:33 +000057void UndefBranchChecker::checkBranchCondition(const Stmt *Condition,
Anna Zaksf236b652011-10-25 19:56:54 +000058 CheckerContext &Ctx) const {
59 SVal X = Ctx.getState()->getSVal(Condition);
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000060 if (X.isUndef()) {
Anna Zakscd656ca2011-10-18 23:06:21 +000061 // Generate a sink node, which implicitly marks both outgoing branches as
62 // infeasible.
Anna Zaksf236b652011-10-25 19:56:54 +000063 ExplodedNode *N = Ctx.generateSink();
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000064 if (N) {
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000065 if (!BT)
Argyrios Kyrtzidiscc05d512011-02-28 01:27:33 +000066 BT.reset(
67 new BuiltinBug("Branch condition evaluates to a garbage value"));
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000068
69 // What's going on here: we want to highlight the subexpression of the
70 // condition that is the most likely source of the "uninitialized
71 // branch condition." We do a recursive walk of the condition's
72 // subexpressions and roughly look for the most nested subexpression
73 // that binds to Undefined. We then highlight that expression's range.
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000074
75 // Get the predecessor node and check if is a PostStmt with the Stmt
76 // being the terminator condition. We want to inspect the state
77 // of that node instead because it will contain main information about
78 // the subexpressions.
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000079
80 // Note: any predecessor will do. They should have identical state,
81 // since all the BlockEdge did was act as an error sink since the value
82 // had to already be undefined.
Anna Zaks9c81bc22011-10-03 21:16:32 +000083 assert (!N->pred_empty());
84 const Expr *Ex = cast<Expr>(Condition);
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000085 ExplodedNode *PrevN = *N->pred_begin();
86 ProgramPoint P = PrevN->getLocation();
Ted Kremenek18c66fd2011-08-15 22:09:50 +000087 const ProgramState *St = N->getState();
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000088
Ted Kremenek9c378f72011-08-12 23:37:29 +000089 if (PostStmt *PS = dyn_cast<PostStmt>(&P))
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000090 if (PS->getStmt() == Ex)
91 St = PrevN->getState();
92
Anna Zaks9c81bc22011-10-03 21:16:32 +000093 FindUndefExpr FindIt(St);
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000094 Ex = FindIt.FindExpr(Ex);
Ted Kremenek616cf052009-11-23 18:12:03 +000095
96 // Emit the bug report.
Anna Zaks50bbc162011-08-19 22:33:38 +000097 BugReport *R = new BugReport(*BT, BT->getDescription(), N);
98 R->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Ex));
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000099 R->addRange(Ex->getSourceRange());
100
Anna Zaksf236b652011-10-25 19:56:54 +0000101 Ctx.EmitReport(R);
Zhongxing Xu0835e4c2009-11-23 03:20:54 +0000102 }
Zhongxing Xu0835e4c2009-11-23 03:20:54 +0000103 }
104}
Argyrios Kyrtzidiscc05d512011-02-28 01:27:33 +0000105
106void ento::registerUndefBranchChecker(CheckerManager &mgr) {
107 mgr.registerChecker<UndefBranchChecker>();
108}