Zhongxing Xu | 0835e4c | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 1 | //=== 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 Kyrtzidis | cc05d51 | 2011-02-28 01:27:33 +0000 | [diff] [blame] | 15 | #include "ClangSACheckers.h" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | cc05d51 | 2011-02-28 01:27:33 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 18 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Zhongxing Xu | 0835e4c | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 22 | using namespace ento; |
Zhongxing Xu | 0835e4c | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 23 | |
| 24 | namespace { |
| 25 | |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 26 | class UndefBranchChecker : public Checker<check::BranchCondition> { |
Argyrios Kyrtzidis | cc05d51 | 2011-02-28 01:27:33 +0000 | [diff] [blame] | 27 | mutable llvm::OwningPtr<BuiltinBug> BT; |
Zhongxing Xu | f155dbf | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 28 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 29 | struct FindUndefExpr { |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 30 | const ProgramState *St; |
Zhongxing Xu | f155dbf | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 31 | |
Anna Zaks | 9c81bc2 | 2011-10-03 21:16:32 +0000 | [diff] [blame] | 32 | FindUndefExpr(const ProgramState *S) : St(S) {} |
Zhongxing Xu | f155dbf | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 34 | const Expr *FindExpr(const Expr *Ex) { |
Zhongxing Xu | f155dbf | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 35 | if (!MatchesCriteria(Ex)) |
| 36 | return 0; |
| 37 | |
Zhongxing Xu | 03509ae | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 38 | for (Stmt::const_child_iterator I = Ex->child_begin(), |
| 39 | E = Ex->child_end();I!=E;++I) |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 40 | if (const Expr *ExI = dyn_cast_or_null<Expr>(*I)) { |
| 41 | const Expr *E2 = FindExpr(ExI); |
Zhongxing Xu | f155dbf | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 42 | if (E2) return E2; |
| 43 | } |
| 44 | |
| 45 | return Ex; |
| 46 | } |
| 47 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 48 | bool MatchesCriteria(const Expr *Ex) { return St->getSVal(Ex).isUndef(); } |
Zhongxing Xu | f155dbf | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
Zhongxing Xu | 0835e4c | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 51 | public: |
Anna Zaks | f236b65 | 2011-10-25 19:56:54 +0000 | [diff] [blame^] | 52 | void checkBranchCondition(const Stmt *Condition, CheckerContext &Ctx) const; |
Zhongxing Xu | 0835e4c | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | } |
| 56 | |
Argyrios Kyrtzidis | cc05d51 | 2011-02-28 01:27:33 +0000 | [diff] [blame] | 57 | void UndefBranchChecker::checkBranchCondition(const Stmt *Condition, |
Anna Zaks | f236b65 | 2011-10-25 19:56:54 +0000 | [diff] [blame^] | 58 | CheckerContext &Ctx) const { |
| 59 | SVal X = Ctx.getState()->getSVal(Condition); |
Zhongxing Xu | 0835e4c | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 60 | if (X.isUndef()) { |
Anna Zaks | cd656ca | 2011-10-18 23:06:21 +0000 | [diff] [blame] | 61 | // Generate a sink node, which implicitly marks both outgoing branches as |
| 62 | // infeasible. |
Anna Zaks | f236b65 | 2011-10-25 19:56:54 +0000 | [diff] [blame^] | 63 | ExplodedNode *N = Ctx.generateSink(); |
Zhongxing Xu | 0835e4c | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 64 | if (N) { |
Zhongxing Xu | 0835e4c | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 65 | if (!BT) |
Argyrios Kyrtzidis | cc05d51 | 2011-02-28 01:27:33 +0000 | [diff] [blame] | 66 | BT.reset( |
| 67 | new BuiltinBug("Branch condition evaluates to a garbage value")); |
Zhongxing Xu | f155dbf | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 68 | |
| 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 Xu | f155dbf | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 74 | |
| 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 Xu | f155dbf | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 79 | |
| 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 Zaks | 9c81bc2 | 2011-10-03 21:16:32 +0000 | [diff] [blame] | 83 | assert (!N->pred_empty()); |
| 84 | const Expr *Ex = cast<Expr>(Condition); |
Zhongxing Xu | f155dbf | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 85 | ExplodedNode *PrevN = *N->pred_begin(); |
| 86 | ProgramPoint P = PrevN->getLocation(); |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 87 | const ProgramState *St = N->getState(); |
Zhongxing Xu | f155dbf | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 88 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 89 | if (PostStmt *PS = dyn_cast<PostStmt>(&P)) |
Zhongxing Xu | f155dbf | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 90 | if (PS->getStmt() == Ex) |
| 91 | St = PrevN->getState(); |
| 92 | |
Anna Zaks | 9c81bc2 | 2011-10-03 21:16:32 +0000 | [diff] [blame] | 93 | FindUndefExpr FindIt(St); |
Zhongxing Xu | f155dbf | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 94 | Ex = FindIt.FindExpr(Ex); |
Ted Kremenek | 616cf05 | 2009-11-23 18:12:03 +0000 | [diff] [blame] | 95 | |
| 96 | // Emit the bug report. |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 97 | BugReport *R = new BugReport(*BT, BT->getDescription(), N); |
| 98 | R->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Ex)); |
Zhongxing Xu | f155dbf | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 99 | R->addRange(Ex->getSourceRange()); |
| 100 | |
Anna Zaks | f236b65 | 2011-10-25 19:56:54 +0000 | [diff] [blame^] | 101 | Ctx.EmitReport(R); |
Zhongxing Xu | 0835e4c | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 102 | } |
Zhongxing Xu | 0835e4c | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
Argyrios Kyrtzidis | cc05d51 | 2011-02-28 01:27:33 +0000 | [diff] [blame] | 105 | |
| 106 | void ento::registerUndefBranchChecker(CheckerManager &mgr) { |
| 107 | mgr.registerChecker<UndefBranchChecker>(); |
| 108 | } |