xref: /freebsd/contrib/llvm-project/clang/lib/StaticAnalyzer/Checkers/ArrayBoundCheckerV2.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //== ArrayBoundCheckerV2.cpp ------------------------------------*- C++ -*--==//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file defines ArrayBoundCheckerV2, which is a path-sensitive check
10*0b57cec5SDimitry Andric // which looks for an out-of-bound array element access.
11*0b57cec5SDimitry Andric //
12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
13*0b57cec5SDimitry Andric 
14*0b57cec5SDimitry Andric #include "Taint.h"
15*0b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
16*0b57cec5SDimitry Andric #include "clang/AST/CharUnits.h"
17*0b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
18*0b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/Checker.h"
19*0b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/CheckerManager.h"
20*0b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
21*0b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
22*0b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
23*0b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
24*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
25*0b57cec5SDimitry Andric 
26*0b57cec5SDimitry Andric using namespace clang;
27*0b57cec5SDimitry Andric using namespace ento;
28*0b57cec5SDimitry Andric using namespace taint;
29*0b57cec5SDimitry Andric 
30*0b57cec5SDimitry Andric namespace {
31*0b57cec5SDimitry Andric class ArrayBoundCheckerV2 :
32*0b57cec5SDimitry Andric     public Checker<check::Location> {
33*0b57cec5SDimitry Andric   mutable std::unique_ptr<BuiltinBug> BT;
34*0b57cec5SDimitry Andric 
35*0b57cec5SDimitry Andric   enum OOB_Kind { OOB_Precedes, OOB_Excedes, OOB_Tainted };
36*0b57cec5SDimitry Andric 
37*0b57cec5SDimitry Andric   void reportOOB(CheckerContext &C, ProgramStateRef errorState, OOB_Kind kind,
38*0b57cec5SDimitry Andric                  std::unique_ptr<BugReporterVisitor> Visitor = nullptr) const;
39*0b57cec5SDimitry Andric 
40*0b57cec5SDimitry Andric public:
41*0b57cec5SDimitry Andric   void checkLocation(SVal l, bool isLoad, const Stmt*S,
42*0b57cec5SDimitry Andric                      CheckerContext &C) const;
43*0b57cec5SDimitry Andric };
44*0b57cec5SDimitry Andric 
45*0b57cec5SDimitry Andric // FIXME: Eventually replace RegionRawOffset with this class.
46*0b57cec5SDimitry Andric class RegionRawOffsetV2 {
47*0b57cec5SDimitry Andric private:
48*0b57cec5SDimitry Andric   const SubRegion *baseRegion;
49*0b57cec5SDimitry Andric   SVal byteOffset;
50*0b57cec5SDimitry Andric 
51*0b57cec5SDimitry Andric   RegionRawOffsetV2()
52*0b57cec5SDimitry Andric     : baseRegion(nullptr), byteOffset(UnknownVal()) {}
53*0b57cec5SDimitry Andric 
54*0b57cec5SDimitry Andric public:
55*0b57cec5SDimitry Andric   RegionRawOffsetV2(const SubRegion* base, SVal offset)
56*0b57cec5SDimitry Andric     : baseRegion(base), byteOffset(offset) {}
57*0b57cec5SDimitry Andric 
58*0b57cec5SDimitry Andric   NonLoc getByteOffset() const { return byteOffset.castAs<NonLoc>(); }
59*0b57cec5SDimitry Andric   const SubRegion *getRegion() const { return baseRegion; }
60*0b57cec5SDimitry Andric 
61*0b57cec5SDimitry Andric   static RegionRawOffsetV2 computeOffset(ProgramStateRef state,
62*0b57cec5SDimitry Andric                                          SValBuilder &svalBuilder,
63*0b57cec5SDimitry Andric                                          SVal location);
64*0b57cec5SDimitry Andric 
65*0b57cec5SDimitry Andric   void dump() const;
66*0b57cec5SDimitry Andric   void dumpToStream(raw_ostream &os) const;
67*0b57cec5SDimitry Andric };
68*0b57cec5SDimitry Andric }
69*0b57cec5SDimitry Andric 
70*0b57cec5SDimitry Andric static SVal computeExtentBegin(SValBuilder &svalBuilder,
71*0b57cec5SDimitry Andric                                const MemRegion *region) {
72*0b57cec5SDimitry Andric   const MemSpaceRegion *SR = region->getMemorySpace();
73*0b57cec5SDimitry Andric   if (SR->getKind() == MemRegion::UnknownSpaceRegionKind)
74*0b57cec5SDimitry Andric     return UnknownVal();
75*0b57cec5SDimitry Andric   else
76*0b57cec5SDimitry Andric     return svalBuilder.makeZeroArrayIndex();
77*0b57cec5SDimitry Andric }
78*0b57cec5SDimitry Andric 
79*0b57cec5SDimitry Andric // TODO: once the constraint manager is smart enough to handle non simplified
80*0b57cec5SDimitry Andric // symbolic expressions remove this function. Note that this can not be used in
81*0b57cec5SDimitry Andric // the constraint manager as is, since this does not handle overflows. It is
82*0b57cec5SDimitry Andric // safe to assume, however, that memory offsets will not overflow.
83*0b57cec5SDimitry Andric static std::pair<NonLoc, nonloc::ConcreteInt>
84*0b57cec5SDimitry Andric getSimplifiedOffsets(NonLoc offset, nonloc::ConcreteInt extent,
85*0b57cec5SDimitry Andric                      SValBuilder &svalBuilder) {
86*0b57cec5SDimitry Andric   Optional<nonloc::SymbolVal> SymVal = offset.getAs<nonloc::SymbolVal>();
87*0b57cec5SDimitry Andric   if (SymVal && SymVal->isExpression()) {
88*0b57cec5SDimitry Andric     if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SymVal->getSymbol())) {
89*0b57cec5SDimitry Andric       llvm::APSInt constant =
90*0b57cec5SDimitry Andric           APSIntType(extent.getValue()).convert(SIE->getRHS());
91*0b57cec5SDimitry Andric       switch (SIE->getOpcode()) {
92*0b57cec5SDimitry Andric       case BO_Mul:
93*0b57cec5SDimitry Andric         // The constant should never be 0 here, since it the result of scaling
94*0b57cec5SDimitry Andric         // based on the size of a type which is never 0.
95*0b57cec5SDimitry Andric         if ((extent.getValue() % constant) != 0)
96*0b57cec5SDimitry Andric           return std::pair<NonLoc, nonloc::ConcreteInt>(offset, extent);
97*0b57cec5SDimitry Andric         else
98*0b57cec5SDimitry Andric           return getSimplifiedOffsets(
99*0b57cec5SDimitry Andric               nonloc::SymbolVal(SIE->getLHS()),
100*0b57cec5SDimitry Andric               svalBuilder.makeIntVal(extent.getValue() / constant),
101*0b57cec5SDimitry Andric               svalBuilder);
102*0b57cec5SDimitry Andric       case BO_Add:
103*0b57cec5SDimitry Andric         return getSimplifiedOffsets(
104*0b57cec5SDimitry Andric             nonloc::SymbolVal(SIE->getLHS()),
105*0b57cec5SDimitry Andric             svalBuilder.makeIntVal(extent.getValue() - constant), svalBuilder);
106*0b57cec5SDimitry Andric       default:
107*0b57cec5SDimitry Andric         break;
108*0b57cec5SDimitry Andric       }
109*0b57cec5SDimitry Andric     }
110*0b57cec5SDimitry Andric   }
111*0b57cec5SDimitry Andric 
112*0b57cec5SDimitry Andric   return std::pair<NonLoc, nonloc::ConcreteInt>(offset, extent);
113*0b57cec5SDimitry Andric }
114*0b57cec5SDimitry Andric 
115*0b57cec5SDimitry Andric void ArrayBoundCheckerV2::checkLocation(SVal location, bool isLoad,
116*0b57cec5SDimitry Andric                                         const Stmt* LoadS,
117*0b57cec5SDimitry Andric                                         CheckerContext &checkerContext) const {
118*0b57cec5SDimitry Andric 
119*0b57cec5SDimitry Andric   // NOTE: Instead of using ProgramState::assumeInBound(), we are prototyping
120*0b57cec5SDimitry Andric   // some new logic here that reasons directly about memory region extents.
121*0b57cec5SDimitry Andric   // Once that logic is more mature, we can bring it back to assumeInBound()
122*0b57cec5SDimitry Andric   // for all clients to use.
123*0b57cec5SDimitry Andric   //
124*0b57cec5SDimitry Andric   // The algorithm we are using here for bounds checking is to see if the
125*0b57cec5SDimitry Andric   // memory access is within the extent of the base region.  Since we
126*0b57cec5SDimitry Andric   // have some flexibility in defining the base region, we can achieve
127*0b57cec5SDimitry Andric   // various levels of conservatism in our buffer overflow checking.
128*0b57cec5SDimitry Andric   ProgramStateRef state = checkerContext.getState();
129*0b57cec5SDimitry Andric 
130*0b57cec5SDimitry Andric   SValBuilder &svalBuilder = checkerContext.getSValBuilder();
131*0b57cec5SDimitry Andric   const RegionRawOffsetV2 &rawOffset =
132*0b57cec5SDimitry Andric     RegionRawOffsetV2::computeOffset(state, svalBuilder, location);
133*0b57cec5SDimitry Andric 
134*0b57cec5SDimitry Andric   if (!rawOffset.getRegion())
135*0b57cec5SDimitry Andric     return;
136*0b57cec5SDimitry Andric 
137*0b57cec5SDimitry Andric   NonLoc rawOffsetVal = rawOffset.getByteOffset();
138*0b57cec5SDimitry Andric 
139*0b57cec5SDimitry Andric   // CHECK LOWER BOUND: Is byteOffset < extent begin?
140*0b57cec5SDimitry Andric   //  If so, we are doing a load/store
141*0b57cec5SDimitry Andric   //  before the first valid offset in the memory region.
142*0b57cec5SDimitry Andric 
143*0b57cec5SDimitry Andric   SVal extentBegin = computeExtentBegin(svalBuilder, rawOffset.getRegion());
144*0b57cec5SDimitry Andric 
145*0b57cec5SDimitry Andric   if (Optional<NonLoc> NV = extentBegin.getAs<NonLoc>()) {
146*0b57cec5SDimitry Andric     if (NV->getAs<nonloc::ConcreteInt>()) {
147*0b57cec5SDimitry Andric       std::pair<NonLoc, nonloc::ConcreteInt> simplifiedOffsets =
148*0b57cec5SDimitry Andric           getSimplifiedOffsets(rawOffset.getByteOffset(),
149*0b57cec5SDimitry Andric                                NV->castAs<nonloc::ConcreteInt>(),
150*0b57cec5SDimitry Andric                                svalBuilder);
151*0b57cec5SDimitry Andric       rawOffsetVal = simplifiedOffsets.first;
152*0b57cec5SDimitry Andric       *NV = simplifiedOffsets.second;
153*0b57cec5SDimitry Andric     }
154*0b57cec5SDimitry Andric 
155*0b57cec5SDimitry Andric     SVal lowerBound = svalBuilder.evalBinOpNN(state, BO_LT, rawOffsetVal, *NV,
156*0b57cec5SDimitry Andric                                               svalBuilder.getConditionType());
157*0b57cec5SDimitry Andric 
158*0b57cec5SDimitry Andric     Optional<NonLoc> lowerBoundToCheck = lowerBound.getAs<NonLoc>();
159*0b57cec5SDimitry Andric     if (!lowerBoundToCheck)
160*0b57cec5SDimitry Andric       return;
161*0b57cec5SDimitry Andric 
162*0b57cec5SDimitry Andric     ProgramStateRef state_precedesLowerBound, state_withinLowerBound;
163*0b57cec5SDimitry Andric     std::tie(state_precedesLowerBound, state_withinLowerBound) =
164*0b57cec5SDimitry Andric       state->assume(*lowerBoundToCheck);
165*0b57cec5SDimitry Andric 
166*0b57cec5SDimitry Andric     // Are we constrained enough to definitely precede the lower bound?
167*0b57cec5SDimitry Andric     if (state_precedesLowerBound && !state_withinLowerBound) {
168*0b57cec5SDimitry Andric       reportOOB(checkerContext, state_precedesLowerBound, OOB_Precedes);
169*0b57cec5SDimitry Andric       return;
170*0b57cec5SDimitry Andric     }
171*0b57cec5SDimitry Andric 
172*0b57cec5SDimitry Andric     // Otherwise, assume the constraint of the lower bound.
173*0b57cec5SDimitry Andric     assert(state_withinLowerBound);
174*0b57cec5SDimitry Andric     state = state_withinLowerBound;
175*0b57cec5SDimitry Andric   }
176*0b57cec5SDimitry Andric 
177*0b57cec5SDimitry Andric   do {
178*0b57cec5SDimitry Andric     // CHECK UPPER BOUND: Is byteOffset >= extent(baseRegion)?  If so,
179*0b57cec5SDimitry Andric     // we are doing a load/store after the last valid offset.
180*0b57cec5SDimitry Andric     DefinedOrUnknownSVal extentVal =
181*0b57cec5SDimitry Andric       rawOffset.getRegion()->getExtent(svalBuilder);
182*0b57cec5SDimitry Andric     if (!extentVal.getAs<NonLoc>())
183*0b57cec5SDimitry Andric       break;
184*0b57cec5SDimitry Andric 
185*0b57cec5SDimitry Andric     if (extentVal.getAs<nonloc::ConcreteInt>()) {
186*0b57cec5SDimitry Andric       std::pair<NonLoc, nonloc::ConcreteInt> simplifiedOffsets =
187*0b57cec5SDimitry Andric           getSimplifiedOffsets(rawOffset.getByteOffset(),
188*0b57cec5SDimitry Andric                                extentVal.castAs<nonloc::ConcreteInt>(),
189*0b57cec5SDimitry Andric                                svalBuilder);
190*0b57cec5SDimitry Andric       rawOffsetVal = simplifiedOffsets.first;
191*0b57cec5SDimitry Andric       extentVal = simplifiedOffsets.second;
192*0b57cec5SDimitry Andric     }
193*0b57cec5SDimitry Andric 
194*0b57cec5SDimitry Andric     SVal upperbound = svalBuilder.evalBinOpNN(state, BO_GE, rawOffsetVal,
195*0b57cec5SDimitry Andric                                               extentVal.castAs<NonLoc>(),
196*0b57cec5SDimitry Andric                                               svalBuilder.getConditionType());
197*0b57cec5SDimitry Andric 
198*0b57cec5SDimitry Andric     Optional<NonLoc> upperboundToCheck = upperbound.getAs<NonLoc>();
199*0b57cec5SDimitry Andric     if (!upperboundToCheck)
200*0b57cec5SDimitry Andric       break;
201*0b57cec5SDimitry Andric 
202*0b57cec5SDimitry Andric     ProgramStateRef state_exceedsUpperBound, state_withinUpperBound;
203*0b57cec5SDimitry Andric     std::tie(state_exceedsUpperBound, state_withinUpperBound) =
204*0b57cec5SDimitry Andric       state->assume(*upperboundToCheck);
205*0b57cec5SDimitry Andric 
206*0b57cec5SDimitry Andric     // If we are under constrained and the index variables are tainted, report.
207*0b57cec5SDimitry Andric     if (state_exceedsUpperBound && state_withinUpperBound) {
208*0b57cec5SDimitry Andric       SVal ByteOffset = rawOffset.getByteOffset();
209*0b57cec5SDimitry Andric       if (isTainted(state, ByteOffset)) {
210*0b57cec5SDimitry Andric         reportOOB(checkerContext, state_exceedsUpperBound, OOB_Tainted,
211*0b57cec5SDimitry Andric                   llvm::make_unique<TaintBugVisitor>(ByteOffset));
212*0b57cec5SDimitry Andric         return;
213*0b57cec5SDimitry Andric       }
214*0b57cec5SDimitry Andric     } else if (state_exceedsUpperBound) {
215*0b57cec5SDimitry Andric       // If we are constrained enough to definitely exceed the upper bound,
216*0b57cec5SDimitry Andric       // report.
217*0b57cec5SDimitry Andric       assert(!state_withinUpperBound);
218*0b57cec5SDimitry Andric       reportOOB(checkerContext, state_exceedsUpperBound, OOB_Excedes);
219*0b57cec5SDimitry Andric       return;
220*0b57cec5SDimitry Andric     }
221*0b57cec5SDimitry Andric 
222*0b57cec5SDimitry Andric     assert(state_withinUpperBound);
223*0b57cec5SDimitry Andric     state = state_withinUpperBound;
224*0b57cec5SDimitry Andric   }
225*0b57cec5SDimitry Andric   while (false);
226*0b57cec5SDimitry Andric 
227*0b57cec5SDimitry Andric   checkerContext.addTransition(state);
228*0b57cec5SDimitry Andric }
229*0b57cec5SDimitry Andric 
230*0b57cec5SDimitry Andric void ArrayBoundCheckerV2::reportOOB(
231*0b57cec5SDimitry Andric     CheckerContext &checkerContext, ProgramStateRef errorState, OOB_Kind kind,
232*0b57cec5SDimitry Andric     std::unique_ptr<BugReporterVisitor> Visitor) const {
233*0b57cec5SDimitry Andric 
234*0b57cec5SDimitry Andric   ExplodedNode *errorNode = checkerContext.generateErrorNode(errorState);
235*0b57cec5SDimitry Andric   if (!errorNode)
236*0b57cec5SDimitry Andric     return;
237*0b57cec5SDimitry Andric 
238*0b57cec5SDimitry Andric   if (!BT)
239*0b57cec5SDimitry Andric     BT.reset(new BuiltinBug(this, "Out-of-bound access"));
240*0b57cec5SDimitry Andric 
241*0b57cec5SDimitry Andric   // FIXME: This diagnostics are preliminary.  We should get far better
242*0b57cec5SDimitry Andric   // diagnostics for explaining buffer overruns.
243*0b57cec5SDimitry Andric 
244*0b57cec5SDimitry Andric   SmallString<256> buf;
245*0b57cec5SDimitry Andric   llvm::raw_svector_ostream os(buf);
246*0b57cec5SDimitry Andric   os << "Out of bound memory access ";
247*0b57cec5SDimitry Andric   switch (kind) {
248*0b57cec5SDimitry Andric   case OOB_Precedes:
249*0b57cec5SDimitry Andric     os << "(accessed memory precedes memory block)";
250*0b57cec5SDimitry Andric     break;
251*0b57cec5SDimitry Andric   case OOB_Excedes:
252*0b57cec5SDimitry Andric     os << "(access exceeds upper limit of memory block)";
253*0b57cec5SDimitry Andric     break;
254*0b57cec5SDimitry Andric   case OOB_Tainted:
255*0b57cec5SDimitry Andric     os << "(index is tainted)";
256*0b57cec5SDimitry Andric     break;
257*0b57cec5SDimitry Andric   }
258*0b57cec5SDimitry Andric 
259*0b57cec5SDimitry Andric   auto BR = llvm::make_unique<BugReport>(*BT, os.str(), errorNode);
260*0b57cec5SDimitry Andric   BR->addVisitor(std::move(Visitor));
261*0b57cec5SDimitry Andric   checkerContext.emitReport(std::move(BR));
262*0b57cec5SDimitry Andric }
263*0b57cec5SDimitry Andric 
264*0b57cec5SDimitry Andric #ifndef NDEBUG
265*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void RegionRawOffsetV2::dump() const {
266*0b57cec5SDimitry Andric   dumpToStream(llvm::errs());
267*0b57cec5SDimitry Andric }
268*0b57cec5SDimitry Andric 
269*0b57cec5SDimitry Andric void RegionRawOffsetV2::dumpToStream(raw_ostream &os) const {
270*0b57cec5SDimitry Andric   os << "raw_offset_v2{" << getRegion() << ',' << getByteOffset() << '}';
271*0b57cec5SDimitry Andric }
272*0b57cec5SDimitry Andric #endif
273*0b57cec5SDimitry Andric 
274*0b57cec5SDimitry Andric // Lazily computes a value to be used by 'computeOffset'.  If 'val'
275*0b57cec5SDimitry Andric // is unknown or undefined, we lazily substitute '0'.  Otherwise,
276*0b57cec5SDimitry Andric // return 'val'.
277*0b57cec5SDimitry Andric static inline SVal getValue(SVal val, SValBuilder &svalBuilder) {
278*0b57cec5SDimitry Andric   return val.getAs<UndefinedVal>() ? svalBuilder.makeArrayIndex(0) : val;
279*0b57cec5SDimitry Andric }
280*0b57cec5SDimitry Andric 
281*0b57cec5SDimitry Andric // Scale a base value by a scaling factor, and return the scaled
282*0b57cec5SDimitry Andric // value as an SVal.  Used by 'computeOffset'.
283*0b57cec5SDimitry Andric static inline SVal scaleValue(ProgramStateRef state,
284*0b57cec5SDimitry Andric                               NonLoc baseVal, CharUnits scaling,
285*0b57cec5SDimitry Andric                               SValBuilder &sb) {
286*0b57cec5SDimitry Andric   return sb.evalBinOpNN(state, BO_Mul, baseVal,
287*0b57cec5SDimitry Andric                         sb.makeArrayIndex(scaling.getQuantity()),
288*0b57cec5SDimitry Andric                         sb.getArrayIndexType());
289*0b57cec5SDimitry Andric }
290*0b57cec5SDimitry Andric 
291*0b57cec5SDimitry Andric // Add an SVal to another, treating unknown and undefined values as
292*0b57cec5SDimitry Andric // summing to UnknownVal.  Used by 'computeOffset'.
293*0b57cec5SDimitry Andric static SVal addValue(ProgramStateRef state, SVal x, SVal y,
294*0b57cec5SDimitry Andric                      SValBuilder &svalBuilder) {
295*0b57cec5SDimitry Andric   // We treat UnknownVals and UndefinedVals the same here because we
296*0b57cec5SDimitry Andric   // only care about computing offsets.
297*0b57cec5SDimitry Andric   if (x.isUnknownOrUndef() || y.isUnknownOrUndef())
298*0b57cec5SDimitry Andric     return UnknownVal();
299*0b57cec5SDimitry Andric 
300*0b57cec5SDimitry Andric   return svalBuilder.evalBinOpNN(state, BO_Add, x.castAs<NonLoc>(),
301*0b57cec5SDimitry Andric                                  y.castAs<NonLoc>(),
302*0b57cec5SDimitry Andric                                  svalBuilder.getArrayIndexType());
303*0b57cec5SDimitry Andric }
304*0b57cec5SDimitry Andric 
305*0b57cec5SDimitry Andric /// Compute a raw byte offset from a base region.  Used for array bounds
306*0b57cec5SDimitry Andric /// checking.
307*0b57cec5SDimitry Andric RegionRawOffsetV2 RegionRawOffsetV2::computeOffset(ProgramStateRef state,
308*0b57cec5SDimitry Andric                                                    SValBuilder &svalBuilder,
309*0b57cec5SDimitry Andric                                                    SVal location)
310*0b57cec5SDimitry Andric {
311*0b57cec5SDimitry Andric   const MemRegion *region = location.getAsRegion();
312*0b57cec5SDimitry Andric   SVal offset = UndefinedVal();
313*0b57cec5SDimitry Andric 
314*0b57cec5SDimitry Andric   while (region) {
315*0b57cec5SDimitry Andric     switch (region->getKind()) {
316*0b57cec5SDimitry Andric       default: {
317*0b57cec5SDimitry Andric         if (const SubRegion *subReg = dyn_cast<SubRegion>(region)) {
318*0b57cec5SDimitry Andric           offset = getValue(offset, svalBuilder);
319*0b57cec5SDimitry Andric           if (!offset.isUnknownOrUndef())
320*0b57cec5SDimitry Andric             return RegionRawOffsetV2(subReg, offset);
321*0b57cec5SDimitry Andric         }
322*0b57cec5SDimitry Andric         return RegionRawOffsetV2();
323*0b57cec5SDimitry Andric       }
324*0b57cec5SDimitry Andric       case MemRegion::ElementRegionKind: {
325*0b57cec5SDimitry Andric         const ElementRegion *elemReg = cast<ElementRegion>(region);
326*0b57cec5SDimitry Andric         SVal index = elemReg->getIndex();
327*0b57cec5SDimitry Andric         if (!index.getAs<NonLoc>())
328*0b57cec5SDimitry Andric           return RegionRawOffsetV2();
329*0b57cec5SDimitry Andric         QualType elemType = elemReg->getElementType();
330*0b57cec5SDimitry Andric         // If the element is an incomplete type, go no further.
331*0b57cec5SDimitry Andric         ASTContext &astContext = svalBuilder.getContext();
332*0b57cec5SDimitry Andric         if (elemType->isIncompleteType())
333*0b57cec5SDimitry Andric           return RegionRawOffsetV2();
334*0b57cec5SDimitry Andric 
335*0b57cec5SDimitry Andric         // Update the offset.
336*0b57cec5SDimitry Andric         offset = addValue(state,
337*0b57cec5SDimitry Andric                           getValue(offset, svalBuilder),
338*0b57cec5SDimitry Andric                           scaleValue(state,
339*0b57cec5SDimitry Andric                           index.castAs<NonLoc>(),
340*0b57cec5SDimitry Andric                           astContext.getTypeSizeInChars(elemType),
341*0b57cec5SDimitry Andric                           svalBuilder),
342*0b57cec5SDimitry Andric                           svalBuilder);
343*0b57cec5SDimitry Andric 
344*0b57cec5SDimitry Andric         if (offset.isUnknownOrUndef())
345*0b57cec5SDimitry Andric           return RegionRawOffsetV2();
346*0b57cec5SDimitry Andric 
347*0b57cec5SDimitry Andric         region = elemReg->getSuperRegion();
348*0b57cec5SDimitry Andric         continue;
349*0b57cec5SDimitry Andric       }
350*0b57cec5SDimitry Andric     }
351*0b57cec5SDimitry Andric   }
352*0b57cec5SDimitry Andric   return RegionRawOffsetV2();
353*0b57cec5SDimitry Andric }
354*0b57cec5SDimitry Andric 
355*0b57cec5SDimitry Andric void ento::registerArrayBoundCheckerV2(CheckerManager &mgr) {
356*0b57cec5SDimitry Andric   mgr.registerChecker<ArrayBoundCheckerV2>();
357*0b57cec5SDimitry Andric }
358*0b57cec5SDimitry Andric 
359*0b57cec5SDimitry Andric bool ento::shouldRegisterArrayBoundCheckerV2(const LangOptions &LO) {
360*0b57cec5SDimitry Andric   return true;
361*0b57cec5SDimitry Andric }
362