1 //== ArrayBoundCheckerV2.cpp ------------------------------------*- C++ -*--==//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines ArrayBoundCheckerV2, which is a path-sensitive check
10 // which looks for an out-of-bound array element access.
12 //===----------------------------------------------------------------------===//
15 #include "clang/AST/CharUnits.h"
16 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
17 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
18 #include "clang/StaticAnalyzer/Core/Checker.h"
19 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicSize.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/Support/raw_ostream.h"
27 using namespace clang;
29 using namespace taint;
32 class ArrayBoundCheckerV2 :
33 public Checker<check::Location> {
34 mutable std::unique_ptr<BuiltinBug> BT;
36 enum OOB_Kind { OOB_Precedes, OOB_Excedes, OOB_Tainted };
38 void reportOOB(CheckerContext &C, ProgramStateRef errorState, OOB_Kind kind,
39 std::unique_ptr<BugReporterVisitor> Visitor = nullptr) const;
42 void checkLocation(SVal l, bool isLoad, const Stmt*S,
43 CheckerContext &C) const;
46 // FIXME: Eventually replace RegionRawOffset with this class.
47 class RegionRawOffsetV2 {
49 const SubRegion *baseRegion;
53 : baseRegion(nullptr), byteOffset(UnknownVal()) {}
56 RegionRawOffsetV2(const SubRegion* base, SVal offset)
57 : baseRegion(base), byteOffset(offset) {}
59 NonLoc getByteOffset() const { return byteOffset.castAs<NonLoc>(); }
60 const SubRegion *getRegion() const { return baseRegion; }
62 static RegionRawOffsetV2 computeOffset(ProgramStateRef state,
63 SValBuilder &svalBuilder,
67 void dumpToStream(raw_ostream &os) const;
71 static SVal computeExtentBegin(SValBuilder &svalBuilder,
72 const MemRegion *region) {
73 const MemSpaceRegion *SR = region->getMemorySpace();
74 if (SR->getKind() == MemRegion::UnknownSpaceRegionKind)
77 return svalBuilder.makeZeroArrayIndex();
80 // TODO: once the constraint manager is smart enough to handle non simplified
81 // symbolic expressions remove this function. Note that this can not be used in
82 // the constraint manager as is, since this does not handle overflows. It is
83 // safe to assume, however, that memory offsets will not overflow.
84 static std::pair<NonLoc, nonloc::ConcreteInt>
85 getSimplifiedOffsets(NonLoc offset, nonloc::ConcreteInt extent,
86 SValBuilder &svalBuilder) {
87 Optional<nonloc::SymbolVal> SymVal = offset.getAs<nonloc::SymbolVal>();
88 if (SymVal && SymVal->isExpression()) {
89 if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SymVal->getSymbol())) {
90 llvm::APSInt constant =
91 APSIntType(extent.getValue()).convert(SIE->getRHS());
92 switch (SIE->getOpcode()) {
94 // The constant should never be 0 here, since it the result of scaling
95 // based on the size of a type which is never 0.
96 if ((extent.getValue() % constant) != 0)
97 return std::pair<NonLoc, nonloc::ConcreteInt>(offset, extent);
99 return getSimplifiedOffsets(
100 nonloc::SymbolVal(SIE->getLHS()),
101 svalBuilder.makeIntVal(extent.getValue() / constant),
104 return getSimplifiedOffsets(
105 nonloc::SymbolVal(SIE->getLHS()),
106 svalBuilder.makeIntVal(extent.getValue() - constant), svalBuilder);
113 return std::pair<NonLoc, nonloc::ConcreteInt>(offset, extent);
116 void ArrayBoundCheckerV2::checkLocation(SVal location, bool isLoad,
118 CheckerContext &checkerContext) const {
120 // NOTE: Instead of using ProgramState::assumeInBound(), we are prototyping
121 // some new logic here that reasons directly about memory region extents.
122 // Once that logic is more mature, we can bring it back to assumeInBound()
123 // for all clients to use.
125 // The algorithm we are using here for bounds checking is to see if the
126 // memory access is within the extent of the base region. Since we
127 // have some flexibility in defining the base region, we can achieve
128 // various levels of conservatism in our buffer overflow checking.
129 ProgramStateRef state = checkerContext.getState();
131 SValBuilder &svalBuilder = checkerContext.getSValBuilder();
132 const RegionRawOffsetV2 &rawOffset =
133 RegionRawOffsetV2::computeOffset(state, svalBuilder, location);
135 if (!rawOffset.getRegion())
138 NonLoc rawOffsetVal = rawOffset.getByteOffset();
140 // CHECK LOWER BOUND: Is byteOffset < extent begin?
141 // If so, we are doing a load/store
142 // before the first valid offset in the memory region.
144 SVal extentBegin = computeExtentBegin(svalBuilder, rawOffset.getRegion());
146 if (Optional<NonLoc> NV = extentBegin.getAs<NonLoc>()) {
147 if (NV->getAs<nonloc::ConcreteInt>()) {
148 std::pair<NonLoc, nonloc::ConcreteInt> simplifiedOffsets =
149 getSimplifiedOffsets(rawOffset.getByteOffset(),
150 NV->castAs<nonloc::ConcreteInt>(),
152 rawOffsetVal = simplifiedOffsets.first;
153 *NV = simplifiedOffsets.second;
156 SVal lowerBound = svalBuilder.evalBinOpNN(state, BO_LT, rawOffsetVal, *NV,
157 svalBuilder.getConditionType());
159 Optional<NonLoc> lowerBoundToCheck = lowerBound.getAs<NonLoc>();
160 if (!lowerBoundToCheck)
163 ProgramStateRef state_precedesLowerBound, state_withinLowerBound;
164 std::tie(state_precedesLowerBound, state_withinLowerBound) =
165 state->assume(*lowerBoundToCheck);
167 // Are we constrained enough to definitely precede the lower bound?
168 if (state_precedesLowerBound && !state_withinLowerBound) {
169 reportOOB(checkerContext, state_precedesLowerBound, OOB_Precedes);
173 // Otherwise, assume the constraint of the lower bound.
174 assert(state_withinLowerBound);
175 state = state_withinLowerBound;
179 // CHECK UPPER BOUND: Is byteOffset >= size(baseRegion)? If so,
180 // we are doing a load/store after the last valid offset.
181 const MemRegion *MR = rawOffset.getRegion();
182 DefinedOrUnknownSVal Size = getDynamicSize(state, MR, svalBuilder);
183 if (!Size.getAs<NonLoc>())
186 if (Size.getAs<nonloc::ConcreteInt>()) {
187 std::pair<NonLoc, nonloc::ConcreteInt> simplifiedOffsets =
188 getSimplifiedOffsets(rawOffset.getByteOffset(),
189 Size.castAs<nonloc::ConcreteInt>(), svalBuilder);
190 rawOffsetVal = simplifiedOffsets.first;
191 Size = simplifiedOffsets.second;
194 SVal upperbound = svalBuilder.evalBinOpNN(state, BO_GE, rawOffsetVal,
195 Size.castAs<NonLoc>(),
196 svalBuilder.getConditionType());
198 Optional<NonLoc> upperboundToCheck = upperbound.getAs<NonLoc>();
199 if (!upperboundToCheck)
202 ProgramStateRef state_exceedsUpperBound, state_withinUpperBound;
203 std::tie(state_exceedsUpperBound, state_withinUpperBound) =
204 state->assume(*upperboundToCheck);
206 // If we are under constrained and the index variables are tainted, report.
207 if (state_exceedsUpperBound && state_withinUpperBound) {
208 SVal ByteOffset = rawOffset.getByteOffset();
209 if (isTainted(state, ByteOffset)) {
210 reportOOB(checkerContext, state_exceedsUpperBound, OOB_Tainted,
211 std::make_unique<TaintBugVisitor>(ByteOffset));
214 } else if (state_exceedsUpperBound) {
215 // If we are constrained enough to definitely exceed the upper bound,
217 assert(!state_withinUpperBound);
218 reportOOB(checkerContext, state_exceedsUpperBound, OOB_Excedes);
222 assert(state_withinUpperBound);
223 state = state_withinUpperBound;
227 checkerContext.addTransition(state);
230 void ArrayBoundCheckerV2::reportOOB(
231 CheckerContext &checkerContext, ProgramStateRef errorState, OOB_Kind kind,
232 std::unique_ptr<BugReporterVisitor> Visitor) const {
234 ExplodedNode *errorNode = checkerContext.generateErrorNode(errorState);
239 BT.reset(new BuiltinBug(this, "Out-of-bound access"));
241 // FIXME: This diagnostics are preliminary. We should get far better
242 // diagnostics for explaining buffer overruns.
244 SmallString<256> buf;
245 llvm::raw_svector_ostream os(buf);
246 os << "Out of bound memory access ";
249 os << "(accessed memory precedes memory block)";
252 os << "(access exceeds upper limit of memory block)";
255 os << "(index is tainted)";
259 auto BR = std::make_unique<PathSensitiveBugReport>(*BT, os.str(), errorNode);
260 BR->addVisitor(std::move(Visitor));
261 checkerContext.emitReport(std::move(BR));
265 LLVM_DUMP_METHOD void RegionRawOffsetV2::dump() const {
266 dumpToStream(llvm::errs());
269 void RegionRawOffsetV2::dumpToStream(raw_ostream &os) const {
270 os << "raw_offset_v2{" << getRegion() << ',' << getByteOffset() << '}';
274 // Lazily computes a value to be used by 'computeOffset'. If 'val'
275 // is unknown or undefined, we lazily substitute '0'. Otherwise,
277 static inline SVal getValue(SVal val, SValBuilder &svalBuilder) {
278 return val.getAs<UndefinedVal>() ? svalBuilder.makeArrayIndex(0) : val;
281 // Scale a base value by a scaling factor, and return the scaled
282 // value as an SVal. Used by 'computeOffset'.
283 static inline SVal scaleValue(ProgramStateRef state,
284 NonLoc baseVal, CharUnits scaling,
286 return sb.evalBinOpNN(state, BO_Mul, baseVal,
287 sb.makeArrayIndex(scaling.getQuantity()),
288 sb.getArrayIndexType());
291 // Add an SVal to another, treating unknown and undefined values as
292 // summing to UnknownVal. Used by 'computeOffset'.
293 static SVal addValue(ProgramStateRef state, SVal x, SVal y,
294 SValBuilder &svalBuilder) {
295 // We treat UnknownVals and UndefinedVals the same here because we
296 // only care about computing offsets.
297 if (x.isUnknownOrUndef() || y.isUnknownOrUndef())
300 return svalBuilder.evalBinOpNN(state, BO_Add, x.castAs<NonLoc>(),
302 svalBuilder.getArrayIndexType());
305 /// Compute a raw byte offset from a base region. Used for array bounds
307 RegionRawOffsetV2 RegionRawOffsetV2::computeOffset(ProgramStateRef state,
308 SValBuilder &svalBuilder,
311 const MemRegion *region = location.getAsRegion();
312 SVal offset = UndefinedVal();
315 switch (region->getKind()) {
317 if (const SubRegion *subReg = dyn_cast<SubRegion>(region)) {
318 offset = getValue(offset, svalBuilder);
319 if (!offset.isUnknownOrUndef())
320 return RegionRawOffsetV2(subReg, offset);
322 return RegionRawOffsetV2();
324 case MemRegion::ElementRegionKind: {
325 const ElementRegion *elemReg = cast<ElementRegion>(region);
326 SVal index = elemReg->getIndex();
327 if (!index.getAs<NonLoc>())
328 return RegionRawOffsetV2();
329 QualType elemType = elemReg->getElementType();
330 // If the element is an incomplete type, go no further.
331 ASTContext &astContext = svalBuilder.getContext();
332 if (elemType->isIncompleteType())
333 return RegionRawOffsetV2();
335 // Update the offset.
336 offset = addValue(state,
337 getValue(offset, svalBuilder),
339 index.castAs<NonLoc>(),
340 astContext.getTypeSizeInChars(elemType),
344 if (offset.isUnknownOrUndef())
345 return RegionRawOffsetV2();
347 region = elemReg->getSuperRegion();
352 return RegionRawOffsetV2();
355 void ento::registerArrayBoundCheckerV2(CheckerManager &mgr) {
356 mgr.registerChecker<ArrayBoundCheckerV2>();
359 bool ento::shouldRegisterArrayBoundCheckerV2(const CheckerManager &mgr) {