xref: /freebsd/contrib/llvm-project/clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp (revision 753f127f3ace09432b2baeffd71a308760641a62)
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"
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric using namespace clang;
310b57cec5SDimitry Andric using namespace ento;
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric namespace {
345ffd83dbSDimitry Andric struct AnyArgExpr {
355ffd83dbSDimitry Andric   // FIXME: Remove constructor in C++17 to turn it into an aggregate.
365ffd83dbSDimitry Andric   AnyArgExpr(const Expr *Expression, unsigned ArgumentIndex)
375ffd83dbSDimitry Andric       : Expression{Expression}, ArgumentIndex{ArgumentIndex} {}
385ffd83dbSDimitry Andric   const Expr *Expression;
395ffd83dbSDimitry Andric   unsigned ArgumentIndex;
405ffd83dbSDimitry Andric };
415ffd83dbSDimitry Andric 
425ffd83dbSDimitry Andric struct SourceArgExpr : AnyArgExpr {
435ffd83dbSDimitry Andric   using AnyArgExpr::AnyArgExpr; // FIXME: Remove using in C++17.
445ffd83dbSDimitry Andric };
455ffd83dbSDimitry Andric 
465ffd83dbSDimitry Andric struct DestinationArgExpr : AnyArgExpr {
475ffd83dbSDimitry Andric   using AnyArgExpr::AnyArgExpr; // FIXME: Same.
485ffd83dbSDimitry Andric };
495ffd83dbSDimitry Andric 
505ffd83dbSDimitry Andric struct SizeArgExpr : AnyArgExpr {
515ffd83dbSDimitry Andric   using AnyArgExpr::AnyArgExpr; // FIXME: Same.
525ffd83dbSDimitry Andric };
535ffd83dbSDimitry Andric 
545ffd83dbSDimitry Andric using ErrorMessage = SmallString<128>;
555ffd83dbSDimitry Andric enum class AccessKind { write, read };
565ffd83dbSDimitry Andric 
575ffd83dbSDimitry Andric static ErrorMessage createOutOfBoundErrorMsg(StringRef FunctionDescription,
585ffd83dbSDimitry Andric                                              AccessKind Access) {
595ffd83dbSDimitry Andric   ErrorMessage Message;
605ffd83dbSDimitry Andric   llvm::raw_svector_ostream Os(Message);
615ffd83dbSDimitry Andric 
625ffd83dbSDimitry Andric   // Function classification like: Memory copy function
635ffd83dbSDimitry Andric   Os << toUppercase(FunctionDescription.front())
645ffd83dbSDimitry Andric      << &FunctionDescription.data()[1];
655ffd83dbSDimitry Andric 
665ffd83dbSDimitry Andric   if (Access == AccessKind::write) {
675ffd83dbSDimitry Andric     Os << " overflows the destination buffer";
685ffd83dbSDimitry Andric   } else { // read access
695ffd83dbSDimitry Andric     Os << " accesses out-of-bound array element";
705ffd83dbSDimitry Andric   }
715ffd83dbSDimitry Andric 
725ffd83dbSDimitry Andric   return Message;
735ffd83dbSDimitry Andric }
745ffd83dbSDimitry Andric 
75480093f4SDimitry Andric enum class ConcatFnKind { none = 0, strcat = 1, strlcat = 2 };
760b57cec5SDimitry Andric class CStringChecker : public Checker< eval::Call,
770b57cec5SDimitry Andric                                          check::PreStmt<DeclStmt>,
780b57cec5SDimitry Andric                                          check::LiveSymbols,
790b57cec5SDimitry Andric                                          check::DeadSymbols,
800b57cec5SDimitry Andric                                          check::RegionChanges
810b57cec5SDimitry Andric                                          > {
820b57cec5SDimitry Andric   mutable std::unique_ptr<BugType> BT_Null, BT_Bounds, BT_Overlap,
8381ad6265SDimitry Andric       BT_NotCString, BT_AdditionOverflow, BT_UninitRead;
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric   mutable const char *CurrentFunctionDescription;
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric public:
880b57cec5SDimitry Andric   /// The filter is used to filter out the diagnostics which are not enabled by
890b57cec5SDimitry Andric   /// the user.
900b57cec5SDimitry Andric   struct CStringChecksFilter {
9181ad6265SDimitry Andric     bool CheckCStringNullArg = false;
9281ad6265SDimitry Andric     bool CheckCStringOutOfBounds = false;
9381ad6265SDimitry Andric     bool CheckCStringBufferOverlap = false;
9481ad6265SDimitry Andric     bool CheckCStringNotNullTerm = false;
9581ad6265SDimitry Andric     bool CheckCStringUninitializedRead = false;
960b57cec5SDimitry Andric 
97a7dea167SDimitry Andric     CheckerNameRef CheckNameCStringNullArg;
98a7dea167SDimitry Andric     CheckerNameRef CheckNameCStringOutOfBounds;
99a7dea167SDimitry Andric     CheckerNameRef CheckNameCStringBufferOverlap;
100a7dea167SDimitry Andric     CheckerNameRef CheckNameCStringNotNullTerm;
10181ad6265SDimitry Andric     CheckerNameRef CheckNameCStringUninitializedRead;
1020b57cec5SDimitry Andric   };
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric   CStringChecksFilter Filter;
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric   static void *getTag() { static int tag; return &tag; }
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   bool evalCall(const CallEvent &Call, CheckerContext &C) const;
1090b57cec5SDimitry Andric   void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const;
1100b57cec5SDimitry Andric   void checkLiveSymbols(ProgramStateRef state, SymbolReaper &SR) const;
1110b57cec5SDimitry Andric   void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric   ProgramStateRef
1140b57cec5SDimitry Andric     checkRegionChanges(ProgramStateRef state,
1150b57cec5SDimitry Andric                        const InvalidatedSymbols *,
1160b57cec5SDimitry Andric                        ArrayRef<const MemRegion *> ExplicitRegions,
1170b57cec5SDimitry Andric                        ArrayRef<const MemRegion *> Regions,
1180b57cec5SDimitry Andric                        const LocationContext *LCtx,
1190b57cec5SDimitry Andric                        const CallEvent *Call) const;
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric   typedef void (CStringChecker::*FnCheck)(CheckerContext &,
1220b57cec5SDimitry Andric                                           const CallExpr *) const;
1230b57cec5SDimitry Andric   CallDescriptionMap<FnCheck> Callbacks = {
1240b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "memcpy", 3}, &CStringChecker::evalMemcpy},
1250b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "mempcpy", 3}, &CStringChecker::evalMempcpy},
1260b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "memcmp", 3}, &CStringChecker::evalMemcmp},
1270b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "memmove", 3}, &CStringChecker::evalMemmove},
1280b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "memset", 3}, &CStringChecker::evalMemset},
1290b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "explicit_memset", 3}, &CStringChecker::evalMemset},
1300b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strcpy", 2}, &CStringChecker::evalStrcpy},
1310b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strncpy", 3}, &CStringChecker::evalStrncpy},
1320b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "stpcpy", 2}, &CStringChecker::evalStpcpy},
1330b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strlcpy", 3}, &CStringChecker::evalStrlcpy},
1340b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strcat", 2}, &CStringChecker::evalStrcat},
1350b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strncat", 3}, &CStringChecker::evalStrncat},
1360b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strlcat", 3}, &CStringChecker::evalStrlcat},
1370b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strlen", 1}, &CStringChecker::evalstrLength},
1380b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strnlen", 2}, &CStringChecker::evalstrnLength},
1390b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strcmp", 2}, &CStringChecker::evalStrcmp},
1400b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strncmp", 3}, &CStringChecker::evalStrncmp},
1410b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strcasecmp", 2}, &CStringChecker::evalStrcasecmp},
1420b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strncasecmp", 3}, &CStringChecker::evalStrncasecmp},
1430b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "strsep", 2}, &CStringChecker::evalStrsep},
1440b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "bcopy", 3}, &CStringChecker::evalBcopy},
1450b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "bcmp", 3}, &CStringChecker::evalMemcmp},
1460b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "bzero", 2}, &CStringChecker::evalBzero},
1470b57cec5SDimitry Andric       {{CDF_MaybeBuiltin, "explicit_bzero", 2}, &CStringChecker::evalBzero},
1480b57cec5SDimitry Andric   };
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric   // These require a bit of special handling.
1510b57cec5SDimitry Andric   CallDescription StdCopy{{"std", "copy"}, 3},
1520b57cec5SDimitry Andric       StdCopyBackward{{"std", "copy_backward"}, 3};
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric   FnCheck identifyCall(const CallEvent &Call, CheckerContext &C) const;
1550b57cec5SDimitry Andric   void evalMemcpy(CheckerContext &C, const CallExpr *CE) const;
1560b57cec5SDimitry Andric   void evalMempcpy(CheckerContext &C, const CallExpr *CE) const;
1570b57cec5SDimitry Andric   void evalMemmove(CheckerContext &C, const CallExpr *CE) const;
1580b57cec5SDimitry Andric   void evalBcopy(CheckerContext &C, const CallExpr *CE) const;
1590b57cec5SDimitry Andric   void evalCopyCommon(CheckerContext &C, const CallExpr *CE,
1605ffd83dbSDimitry Andric                       ProgramStateRef state, SizeArgExpr Size,
1615ffd83dbSDimitry Andric                       DestinationArgExpr Dest, SourceArgExpr Source,
1625ffd83dbSDimitry Andric                       bool Restricted, bool IsMempcpy) const;
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric   void evalMemcmp(CheckerContext &C, const CallExpr *CE) const;
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric   void evalstrLength(CheckerContext &C, const CallExpr *CE) const;
1670b57cec5SDimitry Andric   void evalstrnLength(CheckerContext &C, const CallExpr *CE) const;
1680b57cec5SDimitry Andric   void evalstrLengthCommon(CheckerContext &C,
1690b57cec5SDimitry Andric                            const CallExpr *CE,
1700b57cec5SDimitry Andric                            bool IsStrnlen = false) const;
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric   void evalStrcpy(CheckerContext &C, const CallExpr *CE) const;
1730b57cec5SDimitry Andric   void evalStrncpy(CheckerContext &C, const CallExpr *CE) const;
1740b57cec5SDimitry Andric   void evalStpcpy(CheckerContext &C, const CallExpr *CE) const;
1750b57cec5SDimitry Andric   void evalStrlcpy(CheckerContext &C, const CallExpr *CE) const;
176480093f4SDimitry Andric   void evalStrcpyCommon(CheckerContext &C, const CallExpr *CE, bool ReturnEnd,
177480093f4SDimitry Andric                         bool IsBounded, ConcatFnKind appendK,
1780b57cec5SDimitry Andric                         bool returnPtr = true) const;
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric   void evalStrcat(CheckerContext &C, const CallExpr *CE) const;
1810b57cec5SDimitry Andric   void evalStrncat(CheckerContext &C, const CallExpr *CE) const;
1820b57cec5SDimitry Andric   void evalStrlcat(CheckerContext &C, const CallExpr *CE) const;
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric   void evalStrcmp(CheckerContext &C, const CallExpr *CE) const;
1850b57cec5SDimitry Andric   void evalStrncmp(CheckerContext &C, const CallExpr *CE) const;
1860b57cec5SDimitry Andric   void evalStrcasecmp(CheckerContext &C, const CallExpr *CE) const;
1870b57cec5SDimitry Andric   void evalStrncasecmp(CheckerContext &C, const CallExpr *CE) const;
1880b57cec5SDimitry Andric   void evalStrcmpCommon(CheckerContext &C,
1890b57cec5SDimitry Andric                         const CallExpr *CE,
190480093f4SDimitry Andric                         bool IsBounded = false,
191480093f4SDimitry Andric                         bool IgnoreCase = false) const;
1920b57cec5SDimitry Andric 
1930b57cec5SDimitry Andric   void evalStrsep(CheckerContext &C, const CallExpr *CE) const;
1940b57cec5SDimitry Andric 
1950b57cec5SDimitry Andric   void evalStdCopy(CheckerContext &C, const CallExpr *CE) const;
1960b57cec5SDimitry Andric   void evalStdCopyBackward(CheckerContext &C, const CallExpr *CE) const;
1970b57cec5SDimitry Andric   void evalStdCopyCommon(CheckerContext &C, const CallExpr *CE) const;
1980b57cec5SDimitry Andric   void evalMemset(CheckerContext &C, const CallExpr *CE) const;
1990b57cec5SDimitry Andric   void evalBzero(CheckerContext &C, const CallExpr *CE) const;
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric   // Utility methods
2020b57cec5SDimitry Andric   std::pair<ProgramStateRef , ProgramStateRef >
2030b57cec5SDimitry Andric   static assumeZero(CheckerContext &C,
2040b57cec5SDimitry Andric                     ProgramStateRef state, SVal V, QualType Ty);
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric   static ProgramStateRef setCStringLength(ProgramStateRef state,
2070b57cec5SDimitry Andric                                               const MemRegion *MR,
2080b57cec5SDimitry Andric                                               SVal strLength);
2090b57cec5SDimitry Andric   static SVal getCStringLengthForRegion(CheckerContext &C,
2100b57cec5SDimitry Andric                                         ProgramStateRef &state,
2110b57cec5SDimitry Andric                                         const Expr *Ex,
2120b57cec5SDimitry Andric                                         const MemRegion *MR,
2130b57cec5SDimitry Andric                                         bool hypothetical);
2140b57cec5SDimitry Andric   SVal getCStringLength(CheckerContext &C,
2150b57cec5SDimitry Andric                         ProgramStateRef &state,
2160b57cec5SDimitry Andric                         const Expr *Ex,
2170b57cec5SDimitry Andric                         SVal Buf,
2180b57cec5SDimitry Andric                         bool hypothetical = false) const;
2190b57cec5SDimitry Andric 
2200b57cec5SDimitry Andric   const StringLiteral *getCStringLiteral(CheckerContext &C,
2210b57cec5SDimitry Andric                                          ProgramStateRef &state,
2220b57cec5SDimitry Andric                                          const Expr *expr,
2230b57cec5SDimitry Andric                                          SVal val) const;
2240b57cec5SDimitry Andric 
2250b57cec5SDimitry Andric   static ProgramStateRef InvalidateBuffer(CheckerContext &C,
2260b57cec5SDimitry Andric                                           ProgramStateRef state,
2270b57cec5SDimitry Andric                                           const Expr *Ex, SVal V,
2280b57cec5SDimitry Andric                                           bool IsSourceBuffer,
2290b57cec5SDimitry Andric                                           const Expr *Size);
2300b57cec5SDimitry Andric 
2310b57cec5SDimitry Andric   static bool SummarizeRegion(raw_ostream &os, ASTContext &Ctx,
2320b57cec5SDimitry Andric                               const MemRegion *MR);
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric   static bool memsetAux(const Expr *DstBuffer, SVal CharE,
2350b57cec5SDimitry Andric                         const Expr *Size, CheckerContext &C,
2360b57cec5SDimitry Andric                         ProgramStateRef &State);
2370b57cec5SDimitry Andric 
2380b57cec5SDimitry Andric   // Re-usable checks
2395ffd83dbSDimitry Andric   ProgramStateRef checkNonNull(CheckerContext &C, ProgramStateRef State,
2405ffd83dbSDimitry Andric                                AnyArgExpr Arg, SVal l) const;
2415ffd83dbSDimitry Andric   ProgramStateRef CheckLocation(CheckerContext &C, ProgramStateRef state,
2425ffd83dbSDimitry Andric                                 AnyArgExpr Buffer, SVal Element,
2435ffd83dbSDimitry Andric                                 AccessKind Access) const;
2445ffd83dbSDimitry Andric   ProgramStateRef CheckBufferAccess(CheckerContext &C, ProgramStateRef State,
2455ffd83dbSDimitry Andric                                     AnyArgExpr Buffer, SizeArgExpr Size,
2465ffd83dbSDimitry Andric                                     AccessKind Access) const;
2475ffd83dbSDimitry Andric   ProgramStateRef CheckOverlap(CheckerContext &C, ProgramStateRef state,
2485ffd83dbSDimitry Andric                                SizeArgExpr Size, AnyArgExpr First,
2495ffd83dbSDimitry Andric                                AnyArgExpr Second) const;
2500b57cec5SDimitry Andric   void emitOverlapBug(CheckerContext &C,
2510b57cec5SDimitry Andric                       ProgramStateRef state,
2520b57cec5SDimitry Andric                       const Stmt *First,
2530b57cec5SDimitry Andric                       const Stmt *Second) const;
2540b57cec5SDimitry Andric 
2550b57cec5SDimitry Andric   void emitNullArgBug(CheckerContext &C, ProgramStateRef State, const Stmt *S,
2560b57cec5SDimitry Andric                       StringRef WarningMsg) const;
2570b57cec5SDimitry Andric   void emitOutOfBoundsBug(CheckerContext &C, ProgramStateRef State,
2580b57cec5SDimitry Andric                           const Stmt *S, StringRef WarningMsg) const;
2590b57cec5SDimitry Andric   void emitNotCStringBug(CheckerContext &C, ProgramStateRef State,
2600b57cec5SDimitry Andric                          const Stmt *S, StringRef WarningMsg) const;
2610b57cec5SDimitry Andric   void emitAdditionOverflowBug(CheckerContext &C, ProgramStateRef State) const;
26281ad6265SDimitry Andric   void emitUninitializedReadBug(CheckerContext &C, ProgramStateRef State,
26381ad6265SDimitry Andric                              const Expr *E) const;
2640b57cec5SDimitry Andric   ProgramStateRef checkAdditionOverflow(CheckerContext &C,
2650b57cec5SDimitry Andric                                             ProgramStateRef state,
2660b57cec5SDimitry Andric                                             NonLoc left,
2670b57cec5SDimitry Andric                                             NonLoc right) const;
2680b57cec5SDimitry Andric 
2690b57cec5SDimitry Andric   // Return true if the destination buffer of the copy function may be in bound.
2700b57cec5SDimitry Andric   // Expects SVal of Size to be positive and unsigned.
2710b57cec5SDimitry Andric   // Expects SVal of FirstBuf to be a FieldRegion.
2720b57cec5SDimitry Andric   static bool IsFirstBufInBound(CheckerContext &C,
2730b57cec5SDimitry Andric                                 ProgramStateRef state,
2740b57cec5SDimitry Andric                                 const Expr *FirstBuf,
2750b57cec5SDimitry Andric                                 const Expr *Size);
2760b57cec5SDimitry Andric };
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric } //end anonymous namespace
2790b57cec5SDimitry Andric 
2800b57cec5SDimitry Andric REGISTER_MAP_WITH_PROGRAMSTATE(CStringLength, const MemRegion *, SVal)
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2830b57cec5SDimitry Andric // Individual checks and utility methods.
2840b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2850b57cec5SDimitry Andric 
2860b57cec5SDimitry Andric std::pair<ProgramStateRef , ProgramStateRef >
2870b57cec5SDimitry Andric CStringChecker::assumeZero(CheckerContext &C, ProgramStateRef state, SVal V,
2880b57cec5SDimitry Andric                            QualType Ty) {
2890b57cec5SDimitry Andric   Optional<DefinedSVal> val = V.getAs<DefinedSVal>();
2900b57cec5SDimitry Andric   if (!val)
2910b57cec5SDimitry Andric     return std::pair<ProgramStateRef , ProgramStateRef >(state, state);
2920b57cec5SDimitry Andric 
2930b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
2940b57cec5SDimitry Andric   DefinedOrUnknownSVal zero = svalBuilder.makeZeroVal(Ty);
2950b57cec5SDimitry Andric   return state->assume(svalBuilder.evalEQ(state, *val, zero));
2960b57cec5SDimitry Andric }
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric ProgramStateRef CStringChecker::checkNonNull(CheckerContext &C,
2995ffd83dbSDimitry Andric                                              ProgramStateRef State,
3005ffd83dbSDimitry Andric                                              AnyArgExpr Arg, SVal l) const {
3010b57cec5SDimitry Andric   // If a previous check has failed, propagate the failure.
3025ffd83dbSDimitry Andric   if (!State)
3030b57cec5SDimitry Andric     return nullptr;
3040b57cec5SDimitry Andric 
3050b57cec5SDimitry Andric   ProgramStateRef stateNull, stateNonNull;
3065ffd83dbSDimitry Andric   std::tie(stateNull, stateNonNull) =
3075ffd83dbSDimitry Andric       assumeZero(C, State, l, Arg.Expression->getType());
3080b57cec5SDimitry Andric 
3090b57cec5SDimitry Andric   if (stateNull && !stateNonNull) {
3100b57cec5SDimitry Andric     if (Filter.CheckCStringNullArg) {
3110b57cec5SDimitry Andric       SmallString<80> buf;
312a7dea167SDimitry Andric       llvm::raw_svector_ostream OS(buf);
3130b57cec5SDimitry Andric       assert(CurrentFunctionDescription);
3145ffd83dbSDimitry Andric       OS << "Null pointer passed as " << (Arg.ArgumentIndex + 1)
3155ffd83dbSDimitry Andric          << llvm::getOrdinalSuffix(Arg.ArgumentIndex + 1) << " argument to "
316480093f4SDimitry Andric          << CurrentFunctionDescription;
3170b57cec5SDimitry Andric 
3185ffd83dbSDimitry Andric       emitNullArgBug(C, stateNull, Arg.Expression, OS.str());
3190b57cec5SDimitry Andric     }
3200b57cec5SDimitry Andric     return nullptr;
3210b57cec5SDimitry Andric   }
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric   // From here on, assume that the value is non-null.
3240b57cec5SDimitry Andric   assert(stateNonNull);
3250b57cec5SDimitry Andric   return stateNonNull;
3260b57cec5SDimitry Andric }
3270b57cec5SDimitry Andric 
3280b57cec5SDimitry Andric // FIXME: This was originally copied from ArrayBoundChecker.cpp. Refactor?
3290b57cec5SDimitry Andric ProgramStateRef CStringChecker::CheckLocation(CheckerContext &C,
3300b57cec5SDimitry Andric                                               ProgramStateRef state,
3315ffd83dbSDimitry Andric                                               AnyArgExpr Buffer, SVal Element,
3325ffd83dbSDimitry Andric                                               AccessKind Access) const {
3335ffd83dbSDimitry Andric 
3340b57cec5SDimitry Andric   // If a previous check has failed, propagate the failure.
3350b57cec5SDimitry Andric   if (!state)
3360b57cec5SDimitry Andric     return nullptr;
3370b57cec5SDimitry Andric 
3380b57cec5SDimitry Andric   // Check for out of bound array element access.
3395ffd83dbSDimitry Andric   const MemRegion *R = Element.getAsRegion();
3400b57cec5SDimitry Andric   if (!R)
3410b57cec5SDimitry Andric     return state;
3420b57cec5SDimitry Andric 
3435ffd83dbSDimitry Andric   const auto *ER = dyn_cast<ElementRegion>(R);
3440b57cec5SDimitry Andric   if (!ER)
3450b57cec5SDimitry Andric     return state;
3460b57cec5SDimitry Andric 
3470b57cec5SDimitry Andric   if (ER->getValueType() != C.getASTContext().CharTy)
3480b57cec5SDimitry Andric     return state;
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric   // Get the size of the array.
3515ffd83dbSDimitry Andric   const auto *superReg = cast<SubRegion>(ER->getSuperRegion());
3525ffd83dbSDimitry Andric   DefinedOrUnknownSVal Size =
353fe6060f1SDimitry Andric       getDynamicExtent(state, superReg, C.getSValBuilder());
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric   // Get the index of the accessed element.
3560b57cec5SDimitry Andric   DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>();
3570b57cec5SDimitry Andric 
35881ad6265SDimitry Andric   ProgramStateRef StInBound, StOutBound;
35981ad6265SDimitry Andric   std::tie(StInBound, StOutBound) = state->assumeInBoundDual(Idx, Size);
3600b57cec5SDimitry Andric   if (StOutBound && !StInBound) {
3610b57cec5SDimitry Andric     // These checks are either enabled by the CString out-of-bounds checker
3620b57cec5SDimitry Andric     // explicitly or implicitly by the Malloc checker.
3630b57cec5SDimitry Andric     // In the latter case we only do modeling but do not emit warning.
3640b57cec5SDimitry Andric     if (!Filter.CheckCStringOutOfBounds)
3650b57cec5SDimitry Andric       return nullptr;
3660b57cec5SDimitry Andric 
3675ffd83dbSDimitry Andric     // Emit a bug report.
3685ffd83dbSDimitry Andric     ErrorMessage Message =
3695ffd83dbSDimitry Andric         createOutOfBoundErrorMsg(CurrentFunctionDescription, Access);
3705ffd83dbSDimitry Andric     emitOutOfBoundsBug(C, StOutBound, Buffer.Expression, Message);
3710b57cec5SDimitry Andric     return nullptr;
3720b57cec5SDimitry Andric   }
3730b57cec5SDimitry Andric 
37481ad6265SDimitry Andric   // Ensure that we wouldn't read uninitialized value.
37581ad6265SDimitry Andric   if (Access == AccessKind::read) {
37681ad6265SDimitry Andric     if (Filter.CheckCStringUninitializedRead &&
37781ad6265SDimitry Andric         StInBound->getSVal(ER).isUndef()) {
37881ad6265SDimitry Andric       emitUninitializedReadBug(C, StInBound, Buffer.Expression);
37981ad6265SDimitry Andric       return nullptr;
38081ad6265SDimitry Andric     }
38181ad6265SDimitry Andric   }
38281ad6265SDimitry Andric 
3830b57cec5SDimitry Andric   // Array bound check succeeded.  From this point forward the array bound
3840b57cec5SDimitry Andric   // should always succeed.
3850b57cec5SDimitry Andric   return StInBound;
3860b57cec5SDimitry Andric }
3870b57cec5SDimitry Andric 
3880b57cec5SDimitry Andric ProgramStateRef CStringChecker::CheckBufferAccess(CheckerContext &C,
3895ffd83dbSDimitry Andric                                                   ProgramStateRef State,
3905ffd83dbSDimitry Andric                                                   AnyArgExpr Buffer,
3915ffd83dbSDimitry Andric                                                   SizeArgExpr Size,
3925ffd83dbSDimitry Andric                                                   AccessKind Access) const {
3930b57cec5SDimitry Andric   // If a previous check has failed, propagate the failure.
3945ffd83dbSDimitry Andric   if (!State)
3950b57cec5SDimitry Andric     return nullptr;
3960b57cec5SDimitry Andric 
3970b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
3980b57cec5SDimitry Andric   ASTContext &Ctx = svalBuilder.getContext();
3990b57cec5SDimitry Andric 
4005ffd83dbSDimitry Andric   QualType SizeTy = Size.Expression->getType();
4010b57cec5SDimitry Andric   QualType PtrTy = Ctx.getPointerType(Ctx.CharTy);
4020b57cec5SDimitry Andric 
4030b57cec5SDimitry Andric   // Check that the first buffer is non-null.
4045ffd83dbSDimitry Andric   SVal BufVal = C.getSVal(Buffer.Expression);
4055ffd83dbSDimitry Andric   State = checkNonNull(C, State, Buffer, BufVal);
4065ffd83dbSDimitry Andric   if (!State)
4070b57cec5SDimitry Andric     return nullptr;
4080b57cec5SDimitry Andric 
4090b57cec5SDimitry Andric   // If out-of-bounds checking is turned off, skip the rest.
4100b57cec5SDimitry Andric   if (!Filter.CheckCStringOutOfBounds)
4115ffd83dbSDimitry Andric     return State;
4120b57cec5SDimitry Andric 
4130b57cec5SDimitry Andric   // Get the access length and make sure it is known.
4140b57cec5SDimitry Andric   // FIXME: This assumes the caller has already checked that the access length
4150b57cec5SDimitry Andric   // is positive. And that it's unsigned.
4165ffd83dbSDimitry Andric   SVal LengthVal = C.getSVal(Size.Expression);
4170b57cec5SDimitry Andric   Optional<NonLoc> Length = LengthVal.getAs<NonLoc>();
4180b57cec5SDimitry Andric   if (!Length)
4195ffd83dbSDimitry Andric     return State;
4200b57cec5SDimitry Andric 
4210b57cec5SDimitry Andric   // Compute the offset of the last element to be accessed: size-1.
4225ffd83dbSDimitry Andric   NonLoc One = svalBuilder.makeIntVal(1, SizeTy).castAs<NonLoc>();
4235ffd83dbSDimitry Andric   SVal Offset = svalBuilder.evalBinOpNN(State, BO_Sub, *Length, One, SizeTy);
4240b57cec5SDimitry Andric   if (Offset.isUnknown())
4250b57cec5SDimitry Andric     return nullptr;
4260b57cec5SDimitry Andric   NonLoc LastOffset = Offset.castAs<NonLoc>();
4270b57cec5SDimitry Andric 
4280b57cec5SDimitry Andric   // Check that the first buffer is sufficiently long.
4295ffd83dbSDimitry Andric   SVal BufStart =
4305ffd83dbSDimitry Andric       svalBuilder.evalCast(BufVal, PtrTy, Buffer.Expression->getType());
4310b57cec5SDimitry Andric   if (Optional<Loc> BufLoc = BufStart.getAs<Loc>()) {
4320b57cec5SDimitry Andric 
4335ffd83dbSDimitry Andric     SVal BufEnd =
4345ffd83dbSDimitry Andric         svalBuilder.evalBinOpLN(State, BO_Add, *BufLoc, LastOffset, PtrTy);
4355ffd83dbSDimitry Andric     State = CheckLocation(C, State, Buffer, BufEnd, Access);
4360b57cec5SDimitry Andric 
4370b57cec5SDimitry Andric     // If the buffer isn't large enough, abort.
4385ffd83dbSDimitry Andric     if (!State)
4390b57cec5SDimitry Andric       return nullptr;
4400b57cec5SDimitry Andric   }
4410b57cec5SDimitry Andric 
4420b57cec5SDimitry Andric   // Large enough or not, return this state!
4435ffd83dbSDimitry Andric   return State;
4440b57cec5SDimitry Andric }
4450b57cec5SDimitry Andric 
4460b57cec5SDimitry Andric ProgramStateRef CStringChecker::CheckOverlap(CheckerContext &C,
4470b57cec5SDimitry Andric                                              ProgramStateRef state,
4485ffd83dbSDimitry Andric                                              SizeArgExpr Size, AnyArgExpr First,
4495ffd83dbSDimitry Andric                                              AnyArgExpr Second) const {
4500b57cec5SDimitry Andric   if (!Filter.CheckCStringBufferOverlap)
4510b57cec5SDimitry Andric     return state;
4520b57cec5SDimitry Andric 
4530b57cec5SDimitry Andric   // Do a simple check for overlap: if the two arguments are from the same
4540b57cec5SDimitry Andric   // buffer, see if the end of the first is greater than the start of the second
4550b57cec5SDimitry Andric   // or vice versa.
4560b57cec5SDimitry Andric 
4570b57cec5SDimitry Andric   // If a previous check has failed, propagate the failure.
4580b57cec5SDimitry Andric   if (!state)
4590b57cec5SDimitry Andric     return nullptr;
4600b57cec5SDimitry Andric 
4610b57cec5SDimitry Andric   ProgramStateRef stateTrue, stateFalse;
4620b57cec5SDimitry Andric 
46381ad6265SDimitry Andric   // Assume different address spaces cannot overlap.
46481ad6265SDimitry Andric   if (First.Expression->getType()->getPointeeType().getAddressSpace() !=
46581ad6265SDimitry Andric       Second.Expression->getType()->getPointeeType().getAddressSpace())
46681ad6265SDimitry Andric     return state;
46781ad6265SDimitry Andric 
4680b57cec5SDimitry Andric   // Get the buffer values and make sure they're known locations.
4690b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
4705ffd83dbSDimitry Andric   SVal firstVal = state->getSVal(First.Expression, LCtx);
4715ffd83dbSDimitry Andric   SVal secondVal = state->getSVal(Second.Expression, LCtx);
4720b57cec5SDimitry Andric 
4730b57cec5SDimitry Andric   Optional<Loc> firstLoc = firstVal.getAs<Loc>();
4740b57cec5SDimitry Andric   if (!firstLoc)
4750b57cec5SDimitry Andric     return state;
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric   Optional<Loc> secondLoc = secondVal.getAs<Loc>();
4780b57cec5SDimitry Andric   if (!secondLoc)
4790b57cec5SDimitry Andric     return state;
4800b57cec5SDimitry Andric 
4810b57cec5SDimitry Andric   // Are the two values the same?
4820b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
4830b57cec5SDimitry Andric   std::tie(stateTrue, stateFalse) =
4840b57cec5SDimitry Andric       state->assume(svalBuilder.evalEQ(state, *firstLoc, *secondLoc));
4850b57cec5SDimitry Andric 
4860b57cec5SDimitry Andric   if (stateTrue && !stateFalse) {
4870b57cec5SDimitry Andric     // If the values are known to be equal, that's automatically an overlap.
4885ffd83dbSDimitry Andric     emitOverlapBug(C, stateTrue, First.Expression, Second.Expression);
4890b57cec5SDimitry Andric     return nullptr;
4900b57cec5SDimitry Andric   }
4910b57cec5SDimitry Andric 
4920b57cec5SDimitry Andric   // assume the two expressions are not equal.
4930b57cec5SDimitry Andric   assert(stateFalse);
4940b57cec5SDimitry Andric   state = stateFalse;
4950b57cec5SDimitry Andric 
4960b57cec5SDimitry Andric   // Which value comes first?
4970b57cec5SDimitry Andric   QualType cmpTy = svalBuilder.getConditionType();
4985ffd83dbSDimitry Andric   SVal reverse =
4995ffd83dbSDimitry Andric       svalBuilder.evalBinOpLL(state, BO_GT, *firstLoc, *secondLoc, cmpTy);
5000b57cec5SDimitry Andric   Optional<DefinedOrUnknownSVal> reverseTest =
5010b57cec5SDimitry Andric       reverse.getAs<DefinedOrUnknownSVal>();
5020b57cec5SDimitry Andric   if (!reverseTest)
5030b57cec5SDimitry Andric     return state;
5040b57cec5SDimitry Andric 
5050b57cec5SDimitry Andric   std::tie(stateTrue, stateFalse) = state->assume(*reverseTest);
5060b57cec5SDimitry Andric   if (stateTrue) {
5070b57cec5SDimitry Andric     if (stateFalse) {
5080b57cec5SDimitry Andric       // If we don't know which one comes first, we can't perform this test.
5090b57cec5SDimitry Andric       return state;
5100b57cec5SDimitry Andric     } else {
5110b57cec5SDimitry Andric       // Switch the values so that firstVal is before secondVal.
5120b57cec5SDimitry Andric       std::swap(firstLoc, secondLoc);
5130b57cec5SDimitry Andric 
5140b57cec5SDimitry Andric       // Switch the Exprs as well, so that they still correspond.
5150b57cec5SDimitry Andric       std::swap(First, Second);
5160b57cec5SDimitry Andric     }
5170b57cec5SDimitry Andric   }
5180b57cec5SDimitry Andric 
5190b57cec5SDimitry Andric   // Get the length, and make sure it too is known.
5205ffd83dbSDimitry Andric   SVal LengthVal = state->getSVal(Size.Expression, LCtx);
5210b57cec5SDimitry Andric   Optional<NonLoc> Length = LengthVal.getAs<NonLoc>();
5220b57cec5SDimitry Andric   if (!Length)
5230b57cec5SDimitry Andric     return state;
5240b57cec5SDimitry Andric 
5250b57cec5SDimitry Andric   // Convert the first buffer's start address to char*.
5260b57cec5SDimitry Andric   // Bail out if the cast fails.
5270b57cec5SDimitry Andric   ASTContext &Ctx = svalBuilder.getContext();
5280b57cec5SDimitry Andric   QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy);
5295ffd83dbSDimitry Andric   SVal FirstStart =
5305ffd83dbSDimitry Andric       svalBuilder.evalCast(*firstLoc, CharPtrTy, First.Expression->getType());
5310b57cec5SDimitry Andric   Optional<Loc> FirstStartLoc = FirstStart.getAs<Loc>();
5320b57cec5SDimitry Andric   if (!FirstStartLoc)
5330b57cec5SDimitry Andric     return state;
5340b57cec5SDimitry Andric 
5350b57cec5SDimitry Andric   // Compute the end of the first buffer. Bail out if THAT fails.
5365ffd83dbSDimitry Andric   SVal FirstEnd = svalBuilder.evalBinOpLN(state, BO_Add, *FirstStartLoc,
5375ffd83dbSDimitry Andric                                           *Length, CharPtrTy);
5380b57cec5SDimitry Andric   Optional<Loc> FirstEndLoc = FirstEnd.getAs<Loc>();
5390b57cec5SDimitry Andric   if (!FirstEndLoc)
5400b57cec5SDimitry Andric     return state;
5410b57cec5SDimitry Andric 
5420b57cec5SDimitry Andric   // Is the end of the first buffer past the start of the second buffer?
5435ffd83dbSDimitry Andric   SVal Overlap =
5445ffd83dbSDimitry Andric       svalBuilder.evalBinOpLL(state, BO_GT, *FirstEndLoc, *secondLoc, cmpTy);
5450b57cec5SDimitry Andric   Optional<DefinedOrUnknownSVal> OverlapTest =
5460b57cec5SDimitry Andric       Overlap.getAs<DefinedOrUnknownSVal>();
5470b57cec5SDimitry Andric   if (!OverlapTest)
5480b57cec5SDimitry Andric     return state;
5490b57cec5SDimitry Andric 
5500b57cec5SDimitry Andric   std::tie(stateTrue, stateFalse) = state->assume(*OverlapTest);
5510b57cec5SDimitry Andric 
5520b57cec5SDimitry Andric   if (stateTrue && !stateFalse) {
5530b57cec5SDimitry Andric     // Overlap!
5545ffd83dbSDimitry Andric     emitOverlapBug(C, stateTrue, First.Expression, Second.Expression);
5550b57cec5SDimitry Andric     return nullptr;
5560b57cec5SDimitry Andric   }
5570b57cec5SDimitry Andric 
5580b57cec5SDimitry Andric   // assume the two expressions don't overlap.
5590b57cec5SDimitry Andric   assert(stateFalse);
5600b57cec5SDimitry Andric   return stateFalse;
5610b57cec5SDimitry Andric }
5620b57cec5SDimitry Andric 
5630b57cec5SDimitry Andric void CStringChecker::emitOverlapBug(CheckerContext &C, ProgramStateRef state,
5640b57cec5SDimitry Andric                                   const Stmt *First, const Stmt *Second) const {
5650b57cec5SDimitry Andric   ExplodedNode *N = C.generateErrorNode(state);
5660b57cec5SDimitry Andric   if (!N)
5670b57cec5SDimitry Andric     return;
5680b57cec5SDimitry Andric 
5690b57cec5SDimitry Andric   if (!BT_Overlap)
5700b57cec5SDimitry Andric     BT_Overlap.reset(new BugType(Filter.CheckNameCStringBufferOverlap,
5710b57cec5SDimitry Andric                                  categories::UnixAPI, "Improper arguments"));
5720b57cec5SDimitry Andric 
5730b57cec5SDimitry Andric   // Generate a report for this bug.
574a7dea167SDimitry Andric   auto report = std::make_unique<PathSensitiveBugReport>(
5750b57cec5SDimitry Andric       *BT_Overlap, "Arguments must not be overlapping buffers", N);
5760b57cec5SDimitry Andric   report->addRange(First->getSourceRange());
5770b57cec5SDimitry Andric   report->addRange(Second->getSourceRange());
5780b57cec5SDimitry Andric 
5790b57cec5SDimitry Andric   C.emitReport(std::move(report));
5800b57cec5SDimitry Andric }
5810b57cec5SDimitry Andric 
5820b57cec5SDimitry Andric void CStringChecker::emitNullArgBug(CheckerContext &C, ProgramStateRef State,
5830b57cec5SDimitry Andric                                     const Stmt *S, StringRef WarningMsg) const {
5840b57cec5SDimitry Andric   if (ExplodedNode *N = C.generateErrorNode(State)) {
5850b57cec5SDimitry Andric     if (!BT_Null)
5860b57cec5SDimitry Andric       BT_Null.reset(new BuiltinBug(
5870b57cec5SDimitry Andric           Filter.CheckNameCStringNullArg, categories::UnixAPI,
5880b57cec5SDimitry Andric           "Null pointer argument in call to byte string function"));
5890b57cec5SDimitry Andric 
5900b57cec5SDimitry Andric     BuiltinBug *BT = static_cast<BuiltinBug *>(BT_Null.get());
591a7dea167SDimitry Andric     auto Report = std::make_unique<PathSensitiveBugReport>(*BT, WarningMsg, N);
5920b57cec5SDimitry Andric     Report->addRange(S->getSourceRange());
5930b57cec5SDimitry Andric     if (const auto *Ex = dyn_cast<Expr>(S))
5940b57cec5SDimitry Andric       bugreporter::trackExpressionValue(N, Ex, *Report);
5950b57cec5SDimitry Andric     C.emitReport(std::move(Report));
5960b57cec5SDimitry Andric   }
5970b57cec5SDimitry Andric }
5980b57cec5SDimitry Andric 
59981ad6265SDimitry Andric void CStringChecker::emitUninitializedReadBug(CheckerContext &C,
60081ad6265SDimitry Andric                                               ProgramStateRef State,
60181ad6265SDimitry Andric                                               const Expr *E) const {
60281ad6265SDimitry Andric   if (ExplodedNode *N = C.generateErrorNode(State)) {
60381ad6265SDimitry Andric     const char *Msg =
60481ad6265SDimitry Andric         "Bytes string function accesses uninitialized/garbage values";
60581ad6265SDimitry Andric     if (!BT_UninitRead)
60681ad6265SDimitry Andric       BT_UninitRead.reset(
60781ad6265SDimitry Andric           new BuiltinBug(Filter.CheckNameCStringUninitializedRead,
60881ad6265SDimitry Andric                          "Accessing unitialized/garbage values", Msg));
60981ad6265SDimitry Andric 
61081ad6265SDimitry Andric     BuiltinBug *BT = static_cast<BuiltinBug *>(BT_UninitRead.get());
61181ad6265SDimitry Andric 
61281ad6265SDimitry Andric     auto Report = std::make_unique<PathSensitiveBugReport>(*BT, Msg, N);
61381ad6265SDimitry Andric     Report->addRange(E->getSourceRange());
61481ad6265SDimitry Andric     bugreporter::trackExpressionValue(N, E, *Report);
61581ad6265SDimitry Andric     C.emitReport(std::move(Report));
61681ad6265SDimitry Andric   }
61781ad6265SDimitry Andric }
61881ad6265SDimitry Andric 
6190b57cec5SDimitry Andric void CStringChecker::emitOutOfBoundsBug(CheckerContext &C,
6200b57cec5SDimitry Andric                                         ProgramStateRef State, const Stmt *S,
6210b57cec5SDimitry Andric                                         StringRef WarningMsg) const {
6220b57cec5SDimitry Andric   if (ExplodedNode *N = C.generateErrorNode(State)) {
6230b57cec5SDimitry Andric     if (!BT_Bounds)
6240b57cec5SDimitry Andric       BT_Bounds.reset(new BuiltinBug(
6250b57cec5SDimitry Andric           Filter.CheckCStringOutOfBounds ? Filter.CheckNameCStringOutOfBounds
6260b57cec5SDimitry Andric                                          : Filter.CheckNameCStringNullArg,
6270b57cec5SDimitry Andric           "Out-of-bound array access",
6280b57cec5SDimitry Andric           "Byte string function accesses out-of-bound array element"));
6290b57cec5SDimitry Andric 
6300b57cec5SDimitry Andric     BuiltinBug *BT = static_cast<BuiltinBug *>(BT_Bounds.get());
6310b57cec5SDimitry Andric 
6320b57cec5SDimitry Andric     // FIXME: It would be nice to eventually make this diagnostic more clear,
6330b57cec5SDimitry Andric     // e.g., by referencing the original declaration or by saying *why* this
6340b57cec5SDimitry Andric     // reference is outside the range.
635a7dea167SDimitry Andric     auto Report = std::make_unique<PathSensitiveBugReport>(*BT, WarningMsg, N);
6360b57cec5SDimitry Andric     Report->addRange(S->getSourceRange());
6370b57cec5SDimitry Andric     C.emitReport(std::move(Report));
6380b57cec5SDimitry Andric   }
6390b57cec5SDimitry Andric }
6400b57cec5SDimitry Andric 
6410b57cec5SDimitry Andric void CStringChecker::emitNotCStringBug(CheckerContext &C, ProgramStateRef State,
6420b57cec5SDimitry Andric                                        const Stmt *S,
6430b57cec5SDimitry Andric                                        StringRef WarningMsg) const {
6440b57cec5SDimitry Andric   if (ExplodedNode *N = C.generateNonFatalErrorNode(State)) {
6450b57cec5SDimitry Andric     if (!BT_NotCString)
6460b57cec5SDimitry Andric       BT_NotCString.reset(new BuiltinBug(
6470b57cec5SDimitry Andric           Filter.CheckNameCStringNotNullTerm, categories::UnixAPI,
6480b57cec5SDimitry Andric           "Argument is not a null-terminated string."));
6490b57cec5SDimitry Andric 
650a7dea167SDimitry Andric     auto Report =
651a7dea167SDimitry Andric         std::make_unique<PathSensitiveBugReport>(*BT_NotCString, WarningMsg, N);
6520b57cec5SDimitry Andric 
6530b57cec5SDimitry Andric     Report->addRange(S->getSourceRange());
6540b57cec5SDimitry Andric     C.emitReport(std::move(Report));
6550b57cec5SDimitry Andric   }
6560b57cec5SDimitry Andric }
6570b57cec5SDimitry Andric 
6580b57cec5SDimitry Andric void CStringChecker::emitAdditionOverflowBug(CheckerContext &C,
6590b57cec5SDimitry Andric                                              ProgramStateRef State) const {
6600b57cec5SDimitry Andric   if (ExplodedNode *N = C.generateErrorNode(State)) {
66181ad6265SDimitry Andric     if (!BT_AdditionOverflow)
66281ad6265SDimitry Andric       BT_AdditionOverflow.reset(
6630b57cec5SDimitry Andric           new BuiltinBug(Filter.CheckNameCStringOutOfBounds, "API",
6640b57cec5SDimitry Andric                          "Sum of expressions causes overflow."));
6650b57cec5SDimitry Andric 
6660b57cec5SDimitry Andric     // This isn't a great error message, but this should never occur in real
6670b57cec5SDimitry Andric     // code anyway -- you'd have to create a buffer longer than a size_t can
6680b57cec5SDimitry Andric     // represent, which is sort of a contradiction.
6690b57cec5SDimitry Andric     const char *WarningMsg =
6700b57cec5SDimitry Andric         "This expression will create a string whose length is too big to "
6710b57cec5SDimitry Andric         "be represented as a size_t";
6720b57cec5SDimitry Andric 
67381ad6265SDimitry Andric     auto Report = std::make_unique<PathSensitiveBugReport>(*BT_AdditionOverflow,
67481ad6265SDimitry Andric                                                            WarningMsg, N);
6750b57cec5SDimitry Andric     C.emitReport(std::move(Report));
6760b57cec5SDimitry Andric   }
6770b57cec5SDimitry Andric }
6780b57cec5SDimitry Andric 
6790b57cec5SDimitry Andric ProgramStateRef CStringChecker::checkAdditionOverflow(CheckerContext &C,
6800b57cec5SDimitry Andric                                                      ProgramStateRef state,
6810b57cec5SDimitry Andric                                                      NonLoc left,
6820b57cec5SDimitry Andric                                                      NonLoc right) const {
6830b57cec5SDimitry Andric   // If out-of-bounds checking is turned off, skip the rest.
6840b57cec5SDimitry Andric   if (!Filter.CheckCStringOutOfBounds)
6850b57cec5SDimitry Andric     return state;
6860b57cec5SDimitry Andric 
6870b57cec5SDimitry Andric   // If a previous check has failed, propagate the failure.
6880b57cec5SDimitry Andric   if (!state)
6890b57cec5SDimitry Andric     return nullptr;
6900b57cec5SDimitry Andric 
6910b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
6920b57cec5SDimitry Andric   BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
6930b57cec5SDimitry Andric 
6940b57cec5SDimitry Andric   QualType sizeTy = svalBuilder.getContext().getSizeType();
6950b57cec5SDimitry Andric   const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy);
6960b57cec5SDimitry Andric   NonLoc maxVal = svalBuilder.makeIntVal(maxValInt);
6970b57cec5SDimitry Andric 
6980b57cec5SDimitry Andric   SVal maxMinusRight;
69981ad6265SDimitry Andric   if (isa<nonloc::ConcreteInt>(right)) {
7000b57cec5SDimitry Andric     maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, right,
7010b57cec5SDimitry Andric                                                  sizeTy);
7020b57cec5SDimitry Andric   } else {
7030b57cec5SDimitry Andric     // Try switching the operands. (The order of these two assignments is
7040b57cec5SDimitry Andric     // important!)
7050b57cec5SDimitry Andric     maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, left,
7060b57cec5SDimitry Andric                                             sizeTy);
7070b57cec5SDimitry Andric     left = right;
7080b57cec5SDimitry Andric   }
7090b57cec5SDimitry Andric 
7100b57cec5SDimitry Andric   if (Optional<NonLoc> maxMinusRightNL = maxMinusRight.getAs<NonLoc>()) {
7110b57cec5SDimitry Andric     QualType cmpTy = svalBuilder.getConditionType();
7120b57cec5SDimitry Andric     // If left > max - right, we have an overflow.
7130b57cec5SDimitry Andric     SVal willOverflow = svalBuilder.evalBinOpNN(state, BO_GT, left,
7140b57cec5SDimitry Andric                                                 *maxMinusRightNL, cmpTy);
7150b57cec5SDimitry Andric 
7160b57cec5SDimitry Andric     ProgramStateRef stateOverflow, stateOkay;
7170b57cec5SDimitry Andric     std::tie(stateOverflow, stateOkay) =
7180b57cec5SDimitry Andric       state->assume(willOverflow.castAs<DefinedOrUnknownSVal>());
7190b57cec5SDimitry Andric 
7200b57cec5SDimitry Andric     if (stateOverflow && !stateOkay) {
7210b57cec5SDimitry Andric       // We have an overflow. Emit a bug report.
7220b57cec5SDimitry Andric       emitAdditionOverflowBug(C, stateOverflow);
7230b57cec5SDimitry Andric       return nullptr;
7240b57cec5SDimitry Andric     }
7250b57cec5SDimitry Andric 
7260b57cec5SDimitry Andric     // From now on, assume an overflow didn't occur.
7270b57cec5SDimitry Andric     assert(stateOkay);
7280b57cec5SDimitry Andric     state = stateOkay;
7290b57cec5SDimitry Andric   }
7300b57cec5SDimitry Andric 
7310b57cec5SDimitry Andric   return state;
7320b57cec5SDimitry Andric }
7330b57cec5SDimitry Andric 
7340b57cec5SDimitry Andric ProgramStateRef CStringChecker::setCStringLength(ProgramStateRef state,
7350b57cec5SDimitry Andric                                                 const MemRegion *MR,
7360b57cec5SDimitry Andric                                                 SVal strLength) {
7370b57cec5SDimitry Andric   assert(!strLength.isUndef() && "Attempt to set an undefined string length");
7380b57cec5SDimitry Andric 
7390b57cec5SDimitry Andric   MR = MR->StripCasts();
7400b57cec5SDimitry Andric 
7410b57cec5SDimitry Andric   switch (MR->getKind()) {
7420b57cec5SDimitry Andric   case MemRegion::StringRegionKind:
7430b57cec5SDimitry Andric     // FIXME: This can happen if we strcpy() into a string region. This is
7440b57cec5SDimitry Andric     // undefined [C99 6.4.5p6], but we should still warn about it.
7450b57cec5SDimitry Andric     return state;
7460b57cec5SDimitry Andric 
7470b57cec5SDimitry Andric   case MemRegion::SymbolicRegionKind:
7480b57cec5SDimitry Andric   case MemRegion::AllocaRegionKind:
7495ffd83dbSDimitry Andric   case MemRegion::NonParamVarRegionKind:
7505ffd83dbSDimitry Andric   case MemRegion::ParamVarRegionKind:
7510b57cec5SDimitry Andric   case MemRegion::FieldRegionKind:
7520b57cec5SDimitry Andric   case MemRegion::ObjCIvarRegionKind:
7530b57cec5SDimitry Andric     // These are the types we can currently track string lengths for.
7540b57cec5SDimitry Andric     break;
7550b57cec5SDimitry Andric 
7560b57cec5SDimitry Andric   case MemRegion::ElementRegionKind:
7570b57cec5SDimitry Andric     // FIXME: Handle element regions by upper-bounding the parent region's
7580b57cec5SDimitry Andric     // string length.
7590b57cec5SDimitry Andric     return state;
7600b57cec5SDimitry Andric 
7610b57cec5SDimitry Andric   default:
7620b57cec5SDimitry Andric     // Other regions (mostly non-data) can't have a reliable C string length.
7630b57cec5SDimitry Andric     // For now, just ignore the change.
7640b57cec5SDimitry Andric     // FIXME: These are rare but not impossible. We should output some kind of
7650b57cec5SDimitry Andric     // warning for things like strcpy((char[]){'a', 0}, "b");
7660b57cec5SDimitry Andric     return state;
7670b57cec5SDimitry Andric   }
7680b57cec5SDimitry Andric 
7690b57cec5SDimitry Andric   if (strLength.isUnknown())
7700b57cec5SDimitry Andric     return state->remove<CStringLength>(MR);
7710b57cec5SDimitry Andric 
7720b57cec5SDimitry Andric   return state->set<CStringLength>(MR, strLength);
7730b57cec5SDimitry Andric }
7740b57cec5SDimitry Andric 
7750b57cec5SDimitry Andric SVal CStringChecker::getCStringLengthForRegion(CheckerContext &C,
7760b57cec5SDimitry Andric                                                ProgramStateRef &state,
7770b57cec5SDimitry Andric                                                const Expr *Ex,
7780b57cec5SDimitry Andric                                                const MemRegion *MR,
7790b57cec5SDimitry Andric                                                bool hypothetical) {
7800b57cec5SDimitry Andric   if (!hypothetical) {
7810b57cec5SDimitry Andric     // If there's a recorded length, go ahead and return it.
7820b57cec5SDimitry Andric     const SVal *Recorded = state->get<CStringLength>(MR);
7830b57cec5SDimitry Andric     if (Recorded)
7840b57cec5SDimitry Andric       return *Recorded;
7850b57cec5SDimitry Andric   }
7860b57cec5SDimitry Andric 
7870b57cec5SDimitry Andric   // Otherwise, get a new symbol and update the state.
7880b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
7890b57cec5SDimitry Andric   QualType sizeTy = svalBuilder.getContext().getSizeType();
7900b57cec5SDimitry Andric   SVal strLength = svalBuilder.getMetadataSymbolVal(CStringChecker::getTag(),
7910b57cec5SDimitry Andric                                                     MR, Ex, sizeTy,
7920b57cec5SDimitry Andric                                                     C.getLocationContext(),
7930b57cec5SDimitry Andric                                                     C.blockCount());
7940b57cec5SDimitry Andric 
7950b57cec5SDimitry Andric   if (!hypothetical) {
7960b57cec5SDimitry Andric     if (Optional<NonLoc> strLn = strLength.getAs<NonLoc>()) {
7970b57cec5SDimitry Andric       // In case of unbounded calls strlen etc bound the range to SIZE_MAX/4
7980b57cec5SDimitry Andric       BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
7990b57cec5SDimitry Andric       const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy);
8000b57cec5SDimitry Andric       llvm::APSInt fourInt = APSIntType(maxValInt).getValue(4);
8010b57cec5SDimitry Andric       const llvm::APSInt *maxLengthInt = BVF.evalAPSInt(BO_Div, maxValInt,
8020b57cec5SDimitry Andric                                                         fourInt);
8030b57cec5SDimitry Andric       NonLoc maxLength = svalBuilder.makeIntVal(*maxLengthInt);
8040b57cec5SDimitry Andric       SVal evalLength = svalBuilder.evalBinOpNN(state, BO_LE, *strLn,
8050b57cec5SDimitry Andric                                                 maxLength, sizeTy);
8060b57cec5SDimitry Andric       state = state->assume(evalLength.castAs<DefinedOrUnknownSVal>(), true);
8070b57cec5SDimitry Andric     }
8080b57cec5SDimitry Andric     state = state->set<CStringLength>(MR, strLength);
8090b57cec5SDimitry Andric   }
8100b57cec5SDimitry Andric 
8110b57cec5SDimitry Andric   return strLength;
8120b57cec5SDimitry Andric }
8130b57cec5SDimitry Andric 
8140b57cec5SDimitry Andric SVal CStringChecker::getCStringLength(CheckerContext &C, ProgramStateRef &state,
8150b57cec5SDimitry Andric                                       const Expr *Ex, SVal Buf,
8160b57cec5SDimitry Andric                                       bool hypothetical) const {
8170b57cec5SDimitry Andric   const MemRegion *MR = Buf.getAsRegion();
8180b57cec5SDimitry Andric   if (!MR) {
8190b57cec5SDimitry Andric     // If we can't get a region, see if it's something we /know/ isn't a
8200b57cec5SDimitry Andric     // C string. In the context of locations, the only time we can issue such
8210b57cec5SDimitry Andric     // a warning is for labels.
8220b57cec5SDimitry Andric     if (Optional<loc::GotoLabel> Label = Buf.getAs<loc::GotoLabel>()) {
8230b57cec5SDimitry Andric       if (Filter.CheckCStringNotNullTerm) {
8240b57cec5SDimitry Andric         SmallString<120> buf;
8250b57cec5SDimitry Andric         llvm::raw_svector_ostream os(buf);
8260b57cec5SDimitry Andric         assert(CurrentFunctionDescription);
8270b57cec5SDimitry Andric         os << "Argument to " << CurrentFunctionDescription
8280b57cec5SDimitry Andric            << " is the address of the label '" << Label->getLabel()->getName()
8290b57cec5SDimitry Andric            << "', which is not a null-terminated string";
8300b57cec5SDimitry Andric 
8310b57cec5SDimitry Andric         emitNotCStringBug(C, state, Ex, os.str());
8320b57cec5SDimitry Andric       }
8330b57cec5SDimitry Andric       return UndefinedVal();
8340b57cec5SDimitry Andric     }
8350b57cec5SDimitry Andric 
8360b57cec5SDimitry Andric     // If it's not a region and not a label, give up.
8370b57cec5SDimitry Andric     return UnknownVal();
8380b57cec5SDimitry Andric   }
8390b57cec5SDimitry Andric 
8400b57cec5SDimitry Andric   // If we have a region, strip casts from it and see if we can figure out
8410b57cec5SDimitry Andric   // its length. For anything we can't figure out, just return UnknownVal.
8420b57cec5SDimitry Andric   MR = MR->StripCasts();
8430b57cec5SDimitry Andric 
8440b57cec5SDimitry Andric   switch (MR->getKind()) {
8450b57cec5SDimitry Andric   case MemRegion::StringRegionKind: {
8460b57cec5SDimitry Andric     // Modifying the contents of string regions is undefined [C99 6.4.5p6],
8470b57cec5SDimitry Andric     // so we can assume that the byte length is the correct C string length.
8480b57cec5SDimitry Andric     SValBuilder &svalBuilder = C.getSValBuilder();
8490b57cec5SDimitry Andric     QualType sizeTy = svalBuilder.getContext().getSizeType();
8500b57cec5SDimitry Andric     const StringLiteral *strLit = cast<StringRegion>(MR)->getStringLiteral();
851*753f127fSDimitry Andric     return svalBuilder.makeIntVal(strLit->getLength(), sizeTy);
8520b57cec5SDimitry Andric   }
8530b57cec5SDimitry Andric   case MemRegion::SymbolicRegionKind:
8540b57cec5SDimitry Andric   case MemRegion::AllocaRegionKind:
8555ffd83dbSDimitry Andric   case MemRegion::NonParamVarRegionKind:
8565ffd83dbSDimitry Andric   case MemRegion::ParamVarRegionKind:
8570b57cec5SDimitry Andric   case MemRegion::FieldRegionKind:
8580b57cec5SDimitry Andric   case MemRegion::ObjCIvarRegionKind:
8590b57cec5SDimitry Andric     return getCStringLengthForRegion(C, state, Ex, MR, hypothetical);
8600b57cec5SDimitry Andric   case MemRegion::CompoundLiteralRegionKind:
8610b57cec5SDimitry Andric     // FIXME: Can we track this? Is it necessary?
8620b57cec5SDimitry Andric     return UnknownVal();
8630b57cec5SDimitry Andric   case MemRegion::ElementRegionKind:
8640b57cec5SDimitry Andric     // FIXME: How can we handle this? It's not good enough to subtract the
8650b57cec5SDimitry Andric     // offset from the base string length; consider "123\x00567" and &a[5].
8660b57cec5SDimitry Andric     return UnknownVal();
8670b57cec5SDimitry Andric   default:
8680b57cec5SDimitry Andric     // Other regions (mostly non-data) can't have a reliable C string length.
8690b57cec5SDimitry Andric     // In this case, an error is emitted and UndefinedVal is returned.
8700b57cec5SDimitry Andric     // The caller should always be prepared to handle this case.
8710b57cec5SDimitry Andric     if (Filter.CheckCStringNotNullTerm) {
8720b57cec5SDimitry Andric       SmallString<120> buf;
8730b57cec5SDimitry Andric       llvm::raw_svector_ostream os(buf);
8740b57cec5SDimitry Andric 
8750b57cec5SDimitry Andric       assert(CurrentFunctionDescription);
8760b57cec5SDimitry Andric       os << "Argument to " << CurrentFunctionDescription << " is ";
8770b57cec5SDimitry Andric 
8780b57cec5SDimitry Andric       if (SummarizeRegion(os, C.getASTContext(), MR))
8790b57cec5SDimitry Andric         os << ", which is not a null-terminated string";
8800b57cec5SDimitry Andric       else
8810b57cec5SDimitry Andric         os << "not a null-terminated string";
8820b57cec5SDimitry Andric 
8830b57cec5SDimitry Andric       emitNotCStringBug(C, state, Ex, os.str());
8840b57cec5SDimitry Andric     }
8850b57cec5SDimitry Andric     return UndefinedVal();
8860b57cec5SDimitry Andric   }
8870b57cec5SDimitry Andric }
8880b57cec5SDimitry Andric 
8890b57cec5SDimitry Andric const StringLiteral *CStringChecker::getCStringLiteral(CheckerContext &C,
8900b57cec5SDimitry Andric   ProgramStateRef &state, const Expr *expr, SVal val) const {
8910b57cec5SDimitry Andric 
8920b57cec5SDimitry Andric   // Get the memory region pointed to by the val.
8930b57cec5SDimitry Andric   const MemRegion *bufRegion = val.getAsRegion();
8940b57cec5SDimitry Andric   if (!bufRegion)
8950b57cec5SDimitry Andric     return nullptr;
8960b57cec5SDimitry Andric 
8970b57cec5SDimitry Andric   // Strip casts off the memory region.
8980b57cec5SDimitry Andric   bufRegion = bufRegion->StripCasts();
8990b57cec5SDimitry Andric 
9000b57cec5SDimitry Andric   // Cast the memory region to a string region.
9010b57cec5SDimitry Andric   const StringRegion *strRegion= dyn_cast<StringRegion>(bufRegion);
9020b57cec5SDimitry Andric   if (!strRegion)
9030b57cec5SDimitry Andric     return nullptr;
9040b57cec5SDimitry Andric 
9050b57cec5SDimitry Andric   // Return the actual string in the string region.
9060b57cec5SDimitry Andric   return strRegion->getStringLiteral();
9070b57cec5SDimitry Andric }
9080b57cec5SDimitry Andric 
9090b57cec5SDimitry Andric bool CStringChecker::IsFirstBufInBound(CheckerContext &C,
9100b57cec5SDimitry Andric                                        ProgramStateRef state,
9110b57cec5SDimitry Andric                                        const Expr *FirstBuf,
9120b57cec5SDimitry Andric                                        const Expr *Size) {
9130b57cec5SDimitry Andric   // If we do not know that the buffer is long enough we return 'true'.
9140b57cec5SDimitry Andric   // Otherwise the parent region of this field region would also get
9150b57cec5SDimitry Andric   // invalidated, which would lead to warnings based on an unknown state.
9160b57cec5SDimitry Andric 
9170b57cec5SDimitry Andric   // Originally copied from CheckBufferAccess and CheckLocation.
9180b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
9190b57cec5SDimitry Andric   ASTContext &Ctx = svalBuilder.getContext();
9200b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
9210b57cec5SDimitry Andric 
9220b57cec5SDimitry Andric   QualType sizeTy = Size->getType();
9230b57cec5SDimitry Andric   QualType PtrTy = Ctx.getPointerType(Ctx.CharTy);
9240b57cec5SDimitry Andric   SVal BufVal = state->getSVal(FirstBuf, LCtx);
9250b57cec5SDimitry Andric 
9260b57cec5SDimitry Andric   SVal LengthVal = state->getSVal(Size, LCtx);
9270b57cec5SDimitry Andric   Optional<NonLoc> Length = LengthVal.getAs<NonLoc>();
9280b57cec5SDimitry Andric   if (!Length)
9290b57cec5SDimitry Andric     return true; // cf top comment.
9300b57cec5SDimitry Andric 
9310b57cec5SDimitry Andric   // Compute the offset of the last element to be accessed: size-1.
9320b57cec5SDimitry Andric   NonLoc One = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>();
9330b57cec5SDimitry Andric   SVal Offset = svalBuilder.evalBinOpNN(state, BO_Sub, *Length, One, sizeTy);
9340b57cec5SDimitry Andric   if (Offset.isUnknown())
9350b57cec5SDimitry Andric     return true; // cf top comment
9360b57cec5SDimitry Andric   NonLoc LastOffset = Offset.castAs<NonLoc>();
9370b57cec5SDimitry Andric 
9380b57cec5SDimitry Andric   // Check that the first buffer is sufficiently long.
9390b57cec5SDimitry Andric   SVal BufStart = svalBuilder.evalCast(BufVal, PtrTy, FirstBuf->getType());
9400b57cec5SDimitry Andric   Optional<Loc> BufLoc = BufStart.getAs<Loc>();
9410b57cec5SDimitry Andric   if (!BufLoc)
9420b57cec5SDimitry Andric     return true; // cf top comment.
9430b57cec5SDimitry Andric 
9440b57cec5SDimitry Andric   SVal BufEnd =
9450b57cec5SDimitry Andric       svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc, LastOffset, PtrTy);
9460b57cec5SDimitry Andric 
9470b57cec5SDimitry Andric   // Check for out of bound array element access.
9480b57cec5SDimitry Andric   const MemRegion *R = BufEnd.getAsRegion();
9490b57cec5SDimitry Andric   if (!R)
9500b57cec5SDimitry Andric     return true; // cf top comment.
9510b57cec5SDimitry Andric 
9520b57cec5SDimitry Andric   const ElementRegion *ER = dyn_cast<ElementRegion>(R);
9530b57cec5SDimitry Andric   if (!ER)
9540b57cec5SDimitry Andric     return true; // cf top comment.
9550b57cec5SDimitry Andric 
9560b57cec5SDimitry Andric   // FIXME: Does this crash when a non-standard definition
9570b57cec5SDimitry Andric   // of a library function is encountered?
9580b57cec5SDimitry Andric   assert(ER->getValueType() == C.getASTContext().CharTy &&
9590b57cec5SDimitry Andric          "IsFirstBufInBound should only be called with char* ElementRegions");
9600b57cec5SDimitry Andric 
9610b57cec5SDimitry Andric   // Get the size of the array.
9620b57cec5SDimitry Andric   const SubRegion *superReg = cast<SubRegion>(ER->getSuperRegion());
963fe6060f1SDimitry Andric   DefinedOrUnknownSVal SizeDV = getDynamicExtent(state, superReg, svalBuilder);
9640b57cec5SDimitry Andric 
9650b57cec5SDimitry Andric   // Get the index of the accessed element.
9660b57cec5SDimitry Andric   DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>();
9670b57cec5SDimitry Andric 
9685ffd83dbSDimitry Andric   ProgramStateRef StInBound = state->assumeInBound(Idx, SizeDV, true);
9690b57cec5SDimitry Andric 
9700b57cec5SDimitry Andric   return static_cast<bool>(StInBound);
9710b57cec5SDimitry Andric }
9720b57cec5SDimitry Andric 
9730b57cec5SDimitry Andric ProgramStateRef CStringChecker::InvalidateBuffer(CheckerContext &C,
9740b57cec5SDimitry Andric                                                  ProgramStateRef state,
9750b57cec5SDimitry Andric                                                  const Expr *E, SVal V,
9760b57cec5SDimitry Andric                                                  bool IsSourceBuffer,
9770b57cec5SDimitry Andric                                                  const Expr *Size) {
9780b57cec5SDimitry Andric   Optional<Loc> L = V.getAs<Loc>();
9790b57cec5SDimitry Andric   if (!L)
9800b57cec5SDimitry Andric     return state;
9810b57cec5SDimitry Andric 
9820b57cec5SDimitry Andric   // FIXME: This is a simplified version of what's in CFRefCount.cpp -- it makes
9830b57cec5SDimitry Andric   // some assumptions about the value that CFRefCount can't. Even so, it should
9840b57cec5SDimitry Andric   // probably be refactored.
9850b57cec5SDimitry Andric   if (Optional<loc::MemRegionVal> MR = L->getAs<loc::MemRegionVal>()) {
9860b57cec5SDimitry Andric     const MemRegion *R = MR->getRegion()->StripCasts();
9870b57cec5SDimitry Andric 
9880b57cec5SDimitry Andric     // Are we dealing with an ElementRegion?  If so, we should be invalidating
9890b57cec5SDimitry Andric     // the super-region.
9900b57cec5SDimitry Andric     if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
9910b57cec5SDimitry Andric       R = ER->getSuperRegion();
9920b57cec5SDimitry Andric       // FIXME: What about layers of ElementRegions?
9930b57cec5SDimitry Andric     }
9940b57cec5SDimitry Andric 
9950b57cec5SDimitry Andric     // Invalidate this region.
9960b57cec5SDimitry Andric     const LocationContext *LCtx = C.getPredecessor()->getLocationContext();
9970b57cec5SDimitry Andric 
9980b57cec5SDimitry Andric     bool CausesPointerEscape = false;
9990b57cec5SDimitry Andric     RegionAndSymbolInvalidationTraits ITraits;
10000b57cec5SDimitry Andric     // Invalidate and escape only indirect regions accessible through the source
10010b57cec5SDimitry Andric     // buffer.
10020b57cec5SDimitry Andric     if (IsSourceBuffer) {
10030b57cec5SDimitry Andric       ITraits.setTrait(R->getBaseRegion(),
10040b57cec5SDimitry Andric                        RegionAndSymbolInvalidationTraits::TK_PreserveContents);
10050b57cec5SDimitry Andric       ITraits.setTrait(R, RegionAndSymbolInvalidationTraits::TK_SuppressEscape);
10060b57cec5SDimitry Andric       CausesPointerEscape = true;
10070b57cec5SDimitry Andric     } else {
10080b57cec5SDimitry Andric       const MemRegion::Kind& K = R->getKind();
10090b57cec5SDimitry Andric       if (K == MemRegion::FieldRegionKind)
10100b57cec5SDimitry Andric         if (Size && IsFirstBufInBound(C, state, E, Size)) {
10110b57cec5SDimitry Andric           // If destination buffer is a field region and access is in bound,
10120b57cec5SDimitry Andric           // do not invalidate its super region.
10130b57cec5SDimitry Andric           ITraits.setTrait(
10140b57cec5SDimitry Andric               R,
10150b57cec5SDimitry Andric               RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion);
10160b57cec5SDimitry Andric         }
10170b57cec5SDimitry Andric     }
10180b57cec5SDimitry Andric 
10190b57cec5SDimitry Andric     return state->invalidateRegions(R, E, C.blockCount(), LCtx,
10200b57cec5SDimitry Andric                                     CausesPointerEscape, nullptr, nullptr,
10210b57cec5SDimitry Andric                                     &ITraits);
10220b57cec5SDimitry Andric   }
10230b57cec5SDimitry Andric 
10240b57cec5SDimitry Andric   // If we have a non-region value by chance, just remove the binding.
10250b57cec5SDimitry Andric   // FIXME: is this necessary or correct? This handles the non-Region
10260b57cec5SDimitry Andric   //  cases.  Is it ever valid to store to these?
10270b57cec5SDimitry Andric   return state->killBinding(*L);
10280b57cec5SDimitry Andric }
10290b57cec5SDimitry Andric 
10300b57cec5SDimitry Andric bool CStringChecker::SummarizeRegion(raw_ostream &os, ASTContext &Ctx,
10310b57cec5SDimitry Andric                                      const MemRegion *MR) {
10320b57cec5SDimitry Andric   switch (MR->getKind()) {
10330b57cec5SDimitry Andric   case MemRegion::FunctionCodeRegionKind: {
1034480093f4SDimitry Andric     if (const auto *FD = cast<FunctionCodeRegion>(MR)->getDecl())
10350b57cec5SDimitry Andric       os << "the address of the function '" << *FD << '\'';
10360b57cec5SDimitry Andric     else
10370b57cec5SDimitry Andric       os << "the address of a function";
10380b57cec5SDimitry Andric     return true;
10390b57cec5SDimitry Andric   }
10400b57cec5SDimitry Andric   case MemRegion::BlockCodeRegionKind:
10410b57cec5SDimitry Andric     os << "block text";
10420b57cec5SDimitry Andric     return true;
10430b57cec5SDimitry Andric   case MemRegion::BlockDataRegionKind:
10440b57cec5SDimitry Andric     os << "a block";
10450b57cec5SDimitry Andric     return true;
10460b57cec5SDimitry Andric   case MemRegion::CXXThisRegionKind:
10470b57cec5SDimitry Andric   case MemRegion::CXXTempObjectRegionKind:
1048480093f4SDimitry Andric     os << "a C++ temp object of type "
104981ad6265SDimitry Andric        << cast<TypedValueRegion>(MR)->getValueType();
10500b57cec5SDimitry Andric     return true;
10515ffd83dbSDimitry Andric   case MemRegion::NonParamVarRegionKind:
105281ad6265SDimitry Andric     os << "a variable of type" << cast<TypedValueRegion>(MR)->getValueType();
10530b57cec5SDimitry Andric     return true;
10545ffd83dbSDimitry Andric   case MemRegion::ParamVarRegionKind:
105581ad6265SDimitry Andric     os << "a parameter of type" << cast<TypedValueRegion>(MR)->getValueType();
10565ffd83dbSDimitry Andric     return true;
10570b57cec5SDimitry Andric   case MemRegion::FieldRegionKind:
105881ad6265SDimitry Andric     os << "a field of type " << cast<TypedValueRegion>(MR)->getValueType();
10590b57cec5SDimitry Andric     return true;
10600b57cec5SDimitry Andric   case MemRegion::ObjCIvarRegionKind:
1061480093f4SDimitry Andric     os << "an instance variable of type "
106281ad6265SDimitry Andric        << cast<TypedValueRegion>(MR)->getValueType();
10630b57cec5SDimitry Andric     return true;
10640b57cec5SDimitry Andric   default:
10650b57cec5SDimitry Andric     return false;
10660b57cec5SDimitry Andric   }
10670b57cec5SDimitry Andric }
10680b57cec5SDimitry Andric 
10690b57cec5SDimitry Andric bool CStringChecker::memsetAux(const Expr *DstBuffer, SVal CharVal,
10700b57cec5SDimitry Andric                                const Expr *Size, CheckerContext &C,
10710b57cec5SDimitry Andric                                ProgramStateRef &State) {
10720b57cec5SDimitry Andric   SVal MemVal = C.getSVal(DstBuffer);
10730b57cec5SDimitry Andric   SVal SizeVal = C.getSVal(Size);
10740b57cec5SDimitry Andric   const MemRegion *MR = MemVal.getAsRegion();
10750b57cec5SDimitry Andric   if (!MR)
10760b57cec5SDimitry Andric     return false;
10770b57cec5SDimitry Andric 
10780b57cec5SDimitry Andric   // We're about to model memset by producing a "default binding" in the Store.
10790b57cec5SDimitry Andric   // Our current implementation - RegionStore - doesn't support default bindings
10800b57cec5SDimitry Andric   // that don't cover the whole base region. So we should first get the offset
10810b57cec5SDimitry Andric   // and the base region to figure out whether the offset of buffer is 0.
10820b57cec5SDimitry Andric   RegionOffset Offset = MR->getAsOffset();
10830b57cec5SDimitry Andric   const MemRegion *BR = Offset.getRegion();
10840b57cec5SDimitry Andric 
10850b57cec5SDimitry Andric   Optional<NonLoc> SizeNL = SizeVal.getAs<NonLoc>();
10860b57cec5SDimitry Andric   if (!SizeNL)
10870b57cec5SDimitry Andric     return false;
10880b57cec5SDimitry Andric 
10890b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
10900b57cec5SDimitry Andric   ASTContext &Ctx = C.getASTContext();
10910b57cec5SDimitry Andric 
10920b57cec5SDimitry Andric   // void *memset(void *dest, int ch, size_t count);
10930b57cec5SDimitry Andric   // For now we can only handle the case of offset is 0 and concrete char value.
10940b57cec5SDimitry Andric   if (Offset.isValid() && !Offset.hasSymbolicOffset() &&
10950b57cec5SDimitry Andric       Offset.getOffset() == 0) {
10965ffd83dbSDimitry Andric     // Get the base region's size.
1097fe6060f1SDimitry Andric     DefinedOrUnknownSVal SizeDV = getDynamicExtent(State, BR, svalBuilder);
10980b57cec5SDimitry Andric 
10990b57cec5SDimitry Andric     ProgramStateRef StateWholeReg, StateNotWholeReg;
11000b57cec5SDimitry Andric     std::tie(StateWholeReg, StateNotWholeReg) =
11015ffd83dbSDimitry Andric         State->assume(svalBuilder.evalEQ(State, SizeDV, *SizeNL));
11020b57cec5SDimitry Andric 
11030b57cec5SDimitry Andric     // With the semantic of 'memset()', we should convert the CharVal to
11040b57cec5SDimitry Andric     // unsigned char.
11050b57cec5SDimitry Andric     CharVal = svalBuilder.evalCast(CharVal, Ctx.UnsignedCharTy, Ctx.IntTy);
11060b57cec5SDimitry Andric 
11070b57cec5SDimitry Andric     ProgramStateRef StateNullChar, StateNonNullChar;
11080b57cec5SDimitry Andric     std::tie(StateNullChar, StateNonNullChar) =
11090b57cec5SDimitry Andric         assumeZero(C, State, CharVal, Ctx.UnsignedCharTy);
11100b57cec5SDimitry Andric 
11110b57cec5SDimitry Andric     if (StateWholeReg && !StateNotWholeReg && StateNullChar &&
11120b57cec5SDimitry Andric         !StateNonNullChar) {
11130b57cec5SDimitry Andric       // If the 'memset()' acts on the whole region of destination buffer and
11140b57cec5SDimitry Andric       // the value of the second argument of 'memset()' is zero, bind the second
11150b57cec5SDimitry Andric       // argument's value to the destination buffer with 'default binding'.
11160b57cec5SDimitry Andric       // FIXME: Since there is no perfect way to bind the non-zero character, we
11170b57cec5SDimitry Andric       // can only deal with zero value here. In the future, we need to deal with
11180b57cec5SDimitry Andric       // the binding of non-zero value in the case of whole region.
11190b57cec5SDimitry Andric       State = State->bindDefaultZero(svalBuilder.makeLoc(BR),
11200b57cec5SDimitry Andric                                      C.getLocationContext());
11210b57cec5SDimitry Andric     } else {
11220b57cec5SDimitry Andric       // If the destination buffer's extent is not equal to the value of
11230b57cec5SDimitry Andric       // third argument, just invalidate buffer.
11240b57cec5SDimitry Andric       State = InvalidateBuffer(C, State, DstBuffer, MemVal,
11250b57cec5SDimitry Andric                                /*IsSourceBuffer*/ false, Size);
11260b57cec5SDimitry Andric     }
11270b57cec5SDimitry Andric 
11280b57cec5SDimitry Andric     if (StateNullChar && !StateNonNullChar) {
11290b57cec5SDimitry Andric       // If the value of the second argument of 'memset()' is zero, set the
11300b57cec5SDimitry Andric       // string length of destination buffer to 0 directly.
11310b57cec5SDimitry Andric       State = setCStringLength(State, MR,
11320b57cec5SDimitry Andric                                svalBuilder.makeZeroVal(Ctx.getSizeType()));
11330b57cec5SDimitry Andric     } else if (!StateNullChar && StateNonNullChar) {
11340b57cec5SDimitry Andric       SVal NewStrLen = svalBuilder.getMetadataSymbolVal(
11350b57cec5SDimitry Andric           CStringChecker::getTag(), MR, DstBuffer, Ctx.getSizeType(),
11360b57cec5SDimitry Andric           C.getLocationContext(), C.blockCount());
11370b57cec5SDimitry Andric 
11380b57cec5SDimitry Andric       // If the value of second argument is not zero, then the string length
11390b57cec5SDimitry Andric       // is at least the size argument.
11400b57cec5SDimitry Andric       SVal NewStrLenGESize = svalBuilder.evalBinOp(
11410b57cec5SDimitry Andric           State, BO_GE, NewStrLen, SizeVal, svalBuilder.getConditionType());
11420b57cec5SDimitry Andric 
11430b57cec5SDimitry Andric       State = setCStringLength(
11440b57cec5SDimitry Andric           State->assume(NewStrLenGESize.castAs<DefinedOrUnknownSVal>(), true),
11450b57cec5SDimitry Andric           MR, NewStrLen);
11460b57cec5SDimitry Andric     }
11470b57cec5SDimitry Andric   } else {
11480b57cec5SDimitry Andric     // If the offset is not zero and char value is not concrete, we can do
11490b57cec5SDimitry Andric     // nothing but invalidate the buffer.
11500b57cec5SDimitry Andric     State = InvalidateBuffer(C, State, DstBuffer, MemVal,
11510b57cec5SDimitry Andric                              /*IsSourceBuffer*/ false, Size);
11520b57cec5SDimitry Andric   }
11530b57cec5SDimitry Andric   return true;
11540b57cec5SDimitry Andric }
11550b57cec5SDimitry Andric 
11560b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
11570b57cec5SDimitry Andric // evaluation of individual function calls.
11580b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
11590b57cec5SDimitry Andric 
11605ffd83dbSDimitry Andric void CStringChecker::evalCopyCommon(CheckerContext &C, const CallExpr *CE,
11615ffd83dbSDimitry Andric                                     ProgramStateRef state, SizeArgExpr Size,
11625ffd83dbSDimitry Andric                                     DestinationArgExpr Dest,
11635ffd83dbSDimitry Andric                                     SourceArgExpr Source, bool Restricted,
11640b57cec5SDimitry Andric                                     bool IsMempcpy) const {
11650b57cec5SDimitry Andric   CurrentFunctionDescription = "memory copy function";
11660b57cec5SDimitry Andric 
11670b57cec5SDimitry Andric   // See if the size argument is zero.
11680b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
11695ffd83dbSDimitry Andric   SVal sizeVal = state->getSVal(Size.Expression, LCtx);
11705ffd83dbSDimitry Andric   QualType sizeTy = Size.Expression->getType();
11710b57cec5SDimitry Andric 
11720b57cec5SDimitry Andric   ProgramStateRef stateZeroSize, stateNonZeroSize;
11730b57cec5SDimitry Andric   std::tie(stateZeroSize, stateNonZeroSize) =
11740b57cec5SDimitry Andric       assumeZero(C, state, sizeVal, sizeTy);
11750b57cec5SDimitry Andric 
11760b57cec5SDimitry Andric   // Get the value of the Dest.
11775ffd83dbSDimitry Andric   SVal destVal = state->getSVal(Dest.Expression, LCtx);
11780b57cec5SDimitry Andric 
11790b57cec5SDimitry Andric   // If the size is zero, there won't be any actual memory access, so
11800b57cec5SDimitry Andric   // just bind the return value to the destination buffer and return.
11810b57cec5SDimitry Andric   if (stateZeroSize && !stateNonZeroSize) {
11820b57cec5SDimitry Andric     stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, destVal);
11830b57cec5SDimitry Andric     C.addTransition(stateZeroSize);
11840b57cec5SDimitry Andric     return;
11850b57cec5SDimitry Andric   }
11860b57cec5SDimitry Andric 
11870b57cec5SDimitry Andric   // If the size can be nonzero, we have to check the other arguments.
11880b57cec5SDimitry Andric   if (stateNonZeroSize) {
11890b57cec5SDimitry Andric     state = stateNonZeroSize;
11900b57cec5SDimitry Andric 
11910b57cec5SDimitry Andric     // Ensure the destination is not null. If it is NULL there will be a
11920b57cec5SDimitry Andric     // NULL pointer dereference.
11935ffd83dbSDimitry Andric     state = checkNonNull(C, state, Dest, destVal);
11940b57cec5SDimitry Andric     if (!state)
11950b57cec5SDimitry Andric       return;
11960b57cec5SDimitry Andric 
11970b57cec5SDimitry Andric     // Get the value of the Src.
11985ffd83dbSDimitry Andric     SVal srcVal = state->getSVal(Source.Expression, LCtx);
11990b57cec5SDimitry Andric 
12000b57cec5SDimitry Andric     // Ensure the source is not null. If it is NULL there will be a
12010b57cec5SDimitry Andric     // NULL pointer dereference.
12025ffd83dbSDimitry Andric     state = checkNonNull(C, state, Source, srcVal);
12030b57cec5SDimitry Andric     if (!state)
12040b57cec5SDimitry Andric       return;
12050b57cec5SDimitry Andric 
12060b57cec5SDimitry Andric     // Ensure the accesses are valid and that the buffers do not overlap.
12075ffd83dbSDimitry Andric     state = CheckBufferAccess(C, state, Dest, Size, AccessKind::write);
12085ffd83dbSDimitry Andric     state = CheckBufferAccess(C, state, Source, Size, AccessKind::read);
12095ffd83dbSDimitry Andric 
12100b57cec5SDimitry Andric     if (Restricted)
12110b57cec5SDimitry Andric       state = CheckOverlap(C, state, Size, Dest, Source);
12120b57cec5SDimitry Andric 
12130b57cec5SDimitry Andric     if (!state)
12140b57cec5SDimitry Andric       return;
12150b57cec5SDimitry Andric 
12160b57cec5SDimitry Andric     // If this is mempcpy, get the byte after the last byte copied and
12170b57cec5SDimitry Andric     // bind the expr.
12180b57cec5SDimitry Andric     if (IsMempcpy) {
12190b57cec5SDimitry Andric       // Get the byte after the last byte copied.
12200b57cec5SDimitry Andric       SValBuilder &SvalBuilder = C.getSValBuilder();
12210b57cec5SDimitry Andric       ASTContext &Ctx = SvalBuilder.getContext();
12220b57cec5SDimitry Andric       QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy);
12230b57cec5SDimitry Andric       SVal DestRegCharVal =
12245ffd83dbSDimitry Andric           SvalBuilder.evalCast(destVal, CharPtrTy, Dest.Expression->getType());
12250b57cec5SDimitry Andric       SVal lastElement = C.getSValBuilder().evalBinOp(
12265ffd83dbSDimitry Andric           state, BO_Add, DestRegCharVal, sizeVal, Dest.Expression->getType());
12270b57cec5SDimitry Andric       // If we don't know how much we copied, we can at least
12280b57cec5SDimitry Andric       // conjure a return value for later.
12290b57cec5SDimitry Andric       if (lastElement.isUnknown())
12300b57cec5SDimitry Andric         lastElement = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx,
12310b57cec5SDimitry Andric                                                           C.blockCount());
12320b57cec5SDimitry Andric 
12330b57cec5SDimitry Andric       // The byte after the last byte copied is the return value.
12340b57cec5SDimitry Andric       state = state->BindExpr(CE, LCtx, lastElement);
12350b57cec5SDimitry Andric     } else {
12360b57cec5SDimitry Andric       // All other copies return the destination buffer.
12370b57cec5SDimitry Andric       // (Well, bcopy() has a void return type, but this won't hurt.)
12380b57cec5SDimitry Andric       state = state->BindExpr(CE, LCtx, destVal);
12390b57cec5SDimitry Andric     }
12400b57cec5SDimitry Andric 
12410b57cec5SDimitry Andric     // Invalidate the destination (regular invalidation without pointer-escaping
12420b57cec5SDimitry Andric     // the address of the top-level region).
12430b57cec5SDimitry Andric     // FIXME: Even if we can't perfectly model the copy, we should see if we
12440b57cec5SDimitry Andric     // can use LazyCompoundVals to copy the source values into the destination.
12450b57cec5SDimitry Andric     // This would probably remove any existing bindings past the end of the
12460b57cec5SDimitry Andric     // copied region, but that's still an improvement over blank invalidation.
12475ffd83dbSDimitry Andric     state =
12485ffd83dbSDimitry Andric         InvalidateBuffer(C, state, Dest.Expression, C.getSVal(Dest.Expression),
12495ffd83dbSDimitry Andric                          /*IsSourceBuffer*/ false, Size.Expression);
12500b57cec5SDimitry Andric 
12510b57cec5SDimitry Andric     // Invalidate the source (const-invalidation without const-pointer-escaping
12520b57cec5SDimitry Andric     // the address of the top-level region).
12535ffd83dbSDimitry Andric     state = InvalidateBuffer(C, state, Source.Expression,
12545ffd83dbSDimitry Andric                              C.getSVal(Source.Expression),
12550b57cec5SDimitry Andric                              /*IsSourceBuffer*/ true, nullptr);
12560b57cec5SDimitry Andric 
12570b57cec5SDimitry Andric     C.addTransition(state);
12580b57cec5SDimitry Andric   }
12590b57cec5SDimitry Andric }
12600b57cec5SDimitry Andric 
12610b57cec5SDimitry Andric void CStringChecker::evalMemcpy(CheckerContext &C, const CallExpr *CE) const {
12620b57cec5SDimitry Andric   // void *memcpy(void *restrict dst, const void *restrict src, size_t n);
12630b57cec5SDimitry Andric   // The return value is the address of the destination buffer.
12645ffd83dbSDimitry Andric   DestinationArgExpr Dest = {CE->getArg(0), 0};
12655ffd83dbSDimitry Andric   SourceArgExpr Src = {CE->getArg(1), 1};
12665ffd83dbSDimitry Andric   SizeArgExpr Size = {CE->getArg(2), 2};
12670b57cec5SDimitry Andric 
12685ffd83dbSDimitry Andric   ProgramStateRef State = C.getState();
12695ffd83dbSDimitry Andric 
12705ffd83dbSDimitry Andric   constexpr bool IsRestricted = true;
12715ffd83dbSDimitry Andric   constexpr bool IsMempcpy = false;
12725ffd83dbSDimitry Andric   evalCopyCommon(C, CE, State, Size, Dest, Src, IsRestricted, IsMempcpy);
12730b57cec5SDimitry Andric }
12740b57cec5SDimitry Andric 
12750b57cec5SDimitry Andric void CStringChecker::evalMempcpy(CheckerContext &C, const CallExpr *CE) const {
12760b57cec5SDimitry Andric   // void *mempcpy(void *restrict dst, const void *restrict src, size_t n);
12770b57cec5SDimitry Andric   // The return value is a pointer to the byte following the last written byte.
12785ffd83dbSDimitry Andric   DestinationArgExpr Dest = {CE->getArg(0), 0};
12795ffd83dbSDimitry Andric   SourceArgExpr Src = {CE->getArg(1), 1};
12805ffd83dbSDimitry Andric   SizeArgExpr Size = {CE->getArg(2), 2};
12810b57cec5SDimitry Andric 
12825ffd83dbSDimitry Andric   constexpr bool IsRestricted = true;
12835ffd83dbSDimitry Andric   constexpr bool IsMempcpy = true;
12845ffd83dbSDimitry Andric   evalCopyCommon(C, CE, C.getState(), Size, Dest, Src, IsRestricted, IsMempcpy);
12850b57cec5SDimitry Andric }
12860b57cec5SDimitry Andric 
12870b57cec5SDimitry Andric void CStringChecker::evalMemmove(CheckerContext &C, const CallExpr *CE) const {
12880b57cec5SDimitry Andric   // void *memmove(void *dst, const void *src, size_t n);
12890b57cec5SDimitry Andric   // The return value is the address of the destination buffer.
12905ffd83dbSDimitry Andric   DestinationArgExpr Dest = {CE->getArg(0), 0};
12915ffd83dbSDimitry Andric   SourceArgExpr Src = {CE->getArg(1), 1};
12925ffd83dbSDimitry Andric   SizeArgExpr Size = {CE->getArg(2), 2};
12930b57cec5SDimitry Andric 
12945ffd83dbSDimitry Andric   constexpr bool IsRestricted = false;
12955ffd83dbSDimitry Andric   constexpr bool IsMempcpy = false;
12965ffd83dbSDimitry Andric   evalCopyCommon(C, CE, C.getState(), Size, Dest, Src, IsRestricted, IsMempcpy);
12970b57cec5SDimitry Andric }
12980b57cec5SDimitry Andric 
12990b57cec5SDimitry Andric void CStringChecker::evalBcopy(CheckerContext &C, const CallExpr *CE) const {
13000b57cec5SDimitry Andric   // void bcopy(const void *src, void *dst, size_t n);
13015ffd83dbSDimitry Andric   SourceArgExpr Src(CE->getArg(0), 0);
13025ffd83dbSDimitry Andric   DestinationArgExpr Dest = {CE->getArg(1), 1};
13035ffd83dbSDimitry Andric   SizeArgExpr Size = {CE->getArg(2), 2};
13045ffd83dbSDimitry Andric 
13055ffd83dbSDimitry Andric   constexpr bool IsRestricted = false;
13065ffd83dbSDimitry Andric   constexpr bool IsMempcpy = false;
13075ffd83dbSDimitry Andric   evalCopyCommon(C, CE, C.getState(), Size, Dest, Src, IsRestricted, IsMempcpy);
13080b57cec5SDimitry Andric }
13090b57cec5SDimitry Andric 
13100b57cec5SDimitry Andric void CStringChecker::evalMemcmp(CheckerContext &C, const CallExpr *CE) const {
13110b57cec5SDimitry Andric   // int memcmp(const void *s1, const void *s2, size_t n);
13120b57cec5SDimitry Andric   CurrentFunctionDescription = "memory comparison function";
13130b57cec5SDimitry Andric 
13145ffd83dbSDimitry Andric   AnyArgExpr Left = {CE->getArg(0), 0};
13155ffd83dbSDimitry Andric   AnyArgExpr Right = {CE->getArg(1), 1};
13165ffd83dbSDimitry Andric   SizeArgExpr Size = {CE->getArg(2), 2};
13170b57cec5SDimitry Andric 
13185ffd83dbSDimitry Andric   ProgramStateRef State = C.getState();
13195ffd83dbSDimitry Andric   SValBuilder &Builder = C.getSValBuilder();
13205ffd83dbSDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
13210b57cec5SDimitry Andric 
13220b57cec5SDimitry Andric   // See if the size argument is zero.
13235ffd83dbSDimitry Andric   SVal sizeVal = State->getSVal(Size.Expression, LCtx);
13245ffd83dbSDimitry Andric   QualType sizeTy = Size.Expression->getType();
13250b57cec5SDimitry Andric 
13260b57cec5SDimitry Andric   ProgramStateRef stateZeroSize, stateNonZeroSize;
13270b57cec5SDimitry Andric   std::tie(stateZeroSize, stateNonZeroSize) =
13285ffd83dbSDimitry Andric       assumeZero(C, State, sizeVal, sizeTy);
13290b57cec5SDimitry Andric 
13300b57cec5SDimitry Andric   // If the size can be zero, the result will be 0 in that case, and we don't
13310b57cec5SDimitry Andric   // have to check either of the buffers.
13320b57cec5SDimitry Andric   if (stateZeroSize) {
13335ffd83dbSDimitry Andric     State = stateZeroSize;
13345ffd83dbSDimitry Andric     State = State->BindExpr(CE, LCtx, Builder.makeZeroVal(CE->getType()));
13355ffd83dbSDimitry Andric     C.addTransition(State);
13360b57cec5SDimitry Andric   }
13370b57cec5SDimitry Andric 
13380b57cec5SDimitry Andric   // If the size can be nonzero, we have to check the other arguments.
13390b57cec5SDimitry Andric   if (stateNonZeroSize) {
13405ffd83dbSDimitry Andric     State = stateNonZeroSize;
13410b57cec5SDimitry Andric     // If we know the two buffers are the same, we know the result is 0.
13420b57cec5SDimitry Andric     // First, get the two buffers' addresses. Another checker will have already
13430b57cec5SDimitry Andric     // made sure they're not undefined.
13440b57cec5SDimitry Andric     DefinedOrUnknownSVal LV =
13455ffd83dbSDimitry Andric         State->getSVal(Left.Expression, LCtx).castAs<DefinedOrUnknownSVal>();
13460b57cec5SDimitry Andric     DefinedOrUnknownSVal RV =
13475ffd83dbSDimitry Andric         State->getSVal(Right.Expression, LCtx).castAs<DefinedOrUnknownSVal>();
13480b57cec5SDimitry Andric 
13490b57cec5SDimitry Andric     // See if they are the same.
13505ffd83dbSDimitry Andric     ProgramStateRef SameBuffer, NotSameBuffer;
13515ffd83dbSDimitry Andric     std::tie(SameBuffer, NotSameBuffer) =
13525ffd83dbSDimitry Andric         State->assume(Builder.evalEQ(State, LV, RV));
13530b57cec5SDimitry Andric 
1354480093f4SDimitry Andric     // If the two arguments are the same buffer, we know the result is 0,
13550b57cec5SDimitry Andric     // and we only need to check one size.
13565ffd83dbSDimitry Andric     if (SameBuffer && !NotSameBuffer) {
13575ffd83dbSDimitry Andric       State = SameBuffer;
13585ffd83dbSDimitry Andric       State = CheckBufferAccess(C, State, Left, Size, AccessKind::read);
13595ffd83dbSDimitry Andric       if (State) {
13605ffd83dbSDimitry Andric         State =
13615ffd83dbSDimitry Andric             SameBuffer->BindExpr(CE, LCtx, Builder.makeZeroVal(CE->getType()));
13625ffd83dbSDimitry Andric         C.addTransition(State);
13630b57cec5SDimitry Andric       }
1364480093f4SDimitry Andric       return;
13650b57cec5SDimitry Andric     }
13660b57cec5SDimitry Andric 
1367480093f4SDimitry Andric     // If the two arguments might be different buffers, we have to check
1368480093f4SDimitry Andric     // the size of both of them.
13695ffd83dbSDimitry Andric     assert(NotSameBuffer);
13705ffd83dbSDimitry Andric     State = CheckBufferAccess(C, State, Right, Size, AccessKind::read);
13715ffd83dbSDimitry Andric     State = CheckBufferAccess(C, State, Left, Size, AccessKind::read);
13725ffd83dbSDimitry Andric     if (State) {
13730b57cec5SDimitry Andric       // The return value is the comparison result, which we don't know.
13745ffd83dbSDimitry Andric       SVal CmpV = Builder.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
13755ffd83dbSDimitry Andric       State = State->BindExpr(CE, LCtx, CmpV);
13765ffd83dbSDimitry Andric       C.addTransition(State);
13770b57cec5SDimitry Andric     }
13780b57cec5SDimitry Andric   }
13790b57cec5SDimitry Andric }
13800b57cec5SDimitry Andric 
13810b57cec5SDimitry Andric void CStringChecker::evalstrLength(CheckerContext &C,
13820b57cec5SDimitry Andric                                    const CallExpr *CE) const {
13830b57cec5SDimitry Andric   // size_t strlen(const char *s);
13840b57cec5SDimitry Andric   evalstrLengthCommon(C, CE, /* IsStrnlen = */ false);
13850b57cec5SDimitry Andric }
13860b57cec5SDimitry Andric 
13870b57cec5SDimitry Andric void CStringChecker::evalstrnLength(CheckerContext &C,
13880b57cec5SDimitry Andric                                     const CallExpr *CE) const {
13890b57cec5SDimitry Andric   // size_t strnlen(const char *s, size_t maxlen);
13900b57cec5SDimitry Andric   evalstrLengthCommon(C, CE, /* IsStrnlen = */ true);
13910b57cec5SDimitry Andric }
13920b57cec5SDimitry Andric 
13930b57cec5SDimitry Andric void CStringChecker::evalstrLengthCommon(CheckerContext &C, const CallExpr *CE,
13940b57cec5SDimitry Andric                                          bool IsStrnlen) const {
13950b57cec5SDimitry Andric   CurrentFunctionDescription = "string length function";
13960b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
13970b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
13980b57cec5SDimitry Andric 
13990b57cec5SDimitry Andric   if (IsStrnlen) {
14000b57cec5SDimitry Andric     const Expr *maxlenExpr = CE->getArg(1);
14010b57cec5SDimitry Andric     SVal maxlenVal = state->getSVal(maxlenExpr, LCtx);
14020b57cec5SDimitry Andric 
14030b57cec5SDimitry Andric     ProgramStateRef stateZeroSize, stateNonZeroSize;
14040b57cec5SDimitry Andric     std::tie(stateZeroSize, stateNonZeroSize) =
14050b57cec5SDimitry Andric       assumeZero(C, state, maxlenVal, maxlenExpr->getType());
14060b57cec5SDimitry Andric 
14070b57cec5SDimitry Andric     // If the size can be zero, the result will be 0 in that case, and we don't
14080b57cec5SDimitry Andric     // have to check the string itself.
14090b57cec5SDimitry Andric     if (stateZeroSize) {
14100b57cec5SDimitry Andric       SVal zero = C.getSValBuilder().makeZeroVal(CE->getType());
14110b57cec5SDimitry Andric       stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, zero);
14120b57cec5SDimitry Andric       C.addTransition(stateZeroSize);
14130b57cec5SDimitry Andric     }
14140b57cec5SDimitry Andric 
14150b57cec5SDimitry Andric     // If the size is GUARANTEED to be zero, we're done!
14160b57cec5SDimitry Andric     if (!stateNonZeroSize)
14170b57cec5SDimitry Andric       return;
14180b57cec5SDimitry Andric 
14190b57cec5SDimitry Andric     // Otherwise, record the assumption that the size is nonzero.
14200b57cec5SDimitry Andric     state = stateNonZeroSize;
14210b57cec5SDimitry Andric   }
14220b57cec5SDimitry Andric 
14230b57cec5SDimitry Andric   // Check that the string argument is non-null.
14245ffd83dbSDimitry Andric   AnyArgExpr Arg = {CE->getArg(0), 0};
14255ffd83dbSDimitry Andric   SVal ArgVal = state->getSVal(Arg.Expression, LCtx);
14265ffd83dbSDimitry Andric   state = checkNonNull(C, state, Arg, ArgVal);
14270b57cec5SDimitry Andric 
14280b57cec5SDimitry Andric   if (!state)
14290b57cec5SDimitry Andric     return;
14300b57cec5SDimitry Andric 
14315ffd83dbSDimitry Andric   SVal strLength = getCStringLength(C, state, Arg.Expression, ArgVal);
14320b57cec5SDimitry Andric 
14330b57cec5SDimitry Andric   // If the argument isn't a valid C string, there's no valid state to
14340b57cec5SDimitry Andric   // transition to.
14350b57cec5SDimitry Andric   if (strLength.isUndef())
14360b57cec5SDimitry Andric     return;
14370b57cec5SDimitry Andric 
14380b57cec5SDimitry Andric   DefinedOrUnknownSVal result = UnknownVal();
14390b57cec5SDimitry Andric 
14400b57cec5SDimitry Andric   // If the check is for strnlen() then bind the return value to no more than
14410b57cec5SDimitry Andric   // the maxlen value.
14420b57cec5SDimitry Andric   if (IsStrnlen) {
14430b57cec5SDimitry Andric     QualType cmpTy = C.getSValBuilder().getConditionType();
14440b57cec5SDimitry Andric 
14450b57cec5SDimitry Andric     // It's a little unfortunate to be getting this again,
14460b57cec5SDimitry Andric     // but it's not that expensive...
14470b57cec5SDimitry Andric     const Expr *maxlenExpr = CE->getArg(1);
14480b57cec5SDimitry Andric     SVal maxlenVal = state->getSVal(maxlenExpr, LCtx);
14490b57cec5SDimitry Andric 
14500b57cec5SDimitry Andric     Optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>();
14510b57cec5SDimitry Andric     Optional<NonLoc> maxlenValNL = maxlenVal.getAs<NonLoc>();
14520b57cec5SDimitry Andric 
14530b57cec5SDimitry Andric     if (strLengthNL && maxlenValNL) {
14540b57cec5SDimitry Andric       ProgramStateRef stateStringTooLong, stateStringNotTooLong;
14550b57cec5SDimitry Andric 
14560b57cec5SDimitry Andric       // Check if the strLength is greater than the maxlen.
14570b57cec5SDimitry Andric       std::tie(stateStringTooLong, stateStringNotTooLong) = state->assume(
14580b57cec5SDimitry Andric           C.getSValBuilder()
14590b57cec5SDimitry Andric               .evalBinOpNN(state, BO_GT, *strLengthNL, *maxlenValNL, cmpTy)
14600b57cec5SDimitry Andric               .castAs<DefinedOrUnknownSVal>());
14610b57cec5SDimitry Andric 
14620b57cec5SDimitry Andric       if (stateStringTooLong && !stateStringNotTooLong) {
14630b57cec5SDimitry Andric         // If the string is longer than maxlen, return maxlen.
14640b57cec5SDimitry Andric         result = *maxlenValNL;
14650b57cec5SDimitry Andric       } else if (stateStringNotTooLong && !stateStringTooLong) {
14660b57cec5SDimitry Andric         // If the string is shorter than maxlen, return its length.
14670b57cec5SDimitry Andric         result = *strLengthNL;
14680b57cec5SDimitry Andric       }
14690b57cec5SDimitry Andric     }
14700b57cec5SDimitry Andric 
14710b57cec5SDimitry Andric     if (result.isUnknown()) {
14720b57cec5SDimitry Andric       // If we don't have enough information for a comparison, there's
14730b57cec5SDimitry Andric       // no guarantee the full string length will actually be returned.
14740b57cec5SDimitry Andric       // All we know is the return value is the min of the string length
14750b57cec5SDimitry Andric       // and the limit. This is better than nothing.
14760b57cec5SDimitry Andric       result = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx,
14770b57cec5SDimitry Andric                                                    C.blockCount());
14780b57cec5SDimitry Andric       NonLoc resultNL = result.castAs<NonLoc>();
14790b57cec5SDimitry Andric 
14800b57cec5SDimitry Andric       if (strLengthNL) {
14810b57cec5SDimitry Andric         state = state->assume(C.getSValBuilder().evalBinOpNN(
14820b57cec5SDimitry Andric                                   state, BO_LE, resultNL, *strLengthNL, cmpTy)
14830b57cec5SDimitry Andric                                   .castAs<DefinedOrUnknownSVal>(), true);
14840b57cec5SDimitry Andric       }
14850b57cec5SDimitry Andric 
14860b57cec5SDimitry Andric       if (maxlenValNL) {
14870b57cec5SDimitry Andric         state = state->assume(C.getSValBuilder().evalBinOpNN(
14880b57cec5SDimitry Andric                                   state, BO_LE, resultNL, *maxlenValNL, cmpTy)
14890b57cec5SDimitry Andric                                   .castAs<DefinedOrUnknownSVal>(), true);
14900b57cec5SDimitry Andric       }
14910b57cec5SDimitry Andric     }
14920b57cec5SDimitry Andric 
14930b57cec5SDimitry Andric   } else {
14940b57cec5SDimitry Andric     // This is a plain strlen(), not strnlen().
14950b57cec5SDimitry Andric     result = strLength.castAs<DefinedOrUnknownSVal>();
14960b57cec5SDimitry Andric 
14970b57cec5SDimitry Andric     // If we don't know the length of the string, conjure a return
14980b57cec5SDimitry Andric     // value, so it can be used in constraints, at least.
14990b57cec5SDimitry Andric     if (result.isUnknown()) {
15000b57cec5SDimitry Andric       result = C.getSValBuilder().conjureSymbolVal(nullptr, CE, LCtx,
15010b57cec5SDimitry Andric                                                    C.blockCount());
15020b57cec5SDimitry Andric     }
15030b57cec5SDimitry Andric   }
15040b57cec5SDimitry Andric 
15050b57cec5SDimitry Andric   // Bind the return value.
15060b57cec5SDimitry Andric   assert(!result.isUnknown() && "Should have conjured a value by now");
15070b57cec5SDimitry Andric   state = state->BindExpr(CE, LCtx, result);
15080b57cec5SDimitry Andric   C.addTransition(state);
15090b57cec5SDimitry Andric }
15100b57cec5SDimitry Andric 
15110b57cec5SDimitry Andric void CStringChecker::evalStrcpy(CheckerContext &C, const CallExpr *CE) const {
15120b57cec5SDimitry Andric   // char *strcpy(char *restrict dst, const char *restrict src);
15130b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
1514480093f4SDimitry Andric                    /* ReturnEnd = */ false,
1515480093f4SDimitry Andric                    /* IsBounded = */ false,
1516480093f4SDimitry Andric                    /* appendK = */ ConcatFnKind::none);
15170b57cec5SDimitry Andric }
15180b57cec5SDimitry Andric 
15190b57cec5SDimitry Andric void CStringChecker::evalStrncpy(CheckerContext &C, const CallExpr *CE) const {
15200b57cec5SDimitry Andric   // char *strncpy(char *restrict dst, const char *restrict src, size_t n);
15210b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
1522480093f4SDimitry Andric                    /* ReturnEnd = */ false,
1523480093f4SDimitry Andric                    /* IsBounded = */ true,
1524480093f4SDimitry Andric                    /* appendK = */ ConcatFnKind::none);
15250b57cec5SDimitry Andric }
15260b57cec5SDimitry Andric 
15270b57cec5SDimitry Andric void CStringChecker::evalStpcpy(CheckerContext &C, const CallExpr *CE) const {
15280b57cec5SDimitry Andric   // char *stpcpy(char *restrict dst, const char *restrict src);
15290b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
1530480093f4SDimitry Andric                    /* ReturnEnd = */ true,
1531480093f4SDimitry Andric                    /* IsBounded = */ false,
1532480093f4SDimitry Andric                    /* appendK = */ ConcatFnKind::none);
15330b57cec5SDimitry Andric }
15340b57cec5SDimitry Andric 
15350b57cec5SDimitry Andric void CStringChecker::evalStrlcpy(CheckerContext &C, const CallExpr *CE) const {
1536480093f4SDimitry Andric   // size_t strlcpy(char *dest, const char *src, size_t size);
15370b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
1538480093f4SDimitry Andric                    /* ReturnEnd = */ true,
1539480093f4SDimitry Andric                    /* IsBounded = */ true,
1540480093f4SDimitry Andric                    /* appendK = */ ConcatFnKind::none,
15410b57cec5SDimitry Andric                    /* returnPtr = */ false);
15420b57cec5SDimitry Andric }
15430b57cec5SDimitry Andric 
15440b57cec5SDimitry Andric void CStringChecker::evalStrcat(CheckerContext &C, const CallExpr *CE) const {
15450b57cec5SDimitry Andric   // char *strcat(char *restrict s1, const char *restrict s2);
15460b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
1547480093f4SDimitry Andric                    /* ReturnEnd = */ false,
1548480093f4SDimitry Andric                    /* IsBounded = */ false,
1549480093f4SDimitry Andric                    /* appendK = */ ConcatFnKind::strcat);
15500b57cec5SDimitry Andric }
15510b57cec5SDimitry Andric 
15520b57cec5SDimitry Andric void CStringChecker::evalStrncat(CheckerContext &C, const CallExpr *CE) const {
15530b57cec5SDimitry Andric   // char *strncat(char *restrict s1, const char *restrict s2, size_t n);
15540b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
1555480093f4SDimitry Andric                    /* ReturnEnd = */ false,
1556480093f4SDimitry Andric                    /* IsBounded = */ true,
1557480093f4SDimitry Andric                    /* appendK = */ ConcatFnKind::strcat);
15580b57cec5SDimitry Andric }
15590b57cec5SDimitry Andric 
15600b57cec5SDimitry Andric void CStringChecker::evalStrlcat(CheckerContext &C, const CallExpr *CE) const {
1561480093f4SDimitry Andric   // size_t strlcat(char *dst, const char *src, size_t size);
1562480093f4SDimitry Andric   // It will append at most size - strlen(dst) - 1 bytes,
1563480093f4SDimitry Andric   // NULL-terminating the result.
15640b57cec5SDimitry Andric   evalStrcpyCommon(C, CE,
1565480093f4SDimitry Andric                    /* ReturnEnd = */ false,
1566480093f4SDimitry Andric                    /* IsBounded = */ true,
1567480093f4SDimitry Andric                    /* appendK = */ ConcatFnKind::strlcat,
15680b57cec5SDimitry Andric                    /* returnPtr = */ false);
15690b57cec5SDimitry Andric }
15700b57cec5SDimitry Andric 
15710b57cec5SDimitry Andric void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE,
1572480093f4SDimitry Andric                                       bool ReturnEnd, bool IsBounded,
1573480093f4SDimitry Andric                                       ConcatFnKind appendK,
1574480093f4SDimitry Andric                                       bool returnPtr) const {
1575480093f4SDimitry Andric   if (appendK == ConcatFnKind::none)
15760b57cec5SDimitry Andric     CurrentFunctionDescription = "string copy function";
1577480093f4SDimitry Andric   else
1578480093f4SDimitry Andric     CurrentFunctionDescription = "string concatenation function";
15795ffd83dbSDimitry Andric 
15800b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
15810b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
15820b57cec5SDimitry Andric 
15830b57cec5SDimitry Andric   // Check that the destination is non-null.
15845ffd83dbSDimitry Andric   DestinationArgExpr Dst = {CE->getArg(0), 0};
15855ffd83dbSDimitry Andric   SVal DstVal = state->getSVal(Dst.Expression, LCtx);
15865ffd83dbSDimitry Andric   state = checkNonNull(C, state, Dst, DstVal);
15870b57cec5SDimitry Andric   if (!state)
15880b57cec5SDimitry Andric     return;
15890b57cec5SDimitry Andric 
15900b57cec5SDimitry Andric   // Check that the source is non-null.
15915ffd83dbSDimitry Andric   SourceArgExpr srcExpr = {CE->getArg(1), 1};
15925ffd83dbSDimitry Andric   SVal srcVal = state->getSVal(srcExpr.Expression, LCtx);
15935ffd83dbSDimitry Andric   state = checkNonNull(C, state, srcExpr, srcVal);
15940b57cec5SDimitry Andric   if (!state)
15950b57cec5SDimitry Andric     return;
15960b57cec5SDimitry Andric 
15970b57cec5SDimitry Andric   // Get the string length of the source.
15985ffd83dbSDimitry Andric   SVal strLength = getCStringLength(C, state, srcExpr.Expression, srcVal);
1599480093f4SDimitry Andric   Optional<NonLoc> strLengthNL = strLength.getAs<NonLoc>();
1600480093f4SDimitry Andric 
1601480093f4SDimitry Andric   // Get the string length of the destination buffer.
16025ffd83dbSDimitry Andric   SVal dstStrLength = getCStringLength(C, state, Dst.Expression, DstVal);
1603480093f4SDimitry Andric   Optional<NonLoc> dstStrLengthNL = dstStrLength.getAs<NonLoc>();
16040b57cec5SDimitry Andric 
16050b57cec5SDimitry Andric   // If the source isn't a valid C string, give up.
16060b57cec5SDimitry Andric   if (strLength.isUndef())
16070b57cec5SDimitry Andric     return;
16080b57cec5SDimitry Andric 
16090b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
16100b57cec5SDimitry Andric   QualType cmpTy = svalBuilder.getConditionType();
16110b57cec5SDimitry Andric   QualType sizeTy = svalBuilder.getContext().getSizeType();
16120b57cec5SDimitry Andric 
16130b57cec5SDimitry Andric   // These two values allow checking two kinds of errors:
16140b57cec5SDimitry Andric   // - actual overflows caused by a source that doesn't fit in the destination
16150b57cec5SDimitry Andric   // - potential overflows caused by a bound that could exceed the destination
16160b57cec5SDimitry Andric   SVal amountCopied = UnknownVal();
16170b57cec5SDimitry Andric   SVal maxLastElementIndex = UnknownVal();
16180b57cec5SDimitry Andric   const char *boundWarning = nullptr;
16190b57cec5SDimitry Andric 
16205ffd83dbSDimitry Andric   // FIXME: Why do we choose the srcExpr if the access has no size?
16215ffd83dbSDimitry Andric   //  Note that the 3rd argument of the call would be the size parameter.
16225ffd83dbSDimitry Andric   SizeArgExpr SrcExprAsSizeDummy = {srcExpr.Expression, srcExpr.ArgumentIndex};
16235ffd83dbSDimitry Andric   state = CheckOverlap(
16245ffd83dbSDimitry Andric       C, state,
16255ffd83dbSDimitry Andric       (IsBounded ? SizeArgExpr{CE->getArg(2), 2} : SrcExprAsSizeDummy), Dst,
1626480093f4SDimitry Andric       srcExpr);
16270b57cec5SDimitry Andric 
16280b57cec5SDimitry Andric   if (!state)
16290b57cec5SDimitry Andric     return;
16300b57cec5SDimitry Andric 
16310b57cec5SDimitry Andric   // If the function is strncpy, strncat, etc... it is bounded.
1632480093f4SDimitry Andric   if (IsBounded) {
16330b57cec5SDimitry Andric     // Get the max number of characters to copy.
16345ffd83dbSDimitry Andric     SizeArgExpr lenExpr = {CE->getArg(2), 2};
16355ffd83dbSDimitry Andric     SVal lenVal = state->getSVal(lenExpr.Expression, LCtx);
16360b57cec5SDimitry Andric 
16370b57cec5SDimitry Andric     // Protect against misdeclared strncpy().
16385ffd83dbSDimitry Andric     lenVal =
16395ffd83dbSDimitry Andric         svalBuilder.evalCast(lenVal, sizeTy, lenExpr.Expression->getType());
16400b57cec5SDimitry Andric 
16410b57cec5SDimitry Andric     Optional<NonLoc> lenValNL = lenVal.getAs<NonLoc>();
16420b57cec5SDimitry Andric 
16430b57cec5SDimitry Andric     // If we know both values, we might be able to figure out how much
16440b57cec5SDimitry Andric     // we're copying.
16450b57cec5SDimitry Andric     if (strLengthNL && lenValNL) {
1646480093f4SDimitry Andric       switch (appendK) {
1647480093f4SDimitry Andric       case ConcatFnKind::none:
1648480093f4SDimitry Andric       case ConcatFnKind::strcat: {
16490b57cec5SDimitry Andric         ProgramStateRef stateSourceTooLong, stateSourceNotTooLong;
16500b57cec5SDimitry Andric         // Check if the max number to copy is less than the length of the src.
16510b57cec5SDimitry Andric         // If the bound is equal to the source length, strncpy won't null-
16520b57cec5SDimitry Andric         // terminate the result!
16530b57cec5SDimitry Andric         std::tie(stateSourceTooLong, stateSourceNotTooLong) = state->assume(
1654480093f4SDimitry Andric             svalBuilder
1655480093f4SDimitry Andric                 .evalBinOpNN(state, BO_GE, *strLengthNL, *lenValNL, cmpTy)
16560b57cec5SDimitry Andric                 .castAs<DefinedOrUnknownSVal>());
16570b57cec5SDimitry Andric 
16580b57cec5SDimitry Andric         if (stateSourceTooLong && !stateSourceNotTooLong) {
1659480093f4SDimitry Andric           // Max number to copy is less than the length of the src, so the
1660480093f4SDimitry Andric           // actual strLength copied is the max number arg.
16610b57cec5SDimitry Andric           state = stateSourceTooLong;
16620b57cec5SDimitry Andric           amountCopied = lenVal;
16630b57cec5SDimitry Andric 
16640b57cec5SDimitry Andric         } else if (!stateSourceTooLong && stateSourceNotTooLong) {
16650b57cec5SDimitry Andric           // The source buffer entirely fits in the bound.
16660b57cec5SDimitry Andric           state = stateSourceNotTooLong;
16670b57cec5SDimitry Andric           amountCopied = strLength;
16680b57cec5SDimitry Andric         }
1669480093f4SDimitry Andric         break;
1670480093f4SDimitry Andric       }
1671480093f4SDimitry Andric       case ConcatFnKind::strlcat:
1672480093f4SDimitry Andric         if (!dstStrLengthNL)
1673480093f4SDimitry Andric           return;
1674480093f4SDimitry Andric 
1675480093f4SDimitry Andric         // amountCopied = min (size - dstLen - 1 , srcLen)
1676480093f4SDimitry Andric         SVal freeSpace = svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL,
1677480093f4SDimitry Andric                                                  *dstStrLengthNL, sizeTy);
167881ad6265SDimitry Andric         if (!isa<NonLoc>(freeSpace))
1679480093f4SDimitry Andric           return;
1680480093f4SDimitry Andric         freeSpace =
1681480093f4SDimitry Andric             svalBuilder.evalBinOp(state, BO_Sub, freeSpace,
1682480093f4SDimitry Andric                                   svalBuilder.makeIntVal(1, sizeTy), sizeTy);
1683480093f4SDimitry Andric         Optional<NonLoc> freeSpaceNL = freeSpace.getAs<NonLoc>();
1684480093f4SDimitry Andric 
1685480093f4SDimitry Andric         // While unlikely, it is possible that the subtraction is
1686480093f4SDimitry Andric         // too complex to compute, let's check whether it succeeded.
1687480093f4SDimitry Andric         if (!freeSpaceNL)
1688480093f4SDimitry Andric           return;
1689480093f4SDimitry Andric         SVal hasEnoughSpace = svalBuilder.evalBinOpNN(
1690480093f4SDimitry Andric             state, BO_LE, *strLengthNL, *freeSpaceNL, cmpTy);
1691480093f4SDimitry Andric 
1692480093f4SDimitry Andric         ProgramStateRef TrueState, FalseState;
1693480093f4SDimitry Andric         std::tie(TrueState, FalseState) =
1694480093f4SDimitry Andric             state->assume(hasEnoughSpace.castAs<DefinedOrUnknownSVal>());
1695480093f4SDimitry Andric 
1696480093f4SDimitry Andric         // srcStrLength <= size - dstStrLength -1
1697480093f4SDimitry Andric         if (TrueState && !FalseState) {
1698480093f4SDimitry Andric           amountCopied = strLength;
16990b57cec5SDimitry Andric         }
17000b57cec5SDimitry Andric 
1701480093f4SDimitry Andric         // srcStrLength > size - dstStrLength -1
1702480093f4SDimitry Andric         if (!TrueState && FalseState) {
1703480093f4SDimitry Andric           amountCopied = freeSpace;
1704480093f4SDimitry Andric         }
1705480093f4SDimitry Andric 
1706480093f4SDimitry Andric         if (TrueState && FalseState)
1707480093f4SDimitry Andric           amountCopied = UnknownVal();
1708480093f4SDimitry Andric         break;
1709480093f4SDimitry Andric       }
1710480093f4SDimitry Andric     }
17110b57cec5SDimitry Andric     // We still want to know if the bound is known to be too large.
17120b57cec5SDimitry Andric     if (lenValNL) {
1713480093f4SDimitry Andric       switch (appendK) {
1714480093f4SDimitry Andric       case ConcatFnKind::strcat:
17150b57cec5SDimitry Andric         // For strncat, the check is strlen(dst) + lenVal < sizeof(dst)
17160b57cec5SDimitry Andric 
17170b57cec5SDimitry Andric         // Get the string length of the destination. If the destination is
17180b57cec5SDimitry Andric         // memory that can't have a string length, we shouldn't be copying
17190b57cec5SDimitry Andric         // into it anyway.
17200b57cec5SDimitry Andric         if (dstStrLength.isUndef())
17210b57cec5SDimitry Andric           return;
17220b57cec5SDimitry Andric 
1723480093f4SDimitry Andric         if (dstStrLengthNL) {
1724480093f4SDimitry Andric           maxLastElementIndex = svalBuilder.evalBinOpNN(
1725480093f4SDimitry Andric               state, BO_Add, *lenValNL, *dstStrLengthNL, sizeTy);
1726480093f4SDimitry Andric 
17270b57cec5SDimitry Andric           boundWarning = "Size argument is greater than the free space in the "
17280b57cec5SDimitry Andric                          "destination buffer";
17290b57cec5SDimitry Andric         }
1730480093f4SDimitry Andric         break;
1731480093f4SDimitry Andric       case ConcatFnKind::none:
1732480093f4SDimitry Andric       case ConcatFnKind::strlcat:
1733480093f4SDimitry Andric         // For strncpy and strlcat, this is just checking
1734480093f4SDimitry Andric         //  that lenVal <= sizeof(dst).
17350b57cec5SDimitry Andric         // (Yes, strncpy and strncat differ in how they treat termination.
17360b57cec5SDimitry Andric         // strncat ALWAYS terminates, but strncpy doesn't.)
17370b57cec5SDimitry Andric 
17380b57cec5SDimitry Andric         // We need a special case for when the copy size is zero, in which
17390b57cec5SDimitry Andric         // case strncpy will do no work at all. Our bounds check uses n-1
17400b57cec5SDimitry Andric         // as the last element accessed, so n == 0 is problematic.
17410b57cec5SDimitry Andric         ProgramStateRef StateZeroSize, StateNonZeroSize;
17420b57cec5SDimitry Andric         std::tie(StateZeroSize, StateNonZeroSize) =
17430b57cec5SDimitry Andric             assumeZero(C, state, *lenValNL, sizeTy);
17440b57cec5SDimitry Andric 
17450b57cec5SDimitry Andric         // If the size is known to be zero, we're done.
17460b57cec5SDimitry Andric         if (StateZeroSize && !StateNonZeroSize) {
17470b57cec5SDimitry Andric           if (returnPtr) {
17480b57cec5SDimitry Andric             StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, DstVal);
17490b57cec5SDimitry Andric           } else {
1750480093f4SDimitry Andric             if (appendK == ConcatFnKind::none) {
1751480093f4SDimitry Andric               // strlcpy returns strlen(src)
1752480093f4SDimitry Andric               StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, strLength);
1753480093f4SDimitry Andric             } else {
1754480093f4SDimitry Andric               // strlcat returns strlen(src) + strlen(dst)
1755480093f4SDimitry Andric               SVal retSize = svalBuilder.evalBinOp(
1756480093f4SDimitry Andric                   state, BO_Add, strLength, dstStrLength, sizeTy);
1757480093f4SDimitry Andric               StateZeroSize = StateZeroSize->BindExpr(CE, LCtx, retSize);
1758480093f4SDimitry Andric             }
17590b57cec5SDimitry Andric           }
17600b57cec5SDimitry Andric           C.addTransition(StateZeroSize);
17610b57cec5SDimitry Andric           return;
17620b57cec5SDimitry Andric         }
17630b57cec5SDimitry Andric 
17640b57cec5SDimitry Andric         // Otherwise, go ahead and figure out the last element we'll touch.
17650b57cec5SDimitry Andric         // We don't record the non-zero assumption here because we can't
17660b57cec5SDimitry Andric         // be sure. We won't warn on a possible zero.
17670b57cec5SDimitry Andric         NonLoc one = svalBuilder.makeIntVal(1, sizeTy).castAs<NonLoc>();
1768480093f4SDimitry Andric         maxLastElementIndex =
1769480093f4SDimitry Andric             svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL, one, sizeTy);
17700b57cec5SDimitry Andric         boundWarning = "Size argument is greater than the length of the "
17710b57cec5SDimitry Andric                        "destination buffer";
1772480093f4SDimitry Andric         break;
17730b57cec5SDimitry Andric       }
17740b57cec5SDimitry Andric     }
17750b57cec5SDimitry Andric   } else {
17760b57cec5SDimitry Andric     // The function isn't bounded. The amount copied should match the length
17770b57cec5SDimitry Andric     // of the source buffer.
17780b57cec5SDimitry Andric     amountCopied = strLength;
17790b57cec5SDimitry Andric   }
17800b57cec5SDimitry Andric 
17810b57cec5SDimitry Andric   assert(state);
17820b57cec5SDimitry Andric 
17830b57cec5SDimitry Andric   // This represents the number of characters copied into the destination
17840b57cec5SDimitry Andric   // buffer. (It may not actually be the strlen if the destination buffer
17850b57cec5SDimitry Andric   // is not terminated.)
17860b57cec5SDimitry Andric   SVal finalStrLength = UnknownVal();
1787480093f4SDimitry Andric   SVal strlRetVal = UnknownVal();
1788480093f4SDimitry Andric 
1789480093f4SDimitry Andric   if (appendK == ConcatFnKind::none && !returnPtr) {
1790480093f4SDimitry Andric     // strlcpy returns the sizeof(src)
1791480093f4SDimitry Andric     strlRetVal = strLength;
1792480093f4SDimitry Andric   }
17930b57cec5SDimitry Andric 
17940b57cec5SDimitry Andric   // If this is an appending function (strcat, strncat...) then set the
17950b57cec5SDimitry Andric   // string length to strlen(src) + strlen(dst) since the buffer will
17960b57cec5SDimitry Andric   // ultimately contain both.
1797480093f4SDimitry Andric   if (appendK != ConcatFnKind::none) {
17980b57cec5SDimitry Andric     // Get the string length of the destination. If the destination is memory
17990b57cec5SDimitry Andric     // that can't have a string length, we shouldn't be copying into it anyway.
18000b57cec5SDimitry Andric     if (dstStrLength.isUndef())
18010b57cec5SDimitry Andric       return;
18020b57cec5SDimitry Andric 
1803480093f4SDimitry Andric     if (appendK == ConcatFnKind::strlcat && dstStrLengthNL && strLengthNL) {
1804480093f4SDimitry Andric       strlRetVal = svalBuilder.evalBinOpNN(state, BO_Add, *strLengthNL,
1805480093f4SDimitry Andric                                            *dstStrLengthNL, sizeTy);
1806480093f4SDimitry Andric     }
1807480093f4SDimitry Andric 
1808480093f4SDimitry Andric     Optional<NonLoc> amountCopiedNL = amountCopied.getAs<NonLoc>();
18090b57cec5SDimitry Andric 
18100b57cec5SDimitry Andric     // If we know both string lengths, we might know the final string length.
1811480093f4SDimitry Andric     if (amountCopiedNL && dstStrLengthNL) {
18120b57cec5SDimitry Andric       // Make sure the two lengths together don't overflow a size_t.
1813480093f4SDimitry Andric       state = checkAdditionOverflow(C, state, *amountCopiedNL, *dstStrLengthNL);
18140b57cec5SDimitry Andric       if (!state)
18150b57cec5SDimitry Andric         return;
18160b57cec5SDimitry Andric 
1817480093f4SDimitry Andric       finalStrLength = svalBuilder.evalBinOpNN(state, BO_Add, *amountCopiedNL,
18180b57cec5SDimitry Andric                                                *dstStrLengthNL, sizeTy);
18190b57cec5SDimitry Andric     }
18200b57cec5SDimitry Andric 
18210b57cec5SDimitry Andric     // If we couldn't get a single value for the final string length,
18220b57cec5SDimitry Andric     // we can at least bound it by the individual lengths.
18230b57cec5SDimitry Andric     if (finalStrLength.isUnknown()) {
18240b57cec5SDimitry Andric       // Try to get a "hypothetical" string length symbol, which we can later
18250b57cec5SDimitry Andric       // set as a real value if that turns out to be the case.
18260b57cec5SDimitry Andric       finalStrLength = getCStringLength(C, state, CE, DstVal, true);
18270b57cec5SDimitry Andric       assert(!finalStrLength.isUndef());
18280b57cec5SDimitry Andric 
18290b57cec5SDimitry Andric       if (Optional<NonLoc> finalStrLengthNL = finalStrLength.getAs<NonLoc>()) {
1830480093f4SDimitry Andric         if (amountCopiedNL && appendK == ConcatFnKind::none) {
1831480093f4SDimitry Andric           // we overwrite dst string with the src
18320b57cec5SDimitry Andric           // finalStrLength >= srcStrLength
1833480093f4SDimitry Andric           SVal sourceInResult = svalBuilder.evalBinOpNN(
1834480093f4SDimitry Andric               state, BO_GE, *finalStrLengthNL, *amountCopiedNL, cmpTy);
18350b57cec5SDimitry Andric           state = state->assume(sourceInResult.castAs<DefinedOrUnknownSVal>(),
18360b57cec5SDimitry Andric                                 true);
18370b57cec5SDimitry Andric           if (!state)
18380b57cec5SDimitry Andric             return;
18390b57cec5SDimitry Andric         }
18400b57cec5SDimitry Andric 
1841480093f4SDimitry Andric         if (dstStrLengthNL && appendK != ConcatFnKind::none) {
1842480093f4SDimitry Andric           // we extend the dst string with the src
18430b57cec5SDimitry Andric           // finalStrLength >= dstStrLength
18440b57cec5SDimitry Andric           SVal destInResult = svalBuilder.evalBinOpNN(state, BO_GE,
18450b57cec5SDimitry Andric                                                       *finalStrLengthNL,
18460b57cec5SDimitry Andric                                                       *dstStrLengthNL,
18470b57cec5SDimitry Andric                                                       cmpTy);
18480b57cec5SDimitry Andric           state =
18490b57cec5SDimitry Andric               state->assume(destInResult.castAs<DefinedOrUnknownSVal>(), true);
18500b57cec5SDimitry Andric           if (!state)
18510b57cec5SDimitry Andric             return;
18520b57cec5SDimitry Andric         }
18530b57cec5SDimitry Andric       }
18540b57cec5SDimitry Andric     }
18550b57cec5SDimitry Andric 
18560b57cec5SDimitry Andric   } else {
18570b57cec5SDimitry Andric     // Otherwise, this is a copy-over function (strcpy, strncpy, ...), and
18580b57cec5SDimitry Andric     // the final string length will match the input string length.
18590b57cec5SDimitry Andric     finalStrLength = amountCopied;
18600b57cec5SDimitry Andric   }
18610b57cec5SDimitry Andric 
18620b57cec5SDimitry Andric   SVal Result;
18630b57cec5SDimitry Andric 
18640b57cec5SDimitry Andric   if (returnPtr) {
18650b57cec5SDimitry Andric     // The final result of the function will either be a pointer past the last
18660b57cec5SDimitry Andric     // copied element, or a pointer to the start of the destination buffer.
1867480093f4SDimitry Andric     Result = (ReturnEnd ? UnknownVal() : DstVal);
18680b57cec5SDimitry Andric   } else {
1869480093f4SDimitry Andric     if (appendK == ConcatFnKind::strlcat || appendK == ConcatFnKind::none)
1870480093f4SDimitry Andric       //strlcpy, strlcat
1871480093f4SDimitry Andric       Result = strlRetVal;
1872480093f4SDimitry Andric     else
18730b57cec5SDimitry Andric       Result = finalStrLength;
18740b57cec5SDimitry Andric   }
18750b57cec5SDimitry Andric 
18760b57cec5SDimitry Andric   assert(state);
18770b57cec5SDimitry Andric 
18780b57cec5SDimitry Andric   // If the destination is a MemRegion, try to check for a buffer overflow and
18790b57cec5SDimitry Andric   // record the new string length.
18800b57cec5SDimitry Andric   if (Optional<loc::MemRegionVal> dstRegVal =
18810b57cec5SDimitry Andric       DstVal.getAs<loc::MemRegionVal>()) {
18825ffd83dbSDimitry Andric     QualType ptrTy = Dst.Expression->getType();
18830b57cec5SDimitry Andric 
18840b57cec5SDimitry Andric     // If we have an exact value on a bounded copy, use that to check for
18850b57cec5SDimitry Andric     // overflows, rather than our estimate about how much is actually copied.
18860b57cec5SDimitry Andric     if (Optional<NonLoc> maxLastNL = maxLastElementIndex.getAs<NonLoc>()) {
18875ffd83dbSDimitry Andric       SVal maxLastElement =
18885ffd83dbSDimitry Andric           svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal, *maxLastNL, ptrTy);
18895ffd83dbSDimitry Andric 
18905ffd83dbSDimitry Andric       state = CheckLocation(C, state, Dst, maxLastElement, AccessKind::write);
18910b57cec5SDimitry Andric       if (!state)
18920b57cec5SDimitry Andric         return;
18930b57cec5SDimitry Andric     }
18940b57cec5SDimitry Andric 
18950b57cec5SDimitry Andric     // Then, if the final length is known...
18960b57cec5SDimitry Andric     if (Optional<NonLoc> knownStrLength = finalStrLength.getAs<NonLoc>()) {
18970b57cec5SDimitry Andric       SVal lastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal,
18980b57cec5SDimitry Andric           *knownStrLength, ptrTy);
18990b57cec5SDimitry Andric 
19000b57cec5SDimitry Andric       // ...and we haven't checked the bound, we'll check the actual copy.
19010b57cec5SDimitry Andric       if (!boundWarning) {
19025ffd83dbSDimitry Andric         state = CheckLocation(C, state, Dst, lastElement, AccessKind::write);
19030b57cec5SDimitry Andric         if (!state)
19040b57cec5SDimitry Andric           return;
19050b57cec5SDimitry Andric       }
19060b57cec5SDimitry Andric 
19070b57cec5SDimitry Andric       // If this is a stpcpy-style copy, the last element is the return value.
1908480093f4SDimitry Andric       if (returnPtr && ReturnEnd)
19090b57cec5SDimitry Andric         Result = lastElement;
19100b57cec5SDimitry Andric     }
19110b57cec5SDimitry Andric 
19120b57cec5SDimitry Andric     // Invalidate the destination (regular invalidation without pointer-escaping
19130b57cec5SDimitry Andric     // the address of the top-level region). This must happen before we set the
19140b57cec5SDimitry Andric     // C string length because invalidation will clear the length.
19150b57cec5SDimitry Andric     // FIXME: Even if we can't perfectly model the copy, we should see if we
19160b57cec5SDimitry Andric     // can use LazyCompoundVals to copy the source values into the destination.
19170b57cec5SDimitry Andric     // This would probably remove any existing bindings past the end of the
19180b57cec5SDimitry Andric     // string, but that's still an improvement over blank invalidation.
19195ffd83dbSDimitry Andric     state = InvalidateBuffer(C, state, Dst.Expression, *dstRegVal,
19200b57cec5SDimitry Andric                              /*IsSourceBuffer*/ false, nullptr);
19210b57cec5SDimitry Andric 
19220b57cec5SDimitry Andric     // Invalidate the source (const-invalidation without const-pointer-escaping
19230b57cec5SDimitry Andric     // the address of the top-level region).
19245ffd83dbSDimitry Andric     state = InvalidateBuffer(C, state, srcExpr.Expression, srcVal,
19255ffd83dbSDimitry Andric                              /*IsSourceBuffer*/ true, nullptr);
19260b57cec5SDimitry Andric 
19270b57cec5SDimitry Andric     // Set the C string length of the destination, if we know it.
1928480093f4SDimitry Andric     if (IsBounded && (appendK == ConcatFnKind::none)) {
19290b57cec5SDimitry Andric       // strncpy is annoying in that it doesn't guarantee to null-terminate
19300b57cec5SDimitry Andric       // the result string. If the original string didn't fit entirely inside
19310b57cec5SDimitry Andric       // the bound (including the null-terminator), we don't know how long the
19320b57cec5SDimitry Andric       // result is.
19330b57cec5SDimitry Andric       if (amountCopied != strLength)
19340b57cec5SDimitry Andric         finalStrLength = UnknownVal();
19350b57cec5SDimitry Andric     }
19360b57cec5SDimitry Andric     state = setCStringLength(state, dstRegVal->getRegion(), finalStrLength);
19370b57cec5SDimitry Andric   }
19380b57cec5SDimitry Andric 
19390b57cec5SDimitry Andric   assert(state);
19400b57cec5SDimitry Andric 
19410b57cec5SDimitry Andric   if (returnPtr) {
19420b57cec5SDimitry Andric     // If this is a stpcpy-style copy, but we were unable to check for a buffer
19430b57cec5SDimitry Andric     // overflow, we still need a result. Conjure a return value.
1944480093f4SDimitry Andric     if (ReturnEnd && Result.isUnknown()) {
19450b57cec5SDimitry Andric       Result = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
19460b57cec5SDimitry Andric     }
19470b57cec5SDimitry Andric   }
19480b57cec5SDimitry Andric   // Set the return value.
19490b57cec5SDimitry Andric   state = state->BindExpr(CE, LCtx, Result);
19500b57cec5SDimitry Andric   C.addTransition(state);
19510b57cec5SDimitry Andric }
19520b57cec5SDimitry Andric 
19530b57cec5SDimitry Andric void CStringChecker::evalStrcmp(CheckerContext &C, const CallExpr *CE) const {
19540b57cec5SDimitry Andric   //int strcmp(const char *s1, const char *s2);
1955480093f4SDimitry Andric   evalStrcmpCommon(C, CE, /* IsBounded = */ false, /* IgnoreCase = */ false);
19560b57cec5SDimitry Andric }
19570b57cec5SDimitry Andric 
19580b57cec5SDimitry Andric void CStringChecker::evalStrncmp(CheckerContext &C, const CallExpr *CE) const {
19590b57cec5SDimitry Andric   //int strncmp(const char *s1, const char *s2, size_t n);
1960480093f4SDimitry Andric   evalStrcmpCommon(C, CE, /* IsBounded = */ true, /* IgnoreCase = */ false);
19610b57cec5SDimitry Andric }
19620b57cec5SDimitry Andric 
19630b57cec5SDimitry Andric void CStringChecker::evalStrcasecmp(CheckerContext &C,
19640b57cec5SDimitry Andric     const CallExpr *CE) const {
19650b57cec5SDimitry Andric   //int strcasecmp(const char *s1, const char *s2);
1966480093f4SDimitry Andric   evalStrcmpCommon(C, CE, /* IsBounded = */ false, /* IgnoreCase = */ true);
19670b57cec5SDimitry Andric }
19680b57cec5SDimitry Andric 
19690b57cec5SDimitry Andric void CStringChecker::evalStrncasecmp(CheckerContext &C,
19700b57cec5SDimitry Andric     const CallExpr *CE) const {
19710b57cec5SDimitry Andric   //int strncasecmp(const char *s1, const char *s2, size_t n);
1972480093f4SDimitry Andric   evalStrcmpCommon(C, CE, /* IsBounded = */ true, /* IgnoreCase = */ true);
19730b57cec5SDimitry Andric }
19740b57cec5SDimitry Andric 
19750b57cec5SDimitry Andric void CStringChecker::evalStrcmpCommon(CheckerContext &C, const CallExpr *CE,
1976480093f4SDimitry Andric     bool IsBounded, bool IgnoreCase) const {
19770b57cec5SDimitry Andric   CurrentFunctionDescription = "string comparison function";
19780b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
19790b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
19800b57cec5SDimitry Andric 
19810b57cec5SDimitry Andric   // Check that the first string is non-null
19825ffd83dbSDimitry Andric   AnyArgExpr Left = {CE->getArg(0), 0};
19835ffd83dbSDimitry Andric   SVal LeftVal = state->getSVal(Left.Expression, LCtx);
19845ffd83dbSDimitry Andric   state = checkNonNull(C, state, Left, LeftVal);
19850b57cec5SDimitry Andric   if (!state)
19860b57cec5SDimitry Andric     return;
19870b57cec5SDimitry Andric 
19880b57cec5SDimitry Andric   // Check that the second string is non-null.
19895ffd83dbSDimitry Andric   AnyArgExpr Right = {CE->getArg(1), 1};
19905ffd83dbSDimitry Andric   SVal RightVal = state->getSVal(Right.Expression, LCtx);
19915ffd83dbSDimitry Andric   state = checkNonNull(C, state, Right, RightVal);
19920b57cec5SDimitry Andric   if (!state)
19930b57cec5SDimitry Andric     return;
19940b57cec5SDimitry Andric 
19950b57cec5SDimitry Andric   // Get the string length of the first string or give up.
19965ffd83dbSDimitry Andric   SVal LeftLength = getCStringLength(C, state, Left.Expression, LeftVal);
19975ffd83dbSDimitry Andric   if (LeftLength.isUndef())
19980b57cec5SDimitry Andric     return;
19990b57cec5SDimitry Andric 
20000b57cec5SDimitry Andric   // Get the string length of the second string or give up.
20015ffd83dbSDimitry Andric   SVal RightLength = getCStringLength(C, state, Right.Expression, RightVal);
20025ffd83dbSDimitry Andric   if (RightLength.isUndef())
20030b57cec5SDimitry Andric     return;
20040b57cec5SDimitry Andric 
20050b57cec5SDimitry Andric   // If we know the two buffers are the same, we know the result is 0.
20060b57cec5SDimitry Andric   // First, get the two buffers' addresses. Another checker will have already
20070b57cec5SDimitry Andric   // made sure they're not undefined.
20085ffd83dbSDimitry Andric   DefinedOrUnknownSVal LV = LeftVal.castAs<DefinedOrUnknownSVal>();
20095ffd83dbSDimitry Andric   DefinedOrUnknownSVal RV = RightVal.castAs<DefinedOrUnknownSVal>();
20100b57cec5SDimitry Andric 
20110b57cec5SDimitry Andric   // See if they are the same.
20120b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
20130b57cec5SDimitry Andric   DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV);
20140b57cec5SDimitry Andric   ProgramStateRef StSameBuf, StNotSameBuf;
20150b57cec5SDimitry Andric   std::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf);
20160b57cec5SDimitry Andric 
20170b57cec5SDimitry Andric   // If the two arguments might be the same buffer, we know the result is 0,
20180b57cec5SDimitry Andric   // and we only need to check one size.
20190b57cec5SDimitry Andric   if (StSameBuf) {
20200b57cec5SDimitry Andric     StSameBuf = StSameBuf->BindExpr(CE, LCtx,
20210b57cec5SDimitry Andric         svalBuilder.makeZeroVal(CE->getType()));
20220b57cec5SDimitry Andric     C.addTransition(StSameBuf);
20230b57cec5SDimitry Andric 
20240b57cec5SDimitry Andric     // If the two arguments are GUARANTEED to be the same, we're done!
20250b57cec5SDimitry Andric     if (!StNotSameBuf)
20260b57cec5SDimitry Andric       return;
20270b57cec5SDimitry Andric   }
20280b57cec5SDimitry Andric 
20290b57cec5SDimitry Andric   assert(StNotSameBuf);
20300b57cec5SDimitry Andric   state = StNotSameBuf;
20310b57cec5SDimitry Andric 
20320b57cec5SDimitry Andric   // At this point we can go about comparing the two buffers.
20330b57cec5SDimitry Andric   // For now, we only do this if they're both known string literals.
20340b57cec5SDimitry Andric 
20350b57cec5SDimitry Andric   // Attempt to extract string literals from both expressions.
20365ffd83dbSDimitry Andric   const StringLiteral *LeftStrLiteral =
20375ffd83dbSDimitry Andric       getCStringLiteral(C, state, Left.Expression, LeftVal);
20385ffd83dbSDimitry Andric   const StringLiteral *RightStrLiteral =
20395ffd83dbSDimitry Andric       getCStringLiteral(C, state, Right.Expression, RightVal);
20400b57cec5SDimitry Andric   bool canComputeResult = false;
20410b57cec5SDimitry Andric   SVal resultVal = svalBuilder.conjureSymbolVal(nullptr, CE, LCtx,
20420b57cec5SDimitry Andric       C.blockCount());
20430b57cec5SDimitry Andric 
20445ffd83dbSDimitry Andric   if (LeftStrLiteral && RightStrLiteral) {
20455ffd83dbSDimitry Andric     StringRef LeftStrRef = LeftStrLiteral->getString();
20465ffd83dbSDimitry Andric     StringRef RightStrRef = RightStrLiteral->getString();
20470b57cec5SDimitry Andric 
2048480093f4SDimitry Andric     if (IsBounded) {
20490b57cec5SDimitry Andric       // Get the max number of characters to compare.
20500b57cec5SDimitry Andric       const Expr *lenExpr = CE->getArg(2);
20510b57cec5SDimitry Andric       SVal lenVal = state->getSVal(lenExpr, LCtx);
20520b57cec5SDimitry Andric 
20530b57cec5SDimitry Andric       // If the length is known, we can get the right substrings.
20540b57cec5SDimitry Andric       if (const llvm::APSInt *len = svalBuilder.getKnownValue(state, lenVal)) {
20550b57cec5SDimitry Andric         // Create substrings of each to compare the prefix.
20565ffd83dbSDimitry Andric         LeftStrRef = LeftStrRef.substr(0, (size_t)len->getZExtValue());
20575ffd83dbSDimitry Andric         RightStrRef = RightStrRef.substr(0, (size_t)len->getZExtValue());
20580b57cec5SDimitry Andric         canComputeResult = true;
20590b57cec5SDimitry Andric       }
20600b57cec5SDimitry Andric     } else {
20610b57cec5SDimitry Andric       // This is a normal, unbounded strcmp.
20620b57cec5SDimitry Andric       canComputeResult = true;
20630b57cec5SDimitry Andric     }
20640b57cec5SDimitry Andric 
20650b57cec5SDimitry Andric     if (canComputeResult) {
20660b57cec5SDimitry Andric       // Real strcmp stops at null characters.
20675ffd83dbSDimitry Andric       size_t s1Term = LeftStrRef.find('\0');
20680b57cec5SDimitry Andric       if (s1Term != StringRef::npos)
20695ffd83dbSDimitry Andric         LeftStrRef = LeftStrRef.substr(0, s1Term);
20700b57cec5SDimitry Andric 
20715ffd83dbSDimitry Andric       size_t s2Term = RightStrRef.find('\0');
20720b57cec5SDimitry Andric       if (s2Term != StringRef::npos)
20735ffd83dbSDimitry Andric         RightStrRef = RightStrRef.substr(0, s2Term);
20740b57cec5SDimitry Andric 
20750b57cec5SDimitry Andric       // Use StringRef's comparison methods to compute the actual result.
2076fe6060f1SDimitry Andric       int compareRes = IgnoreCase ? LeftStrRef.compare_insensitive(RightStrRef)
20775ffd83dbSDimitry Andric                                   : LeftStrRef.compare(RightStrRef);
20780b57cec5SDimitry Andric 
20790b57cec5SDimitry Andric       // The strcmp function returns an integer greater than, equal to, or less
20800b57cec5SDimitry Andric       // than zero, [c11, p7.24.4.2].
20810b57cec5SDimitry Andric       if (compareRes == 0) {
20820b57cec5SDimitry Andric         resultVal = svalBuilder.makeIntVal(compareRes, CE->getType());
20830b57cec5SDimitry Andric       }
20840b57cec5SDimitry Andric       else {
20850b57cec5SDimitry Andric         DefinedSVal zeroVal = svalBuilder.makeIntVal(0, CE->getType());
20860b57cec5SDimitry Andric         // Constrain strcmp's result range based on the result of StringRef's
20870b57cec5SDimitry Andric         // comparison methods.
20880b57cec5SDimitry Andric         BinaryOperatorKind op = (compareRes == 1) ? BO_GT : BO_LT;
20890b57cec5SDimitry Andric         SVal compareWithZero =
20900b57cec5SDimitry Andric           svalBuilder.evalBinOp(state, op, resultVal, zeroVal,
20910b57cec5SDimitry Andric               svalBuilder.getConditionType());
20920b57cec5SDimitry Andric         DefinedSVal compareWithZeroVal = compareWithZero.castAs<DefinedSVal>();
20930b57cec5SDimitry Andric         state = state->assume(compareWithZeroVal, true);
20940b57cec5SDimitry Andric       }
20950b57cec5SDimitry Andric     }
20960b57cec5SDimitry Andric   }
20970b57cec5SDimitry Andric 
20980b57cec5SDimitry Andric   state = state->BindExpr(CE, LCtx, resultVal);
20990b57cec5SDimitry Andric 
21000b57cec5SDimitry Andric   // Record this as a possible path.
21010b57cec5SDimitry Andric   C.addTransition(state);
21020b57cec5SDimitry Andric }
21030b57cec5SDimitry Andric 
21040b57cec5SDimitry Andric void CStringChecker::evalStrsep(CheckerContext &C, const CallExpr *CE) const {
21050b57cec5SDimitry Andric   // char *strsep(char **stringp, const char *delim);
21065e801ac6SDimitry Andric   // Verify whether the search string parameter matches the return type.
21075ffd83dbSDimitry Andric   SourceArgExpr SearchStrPtr = {CE->getArg(0), 0};
21085ffd83dbSDimitry Andric 
21095ffd83dbSDimitry Andric   QualType CharPtrTy = SearchStrPtr.Expression->getType()->getPointeeType();
21100b57cec5SDimitry Andric   if (CharPtrTy.isNull() ||
21110b57cec5SDimitry Andric       CE->getType().getUnqualifiedType() != CharPtrTy.getUnqualifiedType())
21120b57cec5SDimitry Andric     return;
21130b57cec5SDimitry Andric 
21140b57cec5SDimitry Andric   CurrentFunctionDescription = "strsep()";
21150b57cec5SDimitry Andric   ProgramStateRef State = C.getState();
21160b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
21170b57cec5SDimitry Andric 
21180b57cec5SDimitry Andric   // Check that the search string pointer is non-null (though it may point to
21190b57cec5SDimitry Andric   // a null string).
21205ffd83dbSDimitry Andric   SVal SearchStrVal = State->getSVal(SearchStrPtr.Expression, LCtx);
21215ffd83dbSDimitry Andric   State = checkNonNull(C, State, SearchStrPtr, SearchStrVal);
21220b57cec5SDimitry Andric   if (!State)
21230b57cec5SDimitry Andric     return;
21240b57cec5SDimitry Andric 
21250b57cec5SDimitry Andric   // Check that the delimiter string is non-null.
21265ffd83dbSDimitry Andric   AnyArgExpr DelimStr = {CE->getArg(1), 1};
21275ffd83dbSDimitry Andric   SVal DelimStrVal = State->getSVal(DelimStr.Expression, LCtx);
21285ffd83dbSDimitry Andric   State = checkNonNull(C, State, DelimStr, DelimStrVal);
21290b57cec5SDimitry Andric   if (!State)
21300b57cec5SDimitry Andric     return;
21310b57cec5SDimitry Andric 
21320b57cec5SDimitry Andric   SValBuilder &SVB = C.getSValBuilder();
21330b57cec5SDimitry Andric   SVal Result;
21340b57cec5SDimitry Andric   if (Optional<Loc> SearchStrLoc = SearchStrVal.getAs<Loc>()) {
21350b57cec5SDimitry Andric     // Get the current value of the search string pointer, as a char*.
21360b57cec5SDimitry Andric     Result = State->getSVal(*SearchStrLoc, CharPtrTy);
21370b57cec5SDimitry Andric 
21380b57cec5SDimitry Andric     // Invalidate the search string, representing the change of one delimiter
21390b57cec5SDimitry Andric     // character to NUL.
21405ffd83dbSDimitry Andric     State = InvalidateBuffer(C, State, SearchStrPtr.Expression, Result,
21410b57cec5SDimitry Andric                              /*IsSourceBuffer*/ false, nullptr);
21420b57cec5SDimitry Andric 
21430b57cec5SDimitry Andric     // Overwrite the search string pointer. The new value is either an address
21440b57cec5SDimitry Andric     // further along in the same string, or NULL if there are no more tokens.
21450b57cec5SDimitry Andric     State = State->bindLoc(*SearchStrLoc,
21460b57cec5SDimitry Andric         SVB.conjureSymbolVal(getTag(),
21470b57cec5SDimitry Andric           CE,
21480b57cec5SDimitry Andric           LCtx,
21490b57cec5SDimitry Andric           CharPtrTy,
21500b57cec5SDimitry Andric           C.blockCount()),
21510b57cec5SDimitry Andric         LCtx);
21520b57cec5SDimitry Andric   } else {
21530b57cec5SDimitry Andric     assert(SearchStrVal.isUnknown());
21540b57cec5SDimitry Andric     // Conjure a symbolic value. It's the best we can do.
21550b57cec5SDimitry Andric     Result = SVB.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
21560b57cec5SDimitry Andric   }
21570b57cec5SDimitry Andric 
21580b57cec5SDimitry Andric   // Set the return value, and finish.
21590b57cec5SDimitry Andric   State = State->BindExpr(CE, LCtx, Result);
21600b57cec5SDimitry Andric   C.addTransition(State);
21610b57cec5SDimitry Andric }
21620b57cec5SDimitry Andric 
21630b57cec5SDimitry Andric // These should probably be moved into a C++ standard library checker.
21640b57cec5SDimitry Andric void CStringChecker::evalStdCopy(CheckerContext &C, const CallExpr *CE) const {
21650b57cec5SDimitry Andric   evalStdCopyCommon(C, CE);
21660b57cec5SDimitry Andric }
21670b57cec5SDimitry Andric 
21680b57cec5SDimitry Andric void CStringChecker::evalStdCopyBackward(CheckerContext &C,
21690b57cec5SDimitry Andric     const CallExpr *CE) const {
21700b57cec5SDimitry Andric   evalStdCopyCommon(C, CE);
21710b57cec5SDimitry Andric }
21720b57cec5SDimitry Andric 
21730b57cec5SDimitry Andric void CStringChecker::evalStdCopyCommon(CheckerContext &C,
21740b57cec5SDimitry Andric     const CallExpr *CE) const {
21750b57cec5SDimitry Andric   if (!CE->getArg(2)->getType()->isPointerType())
21760b57cec5SDimitry Andric     return;
21770b57cec5SDimitry Andric 
21780b57cec5SDimitry Andric   ProgramStateRef State = C.getState();
21790b57cec5SDimitry Andric 
21800b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
21810b57cec5SDimitry Andric 
21820b57cec5SDimitry Andric   // template <class _InputIterator, class _OutputIterator>
21830b57cec5SDimitry Andric   // _OutputIterator
21840b57cec5SDimitry Andric   // copy(_InputIterator __first, _InputIterator __last,
21850b57cec5SDimitry Andric   //        _OutputIterator __result)
21860b57cec5SDimitry Andric 
21870b57cec5SDimitry Andric   // Invalidate the destination buffer
21880b57cec5SDimitry Andric   const Expr *Dst = CE->getArg(2);
21890b57cec5SDimitry Andric   SVal DstVal = State->getSVal(Dst, LCtx);
21900b57cec5SDimitry Andric   State = InvalidateBuffer(C, State, Dst, DstVal, /*IsSource=*/false,
21910b57cec5SDimitry Andric       /*Size=*/nullptr);
21920b57cec5SDimitry Andric 
21930b57cec5SDimitry Andric   SValBuilder &SVB = C.getSValBuilder();
21940b57cec5SDimitry Andric 
21950b57cec5SDimitry Andric   SVal ResultVal = SVB.conjureSymbolVal(nullptr, CE, LCtx, C.blockCount());
21960b57cec5SDimitry Andric   State = State->BindExpr(CE, LCtx, ResultVal);
21970b57cec5SDimitry Andric 
21980b57cec5SDimitry Andric   C.addTransition(State);
21990b57cec5SDimitry Andric }
22000b57cec5SDimitry Andric 
22010b57cec5SDimitry Andric void CStringChecker::evalMemset(CheckerContext &C, const CallExpr *CE) const {
22025ffd83dbSDimitry Andric   // void *memset(void *s, int c, size_t n);
22030b57cec5SDimitry Andric   CurrentFunctionDescription = "memory set function";
22040b57cec5SDimitry Andric 
22055ffd83dbSDimitry Andric   DestinationArgExpr Buffer = {CE->getArg(0), 0};
22065ffd83dbSDimitry Andric   AnyArgExpr CharE = {CE->getArg(1), 1};
22075ffd83dbSDimitry Andric   SizeArgExpr Size = {CE->getArg(2), 2};
22085ffd83dbSDimitry Andric 
22090b57cec5SDimitry Andric   ProgramStateRef State = C.getState();
22100b57cec5SDimitry Andric 
22110b57cec5SDimitry Andric   // See if the size argument is zero.
22120b57cec5SDimitry Andric   const LocationContext *LCtx = C.getLocationContext();
22135ffd83dbSDimitry Andric   SVal SizeVal = C.getSVal(Size.Expression);
22145ffd83dbSDimitry Andric   QualType SizeTy = Size.Expression->getType();
22150b57cec5SDimitry Andric 
22165ffd83dbSDimitry Andric   ProgramStateRef ZeroSize, NonZeroSize;
22175ffd83dbSDimitry Andric   std::tie(ZeroSize, NonZeroSize) = assumeZero(C, State, SizeVal, SizeTy);
22180b57cec5SDimitry Andric 
22190b57cec5SDimitry Andric   // Get the value of the memory area.
22205ffd83dbSDimitry Andric   SVal BufferPtrVal = C.getSVal(Buffer.Expression);
22210b57cec5SDimitry Andric 
22220b57cec5SDimitry Andric   // If the size is zero, there won't be any actual memory access, so
22235ffd83dbSDimitry Andric   // just bind the return value to the buffer and return.
22245ffd83dbSDimitry Andric   if (ZeroSize && !NonZeroSize) {
22255ffd83dbSDimitry Andric     ZeroSize = ZeroSize->BindExpr(CE, LCtx, BufferPtrVal);
22265ffd83dbSDimitry Andric     C.addTransition(ZeroSize);
22270b57cec5SDimitry Andric     return;
22280b57cec5SDimitry Andric   }
22290b57cec5SDimitry Andric 
22300b57cec5SDimitry Andric   // Ensure the memory area is not null.
22310b57cec5SDimitry Andric   // If it is NULL there will be a NULL pointer dereference.
22325ffd83dbSDimitry Andric   State = checkNonNull(C, NonZeroSize, Buffer, BufferPtrVal);
22330b57cec5SDimitry Andric   if (!State)
22340b57cec5SDimitry Andric     return;
22350b57cec5SDimitry Andric 
22365ffd83dbSDimitry Andric   State = CheckBufferAccess(C, State, Buffer, Size, AccessKind::write);
22370b57cec5SDimitry Andric   if (!State)
22380b57cec5SDimitry Andric     return;
22390b57cec5SDimitry Andric 
22400b57cec5SDimitry Andric   // According to the values of the arguments, bind the value of the second
22410b57cec5SDimitry Andric   // argument to the destination buffer and set string length, or just
22420b57cec5SDimitry Andric   // invalidate the destination buffer.
22435ffd83dbSDimitry Andric   if (!memsetAux(Buffer.Expression, C.getSVal(CharE.Expression),
22445ffd83dbSDimitry Andric                  Size.Expression, C, State))
22450b57cec5SDimitry Andric     return;
22460b57cec5SDimitry Andric 
22475ffd83dbSDimitry Andric   State = State->BindExpr(CE, LCtx, BufferPtrVal);
22480b57cec5SDimitry Andric   C.addTransition(State);
22490b57cec5SDimitry Andric }
22500b57cec5SDimitry Andric 
22510b57cec5SDimitry Andric void CStringChecker::evalBzero(CheckerContext &C, const CallExpr *CE) const {
22520b57cec5SDimitry Andric   CurrentFunctionDescription = "memory clearance function";
22530b57cec5SDimitry Andric 
22545ffd83dbSDimitry Andric   DestinationArgExpr Buffer = {CE->getArg(0), 0};
22555ffd83dbSDimitry Andric   SizeArgExpr Size = {CE->getArg(1), 1};
22560b57cec5SDimitry Andric   SVal Zero = C.getSValBuilder().makeZeroVal(C.getASTContext().IntTy);
22570b57cec5SDimitry Andric 
22580b57cec5SDimitry Andric   ProgramStateRef State = C.getState();
22590b57cec5SDimitry Andric 
22600b57cec5SDimitry Andric   // See if the size argument is zero.
22615ffd83dbSDimitry Andric   SVal SizeVal = C.getSVal(Size.Expression);
22625ffd83dbSDimitry Andric   QualType SizeTy = Size.Expression->getType();
22630b57cec5SDimitry Andric 
22640b57cec5SDimitry Andric   ProgramStateRef StateZeroSize, StateNonZeroSize;
22650b57cec5SDimitry Andric   std::tie(StateZeroSize, StateNonZeroSize) =
22660b57cec5SDimitry Andric     assumeZero(C, State, SizeVal, SizeTy);
22670b57cec5SDimitry Andric 
22680b57cec5SDimitry Andric   // If the size is zero, there won't be any actual memory access,
22690b57cec5SDimitry Andric   // In this case we just return.
22700b57cec5SDimitry Andric   if (StateZeroSize && !StateNonZeroSize) {
22710b57cec5SDimitry Andric     C.addTransition(StateZeroSize);
22720b57cec5SDimitry Andric     return;
22730b57cec5SDimitry Andric   }
22740b57cec5SDimitry Andric 
22750b57cec5SDimitry Andric   // Get the value of the memory area.
22765ffd83dbSDimitry Andric   SVal MemVal = C.getSVal(Buffer.Expression);
22770b57cec5SDimitry Andric 
22780b57cec5SDimitry Andric   // Ensure the memory area is not null.
22790b57cec5SDimitry Andric   // If it is NULL there will be a NULL pointer dereference.
22805ffd83dbSDimitry Andric   State = checkNonNull(C, StateNonZeroSize, Buffer, MemVal);
22810b57cec5SDimitry Andric   if (!State)
22820b57cec5SDimitry Andric     return;
22830b57cec5SDimitry Andric 
22845ffd83dbSDimitry Andric   State = CheckBufferAccess(C, State, Buffer, Size, AccessKind::write);
22850b57cec5SDimitry Andric   if (!State)
22860b57cec5SDimitry Andric     return;
22870b57cec5SDimitry Andric 
22885ffd83dbSDimitry Andric   if (!memsetAux(Buffer.Expression, Zero, Size.Expression, C, State))
22890b57cec5SDimitry Andric     return;
22900b57cec5SDimitry Andric 
22910b57cec5SDimitry Andric   C.addTransition(State);
22920b57cec5SDimitry Andric }
22930b57cec5SDimitry Andric 
22940b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
22950b57cec5SDimitry Andric // The driver method, and other Checker callbacks.
22960b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
22970b57cec5SDimitry Andric 
22980b57cec5SDimitry Andric CStringChecker::FnCheck CStringChecker::identifyCall(const CallEvent &Call,
22990b57cec5SDimitry Andric                                                      CheckerContext &C) const {
23000b57cec5SDimitry Andric   const auto *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr());
23010b57cec5SDimitry Andric   if (!CE)
23020b57cec5SDimitry Andric     return nullptr;
23030b57cec5SDimitry Andric 
23040b57cec5SDimitry Andric   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
23050b57cec5SDimitry Andric   if (!FD)
23060b57cec5SDimitry Andric     return nullptr;
23070b57cec5SDimitry Andric 
2308349cc55cSDimitry Andric   if (StdCopy.matches(Call))
23090b57cec5SDimitry Andric     return &CStringChecker::evalStdCopy;
2310349cc55cSDimitry Andric   if (StdCopyBackward.matches(Call))
23110b57cec5SDimitry Andric     return &CStringChecker::evalStdCopyBackward;
23120b57cec5SDimitry Andric 
23130b57cec5SDimitry Andric   // Pro-actively check that argument types are safe to do arithmetic upon.
23140b57cec5SDimitry Andric   // We do not want to crash if someone accidentally passes a structure
23150b57cec5SDimitry Andric   // into, say, a C++ overload of any of these functions. We could not check
23160b57cec5SDimitry Andric   // that for std::copy because they may have arguments of other types.
23170b57cec5SDimitry Andric   for (auto I : CE->arguments()) {
23180b57cec5SDimitry Andric     QualType T = I->getType();
23190b57cec5SDimitry Andric     if (!T->isIntegralOrEnumerationType() && !T->isPointerType())
23200b57cec5SDimitry Andric       return nullptr;
23210b57cec5SDimitry Andric   }
23220b57cec5SDimitry Andric 
23230b57cec5SDimitry Andric   const FnCheck *Callback = Callbacks.lookup(Call);
23240b57cec5SDimitry Andric   if (Callback)
23250b57cec5SDimitry Andric     return *Callback;
23260b57cec5SDimitry Andric 
23270b57cec5SDimitry Andric   return nullptr;
23280b57cec5SDimitry Andric }
23290b57cec5SDimitry Andric 
23300b57cec5SDimitry Andric bool CStringChecker::evalCall(const CallEvent &Call, CheckerContext &C) const {
23310b57cec5SDimitry Andric   FnCheck Callback = identifyCall(Call, C);
23320b57cec5SDimitry Andric 
23330b57cec5SDimitry Andric   // If the callee isn't a string function, let another checker handle it.
23340b57cec5SDimitry Andric   if (!Callback)
23350b57cec5SDimitry Andric     return false;
23360b57cec5SDimitry Andric 
23370b57cec5SDimitry Andric   // Check and evaluate the call.
23380b57cec5SDimitry Andric   const auto *CE = cast<CallExpr>(Call.getOriginExpr());
23390b57cec5SDimitry Andric   (this->*Callback)(C, CE);
23400b57cec5SDimitry Andric 
23410b57cec5SDimitry Andric   // If the evaluate call resulted in no change, chain to the next eval call
23420b57cec5SDimitry Andric   // handler.
23430b57cec5SDimitry Andric   // Note, the custom CString evaluation calls assume that basic safety
23440b57cec5SDimitry Andric   // properties are held. However, if the user chooses to turn off some of these
23450b57cec5SDimitry Andric   // checks, we ignore the issues and leave the call evaluation to a generic
23460b57cec5SDimitry Andric   // handler.
23470b57cec5SDimitry Andric   return C.isDifferent();
23480b57cec5SDimitry Andric }
23490b57cec5SDimitry Andric 
23500b57cec5SDimitry Andric void CStringChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const {
23510b57cec5SDimitry Andric   // Record string length for char a[] = "abc";
23520b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
23530b57cec5SDimitry Andric 
23540b57cec5SDimitry Andric   for (const auto *I : DS->decls()) {
23550b57cec5SDimitry Andric     const VarDecl *D = dyn_cast<VarDecl>(I);
23560b57cec5SDimitry Andric     if (!D)
23570b57cec5SDimitry Andric       continue;
23580b57cec5SDimitry Andric 
23590b57cec5SDimitry Andric     // FIXME: Handle array fields of structs.
23600b57cec5SDimitry Andric     if (!D->getType()->isArrayType())
23610b57cec5SDimitry Andric       continue;
23620b57cec5SDimitry Andric 
23630b57cec5SDimitry Andric     const Expr *Init = D->getInit();
23640b57cec5SDimitry Andric     if (!Init)
23650b57cec5SDimitry Andric       continue;
23660b57cec5SDimitry Andric     if (!isa<StringLiteral>(Init))
23670b57cec5SDimitry Andric       continue;
23680b57cec5SDimitry Andric 
23690b57cec5SDimitry Andric     Loc VarLoc = state->getLValue(D, C.getLocationContext());
23700b57cec5SDimitry Andric     const MemRegion *MR = VarLoc.getAsRegion();
23710b57cec5SDimitry Andric     if (!MR)
23720b57cec5SDimitry Andric       continue;
23730b57cec5SDimitry Andric 
23740b57cec5SDimitry Andric     SVal StrVal = C.getSVal(Init);
23750b57cec5SDimitry Andric     assert(StrVal.isValid() && "Initializer string is unknown or undefined");
23760b57cec5SDimitry Andric     DefinedOrUnknownSVal strLength =
23770b57cec5SDimitry Andric       getCStringLength(C, state, Init, StrVal).castAs<DefinedOrUnknownSVal>();
23780b57cec5SDimitry Andric 
23790b57cec5SDimitry Andric     state = state->set<CStringLength>(MR, strLength);
23800b57cec5SDimitry Andric   }
23810b57cec5SDimitry Andric 
23820b57cec5SDimitry Andric   C.addTransition(state);
23830b57cec5SDimitry Andric }
23840b57cec5SDimitry Andric 
23850b57cec5SDimitry Andric ProgramStateRef
23860b57cec5SDimitry Andric CStringChecker::checkRegionChanges(ProgramStateRef state,
23870b57cec5SDimitry Andric     const InvalidatedSymbols *,
23880b57cec5SDimitry Andric     ArrayRef<const MemRegion *> ExplicitRegions,
23890b57cec5SDimitry Andric     ArrayRef<const MemRegion *> Regions,
23900b57cec5SDimitry Andric     const LocationContext *LCtx,
23910b57cec5SDimitry Andric     const CallEvent *Call) const {
23920b57cec5SDimitry Andric   CStringLengthTy Entries = state->get<CStringLength>();
23930b57cec5SDimitry Andric   if (Entries.isEmpty())
23940b57cec5SDimitry Andric     return state;
23950b57cec5SDimitry Andric 
23960b57cec5SDimitry Andric   llvm::SmallPtrSet<const MemRegion *, 8> Invalidated;
23970b57cec5SDimitry Andric   llvm::SmallPtrSet<const MemRegion *, 32> SuperRegions;
23980b57cec5SDimitry Andric 
23990b57cec5SDimitry Andric   // First build sets for the changed regions and their super-regions.
24000b57cec5SDimitry Andric   for (ArrayRef<const MemRegion *>::iterator
24010b57cec5SDimitry Andric       I = Regions.begin(), E = Regions.end(); I != E; ++I) {
24020b57cec5SDimitry Andric     const MemRegion *MR = *I;
24030b57cec5SDimitry Andric     Invalidated.insert(MR);
24040b57cec5SDimitry Andric 
24050b57cec5SDimitry Andric     SuperRegions.insert(MR);
24060b57cec5SDimitry Andric     while (const SubRegion *SR = dyn_cast<SubRegion>(MR)) {
24070b57cec5SDimitry Andric       MR = SR->getSuperRegion();
24080b57cec5SDimitry Andric       SuperRegions.insert(MR);
24090b57cec5SDimitry Andric     }
24100b57cec5SDimitry Andric   }
24110b57cec5SDimitry Andric 
24120b57cec5SDimitry Andric   CStringLengthTy::Factory &F = state->get_context<CStringLength>();
24130b57cec5SDimitry Andric 
24140b57cec5SDimitry Andric   // Then loop over the entries in the current state.
24150b57cec5SDimitry Andric   for (CStringLengthTy::iterator I = Entries.begin(),
24160b57cec5SDimitry Andric       E = Entries.end(); I != E; ++I) {
24170b57cec5SDimitry Andric     const MemRegion *MR = I.getKey();
24180b57cec5SDimitry Andric 
24190b57cec5SDimitry Andric     // Is this entry for a super-region of a changed region?
24200b57cec5SDimitry Andric     if (SuperRegions.count(MR)) {
24210b57cec5SDimitry Andric       Entries = F.remove(Entries, MR);
24220b57cec5SDimitry Andric       continue;
24230b57cec5SDimitry Andric     }
24240b57cec5SDimitry Andric 
24250b57cec5SDimitry Andric     // Is this entry for a sub-region of a changed region?
24260b57cec5SDimitry Andric     const MemRegion *Super = MR;
24270b57cec5SDimitry Andric     while (const SubRegion *SR = dyn_cast<SubRegion>(Super)) {
24280b57cec5SDimitry Andric       Super = SR->getSuperRegion();
24290b57cec5SDimitry Andric       if (Invalidated.count(Super)) {
24300b57cec5SDimitry Andric         Entries = F.remove(Entries, MR);
24310b57cec5SDimitry Andric         break;
24320b57cec5SDimitry Andric       }
24330b57cec5SDimitry Andric     }
24340b57cec5SDimitry Andric   }
24350b57cec5SDimitry Andric 
24360b57cec5SDimitry Andric   return state->set<CStringLength>(Entries);
24370b57cec5SDimitry Andric }
24380b57cec5SDimitry Andric 
24390b57cec5SDimitry Andric void CStringChecker::checkLiveSymbols(ProgramStateRef state,
24400b57cec5SDimitry Andric     SymbolReaper &SR) const {
24410b57cec5SDimitry Andric   // Mark all symbols in our string length map as valid.
24420b57cec5SDimitry Andric   CStringLengthTy Entries = state->get<CStringLength>();
24430b57cec5SDimitry Andric 
24440b57cec5SDimitry Andric   for (CStringLengthTy::iterator I = Entries.begin(), E = Entries.end();
24450b57cec5SDimitry Andric       I != E; ++I) {
24460b57cec5SDimitry Andric     SVal Len = I.getData();
24470b57cec5SDimitry Andric 
24480b57cec5SDimitry Andric     for (SymExpr::symbol_iterator si = Len.symbol_begin(),
24490b57cec5SDimitry Andric         se = Len.symbol_end(); si != se; ++si)
24500b57cec5SDimitry Andric       SR.markInUse(*si);
24510b57cec5SDimitry Andric   }
24520b57cec5SDimitry Andric }
24530b57cec5SDimitry Andric 
24540b57cec5SDimitry Andric void CStringChecker::checkDeadSymbols(SymbolReaper &SR,
24550b57cec5SDimitry Andric     CheckerContext &C) const {
24560b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
24570b57cec5SDimitry Andric   CStringLengthTy Entries = state->get<CStringLength>();
24580b57cec5SDimitry Andric   if (Entries.isEmpty())
24590b57cec5SDimitry Andric     return;
24600b57cec5SDimitry Andric 
24610b57cec5SDimitry Andric   CStringLengthTy::Factory &F = state->get_context<CStringLength>();
24620b57cec5SDimitry Andric   for (CStringLengthTy::iterator I = Entries.begin(), E = Entries.end();
24630b57cec5SDimitry Andric       I != E; ++I) {
24640b57cec5SDimitry Andric     SVal Len = I.getData();
24650b57cec5SDimitry Andric     if (SymbolRef Sym = Len.getAsSymbol()) {
24660b57cec5SDimitry Andric       if (SR.isDead(Sym))
24670b57cec5SDimitry Andric         Entries = F.remove(Entries, I.getKey());
24680b57cec5SDimitry Andric     }
24690b57cec5SDimitry Andric   }
24700b57cec5SDimitry Andric 
24710b57cec5SDimitry Andric   state = state->set<CStringLength>(Entries);
24720b57cec5SDimitry Andric   C.addTransition(state);
24730b57cec5SDimitry Andric }
24740b57cec5SDimitry Andric 
24750b57cec5SDimitry Andric void ento::registerCStringModeling(CheckerManager &Mgr) {
24760b57cec5SDimitry Andric   Mgr.registerChecker<CStringChecker>();
24770b57cec5SDimitry Andric }
24780b57cec5SDimitry Andric 
24795ffd83dbSDimitry Andric bool ento::shouldRegisterCStringModeling(const CheckerManager &mgr) {
24800b57cec5SDimitry Andric   return true;
24810b57cec5SDimitry Andric }
24820b57cec5SDimitry Andric 
24830b57cec5SDimitry Andric #define REGISTER_CHECKER(name)                                                 \
24840b57cec5SDimitry Andric   void ento::register##name(CheckerManager &mgr) {                             \
24850b57cec5SDimitry Andric     CStringChecker *checker = mgr.getChecker<CStringChecker>();                \
24860b57cec5SDimitry Andric     checker->Filter.Check##name = true;                                        \
2487a7dea167SDimitry Andric     checker->Filter.CheckName##name = mgr.getCurrentCheckerName();             \
24880b57cec5SDimitry Andric   }                                                                            \
24890b57cec5SDimitry Andric                                                                                \
24905ffd83dbSDimitry Andric   bool ento::shouldRegister##name(const CheckerManager &mgr) { return true; }
24910b57cec5SDimitry Andric 
24920b57cec5SDimitry Andric REGISTER_CHECKER(CStringNullArg)
24930b57cec5SDimitry Andric REGISTER_CHECKER(CStringOutOfBounds)
24940b57cec5SDimitry Andric REGISTER_CHECKER(CStringBufferOverlap)
24950b57cec5SDimitry Andric REGISTER_CHECKER(CStringNotNullTerm)
249681ad6265SDimitry Andric REGISTER_CHECKER(CStringUninitializedRead)
2497