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