xref: /freebsd/contrib/llvm-project/clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp (revision 972a253a57b6f144b0e4a3e2080a2a0076ec55a0)
10b57cec5SDimitry Andric //= CStringChecker.cpp - Checks calls to C string functions --------*- C++ -*-//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This defines CStringChecker, which is an assortment of checks on calls
100b57cec5SDimitry Andric // to functions in <string.h>.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "InterCheckerAPI.h"
150b57cec5SDimitry Andric #include "clang/Basic/CharInfo.h"
165ffd83dbSDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
170b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
180b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/Checker.h"
190b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/CheckerManager.h"
20349cc55cSDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h"
210b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
220b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
23fe6060f1SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicExtent.h"
240b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
250b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
260b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
275ffd83dbSDimitry Andric #include "llvm/ADT/StringExtras.h"
280b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
29*972a253aSDimitry Andric #include <functional>
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric using namespace clang;
320b57cec5SDimitry Andric using namespace ento;
33*972a253aSDimitry Andric using namespace std::placeholders;
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric namespace {
365ffd83dbSDimitry Andric struct AnyArgExpr {
375ffd83dbSDimitry Andric   // FIXME: Remove constructor in C++17 to turn it into an aggregate.
385ffd83dbSDimitry Andric   AnyArgExpr(const Expr *Expression, unsigned ArgumentIndex)
395ffd83dbSDimitry Andric       : Expression{Expression}, ArgumentIndex{ArgumentIndex} {}
405ffd83dbSDimitry Andric   const Expr *Expression;
415ffd83dbSDimitry Andric   unsigned ArgumentIndex;
425ffd83dbSDimitry Andric };
435ffd83dbSDimitry Andric 
445ffd83dbSDimitry Andric struct SourceArgExpr : AnyArgExpr {
455ffd83dbSDimitry Andric   using AnyArgExpr::AnyArgExpr; // FIXME: Remove using in C++17.
465ffd83dbSDimitry Andric };
475ffd83dbSDimitry Andric 
485ffd83dbSDimitry Andric struct DestinationArgExpr : AnyArgExpr {
495ffd83dbSDimitry Andric   using AnyArgExpr::AnyArgExpr; // FIXME: Same.
505ffd83dbSDimitry Andric };
515ffd83dbSDimitry Andric 
525ffd83dbSDimitry Andric struct SizeArgExpr : AnyArgExpr {
535ffd83dbSDimitry Andric   using AnyArgExpr::AnyArgExpr; // FIXME: Same.
545ffd83dbSDimitry Andric };
555ffd83dbSDimitry Andric 
565ffd83dbSDimitry Andric using ErrorMessage = SmallString<128>;
575ffd83dbSDimitry Andric enum class AccessKind { write, read };
585ffd83dbSDimitry Andric 
595ffd83dbSDimitry Andric static ErrorMessage createOutOfBoundErrorMsg(StringRef FunctionDescription,
605ffd83dbSDimitry Andric                                              AccessKind Access) {
615ffd83dbSDimitry Andric   ErrorMessage Message;
625ffd83dbSDimitry Andric   llvm::raw_svector_ostream Os(Message);
635ffd83dbSDimitry Andric 
645ffd83dbSDimitry Andric   // Function classification like: Memory copy function
655ffd83dbSDimitry Andric   Os << toUppercase(FunctionDescription.front())
665ffd83dbSDimitry Andric      << &FunctionDescription.data()[1];
675ffd83dbSDimitry Andric 
685ffd83dbSDimitry Andric   if (Access == AccessKind::write) {
695ffd83dbSDimitry Andric     Os << " overflows the destination buffer";
705ffd83dbSDimitry Andric   } else { // read access
715ffd83dbSDimitry Andric     Os << " accesses out-of-bound array element";
725ffd83dbSDimitry Andric   }
735ffd83dbSDimitry Andric 
745ffd83dbSDimitry Andric   return Message;
755ffd83dbSDimitry Andric }
765ffd83dbSDimitry Andric 
77480093f4SDimitry Andric enum class ConcatFnKind { none = 0, strcat = 1, strlcat = 2 };
780b57cec5SDimitry Andric class CStringChecker : public Checker< eval::Call,
790b57cec5SDimitry Andric                                          check::PreStmt<DeclStmt>,
800b57cec5SDimitry Andric                                          check::LiveSymbols,
810b57cec5SDimitry Andric                                          check::DeadSymbols,
820b57cec5SDimitry Andric                                          check::RegionChanges
830b57cec5SDimitry Andric                                          > {
840b57cec5SDimitry Andric   mutable std::unique_ptr<BugType> BT_Null, BT_Bounds, BT_Overlap,
8581ad6265SDimitry Andric       BT_NotCString, BT_AdditionOverflow, BT_UninitRead;
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric   mutable const char *CurrentFunctionDescription;
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric public:
900b57cec5SDimitry Andric   /// The filter is used to filter out the diagnostics which are not enabled by
910b57cec5SDimitry Andric   /// the user.
920b57cec5SDimitry Andric   struct CStringChecksFilter {
9381ad6265SDimitry Andric     bool CheckCStringNullArg = false;
9481ad6265SDimitry Andric     bool CheckCStringOutOfBounds = false;
9581ad6265SDimitry Andric     bool CheckCStringBufferOverlap = false;
9681ad6265SDimitry Andric     bool CheckCStringNotNullTerm = false;
9781ad6265SDimitry Andric     bool CheckCStringUninitializedRead = false;
980b57cec5SDimitry Andric 
99a7dea167SDimitry Andric     CheckerNameRef CheckNameCStringNullArg;
100a7dea167SDimitry Andric     CheckerNameRef CheckNameCStringOutOfBounds;
101a7dea167SDimitry Andric     CheckerNameRef CheckNameCStringBufferOverlap;
102a7dea167SDimitry Andric     CheckerNameRef CheckNameCStringNotNullTerm;
10381ad6265SDimitry Andric     CheckerNameRef CheckNameCStringUninitializedRead;
1040b57cec5SDimitry Andric   };
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric   CStringChecksFilter Filter;
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   static void *getTag() { static int tag; return &tag; }
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric   bool evalCall(const CallEvent &Call, CheckerContext &C) const;
1110b57cec5SDimitry Andric   void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const;
1120b57cec5SDimitry Andric   void checkLiveSymbols(ProgramStateRef state, SymbolReaper &SR) const;
1130b57cec5SDimitry Andric   void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric   ProgramStateRef
1160b57cec5SDimitry Andric     checkRegionChanges(ProgramStateRef state,
1170b57cec5SDimitry Andric                        const InvalidatedSymbols *,
1180b57cec5SDimitry Andric                        ArrayRef<const MemRegion *> ExplicitRegions,
1190b57cec5SDimitry Andric                        ArrayRef<const MemRegion *> Regions,
1200b57cec5SDimitry Andric                        const LocationContext *LCtx,
1210b57cec5SDimitry Andric                        const CallEvent *Call) const;
1220b57cec5SDimitry Andric 
123*972a253aSDimitry Andric   using FnCheck = std::function<void(const CStringChecker *, CheckerContext &,
124*972a253aSDimitry Andric                                      const CallExpr *)>;
125*972a253aSDimitry Andric 
1260b57cec5SDimitry Andric   CallDescriptionMap<FnCheck> Callbacks = {
127*972a253aSDimitry Andric       {{CDF_MaybeBuiltin, "memcpy", 3},
128*972a253aSDimitry Andric        std::bind(&CStringChecker::evalMemcpy, _1, _2, _3, false)},
129*972a253aSDimitry Andric       {{CDF_MaybeBuiltin, "wmemcpy", 3},
130*972a253aSDimitry Andric        std::bind(&CStringChecker::evalMemcpy, _1, _2, _3, true)},
1310b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "mempcpy", 3}, &CStringChecker::evalMempcpy},
1320b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "memcmp", 3}, &CStringChecker::evalMemcmp},
1330b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "memmove", 3}, &CStringChecker::evalMemmove},
1340b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "memset", 3}, &CStringChecker::evalMemset},
1350b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "explicit_memset", 3}, &CStringChecker::evalMemset},
1360b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strcpy", 2}, &CStringChecker::evalStrcpy},
1370b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strncpy", 3}, &CStringChecker::evalStrncpy},
1380b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "stpcpy", 2}, &CStringChecker::evalStpcpy},
1390b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strlcpy", 3}, &CStringChecker::evalStrlcpy},
1400b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strcat", 2}, &CStringChecker::evalStrcat},
1410b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strncat", 3}, &CStringChecker::evalStrncat},
1420b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strlcat", 3}, &CStringChecker::evalStrlcat},
1430b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strlen", 1}, &CStringChecker::evalstrLength},
144*972a253aSDimitry Andric       {{CDF_MaybeBuiltin, "wcslen", 1}, &CStringChecker::evalstrLength},
1450b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strnlen", 2}, &CStringChecker::evalstrnLength},
146*972a253aSDimitry Andric       {{CDF_MaybeBuiltin, "wcsnlen", 2}, &CStringChecker::evalstrnLength},
1470b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strcmp", 2}, &CStringChecker::evalStrcmp},
1480b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strncmp", 3}, &CStringChecker::evalStrncmp},
1490b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strcasecmp", 2}, &CStringChecker::evalStrcasecmp},
1500b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strncasecmp", 3}, &CStringChecker::evalStrncasecmp},
1510b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strsep", 2}, &CStringChecker::evalStrsep},
1520b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "bcopy", 3}, &CStringChecker::evalBcopy},
1530b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "bcmp", 3}, &CStringChecker::evalMemcmp},
1540b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "bzero", 2}, &CStringChecker::evalBzero},
1550b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "explicit_bzero", 2}, &CStringChecker::evalBzero},
1560b57cec5SDimitry Andric   };
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric   // These require a bit of special handling.
1590b57cec5SDimitry Andric   CallDescription StdCopy{{"std", "copy"}, 3},
1600b57cec5SDimitry Andric       StdCopyBackward{{"std", "copy_backward"}, 3};
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric   FnCheck identifyCall(const CallEvent &Call, CheckerContext &C) const;
163*972a253aSDimitry Andric   void evalMemcpy(CheckerContext &C, const CallExpr *CE, bool IsWide) const;
1640b57cec5SDimitry Andric   void evalMempcpy(CheckerContext &C, const CallExpr *CE) const;
1650b57cec5SDimitry Andric   void evalMemmove(CheckerContext &C, const CallExpr *CE) const;
1660b57cec5SDimitry Andric   void evalBcopy(CheckerContext &C, const CallExpr *CE) const;
1670b57cec5SDimitry Andric   void evalCopyCommon(CheckerContext &C, const CallExpr *CE,
1685ffd83dbSDimitry Andric                       ProgramStateRef state, SizeArgExpr Size,
1695ffd83dbSDimitry Andric                       DestinationArgExpr Dest, SourceArgExpr Source,
170*972a253aSDimitry Andric                       bool Restricted, bool IsMempcpy, bool IsWide) const;
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric   void evalMemcmp(CheckerContext &C, const CallExpr *CE) const;
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric   void evalstrLength(CheckerContext &C, const CallExpr *CE) const;
1750b57cec5SDimitry Andric   void evalstrnLength(CheckerContext &C, const CallExpr *CE) const;
1760b57cec5SDimitry Andric   void evalstrLengthCommon(CheckerContext &C,
1770b57cec5SDimitry Andric                            const CallExpr *CE,
1780b57cec5SDimitry Andric                            bool IsStrnlen = false) const;
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric   void evalStrcpy(CheckerContext &C, const CallExpr *CE) const;
1810b57cec5SDimitry Andric   void evalStrncpy(CheckerContext &C, const CallExpr *CE) const;
1820b57cec5SDimitry Andric   void evalStpcpy(CheckerContext &C, const CallExpr *CE) const;
1830b57cec5SDimitry Andric   void evalStrlcpy(CheckerContext &C, const CallExpr *CE) const;
184480093f4SDimitry Andric   void evalStrcpyCommon(CheckerContext &C, const CallExpr *CE, bool ReturnEnd,
185480093f4SDimitry Andric                         bool IsBounded, ConcatFnKind appendK,
1860b57cec5SDimitry Andric                         bool returnPtr = true) const;
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric   void evalStrcat(CheckerContext &C, const CallExpr *CE) const;
1890b57cec5SDimitry Andric   void evalStrncat(CheckerContext &C, const CallExpr *CE) const;
1900b57cec5SDimitry Andric   void evalStrlcat(CheckerContext &C, const CallExpr *CE) const;
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric   void evalStrcmp(CheckerContext &C, const CallExpr *CE) const;
1930b57cec5SDimitry Andric   void evalStrncmp(CheckerContext &C, const CallExpr *CE) const;
1940b57cec5SDimitry Andric   void evalStrcasecmp(CheckerContext &C, const CallExpr *CE) const;
1950b57cec5SDimitry Andric   void evalStrncasecmp(CheckerContext &C, const CallExpr *CE) const;
1960b57cec5SDimitry Andric   void evalStrcmpCommon(CheckerContext &C,
1970b57cec5SDimitry Andric                         const CallExpr *CE,
198480093f4SDimitry Andric                         bool IsBounded = false,
199480093f4SDimitry Andric                         bool IgnoreCase = false) const;
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric   void evalStrsep(CheckerContext &C, const CallExpr *CE) const;
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric   void evalStdCopy(CheckerContext &C, const CallExpr *CE) const;
2040b57cec5SDimitry Andric   void evalStdCopyBackward(CheckerContext &C, const CallExpr *CE) const;
2050b57cec5SDimitry Andric   void evalStdCopyCommon(CheckerContext &C, const CallExpr *CE) const;
2060b57cec5SDimitry Andric   void evalMemset(CheckerContext &C, const CallExpr *CE) const;
2070b57cec5SDimitry Andric   void evalBzero(CheckerContext &C, const CallExpr *CE) const;
2080b57cec5SDimitry Andric 
2090b57cec5SDimitry Andric   // Utility methods
2100b57cec5SDimitry Andric   std::pair<ProgramStateRef , ProgramStateRef >
2110b57cec5SDimitry Andric   static assumeZero(CheckerContext &C,
2120b57cec5SDimitry Andric                     ProgramStateRef state, SVal V, QualType Ty);
2130b57cec5SDimitry Andric 
2140b57cec5SDimitry Andric   static ProgramStateRef setCStringLength(ProgramStateRef state,
2150b57cec5SDimitry Andric                                               const MemRegion *MR,
2160b57cec5SDimitry Andric                                               SVal strLength);
2170b57cec5SDimitry Andric   static SVal getCStringLengthForRegion(CheckerContext &C,
2180b57cec5SDimitry Andric                                         ProgramStateRef &state,
2190b57cec5SDimitry Andric                                         const Expr *Ex,
2200b57cec5SDimitry Andric                                         const MemRegion *MR,
2210b57cec5SDimitry Andric                                         bool hypothetical);
2220b57cec5SDimitry Andric   SVal getCStringLength(CheckerContext &C,
2230b57cec5SDimitry Andric                         ProgramStateRef &state,
2240b57cec5SDimitry Andric                         const Expr *Ex,
2250b57cec5SDimitry Andric                         SVal Buf,
2260b57cec5SDimitry Andric                         bool hypothetical = false) const;
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric   const StringLiteral *getCStringLiteral(CheckerContext &C,
2290b57cec5SDimitry Andric                                          ProgramStateRef &state,
2300b57cec5SDimitry Andric                                          const Expr *expr,
2310b57cec5SDimitry Andric                                          SVal val) const;
2320b57cec5SDimitry Andric 
2330b57cec5SDimitry Andric   static ProgramStateRef InvalidateBuffer(CheckerContext &C,
2340b57cec5SDimitry Andric                                           ProgramStateRef state,
2350b57cec5SDimitry Andric                                           const Expr *Ex, SVal V,
2360b57cec5SDimitry Andric                                           bool IsSourceBuffer,
2370b57cec5SDimitry Andric                                           const Expr *Size);
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric   static bool SummarizeRegion(raw_ostream &os, ASTContext &Ctx,
2400b57cec5SDimitry Andric                               const MemRegion *MR);
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric   static bool memsetAux(const Expr *DstBuffer, SVal CharE,
2430b57cec5SDimitry Andric                         const Expr *Size, CheckerContext &C,
2440b57cec5SDimitry Andric                         ProgramStateRef &State);
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric   // Re-usable checks
2475ffd83dbSDimitry Andric   ProgramStateRef checkNonNull(CheckerContext &C, ProgramStateRef State,
2485ffd83dbSDimitry Andric                                AnyArgExpr Arg, SVal l) const;
2495ffd83dbSDimitry Andric   ProgramStateRef CheckLocation(CheckerContext &C, ProgramStateRef state,
2505ffd83dbSDimitry Andric                                 AnyArgExpr Buffer, SVal Element,
251*972a253aSDimitry Andric                                 AccessKind Access, bool IsWide = false) const;
2525ffd83dbSDimitry Andric   ProgramStateRef CheckBufferAccess(CheckerContext &C, ProgramStateRef State,
2535ffd83dbSDimitry Andric                                     AnyArgExpr Buffer, SizeArgExpr Size,
254*972a253aSDimitry Andric                                     AccessKind Access,
255*972a253aSDimitry Andric                                     bool IsWide = false) const;
2565ffd83dbSDimitry Andric   ProgramStateRef CheckOverlap(CheckerContext &C, ProgramStateRef state,
2575ffd83dbSDimitry Andric                                SizeArgExpr Size, AnyArgExpr First,
258*972a253aSDimitry Andric                                AnyArgExpr Second, bool IsWide = false) const;
2590b57cec5SDimitry Andric   void emitOverlapBug(CheckerContext &C,
2600b57cec5SDimitry Andric                       ProgramStateRef state,
2610b57cec5SDimitry Andric                       const Stmt *First,
2620b57cec5SDimitry Andric                       const Stmt *Second) const;
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric   void emitNullArgBug(CheckerContext &C, ProgramStateRef State, const Stmt *S,
2650b57cec5SDimitry Andric                       StringRef WarningMsg) const;
2660b57cec5SDimitry Andric   void emitOutOfBoundsBug(CheckerContext &C, ProgramStateRef State,
2670b57cec5SDimitry Andric                           const Stmt *S, StringRef WarningMsg) const;
2680b57cec5SDimitry Andric   void emitNotCStringBug(CheckerContext &C, ProgramStateRef State,
2690b57cec5SDimitry Andric                          const Stmt *S, StringRef WarningMsg) const;
2700b57cec5SDimitry Andric   void emitAdditionOverflowBug(CheckerContext &C, ProgramStateRef State) const;
27181ad6265SDimitry Andric   void emitUninitializedReadBug(CheckerContext &C, ProgramStateRef State,
27281ad6265SDimitry Andric                              const Expr *E) const;
2730b57cec5SDimitry Andric   ProgramStateRef checkAdditionOverflow(CheckerContext &C,
2740b57cec5SDimitry Andric                                             ProgramStateRef state,
2750b57cec5SDimitry Andric                                             NonLoc left,
2760b57cec5SDimitry Andric                                             NonLoc right) const;
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric   // Return true if the destination buffer of the copy function may be in bound.
2790b57cec5SDimitry Andric   // Expects SVal of Size to be positive and unsigned.
2800b57cec5SDimitry Andric   // Expects SVal of FirstBuf to be a FieldRegion.
2810b57cec5SDimitry Andric   static bool IsFirstBufInBound(CheckerContext &C,
2820b57cec5SDimitry Andric                                 ProgramStateRef state,
2830b57cec5SDimitry Andric                                 const Expr *FirstBuf,
2840b57cec5SDimitry Andric                                 const Expr *Size);
2850b57cec5SDimitry Andric };
2860b57cec5SDimitry Andric 
2870b57cec5SDimitry Andric } //end anonymous namespace
2880b57cec5SDimitry Andric 
2890b57cec5SDimitry Andric REGISTER_MAP_WITH_PROGRAMSTATE(CStringLength, const MemRegion *, SVal)
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2920b57cec5SDimitry Andric // Individual checks and utility methods.
2930b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2940b57cec5SDimitry Andric 
2950b57cec5SDimitry Andric std::pair<ProgramStateRef , ProgramStateRef >
2960b57cec5SDimitry Andric CStringChecker::assumeZero(CheckerContext &C, ProgramStateRef state, SVal V,
2970b57cec5SDimitry Andric                            QualType Ty) {
2980b57cec5SDimitry Andric   Optional<DefinedSVal> val = V.getAs<DefinedSVal>();
2990b57cec5SDimitry Andric   if (!val)
3000b57cec5SDimitry Andric     return std::pair<ProgramStateRef , ProgramStateRef >(state, state);
3010b57cec5SDimitry Andric 
3020b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
3030b57cec5SDimitry Andric   DefinedOrUnknownSVal zero = svalBuilder.makeZeroVal(Ty);
3040b57cec5SDimitry Andric   return state->assume(svalBuilder.evalEQ(state, *val, zero));
3050b57cec5SDimitry Andric }
3060b57cec5SDimitry Andric 
3070b57cec5SDimitry Andric ProgramStateRef CStringChecker::checkNonNull(CheckerContext &C,
3085ffd83dbSDimitry Andric                                              ProgramStateRef State,
3095ffd83dbSDimitry Andric                                              AnyArgExpr Arg, SVal l) const {
3100b57cec5SDimitry Andric   // If a previous check has failed, propagate the failure.
3115ffd83dbSDimitry Andric   if (!State)
3120b57cec5SDimitry Andric     return nullptr;
3130b57cec5SDimitry Andric 
3140b57cec5SDimitry Andric   ProgramStateRef stateNull, stateNonNull;
3155ffd83dbSDimitry Andric   std::tie(stateNull, stateNonNull) =
3165ffd83dbSDimitry Andric       assumeZero(C, State, l, Arg.Expression->getType());
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric   if (stateNull && !stateNonNull) {
3190b57cec5SDimitry Andric     if (Filter.CheckCStringNullArg) {
3200b57cec5SDimitry Andric       SmallString<80> buf;
321a7dea167SDimitry Andric       llvm::raw_svector_ostream OS(buf);
3220b57cec5SDimitry Andric       assert(CurrentFunctionDescription);
3235ffd83dbSDimitry Andric       OS << "Null pointer passed as " << (Arg.ArgumentIndex + 1)
3245ffd83dbSDimitry Andric          << llvm::getOrdinalSuffix(Arg.ArgumentIndex + 1) << " argument to "
325480093f4SDimitry Andric          << CurrentFunctionDescription;
3260b57cec5SDimitry Andric 
3275ffd83dbSDimitry Andric       emitNullArgBug(C, stateNull, Arg.Expression, OS.str());
3280b57cec5SDimitry Andric     }
3290b57cec5SDimitry Andric     return nullptr;
3300b57cec5SDimitry Andric   }
3310b57cec5SDimitry Andric 
3320b57cec5SDimitry Andric   // From here on, assume that the value is non-null.
3330b57cec5SDimitry Andric   assert(stateNonNull);
3340b57cec5SDimitry Andric   return stateNonNull;
3350b57cec5SDimitry Andric }
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric // FIXME: This was originally copied from ArrayBoundChecker.cpp. Refactor?
3380b57cec5SDimitry Andric ProgramStateRef CStringChecker::CheckLocation(CheckerContext &C,
3390b57cec5SDimitry Andric                                               ProgramStateRef state,
3405ffd83dbSDimitry Andric                                               AnyArgExpr Buffer, SVal Element,
341*972a253aSDimitry Andric                                               AccessKind Access,
342*972a253aSDimitry Andric                                               bool IsWide) const {
3435ffd83dbSDimitry Andric 
3440b57cec5SDimitry Andric   // If a previous check has failed, propagate the failure.
3450b57cec5SDimitry Andric   if (!state)
3460b57cec5SDimitry Andric     return nullptr;
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric   // Check for out of bound array element access.
3495ffd83dbSDimitry Andric   const MemRegion *R = Element.getAsRegion();
3500b57cec5SDimitry Andric   if (!R)
3510b57cec5SDimitry Andric     return state;
3520b57cec5SDimitry Andric 
3535ffd83dbSDimitry Andric   const auto *ER = dyn_cast<ElementRegion>(R);
3540b57cec5SDimitry Andric   if (!ER)
3550b57cec5SDimitry Andric     return state;
3560b57cec5SDimitry Andric 
357*972a253aSDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
358*972a253aSDimitry Andric   ASTContext &Ctx = svalBuilder.getContext();
359*972a253aSDimitry Andric 
360*972a253aSDimitry Andric   // Get the index of the accessed element.
361*972a253aSDimitry Andric   NonLoc Idx = ER->getIndex();
362*972a253aSDimitry Andric 
363*972a253aSDimitry Andric   if (!IsWide) {
364*972a253aSDimitry Andric     if (ER->getValueType() != Ctx.CharTy)
3650b57cec5SDimitry Andric       return state;
366*972a253aSDimitry Andric   } else {
367*972a253aSDimitry Andric     if (ER->getValueType() != Ctx.WideCharTy)
368*972a253aSDimitry Andric       return state;
369*972a253aSDimitry Andric 
370*972a253aSDimitry Andric     QualType SizeTy = Ctx.getSizeType();
371*972a253aSDimitry Andric     NonLoc WideSize =
372*972a253aSDimitry Andric         svalBuilder
373*972a253aSDimitry Andric             .makeIntVal(Ctx.getTypeSizeInChars(Ctx.WideCharTy).getQuantity(),
374*972a253aSDimitry Andric                         SizeTy)
375*972a253aSDimitry Andric             .castAs<NonLoc>();
376*972a253aSDimitry Andric     SVal Offset = svalBuilder.evalBinOpNN(state, BO_Mul, Idx, WideSize, SizeTy);
377*972a253aSDimitry Andric     if (Offset.isUnknown())
378*972a253aSDimitry Andric       return state;
379*972a253aSDimitry Andric     Idx = Offset.castAs<NonLoc>();
380*972a253aSDimitry Andric   }
3810b57cec5SDimitry Andric 
3820b57cec5SDimitry Andric   // Get the size of the array.
3835ffd83dbSDimitry Andric   const auto *superReg = cast<SubRegion>(ER->getSuperRegion());
3845ffd83dbSDimitry Andric   DefinedOrUnknownSVal Size =
385fe6060f1SDimitry Andric       getDynamicExtent(state, superReg, C.getSValBuilder());
3860b57cec5SDimitry Andric 
38781ad6265SDimitry Andric   ProgramStateRef StInBound, StOutBound;
38881ad6265SDimitry Andric   std::tie(StInBound, StOutBound) = state->assumeInBoundDual(Idx, Size);
3890b57cec5SDimitry Andric   if (StOutBound && !StInBound) {
3900b57cec5SDimitry Andric     // These checks are either enabled by the CString out-of-bounds checker
3910b57cec5SDimitry Andric     // explicitly or implicitly by the Malloc checker.
3920b57cec5SDimitry Andric     // In the latter case we only do modeling but do not emit warning.
3930b57cec5SDimitry Andric     if (!Filter.CheckCStringOutOfBounds)
3940b57cec5SDimitry Andric       return nullptr;
3950b57cec5SDimitry Andric 
3965ffd83dbSDimitry Andric     // Emit a bug report.
3975ffd83dbSDimitry Andric     ErrorMessage Message =
3985ffd83dbSDimitry Andric         createOutOfBoundErrorMsg(CurrentFunctionDescription, Access);
3995ffd83dbSDimitry Andric     emitOutOfBoundsBug(C, StOutBound, Buffer.Expression, Message);
4000b57cec5SDimitry Andric     return nullptr;
4010b57cec5SDimitry Andric   }
4020b57cec5SDimitry Andric 
40381ad6265SDimitry Andric   // Ensure that we wouldn't read uninitialized value.
40481ad6265SDimitry Andric   if (Access == AccessKind::read) {
40581ad6265SDimitry Andric     if (Filter.CheckCStringUninitializedRead &&
40681ad6265SDimitry Andric         StInBound->getSVal(ER).isUndef()) {
40781ad6265SDimitry Andric       emitUninitializedReadBug(C, StInBound, Buffer.Expression);
40881ad6265SDimitry Andric       return nullptr;
40981ad6265SDimitry Andric     }
41081ad6265SDimitry Andric   }
41181ad6265SDimitry Andric 
4120b57cec5SDimitry Andric   // Array bound check succeeded.  From this point forward the array bound
4130b57cec5SDimitry Andric   // should always succeed.
4140b57cec5SDimitry Andric   return StInBound;
4150b57cec5SDimitry Andric }
4160b57cec5SDimitry Andric 
417*972a253aSDimitry Andric ProgramStateRef
418*972a253aSDimitry Andric CStringChecker::CheckBufferAccess(CheckerContext &C, ProgramStateRef State,
419*972a253aSDimitry Andric                                   AnyArgExpr Buffer, SizeArgExpr Size,
420*972a253aSDimitry Andric                                   AccessKind Access, bool IsWide) const {
4210b57cec5SDimitry Andric   // If a previous check has failed, propagate the failure.
4225ffd83dbSDimitry Andric   if (!State)
4230b57cec5SDimitry Andric     return nullptr;
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
4260b57cec5SDimitry Andric   ASTContext &Ctx = svalBuilder.getContext();
4270b57cec5SDimitry Andric 
4285ffd83dbSDimitry Andric   QualType SizeTy = Size.Expression->getType();
429*972a253aSDimitry Andric   QualType PtrTy = Ctx.getPointerType(IsWide ? Ctx.WideCharTy : Ctx.CharTy);
4300b57cec5SDimitry Andric 
4310b57cec5SDimitry Andric   // Check that the first buffer is non-null.
4325ffd83dbSDimitry Andric   SVal BufVal = C.getSVal(Buffer.Expression);
4335ffd83dbSDimitry Andric   State = checkNonNull(C, State, Buffer, BufVal);
4345ffd83dbSDimitry Andric   if (!State)
4350b57cec5SDimitry Andric     return nullptr;
4360b57cec5SDimitry Andric 
4370b57cec5SDimitry Andric   // If out-of-bounds checking is turned off, skip the rest.
4380b57cec5SDimitry Andric   if (!Filter.CheckCStringOutOfBounds)
4395ffd83dbSDimitry Andric     return State;
4400b57cec5SDimitry Andric 
4410b57cec5SDimitry Andric   // Get the access length and make sure it is known.
4420b57cec5SDimitry Andric   // FIXME: This assumes the caller has already checked that the access length
4430b57cec5SDimitry Andric   // is positive. And that it's unsigned.
4445ffd83dbSDimitry Andric   SVal LengthVal = C.getSVal(Size.Expression);
4450b57cec5SDimitry Andric   Optional<NonLoc> Length = LengthVal.getAs<NonLoc>();
4460b57cec5SDimitry Andric   if (!Length)
4475ffd83dbSDimitry Andric     return State;
4480b57cec5SDimitry Andric 
4490b57cec5SDimitry Andric   // Compute the offset of the last element to be accessed: size-1.
4505ffd83dbSDimitry Andric   NonLoc One = svalBuilder.makeIntVal(1, SizeTy).castAs<NonLoc>();
4515ffd83dbSDimitry Andric   SVal Offset = svalBuilder.evalBinOpNN(State, BO_Sub, *Length, One, SizeTy);
4520b57cec5SDimitry Andric   if (Offset.isUnknown())
4530b57cec5SDimitry Andric     return nullptr;
4540b57cec5SDimitry Andric   NonLoc LastOffset = Offset.castAs<NonLoc>();
4550b57cec5SDimitry Andric 
4560b57cec5SDimitry Andric   // Check that the first buffer is sufficiently long.
4575ffd83dbSDimitry Andric   SVal BufStart =
4585ffd83dbSDimitry Andric       svalBuilder.evalCast(BufVal, PtrTy, Buffer.Expression->getType());
4590b57cec5SDimitry Andric   if (Optional<Loc> BufLoc = BufStart.getAs<Loc>()) {
4600b57cec5SDimitry Andric 
4615ffd83dbSDimitry Andric     SVal BufEnd =
4625ffd83dbSDimitry Andric         svalBuilder.evalBinOpLN(State, BO_Add, *BufLoc, LastOffset, PtrTy);
463*972a253aSDimitry Andric     State = CheckLocation(C, State, Buffer, BufEnd, Access, IsWide);
4640b57cec5SDimitry Andric 
4650b57cec5SDimitry Andric     // If the buffer isn't large enough, abort.
4665ffd83dbSDimitry Andric     if (!State)
4670b57cec5SDimitry Andric       return nullptr;
4680b57cec5SDimitry Andric   }
4690b57cec5SDimitry Andric 
4700b57cec5SDimitry Andric   // Large enough or not, return this state!
4715ffd83dbSDimitry Andric   return State;
4720b57cec5SDimitry Andric }
4730b57cec5SDimitry Andric 
4740b57cec5SDimitry Andric ProgramStateRef CStringChecker::CheckOverlap(CheckerContext &C,
4750b57cec5SDimitry Andric                                              ProgramStateRef state,
4765ffd83dbSDimitry Andric                                              SizeArgExpr Size, AnyArgExpr First,
477*972a253aSDimitry Andric                                              AnyArgExpr Second,
478*972a253aSDimitry Andric                                              bool IsWide) const {
4790b57cec5SDimitry Andric   if (!Filter.CheckCStringBufferOverlap)
4800b57cec5SDimitry Andric     return state;
4810b57cec5SDimitry Andric 
4820b57cec5SDimitry Andric   // Do a simple check for overlap: if the two arguments are from the same
4830b57cec5SDimitry Andric   // buffer, see if the end of the first is greater than the start of the second
4840b57cec5SDimitry Andric   // or vice versa.
4850b57cec5SDimitry Andric 
4860b57cec5SDimitry Andric   // If a previous check has failed, propagate the failure.
4870b57cec5SDimitry Andric   if (!state)
4880b57cec5SDimitry Andric     return nullptr;
4890b57cec5SDimitry Andric 
4900b57cec5SDimitry Andric   ProgramStateRef stateTrue, stateFalse;
4910b57cec5SDimitry Andric 
49281ad6265SDimitry Andric   // Assume different address spaces cannot overlap.
49381ad6265SDimitry Andric   if (First.Expression->getType()->getPointeeType().getAddressSpace() !=
49481ad6265SDimitry Andric       Second.Expression->getType()->getPointeeType().getAddressSpace())
49581ad6265SDimitry Andric     return state;
49681ad6265SDimitry Andric 
4970b57cec5SDimitry Andric   // Get the buffer values and make sure they're known locations.
4980b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
4995ffd83dbSDimitry Andric   SVal firstVal = state->getSVal(First.Expression, LCtx);
5005ffd83dbSDimitry Andric   SVal secondVal = state->getSVal(Second.Expression, LCtx);
5010b57cec5SDimitry Andric 
5020b57cec5SDimitry Andric   Optional<Loc> firstLoc = firstVal.getAs<Loc>();
5030b57cec5SDimitry Andric   if (!firstLoc)
5040b57cec5SDimitry Andric     return state;
5050b57cec5SDimitry Andric 
5060b57cec5SDimitry Andric   Optional<Loc> secondLoc = secondVal.getAs<Loc>();
5070b57cec5SDimitry Andric   if (!secondLoc)
5080b57cec5SDimitry Andric     return state;
5090b57cec5SDimitry Andric 
5100b57cec5SDimitry Andric   // Are the two values the same?
5110b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
5120b57cec5SDimitry Andric   std::tie(stateTrue, stateFalse) =
5130b57cec5SDimitry Andric       state->assume(svalBuilder.evalEQ(state, *firstLoc, *secondLoc));
5140b57cec5SDimitry Andric 
5150b57cec5SDimitry Andric   if (stateTrue && !stateFalse) {
5160b57cec5SDimitry Andric     // If the values are known to be equal, that's automatically an overlap.
5175ffd83dbSDimitry Andric     emitOverlapBug(C, stateTrue, First.Expression, Second.Expression);
5180b57cec5SDimitry Andric     return nullptr;
5190b57cec5SDimitry Andric   }
5200b57cec5SDimitry Andric 
5210b57cec5SDimitry Andric   // assume the two expressions are not equal.
5220b57cec5SDimitry Andric   assert(stateFalse);
5230b57cec5SDimitry Andric   state = stateFalse;
5240b57cec5SDimitry Andric 
5250b57cec5SDimitry Andric   // Which value comes first?
5260b57cec5SDimitry Andric   QualType cmpTy = svalBuilder.getConditionType();
5275ffd83dbSDimitry Andric   SVal reverse =
5285ffd83dbSDimitry Andric       svalBuilder.evalBinOpLL(state, BO_GT, *firstLoc, *secondLoc, cmpTy);
5290b57cec5SDimitry Andric   Optional<DefinedOrUnknownSVal> reverseTest =
5300b57cec5SDimitry Andric       reverse.getAs<DefinedOrUnknownSVal>();
5310b57cec5SDimitry Andric   if (!reverseTest)
5320b57cec5SDimitry Andric     return state;
5330b57cec5SDimitry Andric 
5340b57cec5SDimitry Andric   std::tie(stateTrue, stateFalse) = state->assume(*reverseTest);
5350b57cec5SDimitry Andric   if (stateTrue) {
5360b57cec5SDimitry Andric     if (stateFalse) {
5370b57cec5SDimitry Andric       // If we don't know which one comes first, we can't perform this test.
5380b57cec5SDimitry Andric       return state;
5390b57cec5SDimitry Andric     } else {
5400b57cec5SDimitry Andric       // Switch the values so that firstVal is before secondVal.
5410b57cec5SDimitry Andric       std::swap(firstLoc, secondLoc);
5420b57cec5SDimitry Andric 
5430b57cec5SDimitry Andric       // Switch the Exprs as well, so that they still correspond.
5440b57cec5SDimitry Andric       std::swap(First, Second);
5450b57cec5SDimitry Andric     }
5460b57cec5SDimitry Andric   }
5470b57cec5SDimitry Andric 
5480b57cec5SDimitry Andric   // Get the length, and make sure it too is known.
5495ffd83dbSDimitry Andric   SVal LengthVal = state->getSVal(Size.Expression, LCtx);
5500b57cec5SDimitry Andric   Optional<NonLoc> Length = LengthVal.getAs<NonLoc>();
5510b57cec5SDimitry Andric   if (!Length)
5520b57cec5SDimitry Andric     return state;
5530b57cec5SDimitry Andric 
5540b57cec5SDimitry Andric   // Convert the first buffer's start address to char*.
5550b57cec5SDimitry Andric   // Bail out if the cast fails.
5560b57cec5SDimitry Andric   ASTContext &Ctx = svalBuilder.getContext();
557*972a253aSDimitry Andric   QualType CharPtrTy = Ctx.getPointerType(IsWide ? Ctx.WideCharTy : Ctx.CharTy);
5585ffd83dbSDimitry Andric   SVal FirstStart =
5595ffd83dbSDimitry Andric       svalBuilder.evalCast(*firstLoc, CharPtrTy, First.Expression->getType());
5600b57cec5SDimitry Andric   Optional<Loc> FirstStartLoc = FirstStart.getAs<Loc>();
5610b57cec5SDimitry Andric   if (!FirstStartLoc)
5620b57cec5SDimitry Andric     return state;
5630b57cec5SDimitry Andric 
5640b57cec5SDimitry Andric   // Compute the end of the first buffer. Bail out if THAT fails.
5655ffd83dbSDimitry Andric   SVal FirstEnd = svalBuilder.evalBinOpLN(state, BO_Add, *FirstStartLoc,
5665ffd83dbSDimitry Andric                                           *Length, CharPtrTy);
5670b57cec5SDimitry Andric   Optional<Loc> FirstEndLoc = FirstEnd.getAs<Loc>();
5680b57cec5SDimitry Andric   if (!FirstEndLoc)
5690b57cec5SDimitry Andric     return state;
5700b57cec5SDimitry Andric 
5710b57cec5SDimitry Andric   // Is the end of the first buffer past the start of the second buffer?
5725ffd83dbSDimitry Andric   SVal Overlap =
5735ffd83dbSDimitry Andric       svalBuilder.evalBinOpLL(state, BO_GT, *FirstEndLoc, *secondLoc, cmpTy);
5740b57cec5SDimitry Andric   Optional<DefinedOrUnknownSVal> OverlapTest =
5750b57cec5SDimitry Andric       Overlap.getAs<DefinedOrUnknownSVal>();
5760b57cec5SDimitry Andric   if (!OverlapTest)
5770b57cec5SDimitry Andric     return state;
5780b57cec5SDimitry Andric 
5790b57cec5SDimitry Andric   std::tie(stateTrue, stateFalse) = state->assume(*OverlapTest);
5800b57cec5SDimitry Andric 
5810b57cec5SDimitry Andric   if (stateTrue && !stateFalse) {
5820b57cec5SDimitry Andric     // Overlap!
5835ffd83dbSDimitry Andric     emitOverlapBug(C, stateTrue, First.Expression, Second.Expression);
5840b57cec5SDimitry Andric     return nullptr;
5850b57cec5SDimitry Andric   }
5860b57cec5SDimitry Andric 
5870b57cec5SDimitry Andric   // assume the two expressions don't overlap.
5880b57cec5SDimitry Andric   assert(stateFalse);
5890b57cec5SDimitry Andric   return stateFalse;
5900b57cec5SDimitry Andric }
5910b57cec5SDimitry Andric 
5920b57cec5SDimitry Andric void CStringChecker::emitOverlapBug(CheckerContext &C, ProgramStateRef state,
5930b57cec5SDimitry Andric                                   const Stmt *First, const Stmt *Second) const {
5940b57cec5SDimitry Andric   ExplodedNode *N = C.generateErrorNode(state);
5950b57cec5SDimitry Andric   if (!N)
5960b57cec5SDimitry Andric     return;
5970b57cec5SDimitry Andric 
5980b57cec5SDimitry Andric   if (!BT_Overlap)
5990b57cec5SDimitry Andric     BT_Overlap.reset(new BugType(Filter.CheckNameCStringBufferOverlap,
6000b57cec5SDimitry Andric                                  categories::UnixAPI, "Improper arguments"));
6010b57cec5SDimitry Andric 
6020b57cec5SDimitry Andric   // Generate a report for this bug.
603a7dea167SDimitry Andric   auto report = std::make_unique<PathSensitiveBugReport>(
6040b57cec5SDimitry Andric       *BT_Overlap, "Arguments must not be overlapping buffers", N);
6050b57cec5SDimitry Andric   report->addRange(First->getSourceRange());
6060b57cec5SDimitry Andric   report->addRange(Second->getSourceRange());
6070b57cec5SDimitry Andric 
6080b57cec5SDimitry Andric   C.emitReport(std::move(report));
6090b57cec5SDimitry Andric }
6100b57cec5SDimitry Andric 
6110b57cec5SDimitry Andric void CStringChecker::emitNullArgBug(CheckerContext &C, ProgramStateRef State,
6120b57cec5SDimitry Andric                                     const Stmt *S, StringRef WarningMsg) const {
6130b57cec5SDimitry Andric   if (ExplodedNode *N = C.generateErrorNode(State)) {
6140b57cec5SDimitry Andric     if (!BT_Null)
6150b57cec5SDimitry Andric       BT_Null.reset(new BuiltinBug(
6160b57cec5SDimitry Andric           Filter.CheckNameCStringNullArg, categories::UnixAPI,
6170b57cec5SDimitry Andric           "Null pointer argument in call to byte string function"));
6180b57cec5SDimitry Andric 
6190b57cec5SDimitry Andric     BuiltinBug *BT = static_cast<BuiltinBug *>(BT_Null.get());
620a7dea167SDimitry Andric     auto Report = std::make_unique<PathSensitiveBugReport>(*BT, WarningMsg, N);
6210b57cec5SDimitry Andric     Report->addRange(S->getSourceRange());
6220b57cec5SDimitry Andric     if (const auto *Ex = dyn_cast<Expr>(S))
6230b57cec5SDimitry Andric       bugreporter::trackExpressionValue(N, Ex, *Report);
6240b57cec5SDimitry Andric     C.emitReport(std::move(Report));
6250b57cec5SDimitry Andric   }
6260b57cec5SDimitry Andric }
6270b57cec5SDimitry Andric 
62881ad6265SDimitry Andric void CStringChecker::emitUninitializedReadBug(CheckerContext &C,
62981ad6265SDimitry Andric                                               ProgramStateRef State,
63081ad6265SDimitry Andric                                               const Expr *E) const {
63181ad6265SDimitry Andric   if (ExplodedNode *N = C.generateErrorNode(State)) {
63281ad6265SDimitry Andric     const char *Msg =
63381ad6265SDimitry Andric         "Bytes string function accesses uninitialized/garbage values";
63481ad6265SDimitry Andric     if (!BT_UninitRead)
63581ad6265SDimitry Andric       BT_UninitRead.reset(
63681ad6265SDimitry Andric           new BuiltinBug(Filter.CheckNameCStringUninitializedRead,
63781ad6265SDimitry Andric                          "Accessing unitialized/garbage values", Msg));
63881ad6265SDimitry Andric 
63981ad6265SDimitry Andric     BuiltinBug *BT = static_cast<BuiltinBug *>(BT_UninitRead.get());
64081ad6265SDimitry Andric 
64181ad6265SDimitry Andric     auto Report = std::make_unique<PathSensitiveBugReport>(*BT, Msg, N);
64281ad6265SDimitry Andric     Report->addRange(E->getSourceRange());
64381ad6265SDimitry Andric     bugreporter::trackExpressionValue(N, E, *Report);
64481ad6265SDimitry Andric     C.emitReport(std::move(Report));
64581ad6265SDimitry Andric   }
64681ad6265SDimitry Andric }
64781ad6265SDimitry Andric 
6480b57cec5SDimitry Andric void CStringChecker::emitOutOfBoundsBug(CheckerContext &C,
6490b57cec5SDimitry Andric                                         ProgramStateRef State, const Stmt *S,
6500b57cec5SDimitry Andric                                         StringRef WarningMsg) const {
6510b57cec5SDimitry Andric   if (ExplodedNode *N = C.generateErrorNode(State)) {
6520b57cec5SDimitry Andric     if (!BT_Bounds)
6530b57cec5SDimitry Andric       BT_Bounds.reset(new BuiltinBug(
6540b57cec5SDimitry Andric           Filter.CheckCStringOutOfBounds ? Filter.CheckNameCStringOutOfBounds
6550b57cec5SDimitry Andric                                          : Filter.CheckNameCStringNullArg,
6560b57cec5SDimitry Andric           "Out-of-bound array access",
6570b57cec5SDimitry Andric           "Byte string function accesses out-of-bound array element"));
6580b57cec5SDimitry Andric 
6590b57cec5SDimitry Andric     BuiltinBug *BT = static_cast<BuiltinBug *>(BT_Bounds.get());
6600b57cec5SDimitry Andric 
6610b57cec5SDimitry Andric     // FIXME: It would be nice to eventually make this diagnostic more clear,
6620b57cec5SDimitry Andric     // e.g., by referencing the original declaration or by saying *why* this
6630b57cec5SDimitry Andric     // reference is outside the range.
664a7dea167SDimitry Andric     auto Report = std::make_unique<PathSensitiveBugReport>(*BT, WarningMsg, N);
6650b57cec5SDimitry Andric     Report->addRange(S->getSourceRange());
6660b57cec5SDimitry Andric     C.emitReport(std::move(Report));
6670b57cec5SDimitry Andric   }
6680b57cec5SDimitry Andric }
6690b57cec5SDimitry Andric 
6700b57cec5SDimitry Andric void CStringChecker::emitNotCStringBug(CheckerContext &C, ProgramStateRef State,
6710b57cec5SDimitry Andric                                        const Stmt *S,
6720b57cec5SDimitry Andric                                        StringRef WarningMsg) const {
6730b57cec5SDimitry Andric   if (ExplodedNode *N = C.generateNonFatalErrorNode(State)) {
6740b57cec5SDimitry Andric     if (!BT_NotCString)
6750b57cec5SDimitry Andric       BT_NotCString.reset(new BuiltinBug(
6760b57cec5SDimitry Andric           Filter.CheckNameCStringNotNullTerm, categories::UnixAPI,
6770b57cec5SDimitry Andric           "Argument is not a null-terminated string."));
6780b57cec5SDimitry Andric 
679a7dea167SDimitry Andric     auto Report =
680a7dea167SDimitry Andric         std::make_unique<PathSensitiveBugReport>(*BT_NotCString, WarningMsg, N);
6810b57cec5SDimitry Andric 
6820b57cec5SDimitry Andric     Report->addRange(S->getSourceRange());
6830b57cec5SDimitry Andric     C.emitReport(std::move(Report));
6840b57cec5SDimitry Andric   }
6850b57cec5SDimitry Andric }
6860b57cec5SDimitry Andric 
6870b57cec5SDimitry Andric void CStringChecker::emitAdditionOverflowBug(CheckerContext &C,
6880b57cec5SDimitry Andric                                              ProgramStateRef State) const {
6890b57cec5SDimitry Andric   if (ExplodedNode *N = C.generateErrorNode(State)) {
69081ad6265SDimitry Andric     if (!BT_AdditionOverflow)
69181ad6265SDimitry Andric       BT_AdditionOverflow.reset(
6920b57cec5SDimitry Andric           new BuiltinBug(Filter.CheckNameCStringOutOfBounds, "API",
6930b57cec5SDimitry Andric                          "Sum of expressions causes overflow."));
6940b57cec5SDimitry Andric 
6950b57cec5SDimitry Andric     // This isn't a great error message, but this should never occur in real
6960b57cec5SDimitry Andric     // code anyway -- you'd have to create a buffer longer than a size_t can
6970b57cec5SDimitry Andric     // represent, which is sort of a contradiction.
6980b57cec5SDimitry Andric     const char *WarningMsg =
6990b57cec5SDimitry Andric         "This expression will create a string whose length is too big to "
7000b57cec5SDimitry Andric         "be represented as a size_t";
7010b57cec5SDimitry Andric 
70281ad6265SDimitry Andric     auto Report = std::make_unique<PathSensitiveBugReport>(*BT_AdditionOverflow,
70381ad6265SDimitry Andric                                                            WarningMsg, N);
7040b57cec5SDimitry Andric     C.emitReport(std::move(Report));
7050b57cec5SDimitry Andric   }
7060b57cec5SDimitry Andric }
7070b57cec5SDimitry Andric 
7080b57cec5SDimitry Andric ProgramStateRef CStringChecker::checkAdditionOverflow(CheckerContext &C,
7090b57cec5SDimitry Andric                                                      ProgramStateRef state,
7100b57cec5SDimitry Andric                                                      NonLoc left,
7110b57cec5SDimitry Andric                                                      NonLoc right) const {
7120b57cec5SDimitry Andric   // If out-of-bounds checking is turned off, skip the rest.
7130b57cec5SDimitry Andric   if (!Filter.CheckCStringOutOfBounds)
7140b57cec5SDimitry Andric     return state;
7150b57cec5SDimitry Andric 
7160b57cec5SDimitry Andric   // If a previous check has failed, propagate the failure.
7170b57cec5SDimitry Andric   if (!state)
7180b57cec5SDimitry Andric     return nullptr;
7190b57cec5SDimitry Andric 
7200b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
7210b57cec5SDimitry Andric   BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
7220b57cec5SDimitry Andric 
7230b57cec5SDimitry Andric   QualType sizeTy = svalBuilder.getContext().getSizeType();
7240b57cec5SDimitry Andric   const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy);
7250b57cec5SDimitry Andric   NonLoc maxVal = svalBuilder.makeIntVal(maxValInt);
7260b57cec5SDimitry Andric 
7270b57cec5SDimitry Andric   SVal maxMinusRight;
72881ad6265SDimitry Andric   if (isa<nonloc::ConcreteInt>(right)) {
7290b57cec5SDimitry Andric     maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, right,
7300b57cec5SDimitry Andric                                                  sizeTy);
7310b57cec5SDimitry Andric   } else {
7320b57cec5SDimitry Andric     // Try switching the operands. (The order of these two assignments is
7330b57cec5SDimitry Andric     // important!)
7340b57cec5SDimitry Andric     maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, left,
7350b57cec5SDimitry Andric                                             sizeTy);
7360b57cec5SDimitry Andric     left = right;
7370b57cec5SDimitry Andric   }
7380b57cec5SDimitry Andric 
7390b57cec5SDimitry Andric   if (Optional<NonLoc> maxMinusRightNL = maxMinusRight.getAs<NonLoc>()) {
7400b57cec5SDimitry Andric     QualType cmpTy = svalBuilder.getConditionType();
7410b57cec5SDimitry Andric     // If left > max - right, we have an overflow.
7420b57cec5SDimitry Andric     SVal willOverflow = svalBuilder.evalBinOpNN(state, BO_GT, left,
7430b57cec5SDimitry Andric                                                 *maxMinusRightNL, cmpTy);
7440b57cec5SDimitry Andric 
7450b57cec5SDimitry Andric     ProgramStateRef stateOverflow, stateOkay;
7460b57cec5SDimitry Andric     std::tie(stateOverflow, stateOkay) =
7470b57cec5SDimitry Andric       state->assume(willOverflow.castAs<DefinedOrUnknownSVal>());
7480b57cec5SDimitry Andric 
7490b57cec5SDimitry Andric     if (stateOverflow && !stateOkay) {
7500b57cec5SDimitry Andric       // We have an overflow. Emit a bug report.
7510b57cec5SDimitry Andric       emitAdditionOverflowBug(C, stateOverflow);
7520b57cec5SDimitry Andric       return nullptr;
7530b57cec5SDimitry Andric     }
7540b57cec5SDimitry Andric 
7550b57cec5SDimitry Andric     // From now on, assume an overflow didn't occur.
7560b57cec5SDimitry Andric     assert(stateOkay);
7570b57cec5SDimitry Andric     state = stateOkay;
7580b57cec5SDimitry Andric   }
7590b57cec5SDimitry Andric 
7600b57cec5SDimitry Andric   return state;
7610b57cec5SDimitry Andric }
7620b57cec5SDimitry Andric 
7630b57cec5SDimitry Andric ProgramStateRef CStringChecker::setCStringLength(ProgramStateRef state,
7640b57cec5SDimitry Andric                                                 const MemRegion *MR,
7650b57cec5SDimitry Andric                                                 SVal strLength) {
7660b57cec5SDimitry Andric   assert(!strLength.isUndef() && "Attempt to set an undefined string length");
7670b57cec5SDimitry Andric 
7680b57cec5SDimitry Andric   MR = MR->StripCasts();
7690b57cec5SDimitry Andric 
7700b57cec5SDimitry Andric   switch (MR->getKind()) {
7710b57cec5SDimitry Andric   case MemRegion::StringRegionKind:
7720b57cec5SDimitry Andric     // FIXME: This can happen if we strcpy() into a string region. This is
7730b57cec5SDimitry Andric     // undefined [C99 6.4.5p6], but we should still warn about it.
7740b57cec5SDimitry Andric     return state;
7750b57cec5SDimitry Andric 
7760b57cec5SDimitry Andric   case MemRegion::SymbolicRegionKind:
7770b57cec5SDimitry Andric   case MemRegion::AllocaRegionKind:
7785ffd83dbSDimitry Andric   case MemRegion::NonParamVarRegionKind:
7795ffd83dbSDimitry Andric   case MemRegion::ParamVarRegionKind:
7800b57cec5SDimitry Andric   case MemRegion::FieldRegionKind:
7810b57cec5SDimitry Andric   case MemRegion::ObjCIvarRegionKind:
7820b57cec5SDimitry Andric     // These are the types we can currently track string lengths for.
7830b57cec5SDimitry Andric     break;
7840b57cec5SDimitry Andric 
7850b57cec5SDimitry Andric   case MemRegion::ElementRegionKind:
7860b57cec5SDimitry Andric     // FIXME: Handle element regions by upper-bounding the parent region's
7870b57cec5SDimitry Andric     // string length.
7880b57cec5SDimitry Andric     return state;
7890b57cec5SDimitry Andric 
7900b57cec5SDimitry Andric   default:
7910b57cec5SDimitry Andric     // Other regions (mostly non-data) can't have a reliable C string length.
7920b57cec5SDimitry Andric     // For now, just ignore the change.
7930b57cec5SDimitry Andric     // FIXME: These are rare but not impossible. We should output some kind of
7940b57cec5SDimitry Andric     // warning for things like strcpy((char[]){'a', 0}, "b");
7950b57cec5SDimitry Andric     return state;
7960b57cec5SDimitry Andric   }
7970b57cec5SDimitry Andric 
7980b57cec5SDimitry Andric   if (strLength.isUnknown())
7990b57cec5SDimitry Andric     return state->remove<CStringLength>(MR);
8000b57cec5SDimitry Andric 
8010b57cec5SDimitry Andric   return state->set<CStringLength>(MR, strLength);
8020b57cec5SDimitry Andric }
8030b57cec5SDimitry Andric 
8040b57cec5SDimitry Andric SVal CStringChecker::getCStringLengthForRegion(CheckerContext &C,
8050b57cec5SDimitry Andric                                                ProgramStateRef &state,
8060b57cec5SDimitry Andric                                                const Expr *Ex,
8070b57cec5SDimitry Andric                                                const MemRegion *MR,
8080b57cec5SDimitry Andric                                                bool hypothetical) {
8090b57cec5SDimitry Andric   if (!hypothetical) {
8100b57cec5SDimitry Andric     // If there's a recorded length, go ahead and return it.
8110b57cec5SDimitry Andric     const SVal *Recorded = state->get<CStringLength>(MR);
8120b57cec5SDimitry Andric     if (Recorded)
8130b57cec5SDimitry Andric       return *Recorded;
8140b57cec5SDimitry Andric   }
8150b57cec5SDimitry Andric 
8160b57cec5SDimitry Andric   // Otherwise, get a new symbol and update the state.
8170b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
8180b57cec5SDimitry Andric   QualType sizeTy = svalBuilder.getContext().getSizeType();
8190b57cec5SDimitry Andric   SVal strLength = svalBuilder.getMetadataSymbolVal(CStringChecker::getTag(),
8200b57cec5SDimitry Andric                                                     MR, Ex, sizeTy,
8210b57cec5SDimitry Andric                                                     C.getLocationContext(),
8220b57cec5SDimitry Andric                                                     C.blockCount());
8230b57cec5SDimitry Andric 
8240b57cec5SDimitry Andric   if (!hypothetical) {
8250b57cec5SDimitry Andric     if (Optional<NonLoc> strLn = strLength.getAs<NonLoc>()) {
8260b57cec5SDimitry Andric       // In case of unbounded calls strlen etc bound the range to SIZE_MAX/4
8270b57cec5SDimitry Andric       BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
8280b57cec5SDimitry Andric       const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy);
8290b57cec5SDimitry Andric       llvm::APSInt fourInt = APSIntType(maxValInt).getValue(4);
8300b57cec5SDimitry Andric       const llvm::APSInt *maxLengthInt = BVF.evalAPSInt(BO_Div, maxValInt,
8310b57cec5SDimitry Andric                                                         fourInt);
8320b57cec5SDimitry Andric       NonLoc maxLength = svalBuilder.makeIntVal(*maxLengthInt);
8330b57cec5SDimitry Andric       SVal evalLength = svalBuilder.evalBinOpNN(state, BO_LE, *strLn,
8340b57cec5SDimitry Andric                                                 maxLength, sizeTy);
8350b57cec5SDimitry Andric       state = state->assume(evalLength.castAs<DefinedOrUnknownSVal>(), true);
8360b57cec5SDimitry Andric     }
8370b57cec5SDimitry Andric     state = state->set<CStringLength>(MR, strLength);
8380b57cec5SDimitry Andric   }
8390b57cec5SDimitry Andric 
8400b57cec5SDimitry Andric   return strLength;
8410b57cec5SDimitry Andric }
8420b57cec5SDimitry Andric 
8430b57cec5SDimitry Andric SVal CStringChecker::getCStringLength(CheckerContext &C, ProgramStateRef &state,
8440b57cec5SDimitry Andric                                       const Expr *Ex, SVal Buf,
8450b57cec5SDimitry Andric                                       bool hypothetical) const {
8460b57cec5SDimitry Andric   const MemRegion *MR = Buf.getAsRegion();
8470b57cec5SDimitry Andric   if (!MR) {
8480b57cec5SDimitry Andric     // If we can't get a region, see if it's something we /know/ isn't a
8490b57cec5SDimitry Andric     // C string. In the context of locations, the only time we can issue such
8500b57cec5SDimitry Andric     // a warning is for labels.
8510b57cec5SDimitry Andric     if (Optional<loc::GotoLabel> Label = Buf.getAs<loc::GotoLabel>()) {
8520b57cec5SDimitry Andric       if (Filter.CheckCStringNotNullTerm) {
8530b57cec5SDimitry Andric         SmallString<120> buf;
8540b57cec5SDimitry Andric         llvm::raw_svector_ostream os(buf);
8550b57cec5SDimitry Andric         assert(CurrentFunctionDescription);
8560b57cec5SDimitry Andric         os << "Argument to " << CurrentFunctionDescription
8570b57cec5SDimitry Andric            << " is the address of the label '" << Label->getLabel()->getName()
8580b57cec5SDimitry Andric            << "', which is not a null-terminated string";
8590b57cec5SDimitry Andric 
8600b57cec5SDimitry Andric         emitNotCStringBug(C, state, Ex, os.str());
8610b57cec5SDimitry Andric       }
8620b57cec5SDimitry Andric       return UndefinedVal();
8630b57cec5SDimitry Andric     }
8640b57cec5SDimitry Andric 
8650b57cec5SDimitry Andric     // If it's not a region and not a label, give up.
8660b57cec5SDimitry Andric     return UnknownVal();
8670b57cec5SDimitry Andric   }
8680b57cec5SDimitry Andric 
8690b57cec5SDimitry Andric   // If we have a region, strip casts from it and see if we can figure out
8700b57cec5SDimitry Andric   // its length. For anything we can't figure out, just return UnknownVal.
8710b57cec5SDimitry Andric   MR = MR->StripCasts();
8720b57cec5SDimitry Andric 
8730b57cec5SDimitry Andric   switch (MR->getKind()) {
8740b57cec5SDimitry Andric   case MemRegion::StringRegionKind: {
8750b57cec5SDimitry Andric     // Modifying the contents of string regions is undefined [C99 6.4.5p6],
8760b57cec5SDimitry Andric     // so we can assume that the byte length is the correct C string length.
8770b57cec5SDimitry Andric     SValBuilder &svalBuilder = C.getSValBuilder();
8780b57cec5SDimitry Andric     QualType sizeTy = svalBuilder.getContext().getSizeType();
8790b57cec5SDimitry Andric     const StringLiteral *strLit = cast<StringRegion>(MR)->getStringLiteral();
880753f127fSDimitry Andric     return svalBuilder.makeIntVal(strLit->getLength(), sizeTy);
8810b57cec5SDimitry Andric   }
8820b57cec5SDimitry Andric   case MemRegion::SymbolicRegionKind:
8830b57cec5SDimitry Andric   case MemRegion::AllocaRegionKind:
8845ffd83dbSDimitry Andric   case MemRegion::NonParamVarRegionKind:
8855ffd83dbSDimitry Andric   case MemRegion::ParamVarRegionKind:
8860b57cec5SDimitry Andric   case MemRegion::FieldRegionKind:
8870b57cec5SDimitry Andric   case MemRegion::ObjCIvarRegionKind:
8880b57cec5SDimitry Andric     return getCStringLengthForRegion(C, state, Ex, MR, hypothetical);
8890b57cec5SDimitry Andric   case MemRegion::CompoundLiteralRegionKind:
8900b57cec5SDimitry Andric     // FIXME: Can we track this? Is it necessary?
8910b57cec5SDimitry Andric     return UnknownVal();
8920b57cec5SDimitry Andric   case MemRegion::ElementRegionKind:
8930b57cec5SDimitry Andric     // FIXME: How can we handle this? It's not good enough to subtract the
8940b57cec5SDimitry Andric     // offset from the base string length; consider "123\x00567" and &a[5].
8950b57cec5SDimitry Andric     return UnknownVal();
8960b57cec5SDimitry Andric   default:
8970b57cec5SDimitry Andric     // Other regions (mostly non-data) can't have a reliable C string length.
8980b57cec5SDimitry Andric     // In this case, an error is emitted and UndefinedVal is returned.
8990b57cec5SDimitry Andric     // The caller should always be prepared to handle this case.
9000b57cec5SDimitry Andric     if (Filter.CheckCStringNotNullTerm) {
9010b57cec5SDimitry Andric       SmallString<120> buf;
9020b57cec5SDimitry Andric       llvm::raw_svector_ostream os(buf);
9030b57cec5SDimitry Andric 
9040b57cec5SDimitry Andric       assert(CurrentFunctionDescription);
9050b57cec5SDimitry Andric       os << "Argument to " << CurrentFunctionDescription << " is ";
9060b57cec5SDimitry Andric 
9070b57cec5SDimitry Andric       if (SummarizeRegion(os, C.getASTContext(), MR))
9080b57cec5SDimitry Andric         os << ", which is not a null-terminated string";
9090b57cec5SDimitry Andric       else
9100b57cec5SDimitry Andric         os << "not a null-terminated string";
9110b57cec5SDimitry Andric 
9120b57cec5SDimitry Andric       emitNotCStringBug(C, state, Ex, os.str());
9130b57cec5SDimitry Andric     }
9140b57cec5SDimitry Andric     return UndefinedVal();
9150b57cec5SDimitry Andric   }
9160b57cec5SDimitry Andric }
9170b57cec5SDimitry Andric 
9180b57cec5SDimitry Andric const StringLiteral *CStringChecker::getCStringLiteral(CheckerContext &C,
9190b57cec5SDimitry Andric   ProgramStateRef &state, const Expr *expr, SVal val) const {
9200b57cec5SDimitry Andric 
9210b57cec5SDimitry Andric   // Get the memory region pointed to by the val.
9220b57cec5SDimitry Andric   const MemRegion *bufRegion = val.getAsRegion();
9230b57cec5SDimitry Andric   if (!bufRegion)
9240b57cec5SDimitry Andric     return nullptr;
9250b57cec5SDimitry Andric 
9260b57cec5SDimitry Andric   // Strip casts off the memory region.
9270b57cec5SDimitry Andric   bufRegion = bufRegion->StripCasts();
9280b57cec5SDimitry Andric 
9290b57cec5SDimitry Andric   // Cast the memory region to a string region.
9300b57cec5SDimitry Andric   const StringRegion *strRegion= dyn_cast<StringRegion>(bufRegion);
9310b57cec5SDimitry Andric   if (!strRegion)
9320b57cec5SDimitry Andric     return nullptr;
9330b57cec5SDimitry Andric 
9340b57cec5SDimitry Andric   // Return the actual string in the string region.
9350b57cec5SDimitry Andric   return strRegion->getStringLiteral();
9360b57cec5SDimitry Andric }
9370b57cec5SDimitry Andric 
9380b57cec5SDimitry Andric bool CStringChecker::IsFirstBufInBound(CheckerContext &C,
9390b57cec5SDimitry Andric                                        ProgramStateRef state,
9400b57cec5SDimitry Andric                                        const Expr *FirstBuf,
9410b57cec5SDimitry Andric                                        const Expr *Size) {
9420b57cec5SDimitry Andric   // If we do not know that the buffer is long enough we return 'true'.
9430b57cec5SDimitry Andric   // Otherwise the parent region of this field region would also get
9440b57cec5SDimitry Andric   // invalidated, which would lead to warnings based on an unknown state.
9450b57cec5SDimitry Andric 
9460b57cec5SDimitry Andric   // Originally copied from CheckBufferAccess and CheckLocation.
9470b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
9480b57cec5SDimitry Andric   ASTContext &Ctx = svalBuilder.getContext();
9490b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
9500b57cec5SDimitry Andric 
9510b57cec5SDimitry Andric   QualType sizeTy = Size->getType();
9520b57cec5SDimitry Andric   QualType PtrTy = Ctx.getPointerType(Ctx.CharTy);
9530b57cec5SDimitry Andric   SVal BufVal = state->getSVal(FirstBuf, LCtx);
9540b57cec5SDimitry Andric 
9550b57cec5SDimitry Andric   SVal LengthVal = state->getSVal(Size, LCtx);
9560b57cec5SDimitry Andric   Optional<NonLoc> Length = LengthVal.getAs<NonLoc>();
9570b57cec5SDimitry Andric   if (!Length)
9580b57cec5SDimitry Andric     return true; // cf top comment.
9590b57cec5SDimitry Andric 
9600b57cec5SDimitry Andric   // Compute the offset of the last element to be accessed: size-1.
9610b57cec5SDimitry Andric   NonLoc One = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>();
9620b57cec5SDimitry Andric   SVal Offset = svalBuilder.evalBinOpNN(state, BO_Sub, *Length, One, sizeTy);
9630b57cec5SDimitry Andric   if (Offset.isUnknown())
9640b57cec5SDimitry Andric     return true; // cf top comment
9650b57cec5SDimitry Andric   NonLoc LastOffset = Offset.castAs<NonLoc>();
9660b57cec5SDimitry Andric 
9670b57cec5SDimitry Andric   // Check that the first buffer is sufficiently long.
9680b57cec5SDimitry Andric   SVal BufStart = svalBuilder.evalCast(BufVal, PtrTy, FirstBuf->getType());
9690b57cec5SDimitry Andric   Optional<Loc> BufLoc = BufStart.getAs<Loc>();
9700b57cec5SDimitry Andric   if (!BufLoc)
9710b57cec5SDimitry Andric     return true; // cf top comment.
9720b57cec5SDimitry Andric 
9730b57cec5SDimitry Andric   SVal BufEnd =
9740b57cec5SDimitry Andric       svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc, LastOffset, PtrTy);
9750b57cec5SDimitry Andric 
9760b57cec5SDimitry Andric   // Check for out of bound array element access.
9770b57cec5SDimitry Andric   const MemRegion *R = BufEnd.getAsRegion();
9780b57cec5SDimitry Andric   if (!R)
9790b57cec5SDimitry Andric     return true; // cf top comment.
9800b57cec5SDimitry Andric 
9810b57cec5SDimitry Andric   const ElementRegion *ER = dyn_cast<ElementRegion>(R);
9820b57cec5SDimitry Andric   if (!ER)
9830b57cec5SDimitry Andric     return true; // cf top comment.
9840b57cec5SDimitry Andric 
9850b57cec5SDimitry Andric   // FIXME: Does this crash when a non-standard definition
9860b57cec5SDimitry Andric   // of a library function is encountered?
9870b57cec5SDimitry Andric   assert(ER->getValueType() == C.getASTContext().CharTy &&
9880b57cec5SDimitry Andric          "IsFirstBufInBound should only be called with char* ElementRegions");
9890b57cec5SDimitry Andric 
9900b57cec5SDimitry Andric   // Get the size of the array.
9910b57cec5SDimitry Andric   const SubRegion *superReg = cast<SubRegion>(ER->getSuperRegion());
992fe6060f1SDimitry Andric   DefinedOrUnknownSVal SizeDV = getDynamicExtent(state, superReg, svalBuilder);
9930b57cec5SDimitry Andric 
9940b57cec5SDimitry Andric   // Get the index of the accessed element.
9950b57cec5SDimitry Andric   DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>();
9960b57cec5SDimitry Andric 
9975ffd83dbSDimitry Andric   ProgramStateRef StInBound = state->assumeInBound(Idx, SizeDV, true);
9980b57cec5SDimitry Andric 
9990b57cec5SDimitry Andric   return static_cast<bool>(StInBound);
10000b57cec5SDimitry Andric }
10010b57cec5SDimitry Andric 
10020b57cec5SDimitry Andric ProgramStateRef CStringChecker::InvalidateBuffer(CheckerContext &C,
10030b57cec5SDimitry Andric                                                  ProgramStateRef state,
10040b57cec5SDimitry Andric                                                  const Expr *E, SVal V,
10050b57cec5SDimitry Andric                                                  bool IsSourceBuffer,
10060b57cec5SDimitry Andric                                                  const Expr *Size) {
10070b57cec5SDimitry Andric   Optional<Loc> L = V.getAs<Loc>();
10080b57cec5SDimitry Andric   if (!L)
10090b57cec5SDimitry Andric     return state;
10100b57cec5SDimitry Andric 
10110b57cec5SDimitry Andric   // FIXME: This is a simplified version of what's in CFRefCount.cpp -- it makes
10120b57cec5SDimitry Andric   // some assumptions about the value that CFRefCount can't. Even so, it should
10130b57cec5SDimitry Andric   // probably be refactored.
10140b57cec5SDimitry Andric   if (Optional<loc::MemRegionVal> MR = L->getAs<loc::MemRegionVal>()) {
10150b57cec5SDimitry Andric     const MemRegion *R = MR->getRegion()->StripCasts();
10160b57cec5SDimitry Andric 
10170b57cec5SDimitry Andric     // Are we dealing with an ElementRegion?  If so, we should be invalidating
10180b57cec5SDimitry Andric     // the super-region.
10190b57cec5SDimitry Andric     if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
10200b57cec5SDimitry Andric       R = ER->getSuperRegion();
10210b57cec5SDimitry Andric       // FIXME: What about layers of ElementRegions?
10220b57cec5SDimitry Andric     }
10230b57cec5SDimitry Andric 
10240b57cec5SDimitry Andric     // Invalidate this region.
10250b57cec5SDimitry Andric     const LocationContext *LCtx = C.getPredecessor()->getLocationContext();
10260b57cec5SDimitry Andric 
10270b57cec5SDimitry Andric     bool CausesPointerEscape = false;
10280b57cec5SDimitry Andric     RegionAndSymbolInvalidationTraits ITraits;
10290b57cec5SDimitry Andric     // Invalidate and escape only indirect regions accessible through the source
10300b57cec5SDimitry Andric     // buffer.
10310b57cec5SDimitry Andric     if (IsSourceBuffer) {
10320b57cec5SDimitry Andric       ITraits.setTrait(R->getBaseRegion(),
10330b57cec5SDimitry Andric                        RegionAndSymbolInvalidationTraits::TK_PreserveContents);
10340b57cec5SDimitry Andric       ITraits.setTrait(R, RegionAndSymbolInvalidationTraits::TK_SuppressEscape);
10350b57cec5SDimitry Andric       CausesPointerEscape = true;
10360b57cec5SDimitry Andric     } else {
10370b57cec5SDimitry Andric       const MemRegion::Kind& K = R->getKind();
10380b57cec5SDimitry Andric       if (K == MemRegion::FieldRegionKind)
10390b57cec5SDimitry Andric         if (Size && IsFirstBufInBound(C, state, E, Size)) {
10400b57cec5SDimitry Andric           // If destination buffer is a field region and access is in bound,
10410b57cec5SDimitry Andric           // do not invalidate its super region.
10420b57cec5SDimitry Andric           ITraits.setTrait(
10430b57cec5SDimitry Andric               R,
10440b57cec5SDimitry Andric               RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion);
10450b57cec5SDimitry Andric         }
10460b57cec5SDimitry Andric     }
10470b57cec5SDimitry Andric 
10480b57cec5SDimitry Andric     return state->invalidateRegions(R, E, C.blockCount(), LCtx,
10490b57cec5SDimitry Andric                                     CausesPointerEscape, nullptr, nullptr,
10500b57cec5SDimitry Andric                                     &ITraits);
10510b57cec5SDimitry Andric   }
10520b57cec5SDimitry Andric 
10530b57cec5SDimitry Andric   // If we have a non-region value by chance, just remove the binding.
10540b57cec5SDimitry Andric   // FIXME: is this necessary or correct? This handles the non-Region
10550b57cec5SDimitry Andric   //  cases.  Is it ever valid to store to these?
10560b57cec5SDimitry Andric   return state->killBinding(*L);
10570b57cec5SDimitry Andric }
10580b57cec5SDimitry Andric 
10590b57cec5SDimitry Andric bool CStringChecker::SummarizeRegion(raw_ostream &os, ASTContext &Ctx,
10600b57cec5SDimitry Andric                                      const MemRegion *MR) {
10610b57cec5SDimitry Andric   switch (MR->getKind()) {
10620b57cec5SDimitry Andric   case MemRegion::FunctionCodeRegionKind: {
1063480093f4SDimitry Andric     if (const auto *FD = cast<FunctionCodeRegion>(MR)->getDecl())
10640b57cec5SDimitry Andric       os << "the address of the function '" << *FD << '\'';
10650b57cec5SDimitry Andric     else
10660b57cec5SDimitry Andric       os << "the address of a function";
10670b57cec5SDimitry Andric     return true;
10680b57cec5SDimitry Andric   }
10690b57cec5SDimitry Andric   case MemRegion::BlockCodeRegionKind:
10700b57cec5SDimitry Andric     os << "block text";
10710b57cec5SDimitry Andric     return true;
10720b57cec5SDimitry Andric   case MemRegion::BlockDataRegionKind:
10730b57cec5SDimitry Andric     os << "a block";
10740b57cec5SDimitry Andric     return true;
10750b57cec5SDimitry Andric   case MemRegion::CXXThisRegionKind:
10760b57cec5SDimitry Andric   case MemRegion::CXXTempObjectRegionKind:
1077480093f4SDimitry Andric     os << "a C++ temp object of type "
107881ad6265SDimitry Andric        << cast<TypedValueRegion>(MR)->getValueType();
10790b57cec5SDimitry Andric     return true;
10805ffd83dbSDimitry Andric   case MemRegion::NonParamVarRegionKind:
108181ad6265SDimitry Andric     os << "a variable of type" << cast<TypedValueRegion>(MR)->getValueType();
10820b57cec5SDimitry Andric     return true;
10835ffd83dbSDimitry Andric   case MemRegion::ParamVarRegionKind:
108481ad6265SDimitry Andric     os << "a parameter of type" << cast<TypedValueRegion>(MR)->getValueType();
10855ffd83dbSDimitry Andric     return true;
10860b57cec5SDimitry Andric   case MemRegion::FieldRegionKind:
108781ad6265SDimitry Andric     os << "a field of type " << cast<TypedValueRegion>(MR)->getValueType();
10880b57cec5SDimitry Andric     return true;
10890b57cec5SDimitry Andric   case MemRegion::ObjCIvarRegionKind:
1090480093f4SDimitry Andric     os << "an instance variable of type "
109181ad6265SDimitry Andric        << cast<TypedValueRegion>(MR)->getValueType();
10920b57cec5SDimitry Andric     return true;
10930b57cec5SDimitry Andric   default:
10940b57cec5SDimitry Andric     return false;
10950b57cec5SDimitry Andric   }
10960b57cec5SDimitry Andric }
10970b57cec5SDimitry Andric 
10980b57cec5SDimitry Andric bool CStringChecker::memsetAux(const Expr *DstBuffer, SVal CharVal,
10990b57cec5SDimitry Andric                                const Expr *Size, CheckerContext &C,
11000b57cec5SDimitry Andric                                ProgramStateRef &State) {
11010b57cec5SDimitry Andric   SVal MemVal = C.getSVal(DstBuffer);
11020b57cec5SDimitry Andric   SVal SizeVal = C.getSVal(Size);
11030b57cec5SDimitry Andric   const MemRegion *MR = MemVal.getAsRegion();
11040b57cec5SDimitry Andric   if (!MR)
11050b57cec5SDimitry Andric     return false;
11060b57cec5SDimitry Andric 
11070b57cec5SDimitry Andric   // We're about to model memset by producing a "default binding" in the Store.
11080b57cec5SDimitry Andric   // Our current implementation - RegionStore - doesn't support default bindings
11090b57cec5SDimitry Andric   // that don't cover the whole base region. So we should first get the offset
11100b57cec5SDimitry Andric   // and the base region to figure out whether the offset of buffer is 0.
11110b57cec5SDimitry Andric   RegionOffset Offset = MR->getAsOffset();
11120b57cec5SDimitry Andric   const MemRegion *BR = Offset.getRegion();
11130b57cec5SDimitry Andric 
11140b57cec5SDimitry Andric   Optional<NonLoc> SizeNL = SizeVal.getAs<NonLoc>();
11150b57cec5SDimitry Andric   if (!SizeNL)
11160b57cec5SDimitry Andric     return false;
11170b57cec5SDimitry Andric 
11180b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
11190b57cec5SDimitry Andric   ASTContext &Ctx = C.getASTContext();
11200b57cec5SDimitry Andric 
11210b57cec5SDimitry Andric   // void *memset(void *dest, int ch, size_t count);
11220b57cec5SDimitry Andric   // For now we can only handle the case of offset is 0 and concrete char value.
11230b57cec5SDimitry Andric   if (Offset.isValid() && !Offset.hasSymbolicOffset() &&
11240b57cec5SDimitry Andric       Offset.getOffset() == 0) {
11255ffd83dbSDimitry Andric     // Get the base region's size.
1126fe6060f1SDimitry Andric     DefinedOrUnknownSVal SizeDV = getDynamicExtent(State, BR, svalBuilder);
11270b57cec5SDimitry Andric 
11280b57cec5SDimitry Andric     ProgramStateRef StateWholeReg, StateNotWholeReg;
11290b57cec5SDimitry Andric     std::tie(StateWholeReg, StateNotWholeReg) =
11305ffd83dbSDimitry Andric         State->assume(svalBuilder.evalEQ(State, SizeDV, *SizeNL));
11310b57cec5SDimitry Andric 
11320b57cec5SDimitry Andric     // With the semantic of 'memset()', we should convert the CharVal to
11330b57cec5SDimitry Andric     // unsigned char.
11340b57cec5SDimitry Andric     CharVal = svalBuilder.evalCast(CharVal, Ctx.UnsignedCharTy, Ctx.IntTy);
11350b57cec5SDimitry Andric 
11360b57cec5SDimitry Andric     ProgramStateRef StateNullChar, StateNonNullChar;
11370b57cec5SDimitry Andric     std::tie(StateNullChar, StateNonNullChar) =
11380b57cec5SDimitry Andric         assumeZero(C, State, CharVal, Ctx.UnsignedCharTy);
11390b57cec5SDimitry Andric 
11400b57cec5SDimitry Andric     if (StateWholeReg && !StateNotWholeReg && StateNullChar &&
11410b57cec5SDimitry Andric         !StateNonNullChar) {
11420b57cec5SDimitry Andric       // If the 'memset()' acts on the whole region of destination buffer and
11430b57cec5SDimitry Andric       // the value of the second argument of 'memset()' is zero, bind the second
11440b57cec5SDimitry Andric       // argument's value to the destination buffer with 'default binding'.
11450b57cec5SDimitry Andric       // FIXME: Since there is no perfect way to bind the non-zero character, we
11460b57cec5SDimitry Andric       // can only deal with zero value here. In the future, we need to deal with
11470b57cec5SDimitry Andric       // the binding of non-zero value in the case of whole region.
11480b57cec5SDimitry Andric       State = State->bindDefaultZero(svalBuilder.makeLoc(BR),
11490b57cec5SDimitry Andric                                      C.getLocationContext());
11500b57cec5SDimitry Andric     } else {
11510b57cec5SDimitry Andric       // If the destination buffer's extent is not equal to the value of
11520b57cec5SDimitry Andric       // third argument, just invalidate buffer.
11530b57cec5SDimitry Andric       State = InvalidateBuffer(C, State, DstBuffer, MemVal,
11540b57cec5SDimitry Andric                                /*IsSourceBuffer*/ false, Size);
11550b57cec5SDimitry Andric     }
11560b57cec5SDimitry Andric 
11570b57cec5SDimitry Andric     if (StateNullChar && !StateNonNullChar) {
11580b57cec5SDimitry Andric       // If the value of the second argument of 'memset()' is zero, set the
11590b57cec5SDimitry Andric       // string length of destination buffer to 0 directly.
11600b57cec5SDimitry Andric       State = setCStringLength(State, MR,
11610b57cec5SDimitry Andric                                svalBuilder.makeZeroVal(Ctx.getSizeType()));
11620b57cec5SDimitry Andric     } else if (!StateNullChar && StateNonNullChar) {
11630b57cec5SDimitry Andric       SVal NewStrLen = svalBuilder.getMetadataSymbolVal(
11640b57cec5SDimitry Andric           CStringChecker::getTag(), MR, DstBuffer, Ctx.getSizeType(),
11650b57cec5SDimitry Andric           C.getLocationContext(), C.blockCount());
11660b57cec5SDimitry Andric 
11670b57cec5SDimitry Andric       // If the value of second argument is not zero, then the string length
11680b57cec5SDimitry Andric       // is at least the size argument.
11690b57cec5SDimitry Andric       SVal NewStrLenGESize = svalBuilder.evalBinOp(
11700b57cec5SDimitry Andric           State, BO_GE, NewStrLen, SizeVal, svalBuilder.getConditionType());
11710b57cec5SDimitry Andric 
11720b57cec5SDimitry Andric       State = setCStringLength(
11730b57cec5SDimitry Andric           State->assume(NewStrLenGESize.castAs<DefinedOrUnknownSVal>(), true),
11740b57cec5SDimitry Andric           MR, NewStrLen);
11750b57cec5SDimitry Andric     }
11760b57cec5SDimitry Andric   } else {
11770b57cec5SDimitry Andric     // If the offset is not zero and char value is not concrete, we can do
11780b57cec5SDimitry Andric     // nothing but invalidate the buffer.
11790b57cec5SDimitry Andric     State = InvalidateBuffer(C, State, DstBuffer, MemVal,
11800b57cec5SDimitry Andric                              /*IsSourceBuffer*/ false, Size);
11810b57cec5SDimitry Andric   }
11820b57cec5SDimitry Andric   return true;
11830b57cec5SDimitry Andric }
11840b57cec5SDimitry Andric 
11850b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
11860b57cec5SDimitry Andric // evaluation of individual function calls.
11870b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
11880b57cec5SDimitry Andric 
11895ffd83dbSDimitry Andric void CStringChecker::evalCopyCommon(CheckerContext &C, const CallExpr *CE,
11905ffd83dbSDimitry Andric                                     ProgramStateRef state, SizeArgExpr Size,
11915ffd83dbSDimitry Andric                                     DestinationArgExpr Dest,
11925ffd83dbSDimitry Andric                                     SourceArgExpr Source, bool Restricted,
1193*972a253aSDimitry Andric                                     bool IsMempcpy, bool IsWide) const {
11940b57cec5SDimitry Andric   CurrentFunctionDescription = "memory copy function";
11950b57cec5SDimitry Andric 
11960b57cec5SDimitry Andric   // See if the size argument is zero.
11970b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
11985ffd83dbSDimitry Andric   SVal sizeVal = state->getSVal(Size.Expression, LCtx);
11995ffd83dbSDimitry Andric   QualType sizeTy = Size.Expression->getType();
12000b57cec5SDimitry Andric 
12010b57cec5SDimitry Andric   ProgramStateRef stateZeroSize, stateNonZeroSize;
12020b57cec5SDimitry Andric   std::tie(stateZeroSize, stateNonZeroSize) =
12030b57cec5SDimitry Andric       assumeZero(C, state, sizeVal, sizeTy);
12040b57cec5SDimitry Andric 
12050b57cec5SDimitry Andric   // Get the value of the Dest.
12065ffd83dbSDimitry Andric   SVal destVal = state->getSVal(Dest.Expression, LCtx);
12070b57cec5SDimitry Andric 
12080b57cec5SDimitry Andric   // If the size is zero, there won't be any actual memory access, so
12090b57cec5SDimitry Andric   // just bind the return value to the destination buffer and return.
12100b57cec5SDimitry Andric   if (stateZeroSize && !stateNonZeroSize) {
12110b57cec5SDimitry Andric     stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, destVal);
12120b57cec5SDimitry Andric     C.addTransition(stateZeroSize);
12130b57cec5SDimitry Andric     return;
12140b57cec5SDimitry Andric   }
12150b57cec5SDimitry Andric 
12160b57cec5SDimitry Andric   // If the size can be nonzero, we have to check the other arguments.
12170b57cec5SDimitry Andric   if (stateNonZeroSize) {
12180b57cec5SDimitry Andric     state = stateNonZeroSize;
12190b57cec5SDimitry Andric 
12200b57cec5SDimitry Andric     // Ensure the destination is not null. If it is NULL there will be a
12210b57cec5SDimitry Andric     // NULL pointer dereference.
12225ffd83dbSDimitry Andric     state = checkNonNull(C, state, Dest, destVal);
12230b57cec5SDimitry Andric     if (!state)
12240b57cec5SDimitry Andric       return;
12250b57cec5SDimitry Andric 
12260b57cec5SDimitry Andric     // Get the value of the Src.
12275ffd83dbSDimitry Andric     SVal srcVal = state->getSVal(Source.Expression, LCtx);
12280b57cec5SDimitry Andric 
12290b57cec5SDimitry Andric     // Ensure the source is not null. If it is NULL there will be a
12300b57cec5SDimitry Andric     // NULL pointer dereference.
12315ffd83dbSDimitry Andric     state = checkNonNull(C, state, Source, srcVal);
12320b57cec5SDimitry Andric     if (!state)
12330b57cec5SDimitry Andric       return;
12340b57cec5SDimitry Andric 
12350b57cec5SDimitry Andric     // Ensure the accesses are valid and that the buffers do not overlap.
1236*972a253aSDimitry Andric     state = CheckBufferAccess(C, state, Dest, Size, AccessKind::write, IsWide);
1237*972a253aSDimitry Andric     state = CheckBufferAccess(C, state, Source, Size, AccessKind::read, IsWide);
12385ffd83dbSDimitry Andric 
12390b57cec5SDimitry Andric     if (Restricted)
1240*972a253aSDimitry Andric       state = CheckOverlap(C, state, Size, Dest, Source, IsWide);
12410b57cec5SDimitry Andric 
12420b57cec5SDimitry Andric     if (!state)
12430b57cec5SDimitry Andric       return;
12440b57cec5SDimitry Andric 
12450b57cec5SDimitry Andric     // If this is mempcpy, get the byte after the last byte copied and
12460b57cec5SDimitry Andric     // bind the expr.
12470b57cec5SDimitry Andric     if (IsMempcpy) {
12480b57cec5SDimitry Andric       // Get the byte after the last byte copied.
12490b57cec5SDimitry Andric       SValBuilder &SvalBuilder = C.getSValBuilder();
12500b57cec5SDimitry Andric       ASTContext &Ctx = SvalBuilder.getContext();
12510b57cec5SDimitry Andric       QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy);
12520b57cec5SDimitry Andric       SVal DestRegCharVal =
12535ffd83dbSDimitry Andric           SvalBuilder.evalCast(destVal, CharPtrTy, Dest.Expression->getType());
12540b57cec5SDimitry Andric       SVal lastElement = C.getSValBuilder().evalBinOp(
12555ffd83dbSDimitry Andric           state, BO_Add, DestRegCharVal, sizeVal, Dest.Expression->getType());
12560b57cec5SDimitry Andric       // If we don't know how much we copied, we can at least
12570b57cec5SDimitry Andric       // conjure a return value for later.
12580b57cec5SDimitry Andric       if (lastElement.isUnknown())
12590b57cec5SDimitry Andric         lastElement = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx,
12600b57cec5SDimitry Andric                                                           C.blockCount());
12610b57cec5SDimitry Andric 
12620b57cec5SDimitry Andric       // The byte after the last byte copied is the return value.
12630b57cec5SDimitry Andric       state = state->BindExpr(CE, LCtx, lastElement);
12640b57cec5SDimitry Andric     } else {
12650b57cec5SDimitry Andric       // All other copies return the destination buffer.
12660b57cec5SDimitry Andric       // (Well, bcopy() has a void return type, but this won't hurt.)
12670b57cec5SDimitry Andric       state = state->BindExpr(CE, LCtx, destVal);
12680b57cec5SDimitry Andric     }
12690b57cec5SDimitry Andric 
12700b57cec5SDimitry Andric     // Invalidate the destination (regular invalidation without pointer-escaping
12710b57cec5SDimitry Andric     // the address of the top-level region).
12720b57cec5SDimitry Andric     // FIXME: Even if we can't perfectly model the copy, we should see if we
12730b57cec5SDimitry Andric     // can use LazyCompoundVals to copy the source values into the destination.
12740b57cec5SDimitry Andric     // This would probably remove any existing bindings past the end of the
12750b57cec5SDimitry Andric     // copied region, but that's still an improvement over blank invalidation.
12765ffd83dbSDimitry Andric     state =
12775ffd83dbSDimitry Andric         InvalidateBuffer(C, state, Dest.Expression, C.getSVal(Dest.Expression),
12785ffd83dbSDimitry Andric                          /*IsSourceBuffer*/ false, Size.Expression);
12790b57cec5SDimitry Andric 
12800b57cec5SDimitry Andric     // Invalidate the source (const-invalidation without const-pointer-escaping
12810b57cec5SDimitry Andric     // the address of the top-level region).
12825ffd83dbSDimitry Andric     state = InvalidateBuffer(C, state, Source.Expression,
12835ffd83dbSDimitry Andric                              C.getSVal(Source.Expression),
12840b57cec5SDimitry Andric                              /*IsSourceBuffer*/ true, nullptr);
12850b57cec5SDimitry Andric 
12860b57cec5SDimitry Andric     C.addTransition(state);
12870b57cec5SDimitry Andric   }
12880b57cec5SDimitry Andric }
12890b57cec5SDimitry Andric 
1290*972a253aSDimitry Andric void CStringChecker::evalMemcpy(CheckerContext &C, const CallExpr *CE,
1291*972a253aSDimitry Andric                                 bool IsWide) const {
12920b57cec5SDimitry Andric   // void *memcpy(void *restrict dst, const void *restrict src, size_t n);
12930b57cec5SDimitry Andric   // The return value is the address of the destination buffer.
12945ffd83dbSDimitry Andric   DestinationArgExpr Dest = {CE->getArg(0), 0};
12955ffd83dbSDimitry Andric   SourceArgExpr Src = {CE->getArg(1), 1};
12965ffd83dbSDimitry Andric   SizeArgExpr Size = {CE->getArg(2), 2};
12970b57cec5SDimitry Andric 
12985ffd83dbSDimitry Andric   ProgramStateRef State = C.getState();
12995ffd83dbSDimitry Andric 
13005ffd83dbSDimitry Andric   constexpr bool IsRestricted = true;
13015ffd83dbSDimitry Andric   constexpr bool IsMempcpy = false;
1302*972a253aSDimitry Andric   evalCopyCommon(C, CE, State, Size, Dest, Src, IsRestricted, IsMempcpy,
1303*972a253aSDimitry Andric                  IsWide);
13040b57cec5SDimitry Andric }
13050b57cec5SDimitry Andric 
13060b57cec5SDimitry Andric void CStringChecker::evalMempcpy(CheckerContext &C, const CallExpr *CE) const {
13070b57cec5SDimitry Andric   // void *mempcpy(void *restrict dst, const void *restrict src, size_t n);
13080b57cec5SDimitry Andric   // The return value is a pointer to the byte following the last written byte.
13095ffd83dbSDimitry Andric   DestinationArgExpr Dest = {CE->getArg(0), 0};
13105ffd83dbSDimitry Andric   SourceArgExpr Src = {CE->getArg(1), 1};
13115ffd83dbSDimitry Andric   SizeArgExpr Size = {CE->getArg(2), 2};
13120b57cec5SDimitry Andric 
13135ffd83dbSDimitry Andric   constexpr bool IsRestricted = true;
13145ffd83dbSDimitry Andric   constexpr bool IsMempcpy = true;
1315*972a253aSDimitry Andric   evalCopyCommon(C, CE, C.getState(), Size, Dest, Src, IsRestricted, IsMempcpy,
1316*972a253aSDimitry Andric                  false);
13170b57cec5SDimitry Andric }
13180b57cec5SDimitry Andric 
13190b57cec5SDimitry Andric void CStringChecker::evalMemmove(CheckerContext &C, const CallExpr *CE) const {
13200b57cec5SDimitry Andric   // void *memmove(void *dst, const void *src, size_t n);
13210b57cec5SDimitry Andric   // The return value is the address of the destination buffer.
13225ffd83dbSDimitry Andric   DestinationArgExpr Dest = {CE->getArg(0), 0};
13235ffd83dbSDimitry Andric   SourceArgExpr Src = {CE->getArg(1), 1};
13245ffd83dbSDimitry Andric   SizeArgExpr Size = {CE->getArg(2), 2};
13250b57cec5SDimitry Andric 
13265ffd83dbSDimitry Andric   constexpr bool IsRestricted = false;
13275ffd83dbSDimitry Andric   constexpr bool IsMempcpy = false;
1328*972a253aSDimitry Andric   evalCopyCommon(C, CE, C.getState(), Size, Dest, Src, IsRestricted, IsMempcpy,
1329*972a253aSDimitry Andric                  false);
13300b57cec5SDimitry Andric }
13310b57cec5SDimitry Andric 
13320b57cec5SDimitry Andric void CStringChecker::evalBcopy(CheckerContext &C, const CallExpr *CE) const {
13330b57cec5SDimitry Andric   // void bcopy(const void *src, void *dst, size_t n);
13345ffd83dbSDimitry Andric   SourceArgExpr Src(CE->getArg(0), 0);
13355ffd83dbSDimitry Andric   DestinationArgExpr Dest = {CE->getArg(1), 1};
13365ffd83dbSDimitry Andric   SizeArgExpr Size = {CE->getArg(2), 2};
13375ffd83dbSDimitry Andric 
13385ffd83dbSDimitry Andric   constexpr bool IsRestricted = false;
13395ffd83dbSDimitry Andric   constexpr bool IsMempcpy = false;
1340*972a253aSDimitry Andric   evalCopyCommon(C, CE, C.getState(), Size, Dest, Src, IsRestricted, IsMempcpy,
1341*972a253aSDimitry Andric                  false);
13420b57cec5SDimitry Andric }
13430b57cec5SDimitry Andric 
13440b57cec5SDimitry Andric void CStringChecker::evalMemcmp(CheckerContext &C, const CallExpr *CE) const {
13450b57cec5SDimitry Andric   // int memcmp(const void *s1, const void *s2, size_t n);
13460b57cec5SDimitry Andric   CurrentFunctionDescription = "memory comparison function";
13470b57cec5SDimitry Andric 
13485ffd83dbSDimitry Andric   AnyArgExpr Left = {CE->getArg(0), 0};
13495ffd83dbSDimitry Andric   AnyArgExpr Right = {CE->getArg(1), 1};
13505ffd83dbSDimitry Andric   SizeArgExpr Size = {CE->getArg(2), 2};
13510b57cec5SDimitry Andric 
13525ffd83dbSDimitry Andric   ProgramStateRef State = C.getState();
13535ffd83dbSDimitry Andric   SValBuilder &Builder = C.getSValBuilder();
13545ffd83dbSDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
13550b57cec5SDimitry Andric 
13560b57cec5SDimitry Andric   // See if the size argument is zero.
13575ffd83dbSDimitry Andric   SVal sizeVal = State->getSVal(Size.Expression, LCtx);
13585ffd83dbSDimitry Andric   QualType sizeTy = Size.Expression->getType();
13590b57cec5SDimitry Andric 
13600b57cec5SDimitry Andric   ProgramStateRef stateZeroSize, stateNonZeroSize;
13610b57cec5SDimitry Andric   std::tie(stateZeroSize, stateNonZeroSize) =
13625ffd83dbSDimitry Andric       assumeZero(C, State, sizeVal, sizeTy);
13630b57cec5SDimitry Andric 
13640b57cec5SDimitry Andric   // If the size can be zero, the result will be 0 in that case, and we don't
13650b57cec5SDimitry Andric   // have to check either of the buffers.
13660b57cec5SDimitry Andric   if (stateZeroSize) {
13675ffd83dbSDimitry Andric     State = stateZeroSize;
13685ffd83dbSDimitry Andric     State = State->BindExpr(CE, LCtx, Builder.makeZeroVal(CE->getType()));
13695ffd83dbSDimitry Andric     C.addTransition(State);
13700b57cec5SDimitry Andric   }
13710b57cec5SDimitry Andric 
13720b57cec5SDimitry Andric   // If the size can be nonzero, we have to check the other arguments.
13730b57cec5SDimitry Andric   if (stateNonZeroSize) {
13745ffd83dbSDimitry Andric     State = stateNonZeroSize;
13750b57cec5SDimitry Andric     // If we know the two buffers are the same, we know the result is 0.
13760b57cec5SDimitry Andric     // First, get the two buffers' addresses. Another checker will have already
13770b57cec5SDimitry Andric     // made sure they're not undefined.
13780b57cec5SDimitry Andric     DefinedOrUnknownSVal LV =
13795ffd83dbSDimitry Andric         State->getSVal(Left.Expression, LCtx).castAs<DefinedOrUnknownSVal>();
13800b57cec5SDimitry Andric     DefinedOrUnknownSVal RV =
13815ffd83dbSDimitry Andric         State->getSVal(Right.Expression, LCtx).castAs<DefinedOrUnknownSVal>();
13820b57cec5SDimitry Andric 
13830b57cec5SDimitry Andric     // See if they are the same.
13845ffd83dbSDimitry Andric     ProgramStateRef SameBuffer, NotSameBuffer;
13855ffd83dbSDimitry Andric     std::tie(SameBuffer, NotSameBuffer) =
13865ffd83dbSDimitry Andric         State->assume(Builder.evalEQ(State, LV, RV));
13870b57cec5SDimitry Andric 
1388480093f4SDimitry Andric     // If the two arguments are the same buffer, we know the result is 0,
13890b57cec5SDimitry Andric     // and we only need to check one size.
13905ffd83dbSDimitry Andric     if (SameBuffer && !NotSameBuffer) {
13915ffd83dbSDimitry Andric       State = SameBuffer;
13925ffd83dbSDimitry Andric       State = CheckBufferAccess(C, State, Left, Size, AccessKind::read);
13935ffd83dbSDimitry Andric       if (State) {
13945ffd83dbSDimitry Andric         State =
13955ffd83dbSDimitry Andric             SameBuffer->BindExpr(CE, LCtx, Builder.makeZeroVal(CE->getType()));
13965ffd83dbSDimitry Andric         C.addTransition(State);
13970b57cec5SDimitry Andric       }
1398480093f4SDimitry Andric       return;
13990b57cec5SDimitry Andric     }
14000b57cec5SDimitry Andric 
1401480093f4SDimitry Andric     // If the two arguments might be different buffers, we have to check
1402480093f4SDimitry Andric     // the size of both of them.
14035ffd83dbSDimitry Andric     assert(NotSameBuffer);
14045ffd83dbSDimitry Andric     State = CheckBufferAccess(C, State, Right, Size, AccessKind::read);
14055ffd83dbSDimitry Andric     State = CheckBufferAccess(C, State, Left, Size, AccessKind::read);
14065ffd83dbSDimitry Andric     if (State) {
14070b57cec5SDimitry Andric       // The return value is the comparison result, which we don't know.
14085ffd83dbSDimitry Andric       SVal CmpV = Builder.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
14095ffd83dbSDimitry Andric       State = State->BindExpr(CE, LCtx, CmpV);
14105ffd83dbSDimitry Andric       C.addTransition(State);
14110b57cec5SDimitry Andric     }
14120b57cec5SDimitry Andric   }
14130b57cec5SDimitry Andric }
14140b57cec5SDimitry Andric 
14150b57cec5SDimitry Andric void CStringChecker::evalstrLength(CheckerContext &C,
14160b57cec5SDimitry Andric                                    const CallExpr *CE) const {
14170b57cec5SDimitry Andric   // size_t strlen(const char *s);
14180b57cec5SDimitry Andric   evalstrLengthCommon(C, CE, /* IsStrnlen = */ false);
14190b57cec5SDimitry Andric }
14200b57cec5SDimitry Andric 
14210b57cec5SDimitry Andric void CStringChecker::evalstrnLength(CheckerContext &C,
14220b57cec5SDimitry Andric                                     const CallExpr *CE) const {
14230b57cec5SDimitry Andric   // size_t strnlen(const char *s, size_t maxlen);
14240b57cec5SDimitry Andric   evalstrLengthCommon(C, CE, /* IsStrnlen = */ true);
14250b57cec5SDimitry Andric }
14260b57cec5SDimitry Andric 
14270b57cec5SDimitry Andric void CStringChecker::evalstrLengthCommon(CheckerContext &C, const CallExpr *CE,
14280b57cec5SDimitry Andric                                          bool IsStrnlen) const {
14290b57cec5SDimitry Andric   CurrentFunctionDescription = "string length function";
14300b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
14310b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
14320b57cec5SDimitry Andric 
14330b57cec5SDimitry Andric   if (IsStrnlen) {
14340b57cec5SDimitry Andric     const Expr *maxlenExpr = CE->getArg(1);
14350b57cec5SDimitry Andric     SVal maxlenVal = state->getSVal(maxlenExpr, LCtx);
14360b57cec5SDimitry Andric 
14370b57cec5SDimitry Andric     ProgramStateRef stateZeroSize, stateNonZeroSize;
14380b57cec5SDimitry Andric     std::tie(stateZeroSize, stateNonZeroSize) =
14390b57cec5SDimitry Andric       assumeZero(C, state, maxlenVal, maxlenExpr->getType());
14400b57cec5SDimitry Andric 
14410b57cec5SDimitry Andric     // If the size can be zero, the result will be 0 in that case, and we don't
14420b57cec5SDimitry Andric     // have to check the string itself.
14430b57cec5SDimitry Andric     if (stateZeroSize) {
14440b57cec5SDimitry Andric       SVal zero = C.getSValBuilder().makeZeroVal(CE->getType());
14450b57cec5SDimitry Andric       stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, zero);
14460b57cec5SDimitry Andric       C.addTransition(stateZeroSize);
14470b57cec5SDimitry Andric     }
14480b57cec5SDimitry Andric 
14490b57cec5SDimitry Andric     // If the size is GUARANTEED to be zero, we're done!
14500b57cec5SDimitry Andric     if (!stateNonZeroSize)
14510b57cec5SDimitry Andric       return;
14520b57cec5SDimitry Andric 
14530b57cec5SDimitry Andric     // Otherwise, record the assumption that the size is nonzero.
14540b57cec5SDimitry Andric     state = stateNonZeroSize;
14550b57cec5SDimitry Andric   }
14560b57cec5SDimitry Andric 
14570b57cec5SDimitry Andric   // Check that the string argument is non-null.
14585ffd83dbSDimitry Andric   AnyArgExpr Arg = {CE->getArg(0), 0};
14595ffd83dbSDimitry Andric   SVal ArgVal = state->getSVal(Arg.Expression, LCtx);
14605ffd83dbSDimitry Andric   state = checkNonNull(C, state, Arg, ArgVal);
14610b57cec5SDimitry Andric 
14620b57cec5SDimitry Andric   if (!state)
14630b57cec5SDimitry Andric     return;
14640b57cec5SDimitry Andric 
14655ffd83dbSDimitry Andric   SVal strLength = getCStringLength(C, state, Arg.Expression, ArgVal);
14660b57cec5SDimitry Andric 
14670b57cec5SDimitry Andric   // If the argument isn't a valid C string, there's no valid state to
14680b57cec5SDimitry Andric   // transition to.
14690b57cec5SDimitry Andric   if (strLength.isUndef())
14700b57cec5SDimitry Andric     return;
14710b57cec5SDimitry Andric 
14720b57cec5SDimitry Andric   DefinedOrUnknownSVal result = UnknownVal();
14730b57cec5SDimitry Andric 
14740b57cec5SDimitry Andric   // If the check is for strnlen() then bind the return value to no more than
14750b57cec5SDimitry Andric   // the maxlen value.
14760b57cec5SDimitry Andric   if (IsStrnlen) {
14770b57cec5SDimitry Andric     QualType cmpTy = C.getSValBuilder().getConditionType();
14780b57cec5SDimitry Andric 
14790b57cec5SDimitry Andric     // It's a little unfortunate to be getting this again,
14800b57cec5SDimitry Andric     // but it's not that expensive...
14810b57cec5SDimitry Andric     const Expr *maxlenExpr = CE->getArg(1);
14820b57cec5SDimitry Andric     SVal maxlenVal = state->getSVal(maxlenExpr, LCtx);
14830b57cec5SDimitry Andric 
14840b57cec5SDimitry Andric     Optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>();
14850b57cec5SDimitry Andric     Optional<NonLoc> maxlenValNL = maxlenVal.getAs<NonLoc>();
14860b57cec5SDimitry Andric 
14870b57cec5SDimitry Andric     if (strLengthNL && maxlenValNL) {
14880b57cec5SDimitry Andric       ProgramStateRef stateStringTooLong, stateStringNotTooLong;
14890b57cec5SDimitry Andric 
14900b57cec5SDimitry Andric       // Check if the strLength is greater than the maxlen.
14910b57cec5SDimitry Andric       std::tie(stateStringTooLong, stateStringNotTooLong) = state->assume(
14920b57cec5SDimitry Andric           C.getSValBuilder()
14930b57cec5SDimitry Andric               .evalBinOpNN(state, BO_GT, *strLengthNL, *maxlenValNL, cmpTy)
14940b57cec5SDimitry Andric               .castAs<DefinedOrUnknownSVal>());
14950b57cec5SDimitry Andric 
14960b57cec5SDimitry Andric       if (stateStringTooLong && !stateStringNotTooLong) {
14970b57cec5SDimitry Andric         // If the string is longer than maxlen, return maxlen.
14980b57cec5SDimitry Andric         result = *maxlenValNL;
14990b57cec5SDimitry Andric       } else if (stateStringNotTooLong && !stateStringTooLong) {
15000b57cec5SDimitry Andric         // If the string is shorter than maxlen, return its length.
15010b57cec5SDimitry Andric         result = *strLengthNL;
15020b57cec5SDimitry Andric       }
15030b57cec5SDimitry Andric     }
15040b57cec5SDimitry Andric 
15050b57cec5SDimitry Andric     if (result.isUnknown()) {
15060b57cec5SDimitry Andric       // If we don't have enough information for a comparison, there's
15070b57cec5SDimitry Andric       // no guarantee the full string length will actually be returned.
15080b57cec5SDimitry Andric       // All we know is the return value is the min of the string length
15090b57cec5SDimitry Andric       // and the limit. This is better than nothing.
15100b57cec5SDimitry Andric       result = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx,
15110b57cec5SDimitry Andric                                                    C.blockCount());
15120b57cec5SDimitry Andric       NonLoc resultNL = result.castAs<NonLoc>();
15130b57cec5SDimitry Andric 
15140b57cec5SDimitry Andric       if (strLengthNL) {
15150b57cec5SDimitry Andric         state = state->assume(C.getSValBuilder().evalBinOpNN(
15160b57cec5SDimitry Andric                                   state, BO_LE, resultNL, *strLengthNL, cmpTy)
15170b57cec5SDimitry Andric                                   .castAs<DefinedOrUnknownSVal>(), true);
15180b57cec5SDimitry Andric       }
15190b57cec5SDimitry Andric 
15200b57cec5SDimitry Andric       if (maxlenValNL) {
15210b57cec5SDimitry Andric         state = state->assume(C.getSValBuilder().evalBinOpNN(
15220b57cec5SDimitry Andric                                   state, BO_LE, resultNL, *maxlenValNL, cmpTy)
15230b57cec5SDimitry Andric                                   .castAs<DefinedOrUnknownSVal>(), true);
15240b57cec5SDimitry Andric       }
15250b57cec5SDimitry Andric     }
15260b57cec5SDimitry Andric 
15270b57cec5SDimitry Andric   } else {
15280b57cec5SDimitry Andric     // This is a plain strlen(), not strnlen().
15290b57cec5SDimitry Andric     result = strLength.castAs<DefinedOrUnknownSVal>();
15300b57cec5SDimitry Andric 
15310b57cec5SDimitry Andric     // If we don't know the length of the string, conjure a return
15320b57cec5SDimitry Andric     // value, so it can be used in constraints, at least.
15330b57cec5SDimitry Andric     if (result.isUnknown()) {
15340b57cec5SDimitry Andric       result = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx,
15350b57cec5SDimitry Andric                                                    C.blockCount());
15360b57cec5SDimitry Andric     }
15370b57cec5SDimitry Andric   }
15380b57cec5SDimitry Andric 
15390b57cec5SDimitry Andric   // Bind the return value.
15400b57cec5SDimitry Andric   assert(!result.isUnknown() && "Should have conjured a value by now");
15410b57cec5SDimitry Andric   state = state->BindExpr(CE, LCtx, result);
15420b57cec5SDimitry Andric   C.addTransition(state);
15430b57cec5SDimitry Andric }
15440b57cec5SDimitry Andric 
15450b57cec5SDimitry Andric void CStringChecker::evalStrcpy(CheckerContext &C, const CallExpr *CE) const {
15460b57cec5SDimitry Andric   // char *strcpy(char *restrict dst, const char *restrict src);
15470b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
1548480093f4SDimitry Andric                    /* ReturnEnd = */ false,
1549480093f4SDimitry Andric                    /* IsBounded = */ false,
1550480093f4SDimitry Andric                    /* appendK = */ ConcatFnKind::none);
15510b57cec5SDimitry Andric }
15520b57cec5SDimitry Andric 
15530b57cec5SDimitry Andric void CStringChecker::evalStrncpy(CheckerContext &C, const CallExpr *CE) const {
15540b57cec5SDimitry Andric   // char *strncpy(char *restrict dst, const char *restrict src, size_t n);
15550b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
1556480093f4SDimitry Andric                    /* ReturnEnd = */ false,
1557480093f4SDimitry Andric                    /* IsBounded = */ true,
1558480093f4SDimitry Andric                    /* appendK = */ ConcatFnKind::none);
15590b57cec5SDimitry Andric }
15600b57cec5SDimitry Andric 
15610b57cec5SDimitry Andric void CStringChecker::evalStpcpy(CheckerContext &C, const CallExpr *CE) const {
15620b57cec5SDimitry Andric   // char *stpcpy(char *restrict dst, const char *restrict src);
15630b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
1564480093f4SDimitry Andric                    /* ReturnEnd = */ true,
1565480093f4SDimitry Andric                    /* IsBounded = */ false,
1566480093f4SDimitry Andric                    /* appendK = */ ConcatFnKind::none);
15670b57cec5SDimitry Andric }
15680b57cec5SDimitry Andric 
15690b57cec5SDimitry Andric void CStringChecker::evalStrlcpy(CheckerContext &C, const CallExpr *CE) const {
1570480093f4SDimitry Andric   // size_t strlcpy(char *dest, const char *src, size_t size);
15710b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
1572480093f4SDimitry Andric                    /* ReturnEnd = */ true,
1573480093f4SDimitry Andric                    /* IsBounded = */ true,
1574480093f4SDimitry Andric                    /* appendK = */ ConcatFnKind::none,
15750b57cec5SDimitry Andric                    /* returnPtr = */ false);
15760b57cec5SDimitry Andric }
15770b57cec5SDimitry Andric 
15780b57cec5SDimitry Andric void CStringChecker::evalStrcat(CheckerContext &C, const CallExpr *CE) const {
15790b57cec5SDimitry Andric   // char *strcat(char *restrict s1, const char *restrict s2);
15800b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
1581480093f4SDimitry Andric                    /* ReturnEnd = */ false,
1582480093f4SDimitry Andric                    /* IsBounded = */ false,
1583480093f4SDimitry Andric                    /* appendK = */ ConcatFnKind::strcat);
15840b57cec5SDimitry Andric }
15850b57cec5SDimitry Andric 
15860b57cec5SDimitry Andric void CStringChecker::evalStrncat(CheckerContext &C, const CallExpr *CE) const {
15870b57cec5SDimitry Andric   // char *strncat(char *restrict s1, const char *restrict s2, size_t n);
15880b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
1589480093f4SDimitry Andric                    /* ReturnEnd = */ false,
1590480093f4SDimitry Andric                    /* IsBounded = */ true,
1591480093f4SDimitry Andric                    /* appendK = */ ConcatFnKind::strcat);
15920b57cec5SDimitry Andric }
15930b57cec5SDimitry Andric 
15940b57cec5SDimitry Andric void CStringChecker::evalStrlcat(CheckerContext &C, const CallExpr *CE) const {
1595480093f4SDimitry Andric   // size_t strlcat(char *dst, const char *src, size_t size);
1596480093f4SDimitry Andric   // It will append at most size - strlen(dst) - 1 bytes,
1597480093f4SDimitry Andric   // NULL-terminating the result.
15980b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
1599480093f4SDimitry Andric                    /* ReturnEnd = */ false,
1600480093f4SDimitry Andric                    /* IsBounded = */ true,
1601480093f4SDimitry Andric                    /* appendK = */ ConcatFnKind::strlcat,
16020b57cec5SDimitry Andric                    /* returnPtr = */ false);
16030b57cec5SDimitry Andric }
16040b57cec5SDimitry Andric 
16050b57cec5SDimitry Andric void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE,
1606480093f4SDimitry Andric                                       bool ReturnEnd, bool IsBounded,
1607480093f4SDimitry Andric                                       ConcatFnKind appendK,
1608480093f4SDimitry Andric                                       bool returnPtr) const {
1609480093f4SDimitry Andric   if (appendK == ConcatFnKind::none)
16100b57cec5SDimitry Andric     CurrentFunctionDescription = "string copy function";
1611480093f4SDimitry Andric   else
1612480093f4SDimitry Andric     CurrentFunctionDescription = "string concatenation function";
16135ffd83dbSDimitry Andric 
16140b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
16150b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
16160b57cec5SDimitry Andric 
16170b57cec5SDimitry Andric   // Check that the destination is non-null.
16185ffd83dbSDimitry Andric   DestinationArgExpr Dst = {CE->getArg(0), 0};
16195ffd83dbSDimitry Andric   SVal DstVal = state->getSVal(Dst.Expression, LCtx);
16205ffd83dbSDimitry Andric   state = checkNonNull(C, state, Dst, DstVal);
16210b57cec5SDimitry Andric   if (!state)
16220b57cec5SDimitry Andric     return;
16230b57cec5SDimitry Andric 
16240b57cec5SDimitry Andric   // Check that the source is non-null.
16255ffd83dbSDimitry Andric   SourceArgExpr srcExpr = {CE->getArg(1), 1};
16265ffd83dbSDimitry Andric   SVal srcVal = state->getSVal(srcExpr.Expression, LCtx);
16275ffd83dbSDimitry Andric   state = checkNonNull(C, state, srcExpr, srcVal);
16280b57cec5SDimitry Andric   if (!state)
16290b57cec5SDimitry Andric     return;
16300b57cec5SDimitry Andric 
16310b57cec5SDimitry Andric   // Get the string length of the source.
16325ffd83dbSDimitry Andric   SVal strLength = getCStringLength(C, state, srcExpr.Expression, srcVal);
1633480093f4SDimitry Andric   Optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>();
1634480093f4SDimitry Andric 
1635480093f4SDimitry Andric   // Get the string length of the destination buffer.
16365ffd83dbSDimitry Andric   SVal dstStrLength = getCStringLength(C, state, Dst.Expression, DstVal);
1637480093f4SDimitry Andric   Optional<NonLoc> dstStrLengthNL = dstStrLength.getAs<NonLoc>();
16380b57cec5SDimitry Andric 
16390b57cec5SDimitry Andric   // If the source isn't a valid C string, give up.
16400b57cec5SDimitry Andric   if (strLength.isUndef())
16410b57cec5SDimitry Andric     return;
16420b57cec5SDimitry Andric 
16430b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
16440b57cec5SDimitry Andric   QualType cmpTy = svalBuilder.getConditionType();
16450b57cec5SDimitry Andric   QualType sizeTy = svalBuilder.getContext().getSizeType();
16460b57cec5SDimitry Andric 
16470b57cec5SDimitry Andric   // These two values allow checking two kinds of errors:
16480b57cec5SDimitry Andric   // - actual overflows caused by a source that doesn't fit in the destination
16490b57cec5SDimitry Andric   // - potential overflows caused by a bound that could exceed the destination
16500b57cec5SDimitry Andric   SVal amountCopied = UnknownVal();
16510b57cec5SDimitry Andric   SVal maxLastElementIndex = UnknownVal();
16520b57cec5SDimitry Andric   const char *boundWarning = nullptr;
16530b57cec5SDimitry Andric 
16545ffd83dbSDimitry Andric   // FIXME: Why do we choose the srcExpr if the access has no size?
16555ffd83dbSDimitry Andric   //  Note that the 3rd argument of the call would be the size parameter.
16565ffd83dbSDimitry Andric   SizeArgExpr SrcExprAsSizeDummy = {srcExpr.Expression, srcExpr.ArgumentIndex};
16575ffd83dbSDimitry Andric   state = CheckOverlap(
16585ffd83dbSDimitry Andric       C, state,
16595ffd83dbSDimitry Andric       (IsBounded ? SizeArgExpr{CE->getArg(2), 2} : SrcExprAsSizeDummy), Dst,
1660480093f4SDimitry Andric       srcExpr);
16610b57cec5SDimitry Andric 
16620b57cec5SDimitry Andric   if (!state)
16630b57cec5SDimitry Andric     return;
16640b57cec5SDimitry Andric 
16650b57cec5SDimitry Andric   // If the function is strncpy, strncat, etc... it is bounded.
1666480093f4SDimitry Andric   if (IsBounded) {
16670b57cec5SDimitry Andric     // Get the max number of characters to copy.
16685ffd83dbSDimitry Andric     SizeArgExpr lenExpr = {CE->getArg(2), 2};
16695ffd83dbSDimitry Andric     SVal lenVal = state->getSVal(lenExpr.Expression, LCtx);
16700b57cec5SDimitry Andric 
16710b57cec5SDimitry Andric     // Protect against misdeclared strncpy().
16725ffd83dbSDimitry Andric     lenVal =
16735ffd83dbSDimitry Andric         svalBuilder.evalCast(lenVal, sizeTy, lenExpr.Expression->getType());
16740b57cec5SDimitry Andric 
16750b57cec5SDimitry Andric     Optional<NonLoc> lenValNL = lenVal.getAs<NonLoc>();
16760b57cec5SDimitry Andric 
16770b57cec5SDimitry Andric     // If we know both values, we might be able to figure out how much
16780b57cec5SDimitry Andric     // we're copying.
16790b57cec5SDimitry Andric     if (strLengthNL && lenValNL) {
1680480093f4SDimitry Andric       switch (appendK) {
1681480093f4SDimitry Andric       case ConcatFnKind::none:
1682480093f4SDimitry Andric       case ConcatFnKind::strcat: {
16830b57cec5SDimitry Andric         ProgramStateRef stateSourceTooLong, stateSourceNotTooLong;
16840b57cec5SDimitry Andric         // Check if the max number to copy is less than the length of the src.
16850b57cec5SDimitry Andric         // If the bound is equal to the source length, strncpy won't null-
16860b57cec5SDimitry Andric         // terminate the result!
16870b57cec5SDimitry Andric         std::tie(stateSourceTooLong, stateSourceNotTooLong) = state->assume(
1688480093f4SDimitry Andric             svalBuilder
1689480093f4SDimitry Andric                 .evalBinOpNN(state, BO_GE, *strLengthNL, *lenValNL, cmpTy)
16900b57cec5SDimitry Andric                 .castAs<DefinedOrUnknownSVal>());
16910b57cec5SDimitry Andric 
16920b57cec5SDimitry Andric         if (stateSourceTooLong && !stateSourceNotTooLong) {
1693480093f4SDimitry Andric           // Max number to copy is less than the length of the src, so the
1694480093f4SDimitry Andric           // actual strLength copied is the max number arg.
16950b57cec5SDimitry Andric           state = stateSourceTooLong;
16960b57cec5SDimitry Andric           amountCopied = lenVal;
16970b57cec5SDimitry Andric 
16980b57cec5SDimitry Andric         } else if (!stateSourceTooLong && stateSourceNotTooLong) {
16990b57cec5SDimitry Andric           // The source buffer entirely fits in the bound.
17000b57cec5SDimitry Andric           state = stateSourceNotTooLong;
17010b57cec5SDimitry Andric           amountCopied = strLength;
17020b57cec5SDimitry Andric         }
1703480093f4SDimitry Andric         break;
1704480093f4SDimitry Andric       }
1705480093f4SDimitry Andric       case ConcatFnKind::strlcat:
1706480093f4SDimitry Andric         if (!dstStrLengthNL)
1707480093f4SDimitry Andric           return;
1708480093f4SDimitry Andric 
1709480093f4SDimitry Andric         // amountCopied = min (size - dstLen - 1 , srcLen)
1710480093f4SDimitry Andric         SVal freeSpace = svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL,
1711480093f4SDimitry Andric                                                  *dstStrLengthNL, sizeTy);
171281ad6265SDimitry Andric         if (!isa<NonLoc>(freeSpace))
1713480093f4SDimitry Andric           return;
1714480093f4SDimitry Andric         freeSpace =
1715480093f4SDimitry Andric             svalBuilder.evalBinOp(state, BO_Sub, freeSpace,
1716480093f4SDimitry Andric                                   svalBuilder.makeIntVal(1, sizeTy), sizeTy);
1717480093f4SDimitry Andric         Optional<NonLoc> freeSpaceNL = freeSpace.getAs<NonLoc>();
1718480093f4SDimitry Andric 
1719480093f4SDimitry Andric         // While unlikely, it is possible that the subtraction is
1720480093f4SDimitry Andric         // too complex to compute, let's check whether it succeeded.
1721480093f4SDimitry Andric         if (!freeSpaceNL)
1722480093f4SDimitry Andric           return;
1723480093f4SDimitry Andric         SVal hasEnoughSpace = svalBuilder.evalBinOpNN(
1724480093f4SDimitry Andric             state, BO_LE, *strLengthNL, *freeSpaceNL, cmpTy);
1725480093f4SDimitry Andric 
1726480093f4SDimitry Andric         ProgramStateRef TrueState, FalseState;
1727480093f4SDimitry Andric         std::tie(TrueState, FalseState) =
1728480093f4SDimitry Andric             state->assume(hasEnoughSpace.castAs<DefinedOrUnknownSVal>());
1729480093f4SDimitry Andric 
1730480093f4SDimitry Andric         // srcStrLength <= size - dstStrLength -1
1731480093f4SDimitry Andric         if (TrueState && !FalseState) {
1732480093f4SDimitry Andric           amountCopied = strLength;
17330b57cec5SDimitry Andric         }
17340b57cec5SDimitry Andric 
1735480093f4SDimitry Andric         // srcStrLength > size - dstStrLength -1
1736480093f4SDimitry Andric         if (!TrueState && FalseState) {
1737480093f4SDimitry Andric           amountCopied = freeSpace;
1738480093f4SDimitry Andric         }
1739480093f4SDimitry Andric 
1740480093f4SDimitry Andric         if (TrueState && FalseState)
1741480093f4SDimitry Andric           amountCopied = UnknownVal();
1742480093f4SDimitry Andric         break;
1743480093f4SDimitry Andric       }
1744480093f4SDimitry Andric     }
17450b57cec5SDimitry Andric     // We still want to know if the bound is known to be too large.
17460b57cec5SDimitry Andric     if (lenValNL) {
1747480093f4SDimitry Andric       switch (appendK) {
1748480093f4SDimitry Andric       case ConcatFnKind::strcat:
17490b57cec5SDimitry Andric         // For strncat, the check is strlen(dst) + lenVal < sizeof(dst)
17500b57cec5SDimitry Andric 
17510b57cec5SDimitry Andric         // Get the string length of the destination. If the destination is
17520b57cec5SDimitry Andric         // memory that can't have a string length, we shouldn't be copying
17530b57cec5SDimitry Andric         // into it anyway.
17540b57cec5SDimitry Andric         if (dstStrLength.isUndef())
17550b57cec5SDimitry Andric           return;
17560b57cec5SDimitry Andric 
1757480093f4SDimitry Andric         if (dstStrLengthNL) {
1758480093f4SDimitry Andric           maxLastElementIndex = svalBuilder.evalBinOpNN(
1759480093f4SDimitry Andric               state, BO_Add, *lenValNL, *dstStrLengthNL, sizeTy);
1760480093f4SDimitry Andric 
17610b57cec5SDimitry Andric           boundWarning = "Size argument is greater than the free space in the "
17620b57cec5SDimitry Andric                          "destination buffer";
17630b57cec5SDimitry Andric         }
1764480093f4SDimitry Andric         break;
1765480093f4SDimitry Andric       case ConcatFnKind::none:
1766480093f4SDimitry Andric       case ConcatFnKind::strlcat:
1767480093f4SDimitry Andric         // For strncpy and strlcat, this is just checking
1768480093f4SDimitry Andric         //  that lenVal <= sizeof(dst).
17690b57cec5SDimitry Andric         // (Yes, strncpy and strncat differ in how they treat termination.
17700b57cec5SDimitry Andric         // strncat ALWAYS terminates, but strncpy doesn't.)
17710b57cec5SDimitry Andric 
17720b57cec5SDimitry Andric         // We need a special case for when the copy size is zero, in which
17730b57cec5SDimitry Andric         // case strncpy will do no work at all. Our bounds check uses n-1
17740b57cec5SDimitry Andric         // as the last element accessed, so n == 0 is problematic.
17750b57cec5SDimitry Andric         ProgramStateRef StateZeroSize, StateNonZeroSize;
17760b57cec5SDimitry Andric         std::tie(StateZeroSize, StateNonZeroSize) =
17770b57cec5SDimitry Andric             assumeZero(C, state, *lenValNL, sizeTy);
17780b57cec5SDimitry Andric 
17790b57cec5SDimitry Andric         // If the size is known to be zero, we're done.
17800b57cec5SDimitry Andric         if (StateZeroSize && !StateNonZeroSize) {
17810b57cec5SDimitry Andric           if (returnPtr) {
17820b57cec5SDimitry Andric             StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, DstVal);
17830b57cec5SDimitry Andric           } else {
1784480093f4SDimitry Andric             if (appendK == ConcatFnKind::none) {
1785480093f4SDimitry Andric               // strlcpy returns strlen(src)
1786480093f4SDimitry Andric               StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, strLength);
1787480093f4SDimitry Andric             } else {
1788480093f4SDimitry Andric               // strlcat returns strlen(src) + strlen(dst)
1789480093f4SDimitry Andric               SVal retSize = svalBuilder.evalBinOp(
1790480093f4SDimitry Andric                   state, BO_Add, strLength, dstStrLength, sizeTy);
1791480093f4SDimitry Andric               StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, retSize);
1792480093f4SDimitry Andric             }
17930b57cec5SDimitry Andric           }
17940b57cec5SDimitry Andric           C.addTransition(StateZeroSize);
17950b57cec5SDimitry Andric           return;
17960b57cec5SDimitry Andric         }
17970b57cec5SDimitry Andric 
17980b57cec5SDimitry Andric         // Otherwise, go ahead and figure out the last element we'll touch.
17990b57cec5SDimitry Andric         // We don't record the non-zero assumption here because we can't
18000b57cec5SDimitry Andric         // be sure. We won't warn on a possible zero.
18010b57cec5SDimitry Andric         NonLoc one = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>();
1802480093f4SDimitry Andric         maxLastElementIndex =
1803480093f4SDimitry Andric             svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL, one, sizeTy);
18040b57cec5SDimitry Andric         boundWarning = "Size argument is greater than the length of the "
18050b57cec5SDimitry Andric                        "destination buffer";
1806480093f4SDimitry Andric         break;
18070b57cec5SDimitry Andric       }
18080b57cec5SDimitry Andric     }
18090b57cec5SDimitry Andric   } else {
18100b57cec5SDimitry Andric     // The function isn't bounded. The amount copied should match the length
18110b57cec5SDimitry Andric     // of the source buffer.
18120b57cec5SDimitry Andric     amountCopied = strLength;
18130b57cec5SDimitry Andric   }
18140b57cec5SDimitry Andric 
18150b57cec5SDimitry Andric   assert(state);
18160b57cec5SDimitry Andric 
18170b57cec5SDimitry Andric   // This represents the number of characters copied into the destination
18180b57cec5SDimitry Andric   // buffer. (It may not actually be the strlen if the destination buffer
18190b57cec5SDimitry Andric   // is not terminated.)
18200b57cec5SDimitry Andric   SVal finalStrLength = UnknownVal();
1821480093f4SDimitry Andric   SVal strlRetVal = UnknownVal();
1822480093f4SDimitry Andric 
1823480093f4SDimitry Andric   if (appendK == ConcatFnKind::none && !returnPtr) {
1824480093f4SDimitry Andric     // strlcpy returns the sizeof(src)
1825480093f4SDimitry Andric     strlRetVal = strLength;
1826480093f4SDimitry Andric   }
18270b57cec5SDimitry Andric 
18280b57cec5SDimitry Andric   // If this is an appending function (strcat, strncat...) then set the
18290b57cec5SDimitry Andric   // string length to strlen(src) + strlen(dst) since the buffer will
18300b57cec5SDimitry Andric   // ultimately contain both.
1831480093f4SDimitry Andric   if (appendK != ConcatFnKind::none) {
18320b57cec5SDimitry Andric     // Get the string length of the destination. If the destination is memory
18330b57cec5SDimitry Andric     // that can't have a string length, we shouldn't be copying into it anyway.
18340b57cec5SDimitry Andric     if (dstStrLength.isUndef())
18350b57cec5SDimitry Andric       return;
18360b57cec5SDimitry Andric 
1837480093f4SDimitry Andric     if (appendK == ConcatFnKind::strlcat && dstStrLengthNL && strLengthNL) {
1838480093f4SDimitry Andric       strlRetVal = svalBuilder.evalBinOpNN(state, BO_Add, *strLengthNL,
1839480093f4SDimitry Andric                                            *dstStrLengthNL, sizeTy);
1840480093f4SDimitry Andric     }
1841480093f4SDimitry Andric 
1842480093f4SDimitry Andric     Optional<NonLoc> amountCopiedNL = amountCopied.getAs<NonLoc>();
18430b57cec5SDimitry Andric 
18440b57cec5SDimitry Andric     // If we know both string lengths, we might know the final string length.
1845480093f4SDimitry Andric     if (amountCopiedNL && dstStrLengthNL) {
18460b57cec5SDimitry Andric       // Make sure the two lengths together don't overflow a size_t.
1847480093f4SDimitry Andric       state = checkAdditionOverflow(C, state, *amountCopiedNL, *dstStrLengthNL);
18480b57cec5SDimitry Andric       if (!state)
18490b57cec5SDimitry Andric         return;
18500b57cec5SDimitry Andric 
1851480093f4SDimitry Andric       finalStrLength = svalBuilder.evalBinOpNN(state, BO_Add, *amountCopiedNL,
18520b57cec5SDimitry Andric                                                *dstStrLengthNL, sizeTy);
18530b57cec5SDimitry Andric     }
18540b57cec5SDimitry Andric 
18550b57cec5SDimitry Andric     // If we couldn't get a single value for the final string length,
18560b57cec5SDimitry Andric     // we can at least bound it by the individual lengths.
18570b57cec5SDimitry Andric     if (finalStrLength.isUnknown()) {
18580b57cec5SDimitry Andric       // Try to get a "hypothetical" string length symbol, which we can later
18590b57cec5SDimitry Andric       // set as a real value if that turns out to be the case.
18600b57cec5SDimitry Andric       finalStrLength = getCStringLength(C, state, CE, DstVal, true);
18610b57cec5SDimitry Andric       assert(!finalStrLength.isUndef());
18620b57cec5SDimitry Andric 
18630b57cec5SDimitry Andric       if (Optional<NonLoc> finalStrLengthNL = finalStrLength.getAs<NonLoc>()) {
1864480093f4SDimitry Andric         if (amountCopiedNL && appendK == ConcatFnKind::none) {
1865480093f4SDimitry Andric           // we overwrite dst string with the src
18660b57cec5SDimitry Andric           // finalStrLength >= srcStrLength
1867480093f4SDimitry Andric           SVal sourceInResult = svalBuilder.evalBinOpNN(
1868480093f4SDimitry Andric               state, BO_GE, *finalStrLengthNL, *amountCopiedNL, cmpTy);
18690b57cec5SDimitry Andric           state = state->assume(sourceInResult.castAs<DefinedOrUnknownSVal>(),
18700b57cec5SDimitry Andric                                 true);
18710b57cec5SDimitry Andric           if (!state)
18720b57cec5SDimitry Andric             return;
18730b57cec5SDimitry Andric         }
18740b57cec5SDimitry Andric 
1875480093f4SDimitry Andric         if (dstStrLengthNL && appendK != ConcatFnKind::none) {
1876480093f4SDimitry Andric           // we extend the dst string with the src
18770b57cec5SDimitry Andric           // finalStrLength >= dstStrLength
18780b57cec5SDimitry Andric           SVal destInResult = svalBuilder.evalBinOpNN(state, BO_GE,
18790b57cec5SDimitry Andric                                                       *finalStrLengthNL,
18800b57cec5SDimitry Andric                                                       *dstStrLengthNL,
18810b57cec5SDimitry Andric                                                       cmpTy);
18820b57cec5SDimitry Andric           state =
18830b57cec5SDimitry Andric               state->assume(destInResult.castAs<DefinedOrUnknownSVal>(), true);
18840b57cec5SDimitry Andric           if (!state)
18850b57cec5SDimitry Andric             return;
18860b57cec5SDimitry Andric         }
18870b57cec5SDimitry Andric       }
18880b57cec5SDimitry Andric     }
18890b57cec5SDimitry Andric 
18900b57cec5SDimitry Andric   } else {
18910b57cec5SDimitry Andric     // Otherwise, this is a copy-over function (strcpy, strncpy, ...), and
18920b57cec5SDimitry Andric     // the final string length will match the input string length.
18930b57cec5SDimitry Andric     finalStrLength = amountCopied;
18940b57cec5SDimitry Andric   }
18950b57cec5SDimitry Andric 
18960b57cec5SDimitry Andric   SVal Result;
18970b57cec5SDimitry Andric 
18980b57cec5SDimitry Andric   if (returnPtr) {
18990b57cec5SDimitry Andric     // The final result of the function will either be a pointer past the last
19000b57cec5SDimitry Andric     // copied element, or a pointer to the start of the destination buffer.
1901480093f4SDimitry Andric     Result = (ReturnEnd ? UnknownVal() : DstVal);
19020b57cec5SDimitry Andric   } else {
1903480093f4SDimitry Andric     if (appendK == ConcatFnKind::strlcat || appendK == ConcatFnKind::none)
1904480093f4SDimitry Andric       //strlcpy, strlcat
1905480093f4SDimitry Andric       Result = strlRetVal;
1906480093f4SDimitry Andric     else
19070b57cec5SDimitry Andric       Result = finalStrLength;
19080b57cec5SDimitry Andric   }
19090b57cec5SDimitry Andric 
19100b57cec5SDimitry Andric   assert(state);
19110b57cec5SDimitry Andric 
19120b57cec5SDimitry Andric   // If the destination is a MemRegion, try to check for a buffer overflow and
19130b57cec5SDimitry Andric   // record the new string length.
19140b57cec5SDimitry Andric   if (Optional<loc::MemRegionVal> dstRegVal =
19150b57cec5SDimitry Andric       DstVal.getAs<loc::MemRegionVal>()) {
19165ffd83dbSDimitry Andric     QualType ptrTy = Dst.Expression->getType();
19170b57cec5SDimitry Andric 
19180b57cec5SDimitry Andric     // If we have an exact value on a bounded copy, use that to check for
19190b57cec5SDimitry Andric     // overflows, rather than our estimate about how much is actually copied.
19200b57cec5SDimitry Andric     if (Optional<NonLoc> maxLastNL = maxLastElementIndex.getAs<NonLoc>()) {
19215ffd83dbSDimitry Andric       SVal maxLastElement =
19225ffd83dbSDimitry Andric           svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal, *maxLastNL, ptrTy);
19235ffd83dbSDimitry Andric 
19245ffd83dbSDimitry Andric       state = CheckLocation(C, state, Dst, maxLastElement, AccessKind::write);
19250b57cec5SDimitry Andric       if (!state)
19260b57cec5SDimitry Andric         return;
19270b57cec5SDimitry Andric     }
19280b57cec5SDimitry Andric 
19290b57cec5SDimitry Andric     // Then, if the final length is known...
19300b57cec5SDimitry Andric     if (Optional<NonLoc> knownStrLength = finalStrLength.getAs<NonLoc>()) {
19310b57cec5SDimitry Andric       SVal lastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal,
19320b57cec5SDimitry Andric           *knownStrLength, ptrTy);
19330b57cec5SDimitry Andric 
19340b57cec5SDimitry Andric       // ...and we haven't checked the bound, we'll check the actual copy.
19350b57cec5SDimitry Andric       if (!boundWarning) {
19365ffd83dbSDimitry Andric         state = CheckLocation(C, state, Dst, lastElement, AccessKind::write);
19370b57cec5SDimitry Andric         if (!state)
19380b57cec5SDimitry Andric           return;
19390b57cec5SDimitry Andric       }
19400b57cec5SDimitry Andric 
19410b57cec5SDimitry Andric       // If this is a stpcpy-style copy, the last element is the return value.
1942480093f4SDimitry Andric       if (returnPtr && ReturnEnd)
19430b57cec5SDimitry Andric         Result = lastElement;
19440b57cec5SDimitry Andric     }
19450b57cec5SDimitry Andric 
19460b57cec5SDimitry Andric     // Invalidate the destination (regular invalidation without pointer-escaping
19470b57cec5SDimitry Andric     // the address of the top-level region). This must happen before we set the
19480b57cec5SDimitry Andric     // C string length because invalidation will clear the length.
19490b57cec5SDimitry Andric     // FIXME: Even if we can't perfectly model the copy, we should see if we
19500b57cec5SDimitry Andric     // can use LazyCompoundVals to copy the source values into the destination.
19510b57cec5SDimitry Andric     // This would probably remove any existing bindings past the end of the
19520b57cec5SDimitry Andric     // string, but that's still an improvement over blank invalidation.
19535ffd83dbSDimitry Andric     state = InvalidateBuffer(C, state, Dst.Expression, *dstRegVal,
19540b57cec5SDimitry Andric                              /*IsSourceBuffer*/ false, nullptr);
19550b57cec5SDimitry Andric 
19560b57cec5SDimitry Andric     // Invalidate the source (const-invalidation without const-pointer-escaping
19570b57cec5SDimitry Andric     // the address of the top-level region).
19585ffd83dbSDimitry Andric     state = InvalidateBuffer(C, state, srcExpr.Expression, srcVal,
19595ffd83dbSDimitry Andric                              /*IsSourceBuffer*/ true, nullptr);
19600b57cec5SDimitry Andric 
19610b57cec5SDimitry Andric     // Set the C string length of the destination, if we know it.
1962480093f4SDimitry Andric     if (IsBounded && (appendK == ConcatFnKind::none)) {
19630b57cec5SDimitry Andric       // strncpy is annoying in that it doesn't guarantee to null-terminate
19640b57cec5SDimitry Andric       // the result string. If the original string didn't fit entirely inside
19650b57cec5SDimitry Andric       // the bound (including the null-terminator), we don't know how long the
19660b57cec5SDimitry Andric       // result is.
19670b57cec5SDimitry Andric       if (amountCopied != strLength)
19680b57cec5SDimitry Andric         finalStrLength = UnknownVal();
19690b57cec5SDimitry Andric     }
19700b57cec5SDimitry Andric     state = setCStringLength(state, dstRegVal->getRegion(), finalStrLength);
19710b57cec5SDimitry Andric   }
19720b57cec5SDimitry Andric 
19730b57cec5SDimitry Andric   assert(state);
19740b57cec5SDimitry Andric 
19750b57cec5SDimitry Andric   if (returnPtr) {
19760b57cec5SDimitry Andric     // If this is a stpcpy-style copy, but we were unable to check for a buffer
19770b57cec5SDimitry Andric     // overflow, we still need a result. Conjure a return value.
1978480093f4SDimitry Andric     if (ReturnEnd && Result.isUnknown()) {
19790b57cec5SDimitry Andric       Result = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
19800b57cec5SDimitry Andric     }
19810b57cec5SDimitry Andric   }
19820b57cec5SDimitry Andric   // Set the return value.
19830b57cec5SDimitry Andric   state = state->BindExpr(CE, LCtx, Result);
19840b57cec5SDimitry Andric   C.addTransition(state);
19850b57cec5SDimitry Andric }
19860b57cec5SDimitry Andric 
19870b57cec5SDimitry Andric void CStringChecker::evalStrcmp(CheckerContext &C, const CallExpr *CE) const {
19880b57cec5SDimitry Andric   //int strcmp(const char *s1, const char *s2);
1989480093f4SDimitry Andric   evalStrcmpCommon(C, CE, /* IsBounded = */ false, /* IgnoreCase = */ false);
19900b57cec5SDimitry Andric }
19910b57cec5SDimitry Andric 
19920b57cec5SDimitry Andric void CStringChecker::evalStrncmp(CheckerContext &C, const CallExpr *CE) const {
19930b57cec5SDimitry Andric   //int strncmp(const char *s1, const char *s2, size_t n);
1994480093f4SDimitry Andric   evalStrcmpCommon(C, CE, /* IsBounded = */ true, /* IgnoreCase = */ false);
19950b57cec5SDimitry Andric }
19960b57cec5SDimitry Andric 
19970b57cec5SDimitry Andric void CStringChecker::evalStrcasecmp(CheckerContext &C,
19980b57cec5SDimitry Andric     const CallExpr *CE) const {
19990b57cec5SDimitry Andric   //int strcasecmp(const char *s1, const char *s2);
2000480093f4SDimitry Andric   evalStrcmpCommon(C, CE, /* IsBounded = */ false, /* IgnoreCase = */ true);
20010b57cec5SDimitry Andric }
20020b57cec5SDimitry Andric 
20030b57cec5SDimitry Andric void CStringChecker::evalStrncasecmp(CheckerContext &C,
20040b57cec5SDimitry Andric     const CallExpr *CE) const {
20050b57cec5SDimitry Andric   //int strncasecmp(const char *s1, const char *s2, size_t n);
2006480093f4SDimitry Andric   evalStrcmpCommon(C, CE, /* IsBounded = */ true, /* IgnoreCase = */ true);
20070b57cec5SDimitry Andric }
20080b57cec5SDimitry Andric 
20090b57cec5SDimitry Andric void CStringChecker::evalStrcmpCommon(CheckerContext &C, const CallExpr *CE,
2010480093f4SDimitry Andric     bool IsBounded, bool IgnoreCase) const {
20110b57cec5SDimitry Andric   CurrentFunctionDescription = "string comparison function";
20120b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
20130b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
20140b57cec5SDimitry Andric 
20150b57cec5SDimitry Andric   // Check that the first string is non-null
20165ffd83dbSDimitry Andric   AnyArgExpr Left = {CE->getArg(0), 0};
20175ffd83dbSDimitry Andric   SVal LeftVal = state->getSVal(Left.Expression, LCtx);
20185ffd83dbSDimitry Andric   state = checkNonNull(C, state, Left, LeftVal);
20190b57cec5SDimitry Andric   if (!state)
20200b57cec5SDimitry Andric     return;
20210b57cec5SDimitry Andric 
20220b57cec5SDimitry Andric   // Check that the second string is non-null.
20235ffd83dbSDimitry Andric   AnyArgExpr Right = {CE->getArg(1), 1};
20245ffd83dbSDimitry Andric   SVal RightVal = state->getSVal(Right.Expression, LCtx);
20255ffd83dbSDimitry Andric   state = checkNonNull(C, state, Right, RightVal);
20260b57cec5SDimitry Andric   if (!state)
20270b57cec5SDimitry Andric     return;
20280b57cec5SDimitry Andric 
20290b57cec5SDimitry Andric   // Get the string length of the first string or give up.
20305ffd83dbSDimitry Andric   SVal LeftLength = getCStringLength(C, state, Left.Expression, LeftVal);
20315ffd83dbSDimitry Andric   if (LeftLength.isUndef())
20320b57cec5SDimitry Andric     return;
20330b57cec5SDimitry Andric 
20340b57cec5SDimitry Andric   // Get the string length of the second string or give up.
20355ffd83dbSDimitry Andric   SVal RightLength = getCStringLength(C, state, Right.Expression, RightVal);
20365ffd83dbSDimitry Andric   if (RightLength.isUndef())
20370b57cec5SDimitry Andric     return;
20380b57cec5SDimitry Andric 
20390b57cec5SDimitry Andric   // If we know the two buffers are the same, we know the result is 0.
20400b57cec5SDimitry Andric   // First, get the two buffers' addresses. Another checker will have already
20410b57cec5SDimitry Andric   // made sure they're not undefined.
20425ffd83dbSDimitry Andric   DefinedOrUnknownSVal LV = LeftVal.castAs<DefinedOrUnknownSVal>();
20435ffd83dbSDimitry Andric   DefinedOrUnknownSVal RV = RightVal.castAs<DefinedOrUnknownSVal>();
20440b57cec5SDimitry Andric 
20450b57cec5SDimitry Andric   // See if they are the same.
20460b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
20470b57cec5SDimitry Andric   DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV);
20480b57cec5SDimitry Andric   ProgramStateRef StSameBuf, StNotSameBuf;
20490b57cec5SDimitry Andric   std::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf);
20500b57cec5SDimitry Andric 
20510b57cec5SDimitry Andric   // If the two arguments might be the same buffer, we know the result is 0,
20520b57cec5SDimitry Andric   // and we only need to check one size.
20530b57cec5SDimitry Andric   if (StSameBuf) {
20540b57cec5SDimitry Andric     StSameBuf = StSameBuf->BindExpr(CE, LCtx,
20550b57cec5SDimitry Andric         svalBuilder.makeZeroVal(CE->getType()));
20560b57cec5SDimitry Andric     C.addTransition(StSameBuf);
20570b57cec5SDimitry Andric 
20580b57cec5SDimitry Andric     // If the two arguments are GUARANTEED to be the same, we're done!
20590b57cec5SDimitry Andric     if (!StNotSameBuf)
20600b57cec5SDimitry Andric       return;
20610b57cec5SDimitry Andric   }
20620b57cec5SDimitry Andric 
20630b57cec5SDimitry Andric   assert(StNotSameBuf);
20640b57cec5SDimitry Andric   state = StNotSameBuf;
20650b57cec5SDimitry Andric 
20660b57cec5SDimitry Andric   // At this point we can go about comparing the two buffers.
20670b57cec5SDimitry Andric   // For now, we only do this if they're both known string literals.
20680b57cec5SDimitry Andric 
20690b57cec5SDimitry Andric   // Attempt to extract string literals from both expressions.
20705ffd83dbSDimitry Andric   const StringLiteral *LeftStrLiteral =
20715ffd83dbSDimitry Andric       getCStringLiteral(C, state, Left.Expression, LeftVal);
20725ffd83dbSDimitry Andric   const StringLiteral *RightStrLiteral =
20735ffd83dbSDimitry Andric       getCStringLiteral(C, state, Right.Expression, RightVal);
20740b57cec5SDimitry Andric   bool canComputeResult = false;
20750b57cec5SDimitry Andric   SVal resultVal = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx,
20760b57cec5SDimitry Andric       C.blockCount());
20770b57cec5SDimitry Andric 
20785ffd83dbSDimitry Andric   if (LeftStrLiteral && RightStrLiteral) {
20795ffd83dbSDimitry Andric     StringRef LeftStrRef = LeftStrLiteral->getString();
20805ffd83dbSDimitry Andric     StringRef RightStrRef = RightStrLiteral->getString();
20810b57cec5SDimitry Andric 
2082480093f4SDimitry Andric     if (IsBounded) {
20830b57cec5SDimitry Andric       // Get the max number of characters to compare.
20840b57cec5SDimitry Andric       const Expr *lenExpr = CE->getArg(2);
20850b57cec5SDimitry Andric       SVal lenVal = state->getSVal(lenExpr, LCtx);
20860b57cec5SDimitry Andric 
20870b57cec5SDimitry Andric       // If the length is known, we can get the right substrings.
20880b57cec5SDimitry Andric       if (const llvm::APSInt *len = svalBuilder.getKnownValue(state, lenVal)) {
20890b57cec5SDimitry Andric         // Create substrings of each to compare the prefix.
20905ffd83dbSDimitry Andric         LeftStrRef = LeftStrRef.substr(0, (size_t)len->getZExtValue());
20915ffd83dbSDimitry Andric         RightStrRef = RightStrRef.substr(0, (size_t)len->getZExtValue());
20920b57cec5SDimitry Andric         canComputeResult = true;
20930b57cec5SDimitry Andric       }
20940b57cec5SDimitry Andric     } else {
20950b57cec5SDimitry Andric       // This is a normal, unbounded strcmp.
20960b57cec5SDimitry Andric       canComputeResult = true;
20970b57cec5SDimitry Andric     }
20980b57cec5SDimitry Andric 
20990b57cec5SDimitry Andric     if (canComputeResult) {
21000b57cec5SDimitry Andric       // Real strcmp stops at null characters.
21015ffd83dbSDimitry Andric       size_t s1Term = LeftStrRef.find('\0');
21020b57cec5SDimitry Andric       if (s1Term != StringRef::npos)
21035ffd83dbSDimitry Andric         LeftStrRef = LeftStrRef.substr(0, s1Term);
21040b57cec5SDimitry Andric 
21055ffd83dbSDimitry Andric       size_t s2Term = RightStrRef.find('\0');
21060b57cec5SDimitry Andric       if (s2Term != StringRef::npos)
21075ffd83dbSDimitry Andric         RightStrRef = RightStrRef.substr(0, s2Term);
21080b57cec5SDimitry Andric 
21090b57cec5SDimitry Andric       // Use StringRef's comparison methods to compute the actual result.
2110fe6060f1SDimitry Andric       int compareRes = IgnoreCase ? LeftStrRef.compare_insensitive(RightStrRef)
21115ffd83dbSDimitry Andric                                   : LeftStrRef.compare(RightStrRef);
21120b57cec5SDimitry Andric 
21130b57cec5SDimitry Andric       // The strcmp function returns an integer greater than, equal to, or less
21140b57cec5SDimitry Andric       // than zero, [c11, p7.24.4.2].
21150b57cec5SDimitry Andric       if (compareRes == 0) {
21160b57cec5SDimitry Andric         resultVal = svalBuilder.makeIntVal(compareRes, CE->getType());
21170b57cec5SDimitry Andric       }
21180b57cec5SDimitry Andric       else {
21190b57cec5SDimitry Andric         DefinedSVal zeroVal = svalBuilder.makeIntVal(0, CE->getType());
21200b57cec5SDimitry Andric         // Constrain strcmp's result range based on the result of StringRef's
21210b57cec5SDimitry Andric         // comparison methods.
21220b57cec5SDimitry Andric         BinaryOperatorKind op = (compareRes == 1) ? BO_GT : BO_LT;
21230b57cec5SDimitry Andric         SVal compareWithZero =
21240b57cec5SDimitry Andric           svalBuilder.evalBinOp(state, op, resultVal, zeroVal,
21250b57cec5SDimitry Andric               svalBuilder.getConditionType());
21260b57cec5SDimitry Andric         DefinedSVal compareWithZeroVal = compareWithZero.castAs<DefinedSVal>();
21270b57cec5SDimitry Andric         state = state->assume(compareWithZeroVal, true);
21280b57cec5SDimitry Andric       }
21290b57cec5SDimitry Andric     }
21300b57cec5SDimitry Andric   }
21310b57cec5SDimitry Andric 
21320b57cec5SDimitry Andric   state = state->BindExpr(CE, LCtx, resultVal);
21330b57cec5SDimitry Andric 
21340b57cec5SDimitry Andric   // Record this as a possible path.
21350b57cec5SDimitry Andric   C.addTransition(state);
21360b57cec5SDimitry Andric }
21370b57cec5SDimitry Andric 
21380b57cec5SDimitry Andric void CStringChecker::evalStrsep(CheckerContext &C, const CallExpr *CE) const {
21390b57cec5SDimitry Andric   // char *strsep(char **stringp, const char *delim);
21405e801ac6SDimitry Andric   // Verify whether the search string parameter matches the return type.
21415ffd83dbSDimitry Andric   SourceArgExpr SearchStrPtr = {CE->getArg(0), 0};
21425ffd83dbSDimitry Andric 
21435ffd83dbSDimitry Andric   QualType CharPtrTy = SearchStrPtr.Expression->getType()->getPointeeType();
21440b57cec5SDimitry Andric   if (CharPtrTy.isNull() ||
21450b57cec5SDimitry Andric       CE->getType().getUnqualifiedType() != CharPtrTy.getUnqualifiedType())
21460b57cec5SDimitry Andric     return;
21470b57cec5SDimitry Andric 
21480b57cec5SDimitry Andric   CurrentFunctionDescription = "strsep()";
21490b57cec5SDimitry Andric   ProgramStateRef State = C.getState();
21500b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
21510b57cec5SDimitry Andric 
21520b57cec5SDimitry Andric   // Check that the search string pointer is non-null (though it may point to
21530b57cec5SDimitry Andric   // a null string).
21545ffd83dbSDimitry Andric   SVal SearchStrVal = State->getSVal(SearchStrPtr.Expression, LCtx);
21555ffd83dbSDimitry Andric   State = checkNonNull(C, State, SearchStrPtr, SearchStrVal);
21560b57cec5SDimitry Andric   if (!State)
21570b57cec5SDimitry Andric     return;
21580b57cec5SDimitry Andric 
21590b57cec5SDimitry Andric   // Check that the delimiter string is non-null.
21605ffd83dbSDimitry Andric   AnyArgExpr DelimStr = {CE->getArg(1), 1};
21615ffd83dbSDimitry Andric   SVal DelimStrVal = State->getSVal(DelimStr.Expression, LCtx);
21625ffd83dbSDimitry Andric   State = checkNonNull(C, State, DelimStr, DelimStrVal);
21630b57cec5SDimitry Andric   if (!State)
21640b57cec5SDimitry Andric     return;
21650b57cec5SDimitry Andric 
21660b57cec5SDimitry Andric   SValBuilder &SVB = C.getSValBuilder();
21670b57cec5SDimitry Andric   SVal Result;
21680b57cec5SDimitry Andric   if (Optional<Loc> SearchStrLoc = SearchStrVal.getAs<Loc>()) {
21690b57cec5SDimitry Andric     // Get the current value of the search string pointer, as a char*.
21700b57cec5SDimitry Andric     Result = State->getSVal(*SearchStrLoc, CharPtrTy);
21710b57cec5SDimitry Andric 
21720b57cec5SDimitry Andric     // Invalidate the search string, representing the change of one delimiter
21730b57cec5SDimitry Andric     // character to NUL.
21745ffd83dbSDimitry Andric     State = InvalidateBuffer(C, State, SearchStrPtr.Expression, Result,
21750b57cec5SDimitry Andric                              /*IsSourceBuffer*/ false, nullptr);
21760b57cec5SDimitry Andric 
21770b57cec5SDimitry Andric     // Overwrite the search string pointer. The new value is either an address
21780b57cec5SDimitry Andric     // further along in the same string, or NULL if there are no more tokens.
21790b57cec5SDimitry Andric     State = State->bindLoc(*SearchStrLoc,
21800b57cec5SDimitry Andric         SVB.conjureSymbolVal(getTag(),
21810b57cec5SDimitry Andric           CE,
21820b57cec5SDimitry Andric           LCtx,
21830b57cec5SDimitry Andric           CharPtrTy,
21840b57cec5SDimitry Andric           C.blockCount()),
21850b57cec5SDimitry Andric         LCtx);
21860b57cec5SDimitry Andric   } else {
21870b57cec5SDimitry Andric     assert(SearchStrVal.isUnknown());
21880b57cec5SDimitry Andric     // Conjure a symbolic value. It's the best we can do.
21890b57cec5SDimitry Andric     Result = SVB.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
21900b57cec5SDimitry Andric   }
21910b57cec5SDimitry Andric 
21920b57cec5SDimitry Andric   // Set the return value, and finish.
21930b57cec5SDimitry Andric   State = State->BindExpr(CE, LCtx, Result);
21940b57cec5SDimitry Andric   C.addTransition(State);
21950b57cec5SDimitry Andric }
21960b57cec5SDimitry Andric 
21970b57cec5SDimitry Andric // These should probably be moved into a C++ standard library checker.
21980b57cec5SDimitry Andric void CStringChecker::evalStdCopy(CheckerContext &C, const CallExpr *CE) const {
21990b57cec5SDimitry Andric   evalStdCopyCommon(C, CE);
22000b57cec5SDimitry Andric }
22010b57cec5SDimitry Andric 
22020b57cec5SDimitry Andric void CStringChecker::evalStdCopyBackward(CheckerContext &C,
22030b57cec5SDimitry Andric     const CallExpr *CE) const {
22040b57cec5SDimitry Andric   evalStdCopyCommon(C, CE);
22050b57cec5SDimitry Andric }
22060b57cec5SDimitry Andric 
22070b57cec5SDimitry Andric void CStringChecker::evalStdCopyCommon(CheckerContext &C,
22080b57cec5SDimitry Andric     const CallExpr *CE) const {
22090b57cec5SDimitry Andric   if (!CE->getArg(2)->getType()->isPointerType())
22100b57cec5SDimitry Andric     return;
22110b57cec5SDimitry Andric 
22120b57cec5SDimitry Andric   ProgramStateRef State = C.getState();
22130b57cec5SDimitry Andric 
22140b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
22150b57cec5SDimitry Andric 
22160b57cec5SDimitry Andric   // template <class _InputIterator, class _OutputIterator>
22170b57cec5SDimitry Andric   // _OutputIterator
22180b57cec5SDimitry Andric   // copy(_InputIterator __first, _InputIterator __last,
22190b57cec5SDimitry Andric   //        _OutputIterator __result)
22200b57cec5SDimitry Andric 
22210b57cec5SDimitry Andric   // Invalidate the destination buffer
22220b57cec5SDimitry Andric   const Expr *Dst = CE->getArg(2);
22230b57cec5SDimitry Andric   SVal DstVal = State->getSVal(Dst, LCtx);
22240b57cec5SDimitry Andric   State = InvalidateBuffer(C, State, Dst, DstVal, /*IsSource=*/false,
22250b57cec5SDimitry Andric       /*Size=*/nullptr);
22260b57cec5SDimitry Andric 
22270b57cec5SDimitry Andric   SValBuilder &SVB = C.getSValBuilder();
22280b57cec5SDimitry Andric 
22290b57cec5SDimitry Andric   SVal ResultVal = SVB.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
22300b57cec5SDimitry Andric   State = State->BindExpr(CE, LCtx, ResultVal);
22310b57cec5SDimitry Andric 
22320b57cec5SDimitry Andric   C.addTransition(State);
22330b57cec5SDimitry Andric }
22340b57cec5SDimitry Andric 
22350b57cec5SDimitry Andric void CStringChecker::evalMemset(CheckerContext &C, const CallExpr *CE) const {
22365ffd83dbSDimitry Andric   // void *memset(void *s, int c, size_t n);
22370b57cec5SDimitry Andric   CurrentFunctionDescription = "memory set function";
22380b57cec5SDimitry Andric 
22395ffd83dbSDimitry Andric   DestinationArgExpr Buffer = {CE->getArg(0), 0};
22405ffd83dbSDimitry Andric   AnyArgExpr CharE = {CE->getArg(1), 1};
22415ffd83dbSDimitry Andric   SizeArgExpr Size = {CE->getArg(2), 2};
22425ffd83dbSDimitry Andric 
22430b57cec5SDimitry Andric   ProgramStateRef State = C.getState();
22440b57cec5SDimitry Andric 
22450b57cec5SDimitry Andric   // See if the size argument is zero.
22460b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
22475ffd83dbSDimitry Andric   SVal SizeVal = C.getSVal(Size.Expression);
22485ffd83dbSDimitry Andric   QualType SizeTy = Size.Expression->getType();
22490b57cec5SDimitry Andric 
22505ffd83dbSDimitry Andric   ProgramStateRef ZeroSize, NonZeroSize;
22515ffd83dbSDimitry Andric   std::tie(ZeroSize, NonZeroSize) = assumeZero(C, State, SizeVal, SizeTy);
22520b57cec5SDimitry Andric 
22530b57cec5SDimitry Andric   // Get the value of the memory area.
22545ffd83dbSDimitry Andric   SVal BufferPtrVal = C.getSVal(Buffer.Expression);
22550b57cec5SDimitry Andric 
22560b57cec5SDimitry Andric   // If the size is zero, there won't be any actual memory access, so
22575ffd83dbSDimitry Andric   // just bind the return value to the buffer and return.
22585ffd83dbSDimitry Andric   if (ZeroSize && !NonZeroSize) {
22595ffd83dbSDimitry Andric     ZeroSize = ZeroSize->BindExpr(CE, LCtx, BufferPtrVal);
22605ffd83dbSDimitry Andric     C.addTransition(ZeroSize);
22610b57cec5SDimitry Andric     return;
22620b57cec5SDimitry Andric   }
22630b57cec5SDimitry Andric 
22640b57cec5SDimitry Andric   // Ensure the memory area is not null.
22650b57cec5SDimitry Andric   // If it is NULL there will be a NULL pointer dereference.
22665ffd83dbSDimitry Andric   State = checkNonNull(C, NonZeroSize, Buffer, BufferPtrVal);
22670b57cec5SDimitry Andric   if (!State)
22680b57cec5SDimitry Andric     return;
22690b57cec5SDimitry Andric 
22705ffd83dbSDimitry Andric   State = CheckBufferAccess(C, State, Buffer, Size, AccessKind::write);
22710b57cec5SDimitry Andric   if (!State)
22720b57cec5SDimitry Andric     return;
22730b57cec5SDimitry Andric 
22740b57cec5SDimitry Andric   // According to the values of the arguments, bind the value of the second
22750b57cec5SDimitry Andric   // argument to the destination buffer and set string length, or just
22760b57cec5SDimitry Andric   // invalidate the destination buffer.
22775ffd83dbSDimitry Andric   if (!memsetAux(Buffer.Expression, C.getSVal(CharE.Expression),
22785ffd83dbSDimitry Andric                  Size.Expression, C, State))
22790b57cec5SDimitry Andric     return;
22800b57cec5SDimitry Andric 
22815ffd83dbSDimitry Andric   State = State->BindExpr(CE, LCtx, BufferPtrVal);
22820b57cec5SDimitry Andric   C.addTransition(State);
22830b57cec5SDimitry Andric }
22840b57cec5SDimitry Andric 
22850b57cec5SDimitry Andric void CStringChecker::evalBzero(CheckerContext &C, const CallExpr *CE) const {
22860b57cec5SDimitry Andric   CurrentFunctionDescription = "memory clearance function";
22870b57cec5SDimitry Andric 
22885ffd83dbSDimitry Andric   DestinationArgExpr Buffer = {CE->getArg(0), 0};
22895ffd83dbSDimitry Andric   SizeArgExpr Size = {CE->getArg(1), 1};
22900b57cec5SDimitry Andric   SVal Zero = C.getSValBuilder().makeZeroVal(C.getASTContext().IntTy);
22910b57cec5SDimitry Andric 
22920b57cec5SDimitry Andric   ProgramStateRef State = C.getState();
22930b57cec5SDimitry Andric 
22940b57cec5SDimitry Andric   // See if the size argument is zero.
22955ffd83dbSDimitry Andric   SVal SizeVal = C.getSVal(Size.Expression);
22965ffd83dbSDimitry Andric   QualType SizeTy = Size.Expression->getType();
22970b57cec5SDimitry Andric 
22980b57cec5SDimitry Andric   ProgramStateRef StateZeroSize, StateNonZeroSize;
22990b57cec5SDimitry Andric   std::tie(StateZeroSize, StateNonZeroSize) =
23000b57cec5SDimitry Andric     assumeZero(C, State, SizeVal, SizeTy);
23010b57cec5SDimitry Andric 
23020b57cec5SDimitry Andric   // If the size is zero, there won't be any actual memory access,
23030b57cec5SDimitry Andric   // In this case we just return.
23040b57cec5SDimitry Andric   if (StateZeroSize && !StateNonZeroSize) {
23050b57cec5SDimitry Andric     C.addTransition(StateZeroSize);
23060b57cec5SDimitry Andric     return;
23070b57cec5SDimitry Andric   }
23080b57cec5SDimitry Andric 
23090b57cec5SDimitry Andric   // Get the value of the memory area.
23105ffd83dbSDimitry Andric   SVal MemVal = C.getSVal(Buffer.Expression);
23110b57cec5SDimitry Andric 
23120b57cec5SDimitry Andric   // Ensure the memory area is not null.
23130b57cec5SDimitry Andric   // If it is NULL there will be a NULL pointer dereference.
23145ffd83dbSDimitry Andric   State = checkNonNull(C, StateNonZeroSize, Buffer, MemVal);
23150b57cec5SDimitry Andric   if (!State)
23160b57cec5SDimitry Andric     return;
23170b57cec5SDimitry Andric 
23185ffd83dbSDimitry Andric   State = CheckBufferAccess(C, State, Buffer, Size, AccessKind::write);
23190b57cec5SDimitry Andric   if (!State)
23200b57cec5SDimitry Andric     return;
23210b57cec5SDimitry Andric 
23225ffd83dbSDimitry Andric   if (!memsetAux(Buffer.Expression, Zero, Size.Expression, C, State))
23230b57cec5SDimitry Andric     return;
23240b57cec5SDimitry Andric 
23250b57cec5SDimitry Andric   C.addTransition(State);
23260b57cec5SDimitry Andric }
23270b57cec5SDimitry Andric 
23280b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
23290b57cec5SDimitry Andric // The driver method, and other Checker callbacks.
23300b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
23310b57cec5SDimitry Andric 
23320b57cec5SDimitry Andric CStringChecker::FnCheck CStringChecker::identifyCall(const CallEvent &Call,
23330b57cec5SDimitry Andric                                                      CheckerContext &C) const {
23340b57cec5SDimitry Andric   const auto *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr());
23350b57cec5SDimitry Andric   if (!CE)
23360b57cec5SDimitry Andric     return nullptr;
23370b57cec5SDimitry Andric 
23380b57cec5SDimitry Andric   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
23390b57cec5SDimitry Andric   if (!FD)
23400b57cec5SDimitry Andric     return nullptr;
23410b57cec5SDimitry Andric 
2342349cc55cSDimitry Andric   if (StdCopy.matches(Call))
23430b57cec5SDimitry Andric     return &CStringChecker::evalStdCopy;
2344349cc55cSDimitry Andric   if (StdCopyBackward.matches(Call))
23450b57cec5SDimitry Andric     return &CStringChecker::evalStdCopyBackward;
23460b57cec5SDimitry Andric 
23470b57cec5SDimitry Andric   // Pro-actively check that argument types are safe to do arithmetic upon.
23480b57cec5SDimitry Andric   // We do not want to crash if someone accidentally passes a structure
23490b57cec5SDimitry Andric   // into, say, a C++ overload of any of these functions. We could not check
23500b57cec5SDimitry Andric   // that for std::copy because they may have arguments of other types.
23510b57cec5SDimitry Andric   for (auto I : CE->arguments()) {
23520b57cec5SDimitry Andric     QualType T = I->getType();
23530b57cec5SDimitry Andric     if (!T->isIntegralOrEnumerationType() && !T->isPointerType())
23540b57cec5SDimitry Andric       return nullptr;
23550b57cec5SDimitry Andric   }
23560b57cec5SDimitry Andric 
23570b57cec5SDimitry Andric   const FnCheck *Callback = Callbacks.lookup(Call);
23580b57cec5SDimitry Andric   if (Callback)
23590b57cec5SDimitry Andric     return *Callback;
23600b57cec5SDimitry Andric 
23610b57cec5SDimitry Andric   return nullptr;
23620b57cec5SDimitry Andric }
23630b57cec5SDimitry Andric 
23640b57cec5SDimitry Andric bool CStringChecker::evalCall(const CallEvent &Call, CheckerContext &C) const {
23650b57cec5SDimitry Andric   FnCheck Callback = identifyCall(Call, C);
23660b57cec5SDimitry Andric 
23670b57cec5SDimitry Andric   // If the callee isn't a string function, let another checker handle it.
23680b57cec5SDimitry Andric   if (!Callback)
23690b57cec5SDimitry Andric     return false;
23700b57cec5SDimitry Andric 
23710b57cec5SDimitry Andric   // Check and evaluate the call.
23720b57cec5SDimitry Andric   const auto *CE = cast<CallExpr>(Call.getOriginExpr());
2373*972a253aSDimitry Andric   Callback(this, C, CE);
23740b57cec5SDimitry Andric 
23750b57cec5SDimitry Andric   // If the evaluate call resulted in no change, chain to the next eval call
23760b57cec5SDimitry Andric   // handler.
23770b57cec5SDimitry Andric   // Note, the custom CString evaluation calls assume that basic safety
23780b57cec5SDimitry Andric   // properties are held. However, if the user chooses to turn off some of these
23790b57cec5SDimitry Andric   // checks, we ignore the issues and leave the call evaluation to a generic
23800b57cec5SDimitry Andric   // handler.
23810b57cec5SDimitry Andric   return C.isDifferent();
23820b57cec5SDimitry Andric }
23830b57cec5SDimitry Andric 
23840b57cec5SDimitry Andric void CStringChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const {
23850b57cec5SDimitry Andric   // Record string length for char a[] = "abc";
23860b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
23870b57cec5SDimitry Andric 
23880b57cec5SDimitry Andric   for (const auto *I : DS->decls()) {
23890b57cec5SDimitry Andric     const VarDecl *D = dyn_cast<VarDecl>(I);
23900b57cec5SDimitry Andric     if (!D)
23910b57cec5SDimitry Andric       continue;
23920b57cec5SDimitry Andric 
23930b57cec5SDimitry Andric     // FIXME: Handle array fields of structs.
23940b57cec5SDimitry Andric     if (!D->getType()->isArrayType())
23950b57cec5SDimitry Andric       continue;
23960b57cec5SDimitry Andric 
23970b57cec5SDimitry Andric     const Expr *Init = D->getInit();
23980b57cec5SDimitry Andric     if (!Init)
23990b57cec5SDimitry Andric       continue;
24000b57cec5SDimitry Andric     if (!isa<StringLiteral>(Init))
24010b57cec5SDimitry Andric       continue;
24020b57cec5SDimitry Andric 
24030b57cec5SDimitry Andric     Loc VarLoc = state->getLValue(D, C.getLocationContext());
24040b57cec5SDimitry Andric     const MemRegion *MR = VarLoc.getAsRegion();
24050b57cec5SDimitry Andric     if (!MR)
24060b57cec5SDimitry Andric       continue;
24070b57cec5SDimitry Andric 
24080b57cec5SDimitry Andric     SVal StrVal = C.getSVal(Init);
24090b57cec5SDimitry Andric     assert(StrVal.isValid() && "Initializer string is unknown or undefined");
24100b57cec5SDimitry Andric     DefinedOrUnknownSVal strLength =
24110b57cec5SDimitry Andric       getCStringLength(C, state, Init, StrVal).castAs<DefinedOrUnknownSVal>();
24120b57cec5SDimitry Andric 
24130b57cec5SDimitry Andric     state = state->set<CStringLength>(MR, strLength);
24140b57cec5SDimitry Andric   }
24150b57cec5SDimitry Andric 
24160b57cec5SDimitry Andric   C.addTransition(state);
24170b57cec5SDimitry Andric }
24180b57cec5SDimitry Andric 
24190b57cec5SDimitry Andric ProgramStateRef
24200b57cec5SDimitry Andric CStringChecker::checkRegionChanges(ProgramStateRef state,
24210b57cec5SDimitry Andric     const InvalidatedSymbols *,
24220b57cec5SDimitry Andric     ArrayRef<const MemRegion *> ExplicitRegions,
24230b57cec5SDimitry Andric     ArrayRef<const MemRegion *> Regions,
24240b57cec5SDimitry Andric     const LocationContext *LCtx,
24250b57cec5SDimitry Andric     const CallEvent *Call) const {
24260b57cec5SDimitry Andric   CStringLengthTy Entries = state->get<CStringLength>();
24270b57cec5SDimitry Andric   if (Entries.isEmpty())
24280b57cec5SDimitry Andric     return state;
24290b57cec5SDimitry Andric 
24300b57cec5SDimitry Andric   llvm::SmallPtrSet<const MemRegion *, 8> Invalidated;
24310b57cec5SDimitry Andric   llvm::SmallPtrSet<const MemRegion *, 32> SuperRegions;
24320b57cec5SDimitry Andric 
24330b57cec5SDimitry Andric   // First build sets for the changed regions and their super-regions.
24340b57cec5SDimitry Andric   for (ArrayRef<const MemRegion *>::iterator
24350b57cec5SDimitry Andric       I = Regions.begin(), E = Regions.end(); I != E; ++I) {
24360b57cec5SDimitry Andric     const MemRegion *MR = *I;
24370b57cec5SDimitry Andric     Invalidated.insert(MR);
24380b57cec5SDimitry Andric 
24390b57cec5SDimitry Andric     SuperRegions.insert(MR);
24400b57cec5SDimitry Andric     while (const SubRegion *SR = dyn_cast<SubRegion>(MR)) {
24410b57cec5SDimitry Andric       MR = SR->getSuperRegion();
24420b57cec5SDimitry Andric       SuperRegions.insert(MR);
24430b57cec5SDimitry Andric     }
24440b57cec5SDimitry Andric   }
24450b57cec5SDimitry Andric 
24460b57cec5SDimitry Andric   CStringLengthTy::Factory &F = state->get_context<CStringLength>();
24470b57cec5SDimitry Andric 
24480b57cec5SDimitry Andric   // Then loop over the entries in the current state.
24490b57cec5SDimitry Andric   for (CStringLengthTy::iterator I = Entries.begin(),
24500b57cec5SDimitry Andric       E = Entries.end(); I != E; ++I) {
24510b57cec5SDimitry Andric     const MemRegion *MR = I.getKey();
24520b57cec5SDimitry Andric 
24530b57cec5SDimitry Andric     // Is this entry for a super-region of a changed region?
24540b57cec5SDimitry Andric     if (SuperRegions.count(MR)) {
24550b57cec5SDimitry Andric       Entries = F.remove(Entries, MR);
24560b57cec5SDimitry Andric       continue;
24570b57cec5SDimitry Andric     }
24580b57cec5SDimitry Andric 
24590b57cec5SDimitry Andric     // Is this entry for a sub-region of a changed region?
24600b57cec5SDimitry Andric     const MemRegion *Super = MR;
24610b57cec5SDimitry Andric     while (const SubRegion *SR = dyn_cast<SubRegion>(Super)) {
24620b57cec5SDimitry Andric       Super = SR->getSuperRegion();
24630b57cec5SDimitry Andric       if (Invalidated.count(Super)) {
24640b57cec5SDimitry Andric         Entries = F.remove(Entries, MR);
24650b57cec5SDimitry Andric         break;
24660b57cec5SDimitry Andric       }
24670b57cec5SDimitry Andric     }
24680b57cec5SDimitry Andric   }
24690b57cec5SDimitry Andric 
24700b57cec5SDimitry Andric   return state->set<CStringLength>(Entries);
24710b57cec5SDimitry Andric }
24720b57cec5SDimitry Andric 
24730b57cec5SDimitry Andric void CStringChecker::checkLiveSymbols(ProgramStateRef state,
24740b57cec5SDimitry Andric     SymbolReaper &SR) const {
24750b57cec5SDimitry Andric   // Mark all symbols in our string length map as valid.
24760b57cec5SDimitry Andric   CStringLengthTy Entries = state->get<CStringLength>();
24770b57cec5SDimitry Andric 
24780b57cec5SDimitry Andric   for (CStringLengthTy::iterator I = Entries.begin(), E = Entries.end();
24790b57cec5SDimitry Andric       I != E; ++I) {
24800b57cec5SDimitry Andric     SVal Len = I.getData();
24810b57cec5SDimitry Andric 
24820b57cec5SDimitry Andric     for (SymExpr::symbol_iterator si = Len.symbol_begin(),
24830b57cec5SDimitry Andric         se = Len.symbol_end(); si != se; ++si)
24840b57cec5SDimitry Andric       SR.markInUse(*si);
24850b57cec5SDimitry Andric   }
24860b57cec5SDimitry Andric }
24870b57cec5SDimitry Andric 
24880b57cec5SDimitry Andric void CStringChecker::checkDeadSymbols(SymbolReaper &SR,
24890b57cec5SDimitry Andric     CheckerContext &C) const {
24900b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
24910b57cec5SDimitry Andric   CStringLengthTy Entries = state->get<CStringLength>();
24920b57cec5SDimitry Andric   if (Entries.isEmpty())
24930b57cec5SDimitry Andric     return;
24940b57cec5SDimitry Andric 
24950b57cec5SDimitry Andric   CStringLengthTy::Factory &F = state->get_context<CStringLength>();
24960b57cec5SDimitry Andric   for (CStringLengthTy::iterator I = Entries.begin(), E = Entries.end();
24970b57cec5SDimitry Andric       I != E; ++I) {
24980b57cec5SDimitry Andric     SVal Len = I.getData();
24990b57cec5SDimitry Andric     if (SymbolRef Sym = Len.getAsSymbol()) {
25000b57cec5SDimitry Andric       if (SR.isDead(Sym))
25010b57cec5SDimitry Andric         Entries = F.remove(Entries, I.getKey());
25020b57cec5SDimitry Andric     }
25030b57cec5SDimitry Andric   }
25040b57cec5SDimitry Andric 
25050b57cec5SDimitry Andric   state = state->set<CStringLength>(Entries);
25060b57cec5SDimitry Andric   C.addTransition(state);
25070b57cec5SDimitry Andric }
25080b57cec5SDimitry Andric 
25090b57cec5SDimitry Andric void ento::registerCStringModeling(CheckerManager &Mgr) {
25100b57cec5SDimitry Andric   Mgr.registerChecker<CStringChecker>();
25110b57cec5SDimitry Andric }
25120b57cec5SDimitry Andric 
25135ffd83dbSDimitry Andric bool ento::shouldRegisterCStringModeling(const CheckerManager &mgr) {
25140b57cec5SDimitry Andric   return true;
25150b57cec5SDimitry Andric }
25160b57cec5SDimitry Andric 
25170b57cec5SDimitry Andric #define REGISTER_CHECKER(name)                                                 \
25180b57cec5SDimitry Andric   void ento::register##name(CheckerManager &mgr) {                             \
25190b57cec5SDimitry Andric     CStringChecker *checker = mgr.getChecker<CStringChecker>();                \
25200b57cec5SDimitry Andric     checker->Filter.Check##name = true;                                        \
2521a7dea167SDimitry Andric     checker->Filter.CheckName##name = mgr.getCurrentCheckerName();             \
25220b57cec5SDimitry Andric   }                                                                            \
25230b57cec5SDimitry Andric                                                                                \
25245ffd83dbSDimitry Andric   bool ento::shouldRegister##name(const CheckerManager &mgr) { return true; }
25250b57cec5SDimitry Andric 
25260b57cec5SDimitry Andric REGISTER_CHECKER(CStringNullArg)
25270b57cec5SDimitry Andric REGISTER_CHECKER(CStringOutOfBounds)
25280b57cec5SDimitry Andric REGISTER_CHECKER(CStringBufferOverlap)
25290b57cec5SDimitry Andric REGISTER_CHECKER(CStringNotNullTerm)
253081ad6265SDimitry Andric REGISTER_CHECKER(CStringUninitializedRead)
2531