blob: c6efe941ec21b81038aa8722a9d2a856996cc98a [file] [log] [blame]
Ted Kremenekc478a142010-12-23 02:42:43 +00001//== ArrayBoundCheckerV2.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 ArrayBoundCheckerV2, which is a path-sensitive check
11// which looks for an out-of-bound array element access.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidis05357012011-02-28 01:26:57 +000015#include "ClangSACheckers.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000016#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis05357012011-02-28 01:26:57 +000017#include "clang/StaticAnalyzer/Core/CheckerManager.h"
18#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000019#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenekc478a142010-12-23 02:42:43 +000021#include "clang/AST/CharUnits.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000022#include "llvm/ADT/SmallString.h"
Benjamin Kramer00bd44d2012-02-04 12:31:12 +000023#include "llvm/ADT/STLExtras.h"
Ted Kremenekc478a142010-12-23 02:42:43 +000024
25using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000026using namespace ento;
Ted Kremenekc478a142010-12-23 02:42:43 +000027
28namespace {
29class ArrayBoundCheckerV2 :
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000030 public Checker<check::Location> {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000031 mutable OwningPtr<BuiltinBug> BT;
Ted Kremenekc478a142010-12-23 02:42:43 +000032
Anna Zaks3bfd6d72012-01-21 05:07:33 +000033 enum OOB_Kind { OOB_Precedes, OOB_Excedes, OOB_Tainted };
Ted Kremenekc478a142010-12-23 02:42:43 +000034
Ted Kremenek8bef8232012-01-26 21:29:00 +000035 void reportOOB(CheckerContext &C, ProgramStateRef errorState,
Argyrios Kyrtzidis05357012011-02-28 01:26:57 +000036 OOB_Kind kind) const;
Ted Kremenekc478a142010-12-23 02:42:43 +000037
38public:
Anna Zaks390909c2011-10-06 00:43:15 +000039 void checkLocation(SVal l, bool isLoad, const Stmt*S,
40 CheckerContext &C) const;
Ted Kremenekc478a142010-12-23 02:42:43 +000041};
42
43// FIXME: Eventually replace RegionRawOffset with this class.
44class RegionRawOffsetV2 {
45private:
46 const SubRegion *baseRegion;
47 SVal byteOffset;
48
49 RegionRawOffsetV2()
50 : baseRegion(0), byteOffset(UnknownVal()) {}
51
52public:
53 RegionRawOffsetV2(const SubRegion* base, SVal offset)
54 : baseRegion(base), byteOffset(offset) {}
55
56 NonLoc getByteOffset() const { return cast<NonLoc>(byteOffset); }
57 const SubRegion *getRegion() const { return baseRegion; }
58
Ted Kremenek8bef8232012-01-26 21:29:00 +000059 static RegionRawOffsetV2 computeOffset(ProgramStateRef state,
Ted Kremenekc478a142010-12-23 02:42:43 +000060 SValBuilder &svalBuilder,
61 SVal location);
62
63 void dump() const;
Ted Kremenek9c378f72011-08-12 23:37:29 +000064 void dumpToStream(raw_ostream &os) const;
Ted Kremenekc478a142010-12-23 02:42:43 +000065};
66}
67
Ted Kremenek82cfc682011-04-12 17:21:33 +000068static SVal computeExtentBegin(SValBuilder &svalBuilder,
69 const MemRegion *region) {
70 while (true)
71 switch (region->getKind()) {
72 default:
73 return svalBuilder.makeZeroArrayIndex();
74 case MemRegion::SymbolicRegionKind:
75 // FIXME: improve this later by tracking symbolic lower bounds
76 // for symbolic regions.
77 return UnknownVal();
78 case MemRegion::ElementRegionKind:
79 region = cast<SubRegion>(region)->getSuperRegion();
80 continue;
81 }
82}
83
Argyrios Kyrtzidis05357012011-02-28 01:26:57 +000084void ArrayBoundCheckerV2::checkLocation(SVal location, bool isLoad,
Anna Zaks390909c2011-10-06 00:43:15 +000085 const Stmt* LoadS,
Argyrios Kyrtzidis05357012011-02-28 01:26:57 +000086 CheckerContext &checkerContext) const {
Ted Kremenekc478a142010-12-23 02:42:43 +000087
Ted Kremenek18c66fd2011-08-15 22:09:50 +000088 // NOTE: Instead of using ProgramState::assumeInBound(), we are prototyping
Ted Kremenekc478a142010-12-23 02:42:43 +000089 // some new logic here that reasons directly about memory region extents.
90 // Once that logic is more mature, we can bring it back to assumeInBound()
91 // for all clients to use.
92 //
93 // The algorithm we are using here for bounds checking is to see if the
94 // memory access is within the extent of the base region. Since we
95 // have some flexibility in defining the base region, we can achieve
96 // various levels of conservatism in our buffer overflow checking.
Ted Kremenek8bef8232012-01-26 21:29:00 +000097 ProgramStateRef state = checkerContext.getState();
98 ProgramStateRef originalState = state;
Ted Kremenekc478a142010-12-23 02:42:43 +000099
100 SValBuilder &svalBuilder = checkerContext.getSValBuilder();
101 const RegionRawOffsetV2 &rawOffset =
102 RegionRawOffsetV2::computeOffset(state, svalBuilder, location);
103
104 if (!rawOffset.getRegion())
105 return;
106
Ted Kremenek82cfc682011-04-12 17:21:33 +0000107 // CHECK LOWER BOUND: Is byteOffset < extent begin?
108 // If so, we are doing a load/store
Ted Kremenekc478a142010-12-23 02:42:43 +0000109 // before the first valid offset in the memory region.
110
Ted Kremenek82cfc682011-04-12 17:21:33 +0000111 SVal extentBegin = computeExtentBegin(svalBuilder, rawOffset.getRegion());
112
113 if (isa<NonLoc>(extentBegin)) {
114 SVal lowerBound
115 = svalBuilder.evalBinOpNN(state, BO_LT, rawOffset.getByteOffset(),
116 cast<NonLoc>(extentBegin),
117 svalBuilder.getConditionType());
Ted Kremenekc478a142010-12-23 02:42:43 +0000118
Ted Kremenek82cfc682011-04-12 17:21:33 +0000119 NonLoc *lowerBoundToCheck = dyn_cast<NonLoc>(&lowerBound);
120 if (!lowerBoundToCheck)
121 return;
Ted Kremenekc478a142010-12-23 02:42:43 +0000122
Ted Kremenek8bef8232012-01-26 21:29:00 +0000123 ProgramStateRef state_precedesLowerBound, state_withinLowerBound;
Ted Kremenek82cfc682011-04-12 17:21:33 +0000124 llvm::tie(state_precedesLowerBound, state_withinLowerBound) =
Ted Kremenekc478a142010-12-23 02:42:43 +0000125 state->assume(*lowerBoundToCheck);
126
Ted Kremenek82cfc682011-04-12 17:21:33 +0000127 // Are we constrained enough to definitely precede the lower bound?
128 if (state_precedesLowerBound && !state_withinLowerBound) {
129 reportOOB(checkerContext, state_precedesLowerBound, OOB_Precedes);
130 return;
131 }
Ted Kremenekc478a142010-12-23 02:42:43 +0000132
Ted Kremenek82cfc682011-04-12 17:21:33 +0000133 // Otherwise, assume the constraint of the lower bound.
134 assert(state_withinLowerBound);
135 state = state_withinLowerBound;
136 }
Ted Kremenekc478a142010-12-23 02:42:43 +0000137
138 do {
139 // CHECK UPPER BOUND: Is byteOffset >= extent(baseRegion)? If so,
140 // we are doing a load/store after the last valid offset.
141 DefinedOrUnknownSVal extentVal =
142 rawOffset.getRegion()->getExtent(svalBuilder);
143 if (!isa<NonLoc>(extentVal))
144 break;
145
146 SVal upperbound
147 = svalBuilder.evalBinOpNN(state, BO_GE, rawOffset.getByteOffset(),
148 cast<NonLoc>(extentVal),
149 svalBuilder.getConditionType());
150
151 NonLoc *upperboundToCheck = dyn_cast<NonLoc>(&upperbound);
152 if (!upperboundToCheck)
153 break;
154
Ted Kremenek8bef8232012-01-26 21:29:00 +0000155 ProgramStateRef state_exceedsUpperBound, state_withinUpperBound;
Ted Kremenekc478a142010-12-23 02:42:43 +0000156 llvm::tie(state_exceedsUpperBound, state_withinUpperBound) =
157 state->assume(*upperboundToCheck);
Anna Zaks9b0970f2011-11-16 19:58:17 +0000158
159 // If we are under constrained and the index variables are tainted, report.
160 if (state_exceedsUpperBound && state_withinUpperBound) {
161 if (state->isTainted(rawOffset.getByteOffset()))
Anna Zaks3bfd6d72012-01-21 05:07:33 +0000162 reportOOB(checkerContext, state_exceedsUpperBound, OOB_Tainted);
Anna Zaks9b0970f2011-11-16 19:58:17 +0000163 return;
164 }
Ted Kremenekc478a142010-12-23 02:42:43 +0000165
Anna Zaks9b0970f2011-11-16 19:58:17 +0000166 // If we are constrained enough to definitely exceed the upper bound, report.
167 if (state_exceedsUpperBound) {
168 assert(!state_withinUpperBound);
Ted Kremenekc478a142010-12-23 02:42:43 +0000169 reportOOB(checkerContext, state_exceedsUpperBound, OOB_Excedes);
170 return;
171 }
172
173 assert(state_withinUpperBound);
174 state = state_withinUpperBound;
175 }
176 while (false);
177
178 if (state != originalState)
Anna Zaks0bd6b112011-10-26 21:06:34 +0000179 checkerContext.addTransition(state);
Ted Kremenekc478a142010-12-23 02:42:43 +0000180}
181
182void ArrayBoundCheckerV2::reportOOB(CheckerContext &checkerContext,
Ted Kremenek8bef8232012-01-26 21:29:00 +0000183 ProgramStateRef errorState,
Argyrios Kyrtzidis05357012011-02-28 01:26:57 +0000184 OOB_Kind kind) const {
Ted Kremenekc478a142010-12-23 02:42:43 +0000185
186 ExplodedNode *errorNode = checkerContext.generateSink(errorState);
187 if (!errorNode)
188 return;
189
190 if (!BT)
Argyrios Kyrtzidis05357012011-02-28 01:26:57 +0000191 BT.reset(new BuiltinBug("Out-of-bound access"));
Ted Kremenekc478a142010-12-23 02:42:43 +0000192
193 // FIXME: This diagnostics are preliminary. We should get far better
194 // diagnostics for explaining buffer overruns.
195
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000196 SmallString<256> buf;
Ted Kremenekc478a142010-12-23 02:42:43 +0000197 llvm::raw_svector_ostream os(buf);
Anna Zaks3bfd6d72012-01-21 05:07:33 +0000198 os << "Out of bound memory access ";
199 switch (kind) {
200 case OOB_Precedes:
201 os << "(accessed memory precedes memory block)";
202 break;
203 case OOB_Excedes:
204 os << "(access exceeds upper limit of memory block)";
205 break;
206 case OOB_Tainted:
207 os << "(index is tainted)";
208 break;
209 }
Ted Kremenekc478a142010-12-23 02:42:43 +0000210
Anna Zakse172e8b2011-08-17 23:00:25 +0000211 checkerContext.EmitReport(new BugReport(*BT, os.str(), errorNode));
Ted Kremenekc478a142010-12-23 02:42:43 +0000212}
213
214void RegionRawOffsetV2::dump() const {
215 dumpToStream(llvm::errs());
216}
217
Ted Kremenek9c378f72011-08-12 23:37:29 +0000218void RegionRawOffsetV2::dumpToStream(raw_ostream &os) const {
Ted Kremenekc478a142010-12-23 02:42:43 +0000219 os << "raw_offset_v2{" << getRegion() << ',' << getByteOffset() << '}';
220}
221
222// FIXME: Merge with the implementation of the same method in Store.cpp
223static bool IsCompleteType(ASTContext &Ctx, QualType Ty) {
224 if (const RecordType *RT = Ty->getAs<RecordType>()) {
225 const RecordDecl *D = RT->getDecl();
226 if (!D->getDefinition())
227 return false;
228 }
229
230 return true;
231}
232
233
234// Lazily computes a value to be used by 'computeOffset'. If 'val'
235// is unknown or undefined, we lazily substitute '0'. Otherwise,
236// return 'val'.
237static inline SVal getValue(SVal val, SValBuilder &svalBuilder) {
238 return isa<UndefinedVal>(val) ? svalBuilder.makeArrayIndex(0) : val;
239}
240
241// Scale a base value by a scaling factor, and return the scaled
242// value as an SVal. Used by 'computeOffset'.
Ted Kremenek8bef8232012-01-26 21:29:00 +0000243static inline SVal scaleValue(ProgramStateRef state,
Ted Kremenekc478a142010-12-23 02:42:43 +0000244 NonLoc baseVal, CharUnits scaling,
245 SValBuilder &sb) {
246 return sb.evalBinOpNN(state, BO_Mul, baseVal,
247 sb.makeArrayIndex(scaling.getQuantity()),
248 sb.getArrayIndexType());
249}
250
251// Add an SVal to another, treating unknown and undefined values as
252// summing to UnknownVal. Used by 'computeOffset'.
Ted Kremenek8bef8232012-01-26 21:29:00 +0000253static SVal addValue(ProgramStateRef state, SVal x, SVal y,
Ted Kremenekc478a142010-12-23 02:42:43 +0000254 SValBuilder &svalBuilder) {
255 // We treat UnknownVals and UndefinedVals the same here because we
256 // only care about computing offsets.
257 if (x.isUnknownOrUndef() || y.isUnknownOrUndef())
258 return UnknownVal();
259
260 return svalBuilder.evalBinOpNN(state, BO_Add,
261 cast<NonLoc>(x), cast<NonLoc>(y),
262 svalBuilder.getArrayIndexType());
263}
264
265/// Compute a raw byte offset from a base region. Used for array bounds
266/// checking.
Ted Kremenek8bef8232012-01-26 21:29:00 +0000267RegionRawOffsetV2 RegionRawOffsetV2::computeOffset(ProgramStateRef state,
Ted Kremenekc478a142010-12-23 02:42:43 +0000268 SValBuilder &svalBuilder,
269 SVal location)
270{
271 const MemRegion *region = location.getAsRegion();
272 SVal offset = UndefinedVal();
273
274 while (region) {
275 switch (region->getKind()) {
276 default: {
Ted Kremenek82cfc682011-04-12 17:21:33 +0000277 if (const SubRegion *subReg = dyn_cast<SubRegion>(region)) {
278 offset = getValue(offset, svalBuilder);
Ted Kremenekc478a142010-12-23 02:42:43 +0000279 if (!offset.isUnknownOrUndef())
280 return RegionRawOffsetV2(subReg, offset);
Ted Kremenek82cfc682011-04-12 17:21:33 +0000281 }
Ted Kremenekc478a142010-12-23 02:42:43 +0000282 return RegionRawOffsetV2();
283 }
284 case MemRegion::ElementRegionKind: {
285 const ElementRegion *elemReg = cast<ElementRegion>(region);
286 SVal index = elemReg->getIndex();
287 if (!isa<NonLoc>(index))
288 return RegionRawOffsetV2();
289 QualType elemType = elemReg->getElementType();
290 // If the element is an incomplete type, go no further.
291 ASTContext &astContext = svalBuilder.getContext();
292 if (!IsCompleteType(astContext, elemType))
293 return RegionRawOffsetV2();
294
295 // Update the offset.
296 offset = addValue(state,
297 getValue(offset, svalBuilder),
298 scaleValue(state,
Anna Zaks9b0970f2011-11-16 19:58:17 +0000299 cast<NonLoc>(index),
300 astContext.getTypeSizeInChars(elemType),
301 svalBuilder),
Ted Kremenekc478a142010-12-23 02:42:43 +0000302 svalBuilder);
303
304 if (offset.isUnknownOrUndef())
305 return RegionRawOffsetV2();
306
307 region = elemReg->getSuperRegion();
308 continue;
309 }
310 }
311 }
312 return RegionRawOffsetV2();
313}
314
315
Argyrios Kyrtzidis05357012011-02-28 01:26:57 +0000316void ento::registerArrayBoundCheckerV2(CheckerManager &mgr) {
317 mgr.registerChecker<ArrayBoundCheckerV2>();
318}