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