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