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