1 //= CStringChecker.cpp - Checks calls to C string functions --------*- C++ -*-//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This defines CStringChecker, which is an assortment of checks on calls
10 // to functions in <string.h>.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "InterCheckerAPI.h"
15 #include "clang/Basic/CharInfo.h"
16 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
17 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
18 #include "clang/StaticAnalyzer/Core/Checker.h"
19 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicExtent.h"
24 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include "llvm/Support/raw_ostream.h"
29 
30 using namespace clang;
31 using namespace ento;
32 
33 namespace {
34 struct AnyArgExpr {
35   // FIXME: Remove constructor in C++17 to turn it into an aggregate.
36   AnyArgExpr(const Expr *Expression, unsigned ArgumentIndex)
37       : Expression{Expression}, ArgumentIndex{ArgumentIndex} {}
38   const Expr *Expression;
39   unsigned ArgumentIndex;
40 };
41 
42 struct SourceArgExpr : AnyArgExpr {
43   using AnyArgExpr::AnyArgExpr; // FIXME: Remove using in C++17.
44 };
45 
46 struct DestinationArgExpr : AnyArgExpr {
47   using AnyArgExpr::AnyArgExpr; // FIXME: Same.
48 };
49 
50 struct SizeArgExpr : AnyArgExpr {
51   using AnyArgExpr::AnyArgExpr; // FIXME: Same.
52 };
53 
54 using ErrorMessage = SmallString<128>;
55 enum class AccessKind { write, read };
56 
57 static ErrorMessage createOutOfBoundErrorMsg(StringRef FunctionDescription,
58                                              AccessKind Access) {
59   ErrorMessage Message;
60   llvm::raw_svector_ostream Os(Message);
61 
62   // Function classification like: Memory copy function
63   Os << toUppercase(FunctionDescription.front())
64      << &FunctionDescription.data()[1];
65 
66   if (Access == AccessKind::write) {
67     Os << " overflows the destination buffer";
68   } else { // read access
69     Os << " accesses out-of-bound array element";
70   }
71 
72   return Message;
73 }
74 
75 enum class ConcatFnKind { none = 0, strcat = 1, strlcat = 2 };
76 class CStringChecker : public Checker< eval::Call,
77                                          check::PreStmt<DeclStmt>,
78                                          check::LiveSymbols,
79                                          check::DeadSymbols,
80                                          check::RegionChanges
81                                          > {
82   mutable std::unique_ptr<BugType> BT_Null, BT_Bounds, BT_Overlap,
83       BT_NotCString, BT_AdditionOverflow, BT_UninitRead;
84 
85   mutable const char *CurrentFunctionDescription;
86 
87 public:
88   /// The filter is used to filter out the diagnostics which are not enabled by
89   /// the user.
90   struct CStringChecksFilter {
91     DefaultBool CheckCStringNullArg;
92     DefaultBool CheckCStringOutOfBounds;
93     DefaultBool CheckCStringBufferOverlap;
94     DefaultBool CheckCStringNotNullTerm;
95     DefaultBool CheckCStringUninitializedRead;
96 
97     CheckerNameRef CheckNameCStringNullArg;
98     CheckerNameRef CheckNameCStringOutOfBounds;
99     CheckerNameRef CheckNameCStringBufferOverlap;
100     CheckerNameRef CheckNameCStringNotNullTerm;
101     CheckerNameRef CheckNameCStringUninitializedRead;
102   };
103 
104   CStringChecksFilter Filter;
105 
106   static void *getTag() { static int tag; return &tag; }
107 
108   bool evalCall(const CallEvent &Call, CheckerContext &C) const;
109   void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const;
110   void checkLiveSymbols(ProgramStateRef state, SymbolReaper &SR) const;
111   void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
112 
113   ProgramStateRef
114     checkRegionChanges(ProgramStateRef state,
115                        const InvalidatedSymbols *,
116                        ArrayRef<const MemRegion *> ExplicitRegions,
117                        ArrayRef<const MemRegion *> Regions,
118                        const LocationContext *LCtx,
119                        const CallEvent *Call) const;
120 
121   typedef void (CStringChecker::*FnCheck)(CheckerContext &,
122                                           const CallExpr *) const;
123   CallDescriptionMap<FnCheck> Callbacks = {
124       {{CDF_MaybeBuiltin, "memcpy", 3}, &CStringChecker::evalMemcpy},
125       {{CDF_MaybeBuiltin, "mempcpy", 3}, &CStringChecker::evalMempcpy},
126       {{CDF_MaybeBuiltin, "memcmp", 3}, &CStringChecker::evalMemcmp},
127       {{CDF_MaybeBuiltin, "memmove", 3}, &CStringChecker::evalMemmove},
128       {{CDF_MaybeBuiltin, "memset", 3}, &CStringChecker::evalMemset},
129       {{CDF_MaybeBuiltin, "explicit_memset", 3}, &CStringChecker::evalMemset},
130       {{CDF_MaybeBuiltin, "strcpy", 2}, &CStringChecker::evalStrcpy},
131       {{CDF_MaybeBuiltin, "strncpy", 3}, &CStringChecker::evalStrncpy},
132       {{CDF_MaybeBuiltin, "stpcpy", 2}, &CStringChecker::evalStpcpy},
133       {{CDF_MaybeBuiltin, "strlcpy", 3}, &CStringChecker::evalStrlcpy},
134       {{CDF_MaybeBuiltin, "strcat", 2}, &CStringChecker::evalStrcat},
135       {{CDF_MaybeBuiltin, "strncat", 3}, &CStringChecker::evalStrncat},
136       {{CDF_MaybeBuiltin, "strlcat", 3}, &CStringChecker::evalStrlcat},
137       {{CDF_MaybeBuiltin, "strlen", 1}, &CStringChecker::evalstrLength},
138       {{CDF_MaybeBuiltin, "strnlen", 2}, &CStringChecker::evalstrnLength},
139       {{CDF_MaybeBuiltin, "strcmp", 2}, &CStringChecker::evalStrcmp},
140       {{CDF_MaybeBuiltin, "strncmp", 3}, &CStringChecker::evalStrncmp},
141       {{CDF_MaybeBuiltin, "strcasecmp", 2}, &CStringChecker::evalStrcasecmp},
142       {{CDF_MaybeBuiltin, "strncasecmp", 3}, &CStringChecker::evalStrncasecmp},
143       {{CDF_MaybeBuiltin, "strsep", 2}, &CStringChecker::evalStrsep},
144       {{CDF_MaybeBuiltin, "bcopy", 3}, &CStringChecker::evalBcopy},
145       {{CDF_MaybeBuiltin, "bcmp", 3}, &CStringChecker::evalMemcmp},
146       {{CDF_MaybeBuiltin, "bzero", 2}, &CStringChecker::evalBzero},
147       {{CDF_MaybeBuiltin, "explicit_bzero", 2}, &CStringChecker::evalBzero},
148   };
149 
150   // These require a bit of special handling.
151   CallDescription StdCopy{{"std", "copy"}, 3},
152       StdCopyBackward{{"std", "copy_backward"}, 3};
153 
154   FnCheck identifyCall(const CallEvent &Call, CheckerContext &C) const;
155   void evalMemcpy(CheckerContext &C, const CallExpr *CE) const;
156   void evalMempcpy(CheckerContext &C, const CallExpr *CE) const;
157   void evalMemmove(CheckerContext &C, const CallExpr *CE) const;
158   void evalBcopy(CheckerContext &C, const CallExpr *CE) const;
159   void evalCopyCommon(CheckerContext &C, const CallExpr *CE,
160                       ProgramStateRef state, SizeArgExpr Size,
161                       DestinationArgExpr Dest, SourceArgExpr Source,
162                       bool Restricted, bool IsMempcpy) const;
163 
164   void evalMemcmp(CheckerContext &C, const CallExpr *CE) const;
165 
166   void evalstrLength(CheckerContext &C, const CallExpr *CE) const;
167   void evalstrnLength(CheckerContext &C, const CallExpr *CE) const;
168   void evalstrLengthCommon(CheckerContext &C,
169                            const CallExpr *CE,
170                            bool IsStrnlen = false) const;
171 
172   void evalStrcpy(CheckerContext &C, const CallExpr *CE) const;
173   void evalStrncpy(CheckerContext &C, const CallExpr *CE) const;
174   void evalStpcpy(CheckerContext &C, const CallExpr *CE) const;
175   void evalStrlcpy(CheckerContext &C, const CallExpr *CE) const;
176   void evalStrcpyCommon(CheckerContext &C, const CallExpr *CE, bool ReturnEnd,
177                         bool IsBounded, ConcatFnKind appendK,
178                         bool returnPtr = true) const;
179 
180   void evalStrcat(CheckerContext &C, const CallExpr *CE) const;
181   void evalStrncat(CheckerContext &C, const CallExpr *CE) const;
182   void evalStrlcat(CheckerContext &C, const CallExpr *CE) const;
183 
184   void evalStrcmp(CheckerContext &C, const CallExpr *CE) const;
185   void evalStrncmp(CheckerContext &C, const CallExpr *CE) const;
186   void evalStrcasecmp(CheckerContext &C, const CallExpr *CE) const;
187   void evalStrncasecmp(CheckerContext &C, const CallExpr *CE) const;
188   void evalStrcmpCommon(CheckerContext &C,
189                         const CallExpr *CE,
190                         bool IsBounded = false,
191                         bool IgnoreCase = false) const;
192 
193   void evalStrsep(CheckerContext &C, const CallExpr *CE) const;
194 
195   void evalStdCopy(CheckerContext &C, const CallExpr *CE) const;
196   void evalStdCopyBackward(CheckerContext &C, const CallExpr *CE) const;
197   void evalStdCopyCommon(CheckerContext &C, const CallExpr *CE) const;
198   void evalMemset(CheckerContext &C, const CallExpr *CE) const;
199   void evalBzero(CheckerContext &C, const CallExpr *CE) const;
200 
201   // Utility methods
202   std::pair<ProgramStateRef , ProgramStateRef >
203   static assumeZero(CheckerContext &C,
204                     ProgramStateRef state, SVal V, QualType Ty);
205 
206   static ProgramStateRef setCStringLength(ProgramStateRef state,
207                                               const MemRegion *MR,
208                                               SVal strLength);
209   static SVal getCStringLengthForRegion(CheckerContext &C,
210                                         ProgramStateRef &state,
211                                         const Expr *Ex,
212                                         const MemRegion *MR,
213                                         bool hypothetical);
214   SVal getCStringLength(CheckerContext &C,
215                         ProgramStateRef &state,
216                         const Expr *Ex,
217                         SVal Buf,
218                         bool hypothetical = false) const;
219 
220   const StringLiteral *getCStringLiteral(CheckerContext &C,
221                                          ProgramStateRef &state,
222                                          const Expr *expr,
223                                          SVal val) const;
224 
225   static ProgramStateRef InvalidateBuffer(CheckerContext &C,
226                                           ProgramStateRef state,
227                                           const Expr *Ex, SVal V,
228                                           bool IsSourceBuffer,
229                                           const Expr *Size);
230 
231   static bool SummarizeRegion(raw_ostream &os, ASTContext &Ctx,
232                               const MemRegion *MR);
233 
234   static bool memsetAux(const Expr *DstBuffer, SVal CharE,
235                         const Expr *Size, CheckerContext &C,
236                         ProgramStateRef &State);
237 
238   // Re-usable checks
239   ProgramStateRef checkNonNull(CheckerContext &C, ProgramStateRef State,
240                                AnyArgExpr Arg, SVal l) const;
241   ProgramStateRef CheckLocation(CheckerContext &C, ProgramStateRef state,
242                                 AnyArgExpr Buffer, SVal Element,
243                                 AccessKind Access) const;
244   ProgramStateRef CheckBufferAccess(CheckerContext &C, ProgramStateRef State,
245                                     AnyArgExpr Buffer, SizeArgExpr Size,
246                                     AccessKind Access) const;
247   ProgramStateRef CheckOverlap(CheckerContext &C, ProgramStateRef state,
248                                SizeArgExpr Size, AnyArgExpr First,
249                                AnyArgExpr Second) const;
250   void emitOverlapBug(CheckerContext &C,
251                       ProgramStateRef state,
252                       const Stmt *First,
253                       const Stmt *Second) const;
254 
255   void emitNullArgBug(CheckerContext &C, ProgramStateRef State, const Stmt *S,
256                       StringRef WarningMsg) const;
257   void emitOutOfBoundsBug(CheckerContext &C, ProgramStateRef State,
258                           const Stmt *S, StringRef WarningMsg) const;
259   void emitNotCStringBug(CheckerContext &C, ProgramStateRef State,
260                          const Stmt *S, StringRef WarningMsg) const;
261   void emitAdditionOverflowBug(CheckerContext &C, ProgramStateRef State) const;
262   ProgramStateRef checkAdditionOverflow(CheckerContext &C,
263                                             ProgramStateRef state,
264                                             NonLoc left,
265                                             NonLoc right) const;
266 
267   // Return true if the destination buffer of the copy function may be in bound.
268   // Expects SVal of Size to be positive and unsigned.
269   // Expects SVal of FirstBuf to be a FieldRegion.
270   static bool IsFirstBufInBound(CheckerContext &C,
271                                 ProgramStateRef state,
272                                 const Expr *FirstBuf,
273                                 const Expr *Size);
274 };
275 
276 } //end anonymous namespace
277 
278 REGISTER_MAP_WITH_PROGRAMSTATE(CStringLength, const MemRegion *, SVal)
279 
280 //===----------------------------------------------------------------------===//
281 // Individual checks and utility methods.
282 //===----------------------------------------------------------------------===//
283 
284 std::pair<ProgramStateRef , ProgramStateRef >
285 CStringChecker::assumeZero(CheckerContext &C, ProgramStateRef state, SVal V,
286                            QualType Ty) {
287   Optional<DefinedSVal> val = V.getAs<DefinedSVal>();
288   if (!val)
289     return std::pair<ProgramStateRef , ProgramStateRef >(state, state);
290 
291   SValBuilder &svalBuilder = C.getSValBuilder();
292   DefinedOrUnknownSVal zero = svalBuilder.makeZeroVal(Ty);
293   return state->assume(svalBuilder.evalEQ(state, *val, zero));
294 }
295 
296 ProgramStateRef CStringChecker::checkNonNull(CheckerContext &C,
297                                              ProgramStateRef State,
298                                              AnyArgExpr Arg, SVal l) const {
299   // If a previous check has failed, propagate the failure.
300   if (!State)
301     return nullptr;
302 
303   ProgramStateRef stateNull, stateNonNull;
304   std::tie(stateNull, stateNonNull) =
305       assumeZero(C, State, l, Arg.Expression->getType());
306 
307   if (stateNull && !stateNonNull) {
308     if (Filter.CheckCStringNullArg) {
309       SmallString<80> buf;
310       llvm::raw_svector_ostream OS(buf);
311       assert(CurrentFunctionDescription);
312       OS << "Null pointer passed as " << (Arg.ArgumentIndex + 1)
313          << llvm::getOrdinalSuffix(Arg.ArgumentIndex + 1) << " argument to "
314          << CurrentFunctionDescription;
315 
316       emitNullArgBug(C, stateNull, Arg.Expression, OS.str());
317     }
318     return nullptr;
319   }
320 
321   // From here on, assume that the value is non-null.
322   assert(stateNonNull);
323   return stateNonNull;
324 }
325 
326 // FIXME: This was originally copied from ArrayBoundChecker.cpp. Refactor?
327 ProgramStateRef CStringChecker::CheckLocation(CheckerContext &C,
328                                               ProgramStateRef state,
329                                               AnyArgExpr Buffer, SVal Element,
330                                               AccessKind Access) const {
331 
332   // If a previous check has failed, propagate the failure.
333   if (!state)
334     return nullptr;
335 
336   // Check for out of bound array element access.
337   const MemRegion *R = Element.getAsRegion();
338   if (!R)
339     return state;
340 
341   const auto *ER = dyn_cast<ElementRegion>(R);
342   if (!ER)
343     return state;
344 
345   if (ER->getValueType() != C.getASTContext().CharTy)
346     return state;
347 
348   // Get the size of the array.
349   const auto *superReg = cast<SubRegion>(ER->getSuperRegion());
350   DefinedOrUnknownSVal Size =
351       getDynamicExtent(state, superReg, C.getSValBuilder());
352 
353   // Get the index of the accessed element.
354   DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>();
355 
356   ProgramStateRef StInBound = state->assumeInBound(Idx, Size, true);
357   ProgramStateRef StOutBound = state->assumeInBound(Idx, Size, false);
358   if (StOutBound && !StInBound) {
359     // These checks are either enabled by the CString out-of-bounds checker
360     // explicitly or implicitly by the Malloc checker.
361     // In the latter case we only do modeling but do not emit warning.
362     if (!Filter.CheckCStringOutOfBounds)
363       return nullptr;
364 
365     // Emit a bug report.
366     ErrorMessage Message =
367         createOutOfBoundErrorMsg(CurrentFunctionDescription, Access);
368     emitOutOfBoundsBug(C, StOutBound, Buffer.Expression, Message);
369     return nullptr;
370   }
371 
372   // Ensure that we wouldn't read uninitialized value.
373   if (Access == AccessKind::read) {
374     if (Filter.CheckCStringUninitializedRead &&
375         StInBound->getSVal(ER).isUndef()) {
376       emitUninitializedReadBug(C, StInBound, Buffer.Expression);
377       return nullptr;
378     }
379   }
380 
381   // Array bound check succeeded.  From this point forward the array bound
382   // should always succeed.
383   return StInBound;
384 }
385 
386 ProgramStateRef CStringChecker::CheckBufferAccess(CheckerContext &C,
387                                                   ProgramStateRef State,
388                                                   AnyArgExpr Buffer,
389                                                   SizeArgExpr Size,
390                                                   AccessKind Access) const {
391   // If a previous check has failed, propagate the failure.
392   if (!State)
393     return nullptr;
394 
395   SValBuilder &svalBuilder = C.getSValBuilder();
396   ASTContext &Ctx = svalBuilder.getContext();
397 
398   QualType SizeTy = Size.Expression->getType();
399   QualType PtrTy = Ctx.getPointerType(Ctx.CharTy);
400 
401   // Check that the first buffer is non-null.
402   SVal BufVal = C.getSVal(Buffer.Expression);
403   State = checkNonNull(C, State, Buffer, BufVal);
404   if (!State)
405     return nullptr;
406 
407   // If out-of-bounds checking is turned off, skip the rest.
408   if (!Filter.CheckCStringOutOfBounds)
409     return State;
410 
411   // Get the access length and make sure it is known.
412   // FIXME: This assumes the caller has already checked that the access length
413   // is positive. And that it's unsigned.
414   SVal LengthVal = C.getSVal(Size.Expression);
415   Optional<NonLoc> Length = LengthVal.getAs<NonLoc>();
416   if (!Length)
417     return State;
418 
419   // Compute the offset of the last element to be accessed: size-1.
420   NonLoc One = svalBuilder.makeIntVal(1, SizeTy).castAs<NonLoc>();
421   SVal Offset = svalBuilder.evalBinOpNN(State, BO_Sub, *Length, One, SizeTy);
422   if (Offset.isUnknown())
423     return nullptr;
424   NonLoc LastOffset = Offset.castAs<NonLoc>();
425 
426   // Check that the first buffer is sufficiently long.
427   SVal BufStart =
428       svalBuilder.evalCast(BufVal, PtrTy, Buffer.Expression->getType());
429   if (Optional<Loc> BufLoc = BufStart.getAs<Loc>()) {
430 
431     SVal BufEnd =
432         svalBuilder.evalBinOpLN(State, BO_Add, *BufLoc, LastOffset, PtrTy);
433     State = CheckLocation(C, State, Buffer, BufEnd, Access);
434 
435     // If the buffer isn't large enough, abort.
436     if (!State)
437       return nullptr;
438   }
439 
440   // Large enough or not, return this state!
441   return State;
442 }
443 
444 ProgramStateRef CStringChecker::CheckOverlap(CheckerContext &C,
445                                              ProgramStateRef state,
446                                              SizeArgExpr Size, AnyArgExpr First,
447                                              AnyArgExpr Second) const {
448   if (!Filter.CheckCStringBufferOverlap)
449     return state;
450 
451   // Do a simple check for overlap: if the two arguments are from the same
452   // buffer, see if the end of the first is greater than the start of the second
453   // or vice versa.
454 
455   // If a previous check has failed, propagate the failure.
456   if (!state)
457     return nullptr;
458 
459   ProgramStateRef stateTrue, stateFalse;
460 
461   // Get the buffer values and make sure they're known locations.
462   const LocationContext *LCtx = C.getLocationContext();
463   SVal firstVal = state->getSVal(First.Expression, LCtx);
464   SVal secondVal = state->getSVal(Second.Expression, LCtx);
465 
466   Optional<Loc> firstLoc = firstVal.getAs<Loc>();
467   if (!firstLoc)
468     return state;
469 
470   Optional<Loc> secondLoc = secondVal.getAs<Loc>();
471   if (!secondLoc)
472     return state;
473 
474   // Are the two values the same?
475   SValBuilder &svalBuilder = C.getSValBuilder();
476   std::tie(stateTrue, stateFalse) =
477       state->assume(svalBuilder.evalEQ(state, *firstLoc, *secondLoc));
478 
479   if (stateTrue && !stateFalse) {
480     // If the values are known to be equal, that's automatically an overlap.
481     emitOverlapBug(C, stateTrue, First.Expression, Second.Expression);
482     return nullptr;
483   }
484 
485   // assume the two expressions are not equal.
486   assert(stateFalse);
487   state = stateFalse;
488 
489   // Which value comes first?
490   QualType cmpTy = svalBuilder.getConditionType();
491   SVal reverse =
492       svalBuilder.evalBinOpLL(state, BO_GT, *firstLoc, *secondLoc, cmpTy);
493   Optional<DefinedOrUnknownSVal> reverseTest =
494       reverse.getAs<DefinedOrUnknownSVal>();
495   if (!reverseTest)
496     return state;
497 
498   std::tie(stateTrue, stateFalse) = state->assume(*reverseTest);
499   if (stateTrue) {
500     if (stateFalse) {
501       // If we don't know which one comes first, we can't perform this test.
502       return state;
503     } else {
504       // Switch the values so that firstVal is before secondVal.
505       std::swap(firstLoc, secondLoc);
506 
507       // Switch the Exprs as well, so that they still correspond.
508       std::swap(First, Second);
509     }
510   }
511 
512   // Get the length, and make sure it too is known.
513   SVal LengthVal = state->getSVal(Size.Expression, LCtx);
514   Optional<NonLoc> Length = LengthVal.getAs<NonLoc>();
515   if (!Length)
516     return state;
517 
518   // Convert the first buffer's start address to char*.
519   // Bail out if the cast fails.
520   ASTContext &Ctx = svalBuilder.getContext();
521   QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy);
522   SVal FirstStart =
523       svalBuilder.evalCast(*firstLoc, CharPtrTy, First.Expression->getType());
524   Optional<Loc> FirstStartLoc = FirstStart.getAs<Loc>();
525   if (!FirstStartLoc)
526     return state;
527 
528   // Compute the end of the first buffer. Bail out if THAT fails.
529   SVal FirstEnd = svalBuilder.evalBinOpLN(state, BO_Add, *FirstStartLoc,
530                                           *Length, CharPtrTy);
531   Optional<Loc> FirstEndLoc = FirstEnd.getAs<Loc>();
532   if (!FirstEndLoc)
533     return state;
534 
535   // Is the end of the first buffer past the start of the second buffer?
536   SVal Overlap =
537       svalBuilder.evalBinOpLL(state, BO_GT, *FirstEndLoc, *secondLoc, cmpTy);
538   Optional<DefinedOrUnknownSVal> OverlapTest =
539       Overlap.getAs<DefinedOrUnknownSVal>();
540   if (!OverlapTest)
541     return state;
542 
543   std::tie(stateTrue, stateFalse) = state->assume(*OverlapTest);
544 
545   if (stateTrue && !stateFalse) {
546     // Overlap!
547     emitOverlapBug(C, stateTrue, First.Expression, Second.Expression);
548     return nullptr;
549   }
550 
551   // assume the two expressions don't overlap.
552   assert(stateFalse);
553   return stateFalse;
554 }
555 
556 void CStringChecker::emitOverlapBug(CheckerContext &C, ProgramStateRef state,
557                                   const Stmt *First, const Stmt *Second) const {
558   ExplodedNode *N = C.generateErrorNode(state);
559   if (!N)
560     return;
561 
562   if (!BT_Overlap)
563     BT_Overlap.reset(new BugType(Filter.CheckNameCStringBufferOverlap,
564                                  categories::UnixAPI, "Improper arguments"));
565 
566   // Generate a report for this bug.
567   auto report = std::make_unique<PathSensitiveBugReport>(
568       *BT_Overlap, "Arguments must not be overlapping buffers", N);
569   report->addRange(First->getSourceRange());
570   report->addRange(Second->getSourceRange());
571 
572   C.emitReport(std::move(report));
573 }
574 
575 void CStringChecker::emitNullArgBug(CheckerContext &C, ProgramStateRef State,
576                                     const Stmt *S, StringRef WarningMsg) const {
577   if (ExplodedNode *N = C.generateErrorNode(State)) {
578     if (!BT_Null)
579       BT_Null.reset(new BuiltinBug(
580           Filter.CheckNameCStringNullArg, categories::UnixAPI,
581           "Null pointer argument in call to byte string function"));
582 
583     BuiltinBug *BT = static_cast<BuiltinBug *>(BT_Null.get());
584     auto Report = std::make_unique<PathSensitiveBugReport>(*BT, WarningMsg, N);
585     Report->addRange(S->getSourceRange());
586     if (const auto *Ex = dyn_cast<Expr>(S))
587       bugreporter::trackExpressionValue(N, Ex, *Report);
588     C.emitReport(std::move(Report));
589   }
590 }
591 
592 void CStringChecker::emitUninitializedReadBug(CheckerContext &C,
593                                               ProgramStateRef State,
594                                               const Expr *E) const {
595   if (ExplodedNode *N = C.generateErrorNode(State)) {
596     const char *Msg =
597         "Bytes string function accesses uninitialized/garbage values";
598     if (!BT_UninitRead)
599       BT_UninitRead.reset(
600           new BuiltinBug(Filter.CheckNameCStringUninitializedRead,
601                          "Accessing unitialized/garbage values", Msg));
602 
603     BuiltinBug *BT = static_cast<BuiltinBug *>(BT_UninitRead.get());
604 
605     auto Report = std::make_unique<PathSensitiveBugReport>(*BT, Msg, N);
606     Report->addRange(E->getSourceRange());
607     bugreporter::trackExpressionValue(N, E, *Report);
608     C.emitReport(std::move(Report));
609   }
610 }
611 
612 void CStringChecker::emitOutOfBoundsBug(CheckerContext &C,
613                                         ProgramStateRef State, const Stmt *S,
614                                         StringRef WarningMsg) const {
615   if (ExplodedNode *N = C.generateErrorNode(State)) {
616     if (!BT_Bounds)
617       BT_Bounds.reset(new BuiltinBug(
618           Filter.CheckCStringOutOfBounds ? Filter.CheckNameCStringOutOfBounds
619                                          : Filter.CheckNameCStringNullArg,
620           "Out-of-bound array access",
621           "Byte string function accesses out-of-bound array element"));
622 
623     BuiltinBug *BT = static_cast<BuiltinBug *>(BT_Bounds.get());
624 
625     // FIXME: It would be nice to eventually make this diagnostic more clear,
626     // e.g., by referencing the original declaration or by saying *why* this
627     // reference is outside the range.
628     auto Report = std::make_unique<PathSensitiveBugReport>(*BT, WarningMsg, N);
629     Report->addRange(S->getSourceRange());
630     C.emitReport(std::move(Report));
631   }
632 }
633 
634 void CStringChecker::emitNotCStringBug(CheckerContext &C, ProgramStateRef State,
635                                        const Stmt *S,
636                                        StringRef WarningMsg) const {
637   if (ExplodedNode *N = C.generateNonFatalErrorNode(State)) {
638     if (!BT_NotCString)
639       BT_NotCString.reset(new BuiltinBug(
640           Filter.CheckNameCStringNotNullTerm, categories::UnixAPI,
641           "Argument is not a null-terminated string."));
642 
643     auto Report =
644         std::make_unique<PathSensitiveBugReport>(*BT_NotCString, WarningMsg, N);
645 
646     Report->addRange(S->getSourceRange());
647     C.emitReport(std::move(Report));
648   }
649 }
650 
651 void CStringChecker::emitAdditionOverflowBug(CheckerContext &C,
652                                              ProgramStateRef State) const {
653   if (ExplodedNode *N = C.generateErrorNode(State)) {
654     if (!BT_AdditionOverflow)
655       BT_AdditionOverflow.reset(
656           new BuiltinBug(Filter.CheckNameCStringOutOfBounds, "API",
657                          "Sum of expressions causes overflow."));
658 
659     // This isn't a great error message, but this should never occur in real
660     // code anyway -- you'd have to create a buffer longer than a size_t can
661     // represent, which is sort of a contradiction.
662     const char *WarningMsg =
663         "This expression will create a string whose length is too big to "
664         "be represented as a size_t";
665 
666     auto Report = std::make_unique<PathSensitiveBugReport>(*BT_AdditionOverflow,
667                                                            WarningMsg, N);
668     C.emitReport(std::move(Report));
669   }
670 }
671 
672 ProgramStateRef CStringChecker::checkAdditionOverflow(CheckerContext &C,
673                                                      ProgramStateRef state,
674                                                      NonLoc left,
675                                                      NonLoc right) const {
676   // If out-of-bounds checking is turned off, skip the rest.
677   if (!Filter.CheckCStringOutOfBounds)
678     return state;
679 
680   // If a previous check has failed, propagate the failure.
681   if (!state)
682     return nullptr;
683 
684   SValBuilder &svalBuilder = C.getSValBuilder();
685   BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
686 
687   QualType sizeTy = svalBuilder.getContext().getSizeType();
688   const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy);
689   NonLoc maxVal = svalBuilder.makeIntVal(maxValInt);
690 
691   SVal maxMinusRight;
692   if (right.getAs<nonloc::ConcreteInt>()) {
693     maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, right,
694                                                  sizeTy);
695   } else {
696     // Try switching the operands. (The order of these two assignments is
697     // important!)
698     maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, left,
699                                             sizeTy);
700     left = right;
701   }
702 
703   if (Optional<NonLoc> maxMinusRightNL = maxMinusRight.getAs<NonLoc>()) {
704     QualType cmpTy = svalBuilder.getConditionType();
705     // If left > max - right, we have an overflow.
706     SVal willOverflow = svalBuilder.evalBinOpNN(state, BO_GT, left,
707                                                 *maxMinusRightNL, cmpTy);
708 
709     ProgramStateRef stateOverflow, stateOkay;
710     std::tie(stateOverflow, stateOkay) =
711       state->assume(willOverflow.castAs<DefinedOrUnknownSVal>());
712 
713     if (stateOverflow && !stateOkay) {
714       // We have an overflow. Emit a bug report.
715       emitAdditionOverflowBug(C, stateOverflow);
716       return nullptr;
717     }
718 
719     // From now on, assume an overflow didn't occur.
720     assert(stateOkay);
721     state = stateOkay;
722   }
723 
724   return state;
725 }
726 
727 ProgramStateRef CStringChecker::setCStringLength(ProgramStateRef state,
728                                                 const MemRegion *MR,
729                                                 SVal strLength) {
730   assert(!strLength.isUndef() && "Attempt to set an undefined string length");
731 
732   MR = MR->StripCasts();
733 
734   switch (MR->getKind()) {
735   case MemRegion::StringRegionKind:
736     // FIXME: This can happen if we strcpy() into a string region. This is
737     // undefined [C99 6.4.5p6], but we should still warn about it.
738     return state;
739 
740   case MemRegion::SymbolicRegionKind:
741   case MemRegion::AllocaRegionKind:
742   case MemRegion::NonParamVarRegionKind:
743   case MemRegion::ParamVarRegionKind:
744   case MemRegion::FieldRegionKind:
745   case MemRegion::ObjCIvarRegionKind:
746     // These are the types we can currently track string lengths for.
747     break;
748 
749   case MemRegion::ElementRegionKind:
750     // FIXME: Handle element regions by upper-bounding the parent region's
751     // string length.
752     return state;
753 
754   default:
755     // Other regions (mostly non-data) can't have a reliable C string length.
756     // For now, just ignore the change.
757     // FIXME: These are rare but not impossible. We should output some kind of
758     // warning for things like strcpy((char[]){'a', 0}, "b");
759     return state;
760   }
761 
762   if (strLength.isUnknown())
763     return state->remove<CStringLength>(MR);
764 
765   return state->set<CStringLength>(MR, strLength);
766 }
767 
768 SVal CStringChecker::getCStringLengthForRegion(CheckerContext &C,
769                                                ProgramStateRef &state,
770                                                const Expr *Ex,
771                                                const MemRegion *MR,
772                                                bool hypothetical) {
773   if (!hypothetical) {
774     // If there's a recorded length, go ahead and return it.
775     const SVal *Recorded = state->get<CStringLength>(MR);
776     if (Recorded)
777       return *Recorded;
778   }
779 
780   // Otherwise, get a new symbol and update the state.
781   SValBuilder &svalBuilder = C.getSValBuilder();
782   QualType sizeTy = svalBuilder.getContext().getSizeType();
783   SVal strLength = svalBuilder.getMetadataSymbolVal(CStringChecker::getTag(),
784                                                     MR, Ex, sizeTy,
785                                                     C.getLocationContext(),
786                                                     C.blockCount());
787 
788   if (!hypothetical) {
789     if (Optional<NonLoc> strLn = strLength.getAs<NonLoc>()) {
790       // In case of unbounded calls strlen etc bound the range to SIZE_MAX/4
791       BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
792       const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy);
793       llvm::APSInt fourInt = APSIntType(maxValInt).getValue(4);
794       const llvm::APSInt *maxLengthInt = BVF.evalAPSInt(BO_Div, maxValInt,
795                                                         fourInt);
796       NonLoc maxLength = svalBuilder.makeIntVal(*maxLengthInt);
797       SVal evalLength = svalBuilder.evalBinOpNN(state, BO_LE, *strLn,
798                                                 maxLength, sizeTy);
799       state = state->assume(evalLength.castAs<DefinedOrUnknownSVal>(), true);
800     }
801     state = state->set<CStringLength>(MR, strLength);
802   }
803 
804   return strLength;
805 }
806 
807 SVal CStringChecker::getCStringLength(CheckerContext &C, ProgramStateRef &state,
808                                       const Expr *Ex, SVal Buf,
809                                       bool hypothetical) const {
810   const MemRegion *MR = Buf.getAsRegion();
811   if (!MR) {
812     // If we can't get a region, see if it's something we /know/ isn't a
813     // C string. In the context of locations, the only time we can issue such
814     // a warning is for labels.
815     if (Optional<loc::GotoLabel> Label = Buf.getAs<loc::GotoLabel>()) {
816       if (Filter.CheckCStringNotNullTerm) {
817         SmallString<120> buf;
818         llvm::raw_svector_ostream os(buf);
819         assert(CurrentFunctionDescription);
820         os << "Argument to " << CurrentFunctionDescription
821            << " is the address of the label '" << Label->getLabel()->getName()
822            << "', which is not a null-terminated string";
823 
824         emitNotCStringBug(C, state, Ex, os.str());
825       }
826       return UndefinedVal();
827     }
828 
829     // If it's not a region and not a label, give up.
830     return UnknownVal();
831   }
832 
833   // If we have a region, strip casts from it and see if we can figure out
834   // its length. For anything we can't figure out, just return UnknownVal.
835   MR = MR->StripCasts();
836 
837   switch (MR->getKind()) {
838   case MemRegion::StringRegionKind: {
839     // Modifying the contents of string regions is undefined [C99 6.4.5p6],
840     // so we can assume that the byte length is the correct C string length.
841     SValBuilder &svalBuilder = C.getSValBuilder();
842     QualType sizeTy = svalBuilder.getContext().getSizeType();
843     const StringLiteral *strLit = cast<StringRegion>(MR)->getStringLiteral();
844     return svalBuilder.makeIntVal(strLit->getByteLength(), sizeTy);
845   }
846   case MemRegion::SymbolicRegionKind:
847   case MemRegion::AllocaRegionKind:
848   case MemRegion::NonParamVarRegionKind:
849   case MemRegion::ParamVarRegionKind:
850   case MemRegion::FieldRegionKind:
851   case MemRegion::ObjCIvarRegionKind:
852     return getCStringLengthForRegion(C, state, Ex, MR, hypothetical);
853   case MemRegion::CompoundLiteralRegionKind:
854     // FIXME: Can we track this? Is it necessary?
855     return UnknownVal();
856   case MemRegion::ElementRegionKind:
857     // FIXME: How can we handle this? It's not good enough to subtract the
858     // offset from the base string length; consider "123\x00567" and &a[5].
859     return UnknownVal();
860   default:
861     // Other regions (mostly non-data) can't have a reliable C string length.
862     // In this case, an error is emitted and UndefinedVal is returned.
863     // The caller should always be prepared to handle this case.
864     if (Filter.CheckCStringNotNullTerm) {
865       SmallString<120> buf;
866       llvm::raw_svector_ostream os(buf);
867 
868       assert(CurrentFunctionDescription);
869       os << "Argument to " << CurrentFunctionDescription << " is ";
870 
871       if (SummarizeRegion(os, C.getASTContext(), MR))
872         os << ", which is not a null-terminated string";
873       else
874         os << "not a null-terminated string";
875 
876       emitNotCStringBug(C, state, Ex, os.str());
877     }
878     return UndefinedVal();
879   }
880 }
881 
882 const StringLiteral *CStringChecker::getCStringLiteral(CheckerContext &C,
883   ProgramStateRef &state, const Expr *expr, SVal val) const {
884 
885   // Get the memory region pointed to by the val.
886   const MemRegion *bufRegion = val.getAsRegion();
887   if (!bufRegion)
888     return nullptr;
889 
890   // Strip casts off the memory region.
891   bufRegion = bufRegion->StripCasts();
892 
893   // Cast the memory region to a string region.
894   const StringRegion *strRegion= dyn_cast<StringRegion>(bufRegion);
895   if (!strRegion)
896     return nullptr;
897 
898   // Return the actual string in the string region.
899   return strRegion->getStringLiteral();
900 }
901 
902 bool CStringChecker::IsFirstBufInBound(CheckerContext &C,
903                                        ProgramStateRef state,
904                                        const Expr *FirstBuf,
905                                        const Expr *Size) {
906   // If we do not know that the buffer is long enough we return 'true'.
907   // Otherwise the parent region of this field region would also get
908   // invalidated, which would lead to warnings based on an unknown state.
909 
910   // Originally copied from CheckBufferAccess and CheckLocation.
911   SValBuilder &svalBuilder = C.getSValBuilder();
912   ASTContext &Ctx = svalBuilder.getContext();
913   const LocationContext *LCtx = C.getLocationContext();
914 
915   QualType sizeTy = Size->getType();
916   QualType PtrTy = Ctx.getPointerType(Ctx.CharTy);
917   SVal BufVal = state->getSVal(FirstBuf, LCtx);
918 
919   SVal LengthVal = state->getSVal(Size, LCtx);
920   Optional<NonLoc> Length = LengthVal.getAs<NonLoc>();
921   if (!Length)
922     return true; // cf top comment.
923 
924   // Compute the offset of the last element to be accessed: size-1.
925   NonLoc One = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>();
926   SVal Offset = svalBuilder.evalBinOpNN(state, BO_Sub, *Length, One, sizeTy);
927   if (Offset.isUnknown())
928     return true; // cf top comment
929   NonLoc LastOffset = Offset.castAs<NonLoc>();
930 
931   // Check that the first buffer is sufficiently long.
932   SVal BufStart = svalBuilder.evalCast(BufVal, PtrTy, FirstBuf->getType());
933   Optional<Loc> BufLoc = BufStart.getAs<Loc>();
934   if (!BufLoc)
935     return true; // cf top comment.
936 
937   SVal BufEnd =
938       svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc, LastOffset, PtrTy);
939 
940   // Check for out of bound array element access.
941   const MemRegion *R = BufEnd.getAsRegion();
942   if (!R)
943     return true; // cf top comment.
944 
945   const ElementRegion *ER = dyn_cast<ElementRegion>(R);
946   if (!ER)
947     return true; // cf top comment.
948 
949   // FIXME: Does this crash when a non-standard definition
950   // of a library function is encountered?
951   assert(ER->getValueType() == C.getASTContext().CharTy &&
952          "IsFirstBufInBound should only be called with char* ElementRegions");
953 
954   // Get the size of the array.
955   const SubRegion *superReg = cast<SubRegion>(ER->getSuperRegion());
956   DefinedOrUnknownSVal SizeDV = getDynamicExtent(state, superReg, svalBuilder);
957 
958   // Get the index of the accessed element.
959   DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>();
960 
961   ProgramStateRef StInBound = state->assumeInBound(Idx, SizeDV, true);
962 
963   return static_cast<bool>(StInBound);
964 }
965 
966 ProgramStateRef CStringChecker::InvalidateBuffer(CheckerContext &C,
967                                                  ProgramStateRef state,
968                                                  const Expr *E, SVal V,
969                                                  bool IsSourceBuffer,
970                                                  const Expr *Size) {
971   Optional<Loc> L = V.getAs<Loc>();
972   if (!L)
973     return state;
974 
975   // FIXME: This is a simplified version of what's in CFRefCount.cpp -- it makes
976   // some assumptions about the value that CFRefCount can't. Even so, it should
977   // probably be refactored.
978   if (Optional<loc::MemRegionVal> MR = L->getAs<loc::MemRegionVal>()) {
979     const MemRegion *R = MR->getRegion()->StripCasts();
980 
981     // Are we dealing with an ElementRegion?  If so, we should be invalidating
982     // the super-region.
983     if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
984       R = ER->getSuperRegion();
985       // FIXME: What about layers of ElementRegions?
986     }
987 
988     // Invalidate this region.
989     const LocationContext *LCtx = C.getPredecessor()->getLocationContext();
990 
991     bool CausesPointerEscape = false;
992     RegionAndSymbolInvalidationTraits ITraits;
993     // Invalidate and escape only indirect regions accessible through the source
994     // buffer.
995     if (IsSourceBuffer) {
996       ITraits.setTrait(R->getBaseRegion(),
997                        RegionAndSymbolInvalidationTraits::TK_PreserveContents);
998       ITraits.setTrait(R, RegionAndSymbolInvalidationTraits::TK_SuppressEscape);
999       CausesPointerEscape = true;
1000     } else {
1001       const MemRegion::Kind& K = R->getKind();
1002       if (K == MemRegion::FieldRegionKind)
1003         if (Size && IsFirstBufInBound(C, state, E, Size)) {
1004           // If destination buffer is a field region and access is in bound,
1005           // do not invalidate its super region.
1006           ITraits.setTrait(
1007               R,
1008               RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion);
1009         }
1010     }
1011 
1012     return state->invalidateRegions(R, E, C.blockCount(), LCtx,
1013                                     CausesPointerEscape, nullptr, nullptr,
1014                                     &ITraits);
1015   }
1016 
1017   // If we have a non-region value by chance, just remove the binding.
1018   // FIXME: is this necessary or correct? This handles the non-Region
1019   //  cases.  Is it ever valid to store to these?
1020   return state->killBinding(*L);
1021 }
1022 
1023 bool CStringChecker::SummarizeRegion(raw_ostream &os, ASTContext &Ctx,
1024                                      const MemRegion *MR) {
1025   switch (MR->getKind()) {
1026   case MemRegion::FunctionCodeRegionKind: {
1027     if (const auto *FD = cast<FunctionCodeRegion>(MR)->getDecl())
1028       os << "the address of the function '" << *FD << '\'';
1029     else
1030       os << "the address of a function";
1031     return true;
1032   }
1033   case MemRegion::BlockCodeRegionKind:
1034     os << "block text";
1035     return true;
1036   case MemRegion::BlockDataRegionKind:
1037     os << "a block";
1038     return true;
1039   case MemRegion::CXXThisRegionKind:
1040   case MemRegion::CXXTempObjectRegionKind:
1041     os << "a C++ temp object of type "
1042        << cast<TypedValueRegion>(MR)->getValueType().getAsString();
1043     return true;
1044   case MemRegion::NonParamVarRegionKind:
1045     os << "a variable of type"
1046        << cast<TypedValueRegion>(MR)->getValueType().getAsString();
1047     return true;
1048   case MemRegion::ParamVarRegionKind:
1049     os << "a parameter of type"
1050        << cast<TypedValueRegion>(MR)->getValueType().getAsString();
1051     return true;
1052   case MemRegion::FieldRegionKind:
1053     os << "a field of type "
1054        << cast<TypedValueRegion>(MR)->getValueType().getAsString();
1055     return true;
1056   case MemRegion::ObjCIvarRegionKind:
1057     os << "an instance variable of type "
1058        << cast<TypedValueRegion>(MR)->getValueType().getAsString();
1059     return true;
1060   default:
1061     return false;
1062   }
1063 }
1064 
1065 bool CStringChecker::memsetAux(const Expr *DstBuffer, SVal CharVal,
1066                                const Expr *Size, CheckerContext &C,
1067                                ProgramStateRef &State) {
1068   SVal MemVal = C.getSVal(DstBuffer);
1069   SVal SizeVal = C.getSVal(Size);
1070   const MemRegion *MR = MemVal.getAsRegion();
1071   if (!MR)
1072     return false;
1073 
1074   // We're about to model memset by producing a "default binding" in the Store.
1075   // Our current implementation - RegionStore - doesn't support default bindings
1076   // that don't cover the whole base region. So we should first get the offset
1077   // and the base region to figure out whether the offset of buffer is 0.
1078   RegionOffset Offset = MR->getAsOffset();
1079   const MemRegion *BR = Offset.getRegion();
1080 
1081   Optional<NonLoc> SizeNL = SizeVal.getAs<NonLoc>();
1082   if (!SizeNL)
1083     return false;
1084 
1085   SValBuilder &svalBuilder = C.getSValBuilder();
1086   ASTContext &Ctx = C.getASTContext();
1087 
1088   // void *memset(void *dest, int ch, size_t count);
1089   // For now we can only handle the case of offset is 0 and concrete char value.
1090   if (Offset.isValid() && !Offset.hasSymbolicOffset() &&
1091       Offset.getOffset() == 0) {
1092     // Get the base region's size.
1093     DefinedOrUnknownSVal SizeDV = getDynamicExtent(State, BR, svalBuilder);
1094 
1095     ProgramStateRef StateWholeReg, StateNotWholeReg;
1096     std::tie(StateWholeReg, StateNotWholeReg) =
1097         State->assume(svalBuilder.evalEQ(State, SizeDV, *SizeNL));
1098 
1099     // With the semantic of 'memset()', we should convert the CharVal to
1100     // unsigned char.
1101     CharVal = svalBuilder.evalCast(CharVal, Ctx.UnsignedCharTy, Ctx.IntTy);
1102 
1103     ProgramStateRef StateNullChar, StateNonNullChar;
1104     std::tie(StateNullChar, StateNonNullChar) =
1105         assumeZero(C, State, CharVal, Ctx.UnsignedCharTy);
1106 
1107     if (StateWholeReg && !StateNotWholeReg && StateNullChar &&
1108         !StateNonNullChar) {
1109       // If the 'memset()' acts on the whole region of destination buffer and
1110       // the value of the second argument of 'memset()' is zero, bind the second
1111       // argument's value to the destination buffer with 'default binding'.
1112       // FIXME: Since there is no perfect way to bind the non-zero character, we
1113       // can only deal with zero value here. In the future, we need to deal with
1114       // the binding of non-zero value in the case of whole region.
1115       State = State->bindDefaultZero(svalBuilder.makeLoc(BR),
1116                                      C.getLocationContext());
1117     } else {
1118       // If the destination buffer's extent is not equal to the value of
1119       // third argument, just invalidate buffer.
1120       State = InvalidateBuffer(C, State, DstBuffer, MemVal,
1121                                /*IsSourceBuffer*/ false, Size);
1122     }
1123 
1124     if (StateNullChar && !StateNonNullChar) {
1125       // If the value of the second argument of 'memset()' is zero, set the
1126       // string length of destination buffer to 0 directly.
1127       State = setCStringLength(State, MR,
1128                                svalBuilder.makeZeroVal(Ctx.getSizeType()));
1129     } else if (!StateNullChar && StateNonNullChar) {
1130       SVal NewStrLen = svalBuilder.getMetadataSymbolVal(
1131           CStringChecker::getTag(), MR, DstBuffer, Ctx.getSizeType(),
1132           C.getLocationContext(), C.blockCount());
1133 
1134       // If the value of second argument is not zero, then the string length
1135       // is at least the size argument.
1136       SVal NewStrLenGESize = svalBuilder.evalBinOp(
1137           State, BO_GE, NewStrLen, SizeVal, svalBuilder.getConditionType());
1138 
1139       State = setCStringLength(
1140           State->assume(NewStrLenGESize.castAs<DefinedOrUnknownSVal>(), true),
1141           MR, NewStrLen);
1142     }
1143   } else {
1144     // If the offset is not zero and char value is not concrete, we can do
1145     // nothing but invalidate the buffer.
1146     State = InvalidateBuffer(C, State, DstBuffer, MemVal,
1147                              /*IsSourceBuffer*/ false, Size);
1148   }
1149   return true;
1150 }
1151 
1152 //===----------------------------------------------------------------------===//
1153 // evaluation of individual function calls.
1154 //===----------------------------------------------------------------------===//
1155 
1156 void CStringChecker::evalCopyCommon(CheckerContext &C, const CallExpr *CE,
1157                                     ProgramStateRef state, SizeArgExpr Size,
1158                                     DestinationArgExpr Dest,
1159                                     SourceArgExpr Source, bool Restricted,
1160                                     bool IsMempcpy) const {
1161   CurrentFunctionDescription = "memory copy function";
1162 
1163   // See if the size argument is zero.
1164   const LocationContext *LCtx = C.getLocationContext();
1165   SVal sizeVal = state->getSVal(Size.Expression, LCtx);
1166   QualType sizeTy = Size.Expression->getType();
1167 
1168   ProgramStateRef stateZeroSize, stateNonZeroSize;
1169   std::tie(stateZeroSize, stateNonZeroSize) =
1170       assumeZero(C, state, sizeVal, sizeTy);
1171 
1172   // Get the value of the Dest.
1173   SVal destVal = state->getSVal(Dest.Expression, LCtx);
1174 
1175   // If the size is zero, there won't be any actual memory access, so
1176   // just bind the return value to the destination buffer and return.
1177   if (stateZeroSize && !stateNonZeroSize) {
1178     stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, destVal);
1179     C.addTransition(stateZeroSize);
1180     return;
1181   }
1182 
1183   // If the size can be nonzero, we have to check the other arguments.
1184   if (stateNonZeroSize) {
1185     state = stateNonZeroSize;
1186 
1187     // Ensure the destination is not null. If it is NULL there will be a
1188     // NULL pointer dereference.
1189     state = checkNonNull(C, state, Dest, destVal);
1190     if (!state)
1191       return;
1192 
1193     // Get the value of the Src.
1194     SVal srcVal = state->getSVal(Source.Expression, LCtx);
1195 
1196     // Ensure the source is not null. If it is NULL there will be a
1197     // NULL pointer dereference.
1198     state = checkNonNull(C, state, Source, srcVal);
1199     if (!state)
1200       return;
1201 
1202     // Ensure the accesses are valid and that the buffers do not overlap.
1203     state = CheckBufferAccess(C, state, Dest, Size, AccessKind::write);
1204     state = CheckBufferAccess(C, state, Source, Size, AccessKind::read);
1205 
1206     if (Restricted)
1207       state = CheckOverlap(C, state, Size, Dest, Source);
1208 
1209     if (!state)
1210       return;
1211 
1212     // If this is mempcpy, get the byte after the last byte copied and
1213     // bind the expr.
1214     if (IsMempcpy) {
1215       // Get the byte after the last byte copied.
1216       SValBuilder &SvalBuilder = C.getSValBuilder();
1217       ASTContext &Ctx = SvalBuilder.getContext();
1218       QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy);
1219       SVal DestRegCharVal =
1220           SvalBuilder.evalCast(destVal, CharPtrTy, Dest.Expression->getType());
1221       SVal lastElement = C.getSValBuilder().evalBinOp(
1222           state, BO_Add, DestRegCharVal, sizeVal, Dest.Expression->getType());
1223       // If we don't know how much we copied, we can at least
1224       // conjure a return value for later.
1225       if (lastElement.isUnknown())
1226         lastElement = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx,
1227                                                           C.blockCount());
1228 
1229       // The byte after the last byte copied is the return value.
1230       state = state->BindExpr(CE, LCtx, lastElement);
1231     } else {
1232       // All other copies return the destination buffer.
1233       // (Well, bcopy() has a void return type, but this won't hurt.)
1234       state = state->BindExpr(CE, LCtx, destVal);
1235     }
1236 
1237     // Invalidate the destination (regular invalidation without pointer-escaping
1238     // the address of the top-level region).
1239     // FIXME: Even if we can't perfectly model the copy, we should see if we
1240     // can use LazyCompoundVals to copy the source values into the destination.
1241     // This would probably remove any existing bindings past the end of the
1242     // copied region, but that's still an improvement over blank invalidation.
1243     state =
1244         InvalidateBuffer(C, state, Dest.Expression, C.getSVal(Dest.Expression),
1245                          /*IsSourceBuffer*/ false, Size.Expression);
1246 
1247     // Invalidate the source (const-invalidation without const-pointer-escaping
1248     // the address of the top-level region).
1249     state = InvalidateBuffer(C, state, Source.Expression,
1250                              C.getSVal(Source.Expression),
1251                              /*IsSourceBuffer*/ true, nullptr);
1252 
1253     C.addTransition(state);
1254   }
1255 }
1256 
1257 void CStringChecker::evalMemcpy(CheckerContext &C, const CallExpr *CE) const {
1258   // void *memcpy(void *restrict dst, const void *restrict src, size_t n);
1259   // The return value is the address of the destination buffer.
1260   DestinationArgExpr Dest = {CE->getArg(0), 0};
1261   SourceArgExpr Src = {CE->getArg(1), 1};
1262   SizeArgExpr Size = {CE->getArg(2), 2};
1263 
1264   ProgramStateRef State = C.getState();
1265 
1266   constexpr bool IsRestricted = true;
1267   constexpr bool IsMempcpy = false;
1268   evalCopyCommon(C, CE, State, Size, Dest, Src, IsRestricted, IsMempcpy);
1269 }
1270 
1271 void CStringChecker::evalMempcpy(CheckerContext &C, const CallExpr *CE) const {
1272   // void *mempcpy(void *restrict dst, const void *restrict src, size_t n);
1273   // The return value is a pointer to the byte following the last written byte.
1274   DestinationArgExpr Dest = {CE->getArg(0), 0};
1275   SourceArgExpr Src = {CE->getArg(1), 1};
1276   SizeArgExpr Size = {CE->getArg(2), 2};
1277 
1278   constexpr bool IsRestricted = true;
1279   constexpr bool IsMempcpy = true;
1280   evalCopyCommon(C, CE, C.getState(), Size, Dest, Src, IsRestricted, IsMempcpy);
1281 }
1282 
1283 void CStringChecker::evalMemmove(CheckerContext &C, const CallExpr *CE) const {
1284   // void *memmove(void *dst, const void *src, size_t n);
1285   // The return value is the address of the destination buffer.
1286   DestinationArgExpr Dest = {CE->getArg(0), 0};
1287   SourceArgExpr Src = {CE->getArg(1), 1};
1288   SizeArgExpr Size = {CE->getArg(2), 2};
1289 
1290   constexpr bool IsRestricted = false;
1291   constexpr bool IsMempcpy = false;
1292   evalCopyCommon(C, CE, C.getState(), Size, Dest, Src, IsRestricted, IsMempcpy);
1293 }
1294 
1295 void CStringChecker::evalBcopy(CheckerContext &C, const CallExpr *CE) const {
1296   // void bcopy(const void *src, void *dst, size_t n);
1297   SourceArgExpr Src(CE->getArg(0), 0);
1298   DestinationArgExpr Dest = {CE->getArg(1), 1};
1299   SizeArgExpr Size = {CE->getArg(2), 2};
1300 
1301   constexpr bool IsRestricted = false;
1302   constexpr bool IsMempcpy = false;
1303   evalCopyCommon(C, CE, C.getState(), Size, Dest, Src, IsRestricted, IsMempcpy);
1304 }
1305 
1306 void CStringChecker::evalMemcmp(CheckerContext &C, const CallExpr *CE) const {
1307   // int memcmp(const void *s1, const void *s2, size_t n);
1308   CurrentFunctionDescription = "memory comparison function";
1309 
1310   AnyArgExpr Left = {CE->getArg(0), 0};
1311   AnyArgExpr Right = {CE->getArg(1), 1};
1312   SizeArgExpr Size = {CE->getArg(2), 2};
1313 
1314   ProgramStateRef State = C.getState();
1315   SValBuilder &Builder = C.getSValBuilder();
1316   const LocationContext *LCtx = C.getLocationContext();
1317 
1318   // See if the size argument is zero.
1319   SVal sizeVal = State->getSVal(Size.Expression, LCtx);
1320   QualType sizeTy = Size.Expression->getType();
1321 
1322   ProgramStateRef stateZeroSize, stateNonZeroSize;
1323   std::tie(stateZeroSize, stateNonZeroSize) =
1324       assumeZero(C, State, sizeVal, sizeTy);
1325 
1326   // If the size can be zero, the result will be 0 in that case, and we don't
1327   // have to check either of the buffers.
1328   if (stateZeroSize) {
1329     State = stateZeroSize;
1330     State = State->BindExpr(CE, LCtx, Builder.makeZeroVal(CE->getType()));
1331     C.addTransition(State);
1332   }
1333 
1334   // If the size can be nonzero, we have to check the other arguments.
1335   if (stateNonZeroSize) {
1336     State = stateNonZeroSize;
1337     // If we know the two buffers are the same, we know the result is 0.
1338     // First, get the two buffers' addresses. Another checker will have already
1339     // made sure they're not undefined.
1340     DefinedOrUnknownSVal LV =
1341         State->getSVal(Left.Expression, LCtx).castAs<DefinedOrUnknownSVal>();
1342     DefinedOrUnknownSVal RV =
1343         State->getSVal(Right.Expression, LCtx).castAs<DefinedOrUnknownSVal>();
1344 
1345     // See if they are the same.
1346     ProgramStateRef SameBuffer, NotSameBuffer;
1347     std::tie(SameBuffer, NotSameBuffer) =
1348         State->assume(Builder.evalEQ(State, LV, RV));
1349 
1350     // If the two arguments are the same buffer, we know the result is 0,
1351     // and we only need to check one size.
1352     if (SameBuffer && !NotSameBuffer) {
1353       State = SameBuffer;
1354       State = CheckBufferAccess(C, State, Left, Size, AccessKind::read);
1355       if (State) {
1356         State =
1357             SameBuffer->BindExpr(CE, LCtx, Builder.makeZeroVal(CE->getType()));
1358         C.addTransition(State);
1359       }
1360       return;
1361     }
1362 
1363     // If the two arguments might be different buffers, we have to check
1364     // the size of both of them.
1365     assert(NotSameBuffer);
1366     State = CheckBufferAccess(C, State, Right, Size, AccessKind::read);
1367     State = CheckBufferAccess(C, State, Left, Size, AccessKind::read);
1368     if (State) {
1369       // The return value is the comparison result, which we don't know.
1370       SVal CmpV = Builder.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
1371       State = State->BindExpr(CE, LCtx, CmpV);
1372       C.addTransition(State);
1373     }
1374   }
1375 }
1376 
1377 void CStringChecker::evalstrLength(CheckerContext &C,
1378                                    const CallExpr *CE) const {
1379   // size_t strlen(const char *s);
1380   evalstrLengthCommon(C, CE, /* IsStrnlen = */ false);
1381 }
1382 
1383 void CStringChecker::evalstrnLength(CheckerContext &C,
1384                                     const CallExpr *CE) const {
1385   // size_t strnlen(const char *s, size_t maxlen);
1386   evalstrLengthCommon(C, CE, /* IsStrnlen = */ true);
1387 }
1388 
1389 void CStringChecker::evalstrLengthCommon(CheckerContext &C, const CallExpr *CE,
1390                                          bool IsStrnlen) const {
1391   CurrentFunctionDescription = "string length function";
1392   ProgramStateRef state = C.getState();
1393   const LocationContext *LCtx = C.getLocationContext();
1394 
1395   if (IsStrnlen) {
1396     const Expr *maxlenExpr = CE->getArg(1);
1397     SVal maxlenVal = state->getSVal(maxlenExpr, LCtx);
1398 
1399     ProgramStateRef stateZeroSize, stateNonZeroSize;
1400     std::tie(stateZeroSize, stateNonZeroSize) =
1401       assumeZero(C, state, maxlenVal, maxlenExpr->getType());
1402 
1403     // If the size can be zero, the result will be 0 in that case, and we don't
1404     // have to check the string itself.
1405     if (stateZeroSize) {
1406       SVal zero = C.getSValBuilder().makeZeroVal(CE->getType());
1407       stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, zero);
1408       C.addTransition(stateZeroSize);
1409     }
1410 
1411     // If the size is GUARANTEED to be zero, we're done!
1412     if (!stateNonZeroSize)
1413       return;
1414 
1415     // Otherwise, record the assumption that the size is nonzero.
1416     state = stateNonZeroSize;
1417   }
1418 
1419   // Check that the string argument is non-null.
1420   AnyArgExpr Arg = {CE->getArg(0), 0};
1421   SVal ArgVal = state->getSVal(Arg.Expression, LCtx);
1422   state = checkNonNull(C, state, Arg, ArgVal);
1423 
1424   if (!state)
1425     return;
1426 
1427   SVal strLength = getCStringLength(C, state, Arg.Expression, ArgVal);
1428 
1429   // If the argument isn't a valid C string, there's no valid state to
1430   // transition to.
1431   if (strLength.isUndef())
1432     return;
1433 
1434   DefinedOrUnknownSVal result = UnknownVal();
1435 
1436   // If the check is for strnlen() then bind the return value to no more than
1437   // the maxlen value.
1438   if (IsStrnlen) {
1439     QualType cmpTy = C.getSValBuilder().getConditionType();
1440 
1441     // It's a little unfortunate to be getting this again,
1442     // but it's not that expensive...
1443     const Expr *maxlenExpr = CE->getArg(1);
1444     SVal maxlenVal = state->getSVal(maxlenExpr, LCtx);
1445 
1446     Optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>();
1447     Optional<NonLoc> maxlenValNL = maxlenVal.getAs<NonLoc>();
1448 
1449     if (strLengthNL && maxlenValNL) {
1450       ProgramStateRef stateStringTooLong, stateStringNotTooLong;
1451 
1452       // Check if the strLength is greater than the maxlen.
1453       std::tie(stateStringTooLong, stateStringNotTooLong) = state->assume(
1454           C.getSValBuilder()
1455               .evalBinOpNN(state, BO_GT, *strLengthNL, *maxlenValNL, cmpTy)
1456               .castAs<DefinedOrUnknownSVal>());
1457 
1458       if (stateStringTooLong && !stateStringNotTooLong) {
1459         // If the string is longer than maxlen, return maxlen.
1460         result = *maxlenValNL;
1461       } else if (stateStringNotTooLong && !stateStringTooLong) {
1462         // If the string is shorter than maxlen, return its length.
1463         result = *strLengthNL;
1464       }
1465     }
1466 
1467     if (result.isUnknown()) {
1468       // If we don't have enough information for a comparison, there's
1469       // no guarantee the full string length will actually be returned.
1470       // All we know is the return value is the min of the string length
1471       // and the limit. This is better than nothing.
1472       result = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx,
1473                                                    C.blockCount());
1474       NonLoc resultNL = result.castAs<NonLoc>();
1475 
1476       if (strLengthNL) {
1477         state = state->assume(C.getSValBuilder().evalBinOpNN(
1478                                   state, BO_LE, resultNL, *strLengthNL, cmpTy)
1479                                   .castAs<DefinedOrUnknownSVal>(), true);
1480       }
1481 
1482       if (maxlenValNL) {
1483         state = state->assume(C.getSValBuilder().evalBinOpNN(
1484                                   state, BO_LE, resultNL, *maxlenValNL, cmpTy)
1485                                   .castAs<DefinedOrUnknownSVal>(), true);
1486       }
1487     }
1488 
1489   } else {
1490     // This is a plain strlen(), not strnlen().
1491     result = strLength.castAs<DefinedOrUnknownSVal>();
1492 
1493     // If we don't know the length of the string, conjure a return
1494     // value, so it can be used in constraints, at least.
1495     if (result.isUnknown()) {
1496       result = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx,
1497                                                    C.blockCount());
1498     }
1499   }
1500 
1501   // Bind the return value.
1502   assert(!result.isUnknown() && "Should have conjured a value by now");
1503   state = state->BindExpr(CE, LCtx, result);
1504   C.addTransition(state);
1505 }
1506 
1507 void CStringChecker::evalStrcpy(CheckerContext &C, const CallExpr *CE) const {
1508   // char *strcpy(char *restrict dst, const char *restrict src);
1509   evalStrcpyCommon(C, CE,
1510                    /* ReturnEnd = */ false,
1511                    /* IsBounded = */ false,
1512                    /* appendK = */ ConcatFnKind::none);
1513 }
1514 
1515 void CStringChecker::evalStrncpy(CheckerContext &C, const CallExpr *CE) const {
1516   // char *strncpy(char *restrict dst, const char *restrict src, size_t n);
1517   evalStrcpyCommon(C, CE,
1518                    /* ReturnEnd = */ false,
1519                    /* IsBounded = */ true,
1520                    /* appendK = */ ConcatFnKind::none);
1521 }
1522 
1523 void CStringChecker::evalStpcpy(CheckerContext &C, const CallExpr *CE) const {
1524   // char *stpcpy(char *restrict dst, const char *restrict src);
1525   evalStrcpyCommon(C, CE,
1526                    /* ReturnEnd = */ true,
1527                    /* IsBounded = */ false,
1528                    /* appendK = */ ConcatFnKind::none);
1529 }
1530 
1531 void CStringChecker::evalStrlcpy(CheckerContext &C, const CallExpr *CE) const {
1532   // size_t strlcpy(char *dest, const char *src, size_t size);
1533   evalStrcpyCommon(C, CE,
1534                    /* ReturnEnd = */ true,
1535                    /* IsBounded = */ true,
1536                    /* appendK = */ ConcatFnKind::none,
1537                    /* returnPtr = */ false);
1538 }
1539 
1540 void CStringChecker::evalStrcat(CheckerContext &C, const CallExpr *CE) const {
1541   // char *strcat(char *restrict s1, const char *restrict s2);
1542   evalStrcpyCommon(C, CE,
1543                    /* ReturnEnd = */ false,
1544                    /* IsBounded = */ false,
1545                    /* appendK = */ ConcatFnKind::strcat);
1546 }
1547 
1548 void CStringChecker::evalStrncat(CheckerContext &C, const CallExpr *CE) const {
1549   // char *strncat(char *restrict s1, const char *restrict s2, size_t n);
1550   evalStrcpyCommon(C, CE,
1551                    /* ReturnEnd = */ false,
1552                    /* IsBounded = */ true,
1553                    /* appendK = */ ConcatFnKind::strcat);
1554 }
1555 
1556 void CStringChecker::evalStrlcat(CheckerContext &C, const CallExpr *CE) const {
1557   // size_t strlcat(char *dst, const char *src, size_t size);
1558   // It will append at most size - strlen(dst) - 1 bytes,
1559   // NULL-terminating the result.
1560   evalStrcpyCommon(C, CE,
1561                    /* ReturnEnd = */ false,
1562                    /* IsBounded = */ true,
1563                    /* appendK = */ ConcatFnKind::strlcat,
1564                    /* returnPtr = */ false);
1565 }
1566 
1567 void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE,
1568                                       bool ReturnEnd, bool IsBounded,
1569                                       ConcatFnKind appendK,
1570                                       bool returnPtr) const {
1571   if (appendK == ConcatFnKind::none)
1572     CurrentFunctionDescription = "string copy function";
1573   else
1574     CurrentFunctionDescription = "string concatenation function";
1575 
1576   ProgramStateRef state = C.getState();
1577   const LocationContext *LCtx = C.getLocationContext();
1578 
1579   // Check that the destination is non-null.
1580   DestinationArgExpr Dst = {CE->getArg(0), 0};
1581   SVal DstVal = state->getSVal(Dst.Expression, LCtx);
1582   state = checkNonNull(C, state, Dst, DstVal);
1583   if (!state)
1584     return;
1585 
1586   // Check that the source is non-null.
1587   SourceArgExpr srcExpr = {CE->getArg(1), 1};
1588   SVal srcVal = state->getSVal(srcExpr.Expression, LCtx);
1589   state = checkNonNull(C, state, srcExpr, srcVal);
1590   if (!state)
1591     return;
1592 
1593   // Get the string length of the source.
1594   SVal strLength = getCStringLength(C, state, srcExpr.Expression, srcVal);
1595   Optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>();
1596 
1597   // Get the string length of the destination buffer.
1598   SVal dstStrLength = getCStringLength(C, state, Dst.Expression, DstVal);
1599   Optional<NonLoc> dstStrLengthNL = dstStrLength.getAs<NonLoc>();
1600 
1601   // If the source isn't a valid C string, give up.
1602   if (strLength.isUndef())
1603     return;
1604 
1605   SValBuilder &svalBuilder = C.getSValBuilder();
1606   QualType cmpTy = svalBuilder.getConditionType();
1607   QualType sizeTy = svalBuilder.getContext().getSizeType();
1608 
1609   // These two values allow checking two kinds of errors:
1610   // - actual overflows caused by a source that doesn't fit in the destination
1611   // - potential overflows caused by a bound that could exceed the destination
1612   SVal amountCopied = UnknownVal();
1613   SVal maxLastElementIndex = UnknownVal();
1614   const char *boundWarning = nullptr;
1615 
1616   // FIXME: Why do we choose the srcExpr if the access has no size?
1617   //  Note that the 3rd argument of the call would be the size parameter.
1618   SizeArgExpr SrcExprAsSizeDummy = {srcExpr.Expression, srcExpr.ArgumentIndex};
1619   state = CheckOverlap(
1620       C, state,
1621       (IsBounded ? SizeArgExpr{CE->getArg(2), 2} : SrcExprAsSizeDummy), Dst,
1622       srcExpr);
1623 
1624   if (!state)
1625     return;
1626 
1627   // If the function is strncpy, strncat, etc... it is bounded.
1628   if (IsBounded) {
1629     // Get the max number of characters to copy.
1630     SizeArgExpr lenExpr = {CE->getArg(2), 2};
1631     SVal lenVal = state->getSVal(lenExpr.Expression, LCtx);
1632 
1633     // Protect against misdeclared strncpy().
1634     lenVal =
1635         svalBuilder.evalCast(lenVal, sizeTy, lenExpr.Expression->getType());
1636 
1637     Optional<NonLoc> lenValNL = lenVal.getAs<NonLoc>();
1638 
1639     // If we know both values, we might be able to figure out how much
1640     // we're copying.
1641     if (strLengthNL && lenValNL) {
1642       switch (appendK) {
1643       case ConcatFnKind::none:
1644       case ConcatFnKind::strcat: {
1645         ProgramStateRef stateSourceTooLong, stateSourceNotTooLong;
1646         // Check if the max number to copy is less than the length of the src.
1647         // If the bound is equal to the source length, strncpy won't null-
1648         // terminate the result!
1649         std::tie(stateSourceTooLong, stateSourceNotTooLong) = state->assume(
1650             svalBuilder
1651                 .evalBinOpNN(state, BO_GE, *strLengthNL, *lenValNL, cmpTy)
1652                 .castAs<DefinedOrUnknownSVal>());
1653 
1654         if (stateSourceTooLong && !stateSourceNotTooLong) {
1655           // Max number to copy is less than the length of the src, so the
1656           // actual strLength copied is the max number arg.
1657           state = stateSourceTooLong;
1658           amountCopied = lenVal;
1659 
1660         } else if (!stateSourceTooLong && stateSourceNotTooLong) {
1661           // The source buffer entirely fits in the bound.
1662           state = stateSourceNotTooLong;
1663           amountCopied = strLength;
1664         }
1665         break;
1666       }
1667       case ConcatFnKind::strlcat:
1668         if (!dstStrLengthNL)
1669           return;
1670 
1671         // amountCopied = min (size - dstLen - 1 , srcLen)
1672         SVal freeSpace = svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL,
1673                                                  *dstStrLengthNL, sizeTy);
1674         if (!freeSpace.getAs<NonLoc>())
1675           return;
1676         freeSpace =
1677             svalBuilder.evalBinOp(state, BO_Sub, freeSpace,
1678                                   svalBuilder.makeIntVal(1, sizeTy), sizeTy);
1679         Optional<NonLoc> freeSpaceNL = freeSpace.getAs<NonLoc>();
1680 
1681         // While unlikely, it is possible that the subtraction is
1682         // too complex to compute, let's check whether it succeeded.
1683         if (!freeSpaceNL)
1684           return;
1685         SVal hasEnoughSpace = svalBuilder.evalBinOpNN(
1686             state, BO_LE, *strLengthNL, *freeSpaceNL, cmpTy);
1687 
1688         ProgramStateRef TrueState, FalseState;
1689         std::tie(TrueState, FalseState) =
1690             state->assume(hasEnoughSpace.castAs<DefinedOrUnknownSVal>());
1691 
1692         // srcStrLength <= size - dstStrLength -1
1693         if (TrueState && !FalseState) {
1694           amountCopied = strLength;
1695         }
1696 
1697         // srcStrLength > size - dstStrLength -1
1698         if (!TrueState && FalseState) {
1699           amountCopied = freeSpace;
1700         }
1701 
1702         if (TrueState && FalseState)
1703           amountCopied = UnknownVal();
1704         break;
1705       }
1706     }
1707     // We still want to know if the bound is known to be too large.
1708     if (lenValNL) {
1709       switch (appendK) {
1710       case ConcatFnKind::strcat:
1711         // For strncat, the check is strlen(dst) + lenVal < sizeof(dst)
1712 
1713         // Get the string length of the destination. If the destination is
1714         // memory that can't have a string length, we shouldn't be copying
1715         // into it anyway.
1716         if (dstStrLength.isUndef())
1717           return;
1718 
1719         if (dstStrLengthNL) {
1720           maxLastElementIndex = svalBuilder.evalBinOpNN(
1721               state, BO_Add, *lenValNL, *dstStrLengthNL, sizeTy);
1722 
1723           boundWarning = "Size argument is greater than the free space in the "
1724                          "destination buffer";
1725         }
1726         break;
1727       case ConcatFnKind::none:
1728       case ConcatFnKind::strlcat:
1729         // For strncpy and strlcat, this is just checking
1730         //  that lenVal <= sizeof(dst).
1731         // (Yes, strncpy and strncat differ in how they treat termination.
1732         // strncat ALWAYS terminates, but strncpy doesn't.)
1733 
1734         // We need a special case for when the copy size is zero, in which
1735         // case strncpy will do no work at all. Our bounds check uses n-1
1736         // as the last element accessed, so n == 0 is problematic.
1737         ProgramStateRef StateZeroSize, StateNonZeroSize;
1738         std::tie(StateZeroSize, StateNonZeroSize) =
1739             assumeZero(C, state, *lenValNL, sizeTy);
1740 
1741         // If the size is known to be zero, we're done.
1742         if (StateZeroSize && !StateNonZeroSize) {
1743           if (returnPtr) {
1744             StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, DstVal);
1745           } else {
1746             if (appendK == ConcatFnKind::none) {
1747               // strlcpy returns strlen(src)
1748               StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, strLength);
1749             } else {
1750               // strlcat returns strlen(src) + strlen(dst)
1751               SVal retSize = svalBuilder.evalBinOp(
1752                   state, BO_Add, strLength, dstStrLength, sizeTy);
1753               StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, retSize);
1754             }
1755           }
1756           C.addTransition(StateZeroSize);
1757           return;
1758         }
1759 
1760         // Otherwise, go ahead and figure out the last element we'll touch.
1761         // We don't record the non-zero assumption here because we can't
1762         // be sure. We won't warn on a possible zero.
1763         NonLoc one = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>();
1764         maxLastElementIndex =
1765             svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL, one, sizeTy);
1766         boundWarning = "Size argument is greater than the length of the "
1767                        "destination buffer";
1768         break;
1769       }
1770     }
1771   } else {
1772     // The function isn't bounded. The amount copied should match the length
1773     // of the source buffer.
1774     amountCopied = strLength;
1775   }
1776 
1777   assert(state);
1778 
1779   // This represents the number of characters copied into the destination
1780   // buffer. (It may not actually be the strlen if the destination buffer
1781   // is not terminated.)
1782   SVal finalStrLength = UnknownVal();
1783   SVal strlRetVal = UnknownVal();
1784 
1785   if (appendK == ConcatFnKind::none && !returnPtr) {
1786     // strlcpy returns the sizeof(src)
1787     strlRetVal = strLength;
1788   }
1789 
1790   // If this is an appending function (strcat, strncat...) then set the
1791   // string length to strlen(src) + strlen(dst) since the buffer will
1792   // ultimately contain both.
1793   if (appendK != ConcatFnKind::none) {
1794     // Get the string length of the destination. If the destination is memory
1795     // that can't have a string length, we shouldn't be copying into it anyway.
1796     if (dstStrLength.isUndef())
1797       return;
1798 
1799     if (appendK == ConcatFnKind::strlcat && dstStrLengthNL && strLengthNL) {
1800       strlRetVal = svalBuilder.evalBinOpNN(state, BO_Add, *strLengthNL,
1801                                            *dstStrLengthNL, sizeTy);
1802     }
1803 
1804     Optional<NonLoc> amountCopiedNL = amountCopied.getAs<NonLoc>();
1805 
1806     // If we know both string lengths, we might know the final string length.
1807     if (amountCopiedNL && dstStrLengthNL) {
1808       // Make sure the two lengths together don't overflow a size_t.
1809       state = checkAdditionOverflow(C, state, *amountCopiedNL, *dstStrLengthNL);
1810       if (!state)
1811         return;
1812 
1813       finalStrLength = svalBuilder.evalBinOpNN(state, BO_Add, *amountCopiedNL,
1814                                                *dstStrLengthNL, sizeTy);
1815     }
1816 
1817     // If we couldn't get a single value for the final string length,
1818     // we can at least bound it by the individual lengths.
1819     if (finalStrLength.isUnknown()) {
1820       // Try to get a "hypothetical" string length symbol, which we can later
1821       // set as a real value if that turns out to be the case.
1822       finalStrLength = getCStringLength(C, state, CE, DstVal, true);
1823       assert(!finalStrLength.isUndef());
1824 
1825       if (Optional<NonLoc> finalStrLengthNL = finalStrLength.getAs<NonLoc>()) {
1826         if (amountCopiedNL && appendK == ConcatFnKind::none) {
1827           // we overwrite dst string with the src
1828           // finalStrLength >= srcStrLength
1829           SVal sourceInResult = svalBuilder.evalBinOpNN(
1830               state, BO_GE, *finalStrLengthNL, *amountCopiedNL, cmpTy);
1831           state = state->assume(sourceInResult.castAs<DefinedOrUnknownSVal>(),
1832                                 true);
1833           if (!state)
1834             return;
1835         }
1836 
1837         if (dstStrLengthNL && appendK != ConcatFnKind::none) {
1838           // we extend the dst string with the src
1839           // finalStrLength >= dstStrLength
1840           SVal destInResult = svalBuilder.evalBinOpNN(state, BO_GE,
1841                                                       *finalStrLengthNL,
1842                                                       *dstStrLengthNL,
1843                                                       cmpTy);
1844           state =
1845               state->assume(destInResult.castAs<DefinedOrUnknownSVal>(), true);
1846           if (!state)
1847             return;
1848         }
1849       }
1850     }
1851 
1852   } else {
1853     // Otherwise, this is a copy-over function (strcpy, strncpy, ...), and
1854     // the final string length will match the input string length.
1855     finalStrLength = amountCopied;
1856   }
1857 
1858   SVal Result;
1859 
1860   if (returnPtr) {
1861     // The final result of the function will either be a pointer past the last
1862     // copied element, or a pointer to the start of the destination buffer.
1863     Result = (ReturnEnd ? UnknownVal() : DstVal);
1864   } else {
1865     if (appendK == ConcatFnKind::strlcat || appendK == ConcatFnKind::none)
1866       //strlcpy, strlcat
1867       Result = strlRetVal;
1868     else
1869       Result = finalStrLength;
1870   }
1871 
1872   assert(state);
1873 
1874   // If the destination is a MemRegion, try to check for a buffer overflow and
1875   // record the new string length.
1876   if (Optional<loc::MemRegionVal> dstRegVal =
1877       DstVal.getAs<loc::MemRegionVal>()) {
1878     QualType ptrTy = Dst.Expression->getType();
1879 
1880     // If we have an exact value on a bounded copy, use that to check for
1881     // overflows, rather than our estimate about how much is actually copied.
1882     if (Optional<NonLoc> maxLastNL = maxLastElementIndex.getAs<NonLoc>()) {
1883       SVal maxLastElement =
1884           svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal, *maxLastNL, ptrTy);
1885 
1886       state = CheckLocation(C, state, Dst, maxLastElement, AccessKind::write);
1887       if (!state)
1888         return;
1889     }
1890 
1891     // Then, if the final length is known...
1892     if (Optional<NonLoc> knownStrLength = finalStrLength.getAs<NonLoc>()) {
1893       SVal lastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal,
1894           *knownStrLength, ptrTy);
1895 
1896       // ...and we haven't checked the bound, we'll check the actual copy.
1897       if (!boundWarning) {
1898         state = CheckLocation(C, state, Dst, lastElement, AccessKind::write);
1899         if (!state)
1900           return;
1901       }
1902 
1903       // If this is a stpcpy-style copy, the last element is the return value.
1904       if (returnPtr && ReturnEnd)
1905         Result = lastElement;
1906     }
1907 
1908     // Invalidate the destination (regular invalidation without pointer-escaping
1909     // the address of the top-level region). This must happen before we set the
1910     // C string length because invalidation will clear the length.
1911     // FIXME: Even if we can't perfectly model the copy, we should see if we
1912     // can use LazyCompoundVals to copy the source values into the destination.
1913     // This would probably remove any existing bindings past the end of the
1914     // string, but that's still an improvement over blank invalidation.
1915     state = InvalidateBuffer(C, state, Dst.Expression, *dstRegVal,
1916                              /*IsSourceBuffer*/ false, nullptr);
1917 
1918     // Invalidate the source (const-invalidation without const-pointer-escaping
1919     // the address of the top-level region).
1920     state = InvalidateBuffer(C, state, srcExpr.Expression, srcVal,
1921                              /*IsSourceBuffer*/ true, nullptr);
1922 
1923     // Set the C string length of the destination, if we know it.
1924     if (IsBounded && (appendK == ConcatFnKind::none)) {
1925       // strncpy is annoying in that it doesn't guarantee to null-terminate
1926       // the result string. If the original string didn't fit entirely inside
1927       // the bound (including the null-terminator), we don't know how long the
1928       // result is.
1929       if (amountCopied != strLength)
1930         finalStrLength = UnknownVal();
1931     }
1932     state = setCStringLength(state, dstRegVal->getRegion(), finalStrLength);
1933   }
1934 
1935   assert(state);
1936 
1937   if (returnPtr) {
1938     // If this is a stpcpy-style copy, but we were unable to check for a buffer
1939     // overflow, we still need a result. Conjure a return value.
1940     if (ReturnEnd && Result.isUnknown()) {
1941       Result = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
1942     }
1943   }
1944   // Set the return value.
1945   state = state->BindExpr(CE, LCtx, Result);
1946   C.addTransition(state);
1947 }
1948 
1949 void CStringChecker::evalStrcmp(CheckerContext &C, const CallExpr *CE) const {
1950   //int strcmp(const char *s1, const char *s2);
1951   evalStrcmpCommon(C, CE, /* IsBounded = */ false, /* IgnoreCase = */ false);
1952 }
1953 
1954 void CStringChecker::evalStrncmp(CheckerContext &C, const CallExpr *CE) const {
1955   //int strncmp(const char *s1, const char *s2, size_t n);
1956   evalStrcmpCommon(C, CE, /* IsBounded = */ true, /* IgnoreCase = */ false);
1957 }
1958 
1959 void CStringChecker::evalStrcasecmp(CheckerContext &C,
1960     const CallExpr *CE) const {
1961   //int strcasecmp(const char *s1, const char *s2);
1962   evalStrcmpCommon(C, CE, /* IsBounded = */ false, /* IgnoreCase = */ true);
1963 }
1964 
1965 void CStringChecker::evalStrncasecmp(CheckerContext &C,
1966     const CallExpr *CE) const {
1967   //int strncasecmp(const char *s1, const char *s2, size_t n);
1968   evalStrcmpCommon(C, CE, /* IsBounded = */ true, /* IgnoreCase = */ true);
1969 }
1970 
1971 void CStringChecker::evalStrcmpCommon(CheckerContext &C, const CallExpr *CE,
1972     bool IsBounded, bool IgnoreCase) const {
1973   CurrentFunctionDescription = "string comparison function";
1974   ProgramStateRef state = C.getState();
1975   const LocationContext *LCtx = C.getLocationContext();
1976 
1977   // Check that the first string is non-null
1978   AnyArgExpr Left = {CE->getArg(0), 0};
1979   SVal LeftVal = state->getSVal(Left.Expression, LCtx);
1980   state = checkNonNull(C, state, Left, LeftVal);
1981   if (!state)
1982     return;
1983 
1984   // Check that the second string is non-null.
1985   AnyArgExpr Right = {CE->getArg(1), 1};
1986   SVal RightVal = state->getSVal(Right.Expression, LCtx);
1987   state = checkNonNull(C, state, Right, RightVal);
1988   if (!state)
1989     return;
1990 
1991   // Get the string length of the first string or give up.
1992   SVal LeftLength = getCStringLength(C, state, Left.Expression, LeftVal);
1993   if (LeftLength.isUndef())
1994     return;
1995 
1996   // Get the string length of the second string or give up.
1997   SVal RightLength = getCStringLength(C, state, Right.Expression, RightVal);
1998   if (RightLength.isUndef())
1999     return;
2000 
2001   // If we know the two buffers are the same, we know the result is 0.
2002   // First, get the two buffers' addresses. Another checker will have already
2003   // made sure they're not undefined.
2004   DefinedOrUnknownSVal LV = LeftVal.castAs<DefinedOrUnknownSVal>();
2005   DefinedOrUnknownSVal RV = RightVal.castAs<DefinedOrUnknownSVal>();
2006 
2007   // See if they are the same.
2008   SValBuilder &svalBuilder = C.getSValBuilder();
2009   DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV);
2010   ProgramStateRef StSameBuf, StNotSameBuf;
2011   std::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf);
2012 
2013   // If the two arguments might be the same buffer, we know the result is 0,
2014   // and we only need to check one size.
2015   if (StSameBuf) {
2016     StSameBuf = StSameBuf->BindExpr(CE, LCtx,
2017         svalBuilder.makeZeroVal(CE->getType()));
2018     C.addTransition(StSameBuf);
2019 
2020     // If the two arguments are GUARANTEED to be the same, we're done!
2021     if (!StNotSameBuf)
2022       return;
2023   }
2024 
2025   assert(StNotSameBuf);
2026   state = StNotSameBuf;
2027 
2028   // At this point we can go about comparing the two buffers.
2029   // For now, we only do this if they're both known string literals.
2030 
2031   // Attempt to extract string literals from both expressions.
2032   const StringLiteral *LeftStrLiteral =
2033       getCStringLiteral(C, state, Left.Expression, LeftVal);
2034   const StringLiteral *RightStrLiteral =
2035       getCStringLiteral(C, state, Right.Expression, RightVal);
2036   bool canComputeResult = false;
2037   SVal resultVal = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx,
2038       C.blockCount());
2039 
2040   if (LeftStrLiteral && RightStrLiteral) {
2041     StringRef LeftStrRef = LeftStrLiteral->getString();
2042     StringRef RightStrRef = RightStrLiteral->getString();
2043 
2044     if (IsBounded) {
2045       // Get the max number of characters to compare.
2046       const Expr *lenExpr = CE->getArg(2);
2047       SVal lenVal = state->getSVal(lenExpr, LCtx);
2048 
2049       // If the length is known, we can get the right substrings.
2050       if (const llvm::APSInt *len = svalBuilder.getKnownValue(state, lenVal)) {
2051         // Create substrings of each to compare the prefix.
2052         LeftStrRef = LeftStrRef.substr(0, (size_t)len->getZExtValue());
2053         RightStrRef = RightStrRef.substr(0, (size_t)len->getZExtValue());
2054         canComputeResult = true;
2055       }
2056     } else {
2057       // This is a normal, unbounded strcmp.
2058       canComputeResult = true;
2059     }
2060 
2061     if (canComputeResult) {
2062       // Real strcmp stops at null characters.
2063       size_t s1Term = LeftStrRef.find('\0');
2064       if (s1Term != StringRef::npos)
2065         LeftStrRef = LeftStrRef.substr(0, s1Term);
2066 
2067       size_t s2Term = RightStrRef.find('\0');
2068       if (s2Term != StringRef::npos)
2069         RightStrRef = RightStrRef.substr(0, s2Term);
2070 
2071       // Use StringRef's comparison methods to compute the actual result.
2072       int compareRes = IgnoreCase ? LeftStrRef.compare_insensitive(RightStrRef)
2073                                   : LeftStrRef.compare(RightStrRef);
2074 
2075       // The strcmp function returns an integer greater than, equal to, or less
2076       // than zero, [c11, p7.24.4.2].
2077       if (compareRes == 0) {
2078         resultVal = svalBuilder.makeIntVal(compareRes, CE->getType());
2079       }
2080       else {
2081         DefinedSVal zeroVal = svalBuilder.makeIntVal(0, CE->getType());
2082         // Constrain strcmp's result range based on the result of StringRef's
2083         // comparison methods.
2084         BinaryOperatorKind op = (compareRes == 1) ? BO_GT : BO_LT;
2085         SVal compareWithZero =
2086           svalBuilder.evalBinOp(state, op, resultVal, zeroVal,
2087               svalBuilder.getConditionType());
2088         DefinedSVal compareWithZeroVal = compareWithZero.castAs<DefinedSVal>();
2089         state = state->assume(compareWithZeroVal, true);
2090       }
2091     }
2092   }
2093 
2094   state = state->BindExpr(CE, LCtx, resultVal);
2095 
2096   // Record this as a possible path.
2097   C.addTransition(state);
2098 }
2099 
2100 void CStringChecker::evalStrsep(CheckerContext &C, const CallExpr *CE) const {
2101   // char *strsep(char **stringp, const char *delim);
2102   // Verify whether the search string parameter matches the return type.
2103   SourceArgExpr SearchStrPtr = {CE->getArg(0), 0};
2104 
2105   QualType CharPtrTy = SearchStrPtr.Expression->getType()->getPointeeType();
2106   if (CharPtrTy.isNull() ||
2107       CE->getType().getUnqualifiedType() != CharPtrTy.getUnqualifiedType())
2108     return;
2109 
2110   CurrentFunctionDescription = "strsep()";
2111   ProgramStateRef State = C.getState();
2112   const LocationContext *LCtx = C.getLocationContext();
2113 
2114   // Check that the search string pointer is non-null (though it may point to
2115   // a null string).
2116   SVal SearchStrVal = State->getSVal(SearchStrPtr.Expression, LCtx);
2117   State = checkNonNull(C, State, SearchStrPtr, SearchStrVal);
2118   if (!State)
2119     return;
2120 
2121   // Check that the delimiter string is non-null.
2122   AnyArgExpr DelimStr = {CE->getArg(1), 1};
2123   SVal DelimStrVal = State->getSVal(DelimStr.Expression, LCtx);
2124   State = checkNonNull(C, State, DelimStr, DelimStrVal);
2125   if (!State)
2126     return;
2127 
2128   SValBuilder &SVB = C.getSValBuilder();
2129   SVal Result;
2130   if (Optional<Loc> SearchStrLoc = SearchStrVal.getAs<Loc>()) {
2131     // Get the current value of the search string pointer, as a char*.
2132     Result = State->getSVal(*SearchStrLoc, CharPtrTy);
2133 
2134     // Invalidate the search string, representing the change of one delimiter
2135     // character to NUL.
2136     State = InvalidateBuffer(C, State, SearchStrPtr.Expression, Result,
2137                              /*IsSourceBuffer*/ false, nullptr);
2138 
2139     // Overwrite the search string pointer. The new value is either an address
2140     // further along in the same string, or NULL if there are no more tokens.
2141     State = State->bindLoc(*SearchStrLoc,
2142         SVB.conjureSymbolVal(getTag(),
2143           CE,
2144           LCtx,
2145           CharPtrTy,
2146           C.blockCount()),
2147         LCtx);
2148   } else {
2149     assert(SearchStrVal.isUnknown());
2150     // Conjure a symbolic value. It's the best we can do.
2151     Result = SVB.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
2152   }
2153 
2154   // Set the return value, and finish.
2155   State = State->BindExpr(CE, LCtx, Result);
2156   C.addTransition(State);
2157 }
2158 
2159 // These should probably be moved into a C++ standard library checker.
2160 void CStringChecker::evalStdCopy(CheckerContext &C, const CallExpr *CE) const {
2161   evalStdCopyCommon(C, CE);
2162 }
2163 
2164 void CStringChecker::evalStdCopyBackward(CheckerContext &C,
2165     const CallExpr *CE) const {
2166   evalStdCopyCommon(C, CE);
2167 }
2168 
2169 void CStringChecker::evalStdCopyCommon(CheckerContext &C,
2170     const CallExpr *CE) const {
2171   if (!CE->getArg(2)->getType()->isPointerType())
2172     return;
2173 
2174   ProgramStateRef State = C.getState();
2175 
2176   const LocationContext *LCtx = C.getLocationContext();
2177 
2178   // template <class _InputIterator, class _OutputIterator>
2179   // _OutputIterator
2180   // copy(_InputIterator __first, _InputIterator __last,
2181   //        _OutputIterator __result)
2182 
2183   // Invalidate the destination buffer
2184   const Expr *Dst = CE->getArg(2);
2185   SVal DstVal = State->getSVal(Dst, LCtx);
2186   State = InvalidateBuffer(C, State, Dst, DstVal, /*IsSource=*/false,
2187       /*Size=*/nullptr);
2188 
2189   SValBuilder &SVB = C.getSValBuilder();
2190 
2191   SVal ResultVal = SVB.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
2192   State = State->BindExpr(CE, LCtx, ResultVal);
2193 
2194   C.addTransition(State);
2195 }
2196 
2197 void CStringChecker::evalMemset(CheckerContext &C, const CallExpr *CE) const {
2198   // void *memset(void *s, int c, size_t n);
2199   CurrentFunctionDescription = "memory set function";
2200 
2201   DestinationArgExpr Buffer = {CE->getArg(0), 0};
2202   AnyArgExpr CharE = {CE->getArg(1), 1};
2203   SizeArgExpr Size = {CE->getArg(2), 2};
2204 
2205   ProgramStateRef State = C.getState();
2206 
2207   // See if the size argument is zero.
2208   const LocationContext *LCtx = C.getLocationContext();
2209   SVal SizeVal = C.getSVal(Size.Expression);
2210   QualType SizeTy = Size.Expression->getType();
2211 
2212   ProgramStateRef ZeroSize, NonZeroSize;
2213   std::tie(ZeroSize, NonZeroSize) = assumeZero(C, State, SizeVal, SizeTy);
2214 
2215   // Get the value of the memory area.
2216   SVal BufferPtrVal = C.getSVal(Buffer.Expression);
2217 
2218   // If the size is zero, there won't be any actual memory access, so
2219   // just bind the return value to the buffer and return.
2220   if (ZeroSize && !NonZeroSize) {
2221     ZeroSize = ZeroSize->BindExpr(CE, LCtx, BufferPtrVal);
2222     C.addTransition(ZeroSize);
2223     return;
2224   }
2225 
2226   // Ensure the memory area is not null.
2227   // If it is NULL there will be a NULL pointer dereference.
2228   State = checkNonNull(C, NonZeroSize, Buffer, BufferPtrVal);
2229   if (!State)
2230     return;
2231 
2232   State = CheckBufferAccess(C, State, Buffer, Size, AccessKind::write);
2233   if (!State)
2234     return;
2235 
2236   // According to the values of the arguments, bind the value of the second
2237   // argument to the destination buffer and set string length, or just
2238   // invalidate the destination buffer.
2239   if (!memsetAux(Buffer.Expression, C.getSVal(CharE.Expression),
2240                  Size.Expression, C, State))
2241     return;
2242 
2243   State = State->BindExpr(CE, LCtx, BufferPtrVal);
2244   C.addTransition(State);
2245 }
2246 
2247 void CStringChecker::evalBzero(CheckerContext &C, const CallExpr *CE) const {
2248   CurrentFunctionDescription = "memory clearance function";
2249 
2250   DestinationArgExpr Buffer = {CE->getArg(0), 0};
2251   SizeArgExpr Size = {CE->getArg(1), 1};
2252   SVal Zero = C.getSValBuilder().makeZeroVal(C.getASTContext().IntTy);
2253 
2254   ProgramStateRef State = C.getState();
2255 
2256   // See if the size argument is zero.
2257   SVal SizeVal = C.getSVal(Size.Expression);
2258   QualType SizeTy = Size.Expression->getType();
2259 
2260   ProgramStateRef StateZeroSize, StateNonZeroSize;
2261   std::tie(StateZeroSize, StateNonZeroSize) =
2262     assumeZero(C, State, SizeVal, SizeTy);
2263 
2264   // If the size is zero, there won't be any actual memory access,
2265   // In this case we just return.
2266   if (StateZeroSize && !StateNonZeroSize) {
2267     C.addTransition(StateZeroSize);
2268     return;
2269   }
2270 
2271   // Get the value of the memory area.
2272   SVal MemVal = C.getSVal(Buffer.Expression);
2273 
2274   // Ensure the memory area is not null.
2275   // If it is NULL there will be a NULL pointer dereference.
2276   State = checkNonNull(C, StateNonZeroSize, Buffer, MemVal);
2277   if (!State)
2278     return;
2279 
2280   State = CheckBufferAccess(C, State, Buffer, Size, AccessKind::write);
2281   if (!State)
2282     return;
2283 
2284   if (!memsetAux(Buffer.Expression, Zero, Size.Expression, C, State))
2285     return;
2286 
2287   C.addTransition(State);
2288 }
2289 
2290 //===----------------------------------------------------------------------===//
2291 // The driver method, and other Checker callbacks.
2292 //===----------------------------------------------------------------------===//
2293 
2294 CStringChecker::FnCheck CStringChecker::identifyCall(const CallEvent &Call,
2295                                                      CheckerContext &C) const {
2296   const auto *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr());
2297   if (!CE)
2298     return nullptr;
2299 
2300   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
2301   if (!FD)
2302     return nullptr;
2303 
2304   if (StdCopy.matches(Call))
2305     return &CStringChecker::evalStdCopy;
2306   if (StdCopyBackward.matches(Call))
2307     return &CStringChecker::evalStdCopyBackward;
2308 
2309   // Pro-actively check that argument types are safe to do arithmetic upon.
2310   // We do not want to crash if someone accidentally passes a structure
2311   // into, say, a C++ overload of any of these functions. We could not check
2312   // that for std::copy because they may have arguments of other types.
2313   for (auto I : CE->arguments()) {
2314     QualType T = I->getType();
2315     if (!T->isIntegralOrEnumerationType() && !T->isPointerType())
2316       return nullptr;
2317   }
2318 
2319   const FnCheck *Callback = Callbacks.lookup(Call);
2320   if (Callback)
2321     return *Callback;
2322 
2323   return nullptr;
2324 }
2325 
2326 bool CStringChecker::evalCall(const CallEvent &Call, CheckerContext &C) const {
2327   FnCheck Callback = identifyCall(Call, C);
2328 
2329   // If the callee isn't a string function, let another checker handle it.
2330   if (!Callback)
2331     return false;
2332 
2333   // Check and evaluate the call.
2334   const auto *CE = cast<CallExpr>(Call.getOriginExpr());
2335   (this->*Callback)(C, CE);
2336 
2337   // If the evaluate call resulted in no change, chain to the next eval call
2338   // handler.
2339   // Note, the custom CString evaluation calls assume that basic safety
2340   // properties are held. However, if the user chooses to turn off some of these
2341   // checks, we ignore the issues and leave the call evaluation to a generic
2342   // handler.
2343   return C.isDifferent();
2344 }
2345 
2346 void CStringChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const {
2347   // Record string length for char a[] = "abc";
2348   ProgramStateRef state = C.getState();
2349 
2350   for (const auto *I : DS->decls()) {
2351     const VarDecl *D = dyn_cast<VarDecl>(I);
2352     if (!D)
2353       continue;
2354 
2355     // FIXME: Handle array fields of structs.
2356     if (!D->getType()->isArrayType())
2357       continue;
2358 
2359     const Expr *Init = D->getInit();
2360     if (!Init)
2361       continue;
2362     if (!isa<StringLiteral>(Init))
2363       continue;
2364 
2365     Loc VarLoc = state->getLValue(D, C.getLocationContext());
2366     const MemRegion *MR = VarLoc.getAsRegion();
2367     if (!MR)
2368       continue;
2369 
2370     SVal StrVal = C.getSVal(Init);
2371     assert(StrVal.isValid() && "Initializer string is unknown or undefined");
2372     DefinedOrUnknownSVal strLength =
2373       getCStringLength(C, state, Init, StrVal).castAs<DefinedOrUnknownSVal>();
2374 
2375     state = state->set<CStringLength>(MR, strLength);
2376   }
2377 
2378   C.addTransition(state);
2379 }
2380 
2381 ProgramStateRef
2382 CStringChecker::checkRegionChanges(ProgramStateRef state,
2383     const InvalidatedSymbols *,
2384     ArrayRef<const MemRegion *> ExplicitRegions,
2385     ArrayRef<const MemRegion *> Regions,
2386     const LocationContext *LCtx,
2387     const CallEvent *Call) const {
2388   CStringLengthTy Entries = state->get<CStringLength>();
2389   if (Entries.isEmpty())
2390     return state;
2391 
2392   llvm::SmallPtrSet<const MemRegion *, 8> Invalidated;
2393   llvm::SmallPtrSet<const MemRegion *, 32> SuperRegions;
2394 
2395   // First build sets for the changed regions and their super-regions.
2396   for (ArrayRef<const MemRegion *>::iterator
2397       I = Regions.begin(), E = Regions.end(); I != E; ++I) {
2398     const MemRegion *MR = *I;
2399     Invalidated.insert(MR);
2400 
2401     SuperRegions.insert(MR);
2402     while (const SubRegion *SR = dyn_cast<SubRegion>(MR)) {
2403       MR = SR->getSuperRegion();
2404       SuperRegions.insert(MR);
2405     }
2406   }
2407 
2408   CStringLengthTy::Factory &F = state->get_context<CStringLength>();
2409 
2410   // Then loop over the entries in the current state.
2411   for (CStringLengthTy::iterator I = Entries.begin(),
2412       E = Entries.end(); I != E; ++I) {
2413     const MemRegion *MR = I.getKey();
2414 
2415     // Is this entry for a super-region of a changed region?
2416     if (SuperRegions.count(MR)) {
2417       Entries = F.remove(Entries, MR);
2418       continue;
2419     }
2420 
2421     // Is this entry for a sub-region of a changed region?
2422     const MemRegion *Super = MR;
2423     while (const SubRegion *SR = dyn_cast<SubRegion>(Super)) {
2424       Super = SR->getSuperRegion();
2425       if (Invalidated.count(Super)) {
2426         Entries = F.remove(Entries, MR);
2427         break;
2428       }
2429     }
2430   }
2431 
2432   return state->set<CStringLength>(Entries);
2433 }
2434 
2435 void CStringChecker::checkLiveSymbols(ProgramStateRef state,
2436     SymbolReaper &SR) const {
2437   // Mark all symbols in our string length map as valid.
2438   CStringLengthTy Entries = state->get<CStringLength>();
2439 
2440   for (CStringLengthTy::iterator I = Entries.begin(), E = Entries.end();
2441       I != E; ++I) {
2442     SVal Len = I.getData();
2443 
2444     for (SymExpr::symbol_iterator si = Len.symbol_begin(),
2445         se = Len.symbol_end(); si != se; ++si)
2446       SR.markInUse(*si);
2447   }
2448 }
2449 
2450 void CStringChecker::checkDeadSymbols(SymbolReaper &SR,
2451     CheckerContext &C) const {
2452   ProgramStateRef state = C.getState();
2453   CStringLengthTy Entries = state->get<CStringLength>();
2454   if (Entries.isEmpty())
2455     return;
2456 
2457   CStringLengthTy::Factory &F = state->get_context<CStringLength>();
2458   for (CStringLengthTy::iterator I = Entries.begin(), E = Entries.end();
2459       I != E; ++I) {
2460     SVal Len = I.getData();
2461     if (SymbolRef Sym = Len.getAsSymbol()) {
2462       if (SR.isDead(Sym))
2463         Entries = F.remove(Entries, I.getKey());
2464     }
2465   }
2466 
2467   state = state->set<CStringLength>(Entries);
2468   C.addTransition(state);
2469 }
2470 
2471 void ento::registerCStringModeling(CheckerManager &Mgr) {
2472   Mgr.registerChecker<CStringChecker>();
2473 }
2474 
2475 bool ento::shouldRegisterCStringModeling(const CheckerManager &mgr) {
2476   return true;
2477 }
2478 
2479 #define REGISTER_CHECKER(name)                                                 \
2480   void ento::register##name(CheckerManager &mgr) {                             \
2481     CStringChecker *checker = mgr.getChecker<CStringChecker>();                \
2482     checker->Filter.Check##name = true;                                        \
2483     checker->Filter.CheckName##name = mgr.getCurrentCheckerName();             \
2484   }                                                                            \
2485                                                                                \
2486   bool ento::shouldRegister##name(const CheckerManager &mgr) { return true; }
2487 
2488 REGISTER_CHECKER(CStringNullArg)
2489 REGISTER_CHECKER(CStringOutOfBounds)
2490 REGISTER_CHECKER(CStringBufferOverlap)
2491 REGISTER_CHECKER(CStringNotNullTerm)
2492 REGISTER_CHECKER(CStringUninitializedRead)