blob: ccbc9343f36dead0cfc53de6b054411114e5a22e [file] [log] [blame]
Jordy Rose134a2362010-07-06 23:11:01 +00001//= CStringChecker.h - Checks calls to C string functions ----------*- 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 defines CStringChecker, which is an assortment of checks on calls
11// to functions in <string.h>.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidis2d3905f2011-02-15 21:25:03 +000015#include "ClangSACheckers.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000016#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000017#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +000018#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000019#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/GRStateTrait.h"
Jordy Rose134a2362010-07-06 23:11:01 +000021#include "llvm/ADT/StringSwitch.h"
22
23using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000024using namespace ento;
Jordy Rose134a2362010-07-06 23:11:01 +000025
26namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000027class CStringChecker : public Checker< eval::Call,
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +000028 check::PreStmt<DeclStmt>,
29 check::LiveSymbols,
30 check::DeadSymbols,
31 check::RegionChanges
32 > {
33 mutable llvm::OwningPtr<BugType> BT_Null, BT_Bounds, BT_BoundsWrite,
34 BT_Overlap, BT_NotCString;
Jordy Rose134a2362010-07-06 23:11:01 +000035public:
Jordy Rose134a2362010-07-06 23:11:01 +000036 static void *getTag() { static int tag; return &tag; }
37
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +000038 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
39 void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const;
40 void checkLiveSymbols(const GRState *state, SymbolReaper &SR) const;
41 void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
42 bool wantsRegionChangeUpdate(const GRState *state) const;
Jordy Rose2a2e21c2010-08-14 21:02:52 +000043
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +000044 const GRState *checkRegionChanges(const GRState *state,
45 const MemRegion * const *Begin,
46 const MemRegion * const *End) const;
Jordy Rose134a2362010-07-06 23:11:01 +000047
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +000048 typedef void (CStringChecker::*FnCheck)(CheckerContext &,
49 const CallExpr *) const;
Jordy Rose134a2362010-07-06 23:11:01 +000050
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +000051 void evalMemcpy(CheckerContext &C, const CallExpr *CE) const;
Lenny Maiorani79d74142011-03-31 21:36:53 +000052 void evalMempcpy(CheckerContext &C, const CallExpr *CE) const;
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +000053 void evalMemmove(CheckerContext &C, const CallExpr *CE) const;
54 void evalBcopy(CheckerContext &C, const CallExpr *CE) const;
Lenny Maiorani79d74142011-03-31 21:36:53 +000055 void evalCopyCommon(CheckerContext &C, const CallExpr *CE,
56 const GRState *state,
Jordy Rosed5d2e502010-07-08 23:57:29 +000057 const Expr *Size, const Expr *Source, const Expr *Dest,
Lenny Maiorani79d74142011-03-31 21:36:53 +000058 bool Restricted = false,
59 bool IsMempcpy = false) const;
Jordy Rosed5d2e502010-07-08 23:57:29 +000060
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +000061 void evalMemcmp(CheckerContext &C, const CallExpr *CE) const;
Jordy Rose134a2362010-07-06 23:11:01 +000062
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +000063 void evalstrLength(CheckerContext &C, const CallExpr *CE) const;
64 void evalstrnLength(CheckerContext &C, const CallExpr *CE) const;
Ted Kremenek280a01f2011-02-22 04:55:05 +000065 void evalstrLengthCommon(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +000066 bool IsStrnlen = false) const;
Jordy Roseb052e8f2010-07-27 01:37:31 +000067
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +000068 void evalStrcpy(CheckerContext &C, const CallExpr *CE) const;
69 void evalStrncpy(CheckerContext &C, const CallExpr *CE) const;
70 void evalStpcpy(CheckerContext &C, const CallExpr *CE) const;
Ted Kremenekfb1a79a2011-02-22 04:58:34 +000071 void evalStrcpyCommon(CheckerContext &C, const CallExpr *CE, bool returnEnd,
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +000072 bool isStrncpy) const;
Jordy Rose722f5582010-08-16 07:51:42 +000073
Jordy Rose134a2362010-07-06 23:11:01 +000074 // Utility methods
Jordy Rosed5d2e502010-07-08 23:57:29 +000075 std::pair<const GRState*, const GRState*>
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +000076 static assumeZero(CheckerContext &C,
77 const GRState *state, SVal V, QualType Ty);
Jordy Rosed5d2e502010-07-08 23:57:29 +000078
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +000079 static const GRState *setCStringLength(const GRState *state,
80 const MemRegion *MR, SVal strLength);
81 static SVal getCStringLengthForRegion(CheckerContext &C,
82 const GRState *&state,
83 const Expr *Ex, const MemRegion *MR);
Ted Kremenek90af9092010-12-02 07:49:45 +000084 SVal getCStringLength(CheckerContext &C, const GRState *&state,
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +000085 const Expr *Ex, SVal Buf) const;
Jordy Roseb052e8f2010-07-27 01:37:31 +000086
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +000087 static const GRState *InvalidateBuffer(CheckerContext &C,
88 const GRState *state,
89 const Expr *Ex, SVal V);
Jordy Rose722f5582010-08-16 07:51:42 +000090
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +000091 static bool SummarizeRegion(llvm::raw_ostream& os, ASTContext& Ctx,
92 const MemRegion *MR);
Jordy Roseb052e8f2010-07-27 01:37:31 +000093
94 // Re-usable checks
Ted Kremenek90af9092010-12-02 07:49:45 +000095 const GRState *checkNonNull(CheckerContext &C, const GRState *state,
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +000096 const Expr *S, SVal l) const;
Jordy Rose134a2362010-07-06 23:11:01 +000097 const GRState *CheckLocation(CheckerContext &C, const GRState *state,
Jordy Rose722f5582010-08-16 07:51:42 +000098 const Expr *S, SVal l,
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +000099 bool IsDestination = false) const;
Jordy Rose134a2362010-07-06 23:11:01 +0000100 const GRState *CheckBufferAccess(CheckerContext &C, const GRState *state,
101 const Expr *Size,
102 const Expr *FirstBuf,
Jordy Rose722f5582010-08-16 07:51:42 +0000103 const Expr *SecondBuf = NULL,
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000104 bool FirstIsDestination = false) const;
Jordy Rose134a2362010-07-06 23:11:01 +0000105 const GRState *CheckOverlap(CheckerContext &C, const GRState *state,
Jordy Rosed5d2e502010-07-08 23:57:29 +0000106 const Expr *Size, const Expr *First,
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000107 const Expr *Second) const;
Ted Kremenek90af9092010-12-02 07:49:45 +0000108 void emitOverlapBug(CheckerContext &C, const GRState *state,
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000109 const Stmt *First, const Stmt *Second) const;
Jordy Rose134a2362010-07-06 23:11:01 +0000110};
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000111
112class CStringLength {
113public:
114 typedef llvm::ImmutableMap<const MemRegion *, SVal> EntryMap;
115};
Jordy Rose134a2362010-07-06 23:11:01 +0000116} //end anonymous namespace
117
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000118namespace clang {
Ted Kremenek98857c92010-12-23 07:20:52 +0000119namespace ento {
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000120 template <>
121 struct GRStateTrait<CStringLength>
122 : public GRStatePartialTrait<CStringLength::EntryMap> {
123 static void *GDMIndex() { return CStringChecker::getTag(); }
124 };
125}
Argyrios Kyrtzidisca08fba2010-12-22 18:53:20 +0000126}
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000127
Jordy Rosed5d2e502010-07-08 23:57:29 +0000128//===----------------------------------------------------------------------===//
129// Individual checks and utility methods.
130//===----------------------------------------------------------------------===//
131
132std::pair<const GRState*, const GRState*>
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000133CStringChecker::assumeZero(CheckerContext &C, const GRState *state, SVal V,
Jordy Rosed5d2e502010-07-08 23:57:29 +0000134 QualType Ty) {
Ted Kremenek90af9092010-12-02 07:49:45 +0000135 DefinedSVal *val = dyn_cast<DefinedSVal>(&V);
136 if (!val)
Jordy Rosed5d2e502010-07-08 23:57:29 +0000137 return std::pair<const GRState*, const GRState *>(state, state);
Jordy Rose33c829a2010-07-07 07:48:06 +0000138
Ted Kremenek90af9092010-12-02 07:49:45 +0000139 SValBuilder &svalBuilder = C.getSValBuilder();
140 DefinedOrUnknownSVal zero = svalBuilder.makeZeroVal(Ty);
141 return state->assume(svalBuilder.evalEQ(state, *val, zero));
Jordy Rosed5d2e502010-07-08 23:57:29 +0000142}
Jordy Rose33c829a2010-07-07 07:48:06 +0000143
Ted Kremenek90af9092010-12-02 07:49:45 +0000144const GRState *CStringChecker::checkNonNull(CheckerContext &C,
Jordy Rosed5d2e502010-07-08 23:57:29 +0000145 const GRState *state,
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000146 const Expr *S, SVal l) const {
Jordy Rosed5d2e502010-07-08 23:57:29 +0000147 // If a previous check has failed, propagate the failure.
148 if (!state)
149 return NULL;
150
151 const GRState *stateNull, *stateNonNull;
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000152 llvm::tie(stateNull, stateNonNull) = assumeZero(C, state, l, S->getType());
Jordy Rosed5d2e502010-07-08 23:57:29 +0000153
154 if (stateNull && !stateNonNull) {
Ted Kremenek750b7ac2010-12-20 21:19:09 +0000155 ExplodedNode *N = C.generateSink(stateNull);
Jordy Rose33c829a2010-07-07 07:48:06 +0000156 if (!N)
157 return NULL;
158
Jordy Rosed5d2e502010-07-08 23:57:29 +0000159 if (!BT_Null)
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000160 BT_Null.reset(new BuiltinBug("API",
161 "Null pointer argument in call to byte string function"));
Jordy Rose33c829a2010-07-07 07:48:06 +0000162
163 // Generate a report for this bug.
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000164 BuiltinBug *BT = static_cast<BuiltinBug*>(BT_Null.get());
Jordy Rose33c829a2010-07-07 07:48:06 +0000165 EnhancedBugReport *report = new EnhancedBugReport(*BT,
166 BT->getDescription(), N);
167
168 report->addRange(S->getSourceRange());
169 report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, S);
170 C.EmitReport(report);
171 return NULL;
172 }
173
174 // From here on, assume that the value is non-null.
Jordy Rosed5d2e502010-07-08 23:57:29 +0000175 assert(stateNonNull);
176 return stateNonNull;
Jordy Rose33c829a2010-07-07 07:48:06 +0000177}
178
Jordy Rose134a2362010-07-06 23:11:01 +0000179// FIXME: This was originally copied from ArrayBoundChecker.cpp. Refactor?
180const GRState *CStringChecker::CheckLocation(CheckerContext &C,
181 const GRState *state,
Jordy Rose722f5582010-08-16 07:51:42 +0000182 const Expr *S, SVal l,
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000183 bool IsDestination) const {
Jordy Rosed5d2e502010-07-08 23:57:29 +0000184 // If a previous check has failed, propagate the failure.
185 if (!state)
186 return NULL;
187
Jordy Rose134a2362010-07-06 23:11:01 +0000188 // Check for out of bound array element access.
189 const MemRegion *R = l.getAsRegion();
190 if (!R)
191 return state;
192
Jordy Rose134a2362010-07-06 23:11:01 +0000193 const ElementRegion *ER = dyn_cast<ElementRegion>(R);
194 if (!ER)
195 return state;
196
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +0000197 assert(ER->getValueType() == C.getASTContext().CharTy &&
Jordy Rose134a2362010-07-06 23:11:01 +0000198 "CheckLocation should only be called with char* ElementRegions");
199
200 // Get the size of the array.
Ted Kremenek90af9092010-12-02 07:49:45 +0000201 const SubRegion *superReg = cast<SubRegion>(ER->getSuperRegion());
202 SValBuilder &svalBuilder = C.getSValBuilder();
203 SVal Extent = svalBuilder.convertToArrayIndex(superReg->getExtent(svalBuilder));
Jordy Rose134a2362010-07-06 23:11:01 +0000204 DefinedOrUnknownSVal Size = cast<DefinedOrUnknownSVal>(Extent);
205
206 // Get the index of the accessed element.
Gabor Greif230ddf32010-09-09 10:51:37 +0000207 DefinedOrUnknownSVal Idx = cast<DefinedOrUnknownSVal>(ER->getIndex());
Jordy Rose134a2362010-07-06 23:11:01 +0000208
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000209 const GRState *StInBound = state->assumeInBound(Idx, Size, true);
210 const GRState *StOutBound = state->assumeInBound(Idx, Size, false);
Jordy Rose134a2362010-07-06 23:11:01 +0000211 if (StOutBound && !StInBound) {
Ted Kremenek750b7ac2010-12-20 21:19:09 +0000212 ExplodedNode *N = C.generateSink(StOutBound);
Jordy Rose134a2362010-07-06 23:11:01 +0000213 if (!N)
214 return NULL;
215
Jordy Rose722f5582010-08-16 07:51:42 +0000216 BuiltinBug *BT;
217 if (IsDestination) {
218 if (!BT_BoundsWrite) {
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000219 BT_BoundsWrite.reset(new BuiltinBug("Out-of-bound array access",
220 "Byte string function overflows destination buffer"));
Jordy Rose722f5582010-08-16 07:51:42 +0000221 }
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000222 BT = static_cast<BuiltinBug*>(BT_BoundsWrite.get());
Jordy Rose722f5582010-08-16 07:51:42 +0000223 } else {
224 if (!BT_Bounds) {
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000225 BT_Bounds.reset(new BuiltinBug("Out-of-bound array access",
226 "Byte string function accesses out-of-bound array element"));
Jordy Rose722f5582010-08-16 07:51:42 +0000227 }
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000228 BT = static_cast<BuiltinBug*>(BT_Bounds.get());
Jordy Rose722f5582010-08-16 07:51:42 +0000229 }
Jordy Rose134a2362010-07-06 23:11:01 +0000230
231 // FIXME: It would be nice to eventually make this diagnostic more clear,
232 // e.g., by referencing the original declaration or by saying *why* this
233 // reference is outside the range.
234
235 // Generate a report for this bug.
Jordy Rose134a2362010-07-06 23:11:01 +0000236 RangedBugReport *report = new RangedBugReport(*BT, BT->getDescription(), N);
237
238 report->addRange(S->getSourceRange());
239 C.EmitReport(report);
240 return NULL;
241 }
242
243 // Array bound check succeeded. From this point forward the array bound
244 // should always succeed.
245 return StInBound;
246}
247
248const GRState *CStringChecker::CheckBufferAccess(CheckerContext &C,
249 const GRState *state,
250 const Expr *Size,
251 const Expr *FirstBuf,
Jordy Rose722f5582010-08-16 07:51:42 +0000252 const Expr *SecondBuf,
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000253 bool FirstIsDestination) const {
Jordy Rosed5d2e502010-07-08 23:57:29 +0000254 // If a previous check has failed, propagate the failure.
255 if (!state)
256 return NULL;
257
Ted Kremenek90af9092010-12-02 07:49:45 +0000258 SValBuilder &svalBuilder = C.getSValBuilder();
Jordy Rose134a2362010-07-06 23:11:01 +0000259 ASTContext &Ctx = C.getASTContext();
260
Ted Kremenek90af9092010-12-02 07:49:45 +0000261 QualType sizeTy = Size->getType();
Jordy Rose134a2362010-07-06 23:11:01 +0000262 QualType PtrTy = Ctx.getPointerType(Ctx.CharTy);
263
Jordy Rose33c829a2010-07-07 07:48:06 +0000264 // Check that the first buffer is non-null.
265 SVal BufVal = state->getSVal(FirstBuf);
Ted Kremenek90af9092010-12-02 07:49:45 +0000266 state = checkNonNull(C, state, FirstBuf, BufVal);
Jordy Rose33c829a2010-07-07 07:48:06 +0000267 if (!state)
268 return NULL;
269
Jordy Rosed5d2e502010-07-08 23:57:29 +0000270 // Get the access length and make sure it is known.
271 SVal LengthVal = state->getSVal(Size);
272 NonLoc *Length = dyn_cast<NonLoc>(&LengthVal);
273 if (!Length)
274 return state;
275
Jordy Rose134a2362010-07-06 23:11:01 +0000276 // Compute the offset of the last element to be accessed: size-1.
Ted Kremenek90af9092010-12-02 07:49:45 +0000277 NonLoc One = cast<NonLoc>(svalBuilder.makeIntVal(1, sizeTy));
278 NonLoc LastOffset = cast<NonLoc>(svalBuilder.evalBinOpNN(state, BO_Sub,
279 *Length, One, sizeTy));
Jordy Rose134a2362010-07-06 23:11:01 +0000280
Jordy Rose33c829a2010-07-07 07:48:06 +0000281 // Check that the first buffer is sufficently long.
Ted Kremenek90af9092010-12-02 07:49:45 +0000282 SVal BufStart = svalBuilder.evalCast(BufVal, PtrTy, FirstBuf->getType());
Jordy Roseafdb0532010-08-05 23:11:30 +0000283 if (Loc *BufLoc = dyn_cast<Loc>(&BufStart)) {
Ted Kremenek90af9092010-12-02 07:49:45 +0000284 SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc,
285 LastOffset, PtrTy);
Jordy Rose722f5582010-08-16 07:51:42 +0000286 state = CheckLocation(C, state, FirstBuf, BufEnd, FirstIsDestination);
Jordy Rose134a2362010-07-06 23:11:01 +0000287
Jordy Roseafdb0532010-08-05 23:11:30 +0000288 // If the buffer isn't large enough, abort.
289 if (!state)
290 return NULL;
291 }
Jordy Rose134a2362010-07-06 23:11:01 +0000292
293 // If there's a second buffer, check it as well.
294 if (SecondBuf) {
295 BufVal = state->getSVal(SecondBuf);
Ted Kremenek90af9092010-12-02 07:49:45 +0000296 state = checkNonNull(C, state, SecondBuf, BufVal);
Jordy Rose33c829a2010-07-07 07:48:06 +0000297 if (!state)
298 return NULL;
299
Ted Kremenek90af9092010-12-02 07:49:45 +0000300 BufStart = svalBuilder.evalCast(BufVal, PtrTy, SecondBuf->getType());
Jordy Roseafdb0532010-08-05 23:11:30 +0000301 if (Loc *BufLoc = dyn_cast<Loc>(&BufStart)) {
Ted Kremenek90af9092010-12-02 07:49:45 +0000302 SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc,
303 LastOffset, PtrTy);
Jordy Roseafdb0532010-08-05 23:11:30 +0000304 state = CheckLocation(C, state, SecondBuf, BufEnd);
305 }
Jordy Rose134a2362010-07-06 23:11:01 +0000306 }
307
308 // Large enough or not, return this state!
309 return state;
310}
311
312const GRState *CStringChecker::CheckOverlap(CheckerContext &C,
313 const GRState *state,
Jordy Rosed5d2e502010-07-08 23:57:29 +0000314 const Expr *Size,
Jordy Rose134a2362010-07-06 23:11:01 +0000315 const Expr *First,
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000316 const Expr *Second) const {
Jordy Rose134a2362010-07-06 23:11:01 +0000317 // Do a simple check for overlap: if the two arguments are from the same
318 // buffer, see if the end of the first is greater than the start of the second
319 // or vice versa.
320
Jordy Rosed5d2e502010-07-08 23:57:29 +0000321 // If a previous check has failed, propagate the failure.
322 if (!state)
323 return NULL;
324
Jordy Rose134a2362010-07-06 23:11:01 +0000325 const GRState *stateTrue, *stateFalse;
326
327 // Get the buffer values and make sure they're known locations.
Ted Kremenek90af9092010-12-02 07:49:45 +0000328 SVal firstVal = state->getSVal(First);
329 SVal secondVal = state->getSVal(Second);
Jordy Rose134a2362010-07-06 23:11:01 +0000330
Ted Kremenek90af9092010-12-02 07:49:45 +0000331 Loc *firstLoc = dyn_cast<Loc>(&firstVal);
332 if (!firstLoc)
Jordy Rose134a2362010-07-06 23:11:01 +0000333 return state;
334
Ted Kremenek90af9092010-12-02 07:49:45 +0000335 Loc *secondLoc = dyn_cast<Loc>(&secondVal);
336 if (!secondLoc)
Jordy Rose134a2362010-07-06 23:11:01 +0000337 return state;
338
339 // Are the two values the same?
Ted Kremenek90af9092010-12-02 07:49:45 +0000340 SValBuilder &svalBuilder = C.getSValBuilder();
341 llvm::tie(stateTrue, stateFalse) =
342 state->assume(svalBuilder.evalEQ(state, *firstLoc, *secondLoc));
Jordy Rose134a2362010-07-06 23:11:01 +0000343
344 if (stateTrue && !stateFalse) {
345 // If the values are known to be equal, that's automatically an overlap.
Ted Kremenek90af9092010-12-02 07:49:45 +0000346 emitOverlapBug(C, stateTrue, First, Second);
Jordy Rose134a2362010-07-06 23:11:01 +0000347 return NULL;
348 }
349
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000350 // assume the two expressions are not equal.
Jordy Rose134a2362010-07-06 23:11:01 +0000351 assert(stateFalse);
352 state = stateFalse;
353
354 // Which value comes first?
Ted Kremenek90af9092010-12-02 07:49:45 +0000355 ASTContext &Ctx = svalBuilder.getContext();
356 QualType cmpTy = Ctx.IntTy;
357 SVal reverse = svalBuilder.evalBinOpLL(state, BO_GT,
358 *firstLoc, *secondLoc, cmpTy);
359 DefinedOrUnknownSVal *reverseTest = dyn_cast<DefinedOrUnknownSVal>(&reverse);
360 if (!reverseTest)
Jordy Rose134a2362010-07-06 23:11:01 +0000361 return state;
362
Ted Kremenek90af9092010-12-02 07:49:45 +0000363 llvm::tie(stateTrue, stateFalse) = state->assume(*reverseTest);
Jordy Rose134a2362010-07-06 23:11:01 +0000364 if (stateTrue) {
365 if (stateFalse) {
366 // If we don't know which one comes first, we can't perform this test.
367 return state;
368 } else {
Ted Kremenek90af9092010-12-02 07:49:45 +0000369 // Switch the values so that firstVal is before secondVal.
370 Loc *tmpLoc = firstLoc;
371 firstLoc = secondLoc;
372 secondLoc = tmpLoc;
Jordy Rose134a2362010-07-06 23:11:01 +0000373
374 // Switch the Exprs as well, so that they still correspond.
375 const Expr *tmpExpr = First;
376 First = Second;
377 Second = tmpExpr;
378 }
379 }
380
381 // Get the length, and make sure it too is known.
382 SVal LengthVal = state->getSVal(Size);
383 NonLoc *Length = dyn_cast<NonLoc>(&LengthVal);
384 if (!Length)
385 return state;
386
387 // Convert the first buffer's start address to char*.
388 // Bail out if the cast fails.
389 QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy);
Ted Kremenek90af9092010-12-02 07:49:45 +0000390 SVal FirstStart = svalBuilder.evalCast(*firstLoc, CharPtrTy, First->getType());
Jordy Rose134a2362010-07-06 23:11:01 +0000391 Loc *FirstStartLoc = dyn_cast<Loc>(&FirstStart);
392 if (!FirstStartLoc)
393 return state;
394
395 // Compute the end of the first buffer. Bail out if THAT fails.
Ted Kremenek90af9092010-12-02 07:49:45 +0000396 SVal FirstEnd = svalBuilder.evalBinOpLN(state, BO_Add,
Jordy Rose134a2362010-07-06 23:11:01 +0000397 *FirstStartLoc, *Length, CharPtrTy);
398 Loc *FirstEndLoc = dyn_cast<Loc>(&FirstEnd);
399 if (!FirstEndLoc)
400 return state;
401
402 // Is the end of the first buffer past the start of the second buffer?
Ted Kremenek90af9092010-12-02 07:49:45 +0000403 SVal Overlap = svalBuilder.evalBinOpLL(state, BO_GT,
404 *FirstEndLoc, *secondLoc, cmpTy);
Jordy Rose134a2362010-07-06 23:11:01 +0000405 DefinedOrUnknownSVal *OverlapTest = dyn_cast<DefinedOrUnknownSVal>(&Overlap);
406 if (!OverlapTest)
407 return state;
408
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000409 llvm::tie(stateTrue, stateFalse) = state->assume(*OverlapTest);
Jordy Rose134a2362010-07-06 23:11:01 +0000410
411 if (stateTrue && !stateFalse) {
412 // Overlap!
Ted Kremenek90af9092010-12-02 07:49:45 +0000413 emitOverlapBug(C, stateTrue, First, Second);
Jordy Rose134a2362010-07-06 23:11:01 +0000414 return NULL;
415 }
416
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000417 // assume the two expressions don't overlap.
Jordy Rose134a2362010-07-06 23:11:01 +0000418 assert(stateFalse);
419 return stateFalse;
420}
421
Ted Kremenek90af9092010-12-02 07:49:45 +0000422void CStringChecker::emitOverlapBug(CheckerContext &C, const GRState *state,
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000423 const Stmt *First, const Stmt *Second) const {
Ted Kremenek750b7ac2010-12-20 21:19:09 +0000424 ExplodedNode *N = C.generateSink(state);
Jordy Rose134a2362010-07-06 23:11:01 +0000425 if (!N)
426 return;
427
428 if (!BT_Overlap)
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000429 BT_Overlap.reset(new BugType("Unix API", "Improper arguments"));
Jordy Rose134a2362010-07-06 23:11:01 +0000430
431 // Generate a report for this bug.
432 RangedBugReport *report =
433 new RangedBugReport(*BT_Overlap,
434 "Arguments must not be overlapping buffers", N);
435 report->addRange(First->getSourceRange());
436 report->addRange(Second->getSourceRange());
437
438 C.EmitReport(report);
439}
440
Ted Kremenek90af9092010-12-02 07:49:45 +0000441const GRState *CStringChecker::setCStringLength(const GRState *state,
Jordy Rose722f5582010-08-16 07:51:42 +0000442 const MemRegion *MR,
Ted Kremenek90af9092010-12-02 07:49:45 +0000443 SVal strLength) {
444 assert(!strLength.isUndef() && "Attempt to set an undefined string length");
445 if (strLength.isUnknown())
Jordy Rose722f5582010-08-16 07:51:42 +0000446 return state;
447
448 MR = MR->StripCasts();
449
450 switch (MR->getKind()) {
451 case MemRegion::StringRegionKind:
452 // FIXME: This can happen if we strcpy() into a string region. This is
453 // undefined [C99 6.4.5p6], but we should still warn about it.
454 return state;
455
456 case MemRegion::SymbolicRegionKind:
457 case MemRegion::AllocaRegionKind:
458 case MemRegion::VarRegionKind:
459 case MemRegion::FieldRegionKind:
460 case MemRegion::ObjCIvarRegionKind:
Ted Kremenek90af9092010-12-02 07:49:45 +0000461 return state->set<CStringLength>(MR, strLength);
Jordy Rose722f5582010-08-16 07:51:42 +0000462
463 case MemRegion::ElementRegionKind:
464 // FIXME: Handle element regions by upper-bounding the parent region's
465 // string length.
466 return state;
467
468 default:
469 // Other regions (mostly non-data) can't have a reliable C string length.
470 // For now, just ignore the change.
471 // FIXME: These are rare but not impossible. We should output some kind of
472 // warning for things like strcpy((char[]){'a', 0}, "b");
473 return state;
474 }
475}
476
Ted Kremenek90af9092010-12-02 07:49:45 +0000477SVal CStringChecker::getCStringLengthForRegion(CheckerContext &C,
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000478 const GRState *&state,
479 const Expr *Ex,
480 const MemRegion *MR) {
481 // If there's a recorded length, go ahead and return it.
482 const SVal *Recorded = state->get<CStringLength>(MR);
483 if (Recorded)
484 return *Recorded;
485
486 // Otherwise, get a new symbol and update the state.
487 unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
Ted Kremenek90af9092010-12-02 07:49:45 +0000488 SValBuilder &svalBuilder = C.getSValBuilder();
489 QualType sizeTy = svalBuilder.getContext().getSizeType();
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000490 SVal strLength = svalBuilder.getMetadataSymbolVal(CStringChecker::getTag(),
491 MR, Ex, sizeTy, Count);
Ted Kremenek90af9092010-12-02 07:49:45 +0000492 state = state->set<CStringLength>(MR, strLength);
493 return strLength;
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000494}
495
Ted Kremenek90af9092010-12-02 07:49:45 +0000496SVal CStringChecker::getCStringLength(CheckerContext &C, const GRState *&state,
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000497 const Expr *Ex, SVal Buf) const {
Jordy Roseb052e8f2010-07-27 01:37:31 +0000498 const MemRegion *MR = Buf.getAsRegion();
499 if (!MR) {
500 // If we can't get a region, see if it's something we /know/ isn't a
501 // C string. In the context of locations, the only time we can issue such
502 // a warning is for labels.
503 if (loc::GotoLabel *Label = dyn_cast<loc::GotoLabel>(&Buf)) {
Ted Kremenek750b7ac2010-12-20 21:19:09 +0000504 if (ExplodedNode *N = C.generateNode(state)) {
Jordy Roseb052e8f2010-07-27 01:37:31 +0000505 if (!BT_NotCString)
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000506 BT_NotCString.reset(new BuiltinBug("API",
507 "Argument is not a null-terminated string."));
Jordy Roseb052e8f2010-07-27 01:37:31 +0000508
509 llvm::SmallString<120> buf;
510 llvm::raw_svector_ostream os(buf);
511 os << "Argument to byte string function is the address of the label '"
Chris Lattner5a9b1ec2011-02-17 05:38:27 +0000512 << Label->getLabel()->getName()
Jordy Roseb052e8f2010-07-27 01:37:31 +0000513 << "', which is not a null-terminated string";
514
515 // Generate a report for this bug.
516 EnhancedBugReport *report = new EnhancedBugReport(*BT_NotCString,
517 os.str(), N);
518
519 report->addRange(Ex->getSourceRange());
520 C.EmitReport(report);
521 }
522
523 return UndefinedVal();
524 }
525
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000526 // If it's not a region and not a label, give up.
527 return UnknownVal();
Jordy Roseb052e8f2010-07-27 01:37:31 +0000528 }
529
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000530 // If we have a region, strip casts from it and see if we can figure out
531 // its length. For anything we can't figure out, just return UnknownVal.
532 MR = MR->StripCasts();
533
534 switch (MR->getKind()) {
535 case MemRegion::StringRegionKind: {
536 // Modifying the contents of string regions is undefined [C99 6.4.5p6],
537 // so we can assume that the byte length is the correct C string length.
Ted Kremenek90af9092010-12-02 07:49:45 +0000538 SValBuilder &svalBuilder = C.getSValBuilder();
539 QualType sizeTy = svalBuilder.getContext().getSizeType();
540 const StringLiteral *strLit = cast<StringRegion>(MR)->getStringLiteral();
541 return svalBuilder.makeIntVal(strLit->getByteLength(), sizeTy);
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000542 }
543 case MemRegion::SymbolicRegionKind:
544 case MemRegion::AllocaRegionKind:
545 case MemRegion::VarRegionKind:
546 case MemRegion::FieldRegionKind:
547 case MemRegion::ObjCIvarRegionKind:
Ted Kremenek90af9092010-12-02 07:49:45 +0000548 return getCStringLengthForRegion(C, state, Ex, MR);
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000549 case MemRegion::CompoundLiteralRegionKind:
550 // FIXME: Can we track this? Is it necessary?
551 return UnknownVal();
552 case MemRegion::ElementRegionKind:
553 // FIXME: How can we handle this? It's not good enough to subtract the
554 // offset from the base string length; consider "123\x00567" and &a[5].
555 return UnknownVal();
556 default:
557 // Other regions (mostly non-data) can't have a reliable C string length.
558 // In this case, an error is emitted and UndefinedVal is returned.
559 // The caller should always be prepared to handle this case.
Ted Kremenek750b7ac2010-12-20 21:19:09 +0000560 if (ExplodedNode *N = C.generateNode(state)) {
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000561 if (!BT_NotCString)
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000562 BT_NotCString.reset(new BuiltinBug("API",
563 "Argument is not a null-terminated string."));
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000564
565 llvm::SmallString<120> buf;
566 llvm::raw_svector_ostream os(buf);
567
568 os << "Argument to byte string function is ";
569
570 if (SummarizeRegion(os, C.getASTContext(), MR))
571 os << ", which is not a null-terminated string";
572 else
573 os << "not a null-terminated string";
574
575 // Generate a report for this bug.
576 EnhancedBugReport *report = new EnhancedBugReport(*BT_NotCString,
577 os.str(), N);
578
579 report->addRange(Ex->getSourceRange());
580 C.EmitReport(report);
581 }
582
583 return UndefinedVal();
584 }
Jordy Roseb052e8f2010-07-27 01:37:31 +0000585}
586
Jordy Rose722f5582010-08-16 07:51:42 +0000587const GRState *CStringChecker::InvalidateBuffer(CheckerContext &C,
588 const GRState *state,
589 const Expr *E, SVal V) {
590 Loc *L = dyn_cast<Loc>(&V);
591 if (!L)
592 return state;
593
594 // FIXME: This is a simplified version of what's in CFRefCount.cpp -- it makes
595 // some assumptions about the value that CFRefCount can't. Even so, it should
596 // probably be refactored.
597 if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(L)) {
598 const MemRegion *R = MR->getRegion()->StripCasts();
599
600 // Are we dealing with an ElementRegion? If so, we should be invalidating
601 // the super-region.
602 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
603 R = ER->getSuperRegion();
604 // FIXME: What about layers of ElementRegions?
605 }
606
607 // Invalidate this region.
608 unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
Ted Kremenekeddeba02011-02-11 19:48:15 +0000609 return state->invalidateRegion(R, E, Count, NULL);
Jordy Rose722f5582010-08-16 07:51:42 +0000610 }
611
612 // If we have a non-region value by chance, just remove the binding.
613 // FIXME: is this necessary or correct? This handles the non-Region
614 // cases. Is it ever valid to store to these?
615 return state->unbindLoc(*L);
616}
617
Jordy Roseb052e8f2010-07-27 01:37:31 +0000618bool CStringChecker::SummarizeRegion(llvm::raw_ostream& os, ASTContext& Ctx,
619 const MemRegion *MR) {
620 const TypedRegion *TR = dyn_cast<TypedRegion>(MR);
621 if (!TR)
622 return false;
623
624 switch (TR->getKind()) {
625 case MemRegion::FunctionTextRegionKind: {
626 const FunctionDecl *FD = cast<FunctionTextRegion>(TR)->getDecl();
627 if (FD)
628 os << "the address of the function '" << FD << "'";
629 else
630 os << "the address of a function";
631 return true;
632 }
633 case MemRegion::BlockTextRegionKind:
634 os << "block text";
635 return true;
636 case MemRegion::BlockDataRegionKind:
637 os << "a block";
638 return true;
639 case MemRegion::CXXThisRegionKind:
Zhongxing Xu03207162010-11-26 08:52:48 +0000640 case MemRegion::CXXTempObjectRegionKind:
641 os << "a C++ temp object of type " << TR->getValueType().getAsString();
Jordy Roseb052e8f2010-07-27 01:37:31 +0000642 return true;
643 case MemRegion::VarRegionKind:
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +0000644 os << "a variable of type" << TR->getValueType().getAsString();
Jordy Roseb052e8f2010-07-27 01:37:31 +0000645 return true;
646 case MemRegion::FieldRegionKind:
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +0000647 os << "a field of type " << TR->getValueType().getAsString();
Jordy Roseb052e8f2010-07-27 01:37:31 +0000648 return true;
649 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +0000650 os << "an instance variable of type " << TR->getValueType().getAsString();
Jordy Roseb052e8f2010-07-27 01:37:31 +0000651 return true;
652 default:
653 return false;
654 }
655}
656
Jordy Rosed5d2e502010-07-08 23:57:29 +0000657//===----------------------------------------------------------------------===//
Ted Kremenekdc891422010-12-01 21:57:22 +0000658// evaluation of individual function calls.
Jordy Rosed5d2e502010-07-08 23:57:29 +0000659//===----------------------------------------------------------------------===//
660
Lenny Maiorani79d74142011-03-31 21:36:53 +0000661void CStringChecker::evalCopyCommon(CheckerContext &C,
662 const CallExpr *CE,
663 const GRState *state,
Jordy Rosed5d2e502010-07-08 23:57:29 +0000664 const Expr *Size, const Expr *Dest,
Lenny Maiorani79d74142011-03-31 21:36:53 +0000665 const Expr *Source, bool Restricted,
666 bool IsMempcpy) const {
Jordy Rosed5d2e502010-07-08 23:57:29 +0000667 // See if the size argument is zero.
Ted Kremenek90af9092010-12-02 07:49:45 +0000668 SVal sizeVal = state->getSVal(Size);
669 QualType sizeTy = Size->getType();
Jordy Rosed5d2e502010-07-08 23:57:29 +0000670
Ted Kremenek90af9092010-12-02 07:49:45 +0000671 const GRState *stateZeroSize, *stateNonZeroSize;
672 llvm::tie(stateZeroSize, stateNonZeroSize) = assumeZero(C, state, sizeVal, sizeTy);
Jordy Rosed5d2e502010-07-08 23:57:29 +0000673
Lenny Maiorani79d74142011-03-31 21:36:53 +0000674 // Get the value of the Dest.
675 SVal destVal = state->getSVal(Dest);
676
677 // If the size is zero, there won't be any actual memory access, so
678 // just bind the return value to the destination buffer and return.
679 if (stateZeroSize) {
Ted Kremenek90af9092010-12-02 07:49:45 +0000680 C.addTransition(stateZeroSize);
Lenny Maiorani79d74142011-03-31 21:36:53 +0000681 if (IsMempcpy)
682 state->BindExpr(CE, destVal);
683 else
684 state->BindExpr(CE, sizeVal);
685 return;
686 }
Jordy Rosed5d2e502010-07-08 23:57:29 +0000687
688 // If the size can be nonzero, we have to check the other arguments.
Ted Kremenek90af9092010-12-02 07:49:45 +0000689 if (stateNonZeroSize) {
Lenny Maiorani79d74142011-03-31 21:36:53 +0000690
691 // Ensure the destination is not null. If it is NULL there will be a
692 // NULL pointer dereference.
693 state = checkNonNull(C, state, Dest, destVal);
694 if (!state)
695 return;
696
697 // Get the value of the Src.
698 SVal srcVal = state->getSVal(Source);
699
700 // Ensure the source is not null. If it is NULL there will be a
701 // NULL pointer dereference.
702 state = checkNonNull(C, state, Source, srcVal);
703 if (!state)
704 return;
705
706 // Ensure the buffers do not overlap.
Ted Kremenek90af9092010-12-02 07:49:45 +0000707 state = stateNonZeroSize;
Jordy Rose722f5582010-08-16 07:51:42 +0000708 state = CheckBufferAccess(C, state, Size, Dest, Source,
709 /* FirstIsDst = */ true);
Jordy Rosed5d2e502010-07-08 23:57:29 +0000710 if (Restricted)
711 state = CheckOverlap(C, state, Size, Dest, Source);
Jordy Rose722f5582010-08-16 07:51:42 +0000712
713 if (state) {
Lenny Maiorani79d74142011-03-31 21:36:53 +0000714
715 // If this is mempcpy, get the byte after the last byte copied and
716 // bind the expr.
717 if (IsMempcpy) {
718 loc::MemRegionVal *destRegVal = dyn_cast<loc::MemRegionVal>(&destVal);
719
720 // Get the length to copy.
721 SVal lenVal = state->getSVal(Size);
722 NonLoc *lenValNonLoc = dyn_cast<NonLoc>(&lenVal);
723
724 // Get the byte after the last byte copied.
725 SVal lastElement = C.getSValBuilder().evalBinOpLN(state, BO_Add,
726 *destRegVal,
727 *lenValNonLoc,
728 Dest->getType());
729
730 // The byte after the last byte copied is the return value.
731 state = state->BindExpr(CE, lastElement);
732 }
733
Jordy Rose722f5582010-08-16 07:51:42 +0000734 // Invalidate the destination.
735 // FIXME: Even if we can't perfectly model the copy, we should see if we
736 // can use LazyCompoundVals to copy the source values into the destination.
737 // This would probably remove any existing bindings past the end of the
738 // copied region, but that's still an improvement over blank invalidation.
739 state = InvalidateBuffer(C, state, Dest, state->getSVal(Dest));
Jordy Rosed5d2e502010-07-08 23:57:29 +0000740 C.addTransition(state);
Jordy Rose722f5582010-08-16 07:51:42 +0000741 }
Jordy Rosed5d2e502010-07-08 23:57:29 +0000742 }
743}
744
745
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000746void CStringChecker::evalMemcpy(CheckerContext &C, const CallExpr *CE) const {
Jordy Rose134a2362010-07-06 23:11:01 +0000747 // void *memcpy(void *restrict dst, const void *restrict src, size_t n);
Jordy Rose134a2362010-07-06 23:11:01 +0000748 // The return value is the address of the destination buffer.
Jordy Rosed5d2e502010-07-08 23:57:29 +0000749 const Expr *Dest = CE->getArg(0);
750 const GRState *state = C.getState();
751 state = state->BindExpr(CE, state->getSVal(Dest));
Lenny Maiorani79d74142011-03-31 21:36:53 +0000752 evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1), true);
753}
754
755void CStringChecker::evalMempcpy(CheckerContext &C, const CallExpr *CE) const {
756 // void *mempcpy(void *restrict dst, const void *restrict src, size_t n);
757 // The return value is a pointer to the byte following the last written byte.
758 const Expr *Dest = CE->getArg(0);
759 const GRState *state = C.getState();
760
761 evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1), true, true);
Jordy Rose134a2362010-07-06 23:11:01 +0000762}
763
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000764void CStringChecker::evalMemmove(CheckerContext &C, const CallExpr *CE) const {
Jordy Rosed5d2e502010-07-08 23:57:29 +0000765 // void *memmove(void *dst, const void *src, size_t n);
766 // The return value is the address of the destination buffer.
767 const Expr *Dest = CE->getArg(0);
768 const GRState *state = C.getState();
769 state = state->BindExpr(CE, state->getSVal(Dest));
Lenny Maiorani79d74142011-03-31 21:36:53 +0000770 evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1));
Jordy Rosed5d2e502010-07-08 23:57:29 +0000771}
772
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000773void CStringChecker::evalBcopy(CheckerContext &C, const CallExpr *CE) const {
Jordy Rosed5d2e502010-07-08 23:57:29 +0000774 // void bcopy(const void *src, void *dst, size_t n);
Lenny Maiorani79d74142011-03-31 21:36:53 +0000775 evalCopyCommon(C, CE, C.getState(),
776 CE->getArg(2), CE->getArg(1), CE->getArg(0));
Jordy Rosed5d2e502010-07-08 23:57:29 +0000777}
778
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000779void CStringChecker::evalMemcmp(CheckerContext &C, const CallExpr *CE) const {
Jordy Rose65136fb2010-07-07 08:15:01 +0000780 // int memcmp(const void *s1, const void *s2, size_t n);
781 const Expr *Left = CE->getArg(0);
782 const Expr *Right = CE->getArg(1);
783 const Expr *Size = CE->getArg(2);
784
785 const GRState *state = C.getState();
Ted Kremenek90af9092010-12-02 07:49:45 +0000786 SValBuilder &svalBuilder = C.getSValBuilder();
Jordy Rose65136fb2010-07-07 08:15:01 +0000787
Jordy Rosed5d2e502010-07-08 23:57:29 +0000788 // See if the size argument is zero.
Ted Kremenek90af9092010-12-02 07:49:45 +0000789 SVal sizeVal = state->getSVal(Size);
790 QualType sizeTy = Size->getType();
Jordy Rose65136fb2010-07-07 08:15:01 +0000791
Ted Kremenek90af9092010-12-02 07:49:45 +0000792 const GRState *stateZeroSize, *stateNonZeroSize;
793 llvm::tie(stateZeroSize, stateNonZeroSize) =
794 assumeZero(C, state, sizeVal, sizeTy);
Jordy Rose65136fb2010-07-07 08:15:01 +0000795
Jordy Rosed5d2e502010-07-08 23:57:29 +0000796 // If the size can be zero, the result will be 0 in that case, and we don't
797 // have to check either of the buffers.
Ted Kremenek90af9092010-12-02 07:49:45 +0000798 if (stateZeroSize) {
799 state = stateZeroSize;
800 state = state->BindExpr(CE, svalBuilder.makeZeroVal(CE->getType()));
Jordy Rosed5d2e502010-07-08 23:57:29 +0000801 C.addTransition(state);
Jordy Rose65136fb2010-07-07 08:15:01 +0000802 }
803
Jordy Rosed5d2e502010-07-08 23:57:29 +0000804 // If the size can be nonzero, we have to check the other arguments.
Ted Kremenek90af9092010-12-02 07:49:45 +0000805 if (stateNonZeroSize) {
806 state = stateNonZeroSize;
Jordy Rosed5d2e502010-07-08 23:57:29 +0000807 // If we know the two buffers are the same, we know the result is 0.
808 // First, get the two buffers' addresses. Another checker will have already
809 // made sure they're not undefined.
810 DefinedOrUnknownSVal LV = cast<DefinedOrUnknownSVal>(state->getSVal(Left));
811 DefinedOrUnknownSVal RV = cast<DefinedOrUnknownSVal>(state->getSVal(Right));
Jordy Rose65136fb2010-07-07 08:15:01 +0000812
Jordy Rosed5d2e502010-07-08 23:57:29 +0000813 // See if they are the same.
Ted Kremenek90af9092010-12-02 07:49:45 +0000814 DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV);
Jordy Rosed5d2e502010-07-08 23:57:29 +0000815 const GRState *StSameBuf, *StNotSameBuf;
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000816 llvm::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf);
Jordy Rosed5d2e502010-07-08 23:57:29 +0000817
818 // If the two arguments might be the same buffer, we know the result is zero,
819 // and we only need to check one size.
820 if (StSameBuf) {
821 state = StSameBuf;
822 state = CheckBufferAccess(C, state, Size, Left);
823 if (state) {
Ted Kremenek90af9092010-12-02 07:49:45 +0000824 state = StSameBuf->BindExpr(CE, svalBuilder.makeZeroVal(CE->getType()));
Jordy Rosed5d2e502010-07-08 23:57:29 +0000825 C.addTransition(state);
826 }
827 }
828
829 // If the two arguments might be different buffers, we have to check the
830 // size of both of them.
831 if (StNotSameBuf) {
832 state = StNotSameBuf;
833 state = CheckBufferAccess(C, state, Size, Left, Right);
834 if (state) {
835 // The return value is the comparison result, which we don't know.
836 unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
Ted Kremenek90af9092010-12-02 07:49:45 +0000837 SVal CmpV = svalBuilder.getConjuredSymbolVal(NULL, CE, Count);
Jordy Rosed5d2e502010-07-08 23:57:29 +0000838 state = state->BindExpr(CE, CmpV);
839 C.addTransition(state);
840 }
841 }
842 }
Jordy Rose65136fb2010-07-07 08:15:01 +0000843}
844
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000845void CStringChecker::evalstrLength(CheckerContext &C,
846 const CallExpr *CE) const {
Jordy Roseb052e8f2010-07-27 01:37:31 +0000847 // size_t strlen(const char *s);
Ted Kremenek280a01f2011-02-22 04:55:05 +0000848 evalstrLengthCommon(C, CE, /* IsStrnlen = */ false);
849}
850
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000851void CStringChecker::evalstrnLength(CheckerContext &C,
852 const CallExpr *CE) const {
Ted Kremenek280a01f2011-02-22 04:55:05 +0000853 // size_t strnlen(const char *s, size_t maxlen);
854 evalstrLengthCommon(C, CE, /* IsStrnlen = */ true);
855}
856
857void CStringChecker::evalstrLengthCommon(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000858 bool IsStrnlen) const {
Jordy Roseb052e8f2010-07-27 01:37:31 +0000859 const GRState *state = C.getState();
860 const Expr *Arg = CE->getArg(0);
861 SVal ArgVal = state->getSVal(Arg);
862
863 // Check that the argument is non-null.
Ted Kremenek90af9092010-12-02 07:49:45 +0000864 state = checkNonNull(C, state, Arg, ArgVal);
Jordy Roseb052e8f2010-07-27 01:37:31 +0000865
866 if (state) {
Ted Kremenek90af9092010-12-02 07:49:45 +0000867 SVal strLength = getCStringLength(C, state, Arg, ArgVal);
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000868
869 // If the argument isn't a valid C string, there's no valid state to
870 // transition to.
Ted Kremenek90af9092010-12-02 07:49:45 +0000871 if (strLength.isUndef())
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000872 return;
873
Ted Kremenek280a01f2011-02-22 04:55:05 +0000874 // If the check is for strnlen() then bind the return value to no more than
875 // the maxlen value.
876 if (IsStrnlen) {
877 const Expr *maxlenExpr = CE->getArg(1);
878 SVal maxlenVal = state->getSVal(maxlenExpr);
879
880 NonLoc *strLengthNL = dyn_cast<NonLoc>(&strLength);
881 NonLoc *maxlenValNL = dyn_cast<NonLoc>(&maxlenVal);
882
883 QualType cmpTy = C.getSValBuilder().getContext().IntTy;
884 const GRState *stateTrue, *stateFalse;
885
886 // Check if the strLength is greater than or equal to the maxlen
887 llvm::tie(stateTrue, stateFalse) =
888 state->assume(cast<DefinedOrUnknownSVal>
889 (C.getSValBuilder().evalBinOpNN(state, BO_GE,
890 *strLengthNL, *maxlenValNL,
891 cmpTy)));
892
893 // If the strLength is greater than or equal to the maxlen, set strLength
894 // to maxlen
895 if (stateTrue && !stateFalse) {
896 strLength = maxlenVal;
897 }
898 }
899
Ted Kremenek90af9092010-12-02 07:49:45 +0000900 // If getCStringLength couldn't figure out the length, conjure a return
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000901 // value, so it can be used in constraints, at least.
Ted Kremenek90af9092010-12-02 07:49:45 +0000902 if (strLength.isUnknown()) {
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000903 unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
Ted Kremenek90af9092010-12-02 07:49:45 +0000904 strLength = C.getSValBuilder().getConjuredSymbolVal(NULL, CE, Count);
Jordy Roseb052e8f2010-07-27 01:37:31 +0000905 }
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000906
907 // Bind the return value.
Ted Kremenek90af9092010-12-02 07:49:45 +0000908 state = state->BindExpr(CE, strLength);
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000909 C.addTransition(state);
Jordy Roseb052e8f2010-07-27 01:37:31 +0000910 }
911}
912
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000913void CStringChecker::evalStrcpy(CheckerContext &C, const CallExpr *CE) const {
Jordy Rose722f5582010-08-16 07:51:42 +0000914 // char *strcpy(char *restrict dst, const char *restrict src);
Ted Kremenekfb1a79a2011-02-22 04:58:34 +0000915 evalStrcpyCommon(C, CE, /* returnEnd = */ false, /* isStrncpy = */ false);
916}
917
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000918void CStringChecker::evalStrncpy(CheckerContext &C, const CallExpr *CE) const {
Ted Kremenekfb1a79a2011-02-22 04:58:34 +0000919 // char *strcpy(char *restrict dst, const char *restrict src);
920 evalStrcpyCommon(C, CE, /* returnEnd = */ false, /* isStrncpy = */ true);
Jordy Rose722f5582010-08-16 07:51:42 +0000921}
922
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000923void CStringChecker::evalStpcpy(CheckerContext &C, const CallExpr *CE) const {
Jordy Rose722f5582010-08-16 07:51:42 +0000924 // char *stpcpy(char *restrict dst, const char *restrict src);
Ted Kremenekfb1a79a2011-02-22 04:58:34 +0000925 evalStrcpyCommon(C, CE, /* returnEnd = */ true, /* isStrncpy = */ false);
Jordy Rose722f5582010-08-16 07:51:42 +0000926}
927
Ted Kremenekdc891422010-12-01 21:57:22 +0000928void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE,
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +0000929 bool returnEnd, bool isStrncpy) const {
Jordy Rose722f5582010-08-16 07:51:42 +0000930 const GRState *state = C.getState();
931
932 // Check that the destination is non-null
933 const Expr *Dst = CE->getArg(0);
934 SVal DstVal = state->getSVal(Dst);
935
Ted Kremenek90af9092010-12-02 07:49:45 +0000936 state = checkNonNull(C, state, Dst, DstVal);
Jordy Rose722f5582010-08-16 07:51:42 +0000937 if (!state)
938 return;
939
940 // Check that the source is non-null.
Ted Kremenek90af9092010-12-02 07:49:45 +0000941 const Expr *srcExpr = CE->getArg(1);
942 SVal srcVal = state->getSVal(srcExpr);
943 state = checkNonNull(C, state, srcExpr, srcVal);
Jordy Rose722f5582010-08-16 07:51:42 +0000944 if (!state)
945 return;
946
947 // Get the string length of the source.
Ted Kremenek90af9092010-12-02 07:49:45 +0000948 SVal strLength = getCStringLength(C, state, srcExpr, srcVal);
Jordy Rose722f5582010-08-16 07:51:42 +0000949
950 // If the source isn't a valid C string, give up.
Ted Kremenek90af9092010-12-02 07:49:45 +0000951 if (strLength.isUndef())
Jordy Rose722f5582010-08-16 07:51:42 +0000952 return;
953
Ted Kremenekfb1a79a2011-02-22 04:58:34 +0000954 if (isStrncpy) {
955 // Get the max number of characters to copy
956 const Expr *lenExpr = CE->getArg(2);
957 SVal lenVal = state->getSVal(lenExpr);
958
959 NonLoc *strLengthNL = dyn_cast<NonLoc>(&strLength);
960 NonLoc *lenValNL = dyn_cast<NonLoc>(&lenVal);
961
962 QualType cmpTy = C.getSValBuilder().getContext().IntTy;
963 const GRState *stateTrue, *stateFalse;
964
965 // Check if the max number to copy is less than the length of the src
966 llvm::tie(stateTrue, stateFalse) =
967 state->assume(cast<DefinedOrUnknownSVal>
968 (C.getSValBuilder().evalBinOpNN(state, BO_GT,
969 *strLengthNL, *lenValNL,
970 cmpTy)));
971
972 if (stateTrue) {
973 // Max number to copy is less than the length of the src, so the actual
974 // strLength copied is the max number arg.
975 strLength = lenVal;
976 }
977 }
978
Ted Kremenek90af9092010-12-02 07:49:45 +0000979 SVal Result = (returnEnd ? UnknownVal() : DstVal);
Jordy Rose722f5582010-08-16 07:51:42 +0000980
981 // If the destination is a MemRegion, try to check for a buffer overflow and
982 // record the new string length.
Ted Kremenek90af9092010-12-02 07:49:45 +0000983 if (loc::MemRegionVal *dstRegVal = dyn_cast<loc::MemRegionVal>(&DstVal)) {
Jordy Rose722f5582010-08-16 07:51:42 +0000984 // If the length is known, we can check for an overflow.
Ted Kremenek90af9092010-12-02 07:49:45 +0000985 if (NonLoc *knownStrLength = dyn_cast<NonLoc>(&strLength)) {
986 SVal lastElement =
987 C.getSValBuilder().evalBinOpLN(state, BO_Add, *dstRegVal,
988 *knownStrLength, Dst->getType());
Jordy Rose722f5582010-08-16 07:51:42 +0000989
Ted Kremenek90af9092010-12-02 07:49:45 +0000990 state = CheckLocation(C, state, Dst, lastElement, /* IsDst = */ true);
Jordy Rose722f5582010-08-16 07:51:42 +0000991 if (!state)
992 return;
993
994 // If this is a stpcpy-style copy, the last element is the return value.
Ted Kremenek90af9092010-12-02 07:49:45 +0000995 if (returnEnd)
996 Result = lastElement;
Jordy Rose722f5582010-08-16 07:51:42 +0000997 }
998
999 // Invalidate the destination. This must happen before we set the C string
1000 // length because invalidation will clear the length.
1001 // FIXME: Even if we can't perfectly model the copy, we should see if we
1002 // can use LazyCompoundVals to copy the source values into the destination.
1003 // This would probably remove any existing bindings past the end of the
1004 // string, but that's still an improvement over blank invalidation.
Ted Kremenek90af9092010-12-02 07:49:45 +00001005 state = InvalidateBuffer(C, state, Dst, *dstRegVal);
Jordy Rose722f5582010-08-16 07:51:42 +00001006
1007 // Set the C string length of the destination.
Ted Kremenek90af9092010-12-02 07:49:45 +00001008 state = setCStringLength(state, dstRegVal->getRegion(), strLength);
Jordy Rose722f5582010-08-16 07:51:42 +00001009 }
1010
1011 // If this is a stpcpy-style copy, but we were unable to check for a buffer
1012 // overflow, we still need a result. Conjure a return value.
Ted Kremenek90af9092010-12-02 07:49:45 +00001013 if (returnEnd && Result.isUnknown()) {
1014 SValBuilder &svalBuilder = C.getSValBuilder();
Jordy Rose722f5582010-08-16 07:51:42 +00001015 unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
Ted Kremenek90af9092010-12-02 07:49:45 +00001016 strLength = svalBuilder.getConjuredSymbolVal(NULL, CE, Count);
Jordy Rose722f5582010-08-16 07:51:42 +00001017 }
1018
1019 // Set the return value.
1020 state = state->BindExpr(CE, Result);
1021 C.addTransition(state);
1022}
1023
Jordy Rosed5d2e502010-07-08 23:57:29 +00001024//===----------------------------------------------------------------------===//
Jordy Rose2a2e21c2010-08-14 21:02:52 +00001025// The driver method, and other Checker callbacks.
Jordy Rosed5d2e502010-07-08 23:57:29 +00001026//===----------------------------------------------------------------------===//
Jordy Rose134a2362010-07-06 23:11:01 +00001027
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +00001028bool CStringChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
Jordy Rose134a2362010-07-06 23:11:01 +00001029 // Get the callee. All the functions we care about are C functions
1030 // with simple identifiers.
1031 const GRState *state = C.getState();
1032 const Expr *Callee = CE->getCallee();
1033 const FunctionDecl *FD = state->getSVal(Callee).getAsFunctionDecl();
1034
1035 if (!FD)
1036 return false;
1037
1038 // Get the name of the callee. If it's a builtin, strip off the prefix.
Douglas Gregor4b8eca82010-11-01 23:16:05 +00001039 IdentifierInfo *II = FD->getIdentifier();
1040 if (!II) // if no identifier, not a simple C function
1041 return false;
1042 llvm::StringRef Name = II->getName();
Jordy Rose134a2362010-07-06 23:11:01 +00001043 if (Name.startswith("__builtin_"))
1044 Name = Name.substr(10);
1045
Ted Kremenekdc891422010-12-01 21:57:22 +00001046 FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
1047 .Cases("memcpy", "__memcpy_chk", &CStringChecker::evalMemcpy)
Lenny Maiorani79d74142011-03-31 21:36:53 +00001048 .Case("mempcpy", &CStringChecker::evalMempcpy)
Ted Kremenekdc891422010-12-01 21:57:22 +00001049 .Cases("memcmp", "bcmp", &CStringChecker::evalMemcmp)
1050 .Cases("memmove", "__memmove_chk", &CStringChecker::evalMemmove)
1051 .Cases("strcpy", "__strcpy_chk", &CStringChecker::evalStrcpy)
Ted Kremenekfb1a79a2011-02-22 04:58:34 +00001052 .Cases("strncpy", "__strncpy_chk", &CStringChecker::evalStrncpy)
Ted Kremenekdc891422010-12-01 21:57:22 +00001053 .Cases("stpcpy", "__stpcpy_chk", &CStringChecker::evalStpcpy)
Ted Kremenek90af9092010-12-02 07:49:45 +00001054 .Case("strlen", &CStringChecker::evalstrLength)
Ted Kremenek280a01f2011-02-22 04:55:05 +00001055 .Case("strnlen", &CStringChecker::evalstrnLength)
Ted Kremenekdc891422010-12-01 21:57:22 +00001056 .Case("bcopy", &CStringChecker::evalBcopy)
Jordy Rose134a2362010-07-06 23:11:01 +00001057 .Default(NULL);
1058
Jordy Rosed5d2e502010-07-08 23:57:29 +00001059 // If the callee isn't a string function, let another checker handle it.
Ted Kremenekdc891422010-12-01 21:57:22 +00001060 if (!evalFunction)
Jordy Rose134a2362010-07-06 23:11:01 +00001061 return false;
1062
Jordy Rosed5d2e502010-07-08 23:57:29 +00001063 // Check and evaluate the call.
Ted Kremenekdc891422010-12-01 21:57:22 +00001064 (this->*evalFunction)(C, CE);
Jordy Rose134a2362010-07-06 23:11:01 +00001065 return true;
1066}
Jordy Rose2a2e21c2010-08-14 21:02:52 +00001067
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +00001068void CStringChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const {
Jordy Rose2a2e21c2010-08-14 21:02:52 +00001069 // Record string length for char a[] = "abc";
1070 const GRState *state = C.getState();
1071
1072 for (DeclStmt::const_decl_iterator I = DS->decl_begin(), E = DS->decl_end();
1073 I != E; ++I) {
1074 const VarDecl *D = dyn_cast<VarDecl>(*I);
1075 if (!D)
1076 continue;
1077
1078 // FIXME: Handle array fields of structs.
1079 if (!D->getType()->isArrayType())
1080 continue;
1081
1082 const Expr *Init = D->getInit();
1083 if (!Init)
1084 continue;
1085 if (!isa<StringLiteral>(Init))
1086 continue;
1087
1088 Loc VarLoc = state->getLValue(D, C.getPredecessor()->getLocationContext());
1089 const MemRegion *MR = VarLoc.getAsRegion();
1090 if (!MR)
1091 continue;
1092
1093 SVal StrVal = state->getSVal(Init);
1094 assert(StrVal.isValid() && "Initializer string is unknown or undefined");
Ted Kremenek90af9092010-12-02 07:49:45 +00001095 DefinedOrUnknownSVal strLength
1096 = cast<DefinedOrUnknownSVal>(getCStringLength(C, state, Init, StrVal));
Jordy Rose2a2e21c2010-08-14 21:02:52 +00001097
Ted Kremenek90af9092010-12-02 07:49:45 +00001098 state = state->set<CStringLength>(MR, strLength);
Jordy Rose2a2e21c2010-08-14 21:02:52 +00001099 }
1100
1101 C.addTransition(state);
1102}
1103
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +00001104bool CStringChecker::wantsRegionChangeUpdate(const GRState *state) const {
Jordy Rose2a2e21c2010-08-14 21:02:52 +00001105 CStringLength::EntryMap Entries = state->get<CStringLength>();
1106 return !Entries.isEmpty();
1107}
1108
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +00001109const GRState *
1110CStringChecker::checkRegionChanges(const GRState *state,
1111 const MemRegion * const *Begin,
1112 const MemRegion * const *End) const {
Jordy Rose2a2e21c2010-08-14 21:02:52 +00001113 CStringLength::EntryMap Entries = state->get<CStringLength>();
1114 if (Entries.isEmpty())
1115 return state;
1116
1117 llvm::SmallPtrSet<const MemRegion *, 8> Invalidated;
1118 llvm::SmallPtrSet<const MemRegion *, 32> SuperRegions;
1119
1120 // First build sets for the changed regions and their super-regions.
1121 for ( ; Begin != End; ++Begin) {
1122 const MemRegion *MR = *Begin;
1123 Invalidated.insert(MR);
1124
1125 SuperRegions.insert(MR);
1126 while (const SubRegion *SR = dyn_cast<SubRegion>(MR)) {
1127 MR = SR->getSuperRegion();
1128 SuperRegions.insert(MR);
1129 }
1130 }
1131
1132 CStringLength::EntryMap::Factory &F = state->get_context<CStringLength>();
1133
1134 // Then loop over the entries in the current state.
1135 for (CStringLength::EntryMap::iterator I = Entries.begin(),
1136 E = Entries.end(); I != E; ++I) {
1137 const MemRegion *MR = I.getKey();
1138
1139 // Is this entry for a super-region of a changed region?
1140 if (SuperRegions.count(MR)) {
Ted Kremenekb3b56c62010-11-24 00:54:37 +00001141 Entries = F.remove(Entries, MR);
Jordy Rose2a2e21c2010-08-14 21:02:52 +00001142 continue;
1143 }
1144
1145 // Is this entry for a sub-region of a changed region?
1146 const MemRegion *Super = MR;
1147 while (const SubRegion *SR = dyn_cast<SubRegion>(Super)) {
1148 Super = SR->getSuperRegion();
1149 if (Invalidated.count(Super)) {
Ted Kremenekb3b56c62010-11-24 00:54:37 +00001150 Entries = F.remove(Entries, MR);
Jordy Rose2a2e21c2010-08-14 21:02:52 +00001151 break;
1152 }
1153 }
1154 }
1155
1156 return state->set<CStringLength>(Entries);
1157}
1158
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +00001159void CStringChecker::checkLiveSymbols(const GRState *state,
1160 SymbolReaper &SR) const {
Jordy Rose2a2e21c2010-08-14 21:02:52 +00001161 // Mark all symbols in our string length map as valid.
1162 CStringLength::EntryMap Entries = state->get<CStringLength>();
1163
1164 for (CStringLength::EntryMap::iterator I = Entries.begin(), E = Entries.end();
1165 I != E; ++I) {
1166 SVal Len = I.getData();
1167 if (SymbolRef Sym = Len.getAsSymbol())
1168 SR.markInUse(Sym);
1169 }
1170}
1171
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +00001172void CStringChecker::checkDeadSymbols(SymbolReaper &SR,
1173 CheckerContext &C) const {
Jordy Rose2a2e21c2010-08-14 21:02:52 +00001174 if (!SR.hasDeadSymbols())
1175 return;
1176
1177 const GRState *state = C.getState();
1178 CStringLength::EntryMap Entries = state->get<CStringLength>();
1179 if (Entries.isEmpty())
1180 return;
1181
1182 CStringLength::EntryMap::Factory &F = state->get_context<CStringLength>();
1183 for (CStringLength::EntryMap::iterator I = Entries.begin(), E = Entries.end();
1184 I != E; ++I) {
1185 SVal Len = I.getData();
1186 if (SymbolRef Sym = Len.getAsSymbol()) {
1187 if (SR.isDead(Sym))
Ted Kremenekb3b56c62010-11-24 00:54:37 +00001188 Entries = F.remove(Entries, I.getKey());
Jordy Rose2a2e21c2010-08-14 21:02:52 +00001189 }
1190 }
1191
1192 state = state->set<CStringLength>(Entries);
Ted Kremenek750b7ac2010-12-20 21:19:09 +00001193 C.generateNode(state);
Jordy Rose2a2e21c2010-08-14 21:02:52 +00001194}
Argyrios Kyrtzidisc26f15d2011-02-24 01:05:30 +00001195
1196void ento::registerCStringChecker(CheckerManager &mgr) {
1197 mgr.registerChecker<CStringChecker>();
1198}