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