blob: 746b3f95d41eadd0b5d684700c36eced21c81ca7 [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:
27 ArrayBoundChecker() : BT(0) {}
28 static void *getTag();
29 void VisitLocation(CheckerContext &C, const Stmt *S, SVal l);
30};
31}
32
33void clang::RegisterArrayBoundChecker(GRExprEngine &Eng) {
34 Eng.registerCheck(new ArrayBoundChecker());
35}
36
37void *ArrayBoundChecker::getTag() {
38 static int x = 0; return &x;
39}
40
41void 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 Xue884ff82009-11-12 02:48:32 +000059 DefinedOrUnknownSVal NumElements
Zhongxing Xu3ed04d32010-01-18 08:54:31 +000060 = C.getStoreManager().getSizeInElements(state, ER->getSuperRegion(),
61 ER->getValueType(C.getASTContext()));
Zhongxing Xu58e689f2009-11-11 12:33:27 +000062
63 const GRState *StInBound = state->AssumeInBound(Idx, NumElements, true);
64 const GRState *StOutBound = state->AssumeInBound(Idx, NumElements, false);
65 if (StOutBound && !StInBound) {
Ted Kremenek19d67b52009-11-23 22:22:01 +000066 ExplodedNode *N = C.GenerateSink(StOutBound);
Zhongxing Xu58e689f2009-11-11 12:33:27 +000067 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 Kramerd02e2322009-11-14 12:08:24 +000080 new RangedBugReport(*BT, BT->getDescription(), N);
Zhongxing Xu58e689f2009-11-11 12:33:27 +000081
82 report->addRange(S->getSourceRange());
Zhongxing Xu58e689f2009-11-11 12:33:27 +000083 C.EmitReport(report);
Ted Kremenekc3120762009-11-23 23:23:26 +000084 return;
Zhongxing Xu58e689f2009-11-11 12:33:27 +000085 }
Ted Kremenekc3120762009-11-23 23:23:26 +000086
87 // Array bound check succeeded. From this point forward the array bound
88 // should always succeed.
89 assert(StInBound);
90 C.addTransition(StInBound);
Zhongxing Xu58e689f2009-11-11 12:33:27 +000091}