blob: 03f9047e960c6ca47fd62d6ea2a7a3182be21ef9 [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 Kyrtzidis507ff532011-02-17 21:39:17 +000016#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000017#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
18#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerVisitor.h"
19#include "clang/StaticAnalyzer/Core/PathSensitive/GRStateTrait.h"
Jordy Rose134a2362010-07-06 23:11:01 +000020#include "llvm/ADT/StringSwitch.h"
21
22using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000023using namespace ento;
Jordy Rose134a2362010-07-06 23:11:01 +000024
25namespace {
26class CStringChecker : public CheckerVisitor<CStringChecker> {
Jordy Rose722f5582010-08-16 07:51:42 +000027 BugType *BT_Null, *BT_Bounds, *BT_BoundsWrite, *BT_Overlap, *BT_NotCString;
Jordy Rose134a2362010-07-06 23:11:01 +000028public:
29 CStringChecker()
Jordy Rose722f5582010-08-16 07:51:42 +000030 : BT_Null(0), BT_Bounds(0), BT_BoundsWrite(0), BT_Overlap(0), BT_NotCString(0)
31 {}
Jordy Rose134a2362010-07-06 23:11:01 +000032 static void *getTag() { static int tag; return &tag; }
33
Ted Kremenekdc891422010-12-01 21:57:22 +000034 bool evalCallExpr(CheckerContext &C, const CallExpr *CE);
Jordy Rose2a2e21c2010-08-14 21:02:52 +000035 void PreVisitDeclStmt(CheckerContext &C, const DeclStmt *DS);
36 void MarkLiveSymbols(const GRState *state, SymbolReaper &SR);
Ted Kremenekdc891422010-12-01 21:57:22 +000037 void evalDeadSymbols(CheckerContext &C, SymbolReaper &SR);
Ted Kremenek926c9622011-01-11 02:34:45 +000038 bool wantsRegionChangeUpdate(const GRState *state);
Jordy Rose2a2e21c2010-08-14 21:02:52 +000039
40 const GRState *EvalRegionChanges(const GRState *state,
41 const MemRegion * const *Begin,
42 const MemRegion * const *End,
43 bool*);
Jordy Rose134a2362010-07-06 23:11:01 +000044
Jordy Rosed5d2e502010-07-08 23:57:29 +000045 typedef void (CStringChecker::*FnCheck)(CheckerContext &, const CallExpr *);
Jordy Rose134a2362010-07-06 23:11:01 +000046
Ted Kremenekdc891422010-12-01 21:57:22 +000047 void evalMemcpy(CheckerContext &C, const CallExpr *CE);
48 void evalMemmove(CheckerContext &C, const CallExpr *CE);
49 void evalBcopy(CheckerContext &C, const CallExpr *CE);
50 void evalCopyCommon(CheckerContext &C, const GRState *state,
Jordy Rosed5d2e502010-07-08 23:57:29 +000051 const Expr *Size, const Expr *Source, const Expr *Dest,
52 bool Restricted = false);
53
Ted Kremenekdc891422010-12-01 21:57:22 +000054 void evalMemcmp(CheckerContext &C, const CallExpr *CE);
Jordy Rose134a2362010-07-06 23:11:01 +000055
Ted Kremenek90af9092010-12-02 07:49:45 +000056 void evalstrLength(CheckerContext &C, const CallExpr *CE);
Jordy Roseb052e8f2010-07-27 01:37:31 +000057
Ted Kremenekdc891422010-12-01 21:57:22 +000058 void evalStrcpy(CheckerContext &C, const CallExpr *CE);
59 void evalStpcpy(CheckerContext &C, const CallExpr *CE);
Ted Kremenek90af9092010-12-02 07:49:45 +000060 void evalStrcpyCommon(CheckerContext &C, const CallExpr *CE, bool returnEnd);
Jordy Rose722f5582010-08-16 07:51:42 +000061
Jordy Rose134a2362010-07-06 23:11:01 +000062 // Utility methods
Jordy Rosed5d2e502010-07-08 23:57:29 +000063 std::pair<const GRState*, const GRState*>
Ted Kremenekc5bea1e2010-12-01 22:16:56 +000064 assumeZero(CheckerContext &C, const GRState *state, SVal V, QualType Ty);
Jordy Rosed5d2e502010-07-08 23:57:29 +000065
Ted Kremenek90af9092010-12-02 07:49:45 +000066 const GRState *setCStringLength(const GRState *state, const MemRegion *MR,
67 SVal strLength);
68 SVal getCStringLengthForRegion(CheckerContext &C, const GRState *&state,
Jordy Rose2a2e21c2010-08-14 21:02:52 +000069 const Expr *Ex, const MemRegion *MR);
Ted Kremenek90af9092010-12-02 07:49:45 +000070 SVal getCStringLength(CheckerContext &C, const GRState *&state,
Jordy Roseb052e8f2010-07-27 01:37:31 +000071 const Expr *Ex, SVal Buf);
72
Jordy Rose722f5582010-08-16 07:51:42 +000073 const GRState *InvalidateBuffer(CheckerContext &C, const GRState *state,
74 const Expr *Ex, SVal V);
75
Jordy Roseb052e8f2010-07-27 01:37:31 +000076 bool SummarizeRegion(llvm::raw_ostream& os, ASTContext& Ctx,
77 const MemRegion *MR);
78
79 // Re-usable checks
Ted Kremenek90af9092010-12-02 07:49:45 +000080 const GRState *checkNonNull(CheckerContext &C, const GRState *state,
Jordy Rosed5d2e502010-07-08 23:57:29 +000081 const Expr *S, SVal l);
Jordy Rose134a2362010-07-06 23:11:01 +000082 const GRState *CheckLocation(CheckerContext &C, const GRState *state,
Jordy Rose722f5582010-08-16 07:51:42 +000083 const Expr *S, SVal l,
84 bool IsDestination = false);
Jordy Rose134a2362010-07-06 23:11:01 +000085 const GRState *CheckBufferAccess(CheckerContext &C, const GRState *state,
86 const Expr *Size,
87 const Expr *FirstBuf,
Jordy Rose722f5582010-08-16 07:51:42 +000088 const Expr *SecondBuf = NULL,
89 bool FirstIsDestination = false);
Jordy Rose134a2362010-07-06 23:11:01 +000090 const GRState *CheckOverlap(CheckerContext &C, const GRState *state,
Jordy Rosed5d2e502010-07-08 23:57:29 +000091 const Expr *Size, const Expr *First,
92 const Expr *Second);
Ted Kremenek90af9092010-12-02 07:49:45 +000093 void emitOverlapBug(CheckerContext &C, const GRState *state,
Jordy Rose134a2362010-07-06 23:11:01 +000094 const Stmt *First, const Stmt *Second);
95};
Jordy Rose2a2e21c2010-08-14 21:02:52 +000096
97class CStringLength {
98public:
99 typedef llvm::ImmutableMap<const MemRegion *, SVal> EntryMap;
100};
Jordy Rose134a2362010-07-06 23:11:01 +0000101} //end anonymous namespace
102
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000103namespace clang {
Ted Kremenek98857c92010-12-23 07:20:52 +0000104namespace ento {
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000105 template <>
106 struct GRStateTrait<CStringLength>
107 : public GRStatePartialTrait<CStringLength::EntryMap> {
108 static void *GDMIndex() { return CStringChecker::getTag(); }
109 };
110}
Argyrios Kyrtzidisca08fba2010-12-22 18:53:20 +0000111}
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000112
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +0000113static void RegisterCStringChecker(ExprEngine &Eng) {
Jordy Rose134a2362010-07-06 23:11:01 +0000114 Eng.registerCheck(new CStringChecker());
115}
116
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +0000117void ento::registerCStringChecker(CheckerManager &mgr) {
118 mgr.addCheckerRegisterFunction(RegisterCStringChecker);
119}
120
Jordy Rosed5d2e502010-07-08 23:57:29 +0000121//===----------------------------------------------------------------------===//
122// Individual checks and utility methods.
123//===----------------------------------------------------------------------===//
124
125std::pair<const GRState*, const GRState*>
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000126CStringChecker::assumeZero(CheckerContext &C, const GRState *state, SVal V,
Jordy Rosed5d2e502010-07-08 23:57:29 +0000127 QualType Ty) {
Ted Kremenek90af9092010-12-02 07:49:45 +0000128 DefinedSVal *val = dyn_cast<DefinedSVal>(&V);
129 if (!val)
Jordy Rosed5d2e502010-07-08 23:57:29 +0000130 return std::pair<const GRState*, const GRState *>(state, state);
Jordy Rose33c829a2010-07-07 07:48:06 +0000131
Ted Kremenek90af9092010-12-02 07:49:45 +0000132 SValBuilder &svalBuilder = C.getSValBuilder();
133 DefinedOrUnknownSVal zero = svalBuilder.makeZeroVal(Ty);
134 return state->assume(svalBuilder.evalEQ(state, *val, zero));
Jordy Rosed5d2e502010-07-08 23:57:29 +0000135}
Jordy Rose33c829a2010-07-07 07:48:06 +0000136
Ted Kremenek90af9092010-12-02 07:49:45 +0000137const GRState *CStringChecker::checkNonNull(CheckerContext &C,
Jordy Rosed5d2e502010-07-08 23:57:29 +0000138 const GRState *state,
139 const Expr *S, SVal l) {
140 // If a previous check has failed, propagate the failure.
141 if (!state)
142 return NULL;
143
144 const GRState *stateNull, *stateNonNull;
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000145 llvm::tie(stateNull, stateNonNull) = assumeZero(C, state, l, S->getType());
Jordy Rosed5d2e502010-07-08 23:57:29 +0000146
147 if (stateNull && !stateNonNull) {
Ted Kremenek750b7ac2010-12-20 21:19:09 +0000148 ExplodedNode *N = C.generateSink(stateNull);
Jordy Rose33c829a2010-07-07 07:48:06 +0000149 if (!N)
150 return NULL;
151
Jordy Rosed5d2e502010-07-08 23:57:29 +0000152 if (!BT_Null)
153 BT_Null = new BuiltinBug("API",
Jordy Rose33c829a2010-07-07 07:48:06 +0000154 "Null pointer argument in call to byte string function");
155
156 // Generate a report for this bug.
Jordy Rosed5d2e502010-07-08 23:57:29 +0000157 BuiltinBug *BT = static_cast<BuiltinBug*>(BT_Null);
Jordy Rose33c829a2010-07-07 07:48:06 +0000158 EnhancedBugReport *report = new EnhancedBugReport(*BT,
159 BT->getDescription(), N);
160
161 report->addRange(S->getSourceRange());
162 report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, S);
163 C.EmitReport(report);
164 return NULL;
165 }
166
167 // From here on, assume that the value is non-null.
Jordy Rosed5d2e502010-07-08 23:57:29 +0000168 assert(stateNonNull);
169 return stateNonNull;
Jordy Rose33c829a2010-07-07 07:48:06 +0000170}
171
Jordy Rose134a2362010-07-06 23:11:01 +0000172// FIXME: This was originally copied from ArrayBoundChecker.cpp. Refactor?
173const GRState *CStringChecker::CheckLocation(CheckerContext &C,
174 const GRState *state,
Jordy Rose722f5582010-08-16 07:51:42 +0000175 const Expr *S, SVal l,
176 bool IsDestination) {
Jordy Rosed5d2e502010-07-08 23:57:29 +0000177 // If a previous check has failed, propagate the failure.
178 if (!state)
179 return NULL;
180
Jordy Rose134a2362010-07-06 23:11:01 +0000181 // Check for out of bound array element access.
182 const MemRegion *R = l.getAsRegion();
183 if (!R)
184 return state;
185
Jordy Rose134a2362010-07-06 23:11:01 +0000186 const ElementRegion *ER = dyn_cast<ElementRegion>(R);
187 if (!ER)
188 return state;
189
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +0000190 assert(ER->getValueType() == C.getASTContext().CharTy &&
Jordy Rose134a2362010-07-06 23:11:01 +0000191 "CheckLocation should only be called with char* ElementRegions");
192
193 // Get the size of the array.
Ted Kremenek90af9092010-12-02 07:49:45 +0000194 const SubRegion *superReg = cast<SubRegion>(ER->getSuperRegion());
195 SValBuilder &svalBuilder = C.getSValBuilder();
196 SVal Extent = svalBuilder.convertToArrayIndex(superReg->getExtent(svalBuilder));
Jordy Rose134a2362010-07-06 23:11:01 +0000197 DefinedOrUnknownSVal Size = cast<DefinedOrUnknownSVal>(Extent);
198
199 // Get the index of the accessed element.
Gabor Greif230ddf32010-09-09 10:51:37 +0000200 DefinedOrUnknownSVal Idx = cast<DefinedOrUnknownSVal>(ER->getIndex());
Jordy Rose134a2362010-07-06 23:11:01 +0000201
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000202 const GRState *StInBound = state->assumeInBound(Idx, Size, true);
203 const GRState *StOutBound = state->assumeInBound(Idx, Size, false);
Jordy Rose134a2362010-07-06 23:11:01 +0000204 if (StOutBound && !StInBound) {
Ted Kremenek750b7ac2010-12-20 21:19:09 +0000205 ExplodedNode *N = C.generateSink(StOutBound);
Jordy Rose134a2362010-07-06 23:11:01 +0000206 if (!N)
207 return NULL;
208
Jordy Rose722f5582010-08-16 07:51:42 +0000209 BuiltinBug *BT;
210 if (IsDestination) {
211 if (!BT_BoundsWrite) {
212 BT_BoundsWrite = new BuiltinBug("Out-of-bound array access",
213 "Byte string function overflows destination buffer");
214 }
215 BT = static_cast<BuiltinBug*>(BT_BoundsWrite);
216 } else {
217 if (!BT_Bounds) {
218 BT_Bounds = new BuiltinBug("Out-of-bound array access",
219 "Byte string function accesses out-of-bound array element");
220 }
221 BT = static_cast<BuiltinBug*>(BT_Bounds);
222 }
Jordy Rose134a2362010-07-06 23:11:01 +0000223
224 // FIXME: It would be nice to eventually make this diagnostic more clear,
225 // e.g., by referencing the original declaration or by saying *why* this
226 // reference is outside the range.
227
228 // Generate a report for this bug.
Jordy Rose134a2362010-07-06 23:11:01 +0000229 RangedBugReport *report = new RangedBugReport(*BT, BT->getDescription(), N);
230
231 report->addRange(S->getSourceRange());
232 C.EmitReport(report);
233 return NULL;
234 }
235
236 // Array bound check succeeded. From this point forward the array bound
237 // should always succeed.
238 return StInBound;
239}
240
241const GRState *CStringChecker::CheckBufferAccess(CheckerContext &C,
242 const GRState *state,
243 const Expr *Size,
244 const Expr *FirstBuf,
Jordy Rose722f5582010-08-16 07:51:42 +0000245 const Expr *SecondBuf,
246 bool FirstIsDestination) {
Jordy Rosed5d2e502010-07-08 23:57:29 +0000247 // If a previous check has failed, propagate the failure.
248 if (!state)
249 return NULL;
250
Ted Kremenek90af9092010-12-02 07:49:45 +0000251 SValBuilder &svalBuilder = C.getSValBuilder();
Jordy Rose134a2362010-07-06 23:11:01 +0000252 ASTContext &Ctx = C.getASTContext();
253
Ted Kremenek90af9092010-12-02 07:49:45 +0000254 QualType sizeTy = Size->getType();
Jordy Rose134a2362010-07-06 23:11:01 +0000255 QualType PtrTy = Ctx.getPointerType(Ctx.CharTy);
256
Jordy Rose33c829a2010-07-07 07:48:06 +0000257 // Check that the first buffer is non-null.
258 SVal BufVal = state->getSVal(FirstBuf);
Ted Kremenek90af9092010-12-02 07:49:45 +0000259 state = checkNonNull(C, state, FirstBuf, BufVal);
Jordy Rose33c829a2010-07-07 07:48:06 +0000260 if (!state)
261 return NULL;
262
Jordy Rosed5d2e502010-07-08 23:57:29 +0000263 // Get the access length and make sure it is known.
264 SVal LengthVal = state->getSVal(Size);
265 NonLoc *Length = dyn_cast<NonLoc>(&LengthVal);
266 if (!Length)
267 return state;
268
Jordy Rose134a2362010-07-06 23:11:01 +0000269 // Compute the offset of the last element to be accessed: size-1.
Ted Kremenek90af9092010-12-02 07:49:45 +0000270 NonLoc One = cast<NonLoc>(svalBuilder.makeIntVal(1, sizeTy));
271 NonLoc LastOffset = cast<NonLoc>(svalBuilder.evalBinOpNN(state, BO_Sub,
272 *Length, One, sizeTy));
Jordy Rose134a2362010-07-06 23:11:01 +0000273
Jordy Rose33c829a2010-07-07 07:48:06 +0000274 // Check that the first buffer is sufficently long.
Ted Kremenek90af9092010-12-02 07:49:45 +0000275 SVal BufStart = svalBuilder.evalCast(BufVal, PtrTy, FirstBuf->getType());
Jordy Roseafdb0532010-08-05 23:11:30 +0000276 if (Loc *BufLoc = dyn_cast<Loc>(&BufStart)) {
Ted Kremenek90af9092010-12-02 07:49:45 +0000277 SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc,
278 LastOffset, PtrTy);
Jordy Rose722f5582010-08-16 07:51:42 +0000279 state = CheckLocation(C, state, FirstBuf, BufEnd, FirstIsDestination);
Jordy Rose134a2362010-07-06 23:11:01 +0000280
Jordy Roseafdb0532010-08-05 23:11:30 +0000281 // If the buffer isn't large enough, abort.
282 if (!state)
283 return NULL;
284 }
Jordy Rose134a2362010-07-06 23:11:01 +0000285
286 // If there's a second buffer, check it as well.
287 if (SecondBuf) {
288 BufVal = state->getSVal(SecondBuf);
Ted Kremenek90af9092010-12-02 07:49:45 +0000289 state = checkNonNull(C, state, SecondBuf, BufVal);
Jordy Rose33c829a2010-07-07 07:48:06 +0000290 if (!state)
291 return NULL;
292
Ted Kremenek90af9092010-12-02 07:49:45 +0000293 BufStart = svalBuilder.evalCast(BufVal, PtrTy, SecondBuf->getType());
Jordy Roseafdb0532010-08-05 23:11:30 +0000294 if (Loc *BufLoc = dyn_cast<Loc>(&BufStart)) {
Ted Kremenek90af9092010-12-02 07:49:45 +0000295 SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc,
296 LastOffset, PtrTy);
Jordy Roseafdb0532010-08-05 23:11:30 +0000297 state = CheckLocation(C, state, SecondBuf, BufEnd);
298 }
Jordy Rose134a2362010-07-06 23:11:01 +0000299 }
300
301 // Large enough or not, return this state!
302 return state;
303}
304
305const GRState *CStringChecker::CheckOverlap(CheckerContext &C,
306 const GRState *state,
Jordy Rosed5d2e502010-07-08 23:57:29 +0000307 const Expr *Size,
Jordy Rose134a2362010-07-06 23:11:01 +0000308 const Expr *First,
Jordy Rosed5d2e502010-07-08 23:57:29 +0000309 const Expr *Second) {
Jordy Rose134a2362010-07-06 23:11:01 +0000310 // Do a simple check for overlap: if the two arguments are from the same
311 // buffer, see if the end of the first is greater than the start of the second
312 // or vice versa.
313
Jordy Rosed5d2e502010-07-08 23:57:29 +0000314 // If a previous check has failed, propagate the failure.
315 if (!state)
316 return NULL;
317
Jordy Rose134a2362010-07-06 23:11:01 +0000318 const GRState *stateTrue, *stateFalse;
319
320 // Get the buffer values and make sure they're known locations.
Ted Kremenek90af9092010-12-02 07:49:45 +0000321 SVal firstVal = state->getSVal(First);
322 SVal secondVal = state->getSVal(Second);
Jordy Rose134a2362010-07-06 23:11:01 +0000323
Ted Kremenek90af9092010-12-02 07:49:45 +0000324 Loc *firstLoc = dyn_cast<Loc>(&firstVal);
325 if (!firstLoc)
Jordy Rose134a2362010-07-06 23:11:01 +0000326 return state;
327
Ted Kremenek90af9092010-12-02 07:49:45 +0000328 Loc *secondLoc = dyn_cast<Loc>(&secondVal);
329 if (!secondLoc)
Jordy Rose134a2362010-07-06 23:11:01 +0000330 return state;
331
332 // Are the two values the same?
Ted Kremenek90af9092010-12-02 07:49:45 +0000333 SValBuilder &svalBuilder = C.getSValBuilder();
334 llvm::tie(stateTrue, stateFalse) =
335 state->assume(svalBuilder.evalEQ(state, *firstLoc, *secondLoc));
Jordy Rose134a2362010-07-06 23:11:01 +0000336
337 if (stateTrue && !stateFalse) {
338 // If the values are known to be equal, that's automatically an overlap.
Ted Kremenek90af9092010-12-02 07:49:45 +0000339 emitOverlapBug(C, stateTrue, First, Second);
Jordy Rose134a2362010-07-06 23:11:01 +0000340 return NULL;
341 }
342
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000343 // assume the two expressions are not equal.
Jordy Rose134a2362010-07-06 23:11:01 +0000344 assert(stateFalse);
345 state = stateFalse;
346
347 // Which value comes first?
Ted Kremenek90af9092010-12-02 07:49:45 +0000348 ASTContext &Ctx = svalBuilder.getContext();
349 QualType cmpTy = Ctx.IntTy;
350 SVal reverse = svalBuilder.evalBinOpLL(state, BO_GT,
351 *firstLoc, *secondLoc, cmpTy);
352 DefinedOrUnknownSVal *reverseTest = dyn_cast<DefinedOrUnknownSVal>(&reverse);
353 if (!reverseTest)
Jordy Rose134a2362010-07-06 23:11:01 +0000354 return state;
355
Ted Kremenek90af9092010-12-02 07:49:45 +0000356 llvm::tie(stateTrue, stateFalse) = state->assume(*reverseTest);
Jordy Rose134a2362010-07-06 23:11:01 +0000357 if (stateTrue) {
358 if (stateFalse) {
359 // If we don't know which one comes first, we can't perform this test.
360 return state;
361 } else {
Ted Kremenek90af9092010-12-02 07:49:45 +0000362 // Switch the values so that firstVal is before secondVal.
363 Loc *tmpLoc = firstLoc;
364 firstLoc = secondLoc;
365 secondLoc = tmpLoc;
Jordy Rose134a2362010-07-06 23:11:01 +0000366
367 // Switch the Exprs as well, so that they still correspond.
368 const Expr *tmpExpr = First;
369 First = Second;
370 Second = tmpExpr;
371 }
372 }
373
374 // Get the length, and make sure it too is known.
375 SVal LengthVal = state->getSVal(Size);
376 NonLoc *Length = dyn_cast<NonLoc>(&LengthVal);
377 if (!Length)
378 return state;
379
380 // Convert the first buffer's start address to char*.
381 // Bail out if the cast fails.
382 QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy);
Ted Kremenek90af9092010-12-02 07:49:45 +0000383 SVal FirstStart = svalBuilder.evalCast(*firstLoc, CharPtrTy, First->getType());
Jordy Rose134a2362010-07-06 23:11:01 +0000384 Loc *FirstStartLoc = dyn_cast<Loc>(&FirstStart);
385 if (!FirstStartLoc)
386 return state;
387
388 // Compute the end of the first buffer. Bail out if THAT fails.
Ted Kremenek90af9092010-12-02 07:49:45 +0000389 SVal FirstEnd = svalBuilder.evalBinOpLN(state, BO_Add,
Jordy Rose134a2362010-07-06 23:11:01 +0000390 *FirstStartLoc, *Length, CharPtrTy);
391 Loc *FirstEndLoc = dyn_cast<Loc>(&FirstEnd);
392 if (!FirstEndLoc)
393 return state;
394
395 // Is the end of the first buffer past the start of the second buffer?
Ted Kremenek90af9092010-12-02 07:49:45 +0000396 SVal Overlap = svalBuilder.evalBinOpLL(state, BO_GT,
397 *FirstEndLoc, *secondLoc, cmpTy);
Jordy Rose134a2362010-07-06 23:11:01 +0000398 DefinedOrUnknownSVal *OverlapTest = dyn_cast<DefinedOrUnknownSVal>(&Overlap);
399 if (!OverlapTest)
400 return state;
401
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000402 llvm::tie(stateTrue, stateFalse) = state->assume(*OverlapTest);
Jordy Rose134a2362010-07-06 23:11:01 +0000403
404 if (stateTrue && !stateFalse) {
405 // Overlap!
Ted Kremenek90af9092010-12-02 07:49:45 +0000406 emitOverlapBug(C, stateTrue, First, Second);
Jordy Rose134a2362010-07-06 23:11:01 +0000407 return NULL;
408 }
409
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000410 // assume the two expressions don't overlap.
Jordy Rose134a2362010-07-06 23:11:01 +0000411 assert(stateFalse);
412 return stateFalse;
413}
414
Ted Kremenek90af9092010-12-02 07:49:45 +0000415void CStringChecker::emitOverlapBug(CheckerContext &C, const GRState *state,
Jordy Rose134a2362010-07-06 23:11:01 +0000416 const Stmt *First, const Stmt *Second) {
Ted Kremenek750b7ac2010-12-20 21:19:09 +0000417 ExplodedNode *N = C.generateSink(state);
Jordy Rose134a2362010-07-06 23:11:01 +0000418 if (!N)
419 return;
420
421 if (!BT_Overlap)
422 BT_Overlap = new BugType("Unix API", "Improper arguments");
423
424 // Generate a report for this bug.
425 RangedBugReport *report =
426 new RangedBugReport(*BT_Overlap,
427 "Arguments must not be overlapping buffers", N);
428 report->addRange(First->getSourceRange());
429 report->addRange(Second->getSourceRange());
430
431 C.EmitReport(report);
432}
433
Ted Kremenek90af9092010-12-02 07:49:45 +0000434const GRState *CStringChecker::setCStringLength(const GRState *state,
Jordy Rose722f5582010-08-16 07:51:42 +0000435 const MemRegion *MR,
Ted Kremenek90af9092010-12-02 07:49:45 +0000436 SVal strLength) {
437 assert(!strLength.isUndef() && "Attempt to set an undefined string length");
438 if (strLength.isUnknown())
Jordy Rose722f5582010-08-16 07:51:42 +0000439 return state;
440
441 MR = MR->StripCasts();
442
443 switch (MR->getKind()) {
444 case MemRegion::StringRegionKind:
445 // FIXME: This can happen if we strcpy() into a string region. This is
446 // undefined [C99 6.4.5p6], but we should still warn about it.
447 return state;
448
449 case MemRegion::SymbolicRegionKind:
450 case MemRegion::AllocaRegionKind:
451 case MemRegion::VarRegionKind:
452 case MemRegion::FieldRegionKind:
453 case MemRegion::ObjCIvarRegionKind:
Ted Kremenek90af9092010-12-02 07:49:45 +0000454 return state->set<CStringLength>(MR, strLength);
Jordy Rose722f5582010-08-16 07:51:42 +0000455
456 case MemRegion::ElementRegionKind:
457 // FIXME: Handle element regions by upper-bounding the parent region's
458 // string length.
459 return state;
460
461 default:
462 // Other regions (mostly non-data) can't have a reliable C string length.
463 // For now, just ignore the change.
464 // FIXME: These are rare but not impossible. We should output some kind of
465 // warning for things like strcpy((char[]){'a', 0}, "b");
466 return state;
467 }
468}
469
Ted Kremenek90af9092010-12-02 07:49:45 +0000470SVal CStringChecker::getCStringLengthForRegion(CheckerContext &C,
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000471 const GRState *&state,
472 const Expr *Ex,
473 const MemRegion *MR) {
474 // If there's a recorded length, go ahead and return it.
475 const SVal *Recorded = state->get<CStringLength>(MR);
476 if (Recorded)
477 return *Recorded;
478
479 // Otherwise, get a new symbol and update the state.
480 unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
Ted Kremenek90af9092010-12-02 07:49:45 +0000481 SValBuilder &svalBuilder = C.getSValBuilder();
482 QualType sizeTy = svalBuilder.getContext().getSizeType();
483 SVal strLength = svalBuilder.getMetadataSymbolVal(getTag(), MR, Ex, sizeTy, Count);
484 state = state->set<CStringLength>(MR, strLength);
485 return strLength;
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000486}
487
Ted Kremenek90af9092010-12-02 07:49:45 +0000488SVal CStringChecker::getCStringLength(CheckerContext &C, const GRState *&state,
Jordy Roseb052e8f2010-07-27 01:37:31 +0000489 const Expr *Ex, SVal Buf) {
490 const MemRegion *MR = Buf.getAsRegion();
491 if (!MR) {
492 // If we can't get a region, see if it's something we /know/ isn't a
493 // C string. In the context of locations, the only time we can issue such
494 // a warning is for labels.
495 if (loc::GotoLabel *Label = dyn_cast<loc::GotoLabel>(&Buf)) {
Ted Kremenek750b7ac2010-12-20 21:19:09 +0000496 if (ExplodedNode *N = C.generateNode(state)) {
Jordy Roseb052e8f2010-07-27 01:37:31 +0000497 if (!BT_NotCString)
498 BT_NotCString = new BuiltinBug("API",
499 "Argument is not a null-terminated string.");
500
501 llvm::SmallString<120> buf;
502 llvm::raw_svector_ostream os(buf);
503 os << "Argument to byte string function is the address of the label '"
Chris Lattner5a9b1ec2011-02-17 05:38:27 +0000504 << Label->getLabel()->getName()
Jordy Roseb052e8f2010-07-27 01:37:31 +0000505 << "', which is not a null-terminated string";
506
507 // Generate a report for this bug.
508 EnhancedBugReport *report = new EnhancedBugReport(*BT_NotCString,
509 os.str(), N);
510
511 report->addRange(Ex->getSourceRange());
512 C.EmitReport(report);
513 }
514
515 return UndefinedVal();
516 }
517
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000518 // If it's not a region and not a label, give up.
519 return UnknownVal();
Jordy Roseb052e8f2010-07-27 01:37:31 +0000520 }
521
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000522 // If we have a region, strip casts from it and see if we can figure out
523 // its length. For anything we can't figure out, just return UnknownVal.
524 MR = MR->StripCasts();
525
526 switch (MR->getKind()) {
527 case MemRegion::StringRegionKind: {
528 // Modifying the contents of string regions is undefined [C99 6.4.5p6],
529 // so we can assume that the byte length is the correct C string length.
Ted Kremenek90af9092010-12-02 07:49:45 +0000530 SValBuilder &svalBuilder = C.getSValBuilder();
531 QualType sizeTy = svalBuilder.getContext().getSizeType();
532 const StringLiteral *strLit = cast<StringRegion>(MR)->getStringLiteral();
533 return svalBuilder.makeIntVal(strLit->getByteLength(), sizeTy);
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000534 }
535 case MemRegion::SymbolicRegionKind:
536 case MemRegion::AllocaRegionKind:
537 case MemRegion::VarRegionKind:
538 case MemRegion::FieldRegionKind:
539 case MemRegion::ObjCIvarRegionKind:
Ted Kremenek90af9092010-12-02 07:49:45 +0000540 return getCStringLengthForRegion(C, state, Ex, MR);
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000541 case MemRegion::CompoundLiteralRegionKind:
542 // FIXME: Can we track this? Is it necessary?
543 return UnknownVal();
544 case MemRegion::ElementRegionKind:
545 // FIXME: How can we handle this? It's not good enough to subtract the
546 // offset from the base string length; consider "123\x00567" and &a[5].
547 return UnknownVal();
548 default:
549 // Other regions (mostly non-data) can't have a reliable C string length.
550 // In this case, an error is emitted and UndefinedVal is returned.
551 // The caller should always be prepared to handle this case.
Ted Kremenek750b7ac2010-12-20 21:19:09 +0000552 if (ExplodedNode *N = C.generateNode(state)) {
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000553 if (!BT_NotCString)
554 BT_NotCString = new BuiltinBug("API",
555 "Argument is not a null-terminated string.");
556
557 llvm::SmallString<120> buf;
558 llvm::raw_svector_ostream os(buf);
559
560 os << "Argument to byte string function is ";
561
562 if (SummarizeRegion(os, C.getASTContext(), MR))
563 os << ", which is not a null-terminated string";
564 else
565 os << "not a null-terminated string";
566
567 // Generate a report for this bug.
568 EnhancedBugReport *report = new EnhancedBugReport(*BT_NotCString,
569 os.str(), N);
570
571 report->addRange(Ex->getSourceRange());
572 C.EmitReport(report);
573 }
574
575 return UndefinedVal();
576 }
Jordy Roseb052e8f2010-07-27 01:37:31 +0000577}
578
Jordy Rose722f5582010-08-16 07:51:42 +0000579const GRState *CStringChecker::InvalidateBuffer(CheckerContext &C,
580 const GRState *state,
581 const Expr *E, SVal V) {
582 Loc *L = dyn_cast<Loc>(&V);
583 if (!L)
584 return state;
585
586 // FIXME: This is a simplified version of what's in CFRefCount.cpp -- it makes
587 // some assumptions about the value that CFRefCount can't. Even so, it should
588 // probably be refactored.
589 if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(L)) {
590 const MemRegion *R = MR->getRegion()->StripCasts();
591
592 // Are we dealing with an ElementRegion? If so, we should be invalidating
593 // the super-region.
594 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
595 R = ER->getSuperRegion();
596 // FIXME: What about layers of ElementRegions?
597 }
598
599 // Invalidate this region.
600 unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
Ted Kremenekeddeba02011-02-11 19:48:15 +0000601 return state->invalidateRegion(R, E, Count, NULL);
Jordy Rose722f5582010-08-16 07:51:42 +0000602 }
603
604 // If we have a non-region value by chance, just remove the binding.
605 // FIXME: is this necessary or correct? This handles the non-Region
606 // cases. Is it ever valid to store to these?
607 return state->unbindLoc(*L);
608}
609
Jordy Roseb052e8f2010-07-27 01:37:31 +0000610bool CStringChecker::SummarizeRegion(llvm::raw_ostream& os, ASTContext& Ctx,
611 const MemRegion *MR) {
612 const TypedRegion *TR = dyn_cast<TypedRegion>(MR);
613 if (!TR)
614 return false;
615
616 switch (TR->getKind()) {
617 case MemRegion::FunctionTextRegionKind: {
618 const FunctionDecl *FD = cast<FunctionTextRegion>(TR)->getDecl();
619 if (FD)
620 os << "the address of the function '" << FD << "'";
621 else
622 os << "the address of a function";
623 return true;
624 }
625 case MemRegion::BlockTextRegionKind:
626 os << "block text";
627 return true;
628 case MemRegion::BlockDataRegionKind:
629 os << "a block";
630 return true;
631 case MemRegion::CXXThisRegionKind:
Zhongxing Xu03207162010-11-26 08:52:48 +0000632 case MemRegion::CXXTempObjectRegionKind:
633 os << "a C++ temp object of type " << TR->getValueType().getAsString();
Jordy Roseb052e8f2010-07-27 01:37:31 +0000634 return true;
635 case MemRegion::VarRegionKind:
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +0000636 os << "a variable of type" << TR->getValueType().getAsString();
Jordy Roseb052e8f2010-07-27 01:37:31 +0000637 return true;
638 case MemRegion::FieldRegionKind:
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +0000639 os << "a field of type " << TR->getValueType().getAsString();
Jordy Roseb052e8f2010-07-27 01:37:31 +0000640 return true;
641 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +0000642 os << "an instance variable of type " << TR->getValueType().getAsString();
Jordy Roseb052e8f2010-07-27 01:37:31 +0000643 return true;
644 default:
645 return false;
646 }
647}
648
Jordy Rosed5d2e502010-07-08 23:57:29 +0000649//===----------------------------------------------------------------------===//
Ted Kremenekdc891422010-12-01 21:57:22 +0000650// evaluation of individual function calls.
Jordy Rosed5d2e502010-07-08 23:57:29 +0000651//===----------------------------------------------------------------------===//
652
Ted Kremenekdc891422010-12-01 21:57:22 +0000653void CStringChecker::evalCopyCommon(CheckerContext &C, const GRState *state,
Jordy Rosed5d2e502010-07-08 23:57:29 +0000654 const Expr *Size, const Expr *Dest,
655 const Expr *Source, bool Restricted) {
656 // See if the size argument is zero.
Ted Kremenek90af9092010-12-02 07:49:45 +0000657 SVal sizeVal = state->getSVal(Size);
658 QualType sizeTy = Size->getType();
Jordy Rosed5d2e502010-07-08 23:57:29 +0000659
Ted Kremenek90af9092010-12-02 07:49:45 +0000660 const GRState *stateZeroSize, *stateNonZeroSize;
661 llvm::tie(stateZeroSize, stateNonZeroSize) = assumeZero(C, state, sizeVal, sizeTy);
Jordy Rosed5d2e502010-07-08 23:57:29 +0000662
663 // If the size is zero, there won't be any actual memory access.
Ted Kremenek90af9092010-12-02 07:49:45 +0000664 if (stateZeroSize)
665 C.addTransition(stateZeroSize);
Jordy Rosed5d2e502010-07-08 23:57:29 +0000666
667 // If the size can be nonzero, we have to check the other arguments.
Ted Kremenek90af9092010-12-02 07:49:45 +0000668 if (stateNonZeroSize) {
669 state = stateNonZeroSize;
Jordy Rose722f5582010-08-16 07:51:42 +0000670 state = CheckBufferAccess(C, state, Size, Dest, Source,
671 /* FirstIsDst = */ true);
Jordy Rosed5d2e502010-07-08 23:57:29 +0000672 if (Restricted)
673 state = CheckOverlap(C, state, Size, Dest, Source);
Jordy Rose722f5582010-08-16 07:51:42 +0000674
675 if (state) {
676 // Invalidate the destination.
677 // FIXME: Even if we can't perfectly model the copy, we should see if we
678 // can use LazyCompoundVals to copy the source values into the destination.
679 // This would probably remove any existing bindings past the end of the
680 // copied region, but that's still an improvement over blank invalidation.
681 state = InvalidateBuffer(C, state, Dest, state->getSVal(Dest));
Jordy Rosed5d2e502010-07-08 23:57:29 +0000682 C.addTransition(state);
Jordy Rose722f5582010-08-16 07:51:42 +0000683 }
Jordy Rosed5d2e502010-07-08 23:57:29 +0000684 }
685}
686
687
Ted Kremenekdc891422010-12-01 21:57:22 +0000688void CStringChecker::evalMemcpy(CheckerContext &C, const CallExpr *CE) {
Jordy Rose134a2362010-07-06 23:11:01 +0000689 // void *memcpy(void *restrict dst, const void *restrict src, size_t n);
Jordy Rose134a2362010-07-06 23:11:01 +0000690 // The return value is the address of the destination buffer.
Jordy Rosed5d2e502010-07-08 23:57:29 +0000691 const Expr *Dest = CE->getArg(0);
692 const GRState *state = C.getState();
693 state = state->BindExpr(CE, state->getSVal(Dest));
Ted Kremenekdc891422010-12-01 21:57:22 +0000694 evalCopyCommon(C, state, CE->getArg(2), Dest, CE->getArg(1), true);
Jordy Rose134a2362010-07-06 23:11:01 +0000695}
696
Ted Kremenekdc891422010-12-01 21:57:22 +0000697void CStringChecker::evalMemmove(CheckerContext &C, const CallExpr *CE) {
Jordy Rosed5d2e502010-07-08 23:57:29 +0000698 // void *memmove(void *dst, const void *src, size_t n);
699 // The return value is the address of the destination buffer.
700 const Expr *Dest = CE->getArg(0);
701 const GRState *state = C.getState();
702 state = state->BindExpr(CE, state->getSVal(Dest));
Ted Kremenekdc891422010-12-01 21:57:22 +0000703 evalCopyCommon(C, state, CE->getArg(2), Dest, CE->getArg(1));
Jordy Rosed5d2e502010-07-08 23:57:29 +0000704}
705
Ted Kremenekdc891422010-12-01 21:57:22 +0000706void CStringChecker::evalBcopy(CheckerContext &C, const CallExpr *CE) {
Jordy Rosed5d2e502010-07-08 23:57:29 +0000707 // void bcopy(const void *src, void *dst, size_t n);
Ted Kremenekdc891422010-12-01 21:57:22 +0000708 evalCopyCommon(C, C.getState(), CE->getArg(2), CE->getArg(1), CE->getArg(0));
Jordy Rosed5d2e502010-07-08 23:57:29 +0000709}
710
Ted Kremenekdc891422010-12-01 21:57:22 +0000711void CStringChecker::evalMemcmp(CheckerContext &C, const CallExpr *CE) {
Jordy Rose65136fb2010-07-07 08:15:01 +0000712 // int memcmp(const void *s1, const void *s2, size_t n);
713 const Expr *Left = CE->getArg(0);
714 const Expr *Right = CE->getArg(1);
715 const Expr *Size = CE->getArg(2);
716
717 const GRState *state = C.getState();
Ted Kremenek90af9092010-12-02 07:49:45 +0000718 SValBuilder &svalBuilder = C.getSValBuilder();
Jordy Rose65136fb2010-07-07 08:15:01 +0000719
Jordy Rosed5d2e502010-07-08 23:57:29 +0000720 // See if the size argument is zero.
Ted Kremenek90af9092010-12-02 07:49:45 +0000721 SVal sizeVal = state->getSVal(Size);
722 QualType sizeTy = Size->getType();
Jordy Rose65136fb2010-07-07 08:15:01 +0000723
Ted Kremenek90af9092010-12-02 07:49:45 +0000724 const GRState *stateZeroSize, *stateNonZeroSize;
725 llvm::tie(stateZeroSize, stateNonZeroSize) =
726 assumeZero(C, state, sizeVal, sizeTy);
Jordy Rose65136fb2010-07-07 08:15:01 +0000727
Jordy Rosed5d2e502010-07-08 23:57:29 +0000728 // If the size can be zero, the result will be 0 in that case, and we don't
729 // have to check either of the buffers.
Ted Kremenek90af9092010-12-02 07:49:45 +0000730 if (stateZeroSize) {
731 state = stateZeroSize;
732 state = state->BindExpr(CE, svalBuilder.makeZeroVal(CE->getType()));
Jordy Rosed5d2e502010-07-08 23:57:29 +0000733 C.addTransition(state);
Jordy Rose65136fb2010-07-07 08:15:01 +0000734 }
735
Jordy Rosed5d2e502010-07-08 23:57:29 +0000736 // If the size can be nonzero, we have to check the other arguments.
Ted Kremenek90af9092010-12-02 07:49:45 +0000737 if (stateNonZeroSize) {
738 state = stateNonZeroSize;
Jordy Rosed5d2e502010-07-08 23:57:29 +0000739 // If we know the two buffers are the same, we know the result is 0.
740 // First, get the two buffers' addresses. Another checker will have already
741 // made sure they're not undefined.
742 DefinedOrUnknownSVal LV = cast<DefinedOrUnknownSVal>(state->getSVal(Left));
743 DefinedOrUnknownSVal RV = cast<DefinedOrUnknownSVal>(state->getSVal(Right));
Jordy Rose65136fb2010-07-07 08:15:01 +0000744
Jordy Rosed5d2e502010-07-08 23:57:29 +0000745 // See if they are the same.
Ted Kremenek90af9092010-12-02 07:49:45 +0000746 DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV);
Jordy Rosed5d2e502010-07-08 23:57:29 +0000747 const GRState *StSameBuf, *StNotSameBuf;
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000748 llvm::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf);
Jordy Rosed5d2e502010-07-08 23:57:29 +0000749
750 // If the two arguments might be the same buffer, we know the result is zero,
751 // and we only need to check one size.
752 if (StSameBuf) {
753 state = StSameBuf;
754 state = CheckBufferAccess(C, state, Size, Left);
755 if (state) {
Ted Kremenek90af9092010-12-02 07:49:45 +0000756 state = StSameBuf->BindExpr(CE, svalBuilder.makeZeroVal(CE->getType()));
Jordy Rosed5d2e502010-07-08 23:57:29 +0000757 C.addTransition(state);
758 }
759 }
760
761 // If the two arguments might be different buffers, we have to check the
762 // size of both of them.
763 if (StNotSameBuf) {
764 state = StNotSameBuf;
765 state = CheckBufferAccess(C, state, Size, Left, Right);
766 if (state) {
767 // The return value is the comparison result, which we don't know.
768 unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
Ted Kremenek90af9092010-12-02 07:49:45 +0000769 SVal CmpV = svalBuilder.getConjuredSymbolVal(NULL, CE, Count);
Jordy Rosed5d2e502010-07-08 23:57:29 +0000770 state = state->BindExpr(CE, CmpV);
771 C.addTransition(state);
772 }
773 }
774 }
Jordy Rose65136fb2010-07-07 08:15:01 +0000775}
776
Ted Kremenek90af9092010-12-02 07:49:45 +0000777void CStringChecker::evalstrLength(CheckerContext &C, const CallExpr *CE) {
Jordy Roseb052e8f2010-07-27 01:37:31 +0000778 // size_t strlen(const char *s);
779 const GRState *state = C.getState();
780 const Expr *Arg = CE->getArg(0);
781 SVal ArgVal = state->getSVal(Arg);
782
783 // Check that the argument is non-null.
Ted Kremenek90af9092010-12-02 07:49:45 +0000784 state = checkNonNull(C, state, Arg, ArgVal);
Jordy Roseb052e8f2010-07-27 01:37:31 +0000785
786 if (state) {
Ted Kremenek90af9092010-12-02 07:49:45 +0000787 SVal strLength = getCStringLength(C, state, Arg, ArgVal);
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000788
789 // If the argument isn't a valid C string, there's no valid state to
790 // transition to.
Ted Kremenek90af9092010-12-02 07:49:45 +0000791 if (strLength.isUndef())
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000792 return;
793
Ted Kremenek90af9092010-12-02 07:49:45 +0000794 // If getCStringLength couldn't figure out the length, conjure a return
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000795 // value, so it can be used in constraints, at least.
Ted Kremenek90af9092010-12-02 07:49:45 +0000796 if (strLength.isUnknown()) {
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000797 unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
Ted Kremenek90af9092010-12-02 07:49:45 +0000798 strLength = C.getSValBuilder().getConjuredSymbolVal(NULL, CE, Count);
Jordy Roseb052e8f2010-07-27 01:37:31 +0000799 }
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000800
801 // Bind the return value.
Ted Kremenek90af9092010-12-02 07:49:45 +0000802 state = state->BindExpr(CE, strLength);
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000803 C.addTransition(state);
Jordy Roseb052e8f2010-07-27 01:37:31 +0000804 }
805}
806
Ted Kremenekdc891422010-12-01 21:57:22 +0000807void CStringChecker::evalStrcpy(CheckerContext &C, const CallExpr *CE) {
Jordy Rose722f5582010-08-16 07:51:42 +0000808 // char *strcpy(char *restrict dst, const char *restrict src);
Ted Kremenek90af9092010-12-02 07:49:45 +0000809 evalStrcpyCommon(C, CE, /* returnEnd = */ false);
Jordy Rose722f5582010-08-16 07:51:42 +0000810}
811
Ted Kremenekdc891422010-12-01 21:57:22 +0000812void CStringChecker::evalStpcpy(CheckerContext &C, const CallExpr *CE) {
Jordy Rose722f5582010-08-16 07:51:42 +0000813 // char *stpcpy(char *restrict dst, const char *restrict src);
Ted Kremenek90af9092010-12-02 07:49:45 +0000814 evalStrcpyCommon(C, CE, /* returnEnd = */ true);
Jordy Rose722f5582010-08-16 07:51:42 +0000815}
816
Ted Kremenekdc891422010-12-01 21:57:22 +0000817void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE,
Ted Kremenek90af9092010-12-02 07:49:45 +0000818 bool returnEnd) {
Jordy Rose722f5582010-08-16 07:51:42 +0000819 const GRState *state = C.getState();
820
821 // Check that the destination is non-null
822 const Expr *Dst = CE->getArg(0);
823 SVal DstVal = state->getSVal(Dst);
824
Ted Kremenek90af9092010-12-02 07:49:45 +0000825 state = checkNonNull(C, state, Dst, DstVal);
Jordy Rose722f5582010-08-16 07:51:42 +0000826 if (!state)
827 return;
828
829 // Check that the source is non-null.
Ted Kremenek90af9092010-12-02 07:49:45 +0000830 const Expr *srcExpr = CE->getArg(1);
831 SVal srcVal = state->getSVal(srcExpr);
832 state = checkNonNull(C, state, srcExpr, srcVal);
Jordy Rose722f5582010-08-16 07:51:42 +0000833 if (!state)
834 return;
835
836 // Get the string length of the source.
Ted Kremenek90af9092010-12-02 07:49:45 +0000837 SVal strLength = getCStringLength(C, state, srcExpr, srcVal);
Jordy Rose722f5582010-08-16 07:51:42 +0000838
839 // If the source isn't a valid C string, give up.
Ted Kremenek90af9092010-12-02 07:49:45 +0000840 if (strLength.isUndef())
Jordy Rose722f5582010-08-16 07:51:42 +0000841 return;
842
Ted Kremenek90af9092010-12-02 07:49:45 +0000843 SVal Result = (returnEnd ? UnknownVal() : DstVal);
Jordy Rose722f5582010-08-16 07:51:42 +0000844
845 // If the destination is a MemRegion, try to check for a buffer overflow and
846 // record the new string length.
Ted Kremenek90af9092010-12-02 07:49:45 +0000847 if (loc::MemRegionVal *dstRegVal = dyn_cast<loc::MemRegionVal>(&DstVal)) {
Jordy Rose722f5582010-08-16 07:51:42 +0000848 // If the length is known, we can check for an overflow.
Ted Kremenek90af9092010-12-02 07:49:45 +0000849 if (NonLoc *knownStrLength = dyn_cast<NonLoc>(&strLength)) {
850 SVal lastElement =
851 C.getSValBuilder().evalBinOpLN(state, BO_Add, *dstRegVal,
852 *knownStrLength, Dst->getType());
Jordy Rose722f5582010-08-16 07:51:42 +0000853
Ted Kremenek90af9092010-12-02 07:49:45 +0000854 state = CheckLocation(C, state, Dst, lastElement, /* IsDst = */ true);
Jordy Rose722f5582010-08-16 07:51:42 +0000855 if (!state)
856 return;
857
858 // If this is a stpcpy-style copy, the last element is the return value.
Ted Kremenek90af9092010-12-02 07:49:45 +0000859 if (returnEnd)
860 Result = lastElement;
Jordy Rose722f5582010-08-16 07:51:42 +0000861 }
862
863 // Invalidate the destination. This must happen before we set the C string
864 // length because invalidation will clear the length.
865 // FIXME: Even if we can't perfectly model the copy, we should see if we
866 // can use LazyCompoundVals to copy the source values into the destination.
867 // This would probably remove any existing bindings past the end of the
868 // string, but that's still an improvement over blank invalidation.
Ted Kremenek90af9092010-12-02 07:49:45 +0000869 state = InvalidateBuffer(C, state, Dst, *dstRegVal);
Jordy Rose722f5582010-08-16 07:51:42 +0000870
871 // Set the C string length of the destination.
Ted Kremenek90af9092010-12-02 07:49:45 +0000872 state = setCStringLength(state, dstRegVal->getRegion(), strLength);
Jordy Rose722f5582010-08-16 07:51:42 +0000873 }
874
875 // If this is a stpcpy-style copy, but we were unable to check for a buffer
876 // overflow, we still need a result. Conjure a return value.
Ted Kremenek90af9092010-12-02 07:49:45 +0000877 if (returnEnd && Result.isUnknown()) {
878 SValBuilder &svalBuilder = C.getSValBuilder();
Jordy Rose722f5582010-08-16 07:51:42 +0000879 unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
Ted Kremenek90af9092010-12-02 07:49:45 +0000880 strLength = svalBuilder.getConjuredSymbolVal(NULL, CE, Count);
Jordy Rose722f5582010-08-16 07:51:42 +0000881 }
882
883 // Set the return value.
884 state = state->BindExpr(CE, Result);
885 C.addTransition(state);
886}
887
Jordy Rosed5d2e502010-07-08 23:57:29 +0000888//===----------------------------------------------------------------------===//
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000889// The driver method, and other Checker callbacks.
Jordy Rosed5d2e502010-07-08 23:57:29 +0000890//===----------------------------------------------------------------------===//
Jordy Rose134a2362010-07-06 23:11:01 +0000891
Ted Kremenekdc891422010-12-01 21:57:22 +0000892bool CStringChecker::evalCallExpr(CheckerContext &C, const CallExpr *CE) {
Jordy Rose134a2362010-07-06 23:11:01 +0000893 // Get the callee. All the functions we care about are C functions
894 // with simple identifiers.
895 const GRState *state = C.getState();
896 const Expr *Callee = CE->getCallee();
897 const FunctionDecl *FD = state->getSVal(Callee).getAsFunctionDecl();
898
899 if (!FD)
900 return false;
901
902 // Get the name of the callee. If it's a builtin, strip off the prefix.
Douglas Gregor4b8eca82010-11-01 23:16:05 +0000903 IdentifierInfo *II = FD->getIdentifier();
904 if (!II) // if no identifier, not a simple C function
905 return false;
906 llvm::StringRef Name = II->getName();
Jordy Rose134a2362010-07-06 23:11:01 +0000907 if (Name.startswith("__builtin_"))
908 Name = Name.substr(10);
909
Ted Kremenekdc891422010-12-01 21:57:22 +0000910 FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
911 .Cases("memcpy", "__memcpy_chk", &CStringChecker::evalMemcpy)
912 .Cases("memcmp", "bcmp", &CStringChecker::evalMemcmp)
913 .Cases("memmove", "__memmove_chk", &CStringChecker::evalMemmove)
914 .Cases("strcpy", "__strcpy_chk", &CStringChecker::evalStrcpy)
915 .Cases("stpcpy", "__stpcpy_chk", &CStringChecker::evalStpcpy)
Ted Kremenek90af9092010-12-02 07:49:45 +0000916 .Case("strlen", &CStringChecker::evalstrLength)
Ted Kremenekdc891422010-12-01 21:57:22 +0000917 .Case("bcopy", &CStringChecker::evalBcopy)
Jordy Rose134a2362010-07-06 23:11:01 +0000918 .Default(NULL);
919
Jordy Rosed5d2e502010-07-08 23:57:29 +0000920 // If the callee isn't a string function, let another checker handle it.
Ted Kremenekdc891422010-12-01 21:57:22 +0000921 if (!evalFunction)
Jordy Rose134a2362010-07-06 23:11:01 +0000922 return false;
923
Jordy Rosed5d2e502010-07-08 23:57:29 +0000924 // Check and evaluate the call.
Ted Kremenekdc891422010-12-01 21:57:22 +0000925 (this->*evalFunction)(C, CE);
Jordy Rose134a2362010-07-06 23:11:01 +0000926 return true;
927}
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000928
929void CStringChecker::PreVisitDeclStmt(CheckerContext &C, const DeclStmt *DS) {
930 // Record string length for char a[] = "abc";
931 const GRState *state = C.getState();
932
933 for (DeclStmt::const_decl_iterator I = DS->decl_begin(), E = DS->decl_end();
934 I != E; ++I) {
935 const VarDecl *D = dyn_cast<VarDecl>(*I);
936 if (!D)
937 continue;
938
939 // FIXME: Handle array fields of structs.
940 if (!D->getType()->isArrayType())
941 continue;
942
943 const Expr *Init = D->getInit();
944 if (!Init)
945 continue;
946 if (!isa<StringLiteral>(Init))
947 continue;
948
949 Loc VarLoc = state->getLValue(D, C.getPredecessor()->getLocationContext());
950 const MemRegion *MR = VarLoc.getAsRegion();
951 if (!MR)
952 continue;
953
954 SVal StrVal = state->getSVal(Init);
955 assert(StrVal.isValid() && "Initializer string is unknown or undefined");
Ted Kremenek90af9092010-12-02 07:49:45 +0000956 DefinedOrUnknownSVal strLength
957 = cast<DefinedOrUnknownSVal>(getCStringLength(C, state, Init, StrVal));
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000958
Ted Kremenek90af9092010-12-02 07:49:45 +0000959 state = state->set<CStringLength>(MR, strLength);
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000960 }
961
962 C.addTransition(state);
963}
964
Ted Kremenek926c9622011-01-11 02:34:45 +0000965bool CStringChecker::wantsRegionChangeUpdate(const GRState *state) {
Jordy Rose2a2e21c2010-08-14 21:02:52 +0000966 CStringLength::EntryMap Entries = state->get<CStringLength>();
967 return !Entries.isEmpty();
968}
969
970const GRState *CStringChecker::EvalRegionChanges(const GRState *state,
971 const MemRegion * const *Begin,
972 const MemRegion * const *End,
973 bool *) {
974 CStringLength::EntryMap Entries = state->get<CStringLength>();
975 if (Entries.isEmpty())
976 return state;
977
978 llvm::SmallPtrSet<const MemRegion *, 8> Invalidated;
979 llvm::SmallPtrSet<const MemRegion *, 32> SuperRegions;
980
981 // First build sets for the changed regions and their super-regions.
982 for ( ; Begin != End; ++Begin) {
983 const MemRegion *MR = *Begin;
984 Invalidated.insert(MR);
985
986 SuperRegions.insert(MR);
987 while (const SubRegion *SR = dyn_cast<SubRegion>(MR)) {
988 MR = SR->getSuperRegion();
989 SuperRegions.insert(MR);
990 }
991 }
992
993 CStringLength::EntryMap::Factory &F = state->get_context<CStringLength>();
994
995 // Then loop over the entries in the current state.
996 for (CStringLength::EntryMap::iterator I = Entries.begin(),
997 E = Entries.end(); I != E; ++I) {
998 const MemRegion *MR = I.getKey();
999
1000 // Is this entry for a super-region of a changed region?
1001 if (SuperRegions.count(MR)) {
Ted Kremenekb3b56c62010-11-24 00:54:37 +00001002 Entries = F.remove(Entries, MR);
Jordy Rose2a2e21c2010-08-14 21:02:52 +00001003 continue;
1004 }
1005
1006 // Is this entry for a sub-region of a changed region?
1007 const MemRegion *Super = MR;
1008 while (const SubRegion *SR = dyn_cast<SubRegion>(Super)) {
1009 Super = SR->getSuperRegion();
1010 if (Invalidated.count(Super)) {
Ted Kremenekb3b56c62010-11-24 00:54:37 +00001011 Entries = F.remove(Entries, MR);
Jordy Rose2a2e21c2010-08-14 21:02:52 +00001012 break;
1013 }
1014 }
1015 }
1016
1017 return state->set<CStringLength>(Entries);
1018}
1019
1020void CStringChecker::MarkLiveSymbols(const GRState *state, SymbolReaper &SR) {
1021 // Mark all symbols in our string length map as valid.
1022 CStringLength::EntryMap Entries = state->get<CStringLength>();
1023
1024 for (CStringLength::EntryMap::iterator I = Entries.begin(), E = Entries.end();
1025 I != E; ++I) {
1026 SVal Len = I.getData();
1027 if (SymbolRef Sym = Len.getAsSymbol())
1028 SR.markInUse(Sym);
1029 }
1030}
1031
Ted Kremenekdc891422010-12-01 21:57:22 +00001032void CStringChecker::evalDeadSymbols(CheckerContext &C, SymbolReaper &SR) {
Jordy Rose2a2e21c2010-08-14 21:02:52 +00001033 if (!SR.hasDeadSymbols())
1034 return;
1035
1036 const GRState *state = C.getState();
1037 CStringLength::EntryMap Entries = state->get<CStringLength>();
1038 if (Entries.isEmpty())
1039 return;
1040
1041 CStringLength::EntryMap::Factory &F = state->get_context<CStringLength>();
1042 for (CStringLength::EntryMap::iterator I = Entries.begin(), E = Entries.end();
1043 I != E; ++I) {
1044 SVal Len = I.getData();
1045 if (SymbolRef Sym = Len.getAsSymbol()) {
1046 if (SR.isDead(Sym))
Ted Kremenekb3b56c62010-11-24 00:54:37 +00001047 Entries = F.remove(Entries, I.getKey());
Jordy Rose2a2e21c2010-08-14 21:02:52 +00001048 }
1049 }
1050
1051 state = state->set<CStringLength>(Entries);
Ted Kremenek750b7ac2010-12-20 21:19:09 +00001052 C.generateNode(state);
Jordy Rose2a2e21c2010-08-14 21:02:52 +00001053}