blob: df21595a4132d578b18179b6c8f39603eacc31a6 [file] [log] [blame]
Zhongxing Xu58e689f2009-11-11 12:33:27 +00001//== 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"
Benjamin Kramer5e2d2c22010-03-27 21:19:47 +000016#include "clang/Checker/BugReporter/BugType.h"
Ted Kremenek1309f9a2010-01-25 04:41:41 +000017#include "clang/Checker/PathSensitive/CheckerVisitor.h"
Benjamin Kramer5e2d2c22010-03-27 21:19:47 +000018#include "clang/Checker/PathSensitive/GRExprEngine.h"
Zhongxing Xu58e689f2009-11-11 12:33:27 +000019
20using namespace clang;
21
22namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000023class ArrayBoundChecker :
Zhongxing Xu58e689f2009-11-11 12:33:27 +000024 public CheckerVisitor<ArrayBoundChecker> {
25 BuiltinBug *BT;
26public:
Ted Kremenek342e9072010-12-20 21:22:47 +000027 ArrayBoundChecker() : BT(0) {}
28 static void *getTag() { static int x = 0; return &x; }
29 void visitLocation(CheckerContext &C, const Stmt *S, SVal l);
Zhongxing Xu58e689f2009-11-11 12:33:27 +000030};
31}
32
33void clang::RegisterArrayBoundChecker(GRExprEngine &Eng) {
34 Eng.registerCheck(new ArrayBoundChecker());
35}
36
Ted Kremenek342e9072010-12-20 21:22:47 +000037void ArrayBoundChecker::visitLocation(CheckerContext &C, const Stmt *S, SVal l){
Zhongxing Xu58e689f2009-11-11 12:33:27 +000038 // Check for out of bound array element access.
39 const MemRegion *R = l.getAsRegion();
40 if (!R)
41 return;
42
Zhongxing Xu58e689f2009-11-11 12:33:27 +000043 const ElementRegion *ER = dyn_cast<ElementRegion>(R);
44 if (!ER)
45 return;
46
47 // Get the index of the accessed element.
Gabor Greif89b06582010-09-09 10:51:37 +000048 DefinedOrUnknownSVal Idx = cast<DefinedOrUnknownSVal>(ER->getIndex());
Zhongxing Xu58e689f2009-11-11 12:33:27 +000049
Zhongxing Xu110eaf12010-11-26 09:14:07 +000050 // Zero index is always in bound, this also passes ElementRegions created for
51 // pointer casts.
52 if (Idx.isZeroConstant())
53 return;
54
Zhongxing Xu58e689f2009-11-11 12:33:27 +000055 const GRState *state = C.getState();
56
57 // Get the size of the array.
Zhongxing Xue884ff82009-11-12 02:48:32 +000058 DefinedOrUnknownSVal NumElements
Zhongxing Xu3ed04d32010-01-18 08:54:31 +000059 = C.getStoreManager().getSizeInElements(state, ER->getSuperRegion(),
Zhongxing Xu018220c2010-08-11 06:10:55 +000060 ER->getValueType());
Zhongxing Xu58e689f2009-11-11 12:33:27 +000061
Ted Kremenek28f47b92010-12-01 22:16:56 +000062 const GRState *StInBound = state->assumeInBound(Idx, NumElements, true);
63 const GRState *StOutBound = state->assumeInBound(Idx, NumElements, false);
Zhongxing Xu58e689f2009-11-11 12:33:27 +000064 if (StOutBound && !StInBound) {
Ted Kremenekd048c6e2010-12-20 21:19:09 +000065 ExplodedNode *N = C.generateSink(StOutBound);
Zhongxing Xu58e689f2009-11-11 12:33:27 +000066 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 Kramerd02e2322009-11-14 12:08:24 +000079 new RangedBugReport(*BT, BT->getDescription(), N);
Zhongxing Xu58e689f2009-11-11 12:33:27 +000080
81 report->addRange(S->getSourceRange());
Zhongxing Xu58e689f2009-11-11 12:33:27 +000082 C.EmitReport(report);
Ted Kremenekc3120762009-11-23 23:23:26 +000083 return;
Zhongxing Xu58e689f2009-11-11 12:33:27 +000084 }
Ted Kremenekc3120762009-11-23 23:23:26 +000085
86 // Array bound check succeeded. From this point forward the array bound
87 // should always succeed.
88 assert(StInBound);
89 C.addTransition(StInBound);
Zhongxing Xu58e689f2009-11-11 12:33:27 +000090}