23 #include "llvm/ADT/SmallString.h" 24 #include "llvm/Support/raw_ostream.h" 26 using namespace clang;
30 class ArrayBoundCheckerV2 :
31 public Checker<check::Location> {
32 mutable std::unique_ptr<BuiltinBug> BT;
34 enum OOB_Kind { OOB_Precedes, OOB_Excedes, OOB_Tainted };
37 std::unique_ptr<BugReporterVisitor> Visitor =
nullptr)
const;
40 void checkLocation(SVal l,
bool isLoad,
const Stmt*S,
41 CheckerContext &C)
const;
45 class RegionRawOffsetV2 {
47 const SubRegion *baseRegion;
51 : baseRegion(nullptr), byteOffset(UnknownVal()) {}
54 RegionRawOffsetV2(
const SubRegion* base, SVal offset)
55 : baseRegion(base), byteOffset(offset) {}
57 NonLoc getByteOffset()
const {
return byteOffset.castAs<NonLoc>(); }
58 const SubRegion *getRegion()
const {
return baseRegion; }
61 SValBuilder &svalBuilder,
65 void dumpToStream(raw_ostream &os)
const;
70 const MemRegion *region) {
71 const MemSpaceRegion *SR = region->getMemorySpace();
72 if (SR->getKind() == MemRegion::UnknownSpaceRegionKind)
75 return svalBuilder.makeZeroArrayIndex();
82 static std::pair<NonLoc, nonloc::ConcreteInt>
84 SValBuilder &svalBuilder) {
86 if (SymVal && SymVal->isExpression()) {
87 if (
const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SymVal->getSymbol())) {
88 llvm::APSInt constant =
89 APSIntType(extent.getValue()).convert(SIE->getRHS());
90 switch (SIE->getOpcode()) {
94 if ((extent.getValue() % constant) != 0)
95 return std::pair<NonLoc, nonloc::ConcreteInt>(offset, extent);
98 nonloc::SymbolVal(SIE->getLHS()),
99 svalBuilder.makeIntVal(extent.getValue() / constant),
103 nonloc::SymbolVal(SIE->getLHS()),
104 svalBuilder.makeIntVal(extent.getValue() - constant), svalBuilder);
111 return std::pair<NonLoc, nonloc::ConcreteInt>(offset, extent);
114 void ArrayBoundCheckerV2::checkLocation(SVal location,
bool isLoad,
116 CheckerContext &checkerContext)
const {
129 SValBuilder &svalBuilder = checkerContext.getSValBuilder();
130 const RegionRawOffsetV2 &rawOffset =
131 RegionRawOffsetV2::computeOffset(state, svalBuilder, location);
133 if (!rawOffset.getRegion())
136 NonLoc rawOffsetVal = rawOffset.getByteOffset();
145 if (NV->getAs<nonloc::ConcreteInt>()) {
146 std::pair<NonLoc, nonloc::ConcreteInt> simplifiedOffsets =
148 NV->castAs<nonloc::ConcreteInt>(),
150 rawOffsetVal = simplifiedOffsets.first;
151 *NV = simplifiedOffsets.second;
154 SVal lowerBound = svalBuilder.evalBinOpNN(state, BO_LT, rawOffsetVal, *NV,
155 svalBuilder.getConditionType());
158 if (!lowerBoundToCheck)
162 std::tie(state_precedesLowerBound, state_withinLowerBound) =
163 state->assume(*lowerBoundToCheck);
166 if (state_precedesLowerBound && !state_withinLowerBound) {
167 reportOOB(checkerContext, state_precedesLowerBound, OOB_Precedes);
172 assert(state_withinLowerBound);
173 state = state_withinLowerBound;
179 DefinedOrUnknownSVal extentVal =
180 rawOffset.getRegion()->getExtent(svalBuilder);
181 if (!extentVal.getAs<NonLoc>())
184 if (extentVal.getAs<nonloc::ConcreteInt>()) {
185 std::pair<NonLoc, nonloc::ConcreteInt> simplifiedOffsets =
187 extentVal.castAs<nonloc::ConcreteInt>(),
189 rawOffsetVal = simplifiedOffsets.first;
190 extentVal = simplifiedOffsets.second;
193 SVal upperbound = svalBuilder.evalBinOpNN(state, BO_GE, rawOffsetVal,
194 extentVal.castAs<NonLoc>(),
195 svalBuilder.getConditionType());
198 if (!upperboundToCheck)
202 std::tie(state_exceedsUpperBound, state_withinUpperBound) =
203 state->assume(*upperboundToCheck);
206 if (state_exceedsUpperBound && state_withinUpperBound) {
207 SVal ByteOffset = rawOffset.getByteOffset();
208 if (state->isTainted(ByteOffset)) {
209 reportOOB(checkerContext, state_exceedsUpperBound, OOB_Tainted,
210 llvm::make_unique<TaintBugVisitor>(ByteOffset));
213 }
else if (state_exceedsUpperBound) {
216 assert(!state_withinUpperBound);
217 reportOOB(checkerContext, state_exceedsUpperBound, OOB_Excedes);
221 assert(state_withinUpperBound);
222 state = state_withinUpperBound;
226 checkerContext.addTransition(state);
229 void ArrayBoundCheckerV2::reportOOB(
231 std::unique_ptr<BugReporterVisitor> Visitor)
const {
233 ExplodedNode *errorNode = checkerContext.generateErrorNode(errorState);
238 BT.reset(
new BuiltinBug(
this,
"Out-of-bound access"));
244 llvm::raw_svector_ostream os(buf);
245 os <<
"Out of bound memory access ";
248 os <<
"(accessed memory precedes memory block)";
251 os <<
"(access exceeds upper limit of memory block)";
254 os <<
"(index is tainted)";
258 auto BR = llvm::make_unique<BugReport>(*BT, os.str(), errorNode);
259 BR->addVisitor(std::move(Visitor));
260 checkerContext.emitReport(std::move(BR));
265 dumpToStream(llvm::errs());
268 void RegionRawOffsetV2::dumpToStream(raw_ostream &os)
const {
269 os <<
"raw_offset_v2{" << getRegion() <<
',' << getByteOffset() <<
'}';
276 static inline SVal
getValue(SVal val, SValBuilder &svalBuilder) {
277 return val.getAs<UndefinedVal>() ? svalBuilder.makeArrayIndex(0) : val;
285 return sb.evalBinOpNN(state, BO_Mul, baseVal,
287 sb.getArrayIndexType());
293 SValBuilder &svalBuilder) {
296 if (x.isUnknownOrUndef() || y.isUnknownOrUndef())
299 return svalBuilder.evalBinOpNN(state, BO_Add, x.castAs<NonLoc>(),
301 svalBuilder.getArrayIndexType());
307 SValBuilder &svalBuilder,
310 const MemRegion *region = location.getAsRegion();
311 SVal offset = UndefinedVal();
314 switch (region->getKind()) {
316 if (
const SubRegion *subReg = dyn_cast<SubRegion>(region)) {
317 offset =
getValue(offset, svalBuilder);
318 if (!offset.isUnknownOrUndef())
319 return RegionRawOffsetV2(subReg, offset);
321 return RegionRawOffsetV2();
323 case MemRegion::ElementRegionKind: {
324 const ElementRegion *elemReg = cast<ElementRegion>(region);
325 SVal index = elemReg->getIndex();
326 if (!index.getAs<NonLoc>())
327 return RegionRawOffsetV2();
328 QualType elemType = elemReg->getElementType();
330 ASTContext &astContext = svalBuilder.getContext();
332 return RegionRawOffsetV2();
338 index.castAs<NonLoc>(),
343 if (offset.isUnknownOrUndef())
344 return RegionRawOffsetV2();
346 region = elemReg->getSuperRegion();
351 return RegionRawOffsetV2();
354 void ento::registerArrayBoundCheckerV2(CheckerManager &mgr) {
355 mgr.registerChecker<ArrayBoundCheckerV2>();
A (possibly-)qualified type.
static SVal addValue(ProgramStateRef state, SVal x, SVal y, SValBuilder &svalBuilder)
Stmt - This represents one statement.
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
i32 captured_struct **param SharedsTy A type which contains references the shared variables *param Shareds Context with the list of shared variables from the p *TaskFunction *param Data Additional data for task generation like final * state
CharUnits - This is an opaque type for sizes expressed in character units.
static void dump(llvm::raw_ostream &OS, StringRef FunctionName, ArrayRef< CounterExpression > Expressions, ArrayRef< CounterMappingRegion > Regions)
static SVal computeExtentBegin(SValBuilder &svalBuilder, const MemRegion *region)
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
static SVal getValue(SVal val, SValBuilder &svalBuilder)
static SVal scaleValue(ProgramStateRef state, NonLoc baseVal, CharUnits scaling, SValBuilder &sb)
Dataflow Directional Tag Classes.
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types...
unsigned kind
All of the diagnostics that can be emitted by the frontend.
static std::pair< NonLoc, nonloc::ConcreteInt > getSimplifiedOffsets(NonLoc offset, nonloc::ConcreteInt extent, SValBuilder &svalBuilder)
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.