Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 1 | //== ArrayBoundChecker.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 ArrayBoundChecker, which is a path-sensitive check |
| 11 | // which looks for an out-of-bound array element access. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Argyrios Kyrtzidis | 8be5b3a | 2011-02-24 08:42:12 +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 | 8be5b3a | 2011-02-24 08:42:12 +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" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 23 | using namespace ento; |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 24 | |
| 25 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 26 | class ArrayBoundChecker : |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame^] | 27 | public Checker<check::Location> { |
Argyrios Kyrtzidis | 8be5b3a | 2011-02-24 08:42:12 +0000 | [diff] [blame] | 28 | mutable llvm::OwningPtr<BuiltinBug> BT; |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 29 | public: |
Argyrios Kyrtzidis | 8be5b3a | 2011-02-24 08:42:12 +0000 | [diff] [blame] | 30 | void checkLocation(SVal l, bool isLoad, CheckerContext &C) const; |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 31 | }; |
| 32 | } |
| 33 | |
Argyrios Kyrtzidis | 8be5b3a | 2011-02-24 08:42:12 +0000 | [diff] [blame] | 34 | void ArrayBoundChecker::checkLocation(SVal l, bool isLoad, |
| 35 | CheckerContext &C) const { |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 36 | // Check for out of bound array element access. |
| 37 | const MemRegion *R = l.getAsRegion(); |
| 38 | if (!R) |
| 39 | return; |
| 40 | |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 41 | const ElementRegion *ER = dyn_cast<ElementRegion>(R); |
| 42 | if (!ER) |
| 43 | return; |
| 44 | |
| 45 | // Get the index of the accessed element. |
Gabor Greif | 89b0658 | 2010-09-09 10:51:37 +0000 | [diff] [blame] | 46 | DefinedOrUnknownSVal Idx = cast<DefinedOrUnknownSVal>(ER->getIndex()); |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 47 | |
Zhongxing Xu | 110eaf1 | 2010-11-26 09:14:07 +0000 | [diff] [blame] | 48 | // Zero index is always in bound, this also passes ElementRegions created for |
| 49 | // pointer casts. |
| 50 | if (Idx.isZeroConstant()) |
| 51 | return; |
| 52 | |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 53 | const GRState *state = C.getState(); |
| 54 | |
| 55 | // Get the size of the array. |
Zhongxing Xu | e884ff8 | 2009-11-12 02:48:32 +0000 | [diff] [blame] | 56 | DefinedOrUnknownSVal NumElements |
Zhongxing Xu | 3ed04d3 | 2010-01-18 08:54:31 +0000 | [diff] [blame] | 57 | = C.getStoreManager().getSizeInElements(state, ER->getSuperRegion(), |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 58 | ER->getValueType()); |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 59 | |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 60 | const GRState *StInBound = state->assumeInBound(Idx, NumElements, true); |
| 61 | const GRState *StOutBound = state->assumeInBound(Idx, NumElements, false); |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 62 | if (StOutBound && !StInBound) { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 63 | ExplodedNode *N = C.generateSink(StOutBound); |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 64 | if (!N) |
| 65 | return; |
| 66 | |
| 67 | if (!BT) |
Argyrios Kyrtzidis | 8be5b3a | 2011-02-24 08:42:12 +0000 | [diff] [blame] | 68 | BT.reset(new BuiltinBug("Out-of-bound array access", |
| 69 | "Access out-of-bound array element (buffer overflow)")); |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 70 | |
| 71 | // FIXME: It would be nice to eventually make this diagnostic more clear, |
| 72 | // e.g., by referencing the original declaration or by saying *why* this |
| 73 | // reference is outside the range. |
| 74 | |
| 75 | // Generate a report for this bug. |
| 76 | RangedBugReport *report = |
Benjamin Kramer | d02e232 | 2009-11-14 12:08:24 +0000 | [diff] [blame] | 77 | new RangedBugReport(*BT, BT->getDescription(), N); |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 78 | |
Argyrios Kyrtzidis | 8be5b3a | 2011-02-24 08:42:12 +0000 | [diff] [blame] | 79 | report->addRange(C.getStmt()->getSourceRange()); |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 80 | C.EmitReport(report); |
Ted Kremenek | c312076 | 2009-11-23 23:23:26 +0000 | [diff] [blame] | 81 | return; |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 82 | } |
Ted Kremenek | c312076 | 2009-11-23 23:23:26 +0000 | [diff] [blame] | 83 | |
| 84 | // Array bound check succeeded. From this point forward the array bound |
| 85 | // should always succeed. |
| 86 | assert(StInBound); |
| 87 | C.addTransition(StInBound); |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 88 | } |
Argyrios Kyrtzidis | 8be5b3a | 2011-02-24 08:42:12 +0000 | [diff] [blame] | 89 | |
| 90 | void ento::registerArrayBoundChecker(CheckerManager &mgr) { |
| 91 | mgr.registerChecker<ArrayBoundChecker>(); |
| 92 | } |