Zhongxing Xu | 56dd5f0 | 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 | 753b3ca | 2011-02-28 01:27:33 +0000 | [diff] [blame] | 15 | #include "ClangSACheckers.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 753b3ca | 2011-02-28 01:27:33 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 19 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 20 | #include <utility> |
Zhongxing Xu | 56dd5f0 | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 23 | using namespace ento; |
Zhongxing Xu | 56dd5f0 | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 24 | |
| 25 | namespace { |
| 26 | |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 27 | class UndefBranchChecker : public Checker<check::BranchCondition> { |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 28 | mutable std::unique_ptr<BuiltinBug> BT; |
Zhongxing Xu | 5f76620 | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 29 | |
Kovarththanan Rajaratnam | 65c6566 | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 30 | struct FindUndefExpr { |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 31 | ProgramStateRef St; |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 32 | const LocationContext *LCtx; |
Zhongxing Xu | 5f76620 | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 34 | FindUndefExpr(ProgramStateRef S, const LocationContext *L) |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 35 | : St(std::move(S)), LCtx(L) {} |
Zhongxing Xu | 5f76620 | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 36 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 37 | const Expr *FindExpr(const Expr *Ex) { |
Zhongxing Xu | 5f76620 | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 38 | if (!MatchesCriteria(Ex)) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 39 | return nullptr; |
Zhongxing Xu | 5f76620 | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 40 | |
Benjamin Kramer | 973431b | 2015-07-03 15:12:24 +0000 | [diff] [blame] | 41 | for (const Stmt *SubStmt : Ex->children()) |
| 42 | if (const Expr *ExI = dyn_cast_or_null<Expr>(SubStmt)) |
| 43 | if (const Expr *E2 = FindExpr(ExI)) |
| 44 | return E2; |
Zhongxing Xu | 5f76620 | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 45 | |
| 46 | return Ex; |
| 47 | } |
| 48 | |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 49 | bool MatchesCriteria(const Expr *Ex) { |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 50 | return St->getSVal(Ex, LCtx).isUndef(); |
| 51 | } |
Zhongxing Xu | 5f76620 | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
Zhongxing Xu | 56dd5f0 | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 54 | public: |
Anna Zaks | f380534 | 2011-10-25 19:56:54 +0000 | [diff] [blame] | 55 | void checkBranchCondition(const Stmt *Condition, CheckerContext &Ctx) const; |
Zhongxing Xu | 56dd5f0 | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 58 | } |
Zhongxing Xu | 56dd5f0 | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 59 | |
Argyrios Kyrtzidis | 753b3ca | 2011-02-28 01:27:33 +0000 | [diff] [blame] | 60 | void UndefBranchChecker::checkBranchCondition(const Stmt *Condition, |
Anna Zaks | f380534 | 2011-10-25 19:56:54 +0000 | [diff] [blame] | 61 | CheckerContext &Ctx) const { |
George Karpenkov | d703ec9 | 2018-01-17 20:27:29 +0000 | [diff] [blame] | 62 | SVal X = Ctx.getSVal(Condition); |
Zhongxing Xu | 56dd5f0 | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 63 | if (X.isUndef()) { |
Anna Zaks | 6d285c5 | 2011-10-18 23:06:21 +0000 | [diff] [blame] | 64 | // Generate a sink node, which implicitly marks both outgoing branches as |
| 65 | // infeasible. |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 66 | ExplodedNode *N = Ctx.generateErrorNode(); |
Zhongxing Xu | 56dd5f0 | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 67 | if (N) { |
Zhongxing Xu | 56dd5f0 | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 68 | if (!BT) |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 69 | BT.reset(new BuiltinBug( |
| 70 | this, "Branch condition evaluates to a garbage value")); |
Zhongxing Xu | 5f76620 | 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. |
Zhongxing Xu | 5f76620 | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 77 | |
| 78 | // Get the predecessor node and check if is a PostStmt with the Stmt |
| 79 | // being the terminator condition. We want to inspect the state |
| 80 | // of that node instead because it will contain main information about |
| 81 | // the subexpressions. |
Zhongxing Xu | 5f76620 | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 82 | |
| 83 | // Note: any predecessor will do. They should have identical state, |
| 84 | // since all the BlockEdge did was act as an error sink since the value |
| 85 | // had to already be undefined. |
Anna Zaks | c42197d | 2011-10-03 21:16:32 +0000 | [diff] [blame] | 86 | assert (!N->pred_empty()); |
| 87 | const Expr *Ex = cast<Expr>(Condition); |
Zhongxing Xu | 5f76620 | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 88 | ExplodedNode *PrevN = *N->pred_begin(); |
| 89 | ProgramPoint P = PrevN->getLocation(); |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 90 | ProgramStateRef St = N->getState(); |
Zhongxing Xu | 5f76620 | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 91 | |
David Blaikie | 87396b9 | 2013-02-21 22:23:56 +0000 | [diff] [blame] | 92 | if (Optional<PostStmt> PS = P.getAs<PostStmt>()) |
Zhongxing Xu | 5f76620 | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 93 | if (PS->getStmt() == Ex) |
| 94 | St = PrevN->getState(); |
| 95 | |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 96 | FindUndefExpr FindIt(St, Ctx.getLocationContext()); |
Zhongxing Xu | 5f76620 | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 97 | Ex = FindIt.FindExpr(Ex); |
Ted Kremenek | 02d6aca | 2009-11-23 18:12:03 +0000 | [diff] [blame] | 98 | |
| 99 | // Emit the bug report. |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 100 | auto R = llvm::make_unique<BugReport>(*BT, BT->getDescription(), N); |
Jordan Rose | a0f7d35 | 2012-08-28 00:50:51 +0000 | [diff] [blame] | 101 | bugreporter::trackNullOrUndefValue(N, Ex, *R); |
Zhongxing Xu | 5f76620 | 2009-11-23 03:29:59 +0000 | [diff] [blame] | 102 | R->addRange(Ex->getSourceRange()); |
| 103 | |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 104 | Ctx.emitReport(std::move(R)); |
Zhongxing Xu | 56dd5f0 | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 105 | } |
Zhongxing Xu | 56dd5f0 | 2009-11-23 03:20:54 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
Argyrios Kyrtzidis | 753b3ca | 2011-02-28 01:27:33 +0000 | [diff] [blame] | 108 | |
| 109 | void ento::registerUndefBranchChecker(CheckerManager &mgr) { |
| 110 | mgr.registerChecker<UndefBranchChecker>(); |
| 111 | } |