blob: fc0b9c2ec6fd5a4907ceef89d96de2b3a9226efc [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;
Ted Kremenek5eca4822012-01-06 22:09:28 +000031 const LocationContext *LCtx;
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000032
Ted Kremenek5eca4822012-01-06 22:09:28 +000033 FindUndefExpr(const ProgramState *S, const LocationContext *L)
34 : St(S), LCtx(L) {}
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000035
Ted Kremenek9c378f72011-08-12 23:37:29 +000036 const Expr *FindExpr(const Expr *Ex) {
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000037 if (!MatchesCriteria(Ex))
38 return 0;
39
Zhongxing Xu03509ae2010-07-20 06:22:24 +000040 for (Stmt::const_child_iterator I = Ex->child_begin(),
41 E = Ex->child_end();I!=E;++I)
Ted Kremenek9c378f72011-08-12 23:37:29 +000042 if (const Expr *ExI = dyn_cast_or_null<Expr>(*I)) {
43 const Expr *E2 = FindExpr(ExI);
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000044 if (E2) return E2;
45 }
46
47 return Ex;
48 }
49
Ted Kremenek5eca4822012-01-06 22:09:28 +000050 bool MatchesCriteria(const Expr *Ex) {
51 return St->getSVal(Ex, LCtx).isUndef();
52 }
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000053 };
54
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000055public:
Anna Zaksf236b652011-10-25 19:56:54 +000056 void checkBranchCondition(const Stmt *Condition, CheckerContext &Ctx) const;
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000057};
58
59}
60
Argyrios Kyrtzidiscc05d512011-02-28 01:27:33 +000061void UndefBranchChecker::checkBranchCondition(const Stmt *Condition,
Anna Zaksf236b652011-10-25 19:56:54 +000062 CheckerContext &Ctx) const {
Ted Kremenek5eca4822012-01-06 22:09:28 +000063 SVal X = Ctx.getState()->getSVal(Condition, Ctx.getLocationContext());
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000064 if (X.isUndef()) {
Anna Zakscd656ca2011-10-18 23:06:21 +000065 // Generate a sink node, which implicitly marks both outgoing branches as
66 // infeasible.
Anna Zaksf236b652011-10-25 19:56:54 +000067 ExplodedNode *N = Ctx.generateSink();
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000068 if (N) {
Zhongxing Xu0835e4c2009-11-23 03:20:54 +000069 if (!BT)
Argyrios Kyrtzidiscc05d512011-02-28 01:27:33 +000070 BT.reset(
71 new BuiltinBug("Branch condition evaluates to a garbage value"));
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000072
73 // What's going on here: we want to highlight the subexpression of the
74 // condition that is the most likely source of the "uninitialized
75 // branch condition." We do a recursive walk of the condition's
76 // subexpressions and roughly look for the most nested subexpression
77 // that binds to Undefined. We then highlight that expression's range.
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000078
79 // Get the predecessor node and check if is a PostStmt with the Stmt
80 // being the terminator condition. We want to inspect the state
81 // of that node instead because it will contain main information about
82 // the subexpressions.
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000083
84 // Note: any predecessor will do. They should have identical state,
85 // since all the BlockEdge did was act as an error sink since the value
86 // had to already be undefined.
Anna Zaks9c81bc22011-10-03 21:16:32 +000087 assert (!N->pred_empty());
88 const Expr *Ex = cast<Expr>(Condition);
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000089 ExplodedNode *PrevN = *N->pred_begin();
90 ProgramPoint P = PrevN->getLocation();
Ted Kremenek18c66fd2011-08-15 22:09:50 +000091 const ProgramState *St = N->getState();
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000092
Ted Kremenek9c378f72011-08-12 23:37:29 +000093 if (PostStmt *PS = dyn_cast<PostStmt>(&P))
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000094 if (PS->getStmt() == Ex)
95 St = PrevN->getState();
96
Ted Kremenek5eca4822012-01-06 22:09:28 +000097 FindUndefExpr FindIt(St, Ctx.getLocationContext());
Zhongxing Xuf155dbf2009-11-23 03:29:59 +000098 Ex = FindIt.FindExpr(Ex);
Ted Kremenek616cf052009-11-23 18:12:03 +000099
100 // Emit the bug report.
Anna Zaks50bbc162011-08-19 22:33:38 +0000101 BugReport *R = new BugReport(*BT, BT->getDescription(), N);
102 R->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Ex));
Zhongxing Xuf155dbf2009-11-23 03:29:59 +0000103 R->addRange(Ex->getSourceRange());
104
Anna Zaksf236b652011-10-25 19:56:54 +0000105 Ctx.EmitReport(R);
Zhongxing Xu0835e4c2009-11-23 03:20:54 +0000106 }
Zhongxing Xu0835e4c2009-11-23 03:20:54 +0000107 }
108}
Argyrios Kyrtzidiscc05d512011-02-28 01:27:33 +0000109
110void ento::registerUndefBranchChecker(CheckerManager &mgr) {
111 mgr.registerChecker<UndefBranchChecker>();
112}