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" |
| 16 | #include "clang/StaticAnalyzer/Core/CheckerV2.h" |
| 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 | cc05d51 | 2011-02-28 01:27:33 +0000 | [diff] [blame^] | 26 | class UndefBranchChecker : public CheckerV2<check::BranchCondition> { |
| 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 { |
Zhongxing Xu | f155dbf | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 30 | GRStateManager& VM; |
| 31 | const GRState* St; |
| 32 | |
| 33 | FindUndefExpr(GRStateManager& V, const GRState* S) : VM(V), St(S) {} |
| 34 | |
Zhongxing Xu | 03509ae | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 35 | const Expr* FindExpr(const Expr* Ex) { |
Zhongxing Xu | f155dbf | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 36 | if (!MatchesCriteria(Ex)) |
| 37 | return 0; |
| 38 | |
Zhongxing Xu | 03509ae | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 39 | for (Stmt::const_child_iterator I = Ex->child_begin(), |
| 40 | E = Ex->child_end();I!=E;++I) |
| 41 | if (const Expr* ExI = dyn_cast_or_null<Expr>(*I)) { |
| 42 | const Expr* E2 = FindExpr(ExI); |
Zhongxing Xu | f155dbf | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 43 | if (E2) return E2; |
| 44 | } |
| 45 | |
| 46 | return Ex; |
| 47 | } |
| 48 | |
Zhongxing Xu | 03509ae | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 49 | bool MatchesCriteria(const Expr* Ex) { return St->getSVal(Ex).isUndef(); } |
Zhongxing Xu | f155dbf | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
Zhongxing Xu | 0835e4c | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 52 | public: |
Argyrios Kyrtzidis | cc05d51 | 2011-02-28 01:27:33 +0000 | [diff] [blame^] | 53 | void checkBranchCondition(const Stmt *Condition, BranchNodeBuilder &Builder, |
| 54 | ExprEngine &Eng) const; |
Zhongxing Xu | 0835e4c | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | } |
| 58 | |
Argyrios Kyrtzidis | cc05d51 | 2011-02-28 01:27:33 +0000 | [diff] [blame^] | 59 | void UndefBranchChecker::checkBranchCondition(const Stmt *Condition, |
| 60 | BranchNodeBuilder &Builder, |
| 61 | ExprEngine &Eng) const { |
Zhongxing Xu | 0835e4c | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 62 | const GRState *state = Builder.getState(); |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 63 | SVal X = state->getSVal(Condition); |
Zhongxing Xu | 0835e4c | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 64 | if (X.isUndef()) { |
| 65 | ExplodedNode *N = Builder.generateNode(state, true); |
| 66 | if (N) { |
| 67 | N->markAsSink(); |
| 68 | if (!BT) |
Argyrios Kyrtzidis | cc05d51 | 2011-02-28 01:27:33 +0000 | [diff] [blame^] | 69 | BT.reset( |
| 70 | new BuiltinBug("Branch condition evaluates to a garbage value")); |
Zhongxing Xu | f155dbf | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 71 | |
| 72 | // What's going on here: we want to highlight the subexpression of the |
| 73 | // condition that is the most likely source of the "uninitialized |
| 74 | // branch condition." We do a recursive walk of the condition's |
| 75 | // subexpressions and roughly look for the most nested subexpression |
| 76 | // that binds to Undefined. We then highlight that expression's range. |
| 77 | BlockEdge B = cast<BlockEdge>(N->getLocation()); |
Zhongxing Xu | 03509ae | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 78 | const Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition()); |
Zhongxing Xu | f155dbf | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 79 | assert (Ex && "Block must have a terminator."); |
| 80 | |
| 81 | // Get the predecessor node and check if is a PostStmt with the Stmt |
| 82 | // being the terminator condition. We want to inspect the state |
| 83 | // of that node instead because it will contain main information about |
| 84 | // the subexpressions. |
| 85 | assert (!N->pred_empty()); |
| 86 | |
| 87 | // Note: any predecessor will do. They should have identical state, |
| 88 | // since all the BlockEdge did was act as an error sink since the value |
| 89 | // had to already be undefined. |
| 90 | ExplodedNode *PrevN = *N->pred_begin(); |
| 91 | ProgramPoint P = PrevN->getLocation(); |
| 92 | const GRState* St = N->getState(); |
| 93 | |
| 94 | if (PostStmt* PS = dyn_cast<PostStmt>(&P)) |
| 95 | if (PS->getStmt() == Ex) |
| 96 | St = PrevN->getState(); |
| 97 | |
| 98 | FindUndefExpr FindIt(Eng.getStateManager(), St); |
| 99 | Ex = FindIt.FindExpr(Ex); |
Ted Kremenek | 616cf05 | 2009-11-23 18:12:03 +0000 | [diff] [blame] | 100 | |
| 101 | // Emit the bug report. |
| 102 | EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getDescription(),N); |
| 103 | R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, Ex); |
Zhongxing Xu | f155dbf | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 104 | R->addRange(Ex->getSourceRange()); |
| 105 | |
Zhongxing Xu | 0835e4c | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 106 | Eng.getBugReporter().EmitReport(R); |
| 107 | } |
| 108 | |
| 109 | Builder.markInfeasible(true); |
| 110 | Builder.markInfeasible(false); |
| 111 | } |
| 112 | } |
Argyrios Kyrtzidis | cc05d51 | 2011-02-28 01:27:33 +0000 | [diff] [blame^] | 113 | |
| 114 | void ento::registerUndefBranchChecker(CheckerManager &mgr) { |
| 115 | mgr.registerChecker<UndefBranchChecker>(); |
| 116 | } |