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 | |
| 15 | #include "GRExprEngineInternalChecks.h" |
| 16 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
| 17 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
| 18 | #include "clang/Analysis/PathSensitive/CheckerVisitor.h" |
| 19 | |
| 20 | using namespace clang; |
| 21 | |
| 22 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 23 | class ArrayBoundChecker : |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 24 | public CheckerVisitor<ArrayBoundChecker> { |
| 25 | BuiltinBug *BT; |
| 26 | public: |
| 27 | ArrayBoundChecker() : BT(0) {} |
| 28 | static void *getTag(); |
| 29 | void VisitLocation(CheckerContext &C, const Stmt *S, SVal l); |
| 30 | }; |
| 31 | } |
| 32 | |
| 33 | void clang::RegisterArrayBoundChecker(GRExprEngine &Eng) { |
| 34 | Eng.registerCheck(new ArrayBoundChecker()); |
| 35 | } |
| 36 | |
| 37 | void *ArrayBoundChecker::getTag() { |
| 38 | static int x = 0; return &x; |
| 39 | } |
| 40 | |
| 41 | void ArrayBoundChecker::VisitLocation(CheckerContext &C, const Stmt *S, SVal l){ |
| 42 | // Check for out of bound array element access. |
| 43 | const MemRegion *R = l.getAsRegion(); |
| 44 | if (!R) |
| 45 | return; |
| 46 | |
| 47 | R = R->StripCasts(); |
| 48 | |
| 49 | const ElementRegion *ER = dyn_cast<ElementRegion>(R); |
| 50 | if (!ER) |
| 51 | return; |
| 52 | |
| 53 | // Get the index of the accessed element. |
| 54 | DefinedOrUnknownSVal &Idx = cast<DefinedOrUnknownSVal>(ER->getIndex()); |
| 55 | |
| 56 | const GRState *state = C.getState(); |
| 57 | |
| 58 | // Get the size of the array. |
Zhongxing Xu | e884ff8 | 2009-11-12 02:48:32 +0000 | [diff] [blame] | 59 | DefinedOrUnknownSVal NumElements |
| 60 | = C.getStoreManager().getSizeInElements(state, ER->getSuperRegion()); |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 61 | |
| 62 | const GRState *StInBound = state->AssumeInBound(Idx, NumElements, true); |
| 63 | const GRState *StOutBound = state->AssumeInBound(Idx, NumElements, false); |
| 64 | if (StOutBound && !StInBound) { |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 65 | ExplodedNode *N = C.GenerateSink(StOutBound); |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 66 | if (!N) |
| 67 | return; |
| 68 | |
| 69 | if (!BT) |
| 70 | BT = new BuiltinBug("Out-of-bound array access", |
| 71 | "Access out-of-bound array element (buffer overflow)"); |
| 72 | |
| 73 | // FIXME: It would be nice to eventually make this diagnostic more clear, |
| 74 | // e.g., by referencing the original declaration or by saying *why* this |
| 75 | // reference is outside the range. |
| 76 | |
| 77 | // Generate a report for this bug. |
| 78 | RangedBugReport *report = |
Benjamin Kramer | d02e232 | 2009-11-14 12:08:24 +0000 | [diff] [blame] | 79 | new RangedBugReport(*BT, BT->getDescription(), N); |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 80 | |
| 81 | report->addRange(S->getSourceRange()); |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 82 | C.EmitReport(report); |
Ted Kremenek | c312076 | 2009-11-23 23:23:26 +0000 | [diff] [blame] | 83 | return; |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 84 | } |
Ted Kremenek | c312076 | 2009-11-23 23:23:26 +0000 | [diff] [blame] | 85 | |
| 86 | // Array bound check succeeded. From this point forward the array bound |
| 87 | // should always succeed. |
| 88 | assert(StInBound); |
| 89 | C.addTransition(StInBound); |
Zhongxing Xu | 58e689f | 2009-11-11 12:33:27 +0000 | [diff] [blame] | 90 | } |